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