From 248eabc4622c4c7fa1c226fb94671ce5c22f0c36 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Wed, 27 Dec 2023 12:07:24 -0500 Subject: [PATCH] Use unix sockets, not TCP sockets --- listener.ts | 44 +++++++++++++++++++------------------------- sender.ts | 16 ++++++++-------- unix.ts | 7 +++++++ 3 files changed, 34 insertions(+), 33 deletions(-) create mode 100644 unix.ts diff --git a/listener.ts b/listener.ts index 566052f..418dd04 100644 --- a/listener.ts +++ b/listener.ts @@ -2,35 +2,23 @@ import { run } from "@ludus/ludus-js-pure" import { unlinkSync, readFileSync } from "node:fs" export async function listen () { - const port = Math.floor((Math.random() * 1000) + 50000) - const session_token = crypto.randomUUID(); + const id = Math.floor(Math.random() * 1000) + 5000 + const unix_socket = `/tmp/ludus-${id}.sock` const repl_file_path = ".lrepl" - const repl_info = { port, session: session_token } + const repl_info = { unix_socket } const repl_file = Bun.file(repl_file_path) await Bun.write(repl_file, JSON.stringify(repl_info)) - console.log(`Ludus REPL listening on localhost:${port}`) - console.log(`Session token: ${session_token}`) + console.log(`Ludus REPL listening on ${unix_socket}`) Bun.listen({ - hostname: "localhost", - port, + //hostname: "localhost", + //port, + unix: unix_socket, socket: { + open: (_) => {console.log("=== === ===") }, data: (_, data) => { - const payload = data.toString("utf-8") - let session, source - try { - const json_msg = JSON.parse(payload) - session = json_msg.session - source = json_msg.source - } catch (e) { - console.log("Received malformed message.") - return - } - if (session !== session_token) { - console.log("Received message not from this REPL session.") - return - } + const source = data.toString("utf-8") const result = run(source) if (!result.errors) console.log("=>", result.result) else { @@ -44,7 +32,7 @@ export async function listen () { process.on("exit", async function () { - try { cleanup(repl_file_path, session_token) } catch (e) {} + try { cleanup(repl_file_path, unix_socket) } catch (e) {} console.log("\nGoodbye.") }) @@ -65,9 +53,15 @@ export async function listen () { } // Don't delete an .lrepl we don't own -function cleanup (path: string, session_token: string) { +function cleanup (path: string, my_socket: string) { const repl_file = readFileSync(path).toString("utf-8") - const {session} = JSON.parse(repl_file) - if (session === session_token) unlinkSync(path) + let their_socket + try { their_socket = JSON.parse(repl_file).unix_socket } catch (e) { return } + if (their_socket === my_socket) { + unlinkSync(path) + unlinkSync(my_socket) + } } +// await listen() + diff --git a/sender.ts b/sender.ts index b0b954b..e4c6219 100644 --- a/sender.ts +++ b/sender.ts @@ -7,12 +7,12 @@ export async function send() { console.log("No .lrepl file found.") process.exit(1) } - const port = repl_info.port - const session = repl_info.session + const unix_socket = repl_info.unix_socket + + // console.log("Attempting connection to", unix_socket) const socket = await Bun.connect({ - hostname: "localhost", - port, + unix: unix_socket, socket: { data: () => process.exit() } @@ -20,12 +20,12 @@ export async function send() { for await (const input of Bun.stdin.stream()) { const chunk = Buffer.from(input).toString("utf-8") - const payload = {source: chunk, session} - // console.log("Sending:", payload) - // console.log(`To: ${port}`) - socket.write(JSON.stringify(payload)) + // console.log("Sending:", chunk) + // console.log(`To: ${unix_socket}`) + socket.write(chunk) socket.end() process.exit() } } +// await send() diff --git a/unix.ts b/unix.ts new file mode 100644 index 0000000..c0d73e5 --- /dev/null +++ b/unix.ts @@ -0,0 +1,7 @@ +Bun.listen({ + unix: "/tmp/test.sock", + socket: { + open: (_) => console.log("Somebody opened my socket."), + data: (_, d) => { console.log("I got data!", d) } + } +})