|
| 1 | +# Class: EnvHttpProxyAgent |
| 2 | + |
| 3 | +Stability: Experimental. |
| 4 | + |
| 5 | +Extends: `undici.Dispatcher` |
| 6 | + |
| 7 | +EnvHttpProxyAgent automatically reads the proxy configuration from the environment variables `http_proxy`, `https_proxy`, and `no_proxy` and sets up the proxy agents accordingly. When `http_proxy` and `https_proxy` are set, `http_proxy` is used for HTTP requests and `https_proxy` is used for HTTPS requests. If only `http_proxy` is set, `http_proxy` is used for both HTTP and HTTPS requests. If only `https_proxy` is set, it is only used for HTTPS requests. |
| 8 | + |
| 9 | +`no_proxy` is a comma or space-separated list of hostnames that should not be proxied. The list may contain leading wildcard characters (`*`). If `no_proxy` is set, the EnvHttpProxyAgent will bypass the proxy for requests to hosts that match the list. If `no_proxy` is set to `"*"`, the EnvHttpProxyAgent will bypass the proxy for all requests. |
| 10 | + |
| 11 | +Uppercase environment variables are also supported: `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY`. However, if both the lowercase and uppercase environment variables are set, the uppercase environment variables will be ignored. |
| 12 | + |
| 13 | +## `new EnvHttpProxyAgent([options])` |
| 14 | + |
| 15 | +Arguments: |
| 16 | + |
| 17 | +* **options** `EnvHttpProxyAgentOptions` (optional) - extends the `Agent` options. |
| 18 | + |
| 19 | +Returns: `EnvHttpProxyAgent` |
| 20 | + |
| 21 | +### Parameter: `EnvHttpProxyAgentOptions` |
| 22 | + |
| 23 | +Extends: [`AgentOptions`](Agent.md#parameter-agentoptions) |
| 24 | + |
| 25 | +* **httpProxy** `string` (optional) - When set, it will override the `HTTP_PROXY` environment variable. |
| 26 | +* **httpsProxy** `string` (optional) - When set, it will override the `HTTPS_PROXY` environment variable. |
| 27 | +* **noProxy** `string` (optional) - When set, it will override the `NO_PROXY` environment variable. |
| 28 | + |
| 29 | +Examples: |
| 30 | + |
| 31 | +```js |
| 32 | +import { EnvHttpProxyAgent } from 'undici' |
| 33 | + |
| 34 | +const envHttpProxyAgent = new EnvHttpProxyAgent() |
| 35 | +// or |
| 36 | +const envHttpProxyAgent = new EnvHttpProxyAgent({ httpProxy: 'my.proxy.server:8080', httpsProxy: 'my.proxy.server:8443', noProxy: 'localhost' }) |
| 37 | +``` |
| 38 | + |
| 39 | +#### Example - EnvHttpProxyAgent instantiation |
| 40 | + |
| 41 | +This will instantiate the EnvHttpProxyAgent. It will not do anything until registered as the agent to use with requests. |
| 42 | + |
| 43 | +```js |
| 44 | +import { EnvHttpProxyAgent } from 'undici' |
| 45 | + |
| 46 | +const envHttpProxyAgent = new EnvHttpProxyAgent() |
| 47 | +``` |
| 48 | + |
| 49 | +#### Example - Basic Proxy Fetch with global agent dispatcher |
| 50 | + |
| 51 | +```js |
| 52 | +import { setGlobalDispatcher, fetch, EnvHttpProxyAgent } from 'undici' |
| 53 | + |
| 54 | +const envHttpProxyAgent = new EnvHttpProxyAgent() |
| 55 | +setGlobalDispatcher(envHttpProxyAgent) |
| 56 | + |
| 57 | +const { status, json } = await fetch('http://localhost:3000/foo') |
| 58 | + |
| 59 | +console.log('response received', status) // response received 200 |
| 60 | + |
| 61 | +const data = await json() // data { foo: "bar" } |
| 62 | +``` |
| 63 | + |
| 64 | +#### Example - Basic Proxy Request with global agent dispatcher |
| 65 | + |
| 66 | +```js |
| 67 | +import { setGlobalDispatcher, request, EnvHttpProxyAgent } from 'undici' |
| 68 | + |
| 69 | +const envHttpProxyAgent = new EnvHttpProxyAgent() |
| 70 | +setGlobalDispatcher(envHttpProxyAgent) |
| 71 | + |
| 72 | +const { statusCode, body } = await request('http://localhost:3000/foo') |
| 73 | + |
| 74 | +console.log('response received', statusCode) // response received 200 |
| 75 | + |
| 76 | +for await (const data of body) { |
| 77 | + console.log('data', data.toString('utf8')) // data foo |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +#### Example - Basic Proxy Request with local agent dispatcher |
| 82 | + |
| 83 | +```js |
| 84 | +import { EnvHttpProxyAgent, request } from 'undici' |
| 85 | + |
| 86 | +const envHttpProxyAgent = new EnvHttpProxyAgent() |
| 87 | + |
| 88 | +const { |
| 89 | + statusCode, |
| 90 | + body |
| 91 | +} = await request('http://localhost:3000/foo', { dispatcher: envHttpProxyAgent }) |
| 92 | + |
| 93 | +console.log('response received', statusCode) // response received 200 |
| 94 | + |
| 95 | +for await (const data of body) { |
| 96 | + console.log('data', data.toString('utf8')) // data foo |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +#### Example - Basic Proxy Fetch with local agent dispatcher |
| 101 | + |
| 102 | +```js |
| 103 | +import { EnvHttpProxyAgent, fetch } from 'undici' |
| 104 | + |
| 105 | +const envHttpProxyAgent = new EnvHttpProxyAgent() |
| 106 | + |
| 107 | +const { |
| 108 | + status, |
| 109 | + json |
| 110 | +} = await fetch('http://localhost:3000/foo', { dispatcher: envHttpProxyAgent }) |
| 111 | + |
| 112 | +console.log('response received', status) // response received 200 |
| 113 | + |
| 114 | +const data = await json() // data { foo: "bar" } |
| 115 | +``` |
| 116 | + |
| 117 | +## Instance Methods |
| 118 | + |
| 119 | +### `EnvHttpProxyAgent.close([callback])` |
| 120 | + |
| 121 | +Implements [`Dispatcher.close([callback])`](Dispatcher.md#dispatcherclosecallback-promise). |
| 122 | + |
| 123 | +### `EnvHttpProxyAgent.destroy([error, callback])` |
| 124 | + |
| 125 | +Implements [`Dispatcher.destroy([error, callback])`](Dispatcher.md#dispatcherdestroyerror-callback-promise). |
| 126 | + |
| 127 | +### `EnvHttpProxyAgent.dispatch(options, handler: AgentDispatchOptions)` |
| 128 | + |
| 129 | +Implements [`Dispatcher.dispatch(options, handler)`](Dispatcher.md#dispatcherdispatchoptions-handler). |
| 130 | + |
| 131 | +#### Parameter: `AgentDispatchOptions` |
| 132 | + |
| 133 | +Extends: [`DispatchOptions`](Dispatcher.md#parameter-dispatchoptions) |
| 134 | + |
| 135 | +* **origin** `string | URL` |
| 136 | +* **maxRedirections** `Integer`. |
| 137 | + |
| 138 | +Implements [`Dispatcher.destroy([error, callback])`](Dispatcher.md#dispatcherdestroyerror-callback-promise). |
| 139 | + |
| 140 | +### `EnvHttpProxyAgent.connect(options[, callback])` |
| 141 | + |
| 142 | +See [`Dispatcher.connect(options[, callback])`](Dispatcher.md#dispatcherconnectoptions-callback). |
| 143 | + |
| 144 | +### `EnvHttpProxyAgent.dispatch(options, handler)` |
| 145 | + |
| 146 | +Implements [`Dispatcher.dispatch(options, handler)`](Dispatcher.md#dispatcherdispatchoptions-handler). |
| 147 | + |
| 148 | +### `EnvHttpProxyAgent.pipeline(options, handler)` |
| 149 | + |
| 150 | +See [`Dispatcher.pipeline(options, handler)`](Dispatcher.md#dispatcherpipelineoptions-handler). |
| 151 | + |
| 152 | +### `EnvHttpProxyAgent.request(options[, callback])` |
| 153 | + |
| 154 | +See [`Dispatcher.request(options [, callback])`](Dispatcher.md#dispatcherrequestoptions-callback). |
| 155 | + |
| 156 | +### `EnvHttpProxyAgent.stream(options, factory[, callback])` |
| 157 | + |
| 158 | +See [`Dispatcher.stream(options, factory[, callback])`](Dispatcher.md#dispatcherstreamoptions-factory-callback). |
| 159 | + |
| 160 | +### `EnvHttpProxyAgent.upgrade(options[, callback])` |
| 161 | + |
| 162 | +See [`Dispatcher.upgrade(options[, callback])`](Dispatcher.md#dispatcherupgradeoptions-callback). |
0 commit comments