add host script
This commit is contained in:
parent
c62a03b943
commit
bccb40e8e7
2 changed files with 46 additions and 0 deletions
38
cc-tweaked/host.ts
Executable file
38
cc-tweaked/host.ts
Executable file
|
@ -0,0 +1,38 @@
|
|||
#!/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 <program_path>");
|
||||
const port = 1728;
|
||||
const server = Deno.listen({ port });
|
||||
console.log("listening on port", port);
|
||||
for await (const connection of server) handle(connection, program_path);
|
||||
}
|
||||
|
||||
async function handle(connection: Deno.TcpConn, program_path: string) {
|
||||
console.log("serving");
|
||||
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));
|
||||
}
|
||||
|
||||
function fail_with(...message: string[]) {
|
||||
console.error(...message);
|
||||
return Deno.exit(1);
|
||||
}
|
||||
|
||||
async function try_else<T>(operation: () => T | Promise<T>, on_error: (error: unknown) => T | Promise<T>) {
|
||||
try {
|
||||
return await operation();
|
||||
} catch (error) {
|
||||
return await on_error(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function try_<T>(operation: () => T | Promise<T>) {
|
||||
return await try_else(operation, () => null);
|
||||
}
|
||||
|
||||
if (import.meta.main) await main();
|
8
cc-tweaked/scripts/cat.lua
Normal file
8
cc-tweaked/scripts/cat.lua
Normal file
|
@ -0,0 +1,8 @@
|
|||
local arg1, arg2, arg3 = ...
|
||||
|
||||
local function main()
|
||||
print("arg1", arg1, "arg2", arg2, "arg3", arg3)
|
||||
local file = fs.open()
|
||||
end
|
||||
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue