step 2 complete

This commit is contained in:
Emil Lerch 2023-10-17 19:53:48 -07:00
parent 4e90d05a77
commit ed49714d4b
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -13,19 +13,8 @@ pub fn main() !void {
x_auth_key = std.os.getenv("CF_X_AUTH_KEY").?;
// TODO: All this stuff needs to be different
//
// 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 index = @embedFile("index.js");
const wasm = @embedFile("demo.wasm");
const memfs = @embedFile("dist/memfs.wasm");
const accountid = @embedFile("accountid.txt");
const worker_name = @embedFile("worker_name.txt");
const deploy_request = @embedFile("deploy_request.txt");
_ = deploy_request;
const put_script = "https://api.cloudflare.com/client/v4/accounts/{s}/workers/scripts/{s}?include_subdomain_availability=true&excludeScript=true";
_ = put_script;
const get_subdomain = "https://api.cloudflare.com/client/v4/accounts/{s}/workers/scripts/{s}/subdomain";
_ = get_subdomain;
@ -36,19 +25,90 @@ pub fn main() !void {
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Index bytes: {d}\n", .{index.len});
try stdout.print("Wasm bytes: {d}\n", .{wasm.len});
try stdout.print("Memfs bytes: {d}\n", .{memfs.len});
try stdout.print("Account: {s}\n", .{accountid});
try stdout.print("Worker name: {s}\n", .{worker_name});
// Determine if worker exists. This lets us know if we need to enable it later
const worker_exists = try workerExists(allocator, &client, accountid, worker_name);
try stdout.print(
"Worker exists: {}\n",
.{worker_exists},
);
try putNewWorker(allocator, &client, accountid, worker_name);
try bw.flush(); // don't forget to flush!
}
fn putNewWorker(allocator: std.mem.Allocator, client: *std.http.Client, account_id: []const u8, name: []const u8) !void {
const put_script = "https://api.cloudflare.com/client/v4/accounts/{s}/workers/scripts/{s}?include_subdomain_availability=true&excludeScript=true";
const url = try std.fmt.allocPrint(allocator, put_script, .{ account_id, name });
defer allocator.free(url);
// TODO: All this stuff needs to be different
//
// 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 = @embedFile("index.js");
const wasm = @embedFile("demo.wasm");
const memfs = @embedFile("dist/memfs.wasm");
const deploy_request =
"------formdata-undici-032998177938\r\n" ++
"Content-Disposition: form-data; name=\"metadata\"\r\n\r\n" ++
"{{\"main_module\":\"index.js\",\"bindings\":[],\"compatibility_date\":\"2023-10-02\",\"compatibility_flags\":[]}}\r\n" ++
"------formdata-undici-032998177938\r\n" ++
"Content-Disposition: form-data; name=\"index.js\"; filename=\"index.js\"\r\n" ++
"Content-Type: application/javascript+module\r\n" ++
"\r\n" ++
"{[script]s}\r\n" ++
"------formdata-undici-032998177938\r\n" ++
"Content-Disposition: form-data; name=\"./24526702f6c3ed7fb02b15125f614dd38804525f-demo.wasm\"; filename=\"./24526702f6c3ed7fb02b15125f614dd38804525f-demo.wasm\"\r\n" ++
"Content-Type: application/wasm\r\n" ++
"\r\n" ++
"{[wasm]s}\r\n" ++
"------formdata-undici-032998177938\r\n" ++
"Content-Disposition: form-data; name=\"./c5f1acc97ad09df861eff9ef567c2186d4e38de3-memfs.wasm\"; filename=\"./c5f1acc97ad09df861eff9ef567c2186d4e38de3-memfs.wasm\"\r\n" ++
"Content-Type: application/wasm\r\n" ++
"\r\n" ++
"{[memfs]s}\r\n" ++
"------formdata-undici-032998177938--";
var headers = std.http.Headers.init(allocator);
defer headers.deinit();
try headers.append("X-Auth-Email", x_auth_email);
try headers.append("X-Auth-Key", x_auth_key);
// TODO: fix this
try headers.append("Content-Type", "multipart/form-data; boundary=----formdata-undici-032998177938");
const request_payload = try std.fmt.allocPrint(allocator, deploy_request, .{
.script = script,
.wasm = wasm,
.memfs = memfs,
});
defer allocator.free(request_payload);
// Get content length. For some reason it's forcing a chunked transfer type without this.
// That's not entirely a bad idea, but for now I want to match what I see
// coming through wrangler
const cl = try std.fmt.allocPrint(allocator, "{d}", .{request_payload.len});
defer allocator.free(cl);
try headers.append("Content-Length", cl);
std.debug.print("payload length {d}\n", .{request_payload.len});
const blah1 = "------formdata-undici-032998177938\r\nC";
std.debug.print("first 80 bytes {s}", .{request_payload[0..80]});
std.debug.print("Starts with? {}", .{std.mem.startsWith(u8, request_payload, blah1)});
var req = try client.request(.PUT, try std.Uri.parse(url), headers, .{});
defer req.deinit();
req.transfer_encoding = .{ .content_length = @as(u64, request_payload.len) };
try req.start();
try req.writeAll(request_payload);
try req.finish();
try req.wait();
// std.debug.print("Status is {}\n", .{req.response.status});
// std.debug.print("Url is {s}\n", .{url});
if (req.response.status != .ok) {
std.debug.print("Status is {}\n", .{req.response.status});
return error.RequestFailed;
}
}
fn workerExists(allocator: std.mem.Allocator, client: *std.http.Client, account_id: []const u8, name: []const u8) !bool {
const existence_check = "https://api.cloudflare.com/client/v4/accounts/{s}/workers/services/{s}";