* Pick a random free port by asking the OS for one. Uses Bun.serve + immediate stop.
()
| 58 | * Pick a random free port by asking the OS for one. Uses Bun.serve + immediate stop. |
| 59 | */ |
| 60 | async function pickFreePort(): Promise<number> { |
| 61 | const server = Bun.serve({ port: 0, fetch: () => new Response() }); |
| 62 | const port: number = server.port ?? 0; |
| 63 | server.stop(true); |
| 64 | if (!port) throw new Error("could not allocate a free port"); |
| 65 | return port; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Create isolated config/data/cache dirs under a unique temp subdir. |