| 108 | }; |
| 109 | |
| 110 | class WebSocketMock extends EventTarget { |
| 111 | static readonly CONNECTING: 0 = 0; // WebSocket.CONNECTING |
| 112 | static readonly OPEN: 1 = 1; // WebSocket.OPEN |
| 113 | static readonly CLOSING: 2 = 2; // WebSocket.CLOSING |
| 114 | static readonly CLOSED: 3 = 3; // WebSocket.CLOSED |
| 115 | |
| 116 | CONNECTING: 0 = 0; // WebSocket.CONNECTING |
| 117 | OPEN: 1 = 1; // WebSocket.OPEN |
| 118 | CLOSING: 2 = 2; // WebSocket.CLOSING |
| 119 | CLOSED: 3 = 3; // WebSocket.CLOSED |
| 120 | |
| 121 | private _oncloseListener: WebSocket['onclose'] = null; |
| 122 | private _onerrorListener: WebSocket['onerror'] = null; |
| 123 | private _onmessageListener: WebSocket['onmessage'] = null; |
| 124 | private _onopenListener: WebSocket['onopen'] = null; |
| 125 | |
| 126 | bufferedAmount: number = 0; |
| 127 | extensions: string = ''; |
| 128 | protocol: string = ''; |
| 129 | readyState: number = 0; |
| 130 | readonly url: string; |
| 131 | |
| 132 | private _id: string; |
| 133 | private _origin: string = ''; |
| 134 | private _protocols?: string | string[]; |
| 135 | private _ws?: WebSocket; |
| 136 | private _passthrough = false; |
| 137 | private _wsBufferedMessages: WebSocketMessage[] = []; |
| 138 | private _binaryType: BinaryType = 'blob'; |
| 139 | |
| 140 | constructor(url: string | URL, protocols?: string | string[]) { |
| 141 | super(); |
| 142 | |
| 143 | // https://github.com/whatwg/websockets/issues/20 |
| 144 | this.url = new URL(url, globalThis.window.document.baseURI).href.replace(/^http/, 'ws'); |
| 145 | this._origin = URL.parse(this.url)?.origin ?? ''; |
| 146 | this._protocols = protocols; |
| 147 | |
| 148 | this._id = generateId(); |
| 149 | idToWebSocket.set(this._id, this); |
| 150 | const protocolsList = Array.isArray(protocols) ? [...protocols] : (protocols ? [protocols] : []); |
| 151 | binding({ type: 'onCreate', id: this._id, url: this.url, protocols: protocolsList }); |
| 152 | } |
| 153 | |
| 154 | // --- native WebSocket implementation --- |
| 155 | |
| 156 | get binaryType() { |
| 157 | return this._binaryType; |
| 158 | } |
| 159 | |
| 160 | set binaryType(type) { |
| 161 | this._binaryType = type; |
| 162 | if (this._ws) |
| 163 | this._ws.binaryType = type; |
| 164 | } |
| 165 | |
| 166 | get onclose() { |
| 167 | return this._oncloseListener; |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…