Skip to content

feat(fetch): support custom Response constructor - #186

Merged
pi0 merged 1 commit into
mainfrom
feat/custom-res
Oct 23, 2026
Merged

pi0 merged 1 commit into
mainfrom
feat/custom-res

Conversation

@pi0x

@pi0x pi0x commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #185 (option 1).

Adds a Response option to proxyFetch():

import { FastResponse } from "srvx";

const res = await proxyFetch(addr, req, undefined, { Response: FastResponse });

When a custom constructor is set, the upstream IncomingMessage is passed as the body as-is, with no Readable.toWeb(). Constructors that accept Node.js streams, like srvx's FastResponse / NodeResponse, can then pipe it natively, so a Node-to-Node proxy (e.g. env-runner's runner.fetch()) skips both stream conversions. httpxy still has no srvx dependency because the feature is opt-in.

Status, statusText and headers are built the same way as before: hop-by-hop headers are stripped and multi-value headers like set-cookie are kept. 204/304 still get a null body. Without the option, nothing changes: the global Response with a web stream body.

Tests

  • test/fetch.test.ts, new Response option block:
    • the custom constructor receives the IncomingMessage as its body
    • header handling (hop-by-hop stripped, multi-value kept)
    • null body for 204
    • Node's global Response also accepts the stream (async iterable)
  • Checked by hand end to end with [email protected]: serve() + FastResponse proxies a 1 MB body and repeated set-cookie headers correctly.

README and AGENTS.md are updated.

🤖 Generated with AI assistant

Summary by CodeRabbit

  • New Features
    • proxyFetch now supports an optional custom Response constructor, allowing upstream response bodies to be used as Node.js streams without conversion.
    • Without a custom constructor, responses continue to use the global Response and web streams.
    • Response status and headers are preserved across both options, including handling of empty bodies for status 204 and 304.

Allows passing a Response constructor (e.g. srvx's `FastResponse`) via
`proxyFetch(addr, req, init, { Response })`. When set, the upstream
`IncomingMessage` is passed as the body as-is, skipping the
`Readable.toWeb()` conversion for Node-to-Node proxies.

Resolves #185
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 29178385-c6bd-4808-b06e-8117b0fdc3ec
📥 Commits

Reviewing files that changed from the base of the PR and between b202844 and 3226979.

📒 Files selected for processing: 4
  • AGENTS.md
  • README.md
  • src/fetch.ts
  • test/fetch.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

📝 Walkthrough

proxyFetch adds an optional Response constructor. When supplied, it receives the upstream Node.js response stream directly, or null for status 204 or 304. Without the option, proxyFetch continues converting the stream to a web stream for the global Response.

Changes: Response stream option

Layer / File(s) Summary
Custom response constructor and stream handling
src/fetch.ts, test/fetch.test.ts, README.md, AGENTS.md
ProxyFetchOptions adds an optional Response constructor. proxyFetch passes it the upstream stream, or null for status 204 or 304; the default path converts the stream to a web stream. Tests cover the custom and default paths, header handling, and body contents. The documentation describes both paths.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant Upstream as Upstream HTTP response
  participant Proxy as proxyFetch
  participant Constructor as Response constructor
  Upstream->>Proxy: Provide IncomingMessage
  alt Custom Response option
    Proxy->>Constructor: Pass IncomingMessage or null for status 204 or 304
  else Default Response
    Proxy->>Proxy: Convert IncomingMessage with Readable.toWeb()
    Proxy->>Constructor: Pass web-stream body
  end
Loading

Suggested reviewers: pi0

Merge Risk: ⚪ Minimal · up to 32269

The reported upstream-disconnect crash is not supported, and no actionable merge blocker remains.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "feat(fetch): support custom Response constructor" matches the main change. The PR adds an optional Response constructor option to proxyFetch() in src/fetch.ts. When the option is set,…
Linked Issues check Passed Issue #185 asks for an opt-in way to keep the upstream Node.js stream as the proxyFetch() response body, without a srvx dependency. The PR implements the constructor option shape from the issue. In sr…
Out of Scope Changes check Passed All changed files relate to #185. src/fetch.ts adds the Response option and its branch. test/fetch.test.ts adds tests for that option, and its import of IncomingMessage supports the instanceof asserti…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (2 skipped: 2 …
Full details: Title check

Explanation

The title "feat(fetch): support custom Response constructor" matches the main change. The PR adds an optional Response constructor option to proxyFetch() in src/fetch.ts. When the option is set, the upstream Node.js response stream goes to that constructor without Readable.toWeb() conversion. The title is short, specific, and uses the conventional commit form. It names the feature and the affected area without listing files or using vague terms.

Full details: Linked Issues check

Explanation

Issue #185 asks for an opt-in way to keep the upstream Node.js stream as the proxyFetch() response body, without a srvx dependency. The PR implements the constructor option shape from the issue. In src/fetch.ts, ProxyFetchOptions gains an optional Response?: typeof Response property. When set, proxyFetch() returns new opts.Response(res, resInit) with the IncomingMessage passed as the body and no Readable.toWeb() call. For 204/304 the body is null. Without the option, the original new Response(Readable.toWeb(res), ...) path is unchanged. Status, statusText and headers are built from the same resInit as before, and transfer-encoding, keep-alive and connection headers are still stripped. The new test block in test/fetch.test.ts covers the pass-through body (asserted as an IncomingMessage instance), hop-by-hop stripping with multi-value set-cookie retained, a null body for 204, and the global Response accepting the stream as an async iterable. No srvx import was added to httpxy. The issue's coding requirements are met by the code at the reviewed head.

Full details: Out of Scope Changes check

Explanation

All changed files relate to #185. src/fetch.ts adds the Response option and its branch. test/fetch.test.ts adds tests for that option, and its import of IncomingMessage supports the instanceof assertion. README.md and AGENTS.md document the new option. No unrelated refactoring or behavior change appears in the diff. The default path (no Response option) still uses the global Response with Readable.toWeb(res), so existing behavior is preserved.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (2 skipped: 2 unsupported.)

✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

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

@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.05%. Comparing base (b202844) to head (3226979).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #186      +/-   ##
==========================================
+ Coverage   95.03%   95.05%   +0.01%     
==========================================
  Files           8        8              
  Lines         806      809       +3     
  Branches      330      332       +2     
==========================================
+ Hits          766      769       +3     
  Misses         35       35              
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pi0
pi0 merged commit 25753cf into main Oct 23, 2026
7 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.

proxyFetch(): option to keep the upstream Node.js stream as the response body

2 participants