Repository navigation
refactor(proxy): table-driven route forwarding - #7
Merged
Merged
Conversation
Comment on lines
+327
to
+336
| handler: async (request, reply) => { | ||
| if (!authGuard[route.auth](request, reply)) { | ||
| return; | ||
| } | ||
|
|
||
| const base = baseUrl[route.upstream](); | ||
| const query = applyQueryPolicy(route.query, (request.query as Record<string, unknown>) ?? {}); | ||
| const url = buildUrl(base, fillPathParams(route.upstreamPath, request.params), query); | ||
| await forwardRequest(request, reply, url, route.method, timeoutMs[route.timeout], getBreakerForUrl(base)); | ||
| }, |
Comment on lines
+348
to
+361
| app.get(path, async (request, reply) => { | ||
| if (!authGuard[auth](request, reply)) { | ||
| return; | ||
| } | ||
|
|
||
| const parsedQuery = schema.safeParse(request.query ?? {}); | ||
| if (!parsedQuery.success) { | ||
| return sendValidationError(reply, parsedQuery.error); | ||
| } | ||
|
|
||
| const base = baseUrl[upstream](); | ||
| const url = buildUrl(base, path, query(parsedQuery.data)); | ||
| await forwardRequest(request, reply, url, "GET", upstreamTimeoutMs, getBreakerForUrl(base), "json"); | ||
| }); |
Sebas1705
force-pushed
the
perf/phase2-raw-proxy
branch
from
November 12, 2026 17:53
56ec2ad to
66ec41a
Compare
Fixture extracted mechanically from main's hand-written proxy.ts (62 routes, incl. fail-closed edge auth and the /internal token rules). Also excludes dist/** from vitest.
Replace the 59 near-identical forward handlers with routeTable.ts plus one generic handler. Auth kinds: none, edge (fail-closed) and internal (x-internal-token). Admin routes and the validated query routes stay explicit.
Sebas1705
force-pushed
the
refactor/gateway-route-table
branch
from
November 12, 2026 18:41
633fec7 to
c6a5f90
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
What
routes/proxy.tsregistered 62 routes by hand: 59 near-identicalbuildUrl -> forwardRequestwrappers. This replaces the 56 plain forwards with a declarative table (routes/routeTable.ts, data only) and one generic handler. Adding a BFF route is now one table line.Kept explicit, unchanged in behaviour:
GET /v1/mobile/games/{quiz,wordpass}/random,GET /v1/backoffice/users/leaderboard), still forwarded injsonmode;/internal/admin/ai-engine/targetroutes (admin token viaisGatewayAdminAuthorized, body validation,applyAiEngineTargetwith the protocol and routing-allowlist checks, runtime target store);forwardRequest, the raw JSON body parser), CORS, rate limiting, circuit breakers, and the runtime-switchable ai-engine target (base URL still resolved per request).No external contract change: same paths, methods, status codes, auth, query handling, timeouts.
Rework on top of current main (#3 and #6 merged)
The PR was first written before #3 (auth fail-closed,
/internal/*token,TRUST_PROXY, routing allowlist) and #6 landed. It is now rebuilt fromorigin/main(two commits, base ismain) and carries main's security behaviour:checkEdgeAuth(request, reply, config)keeps main's fail-closed behaviour (503 whenEDGE_API_TOKENis unset outside development/test, 401 on a bad bearer, constant-time compare). Theedgeentry ofauthGuardcalls it."internal"auth kind in theAuthtype, guarded bycheckInternalAuth(x-internal-token, 503 whenINTERNAL_SERVICE_TOKENis unset outside development/test, 401 on mismatch). The two/internal/ai-enginesections of the table (7 routes) moved fromnonetointernal, replacing the 7 per-routecheckInternalAuthblocks; effective protection is identical.isGatewayAdminAuthorized,applyAiEngineTarget,security.jsimport) are main's code, untouched, and are not in the table.proxy.tswith a script (not by hand). Compared with the pre-rework fixture, the only differences areauth: none -> internalon the 7/internal/ai-engine/*routes.How equivalence is checked
Commit 1 adds
tests/fixtures/gatewayRoutes.json(method, path, upstream, upstream path, query policy, timeout, forward mode, auth, validated; 62 routes: 3 admin, 3 validated, 56 plain; 44 edge, 7 internal, 8 none) andtests/proxyRouteEquivalence.test.ts, and passes against main's hand-writtenproxy.ts. Commit 2 is the refactor and leaves the test untouched. For every fixture entry the test asserts:NODE_ENV=production; never forwarded in either case;x-internal-token, and for the edge bearer alone; 503 whenINTERNAL_SERVICE_TOKENis unset in production; never forwarded;noneroutes: reachable without credentials, also in production with no tokens;encodeURIComponentre-encoding of path params, tested witha%2Fb), method, timeout, forward mode (raw vs json) and query handling match;Mutation check: flipping one internal section back to
nonemakes 12 tests fail. The existingproxy.test.ts,proxyRaw.test.ts,authFailClosed.test.tspass untouched.Size
proxy.ts953 -> 400 LOC, plusrouteTable.ts140 LOC (540 total, -413). The equivalence test (359 LOC) and fixture are not counted.Also:
vitest.config.tsnow excludesdist/**.tscemits compiled tests there and vitest was collecting them (running every test twice after a build; the new test's fixture does not exist indist).Verification
Run in
src/:npm ci,npm run build,npm run lint,npm run test:coverageall pass locally. Vitest: 12 files, 301 tests, green. Coverage: statements 98.33%, branches 91.57%, functions 97.87%, lines 98.3% (thresholds 90%).Not verified: no run against live BFFs or the real docker-compose stack, and no load or latency comparison (the table adds one object lookup per request). Behaviour equivalence rests on the fixture-based test.