Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

  • https://www.npmjs.com/package/node-fetch#custom-agent
  • https://nodejs.org/api/globals.html#fetch
like image 695
John Avatar asked Dec 05 '25 14:12

John


1 Answers

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.

like image 143
josh3736 Avatar answered Dec 07 '25 08:12

josh3736