• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Class: ProxyAgent
2
3Extends: `undici.Dispatcher`
4
5A Proxy Agent class that implements the Agent API. It allows the connection through proxy in a simple way.
6
7## `new ProxyAgent([options])`
8
9Arguments:
10
11* **options** `ProxyAgentOptions` (required) - It extends the `Agent` options.
12
13Returns: `ProxyAgent`
14
15### Parameter: `ProxyAgentOptions`
16
17Extends: [`AgentOptions`](Agent.md#parameter-agentoptions)
18
19* **uri** `string` (required) - It can be passed either by a string or a object containing `uri` as string.
20* **token** `string` (optional) - It can be passed by a string of token for authentication.
21* **auth** `string` (**deprecated**) - Use token.
22* **clientFactory** `(origin: URL, opts: Object) => Dispatcher` - Default: `(origin, opts) => new Pool(origin, opts)`
23
24Examples:
25
26```js
27import { ProxyAgent } from 'undici'
28
29const proxyAgent = new ProxyAgent('my.proxy.server')
30// or
31const proxyAgent = new ProxyAgent({ uri: 'my.proxy.server' })
32```
33
34#### Example - Basic ProxyAgent instantiation
35
36This will instantiate the ProxyAgent. It will not do anything until registered as the agent to use with requests.
37
38```js
39import { ProxyAgent } from 'undici'
40
41const proxyAgent = new ProxyAgent('my.proxy.server')
42```
43
44#### Example - Basic Proxy Request with global agent dispatcher
45
46```js
47import { setGlobalDispatcher, request, ProxyAgent } from 'undici'
48
49const proxyAgent = new ProxyAgent('my.proxy.server')
50setGlobalDispatcher(proxyAgent)
51
52const { statusCode, body } = await request('http://localhost:3000/foo')
53
54console.log('response received', statusCode) // response received 200
55
56for await (const data of body) {
57  console.log('data', data.toString('utf8')) // data foo
58}
59```
60
61#### Example - Basic Proxy Request with local agent dispatcher
62
63```js
64import { ProxyAgent, request } from 'undici'
65
66const proxyAgent = new ProxyAgent('my.proxy.server')
67
68const {
69  statusCode,
70  body
71} = await request('http://localhost:3000/foo', { dispatcher: proxyAgent })
72
73console.log('response received', statusCode) // response received 200
74
75for await (const data of body) {
76  console.log('data', data.toString('utf8')) // data foo
77}
78```
79
80#### Example - Basic Proxy Request with authentication
81
82```js
83import { setGlobalDispatcher, request, ProxyAgent } from 'undici';
84
85const proxyAgent = new ProxyAgent({
86  uri: 'my.proxy.server',
87  // token: 'Bearer xxxx'
88  token: `Basic ${Buffer.from('username:password').toString('base64')}`
89});
90setGlobalDispatcher(proxyAgent);
91
92const { statusCode, body } = await request('http://localhost:3000/foo');
93
94console.log('response received', statusCode); // response received 200
95
96for await (const data of body) {
97  console.log('data', data.toString('utf8')); // data foo
98}
99```
100
101### `ProxyAgent.close()`
102
103Closes the proxy agent and waits for registered pools and clients to also close before resolving.
104
105Returns: `Promise<void>`
106
107#### Example - clean up after tests are complete
108
109```js
110import { ProxyAgent, setGlobalDispatcher } from 'undici'
111
112const proxyAgent = new ProxyAgent('my.proxy.server')
113setGlobalDispatcher(proxyAgent)
114
115await proxyAgent.close()
116```
117
118### `ProxyAgent.dispatch(options, handlers)`
119
120Implements [`Agent.dispatch(options, handlers)`](Agent.md#parameter-agentdispatchoptions).
121
122### `ProxyAgent.request(options[, callback])`
123
124See [`Dispatcher.request(options [, callback])`](Dispatcher.md#dispatcherrequestoptions-callback).
125