Avoid port conflicts

This commit is contained in:
Scott Richmond 2023-12-28 19:32:10 -05:00
parent 943ca48aba
commit abc647cb4d

View File

@ -1,9 +1,18 @@
import { run } from "@ludus/ludus-js-pure" import { run } from "@ludus/ludus-js-pure"
import { unlinkSync, readFileSync } from "node:fs" import { unlinkSync, readFileSync } from "node:fs"
function random_port () {
return Math.floor(Math.random() * 10000)
}
export async function listen () { export async function listen () {
const id = Math.floor(Math.random() * 1000) + 5000 let id = random_port()
const unix_socket = `/tmp/ludus-${id}.sock` 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_file_path = ".lrepl"
const repl_info = { unix_socket } const repl_info = { unix_socket }
const repl_file = Bun.file(repl_file_path) const repl_file = Bun.file(repl_file_path)