get some basic structure

This commit is contained in:
Emil Lerch 2023-10-17 13:49:08 -07:00
parent abe59468b6
commit 6988555e51
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
4 changed files with 59 additions and 4 deletions

View File

@ -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:
--------------

BIN
src/demo.wasm Executable file

Binary file not shown.

41
src/index.js Normal file
View File

@ -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);
},
};

View File

@ -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!
}