Skip to content

Commit

Permalink
feat: make worker-build support custom JS shims
Browse files Browse the repository at this point in the history
This allows for users to provide their own JS shim and allowing them to
customize behaviour: panic handling, wasm coredumps, and so on...

It defaults to the embedded shim in the binary to avoid breaking
changes,
  • Loading branch information
LuisDuarte1 authored and mendess committed Jan 6, 2025
1 parent 5c31380 commit 2f401a4
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions worker-build/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{
env::{self, VarError},
fs::{self, File},
fs::{self, read_to_string, File},
io::{Read, Write},
path::{Path, PathBuf},
process::{Command, Stdio},
Expand Down Expand Up @@ -38,10 +38,20 @@ pub fn main() -> Result<()> {
create_worker_dir()?;
copy_generated_code_to_worker_dir()?;

let shim_template = match env::var("CUSTOM_SHIM") {
Ok(path) => {
let path = Path::new(&path).to_owned();
println!("Using custom shim from {}", path.display());
// NOTE: we fail in case that file doesnt exist or something else happens
read_to_string(path)?
}
Err(_) => SHIM_TEMPLATE.to_owned(),
};

let shim = if env::var("RUN_TO_COMPLETION").is_ok() {
SHIM_TEMPLATE.replace("$WAIT_UNTIL_RESPONSE", "this.ctx.waitUntil(response);")
shim_template.replace("$WAIT_UNTIL_RESPONSE", "this.ctx.waitUntil(response);")
} else {
SHIM_TEMPLATE.replace("$WAIT_UNTIL_RESPONSE", "")
shim_template.replace("$WAIT_UNTIL_RESPONSE", "")
};

write_string_to_file(worker_path("shim.js"), shim)?;
Expand Down

0 comments on commit 2f401a4

Please sign in to comment.