2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2021-07-04 11:38:04 -04:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
isAnyArrayBuffer,
|
|
|
|
isArrayBuffer,
|
|
|
|
} = core;
|
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_buffered_amount,
|
|
|
|
op_ws_get_error,
|
|
|
|
op_ws_next_event,
|
|
|
|
op_ws_send_binary,
|
|
|
|
op_ws_send_binary_ab,
|
|
|
|
op_ws_send_ping,
|
|
|
|
op_ws_send_text,
|
2024-01-26 17:46:46 -05:00
|
|
|
} from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
ArrayBufferIsView,
|
|
|
|
ArrayPrototypeJoin,
|
|
|
|
ArrayPrototypeMap,
|
2024-06-08 12:36:43 -04:00
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypeShift,
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypeSome,
|
2024-11-12 06:40:07 -05:00
|
|
|
Error,
|
2023-02-07 14:22:46 -05:00
|
|
|
ErrorPrototypeToString,
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2024-01-03 23:12:38 -05:00
|
|
|
PromisePrototypeCatch,
|
2023-02-07 14:22:46 -05:00
|
|
|
PromisePrototypeThen,
|
2023-06-05 04:52:40 -04:00
|
|
|
RegExpPrototypeExec,
|
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
|
|
|
String,
|
|
|
|
StringPrototypeEndsWith,
|
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
2024-01-03 23:12:38 -05:00
|
|
|
SymbolIterator,
|
2023-04-02 13:41:41 -04:00
|
|
|
TypedArrayPrototypeGetByteLength,
|
2023-02-07 14:22:46 -05:00
|
|
|
} = primordials;
|
2024-01-10 17:37:25 -05:00
|
|
|
|
|
|
|
import { URL } from "ext:deno_url/00_url.js";
|
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
|
|
|
|
import { HTTP_TOKEN_CODE_POINT_RE } from "ext:deno_web/00_infra.js";
|
|
|
|
import { DOMException } from "ext:deno_web/01_dom_exception.js";
|
2024-06-20 01:02:51 -04:00
|
|
|
import { clearTimeout, setTimeout } from "ext:deno_web/02_timers.js";
|
2023-12-24 08:04:32 -05:00
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
CloseEvent,
|
|
|
|
defineEventHandler,
|
|
|
|
dispatch,
|
|
|
|
ErrorEvent,
|
|
|
|
Event,
|
|
|
|
EventTarget,
|
|
|
|
MessageEvent,
|
|
|
|
setIsTrusted,
|
|
|
|
} from "ext:deno_web/02_event.js";
|
|
|
|
import { Blob, BlobPrototype } from "ext:deno_web/09_file.js";
|
|
|
|
import { getLocationHref } from "ext:deno_web/12_location.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-05-01 06:47:13 -04:00
|
|
|
webidl.converters["sequence<DOMString> or DOMString"] = (
|
|
|
|
V,
|
|
|
|
prefix,
|
|
|
|
context,
|
|
|
|
opts,
|
|
|
|
) => {
|
2023-02-07 14:22:46 -05:00
|
|
|
// Union for (sequence<DOMString> or DOMString)
|
|
|
|
if (webidl.type(V) === "Object" && V !== null) {
|
|
|
|
if (V[SymbolIterator] !== undefined) {
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["sequence<DOMString>"](V, prefix, context, opts);
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters.DOMString(V, prefix, context, opts);
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-05-01 06:47:13 -04:00
|
|
|
webidl.converters["WebSocketSend"] = (V, prefix, context, opts) => {
|
2023-02-07 14:22:46 -05:00
|
|
|
// Union for (Blob or ArrayBufferView or ArrayBuffer or USVString)
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(BlobPrototype, V)) {
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["Blob"](V, prefix, context, opts);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (typeof V === "object") {
|
2024-01-03 23:12:38 -05:00
|
|
|
if (isAnyArrayBuffer(V)) {
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["ArrayBuffer"](V, prefix, context, opts);
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (ArrayBufferIsView(V)) {
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["ArrayBufferView"](V, prefix, context, opts);
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-05-01 06:47:13 -04:00
|
|
|
return webidl.converters["USVString"](V, prefix, context, opts);
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
|
|
|
|
2023-04-06 09:14:31 -04:00
|
|
|
/** role */
|
|
|
|
const SERVER = 0;
|
|
|
|
const CLIENT = 1;
|
|
|
|
|
|
|
|
/** state */
|
2023-02-07 14:22:46 -05:00
|
|
|
const CONNECTING = 0;
|
|
|
|
const OPEN = 1;
|
|
|
|
const CLOSING = 2;
|
|
|
|
const CLOSED = 3;
|
|
|
|
|
|
|
|
const _readyState = Symbol("[[readyState]]");
|
|
|
|
const _url = Symbol("[[url]]");
|
|
|
|
const _rid = Symbol("[[rid]]");
|
2023-04-06 09:14:31 -04:00
|
|
|
const _role = Symbol("[[role]]");
|
2023-02-07 14:22:46 -05:00
|
|
|
const _extensions = Symbol("[[extensions]]");
|
|
|
|
const _protocol = Symbol("[[protocol]]");
|
|
|
|
const _binaryType = Symbol("[[binaryType]]");
|
|
|
|
const _eventLoop = Symbol("[[eventLoop]]");
|
2024-06-08 12:36:43 -04:00
|
|
|
const _sendQueue = Symbol("[[sendQueue]]");
|
|
|
|
const _queueSend = Symbol("[[queueSend]]");
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
const _server = Symbol("[[server]]");
|
|
|
|
const _idleTimeoutDuration = Symbol("[[idleTimeout]]");
|
|
|
|
const _idleTimeoutTimeout = Symbol("[[idleTimeoutTimeout]]");
|
|
|
|
const _serverHandleIdleTimeout = Symbol("[[serverHandleIdleTimeout]]");
|
2024-06-08 12:36:43 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class WebSocket extends EventTarget {
|
|
|
|
constructor(url, protocols = []) {
|
|
|
|
super();
|
|
|
|
this[webidl.brand] = webidl.brand;
|
2023-06-07 05:54:49 -04:00
|
|
|
this[_rid] = undefined;
|
|
|
|
this[_role] = undefined;
|
|
|
|
this[_readyState] = CONNECTING;
|
|
|
|
this[_extensions] = "";
|
|
|
|
this[_protocol] = "";
|
|
|
|
this[_url] = "";
|
|
|
|
this[_binaryType] = "blob";
|
|
|
|
this[_idleTimeoutDuration] = 0;
|
|
|
|
this[_idleTimeoutTimeout] = undefined;
|
2024-06-08 12:36:43 -04:00
|
|
|
this[_sendQueue] = [];
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const prefix = "Failed to construct 'WebSocket'";
|
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");
|
2023-02-07 14:22:46 -05:00
|
|
|
protocols = webidl.converters["sequence<DOMString> or DOMString"](
|
|
|
|
protocols,
|
2023-05-01 06:47:13 -04:00
|
|
|
prefix,
|
|
|
|
"Argument 2",
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let wsURL;
|
|
|
|
|
|
|
|
try {
|
2023-07-28 02:29:41 -04:00
|
|
|
wsURL = new URL(url, getLocationHref());
|
2023-02-07 14:22:46 -05:00
|
|
|
} catch (e) {
|
|
|
|
throw new DOMException(e.message, "SyntaxError");
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
|
|
|
|
2023-07-28 02:29:41 -04:00
|
|
|
if (wsURL.protocol === "http:") {
|
|
|
|
wsURL.protocol = "ws:";
|
|
|
|
} else if (wsURL.protocol === "https:") {
|
|
|
|
wsURL.protocol = "wss:";
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (wsURL.protocol !== "ws:" && wsURL.protocol !== "wss:") {
|
|
|
|
throw new DOMException(
|
2024-09-17 18:32:12 -04:00
|
|
|
`Only ws & wss schemes are allowed in a WebSocket URL: received ${wsURL.protocol}`,
|
2023-02-07 14:22:46 -05:00
|
|
|
"SyntaxError",
|
|
|
|
);
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (wsURL.hash !== "" || StringPrototypeEndsWith(wsURL.href, "#")) {
|
|
|
|
throw new DOMException(
|
2024-09-17 18:32:12 -04:00
|
|
|
"Fragments are not allowed in a WebSocket URL",
|
2023-02-07 14:22:46 -05:00
|
|
|
"SyntaxError",
|
|
|
|
);
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
this[_url] = wsURL.href;
|
2023-04-06 09:14:31 -04:00
|
|
|
this[_role] = CLIENT;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-27 06:47:52 -04:00
|
|
|
op_ws_check_permission_and_cancel_handle(
|
2023-02-07 14:22:46 -05:00
|
|
|
"WebSocket.abort()",
|
|
|
|
this[_url],
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (typeof protocols === "string") {
|
|
|
|
protocols = [protocols];
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (
|
|
|
|
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(protocols, (p) => StringPrototypeToLowerCase(p)),
|
|
|
|
),
|
|
|
|
)
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
|
|
|
throw new DOMException(
|
2024-09-17 18:32:12 -04:00
|
|
|
"Cannot supply multiple times the same protocol",
|
2023-02-07 14:22:46 -05:00
|
|
|
"SyntaxError",
|
|
|
|
);
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (
|
|
|
|
ArrayPrototypeSome(
|
2021-06-21 06:15:08 -04:00
|
|
|
protocols,
|
2023-06-05 04:52:40 -04:00
|
|
|
(protocol) =>
|
|
|
|
RegExpPrototypeExec(HTTP_TOKEN_CODE_POINT_RE, protocol) === null,
|
2023-02-07 14:22:46 -05:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
2024-09-17 18:32:12 -04:00
|
|
|
"Invalid protocol value",
|
2023-02-07 14:22:46 -05:00
|
|
|
"SyntaxError",
|
2021-06-21 06:15:08 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
PromisePrototypeThen(
|
2023-05-01 11:40:00 -04:00
|
|
|
op_ws_create(
|
2023-02-07 14:22:46 -05:00
|
|
|
"new WebSocket()",
|
|
|
|
wsURL.href,
|
|
|
|
ArrayPrototypeJoin(protocols, ", "),
|
|
|
|
),
|
|
|
|
(create) => {
|
|
|
|
this[_rid] = create.rid;
|
|
|
|
this[_extensions] = create.extensions;
|
|
|
|
this[_protocol] = create.protocol;
|
|
|
|
|
|
|
|
if (this[_readyState] === CLOSING) {
|
|
|
|
PromisePrototypeThen(
|
2023-05-01 11:40:00 -04:00
|
|
|
op_ws_close(this[_rid]),
|
2023-02-07 14:22:46 -05:00
|
|
|
() => {
|
|
|
|
this[_readyState] = CLOSED;
|
|
|
|
|
|
|
|
const errEvent = new ErrorEvent("error");
|
|
|
|
this.dispatchEvent(errEvent);
|
|
|
|
|
|
|
|
const event = new CloseEvent("close");
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this[_readyState] = OPEN;
|
|
|
|
const event = new Event("open");
|
|
|
|
this.dispatchEvent(event);
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_eventLoop]();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
this[_readyState] = CLOSED;
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const errorEv = new ErrorEvent(
|
|
|
|
"error",
|
|
|
|
{ error: err, message: ErrorPrototypeToString(err) },
|
2020-09-05 10:39:25 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
this.dispatchEvent(errorEv);
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const closeEv = new CloseEvent("close");
|
|
|
|
this.dispatchEvent(closeEv);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-11-25 09:17:46 -05:00
|
|
|
|
2023-06-07 05:54:49 -04:00
|
|
|
get readyState() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_readyState];
|
|
|
|
}
|
|
|
|
|
|
|
|
get CONNECTING() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return CONNECTING;
|
|
|
|
}
|
|
|
|
get OPEN() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return OPEN;
|
|
|
|
}
|
|
|
|
get CLOSING() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return CLOSING;
|
|
|
|
}
|
|
|
|
get CLOSED() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
get extensions() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_extensions];
|
|
|
|
}
|
|
|
|
|
|
|
|
get protocol() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_protocol];
|
|
|
|
}
|
|
|
|
|
|
|
|
get url() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_url];
|
|
|
|
}
|
|
|
|
|
|
|
|
get binaryType() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_binaryType];
|
|
|
|
}
|
|
|
|
set binaryType(value) {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
value = webidl.converters.DOMString(
|
|
|
|
value,
|
|
|
|
"Failed to set 'binaryType' on 'WebSocket'",
|
|
|
|
);
|
|
|
|
if (value === "blob" || value === "arraybuffer") {
|
|
|
|
this[_binaryType] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get bufferedAmount() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
if (this[_readyState] === OPEN) {
|
|
|
|
return op_ws_get_buffered_amount(this[_rid]);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
send(data) {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
const prefix = "Failed to execute 'send' on 'WebSocket'";
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
data = webidl.converters.WebSocketSend(data, prefix, "Argument 1");
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (this[_readyState] !== OPEN) {
|
2024-09-17 18:32:12 -04:00
|
|
|
throw new DOMException("'readyState' not OPEN", "InvalidStateError");
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2024-06-08 12:36:43 -04:00
|
|
|
if (this[_sendQueue].length === 0) {
|
|
|
|
// Fast path if the send queue is empty, for example when only synchronous
|
|
|
|
// data is being sent.
|
|
|
|
if (ArrayBufferIsView(data)) {
|
|
|
|
op_ws_send_binary(this[_rid], data);
|
|
|
|
} else if (isArrayBuffer(data)) {
|
|
|
|
op_ws_send_binary_ab(this[_rid], data);
|
|
|
|
} else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, data)) {
|
|
|
|
this[_queueSend](data);
|
|
|
|
} else {
|
|
|
|
const string = String(data);
|
|
|
|
op_ws_send_text(
|
|
|
|
this[_rid],
|
|
|
|
string,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2024-06-08 12:36:43 -04:00
|
|
|
// Slower path if the send queue is not empty, for example when sending
|
|
|
|
// asynchronous data like a Blob.
|
|
|
|
this[_queueSend](data);
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close(code = undefined, reason = undefined) {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
const prefix = "Failed to execute 'close' on 'WebSocket'";
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (code !== undefined) {
|
2023-05-01 06:47:13 -04:00
|
|
|
code = webidl.converters["unsigned short"](code, prefix, "Argument 1", {
|
2023-02-07 14:22:46 -05:00
|
|
|
clamp: true,
|
2021-06-21 06:15:08 -04:00
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (reason !== undefined) {
|
2023-05-01 06:47:13 -04:00
|
|
|
reason = webidl.converters.USVString(reason, prefix, "Argument 2");
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!this[_server]) {
|
|
|
|
if (
|
|
|
|
code !== undefined &&
|
|
|
|
!(code === 1000 || (3000 <= code && code < 5000))
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
2024-09-17 18:32:12 -04:00
|
|
|
`The close code must be either 1000 or in the range of 3000 to 4999: received ${code}`,
|
2023-02-07 14:22:46 -05:00
|
|
|
"InvalidAccessError",
|
2021-07-04 11:38:04 -04:00
|
|
|
);
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 13:41:41 -04:00
|
|
|
if (
|
|
|
|
reason !== undefined &&
|
|
|
|
TypedArrayPrototypeGetByteLength(core.encode(reason)) > 123
|
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new DOMException(
|
2024-09-17 18:32:12 -04:00
|
|
|
"The close reason may not be longer than 123 bytes",
|
2023-02-07 14:22:46 -05:00
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (this[_readyState] === CONNECTING) {
|
|
|
|
this[_readyState] = CLOSING;
|
|
|
|
} else if (this[_readyState] === OPEN) {
|
|
|
|
this[_readyState] = CLOSING;
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
PromisePrototypeCatch(
|
2023-05-01 11:40:00 -04:00
|
|
|
op_ws_close(
|
2023-04-06 09:14:31 -04:00
|
|
|
this[_rid],
|
|
|
|
code,
|
|
|
|
reason,
|
|
|
|
),
|
2023-02-07 14:22:46 -05:00
|
|
|
(err) => {
|
|
|
|
this[_readyState] = CLOSED;
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const errorEv = new ErrorEvent("error", {
|
|
|
|
error: err,
|
|
|
|
message: ErrorPrototypeToString(err),
|
|
|
|
});
|
|
|
|
this.dispatchEvent(errorEv);
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const closeEv = new CloseEvent("close");
|
|
|
|
this.dispatchEvent(closeEv);
|
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
},
|
|
|
|
);
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async [_eventLoop]() {
|
2023-06-08 11:32:08 -04:00
|
|
|
const rid = this[_rid];
|
2023-02-07 14:22:46 -05:00
|
|
|
while (this[_readyState] !== CLOSED) {
|
2023-06-08 11:32:08 -04:00
|
|
|
const kind = await op_ws_next_event(rid);
|
2024-06-25 09:39:02 -04:00
|
|
|
/* close the connection if read was cancelled, and we didn't get a close frame */
|
|
|
|
if (
|
|
|
|
(this[_readyState] == CLOSING) &&
|
|
|
|
kind <= 3 && this[_role] !== CLIENT
|
|
|
|
) {
|
|
|
|
this[_readyState] = CLOSED;
|
|
|
|
|
|
|
|
const event = new CloseEvent("close");
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
core.tryClose(rid);
|
|
|
|
break;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
switch (kind) {
|
2023-03-31 01:04:12 -04:00
|
|
|
case 0: {
|
|
|
|
/* string */
|
2024-03-08 23:06:17 -05:00
|
|
|
const data = op_ws_get_buffer_as_string(rid);
|
|
|
|
if (data === undefined) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_serverHandleIdleTimeout]();
|
|
|
|
const event = new MessageEvent("message", {
|
2024-03-08 23:06:17 -05:00
|
|
|
data,
|
2023-02-07 14:22:46 -05:00
|
|
|
origin: this[_url],
|
|
|
|
});
|
2023-07-31 17:22:07 -04:00
|
|
|
setIsTrusted(event, true);
|
2023-04-30 06:24:56 -04:00
|
|
|
dispatch(this, event);
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
}
|
2023-03-31 01:04:12 -04:00
|
|
|
case 1: {
|
|
|
|
/* binary */
|
2024-03-08 23:06:17 -05:00
|
|
|
const d = op_ws_get_buffer(rid);
|
|
|
|
if (d == undefined) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_serverHandleIdleTimeout]();
|
2023-06-08 11:32:08 -04:00
|
|
|
// deno-lint-ignore prefer-primordials
|
2024-03-08 23:06:17 -05:00
|
|
|
const buffer = d.buffer;
|
2023-02-07 14:22:46 -05:00
|
|
|
let data;
|
|
|
|
if (this.binaryType === "blob") {
|
2023-06-08 11:32:08 -04:00
|
|
|
data = new Blob([buffer]);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-06-08 11:32:08 -04:00
|
|
|
data = buffer;
|
2021-01-10 14:05:24 -05:00
|
|
|
}
|
2021-04-22 19:31:34 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const event = new MessageEvent("message", {
|
|
|
|
data,
|
|
|
|
origin: this[_url],
|
|
|
|
});
|
2023-07-31 17:22:07 -04:00
|
|
|
setIsTrusted(event, true);
|
2023-04-30 06:24:56 -04:00
|
|
|
dispatch(this, event);
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
}
|
2023-03-31 01:04:12 -04:00
|
|
|
case 2: {
|
|
|
|
/* pong */
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_serverHandleIdleTimeout]();
|
|
|
|
break;
|
|
|
|
}
|
2023-04-26 06:07:38 -04:00
|
|
|
case 3: {
|
2023-03-31 01:04:12 -04:00
|
|
|
/* error */
|
|
|
|
this[_readyState] = CLOSED;
|
|
|
|
|
2024-11-12 06:40:07 -05:00
|
|
|
const message = op_ws_get_error(rid);
|
|
|
|
const error = new Error(message);
|
2023-03-31 01:04:12 -04:00
|
|
|
const errorEv = new ErrorEvent("error", {
|
2024-11-12 06:40:07 -05:00
|
|
|
error,
|
|
|
|
message,
|
2023-03-31 01:04:12 -04:00
|
|
|
});
|
|
|
|
this.dispatchEvent(errorEv);
|
|
|
|
|
|
|
|
const closeEv = new CloseEvent("close");
|
|
|
|
this.dispatchEvent(closeEv);
|
2023-06-08 11:32:08 -04:00
|
|
|
core.tryClose(rid);
|
2023-03-31 01:04:12 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
/* close */
|
|
|
|
const code = kind;
|
2023-06-29 09:24:01 -04:00
|
|
|
const reason = code == 1005 ? "" : op_ws_get_error(rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
const prevState = this[_readyState];
|
|
|
|
this[_readyState] = CLOSED;
|
|
|
|
clearTimeout(this[_idleTimeoutTimeout]);
|
|
|
|
|
|
|
|
if (prevState === OPEN) {
|
|
|
|
try {
|
2023-05-01 11:40:00 -04:00
|
|
|
await op_ws_close(
|
2023-06-08 11:32:08 -04:00
|
|
|
rid,
|
2023-03-31 01:04:12 -04:00
|
|
|
code,
|
2023-06-29 09:24:01 -04:00
|
|
|
reason,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
} catch {
|
|
|
|
// ignore failures
|
2021-04-22 19:31:34 -04:00
|
|
|
}
|
|
|
|
}
|
2022-05-23 07:21:11 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const event = new CloseEvent("close", {
|
|
|
|
wasClean: true,
|
2023-03-31 01:04:12 -04:00
|
|
|
code: code,
|
2023-06-29 09:24:01 -04:00
|
|
|
reason,
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
2023-06-08 11:32:08 -04:00
|
|
|
core.tryClose(rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
2022-01-06 11:41:16 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-05-23 07:21:11 -04:00
|
|
|
|
2024-06-08 12:36:43 -04:00
|
|
|
async [_queueSend](data) {
|
|
|
|
const queue = this[_sendQueue];
|
|
|
|
|
|
|
|
ArrayPrototypePush(queue, data);
|
|
|
|
|
|
|
|
if (queue.length > 1) {
|
|
|
|
// There is already a send in progress, so we just push to the queue
|
|
|
|
// and let that task handle sending of this data.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (queue.length > 0) {
|
|
|
|
const data = queue[0];
|
|
|
|
if (ArrayBufferIsView(data)) {
|
|
|
|
op_ws_send_binary(this[_rid], data);
|
|
|
|
} else if (isArrayBuffer(data)) {
|
|
|
|
op_ws_send_binary_ab(this[_rid], data);
|
|
|
|
} else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, data)) {
|
|
|
|
// deno-lint-ignore prefer-primordials
|
|
|
|
const ab = await data.slice().arrayBuffer();
|
|
|
|
op_ws_send_binary_ab(this[_rid], ab);
|
|
|
|
} else {
|
|
|
|
const string = String(data);
|
|
|
|
op_ws_send_text(
|
|
|
|
this[_rid],
|
|
|
|
string,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
ArrayPrototypeShift(queue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[_serverHandleIdleTimeout]() {
|
|
|
|
if (this[_idleTimeoutDuration]) {
|
|
|
|
clearTimeout(this[_idleTimeoutTimeout]);
|
|
|
|
this[_idleTimeoutTimeout] = setTimeout(async () => {
|
|
|
|
if (this[_readyState] === OPEN) {
|
2024-01-09 09:26:54 -05:00
|
|
|
await PromisePrototypeCatch(op_ws_send_ping(this[_rid]), () => {});
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_idleTimeoutTimeout] = setTimeout(async () => {
|
|
|
|
if (this[_readyState] === OPEN) {
|
|
|
|
this[_readyState] = CLOSING;
|
|
|
|
const reason = "No response from ping frame.";
|
2024-01-09 09:26:54 -05:00
|
|
|
await PromisePrototypeCatch(
|
|
|
|
op_ws_close(this[_rid], 1001, reason),
|
|
|
|
() => {},
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_readyState] = CLOSED;
|
|
|
|
|
|
|
|
const errEvent = new ErrorEvent("error", {
|
|
|
|
message: reason,
|
|
|
|
});
|
|
|
|
this.dispatchEvent(errEvent);
|
|
|
|
|
|
|
|
const event = new CloseEvent("close", {
|
|
|
|
wasClean: false,
|
|
|
|
code: 1001,
|
|
|
|
reason,
|
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
} else {
|
|
|
|
clearTimeout(this[_idleTimeoutTimeout]);
|
|
|
|
}
|
|
|
|
}, (this[_idleTimeoutDuration] / 2) * 1000);
|
|
|
|
} else {
|
|
|
|
clearTimeout(this[_idleTimeoutTimeout]);
|
|
|
|
}
|
|
|
|
}, (this[_idleTimeoutDuration] / 2) * 1000);
|
2022-05-23 07:21:11 -04:00
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
2023-11-19 03:13:38 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(WebSocketPrototype, this),
|
|
|
|
keys: [
|
|
|
|
"url",
|
|
|
|
"readyState",
|
|
|
|
"extensions",
|
|
|
|
"protocol",
|
|
|
|
"binaryType",
|
|
|
|
"bufferedAmount",
|
|
|
|
"onmessage",
|
|
|
|
"onerror",
|
|
|
|
"onclose",
|
|
|
|
"onopen",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectDefineProperties(WebSocket, {
|
|
|
|
CONNECTING: {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-07 14:22:46 -05:00
|
|
|
value: 0,
|
|
|
|
},
|
|
|
|
OPEN: {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-07 14:22:46 -05:00
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
CLOSING: {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-07 14:22:46 -05:00
|
|
|
value: 2,
|
|
|
|
},
|
|
|
|
CLOSED: {
|
2024-09-06 06:52:59 -04:00
|
|
|
__proto__: null,
|
2023-02-07 14:22:46 -05:00
|
|
|
value: 3,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
defineEventHandler(WebSocket.prototype, "message");
|
|
|
|
defineEventHandler(WebSocket.prototype, "error");
|
|
|
|
defineEventHandler(WebSocket.prototype, "close");
|
|
|
|
defineEventHandler(WebSocket.prototype, "open");
|
|
|
|
|
2023-10-09 23:01:01 -04:00
|
|
|
webidl.configureInterface(WebSocket);
|
2023-02-07 14:22:46 -05:00
|
|
|
const WebSocketPrototype = WebSocket.prototype;
|
|
|
|
|
2023-12-13 09:21:34 -05:00
|
|
|
function createWebSocketBranded() {
|
|
|
|
const socket = webidl.createBranded(WebSocket);
|
|
|
|
socket[_rid] = undefined;
|
|
|
|
socket[_role] = undefined;
|
|
|
|
socket[_readyState] = CONNECTING;
|
|
|
|
socket[_extensions] = "";
|
|
|
|
socket[_protocol] = "";
|
|
|
|
socket[_url] = "";
|
2024-01-02 00:00:09 -05:00
|
|
|
// We use ArrayBuffer for server websockets for backwards compatibility
|
|
|
|
// and performance reasons.
|
|
|
|
//
|
|
|
|
// https://github.com/denoland/deno/issues/15340#issuecomment-1872353134
|
|
|
|
socket[_binaryType] = "arraybuffer";
|
2023-12-13 09:21:34 -05:00
|
|
|
socket[_idleTimeoutDuration] = 0;
|
|
|
|
socket[_idleTimeoutTimeout] = undefined;
|
2024-06-08 12:36:43 -04:00
|
|
|
socket[_sendQueue] = [];
|
2023-12-13 09:21:34 -05:00
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export {
|
|
|
|
_eventLoop,
|
|
|
|
_idleTimeoutDuration,
|
|
|
|
_idleTimeoutTimeout,
|
|
|
|
_protocol,
|
|
|
|
_readyState,
|
|
|
|
_rid,
|
2023-04-06 09:14:31 -04:00
|
|
|
_role,
|
2023-02-07 14:22:46 -05:00
|
|
|
_server,
|
|
|
|
_serverHandleIdleTimeout,
|
2023-12-13 09:21:34 -05:00
|
|
|
createWebSocketBranded,
|
2023-04-06 09:14:31 -04:00
|
|
|
SERVER,
|
2023-02-07 14:22:46 -05:00
|
|
|
WebSocket,
|
|
|
|
};
|