Skip to content

Commit

Permalink
feat(cli/dts): stricter typings for Listener & Conn (#10012)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Apr 13, 2021
1 parent 8b59d9f commit a8057e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
26 changes: 15 additions & 11 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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. */
Expand All @@ -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. */
Expand All @@ -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. */
Expand All @@ -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.
*
Expand Down
13 changes: 8 additions & 5 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions cli/tests/unit/net_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit a8057e3

Please sign in to comment.