From a8057e3e06962a8d7c6330a085704bb4493eed04 Mon Sep 17 00:00:00 2001 From: crowlKats <13135287+crowlKats@users.noreply.github.com> Date: Tue, 13 Apr 2021 13:33:17 +0200 Subject: [PATCH] feat(cli/dts): stricter typings for Listener & Conn (#10012) --- cli/dts/lib.deno.ns.d.ts | 26 +++++++++++++++----------- cli/dts/lib.deno.unstable.d.ts | 13 ++++++++----- 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 738d0ba54be218..8b0d8d7d91b266 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 { + export interface Listener
+ extends AsyncIterable> { /** Waits for and resolves to the next connection to the `Listener`. */ - accept(): Promise; + accept(): Promise>; /** 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; + [Symbol.asyncIterator](): AsyncIterableIterator>; } - export interface Conn extends Reader, Writer, Closer { + export interface Conn
+ 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; 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; 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; + export function connect(options: ConnectOptions): Promise>; 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; + export function connectTls( + options: ConnectTlsOptions, + ): Promise>; /** Shutdown socket send operations. * diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts index 508e1ca6ca1e76..54869e15ae45e2 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; /** **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; + options: ConnectOptions, + ): Promise>; + export function connect( + options: UnixConnectOptions, + ): Promise>; 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, options?: StartTlsOptions, - ): Promise; + ): Promise>; 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 725bb9684e4666..5f9fc916afd109 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 },