* @see https://websockets.spec.whatwg.org/#dom-websocket-close * @param {number|undefined} code * @param {string|undefined} reason
(code = void 0, reason = void 0)
| 63668 | this[kReadyState] = _WebSocket.CONNECTING; |
| 63669 | this[kBinaryType] = "blob"; |
| 63670 | } |
| 63671 | /** |
| 63672 | * @see https://websockets.spec.whatwg.org/#dom-websocket-close |
| 63673 | * @param {number|undefined} code |
| 63674 | * @param {string|undefined} reason |
| 63675 | */ |
| 63676 | close(code = void 0, reason = void 0) { |
| 63677 | webidl.brandCheck(this, _WebSocket); |
| 63678 | if (code !== void 0) { |
| 63679 | code = webidl.converters["unsigned short"](code, { clamp: true }); |
| 63680 | } |
| 63681 | if (reason !== void 0) { |
| 63682 | reason = webidl.converters.USVString(reason); |
| 63683 | } |
| 63684 | if (code !== void 0) { |
| 63685 | if (code !== 1e3 && (code < 3e3 || code > 4999)) { |
| 63686 | throw new DOMException3("invalid code", "InvalidAccessError"); |
| 63687 | } |
| 63688 | } |
| 63689 | let reasonByteLength = 0; |
| 63690 | if (reason !== void 0) { |
| 63691 | reasonByteLength = Buffer.byteLength(reason); |
| 63692 | if (reasonByteLength > 123) { |
| 63693 | throw new DOMException3( |
| 63694 | `Reason must be less than 123 bytes; received ${reasonByteLength}`, |
| 63695 | "SyntaxError" |
| 63696 | ); |
| 63697 | } |
| 63698 | } |
| 63699 | if (this[kReadyState] === _WebSocket.CLOSING || this[kReadyState] === _WebSocket.CLOSED) { |
| 63700 | } else if (!isEstablished(this)) { |
| 63701 | failWebsocketConnection(this, "Connection was closed before it was established."); |
| 63702 | this[kReadyState] = _WebSocket.CLOSING; |
| 63703 | } else if (!isClosing(this)) { |
| 63704 | const frame = new WebsocketFrameSend(); |
| 63705 | if (code !== void 0 && reason === void 0) { |
| 63706 | frame.frameData = Buffer.allocUnsafe(2); |
| 63707 | frame.frameData.writeUInt16BE(code, 0); |
| 63708 | } else if (code !== void 0 && reason !== void 0) { |
| 63709 | frame.frameData = Buffer.allocUnsafe(2 + reasonByteLength); |
| 63710 | frame.frameData.writeUInt16BE(code, 0); |
| 63711 | frame.frameData.write(reason, 2, "utf-8"); |
| 63712 | } else { |
| 63713 | frame.frameData = emptyBuffer; |
| 63714 | } |
| 63715 | const socket = this[kResponse].socket; |
| 63716 | socket.write(frame.createFrame(opcodes.CLOSE), (err) => { |
| 63717 | if (!err) { |
| 63718 | this[kSentClose] = true; |
| 63719 | } |
| 63720 | }); |
| 63721 | this[kReadyState] = states.CLOSING; |
| 63722 | } else { |
nothing calls this directly
no test coverage detected