Skip to content

Commit

Permalink
Add proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-kinney committed May 31, 2024
1 parent 603dc39 commit b94b133
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 6 deletions.
71 changes: 71 additions & 0 deletions get-json.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion get-node-versions.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions test/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit b94b133

Please sign in to comment.