From 25a8fce1031fdd17be333de7ccc612019e0156fd Mon Sep 17 00:00:00 2001 From: Joshua Mouch Date: Tue, 18 Aug 2026 17:14:29 -0400 Subject: [PATCH] Replace deprecated URL parser --- src/index.ts | 7 +++-- tests/src/resolveProxyByURL.test.ts | 41 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index d018f54..3f19c01 100644 --- a/src/index.ts +++ b/src/index.ts @@ -242,9 +242,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(); @@ -304,7 +304,7 @@ 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') { @@ -1464,4 +1464,3 @@ export function toLogString(args: any[]) { })).join(', ')}]`; } - diff --git a/tests/src/resolveProxyByURL.test.ts b/tests/src/resolveProxyByURL.test.ts index 48c07b8..25a71ea 100644 --- a/tests/src/resolveProxyByURL.test.ts +++ b/tests/src/resolveProxyByURL.test.ts @@ -1,4 +1,5 @@ import * as assert from 'assert'; +import { spawnSync } from 'child_process'; import { createProxyResolver, LogLevel, ProxyAgentParams } from '../../src'; function createParams(overrides: Partial): ProxyAgentParams { @@ -24,6 +25,46 @@ function createParams(overrides: Partial): ProxyAgentParams { } describe('resolveProxyByURL', function () { + it('does not emit the legacy URL parser deprecation', async function () { + if (process.env['VSCODE_PROXY_AGENT_DEPRECATION_CHILD'] !== '1') { + const result = spawnSync(process.execPath, [ + '--pending-deprecation', + require.resolve('mocha/bin/mocha'), + '--exit', + '-r', + 'ts-node/register', + __filename, + '--grep', + 'legacy URL parser', + ], { + encoding: 'utf8', + env: { + ...process.env, + TS_NODE_COMPILER_OPTIONS: JSON.stringify({ types: ['node', 'mocha'] }), + VSCODE_PROXY_AGENT_DEPRECATION_CHILD: '1', + }, + }); + assert.strictEqual(result.status, 0, result.stdout + result.stderr); + return; + } + + const warnings: Error[] = []; + const onWarning = (warning: Error & { code?: string }) => { + if (warning.code === 'DEP0169') { + warnings.push(warning); + } + }; + process.on('warning', onWarning); + try { + const { resolveProxyByURL } = createProxyResolver(createParams({})); + await resolveProxyByURL('https://example.com/path?query=value#fragment'); + await new Promise(resolve => setImmediate(resolve)); + } finally { + process.off('warning', onWarning); + } + assert.deepStrictEqual(warnings, []); + }); + it('reports localhost as a direct connection', async function () { const { resolveProxyByURL } = createProxyResolver(createParams({})); assert.deepStrictEqual(await resolveProxyByURL('http://localhost:3000/'), {