diff --git a/README.md b/README.md index 54f546a..a5f63fc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,14 @@ Upload worker to CloudFlare =========================== +Until we're better +------------------ + +1. Add `accountid.txt` to `src/` with the CloudFlare account id +2. Add `worker_name.txt` to `src/` with CloudFlare worker name +3. `zig build run` + + Steps we take: -------------- diff --git a/src/demo.wasm b/src/demo.wasm new file mode 100755 index 0000000..716016a Binary files /dev/null and b/src/demo.wasm differ diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..67115a9 --- /dev/null +++ b/src/index.js @@ -0,0 +1,41 @@ +import { WASI } from "@cloudflare/workers-wasi"; +import demoWasm from "./demo.wasm"; + +export default { + async fetch(request, _env, ctx) { + // Creates a TransformStream we can use to pipe our stdout to our response body. + const stdout = new TransformStream(); + console.log(request); + console.log(_env); + console.log(ctx); + + // Get our headers into environment variables (should we prefix this?) + let env = {}; + request.headers.forEach((value, key) => { + env[key] = value; + }); + const wasi = new WASI({ + args: [ + './demo.wasm', // In a CLI, the first arg is the name of the exe + '--url=' + request.url, // this contains the target but is the full url, so we will use a different arg for this + '--method=' + request.method, + '-request="' + JSON.stringify(request) + '"', + ], + env: env, + stdin: request.body, + stdout: stdout.writable, + }); + + // Instantiate our WASM with our demo module and our configured WASI import. + const instance = new WebAssembly.Instance(demoWasm, { + wasi_snapshot_preview1: wasi.wasiImport, + }); + + // Keep our worker alive until the WASM has finished executing. + ctx.waitUntil(wasi.start(instance)); + + // Finally, let's reply with the WASM's output. + return new Response(stdout.readable); + }, +}; + diff --git a/src/main.zig b/src/main.zig index c8a3f67..f72e41b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,9 +1,12 @@ const std = @import("std"); -pub fn main() !void { - // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); +// TODO: All this stuff needs to be different +const index = @embedFile("index.js"); +const wasm = @embedFile("demo.wasm"); +const accountid = @embedFile("accountid.txt"); +const worker_name = @embedFile("worker_name.txt"); +pub fn main() !void { // stdout is for the actual output of your application, for example if you // are implementing gzip, then only the compressed bytes should be sent to // stdout, not any debugging messages. @@ -11,7 +14,10 @@ pub fn main() !void { var bw = std.io.bufferedWriter(stdout_file); const stdout = bw.writer(); - try stdout.print("Run `zig build test` to run the tests.\n", .{}); + try stdout.print("Index bytes: {d}\n", .{index.len}); + try stdout.print("Wasm bytes: {d}\n", .{wasm.len}); + try stdout.print("Account: {s}\n", .{accountid}); + try stdout.print("Worker name: {s}\n", .{worker_name}); try bw.flush(); // don't forget to flush! }