|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const { kProxy, kClose, kDestroy } = require('./core/symbols') |
| 3 | +const { kProxy, kClose, kDestroy } = require('../core/symbols') |
4 | 4 | const { URL } = require('node:url') |
5 | | -const Agent = require('./agent') |
6 | | -const Pool = require('./pool') |
7 | | -const DispatcherBase = require('./dispatcher-base') |
8 | | -const { InvalidArgumentError, RequestAbortedError } = require('./core/errors') |
9 | | -const buildConnector = require('./core/connect') |
| 5 | +const Agent = require('../agent') |
| 6 | +const Pool = require('../pool') |
| 7 | +const DispatcherBase = require('../dispatcher-base') |
| 8 | +const { InvalidArgumentError, RequestAbortedError } = require('../core/errors') |
| 9 | +const buildConnector = require('../core/connect') |
10 | 10 |
|
11 | 11 | const kAgent = Symbol('proxy agent') |
12 | 12 | const kClient = Symbol('proxy client') |
@@ -39,10 +39,10 @@ function defaultFactory (origin, opts) { |
39 | 39 | } |
40 | 40 |
|
41 | 41 | class ProxyAgent extends DispatcherBase { |
42 | | - constructor (opts) { |
| 42 | + constructor (dispatcher, opts) { |
43 | 43 | super(opts) |
44 | 44 | this[kProxy] = buildProxyOptions(opts) |
45 | | - this[kAgent] = new Agent(opts) |
| 45 | + this[kAgent] = dispatcher |
46 | 46 |
|
47 | 47 | if (typeof opts === 'string') { |
48 | 48 | opts = { uri: opts } |
@@ -183,4 +183,24 @@ function throwIfProxyAuthIsSent (headers) { |
183 | 183 | } |
184 | 184 | } |
185 | 185 |
|
186 | | -module.exports = ProxyAgent |
| 186 | +module.exports = (opts) => { |
| 187 | + if (typeof opts === 'string') { |
| 188 | + opts = { uri: opts } |
| 189 | + } |
| 190 | + |
| 191 | + if (!opts || !opts.uri) { |
| 192 | + throw new InvalidArgumentError('Proxy opts.uri is mandatory') |
| 193 | + } |
| 194 | + |
| 195 | + const { clientFactory = defaultFactory } = opts |
| 196 | + |
| 197 | + if (typeof clientFactory !== 'function') { |
| 198 | + throw new InvalidArgumentError('Proxy opts.clientFactory must be a function.') |
| 199 | + } |
| 200 | + |
| 201 | + if (opts.auth && opts.token) { |
| 202 | + throw new InvalidArgumentError('opts.auth cannot be used in combination with opts.token') |
| 203 | + } |
| 204 | + |
| 205 | + return (dispatcher) => new ProxyAgent(dispatcher, opts) |
| 206 | +} |
0 commit comments