Repository navigation
Add req.originalRoute, the full matched route pattern (#2501) - #73
Merged
Merged
Conversation
req.originalRoute is the pattern of every mount the route is under, then
the route's own path: '/api/users/:id' for router.get('/users/:id') under
app.use('/api', router). The router records each mount it enters and each
route it matches on the request; the string is built only when read.
Mount patterns are joined as req.baseUrl joins paths, a path array gives
the entry that matched, a RegExp route or mount gives undefined, and the
value is kept as long as req.route is. The getter is not enumerable, so
the request prototype keeps the keys of Express 5.3.0.
Layer gains rawPath (router 2.3.0's line from pillarjs/router#174) and
rawMatchers; matching itself is unchanged.
# Conflicts: # docs/fork/differences.md
Coverage Report for CI Build 45032051789Coverage remained the same at 100.0%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
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
Adds
req.originalRoute(expressjs#2501): the full path pattern of the route that matched, the pattern of every router and sub-app mount it is under followed by its own path. Forapp.use('/api', router)androuter.get('/users/:id'),GET /api/users/7gives'/api/users/:id'. It is built when read, from records the router already has in hand while routing.Changes
src/router/index.ts: whentrimPrefixenters a mount that is not a/mount, it stores{ layer, path, noSlash, parent }inreq[mountMatch]and puts the parent back when the request comes back tonext(). Wherenext()setsreq.route, it also setsreq[routeMatch]to the same kind of record, with the current mount as its parent.src/router/lib/layer.ts:rawPath(router 2.3.0's line from feat: add listRoutes method to list registered routes pillarjs/router#174: the path as registered, arrays flattened) andrawMatchers(one matcher perrawPathentry; the same array asmatchersunless the path nests arrays). Matching still usesmatchers.src/router/lib/symbols.ts(new; 2.3.0 has a file of this name for its ownmatchedRoutesymbol) andsrc/router/lib/original-route.ts(new), which walks the records and joins the patterns.src/express.ts: the getter on the request prototype, not enumerable, soObject.keys(express.request)still matches Express 5.3.0 (test/exports.js).readonly originalRoute?: string | undefined), type tests,docs/api/request.md,docs/fork/differences.md, History, VENDORED and the ROADMAP row.Behaviour
req.originalRoute'/api/v1/users/:id'req.baseUrljoins paths; a mount used without a path adds nothing/under/api'/api/'app.get)match.pattern(pillarjs/router#207)undefinedString(regexp)joined with paths is neither a path nor a RegExp, and the matched text would put IDs back into metric names.req.route.pathstill has itapp.usemiddleware, before any routeundefinedapp.use(path, subApp)mounts through the sametrimPrefixrouter.route()/app.route(),router.param()callbacks of a routerouter.get()undefinedif the error came before any routereq.routeundefinedif no route matched; else the last route that passed the request onreq.route, so the two never disagreereq.routereplaced by user codeundefinedreq.routeres.on('finish'))Name. Upstream discussion never settled a name: pillarjs/router#34 and pillarjs/router#86 used
req.matchedRoutes(an array of every matched path, built on every request, never finished), withmatchedPaths/matchedPathnamesfloated in review. KeptoriginalRoutefrom the roadmap: it is toreq.routewhatreq.originalUrlis toreq.url.Rejected alternatives. Re-matching the app's router tree at read time (ambiguous with routers mounted twice or falling through to siblings, and breaks on rewritten
req.url). Building the string or an array per request (the cost the issue's earlier PRs carried). AWeakMapper request (costs more than a property store). Porting 2.3.0'smatchers = rawPath.map(matcher)(would make a nested array holding a RegExp stop throwing at registration, which changes existing behaviour; the re-sync brings it).Proof
npm run lint,npm run typecheck,npm run test:types: clean.npm run test-ci: 146 files, 8694 passed, 5 skipped; 100% statements, branches, functions and lines; coverage-files check passes.test/req.originalRoute.js. Planted breaks: removing the route record fails 30 of them, removing the mount record fails 21, removing the restore of the parent mount fails 3.node scripts/ecosystem-suites.js): every test that passes on Express 5.3.0 passes here (cors 49, express-session 160, multer 173, express-rate-limit 252, http-proxy-middleware 325, express-basic-auth 25, express-openapi-validator 468, express-fileupload 203, express-mongo-sanitize 20, hpp 17).allowedentry.Benchmark
Local instruction count,
node benchmarks/count/run.ts --counter valgrind --runs 1 --jobs 2 --against origin/master --report-only:All under the +0.3% goal. The route scenarios pay about 70 to 210 instructions per request for one small record and one symbol-keyed store per matched route; middleware pays one
layer.slashcheck, andnext()one closure check.not-found(no route matched) has nothing new to do; its -1.78% is not explained by this change and is worth re-measuring.Risks / follow-ups
util.inspect(req)now lists the two symbol-keyed records (2.3.0'sreq[matchedRoute]does the same), andconsole.dir(req.route)showsrawPathandrawMatcherson each layer (thereq.routedocs example is updated).src/express.tsbecausesrc/request.tsis being changed in a parallel PR; it can move there later.differences.mdget a new "Additions" heading, as Add the "view engines" setting: try several engines for a view name without a known extension #72 does; whichever merges second should fold the two into one.layer.match()returnsmatch.pattern, which can go into the records and replace the re-matching inoriginal-route.ts;rawMatchersthen goes away (see VENDORED.md).