From c18517093615a735bcff0449be46145280725f8d Mon Sep 17 00:00:00 2001 From: Unicatevn Date: Wed, 12 Jun 2024 09:42:30 +0700 Subject: [PATCH] fix(cli): installation via authenticated proxy --- cli/npm/install.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/cli/npm/install.js b/cli/npm/install.js index c18ae6b5..f2a4944d 100755 --- a/cli/npm/install.js +++ b/cli/npm/install.js @@ -101,12 +101,23 @@ function get(url, callback) { const requestPort = requestUrl.port || (requestUrl.protocol === 'https:' ? 443 : 80); const proxyUrl = new URL(proxyEnv); const request = proxyUrl.protocol === 'https:' ? https : http; - request.request({ + const requestOption = { host: proxyUrl.hostname, port: proxyUrl.port || (proxyUrl.protocol === 'https:' ? 443 : 80), method: 'CONNECT', path: `${requestUrl.hostname}:${requestPort}`, - }).on('connect', (response, socket, _head) => { + }; + if (proxyUrl.username || proxyUrl.password) { + const auth = `${decodeURIComponent( + proxyUrl.username + )}:${decodeURIComponent(proxyUrl.password)}`; + requestOption.headers = { + 'Proxy-Authorization': `Basic ${Buffer.from( + auth + ).toString('base64')}`, + } + } + request.request(requestOption).on('connect', (response, socket, _head) => { if (response.statusCode !== 200) { // let caller handle error callback(response);