Skip to content
Open
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
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -1464,4 +1464,3 @@ export function toLogString(args: any[]) {
})).join(', ')}]`;
}


41 changes: 41 additions & 0 deletions tests/src/resolveProxyByURL.test.ts
Original file line number Diff line number Diff line change
@@ -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>): ProxyAgentParams {
Expand All @@ -24,6 +25,46 @@ function createParams(overrides: Partial<ProxyAgentParams>): 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<void>(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/'), {
Expand Down