Use unix sockets, not TCP sockets

This commit is contained in:
Scott Richmond 2023-12-27 12:07:24 -05:00
parent 8b8b1d5fc0
commit 248eabc462
3 changed files with 34 additions and 33 deletions

View File

@ -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()

View File

@ -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()

7
unix.ts Normal file
View File

@ -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) }
}
})