MCPcopy Index your code
hub / github.com/nodejs/node / formatWSFrame

Function formatWSFrame

test/common/inspector-helper.js:92–120  ·  view source on GitHub ↗
(message)

Source from the content-addressed store, hash-verified

90}
91
92function formatWSFrame(message) {
93 const messageBuf = Buffer.from(JSON.stringify(message));
94
95 const wsHeaderBuf = Buffer.allocUnsafe(16);
96 wsHeaderBuf.writeUInt8(0x81, 0);
97 let byte2 = 0x80;
98 const bodyLen = messageBuf.length;
99
100 let maskOffset = 2;
101 if (bodyLen < 126) {
102 byte2 = 0x80 + bodyLen;
103 } else if (bodyLen < 65536) {
104 byte2 = 0xFE;
105 wsHeaderBuf.writeUInt16BE(bodyLen, 2);
106 maskOffset = 4;
107 } else {
108 byte2 = 0xFF;
109 wsHeaderBuf.writeUInt32BE(bodyLen, 2);
110 wsHeaderBuf.writeUInt32BE(0, 6);
111 maskOffset = 10;
112 }
113 wsHeaderBuf.writeUInt8(byte2, 1);
114 wsHeaderBuf.writeUInt32BE(0x01020408, maskOffset);
115
116 for (let i = 0; i < messageBuf.length; i++)
117 messageBuf[i] = messageBuf[i] ^ (1 << (i % 4));
118
119 return Buffer.concat([wsHeaderBuf.slice(0, maskOffset + 4), messageBuf]);
120}
121
122class InspectorSession {
123 constructor(socket, instance) {

Callers 1

_sendMessageMethod · 0.85

Calls 3

concatMethod · 0.80
sliceMethod · 0.65
fromMethod · 0.45

Tested by

no test coverage detected