why is the agent option not available in node native fetch?
Asked Answered
C

2

17

I have started using the native fetch function recently (node 17+)

I realized today it is lacking a few functionalities from node-fetch, e.g. agent

Why is that?

Is there are plan to add it?

It is a shame because I needed to add node-fetch to my project as a result

see

Cayuse answered 22/9, 2022 at 15:42 Comment(1)
Isn't the entire point of having fetch() built into nodejs that it is the identical API as what's in the browser? There are richer options such as got() or axios() or node-fetch() if you want support for node-specific behaviors.Inflight
A
12

The actual answer is to why the options you're used to from the http module aren't available is that perhaps surprisingly, node's builtin fetch() global does not use the HTTP stack provided by the traditional builtin http/https modules.

Instead, it uses a parallel, from-scratch HTTP stack rewrite called undici.

Given that fetch()'s HTTP stack is entirely separate from the standard HTTP stack, it should not be surprising that the options you can supply to http.get et al don't work with fetch().

undici's docs are available here. http Agents are replaced by a Dispatcher. You can pass a custom Dispatcher in to fetch(…, { dispatcher }), which allows you to customize fetch's HTTP behavior.

Abib answered 21/4, 2023 at 4:49 Comment(6)
So does this mean that fetch(url, { dispatcher: agent }) is what we're looking for here? See github.com/nodejs/undici/discussions/2167Dom
That depends on exactly what agent is. If it's a traditional http.Agent, no, the Dispatcher API is entirely different than the http.Agent API, so passing a http.Agent to the dispatcher option won't work. If it's a undici.Agent, then yes, that's correct. The dispatcher option takes an undici.Dispatcher, which is basically an abstract base class; undici.Agent inherits from it.Abib
Gotcha, that makes a lot of sense. Thank you very much for the clarification!Dom
@Abib Is it possible to access or use the Dispatcher/Agent classes without installing undici explicitly?Azilian
@joematune: only if you start node with --expose-internals, you can require('internal/deps/undici/undici').Abib
@Abib Thanks! It seems they aren't exporting Dispatcher or Agent as of v21 - only the globals. Am I missing some way to access dispatcher?Azilian
S
1

The native fetch function is Experimental ie not ready for production and very likely has issues.

https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch

Also jfriend00 point is very valid: "Isn't the entire point of having fetch() built into nodejs that it is the identical API as what's in the browser?"

Sublimity answered 23/9, 2022 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.