Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make worker-build support custom JS shims #686

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading