Repository navigation
fix(apiv2): use undici's fetch with the bundled undici ProxyAgent (Node 26) - #11660
vivekkartha7 wants to merge 3 commits into
Conversation
When a proxy is set, apiv2 builds a ProxyAgent from the bundled undici (6.x) and passed it as `dispatcher` to Node's global fetch. Node 26's built-in fetch is undici 8, which rejects that dispatcher with "InvalidArgumentError: invalid onError method". Use the bundled undici's fetch whenever a dispatcher is set, so both come from the same version. The proxy tests no longer swap global fetch for undici's fetch, so they now check that apiv2 itself sends proxied requests through the proxy. Fixes firebase#11659
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Address review feedback: pass the request options to undici's fetch as undici's RequestInit rather than `any`, note why the Response cast is safe, and trim the proxy test comments.
a805162 to
b9af60e
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request fixes a TypeError: fetch failed issue on Node.js 26 when HTTP_PROXY or HTTPS_PROXY is set by using the bundled undici's fetch when a dispatcher is present. It also cleans up the corresponding proxy tests in src/apiv2.spec.ts. The review feedback recommends removing the intermediate unknown cast when casting the undiciFetch result to Response to adhere to the repository's TypeScript style guide against using any or unknown as escape hatches.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request resolves a TypeError: fetch failed issue on Node.js 26 when HTTP_PROXY or HTTPS_PROXY is configured. It ensures that when a custom dispatcher is present in the fetch options, the client uses the bundled undiciFetch instead of the environment's built-in fetch to prevent version mismatch issues. The review feedback points out a TypeScript style guide violation where unknown is used as an escape hatch during a type cast in src/apiv2.ts, and suggests a safer alternative using Omit to maintain type safety.
…roxy-dispatcher # Conflicts: # CHANGELOG.md
Description
Fixes #11659.
When
HTTPS_PROXY/HTTP_PROXYis set,apiv2builds aProxyAgentfrom the bundledundici(6.29.x) and passes it asdispatcherto Node's globalfetch. Node's built-in fetch uses its own undici. On Node 26 that is undici 8, which removed the legacyonErrorhandler hook (nodejs/undici#4753), so it rejects the undici 6 dispatcher, and every request fails:With this change,
apiv2calls the bundled undici'sfetchwhenever it sets a dispatcher, so the agent and fetch always come from the same undici version. Requests without a proxy, and requests thatNO_PROXYbypasses, still use globalfetchas before. That keeps the existing nock-based tests (which rely on the Mocha bootstrap's node-fetch) and the premature-close keep-alive workaround unchanged. Thecompress: falsepath already used undici'srequest()and is unaffected.Test change: the "with a proxy" tests swapped global
fetchfor undici'sfetch, which masked this problem. They no longer do. Global fetch stays node-fetch, which ignores dispatchers, so the proxied tests now pass only ifapiv2itself proxies with undici's fetch. Without theapiv2.tschange, the two proxied tests fail withexpected 0 to equal 1(no tunnel).Alternatives considered: bumping
undicito 7/8 is a bigger dependency change and doesn't guarantee compatibility with whatever undici a future Node ships.NODE_USE_ENV_PROXY=1would need every user to opt in.Scenarios Tested
Local CONNECT proxy on
127.0.0.1:8080, local target on127.0.0.1:8081,HTTPS_PROXY=http://127.0.0.1:8080, callingapiv2.Client.requestfrom the builtlib/:mainInvalidArgumentError: invalid onError methodBuilt CLI on Node 26 with nothing listening on the proxy port (
HTTPS_PROXY=http://127.0.0.1:8080 node lib/bin/firebase.js projects:list --debug):main:error from fetch(https://www.googleapis.com/oauth2/v3/token, ...): InvalidArgumentError: invalid onError methoderror from fetch(https://www.googleapis.com/oauth2/v3/token, ...): Error: connect ECONNREFUSED 127.0.0.1:8080, which is the expected error for a proxy that isn't running.Other checks:
npx mocha src/apiv2.spec.ts: 78 passing on Node 24 and Node 26.npm run buildandtsc --noEmitare clean. ESLint has 0 errors on the changed files, and Prettier is clean.Sample Commands