From 68e6e14beabb9be2f233ed01484c1ddd5033979f Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Mon, 6 May 2024 11:16:25 -0700 Subject: [PATCH] merge changes made in universal lambda (options and wasm directory) --- build.zig | 22 ++++++++++++++++++++++ src/CloudflareDeployStep.zig | 35 +++++++++++++++++++++++++++++++---- src/main.zig | 11 +++++++---- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/build.zig b/build.zig index c924b3a..e0aa461 100644 --- a/build.zig +++ b/build.zig @@ -76,3 +76,25 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_unit_tests.step); } + +pub fn configureBuild(b: *std.Build, cs: *std.Build.Step.Compile, function_name: []const u8) !void { + const script = @embedFile("index.js"); + const wasm_name = try std.fmt.allocPrint(b.allocator, "{s}.wasm", .{cs.name}); + const deploy_cmd = CloudflareDeployStep.create( + b, + function_name, + .{ .path = "index.js" }, + .{ + .primary_file_data = script, + .wasm_name = .{ + .search = "custom.wasm", + .replace = wasm_name, + }, + .wasm_dir = b.getInstallPath(.bin, "."), + }, + ); + deploy_cmd.step.dependOn(b.getInstallStep()); + + const deploy_step = b.step("cloudflare", "Deploy as Cloudflare worker (must be compiled with -Dtarget=wasm32-wasi)"); + deploy_step.dependOn(&deploy_cmd.step); +} diff --git a/src/CloudflareDeployStep.zig b/src/CloudflareDeployStep.zig index 29586c4..cb4e147 100644 --- a/src/CloudflareDeployStep.zig +++ b/src/CloudflareDeployStep.zig @@ -7,8 +7,23 @@ pub const base_id: std.Build.Step.Id = .custom; step: std.Build.Step, primary_javascript_path: std.Build.LazyPath, worker_name: []const u8, +options: Options, -pub const Options = struct {}; +pub const Options = struct { + /// if set, the primary file will not be read (and may not exist). This data + /// will be used instead + primary_file_data: ?[]const u8 = null, + + /// When set, the Javascript file will be searched/replaced with the target + /// file name for import + wasm_name: ?struct { + search: []const u8, + replace: []const u8, + } = null, + + /// When set, the directory specified will be used rather than the current directory + wasm_dir: ?[]const u8 = null, +}; pub fn create( owner: *std.Build, @@ -16,7 +31,6 @@ pub fn create( primary_javascript_path: std.Build.LazyPath, options: Options, ) *CloudflareDeployStep { - _ = options; const self = owner.allocator.create(CloudflareDeployStep) catch @panic("OOM"); self.* = CloudflareDeployStep{ .step = std.Build.Step.init(.{ @@ -27,8 +41,10 @@ pub fn create( }), .primary_javascript_path = primary_javascript_path, .worker_name = worker_name, + .options = options, }; - primary_javascript_path.addStepDependencies(&self.step); + if (options.primary_file_data == null) + primary_javascript_path.addStepDependencies(&self.step); return self; } @@ -38,9 +54,19 @@ fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void { const self = @as(*CloudflareDeployStep, @fieldParentPtr("step", step)); var client = std.http.Client{ .allocator = b.allocator }; + try client.initDefaultProxies(b.allocator); defer client.deinit(); - const script = try std.fs.cwd().readFileAlloc(b.allocator, self.primary_javascript_path.path, std.math.maxInt(usize)); + const script = self.options.primary_file_data orelse + try std.fs.cwd().readFileAlloc(b.allocator, self.primary_javascript_path.path, std.math.maxInt(usize)); + defer if (self.options.primary_file_data == null) b.allocator.free(script); + + var final_script = script; + if (self.options.wasm_name) |n| { + final_script = try std.mem.replaceOwned(u8, b.allocator, script, n.search, n.replace); + if (self.options.primary_file_data == null) b.allocator.free(script); + } + defer if (self.options.wasm_name) |_| b.allocator.free(final_script); var al = std.ArrayList(u8).init(b.allocator); defer al.deinit(); @@ -48,6 +74,7 @@ fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void { b.allocator, &client, self.worker_name, + self.options.wasm_dir orelse ".", script, al.writer(), std.io.getStdErr().writer(), diff --git a/src/main.zig b/src/main.zig index a3f3f9c..f3a5851 100644 --- a/src/main.zig +++ b/src/main.zig @@ -50,7 +50,7 @@ pub fn main() !u8 { 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! return 0; } @@ -75,11 +75,12 @@ pub fn pushWorker( allocator: std.mem.Allocator, client: *std.http.Client, worker_name: []const u8, + wasm_dir: []const u8, script: []const u8, writer: anytype, err_writer: anytype, ) !void { - var wasm = try loadWasm(allocator, script); + var wasm = try loadWasm(allocator, script, wasm_dir); defer wasm.deinit(); var accountid = std.posix.getenv("CLOUDFLARE_ACCOUNT_ID"); @@ -118,7 +119,7 @@ pub fn pushWorker( try enableWorker(allocator, client, accountid.?, worker_name); } -fn loadWasm(allocator: std.mem.Allocator, script: []const u8) !Wasm { +fn loadWasm(allocator: std.mem.Allocator, script: []const u8, wasm_dir: []const u8) !Wasm { // Looking for a string like this: import demoWasm from "demo.wasm" // JavaScript may or may not have ; characters. We're not doing // a full JS parsing here, so this may not be the most robust @@ -159,7 +160,9 @@ fn loadWasm(allocator: std.mem.Allocator, script: []const u8) !Wasm { const nm = try allocator.dupe(u8, name.?); errdefer allocator.free(nm); - const data = try std.fs.cwd().readFileAlloc(allocator, nm, std.math.maxInt(usize)); + const path = try std.fs.path.join(allocator, &[_][]const u8{ wasm_dir, nm }); + defer allocator.free(path); + const data = try std.fs.cwd().readFileAlloc(allocator, path, std.math.maxInt(usize)); return Wasm{ .allocator = allocator, .name = nm,