2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-12-04 04:50:40 +00:00
|
|
|
|
2024-01-11 06:12:47 +00:00
|
|
|
/** Options for {@linkcode getAvailablePort}. */
|
|
|
|
export interface GetAvailablePortOptions {
|
|
|
|
/**
|
|
|
|
* A port to check availability of first. If the port isn't available, fall
|
|
|
|
* back to another port.
|
2024-06-07 03:54:15 +00:00
|
|
|
*
|
|
|
|
* Defaults to port 0, which will let the operating system choose an available
|
|
|
|
* port.
|
|
|
|
*
|
|
|
|
* @default {0}
|
2024-01-11 06:12:47 +00:00
|
|
|
*/
|
|
|
|
preferredPort?: number;
|
|
|
|
}
|
|
|
|
|
2023-12-04 04:50:40 +00:00
|
|
|
/**
|
|
|
|
* Returns an available network port.
|
|
|
|
*
|
2024-07-09 11:19:51 +00:00
|
|
|
* > [!IMPORTANT]
|
|
|
|
* > In most cases, this function is not needed. Do not use it for trivial uses
|
|
|
|
* > such as when using {@linkcode Deno.serve} or {@linkcode Deno.listen}
|
|
|
|
* > directly. Instead, set the `port` option to `0` to automatically use an
|
|
|
|
* > available port, then get the assigned port from the function's return
|
|
|
|
* > object (see "Recommended Usage" example).
|
|
|
|
*
|
2024-06-07 03:54:15 +00:00
|
|
|
* @param options Options for getting an available port.
|
|
|
|
* @returns An available network port.
|
|
|
|
*
|
2024-07-09 11:19:51 +00:00
|
|
|
* @example Recommended Usage
|
|
|
|
*
|
|
|
|
* Bad:
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore no-assert
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { getAvailablePort } from "@std/net/get-available-port";
|
2023-12-04 04:50:40 +00:00
|
|
|
*
|
|
|
|
* const port = getAvailablePort();
|
|
|
|
* Deno.serve({ port }, () => new Response("Hello, world!"));
|
|
|
|
* ```
|
2024-07-09 11:19:51 +00:00
|
|
|
*
|
|
|
|
* Good:
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore no-assert
|
2024-07-09 11:19:51 +00:00
|
|
|
* const { port } = Deno.serve({ port: 0 }, () => new Response("Hello, world!")).addr;
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Good:
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore no-assert
|
2024-07-09 11:19:51 +00:00
|
|
|
* import { getAvailablePort } from "@std/net/get-available-port";
|
|
|
|
*
|
|
|
|
* const command = new Deno.Command(Deno.execPath(), {
|
|
|
|
* args: ["test.ts", "--port", getAvailablePort().toString()],
|
|
|
|
* });
|
|
|
|
* // ...
|
|
|
|
* ```
|
2023-12-04 04:50:40 +00:00
|
|
|
*/
|
2024-01-11 06:12:47 +00:00
|
|
|
export function getAvailablePort(options?: GetAvailablePortOptions): number {
|
|
|
|
if (options?.preferredPort) {
|
|
|
|
try {
|
|
|
|
// Check if the preferred port is available
|
2024-01-14 21:35:50 +00:00
|
|
|
using listener = Deno.listen({ port: options.preferredPort });
|
2024-04-15 06:47:28 +00:00
|
|
|
return listener.addr.port;
|
2024-01-11 06:12:47 +00:00
|
|
|
} catch (e) {
|
|
|
|
// If the preferred port is not available, fall through and find an available port
|
|
|
|
if (!(e instanceof Deno.errors.AddrInUse)) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:35:50 +00:00
|
|
|
using listener = Deno.listen({ port: 0 });
|
2024-04-15 06:47:28 +00:00
|
|
|
return listener.addr.port;
|
2023-12-04 04:50:40 +00:00
|
|
|
}
|