-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -268,8 +268,8 @@ import { Agent } from "undici"; | |||||
| // Create a proxy server with fetch and HTTP/2 support | ||||||
| const proxy = createProxyServer({ | ||||||
| target: "https://127.0.0.1:5050", | ||||||
| fetch: { | ||||||
| dispatcher: new Agent({ allowH2: true }), | ||||||
| fetchOptions: { | ||||||
| requestOptions: {dispatcher: new Agent({ allowH2: true })}, | ||||||
| // Modify the request before it's sent | ||||||
| onBeforeRequest: async (requestOptions, req, res, options) => { | ||||||
| requestOptions.headers['X-Special-Proxy-Header'] = 'foobar'; | ||||||
|
|
@@ -460,8 +460,8 @@ setGlobalDispatcher(new Agent({ allowH2: true })); | |||||
| // Or create a proxy with HTTP/2 support using fetch | ||||||
| const proxy = createProxyServer({ | ||||||
| target: "https://http2-server.example.com", | ||||||
| fetch: { | ||||||
| dispatcher: new Agent({ allowH2: true }) | ||||||
| fetchOptions: { | ||||||
| requestOptions: {dispatcher: new Agent({ allowH2: true })} | ||||||
| } | ||||||
| }); | ||||||
| ``` | ||||||
|
|
@@ -472,7 +472,7 @@ const proxy = createProxyServer({ | |||||
| // Shorthand to enable fetch with defaults | ||||||
| const proxy = createProxyServer({ | ||||||
| target: "https://http2-server.example.com", | ||||||
| fetch: true // Uses default fetch configuration | ||||||
| fetch // Uses default fetch configuration | ||||||
| }); | ||||||
| ``` | ||||||
|
|
||||||
|
|
@@ -481,17 +481,17 @@ const proxy = createProxyServer({ | |||||
| ```js | ||||||
| const proxy = createProxyServer({ | ||||||
| target: "https://api.example.com", | ||||||
| fetch: { | ||||||
| // Use undici's Agent for HTTP/2 support | ||||||
| dispatcher: new Agent({ | ||||||
| allowH2: true, | ||||||
| connect: { | ||||||
| rejectUnauthorized: false, // For self-signed certs | ||||||
| timeout: 10000 | ||||||
| } | ||||||
| }), | ||||||
| // Additional fetch request options | ||||||
| fetchOptions: { | ||||||
| requestOptions: { | ||||||
| // Use undici's Agent for HTTP/2 support | ||||||
| dispatcher: new Agent({ | ||||||
| allowH2: true, | ||||||
| connect: { | ||||||
| rejectUnauthorized: false, // For self-signed certs | ||||||
| timeout: 10000 | ||||||
| } | ||||||
| }), | ||||||
| // Additional fetch request options | ||||||
| headersTimeout: 30000, | ||||||
| bodyTimeout: 60000 | ||||||
| }, | ||||||
|
|
@@ -524,13 +524,14 @@ const proxy = createProxyServer({ | |||||
| key: readFileSync("server-key.pem"), | ||||||
| cert: readFileSync("server-cert.pem") | ||||||
| }, | ||||||
| fetch: { | ||||||
| dispatcher: new Agent({ | ||||||
| allowH2: true, | ||||||
| connect: { rejectUnauthorized: false } | ||||||
| }) | ||||||
| fetchOptions: { | ||||||
| requestOptions: { | ||||||
| dispatcher: new Agent({ | ||||||
| allowH2: true, | ||||||
| connect: { rejectUnauthorized: false } | ||||||
| }) | ||||||
| } | ||||||
| }, | ||||||
| secure: false // Skip SSL verification for self-signed certs | ||||||
| }).listen(8443); | ||||||
| ``` | ||||||
|
|
||||||
|
|
@@ -639,9 +640,8 @@ const proxy = createProxyServer({ | |||||
|
|
||||||
| - **ca**: Optionally override the trusted CA certificates. This is passed to https.request. | ||||||
|
|
||||||
| - **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: | ||||||
|
||||||
| - **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.
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -98,18 +98,10 @@ export interface ServerOptions { | |||||||||||||
| */ | ||||||||||||||
| ca?: string; | ||||||||||||||
| /** Enable using fetch for proxy requests. Set to true for defaults, or provide custom configuration. */ | ||||||||||||||
|
||||||||||||||
| /** 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
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).
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -86,7 +86,7 @@ export function stream( | |||||
| // And we begin! | ||||||
| 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") { | ||||||
|
||||||
| 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.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||
| /* | ||||||
| pnpm test 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
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
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,
fetchshould be set to the globalfetchfunction to use default fetch configuration. This should befetch: fetchor just importing and using the global fetch. The current syntaxfetchwithout 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.