MCPcopy
hub / github.com/nodejs/undici / createFastTextFrame

Method createFastTextFrame

lib/web/websocket/frame.js:84–121  ·  view source on GitHub ↗

* @param {Uint8Array} buffer

(buffer)

Source from the content-addressed store, hash-verified

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
124module.exports = {

Callers 1

addMethod · 0.80

Calls 1

generateMaskFunction · 0.85

Tested by

no test coverage detected