diff --git a/.changeset/cool-insects-retire.md b/.changeset/cool-insects-retire.md new file mode 100644 index 0000000000..1ba55c7192 --- /dev/null +++ b/.changeset/cool-insects-retire.md @@ -0,0 +1,5 @@ +--- +"@pnpm/npm-registry-agent": patch +--- + +Creating a proper URL for socks proxy. diff --git a/packages/npm-registry-agent/src/index.ts b/packages/npm-registry-agent/src/index.ts index a08da02c52..cac8aaefe6 100644 --- a/packages/npm-registry-agent/src/index.ts +++ b/packages/npm-registry-agent/src/index.ts @@ -125,8 +125,8 @@ function getProxyUri ( return null } - if (!proxy.startsWith('http')) { - proxy = protocol + '//' + proxy + if (!proxy.includes('://')) { + proxy = `${protocol}//${proxy}` } const parsedProxy = (typeof proxy === 'string') ? new URL(proxy) : proxy diff --git a/packages/npm-registry-agent/test/index.ts b/packages/npm-registry-agent/test/index.ts index afc87033ae..0f4dc22199 100644 --- a/packages/npm-registry-agent/test/index.ts +++ b/packages/npm-registry-agent/test/index.ts @@ -1,5 +1,6 @@ /// -import agent from '@pnpm/npm-registry-agent' +import getAgent from '@pnpm/npm-registry-agent' +import SocksProxyAgent from 'socks-proxy-agent' jest.mock('agentkeepalive', () => { const MockHttp = mockHttpAgent('http') @@ -26,7 +27,7 @@ const OPTS = { } test('all expected options passed down to HttpAgent', () => { - expect(agent('http://foo.com/bar', OPTS)).toEqual({ + expect(getAgent('http://foo.com/bar', OPTS)).toEqual({ __type: 'http', localAddress: 'localAddress', maxSockets: 5, @@ -35,7 +36,7 @@ test('all expected options passed down to HttpAgent', () => { }) test('all expected options passed down to HttpsAgent', () => { - expect(agent('https://foo.com/bar', OPTS)).toEqual({ + expect(getAgent('https://foo.com/bar', OPTS)).toEqual({ __type: 'https', ca: 'ca', cert: 'cert', @@ -48,10 +49,11 @@ test('all expected options passed down to HttpsAgent', () => { }) test('all expected options passed down to proxy agent', () => { - const opts = Object.assign({ + const opts = { httpsProxy: 'https://user:pass@my.proxy:1234/foo', - }, OPTS) - expect(agent('https://foo.com/bar', opts)).toEqual({ + ...OPTS + } + expect(getAgent('https://foo.com/bar', opts)).toEqual({ __type: 'https-proxy', auth: 'user:pass', ca: 'ca', @@ -67,3 +69,17 @@ test('all expected options passed down to proxy agent', () => { timeout: 6, }) }) + +test('a socks proxy', () => { + const opts = { + httpsProxy: 'socks://user:pass@my.proxy:1234/foo', + ...OPTS, + } + const agent = getAgent('https://foo.com/bar', opts) + expect(agent instanceof SocksProxyAgent).toBeTruthy() + expect(agent.proxy).toEqual({ + host: 'my.proxy', + port: 1234, + type: 5, + }) +})