-
Notifications
You must be signed in to change notification settings - Fork 10
Allow custom fetch and remove undici-specific dispatcher option #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the fetch configuration API to support custom fetch implementations and remove the undici-specific dispatcher option from the top level of FetchOptions.
Key Changes:
- Separates
fetch(custom fetch function) fromfetchOptions(configuration object) in theServerOptionsinterface - Moves the
dispatcherproperty from a dedicated field inFetchOptionsto be part of the standardrequestOptions - Updates all tests and documentation to reflect the new API structure
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
lib/http-proxy/index.ts |
Refactors TypeScript interfaces to split fetch option into fetchOptions (configuration) and fetch (custom fetch function), removes Dispatcher type |
lib/http-proxy/passes/web-incoming.ts |
Updates implementation to use custom fetch and retrieve dispatcher from requestOptions instead of dedicated property |
lib/test/http/proxy-http2-to-http2.test.ts |
Migrates test to use new fetchOptions.requestOptions.dispatcher structure |
lib/test/http/proxy-callbacks.test.ts |
Migrates test to use new fetchOptions.requestOptions.dispatcher structure |
lib/test/http/customFetch.test.ts |
Adds new test demonstrating custom fetch implementation usage |
README.md |
Updates all examples and documentation to reflect the new API structure |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| const proxy = createProxyServer({ | ||
| target: "https://http2-server.example.com", | ||
| fetch: true // Uses default fetch configuration | ||
| fetch // Uses default fetch configuration |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example is incomplete or unclear. Based on the PR changes, fetch should be set to the global fetch function to use default fetch configuration. This should be fetch: fetch or just importing and using the global fetch. The current syntax fetch without assignment is ambiguous and may not work as intended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems fine to me as is.
| - **fetch**: Enable fetch API for HTTP/2 support. Set to `true` for defaults, or provide custom configuration: | ||
| - `dispatcher`: Custom fetch dispatcher (e.g., undici Agent with `allowH2: true` for HTTP/2) | ||
| - `requestOptions`: Additional fetch request options | ||
| - **fetchOptions**: Enable fetch API for HTTP/2 support. Set to `true` for defaults, or provide custom configuration: |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation is incorrect. fetchOptions cannot be set to true according to the new TypeScript interface definition. It should be an object of type FetchOptions or undefined. Remove the "Set to true for defaults, or" part, or update the interface to support true as a value.
| - **fetchOptions**: Enable fetch API for HTTP/2 support. Set to `true` for defaults, or provide custom configuration: | |
| - **fetchOptions**: Enable fetch API for HTTP/2 support. Provide an object of type `FetchOptions` for custom configuration: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has a very good point.
| @@ -0,0 +1,70 @@ | |||
| /* | |||
| pnpm test proxy-http2-to-http2.test.ts | |||
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment references the wrong test file. It should reference customFetch.test.ts instead of proxy-http2-to-http2.test.ts.
| pnpm test proxy-http2-to-http2.test.ts | |
| pnpm test customFetch.test.ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point
| return fetch(url, {...options, dispatcher: TestAgent as any }); | ||
| }; | ||
|
|
||
| describe("Basic example of proxying over HTTP2 to a target HTTP2 with custom Fetch option server", () => { |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test description has awkward wording with "custom Fetch option server" at the end. It should be "with custom Fetch option" or "server with custom Fetch option".
| describe("Basic example of proxying over HTTP2 to a target HTTP2 with custom Fetch option server", () => { | |
| describe("Basic example of proxying over HTTP2 to a target HTTP2 server with custom Fetch option", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fine either way
| * This is passed to https.request. | ||
| */ | ||
| ca?: string; | ||
| /** Enable using fetch for proxy requests. Set to true for defaults, or provide custom configuration. */ |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSDoc comment is outdated. It mentions "Set to true for defaults, or provide custom configuration", but the fetchOptions property only accepts FetchOptions type, not boolean. The comment should be updated to reflect the new API design where fetchOptions is an optional object and fetch is a separate property for custom fetch implementations.
| /** Enable using fetch for proxy requests. Set to true for defaults, or provide custom configuration. */ | |
| /** Optional configuration object for fetch-based proxy requests. Use this to customize fetch request and response handling. For custom fetch implementations, use the `fetch` property. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point
| ca?: string; | ||
| /** Enable using fetch for proxy requests. Set to true for defaults, or provide custom configuration. */ | ||
| fetch?: boolean | FetchOptions; | ||
| fetchOptions?: FetchOptions; |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing JSDoc comment for the new fetch property. This should document that it allows providing a custom fetch implementation (e.g., a wrapper around fetch or from a different package).
| fetchOptions?: FetchOptions; | |
| fetchOptions?: FetchOptions; | |
| /** | |
| * Allows providing a custom fetch implementation (e.g., a wrapper around fetch or from a different package). | |
| * If set, this function will be used for proxy requests instead of the default fetch. | |
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems fine as you have it, since the comment applies to both options (fetch and fetchOptions).
| server.emit("start", req, res, options.target || options.forward!); | ||
|
|
||
| if (options.fetch || process.env.FORCE_FETCH_PATH === "true") { | ||
| if (options.fetch ||options.fetchOptions || process.env.FORCE_FETCH_PATH === "true") { |
Copilot
AI
Nov 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space after || operator. Should be options.fetch || options.fetchOptions.
| if (options.fetch ||options.fetchOptions || process.env.FORCE_FETCH_PATH === "true") { | |
| if (options.fetch || options.fetchOptions || process.env.FORCE_FETCH_PATH === "true") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'll run prettier so don't care.
|
The code looks good to me, but the AI did have some valid concerns about some comments/docs. |
Updated the fetch path options to
dispatcheroption, which is a undici (and thus nodejs) specific requestoption. Dispatcher should now be set under the reqular requestOptions, and users must make sure that it is valid fetch option.