Skip to content

Commit a68cff1

Browse files
authored
Merge pull request #653 from cloudflare/kflansburg/run-to-completion
Optionally run Response Promise to completion in waitUntil context
2 parents 636d760 + dd1a91e commit a68cff1

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

worker-build/src/js/shim.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ export { wasmModule };
1010

1111
class Entrypoint extends WorkerEntrypoint {
1212
async fetch(request) {
13-
return await imports.fetch(request, this.env, this.ctx)
13+
let response = imports.fetch(request, this.env, this.ctx);
14+
$WAIT_UNTIL_RESPONSE
15+
return await response;
1416
}
1517

1618
async queue(batch) {

worker-build/src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const OUT_DIR: &str = "build";
1818
const OUT_NAME: &str = "index";
1919
const WORKER_SUBDIR: &str = "worker";
2020

21+
const SHIM_TEMPLATE: &str = include_str!("./js/shim.js");
22+
2123
const WASM_IMPORT: &str = r#"let wasm;
2224
export function __wbg_set_wasm(val) {
2325
wasm = val;
@@ -54,7 +56,13 @@ pub fn main() -> Result<()> {
5456
use_glue_import()?;
5557

5658
write_string_to_file(worker_path("glue.js"), include_str!("./js/glue.js"))?;
57-
write_string_to_file(worker_path("shim.js"), include_str!("./js/shim.js"))?;
59+
let shim = if env::var("RUN_TO_COMPLETION").is_ok() {
60+
SHIM_TEMPLATE.replace("$WAIT_UNTIL_RESPONSE", "this.ctx.waitUntil(response);")
61+
} else {
62+
SHIM_TEMPLATE.replace("$WAIT_UNTIL_RESPONSE", "")
63+
};
64+
65+
write_string_to_file(worker_path("shim.js"), shim)?;
5866

5967
bundle(&esbuild_path)?;
6068

0 commit comments

Comments
 (0)