2020-07-19 13:49:44 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2020-07-19 13:49:44 -04:00
|
|
|
const { errors } = window.__bootstrap.errors;
|
|
|
|
const { read, write } = window.__bootstrap.io;
|
|
|
|
|
|
|
|
const ShutdownMode = {
|
|
|
|
// See http://man7.org/linux/man-pages/man2/shutdown.2.html
|
|
|
|
// Corresponding to SHUT_RD, SHUT_WR, SHUT_RDWR
|
|
|
|
0: "Read",
|
|
|
|
1: "Write",
|
|
|
|
2: "ReadWrite",
|
|
|
|
Read: 0,
|
|
|
|
Write: 1,
|
|
|
|
ReadWrite: 2, // unused
|
|
|
|
};
|
|
|
|
|
|
|
|
function shutdown(rid, how) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_shutdown", { rid, how });
|
2020-07-19 13:49:44 -04:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
function opAccept(
|
|
|
|
rid,
|
|
|
|
transport,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpAsync("op_accept", { rid, transport });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opListen(args) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpSync("op_listen", args);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opConnect(args) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpAsync("op_connect", args);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opReceive(
|
|
|
|
rid,
|
|
|
|
transport,
|
|
|
|
zeroCopy,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpAsync(
|
|
|
|
"op_datagram_receive",
|
|
|
|
{ rid, transport },
|
|
|
|
zeroCopy,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function opSend(args, zeroCopy) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpAsync("op_datagram_send", args, zeroCopy);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class Conn {
|
|
|
|
#rid = 0;
|
|
|
|
#remoteAddr = null;
|
|
|
|
#localAddr = null;
|
|
|
|
constructor(
|
|
|
|
rid,
|
|
|
|
remoteAddr,
|
|
|
|
localAddr,
|
|
|
|
) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(lucacasonato): make this unavailable in stable
|
|
|
|
closeWrite() {
|
|
|
|
shutdown(this.rid, ShutdownMode.Write);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Listener {
|
|
|
|
#rid = 0;
|
|
|
|
#addr = null;
|
|
|
|
|
|
|
|
constructor(rid, addr) {
|
|
|
|
this.#rid = rid;
|
|
|
|
this.#addr = addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
get addr() {
|
|
|
|
return this.#addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
async accept() {
|
|
|
|
const res = await opAccept(this.rid, this.addr.transport);
|
|
|
|
return new Conn(res.rid, res.remoteAddr, res.localAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
async next() {
|
|
|
|
let conn;
|
|
|
|
try {
|
|
|
|
conn = await this.accept();
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof errors.BadResource) {
|
|
|
|
return { value: undefined, done: true };
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
return { value: conn, done: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
return(value) {
|
|
|
|
this.close();
|
|
|
|
return Promise.resolve({ value, done: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2020-09-17 12:09:50 -04:00
|
|
|
core.close(this.rid);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
[Symbol.asyncIterator]() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Datagram {
|
|
|
|
#rid = 0;
|
|
|
|
#addr = null;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
rid,
|
|
|
|
addr,
|
|
|
|
bufSize = 1024,
|
|
|
|
) {
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
const sub = buf.subarray(0, size);
|
|
|
|
return [sub, remoteAddr];
|
|
|
|
}
|
|
|
|
|
|
|
|
send(p, addr) {
|
|
|
|
const remote = { hostname: "127.0.0.1", ...addr };
|
|
|
|
|
|
|
|
const args = { ...remote, rid: this.rid };
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
async *[Symbol.asyncIterator]() {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
yield await this.receive();
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof errors.BadResource) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 10:44:53 -04:00
|
|
|
function listen({ hostname, ...options }) {
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Listener(res.rid, res.localAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function connect(
|
|
|
|
options,
|
|
|
|
) {
|
|
|
|
let res;
|
|
|
|
|
|
|
|
if (options.transport === "unix") {
|
|
|
|
res = await opConnect(options);
|
|
|
|
} else {
|
|
|
|
res = await opConnect({
|
|
|
|
transport: "tcp",
|
|
|
|
hostname: "127.0.0.1",
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Conn(res.rid, res.remoteAddr, res.localAddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.net = {
|
|
|
|
connect,
|
|
|
|
Conn,
|
|
|
|
opConnect,
|
|
|
|
listen,
|
|
|
|
opListen,
|
|
|
|
Listener,
|
|
|
|
shutdown,
|
|
|
|
ShutdownMode,
|
|
|
|
Datagram,
|
|
|
|
};
|
|
|
|
})(this);
|