Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli/dts): stricter typings for Listener & Conn #10012

Merged
merged 3 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1713,26 +1713,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 @@ -1760,7 +1762,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 @@ -1779,7 +1781,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 @@ -1802,7 +1804,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 @@ -1828,7 +1830,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 Expand Up @@ -1944,10 +1948,10 @@ declare namespace Deno {
*
* If `stdout` and/or `stderr` were set to `"piped"`, they must be closed
* manually before the process can exit.
*
*
* To run process to completion and collect output from both `stdout` and
* `stderr` use:
*
*
* ```ts
* const p = Deno.run({ cmd, stderr: 'piped', stdout: 'piped' });
* const [status, stdout, stderr] = await Promise.all([
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 @@ -899,7 +899,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 @@ -959,8 +959,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 @@ -987,9 +990,9 @@ declare namespace Deno {
* Requires `allow-net` permission.
*/
export function startTls(
conn: Conn,
conn: Conn<NetAddr>,
options?: StartTlsOptions,
): Promise<Conn>;
): Promise<Conn<NetAddr>>;

/** **UNSTABLE**: The `signo` argument may change to require the Deno.Signal
* enum.
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