MCPcopy Create free account
hub / github.com/nodejs/node / validateCloseCodeAndReason

Function validateCloseCodeAndReason

deps/undici/src/lib/web/websocket/util.js:288–309  ·  view source on GitHub ↗
(code, reason)

Source from the content-addressed store, hash-verified

286
287// https://whatpr.org/websockets/48.html#validate-close-code-and-reason
288function validateCloseCodeAndReason (code, reason) {
289 // 1. If code is not null, but is neither an integer equal to
290 // 1000 nor an integer in the range 3000 to 4999, inclusive,
291 // throw an "InvalidAccessError" DOMException.
292 if (code !== null) {
293 if (code !== 1000 && (code < 3000 || code > 4999)) {
294 throw new DOMException('invalid code', 'InvalidAccessError')
295 }
296 }
297
298 // 2. If reason is not null, then:
299 if (reason !== null) {
300 // 2.1. Let reasonBytes be the result of UTF-8 encoding reason.
301 // 2.2. If reasonBytes is longer than 123 bytes, then throw a
302 // "SyntaxError" DOMException.
303 const reasonBytesLength = Buffer.byteLength(reason)
304
305 if (reasonBytesLength > 123) {
306 throw new DOMException(`Reason must be less than 123 bytes; received ${reasonBytesLength}`, 'SyntaxError')
307 }
308 }
309}
310
311/**
312 * Converts a Buffer to utf-8, even on platforms without icu.

Callers 2

closeWebSocketConnectionFunction · 0.70
constructorMethod · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected