From 52cc33f8077b789298fa54e7fea0f95c7c3db93f Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Tue, 14 May 2024 22:36:11 +0200 Subject: [PATCH 1/3] fix host script --- cc-tweaked/host.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/cc-tweaked/host.ts b/cc-tweaked/host.ts index 4f146ca..50dad33 100755 --- a/cc-tweaked/host.ts +++ b/cc-tweaked/host.ts @@ -4,18 +4,17 @@ async function main() { const [program_path] = Deno.args; if (program_path === undefined) fail_with("Usage: host.ts "); const port = 1728; - const server = Deno.listen({ port }); - console.log("listening on port", port); - for await (const connection of server) handle(connection, program_path); + const server = Deno.serve({ port }, () => handle(program_path)); + await server.finished; } -async function handle(connection: Deno.TcpConn, program_path: string) { - console.log("serving"); +async function handle(program_path: string) { + console.log("Serving", program_path); const file = await try_else( () => Deno.open(program_path, { read: true }), () => fail_with("Program at", program_path, "does not exists"), ); - await try_(() => file.readable.pipeTo(connection.writable)); + return new Response(file.readable); } function fail_with(...message: string[]) { @@ -31,8 +30,4 @@ async function try_else(operation: () => T | Promise, on_error: (error: un } } -async function try_(operation: () => T | Promise) { - return await try_else(operation, () => null); -} - if (import.meta.main) await main(); From 62cd230d5bee0a7b5d6e92ce59be60d8912fb94e Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Wed, 15 May 2024 00:33:42 +0200 Subject: [PATCH 2/3] fix host script --- cc-tweaked/host.ts | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/cc-tweaked/host.ts b/cc-tweaked/host.ts index 50dad33..18e659e 100755 --- a/cc-tweaked/host.ts +++ b/cc-tweaked/host.ts @@ -1,32 +1,29 @@ #!/bin/env -S deno run --allow-read --allow-net async function main() { - const [program_path] = Deno.args; - if (program_path === undefined) fail_with("Usage: host.ts "); const port = 1728; - const server = Deno.serve({ port }, () => handle(program_path)); + const server = Deno.serve({ port }, (request) => handle(request)); await server.finished; } -async function handle(program_path: string) { - console.log("Serving", program_path); - const file = await try_else( - () => Deno.open(program_path, { read: true }), - () => fail_with("Program at", program_path, "does not exists"), - ); - return new Response(file.readable); +async function handle(request: Request) { + const script = await open_requested_script(request); + if (script instanceof Response) return script; + console.log("Serving", script.path); + return new Response(script.content.readable); } -function fail_with(...message: string[]) { - console.error(...message); - return Deno.exit(1); -} - -async function try_else(operation: () => T | Promise, on_error: (error: unknown) => T | Promise) { +async function open_requested_script(request: Request) { + const requested_path = new URL(request.url).pathname.slice(1); + const invalid = () => new Response("Invalid script.", { status: 400 }); + if (requested_path.includes("/")) return invalid(); + if (requested_path.includes(".")) return invalid(); try { - return await operation(); - } catch (error) { - return await on_error(error); + const path = "./scripts/" + requested_path + ".lua"; + const content = await Deno.open(path, { read: true }); + return { path, content }; + } catch (_) { + return new Response("Unknown script.", { status: 400 }); } } From 7007a2c9943c6ee3a1476da6eed8bc74eac18648 Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Wed, 15 May 2024 00:33:54 +0200 Subject: [PATCH 3/3] implement cat script --- cc-tweaked/scripts/cat.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cc-tweaked/scripts/cat.lua b/cc-tweaked/scripts/cat.lua index d92e5af..b3e0820 100644 --- a/cc-tweaked/scripts/cat.lua +++ b/cc-tweaked/scripts/cat.lua @@ -1,8 +1,17 @@ -local arg1, arg2, arg3 = ... +local path = ... local function main() - print("arg1", arg1, "arg2", arg2, "arg3", arg3) - local file = fs.open() + if path == nil then + print("Usage: cat ") + return + end + local file = fs.open(path, "r") + if file == nil then + print("No such file:", path) + return + end + local content = file.readAll() + print(content) end main()