Skip to content

feat(functions): implement waitUntil runtime integration#207

Merged
andrelandgraf merged 3 commits into
mainfrom
feat/functions-wait-until
Jun 14, 2026
Merged

feat(functions): implement waitUntil runtime integration#207
andrelandgraf merged 3 commits into
mainfrom
feat/functions-wait-until

Conversation

@andrelandgraf

Copy link
Copy Markdown
Collaborator

Summary

Implements waitUntil() in @neondatabase/functions (replaces the no-op placeholder).

  • The function returned by waitUntil() now forwards the promise to the Neon Functions runtime via its per-invocation context: globalThis[NEON_FUNCTIONS_CONTEXT], where NEON_FUNCTIONS_CONTEXT = Symbol.for("@neondatabase/functions/request-context") (mirrors Vercel's @vercel/request-context convention).
  • Matches Vercel's waitUntil spec: a non-Promise argument throws TypeError.
  • No-ops when the runtime hasn't published a context (local dev, tests, non-Neon hosts), so the same handler code runs everywhere.
  • Context is resolved lazily (at defer time), so it stays correct even if waitUntil() is called at module scope.
  • Exports NEON_FUNCTIONS_CONTEXT as the documented runtime contract; README updated; changeset added (minor).

Open question for review

The global context key is not yet confirmed against the runtime host — please verify Symbol.for("@neondatabase/functions/request-context") (and the flat .waitUntil shape vs. a Vercel-style .get() accessor) matches what the Functions runtime actually publishes. If it doesn't, waitUntil will silently no-op in production.

Test plan

  • pnpm tsc (typecheck)
  • pnpm test:ci (5 tests, real globalThis wiring, no mocks)
  • pnpm build (tsdown)

Forward the deferred promise to the Neon Functions runtime via its
per-invocation context (globalThis[NEON_FUNCTIONS_CONTEXT], a
Symbol.for("@neondatabase/functions/request-context")), keeping the
invocation alive until the promise settles. Matches Vercel's spec: a
non-Promise argument throws TypeError, and the function no-ops when no
runtime context is present (local dev, tests, non-Neon hosts). Export
NEON_FUNCTIONS_CONTEXT as the documented runtime contract.
…LocalStorage context

Switch waitUntil from a factory to a direct call (waitUntil(promise)) to mirror
@vercel/functions. Carry per-invocation context in an AsyncLocalStorage published on
globalThis[NEON_FUNCTIONS_CONTEXT] as a `{ get() }` provider (Vercel/Next contract),
fixing context clobbering across concurrent invocations. Add runWithRequestContext for
the runtime to bind a context per invocation.
@andrelandgraf andrelandgraf merged commit 63175ef into main Jun 14, 2026
10 checks passed
@andrelandgraf andrelandgraf deleted the feat/functions-wait-until branch June 14, 2026 02:49
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.

2 participants