Spaces:
Paused
Paused
File size: 17,980 Bytes
3401f26 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | 'use strict'
const { createDeferredPromise } = require('../../../util/promise')
const { environmentSettingsObject } = require('../../fetch/util')
const { states, opcodes, sentCloseFrameState } = require('../constants')
const { webidl } = require('../../webidl')
const { getURLRecord, isValidSubprotocol, isEstablished, utf8Decode } = require('../util')
const { establishWebSocketConnection, failWebsocketConnection, closeWebSocketConnection } = require('../connection')
const { channels } = require('../../../core/diagnostics')
const { WebsocketFrameSend } = require('../frame')
const { ByteParser } = require('../receiver')
const { WebSocketError, createUnvalidatedWebSocketError } = require('./websocketerror')
const { kEnumerableProperty } = require('../../../core/util')
const { utf8DecodeBytes } = require('../../../encoding')
let emittedExperimentalWarning = false
class WebSocketStream {
// Each WebSocketStream object has an associated url , which is a URL record .
/** @type {URL} */
#url
// Each WebSocketStream object has an associated opened promise , which is a promise.
/** @type {import('../../../util/promise').DeferredPromise} */
#openedPromise
// Each WebSocketStream object has an associated closed promise , which is a promise.
/** @type {import('../../../util/promise').DeferredPromise} */
#closedPromise
// Each WebSocketStream object has an associated readable stream , which is a ReadableStream .
/** @type {ReadableStream} */
#readableStream
/** @type {ReadableStreamDefaultController} */
#readableStreamController
// Each WebSocketStream object has an associated writable stream , which is a WritableStream .
/** @type {WritableStream} */
#writableStream
// Each WebSocketStream object has an associated boolean handshake aborted , which is initially false.
#handshakeAborted = false
/** @type {import('../websocket').Handler} */
#handler = {
// https://whatpr.org/websockets/48/7b748d3...d5570f3.html#feedback-to-websocket-stream-from-the-protocol
onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions),
onMessage: (opcode, data) => this.#onMessage(opcode, data),
onParserError: (err) => failWebsocketConnection(this.#handler, null, err.message),
onParserDrain: () => this.#handler.socket.resume(),
onSocketData: (chunk) => {
if (!this.#parser.write(chunk)) {
this.#handler.socket.pause()
}
},
onSocketError: (err) => {
this.#handler.readyState = states.CLOSING
if (channels.socketError.hasSubscribers) {
channels.socketError.publish(err)
}
this.#handler.socket.destroy()
},
onSocketClose: () => this.#onSocketClose(),
onPing: () => {},
onPong: () => {},
readyState: states.CONNECTING,
socket: null,
closeState: new Set(),
controller: null,
wasEverConnected: false
}
/** @type {import('../receiver').ByteParser} */
#parser
constructor (url, options = undefined) {
if (!emittedExperimentalWarning) {
process.emitWarning('WebSocketStream is experimental! Expect it to change at any time.', {
code: 'UNDICI-WSS'
})
emittedExperimentalWarning = true
}
webidl.argumentLengthCheck(arguments, 1, 'WebSocket')
url = webidl.converters.USVString(url)
if (options !== null) {
options = webidl.converters.WebSocketStreamOptions(options)
}
// 1. Let baseURL be this 's relevant settings object 's API base URL .
const baseURL = environmentSettingsObject.settingsObject.baseUrl
// 2. Let urlRecord be the result of getting a URL record given url and baseURL .
const urlRecord = getURLRecord(url, baseURL)
// 3. Let protocols be options [" protocols "] if it exists , otherwise an empty sequence.
const protocols = options.protocols
// 4. If any of the values in protocols occur more than once or otherwise fail to match the requirements for elements that comprise the value of ` Sec-WebSocket-Protocol ` fields as defined by The WebSocket Protocol , then throw a " SyntaxError " DOMException . [WSP]
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')
}
// 5. Set this 's url to urlRecord .
this.#url = urlRecord.toString()
// 6. Set this 's opened promise and closed promise to new promises.
this.#openedPromise = createDeferredPromise()
this.#closedPromise = createDeferredPromise()
// 7. Apply backpressure to the WebSocket.
// TODO
// 8. If options [" signal "] exists ,
if (options.signal != null) {
// 8.1. Let signal be options [" signal "].
const signal = options.signal
// 8.2. If signal is aborted , then reject this 's opened promise and closed promise with signal ’s abort reason
// and return.
if (signal.aborted) {
this.#openedPromise.reject(signal.reason)
this.#closedPromise.reject(signal.reason)
return
}
// 8.3. Add the following abort steps to signal :
signal.addEventListener('abort', () => {
// 8.3.1. If the WebSocket connection is not yet established : [WSP]
if (!isEstablished(this.#handler.readyState)) {
// 8.3.1.1. Fail the WebSocket connection .
failWebsocketConnection(this.#handler)
// Set this 's ready state to CLOSING .
this.#handler.readyState = states.CLOSING
// Reject this 's opened promise and closed promise with signal ’s abort reason .
this.#openedPromise.reject(signal.reason)
this.#closedPromise.reject(signal.reason)
// Set this 's handshake aborted to true.
this.#handshakeAborted = true
}
}, { once: true })
}
// 9. Let client be this 's relevant settings object .
const client = environmentSettingsObject.settingsObject
// 10. Run this step in parallel :
// 10.1. Establish a WebSocket connection given urlRecord , protocols , and client . [FETCH]
this.#handler.controller = establishWebSocketConnection(
urlRecord,
protocols,
client,
this.#handler,
options
)
}
// The url getter steps are to return this 's url , serialized .
get url () {
return this.#url.toString()
}
// The opened getter steps are to return this 's opened promise .
get opened () {
return this.#openedPromise.promise
}
// The closed getter steps are to return this 's closed promise .
get closed () {
return this.#closedPromise.promise
}
// The close( closeInfo ) method steps are:
close (closeInfo = undefined) {
if (closeInfo !== null) {
closeInfo = webidl.converters.WebSocketCloseInfo(closeInfo)
}
// 1. Let code be closeInfo [" closeCode "] if present, or null otherwise.
const code = closeInfo.closeCode ?? null
// 2. Let reason be closeInfo [" reason "].
const reason = closeInfo.reason
// 3. Close the WebSocket with this , code , and reason .
closeWebSocketConnection(this.#handler, code, reason, true)
}
#write (chunk) {
// See /websockets/stream/tentative/write.any.html
chunk = webidl.converters.WebSocketStreamWrite(chunk)
// 1. Let promise be a new promise created in stream ’s relevant realm .
const promise = createDeferredPromise()
// 2. Let data be null.
let data = null
// 3. Let opcode be null.
let opcode = null
// 4. If chunk is a BufferSource ,
if (webidl.is.BufferSource(chunk)) {
// 4.1. Set data to a copy of the bytes given chunk .
data = new Uint8Array(ArrayBuffer.isView(chunk) ? new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength) : chunk.slice())
// 4.2. Set opcode to a binary frame opcode.
opcode = opcodes.BINARY
} else {
// 5. Otherwise,
// 5.1. Let string be the result of converting chunk to an IDL USVString .
// If this throws an exception, return a promise rejected with the exception.
let string
try {
string = webidl.converters.DOMString(chunk)
} catch (e) {
promise.reject(e)
return promise.promise
}
// 5.2. Set data to the result of UTF-8 encoding string .
data = new TextEncoder().encode(string)
// 5.3. Set opcode to a text frame opcode.
opcode = opcodes.TEXT
}
// 6. In parallel,
// 6.1. Wait until there is sufficient buffer space in stream to send the message.
// 6.2. If the closing handshake has not yet started , Send a WebSocket Message to stream comprised of data using opcode .
if (!this.#handler.closeState.has(sentCloseFrameState.SENT) && !this.#handler.closeState.has(sentCloseFrameState.RECEIVED)) {
const frame = new WebsocketFrameSend(data)
this.#handler.socket.write(frame.createFrame(opcode), () => {
promise.resolve(undefined)
})
}
// 6.3. Queue a global task on the WebSocket task source given stream ’s relevant global object to resolve promise with undefined.
return promise.promise
}
/** @type {import('../websocket').Handler['onConnectionEstablished']} */
#onConnectionEstablished (response, parsedExtensions) {
this.#handler.socket = response.socket
// Get options from dispatcher options
const maxFragments = this.#handler.controller.dispatcher?.webSocketOptions?.maxFragments
const maxPayloadSize = this.#handler.controller.dispatcher?.webSocketOptions?.maxPayloadSize
const parser = new ByteParser(this.#handler, parsedExtensions, {
maxFragments,
maxPayloadSize
})
parser.on('drain', () => this.#handler.onParserDrain())
parser.on('error', (err) => this.#handler.onParserError(err))
this.#parser = parser
// 1. Change stream ’s ready state to OPEN (1).
this.#handler.readyState = states.OPEN
// 2. Set stream ’s was ever connected to true.
// This is done in the opening handshake.
// 3. Let extensions be the extensions in use .
const extensions = parsedExtensions ?? ''
// 4. Let protocol be the subprotocol in use .
const protocol = response.headersList.get('sec-websocket-protocol') ?? ''
// 5. Let pullAlgorithm be an action that pulls bytes from stream .
// 6. Let cancelAlgorithm be an action that cancels stream with reason , given reason .
// 7. Let readable be a new ReadableStream .
// 8. Set up readable with pullAlgorithm and cancelAlgorithm .
const readable = new ReadableStream({
start: (controller) => {
this.#readableStreamController = controller
},
cancel: (reason) => this.#cancel(reason)
})
// 9. Let writeAlgorithm be an action that writes chunk to stream , given chunk .
// 10. Let closeAlgorithm be an action that closes stream .
// 11. Let abortAlgorithm be an action that aborts stream with reason , given reason .
// 12. Let writable be a new WritableStream .
// 13. Set up writable with writeAlgorithm , closeAlgorithm , and abortAlgorithm .
const writable = new WritableStream({
write: (chunk) => this.#write(chunk),
close: () => closeWebSocketConnection(this.#handler, null, null),
abort: (reason) => this.#closeUsingReason(reason)
})
// Set stream ’s readable stream to readable .
this.#readableStream = readable
// Set stream ’s writable stream to writable .
this.#writableStream = writable
// Resolve stream ’s opened promise with WebSocketOpenInfo «[ " extensions " → extensions , " protocol " → protocol , " readable " → readable , " writable " → writable ]».
this.#openedPromise.resolve({
extensions,
protocol,
readable,
writable
})
}
/** @type {import('../websocket').Handler['onMessage']} */
#onMessage (type, data) {
// 1. If stream’s ready state is not OPEN (1), then return.
if (this.#handler.readyState !== states.OPEN) {
return
}
// 2. Let chunk be determined by switching on type:
// - type indicates that the data is Text
// a new DOMString containing data
// - type indicates that the data is Binary
// a new Uint8Array object, created in the relevant Realm of the
// WebSocketStream object, whose contents are data
let chunk
if (type === opcodes.TEXT) {
try {
chunk = utf8Decode(data)
} catch {
failWebsocketConnection(this.#handler, 1007, 'Received invalid UTF-8 in text frame.')
return
}
} else if (type === opcodes.BINARY) {
chunk = new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
}
// 3. Enqueue chunk into stream’s readable stream.
this.#readableStreamController.enqueue(chunk)
// 4. Apply backpressure to the WebSocket.
}
/** @type {import('../websocket').Handler['onSocketClose']} */
#onSocketClose () {
const wasClean =
this.#handler.closeState.has(sentCloseFrameState.SENT) &&
this.#handler.closeState.has(sentCloseFrameState.RECEIVED)
// 1. Change the ready state to CLOSED (3).
this.#handler.readyState = states.CLOSED
// 2. If stream ’s handshake aborted is true, then return.
if (this.#handshakeAborted) {
return
}
// 3. If stream ’s was ever connected is false, then reject stream ’s opened promise with a new WebSocketError.
if (!this.#handler.wasEverConnected) {
this.#openedPromise.reject(new WebSocketError('Socket never opened'))
}
const result = this.#parser?.closingInfo
// 4. Let code be the WebSocket connection close code .
// https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.5
// If this Close control frame contains no status code, _The WebSocket
// Connection Close Code_ is considered to be 1005. If _The WebSocket
// Connection is Closed_ and no Close control frame was received by the
// endpoint (such as could occur if the underlying transport connection
// is lost), _The WebSocket Connection Close Code_ is considered to be
// 1006.
let code = result?.code ?? 1005
if (!this.#handler.closeState.has(sentCloseFrameState.SENT) && !this.#handler.closeState.has(sentCloseFrameState.RECEIVED)) {
code = 1006
}
// 5. Let reason be the result of applying UTF-8 decode without BOM to the WebSocket connection close reason .
const reason = result?.reason == null ? '' : utf8DecodeBytes(Buffer.from(result.reason))
// 6. If the connection was closed cleanly ,
if (wasClean) {
// 6.1. Close stream ’s readable stream .
this.#readableStreamController.close()
// 6.2. Error stream ’s writable stream with an " InvalidStateError " DOMException indicating that a closed WebSocketStream cannot be written to.
if (!this.#writableStream.locked) {
this.#writableStream.abort(new DOMException('A closed WebSocketStream cannot be written to', 'InvalidStateError'))
}
// 6.3. Resolve stream ’s closed promise with WebSocketCloseInfo «[ " closeCode " → code , " reason " → reason ]».
this.#closedPromise.resolve({
closeCode: code,
reason
})
} else {
// 7. Otherwise,
// 7.1. Let error be a new WebSocketError whose closeCode is code and reason is reason .
const error = createUnvalidatedWebSocketError('unclean close', code, reason)
// 7.2. Error stream ’s readable stream with error .
this.#readableStreamController?.error(error)
// 7.3. Error stream ’s writable stream with error .
this.#writableStream?.abort(error)
// 7.4. Reject stream ’s closed promise with error .
this.#closedPromise.reject(error)
}
}
#closeUsingReason (reason) {
// 1. Let code be null.
let code = null
// 2. Let reasonString be the empty string.
let reasonString = ''
// 3. If reason implements WebSocketError ,
if (webidl.is.WebSocketError(reason)) {
// 3.1. Set code to reason ’s closeCode .
code = reason.closeCode
// 3.2. Set reasonString to reason ’s reason .
reasonString = reason.reason
}
// 4. Close the WebSocket with stream , code , and reasonString . If this throws an exception,
// discard code and reasonString and close the WebSocket with stream .
closeWebSocketConnection(this.#handler, code, reasonString)
}
// To cancel a WebSocketStream stream given reason , close using reason giving stream and reason .
#cancel (reason) {
this.#closeUsingReason(reason)
}
}
Object.defineProperties(WebSocketStream.prototype, {
url: kEnumerableProperty,
opened: kEnumerableProperty,
closed: kEnumerableProperty,
close: kEnumerableProperty,
[Symbol.toStringTag]: {
value: 'WebSocketStream',
writable: false,
enumerable: false,
configurable: true
}
})
webidl.converters.WebSocketStreamOptions = webidl.dictionaryConverter([
{
key: 'protocols',
converter: webidl.sequenceConverter(webidl.converters.USVString),
defaultValue: () => []
},
{
key: 'signal',
converter: webidl.nullableConverter(webidl.converters.AbortSignal),
defaultValue: () => null
}
])
webidl.converters.WebSocketCloseInfo = webidl.dictionaryConverter([
{
key: 'closeCode',
converter: (V) => webidl.converters['unsigned short'](V, webidl.attributes.EnforceRange)
},
{
key: 'reason',
converter: webidl.converters.USVString,
defaultValue: () => ''
}
])
webidl.converters.WebSocketStreamWrite = function (V) {
if (typeof V === 'string') {
return webidl.converters.USVString(V)
}
return webidl.converters.BufferSource(V)
}
module.exports = { WebSocketStream }
|