| 36 | function noop() {} |
| 37 | |
| 38 | class Socket extends EventEmitter { |
| 39 | id; |
| 40 | connected = false; |
| 41 | |
| 42 | #uri; |
| 43 | #opts; |
| 44 | #ws; |
| 45 | #pingTimeoutTimer; |
| 46 | #pingTimeoutDelay; |
| 47 | #sendBuffer = []; |
| 48 | #reconnectTimer; |
| 49 | #shouldReconnect = true; |
| 50 | |
| 51 | constructor(uri, opts) { |
| 52 | super(); |
| 53 | this.#uri = uri; |
| 54 | this.#opts = Object.assign( |
| 55 | { |
| 56 | path: "/socket.io/", |
| 57 | reconnectionDelay: 2000, |
| 58 | }, |
| 59 | opts |
| 60 | ); |
| 61 | this.#open(); |
| 62 | } |
| 63 | |
| 64 | #open() { |
| 65 | this.#ws = new WebSocket(this.#createUrl()); |
| 66 | this.#ws.onmessage = ({ data }) => this.#onMessage(data); |
| 67 | // dummy handler for Node.js |
| 68 | this.#ws.onerror = noop; |
| 69 | this.#ws.onclose = () => this.#onClose("transport close"); |
| 70 | } |
| 71 | |
| 72 | #createUrl() { |
| 73 | const uri = this.#uri.replace(/^http/, "ws"); |
| 74 | const queryParams = "?EIO=4&transport=websocket"; |
| 75 | return `${uri}${this.#opts.path}${queryParams}`; |
| 76 | } |
| 77 | |
| 78 | #onMessage(data) { |
| 79 | if (typeof data !== "string") { |
| 80 | // TODO handle binary payloads |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | switch (data[0]) { |
| 85 | case EIOPacketType.OPEN: |
| 86 | this.#onOpen(data); |
| 87 | break; |
| 88 | |
| 89 | case EIOPacketType.CLOSE: |
| 90 | this.#onClose("transport close"); |
| 91 | break; |
| 92 | |
| 93 | case EIOPacketType.PING: |
| 94 | this.#resetPingTimeout(); |
| 95 | this.#send(EIOPacketType.PONG); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…