Skip to content

feat: type a pipeline in an unanchored stack against the enclosing context - #55

Draft
johnstonmatt wants to merge 2 commits into
FUNC-1185/middleware/pipeline-anchored-stackfrom
FUNC-1177/middleware/pipeline-handler-position
Draft

johnstonmatt wants to merge 2 commits into
FUNC-1185/middleware/pipeline-anchored-stackfrom
FUNC-1177/middleware/pipeline-handler-position

Conversation

@johnstonmatt

@johnstonmatt johnstonmatt commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Stacked on #56, which types a pipeline against the enclosing context when the stack is anchored. This PR removes the need for the anchor: a pipeline written as a middleware's or composite's handler argument sees the keys around it in any stack written as one expression.

export default {
  fetch: withA(
    pipeline([withB()], async (_req, ctx) => {
      ctx.a // #56: Property 'a' does not exist · here: string
      ctx.b
    }),
  ),
}

Why the unanchored form fails without this:

typing an inline handler inside a generic call
  no type parameter inferred yet → from the call's expected return type   ← a middleware's handler
  otherwise                      → from the ordinary inferences           ← pipeline's handler (Entries comes from the first argument)
    an enclosing call's not-yet-inferred Base reads as `never` there, unless an anchor supplied it
      → the enclosing middleware's handler type collapses, and its keys are lost
 Middleware / Composite handler ctx
-  NoInfer<Base> & { key: … }             # a `never` Base collapses it
+  NoInfer<Upstream<Base>> & { key: … }   # a `never` Base reads as the empty upstream
 NoConflict
-  keyof Base
+  keyof Upstream<Base>
 propagation overloads
+  NoMismatch: a requirement is discharged only by a contribution of a type it accepts

Nested stacks without satisfies FetchHandler now infer the way anchored ones do, so they also catch a duplicate key. There are no new exports.

Hand-written signatures

Nesting a middleware inside one that publishes a hand-written generic signature, such as withSupabase, fails to type-check before TypeScript 5.9 with these handler types:

withSupabase(config, withPostgresClient(async (_req, ctx) => {
  // TS 5.5–5.8: No overload matches this call
  ctx.supabase // TS 5.5–5.8: Property 'supabase' does not exist
}))

Before 5.9, TypeScript passes the outer signature's unresolved Base into the inner call, where Upstream and NoMismatch can't be evaluated. That covers deno check on Deno 2.0 to 2.4, and Edge Functions' runtime is built on Deno 2.1.4. Middleware typed with the engine's own Middleware and Composite types nest correctly on every version from 5.4. So this ships no earlier than a @supabase/server release with supabase/server#209, which types withSupabase with the engine's Composite. Nesting inside withOpenFeature without an anchor already fails on 5.4 to 5.8 with 1.0.0, so this doesn't make it worse. Third-party middleware written with the authoring guide's hand-written-signature variant still break; whether this ships anyway is the open decision in FUNC-1186.

Evidence

withA(pipeline([withB()], h))                            ctx.a missing (TS2339)        → ctx is exactly BaseContext & { a } & { b }
withA(pipeline([withNeedsA()], h))                       middleware-prereq: 'a'        → compiles, ctx exact
withA(pipeline([withA()], h))                            compiles                      → middleware-conflict
withAuth(cfg, pipeline([withX()], h))                    composite keys missing        → compiles
withFoo(withFoo(h)), no anchor                           compiles                      → middleware-conflict
withA(withB(withNeedsA(h))) satisfies FetchHandler, a provider of the wrong type two layers up   compiles   → middleware-prereq: incompatible type
  • typecheck (TS 5.9), typecheck:consumer, lint, Prettier, check-exports, the docs build and 154 vitest tests pass.
  • typecheck:min (TS 5.4) fails on one test, a middleware with a hand-written signature, by design. The repo typechecks with no other errors on TypeScript 5.4 to 5.8, and with none at all on 5.9.2, 5.9.3, 6.0.3 and 7.0.2.
  • Dependents against 1.0.0 on each TypeScript version from 5.4 to 7.0.2:
Dependent 5.4 5.5 – 5.8 5.9 – 7.0
@supabase/server: source and tests its 3 tests that nest a middleware inside withSupabase fail, as they do on 1.0.0 those 3 tests fail; they pass on 1.0.0 unchanged
@supabase/server: examples, e2e unchanged unchanged unchanged
@supabase-labs/middleware-openfeature unchanged unchanged unchanged
one internal dependent unchanged unchanged unchanged
  • deno check of a function nesting a middleware inside withSupabase, with @supabase/server sharing this build: fails on Deno 2.1.4 and 2.4.5, passes on 2.5.0 and 2.9.8. The same function bundles and serves in edge-runtime v1.79.5 (the image Supabase CLI v3.4.0 uses), with responses identical to 1.0.0: types only.
  • With withSupabase typed as a Composite, and with a plain Middleware outer, a nested middleware, a nested withPostgresClient and a pipeline as the handler argument all type-check on TypeScript 5.4 to 7.0.
  • Type-check cost, in instantiations at depth 16 against main: anchored nesting 3,647 → 4,234 (+16%), nesting without an anchor 9,616 → 5,901 (−39%), pipeline 11,602 → 14,413 (+24%), composite 10,699 → 12,095 (+13%). Check time stays under 0.03 s.

Merge Danger

Door: two-way

Types only, with no runtime change; reverting restores #56's types.

Blast Radius: types

  • Nesting a middleware inside a hand-written generic signature stops type-checking on TypeScript 5.5 to 5.8. withSupabase is covered once feat: type withSupabase with the engine's Composite server#209 is released; a project pinned to an earlier @supabase/server still breaks, as do third-party middleware that follow the guide's hand-written-signature variant. Whether this ships anyway is the open decision in FUNC-1186.
  • typecheck:min fails by design until then.
  • Code that compiled can stop compiling, where it was already wrong: a duplicate key in a nested stack without an anchor, or a prerequisite supplied at the wrong type two layers up.
  • Not changed: a nested stack inline in a host-typed export still reads env as upstream context (FUNC-1184).

Refs FUNC-1177

@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@supabase/middleware@55

commit: 6619f4f

…ntext

`Middleware` and `Composite` keep the enclosing keys on their handler parameter while their own upstream is still being inferred (`Upstream<Base>`). A `pipeline` written as their handler argument then sees those keys with no anchor, and nesting catches a duplicate key with no `satisfies FetchHandler`. The propagation overloads also check a prerequisite's type in the direction the optional-key constraint can't (`NoMismatch`), so a provider of the wrong type two layers up fails to compile.

With these handler types, nesting a middleware inside one with a hand-written generic signature fails to type-check before TypeScript 5.9. The test `a middleware with a hand-written signature` fails there by design, until `withSupabase` and `withOpenFeature` are typed with the engine's own types.

Refs FUNC-1177
The README, the authoring guide, the skill and the core README say that `ctx` accumulation, collision detection and a `pipeline` in a middleware's handler argument need no anchor, and that `satisfies FetchHandler` asserts the stack can be the `fetch` export.

Refs FUNC-1177
@johnstonmatt
johnstonmatt force-pushed the FUNC-1177/middleware/pipeline-handler-position branch from 56aded2 to 6619f4f Compare November 27, 2026 21:36
@johnstonmatt johnstonmatt changed the title feat: type a pipeline written where a handler is expected feat: type a pipeline in an unanchored stack against the enclosing context Nov 27, 2026
@johnstonmatt
johnstonmatt changed the base branch from main to FUNC-1185/middleware/pipeline-anchored-stack November 27, 2026 21:36

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.

1 participant