forked from nodejs/undici
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
435 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Class: ProxyAgent | ||
|
||
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: | ||
|
||
* **options** `ProxyAgentOptions` (required) - It extends the `Agent` options. | ||
|
||
Returns: `ProxyAgent` | ||
|
||
### Parameter: `ProxyAgentOptions` | ||
|
||
Extends: [`AgentOptions`](docs/api/Agent.md#parameter-agentoptions) | ||
|
||
* **uri** `string` (required) - It can be passed either by a string or a object containing `uri` as string. | ||
|
||
Examples: | ||
|
||
```js | ||
import { ProxyAgent } from 'undici' | ||
|
||
const proxyAgent = new ProxyAgent('my.proxy.server') | ||
// or | ||
const proxyAgent = new ProxyAgent({ uri: 'my.proxy.server' }) | ||
``` | ||
|
||
#### Example - Basic ProxyAgent instantiation | ||
|
||
This will instantiate the ProxyAgent. It will not do anything until registered as the agent to use with requests. | ||
|
||
```js | ||
import { ProxyAgent } from 'undici' | ||
|
||
const proxyAgent = new ProxyAgent('my.proxy.server') | ||
``` | ||
|
||
#### Example - Basic Proxy Request with global agent dispatcher | ||
|
||
```js | ||
import { setGlobalDispatcher, request, ProxyAgent } from 'undici' | ||
|
||
const proxyAgent = new ProxyAgent('my.proxy.server') | ||
setGlobalDispatcher(proxyAgent) | ||
|
||
const { statusCode, body } = await request('https://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 | ||
} | ||
``` | ||
|
||
#### Example - Basic Proxy Request with local agent dispatcher | ||
|
||
```js | ||
import { ProxyAgent, request } from 'undici' | ||
|
||
const proxyAgent = new ProxyAgent('my.proxy.server') | ||
|
||
const { | ||
statusCode, | ||
body | ||
} = await request('https://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 | ||
} | ||
``` | ||
|
||
### `ProxyAgent.close()` | ||
|
||
Closes the proxy agent and waits for registered pools and clients to also close before resolving. | ||
|
||
Returns: `Promise<void>` | ||
|
||
#### Example - clean up after tests are complete | ||
|
||
```js | ||
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)`](docs/api/Agent.md#parameter-agentdispatchoptions). | ||
|
||
### `ProxyAgent.request(options[, callback])` | ||
|
||
See [`Dispatcher.request(options [, callback])`](docs/api/Dispatcher.md#clientrequestoptions--callback). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
const { request, setGlobalDispatcher, ProxyAgent } = require('../') | ||
|
||
setGlobalDispatcher(new ProxyAgent('https://localhost:8000/')) | ||
|
||
async function main() { | ||
const { | ||
statusCode, | ||
headers, | ||
trailers, | ||
body | ||
// send the request via the https://localhost:8000/ HTTP proxy | ||
} = await request('https://localhost:3000/undici') | ||
|
||
console.log('response received', statusCode) | ||
console.log('headers', headers) | ||
|
||
for await (const data of body) { | ||
console.log('data', data) | ||
} | ||
|
||
console.log('trailers', trailers) | ||
} | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict' | ||
|
||
const { kClients, kProxy } = require('./core/symbols') | ||
const url = require('url') | ||
const Agent = require('./agent') | ||
const Dispatcher = require('./dispatcher') | ||
const { InvalidArgumentError } = require('./core/errors') | ||
|
||
const kAgent = Symbol('proxy agent') | ||
|
||
class ProxyAgent extends Dispatcher { | ||
constructor (opts) { | ||
super(opts) | ||
this[kProxy] = buildProxyOptions(opts) | ||
|
||
const agent = new Agent(opts) | ||
this[kAgent] = agent | ||
|
||
this[kClients] = agent[kClients] | ||
} | ||
|
||
dispatch (opts, handler) { | ||
const { host } = url.parse(opts.origin) | ||
return this[kAgent].dispatch( | ||
{ | ||
...opts, | ||
origin: this[kProxy].uri, | ||
path: opts.origin + opts.path, | ||
headers: { | ||
...opts.headers, | ||
host | ||
}, | ||
}, | ||
handler | ||
) | ||
} | ||
|
||
async close () { | ||
await this[kAgent].close() | ||
this[kClients].clear() | ||
} | ||
} | ||
|
||
function buildProxyOptions(opts) { | ||
if (typeof opts === 'string') { | ||
opts = { uri: opts } | ||
} | ||
|
||
if (!opts || !opts.uri) { | ||
throw new InvalidArgumentError('Proxy opts.uri is mandatory') | ||
} | ||
|
||
return { | ||
uri: opts.uri, | ||
protocol: opts.protocol || 'https' | ||
} | ||
} | ||
|
||
module.exports = ProxyAgent |
Oops, something went wrong.