| 'use strict' |
|
|
| const { webidl } = require('../fetch/webidl') |
| const { DOMException } = require('../fetch/constants') |
| const { URLSerializer } = require('../fetch/dataURL') |
| const { getGlobalOrigin } = require('../fetch/global') |
| const { staticPropertyDescriptors, states, opcodes, emptyBuffer } = require('./constants') |
| const { |
| kWebSocketURL, |
| kReadyState, |
| kController, |
| kBinaryType, |
| kResponse, |
| kSentClose, |
| kByteParser |
| } = require('./symbols') |
| const { isEstablished, isClosing, isValidSubprotocol, failWebsocketConnection, fireEvent } = require('./util') |
| const { establishWebSocketConnection } = require('./connection') |
| const { WebsocketFrameSend } = require('./frame') |
| const { ByteParser } = require('./receiver') |
| const { kEnumerableProperty, isBlobLike } = require('../core/util') |
| const { getGlobalDispatcher } = require('../global') |
| const { types } = require('util') |
|
|
| let experimentalWarned = false |
|
|
| |
| class WebSocket extends EventTarget { |
| #events = { |
| open: null, |
| error: null, |
| close: null, |
| message: null |
| } |
|
|
| #bufferedAmount = 0 |
| #protocol = '' |
| #extensions = '' |
|
|
| |
| |
| |
| |
| constructor (url, protocols = []) { |
| super() |
|
|
| webidl.argumentLengthCheck(arguments, 1, { header: 'WebSocket constructor' }) |
|
|
| if (!experimentalWarned) { |
| experimentalWarned = true |
| process.emitWarning('WebSockets are experimental, expect them to change at any time.', { |
| code: 'UNDICI-WS' |
| }) |
| } |
|
|
| const options = webidl.converters['DOMString or sequence<DOMString> or WebSocketInit'](protocols) |
|
|
| url = webidl.converters.USVString(url) |
| protocols = options.protocols |
|
|
| |
| const baseURL = getGlobalOrigin() |
|
|
| |
| let urlRecord |
|
|
| try { |
| urlRecord = new URL(url, baseURL) |
| } catch (e) { |
| |
| throw new DOMException(e, 'SyntaxError') |
| } |
|
|
| |
| if (urlRecord.protocol === 'http:') { |
| urlRecord.protocol = 'ws:' |
| } else if (urlRecord.protocol === 'https:') { |
| |
| urlRecord.protocol = 'wss:' |
| } |
|
|
| |
| if (urlRecord.protocol !== 'ws:' && urlRecord.protocol !== 'wss:') { |
| throw new DOMException( |
| `Expected a ws: or wss: protocol, got ${urlRecord.protocol}`, |
| 'SyntaxError' |
| ) |
| } |
|
|
| |
| |
| if (urlRecord.hash || urlRecord.href.endsWith('#')) { |
| throw new DOMException('Got fragment', 'SyntaxError') |
| } |
|
|
| |
| |
| if (typeof protocols === 'string') { |
| protocols = [protocols] |
| } |
|
|
| |
| |
| |
| |
| if (protocols.length !== new Set(protocols.map(p => p.toLowerCase())).size) { |
| throw new DOMException('Invalid Sec-WebSocket-Protocol value', 'SyntaxError') |
| } |
|
|
| if (protocols.length > 0 && !protocols.every(p => isValidSubprotocol(p))) { |
| throw new DOMException('Invalid Sec-WebSocket-Protocol value', 'SyntaxError') |
| } |
|
|
| |
| this[kWebSocketURL] = new URL(urlRecord.href) |
|
|
| |
|
|
| |
|
|
| |
| |
| this[kController] = establishWebSocketConnection( |
| urlRecord, |
| protocols, |
| this, |
| (response) => this.#onConnectionEstablished(response), |
| options |
| ) |
|
|
| |
| |
| |
| this[kReadyState] = WebSocket.CONNECTING |
|
|
| |
|
|
| |
|
|
| |
| |
| this[kBinaryType] = 'blob' |
| } |
|
|
| |
| |
| |
| |
| |
| close (code = undefined, reason = undefined) { |
| webidl.brandCheck(this, WebSocket) |
|
|
| if (code !== undefined) { |
| code = webidl.converters['unsigned short'](code, { clamp: true }) |
| } |
|
|
| if (reason !== undefined) { |
| reason = webidl.converters.USVString(reason) |
| } |
|
|
| |
| |
| |
| if (code !== undefined) { |
| if (code !== 1000 && (code < 3000 || code > 4999)) { |
| throw new DOMException('invalid code', 'InvalidAccessError') |
| } |
| } |
|
|
| let reasonByteLength = 0 |
|
|
| |
| if (reason !== undefined) { |
| |
| |
| |
| reasonByteLength = Buffer.byteLength(reason) |
|
|
| if (reasonByteLength > 123) { |
| throw new DOMException( |
| `Reason must be less than 123 bytes; received ${reasonByteLength}`, |
| 'SyntaxError' |
| ) |
| } |
| } |
|
|
| |
| if (this[kReadyState] === WebSocket.CLOSING || this[kReadyState] === WebSocket.CLOSED) { |
| |
| |
| } else if (!isEstablished(this)) { |
| |
| |
| |
| failWebsocketConnection(this, 'Connection was closed before it was established.') |
| this[kReadyState] = WebSocket.CLOSING |
| } else if (!isClosing(this)) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| const frame = new WebsocketFrameSend() |
|
|
| |
| |
|
|
| |
| |
| if (code !== undefined && reason === undefined) { |
| frame.frameData = Buffer.allocUnsafe(2) |
| frame.frameData.writeUInt16BE(code, 0) |
| } else if (code !== undefined && reason !== undefined) { |
| |
| |
| frame.frameData = Buffer.allocUnsafe(2 + reasonByteLength) |
| frame.frameData.writeUInt16BE(code, 0) |
| |
| frame.frameData.write(reason, 2, 'utf-8') |
| } else { |
| frame.frameData = emptyBuffer |
| } |
|
|
| |
| const socket = this[kResponse].socket |
|
|
| socket.write(frame.createFrame(opcodes.CLOSE), (err) => { |
| if (!err) { |
| this[kSentClose] = true |
| } |
| }) |
|
|
| |
| |
| |
| this[kReadyState] = states.CLOSING |
| } else { |
| |
| |
| this[kReadyState] = WebSocket.CLOSING |
| } |
| } |
|
|
| |
| |
| |
| |
| send (data) { |
| webidl.brandCheck(this, WebSocket) |
|
|
| webidl.argumentLengthCheck(arguments, 1, { header: 'WebSocket.send' }) |
|
|
| data = webidl.converters.WebSocketSendData(data) |
|
|
| |
| |
| if (this[kReadyState] === WebSocket.CONNECTING) { |
| throw new DOMException('Sent before connected.', 'InvalidStateError') |
| } |
|
|
| |
| |
| |
|
|
| if (!isEstablished(this) || isClosing(this)) { |
| return |
| } |
|
|
| |
| const socket = this[kResponse].socket |
|
|
| |
| if (typeof data === 'string') { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| const value = Buffer.from(data) |
| const frame = new WebsocketFrameSend(value) |
| const buffer = frame.createFrame(opcodes.TEXT) |
|
|
| this.#bufferedAmount += value.byteLength |
| socket.write(buffer, () => { |
| this.#bufferedAmount -= value.byteLength |
| }) |
| } else if (types.isArrayBuffer(data)) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| const value = Buffer.from(data) |
| const frame = new WebsocketFrameSend(value) |
| const buffer = frame.createFrame(opcodes.BINARY) |
|
|
| this.#bufferedAmount += value.byteLength |
| socket.write(buffer, () => { |
| this.#bufferedAmount -= value.byteLength |
| }) |
| } else if (ArrayBuffer.isView(data)) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| const ab = Buffer.from(data, data.byteOffset, data.byteLength) |
|
|
| const frame = new WebsocketFrameSend(ab) |
| const buffer = frame.createFrame(opcodes.BINARY) |
|
|
| this.#bufferedAmount += ab.byteLength |
| socket.write(buffer, () => { |
| this.#bufferedAmount -= ab.byteLength |
| }) |
| } else if (isBlobLike(data)) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| const frame = new WebsocketFrameSend() |
|
|
| data.arrayBuffer().then((ab) => { |
| const value = Buffer.from(ab) |
| frame.frameData = value |
| const buffer = frame.createFrame(opcodes.BINARY) |
|
|
| this.#bufferedAmount += value.byteLength |
| socket.write(buffer, () => { |
| this.#bufferedAmount -= value.byteLength |
| }) |
| }) |
| } |
| } |
|
|
| get readyState () { |
| webidl.brandCheck(this, WebSocket) |
|
|
| |
| return this[kReadyState] |
| } |
|
|
| get bufferedAmount () { |
| webidl.brandCheck(this, WebSocket) |
|
|
| return this.#bufferedAmount |
| } |
|
|
| get url () { |
| webidl.brandCheck(this, WebSocket) |
|
|
| |
| return URLSerializer(this[kWebSocketURL]) |
| } |
|
|
| get extensions () { |
| webidl.brandCheck(this, WebSocket) |
|
|
| return this.#extensions |
| } |
|
|
| get protocol () { |
| webidl.brandCheck(this, WebSocket) |
|
|
| return this.#protocol |
| } |
|
|
| get onopen () { |
| webidl.brandCheck(this, WebSocket) |
|
|
| return this.#events.open |
| } |
|
|
| set onopen (fn) { |
| webidl.brandCheck(this, WebSocket) |
|
|
| if (this.#events.open) { |
| this.removeEventListener('open', this.#events.open) |
| } |
|
|
| if (typeof fn === 'function') { |
| this.#events.open = fn |
| this.addEventListener('open', fn) |
| } else { |
| this.#events.open = null |
| } |
| } |
|
|
| get onerror () { |
| webidl.brandCheck(this, WebSocket) |
|
|
| return this.#events.error |
| } |
|
|
| set onerror (fn) { |
| webidl.brandCheck(this, WebSocket) |
|
|
| if (this.#events.error) { |
| this.removeEventListener('error', this.#events.error) |
| } |
|
|
| if (typeof fn === 'function') { |
| this.#events.error = fn |
| this.addEventListener('error', fn) |
| } else { |
| this.#events.error = null |
| } |
| } |
|
|
| get onclose () { |
| webidl.brandCheck(this, WebSocket) |
|
|
| return this.#events.close |
| } |
|
|
| set onclose (fn) { |
| webidl.brandCheck(this, WebSocket) |
|
|
| if (this.#events.close) { |
| this.removeEventListener('close', this.#events.close) |
| } |
|
|
| if (typeof fn === 'function') { |
| this.#events.close = fn |
| this.addEventListener('close', fn) |
| } else { |
| this.#events.close = null |
| } |
| } |
|
|
| get onmessage () { |
| webidl.brandCheck(this, WebSocket) |
|
|
| return this.#events.message |
| } |
|
|
| set onmessage (fn) { |
| webidl.brandCheck(this, WebSocket) |
|
|
| if (this.#events.message) { |
| this.removeEventListener('message', this.#events.message) |
| } |
|
|
| if (typeof fn === 'function') { |
| this.#events.message = fn |
| this.addEventListener('message', fn) |
| } else { |
| this.#events.message = null |
| } |
| } |
|
|
| get binaryType () { |
| webidl.brandCheck(this, WebSocket) |
|
|
| return this[kBinaryType] |
| } |
|
|
| set binaryType (type) { |
| webidl.brandCheck(this, WebSocket) |
|
|
| if (type !== 'blob' && type !== 'arraybuffer') { |
| this[kBinaryType] = 'blob' |
| } else { |
| this[kBinaryType] = type |
| } |
| } |
|
|
| |
| |
| |
| #onConnectionEstablished (response) { |
| |
| |
| this[kResponse] = response |
|
|
| const parser = new ByteParser(this) |
| parser.on('drain', function onParserDrain () { |
| this.ws[kResponse].socket.resume() |
| }) |
|
|
| response.socket.ws = this |
| this[kByteParser] = parser |
|
|
| |
| this[kReadyState] = states.OPEN |
|
|
| |
| |
| |
| const extensions = response.headersList.get('sec-websocket-extensions') |
|
|
| if (extensions !== null) { |
| this.#extensions = extensions |
| } |
|
|
| |
| |
| |
| const protocol = response.headersList.get('sec-websocket-protocol') |
|
|
| if (protocol !== null) { |
| this.#protocol = protocol |
| } |
|
|
| |
| fireEvent('open', this) |
| } |
| } |
|
|
| |
| WebSocket.CONNECTING = WebSocket.prototype.CONNECTING = states.CONNECTING |
| |
| WebSocket.OPEN = WebSocket.prototype.OPEN = states.OPEN |
| |
| WebSocket.CLOSING = WebSocket.prototype.CLOSING = states.CLOSING |
| |
| WebSocket.CLOSED = WebSocket.prototype.CLOSED = states.CLOSED |
|
|
| Object.defineProperties(WebSocket.prototype, { |
| CONNECTING: staticPropertyDescriptors, |
| OPEN: staticPropertyDescriptors, |
| CLOSING: staticPropertyDescriptors, |
| CLOSED: staticPropertyDescriptors, |
| url: kEnumerableProperty, |
| readyState: kEnumerableProperty, |
| bufferedAmount: kEnumerableProperty, |
| onopen: kEnumerableProperty, |
| onerror: kEnumerableProperty, |
| onclose: kEnumerableProperty, |
| close: kEnumerableProperty, |
| onmessage: kEnumerableProperty, |
| binaryType: kEnumerableProperty, |
| send: kEnumerableProperty, |
| extensions: kEnumerableProperty, |
| protocol: kEnumerableProperty, |
| [Symbol.toStringTag]: { |
| value: 'WebSocket', |
| writable: false, |
| enumerable: false, |
| configurable: true |
| } |
| }) |
|
|
| Object.defineProperties(WebSocket, { |
| CONNECTING: staticPropertyDescriptors, |
| OPEN: staticPropertyDescriptors, |
| CLOSING: staticPropertyDescriptors, |
| CLOSED: staticPropertyDescriptors |
| }) |
|
|
| webidl.converters['sequence<DOMString>'] = webidl.sequenceConverter( |
| webidl.converters.DOMString |
| ) |
|
|
| webidl.converters['DOMString or sequence<DOMString>'] = function (V) { |
| if (webidl.util.Type(V) === 'Object' && Symbol.iterator in V) { |
| return webidl.converters['sequence<DOMString>'](V) |
| } |
|
|
| return webidl.converters.DOMString(V) |
| } |
|
|
| |
| webidl.converters.WebSocketInit = webidl.dictionaryConverter([ |
| { |
| key: 'protocols', |
| converter: webidl.converters['DOMString or sequence<DOMString>'], |
| get defaultValue () { |
| return [] |
| } |
| }, |
| { |
| key: 'dispatcher', |
| converter: (V) => V, |
| get defaultValue () { |
| return getGlobalDispatcher() |
| } |
| }, |
| { |
| key: 'headers', |
| converter: webidl.nullableConverter(webidl.converters.HeadersInit) |
| } |
| ]) |
|
|
| webidl.converters['DOMString or sequence<DOMString> or WebSocketInit'] = function (V) { |
| if (webidl.util.Type(V) === 'Object' && !(Symbol.iterator in V)) { |
| return webidl.converters.WebSocketInit(V) |
| } |
|
|
| return { protocols: webidl.converters['DOMString or sequence<DOMString>'](V) } |
| } |
|
|
| webidl.converters.WebSocketSendData = function (V) { |
| if (webidl.util.Type(V) === 'Object') { |
| if (isBlobLike(V)) { |
| return webidl.converters.Blob(V, { strict: false }) |
| } |
|
|
| if (ArrayBuffer.isView(V) || types.isAnyArrayBuffer(V)) { |
| return webidl.converters.BufferSource(V) |
| } |
| } |
|
|
| return webidl.converters.USVString(V) |
| } |
|
|
| module.exports = { |
| WebSocket |
| } |
|
|