Skip to content

Make it possible to use Fetch with proxies or other agents #42814

Closed
@pimterry

Description

@pimterry

What is the problem this feature will solve?

The new fetch API as implemented cannot be used with an HTTP proxy, which is required for connectivity in many environments.

For normal HTTP this is implemented via agents, but there's no way to use any agents with this fetch API. Many of us working in environments with proxies use libraries like global-agent which set node's globalAgent to a proxy agent based on the system settings to automatically configure all libraries, but that doesn't work with fetch either.

Notably, this means the docs are wrong right now, when they say:

http.globalAgent: Global instance of Agent which is used as the default for all HTTP client requests.

This agent is not used for HTTP client requests if you use the fetch API.

Node's fetch implementation comes from Undici, and although Undici doesn't offer an explicit way to set this per fetch request (see nodejs/undici#1350) it does offer an agent-equivalent dispatcher option on all other request methods, and a setGlobalDispatcher method to configure a dispatcher globally (like node's globalAgent) which does work for fetch.

That means it is possible to use proxies with Undici's fetch right now, but not in Node as this isn't exposed anywhere (AFAICT).

What is the feature you are proposing to solve the problem?

  • Provide a way to specify a Node.js agent or a Undici dispatcher for a fetch request
  • Provide a way to set a global agent/dispatcher, which applies to all HTTP & fetch requests

Since they're very closely related, it seems like it would be sensible to aim to move everything to either agents or dispatchers for all HTTP APIs in future. While fetch is experimental though it seems reasonable to me to implement this only with Undici's existing dispatchers for now and pick one direction or the other to commonize later.

What alternatives have you considered?

As far as I can tell, there's currently no alternative or workaround available to use proxies with fetch in Node. If you need to use an HTTP proxy for connectivity, the current fetch API is unusable.

This is particularly bad because some libraries that support both browsers & node will use the fetch global automatically when available or node-fetch otherwise (which uses http internally) for their requests. Although it used to be possible to use these libraries in a proxy environment by using global-agent or passing an agent explicitly, it's now impossible to use these libraries at all, because they use the new fetch global which ignores all agent configuration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.fetchIssues and PRs related to the Fetch API

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions