fix: honor prioRequestsProcessing default and stop sharing cached params#52
Merged
Merged
Conversation
Two correctness fixes, each validated by a regression test that fails on
the prior code:
1. prioRequestsProcessing default — the `true` default lived only in the
parameter default `(config = { prioRequestsProcessing: true })`, so any
`zero({ ...otherConfig })` call left the flag `undefined` (falsy) and
silently disabled it, contradicting the documented `default: true`.
Now resolved inside the body via `config.prioRequestsProcessing ?? true`.
2. Shared cached params — `router.lookup` caches trouter's full `match`
object (including `params`) in the LRU and assigned `req.params = params`
by reference. All requests to the same method+path shared one mutable
object, so a non-idempotent middleware mutation (e.g. `req.params.id += x`)
bled into later requests. Now shallow-copied on assignment.
Regression test: tests/regression-findings.test.js (fails before, passes after).
Full suite: 69 passing, coverage 97%.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two correctness bugs found during a code review of the router core, each validated by a regression test that fails on
masterand passes with the fix.#1 —
prioRequestsProcessingdefault silently disabled by any config (index.js)The documented
default: truelived only in the parameter default:That default only applies to the zero-arg call. Any real-world call that passes config —
zero({ router }),zero({ errorHandler }),zero({ server })— leavesconfig.prioRequestsProcessingasundefined(falsy), so the optimization is silently turned off, contradicting the docs/landing-page config table.The existing
prio-request-processing.test.jsnever caught this because all three cases pass the flag explicitly.Fix: resolve inside the body —
const prioRequestsProcessing = config.prioRequestsProcessing ?? true.#2 — Cached
paramsshared by reference across requests (lib/router/sequential.js)router.lookupcaches trouter's wholematchobject (including itsparams) in the LRU cache (on by default,cacheSize = -1). On a cache hit it assignedreq.params = paramsby reference, so every request to the samemethod+pathshared one mutable object. A non-idempotent middleware mutation bled into later requests.Demonstrated by the new test: a handler doing
req.params.id = req.params.id + '!'on route/item/:idreturnsx!on the first hit butx!!on the second (shared object) before the fix.Fix: shallow-copy on assignment —
req.params = params ? { ...params } : Object.create(null). The pre-existingelse if (params)branch already copied into an existingreq.params, so only the direct-assignment path needed the change.Validation
tests/regression-findings.test.js— both tests fail onmaster(expected undefined to equal true;expected 'x!!' to equal 'x!') and pass with the fix.standardlint clean.🤖 Generated with Claude Code