Extends: undici.Dispatcher
A Proxy Agent class that implements the Agent API. It allows the connection through proxy in a simple way.
new ProxyAgent([options])
Arguments:
ProxyAgentOptions
(required) - It extends the Agent
options.Returns: ProxyAgent
ProxyAgentOptions
Extends: AgentOptions
string
(required) - It can be passed either by a string or a object containing uri
as string.string
(optional) - It can be passed by a string of token for authentication.string
(deprecated) - Use token.(origin: URL, opts: Object) => Dispatcher
(optional) - Default: (origin, opts) => new Pool(origin, opts)
BuildOptions
(optional) - Options object passed when creating the underlying socket via the connector builder for the request. See TLS.BuildOptions
(optional) - Options object passed when creating the underlying socket via the connector builder for the proxy server. See TLS.Examples:
import { ProxyAgent } from 'undici'
const proxyAgent = new ProxyAgent('my.proxy.server')
// or
const proxyAgent = new ProxyAgent({ uri: 'my.proxy.server' })
This will instantiate the ProxyAgent. It will not do anything until registered as the agent to use with requests.
import { ProxyAgent } from 'undici'
const proxyAgent = new ProxyAgent('my.proxy.server')
import { setGlobalDispatcher, request, ProxyAgent } from 'undici'
const proxyAgent = new ProxyAgent('my.proxy.server')
setGlobalDispatcher(proxyAgent)
const { statusCode, body } = await request('http://localhost:3000/foo')
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data.toString('utf8')) // data foo
}
import { ProxyAgent, request } from 'undici'
const proxyAgent = new ProxyAgent('my.proxy.server')
const {
statusCode,
body
} = await request('http://localhost:3000/foo', { dispatcher: proxyAgent })
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data.toString('utf8')) // data foo
}
import { setGlobalDispatcher, request, ProxyAgent } from 'undici';
const proxyAgent = new ProxyAgent({
uri: 'my.proxy.server',
// token: 'Bearer xxxx'
token: `Basic ${Buffer.from('username:password').toString('base64')}`
});
setGlobalDispatcher(proxyAgent);
const { statusCode, body } = await request('http://localhost:3000/foo');
console.log('response received', statusCode); // response received 200
for await (const data of body) {
console.log('data', data.toString('utf8')); // data foo
}
ProxyAgent.close()
Closes the proxy agent and waits for registered pools and clients to also close before resolving.
Returns: Promise<void>
import { ProxyAgent, setGlobalDispatcher } from 'undici'
const proxyAgent = new ProxyAgent('my.proxy.server')
setGlobalDispatcher(proxyAgent)
await proxyAgent.close()
ProxyAgent.dispatch(options, handlers)
Implements Agent.dispatch(options, handlers)
.
ProxyAgent.request(options[, callback])