Skip to content

feat: add withTiming middleware - #71

Open
johnstonmatt wants to merge 2 commits into
mainfrom
feat/add-timing-middleware
Open

johnstonmatt wants to merge 2 commits into
mainfrom
feat/add-timing-middleware

Conversation

@johnstonmatt

Copy link
Copy Markdown
Collaborator

Summary

Adds withTiming, a third built-in, at @supabase/middleware/timing. It adds the time only the function can see to the response's Server-Timing header, alongside the entries platform hops add: the time spent in the middleware before the handler, and the time spent in named calls such as database queries.

pipeline([withTiming(), withAuth(), withDbClient()], async (_req, ctx) => {
  ctx.timing.mark()                                    // ends the `mw` entry
  const user = await ctx.timing.measure('db', () => …) // calls sharing a name are summed
  …
})
// Server-Timing: mw;dur=3.2, db;dur=8.4;desc="2 calls"
withTiming (first in the pipeline)
  start clock                        performance.now(), per request
  yield { timing: { mark, measure } }
    …other middleware…
    handler
      ctx.timing.mark()              pipeline entry = now − start (first call wins)
      ctx.timing.measure('db', fn)   sum duration + count per name, even when fn throws
  append entries to Server-Timing    pass through untouched when nothing was recorded
 src/middleware/
 ├── cors/
 ├── feature-flag/
+└── timing/
+    ├── README.md
+    ├── index.ts
+    ├── with-timing.ts
+    └── with-timing.test.ts

The subpath is wired into package.json, tsdown.config.ts, jsr.json and typedoc.json. The authoring guide's response-seam variant now walks through the real withTiming rather than a standalone sketch, and every list of built-ins names it. The repository still takes no new middleware; timing is one of its worked examples.

Evidence

  • Before: import { withTiming } from '@supabase/middleware/timing' doesn't resolve; the guide's withTiming only stamped a single total entry.
    After: pnpm test passes 135 tests, 11 of them new in with-timing.test.ts against a hand-driven clock:
    pipeline([withTiming(), slowSetup(3.25)], handler: mark; measure db 5ms; measure db 2ms; measure storage 1ms)
      → Server-Timing: mw;dur=3.3, db;dur=7.0;desc="2 calls", storage;dur=1.0;desc="1 call"
    handler sets `app;dur=1`            → app;dur=1, mw;dur=0.0
    nothing recorded                    → same Response object, no header
    measure fn throws                   → entry recorded, error rethrown
    name "two words" / "db;dur=0"       → TypeError
    Response.error()                    → passed through
    
    pnpm build && pnpm smoke loads all 8 entrypoints, including ok esm ./timing and ok cjs ./timing; pnpm check-exports, pnpm typecheck, pnpm lint and pnpm format:check are clean. The Deno, Bun and Workers runtime suites weren't run locally.

Merge Danger

Door: one-way

A new public subpath after 1.0. Once released, removing /timing or changing ctx.timing's shape is a breaking change. Merging alone is two-way: revert before the release-please PR ships it.

Blast Radius: additive

Nothing existing changes behavior. Consumers who don't import /timing load none of it: it's its own entry behind "sideEffects": false. The feat: commit makes the next release a minor.

Add `@supabase/middleware/timing`. `withTiming` contributes `ctx.timing`: `mark()` ends the entry for the middleware that ran before the handler, and `measure(name, fn)` times named calls such as database queries, summing calls that share a name. On the way out it appends the entries to `Server-Timing`, timed with `performance.now()`.
The authoring guide's response-seam variant now walks through the built-in `withTiming` rather than a standalone sketch, and every list of built-ins names timing alongside feature-flag and cors.
@johnstonmatt
johnstonmatt requested review from a team as code owners December 5, 2026 20:19
@pkg-pr-new

pkg-pr-new Bot commented Dec 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 1f6a98d

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