@@ -4361,6 +4361,32 @@ added:
43614361
43624362Set the maximum number of idle HTTP parsers.
43634363
4364+ ## ` http .setGlobalProxyFromEnv ([proxyEnv])`
4365+
4366+ <!-- YAML
4367+ added:
4368+ - REPLACEME
4369+ -->
4370+
4371+ * ` proxyEnv` {Object} An object containing proxy configuration. This accepts the
4372+ same options as the ` proxyEnv` option accepted by [` Agent` ][]. **Default:**
4373+ ` process .env ` .
4374+ * Returns: {Function} A function that restores the original agent and dispatcher
4375+ settings to the state before this ` http .setGlobalProxyFromEnv ()` is invoked.
4376+
4377+ Dynamically resets the global configurations to enable built-in proxy support for
4378+ ` fetch ()` and ` http .request ()` /` https .request ()` at runtime, as an alternative
4379+ to using the ` -- use- env- proxy` flag or ` NODE_USE_ENV_PROXY ` environment variable.
4380+ It can also be used to override settings configured from the environment variables.
4381+
4382+ As this function resets the global configurations, any previously configured
4383+ ` http .globalAgent ` , ` https .globalAgent ` or undici global dispatcher would be
4384+ overridden after this function is invoked. It's recommended to invoke it before any
4385+ requests are made and avoid invoking it in the middle of any requests.
4386+
4387+ See [Built-in Proxy Support][] for details on proxy URL formats and ` NO_PROXY `
4388+ syntax.
4389+
43644390## Class: ` WebSocket `
43654391
43664392<!-- YAML
@@ -4383,6 +4409,9 @@ added:
43834409When Node.js creates the global agent, if the ` NODE_USE_ENV_PROXY ` environment variable is
43844410set to ` 1 ` or ` -- use- env- proxy` is enabled, the global agent will be constructed
43854411with ` proxyEnv: process .env ` , enabling proxy support based on the environment variables.
4412+
4413+ To enable proxy support dynamically and globally, use [` http .setGlobalProxyFromEnv ()` ][].
4414+
43864415Custom agents can also be created with proxy support by passing a
43874416` proxyEnv` option when constructing the agent. The value can be ` process .env `
43884417if they just want to inherit the configuration from the environment variables,
@@ -4438,6 +4467,86 @@ Or the `--use-env-proxy` flag.
44384467HTTP_PROXY=http://proxy.example.com:8080 NO_PROXY=localhost,127.0.0.1 node --use-env-proxy client.js
44394468` ` `
44404469
4470+ To enable proxy support dynamically and globally with ` process.env` (the default option of ` http.setGlobalProxyFromEnv()` ):
4471+
4472+ ` ` ` cjs
4473+ const http = require('node:http');
4474+
4475+ // Reads proxy-related environment variables from process.env
4476+ const restore = http.setGlobalProxyFromEnv();
4477+
4478+ // Subsequent requests will use the configured proxies from environment variables
4479+ http.get('http://www.example.com', (res) => {
4480+ // This request will be proxied if HTTP_PROXY or http_proxy is set
4481+ });
4482+
4483+ fetch('https://www.example.com', (res) => {
4484+ // This request will be proxied if HTTPS_PROXY or https_proxy is set
4485+ });
4486+
4487+ // To restore the original global agent and dispatcher settings, call the returned function.
4488+ // restore();
4489+ ` ` `
4490+
4491+ ` ` ` mjs
4492+ import http from 'node:http';
4493+
4494+ // Reads proxy-related environment variables from process.env
4495+ http.setGlobalProxyFromEnv();
4496+
4497+ // Subsequent requests will use the configured proxies from environment variables
4498+ http.get('http://www.example.com', (res) => {
4499+ // This request will be proxied if HTTP_PROXY or http_proxy is set
4500+ });
4501+
4502+ fetch('https://www.example.com', (res) => {
4503+ // This request will be proxied if HTTPS_PROXY or https_proxy is set
4504+ });
4505+
4506+ // To restore the original global agent and dispatcher settings, call the returned function.
4507+ // restore();
4508+ ` ` `
4509+
4510+ To enable proxy support dynamically and globally with custom settings:
4511+
4512+ ` ` ` cjs
4513+ const http = require('node:http');
4514+
4515+ const restore = http.setGlobalProxyFromEnv({
4516+ http_proxy: 'http://proxy.example.com:8080',
4517+ https_proxy: 'https://proxy.example.com:8443',
4518+ no_proxy: 'localhost,127.0.0.1,.internal.example.com',
4519+ });
4520+
4521+ // Subsequent requests will use the configured proxies
4522+ http.get('http://www.example.com', (res) => {
4523+ // This request will be proxied through proxy.example.com:8080
4524+ });
4525+
4526+ fetch('https://www.example.com', (res) => {
4527+ // This request will be proxied through proxy.example.com:8443
4528+ });
4529+ ` ` `
4530+
4531+ ` ` ` mjs
4532+ import http from 'node:http';
4533+
4534+ http.setGlobalProxyFromEnv({
4535+ http_proxy: 'http://proxy.example.com:8080',
4536+ https_proxy: 'https://proxy.example.com:8443',
4537+ no_proxy: 'localhost,127.0.0.1,.internal.example.com',
4538+ });
4539+
4540+ // Subsequent requests will use the configured proxies
4541+ http.get('http://www.example.com', (res) => {
4542+ // This request will be proxied through proxy.example.com:8080
4543+ });
4544+
4545+ fetch('https://www.example.com', (res) => {
4546+ // This request will be proxied through proxy.example.com:8443
4547+ });
4548+ ` ` `
4549+
44414550To create a custom agent with built- in proxy support:
44424551
44434552` ` ` cjs
@@ -4501,6 +4610,7 @@ const agent2 = new http.Agent({ proxyEnv: process.env });
45014610[` http.get()` ]: #httpgetoptions- callback
45024611[` http.globalAgent` ]: #httpglobalagent
45034612[` http.request()` ]: #httprequestoptions- callback
4613+ [` http.setGlobalProxyFromEnv()` ]: #httpsetglobalproxyfromenvproxyenv
45044614[` message.headers` ]: #messageheaders
45054615[` message.rawHeaders` ]: #messagerawheaders
45064616[` message.socket` ]: #messagesocket
0 commit comments