Skip to content

feat: honor the Workers ExecutionContext through waitUntil - #65

Open
martijn00 wants to merge 1 commit into
supabase:mainfrom
martijn00:feat/execution-context-wait-until
Open

martijn00 wants to merge 1 commit into
supabase:mainfrom
martijn00:feat/execution-context-wait-until

Conversation

@martijn00

Copy link
Copy Markdown

What

  • The entry call keeps the third fetch argument (the Workers ExecutionContext) on the seeded context behind a Symbol.for('@supabase/middleware:execution-context') key, the same way the context marker rides it. It is not a ctx key, so ctx still carries only middleware contributions, and the one-time "not honored" warning is gone.
  • New export waitUntil(ctx, promise). With an execution context it calls executionContext.waitUntil(promise) (as a method, so workerd's this check holds). Without one it runs the promise unattached and logs a rejection with console.error, so middleware can call it on any host.
  • seedContext(platformArg?, executionContext?) gains an optional second parameter, so a host embedding the engine can pass the execution context too. Existing callers are unaffected.
  • New ExecutionContextLike type, README update for the entry signatures.

Why

waitUntil is how Workers keep work running after the response is sent (telemetry, audit logs, cache revalidation). Today the entry drops it, so neither the middlewares in a stack nor the handler at the bottom can schedule background work without smuggling the execution context through some other channel.

Closes #53

Testing

  • src/core/runtime.test.ts: promises from a middleware and from the handler reach the host's execution context through a pipeline and a composite, two concurrent requests keep their own execution contexts, the fallback logs a rejection, and an argument without waitUntil is ignored.
  • src/core/define-middleware.test.ts: the old warn-and-ignore test now checks that the third argument neither warns nor leaks into ctx.
  • test/runtimes/workers: the built dist calls waitUntil inside workerd with the real ExecutionContext.

Ran locally: pnpm format:check, pnpm lint, pnpm typecheck, pnpm typecheck:min, pnpm build, pnpm typecheck:consumer, pnpm smoke, pnpm check-exports, pnpm test (128 tests), pnpm test:workers, pnpm test:bun, and the JSR dry run. I did not run pnpm test:deno (no Deno locally); the Deno target never passes a third argument.

Alternatives

  • The issue suggested ctx.waitUntil(promise). I went with an importable function because the README says ctx carries no reserved framework key, and a waitUntil key could collide with a middleware's contribution. getEnv sets the precedent for importable runtime access.
  • The execution context rides the context rather than module scope (like the platform env does) because it is different for every request, and module scope would race across concurrent requests.
  • passThroughOnException is not exposed. A getExecutionContext(ctx) accessor could expose it later if there is demand.

Prior art: the better-supabase edge bridge stores the execution context on the seeded context under a private symbol and hands it to the handler.

🤖 Generated with Claude Code

The entry call now keeps the third fetch argument on the seeded context
behind a symbol instead of warning and dropping it. A new waitUntil(ctx,
promise) hands background work to it from any middleware or the handler,
and runs the promise unattached with a logged rejection when the host
passed no execution context.

Closes supabase#53

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Honor the Workers ExecutionContext (waitUntil) instead of dropping it

1 participant