Skip to content

Commit 4f8f814

Browse files
authored
perf(proxy-agent): avoid extra header allocations in auth guard (#5164)
Assisted-by: openai:gpt-5.5 Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
1 parent 6929aa5 commit 4f8f814

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

lib/dispatcher/proxy-agent.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const kProxyTls = Symbol('proxy tls settings')
1818
const kConnectEndpoint = Symbol('connect endpoint function')
1919
const kConnectEndpointHTTP1 = Symbol('connect endpoint function (http/1.1 only)')
2020
const kTunnelProxy = Symbol('tunnel proxy')
21+
const proxyAuthorization = 'proxy-authorization'
2122

2223
function defaultProtocolPort (protocol) {
2324
return protocol === 'https:' ? 443 : 80
@@ -298,6 +299,10 @@ function buildHeaders (headers) {
298299
const headersPair = {}
299300

300301
for (let i = 0; i < headers.length; i += 2) {
302+
if (isProxyAuthorizationHeader(headers[i])) {
303+
throwProxyAuthError()
304+
}
305+
301306
headersPair[headers[i]] = headers[i + 1]
302307
}
303308

@@ -316,11 +321,23 @@ function buildHeaders (headers) {
316321
* It should be removed in the next major version for performance reasons
317322
*/
318323
function throwIfProxyAuthIsSent (headers) {
319-
const existProxyAuth = headers && Object.keys(headers)
320-
.find((key) => key.toLowerCase() === 'proxy-authorization')
321-
if (existProxyAuth) {
322-
throw new InvalidArgumentError('Proxy-Authorization should be sent in ProxyAgent constructor')
324+
for (const key in headers) {
325+
if (isProxyAuthorizationHeader(key)) {
326+
throwProxyAuthError()
327+
}
323328
}
324329
}
325330

331+
/**
332+
* @param {string} key
333+
* @returns {boolean}
334+
*/
335+
function isProxyAuthorizationHeader (key) {
336+
return key.length === proxyAuthorization.length && key.toLowerCase() === proxyAuthorization
337+
}
338+
339+
function throwProxyAuthError () {
340+
throw new InvalidArgumentError('Proxy-Authorization should be sent in ProxyAgent constructor')
341+
}
342+
326343
module.exports = ProxyAgent

0 commit comments

Comments
 (0)