From 0c6a9366f51404ed08988d62bd0d6569b4a7d035 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Fri, 20 Oct 2023 22:28:38 -0700 Subject: [PATCH] cloudflare fixes --- src/CloudflareDeployStep.zig | 2 +- src/cloudflare_build.zig | 11 +++++------ src/index.js | 6 +++--- src/lambda_build.zig | 3 +-- src/universal_lambda.zig | 2 +- src/universal_lambda_build.zig | 5 +++-- 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/CloudflareDeployStep.zig b/src/CloudflareDeployStep.zig index 533a311..092bad8 100644 --- a/src/CloudflareDeployStep.zig +++ b/src/CloudflareDeployStep.zig @@ -73,8 +73,8 @@ fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void { b.allocator, &client, self.worker_name, - script, self.options.wasm_dir orelse ".", + script, al.writer(), std.io.getStdErr().writer(), ); diff --git a/src/cloudflare_build.zig b/src/cloudflare_build.zig index 9860a20..2bf921d 100644 --- a/src/cloudflare_build.zig +++ b/src/cloudflare_build.zig @@ -1,22 +1,21 @@ const std = @import("std"); const builtin = @import("builtin"); -const CloudflareDeployStep = @import("CloudflareDeployStep"); +const CloudflareDeployStep = @import("CloudflareDeployStep.zig"); const script = @embedFile("index.js"); -pub fn configureBuild(b: *std.build.Builder, cs: *std.Build.Step.Compile, build_root_src: []const u8) !void { - _ = build_root_src; +pub fn configureBuild(b: *std.build.Builder, cs: *std.Build.Step.Compile, function_name: []const u8) !void { const deploy_cmd = CloudflareDeployStep.create( b, - "zigwasi", + function_name, .{ .path = "index.js" }, .{ .primary_file_data = script, .wasm_name = .{ - .search = "zigout.wasm", + .search = "custom.wasm", .replace = cs.name, }, - .wasm_dir = b.getInstallDir(.bin, "."), + .wasm_dir = b.getInstallPath(.bin, "."), }, ); deploy_cmd.step.dependOn(b.getInstallStep()); diff --git a/src/index.js b/src/index.js index 0279d9b..f7670ec 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -import zigWasm from "zigout.wasm"; +import customWasm from "custom.wasm"; export default { async fetch(request, _env2, ctx) { const stdout = new TransformStream(); @@ -11,7 +11,7 @@ export default { }); const wasi = new WASI({ args: [ - "./zigout.wasm", + "./custom.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 @@ -22,7 +22,7 @@ export default { stdin: request.body, stdout: stdout.writable }); - const instance = new WebAssembly.Instance(demoWasm, { + const instance = new WebAssembly.Instance(customWasm, { wasi_snapshot_preview1: wasi.wasiImport }); ctx.waitUntil(wasi.start(instance)); diff --git a/src/lambda_build.zig b/src/lambda_build.zig index 41225a0..dbcc8c3 100644 --- a/src/lambda_build.zig +++ b/src/lambda_build.zig @@ -30,7 +30,7 @@ fn addArgs(allocator: std.mem.Allocator, original: []const u8, args: [][]const u /// awslambda_deploy depends on iam and package /// /// iam and package do not have any dependencies -pub fn configureBuild(b: *std.build.Builder, exe: *std.Build.Step.Compile) !void { +pub fn configureBuild(b: *std.build.Builder, exe: *std.Build.Step.Compile, function_name: []const u8) !void { // The rest of this function is currently reliant on the use of Linux // system being used to build the lambda function // @@ -132,7 +132,6 @@ pub fn configureBuild(b: *std.build.Builder, exe: *std.Build.Step.Compile) !void break :blk try std.fmt.allocPrint(b.allocator, "--role \"$(cat {s})\"", .{iam_role_file}); }; - const function_name = b.option([]const u8, "function-name", "Function name for Lambda [zig-fn]") orelse "zig-fn"; const function_name_file = b.getInstallPath(.bin, function_name); const ifstatement = "if [ ! -f {s} ] || [ {s} -nt {s} ]; then if aws lambda get-function --function-name {s} 2>&1 |grep -q ResourceNotFoundException; then echo not found > /dev/null; {s}; else echo found > /dev/null; {s}; fi; fi"; // The architectures option was introduced in 2.2.43 released 2021-10-01 diff --git a/src/universal_lambda.zig b/src/universal_lambda.zig index c5dd228..0d6f410 100644 --- a/src/universal_lambda.zig +++ b/src/universal_lambda.zig @@ -24,7 +24,7 @@ const runFn = blk: { switch (build_options.build_type) { .awslambda => break :blk @import("lambda.zig").run, .standalone_server => break :blk runStandaloneServer, - .exe_run => break :blk runExe, + .exe_run, .cloudflare => break :blk runExe, else => @compileError("Provider interface for " ++ @tagName(build_options.build_type) ++ " has not yet been implemented"), } }; diff --git a/src/universal_lambda_build.zig b/src/universal_lambda_build.zig index f9f9e73..461df41 100644 --- a/src/universal_lambda_build.zig +++ b/src/universal_lambda_build.zig @@ -15,6 +15,7 @@ pub const BuildType = enum { pub var module_root: ?[]const u8 = null; pub fn configureBuild(b: *std.Build, cs: *std.Build.Step.Compile) !void { + const function_name = b.option([]const u8, "function-name", "Function name for Lambda [zig-fn]") orelse "zig-fn"; const file_location = try findFileLocation(b); ///////////////////////////////////////////////////////////////////////// // Add modules @@ -62,10 +63,10 @@ pub fn configureBuild(b: *std.Build, cs: *std.Build.Step.Compile) !void { }); // Add steps - try @import("lambda_build.zig").configureBuild(b, cs); + try @import("lambda_build.zig").configureBuild(b, cs, function_name); try @import("standalone_server_build.zig").configureBuild(b, cs); try @import("flexilib_build.zig").configureBuild(b, cs, file_location); - try @import("cloudflare_build.zig").configureBuild(b, cs, file_location); + try @import("cloudflare_build.zig").configureBuild(b, cs, function_name); } /// This function relies on internal implementation of the build runner