refactor: extract applyMiddlewareRequestHeaders into config-matchers#305
Conversation
|
/bigbonk review |
commit: |
|
There was a problem hiding this comment.
Review
Clean refactoring — the extraction is well-motivated and the shared function correctly handles both the Node.js prod-server and Workers deploy paths.
Failing test
The test at tests/deploy.test.ts:550-557 fails because it asserts the old inline implementation exists in the generated worker entry string:
it("preserves x-middleware-request-* headers for prod request override handling", () => {
const content = generatePagesRouterWorkerEntry();
// Worker entry must unpack x-middleware-request-* into the actual request
expect(content).toContain('const mwReqPrefix = "x-middleware-request-"');
expect(content).toContain('key.startsWith(mwReqPrefix)');
// Worker entry must also strip remaining x-middleware-* headers (defense-in-depth)
expect(content).toContain('key.startsWith("x-middleware-")');
});The generated entry now calls applyMiddlewareRequestHeaders(middlewareHeaders, request) instead of inlining the logic, so all three assertions fail. This test needs updating — e.g., assert the import and the function call instead:
expect(content).toContain("applyMiddlewareRequestHeaders");
expect(content).toContain("applyMiddlewareRequestHeaders(middlewareHeaders, request)");Subtle behavioral difference in prod-server (benign)
The old prod-server.ts code mutated webRequest.headers in-place:
webRequest.headers.set(realName, middlewareHeaders[key]);The new shared function always clones via new Request(...) when there are headers to apply. This is slightly more work for the Node.js prod-server path (where headers are mutable anyway), but it's the correct approach — it makes the behavior uniform across runtimes and avoids assumptions about header mutability. The const → let change on webRequest is the necessary counterpart. No behavioral issue here.
Everything else looks good
- The JSDoc is thorough and accurately describes mutation semantics and the return value
- The
requestContextFromRequestcall is correctly placed after the potential request clone - Both call sites (
deploy.tsandprod-server.ts) destructure and reassign identically - The generated worker entry correctly adds the import from
vinext/config/config-matchers - The
index.tsdev server (line ~2797) isn't deduplicated, which is fine — it operates on Node.jsIncomingMessage, not WebRequest, so it wouldn't cleanly use this shared function
|
The review has been posted. Here's the summary: The PR is a clean refactoring that correctly extracts duplicated One issue found: The test at There's also a benign behavioral difference — the old |
|
/bigbonk review |
There was a problem hiding this comment.
Review
Clean, well-scoped refactoring. All CI checks pass. Approving.
What it does
Extracts the duplicated x-middleware-request-* unpacking + x-middleware-* stripping logic from two inline call sites (deploy.ts and prod-server.ts) into a single applyMiddlewareRequestHeaders() in config-matchers.ts. Both call sites now destructure and reassign identically, and the test has been updated to assert the import + function call rather than the old inline implementation strings.
Notes
- The
const → letchange onwebRequestinprod-server.tsis correct — needed because the shared function returns a (possibly new)Requestrather than mutating in-place. - The JSDoc accurately documents the mutation semantics on
middlewareHeadersand the clone-on-write behavior for theRequest. - The two remaining inline copies (
index.ts:2797operating on NodeIncomingMessageandapp-dev-server.ts:1470using a differentapplyMiddlewareRequestHeadersthat operates on the RSC entry's generated code) aren't consolidatable with this shared function since they operate on different object types. The previous review correctly noted this. - The updated test at
tests/deploy.test.ts:550-557now asserts"applyMiddlewareRequestHeaders"and"vinext/config/config-matchers"which validates the generated worker entry imports and calls the shared function. This is a better test — it verifies the integration contract rather than brittle implementation strings.
|
Approved the PR. Here's the summary: Clean refactoring, all 24 CI checks pass. The PR correctly extracts duplicated Key observations:
|
|
Wait for 297 to merge, then rebase against and update func to handle those changes. |
Move the x-middleware-request-* unpacking + postMwReqCtx rebuild logic out of prod-server.ts and deploy.ts and into a shared applyMiddlewareRequestHeaders() in config-matchers.ts, eliminating ~30 lines of duplicated code from each callsite.
25df1a3 to
f10a781
Compare
|
/bigbonk review |
There was a problem hiding this comment.
Review
Clean, correct refactoring. All 24 CI checks pass (one E2E still pending but unrelated). Approving.
Summary
Extracts the duplicated x-middleware-request-* unpacking + x-middleware-* stripping logic from two inline sites (deploy.ts and prod-server.ts) into a shared applyMiddlewareRequestHeaders() in config-matchers.ts. Both call sites now destructure identically.
Observations
Correctness: The shared function faithfully reproduces the old inline logic. The two-pass approach (collect into toApply, then clone-if-needed) is equivalent to the old deploy.ts code and strictly better than the old prod-server.ts code, which mutated headers in-place — the uniform clone-on-write via new Request(...) avoids assumptions about header mutability across runtimes.
const → let in prod-server.ts:792: Correct and necessary since webRequest is now reassigned from the shared function's return value.
Test update (deploy.test.ts:582-589): The new assertions (applyMiddlewareRequestHeaders and vinext/config/config-matchers) verify the integration contract — that the generated worker entry imports and calls the shared function — rather than asserting on brittle inline implementation strings. This is an improvement.
Non-consolidated copies are correct to leave alone: The inline copies in index.ts (operates on Node IncomingMessage) and app-dev-server.ts (generated RSC entry code as a string template) operate on different object types and can't cleanly use this shared Web Request-based function.
Note from PR comments: Author mentions waiting for PR #297 to merge then rebasing. No issues with the code as-is — the rebase should be mechanical.
|
The review has been posted. Here's the summary: Approved. All CI checks pass. This is a clean, well-scoped refactoring that correctly extracts duplicated Key points:
Author notes a pending rebase against PR #297 — no issues with the code as-is. |
Moves the duplicated x-middleware-request-* unpacking logic from
prod-server.tsanddeploy.tsinto a sharedapplyMiddlewareRequestHeaders()inconfig-matchers.ts.