diff --git a/get-json.js b/get-json.js new file mode 100644 index 0000000..60d1a69 --- /dev/null +++ b/get-json.js @@ -0,0 +1,71 @@ +'use strict'; + +const { HttpProxyAgent } = require('http-proxy-agent'); +const { HttpsProxyAgent } = require('https-proxy-agent'); +const https = require('https'); + +// eslint-disable-next-line no-underscore-dangle +const _getJSON = (url, callback) => { + let agent; + if (new URL(url).protocol === 'http:' && process.env.HTTP_PROXY) { + agent = new HttpProxyAgent(process.env.HTTP_PROXY); + } else if (new URL(url).protocol === 'http:' && process.env.http_proxy) { + agent = new HttpProxyAgent(process.env.http_proxy); + } else if (new URL(url).protocol === 'https:' && process.env.HTTPS_PROXY) { + agent = new HttpsProxyAgent(process.env.HTTPS_PROXY); + } else if (new URL(url).protocol === 'https:' && process.env.https_proxy) { + agent = new HttpsProxyAgent(process.env.https_proxy); + } + https.get(url, { agent }, (response) => { + let data = ''; + + // Handle incoming data + response.on('data', (chunk) => { + data += chunk; + }); + + // Handle end of the response + response.on('end', () => { + let body; + try { + body = JSON.parse(data); + } catch (parseError) { + callback(`Parse error: ${parseError}`); + return; + } + + // Check response status code + // eslint-disable-next-line no-magic-numbers + if (response.statusCode !== 200) { + callback('Unexpected response code.'); + return; + } + + callback(null, body); + }); + + }).on('error', (error) => { + callback(error); + }); +}; + +const getJSON = (url, callback) => { + // eslint-disable-next-line no-underscore-dangle + let _callback = callback; + if (!_callback) { + _callback = () => { }; + } + return new Promise((resolve, reject) => { + _getJSON(url, (error, body) => { + if (error) { + reject(error); + _callback(error); + return; + } + resolve(body); + _callback(null, body); + }); + }); +}; + +module.exports = getJSON; diff --git a/get-node-versions.js b/get-node-versions.js index aa93cee..7f67246 100644 --- a/get-node-versions.js +++ b/get-node-versions.js @@ -1,6 +1,6 @@ 'use strict'; -const getJSON = require('get-json'); +const getJSON = require('./get-json'); const semver = require('semver'); module.exports = async function getNodeVersions() { diff --git a/package.json b/package.json index 172ad86..5b0da3d 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,8 @@ "colors": "=1.4.0", "fast_array_intersect": "^1.1.0", "get-dep-tree": "^1.0.4", - "get-json": "^1.0.1", + "http-proxy-agent": "^5.0.0", + "https-proxy-agent": "^5.0.1", "json-file-plus": "^3.3.1", "lockfile-info": "^1.0.0", "object.fromentries": "^2.0.7", diff --git a/test/mock.js b/test/mock.js index 05deb39..3ef3e40 100644 --- a/test/mock.js +++ b/test/mock.js @@ -6,11 +6,11 @@ function mockModule(resolvedSpecifier, replacement) { Module.exports = replacement; } -const origGetJSON = require('get-json'); +const origGetJSON = require('../get-json'); const mockedNodeVersions = require('./mocks/node-versions.json'); mockModule( - require.resolve('get-json'), + require.resolve('../get-json'), async function getJSON(url) { if (url === 'https://nodejs.org/dist/index.json') { return mockedNodeVersions; diff --git a/test/mocks/index.js b/test/mocks/index.js index 4a5da95..bc67a04 100644 --- a/test/mocks/index.js +++ b/test/mocks/index.js @@ -2,9 +2,9 @@ const Module = require('module'); -const getJSON = require('get-json'); +const getJSON = require('../../get-json'); -const id = require.resolve('get-json'); +const id = require.resolve('../../get-json'); const mod = new Module(id); mod.exports = (url, ...args) => { if (url === 'http://nodejs.org/dist/index.json') {