diff options
author | crowlKats <13135287+crowlKats@users.noreply.github.com> | 2021-04-13 13:33:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-13 13:33:17 +0200 |
commit | a8057e3e06962a8d7c6330a085704bb4493eed04 (patch) | |
tree | 5e33e95056bed7129eed8bf7272dc75dcf8f6c4e | |
parent | 8b59d9f7bc2ce4aa33b6b8ac41495c62d3791f3c (diff) |
feat(cli/dts): stricter typings for Listener & Conn (#10012)
-rw-r--r-- | cli/dts/lib.deno.ns.d.ts | 26 | ||||
-rw-r--r-- | cli/dts/lib.deno.unstable.d.ts | 13 | ||||
-rw-r--r-- | cli/tests/unit/net_test.ts | 6 |
3 files changed, 29 insertions, 16 deletions
diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts index 738d0ba54..8b0d8d7d9 100644 --- a/cli/dts/lib.deno.ns.d.ts +++ b/cli/dts/lib.deno.ns.d.ts @@ -1740,26 +1740,28 @@ declare namespace Deno { export type Addr = NetAddr | UnixAddr; /** A generic network listener for stream-oriented protocols. */ - export interface Listener extends AsyncIterable<Conn> { + export interface Listener<Address extends Addr = Addr> + extends AsyncIterable<Conn<Address>> { /** Waits for and resolves to the next connection to the `Listener`. */ - accept(): Promise<Conn>; + accept(): Promise<Conn<Address>>; /** Close closes the listener. Any pending accept promises will be rejected * with errors. */ close(): void; /** Return the address of the `Listener`. */ - readonly addr: Addr; + readonly addr: Address; /** Return the rid of the `Listener`. */ readonly rid: number; - [Symbol.asyncIterator](): AsyncIterableIterator<Conn>; + [Symbol.asyncIterator](): AsyncIterableIterator<Conn<Address>>; } - export interface Conn extends Reader, Writer, Closer { + export interface Conn<Address extends Addr = Addr> + extends Reader, Writer, Closer { /** The local address of the connection. */ - readonly localAddr: Addr; + readonly localAddr: Address; /** The remote address of the connection. */ - readonly remoteAddr: Addr; + readonly remoteAddr: Address; /** The resource ID of the connection. */ readonly rid: number; /** Shuts down (`shutdown(2)`) the write side of the connection. Most @@ -1787,7 +1789,7 @@ declare namespace Deno { * Requires `allow-net` permission. */ export function listen( options: ListenOptions & { transport?: "tcp" }, - ): Listener; + ): Listener<NetAddr>; export interface ListenTlsOptions extends ListenOptions { /** Server certificate file. */ @@ -1806,7 +1808,7 @@ declare namespace Deno { * ``` * * Requires `allow-net` permission. */ - export function listenTls(options: ListenTlsOptions): Listener; + export function listenTls(options: ListenTlsOptions): Listener<NetAddr>; export interface ConnectOptions { /** The port to connect to. */ @@ -1829,7 +1831,7 @@ declare namespace Deno { * ``` * * Requires `allow-net` permission for "tcp". */ - export function connect(options: ConnectOptions): Promise<Conn>; + export function connect(options: ConnectOptions): Promise<Conn<NetAddr>>; export interface ConnectTlsOptions { /** The port to connect to. */ @@ -1855,7 +1857,9 @@ declare namespace Deno { * * Requires `allow-net` permission. */ - export function connectTls(options: ConnectTlsOptions): Promise<Conn>; + export function connectTls( + options: ConnectTlsOptions, + ): Promise<Conn<NetAddr>>; /** Shutdown socket send operations. * diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts index 508e1ca6c..54869e15a 100644 --- a/cli/dts/lib.deno.unstable.d.ts +++ b/cli/dts/lib.deno.unstable.d.ts @@ -909,7 +909,7 @@ declare namespace Deno { * Requires `allow-read` and `allow-write` permission. */ export function listen( options: UnixListenOptions & { transport: "unix" }, - ): Listener; + ): Listener<UnixAddr>; /** **UNSTABLE**: new API, yet to be vetted * @@ -969,8 +969,11 @@ declare namespace Deno { * * Requires `allow-net` permission for "tcp" and `allow-read` for "unix". */ export function connect( - options: ConnectOptions | UnixConnectOptions, - ): Promise<Conn>; + options: ConnectOptions, + ): Promise<Conn<NetAddr>>; + export function connect( + options: UnixConnectOptions, + ): Promise<Conn<UnixAddr>>; export interface StartTlsOptions { /** A literal IP address or host name that can be resolved to an IP address. @@ -997,9 +1000,9 @@ declare namespace Deno { * Requires `allow-net` permission. */ export function startTls( - conn: Conn, + conn: Conn<NetAddr>, options?: StartTlsOptions, - ): Promise<Conn>; + ): Promise<Conn<NetAddr>>; export interface ListenTlsOptions { /** **UNSTABLE**: new API, yet to be vetted. diff --git a/cli/tests/unit/net_test.ts b/cli/tests/unit/net_test.ts index 725bb9684..5f9fc916a 100644 --- a/cli/tests/unit/net_test.ts +++ b/cli/tests/unit/net_test.ts @@ -18,6 +18,12 @@ unitTest({ perms: { net: true } }, function netTcpListenClose(): void { listener.close(); }); +unitTest({ perms: { net: true } }, function netListenPortType(): void { + const listener = Deno.listen({ port: 0, transport: "tcp" }); + listener.addr.port; + listener.close(); +}); + unitTest( { perms: { net: true }, |