merge changes made in universal lambda (options and wasm directory)
Some checks failed
Generic zig build / build (push) Failing after 1m31s

This commit is contained in:
Emil Lerch 2024-05-06 11:16:25 -07:00
parent feb4d108ca
commit 26675fac0d
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
3 changed files with 59 additions and 7 deletions

View File

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

View File

@ -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(),

View File

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