2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-08-09 18:28:17 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
const core = window.Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2021-08-09 18:28:17 -04:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
|
|
|
const { writableStreamClose, Deferred } = window.__bootstrap.streams;
|
|
|
|
const { DOMException } = window.__bootstrap.domException;
|
|
|
|
const { add, remove } = window.__bootstrap.abortSignal;
|
2022-02-07 07:54:32 -05:00
|
|
|
const { headersFromHeaderList, headerListFromHeaders, fillHeaders } =
|
|
|
|
window.__bootstrap.headers;
|
2021-08-09 18:28:17 -04:00
|
|
|
|
|
|
|
const {
|
2022-02-01 12:06:11 -05:00
|
|
|
ArrayPrototypeJoin,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
Error,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
PromisePrototypeCatch,
|
|
|
|
PromisePrototypeThen,
|
|
|
|
Set,
|
2021-08-09 18:28:17 -04:00
|
|
|
StringPrototypeEndsWith,
|
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
TypeError,
|
2022-02-01 12:06:11 -05:00
|
|
|
Uint8ArrayPrototype,
|
2021-08-09 18:28:17 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
|
|
|
|
|
|
|
webidl.converters.WebSocketStreamOptions = webidl.createDictionaryConverter(
|
|
|
|
"WebSocketStreamOptions",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "protocols",
|
|
|
|
converter: webidl.converters["sequence<USVString>"],
|
|
|
|
get defaultValue() {
|
|
|
|
return [];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "signal",
|
|
|
|
converter: webidl.converters.AbortSignal,
|
|
|
|
},
|
2022-01-05 11:41:44 -05:00
|
|
|
{
|
|
|
|
key: "headers",
|
|
|
|
converter: webidl.converters.HeadersInit,
|
|
|
|
},
|
2021-08-09 18:28:17 -04:00
|
|
|
],
|
|
|
|
);
|
|
|
|
webidl.converters.WebSocketCloseInfo = webidl.createDictionaryConverter(
|
|
|
|
"WebSocketCloseInfo",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
key: "code",
|
|
|
|
converter: webidl.converters["unsigned short"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "reason",
|
|
|
|
converter: webidl.converters.USVString,
|
|
|
|
defaultValue: "",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2022-08-29 21:43:17 -04:00
|
|
|
const CLOSE_RESPONSE_TIMEOUT = 5000;
|
|
|
|
|
2021-08-09 18:28:17 -04:00
|
|
|
const _rid = Symbol("[[rid]]");
|
|
|
|
const _url = Symbol("[[url]]");
|
|
|
|
const _connection = Symbol("[[connection]]");
|
|
|
|
const _closed = Symbol("[[closed]]");
|
|
|
|
const _earlyClose = Symbol("[[earlyClose]]");
|
2022-08-29 21:43:17 -04:00
|
|
|
const _closeSent = Symbol("[[closeSent]]");
|
2021-08-09 18:28:17 -04:00
|
|
|
class WebSocketStream {
|
|
|
|
[_rid];
|
|
|
|
|
|
|
|
[_url];
|
|
|
|
get url() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketStreamPrototype);
|
2021-08-09 18:28:17 -04:00
|
|
|
return this[_url];
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(url, options) {
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
const prefix = "Failed to construct 'WebSocketStream'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
url = webidl.converters.USVString(url, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
options = webidl.converters.WebSocketStreamOptions(options, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
});
|
|
|
|
|
|
|
|
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 !== "" || StringPrototypeEndsWith(wsURL.href, "#")) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Fragments are not allowed in a WebSocket URL.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this[_url] = wsURL.href;
|
|
|
|
|
|
|
|
if (
|
|
|
|
options.protocols.length !==
|
|
|
|
new Set(
|
|
|
|
ArrayPrototypeMap(
|
|
|
|
options.protocols,
|
|
|
|
(p) => StringPrototypeToLowerCase(p),
|
|
|
|
),
|
|
|
|
).size
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Can't supply multiple times the same protocol.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-07 07:54:32 -05:00
|
|
|
const headers = headersFromHeaderList([], "request");
|
|
|
|
if (options.headers !== undefined) {
|
|
|
|
fillHeaders(headers, options.headers);
|
|
|
|
}
|
|
|
|
|
2022-08-11 09:56:56 -04:00
|
|
|
const cancelRid = ops.op_ws_check_permission_and_cancel_handle(
|
2022-09-27 16:36:33 -04:00
|
|
|
"WebSocketStream.abort()",
|
2021-08-09 18:28:17 -04:00
|
|
|
this[_url],
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (options.signal?.aborted) {
|
|
|
|
core.close(cancelRid);
|
2021-12-16 06:57:26 -05:00
|
|
|
const err = options.signal.reason;
|
2021-08-09 18:28:17 -04:00
|
|
|
this[_connection].reject(err);
|
|
|
|
this[_closed].reject(err);
|
|
|
|
} else {
|
|
|
|
const abort = () => {
|
|
|
|
core.close(cancelRid);
|
|
|
|
};
|
|
|
|
options.signal?.[add](abort);
|
|
|
|
PromisePrototypeThen(
|
2022-04-04 05:49:50 -04:00
|
|
|
core.opAsync(
|
|
|
|
"op_ws_create",
|
2022-09-27 16:36:33 -04:00
|
|
|
"new WebSocketStream()",
|
2022-04-04 05:49:50 -04:00
|
|
|
this[_url],
|
|
|
|
options.protocols
|
2021-08-09 18:28:17 -04:00
|
|
|
? ArrayPrototypeJoin(options.protocols, ", ")
|
|
|
|
: "",
|
2022-04-04 05:49:50 -04:00
|
|
|
cancelRid,
|
|
|
|
headerListFromHeaders(headers),
|
|
|
|
),
|
2021-08-09 18:28:17 -04:00
|
|
|
(create) => {
|
|
|
|
options.signal?.[remove](abort);
|
|
|
|
if (this[_earlyClose]) {
|
|
|
|
PromisePrototypeThen(
|
2022-04-04 05:49:50 -04:00
|
|
|
core.opAsync("op_ws_close", create.rid),
|
2021-08-09 18:28:17 -04:00
|
|
|
() => {
|
|
|
|
PromisePrototypeThen(
|
|
|
|
(async () => {
|
|
|
|
while (true) {
|
|
|
|
const { kind } = await core.opAsync(
|
|
|
|
"op_ws_next_event",
|
|
|
|
create.rid,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (kind === "close") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})(),
|
|
|
|
() => {
|
|
|
|
const err = new DOMException(
|
|
|
|
"Closed while connecting",
|
|
|
|
"NetworkError",
|
|
|
|
);
|
|
|
|
this[_connection].reject(err);
|
|
|
|
this[_closed].reject(err);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
const err = new DOMException(
|
|
|
|
"Closed while connecting",
|
|
|
|
"NetworkError",
|
|
|
|
);
|
|
|
|
this[_connection].reject(err);
|
|
|
|
this[_closed].reject(err);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this[_rid] = create.rid;
|
|
|
|
|
|
|
|
const writable = new WritableStream({
|
|
|
|
write: async (chunk) => {
|
|
|
|
if (typeof chunk === "string") {
|
2021-10-05 16:38:27 -04:00
|
|
|
await core.opAsync("op_ws_send", this[_rid], {
|
2021-08-09 18:28:17 -04:00
|
|
|
kind: "text",
|
2021-10-05 16:38:27 -04:00
|
|
|
value: chunk,
|
2021-08-09 18:28:17 -04:00
|
|
|
});
|
2022-02-01 12:06:11 -05:00
|
|
|
} else if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, chunk)
|
|
|
|
) {
|
2021-10-05 16:38:27 -04:00
|
|
|
await core.opAsync("op_ws_send", this[_rid], {
|
2021-08-09 18:28:17 -04:00
|
|
|
kind: "binary",
|
2021-10-05 16:38:27 -04:00
|
|
|
value: chunk,
|
2021-08-09 18:28:17 -04:00
|
|
|
}, chunk);
|
|
|
|
} else {
|
|
|
|
throw new TypeError(
|
|
|
|
"A chunk may only be either a string or an Uint8Array",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
close: async (reason) => {
|
|
|
|
try {
|
|
|
|
this.close(reason?.code !== undefined ? reason : {});
|
|
|
|
} catch (_) {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
await this.closed;
|
|
|
|
},
|
|
|
|
abort: async (reason) => {
|
|
|
|
try {
|
|
|
|
this.close(reason?.code !== undefined ? reason : {});
|
|
|
|
} catch (_) {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
await this.closed;
|
|
|
|
},
|
|
|
|
});
|
2022-07-18 16:49:49 -04:00
|
|
|
const pull = async (controller) => {
|
|
|
|
const { kind, value } = await core.opAsync(
|
|
|
|
"op_ws_next_event",
|
|
|
|
this[_rid],
|
|
|
|
);
|
|
|
|
|
|
|
|
switch (kind) {
|
|
|
|
case "string": {
|
|
|
|
controller.enqueue(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "binary": {
|
|
|
|
controller.enqueue(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "ping": {
|
|
|
|
await core.opAsync("op_ws_send", this[_rid], {
|
|
|
|
kind: "pong",
|
|
|
|
});
|
|
|
|
await pull(controller);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "closed":
|
|
|
|
case "close": {
|
|
|
|
this[_closed].resolve(value);
|
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "error": {
|
|
|
|
const err = new Error(value);
|
|
|
|
this[_closed].reject(err);
|
|
|
|
controller.error(err);
|
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 21:43:17 -04:00
|
|
|
|
|
|
|
if (
|
|
|
|
this[_closeSent].state === "fulfilled" &&
|
|
|
|
this[_closed].state === "pending"
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
new Date().getTime() - await this[_closeSent].promise <=
|
|
|
|
CLOSE_RESPONSE_TIMEOUT
|
|
|
|
) {
|
|
|
|
return pull(controller);
|
|
|
|
}
|
|
|
|
|
|
|
|
this[_closed].resolve(value);
|
|
|
|
core.tryClose(this[_rid]);
|
|
|
|
}
|
2022-07-18 16:49:49 -04:00
|
|
|
};
|
2021-08-09 18:28:17 -04:00
|
|
|
const readable = new ReadableStream({
|
|
|
|
start: (controller) => {
|
|
|
|
PromisePrototypeThen(this.closed, () => {
|
|
|
|
try {
|
|
|
|
controller.close();
|
|
|
|
} catch (_) {
|
|
|
|
// needed to ignore warnings & assertions
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
PromisePrototypeCatch(
|
|
|
|
writableStreamClose(writable),
|
|
|
|
() => {},
|
|
|
|
);
|
|
|
|
} catch (_) {
|
|
|
|
// needed to ignore warnings & assertions
|
|
|
|
}
|
|
|
|
});
|
2022-08-29 21:43:17 -04:00
|
|
|
|
|
|
|
PromisePrototypeThen(this[_closeSent].promise, () => {
|
|
|
|
if (this[_closed].state === "pending") {
|
|
|
|
return pull(controller);
|
|
|
|
}
|
|
|
|
});
|
2021-08-09 18:28:17 -04:00
|
|
|
},
|
2022-07-18 16:49:49 -04:00
|
|
|
pull,
|
2021-08-09 18:28:17 -04:00
|
|
|
cancel: async (reason) => {
|
|
|
|
try {
|
|
|
|
this.close(reason?.code !== undefined ? reason : {});
|
|
|
|
} catch (_) {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
await this.closed;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this[_connection].resolve({
|
|
|
|
readable,
|
|
|
|
writable,
|
|
|
|
extensions: create.extensions ?? "",
|
|
|
|
protocol: create.protocol ?? "",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(err) => {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(core.InterruptedPrototype, err)) {
|
2021-12-16 06:57:26 -05:00
|
|
|
// The signal was aborted.
|
|
|
|
err = options.signal.reason;
|
|
|
|
} else {
|
|
|
|
core.tryClose(cancelRid);
|
|
|
|
}
|
2021-08-09 18:28:17 -04:00
|
|
|
this[_connection].reject(err);
|
|
|
|
this[_closed].reject(err);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[_connection] = new Deferred();
|
|
|
|
get connection() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketStreamPrototype);
|
2021-08-09 18:28:17 -04:00
|
|
|
return this[_connection].promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
[_earlyClose] = false;
|
|
|
|
[_closed] = new Deferred();
|
2022-08-29 21:43:17 -04:00
|
|
|
[_closeSent] = new Deferred();
|
2021-08-09 18:28:17 -04:00
|
|
|
get closed() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketStreamPrototype);
|
2021-08-09 18:28:17 -04:00
|
|
|
return this[_closed].promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(closeInfo) {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, WebSocketStreamPrototype);
|
2021-08-09 18:28:17 -04:00
|
|
|
closeInfo = webidl.converters.WebSocketCloseInfo(closeInfo, {
|
|
|
|
prefix: "Failed to execute 'close' on 'WebSocketStream'",
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
|
|
|
if (
|
|
|
|
closeInfo.code &&
|
|
|
|
!(closeInfo.code === 1000 ||
|
|
|
|
(3000 <= closeInfo.code && closeInfo.code < 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 && encoder.encode(closeInfo.reason).byteLength > 123
|
|
|
|
) {
|
|
|
|
throw new DOMException(
|
|
|
|
"The close reason may not be longer than 123 bytes.",
|
|
|
|
"SyntaxError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let code = closeInfo.code;
|
|
|
|
if (closeInfo.reason && code === undefined) {
|
|
|
|
code = 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this[_connection].state === "pending") {
|
|
|
|
this[_earlyClose] = true;
|
|
|
|
} else if (this[_closed].state === "pending") {
|
2022-08-29 21:43:17 -04:00
|
|
|
PromisePrototypeThen(
|
2022-04-04 05:49:50 -04:00
|
|
|
core.opAsync("op_ws_close", this[_rid], code, closeInfo.reason),
|
2022-08-29 21:43:17 -04:00
|
|
|
() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
this[_closeSent].resolve(new Date().getTime());
|
|
|
|
}, 0);
|
|
|
|
},
|
2021-08-09 18:28:17 -04:00
|
|
|
(err) => {
|
2021-09-10 20:54:37 -04:00
|
|
|
this[_rid] && core.tryClose(this[_rid]);
|
2021-08-09 18:28:17 -04:00
|
|
|
this[_closed].reject(err);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[SymbolFor("Deno.customInspect")](inspect) {
|
|
|
|
return `${this.constructor.name} ${
|
|
|
|
inspect({
|
|
|
|
url: this.url,
|
|
|
|
})
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 12:06:11 -05:00
|
|
|
const WebSocketStreamPrototype = WebSocketStream.prototype;
|
|
|
|
|
2021-08-09 18:28:17 -04:00
|
|
|
window.__bootstrap.webSocket.WebSocketStream = WebSocketStream;
|
|
|
|
})(this);
|