2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 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
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2021-01-06 10:57:28 -05:00
|
|
|
|
|
|
|
// provided by "deno_web"
|
|
|
|
const { URL } = window.__bootstrap.url;
|
|
|
|
|
2020-09-05 10:39:25 -04:00
|
|
|
const CONNECTING = 0;
|
|
|
|
const OPEN = 1;
|
|
|
|
const CLOSING = 2;
|
|
|
|
const CLOSED = 3;
|
|
|
|
|
2021-01-06 10:57:28 -05:00
|
|
|
function requiredArguments(
|
|
|
|
name,
|
|
|
|
length,
|
|
|
|
required,
|
|
|
|
) {
|
|
|
|
if (length < required) {
|
|
|
|
const errMsg = `${name} requires at least ${required} argument${
|
|
|
|
required === 1 ? "" : "s"
|
|
|
|
}, but only ${length} present`;
|
|
|
|
throw new TypeError(errMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 18:55:22 -04:00
|
|
|
/**
|
|
|
|
* Tries to close the resource (and ignores BadResource errors).
|
|
|
|
* @param {number} rid
|
|
|
|
*/
|
|
|
|
function tryClose(rid) {
|
|
|
|
try {
|
|
|
|
core.close(rid);
|
|
|
|
} catch (err) {
|
|
|
|
// Ignore error if the socket has already been closed.
|
|
|
|
if (!(err instanceof Deno.errors.BadResource)) throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 10:57:28 -05:00
|
|
|
const handlerSymbol = Symbol("eventHandlers");
|
|
|
|
function makeWrappedHandler(handler) {
|
|
|
|
function wrappedHandler(...args) {
|
|
|
|
if (typeof wrappedHandler.handler !== "function") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return wrappedHandler.handler.call(this, ...args);
|
|
|
|
}
|
|
|
|
wrappedHandler.handler = handler;
|
|
|
|
return wrappedHandler;
|
|
|
|
}
|
|
|
|
// TODO(lucacasonato) reuse when we can reuse code between web crates
|
|
|
|
function defineEventHandler(emitter, name) {
|
|
|
|
// HTML specification section 8.1.5.1
|
|
|
|
Object.defineProperty(emitter, `on${name}`, {
|
|
|
|
get() {
|
|
|
|
return this[handlerSymbol]?.get(name)?.handler;
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
if (!this[handlerSymbol]) {
|
|
|
|
this[handlerSymbol] = new Map();
|
|
|
|
}
|
|
|
|
let handlerWrapper = this[handlerSymbol]?.get(name);
|
|
|
|
if (handlerWrapper) {
|
|
|
|
handlerWrapper.handler = value;
|
|
|
|
} else {
|
|
|
|
handlerWrapper = makeWrappedHandler(value);
|
|
|
|
this.addEventListener(name, handlerWrapper);
|
|
|
|
}
|
|
|
|
this[handlerSymbol].set(name, handlerWrapper);
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-05 10:39:25 -04:00
|
|
|
class WebSocket extends EventTarget {
|
|
|
|
#readyState = CONNECTING;
|
|
|
|
|
|
|
|
constructor(url, protocols = []) {
|
|
|
|
super();
|
|
|
|
requiredArguments("WebSocket", arguments.length, 1);
|
|
|
|
|
|
|
|
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",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wsURL.hash !== "" || wsURL.href.endsWith("#")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Fragments are not allowed in a WebSocket URL.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#url = wsURL.href;
|
|
|
|
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("op_ws_check_permission", this.#url);
|
2020-11-25 09:17:46 -05:00
|
|
|
|
2020-09-05 10:39:25 -04:00
|
|
|
if (protocols && typeof protocols === "string") {
|
|
|
|
protocols = [protocols];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
protocols.some((x) => protocols.indexOf(x) !== protocols.lastIndexOf(x))
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Can't supply multiple times the same protocol.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opAsync("op_ws_create", {
|
2020-09-05 10:39:25 -04:00
|
|
|
url: wsURL.href,
|
2020-10-22 11:09:44 -04:00
|
|
|
protocols: protocols.join(", "),
|
2020-09-05 10:39:25 -04:00
|
|
|
}).then((create) => {
|
2021-05-01 14:52:13 -04:00
|
|
|
this.#rid = create.rid;
|
|
|
|
this.#extensions = create.extensions;
|
|
|
|
this.#protocol = create.protocol;
|
|
|
|
|
|
|
|
if (this.#readyState === CLOSING) {
|
|
|
|
core.opAsync("op_ws_close", {
|
|
|
|
rid: this.#rid,
|
|
|
|
}).then(() => {
|
|
|
|
this.#readyState = CLOSED;
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2021-05-01 14:52:13 -04:00
|
|
|
const errEvent = new ErrorEvent("error");
|
|
|
|
errEvent.target = this;
|
|
|
|
this.dispatchEvent(errEvent);
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2021-05-01 14:52:13 -04:00
|
|
|
const event = new CloseEvent("close");
|
2020-09-05 10:39:25 -04:00
|
|
|
event.target = this;
|
|
|
|
this.dispatchEvent(event);
|
2021-05-01 14:52:13 -04:00
|
|
|
tryClose(this.#rid);
|
|
|
|
});
|
2020-09-05 10:39:25 -04:00
|
|
|
} else {
|
2021-05-01 14:52:13 -04:00
|
|
|
this.#readyState = OPEN;
|
|
|
|
const event = new Event("open");
|
|
|
|
event.target = this;
|
|
|
|
this.dispatchEvent(event);
|
2020-09-05 10:39:25 -04:00
|
|
|
|
2021-05-01 14:52:13 -04:00
|
|
|
this.#eventLoop();
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
2020-09-13 05:52:20 -04:00
|
|
|
}).catch((err) => {
|
2020-09-29 05:42:29 -04:00
|
|
|
this.#readyState = CLOSED;
|
|
|
|
|
|
|
|
const errorEv = new ErrorEvent(
|
2020-09-13 05:52:20 -04:00
|
|
|
"error",
|
|
|
|
{ error: err, message: err.toString() },
|
|
|
|
);
|
2020-09-29 05:42:29 -04:00
|
|
|
errorEv.target = this;
|
|
|
|
this.dispatchEvent(errorEv);
|
|
|
|
|
|
|
|
const closeEv = new CloseEvent("close");
|
|
|
|
closeEv.target = this;
|
|
|
|
this.dispatchEvent(closeEv);
|
2020-09-05 10:39:25 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get CONNECTING() {
|
|
|
|
return CONNECTING;
|
|
|
|
}
|
|
|
|
get OPEN() {
|
|
|
|
return OPEN;
|
|
|
|
}
|
|
|
|
get CLOSING() {
|
|
|
|
return CLOSING;
|
|
|
|
}
|
|
|
|
get CLOSED() {
|
|
|
|
return CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
get readyState() {
|
|
|
|
return this.#readyState;
|
|
|
|
}
|
|
|
|
|
|
|
|
#extensions = "";
|
|
|
|
#protocol = "";
|
|
|
|
#url = "";
|
|
|
|
#rid;
|
|
|
|
|
|
|
|
get extensions() {
|
|
|
|
return this.#extensions;
|
|
|
|
}
|
|
|
|
get protocol() {
|
|
|
|
return this.#protocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
#binaryType = "blob";
|
|
|
|
get binaryType() {
|
|
|
|
return this.#binaryType;
|
|
|
|
}
|
|
|
|
set binaryType(value) {
|
|
|
|
if (value === "blob" || value === "arraybuffer") {
|
|
|
|
this.#binaryType = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#bufferedAmount = 0;
|
|
|
|
get bufferedAmount() {
|
|
|
|
return this.#bufferedAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
get url() {
|
|
|
|
return this.#url;
|
|
|
|
}
|
|
|
|
|
|
|
|
send(data) {
|
|
|
|
requiredArguments("WebSocket.send", arguments.length, 1);
|
|
|
|
|
|
|
|
if (this.#readyState != OPEN) {
|
|
|
|
throw Error("readyState not OPEN");
|
|
|
|
}
|
|
|
|
|
|
|
|
const sendTypedArray = (ta) => {
|
|
|
|
this.#bufferedAmount += ta.size;
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opAsync("op_ws_send", {
|
2020-09-05 10:39:25 -04:00
|
|
|
rid: this.#rid,
|
2021-01-05 07:37:02 -05:00
|
|
|
kind: "binary",
|
2020-09-05 10:39:25 -04:00
|
|
|
}, ta).then(() => {
|
|
|
|
this.#bufferedAmount -= ta.size;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data instanceof Blob) {
|
|
|
|
data.slice().arrayBuffer().then((ab) =>
|
|
|
|
sendTypedArray(new DataView(ab))
|
|
|
|
);
|
|
|
|
} else if (
|
|
|
|
data instanceof Int8Array || data instanceof Int16Array ||
|
|
|
|
data instanceof Int32Array || data instanceof Uint8Array ||
|
|
|
|
data instanceof Uint16Array || data instanceof Uint32Array ||
|
|
|
|
data instanceof Uint8ClampedArray || data instanceof Float32Array ||
|
|
|
|
data instanceof Float64Array || data instanceof DataView
|
|
|
|
) {
|
|
|
|
sendTypedArray(data);
|
|
|
|
} else if (data instanceof ArrayBuffer) {
|
|
|
|
sendTypedArray(new DataView(data));
|
|
|
|
} else {
|
|
|
|
const string = String(data);
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const d = encoder.encode(string);
|
|
|
|
this.#bufferedAmount += d.size;
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opAsync("op_ws_send", {
|
2020-09-05 10:39:25 -04:00
|
|
|
rid: this.#rid,
|
2021-01-05 07:37:02 -05:00
|
|
|
kind: "text",
|
2020-09-05 10:39:25 -04:00
|
|
|
text: string,
|
|
|
|
}).then(() => {
|
|
|
|
this.#bufferedAmount -= d.size;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(code, reason) {
|
2021-02-12 00:50:08 -05:00
|
|
|
if (code && !(code === 1000 || (3000 <= code && code < 5000))) {
|
2020-09-05 10:39:25 -04:00
|
|
|
throw new DOMException(
|
|
|
|
"The close code must be either 1000 or in the range of 3000 to 4999.",
|
|
|
|
"NotSupportedError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
if (reason && encoder.encode(reason).byteLength > 123) {
|
|
|
|
throw new DOMException(
|
|
|
|
"The close reason may not be longer than 123 bytes.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.#readyState === CONNECTING) {
|
|
|
|
this.#readyState = CLOSING;
|
|
|
|
} else if (this.#readyState === OPEN) {
|
|
|
|
this.#readyState = CLOSING;
|
|
|
|
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opAsync("op_ws_close", {
|
2020-09-05 10:39:25 -04:00
|
|
|
rid: this.#rid,
|
|
|
|
code,
|
|
|
|
reason,
|
|
|
|
}).then(() => {
|
|
|
|
this.#readyState = CLOSED;
|
|
|
|
const event = new CloseEvent("close", {
|
|
|
|
wasClean: true,
|
|
|
|
code,
|
|
|
|
reason,
|
|
|
|
});
|
|
|
|
event.target = this;
|
|
|
|
this.dispatchEvent(event);
|
2021-04-01 18:55:22 -04:00
|
|
|
tryClose(this.#rid);
|
2020-09-05 10:39:25 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async #eventLoop() {
|
2021-01-10 14:05:24 -05: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-04-05 12:40:24 -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": {
|
|
|
|
const event = new MessageEvent("message", {
|
|
|
|
data: value,
|
|
|
|
origin: this.#url,
|
|
|
|
});
|
|
|
|
event.target = this;
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
break;
|
2021-01-10 14:05:24 -05:00
|
|
|
}
|
2021-04-22 19:31:34 -04:00
|
|
|
case "binary": {
|
|
|
|
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,
|
|
|
|
origin: this.#url,
|
|
|
|
});
|
|
|
|
event.target = this;
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ping": {
|
|
|
|
core.opAsync("op_ws_send", {
|
|
|
|
rid: this.#rid,
|
|
|
|
kind: "pong",
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "close": {
|
|
|
|
this.#readyState = CLOSED;
|
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,
|
|
|
|
});
|
|
|
|
event.target = this;
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
tryClose(this.#rid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "error": {
|
|
|
|
this.#readyState = CLOSED;
|
|
|
|
|
2021-05-01 14:52:13 -04:00
|
|
|
const errorEv = new ErrorEvent("error", {
|
|
|
|
message: value,
|
|
|
|
});
|
2021-04-22 19:31:34 -04:00
|
|
|
errorEv.target = this;
|
|
|
|
this.dispatchEvent(errorEv);
|
|
|
|
|
|
|
|
const closeEv = new CloseEvent("close");
|
|
|
|
closeEv.target = this;
|
|
|
|
this.dispatchEvent(closeEv);
|
|
|
|
tryClose(this.#rid);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-05 10:39:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperties(WebSocket, {
|
|
|
|
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
|
|
|
|
|
|
|
window.__bootstrap.webSocket = { WebSocket };
|
2020-09-05 10:39:25 -04:00
|
|
|
})(this);
|