(opcode, payload)
| 13 | } |
| 14 | |
| 15 | function encodeFrame(opcode, payload) { |
| 16 | const fin = 0x80; |
| 17 | const len = payload.length; |
| 18 | let header; |
| 19 | |
| 20 | if (len < 126) { |
| 21 | header = Buffer.alloc(2); |
| 22 | header[0] = fin | opcode; |
| 23 | header[1] = len; |
| 24 | } else if (len < 65536) { |
| 25 | header = Buffer.alloc(4); |
| 26 | header[0] = fin | opcode; |
| 27 | header[1] = 126; |
| 28 | header.writeUInt16BE(len, 2); |
| 29 | } else { |
| 30 | header = Buffer.alloc(10); |
| 31 | header[0] = fin | opcode; |
| 32 | header[1] = 127; |
| 33 | header.writeBigUInt64BE(BigInt(len), 2); |
| 34 | } |
| 35 | |
| 36 | return Buffer.concat([header, payload]); |
| 37 | } |
| 38 | |
| 39 | function decodeFrame(buffer) { |
| 40 | if (buffer.length < 2) return null; |
no outgoing calls
no test coverage detected