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-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2022-02-01 12:06:11 -05:00
|
|
|
const { BadResourcePrototype, InterruptedPrototype } = core;
|
2022-04-20 20:22:55 -04:00
|
|
|
const { ReadableStream, WritableStream } = 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,
|
2022-03-22 23:04:20 -04:00
|
|
|
Symbol,
|
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-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 read(
|
|
|
|
rid,
|
|
|
|
buffer,
|
|
|
|
) {
|
|
|
|
if (buffer.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-11-09 13:26:17 -05:00
|
|
|
const nread = await core.read(rid, buffer);
|
2021-06-28 19:43:03 -04:00
|
|
|
return nread === 0 ? null : nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
function opAccept(rid, transport) {
|
2021-10-30 12:51:42 -04:00
|
|
|
return core.opAsync("op_net_accept", { rid, transport });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opListen(args) {
|
2021-10-30 12:51:42 -04:00
|
|
|
return core.opSync("op_net_listen", args);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opConnect(args) {
|
2021-10-30 12:51:42 -04:00
|
|
|
return core.opAsync("op_net_connect", args);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
function opReceive(rid, transport, zeroCopy) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opAsync(
|
2021-10-30 12:51:42 -04:00
|
|
|
"op_dgram_recv",
|
2020-09-16 16:22:43 -04:00
|
|
|
{ rid, transport },
|
|
|
|
zeroCopy,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opSend(args, zeroCopy) {
|
2021-10-30 12:51:42 -04:00
|
|
|
return core.opAsync("op_dgram_send", args, zeroCopy);
|
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
|
|
|
}
|
|
|
|
|
2022-04-20 20:22:55 -04:00
|
|
|
const DEFAULT_CHUNK_SIZE = 64 * 1024;
|
|
|
|
|
2022-02-15 07:35:22 -05:00
|
|
|
function tryClose(rid) {
|
|
|
|
try {
|
|
|
|
core.close(rid);
|
|
|
|
} catch {
|
|
|
|
// Ignore errors
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:22:55 -04:00
|
|
|
function readableStreamForRid(rid) {
|
|
|
|
return new ReadableStream({
|
|
|
|
type: "bytes",
|
|
|
|
async pull(controller) {
|
|
|
|
const v = controller.byobRequest.view;
|
|
|
|
try {
|
|
|
|
const bytesRead = await read(rid, v);
|
|
|
|
if (bytesRead === null) {
|
|
|
|
tryClose(rid);
|
|
|
|
controller.close();
|
|
|
|
controller.byobRequest.respond(0);
|
|
|
|
} else {
|
|
|
|
controller.byobRequest.respond(bytesRead);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
controller.error(e);
|
|
|
|
tryClose(rid);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cancel() {
|
|
|
|
tryClose(rid);
|
|
|
|
},
|
|
|
|
autoAllocateChunkSize: DEFAULT_CHUNK_SIZE,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-15 07:35:22 -05:00
|
|
|
function writableStreamForRid(rid) {
|
|
|
|
return new WritableStream({
|
|
|
|
async write(chunk, controller) {
|
|
|
|
try {
|
|
|
|
let nwritten = 0;
|
|
|
|
while (nwritten < chunk.length) {
|
|
|
|
nwritten += await write(
|
|
|
|
rid,
|
|
|
|
TypedArrayPrototypeSubarray(chunk, nwritten),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
controller.error(e);
|
|
|
|
tryClose(rid);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
close() {
|
|
|
|
tryClose(rid);
|
|
|
|
},
|
|
|
|
abort() {
|
|
|
|
tryClose(rid);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
class Conn {
|
|
|
|
#rid = 0;
|
|
|
|
#remoteAddr = null;
|
|
|
|
#localAddr = null;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
read(p) {
|
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
this.#readable = readableStreamForRid(this.rid);
|
|
|
|
}
|
|
|
|
return this.#readable;
|
|
|
|
}
|
|
|
|
|
|
|
|
get writable() {
|
|
|
|
if (this.#writable === undefined) {
|
|
|
|
this.#writable = writableStreamForRid(this.rid);
|
|
|
|
}
|
|
|
|
return this.#writable;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-02-27 09:18:30 -05:00
|
|
|
class TcpConn extends Conn {
|
|
|
|
setNoDelay(nodelay = true) {
|
|
|
|
return core.opSync("op_set_nodelay", this.rid, nodelay);
|
|
|
|
}
|
|
|
|
|
|
|
|
setKeepAlive(keepalive = true) {
|
|
|
|
return core.opSync("op_set_keepalive", this.rid, keepalive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 14:33:13 -05:00
|
|
|
class UnixConn extends Conn {}
|
|
|
|
|
2022-03-22 23:04:20 -04:00
|
|
|
// Use symbols for method names to hide these in stable API.
|
|
|
|
// TODO(kt3k): Remove these symbols when ref/unref become stable.
|
|
|
|
const listenerRef = Symbol("listenerRef");
|
|
|
|
const listenerUnref = Symbol("listenerUnref");
|
|
|
|
|
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-03-22 23:04:20 -04:00
|
|
|
accept() {
|
|
|
|
const promise = opAccept(this.rid, this.addr.transport);
|
|
|
|
this.#promiseId = promise[promiseIdSymbol];
|
|
|
|
if (this.#unref) {
|
|
|
|
this.#unrefOpAccept();
|
2022-02-27 09:18:30 -05:00
|
|
|
}
|
2022-03-22 23:04:20 -04:00
|
|
|
return promise.then((res) => {
|
|
|
|
if (this.addr.transport == "tcp") {
|
|
|
|
return new TcpConn(res.rid, res.remoteAddr, res.localAddr);
|
|
|
|
} else if (this.addr.transport == "unix") {
|
|
|
|
return new UnixConn(res.rid, res.remoteAddr, res.localAddr);
|
|
|
|
} else {
|
|
|
|
throw new Error("unreachable");
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
|
|
|
[listenerRef]() {
|
|
|
|
this.#unref = false;
|
|
|
|
this.#refOpAccept();
|
|
|
|
}
|
|
|
|
|
|
|
|
[listenerUnref]() {
|
|
|
|
this.#unref = true;
|
|
|
|
this.#unrefOpAccept();
|
|
|
|
}
|
|
|
|
|
|
|
|
#refOpAccept() {
|
|
|
|
if (typeof this.#promiseId === "number") {
|
|
|
|
core.refOp(this.#promiseId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#unrefOpAccept() {
|
|
|
|
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);
|
|
|
|
const { size, remoteAddr } = await opReceive(
|
|
|
|
this.rid,
|
|
|
|
this.addr.transport,
|
|
|
|
buf,
|
|
|
|
);
|
2021-07-03 11:02:14 -04:00
|
|
|
const sub = TypedArrayPrototypeSubarray(buf, 0, size);
|
2020-07-19 13:49:44 -04:00
|
|
|
return [sub, remoteAddr];
|
|
|
|
}
|
|
|
|
|
|
|
|
send(p, addr) {
|
2022-03-19 05:26:54 -04:00
|
|
|
const args = { hostname: "127.0.0.1", ...addr, rid: this.rid };
|
2020-07-19 13:49:44 -04:00
|
|
|
return opSend(args, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-22 23:04:20 -04:00
|
|
|
function listen({ hostname, ...options }, constructor = Listener) {
|
2020-07-19 13:49:44 -04:00
|
|
|
const res = opListen({
|
|
|
|
transport: "tcp",
|
2020-09-27 10:44:53 -04:00
|
|
|
hostname: typeof hostname === "undefined" ? "0.0.0.0" : hostname,
|
2020-07-19 13:49:44 -04:00
|
|
|
...options,
|
|
|
|
});
|
|
|
|
|
2022-03-22 23:04:20 -04:00
|
|
|
return new constructor(res.rid, res.localAddr);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2020-12-16 11:14:12 -05:00
|
|
|
async function connect(options) {
|
2020-07-19 13:49:44 -04:00
|
|
|
if (options.transport === "unix") {
|
2022-03-04 14:33:13 -05:00
|
|
|
const res = await opConnect(options);
|
|
|
|
return new UnixConn(res.rid, res.remoteAddr, res.localAddr);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-03-04 14:33:13 -05:00
|
|
|
const res = await opConnect({
|
|
|
|
transport: "tcp",
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
...options,
|
|
|
|
});
|
2022-02-27 09:18:30 -05:00
|
|
|
return new TcpConn(res.rid, res.remoteAddr, res.localAddr);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.net = {
|
|
|
|
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
|
|
|
opConnect,
|
|
|
|
listen,
|
2022-03-22 23:04:20 -04:00
|
|
|
listenerRef,
|
|
|
|
listenerUnref,
|
2020-07-19 13:49:44 -04:00
|
|
|
opListen,
|
|
|
|
Listener,
|
|
|
|
shutdown,
|
|
|
|
Datagram,
|
2021-01-19 09:39:04 -05:00
|
|
|
resolveDns,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
2022-02-15 07:35:22 -05:00
|
|
|
window.__bootstrap.streamUtils = {
|
|
|
|
readableStreamForRid,
|
|
|
|
writableStreamForRid,
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
})(this);
|