From eaa2cdba9d809de6a82959a620f2e21a361e48f9 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:22:47 -0800 Subject: [PATCH] Replace deprecated url.parse in proxy resolution Co-authored-by: Joshua Mouch <3098820+joshmouch@users.noreply.github.com> --- src/index.ts | 10 +++---- tests/src/resolveProxyByURL.test.ts | 46 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index daac260..1b39faa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -284,9 +284,9 @@ export function createProxyResolver(params: ProxyAgentParams) { } } - function getCacheKey(url: nodeurl.UrlWithStringQuery) { + function getCacheKey(url: nodeurl.URL) { // Expecting proxies to usually be the same per scheme://host:port. Assuming that for performance. - return nodeurl.format({ ...url, ...{ pathname: undefined, search: undefined, hash: undefined } }); + return `${url.protocol}//${url.host}`; } function getCachedProxy(key: string) { checkAndFlushCacheIfNetworkChanged(); @@ -346,10 +346,10 @@ export function createProxyResolver(params: ProxyAgentParams) { } function useProxySettings(url: string, req: http.ClientRequest | undefined, stackText: string, callback: (proxy: string | undefined, source: ProxyResolveSource) => void) { - const parsedUrl = nodeurl.parse(url); // Coming from Node's URL, sticking with that. + const parsedUrl = new nodeurl.URL(url); - const hostname = parsedUrl.hostname; - if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' || hostname === '::ffff:127.0.0.1') { + const hostname = parsedUrl.hostname.replace(/^\[(.*)\]$/, '$1'); // WHATWG URL keeps the brackets around IPv6 addresses. + if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1' || hostname === '::ffff:7f00:1') { localhostCount++; callback('DIRECT', 'localhost'); log.debug('ProxyResolver#resolveProxy localhost', url, 'DIRECT', stackText); diff --git a/tests/src/resolveProxyByURL.test.ts b/tests/src/resolveProxyByURL.test.ts index 8790fef..ecb41f9 100644 --- a/tests/src/resolveProxyByURL.test.ts +++ b/tests/src/resolveProxyByURL.test.ts @@ -1,4 +1,6 @@ import * as assert from 'assert'; +import { spawnSync } from 'child_process'; +import * as path from 'path'; import { createProxyResolver, getOrLoadAdditionalCertificates, loadSystemCertificates, Log, LogLevel, ProxyAgentParams, resetCaches } from '../../src'; function createParams(overrides: Partial): ProxyAgentParams { @@ -70,6 +72,50 @@ describe('resolveProxyByURL', function () { }); }); + it('reports IPv6 loopback addresses as a direct connection', async function () { + const { resolveProxyByURL } = createProxyResolver(createParams({ + getProxyURL: () => 'http://proxy.example.com:8080', + })); + const direct = { url: undefined, type: 'DIRECT', source: 'localhost', result: 'DIRECT' }; + assert.deepStrictEqual([ + await resolveProxyByURL('http://[::1]:3000/'), + await resolveProxyByURL('http://[::ffff:127.0.0.1]:3000/'), + ], [direct, direct]); + }); + + it('caches system results per scheme, host and port', async function () { + const { resolveProxyByURL } = createProxyResolver(createParams({ + resolveProxy: async url => `PROXY ${new URL(url).port || 'default'}.example.com:8080`, + })); + assert.deepStrictEqual([ + (await resolveProxyByURL('https://example.com/a?q#h')).result, + (await resolveProxyByURL('https://example.com:8443/a')).result, + (await resolveProxyByURL('https://example.com/b')), + ], [ + 'PROXY default.example.com:8080', + 'PROXY 8443.example.com:8080', + { url: 'http://default.example.com:8080', type: 'PROXY', source: 'system_cached', result: 'PROXY default.example.com:8080' }, + ]); + }); + + it('does not emit the url.parse() deprecation warning (DEP0169)', function () { + const script = `require('./src').createProxyResolver({ ${[ + 'getProxyURL: () => undefined', + 'getNoProxyConfig: () => []', + 'addCertificatesV1: () => false', + 'loadSystemCertificatesFromNode: () => false', + 'log: { debug() { }, error() { } }', + 'isUseHostProxyEnabled: () => true', + 'resolveProxy: async () => "DIRECT"', + 'env: {}', + ].join(', ')} }).resolveProxyByURL('https://example.com/');`; + const { status, stderr } = spawnSync(process.execPath, ['--throw-deprecation', '-r', 'ts-node/register', '-e', script], { + cwd: path.join(__dirname, '..', '..'), + encoding: 'utf8', + }); + assert.deepStrictEqual({ status, stderr }, { status: 0, stderr: '' }); + }); + it('reports the http.proxy setting source', async function () { const { resolveProxyByURL } = createProxyResolver(createParams({ getProxyURL: () => 'http://proxy.example.com:8080',