Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@amondnet
Copy link
Contributor

@amondnet amondnet commented Nov 28, 2025

Summary

This PR fixes #1246 where proxyRequest does not work correctly with Bun runtime behind reverse proxies (Caddy, NGINX), resulting in 502 errors.

Problem

When using proxyRequest with Bun behind a reverse proxy:

  1. H3 successfully fetches the upstream response
  2. The reverse proxy returns 502 Bad Gateway to the client
  3. Direct access without the reverse proxy works correctly
  4. Node.js runtime does not exhibit this issue

Root Cause

The proxy() function was not filtering out the transfer-encoding header from the upstream response. When the upstream returns transfer-encoding: chunked, this header was being passed through to the final response.

Since the Response object handles its own chunked encoding when streaming, passing through the upstream's transfer-encoding header creates conflicts. This is especially problematic with Bun's server implementation and reverse proxies.

Solution

Added transfer-encoding to the list of headers to filter out in proxy(), consistent with the existing approach for content-encoding and content-length headers.

Test Plan

  • All existing proxy tests pass (38 tests)
  • Full test suite passes (725 tests)
  • Added test case for verifying hop-by-hop headers handling

Related

Summary by CodeRabbit

  • Bug Fixes

    • Proxy now omits the transfer-encoding header when forwarding responses, improving header consistency and downstream handling.
  • Tests

    • Added tests to ensure custom response headers are preserved, transfer-encoding is stripped, and response bodies are passed through correctly.

✏️ Tip: You can customize this high-level summary in your review settings.

@amondnet amondnet requested a review from pi0 as a code owner November 28, 2025 07:20
@coderabbitai
Copy link

coderabbitai bot commented Nov 28, 2025

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Response header handling in the proxy was changed to also omit the transfer-encoding header when forwarding upstream responses. Two tests were added: one ensures custom headers are preserved; another (web target only) verifies transfer-encoding is stripped while other headers and body remain.

Changes

Cohort / File(s) Summary
Proxy utility header filtering
src/utils/proxy.ts
Adjusted response header iteration to ignore transfer-encoding in addition to content-encoding and content-length, so transfer-encoding is not propagated to proxied responses.
Proxy tests
test/proxy.test.ts
Added two tests: one verifies preservation of custom headers and content-type with proxied body; another (scoped to the web target) asserts transfer-encoding is stripped while other headers and body are preserved.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Small, localized logic change in a single utility and straightforward test additions.
  • Areas to spot-check:
    • Ensuring behavior is correct across runtimes/targets (web vs. node).
    • Confirm no regressions for streaming/chunked responses that rely on transfer-encoding.

Poem

🐇 I hopped through headers, soft and quick,
Snipped the hops that made things stick.
Kept your custom notes intact,
Left the body, tidy, packed.
Tiny paws, a careful fix — hop! 🥕

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change—adding transfer-encoding to filtered headers in the proxy function to fix Bun compatibility.
Linked Issues check ✅ Passed The PR correctly addresses issue #1246 by filtering transfer-encoding headers, which resolves the root cause of 502 errors in Bun runtime behind reverse proxies.
Out of Scope Changes check ✅ Passed All changes are in-scope: proxy.ts modification filters transfer-encoding headers, and test additions verify the fix and header preservation behavior.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3b87cc7 and de86dbb.

📒 Files selected for processing (1)
  • src/utils/proxy.ts (1 hunks)

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
test/proxy.test.ts (1)

171-191: Test name vs. assertions – consider tightening or clarifying

The new test is useful as a smoke test (custom header preserved, body proxied), but it doesn’t currently assert that any hop‑by‑hop header (e.g. transfer-encoding) is actually stripped, despite the test name.

If the test harness makes it feasible across runtimes, consider also asserting that transfer-encoding is absent from the proxied response (by setting it on the upstream and checking res.headers.has("transfer-encoding") === false), or alternatively tweak the test description to better reflect what is being asserted.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d6475e1 and 7dd54fa.

📒 Files selected for processing (2)
  • src/utils/proxy.ts (1 hunks)
  • test/proxy.test.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
test/proxy.test.ts (1)
src/utils/proxy.ts (1)
  • proxyRequest (27-61)
🔇 Additional comments (1)
src/utils/proxy.ts (1)

90-105: Stripping transfer-encoding here is correct and consistent

Skipping transfer-encoding alongside content-encoding and content-length aligns with HTTP hop‑by‑hop semantics and avoids double chunking when re-wrapping the response. The change is minimal, scoped, and looks correct.

When proxying requests, the transfer-encoding header from the upstream
response was being passed through to the final response. This caused
issues with Bun runtime behind reverse proxies (Caddy, NGINX) where
the 502 errors occurred because of conflicting transfer-encoding handling.

The Response object handles its own chunked encoding when streaming,
so passing through the upstream's transfer-encoding header creates
conflicts. This is consistent with the existing approach for
content-encoding and content-length headers.

Fixes h3js#1246
@amondnet amondnet force-pushed the fix/proxy-transfer-encoding-1246 branch from 7dd54fa to 3b87cc7 Compare November 28, 2025 07:25
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
test/proxy.test.ts (1)

192-217: LGTM! Test correctly validates the transfer-encoding fix.

The test appropriately verifies that the transfer-encoding header is stripped from proxied responses while preserving other headers and the response body. The web-only restriction is well-justified given the runtime-specific nature of the bug.

Optional consideration: If the proxy implementation filters other hop-by-hop headers beyond transfer-encoding (e.g., Connection, Keep-Alive, TE, Trailers), you might consider adding test cases for those headers to ensure comprehensive coverage of the filtering logic.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7dd54fa and 3b87cc7.

📒 Files selected for processing (2)
  • src/utils/proxy.ts (1 hunks)
  • test/proxy.test.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/utils/proxy.ts
🧰 Additional context used
🧬 Code graph analysis (1)
test/proxy.test.ts (2)
src/utils/proxy.ts (1)
  • proxyRequest (27-61)
src/event.ts (1)
  • res (78-80)
🔇 Additional comments (1)
test/proxy.test.ts (1)

171-190: LGTM! Good baseline test for header preservation.

This test appropriately verifies that custom headers are preserved during proxying, serving as a regression check to ensure the transfer-encoding fix doesn't inadvertently filter other headers.

Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ❤️

@pi0 pi0 changed the title fix(proxy): strip transfer-encoding header from proxied response fix(proxy): strip transfer-encoding header from proxied response Dec 8, 2025
@pi0 pi0 merged commit 8319767 into h3js:main Dec 8, 2025
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

proxyRequest does not work correctly with Bun

2 participants