2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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-02-07 14:22:46 -05:00
|
|
|
const core = globalThis.Deno.core;
|
|
|
|
const ops = core.ops;
|
2023-03-08 06:44:54 -05:00
|
|
|
import { URL } from "ext:deno_url/00_url.js";
|
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
|
|
|
import { HTTP_TOKEN_CODE_POINT_RE } from "ext:deno_web/00_infra.js";
|
|
|
|
import DOMException from "ext:deno_web/01_dom_exception.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
_skipInternalInit,
|
|
|
|
CloseEvent,
|
|
|
|
defineEventHandler,
|
|
|
|
ErrorEvent,
|
|
|
|
Event,
|
|
|
|
EventTarget,
|
|
|
|
MessageEvent,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_web/02_event.js";
|
|
|
|
import { Blob, BlobPrototype } from "ext:deno_web/09_file.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
const primordials = globalThis.__bootstrap.primordials;
|
|
|
|
const {
|
|
|
|
ArrayBufferPrototype,
|
|
|
|
ArrayBufferIsView,
|
|
|
|
ArrayPrototypeJoin,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ArrayPrototypeSome,
|
|
|
|
DataView,
|
|
|
|
ErrorPrototypeToString,
|
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
PromisePrototypeThen,
|
|
|
|
RegExpPrototypeTest,
|
|
|
|
Set,
|
|
|
|
// TODO(lucacasonato): add SharedArrayBuffer to primordials
|
|
|
|
// SharedArrayBufferPrototype
|
|
|
|
String,
|
|
|
|
StringPrototypeEndsWith,
|
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
Symbol,
|
|
|
|
SymbolIterator,
|
|
|
|
PromisePrototypeCatch,
|
|
|
|
SymbolFor,
|
|
|
|
} = primordials;
|
|
|
|
|
|
|
|
webidl.converters["sequence<DOMString> or DOMString"] = (V, opts) => {
|
|
|
|
// Union for (sequence<DOMString> or DOMString)
|
|
|
|
if (webidl.type(V) === "Object" && V !== null) {
|
|
|
|
if (V[SymbolIterator] !== undefined) {
|
|
|
|
return webidl.converters["sequence<DOMString>"](V, opts);
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
return webidl.converters.DOMString(V, opts);
|
|
|
|
};
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
webidl.converters["WebSocketSend"] = (V, opts) => {
|
|
|
|
// Union for (Blob or ArrayBufferView or ArrayBuffer or USVString)
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(BlobPrototype, V)) {
|
|
|
|
return webidl.converters["Blob"](V, opts);
|
|
|
|
}
|
|
|
|
if (typeof V === "object") {
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V) ||
|
|
|
|
// deno-lint-ignore prefer-primordials
|
|
|
|
ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V)
|
|
|
|
) {
|
|
|
|
return webidl.converters["ArrayBuffer"](V, opts);
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (ArrayBufferIsView(V)) {
|
|
|
|
return webidl.converters["ArrayBufferView"](V, opts);
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
return webidl.converters["USVString"](V, opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
const CONNECTING = 0;
|
|
|
|
const OPEN = 1;
|
|
|
|
const CLOSING = 2;
|
|
|
|
const CLOSED = 3;
|
|
|
|
|
|
|
|
const _readyState = Symbol("[[readyState]]");
|
|
|
|
const _url = Symbol("[[url]]");
|
|
|
|
const _rid = Symbol("[[rid]]");
|
|
|
|
const _extensions = Symbol("[[extensions]]");
|
|
|
|
const _protocol = Symbol("[[protocol]]");
|
|
|
|
const _binaryType = Symbol("[[binaryType]]");
|
|
|
|
const _bufferedAmount = Symbol("[[bufferedAmount]]");
|
|
|
|
const _eventLoop = Symbol("[[eventLoop]]");
|
|
|
|
|
|
|
|
const _server = Symbol("[[server]]");
|
|
|
|
const _idleTimeoutDuration = Symbol("[[idleTimeout]]");
|
|
|
|
const _idleTimeoutTimeout = Symbol("[[idleTimeoutTimeout]]");
|
|
|
|
const _serverHandleIdleTimeout = Symbol("[[serverHandleIdleTimeout]]");
|
|
|
|
class WebSocket extends EventTarget {
|
|
|
|
[_rid];
|
|
|
|
|
|
|
|
[_readyState] = CONNECTING;
|
|
|
|
get readyState() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_readyState];
|
|
|
|
}
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
[_extensions] = "";
|
|
|
|
get extensions() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_extensions];
|
|
|
|
}
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[_protocol] = "";
|
|
|
|
get protocol() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_protocol];
|
|
|
|
}
|
|
|
|
|
|
|
|
[_url] = "";
|
|
|
|
get url() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_url];
|
|
|
|
}
|
|
|
|
|
|
|
|
[_binaryType] = "blob";
|
|
|
|
get binaryType() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_binaryType];
|
|
|
|
}
|
|
|
|
set binaryType(value) {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
value = webidl.converters.DOMString(value, {
|
|
|
|
prefix: "Failed to set 'binaryType' on 'WebSocket'",
|
|
|
|
});
|
|
|
|
if (value === "blob" || value === "arraybuffer") {
|
|
|
|
this[_binaryType] = value;
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[_bufferedAmount] = 0;
|
|
|
|
get bufferedAmount() {
|
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
|
|
|
return this[_bufferedAmount];
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(url, protocols = []) {
|
|
|
|
super();
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
const prefix = "Failed to construct 'WebSocket'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, {
|
|
|
|
prefix,
|
|
|
|
});
|
|
|
|
url = webidl.converters.USVString(url, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
protocols = webidl.converters["sequence<DOMString> or DOMString"](
|
|
|
|
protocols,
|
|
|
|
{
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
},
|
|
|
|
);
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
let wsURL;
|
|
|
|
|
|
|
|
try {
|
|
|
|
wsURL = new URL(url);
|
|
|
|
} catch (e) {
|
|
|
|
throw new DOMException(e.message, "SyntaxError");
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (wsURL.protocol !== "ws:" && wsURL.protocol !== "wss:") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Only ws & wss schemes are allowed in a WebSocket URL.",
|
|
|
|
"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(
|
|
|
|
"Fragments are not allowed in a WebSocket URL.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
2021-06-21 06:15:08 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
this[_url] = wsURL.href;
|
|
|
|
|
|
|
|
ops.op_ws_check_permission_and_cancel_handle(
|
|
|
|
"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 !==
|
|
|
|
new Set(
|
|
|
|
ArrayPrototypeMap(protocols, (p) => StringPrototypeToLowerCase(p)),
|
|
|
|
).size
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Can't supply multiple times the same protocol.",
|
|
|
|
"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-02-07 14:22:46 -05:00
|
|
|
(protocol) => !RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, protocol),
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Invalid protocol value.",
|
|
|
|
"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(
|
|
|
|
core.opAsync(
|
|
|
|
"op_ws_create",
|
|
|
|
"new WebSocket()",
|
|
|
|
wsURL.href,
|
|
|
|
ArrayPrototypeJoin(protocols, ", "),
|
|
|
|
),
|
|
|
|
(create) => {
|
|
|
|
this[_rid] = create.rid;
|
|
|
|
this[_extensions] = create.extensions;
|
|
|
|
this[_protocol] = create.protocol;
|
|
|
|
|
|
|
|
if (this[_readyState] === CLOSING) {
|
|
|
|
PromisePrototypeThen(
|
|
|
|
core.opAsync("op_ws_close", this[_rid]),
|
|
|
|
() => {
|
|
|
|
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-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-02-07 14:22:46 -05:00
|
|
|
webidl.requiredArguments(arguments.length, 1, {
|
|
|
|
prefix,
|
|
|
|
});
|
|
|
|
data = webidl.converters.WebSocketSend(data, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (this[_readyState] !== OPEN) {
|
|
|
|
throw new DOMException("readyState not OPEN", "InvalidStateError");
|
|
|
|
}
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const sendTypedArray = (ta) => {
|
|
|
|
this[_bufferedAmount] += ta.byteLength;
|
2021-07-04 11:38:04 -04:00
|
|
|
PromisePrototypeThen(
|
2023-03-31 11:58:21 -04:00
|
|
|
core.opAsync2("op_ws_send_binary", this[_rid], ta),
|
2023-02-07 14:22:46 -05:00
|
|
|
() => {
|
|
|
|
this[_bufferedAmount] -= ta.byteLength;
|
2021-07-04 11:38:04 -04:00
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
};
|
2020-09-29 05:42:29 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(BlobPrototype, data)) {
|
|
|
|
PromisePrototypeThen(
|
|
|
|
data.slice().arrayBuffer(),
|
|
|
|
(ab) => sendTypedArray(new DataView(ab)),
|
|
|
|
);
|
|
|
|
} else if (ArrayBufferIsView(data)) {
|
|
|
|
sendTypedArray(data);
|
|
|
|
} else if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, data)) {
|
|
|
|
sendTypedArray(new DataView(data));
|
|
|
|
} else {
|
|
|
|
const string = String(data);
|
|
|
|
const d = core.encode(string);
|
|
|
|
this[_bufferedAmount] += d.byteLength;
|
|
|
|
PromisePrototypeThen(
|
2023-03-31 11:58:21 -04:00
|
|
|
core.opAsync2("op_ws_send_text", this[_rid], string),
|
2023-02-07 14:22:46 -05:00
|
|
|
() => {
|
|
|
|
this[_bufferedAmount] -= d.byteLength;
|
2021-07-04 11:38:04 -04:00
|
|
|
},
|
|
|
|
);
|
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) {
|
|
|
|
code = webidl.converters["unsigned short"](code, {
|
2021-06-21 06:15:08 -04:00
|
|
|
prefix,
|
2023-02-07 14:22:46 -05:00
|
|
|
clamp: true,
|
2021-06-21 06:15:08 -04:00
|
|
|
context: "Argument 1",
|
|
|
|
});
|
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) {
|
|
|
|
reason = webidl.converters.USVString(reason, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
});
|
|
|
|
}
|
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(
|
|
|
|
"The close code must be either 1000 or in the range of 3000 to 4999.",
|
|
|
|
"InvalidAccessError",
|
2021-07-04 11:38:04 -04:00
|
|
|
);
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (reason !== undefined && core.encode(reason).byteLength > 123) {
|
|
|
|
throw new DOMException(
|
|
|
|
"The close reason may not be longer than 123 bytes.",
|
|
|
|
"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(
|
|
|
|
core.opAsync("op_ws_close", this[_rid], code, reason),
|
|
|
|
(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]() {
|
|
|
|
while (this[_readyState] !== CLOSED) {
|
2023-03-31 11:58:21 -04:00
|
|
|
const { 0: kind, 1: value } = await core.opAsync2(
|
2023-02-07 14:22:46 -05:00
|
|
|
"op_ws_next_event",
|
|
|
|
this[_rid],
|
|
|
|
);
|
|
|
|
|
|
|
|
switch (kind) {
|
2023-03-31 01:04:12 -04:00
|
|
|
case 0: {
|
|
|
|
/* string */
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_serverHandleIdleTimeout]();
|
|
|
|
const event = new MessageEvent("message", {
|
|
|
|
data: value,
|
|
|
|
origin: this[_url],
|
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
break;
|
|
|
|
}
|
2023-03-31 01:04:12 -04:00
|
|
|
case 1: {
|
|
|
|
/* binary */
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_serverHandleIdleTimeout]();
|
|
|
|
let data;
|
2022-11-01 11:06:06 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (this.binaryType === "blob") {
|
|
|
|
data = new Blob([value]);
|
|
|
|
} else {
|
2023-03-31 01:04:12 -04:00
|
|
|
data = value;
|
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],
|
|
|
|
[_skipInternalInit]: true,
|
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
break;
|
|
|
|
}
|
2023-03-31 01:04:12 -04:00
|
|
|
case 2: {
|
|
|
|
/* pong */
|
2023-02-07 14:22:46 -05:00
|
|
|
this[_serverHandleIdleTimeout]();
|
|
|
|
break;
|
|
|
|
}
|
2023-03-31 01:04:12 -04:00
|
|
|
case 5: {
|
|
|
|
/* error */
|
|
|
|
this[_readyState] = CLOSED;
|
|
|
|
|
|
|
|
const errorEv = new ErrorEvent("error", {
|
|
|
|
message: value,
|
|
|
|
});
|
|
|
|
this.dispatchEvent(errorEv);
|
|
|
|
|
|
|
|
const closeEv = new CloseEvent("close");
|
|
|
|
this.dispatchEvent(closeEv);
|
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
|
|
|
/* ping */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
/* close */
|
|
|
|
const code = kind;
|
2023-02-07 14:22:46 -05:00
|
|
|
const prevState = this[_readyState];
|
|
|
|
this[_readyState] = CLOSED;
|
|
|
|
clearTimeout(this[_idleTimeoutTimeout]);
|
|
|
|
|
|
|
|
if (prevState === OPEN) {
|
|
|
|
try {
|
|
|
|
await core.opAsync(
|
|
|
|
"op_ws_close",
|
|
|
|
this[_rid],
|
2023-03-31 01:04:12 -04:00
|
|
|
code,
|
|
|
|
value,
|
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,
|
|
|
|
reason: value,
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
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
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[_serverHandleIdleTimeout]() {
|
|
|
|
if (this[_idleTimeoutDuration]) {
|
|
|
|
clearTimeout(this[_idleTimeoutTimeout]);
|
|
|
|
this[_idleTimeoutTimeout] = setTimeout(async () => {
|
|
|
|
if (this[_readyState] === OPEN) {
|
|
|
|
await core.opAsync("op_ws_send", this[_rid], {
|
|
|
|
kind: "ping",
|
|
|
|
});
|
|
|
|
this[_idleTimeoutTimeout] = setTimeout(async () => {
|
|
|
|
if (this[_readyState] === OPEN) {
|
|
|
|
this[_readyState] = CLOSING;
|
|
|
|
const reason = "No response from ping frame.";
|
|
|
|
await core.opAsync("op_ws_close", this[_rid], 1001, reason);
|
|
|
|
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-02-07 14:22:46 -05:00
|
|
|
[SymbolFor("Deno.customInspect")](inspect) {
|
|
|
|
return `${this.constructor.name} ${
|
|
|
|
inspect({
|
|
|
|
url: this.url,
|
|
|
|
readyState: this.readyState,
|
|
|
|
extensions: this.extensions,
|
|
|
|
protocol: this.protocol,
|
|
|
|
binaryType: this.binaryType,
|
|
|
|
bufferedAmount: this.bufferedAmount,
|
|
|
|
})
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectDefineProperties(WebSocket, {
|
|
|
|
CONNECTING: {
|
|
|
|
value: 0,
|
|
|
|
},
|
|
|
|
OPEN: {
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
CLOSING: {
|
|
|
|
value: 2,
|
|
|
|
},
|
|
|
|
CLOSED: {
|
|
|
|
value: 3,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
defineEventHandler(WebSocket.prototype, "message");
|
|
|
|
defineEventHandler(WebSocket.prototype, "error");
|
|
|
|
defineEventHandler(WebSocket.prototype, "close");
|
|
|
|
defineEventHandler(WebSocket.prototype, "open");
|
|
|
|
|
|
|
|
webidl.configurePrototype(WebSocket);
|
|
|
|
const WebSocketPrototype = WebSocket.prototype;
|
|
|
|
|
|
|
|
export {
|
|
|
|
_eventLoop,
|
|
|
|
_idleTimeoutDuration,
|
|
|
|
_idleTimeoutTimeout,
|
|
|
|
_protocol,
|
|
|
|
_readyState,
|
|
|
|
_rid,
|
|
|
|
_server,
|
|
|
|
_serverHandleIdleTimeout,
|
|
|
|
WebSocket,
|
|
|
|
};
|