merge changes made in universal lambda (options and wasm directory)
All checks were successful
Generic zig build / build (push) Successful in 1m9s
All checks were successful
Generic zig build / build (push) Successful in 1m9s
This commit is contained in:
parent
feb4d108ca
commit
68e6e14bea
22
build.zig
22
build.zig
|
@ -76,3 +76,25 @@ pub fn build(b: *std.Build) void {
|
||||||
const test_step = b.step("test", "Run unit tests");
|
const test_step = b.step("test", "Run unit tests");
|
||||||
test_step.dependOn(&run_unit_tests.step);
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -7,8 +7,23 @@ pub const base_id: std.Build.Step.Id = .custom;
|
||||||
step: std.Build.Step,
|
step: std.Build.Step,
|
||||||
primary_javascript_path: std.Build.LazyPath,
|
primary_javascript_path: std.Build.LazyPath,
|
||||||
worker_name: []const u8,
|
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(
|
pub fn create(
|
||||||
owner: *std.Build,
|
owner: *std.Build,
|
||||||
|
@ -16,7 +31,6 @@ pub fn create(
|
||||||
primary_javascript_path: std.Build.LazyPath,
|
primary_javascript_path: std.Build.LazyPath,
|
||||||
options: Options,
|
options: Options,
|
||||||
) *CloudflareDeployStep {
|
) *CloudflareDeployStep {
|
||||||
_ = options;
|
|
||||||
const self = owner.allocator.create(CloudflareDeployStep) catch @panic("OOM");
|
const self = owner.allocator.create(CloudflareDeployStep) catch @panic("OOM");
|
||||||
self.* = CloudflareDeployStep{
|
self.* = CloudflareDeployStep{
|
||||||
.step = std.Build.Step.init(.{
|
.step = std.Build.Step.init(.{
|
||||||
|
@ -27,7 +41,9 @@ pub fn create(
|
||||||
}),
|
}),
|
||||||
.primary_javascript_path = primary_javascript_path,
|
.primary_javascript_path = primary_javascript_path,
|
||||||
.worker_name = worker_name,
|
.worker_name = worker_name,
|
||||||
|
.options = options,
|
||||||
};
|
};
|
||||||
|
if (options.primary_file_data == null)
|
||||||
primary_javascript_path.addStepDependencies(&self.step);
|
primary_javascript_path.addStepDependencies(&self.step);
|
||||||
return self;
|
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));
|
const self = @as(*CloudflareDeployStep, @fieldParentPtr("step", step));
|
||||||
|
|
||||||
var client = std.http.Client{ .allocator = b.allocator };
|
var client = std.http.Client{ .allocator = b.allocator };
|
||||||
|
try client.initDefaultProxies(b.allocator);
|
||||||
defer client.deinit();
|
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);
|
var al = std.ArrayList(u8).init(b.allocator);
|
||||||
defer al.deinit();
|
defer al.deinit();
|
||||||
|
@ -48,6 +74,7 @@ fn make(step: *std.Build.Step, prog_node: *std.Progress.Node) !void {
|
||||||
b.allocator,
|
b.allocator,
|
||||||
&client,
|
&client,
|
||||||
self.worker_name,
|
self.worker_name,
|
||||||
|
self.options.wasm_dir orelse ".",
|
||||||
script,
|
script,
|
||||||
al.writer(),
|
al.writer(),
|
||||||
std.io.getStdErr().writer(),
|
std.io.getStdErr().writer(),
|
||||||
|
|
11
src/main.zig
11
src/main.zig
|
@ -50,7 +50,7 @@ pub fn main() !u8 {
|
||||||
return err;
|
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;
|
||||||
}
|
}
|
||||||
|
@ -75,11 +75,12 @@ pub fn pushWorker(
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
client: *std.http.Client,
|
client: *std.http.Client,
|
||||||
worker_name: []const u8,
|
worker_name: []const u8,
|
||||||
|
wasm_dir: []const u8,
|
||||||
script: []const u8,
|
script: []const u8,
|
||||||
writer: anytype,
|
writer: anytype,
|
||||||
err_writer: anytype,
|
err_writer: anytype,
|
||||||
) !void {
|
) !void {
|
||||||
var wasm = try loadWasm(allocator, script);
|
var wasm = try loadWasm(allocator, script, wasm_dir);
|
||||||
defer wasm.deinit();
|
defer wasm.deinit();
|
||||||
|
|
||||||
var accountid = std.posix.getenv("CLOUDFLARE_ACCOUNT_ID");
|
var accountid = std.posix.getenv("CLOUDFLARE_ACCOUNT_ID");
|
||||||
|
@ -118,7 +119,7 @@ pub fn pushWorker(
|
||||||
try enableWorker(allocator, client, accountid.?, worker_name);
|
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"
|
// Looking for a string like this: import demoWasm from "demo.wasm"
|
||||||
// JavaScript may or may not have ; characters. We're not doing
|
// JavaScript may or may not have ; characters. We're not doing
|
||||||
// a full JS parsing here, so this may not be the most robust
|
// 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.?);
|
const nm = try allocator.dupe(u8, name.?);
|
||||||
errdefer allocator.free(nm);
|
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{
|
return Wasm{
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
.name = nm,
|
.name = nm,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user