2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const { BadResourcePrototype, InterruptedPrototype, ops } = core;
|
2022-12-28 04:29:48 -05:00
|
|
|
const {
|
|
|
|
readableStreamForRidUnrefable,
|
|
|
|
readableStreamForRidUnrefableRef,
|
|
|
|
readableStreamForRidUnrefableUnref,
|
|
|
|
writableStreamForRid,
|
|
|
|
} = window.__bootstrap.streams;
|
2021-07-03 11:02:14 -04:00
|
|
|
const {
|
2022-03-22 23:04:20 -04:00
|
|
|
Error,
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2021-07-03 11:02:14 -04:00
|
|
|
PromiseResolve,
|
2021-07-03 11:41:49 -04:00
|
|
|
SymbolAsyncIterator,
|
2022-03-22 23:04:20 -04:00
|
|
|
SymbolFor,
|
2021-07-03 11:02:14 -04:00
|
|
|
TypedArrayPrototypeSubarray,
|
2022-10-25 16:50:55 -04:00
|
|
|
TypeError,
|
2022-03-22 23:04:20 -04:00
|
|
|
Uint8Array,
|
2021-07-03 11:02:14 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2021-06-28 19:43:03 -04:00
|
|
|
|
2022-03-22 23:04:20 -04:00
|
|
|
const promiseIdSymbol = SymbolFor("Deno.core.internalPromiseId");
|
|
|
|
|
2021-06-28 19:43:03 -04:00
|
|
|
async function write(rid, data) {
|
2021-11-09 13:26:17 -05:00
|
|
|
return await core.write(rid, data);
|
2021-06-28 19:43:03 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-12 19:17:31 -05:00
|
|
|
function shutdown(rid) {
|
2021-11-09 13:26:17 -05:00
|
|
|
return core.shutdown(rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-19 09:39:04 -05:00
|
|
|
function resolveDns(query, recordType, options) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opAsync("op_dns_resolve", { query, recordType, options });
|
2021-01-19 09:39:04 -05:00
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
class Conn {
|
|
|
|
#rid = 0;
|
|
|
|
#remoteAddr = null;
|
|
|
|
#localAddr = null;
|
2022-12-28 04:29:48 -05:00
|
|
|
#unref = false;
|
|
|
|
#pendingReadPromiseIds = [];
|
2022-02-15 07:35:22 -05:00
|
|
|
|
|
|
|
#readable;
|
|
|
|
#writable;
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
constructor(rid, remoteAddr, localAddr) {
|
2020-07-19 13:49:44 -04:00
|
|
|
this.#rid = rid;
|
|
|
|
this.#remoteAddr = remoteAddr;
|
|
|
|
this.#localAddr = localAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
get remoteAddr() {
|
|
|
|
return this.#remoteAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
get localAddr() {
|
|
|
|
return this.#localAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
write(p) {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2022-12-28 04:29:48 -05:00
|
|
|
async read(buffer) {
|
|
|
|
if (buffer.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const promise = core.read(this.rid, buffer);
|
|
|
|
const promiseId = promise[promiseIdSymbol];
|
|
|
|
if (this.#unref) core.unrefOp(promiseId);
|
|
|
|
this.#pendingReadPromiseIds.push(promiseId);
|
|
|
|
let nread;
|
|
|
|
try {
|
|
|
|
nread = await promise;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
} finally {
|
|
|
|
this.#pendingReadPromiseIds = this.#pendingReadPromiseIds.filter((id) =>
|
|
|
|
id !== promiseId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return nread === 0 ? null : nread;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
closeWrite() {
|
2021-01-14 23:32:27 -05:00
|
|
|
return shutdown(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2022-01-31 10:36:54 -05:00
|
|
|
|
2022-02-15 07:35:22 -05:00
|
|
|
get readable() {
|
|
|
|
if (this.#readable === undefined) {
|
2022-12-28 04:29:48 -05:00
|
|
|
this.#readable = readableStreamForRidUnrefable(this.rid);
|
|
|
|
if (this.#unref) {
|
|
|
|
readableStreamForRidUnrefableUnref(this.#readable);
|
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
}
|
|
|
|
return this.#readable;
|
|
|
|
}
|
|
|
|
|
|
|
|
get writable() {
|
|
|
|
if (this.#writable === undefined) {
|
|
|
|
this.#writable = writableStreamForRid(this.rid);
|
|
|
|
}
|
|
|
|
return this.#writable;
|
|
|
|
}
|
2022-12-28 04:29:48 -05:00
|
|
|
|
|
|
|
ref() {
|
|
|
|
this.#unref = false;
|
|
|
|
if (this.#readable) {
|
|
|
|
readableStreamForRidUnrefableRef(this.#readable);
|
|
|
|
}
|
|
|
|
this.#pendingReadPromiseIds.forEach((id) => core.refOp(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
unref() {
|
|
|
|
this.#unref = true;
|
|
|
|
if (this.#readable) {
|
|
|
|
readableStreamForRidUnrefableUnref(this.#readable);
|
|
|
|
}
|
|
|
|
this.#pendingReadPromiseIds.forEach((id) => core.unrefOp(id));
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-02-27 09:18:30 -05:00
|
|
|
class TcpConn extends Conn {
|
2022-12-13 18:54:11 -05:00
|
|
|
setNoDelay(noDelay = true) {
|
|
|
|
return ops.op_set_nodelay(this.rid, noDelay);
|
2022-02-27 09:18:30 -05:00
|
|
|
}
|
|
|
|
|
2022-12-13 18:54:11 -05:00
|
|
|
setKeepAlive(keepAlive = true) {
|
|
|
|
return ops.op_set_keepalive(this.rid, keepAlive);
|
2022-02-27 09:18:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 14:33:13 -05:00
|
|
|
class UnixConn extends Conn {}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
class Listener {
|
|
|
|
#rid = 0;
|
|
|
|
#addr = null;
|
2022-03-22 23:04:20 -04:00
|
|
|
#unref = false;
|
|
|
|
#promiseId = null;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
constructor(rid, addr) {
|
|
|
|
this.#rid = rid;
|
|
|
|
this.#addr = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
get addr() {
|
|
|
|
return this.#addr;
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
async accept() {
|
|
|
|
let promise;
|
|
|
|
switch (this.addr.transport) {
|
|
|
|
case "tcp":
|
|
|
|
promise = core.opAsync("op_net_accept_tcp", this.rid);
|
|
|
|
break;
|
|
|
|
case "unix":
|
|
|
|
promise = core.opAsync("op_net_accept_unix", this.rid);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported transport: ${this.addr.transport}`);
|
|
|
|
}
|
2022-03-22 23:04:20 -04:00
|
|
|
this.#promiseId = promise[promiseIdSymbol];
|
2022-10-25 16:50:55 -04:00
|
|
|
if (this.#unref) core.unrefOp(this.#promiseId);
|
2023-01-16 11:17:18 -05:00
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await promise;
|
2022-10-25 16:50:55 -04:00
|
|
|
this.#promiseId = null;
|
|
|
|
if (this.addr.transport == "tcp") {
|
|
|
|
localAddr.transport = "tcp";
|
|
|
|
remoteAddr.transport = "tcp";
|
|
|
|
return new TcpConn(rid, remoteAddr, localAddr);
|
|
|
|
} else if (this.addr.transport == "unix") {
|
|
|
|
return new UnixConn(
|
|
|
|
rid,
|
|
|
|
{ transport: "unix", path: remoteAddr },
|
|
|
|
{ transport: "unix", path: localAddr },
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw new Error("unreachable");
|
2022-02-27 09:18:30 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async next() {
|
|
|
|
let conn;
|
|
|
|
try {
|
|
|
|
conn = await this.accept();
|
|
|
|
} catch (error) {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(InterruptedPrototype, error)
|
|
|
|
) {
|
2020-07-19 13:49:44 -04:00
|
|
|
return { value: undefined, done: true };
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return { value: conn, done: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
return(value) {
|
|
|
|
this.close();
|
2021-07-03 11:02:14 -04:00
|
|
|
return PromiseResolve({ value, done: true });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-07-03 11:41:49 -04:00
|
|
|
[SymbolAsyncIterator]() {
|
2020-07-19 13:49:44 -04:00
|
|
|
return this;
|
|
|
|
}
|
2022-03-22 23:04:20 -04:00
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
ref() {
|
2022-03-22 23:04:20 -04:00
|
|
|
this.#unref = false;
|
|
|
|
if (typeof this.#promiseId === "number") {
|
|
|
|
core.refOp(this.#promiseId);
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 16:50:55 -04:00
|
|
|
|
|
|
|
unref() {
|
|
|
|
this.#unref = true;
|
2022-03-22 23:04:20 -04:00
|
|
|
if (typeof this.#promiseId === "number") {
|
|
|
|
core.unrefOp(this.#promiseId);
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Datagram {
|
|
|
|
#rid = 0;
|
|
|
|
#addr = null;
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
constructor(rid, addr, bufSize = 1024) {
|
2020-07-19 13:49:44 -04:00
|
|
|
this.#rid = rid;
|
|
|
|
this.#addr = addr;
|
|
|
|
this.bufSize = bufSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
get addr() {
|
|
|
|
return this.#addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
async receive(p) {
|
|
|
|
const buf = p || new Uint8Array(this.bufSize);
|
2022-10-25 16:50:55 -04:00
|
|
|
let nread;
|
|
|
|
let remoteAddr;
|
|
|
|
switch (this.addr.transport) {
|
|
|
|
case "udp": {
|
2023-01-16 11:17:18 -05:00
|
|
|
({ 0: nread, 1: remoteAddr } = await core.opAsync(
|
2022-10-25 16:50:55 -04:00
|
|
|
"op_net_recv_udp",
|
|
|
|
this.rid,
|
|
|
|
buf,
|
2023-01-16 11:17:18 -05:00
|
|
|
));
|
2022-10-25 16:50:55 -04:00
|
|
|
remoteAddr.transport = "udp";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "unixpacket": {
|
|
|
|
let path;
|
2023-01-16 11:17:18 -05:00
|
|
|
({ 0: nread, 1: path } = await core.opAsync(
|
2022-10-25 16:50:55 -04:00
|
|
|
"op_net_recv_unixpacket",
|
|
|
|
this.rid,
|
|
|
|
buf,
|
2023-01-16 11:17:18 -05:00
|
|
|
));
|
2022-10-25 16:50:55 -04:00
|
|
|
remoteAddr = { transport: "unixpacket", path };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported transport: ${this.addr.transport}`);
|
|
|
|
}
|
|
|
|
const sub = TypedArrayPrototypeSubarray(buf, 0, nread);
|
2020-07-19 13:49:44 -04:00
|
|
|
return [sub, remoteAddr];
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
async send(p, opts) {
|
|
|
|
switch (this.addr.transport) {
|
|
|
|
case "udp":
|
|
|
|
return await core.opAsync(
|
|
|
|
"op_net_send_udp",
|
|
|
|
this.rid,
|
|
|
|
{ hostname: opts.hostname ?? "127.0.0.1", port: opts.port },
|
|
|
|
p,
|
|
|
|
);
|
|
|
|
case "unixpacket":
|
|
|
|
return await core.opAsync(
|
|
|
|
"op_net_send_unixpacket",
|
|
|
|
this.rid,
|
|
|
|
opts.path,
|
|
|
|
p,
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported transport: ${this.addr.transport}`);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-07-04 11:26:38 -04:00
|
|
|
async *[SymbolAsyncIterator]() {
|
2020-07-19 13:49:44 -04:00
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
yield await this.receive();
|
|
|
|
} catch (err) {
|
2022-02-01 12:06:11 -05:00
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(BadResourcePrototype, err) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(InterruptedPrototype, err)
|
|
|
|
) {
|
2020-07-19 13:49:44 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
function listen(args) {
|
|
|
|
switch (args.transport ?? "tcp") {
|
|
|
|
case "tcp": {
|
2023-01-16 11:17:18 -05:00
|
|
|
const { 0: rid, 1: addr } = ops.op_net_listen_tcp({
|
2022-10-25 16:50:55 -04:00
|
|
|
hostname: args.hostname ?? "0.0.0.0",
|
|
|
|
port: args.port,
|
2022-10-26 15:04:27 -04:00
|
|
|
}, args.reusePort);
|
2022-10-25 16:50:55 -04:00
|
|
|
addr.transport = "tcp";
|
|
|
|
return new Listener(rid, addr);
|
|
|
|
}
|
|
|
|
case "unix": {
|
2023-01-16 11:17:18 -05:00
|
|
|
const { 0: rid, 1: path } = ops.op_net_listen_unix(args.path);
|
2022-10-25 16:50:55 -04:00
|
|
|
const addr = {
|
|
|
|
transport: "unix",
|
|
|
|
path,
|
|
|
|
};
|
|
|
|
return new Listener(rid, addr);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2022-11-10 16:03:28 -05:00
|
|
|
function createListenDatagram(udpOpFn, unixOpFn) {
|
|
|
|
return function listenDatagram(args) {
|
|
|
|
switch (args.transport) {
|
|
|
|
case "udp": {
|
2023-01-16 11:17:18 -05:00
|
|
|
const { 0: rid, 1: addr } = udpOpFn(
|
2022-11-10 16:03:28 -05:00
|
|
|
{
|
|
|
|
hostname: args.hostname ?? "127.0.0.1",
|
|
|
|
port: args.port,
|
|
|
|
},
|
|
|
|
args.reuseAddress ?? false,
|
|
|
|
);
|
|
|
|
addr.transport = "udp";
|
|
|
|
return new Datagram(rid, addr);
|
|
|
|
}
|
|
|
|
case "unixpacket": {
|
2023-01-16 11:17:18 -05:00
|
|
|
const { 0: rid, 1: path } = unixOpFn(args.path);
|
2022-11-10 16:03:28 -05:00
|
|
|
const addr = {
|
|
|
|
transport: "unixpacket",
|
|
|
|
path,
|
|
|
|
};
|
|
|
|
return new Datagram(rid, addr);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
2022-10-25 16:50:55 -04:00
|
|
|
}
|
2022-11-10 16:03:28 -05:00
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
async function connect(args) {
|
|
|
|
switch (args.transport ?? "tcp") {
|
|
|
|
case "tcp": {
|
2023-01-16 11:17:18 -05:00
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await core.opAsync(
|
2022-10-25 16:50:55 -04:00
|
|
|
"op_net_connect_tcp",
|
|
|
|
{
|
|
|
|
hostname: args.hostname ?? "127.0.0.1",
|
|
|
|
port: args.port,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
localAddr.transport = "tcp";
|
|
|
|
remoteAddr.transport = "tcp";
|
|
|
|
return new TcpConn(rid, remoteAddr, localAddr);
|
|
|
|
}
|
|
|
|
case "unix": {
|
2023-01-16 11:17:18 -05:00
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await core.opAsync(
|
2022-10-25 16:50:55 -04:00
|
|
|
"op_net_connect_unix",
|
|
|
|
args.path,
|
|
|
|
);
|
|
|
|
return new UnixConn(
|
|
|
|
rid,
|
|
|
|
{ transport: "unix", path: remoteAddr },
|
|
|
|
{ transport: "unix", path: localAddr },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2022-10-25 16:50:55 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2022-10-25 16:50:55 -04:00
|
|
|
function setup(unstable) {
|
|
|
|
if (!unstable) {
|
|
|
|
delete Listener.prototype.ref;
|
|
|
|
delete Listener.prototype.unref;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.net = {
|
2022-10-25 16:50:55 -04:00
|
|
|
setup,
|
2020-07-19 13:49:44 -04:00
|
|
|
connect,
|
|
|
|
Conn,
|
2022-02-27 09:18:30 -05:00
|
|
|
TcpConn,
|
2022-03-04 14:33:13 -05:00
|
|
|
UnixConn,
|
2020-07-19 13:49:44 -04:00
|
|
|
listen,
|
2022-11-10 16:03:28 -05:00
|
|
|
createListenDatagram,
|
2020-07-19 13:49:44 -04:00
|
|
|
Listener,
|
|
|
|
shutdown,
|
|
|
|
Datagram,
|
2021-01-19 09:39:04 -05:00
|
|
|
resolveDns,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|