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

Function parseWSFrame

test/common/inspector-helper.js:56–90  ·  view source on GitHub ↗
(buffer)

Source from the content-addressed store, hash-verified

54}
55
56function parseWSFrame(buffer) {
57 // Protocol described in https://tools.ietf.org/html/rfc6455#section-5
58 let message = null;
59 if (buffer.length < 2)
60 return { length: 0, message };
61 if (buffer[0] === 0x88 && buffer[1] === 0x00) {
62 return { length: 2, message, closed: true };
63 }
64 assert.strictEqual(buffer[0], 0x81);
65 let dataLen = 0x7F & buffer[1];
66 let bodyOffset = 2;
67 if (buffer.length < bodyOffset + dataLen)
68 return 0;
69 if (dataLen === 126) {
70 dataLen = buffer.readUInt16BE(2);
71 bodyOffset = 4;
72 } else if (dataLen === 127) {
73 assert(buffer[2] === 0 && buffer[3] === 0, 'Inspector message too big');
74 dataLen = buffer.readUIntBE(4, 6);
75 bodyOffset = 10;
76 }
77 if (buffer.length < bodyOffset + dataLen)
78 return { length: 0, message };
79 const jsonPayload =
80 buffer.slice(bodyOffset, bodyOffset + dataLen).toString('utf8');
81 try {
82 message = JSON.parse(jsonPayload);
83 } catch (e) {
84 console.error(`JSON.parse() failed for: ${jsonPayload}`);
85 throw e;
86 }
87 if (DEBUG)
88 console.log('[received]', JSON.stringify(message));
89 return { length: bodyOffset + dataLen, message };
90}
91
92function formatWSFrame(message) {
93 const messageBuf = Buffer.from(JSON.stringify(message));

Callers 1

constructorMethod · 0.85

Calls 6

sliceMethod · 0.65
parseMethod · 0.65
assertFunction · 0.50
toStringMethod · 0.45
errorMethod · 0.45
logMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…