| 58 | |
| 59 | // https://websockets.spec.whatwg.org/#interface-definition |
| 60 | class WebSocket extends EventTarget { |
| 61 | #events = { |
| 62 | open: null, |
| 63 | error: null, |
| 64 | close: null, |
| 65 | message: null |
| 66 | } |
| 67 | |
| 68 | #bufferedAmount = 0 |
| 69 | #protocol = '' |
| 70 | #extensions = '' |
| 71 | |
| 72 | /** @type {SendQueue} */ |
| 73 | #sendQueue |
| 74 | |
| 75 | /** @type {Handler} */ |
| 76 | #handler = { |
| 77 | onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions), |
| 78 | onMessage: (opcode, data) => this.#onMessage(opcode, data), |
| 79 | onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message), |
| 80 | onParserDrain: () => this.#onParserDrain(), |
| 81 | onSocketData: (chunk) => { |
| 82 | if (!this.#parser.write(chunk)) { |
| 83 | this.#handler.socket.pause() |
| 84 | } |
| 85 | }, |
| 86 | onSocketError: (err) => { |
| 87 | this.#handler.readyState = states.CLOSING |
| 88 | |
| 89 | if (channels.socketError.hasSubscribers) { |
| 90 | channels.socketError.publish(err) |
| 91 | } |
| 92 | |
| 93 | this.#handler.socket.destroy() |
| 94 | }, |
| 95 | onSocketClose: () => this.#onSocketClose(), |
| 96 | onPing: (body) => { |
| 97 | if (channels.ping.hasSubscribers) { |
| 98 | channels.ping.publish({ |
| 99 | payload: body, |
| 100 | websocket: this |
| 101 | }) |
| 102 | } |
| 103 | }, |
| 104 | onPong: (body) => { |
| 105 | if (channels.pong.hasSubscribers) { |
| 106 | channels.pong.publish({ |
| 107 | payload: body, |
| 108 | websocket: this |
| 109 | }) |
| 110 | } |
| 111 | }, |
| 112 | |
| 113 | readyState: states.CONNECTING, |
| 114 | socket: null, |
| 115 | closeState: new Set(), |
| 116 | controller: null, |
| 117 | wasEverConnected: false |
nothing calls this directly
no test coverage detected