refactor(request): align jsdocs with getRequestHost#1316
Conversation
📝 WalkthroughWalkthroughgetRequestURL now parses the x-forwarded-host header directly (uses first comma-separated value, clears port if absent). getRequestHost docstring now states it returns an empty string when no Host header exists. getRequestHost was exported and covered by new tests. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/utils/request.ts (1)
388-397: Intentional divergence fromgetRequestHostis correct.The direct header parsing here (duplicating lines 331-332) is the right approach. Using
getRequestHostwould incorrectly apply the"localhost"fallback, potentially overwriting a valid host already present in the request URL. This ensuresurl.hostis only modified when an actualx-forwarded-hostheader exists.The port clearing logic on lines 394-396 correctly handles cases where the forwarded host doesn't include a port.
If desired, the shared parsing logic could be extracted to a private helper to reduce duplication:
♻️ Optional: Extract parsing helper
+function _parseFirstForwardedHost(header: string | null): string | undefined { + return (header || "").split(",").shift()?.trim() || undefined; +} + export function getRequestHost(event: HTTPEvent, opts: { xForwardedHost?: boolean } = {}): string { if (opts.xForwardedHost) { - const _header = event.req.headers.get("x-forwarded-host"); - const xForwardedHost = (_header || "").split(",").shift()?.trim(); + const xForwardedHost = _parseFirstForwardedHost(event.req.headers.get("x-forwarded-host")); if (xForwardedHost) { return xForwardedHost; } } return event.req.headers.get("host") || "localhost"; }This is minor and optional—the current duplication is just 4 lines and the intent is clear.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/utils/request.ts` around lines 388 - 397, The direct parsing of the x-forwarded-host header in this block is correct: ensure you only set url.host when xForwardedHost (derived from event.req.headers.get("x-forwarded-host")) is truthy and clear url.port when xForwardedHost does not include ":" (preserve the existing url.host/url.port assignments), and keep the current behavior rather than calling getRequestHost (which applies a "localhost" fallback). Optionally, if you want to remove the small duplication, extract the header-splitting/trimming logic into a private helper (e.g., parseForwardedHost) and call it from both this block and getRequestHost, but this is not required.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/utils/request.ts`:
- Around line 388-397: The direct parsing of the x-forwarded-host header in this
block is correct: ensure you only set url.host when xForwardedHost (derived from
event.req.headers.get("x-forwarded-host")) is truthy and clear url.port when
xForwardedHost does not include ":" (preserve the existing url.host/url.port
assignments), and keep the current behavior rather than calling getRequestHost
(which applies a "localhost" fallback). Optionally, if you want to remove the
small duplication, extract the header-splitting/trimming logic into a private
helper (e.g., parseForwardedHost) and call it from both this block and
getRequestHost, but this is not required.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9e1d9f67-3412-4e35-876f-e1b96bac6cae
📒 Files selected for processing (2)
src/utils/request.tstest/utils.test.ts
| } | ||
| } | ||
| return event.req.headers.get("host") || ""; | ||
| return event.req.headers.get("host") || "localhost"; |
There was a problem hiding this comment.
I think we should fix jsdocs instead, and do not fallback to localhost (it can lead to wrong asusmptions in code logic)
- `getRequestHost` returned empty string when no host header was found, but the JSDoc documented "localhost" as the default. Now returns "localhost" to match the documented behavior. - Updated `getRequestURL` to only override the URL host when an actual `x-forwarded-host` header is present, preventing the "localhost" fallback from incorrectly replacing a valid host in the request URL. - Added tests for `getRequestHost` covering default, x-forwarded-host, and multi-value scenarios. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Per maintainer review: returning "localhost" as fallback can lead to wrong assumptions in code logic. Keep the empty string return and fix the JSDoc to match the actual behavior. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
f204f92 to
1419122
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/utils/request.ts`:
- Around line 388-395: The check that clears url.port uses
xForwardedHost.includes(":") which misclassifies bracketed IPv6 literals (e.g.
"[2001:db8::1]") as having a port; change the logic to detect an explicit port
instead (e.g. test for a trailing :port with a regex like /:\d+$/) and only keep
url.port when that test matches; update the block that reads xForwardedHost and
sets url.host/url.port (references: xForwardedHost, url.host, url.port) so
bracketed IPv6 hosts without a port correctly clear url.port.
In `@test/utils.test.ts`:
- Around line 88-93: The test currently only asserts truthiness of
getRequestHost result which misses regressions for the no-Host case; add an
explicit assertion that simulates a request with no Host header (e.g., await
t.fetch("/", { headers: {} }) or explicitly remove the Host header) and assert
the response equals the known fallback value used by getRequestHost (import or
reference the fallback constant/behavior from getRequestHost) so the test
verifies both the normal client-set Host and the explicit no-host fallback.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1cb80c88-aa2f-4e94-b846-cb20e0ba1e39
📒 Files selected for processing (3)
docs/2.utils/1.request.mdsrc/utils/request.tstest/utils.test.ts
Per maintainer review: revert the x-forwarded-host inline parsing back to using getRequestHost(event, opts) as before. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
getRequestHost
Summary
getRequestHost()returned""when no host header was found, but its JSDoc documented"localhost"as the default — now returns"localhost"to matchgetRequestURL()to only override the URL host when an actualx-forwarded-hostheader is present, preventing the"localhost"fallback from incorrectly replacing a valid host in the request URLgetRequestHostcovering default value,x-forwarded-host, and multi-value scenariosTest plan
getRequestHosttests pass in both web and node modes🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests
Documentation