2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2024-01-24 12:52:55 -05:00
|
|
|
import { core, internals, primordials } from "ext:core/mod.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
BadResourcePrototype,
|
|
|
|
InterruptedPrototype,
|
2024-01-26 14:04:07 -05:00
|
|
|
internalRidSymbol,
|
2024-01-10 17:37:25 -05:00
|
|
|
} = core;
|
2023-12-26 20:30:26 -05:00
|
|
|
const {
|
|
|
|
op_dns_resolve,
|
|
|
|
op_net_accept_tcp,
|
|
|
|
op_net_accept_unix,
|
|
|
|
op_net_connect_tcp,
|
|
|
|
op_net_connect_unix,
|
2024-01-10 17:37:25 -05:00
|
|
|
op_net_join_multi_v4_udp,
|
|
|
|
op_net_join_multi_v6_udp,
|
|
|
|
op_net_leave_multi_v4_udp,
|
|
|
|
op_net_leave_multi_v6_udp,
|
|
|
|
op_net_listen_tcp,
|
|
|
|
op_net_listen_unix,
|
2023-12-26 20:30:26 -05:00
|
|
|
op_net_recv_udp,
|
|
|
|
op_net_recv_unixpacket,
|
|
|
|
op_net_send_udp,
|
|
|
|
op_net_send_unixpacket,
|
|
|
|
op_net_set_multi_loopback_udp,
|
|
|
|
op_net_set_multi_ttl_udp,
|
2024-01-10 17:37:25 -05:00
|
|
|
op_set_keepalive,
|
|
|
|
op_set_nodelay,
|
2023-12-26 20:30:26 -05:00
|
|
|
} = core.ensureFastOps();
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
op_cancel_handle,
|
|
|
|
} = core.ensureFastOps(true);
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
Error,
|
2023-07-25 02:26:18 -04:00
|
|
|
Number,
|
2023-02-07 14:22:46 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2024-01-26 17:19:00 -05:00
|
|
|
ObjectDefineProperty,
|
2023-02-07 14:22:46 -05:00
|
|
|
PromiseResolve,
|
2023-11-09 15:57:26 -05:00
|
|
|
SafeSet,
|
|
|
|
SetPrototypeAdd,
|
|
|
|
SetPrototypeDelete,
|
|
|
|
SetPrototypeForEach,
|
2023-02-07 14:22:46 -05:00
|
|
|
SymbolAsyncIterator,
|
2023-10-03 22:37:39 -04:00
|
|
|
Symbol,
|
2023-02-07 14:22:46 -05:00
|
|
|
TypeError,
|
2023-05-01 09:30:02 -04:00
|
|
|
TypedArrayPrototypeSubarray,
|
2023-02-07 14:22:46 -05:00
|
|
|
Uint8Array,
|
|
|
|
} = primordials;
|
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
import {
|
|
|
|
readableStreamForRidUnrefable,
|
|
|
|
readableStreamForRidUnrefableRef,
|
|
|
|
readableStreamForRidUnrefableUnref,
|
|
|
|
writableStreamForRid,
|
|
|
|
} from "ext:deno_web/06_streams.js";
|
|
|
|
import * as abortSignal from "ext:deno_web/03_abort_signal.js";
|
|
|
|
import { SymbolDispose } from "ext:deno_web/00_infra.js";
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async function write(rid, data) {
|
|
|
|
return await core.write(rid, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function shutdown(rid) {
|
|
|
|
return core.shutdown(rid);
|
|
|
|
}
|
|
|
|
|
2023-02-11 09:14:02 -05:00
|
|
|
async function resolveDns(query, recordType, options) {
|
|
|
|
let cancelRid;
|
|
|
|
let abortHandler;
|
|
|
|
if (options?.signal) {
|
|
|
|
options.signal.throwIfAborted();
|
2024-01-10 17:37:25 -05:00
|
|
|
cancelRid = op_cancel_handle();
|
2023-02-11 09:14:02 -05:00
|
|
|
abortHandler = () => core.tryClose(cancelRid);
|
|
|
|
options.signal[abortSignal.add](abortHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-12-26 20:30:26 -05:00
|
|
|
return await op_dns_resolve({
|
2023-02-11 09:14:02 -05:00
|
|
|
cancelRid,
|
|
|
|
query,
|
|
|
|
recordType,
|
|
|
|
options,
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
if (options?.signal) {
|
|
|
|
options.signal[abortSignal.remove](abortHandler);
|
|
|
|
|
|
|
|
// always throw the abort error when aborted
|
|
|
|
options.signal.throwIfAborted();
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
class Conn {
|
|
|
|
#rid = 0;
|
|
|
|
#remoteAddr = null;
|
|
|
|
#localAddr = null;
|
|
|
|
#unref = false;
|
2023-11-09 15:57:26 -05:00
|
|
|
#pendingReadPromises = new SafeSet();
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
#readable;
|
|
|
|
#writable;
|
|
|
|
|
|
|
|
constructor(rid, remoteAddr, localAddr) {
|
2024-01-26 17:19:00 -05:00
|
|
|
ObjectDefineProperty(this, internalRidSymbol, {
|
|
|
|
enumerable: false,
|
|
|
|
value: rid,
|
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
this.#rid = rid;
|
|
|
|
this.#remoteAddr = remoteAddr;
|
|
|
|
this.#localAddr = localAddr;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get rid() {
|
2024-01-24 16:12:10 -05:00
|
|
|
internals.warnOnDeprecatedApi(
|
|
|
|
"Deno.Conn.rid",
|
|
|
|
new Error().stack,
|
|
|
|
"Use `Deno.Conn` instance methods instead.",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.#rid;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get remoteAddr() {
|
|
|
|
return this.#remoteAddr;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get localAddr() {
|
|
|
|
return this.#localAddr;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
write(p) {
|
2024-01-24 16:12:10 -05:00
|
|
|
return write(this.#rid, p);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async read(buffer) {
|
|
|
|
if (buffer.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2024-01-24 16:12:10 -05:00
|
|
|
const promise = core.read(this.#rid, buffer);
|
2023-11-09 15:57:26 -05:00
|
|
|
if (this.#unref) core.unrefOpPromise(promise);
|
|
|
|
SetPrototypeAdd(this.#pendingReadPromises, promise);
|
2023-02-07 14:22:46 -05:00
|
|
|
let nread;
|
|
|
|
try {
|
|
|
|
nread = await promise;
|
|
|
|
} catch (e) {
|
|
|
|
throw e;
|
|
|
|
} finally {
|
2023-11-09 15:57:26 -05:00
|
|
|
SetPrototypeDelete(this.#pendingReadPromises, promise);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
return nread === 0 ? null : nread;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
2024-01-24 16:12:10 -05:00
|
|
|
core.close(this.#rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-01-31 10:36:54 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
closeWrite() {
|
2024-01-24 16:12:10 -05:00
|
|
|
return shutdown(this.#rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-02-15 07:35:22 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get readable() {
|
|
|
|
if (this.#readable === undefined) {
|
2024-01-24 16:12:10 -05:00
|
|
|
this.#readable = readableStreamForRidUnrefable(this.#rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (this.#unref) {
|
|
|
|
readableStreamForRidUnrefableUnref(this.#readable);
|
2022-02-15 07:35:22 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.#readable;
|
|
|
|
}
|
2022-12-28 04:29:48 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get writable() {
|
|
|
|
if (this.#writable === undefined) {
|
2024-01-24 16:12:10 -05:00
|
|
|
this.#writable = writableStreamForRid(this.#rid);
|
2022-12-28 04:29:48 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.#writable;
|
|
|
|
}
|
2022-12-28 04:29:48 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
ref() {
|
|
|
|
this.#unref = false;
|
|
|
|
if (this.#readable) {
|
|
|
|
readableStreamForRidUnrefableRef(this.#readable);
|
2022-12-28 04:29:48 -05:00
|
|
|
}
|
2023-11-09 15:57:26 -05:00
|
|
|
|
|
|
|
SetPrototypeForEach(
|
|
|
|
this.#pendingReadPromises,
|
|
|
|
(promise) => core.refOpPromise(promise),
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
unref() {
|
|
|
|
this.#unref = true;
|
|
|
|
if (this.#readable) {
|
|
|
|
readableStreamForRidUnrefableUnref(this.#readable);
|
2022-02-27 09:18:30 -05:00
|
|
|
}
|
2023-11-09 15:57:26 -05:00
|
|
|
SetPrototypeForEach(
|
|
|
|
this.#pendingReadPromises,
|
|
|
|
(promise) => core.unrefOpPromise(promise),
|
2023-05-01 09:30:02 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-11-01 15:26:12 -04:00
|
|
|
|
|
|
|
[SymbolDispose]() {
|
|
|
|
core.tryClose(this.#rid);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-02-27 09:18:30 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class TcpConn extends Conn {
|
2024-01-24 16:12:10 -05:00
|
|
|
#rid = 0;
|
|
|
|
|
|
|
|
constructor(rid, remoteAddr, localAddr) {
|
|
|
|
super(rid, remoteAddr, localAddr);
|
2024-01-26 17:19:00 -05:00
|
|
|
ObjectDefineProperty(this, internalRidSymbol, {
|
|
|
|
enumerable: false,
|
|
|
|
value: rid,
|
|
|
|
});
|
2024-01-24 16:12:10 -05:00
|
|
|
this.#rid = rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
internals.warnOnDeprecatedApi(
|
|
|
|
"Deno.TcpConn.rid",
|
|
|
|
new Error().stack,
|
|
|
|
"Use `Deno.TcpConn` instance methods instead.",
|
|
|
|
);
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
setNoDelay(noDelay = true) {
|
2024-01-24 16:12:10 -05:00
|
|
|
return op_set_nodelay(this.#rid, noDelay);
|
2022-02-27 09:18:30 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
setKeepAlive(keepAlive = true) {
|
2024-01-24 16:12:10 -05:00
|
|
|
return op_set_keepalive(this.#rid, keepAlive);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
2022-03-04 14:33:13 -05:00
|
|
|
|
2024-01-24 16:12:10 -05:00
|
|
|
class UnixConn extends Conn {
|
|
|
|
#rid = 0;
|
|
|
|
|
|
|
|
constructor(rid, remoteAddr, localAddr) {
|
|
|
|
super(rid, remoteAddr, localAddr);
|
2024-01-26 17:19:00 -05:00
|
|
|
ObjectDefineProperty(this, internalRidSymbol, {
|
|
|
|
enumerable: false,
|
|
|
|
value: rid,
|
|
|
|
});
|
2024-01-24 16:12:10 -05:00
|
|
|
this.#rid = rid;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
internals.warnOnDeprecatedApi(
|
|
|
|
"Deno.UnixConn.rid",
|
|
|
|
new Error().stack,
|
|
|
|
"Use `Deno.UnixConn` instance methods instead.",
|
|
|
|
);
|
|
|
|
return this.#rid;
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class Listener {
|
|
|
|
#rid = 0;
|
|
|
|
#addr = null;
|
|
|
|
#unref = false;
|
2023-11-09 15:57:26 -05:00
|
|
|
#promise = null;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor(rid, addr) {
|
2024-01-26 17:19:00 -05:00
|
|
|
ObjectDefineProperty(this, internalRidSymbol, {
|
|
|
|
enumerable: false,
|
|
|
|
value: rid,
|
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
this.#rid = rid;
|
|
|
|
this.#addr = addr;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get rid() {
|
2024-01-24 12:52:55 -05:00
|
|
|
internals.warnOnDeprecatedApi(
|
|
|
|
"Deno.Listener.rid",
|
|
|
|
new Error().stack,
|
|
|
|
"Use `Deno.Listener` instance methods instead.",
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
return this.#rid;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get addr() {
|
|
|
|
return this.#addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
async accept() {
|
|
|
|
let promise;
|
|
|
|
switch (this.addr.transport) {
|
|
|
|
case "tcp":
|
2024-01-24 12:52:55 -05:00
|
|
|
promise = op_net_accept_tcp(this.#rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
case "unix":
|
2024-01-24 12:52:55 -05:00
|
|
|
promise = op_net_accept_unix(this.#rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported transport: ${this.addr.transport}`);
|
|
|
|
}
|
2023-11-09 15:57:26 -05:00
|
|
|
this.#promise = promise;
|
|
|
|
if (this.#unref) core.unrefOpPromise(promise);
|
2023-02-07 14:22:46 -05:00
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await promise;
|
2023-11-09 15:57:26 -05:00
|
|
|
this.#promise = null;
|
2023-02-07 14:22:46 -05:00
|
|
|
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");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async next() {
|
|
|
|
let conn;
|
|
|
|
try {
|
|
|
|
conn = await this.accept();
|
|
|
|
} catch (error) {
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(BadResourcePrototype, error) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(InterruptedPrototype, error)
|
|
|
|
) {
|
|
|
|
return { value: undefined, done: true };
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
throw error;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return { value: conn, done: false };
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return(value) {
|
|
|
|
this.close();
|
|
|
|
return PromiseResolve({ value, done: true });
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
2024-01-24 12:52:55 -05:00
|
|
|
core.close(this.#rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-11-01 15:26:12 -04:00
|
|
|
[SymbolDispose]() {
|
|
|
|
core.tryClose(this.#rid);
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolAsyncIterator]() {
|
|
|
|
return this;
|
|
|
|
}
|
2022-03-22 23:04:20 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
ref() {
|
|
|
|
this.#unref = false;
|
2023-11-09 15:57:26 -05:00
|
|
|
if (this.#promise !== null) {
|
|
|
|
core.refOpPromise(this.#promise);
|
2022-03-22 23:04:20 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2022-10-25 16:50:55 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
unref() {
|
|
|
|
this.#unref = true;
|
2023-11-09 15:57:26 -05:00
|
|
|
if (this.#promise !== null) {
|
|
|
|
core.unrefOpPromise(this.#promise);
|
2022-03-22 23:04:20 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class Datagram {
|
|
|
|
#rid = 0;
|
|
|
|
#addr = null;
|
2024-01-03 22:21:39 -05:00
|
|
|
#unref = false;
|
|
|
|
#promise = null;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
constructor(rid, addr, bufSize = 1024) {
|
|
|
|
this.#rid = rid;
|
|
|
|
this.#addr = addr;
|
|
|
|
this.bufSize = bufSize;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get rid() {
|
|
|
|
return this.#rid;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get addr() {
|
|
|
|
return this.#addr;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-03-20 17:27:00 -04:00
|
|
|
async joinMulticastV4(addr, multiInterface) {
|
2023-12-26 20:30:26 -05:00
|
|
|
await op_net_join_multi_v4_udp(
|
2023-03-20 17:27:00 -04:00
|
|
|
this.rid,
|
|
|
|
addr,
|
|
|
|
multiInterface,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
leave: () =>
|
2023-12-26 20:30:26 -05:00
|
|
|
op_net_leave_multi_v4_udp(
|
2023-03-20 17:27:00 -04:00
|
|
|
this.rid,
|
|
|
|
addr,
|
|
|
|
multiInterface,
|
|
|
|
),
|
|
|
|
setLoopback: (loopback) =>
|
2023-12-26 20:30:26 -05:00
|
|
|
op_net_set_multi_loopback_udp(
|
2023-03-20 17:27:00 -04:00
|
|
|
this.rid,
|
|
|
|
true,
|
|
|
|
loopback,
|
|
|
|
),
|
|
|
|
setTTL: (ttl) =>
|
2023-12-26 20:30:26 -05:00
|
|
|
op_net_set_multi_ttl_udp(
|
2023-03-20 17:27:00 -04:00
|
|
|
this.rid,
|
|
|
|
ttl,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async joinMulticastV6(addr, multiInterface) {
|
2023-12-26 20:30:26 -05:00
|
|
|
await op_net_join_multi_v6_udp(
|
2023-03-20 17:27:00 -04:00
|
|
|
this.rid,
|
|
|
|
addr,
|
|
|
|
multiInterface,
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
leave: () =>
|
2023-12-26 20:30:26 -05:00
|
|
|
op_net_leave_multi_v6_udp(
|
2023-03-20 17:27:00 -04:00
|
|
|
this.rid,
|
|
|
|
addr,
|
|
|
|
multiInterface,
|
|
|
|
),
|
|
|
|
setLoopback: (loopback) =>
|
2023-12-26 20:30:26 -05:00
|
|
|
op_net_set_multi_loopback_udp(
|
2023-03-20 17:27:00 -04:00
|
|
|
this.rid,
|
|
|
|
false,
|
|
|
|
loopback,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async receive(p) {
|
|
|
|
const buf = p || new Uint8Array(this.bufSize);
|
|
|
|
let nread;
|
|
|
|
let remoteAddr;
|
|
|
|
switch (this.addr.transport) {
|
|
|
|
case "udp": {
|
2024-01-03 22:21:39 -05:00
|
|
|
this.#promise = op_net_recv_udp(
|
2023-02-07 14:22:46 -05:00
|
|
|
this.rid,
|
|
|
|
buf,
|
2024-01-03 22:21:39 -05:00
|
|
|
);
|
|
|
|
if (this.#unref) core.unrefOpPromise(this.#promise);
|
|
|
|
({ 0: nread, 1: remoteAddr } = await this.#promise);
|
2023-02-07 14:22:46 -05:00
|
|
|
remoteAddr.transport = "udp";
|
|
|
|
break;
|
2022-10-25 16:50:55 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "unixpacket": {
|
|
|
|
let path;
|
2023-12-26 20:30:26 -05:00
|
|
|
({ 0: nread, 1: path } = await op_net_recv_unixpacket(
|
2023-02-07 14:22:46 -05:00
|
|
|
this.rid,
|
|
|
|
buf,
|
|
|
|
));
|
|
|
|
remoteAddr = { transport: "unixpacket", path };
|
|
|
|
break;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default:
|
|
|
|
throw new Error(`Unsupported transport: ${this.addr.transport}`);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const sub = TypedArrayPrototypeSubarray(buf, 0, nread);
|
|
|
|
return [sub, remoteAddr];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async send(p, opts) {
|
|
|
|
switch (this.addr.transport) {
|
|
|
|
case "udp":
|
2023-12-26 20:30:26 -05:00
|
|
|
return await op_net_send_udp(
|
2023-02-07 14:22:46 -05:00
|
|
|
this.rid,
|
|
|
|
{ hostname: opts.hostname ?? "127.0.0.1", port: opts.port },
|
|
|
|
p,
|
|
|
|
);
|
|
|
|
case "unixpacket":
|
2023-12-26 20:30:26 -05:00
|
|
|
return await op_net_send_unixpacket(
|
2023-02-07 14:22:46 -05:00
|
|
|
this.rid,
|
|
|
|
opts.path,
|
|
|
|
p,
|
|
|
|
);
|
2022-10-25 16:50:55 -04:00
|
|
|
default:
|
2023-02-07 14:22:46 -05:00
|
|
|
throw new Error(`Unsupported transport: ${this.addr.transport}`);
|
2022-10-25 16:50:55 -04:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
close() {
|
|
|
|
core.close(this.rid);
|
|
|
|
}
|
|
|
|
|
2024-01-03 22:21:39 -05:00
|
|
|
ref() {
|
|
|
|
this.#unref = false;
|
|
|
|
if (this.#promise !== null) {
|
|
|
|
core.refOpPromise(this.#promise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unref() {
|
|
|
|
this.#unref = true;
|
|
|
|
if (this.#promise !== null) {
|
|
|
|
core.unrefOpPromise(this.#promise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
async *[SymbolAsyncIterator]() {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
yield await this.receive();
|
|
|
|
} catch (err) {
|
|
|
|
if (
|
|
|
|
ObjectPrototypeIsPrototypeOf(BadResourcePrototype, err) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(InterruptedPrototype, err)
|
|
|
|
) {
|
|
|
|
break;
|
2022-11-10 16:03:28 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
throw err;
|
2022-10-25 16:50:55 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 22:37:39 -04:00
|
|
|
const listenOptionApiName = Symbol("listenOptionApiName");
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function listen(args) {
|
|
|
|
switch (args.transport ?? "tcp") {
|
|
|
|
case "tcp": {
|
2024-01-10 17:37:25 -05:00
|
|
|
const { 0: rid, 1: addr } = op_net_listen_tcp({
|
2023-02-07 14:22:46 -05:00
|
|
|
hostname: args.hostname ?? "0.0.0.0",
|
2023-07-25 02:26:18 -04:00
|
|
|
port: Number(args.port),
|
2023-02-07 14:22:46 -05:00
|
|
|
}, args.reusePort);
|
|
|
|
addr.transport = "tcp";
|
|
|
|
return new Listener(rid, addr);
|
|
|
|
}
|
|
|
|
case "unix": {
|
2024-01-10 17:37:25 -05:00
|
|
|
const { 0: rid, 1: path } = op_net_listen_unix(
|
2023-10-03 22:37:39 -04:00
|
|
|
args.path,
|
|
|
|
args[listenOptionApiName] ?? "Deno.listen",
|
|
|
|
);
|
2023-02-07 14:22:46 -05: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
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function createListenDatagram(udpOpFn, unixOpFn) {
|
|
|
|
return function listenDatagram(args) {
|
|
|
|
switch (args.transport) {
|
|
|
|
case "udp": {
|
|
|
|
const { 0: rid, 1: addr } = udpOpFn(
|
2022-10-25 16:50:55 -04:00
|
|
|
{
|
|
|
|
hostname: args.hostname ?? "127.0.0.1",
|
|
|
|
port: args.port,
|
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
args.reuseAddress ?? false,
|
2023-03-20 17:27:00 -04:00
|
|
|
args.loopback ?? false,
|
2022-10-25 16:50:55 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
addr.transport = "udp";
|
|
|
|
return new Datagram(rid, addr);
|
2022-10-25 16:50:55 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case "unixpacket": {
|
|
|
|
const { 0: rid, 1: path } = unixOpFn(args.path);
|
|
|
|
const addr = {
|
|
|
|
transport: "unixpacket",
|
|
|
|
path,
|
|
|
|
};
|
|
|
|
return new Datagram(rid, addr);
|
2022-10-25 16:50:55 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function connect(args) {
|
|
|
|
switch (args.transport ?? "tcp") {
|
|
|
|
case "tcp": {
|
2023-12-26 20:30:26 -05:00
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await op_net_connect_tcp(
|
2023-02-07 14:22:46 -05:00
|
|
|
{
|
|
|
|
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-12-26 20:30:26 -05:00
|
|
|
const { 0: rid, 1: localAddr, 2: remoteAddr } = await op_net_connect_unix(
|
2023-02-07 14:22:46 -05:00
|
|
|
args.path,
|
|
|
|
);
|
|
|
|
return new UnixConn(
|
|
|
|
rid,
|
|
|
|
{ transport: "unix", path: remoteAddr },
|
|
|
|
{ transport: "unix", path: localAddr },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new TypeError(`Unsupported transport: '${transport}'`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
Conn,
|
|
|
|
connect,
|
|
|
|
createListenDatagram,
|
|
|
|
Datagram,
|
|
|
|
listen,
|
|
|
|
Listener,
|
2023-10-03 22:37:39 -04:00
|
|
|
listenOptionApiName,
|
2023-02-07 14:22:46 -05:00
|
|
|
resolveDns,
|
|
|
|
shutdown,
|
|
|
|
TcpConn,
|
|
|
|
UnixConn,
|
|
|
|
};
|