freePort

tcmts.cli.sync.freePort · #f3juxnj4 · term · runs on the server

uses node:http

A free port on this machine, as the system gives one.

import http from "node:http";

export function freePort(): Promise<number> {
  return new Promise((resolve, reject) => {
    const s = http.createServer();
    s.on("error", reject);
    s.listen(0, "127.0.0.1", () => {
      const { port } = s.address() as { port: number };
      s.close(() => resolve(port));
    });
  });
}

used by