move user-editable things to a place where users can deal

This commit is contained in:
Emil Lerch 2023-10-18 10:17:38 -07:00
parent 339363ed2d
commit 812d48b681
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
4 changed files with 69 additions and 57 deletions

View File

@ -1,16 +1,12 @@
Upload worker to CloudFlare Upload worker to CloudFlare
=========================== ===========================
Until we're better `zig build run -- <worker name> <script file>`. Make sure that CF_X_AUTH_EMAIL
------------------ and CF_X_AUTH_KEY environment variables are set with the correct authentication
parameters. An example index.js file is included in the root of the project
1. Add `accountid.txt` to `src/` with the CloudFlare account id Getting new src/script_harness.js
2. Add `worker_name.txt` to `src/` with CloudFlare worker name ---------------------------------
3. `zig build run`. Make sure that CF_X_AUTH_EMAIL and CF_X_AUTH_KEY environment
variables are set with the correct authentication parameters
Getting new index.js
--------------------
* Run a real wrangler deploy, then go into the console and copy/paste * Run a real wrangler deploy, then go into the console and copy/paste
@ -27,7 +23,8 @@ Getting new memfs.wasm
Steps we take: Steps we take:
-------------- --------------
0. Get account id. CF_ACCOUNT_ID environment variable will be checked first. If not,
GET https://api.cloudflare.com/client/v4/accounts/
1. Check if the worker exists: 1. Check if the worker exists:
GET https://api.cloudflare.com/client/v4/accounts/<account id>/workers/services/<worker_name> GET https://api.cloudflare.com/client/v4/accounts/<account id>/workers/services/<worker_name>
404 - does not exist 404 - does not exist

34
index.js Normal file
View File

@ -0,0 +1,34 @@
import demoWasm from "demo.wasm";
var src_default = {
async fetch(request, _env2, ctx) {
const stdout = new TransformStream();
console.log(request);
console.log(_env2);
console.log(ctx);
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,
stdin: request.body,
stdout: stdout.writable
});
const instance = new WebAssembly.Instance(demoWasm, {
wasi_snapshot_preview1: wasi.wasiImport
});
ctx.waitUntil(wasi.start(instance));
return new Response(stdout.readable);
}
};
export {
src_default as default
};

View File

@ -12,60 +12,41 @@ pub fn main() !u8 {
const allocator = arena.allocator(); const allocator = arena.allocator();
var client = std.http.Client{ .allocator = allocator }; var client = std.http.Client{ .allocator = allocator };
// TODO: All this stuff needs to be different
//
const worker_name: []const u8 = @embedFile("worker_name.txt");
// TODO: We need to break index.js into the wrangler-generated bundling thing
// and the actual code we're using to run the wasm file.
// We might actually want a "run this wasm" upload vs a "these are my
// js files" upload. But for now we'll optimize for wasm
const script =
\\import demoWasm from "demo.wasm";
\\var src_default = {
\\ async fetch(request, _env2, ctx) {
\\ const stdout = new TransformStream();
\\ console.log(request);
\\ console.log(_env2);
\\ console.log(ctx);
\\ 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,
\\ stdin: request.body,
\\ stdout: stdout.writable
\\ });
\\ const instance = new WebAssembly.Instance(demoWasm, {
\\ wasi_snapshot_preview1: wasi.wasiImport
\\ });
\\ ctx.waitUntil(wasi.start(instance));
\\ return new Response(stdout.readable);
\\ }
\\};
\\export {
\\ src_default as default
\\};
;
// 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.
const stdout_file = std.io.getStdOut().writer(); const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file); var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer(); const stdout = bw.writer();
var argIterator = try std.process.argsWithAllocator(allocator);
defer argIterator.deinit();
const exe_name = argIterator.next().?;
var maybe_name = argIterator.next();
if (maybe_name == null) {
try usage(std.io.getStdErr().writer(), exe_name);
return 1;
}
const worker_name = maybe_name.?;
if (std.mem.eql(u8, worker_name, "-h")) {
try usage(stdout, exe_name);
return 0;
}
var maybe_script_name = argIterator.next();
if (maybe_script_name == null) {
try usage(std.io.getStdErr().writer(), exe_name);
return 1;
}
const script = std.fs.cwd().readFileAlloc(allocator, maybe_script_name.?, std.math.maxInt(usize)) catch |err| {
try usage(std.io.getStdErr().writer(), exe_name);
return err;
};
pushWorker(allocator, &client, worker_name, script, stdout, std.io.getStdErr().writer()) catch return 1; pushWorker(allocator, &client, worker_name, script, stdout, std.io.getStdErr().writer()) catch return 1;
try bw.flush(); // don't forget to flush! try bw.flush(); // don't forget to flush!
return 0; return 0;
} }
fn usage(writer: anytype, this: []const u8) !void {
try writer.print("usage: {s} <worker name> <script file>\n", .{this});
}
const Wasm = struct { const Wasm = struct {
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
name: []const u8, name: []const u8,
@ -256,7 +237,7 @@ fn putNewWorker(allocator: std.mem.Allocator, client: *std.http.Client, worker:
const url = try std.fmt.allocPrint(allocator, put_script, .{ worker.account_id, worker.name }); const url = try std.fmt.allocPrint(allocator, put_script, .{ worker.account_id, worker.name });
defer allocator.free(url); defer allocator.free(url);
const memfs = @embedFile("dist/memfs.wasm"); const memfs = @embedFile("dist/memfs.wasm");
const outer_script_shell = @embedFile("index.js"); const outer_script_shell = @embedFile("script_harness.js");
const script = try std.fmt.allocPrint(allocator, "{s}{s}", .{ outer_script_shell, worker.main_module }); const script = try std.fmt.allocPrint(allocator, "{s}{s}", .{ outer_script_shell, worker.main_module });
defer allocator.free(script); defer allocator.free(script);
const deploy_request = const deploy_request =