Change listener.ts

This commit is contained in:
Scott Richmond 2023-12-26 17:54:34 -05:00
parent 25f50b1100
commit 8cd4f0b47b

View File

@ -1,35 +1,40 @@
import { run } from "@ludus/ludus-js-pure" import { run } from "@ludus/ludus-js-pure"
const port = Math.floor((Math.random() * 1000) + 50000) async function listen() {
const port = Math.floor((Math.random() * 1000) + 50000)
const repl_info = { port } const repl_info = { port }
const repl_file = Bun.file("./.lrepl") const repl_file = Bun.file("./.lrepl")
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 localhost:${port}`)
Bun.listen({ Bun.listen({
hostname: "localhost", hostname: "localhost",
port, port,
socket: { socket: {
data: (socket, data) => { data: (socket, data) => {
const source = data.toString("utf-8") console.log("I got data!")
const result = run(source) const source = data.toString("utf-8")
const msgs = result.console const result = run(source)
for (const msg of msgs) { const msgs = result.console
console.log(msg) for (const msg of msgs) {
} console.log(msg)
if (!result.errors) console.log(result.result) }
else { if (!result.errors) console.log(result.result)
console.log("Ludus had some errors:") else {
console.log(result.errors) console.log("Ludus had some errors:")
} console.log(result.errors)
}, }
error: (socket, error) => { console.log(error); socket.end() } },
} error: (socket, error) => { console.log(error); socket.end() }
}) }
})
}
await listen()