Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions lib/dispatcher/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,17 @@ class Agent extends DispatcherBase {
}

[kDispatch] (opts, handler) {
let key
let origin
if (opts.origin && (typeof opts.origin === 'string' || opts.origin instanceof URL)) {
key = String(opts.origin)
origin = String(opts.origin)
} else {
throw new InvalidArgumentError('opts.origin must be a non-empty string or URL.')
}

if (this[kOrigins].size >= this[kOptions].maxOrigins && !this[kOrigins].has(key)) {
const allowH2 = opts.allowH2 ?? this[kOptions].allowH2
const key = allowH2 === false ? `${origin}#http1-only` : origin

if (this[kOrigins].size >= this[kOptions].maxOrigins && !this[kOrigins].has(origin)) {
throw new MaxOriginsReachedError()
}

Expand All @@ -96,10 +99,23 @@ class Agent extends DispatcherBase {
result.dispatcher.close()
}
}
this[kOrigins].delete(key)

let hasOrigin = false
for (const entry of this[kClients].values()) {
if (entry.origin === origin) {
hasOrigin = true
break
}
}

if (!hasOrigin) {
this[kOrigins].delete(origin)
}
}
}
dispatcher = this[kFactory](opts.origin, this[kOptions])
dispatcher = this[kFactory](opts.origin, allowH2 === false
? { ...this[kOptions], allowH2: false }
: this[kOptions])
.on('drain', this[kOnDrain])
.on('connect', (origin, targets) => {
const result = this[kClients].get(key)
Expand All @@ -117,8 +133,8 @@ class Agent extends DispatcherBase {
this[kOnConnectionError](origin, targets, err)
})

this[kClients].set(key, { count: 0, dispatcher })
this[kOrigins].add(key)
this[kClients].set(key, { count: 0, dispatcher, origin })
this[kOrigins].add(origin)
}

return dispatcher.dispatch(opts, handler)
Expand Down
8 changes: 6 additions & 2 deletions lib/dispatcher/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,13 @@ class Client extends DispatcherBase {
...(typeof autoSelectFamily === 'boolean' ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
...connect
})
} else if (socketPath != null) {
} else {
const customConnect = connect
connect = (opts, callback) => customConnect({ ...opts, socketPath }, callback)
connect = (opts, callback) => customConnect({
...opts,
...(socketPath != null ? { socketPath } : null),
...(allowH2 != null ? { allowH2 } : null)
}, callback)
}

this[kUrl] = util.parseOrigin(url)
Expand Down
8 changes: 7 additions & 1 deletion lib/dispatcher/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const kProxyHeaders = Symbol('proxy headers')
const kRequestTls = Symbol('request tls settings')
const kProxyTls = Symbol('proxy tls settings')
const kConnectEndpoint = Symbol('connect endpoint function')
const kConnectEndpointHTTP1 = Symbol('connect endpoint function (http/1.1 only)')
const kTunnelProxy = Symbol('tunnel proxy')

function defaultProtocolPort (protocol) {
Expand Down Expand Up @@ -129,6 +130,7 @@ class ProxyAgent extends DispatcherBase {

const connect = buildConnector({ ...opts.proxyTls })
this[kConnectEndpoint] = buildConnector({ ...opts.requestTls })
this[kConnectEndpointHTTP1] = buildConnector({ ...opts.requestTls, allowH2: false })

const agentFactory = opts.factory || defaultAgentFactory
const factory = (origin, options) => {
Expand Down Expand Up @@ -216,7 +218,11 @@ class ProxyAgent extends DispatcherBase {
} else {
servername = opts.servername
}
this[kConnectEndpoint]({ ...opts, servername, httpSocket: socket }, callback)
const connectEndpoint = opts.allowH2 === false
? this[kConnectEndpointHTTP1]
: this[kConnectEndpoint]

connectEndpoint({ ...opts, servername, httpSocket: socket }, callback)
} catch (err) {
if (err.code === 'ERR_TLS_CERT_ALTNAME_INVALID') {
// Throw a custom error to avoid loop in client.js#connect
Expand Down
Loading
Loading