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 committed Dec 23, 2024
1 parent 5c31380 commit bbbf9d9
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 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,29 @@ pub fn main() -> Result<()> {
create_worker_dir()?;
copy_generated_code_to_worker_dir()?;

let custom_shim = match env::var("CUSTOM_SHIM") {
Ok(path) => {
let path = Path::new(&path).to_owned();

if !path.exists() {
None
} else {
println!("Using custom shim from {}", path.display());
Some(path)
}
}
Err(_) => None,
};

let shim_template = match custom_shim {
Some(path) => read_to_string(path)?,
None => 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 bbbf9d9

Please sign in to comment.