Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement WebSocketStream #2713

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/web/fetch/webidl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const webidl = {}
webidl.converters = {}
webidl.util = {}
webidl.errors = {}
webidl.is = {}

webidl.errors.exception = function (message) {
return new TypeError(`${message.header}: ${message.message}`)
Expand Down Expand Up @@ -675,6 +676,10 @@ webidl.converters['record<ByteString, ByteString>'] = webidl.recordConverter(
webidl.converters.ByteString
)

webidl.is.BufferSource = function (V) {
return ArrayBuffer.isView(V) || types.isAnyArrayBuffer(V)
}

module.exports = {
webidl
}
76 changes: 76 additions & 0 deletions lib/web/websocket/stream/websocketerror.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict'

const { kEnumerableProperty } = require('../../../core/util')
const { webidl } = require('../../fetch/webidl')
const { validateCloseCodeAndReason } = require('../util')

/**
* @see https://whatpr.org/websockets/48/7b748d3...7b81f79.html#websocketerror
*/
class WebSocketError extends DOMException {
/** @type {string} */
#reason
/** @type {number|null} */
#closeCode = null

/**
* @param {string} message
* @param {import('./websocketstream').WebSocketCloseInfo} init
*/
constructor (message = '', init = {}) {
super(message, 'WebSocketError')

message = webidl.converters.DOMString(message)
init = webidl.converters.WebSocketCloseInfo(init)

// 1. Set this 's name to " WebSocketError ".
// 2. Set this 's message to message .

// 3. Let code be init [" closeCode "] if it exists , or null otherwise.
let code = init.closeCode ?? null

// 4. Let reason be init [" reason "] if it exists , or the empty string otherwise.
const reason = init.reason

// 5. Validate close code and reason with code and reason .
validateCloseCodeAndReason(code, reason)

// 6. If reason is non-empty, but code is not set, then set code to 1000 ("Normal Closure").
if (reason.length) code ??= 1000

// 7. Set this 's closeCode to code .
this.#closeCode = code

// 8. Set this 's reason to reason .
this.#reason = reason
}

get closeCode () {
return this.#closeCode
}

get reason () {
return this.#reason
}
}

Object.defineProperties(WebSocketError.prototype, {
closeCode: kEnumerableProperty,
reason: kEnumerableProperty
})

webidl.converters.WebSocketCloseInfo = webidl.dictionaryConverter([
{
converter: webidl.nullableConverter((V) => webidl.converters['unsigned short'](V, { enforceRange: true })),
key: 'closeCode'
},
{
converter: webidl.converters.USVString,
key: 'reason',
defaultValue: ''
}
])

module.exports = {
WebSocketError
}
Loading
Loading