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

Function closeWebSocketConnection

deps/undici/undici.js:15248–15280  ·  view source on GitHub ↗
(object, code, reason, validate = false)

Source from the content-addressed store, hash-verified

15246 }
15247 __name(establishWebSocketConnection, "establishWebSocketConnection");
15248 function closeWebSocketConnection(object, code, reason, validate = false) {
15249 code ??= null;
15250 reason ??= "";
15251 if (validate) validateCloseCodeAndReason(code, reason);
15252 if (isClosed(object.readyState) || isClosing(object.readyState)) {
15253 } else if (!isEstablished(object.readyState)) {
15254 failWebsocketConnection(object);
15255 object.readyState = states.CLOSING;
15256 } else if (!object.closeState.has(sentCloseFrameState.SENT) && !object.closeState.has(sentCloseFrameState.RECEIVED)) {
15257 const frame = new WebsocketFrameSend();
15258 if (reason.length !== 0 && code === null) {
15259 code = 1e3;
15260 }
15261 assert(code === null || Number.isInteger(code));
15262 if (code === null && reason.length === 0) {
15263 frame.frameData = emptyBuffer;
15264 } else if (code !== null && reason === null) {
15265 frame.frameData = Buffer.allocUnsafe(2);
15266 frame.frameData.writeUInt16BE(code, 0);
15267 } else if (code !== null && reason !== null) {
15268 frame.frameData = Buffer.allocUnsafe(2 + Buffer.byteLength(reason));
15269 frame.frameData.writeUInt16BE(code, 0);
15270 frame.frameData.write(reason, 2, "utf-8");
15271 } else {
15272 frame.frameData = emptyBuffer;
15273 }
15274 object.socket.write(frame.createFrame(opcodes.CLOSE));
15275 object.closeState.add(sentCloseFrameState.SENT);
15276 object.readyState = states.CLOSING;
15277 } else {
15278 object.readyState = states.CLOSING;
15279 }
15280 }
15281 __name(closeWebSocketConnection, "closeWebSocketConnection");
15282 function failWebsocketConnection(handler, code, reason, cause) {
15283 if (isEstablished(handler.readyState)) {

Callers 2

failWebsocketConnectionFunction · 0.70
closeMethod · 0.70

Calls 10

createFrameMethod · 0.95
isClosedFunction · 0.70
isClosingFunction · 0.70
isEstablishedFunction · 0.70
failWebsocketConnectionFunction · 0.70
hasMethod · 0.65
addMethod · 0.65
assertFunction · 0.50
writeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…