ardatan / whatwg-node

Helper packages to create platform agnostic applications and libraries without worrying about the lack of WHATWG support in Node.js
MIT License
148 stars 32 forks source link

Request Agent from RequestInit not used and therefore not propagted #1342

Open sonewman opened 1 month ago

sonewman commented 1 month ago

When instantiating a Request the agent option is not set on the Request, it can only be set manually on the request object.

This introduces the additional issue that the agent is not propagated when another Request is cloned or instantiated from it.

To Reproduce Steps to reproduce the behavior:

const agent = new Agent();
const request1 = new PonyfillRequest('http://a', {
  agent,
});

// request1.agent === undefined

request1.agent = agent;

const request2 = request1.clone();

// request2.agent === undefined

Expected behavior

I would expect the agent to be set from the supplied arguments (since it is defined in the type definitions) I would expect the agent to be propagated to clones of said request

Environment:

Additional context

FoxBuru commented 2 weeks ago

I'm not really sure if this is the most compatible option given what's supported now on standard Fetch API. Apparently, the way to set an Agent while conforming to that standard, looks like:

const sampleRequestInit : RequestInit {
   ...otherArgs,
   dispatcher: new Agent({ 
     connect: {
       rejectUnauthorized: this.rejectUnauthorized
     }
   }),
}

Which runs on node >= 20 (and, for this concrete library, by enabling both skipPolyfill and useNodeFetch). For previous versions, the node-fetch shim was used to provide a somewhat compatible contract with the Fetch API.

Given this library tries to provide an abstraction not only over Fetch API, but also with other fetch-look-alike implementations like node-libcurl, I'm not sure how to reconcile those two views. Regardless, to maximize compatibility and stability, sounds like a good idea to keep this issue open, until either a fix or a decision is reached on how something like that can be achieved.