Skip to content

Commit

Permalink
[Refactor] replace phin with a simplified inlined version
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 17, 2024
1 parent f2d9797 commit c649fb5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 28 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"callback-return": 0,
"eqeqeq": 1,
"no-underscore-dangle": 0,
"prefer-promise-reject-errors": 0,
},
}
1 change: 0 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package-lock=false
allow-same-version=true
message=v%s
audit-level=high # phin is a false positive
86 changes: 61 additions & 25 deletions lib/node.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
'use strict';

var phin = require('phin');
/* eslint max-lines-per-function: 0, max-statements: 0, sort-keys: 0 */

var internalGetJSON = function _getJSON(url, callback) {
phin({ url: url }, function (error, response) {
if (error) {
callback(error);
return;
}
var parse = require('url').parse;
var http = require('http');
var https = require('https');
var Buffer = require('safe-buffer').Buffer;
var regexTester = require('safe-regex-test');

var body;
try {
body = JSON.parse(response.body);
} catch (parseError) {
callback('Parse error: ' + parseError);
return;
}
var isHTTPx = regexTester(/^https?:/i);
var isHTTPS = regexTester(/^https:/i);

if (response.statusCode != 200) {
callback('Unexpected response code.');
return;
}
var phinish = function (addr, cb) {
var protocolIsHTTPS = isHTTPS(addr.protocol);

var options = {
hostname: addr.hostname,
port: addr.port || (protocolIsHTTPS ? 443 : 80),
path: addr.path,
method: 'GET',
headers: {},
auth: addr.auth || null,
parse: 'none',
stream: false
};

var resHandler = function (res) {
/* eslint no-param-reassign: 0 */
res.body = Buffer.from([]);
res.on('data', function (chunk) {
res.body = Buffer.concat([res.body, chunk]);
});
res.on('end', function () {
cb(null, res);
});
};

callback(null, body);
var req = (protocolIsHTTPS ? https : http).request(options, resHandler);

req.on('error', function (err) {
cb(err, null);
});

req.end();
};

module.exports = function getJSON(url, callback) {
Expand All @@ -32,17 +51,34 @@ module.exports = function getJSON(url, callback) {
throw new TypeError('`url` is not a string');
}

internalGetJSON(url, function (error, body) {
var addr = parse(url);

if (!isHTTPx(addr.protocol)) {
reject(new Error('Invalid / unknown URL protocol. Expected HTTP or HTTPS, got `' + addr.protocol + '`'), null);
return;
}

phinish(addr, function (error, response) {
if (error) {
reject(error);
if (callback) {
callback(error);
}
return;
}
resolve(body);
if (callback) {
callback(null, body);
} else {
try {
var body = JSON.parse(response.body);

if (response.statusCode == 200) {
resolve(body);
if (callback) {
callback(null, body);
}
} else {
reject('Unexpected response code ' + response.statusCode);
}
} catch (parseError) {
reject('Parse error: ' + parseError);
}
}
});
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"homepage": "https://github.com/ljharb/get-json#readme",
"dependencies": {
"jsonp": "^0.2.1",
"phin": "^2.9.3"
"safe-buffer": "^5.2.1",
"safe-regex-test": "^1.0.3"
},
"devDependencies": {
"@ljharb/eslint-config": "^21.1.1",
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('Get IP from JSONTest API', function (t) {
t.plan(2);

getJSON('http://ip.jsontest.com/', function (error, body) {
t.error(error);
t.error(error, 'is not an error');
t.ok(body.ip);
});
});
Expand Down

0 comments on commit c649fb5

Please sign in to comment.