Skip to content

Commit

Permalink
fix: Agent pass connect
Browse files Browse the repository at this point in the history
Fixes: #884
  • Loading branch information
ronag committed Jul 18, 2021
1 parent def2b77 commit 104be9c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
12 changes: 10 additions & 2 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,26 @@ function defaultFactory (origin, opts) {
}

class Agent extends Dispatcher {
constructor ({ factory = defaultFactory, maxRedirections = 0, ...options } = {}) {
constructor ({ factory = defaultFactory, maxRedirections = 0, connect, ...options } = {}) {
super()

if (typeof factory !== 'function') {
throw new InvalidArgumentError('factory must be a function.')
}

if (connect != null && typeof connect !== 'function' && typeof connect !== 'object') {
throw new InvalidArgumentError('connect must be a function or an object')
}

if (!Number.isInteger(maxRedirections) || maxRedirections < 0) {
throw new InvalidArgumentError('maxRedirections must be a positive number')
}

this[kOptions] = JSON.parse(JSON.stringify(options))
if (connect && typeof connect !== 'function') {
connect = util.deepClone(connect)
}

this[kOptions] = { ...util.deepClone(options), connect }
this[kMaxRedirections] = maxRedirections
this[kFactory] = factory
this[kClients] = new Map()
Expand Down
5 changes: 5 additions & 0 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ function getServerName (host) {
return servername
}

function deepClone (obj) {
return JSON.parse(JSON.stringify(obj))
}

function isIterable (obj) {
return !!(obj != null && (typeof obj[Symbol.iterator] === 'function' || typeof obj[Symbol.asyncIterator] === 'function'))
}
Expand Down Expand Up @@ -198,5 +202,6 @@ module.exports = {
parseKeepAliveTimeout,
destroy,
bodyLength,
deepClone,
isBuffer
}
4 changes: 2 additions & 2 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Pool extends Dispatcher {
throw new InvalidArgumentError('factory must be a function.')
}

if (connect != null && typeof connect !== 'function') {
if (connect != null && typeof connect !== 'function' && typeof connect !== 'object') {
throw new InvalidArgumentError('connect must be a function or an object')
}

Expand All @@ -53,7 +53,7 @@ class Pool extends Dispatcher {

this[kConnections] = connections || null
this[kUrl] = util.parseOrigin(origin)
this[kOptions] = { ...JSON.parse(JSON.stringify(options)), connect }
this[kOptions] = { ...util.deepClone(options), connect }
this[kQueue] = new FixedQueue()
this[kClosedPromise] = null
this[kClosedResolve] = null
Expand Down

0 comments on commit 104be9c

Please sign in to comment.