From 2f3b638dca63ab5cabc4592feabeb2b83fd473ae Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 24 Jul 2025 18:40:05 +0200 Subject: [PATCH] perf: use Pool instead of Client in ProxyAgent for HTTP proxy connections MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ProxyClient now uses Pool instead of Client for better connection management - Added connector storage for direct CONNECT method handling when tunneling is disabled - Updated factory function to use Pool for non-tunneling HTTP-to-HTTP connections - Maintains backward compatibility for both tunneling and non-tunneling scenarios - Removes unused imports (kConnector, Client) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude Signed-off-by: Matteo Collina --- lib/dispatcher/proxy-agent.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/dispatcher/proxy-agent.js b/lib/dispatcher/proxy-agent.js index cd9bc45ffad..9eb1ef8f67a 100644 --- a/lib/dispatcher/proxy-agent.js +++ b/lib/dispatcher/proxy-agent.js @@ -1,13 +1,12 @@ 'use strict' -const { kProxy, kClose, kDestroy, kDispatch, kConnector } = require('../core/symbols') +const { kProxy, kClose, kDestroy, kDispatch } = require('../core/symbols') const { URL } = require('node:url') const Agent = require('./agent') const Pool = require('./pool') const DispatcherBase = require('./dispatcher-base') const { InvalidArgumentError, RequestAbortedError, SecureProxyConnectionError } = require('../core/errors') const buildConnector = require('../core/connect') -const Client = require('./client') const kAgent = Symbol('proxy agent') const kClient = Symbol('proxy client') @@ -29,6 +28,7 @@ const noop = () => {} class ProxyClient extends DispatcherBase { #client = null + #connector = null constructor (origin, opts) { if (typeof origin === 'string') { origin = new URL(origin) @@ -40,7 +40,9 @@ class ProxyClient extends DispatcherBase { super() - this.#client = new Client(origin, opts) + // Store the connector for direct socket connections + this.#connector = opts?.connect || buildConnector(opts) + this.#client = new Pool(origin, opts) } async [kClose] () { @@ -54,7 +56,7 @@ class ProxyClient extends DispatcherBase { async [kDispatch] (opts, handler) { const { method, origin } = opts if (method === 'CONNECT') { - this.#client[kConnector]({ + this.#connector({ origin, port: opts.port || defaultProtocolPort(opts.protocol), path: opts.host, @@ -121,7 +123,7 @@ class ProxyAgent extends DispatcherBase { if (origin.protocol === 'http:') { return new ProxyClient(origin, options) } - return new Client(origin, options) + return new Pool(origin, options) } : undefined