Skip to content

Commit

Permalink
Merge pull request #653 from cloudflare/kflansburg/run-to-completion
Browse files Browse the repository at this point in the history
Optionally run Response Promise to completion in waitUntil context
  • Loading branch information
zebp authored Nov 19, 2024
2 parents 636d760 + dd1a91e commit a68cff1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion worker-build/src/js/shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export { wasmModule };

class Entrypoint extends WorkerEntrypoint {
async fetch(request) {
return await imports.fetch(request, this.env, this.ctx)
let response = imports.fetch(request, this.env, this.ctx);
$WAIT_UNTIL_RESPONSE
return await response;
}

async queue(batch) {
Expand Down
10 changes: 9 additions & 1 deletion worker-build/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const OUT_DIR: &str = "build";
const OUT_NAME: &str = "index";
const WORKER_SUBDIR: &str = "worker";

const SHIM_TEMPLATE: &str = include_str!("./js/shim.js");

const WASM_IMPORT: &str = r#"let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
Expand Down Expand Up @@ -54,7 +56,13 @@ pub fn main() -> Result<()> {
use_glue_import()?;

write_string_to_file(worker_path("glue.js"), include_str!("./js/glue.js"))?;
write_string_to_file(worker_path("shim.js"), include_str!("./js/shim.js"))?;
let shim = if env::var("RUN_TO_COMPLETION").is_ok() {
SHIM_TEMPLATE.replace("$WAIT_UNTIL_RESPONSE", "this.ctx.waitUntil(response);")
} else {
SHIM_TEMPLATE.replace("$WAIT_UNTIL_RESPONSE", "")
};

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

bundle(&esbuild_path)?;

Expand Down

0 comments on commit a68cff1

Please sign in to comment.