(fin: boolean, opcode: number, payload: Buffer)
| 177 | } |
| 178 | |
| 179 | private handleFrame(fin: boolean, opcode: number, payload: Buffer): void { |
| 180 | // Control frames (opcode >= 0x8) must not be fragmented |
| 181 | if (opcode === OP_PING) { |
| 182 | this.writeFrame(OP_PONG, payload); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | if (opcode === OP_PONG) { |
| 187 | // Ignore unsolicited pongs |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | if (opcode === OP_CLOSE) { |
| 192 | const code = payload.length >= 2 ? payload.readUInt16BE(0) : 1005; |
| 193 | const reason = payload.length > 2 ? payload.subarray(2).toString("utf-8") : ""; |
| 194 | |
| 195 | if (!this.closed) { |
| 196 | this.closed = true; |
| 197 | // Echo close frame back |
| 198 | this.writeFrame(OP_CLOSE, payload); |
| 199 | this.socket.end(); |
| 200 | this.emit("close", code, reason); |
| 201 | } |
| 202 | // If already closed (server-initiated or duplicate), ignore — the |
| 203 | // close event was already emitted by close() or the first OP_CLOSE. |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | // Text or continuation frames |
| 208 | if (opcode === OP_TEXT || opcode === OP_CONTINUATION) { |
| 209 | this.fragments.push(payload); |
| 210 | |
| 211 | if (fin) { |
| 212 | const message = Buffer.concat(this.fragments).toString("utf-8"); |
| 213 | this.fragments = []; |
| 214 | this.emit("message", message); |
| 215 | } |
| 216 | // If !fin, wait for more continuation frames |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | // Binary or unknown — just ignore for a mock server |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | export function computeAcceptKey(wsKey: string): string { |
no test coverage detected