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" import { unlinkSync, readFileSync } from "node:fs"
export async function listen () { export async function listen () {
const port = Math.floor((Math.random() * 1000) + 50000) const id = Math.floor(Math.random() * 1000) + 5000
const session_token = crypto.randomUUID(); const unix_socket = `/tmp/ludus-${id}.sock`
const repl_file_path = ".lrepl" 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) const repl_file = Bun.file(repl_file_path)
await Bun.write(repl_file, JSON.stringify(repl_info)) await Bun.write(repl_file, JSON.stringify(repl_info))
console.log(`Ludus REPL listening on localhost:${port}`) console.log(`Ludus REPL listening on ${unix_socket}`)
console.log(`Session token: ${session_token}`)
Bun.listen({ Bun.listen({
hostname: "localhost", //hostname: "localhost",
port, //port,
unix: unix_socket,
socket: { socket: {
open: (_) => {console.log("=== === ===") },
data: (_, data) => { data: (_, data) => {
const payload = data.toString("utf-8") const source = 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 result = run(source) const result = run(source)
if (!result.errors) console.log("=>", result.result) if (!result.errors) console.log("=>", result.result)
else { else {
@ -44,7 +32,7 @@ export async function listen () {
process.on("exit", async function () { 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.") console.log("\nGoodbye.")
}) })
@ -65,9 +53,15 @@ export async function listen () {
} }
// Don't delete an .lrepl we don't own // 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 repl_file = readFileSync(path).toString("utf-8")
const {session} = JSON.parse(repl_file) let their_socket
if (session === session_token) unlinkSync(path) 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.") console.log("No .lrepl file found.")
process.exit(1) process.exit(1)
} }
const port = repl_info.port const unix_socket = repl_info.unix_socket
const session = repl_info.session
// console.log("Attempting connection to", unix_socket)
const socket = await Bun.connect({ const socket = await Bun.connect({
hostname: "localhost", unix: unix_socket,
port,
socket: { socket: {
data: () => process.exit() data: () => process.exit()
} }
@ -20,12 +20,12 @@ export async function send() {
for await (const input of Bun.stdin.stream()) { for await (const input of Bun.stdin.stream()) {
const chunk = Buffer.from(input).toString("utf-8") const chunk = Buffer.from(input).toString("utf-8")
const payload = {source: chunk, session} // console.log("Sending:", chunk)
// console.log("Sending:", payload) // console.log(`To: ${unix_socket}`)
// console.log(`To: ${port}`) socket.write(chunk)
socket.write(JSON.stringify(payload))
socket.end() socket.end()
process.exit() 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) }
}
})