Skip to content

Commit 2f401a4

Browse files
LuisDuarte1mendess
authored andcommitted
feat: make worker-build support custom JS shims
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,
1 parent 5c31380 commit 2f401a4

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

worker-build/src/main.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{
44
env::{self, VarError},
5-
fs::{self, File},
5+
fs::{self, read_to_string, File},
66
io::{Read, Write},
77
path::{Path, PathBuf},
88
process::{Command, Stdio},
@@ -38,10 +38,20 @@ pub fn main() -> Result<()> {
3838
create_worker_dir()?;
3939
copy_generated_code_to_worker_dir()?;
4040

41+
let shim_template = match env::var("CUSTOM_SHIM") {
42+
Ok(path) => {
43+
let path = Path::new(&path).to_owned();
44+
println!("Using custom shim from {}", path.display());
45+
// NOTE: we fail in case that file doesnt exist or something else happens
46+
read_to_string(path)?
47+
}
48+
Err(_) => SHIM_TEMPLATE.to_owned(),
49+
};
50+
4151
let shim = if env::var("RUN_TO_COMPLETION").is_ok() {
42-
SHIM_TEMPLATE.replace("$WAIT_UNTIL_RESPONSE", "this.ctx.waitUntil(response);")
52+
shim_template.replace("$WAIT_UNTIL_RESPONSE", "this.ctx.waitUntil(response);")
4353
} else {
44-
SHIM_TEMPLATE.replace("$WAIT_UNTIL_RESPONSE", "")
54+
shim_template.replace("$WAIT_UNTIL_RESPONSE", "")
4555
};
4656

4757
write_string_to_file(worker_path("shim.js"), shim)?;

0 commit comments

Comments
 (0)