From abc647cb4d805f09132b50858e2bf2761a8d09c9 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Thu, 28 Dec 2023 19:32:10 -0500 Subject: [PATCH] Avoid port conflicts --- listener.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/listener.ts b/listener.ts index 50a9a2d..a202371 100644 --- a/listener.ts +++ b/listener.ts @@ -1,9 +1,18 @@ import { run } from "@ludus/ludus-js-pure" import { unlinkSync, readFileSync } from "node:fs" +function random_port () { + return Math.floor(Math.random() * 10000) +} + export async function listen () { - const id = Math.floor(Math.random() * 1000) + 5000 - const unix_socket = `/tmp/ludus-${id}.sock` + let id = random_port() + let unix_socket = `/tmp/ludus-${id}.sock` + // make sure the socket path isn't already taken + while (await Bun.file(unix_socket).exists()) { + id = random_port() + unix_socket = `/tmp/ludus-${id}.sock` + } const repl_file_path = ".lrepl" const repl_info = { unix_socket } const repl_file = Bun.file(repl_file_path)