ludus-repl/sender.ts

32 lines
683 B
TypeScript
Raw Normal View History

export async function send() {
const repl_file = Bun.file("./.lrepl")
let repl_info
try {
repl_info = await repl_file.json()
} catch (e) {
console.log("No .lrepl file found.")
process.exit(1)
}
const port = repl_info.port
const session = repl_info.session
const socket = await Bun.connect({
hostname: "localhost",
port,
socket: {
data: () => process.exit()
}
})
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))
socket.end()
process.exit()
}
}