Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing a custom agent to fetch() #1489

Closed
mataha opened this issue Jun 11, 2022 · 6 comments
Closed

Passing a custom agent to fetch() #1489

mataha opened this issue Jun 11, 2022 · 6 comments
Labels
enhancement New feature or request

Comments

@mataha
Copy link

mataha commented Jun 11, 2022

This would solve...

In some otherwise trivial cases for fetch() a custom, one-purpose agent is required to manage a request, e.g. in order to check a server's identity manually or to disregard that verification completely (self-signed certificates come to mind). Current approach, from what I've seen, requires passing a set of TLS options to a dispatcher explicitly, then using said dispatcher to invoke a request - maybe it's just me, but I feel this could be simplified.

The implementation should look like...

Using https.Agent as an example:

import https from 'node:https';

const result = await fetch("https://example.com", {
    (...)
    agent: new https.Agent({
        rejectUnauthorized: false,
    }),
});

(...)

I have also considered...

Some parameters can be set via alternative means; environment variables come to mind:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
process.env.NODE_EXTRA_CA_CERTS = ...;

However, I don't think applying this to every request is a good idea (same goes for any global agents).

Additional context

Implementations in similar projects:

Possibly relevant issues:

@mataha mataha added the enhancement New feature or request label Jun 11, 2022
@mcollina
Copy link
Member

This was already added in #1411.

Support for Node.js https.Agent is not planned due to API incompatibilities.

@mataha
Copy link
Author

mataha commented Jun 12, 2022

I understand, but: how can I do this in bare Node (with --experimental-fetch, of course) without installing this package?

@mcollina
Copy link
Member

In order to do this you would need to:

  1. use at least 18.3.0 (no --experimental-fetch needed)
  2. use this module

At some future time this will be bundled in core.

@bancek
Copy link

bancek commented May 11, 2023

If anyone else is looking for a way to pass rejectUnauthorized to fetch. I would expect to find this in Node.js docs or at least in Undici fetch or best practices docs.

You need to install undici dependency (npm install undici).

import { Agent } from 'undici';

await fetch('https://example.com', {
  dispatcher: new Agent({
    connect: {
      rejectUnauthorized: false,
    },
  }),
});

@Allon-Guralnek
Copy link

@bancek Using rejectUnauthorized didn't work for me when using ProxyAgent. It seems to ignore it. Perhaps it's only implemented in Agent.

Doing process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0" worked but that isn't an ideal solution.

@monjer
Copy link

monjer commented Jul 25, 2023

After read the source code and the test case code , i find the solution. The ProxyAgent class has an options.requestTls and options.proxyTls, which are not described in document. In order to use rejectUnauthorized : false options, just code like below:

import { ProxyAgent, request } from 'undici'

const proxyAgent = new ProxyAgent('my.proxy.server',{
    requestTls: {  // this is the key point
         rejectUnauthorized: false,
    }
})

const {  statusCode,  body } = await request('http://localhost:3000/foo', { dispatcher: proxyAgent })
for await (const data of body) {
  console.log('data', data.toString('utf8'))
}

Also check the test case Proxy via HTTP to HTTPS endpoint and the proxy-agent source code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants