Skip to content

fix(apiv2): use undici's fetch with the bundled undici ProxyAgent (Node 26) - #11660

Open
vivekkartha7 wants to merge 3 commits into
firebase:mainfrom
vivekkartha7:fix/node26-undici-proxy-dispatcher
Open

vivekkartha7 wants to merge 3 commits into
firebase:mainfrom
vivekkartha7:fix/node26-undici-proxy-dispatcher

Conversation

@vivekkartha7

@vivekkartha7 vivekkartha7 commented Dec 4, 2026 •

Copy link
Copy Markdown

Description

Fixes #11659.

When HTTPS_PROXY / HTTP_PROXY is set, apiv2 builds a ProxyAgent from the bundled undici (6.29.x) and passes it as dispatcher to Node's global fetch. Node's built-in fetch uses its own undici. On Node 26 that is undici 8, which removed the legacy onError handler hook (nodejs/undici#4753), so it rejects the undici 6 dispatcher, and every request fails:

TypeError: fetch failed
  [cause]: InvalidArgumentError: invalid onError method  (UND_ERR_INVALID_ARG)

With this change, apiv2 calls the bundled undici's fetch whenever it sets a dispatcher, so the agent and fetch always come from the same undici version. Requests without a proxy, and requests that NO_PROXY bypasses, still use global fetch as 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. The compress: false path already used undici's request() and is unaffected.

Test change: the "with a proxy" tests swapped global fetch for undici's fetch, which masked this problem. They no longer do. Global fetch stays node-fetch, which ignores dispatchers, so the proxied tests now pass only if apiv2 itself proxies with undici's fetch. Without the apiv2.ts change, the two proxied tests fail with expected 0 to equal 1 (no tunnel).

Alternatives considered: bumping undici to 7/8 is a bigger dependency change and doesn't guarantee compatibility with whatever undici a future Node ships. NODE_USE_ENV_PROXY=1 would need every user to opt in.

Scenarios Tested

Local CONNECT proxy on 127.0.0.1:8080, local target on 127.0.0.1:8081, HTTPS_PROXY=http://127.0.0.1:8080, calling apiv2.Client.request from the built lib/:

Node 24.16.0 (undici 7.25.0) Node 26.14.0 (undici 8.12.4)
main OK, 1 proxied request InvalidArgumentError: invalid onError method
this PR OK, 1 proxied request OK, 1 proxied request

Built 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 method
  • this PR: error 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 build and tsc --noEmit are clean. ESLint has 0 errors on the changed files, and Prettier is clean.

Sample Commands

HTTPS_PROXY=http://proxy.example:8080 firebase projects:list

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
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@google-cla

google-cla Bot commented Dec 4, 2026

Copy link
Copy Markdown

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.

@vivekkartha7 vivekkartha7 changed the title fix(apiv2): use undici's fetch with the bundled undici ProxyAgent (Node 26) fix(apiv2): use undici's fetch with the bundled undici ProxyAgent (Node 26) Dec 4, 2026
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.
@vivekkartha7
vivekkartha7 force-pushed the fix/node26-undici-proxy-dispatcher branch from a805162 to b9af60e Compare December 4, 2026 21:49
@vivekkartha7

Copy link
Copy Markdown
Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread src/apiv2.ts Resolved
@vivekkartha7

Copy link
Copy Markdown
Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread src/apiv2.ts Resolved

This branch has not been deployed

No deployments
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.

Node 26: TypeError: fetch failed (UND_ERR_INVALID_ARG: invalid onError method) when HTTP_PROXY / HTTPS_PROXY is set

1 participant