* @param {Uint8Array} buffer
(buffer)
| 82 | * @param {Uint8Array} buffer |
| 83 | */ |
| 84 | static createFastTextFrame (buffer) { |
| 85 | const maskKey = generateMask() |
| 86 | |
| 87 | const bodyLength = buffer.length |
| 88 | |
| 89 | // mask body |
| 90 | for (let i = 0; i < bodyLength; ++i) { |
| 91 | buffer[i] ^= maskKey[i & 3] |
| 92 | } |
| 93 | |
| 94 | let payloadLength = bodyLength |
| 95 | let offset = 6 |
| 96 | |
| 97 | if (bodyLength > maxUnsigned16Bit) { |
| 98 | offset += 8 // payload length is next 8 bytes |
| 99 | payloadLength = 127 |
| 100 | } else if (bodyLength > 125) { |
| 101 | offset += 2 // payload length is next 2 bytes |
| 102 | payloadLength = 126 |
| 103 | } |
| 104 | const head = Buffer.allocUnsafeSlow(offset) |
| 105 | |
| 106 | head[0] = 0x80 /* FIN */ | opcodes.TEXT /* opcode TEXT */ |
| 107 | head[1] = payloadLength | 0x80 /* MASK */ |
| 108 | head[offset - 4] = maskKey[0] |
| 109 | head[offset - 3] = maskKey[1] |
| 110 | head[offset - 2] = maskKey[2] |
| 111 | head[offset - 1] = maskKey[3] |
| 112 | |
| 113 | if (payloadLength === 126) { |
| 114 | head.writeUInt16BE(bodyLength, 2) |
| 115 | } else if (payloadLength === 127) { |
| 116 | head[2] = head[3] = 0 |
| 117 | head.writeUIntBE(bodyLength, 4, 6) |
| 118 | } |
| 119 | |
| 120 | return [head, buffer] |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | module.exports = { |