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

Method parseCloseBody

lib/web/websocket/receiver.js:394–428  ·  view source on GitHub ↗
(data)

Source from the content-addressed store, hash-verified

392 }
393
394 parseCloseBody (data) {
395 assert(data.length !== 1)
396
397 // https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.5
398 /** @type {number|undefined} */
399 let code
400
401 if (data.length >= 2) {
402 // _The WebSocket Connection Close Code_ is
403 // defined as the status code (Section 7.4) contained in the first Close
404 // control frame received by the application
405 code = data.readUInt16BE(0)
406 }
407
408 if (code !== undefined && !isValidStatusCode(code)) {
409 return { code: 1002, reason: 'Invalid status code', error: true }
410 }
411
412 // https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.6
413 /** @type {Buffer} */
414 let reason = data.subarray(2)
415
416 // Remove BOM
417 if (reason[0] === 0xEF && reason[1] === 0xBB && reason[2] === 0xBF) {
418 reason = reason.subarray(3)
419 }
420
421 try {
422 reason = utf8Decode(reason)
423 } catch {
424 return { code: 1007, reason: 'Invalid UTF-8', error: true }
425 }
426
427 return { code, reason, error: false }
428 }
429
430 /**
431 * Parses control frames.

Callers 1

parseControlFrameMethod · 0.95

Calls 1

isValidStatusCodeFunction · 0.85

Tested by

no test coverage detected