Skip to content

Commit

Permalink
chore: add dispatchEntry(entry, request) example (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Oct 27, 2024
1 parent bc1869d commit c6010e7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
4 changes: 3 additions & 1 deletion examples/workerd-cli/e2e/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export function createProcessHelper(
listeners.add(listener);
});
const timeout = sleep(5000).then(() => {
throw new Error("waitFor timeout", { cause: stdout });
const error = new Error("waitFor timeout", { cause: stdout });
Error.captureStackTrace(error, waitFor);
throw error;
});
return Promise.race([promise, timeout]);
}
Expand Down
25 changes: 14 additions & 11 deletions examples/workerd-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,21 @@ async function main() {
if (!cmd.includes("return")) {
cmd = `return ${cmd}`;
}
// TODO: we can invalidate virtual entry after eval
const entrySource = `export default async function (env) { ${cmd} }`;
const entry = "virtual:eval/" + encodeURI(entrySource);
await devEnv.api.eval({
entry,
fn: async ({ mod, env }) => {
const result = await mod.default(env);
if (typeof result !== "undefined") {
console.log(result);
const entrySource = `
async function run(env) { ${cmd} };
export default {
async fetch(request, env) {
const result = await run(env);
if (typeof result !== "undefined") {
console.log(result);
}
return new Response(null);
}
},
});
}
`;
const entry = "virtual:eval/" + encodeURI(entrySource);
await devEnv.api.dispatchFetch(entry, new Request("http://localhost"));
}

const replServer = repl.start({
Expand Down
3 changes: 2 additions & 1 deletion packages/workerd/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export class RunnerObject implements DurableObject {
const handler = mod.default as ExportedHandler;
tinyassert(handler.fetch);

return handler.fetch(request, this.#env, {
const env = objectPickBy(this.#env, (_v, k) => !k.startsWith("__vite"));
return handler.fetch(request, env, {
waitUntil(_promise: Promise<any>) {},
passThroughOnException() {},
abort(_reason?: any) {},
Expand Down

0 comments on commit c6010e7

Please sign in to comment.