getAvailablePort

import { getAvailablePort } from "jsr:@ende/net";

function

function getAvailablePort(options?: GetAvailablePortOptions): number

Returns an available network port.

Important

In most cases, this function is not needed. Do not use it for trivial uses such as when using Ende.serve or Ende.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).

options?: GetAvailablePortOptions

Options for getting an available port.

Returns

An available network port.

Example: Recommended Usage

Bad:

import { getAvailablePort } from "@ende/net/get-available-port";

const port = getAvailablePort();
Ende.serve({ port }, () => new Response("Hello, world!"));

Good:

const { port } = Ende.serve({ port: 0 }, () => new Response("Hello, world!")).addr;

Good:

import { getAvailablePort } from "@ende/net/get-available-port";

const command = new Ende.Command(Ende.execPath(), {
  args: ["test.ts", "--port", getAvailablePort().toString()],
});
// ...

Source