Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Sep 7, 2024
1 parent 99c1234 commit 5ee5114
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
22 changes: 5 additions & 17 deletions lib/web/websocket/connection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const { uid, states, sentCloseFrameState, emptyBuffer, opcodes } = require('./constants')
const { failWebsocketConnection, parseExtensions, isClosed, isClosing, isEstablished } = require('./util')
const { failWebsocketConnection, parseExtensions, isClosed, isClosing, isEstablished, validateCloseCodeAndReason } = require('./util')
const { channels } = require('../../core/diagnostics')
const { makeRequest } = require('../fetch/request')
const { fetching } = require('../fetch/index')
Expand Down Expand Up @@ -208,32 +208,21 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
return controller
}

/**
* @param {import('./websocket').Handler} handler
* @param {number} code
* @param {any} reason
* @param {number} reasonByteLength
*/
function closeWebSocketConnection (handler, code, reason) {
handler.onClose(code, reason)
}

/**
* @see https://whatpr.org/websockets/48.html#close-the-websocket
* @param {import('./websocket').Handler} object
* @param {number} [code]
* @param {string} [reason]
*/
function $closeWebSocketConnection (object, code, reason) {
function closeWebSocketConnection (object, code, reason, validate = false) {
// 1. If code was not supplied, let code be null.
code ??= null

// 2. If reason was not supplied, let reason be the empty string.
reason ??= ''

// 3. Validate close code and reason with code and reason.
// TODO
// validateCloseCodeAndReason(code, reason)
if (validate) validateCloseCodeAndReason(code, reason)

// 4. Run the first matching steps from the following list:
// - If object’s ready state is CLOSING (2) or CLOSED (3)
Expand Down Expand Up @@ -263,7 +252,7 @@ function $closeWebSocketConnection (object, code, reason) {
} else if (code !== null && reason !== null) {
// If reason is also present, then reasonBytes must be
// provided in the Close message after the status code.
frame.frameData = Buffer.allocUnsafe(2 + reason.length)
frame.frameData = Buffer.allocUnsafe(2 + Buffer.byteLength(reason))
frame.frameData.writeUInt16BE(code, 0)
// the body MAY contain UTF-8-encoded data with value /reason/
frame.frameData.write(reason, 2, 'utf-8')
Expand All @@ -287,6 +276,5 @@ function $closeWebSocketConnection (object, code, reason) {

module.exports = {
establishWebSocketConnection,
closeWebSocketConnection,
$closeWebSocketConnection
closeWebSocketConnection
}
40 changes: 29 additions & 11 deletions lib/web/websocket/stream/websocketstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { createDeferredPromise, environmentSettingsObject } = require('../../fetc
const { states, opcodes, sentCloseFrameState } = require('../constants')
const { webidl } = require('../../fetch/webidl')
const { getURLRecord, isValidSubprotocol, isEstablished, failWebsocketConnection, utf8Decode } = require('../util')
const { establishWebSocketConnection, $closeWebSocketConnection } = require('../connection')
const { establishWebSocketConnection, closeWebSocketConnection } = require('../connection')
const { types } = require('node:util')
const { channels } = require('../../../core/diagnostics')
const { WebsocketFrameSend } = require('../frame')
Expand Down Expand Up @@ -45,7 +45,6 @@ class WebSocketStream {
onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions),
onFail: (reason) => {},
onMessage: (opcode, data) => this.#onMessage(opcode, data),
onClose: (code, reason) => {},
onParserError: (err) => console.log({ err }),
onParserDrain: () => {},
onSocketData: (chunk) => {
Expand All @@ -54,7 +53,6 @@ class WebSocketStream {
}
},
onSocketError: (err) => {
console.log({ err })
this.#handler.readyState = states.CLOSING

if (channels.socketError.hasSubscribers) {
Expand Down Expand Up @@ -192,12 +190,7 @@ class WebSocketStream {
const reason = closeInfo.reason

// 3. Close the WebSocket with this , code , and reason .
$closeWebSocketConnection(this.#handler, code, reason)
}

// To cancel a WebSocketStream stream given reason , close using reason giving stream and reason .
#cancel (reason) {
$closeWebSocketConnection(this.#handler, null, reason)
closeWebSocketConnection(this.#handler, code, reason, true)
}

#write (chunk) {
Expand All @@ -211,7 +204,7 @@ class WebSocketStream {
let opcode = null

// 4. If chunk is a BufferSource ,
if (types.isTypedArray(chunk) || types.isAnyArrayBuffer(chunk)) {
if (ArrayBuffer.isView(chunk) || types.isAnyArrayBuffer(chunk)) {
// 4.1. Set data to a copy of the bytes given chunk .
data = Buffer.from(chunk)

Expand Down Expand Up @@ -299,7 +292,8 @@ class WebSocketStream {
// 13. Set up writable with writeAlgorithm , closeAlgorithm , and abortAlgorithm .
const writable = new WritableStream({
write: (chunk) => this.#write(chunk),
close: () => $closeWebSocketConnection(this.#handler, null, null)
close: () => closeWebSocketConnection(this.#handler, null, null),
abort: (reason) => this.#closeUsingReason(reason)
})

// Set stream ’s readable stream to readable .
Expand Down Expand Up @@ -348,6 +342,30 @@ class WebSocketStream {

// 4. Apply backpressure to the WebSocket.
}

#closeUsingReason (reason) {
// 1. Let code be null.
const code = null

// 2. Let reasonString be the empty string.
const reasonString = ''

// TODO
// 3. If reason implements WebSocketError ,

// 3.1. Set code to reason ’s closeCode .

// 3.2. Set reasonString to reason ’s 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)
}
}

webidl.converters.WebSocketStreamOptions = webidl.dictionaryConverter([
Expand Down
10 changes: 2 additions & 8 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const {
toArrayBuffer,
getURLRecord
} = require('./util')
const { establishWebSocketConnection, closeWebSocketConnection, $closeWebSocketConnection } = require('./connection')
const { establishWebSocketConnection, closeWebSocketConnection } = require('./connection')
const { ByteParser } = require('./receiver')
const { kEnumerableProperty } = require('../../core/util')
const { getGlobalDispatcher } = require('../../global')
Expand All @@ -29,7 +29,6 @@ const { channels } = require('../../core/diagnostics')
* @property {(response: any, extensions?: string[]) => void} onConnectionEstablished
* @property {(reason: any) => void} onFail
* @property {(opcode: number, data: Buffer) => void} onMessage
* @property {(code: number, reason: any) => void} onClose
* @property {(error: Error) => void} onParserError
* @property {() => void} onParserDrain
* @property {(chunk: Buffer) => void} onSocketData
Expand Down Expand Up @@ -63,7 +62,6 @@ class WebSocket extends EventTarget {
onConnectionEstablished: (response, extensions) => this.#onConnectionEstablished(response, extensions),
onFail: (reason) => this.#onFail(reason),
onMessage: (opcode, data) => this.#onMessage(opcode, data),
onClose: (code, reason) => this.#onClose(code, reason),
onParserError: (err) => this.#onParserError(err),
onParserDrain: () => this.#onParserDrain(),
onSocketData: (chunk) => {
Expand Down Expand Up @@ -191,7 +189,7 @@ class WebSocket extends EventTarget {
reason ??= ''

// 3. Close the WebSocket with this, code, and reason.
$closeWebSocketConnection(this.#handler, code, reason)
closeWebSocketConnection(this.#handler, code, reason, true)
}

/**
Expand Down Expand Up @@ -523,10 +521,6 @@ class WebSocket extends EventTarget {
})
}

#onClose (code, reason) {
$closeWebSocketConnection(this.#handler, code, reason)
}

#onParserError (err) {
let message
let code
Expand Down

0 comments on commit 5ee5114

Please sign in to comment.