2022-01-20 02:10:16 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-06-28 19:43:03 -04:00
|
|
|
|
|
|
|
/// <reference no-default-lib="true" />
|
|
|
|
/// <reference lib="esnext" />
|
|
|
|
|
|
|
|
declare namespace Deno {
|
|
|
|
export interface NetAddr {
|
|
|
|
transport: "tcp" | "udp";
|
|
|
|
hostname: string;
|
|
|
|
port: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface UnixAddr {
|
|
|
|
transport: "unix" | "unixpacket";
|
|
|
|
path: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Addr = NetAddr | UnixAddr;
|
|
|
|
|
|
|
|
/** A generic network listener for stream-oriented protocols. */
|
|
|
|
export interface Listener extends AsyncIterable<Conn> {
|
|
|
|
/** Waits for and resolves to the next connection to the `Listener`. */
|
|
|
|
accept(): Promise<Conn>;
|
|
|
|
/** Close closes the listener. Any pending accept promises will be rejected
|
2021-09-02 18:28:12 -04:00
|
|
|
* with errors. */
|
2021-06-28 19:43:03 -04:00
|
|
|
close(): void;
|
|
|
|
/** Return the address of the `Listener`. */
|
|
|
|
readonly addr: Addr;
|
|
|
|
|
|
|
|
/** Return the rid of the `Listener`. */
|
|
|
|
readonly rid: number;
|
|
|
|
|
|
|
|
[Symbol.asyncIterator](): AsyncIterableIterator<Conn>;
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:27:47 -04:00
|
|
|
/** Specialized listener that accepts TLS connections. */
|
|
|
|
export interface TlsListener extends Listener, AsyncIterable<TlsConn> {
|
|
|
|
/** Waits for a TLS client to connect and accepts the connection. */
|
|
|
|
accept(): Promise<TlsConn>;
|
|
|
|
[Symbol.asyncIterator](): AsyncIterableIterator<TlsConn>;
|
|
|
|
}
|
|
|
|
|
2021-06-28 19:43:03 -04:00
|
|
|
export interface Conn extends Reader, Writer, Closer {
|
|
|
|
/** The local address of the connection. */
|
|
|
|
readonly localAddr: Addr;
|
|
|
|
/** The remote address of the connection. */
|
|
|
|
readonly remoteAddr: Addr;
|
|
|
|
/** The resource ID of the connection. */
|
|
|
|
readonly rid: number;
|
|
|
|
/** Shuts down (`shutdown(2)`) the write side of the connection. Most
|
2021-09-02 18:28:12 -04:00
|
|
|
* callers should just use `close()`. */
|
2021-06-28 19:43:03 -04:00
|
|
|
closeWrite(): Promise<void>;
|
2022-02-15 07:35:22 -05:00
|
|
|
|
|
|
|
readonly readable: ReadableStream<Uint8Array>;
|
|
|
|
readonly writable: WritableStream<Uint8Array>;
|
2021-06-28 19:43:03 -04:00
|
|
|
}
|
|
|
|
|
2021-11-26 13:59:53 -05:00
|
|
|
// deno-lint-ignore no-empty-interface
|
|
|
|
export interface TlsHandshakeInfo {}
|
|
|
|
|
2021-10-26 16:27:47 -04:00
|
|
|
export interface TlsConn extends Conn {
|
|
|
|
/** Runs the client or server handshake protocol to completion if that has
|
|
|
|
* not happened yet. Calling this method is optional; the TLS handshake
|
|
|
|
* will be completed automatically as soon as data is sent or received. */
|
2021-11-26 13:59:53 -05:00
|
|
|
handshake(): Promise<TlsHandshakeInfo>;
|
2021-10-26 16:27:47 -04:00
|
|
|
}
|
|
|
|
|
2021-06-28 19:43:03 -04:00
|
|
|
export interface ListenOptions {
|
|
|
|
/** The port to listen on. */
|
|
|
|
port: number;
|
|
|
|
/** A literal IP address or host name that can be resolved to an IP address.
|
2021-09-07 03:26:21 -04:00
|
|
|
* If not specified, defaults to `0.0.0.0`.
|
|
|
|
*
|
|
|
|
* __Note about `0.0.0.0`__ While listening `0.0.0.0` works on all platforms,
|
|
|
|
* the browsers on Windows don't work with the address `0.0.0.0`.
|
|
|
|
* You should show the message like `server running on localhost:8080` instead of
|
|
|
|
* `server running on 0.0.0.0:8080` if your program supports Windows. */
|
2021-06-28 19:43:03 -04:00
|
|
|
hostname?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Listen announces on the local transport address.
|
2021-09-02 18:28:12 -04:00
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const listener1 = Deno.listen({ port: 80 })
|
|
|
|
* const listener2 = Deno.listen({ hostname: "192.0.2.1", port: 80 })
|
|
|
|
* const listener3 = Deno.listen({ hostname: "[2001:db8::1]", port: 80 });
|
|
|
|
* const listener4 = Deno.listen({ hostname: "golang.org", port: 80, transport: "tcp" });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Requires `allow-net` permission. */
|
2021-06-28 19:43:03 -04:00
|
|
|
export function listen(
|
|
|
|
options: ListenOptions & { transport?: "tcp" },
|
|
|
|
): Listener;
|
|
|
|
|
|
|
|
export interface ListenTlsOptions extends ListenOptions {
|
2022-02-23 23:16:56 -05:00
|
|
|
/** Server private key in PEM format */
|
|
|
|
key?: string;
|
|
|
|
/** Cert chain in PEM format */
|
|
|
|
cert?: string;
|
2021-08-09 09:55:00 -04:00
|
|
|
/** Path to a file containing a PEM formatted CA certificate. Requires
|
2022-02-23 23:16:56 -05:00
|
|
|
* `--allow-read`.
|
|
|
|
*
|
|
|
|
* @deprecated This option is deprecated and will be removed in Deno 2.0.
|
|
|
|
*/
|
|
|
|
certFile?: string;
|
|
|
|
/** Server private key file. Requires `--allow-read`.
|
|
|
|
*
|
|
|
|
* @deprecated This option is deprecated and will be removed in Deno 2.0.
|
|
|
|
*/
|
|
|
|
keyFile?: string;
|
2021-06-28 19:43:03 -04:00
|
|
|
|
|
|
|
transport?: "tcp";
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Listen announces on the local transport address over TLS (transport layer
|
2021-09-02 18:28:12 -04:00
|
|
|
* security).
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const lstnr = Deno.listenTls({ port: 443, certFile: "./server.crt", keyFile: "./server.key" });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Requires `allow-net` permission. */
|
2021-10-26 16:27:47 -04:00
|
|
|
export function listenTls(options: ListenTlsOptions): TlsListener;
|
2021-06-28 19:43:03 -04:00
|
|
|
|
|
|
|
export interface ConnectOptions {
|
|
|
|
/** The port to connect to. */
|
|
|
|
port: number;
|
|
|
|
/** A literal IP address or host name that can be resolved to an IP address.
|
2021-09-02 18:28:12 -04:00
|
|
|
* If not specified, defaults to `127.0.0.1`. */
|
2021-06-28 19:43:03 -04:00
|
|
|
hostname?: string;
|
|
|
|
transport?: "tcp";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-02 18:28:12 -04:00
|
|
|
* Connects to the hostname (default is "127.0.0.1") and port on the named
|
|
|
|
* transport (default is "tcp"), and resolves to the connection (`Conn`).
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const conn1 = await Deno.connect({ port: 80 });
|
|
|
|
* const conn2 = await Deno.connect({ hostname: "192.0.2.1", port: 80 });
|
|
|
|
* const conn3 = await Deno.connect({ hostname: "[2001:db8::1]", port: 80 });
|
|
|
|
* const conn4 = await Deno.connect({ hostname: "golang.org", port: 80, transport: "tcp" });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Requires `allow-net` permission for "tcp". */
|
2022-02-27 09:18:30 -05:00
|
|
|
export function connect(options: ConnectOptions): Promise<TcpConn>;
|
|
|
|
|
|
|
|
export interface TcpConn extends Conn {
|
|
|
|
/**
|
|
|
|
* **UNSTABLE**: new API, see https://github.com/denoland/deno/issues/13617.
|
|
|
|
*
|
|
|
|
* Enable/disable the use of Nagle's algorithm. Defaults to true.
|
|
|
|
*/
|
|
|
|
setNoDelay(nodelay?: boolean): void;
|
|
|
|
/**
|
|
|
|
* **UNSTABLE**: new API, see https://github.com/denoland/deno/issues/13617.
|
|
|
|
*
|
|
|
|
* Enable/disable keep-alive functionality.
|
|
|
|
*/
|
|
|
|
setKeepAlive(keepalive?: boolean): void;
|
|
|
|
}
|
2021-06-28 19:43:03 -04:00
|
|
|
|
|
|
|
export interface ConnectTlsOptions {
|
|
|
|
/** The port to connect to. */
|
|
|
|
port: number;
|
|
|
|
/** A literal IP address or host name that can be resolved to an IP address.
|
2021-09-02 18:28:12 -04:00
|
|
|
* If not specified, defaults to `127.0.0.1`. */
|
2021-06-28 19:43:03 -04:00
|
|
|
hostname?: string;
|
2021-09-30 03:26:15 -04:00
|
|
|
/**
|
2021-12-09 19:05:50 -05:00
|
|
|
* Server certificate file.
|
|
|
|
*
|
2021-09-30 03:26:15 -04:00
|
|
|
* @deprecated This option is deprecated and will be removed in a future
|
|
|
|
* release.
|
|
|
|
*/
|
2021-06-28 19:43:03 -04:00
|
|
|
certFile?: string;
|
2021-09-30 03:26:15 -04:00
|
|
|
/** A list of root certificates that will be used in addition to the
|
|
|
|
* default root certificates to verify the peer's certificate.
|
|
|
|
*
|
|
|
|
* Must be in PEM format. */
|
|
|
|
caCerts?: string[];
|
2021-06-28 19:43:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Establishes a secure connection over TLS (transport layer security) using
|
2021-09-02 18:28:12 -04:00
|
|
|
* an optional cert file, hostname (default is "127.0.0.1") and port. The
|
|
|
|
* cert file is optional and if not included Mozilla's root certificates will
|
|
|
|
* be used (see also https://github.com/ctz/webpki-roots for specifics)
|
|
|
|
*
|
|
|
|
* ```ts
|
2021-09-30 03:26:15 -04:00
|
|
|
* const caCert = await Deno.readTextFile("./certs/my_custom_root_CA.pem");
|
2021-09-02 18:28:12 -04:00
|
|
|
* const conn1 = await Deno.connectTls({ port: 80 });
|
2021-09-30 03:26:15 -04:00
|
|
|
* const conn2 = await Deno.connectTls({ caCerts: [caCert], hostname: "192.0.2.1", port: 80 });
|
2021-09-02 18:28:12 -04:00
|
|
|
* const conn3 = await Deno.connectTls({ hostname: "[2001:db8::1]", port: 80 });
|
2021-09-30 03:26:15 -04:00
|
|
|
* const conn4 = await Deno.connectTls({ caCerts: [caCert], hostname: "golang.org", port: 80});
|
2021-09-02 18:28:12 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Requires `allow-net` permission.
|
|
|
|
*/
|
2021-10-26 16:27:47 -04:00
|
|
|
export function connectTls(options: ConnectTlsOptions): Promise<TlsConn>;
|
2021-06-28 19:43:03 -04:00
|
|
|
|
2021-10-29 11:13:31 -04:00
|
|
|
export interface StartTlsOptions {
|
|
|
|
/** A literal IP address or host name that can be resolved to an IP address.
|
|
|
|
* If not specified, defaults to `127.0.0.1`. */
|
|
|
|
hostname?: string;
|
|
|
|
/** A list of root certificates that will be used in addition to the
|
|
|
|
* default root certificates to verify the peer's certificate.
|
|
|
|
*
|
|
|
|
* Must be in PEM format. */
|
|
|
|
caCerts?: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Start TLS handshake from an existing connection using an optional list of
|
|
|
|
* CA certificates, and hostname (default is "127.0.0.1"). Specifying CA certs
|
|
|
|
* is optional. By default the configured root certificates are used. Using
|
|
|
|
* this function requires that the other end of the connection is prepared for
|
|
|
|
* a TLS handshake.
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const conn = await Deno.connect({ port: 80, hostname: "127.0.0.1" });
|
|
|
|
* const caCert = await Deno.readTextFile("./certs/my_custom_root_CA.pem");
|
|
|
|
* const tlsConn = await Deno.startTls(conn, { caCerts: [caCert], hostname: "localhost" });
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Requires `allow-net` permission.
|
|
|
|
*/
|
|
|
|
export function startTls(
|
|
|
|
conn: Conn,
|
|
|
|
options?: StartTlsOptions,
|
|
|
|
): Promise<TlsConn>;
|
|
|
|
|
2021-06-28 19:43:03 -04:00
|
|
|
/** Shutdown socket send operations.
|
2021-09-02 18:28:12 -04:00
|
|
|
*
|
|
|
|
* Matches behavior of POSIX shutdown(3).
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const listener = Deno.listen({ port: 80 });
|
|
|
|
* const conn = await listener.accept();
|
|
|
|
* Deno.shutdown(conn.rid);
|
|
|
|
* ```
|
|
|
|
*/
|
2021-06-28 19:43:03 -04:00
|
|
|
export function shutdown(rid: number): Promise<void>;
|
|
|
|
}
|