@@ -4338,6 +4338,32 @@ added:
43384338
43394339Set the maximum number of idle HTTP parsers.
43404340
4341+ ## ` http .setGlobalProxyFromEnv ([proxyEnv])`
4342+
4343+ <!-- YAML
4344+ added:
4345+ - REPLACEME
4346+ -->
4347+
4348+ * ` proxyEnv` {Object} An object containing proxy configuration. This accepts the
4349+ same options as the ` proxyEnv` option accepted by [` Agent` ][]. **Default:**
4350+ ` process .env ` .
4351+ * Returns: {Function} A function that restores the original agent and dispatcher
4352+ settings to the state before this ` http .setGlobalProxyFromEnv ()` is invoked.
4353+
4354+ Dynamically resets the global configurations to enable built-in proxy support for
4355+ ` fetch ()` and ` http .request ()` /` https .request ()` at runtime, as an alternative
4356+ to using the ` -- use- env- proxy` flag or ` NODE_USE_ENV_PROXY ` environment variable.
4357+ It can also be used to override settings configured from the environment variables.
4358+
4359+ As this function resets the global configurations, any previously configured
4360+ ` http .globalAgent ` , ` https .globalAgent ` or undici global dispatcher would be
4361+ overridden after this function is invoked. It's recommended to invoke it before any
4362+ requests are made and avoid invoking it in the middle of any requests.
4363+
4364+ See [Built-in Proxy Support][] for details on proxy URL formats and ` NO_PROXY `
4365+ syntax.
4366+
43414367## Class: ` WebSocket `
43424368
43434369<!-- YAML
@@ -4358,6 +4384,9 @@ added: v24.5.0
43584384When Node.js creates the global agent, if the ` NODE_USE_ENV_PROXY ` environment variable is
43594385set to ` 1 ` or ` -- use- env- proxy` is enabled, the global agent will be constructed
43604386with ` proxyEnv: process .env ` , enabling proxy support based on the environment variables.
4387+
4388+ To enable proxy support dynamically and globally, use [` http .setGlobalProxyFromEnv ()` ][].
4389+
43614390Custom agents can also be created with proxy support by passing a
43624391` proxyEnv` option when constructing the agent. The value can be ` process .env `
43634392if they just want to inherit the configuration from the environment variables,
@@ -4413,6 +4442,86 @@ Or the `--use-env-proxy` flag.
44134442HTTP_PROXY=http://proxy.example.com:8080 NO_PROXY=localhost,127.0.0.1 node --use-env-proxy client.js
44144443` ` `
44154444
4445+ To enable proxy support dynamically and globally with ` process.env` (the default option of ` http.setGlobalProxyFromEnv()` ):
4446+
4447+ ` ` ` cjs
4448+ const http = require('node:http');
4449+
4450+ // Reads proxy-related environment variables from process.env
4451+ const restore = http.setGlobalProxyFromEnv();
4452+
4453+ // Subsequent requests will use the configured proxies from environment variables
4454+ http.get('http://www.example.com', (res) => {
4455+ // This request will be proxied if HTTP_PROXY or http_proxy is set
4456+ });
4457+
4458+ fetch('https://www.example.com', (res) => {
4459+ // This request will be proxied if HTTPS_PROXY or https_proxy is set
4460+ });
4461+
4462+ // To restore the original global agent and dispatcher settings, call the returned function.
4463+ // restore();
4464+ ` ` `
4465+
4466+ ` ` ` mjs
4467+ import http from 'node:http';
4468+
4469+ // Reads proxy-related environment variables from process.env
4470+ http.setGlobalProxyFromEnv();
4471+
4472+ // Subsequent requests will use the configured proxies from environment variables
4473+ http.get('http://www.example.com', (res) => {
4474+ // This request will be proxied if HTTP_PROXY or http_proxy is set
4475+ });
4476+
4477+ fetch('https://www.example.com', (res) => {
4478+ // This request will be proxied if HTTPS_PROXY or https_proxy is set
4479+ });
4480+
4481+ // To restore the original global agent and dispatcher settings, call the returned function.
4482+ // restore();
4483+ ` ` `
4484+
4485+ To enable proxy support dynamically and globally with custom settings:
4486+
4487+ ` ` ` cjs
4488+ const http = require('node:http');
4489+
4490+ const restore = http.setGlobalProxyFromEnv({
4491+ http_proxy: 'http://proxy.example.com:8080',
4492+ https_proxy: 'https://proxy.example.com:8443',
4493+ no_proxy: 'localhost,127.0.0.1,.internal.example.com',
4494+ });
4495+
4496+ // Subsequent requests will use the configured proxies
4497+ http.get('http://www.example.com', (res) => {
4498+ // This request will be proxied through proxy.example.com:8080
4499+ });
4500+
4501+ fetch('https://www.example.com', (res) => {
4502+ // This request will be proxied through proxy.example.com:8443
4503+ });
4504+ ` ` `
4505+
4506+ ` ` ` mjs
4507+ import http from 'node:http';
4508+
4509+ http.setGlobalProxyFromEnv({
4510+ http_proxy: 'http://proxy.example.com:8080',
4511+ https_proxy: 'https://proxy.example.com:8443',
4512+ no_proxy: 'localhost,127.0.0.1,.internal.example.com',
4513+ });
4514+
4515+ // Subsequent requests will use the configured proxies
4516+ http.get('http://www.example.com', (res) => {
4517+ // This request will be proxied through proxy.example.com:8080
4518+ });
4519+
4520+ fetch('https://www.example.com', (res) => {
4521+ // This request will be proxied through proxy.example.com:8443
4522+ });
4523+ ` ` `
4524+
44164525To create a custom agent with built- in proxy support:
44174526
44184527` ` ` cjs
@@ -4476,6 +4585,7 @@ const agent2 = new http.Agent({ proxyEnv: process.env });
44764585[` http.get()` ]: #httpgetoptions- callback
44774586[` http.globalAgent` ]: #httpglobalagent
44784587[` http.request()` ]: #httprequestoptions- callback
4588+ [` http.setGlobalProxyFromEnv()` ]: #httpsetglobalproxyfromenvproxyenv
44794589[` message.headers` ]: #messageheaders
44804590[` message.rawHeaders` ]: #messagerawheaders
44814591[` message.socket` ]: #messagesocket
0 commit comments