2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2021-07-04 11:38:04 -04:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
2020-09-05 10:39:25 -04:00
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2021-01-06 10:57:28 -05:00
|
|
|
const { URL } = window.__bootstrap.url;
|
2021-06-21 06:15:08 -04:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
|
|
|
const { HTTP_TOKEN_CODE_POINT_RE } = window.__bootstrap.infra;
|
2021-07-03 15:32:28 -04:00
|
|
|
const { DOMException } = window.__bootstrap.domException;
|
2021-10-08 03:53:31 -04:00
|
|
|
const { defineEventHandler } = window.__bootstrap.event;
|
2022-02-01 12:06:11 -05:00
|
|
|
const { Blob, BlobPrototype } = globalThis.__bootstrap.file;
|
2021-07-04 11:38:04 -04:00
|
|
|
const {
|
2022-02-01 12:06:11 -05:00
|
|
|
ArrayBufferPrototype,
|
2021-07-04 11:38:04 -04:00
|
|
|
ArrayBufferIsView,
|
|
|
|
ArrayPrototypeJoin,
|
2022-02-01 12:06:11 -05:00
|
|
|
ArrayPrototypeMap,
|
|
|
|
ArrayPrototypeSome,
|
2021-07-04 11:38:04 -04:00
|
|
|
DataView,
|
|
|
|
ErrorPrototypeToString,
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectDefineProperties,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
PromisePrototypeThen,
|
|
|
|
RegExpPrototypeTest,
|
2021-07-04 11:38:04 -04:00
|
|
|
Set,
|
|
|
|
String,
|
2022-01-27 10:27:22 -05:00
|
|
|
StringPrototypeEndsWith,
|
2022-02-01 12:06:11 -05:00
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
Symbol,
|
|
|
|
SymbolIterator,
|
2021-07-04 11:38:04 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2021-06-21 06:15:08 -04:00
|
|
|
|
|
|
|
webidl.converters["sequence<DOMString> or DOMString"] = (V, opts) => {
|
|
|
|
// Union for (sequence<DOMString> or DOMString)
|
|
|
|
if (webidl.type(V) === "Object" && V !== null) {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (V[SymbolIterator] !== undefined) {
|
2021-06-21 06:15:08 -04:00
|
|
|
return webidl.converters["sequence<DOMString>"](V, opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return webidl.converters.DOMString(V, opts);
|
|
|
|
};
|
|
|
|
|
|
|
|
webidl.converters["WebSocketSend"] = (V, opts) => {
|
|
|
|
// Union for (Blob or ArrayBufferView or ArrayBuffer or USVString)
|
2022-02-01 12:06:11 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(BlobPrototype, V)) {
|
2021-06-21 06:15:08 -04:00
|
|
|
return webidl.converters["Blob"](V, opts);
|
|
|
|
}
|
|
|
|
if (typeof V === "object") {
|
2021-07-04 11:38:04 -04:00
|
|
|
// TODO(littledivy): use primordial for SharedArrayBuffer
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V)
|
|
|
|
) {
|
2021-06-21 06:15:08 -04:00
|
|
|
return webidl.converters["ArrayBuffer"](V, opts);
|
|
|
|
}
|
2021-07-04 11:38:04 -04:00
|
|
|
if (ArrayBufferIsView(V)) {
|
2021-06-21 06:15:08 -04:00
|
|
|
return webidl.converters["ArrayBufferView"](V, opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return webidl.converters["USVString"](V, opts);
|
|
|
|
};
|
2021-01-06 10:57:28 -05:00
|
|
|
|
2020-09-05 10:39:25 -04:00
|
|
|
const CONNECTING = 0;
|
|
|
|
const OPEN = 1;
|
|
|
|
const CLOSING = 2;
|
|
|
|
const CLOSED = 3;
|
|
|
|
|
2021-06-21 06:15:08 -04:00
|
|
|
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]]");
|
2021-07-08 07:33:01 -04:00
|
|
|
const _eventLoop = Symbol("[[eventLoop]]");
|
2022-01-06 11:41:16 -05:00
|
|
|
|
2021-08-09 04:45:59 -04:00
|
|
|
const _server = Symbol("[[server]]");
|
2022-01-06 11:41:16 -05:00
|
|
|
const _idleTimeoutDuration = Symbol("[[idleTimeout]]");
|
|
|
|
const _idleTimeoutTimeout = Symbol("[[idleTimeoutTimeout]]");
|
|
|
|
const _serverHandleIdleTimeout = Symbol("[[serverHandleIdleTimeout]]");
|
2020-09-05 10:39:25 -04:00
|
|
|
class WebSocket extends EventTarget {
|
2021-06-21 06:15:08 -04:00
|
|
|
[_rid];
|
|
|
|
|
|
|
|
[_readyState] = CONNECTING;
|
|
|
|
get readyState() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
return this[_readyState];
|
|
|
|
}
|
|
|
|
|
|
|
|
get CONNECTING() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
return CONNECTING;
|
|
|
|
}
|
|
|
|
get OPEN() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
return OPEN;
|
|
|
|
}
|
|
|
|
get CLOSING() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
return CLOSING;
|
|
|
|
}
|
|
|
|
get CLOSED() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
return CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
[_extensions] = "";
|
|
|
|
get extensions() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
return this[_extensions];
|
|
|
|
}
|
|
|
|
|
|
|
|
[_protocol] = "";
|
|
|
|
get protocol() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
return this[_protocol];
|
|
|
|
}
|
|
|
|
|
|
|
|
[_url] = "";
|
|
|
|
get url() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
return this[_url];
|
|
|
|
}
|
|
|
|
|
|
|
|
[_binaryType] = "blob";
|
|
|
|
get binaryType() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
return this[_binaryType];
|
|
|
|
}
|
|
|
|
set binaryType(value) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
value = webidl.converters.DOMString(value, {
|
|
|
|
prefix: "Failed to set 'binaryType' on 'WebSocket'",
|
|
|
|
});
|
|
|
|
if (value === "blob" || value === "arraybuffer") {
|
|
|
|
this[_binaryType] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[_bufferedAmount] = 0;
|
|
|
|
get bufferedAmount() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
return this[_bufferedAmount];
|
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
|
|
|
|
constructor(url, protocols = []) {
|
|
|
|
super();
|
2021-06-21 06:15:08 -04:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let wsURL;
|
|
|
|
|
|
|
|
try {
|
|
|
|
wsURL = new URL(url);
|
|
|
|
} catch (e) {
|
|
|
|
throw new DOMException(e.message, "SyntaxError");
|
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
|
|
|
|
if (wsURL.protocol !== "ws:" && wsURL.protocol !== "wss:") {
|
|
|
|
throw new DOMException(
|
|
|
|
"Only ws & wss schemes are allowed in a WebSocket URL.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:38:04 -04:00
|
|
|
if (wsURL.hash !== "" || StringPrototypeEndsWith(wsURL.href, "#")) {
|
2020-09-05 10:39:25 -04:00
|
|
|
throw new DOMException(
|
|
|
|
"Fragments are not allowed in a WebSocket URL.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-21 06:15:08 -04:00
|
|
|
this[_url] = wsURL.href;
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2021-08-09 18:28:17 -04:00
|
|
|
core.opSync(
|
|
|
|
"op_ws_check_permission_and_cancel_handle",
|
|
|
|
this[_url],
|
|
|
|
false,
|
|
|
|
);
|
2020-11-25 09:17:46 -05:00
|
|
|
|
2021-06-21 06:15:08 -04:00
|
|
|
if (typeof protocols === "string") {
|
2020-09-05 10:39:25 -04:00
|
|
|
protocols = [protocols];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2021-07-04 11:38:04 -04:00
|
|
|
protocols.length !==
|
|
|
|
new Set(
|
|
|
|
ArrayPrototypeMap(protocols, (p) => StringPrototypeToLowerCase(p)),
|
|
|
|
).size
|
2020-09-05 10:39:25 -04:00
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Can't supply multiple times the same protocol.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-21 06:15:08 -04:00
|
|
|
if (
|
2021-07-04 11:38:04 -04:00
|
|
|
ArrayPrototypeSome(
|
|
|
|
protocols,
|
|
|
|
(protocol) =>
|
|
|
|
!RegExpPrototypeTest(HTTP_TOKEN_CODE_POINT_RE, protocol),
|
|
|
|
)
|
2021-06-21 06:15:08 -04:00
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Invalid protocol value.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-04 11:38:04 -04:00
|
|
|
PromisePrototypeThen(
|
2022-04-04 05:49:50 -04:00
|
|
|
core.opAsync(
|
|
|
|
"op_ws_create",
|
|
|
|
wsURL.href,
|
|
|
|
ArrayPrototypeJoin(protocols, ", "),
|
|
|
|
),
|
2021-07-04 11:38:04 -04:00
|
|
|
(create) => {
|
|
|
|
this[_rid] = create.rid;
|
|
|
|
this[_extensions] = create.extensions;
|
|
|
|
this[_protocol] = create.protocol;
|
|
|
|
|
|
|
|
if (this[_readyState] === CLOSING) {
|
|
|
|
PromisePrototypeThen(
|
2022-04-04 05:49:50 -04:00
|
|
|
core.opAsync("op_ws_close", this[_rid]),
|
2021-07-04 11:38:04 -04:00
|
|
|
() => {
|
|
|
|
this[_readyState] = CLOSED;
|
|
|
|
|
|
|
|
const errEvent = new ErrorEvent("error");
|
|
|
|
this.dispatchEvent(errEvent);
|
|
|
|
|
|
|
|
const event = new CloseEvent("close");
|
|
|
|
this.dispatchEvent(event);
|
2021-09-10 20:54:37 -04:00
|
|
|
core.tryClose(this[_rid]);
|
2021-07-04 11:38:04 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this[_readyState] = OPEN;
|
|
|
|
const event = new Event("open");
|
2020-09-05 10:39:25 -04:00
|
|
|
this.dispatchEvent(event);
|
|
|
|
|
2021-07-08 07:33:01 -04:00
|
|
|
this[_eventLoop]();
|
2021-07-04 11:38:04 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
this[_readyState] = CLOSED;
|
2020-09-29 05:42:29 -04:00
|
|
|
|
2021-07-04 11:38:04 -04:00
|
|
|
const errorEv = new ErrorEvent(
|
|
|
|
"error",
|
|
|
|
{ error: err, message: ErrorPrototypeToString(err) },
|
|
|
|
);
|
|
|
|
this.dispatchEvent(errorEv);
|
2020-09-29 05:42:29 -04:00
|
|
|
|
2021-07-04 11:38:04 -04:00
|
|
|
const closeEv = new CloseEvent("close");
|
|
|
|
this.dispatchEvent(closeEv);
|
|
|
|
},
|
|
|
|
);
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
send(data) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
const prefix = "Failed to execute 'send' on 'WebSocket'";
|
|
|
|
|
|
|
|
webidl.requiredArguments(arguments.length, 1, {
|
|
|
|
prefix,
|
|
|
|
});
|
|
|
|
data = webidl.converters.WebSocketSend(data, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2021-06-21 06:15:08 -04:00
|
|
|
if (this[_readyState] !== OPEN) {
|
|
|
|
throw new DOMException("readyState not OPEN", "InvalidStateError");
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const sendTypedArray = (ta) => {
|
2021-06-21 06:15:08 -04:00
|
|
|
this[_bufferedAmount] += ta.byteLength;
|
2021-07-04 11:38:04 -04:00
|
|
|
PromisePrototypeThen(
|
2021-10-05 16:38:27 -04:00
|
|
|
core.opAsync("op_ws_send", this[_rid], {
|
2021-07-04 11:38:04 -04:00
|
|
|
kind: "binary",
|
2021-10-05 16:38:27 -04:00
|
|
|
value: ta,
|
|
|
|
}),
|
2021-07-04 11:38:04 -04:00
|
|
|
() => {
|
|
|
|
this[_bufferedAmount] -= ta.byteLength;
|
|
|
|
},
|
|
|
|
);
|
2020-09-05 10:39:25 -04:00
|
|
|
};
|
|
|
|
|
2022-02-01 12:06:11 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(BlobPrototype, data)) {
|
2021-07-04 11:38:04 -04:00
|
|
|
PromisePrototypeThen(
|
|
|
|
data.slice().arrayBuffer(),
|
|
|
|
(ab) => sendTypedArray(new DataView(ab)),
|
2020-09-05 10:39:25 -04:00
|
|
|
);
|
2021-07-04 11:38:04 -04:00
|
|
|
} else if (ArrayBufferIsView(data)) {
|
2020-09-05 10:39:25 -04:00
|
|
|
sendTypedArray(data);
|
2022-02-01 12:06:11 -05:00
|
|
|
} else if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, data)) {
|
2020-09-05 10:39:25 -04:00
|
|
|
sendTypedArray(new DataView(data));
|
|
|
|
} else {
|
|
|
|
const string = String(data);
|
2021-06-05 17:10:07 -04:00
|
|
|
const d = core.encode(string);
|
2021-06-21 06:15:08 -04:00
|
|
|
this[_bufferedAmount] += d.byteLength;
|
2021-07-04 11:38:04 -04:00
|
|
|
PromisePrototypeThen(
|
2021-10-05 16:38:27 -04:00
|
|
|
core.opAsync("op_ws_send", this[_rid], {
|
2021-07-04 11:38:04 -04:00
|
|
|
kind: "text",
|
2021-10-05 16:38:27 -04:00
|
|
|
value: string,
|
2021-07-04 11:38:04 -04:00
|
|
|
}),
|
|
|
|
() => {
|
|
|
|
this[_bufferedAmount] -= d.byteLength;
|
|
|
|
},
|
|
|
|
);
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 06:15:08 -04:00
|
|
|
close(code = undefined, reason = undefined) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketPrototype);
|
2021-06-21 06:15:08 -04:00
|
|
|
const prefix = "Failed to execute 'close' on 'WebSocket'";
|
|
|
|
|
|
|
|
if (code !== undefined) {
|
|
|
|
code = webidl.converters["unsigned short"](code, {
|
|
|
|
prefix,
|
|
|
|
clamp: true,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reason !== undefined) {
|
|
|
|
reason = webidl.converters.USVString(reason, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-09 04:45:59 -04: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",
|
|
|
|
);
|
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
2021-06-21 06:15:08 -04:00
|
|
|
if (reason !== undefined && core.encode(reason).byteLength > 123) {
|
2020-09-05 10:39:25 -04:00
|
|
|
throw new DOMException(
|
|
|
|
"The close reason may not be longer than 123 bytes.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-21 06:15:08 -04:00
|
|
|
if (this[_readyState] === CONNECTING) {
|
|
|
|
this[_readyState] = CLOSING;
|
|
|
|
} else if (this[_readyState] === OPEN) {
|
|
|
|
this[_readyState] = CLOSING;
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2021-07-04 11:38:04 -04:00
|
|
|
PromisePrototypeThen(
|
2022-04-04 05:49:50 -04:00
|
|
|
core.opAsync("op_ws_close", this[_rid], code, reason),
|
2021-07-04 11:38:04 -04:00
|
|
|
() => {
|
|
|
|
this[_readyState] = CLOSED;
|
|
|
|
const event = new CloseEvent("close", {
|
|
|
|
wasClean: true,
|
|
|
|
code: code ?? 1005,
|
|
|
|
reason,
|
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
2021-09-10 20:54:37 -04:00
|
|
|
core.tryClose(this[_rid]);
|
2021-07-04 11:38:04 -04:00
|
|
|
},
|
|
|
|
);
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-08 07:33:01 -04:00
|
|
|
async [_eventLoop]() {
|
2021-06-21 06:15:08 -04:00
|
|
|
while (this[_readyState] === OPEN) {
|
2021-04-22 19:31:34 -04:00
|
|
|
const { kind, value } = await core.opAsync(
|
2020-09-16 16:22:43 -04:00
|
|
|
"op_ws_next_event",
|
2021-06-21 06:15:08 -04:00
|
|
|
this[_rid],
|
2020-09-16 16:22:43 -04:00
|
|
|
);
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2021-04-22 19:31:34 -04:00
|
|
|
switch (kind) {
|
|
|
|
case "string": {
|
2022-01-06 11:41:16 -05:00
|
|
|
this[_serverHandleIdleTimeout]();
|
2021-04-22 19:31:34 -04:00
|
|
|
const event = new MessageEvent("message", {
|
|
|
|
data: value,
|
2021-06-21 06:15:08 -04:00
|
|
|
origin: this[_url],
|
2021-04-22 19:31:34 -04:00
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
break;
|
2021-01-10 14:05:24 -05:00
|
|
|
}
|
2021-04-22 19:31:34 -04:00
|
|
|
case "binary": {
|
2022-01-06 11:41:16 -05:00
|
|
|
this[_serverHandleIdleTimeout]();
|
2021-04-22 19:31:34 -04:00
|
|
|
let data;
|
|
|
|
|
|
|
|
if (this.binaryType === "blob") {
|
2021-04-30 11:03:50 -04:00
|
|
|
data = new Blob([value]);
|
2021-04-22 19:31:34 -04:00
|
|
|
} else {
|
2021-04-30 11:03:50 -04:00
|
|
|
data = value.buffer;
|
2021-04-22 19:31:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const event = new MessageEvent("message", {
|
|
|
|
data,
|
2021-06-21 06:15:08 -04:00
|
|
|
origin: this[_url],
|
2021-04-22 19:31:34 -04:00
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ping": {
|
2021-10-05 16:38:27 -04:00
|
|
|
core.opAsync("op_ws_send", this[_rid], {
|
2021-04-22 19:31:34 -04:00
|
|
|
kind: "pong",
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2022-01-06 11:41:16 -05:00
|
|
|
case "pong": {
|
|
|
|
this[_serverHandleIdleTimeout]();
|
|
|
|
break;
|
|
|
|
}
|
2021-10-19 12:21:15 -04:00
|
|
|
case "closed":
|
2021-04-22 19:31:34 -04:00
|
|
|
case "close": {
|
2021-06-21 06:15:08 -04:00
|
|
|
this[_readyState] = CLOSED;
|
2022-01-06 11:41:16 -05:00
|
|
|
clearTimeout(this[_idleTimeoutTimeout]);
|
2020-09-29 05:42:29 -04:00
|
|
|
|
2021-04-22 19:31:34 -04:00
|
|
|
const event = new CloseEvent("close", {
|
|
|
|
wasClean: true,
|
|
|
|
code: value.code,
|
|
|
|
reason: value.reason,
|
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
2021-09-10 20:54:37 -04:00
|
|
|
core.tryClose(this[_rid]);
|
2021-04-22 19:31:34 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "error": {
|
2021-06-21 06:15:08 -04:00
|
|
|
this[_readyState] = CLOSED;
|
2021-04-22 19:31:34 -04:00
|
|
|
|
2021-05-01 14:52:13 -04:00
|
|
|
const errorEv = new ErrorEvent("error", {
|
|
|
|
message: value,
|
|
|
|
});
|
2021-04-22 19:31:34 -04:00
|
|
|
this.dispatchEvent(errorEv);
|
|
|
|
|
|
|
|
const closeEv = new CloseEvent("close");
|
|
|
|
this.dispatchEvent(closeEv);
|
2021-09-10 20:54:37 -04:00
|
|
|
core.tryClose(this[_rid]);
|
2021-04-22 19:31:34 -04:00
|
|
|
break;
|
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 11:41:16 -05:00
|
|
|
|
|
|
|
[_serverHandleIdleTimeout]() {
|
|
|
|
if (this[_idleTimeoutDuration]) {
|
|
|
|
clearTimeout(this[_idleTimeoutTimeout]);
|
|
|
|
this[_idleTimeoutTimeout] = setTimeout(async () => {
|
|
|
|
await core.opAsync("op_ws_send", this[_rid], {
|
|
|
|
kind: "ping",
|
|
|
|
});
|
|
|
|
this[_idleTimeoutTimeout] = setTimeout(async () => {
|
|
|
|
this[_readyState] = CLOSING;
|
|
|
|
const reason = "No response from ping frame.";
|
2022-04-04 05:49:50 -04:00
|
|
|
await core.opAsync("op_ws_close", this[_rid], 1001, reason);
|
2022-01-06 11:41:16 -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]);
|
|
|
|
}, (this[_idleTimeoutDuration] / 2) * 1000);
|
|
|
|
}, (this[_idleTimeoutDuration] / 2) * 1000);
|
|
|
|
}
|
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 11:38:04 -04:00
|
|
|
ObjectDefineProperties(WebSocket, {
|
2020-09-05 10:39:25 -04:00
|
|
|
CONNECTING: {
|
|
|
|
value: 0,
|
|
|
|
},
|
|
|
|
OPEN: {
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
CLOSING: {
|
|
|
|
value: 2,
|
|
|
|
},
|
|
|
|
CLOSED: {
|
|
|
|
value: 3,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-11-09 22:34:42 -05:00
|
|
|
defineEventHandler(WebSocket.prototype, "message");
|
|
|
|
defineEventHandler(WebSocket.prototype, "error");
|
|
|
|
defineEventHandler(WebSocket.prototype, "close");
|
|
|
|
defineEventHandler(WebSocket.prototype, "open");
|
2021-01-06 10:57:28 -05:00
|
|
|
|
2021-06-21 06:15:08 -04:00
|
|
|
webidl.configurePrototype(WebSocket);
|
2022-02-01 12:06:11 -05:00
|
|
|
const WebSocketPrototype = WebSocket.prototype;
|
2021-06-21 06:15:08 -04:00
|
|
|
|
2021-07-08 07:33:01 -04:00
|
|
|
window.__bootstrap.webSocket = {
|
|
|
|
WebSocket,
|
|
|
|
_rid,
|
|
|
|
_readyState,
|
|
|
|
_eventLoop,
|
|
|
|
_protocol,
|
2021-08-09 04:45:59 -04:00
|
|
|
_server,
|
2022-01-06 11:41:16 -05:00
|
|
|
_idleTimeoutDuration,
|
|
|
|
_idleTimeoutTimeout,
|
|
|
|
_serverHandleIdleTimeout,
|
2021-07-08 07:33:01 -04:00
|
|
|
};
|
2020-09-05 10:39:25 -04:00
|
|
|
})(this);
|