Skip to content

feat: type a pipeline in an anchored stack against the enclosing context - #56

Draft
johnstonmatt wants to merge 4 commits into
mainfrom
FUNC-1185/middleware/pipeline-anchored-stack
Draft

johnstonmatt wants to merge 4 commits into
mainfrom
FUNC-1185/middleware/pipeline-anchored-stack

Conversation

@johnstonmatt

Copy link
Copy Markdown
Collaborator

Summary

A pipeline written where a handler is expected takes its upstream from that position, so its handler sees the keys of the layers around it and its entries are checked against them. In a middleware's or composite's handler argument that needs the stack anchored: satisfies FetchHandler, a typed declaration, or a host signature such as Deno.serve.

export default {
  fetch: withA(
    pipeline([withNeedsA(), withB()], async (_req, ctx) => {
      ctx.a // main: Property 'a' does not exist · here: string
      ctx.b
    }),
  ),
} satisfies { fetch: FetchHandler }
 pipeline(entries, handler)
-  checks entries against BaseContext, returns FetchHandler
+  checks entries against the context it is placed in, returns (req, ctx?: Base) => …
+  another pipeline's handler argument, a position typed as a handler: always
+  a middleware's or composite's handler argument: once the outermost call is anchored
+  a host-typed export (Workers ExportedHandler, Deno.serve): keeps BaseContext
 ValidateEntries
-  checks prerequisites by key name
+  checks prerequisites by key name and type
 defineComposite
-  Config inferred as `unknown` when build takes no parameter
+  Config defaults to `void`, like a config-less middleware
 Middleware, Composite
   unchanged

Middleware and Composite keep their 1.0.0 types, so nesting a middleware inside one with a hand-written signature, such as withSupabase, types as before on every TypeScript version. A new test pins that. An unanchored stack such as export default { fetch: withA(pipeline(…)) } still starts the pipeline from the empty upstream; FUNC-1177 covers it.

Evidence

  • Before (main) / After (here), from the new type tests in pipeline.test.ts and define-composite.test.ts:
withA(pipeline([withB()], h)) satisfies FetchHandler            ctx.a missing (TS2339)    → ctx is exactly BaseContext & { a } & { b }
withA(pipeline([withNeedsA()], h)) satisfies FetchHandler       middleware-prereq: 'a'    → compiles, ctx exact
const h: (req, ctx: BaseContext & { a }) => … = pipeline(…)     ctx.a missing             → compiles, ctx exact
withA(pipeline([withB()], pipeline([withC()], h))) satisfies …  ctx.a, ctx.b missing      → compiles, ctx exact
withAuth(cfg, pipeline([withX()], h)) satisfies FetchHandler    composite keys missing    → compiles
withA(pipeline([withA()], h)) satisfies FetchHandler            compiles                  → middleware-conflict
pipeline([withA()], pipeline([withB(), withA()], h))            compiles                  → middleware-conflict
pipeline([withAuthAsNumber(), withNeedsAuthAsString()], h)      compiles                  → middleware-prereq: incompatible type
call nested in a config-less composite                          ctx.a missing on TS 5.9   → compiles
  • typecheck, typecheck:min (TS 5.4), typecheck:consumer, lint, Prettier, check-exports, the docs build, 144 vitest tests, and the Deno, Bun and Workers suites pass.
  • The repo typechecks with no errors on TypeScript 5.4, 5.5.4, 5.6.2, 5.6.3, 5.7.3, 5.8.3, 5.9.2, 5.9.3, 6.0.3 and 7.0.2.
  • Dependents, typechecked against 1.0.0 and against this branch on TypeScript 5.4 to 7.0: no added errors in @supabase/server (source, tests, examples, e2e), @supabase-labs/middleware-openfeature, or one internal dependent.
  • deno check on Deno 2.1.4 (TS 5.6.2), 2.4.5, 2.5.0 and 2.9.8, with @supabase/server sharing this build: the server README's handlers, and a middleware nested inside withSupabase and in a pipeline with it, give the same results as 1.0.0. Edge Functions run on edge-runtime, which is built on Deno 2.1.4.

Merge Danger

Door: two-way

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

Blast Radius: types

  • Code that compiled can stop compiling, where it was already wrong: a pipeline entry's prerequisite supplied at the wrong type, or an entry repeating a key from an enclosing pipeline or an anchored enclosing middleware.
  • A pipeline written directly in a host-typed export (satisfies ExportedHandler<Env>, Deno.serve(…)) returns Promise<Response> | Response, so .then on a direct call to it no longer compiles.
  • A composite whose build takes no config rejects a config argument.
  • A top-level pipeline hovers as (req: Request, ctx?: object) => Promise<Response> rather than FetchHandler, which is the same type.
  • Releases as 1.1.0 (feat).

Refs FUNC-1185

`ValidateEntries` checked an entry's prerequisites by key name only, so `pipeline([withAuthAsNumber(), withNeedsAuthAsString()], handler)` compiled. It now also checks each prerequisite's type against the context accumulated so far, and reports `middleware-prereq: key '…' is on the upstream context with an incompatible type` on the handler argument.

Refs FUNC-1185
…middleware

With no `build` parameter, `defineComposite` inferred `Config` as `unknown`, which collapsed the expected type of the composite's handler argument to `unknown`. On TypeScript 5.9, a middleware call nested there lost the composite's keys. `Config` now defaults to `void`, as it does for a config-less middleware, so passing a config value to such a composite is also a type error.

Refs FUNC-1185
A `pipeline` written where a handler is expected now takes its upstream from that position, so its handler sees the keys of the layers around it and its entries are checked against them: a key they already carry is a `middleware-conflict`, and a key they supply meets a prerequisite.

That holds in another `pipeline`'s handler argument and in any position typed as a handler. In a middleware's or composite's handler argument it holds once the outermost call is anchored: `satisfies FetchHandler`, a typed declaration, or a host signature such as `Deno.serve`. Unanchored, the pipeline there starts from the empty upstream, as before. A `pipeline` written directly in a host-typed export (`Deno.serve`, a Workers `ExportedHandler`) also keeps the empty upstream, because the host's second parameter is a platform value, not a context.

`Middleware` and `Composite` are unchanged, so nesting a middleware inside one with a hand-written signature types as before on every TypeScript version. A new test pins that.

Refs FUNC-1185
The README, the authoring guide, the skill and the core README say that a `pipeline` can be a middleware's handler argument and sees the keys around it once the outermost call is anchored, and that a middleware with a hand-written type signature, such as `withSupabase`, goes in the `pipeline` array instead. The `pipeline` API row describes what it returns in a handler position.

Refs FUNC-1185
@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@56

commit: feac097

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