Repository navigation
fix(express): resolve mount prefixes for routers in other files - #22
Merged
Merged
Conversation
…ile-mounts # Conflicts: # CHANGELOG.md
Contributor
Author
Second-model review
Verdict: approve. In plain EnglishIn most Express projects, routes are written in one file and attached under a path such as |
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.
Closes #8
What changed
lib/parsers/express-modules.mjs(pure, no AST): resolves relative specifiers (./x.js,./x,./dirtodir/index.*), composes mount chains across files (nesting, cycles, at most 64 prefixes, all or nothing) and rewrites the endpoints.joinPathmoved here fromexpress.mjs.lib/parsers/express.mjs: per file it also records exports (ESMexport, CommonJSmodule.exportsandexports.x), imports, andX.use([prefix, ...middleware,] importedRouter)mounts, where the last argument is the router. It reuses the router recognition from Express: any router name, route() chains, router-level auth #7 (analyzeBindings,isStableRouterBinding,isPathlessUse,collectUseMarkers) and the shared candidate file list: no new directory walk, no new dependency. After the file loop, routes get their full prefix, and the mount middleware becomes their auth markers.express()app down to it is known and static. Otherwise it getsresolved: false,path: nullandunresolvedReason: "mount-prefix-cross-file". That covers an exportedRouter()that no resolved mount reaches (alias or package imports, mounts in.tsfiles, a scan of a sub-directory, factory exports, cycles), a router mounted both in its own file and from another file, a router nested on another local router inside a chain, and a router on an app that is itself mounted. Anexpress()app that nothing mounts is a root. A dynamic prefix keeps the existing reasonmount-prefix-not-static.lib/utils.mjssortEndpointssortsnullpaths last. The old comparator was not a total order oncenulland string paths mixed, so reversed input gave different output.lib/capabilities.mjs:crossFileMountPrefix: true, a newcrossFileMountsdescription and the reasonmount-prefix-cross-file. README (Express row, capabilities example, limits) andCHANGELOG.md[Unreleased].test/fixtures/express-multifile/(the issue's reproduction plus admin, nested, cycle, alias, factory, dynamic, sub-directory and same-file cases), new cases intest/parser-express.mjs, a reversed-order case intest/determinism.mjs, andtest/capabilities.mjs.test/fixtures/express-router-negatives/conditional-use.jsandunless.js(from Express: any router name, route() chains, router-level auth #7) exported their routers only as a leftover. Under the new rule those unmounted exported routers became unresolved, and the tests stopped checking what they exist for: the auth markers of a path-lessuse(). The two export lines are removed, and the marker results are unchanged.mainis merged into the branch twice (after docs: remove links to private Stelnyx repositories #19 and chore(deps): clear audit advisories, bump ESLint 10, drop Node 18 #20, then after feat(nest): detect global guards and setGlobalPrefix #21). Only the[Unreleased]section ofCHANGELOG.mdoverlapped.Proof per acceptance criterion
express-multifile:routes/users.jsyieldsGET /api/usersandPOST /api/users, both resolved. Onmain, the same files give bareGET /andPOST /, marked resolved.app.use("/admin", requireAuth, adminRouter)across files:routes/admin.jsyieldsDELETE /admin/wipeGUARDED (markerrequireAuth).@/routes/ghost, the factory export inroutes/legacy.jsand the cycle incycle/a.jsandcycle/b.jsall yieldpath: null,resolved: falseandunresolvedReason: "mount-prefix-cross-file". The dynamic prefix inroutes/dyn.jsyieldsmount-prefix-not-static.test/determinism.mjspasses, and a new case that feeds the candidate files in reversed order gets identical output.npm testpasses. No golden number moved:test/golden-apigate.mjspasses unchanged, because the sample app has no exported router that the new rule touches.samefile6andsamefile7giveGET /r/x/zresolved without the last guard, andGET null mount-prefix-cross-filewith it.Gate
npm ci && npm run lint && npm testat 29c2cc9, after mergingmain(so with ESLint 10):N passed, 0 failed.Not done (documented in README and
parserCapabilities)this.appis treated as a root.requireand router factories are not followed (the last three are out of scope in the issue). TypeScript files go through the regex extractor and are outside the cross-file rule.express()apps in the same file reports only the last app's prefix. This same-file rule predates this change, and the reported path is one that Express really serves.mount-prefix-cross-fileinstead ofmount-prefix-not-static. They are unresolved either way.app.use(requireAuth, router)) makes the prefix dynamic:mount-prefix-not-static.Suggested labels: bug