2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-08-09 18:28:17 -04:00
|
|
|
|
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_ws_check_permission_and_cancel_handle,
|
|
|
|
op_ws_close,
|
|
|
|
op_ws_create,
|
|
|
|
op_ws_get_buffer,
|
|
|
|
op_ws_get_buffer_as_string,
|
|
|
|
op_ws_get_error,
|
|
|
|
op_ws_next_event,
|
|
|
|
op_ws_send_binary_async,
|
|
|
|
op_ws_send_text_async,
|
2024-01-26 17:46:46 -05:00
|
|
|
} from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
ArrayPrototypeJoin,
|
|
|
|
ArrayPrototypeMap,
|
2023-05-01 09:30:02 -04:00
|
|
|
DateNow,
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
PromisePrototypeCatch,
|
|
|
|
PromisePrototypeThen,
|
2023-04-14 16:23:28 -04:00
|
|
|
SafeSet,
|
2023-04-02 13:41:41 -04:00
|
|
|
SetPrototypeGetSize,
|
2023-02-07 14:22:46 -05:00
|
|
|
StringPrototypeEndsWith,
|
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
TypeError,
|
2023-05-01 09:30:02 -04:00
|
|
|
TypedArrayPrototypeGetByteLength,
|
2024-01-03 23:12:38 -05:00
|
|
|
TypedArrayPrototypeGetSymbolToStringTag,
|
2023-02-07 14:22:46 -05:00
|
|
|
} = primordials;
|
2024-01-10 17:37:25 -05:00
|
|
|
|
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
|
|
|
|
import { Deferred, writableStreamClose } from "ext:deno_web/06_streams.js";
|
|
|
|
import { DOMException } from "ext:deno_web/01_dom_exception.js";
|
|
|
|
import { add, remove } from "ext:deno_web/03_abort_signal.js";
|
2023-12-24 08:04:32 -05:00
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
fillHeaders,
|
|
|
|
headerListFromHeaders,
|
|
|
|
headersFromHeaderList,
|
|
|
|
} from "ext:deno_fetch/20_headers.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
webidl.converters.WebSocketStreamOptions = webidl.createDictionaryConverter(
|
|
|
|
"WebSocketStreamOptions",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "protocols",
|
|
|
|
converter: webidl.converters["sequence<USVString>"],
|
|
|
|
get defaultValue() {
|
|
|
|
return [];
|
2022-01-05 11:41:44 -05:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "signal",
|
|
|
|
converter: webidl.converters.AbortSignal,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "headers",
|
|
|
|
converter: webidl.converters.HeadersInit,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
webidl.converters.WebSocketCloseInfo = webidl.createDictionaryConverter(
|
|
|
|
"WebSocketCloseInfo",
|
|
|
|
[
|
|
|
|
{
|
2024-04-08 14:46:53 -04:00
|
|
|
key: "closeCode",
|
|
|
|
converter: (V, prefix, context, opts) =>
|
|
|
|
webidl.converters["unsigned short"](V, prefix, context, {
|
|
|
|
...opts,
|
|
|
|
enforceRange: true,
|
|
|
|
}),
|
2023-02-07 14:22:46 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "reason",
|
|
|
|
converter: webidl.converters.USVString,
|
|
|
|
defaultValue: "",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
const CLOSE_RESPONSE_TIMEOUT = 5000;
|
|
|
|
|
|
|
|
const _rid = Symbol("[[rid]]");
|
|
|
|
const _url = Symbol("[[url]]");
|
2023-10-11 01:31:05 -04:00
|
|
|
const _opened = Symbol("[[opened]]");
|
2023-02-07 14:22:46 -05:00
|
|
|
const _closed = Symbol("[[closed]]");
|
|
|
|
const _earlyClose = Symbol("[[earlyClose]]");
|
|
|
|
const _closeSent = Symbol("[[closeSent]]");
|
|
|
|
class WebSocketStream {
|
|
|
|
[_rid];
|
|
|
|
|
|
|
|
[_url];
|
|
|
|
get url() {
|
|
|
|
webidl.assertBranded(this, WebSocketStreamPrototype);
|
|
|
|
return this[_url];
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(url, options) {
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
const prefix = "Failed to construct 'WebSocketStream'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
url = webidl.converters.USVString(url, prefix, "Argument 1");
|
|
|
|
options = webidl.converters.WebSocketStreamOptions(
|
|
|
|
options,
|
2023-02-07 14:22:46 -05:00
|
|
|
prefix,
|
2023-05-01 06:47:13 -04:00
|
|
|
"Argument 2",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
const wsURL = new URL(url);
|
|
|
|
|
|
|
|
if (wsURL.protocol !== "ws:" && wsURL.protocol !== "wss:") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Only ws & wss schemes are allowed in a WebSocket URL.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
2021-08-09 18:28:17 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (wsURL.hash !== "" || StringPrototypeEndsWith(wsURL.href, "#")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Fragments are not allowed in a WebSocket URL.",
|
|
|
|
"SyntaxError",
|
2021-08-09 18:28:17 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-08-09 18:28:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_url] = wsURL.href;
|
|
|
|
|
|
|
|
if (
|
|
|
|
options.protocols.length !==
|
2023-04-02 13:41:41 -04:00
|
|
|
SetPrototypeGetSize(
|
2023-04-14 16:23:28 -04:00
|
|
|
new SafeSet(
|
2023-04-02 13:41:41 -04:00
|
|
|
ArrayPrototypeMap(
|
|
|
|
options.protocols,
|
|
|
|
(p) => StringPrototypeToLowerCase(p),
|
|
|
|
),
|
2022-04-04 05:49:50 -04:00
|
|
|
),
|
2023-04-02 13:41:41 -04:00
|
|
|
)
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Can't supply multiple times the same protocol.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const headers = headersFromHeaderList([], "request");
|
|
|
|
if (options.headers !== undefined) {
|
|
|
|
fillHeaders(headers, options.headers);
|
|
|
|
}
|
|
|
|
|
2023-12-24 08:04:32 -05:00
|
|
|
const cancelRid = op_ws_check_permission_and_cancel_handle(
|
2023-02-07 14:22:46 -05:00
|
|
|
"WebSocketStream.abort()",
|
|
|
|
this[_url],
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (options.signal?.aborted) {
|
|
|
|
core.close(cancelRid);
|
|
|
|
const err = options.signal.reason;
|
2023-10-11 01:31:05 -04:00
|
|
|
this[_opened].reject(err);
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_closed].reject(err);
|
|
|
|
} else {
|
|
|
|
const abort = () => {
|
|
|
|
core.close(cancelRid);
|
|
|
|
};
|
|
|
|
options.signal?.[add](abort);
|
|
|
|
PromisePrototypeThen(
|
2023-05-01 11:40:00 -04:00
|
|
|
op_ws_create(
|
2023-02-07 14:22:46 -05:00
|
|
|
"new WebSocketStream()",
|
|
|
|
this[_url],
|
|
|
|
options.protocols ? ArrayPrototypeJoin(options.protocols, ", ") : "",
|
|
|
|
cancelRid,
|
|
|
|
headerListFromHeaders(headers),
|
|
|
|
),
|
|
|
|
(create) => {
|
|
|
|
options.signal?.[remove](abort);
|
|
|
|
if (this[_earlyClose]) {
|
|
|
|
PromisePrototypeThen(
|
2023-05-01 11:40:00 -04:00
|
|
|
op_ws_close(create.rid),
|
2023-02-07 14:22:46 -05:00
|
|
|
() => {
|
|
|
|
PromisePrototypeThen(
|
|
|
|
(async () => {
|
|
|
|
while (true) {
|
2023-06-08 11:32:08 -04:00
|
|
|
const kind = await op_ws_next_event(create.rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-20 12:24:22 -04:00
|
|
|
if (kind > 5) {
|
2023-03-31 01:04:12 -04:00
|
|
|
/* close */
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})(),
|
|
|
|
() => {
|
2024-04-08 14:46:53 -04:00
|
|
|
const err = new WebSocketError("Closed while connecting");
|
2023-10-11 01:31:05 -04:00
|
|
|
this[_opened].reject(err);
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_closed].reject(err);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
() => {
|
2024-04-08 14:46:53 -04:00
|
|
|
const err = new WebSocketError("Closed while connecting");
|
2023-10-11 01:31:05 -04:00
|
|
|
this[_opened].reject(err);
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_closed].reject(err);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this[_rid] = create.rid;
|
|
|
|
|
|
|
|
const writable = new WritableStream({
|
|
|
|
write: async (chunk) => {
|
|
|
|
if (typeof chunk === "string") {
|
2023-06-06 09:58:18 -04:00
|
|
|
await op_ws_send_text_async(this[_rid], chunk);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (
|
2024-01-03 23:12:38 -05:00
|
|
|
TypedArrayPrototypeGetSymbolToStringTag(chunk) ===
|
|
|
|
"Uint8Array"
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
2023-06-06 09:58:18 -04:00
|
|
|
await op_ws_send_binary_async(this[_rid], chunk);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
|
|
|
throw new TypeError(
|
|
|
|
"A chunk may only be either a string or an Uint8Array",
|
2021-08-09 18:28:17 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
},
|
2024-04-08 14:46:53 -04:00
|
|
|
close: async () => {
|
|
|
|
this.close();
|
2023-02-07 14:22:46 -05:00
|
|
|
await this.closed;
|
|
|
|
},
|
|
|
|
abort: async (reason) => {
|
2024-04-08 14:46:53 -04:00
|
|
|
let closeCode = null;
|
|
|
|
let reasonString = "";
|
|
|
|
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(WebSocketErrorPrototype, reason)
|
|
|
|
) {
|
|
|
|
closeCode = reason.closeCode;
|
|
|
|
reasonString = reason.reason;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
2024-04-08 14:46:53 -04:00
|
|
|
this.close({
|
|
|
|
closeCode,
|
|
|
|
reason: reasonString,
|
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
} catch (_) {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
await this.closed;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const pull = async (controller) => {
|
2023-06-29 09:24:01 -04:00
|
|
|
// Remember that this pull method may be re-entered before it has completed
|
2023-06-08 11:32:08 -04:00
|
|
|
const kind = await op_ws_next_event(this[_rid]);
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (kind) {
|
2023-03-31 01:04:12 -04:00
|
|
|
case 0:
|
|
|
|
/* string */
|
2023-06-08 11:32:08 -04:00
|
|
|
controller.enqueue(op_ws_get_buffer_as_string(this[_rid]));
|
|
|
|
break;
|
|
|
|
case 1: {
|
2023-03-31 01:04:12 -04:00
|
|
|
/* binary */
|
2023-06-08 11:32:08 -04:00
|
|
|
controller.enqueue(op_ws_get_buffer(this[_rid]));
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
}
|
2023-04-26 06:07:38 -04:00
|
|
|
case 2: {
|
|
|
|
/* pong */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
2023-03-31 01:04:12 -04:00
|
|
|
/* error */
|
2024-04-08 14:46:53 -04:00
|
|
|
const err = new WebSocketError(op_ws_get_error(this[_rid]));
|
2023-03-31 01:04:12 -04:00
|
|
|
this[_closed].reject(err);
|
|
|
|
controller.error(err);
|
|
|
|
core.tryClose(this[_rid]);
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
}
|
2023-06-29 09:24:01 -04:00
|
|
|
case 1005: {
|
2023-03-31 01:04:12 -04:00
|
|
|
/* closed */
|
2024-04-08 14:46:53 -04:00
|
|
|
this[_closed].resolve({ closeCode: 1005, reason: "" });
|
2023-02-07 14:22:46 -05:00
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
break;
|
|
|
|
}
|
2023-03-31 01:04:12 -04:00
|
|
|
default: {
|
|
|
|
/* close */
|
2023-06-29 09:24:01 -04:00
|
|
|
const reason = op_ws_get_error(this[_rid]);
|
2023-03-31 01:04:12 -04:00
|
|
|
this[_closed].resolve({
|
2024-04-08 14:46:53 -04:00
|
|
|
closeCode: kind,
|
2023-06-29 09:24:01 -04:00
|
|
|
reason,
|
2023-03-31 01:04:12 -04:00
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
this[_closeSent].state === "fulfilled" &&
|
|
|
|
this[_closed].state === "pending"
|
|
|
|
) {
|
|
|
|
if (
|
2023-05-01 09:30:02 -04:00
|
|
|
DateNow() - await this[_closeSent].promise <=
|
2023-02-07 14:22:46 -05:00
|
|
|
CLOSE_RESPONSE_TIMEOUT
|
|
|
|
) {
|
|
|
|
return pull(controller);
|
|
|
|
}
|
|
|
|
|
2023-06-29 09:24:01 -04:00
|
|
|
const error = op_ws_get_error(this[_rid]);
|
2024-04-08 14:46:53 -04:00
|
|
|
this[_closed].reject(new WebSocketError(error));
|
2023-02-07 14:22:46 -05:00
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const readable = new ReadableStream({
|
|
|
|
start: (controller) => {
|
|
|
|
PromisePrototypeThen(this.closed, () => {
|
2021-08-09 18:28:17 -04:00
|
|
|
try {
|
2023-02-07 14:22:46 -05:00
|
|
|
controller.close();
|
2021-08-09 18:28:17 -04:00
|
|
|
} catch (_) {
|
2023-02-07 14:22:46 -05:00
|
|
|
// needed to ignore warnings & assertions
|
2021-08-09 18:28:17 -04:00
|
|
|
}
|
|
|
|
try {
|
2023-02-07 14:22:46 -05:00
|
|
|
PromisePrototypeCatch(
|
|
|
|
writableStreamClose(writable),
|
|
|
|
() => {},
|
|
|
|
);
|
2021-08-09 18:28:17 -04:00
|
|
|
} catch (_) {
|
2023-02-07 14:22:46 -05:00
|
|
|
// needed to ignore warnings & assertions
|
2021-08-09 18:28:17 -04:00
|
|
|
}
|
2024-08-21 17:00:23 -04:00
|
|
|
}, () => {
|
|
|
|
// needed to ignore warnings & assertions
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
2022-11-01 11:06:06 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
PromisePrototypeThen(this[_closeSent].promise, () => {
|
|
|
|
if (this[_closed].state === "pending") {
|
|
|
|
return pull(controller);
|
2022-07-18 16:49:49 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
pull,
|
|
|
|
cancel: async (reason) => {
|
2024-04-08 14:46:53 -04:00
|
|
|
let closeCode = null;
|
|
|
|
let reasonString = "";
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(WebSocketErrorPrototype, reason)
|
|
|
|
) {
|
|
|
|
closeCode = reason.closeCode;
|
|
|
|
reasonString = reason.reason;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
2024-04-08 14:46:53 -04:00
|
|
|
this.close({
|
|
|
|
closeCode,
|
|
|
|
reason: reasonString,
|
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
} catch (_) {
|
|
|
|
this.close();
|
2022-07-18 16:49:49 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
await this.closed;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-10-11 01:31:05 -04:00
|
|
|
this[_opened].resolve({
|
2023-02-07 14:22:46 -05:00
|
|
|
readable,
|
|
|
|
writable,
|
|
|
|
extensions: create.extensions ?? "",
|
|
|
|
protocol: create.protocol ?? "",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(core.InterruptedPrototype, err)) {
|
|
|
|
// The signal was aborted.
|
|
|
|
err = options.signal.reason;
|
|
|
|
} else {
|
|
|
|
core.tryClose(cancelRid);
|
2024-04-08 14:46:53 -04:00
|
|
|
err = new WebSocketError(err.message);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-10-11 01:31:05 -04:00
|
|
|
this[_opened].reject(err);
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_closed].reject(err);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 21:43:17 -04:00
|
|
|
|
2023-10-11 01:31:05 -04:00
|
|
|
[_opened] = new Deferred();
|
|
|
|
get opened() {
|
2023-02-07 14:22:46 -05:00
|
|
|
webidl.assertBranded(this, WebSocketStreamPrototype);
|
2023-10-11 01:31:05 -04:00
|
|
|
return this[_opened].promise;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-08-29 21:43:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[_earlyClose] = false;
|
|
|
|
[_closed] = new Deferred();
|
|
|
|
[_closeSent] = new Deferred();
|
|
|
|
get closed() {
|
|
|
|
webidl.assertBranded(this, WebSocketStreamPrototype);
|
|
|
|
return this[_closed].promise;
|
|
|
|
}
|
2022-08-29 21:43:17 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close(closeInfo) {
|
|
|
|
webidl.assertBranded(this, WebSocketStreamPrototype);
|
2023-05-01 06:47:13 -04:00
|
|
|
closeInfo = webidl.converters.WebSocketCloseInfo(
|
|
|
|
closeInfo,
|
|
|
|
"Failed to execute 'close' on 'WebSocketStream'",
|
|
|
|
"Argument 1",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2024-04-08 14:46:53 -04:00
|
|
|
validateCloseCodeAndReason(closeInfo);
|
2021-08-09 18:28:17 -04:00
|
|
|
|
2024-04-08 14:46:53 -04:00
|
|
|
if (closeInfo.reason && closeInfo.closeCode === null) {
|
|
|
|
closeInfo.closeCode = 1000;
|
2021-08-09 18:28:17 -04:00
|
|
|
}
|
|
|
|
|
2023-10-11 01:31:05 -04:00
|
|
|
if (this[_opened].state === "pending") {
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_earlyClose] = true;
|
|
|
|
} else if (this[_closed].state === "pending") {
|
|
|
|
PromisePrototypeThen(
|
2024-04-08 14:46:53 -04:00
|
|
|
op_ws_close(this[_rid], closeInfo.closeCode, closeInfo.reason),
|
2023-02-07 14:22:46 -05:00
|
|
|
() => {
|
|
|
|
setTimeout(() => {
|
2023-05-01 09:30:02 -04:00
|
|
|
this[_closeSent].resolve(DateNow());
|
2023-02-07 14:22:46 -05:00
|
|
|
}, 0);
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
this[_rid] && core.tryClose(this[_rid]);
|
2024-04-08 14:46:53 -04:00
|
|
|
err = new WebSocketError(err.message);
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_closed].reject(err);
|
|
|
|
},
|
|
|
|
);
|
2021-08-09 18:28:17 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-08-09 18:28:17 -04:00
|
|
|
|
2023-11-19 03:13:38 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(WebSocketStreamPrototype, this),
|
|
|
|
keys: [
|
|
|
|
"closed",
|
|
|
|
"opened",
|
|
|
|
"url",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
2021-08-09 18:28:17 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
const WebSocketStreamPrototype = WebSocketStream.prototype;
|
2022-02-01 12:06:11 -05:00
|
|
|
|
2024-04-08 14:46:53 -04:00
|
|
|
function validateCloseCodeAndReason(closeInfo) {
|
|
|
|
if (!closeInfo.closeCode) {
|
|
|
|
closeInfo.closeCode = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
closeInfo.closeCode &&
|
|
|
|
!(closeInfo.closeCode === 1000 ||
|
|
|
|
(3000 <= closeInfo.closeCode && closeInfo.closeCode < 5000))
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"The close code must be either 1000 or in the range of 3000 to 4999.",
|
|
|
|
"InvalidAccessError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
if (
|
|
|
|
closeInfo.reason &&
|
|
|
|
TypedArrayPrototypeGetByteLength(encoder.encode(closeInfo.reason)) > 123
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"The close reason may not be longer than 123 bytes.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class WebSocketError extends DOMException {
|
|
|
|
#closeCode;
|
|
|
|
#reason;
|
|
|
|
|
2024-05-22 18:03:35 -04:00
|
|
|
constructor(message = "", init = { __proto__: null }) {
|
2024-04-08 14:46:53 -04:00
|
|
|
super(message, "WebSocketError");
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
|
|
|
|
init = webidl.converters["WebSocketCloseInfo"](
|
|
|
|
init,
|
|
|
|
"Failed to construct 'WebSocketError'",
|
|
|
|
"Argument 2",
|
|
|
|
);
|
|
|
|
|
|
|
|
validateCloseCodeAndReason(init);
|
|
|
|
|
|
|
|
if (init.reason && init.closeCode === null) {
|
|
|
|
init.closeCode = 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#closeCode = init.closeCode;
|
|
|
|
this.#reason = init.reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
get closeCode() {
|
|
|
|
webidl.assertBranded(this, WebSocketErrorPrototype);
|
|
|
|
return this.#closeCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
get reason() {
|
|
|
|
webidl.assertBranded(this, WebSocketErrorPrototype);
|
|
|
|
return this.#reason;
|
|
|
|
}
|
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(WebSocketErrorPrototype, this),
|
|
|
|
keys: [
|
|
|
|
"message",
|
|
|
|
"name",
|
|
|
|
"closeCode",
|
|
|
|
"reason",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
webidl.configureInterface(WebSocketError);
|
|
|
|
const WebSocketErrorPrototype = WebSocketError.prototype;
|
|
|
|
|
|
|
|
export { WebSocketError, WebSocketStream };
|