Skip to content

Commit

Permalink
implement WebSocketStream
Browse files Browse the repository at this point in the history
enumerable

implement some more

implement close

finish open

run WPTs

update wpts and fix 9/19 failures

fix most remaining failures

fix remaining test failures

fix writing

fix: implement writing completely

fixup

fixup
  • Loading branch information
KhafraDev committed Mar 6, 2024
1 parent 03a2d43 commit 13187d0
Show file tree
Hide file tree
Showing 14 changed files with 1,189 additions and 66 deletions.
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
}

0 comments on commit 13187d0

Please sign in to comment.