From 301ba94ef67155a0a234a2b6c073145207b07698 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 15 Dec 2017 22:11:55 -0500 Subject: [PATCH] refactor httpGet --- lib/core/engine.js | 36 +++++++++++++++--------------------- lib/utils/utils.js | 16 +++++++++++++++- test_app/npm-debug.log | 27 +++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 test_app/npm-debug.log diff --git a/lib/core/engine.js b/lib/core/engine.js index 852ee8045..050c7528d 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -200,29 +200,23 @@ class Engine { //Ideally this method should be in an IPFS API JSONRPC wrapper //The URL should also be flexible to accept non-default IPFS url self.logger.trace("Checking IPFS version..."); - utils.httpGet('http://' + host + ':' + port + '/api/v0/version', function (res) { - let body = ''; - res.on('data', function (d) { - body += d; - }); - res.on('end', function () { - try { - let parsed = JSON.parse(body); - if (parsed.Version) { - return cb({name: ("IPFS " + parsed.Version), status: 'on'}); - } - else { - return cb({name: "IPFS ", status: 'on'}); - } - } - catch (e) { - return cb({name: "IPFS ", status: 'off'}); - } - }); - res.on('error', function (err) { + utils.httpGet('http://' + host + ':' + port + '/api/v0/version', function (err, body) { + if (err) { self.logger.trace("Check IPFS version error: " + err); return cb({name: "IPFS ", status: 'off'}); - }); + } + try { + let parsed = JSON.parse(body); + if (parsed.Version) { + return cb({name: ("IPFS " + parsed.Version), status: 'on'}); + } + else { + return cb({name: "IPFS ", status: 'on'}); + } + } + catch (e) { + return cb({name: "IPFS ", status: 'off'}); + } }); } else { diff --git a/lib/utils/utils.js b/lib/utils/utils.js index 458a1a136..20952c705 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -33,7 +33,18 @@ function checkIsAvailable(url, callback) { } function httpGet(url, callback) { - return http.get(url, callback); + http.get(url, function(res) { + let body = ''; + res.on('data', function (d) { + body += d; + }); + res.on('end', function () { + callback(null, body); + }); + res.on('error', function (err) { + callback(err); + }); + }); } function httpsGet(url, callback) { @@ -45,6 +56,9 @@ function httpsGet(url, callback) { res.on('end', function () { callback(null, body); }); + res.on('error', function (err) { + callback(err); + }); }); } diff --git a/test_app/npm-debug.log b/test_app/npm-debug.log new file mode 100644 index 000000000..9882b8146 --- /dev/null +++ b/test_app/npm-debug.log @@ -0,0 +1,27 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/Users/iurimatias/.nvm/versions/node/v6.9.5/bin/node', +1 verbose cli '/Users/iurimatias/.nvm/versions/node/v6.9.5/bin/npm', +1 verbose cli 'run', +1 verbose cli 'fulltest' ] +2 info using npm@3.10.10 +3 info using node@v6.9.5 +4 verbose stack Error: missing script: fulltest +4 verbose stack at run (/Users/iurimatias/.nvm/versions/node/v6.9.5/lib/node_modules/npm/lib/run-script.js:151:19) +4 verbose stack at /Users/iurimatias/.nvm/versions/node/v6.9.5/lib/node_modules/npm/lib/run-script.js:61:5 +4 verbose stack at /Users/iurimatias/.nvm/versions/node/v6.9.5/lib/node_modules/npm/node_modules/read-package-json/read-json.js:356:5 +4 verbose stack at checkBinReferences_ (/Users/iurimatias/.nvm/versions/node/v6.9.5/lib/node_modules/npm/node_modules/read-package-json/read-json.js:320:45) +4 verbose stack at final (/Users/iurimatias/.nvm/versions/node/v6.9.5/lib/node_modules/npm/node_modules/read-package-json/read-json.js:354:3) +4 verbose stack at then (/Users/iurimatias/.nvm/versions/node/v6.9.5/lib/node_modules/npm/node_modules/read-package-json/read-json.js:124:5) +4 verbose stack at /Users/iurimatias/.nvm/versions/node/v6.9.5/lib/node_modules/npm/node_modules/read-package-json/read-json.js:243:12 +4 verbose stack at /Users/iurimatias/.nvm/versions/node/v6.9.5/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16 +4 verbose stack at tryToString (fs.js:455:3) +4 verbose stack at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:442:12) +5 verbose cwd /Users/iurimatias/Projects/embark-framework/test_app +6 error Darwin 16.7.0 +7 error argv "/Users/iurimatias/.nvm/versions/node/v6.9.5/bin/node" "/Users/iurimatias/.nvm/versions/node/v6.9.5/bin/npm" "run" "fulltest" +8 error node v6.9.5 +9 error npm v3.10.10 +10 error missing script: fulltest +11 error If you need help, you may report this error at: +11 error +12 verbose exit [ 1, true ]