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
Expose Undici's ProxyAgent and setGlobalDispatcher within Node #43187
Comments
global[Symbol.for('undici.globalDispatcher.1')] = yourDispatcher; |
Replying to the last comment from @mcollina in the previous issue:
This makes sense! I'm not aware of Undici's process around stability guarantees like this, but I can understand how there are constraints there. I do understand this isn't a top top priority since installing & importing Undici elsewhere is a usable workaround, so there's certainly no rush just would justify shipping an unstable API unnecessarily. I do think that the current situation isn't a good end state though, and there are good options we can aim for to fix this, so it would be great to find agreement to aim in that direction in the medium term. I'm not sure how to take anything to the TSC, but very happy to be included on any discussion of this any time. |
That's does work as a workaround, but it's not especially nice as the official API for this, and I think ProxyAgent needs to be available too to make this usable for global Fetch. |
I don't think we should expose |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
2022-11-14, Version 19.1.0
|
That does not mark it stable, on purpose. It's the first step on that journey. This is something we might want to consider right now. |
In the TSC meeeting of today, we decided that we are going to support HTTP_PROXY, HTTPS_PROXY and NO_PROXY env variables directly in Undici: nodejs/undici#1650. |
How is it going to handle different casing rules? |
Is there a workaround to obtain |
Hi All, I would like if you expose the whole
If you expose only a certain feature of undici then there will be someone who needs some unexposed feature, so better to expose everything in 1 step. I think it is not worth polluting the global namespace so I suggest using |
While I would like to see With this, node's fetch would diverge from web fetch, but imho for good reason because the option is not relevant in browsers where proxies are handled outside of JS. |
I think it's time for the TSC to make some plans about this. I'll cover this at the collab summit too. |
In addition to |
We're using HttpAgent similarly now so that we can override DNS resolution with our own caching resolver. |
In case anyone else was looking for a way to overriding the global dispatcher with a new agent with some custom config options, here's an example // there is no official Node API to get the global dispatcher (as of Node 20.11.1)
// getting the dispatcher from global is considered a public API https://github.com/nodejs/undici/discussions/2167#discussioncomment-6265039
// hard-coded symbol for the undici global dispatcher https://github.com/nodejs/undici/blob/main/lib/global.js#L5
const unidiciGlobalDispatcherSymbol = Symbol.for('undici.globalDispatcher.1');
const undiciGlobalDispatcher = global[unidiciGlobalDispatcherSymbol];
if (!undiciGlobalDispatcher) {
throw new Error('Could not find the global Undici dispatcher, maybe not in node runtime?');
}
// initialize a new agent/dispatcher https://undici.nodejs.org/#/docs/api/Agent
// inspired by https://github.com/orgs/nodejs/discussions/51260#discussioncomment-7949240
const newDispatcher = new undiciGlobalDispatcher.constructor({
// limit the number of client (host) connections
// https://undici.nodejs.org/#/docs/api/Pool
connections: 128,
});
// override the Undici global dispatcher
global[unidiciGlobalDispatcherSymbol] = newDispatcher; |
so, there is no way to set a dispatcher on a per-call basis yet? |
You can already set |
right. @mcollina any updates from TSC? |
There is no consensus on exposing more of undici. My current plan is to add support for the HTTP_PROXY env variable behind a flag in v22 (and possibly v20), then unflag in v23. |
@mcollina reading HTTP_PROXY is probably not powerful enough. I have (dev only, for use with Fiddler) a loop that monitors for the proxy server being up and only then enrolling requests in the proxy. This allows the use of Fiddler to be decided for an already running process and not depend on Fiddler running for calls to succeed. I don't suggest that precisely this better but still imperfect mechanism be built into node - but we ought to be able to write our own. Exposing the primitives for this is better than choosing one underpowered implementation for all. |
Agreed. If you need that level of sophistication you are better off in using undici directly for now. |
Originally posted by @pimterry in #42814 (comment)
The text was updated successfully, but these errors were encountered: