ReviewOS

stacks/bun-router

fix(router): scope static responses by HTTP method

#903
Closed chris opened this 10 days ago ·
10 days ago

Problem

A prebuilt Response is documented and typed as an ActionHandler, but bun-router records it in staticResponses: Map<string, Response>. The HTTP method is discarded.

On Bun 1.4.2, the Bun.serve({ static }) table serves a path for every HTTP method. This means:

router.post('/ready', new Response('posted'))

also answers GET /ready, and registering two static responses for the same path lets the later method overwrite the earlier one. The registration also overwrites duplicate method/path entries even though the fetch router uses first registration wins.

The static table is not rebuilt by router.reload(), so prebuilt responses can disappear after reload.

Bun 1.4.2 evidence

A direct Bun probe returned the static response for both GET and POST. A method-scoped native route preserves the intended contract:

routes: {
  '/ready': { POST: new Response('posted') },
}

An absent method falls through to fetch, which lets bun-router retain its existing 404, 405, middleware, and fallback semantics. GET continues to answer HEAD automatically.

Proposed fix

  • Retain prebuilt responses by path and HTTP method.
  • Merge them into Bun's method-scoped routes table on serve() and reload().
  • Merge compatible dynamic native routes into the same table when nativeRoutes: true.
  • Preserve first registration wins for each method/path pair.
  • Add Bun 1.4.2 HTTP coverage for method isolation, multiple methods on one path, duplicate registration, fetch fallback, and reload.
  • Keep prebuilt responses on Bun's zero-allocation path without invoking middleware or request context.
10 days ago

Implemented on main by c3baec0.

The router now retains each prebuilt response by path and HTTP method, merges it into Bun's method-scoped native routes table, preserves first registration, combines static and dynamic native methods, and rebuilds the static table on reload.

Validated with the exact Bun 1.4.2 release:

  • full router suite: 570 passed, 0 failed
  • TypeScript checks: passed
  • package build: passed
  • touched-file pickier lint: passed

The new HTTP coverage verifies method isolation, multiple static methods on one path, static/dynamic duplicate order, native route merging, and reload behavior.

10 days ago

Hosted CI is green for c3baec09d48d7ea6f92b26c75f5501fdcb375801: https://github.com/stacksjs/bun-router/actions/runs/35574869684

Lint, type checks, all tests, build, and the commit package publication job passed on the exact pushed commit. Closing as completed.

Sign in to comment on this issue.