diff --git a/lib/api-browser.js b/lib/api-browser.js index 8ad9608..3f9e8a1 100644 --- a/lib/api-browser.js +++ b/lib/api-browser.js @@ -27,7 +27,7 @@ var mimetype = { var defaultArchives = {}; var downloadUrl = null; -var request = require("xhr-request-promise"); +var request = require("xhr-request"); var bytes = require("eth-lib/lib/bytes"); diff --git a/lib/api-node.js b/lib/api-node.js index 94211be..3945fdd 100644 --- a/lib/api-node.js +++ b/lib/api-node.js @@ -12,7 +12,7 @@ var mimetype = require('mime-types'); var defaultArchives = require("./../archives/archives.json"); -var requester = require("xhr-request-promise"); +var requester = require("xhr-request"); var downloadUrl = "http://ethereum-mist.s3.amazonaws.com/swarm/"; @@ -25,7 +25,7 @@ var pick = require("./pick.js"); var swarm = require("./swarm"); // Fixes issue that causes xhr-request-promise on Node.js to only accept Buffer -var request = function request(url, params) { +var request = function request(url, params, callback) { var newParams = {}; for (var key in params) { @@ -36,7 +36,7 @@ var request = function request(url, params) { newParams.body = newParams.body instanceof Buffer ? newParams.body : new Buffer(newParams.body); } - return requester(url, newParams); + return requester(url, newParams, callback); }; module.exports = swarm({ diff --git a/lib/swarm.js b/lib/swarm.js index 8a3a35b..9ebd973 100644 --- a/lib/swarm.js +++ b/lib/swarm.js @@ -84,13 +84,20 @@ module.exports = function (_ref) { var downloadData = function downloadData(swarmUrl) { return function (hash) { - return request(rawUrl(swarmUrl)(hash), { - responseType: "arraybuffer" - }).then(function (arrayBuffer) { - var uint8Array = new Uint8Array(arrayBuffer); - var error404 = [52, 48, 52, 32, 112, 97, 103, 101, 32, 110, 111, 116, 32, 102, 111, 117, 110, 100, 10]; - if (equals(uint8Array)(error404)) throw "Error 404."; - return uint8Array; + return new Promise(function (resolve, reject) { + request(rawUrl(swarmUrl)(hash), { + responseType: "arraybuffer" + }, function (err, arrayBuffer, response) { + if (err) { + return reject(err); + } + + if (response.statusCode >= 400) { + return reject(new Error("Error ".concat(response.statusCode, "."))); + } + + return resolve(new Uint8Array(arrayBuffer)); + }); }); }; }; // type Entry = {"type": String, "hash": String} @@ -226,9 +233,18 @@ module.exports = function (_ref) { var uploadData = function uploadData(swarmUrl) { return function (data) { - return request("".concat(swarmUrl, "/bzz-raw:/"), { - body: typeof data === "string" ? fromString(data) : data, - method: "POST" + return new Promise(function (resolve, reject) { + var params = { + body: typeof data === "string" ? fromString(data) : data, + method: "POST" + }; + request("".concat(swarmUrl, "/bzz-raw:/"), params, function (err, data) { + if (err) { + return reject(err); + } + + return resolve(data); + }); }); }; }; // String -> String -> String -> File -> Promise String @@ -252,12 +268,18 @@ module.exports = function (_ref) { }, body: file.data }; - return request(url, opt).then(function (response) { - if (response.indexOf("error") !== -1) { - throw response; - } - - return response; + return new Promise(function (resolve, reject) { + request(url, opt, function (err, data) { + if (err) { + return reject(err); + } + + if (data.indexOf("error") !== -1) { + return reject(data); + } + + return data; + }); }).catch(function (e) { return n > 0 && attempt(n - 1); }); diff --git a/package.json b/package.json index e65e905..82eeade 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "mock-fs": "^4.1.0", "setimmediate": "^1.0.5", "tar": "^4.0.2", - "xhr-request-promise": "^0.1.2" + "xhr-request": "^1.0.1" }, "devDependencies": { "@babel/cli": "^7.0.0", diff --git a/src/api-browser.js b/src/api-browser.js index 2c046a0..cfef377 100644 --- a/src/api-browser.js +++ b/src/api-browser.js @@ -8,7 +8,7 @@ const child_process = {spawn: unavailable}; const mimetype = {lookup: unavailable}; const defaultArchives = {}; const downloadUrl = null; -const request = require("xhr-request-promise"); +const request = require("xhr-request"); const bytes = require("eth-lib/lib/bytes"); const hash = require("./swarm-hash.js"); const pick = require("./pick.js"); diff --git a/src/api-node.js b/src/api-node.js index f847e90..e46c927 100644 --- a/src/api-node.js +++ b/src/api-node.js @@ -5,7 +5,7 @@ const path = require("path"); const child_process = require("child_process"); const mimetype = require('mime-types'); const defaultArchives = require("./../archives/archives.json"); -const requester = require("xhr-request-promise"); +const requester = require("xhr-request"); const downloadUrl = "http://ethereum-mist.s3.amazonaws.com/swarm/"; const bytes = require("eth-lib/lib/bytes"); const hash = require("./swarm-hash.js"); @@ -13,7 +13,7 @@ const pick = require("./pick.js"); const swarm = require("./swarm"); // Fixes issue that causes xhr-request-promise on Node.js to only accept Buffer -const request = (url, params) => { +const request = (url, params, callback) => { let newParams = {}; for (let key in params) { newParams[key] = params[key]; @@ -23,7 +23,7 @@ const request = (url, params) => { ? newParams.body : new Buffer(newParams.body); } - return requester(url, newParams); + return requester(url, newParams, callback); }; module.exports = swarm({ diff --git a/src/swarm.js b/src/swarm.js index 5adfea4..cbb3243 100644 --- a/src/swarm.js +++ b/src/swarm.js @@ -61,14 +61,17 @@ module.exports = ({ // String -> String -> Promise Uint8Array // Gets the raw contents of a Swarm hash address. const downloadData = swarmUrl => hash => - request(rawUrl(swarmUrl)(hash), {responseType: "arraybuffer"}) - .then(arrayBuffer => { - const uint8Array = new Uint8Array(arrayBuffer); - const error404 = [52,48,52,32,112,97,103,101,32,110,111,116,32,102,111,117,110,100,10]; - if (equals(uint8Array)(error404)) - throw "Error 404."; - return uint8Array; - }); + new Promise((resolve, reject) => { + request(rawUrl(swarmUrl)(hash), {responseType: "arraybuffer"}, (err, arrayBuffer, response) => { + if (err) { + return reject(err); + } + if (response.statusCode >= 400) { + return reject(new Error(`Error ${response.statusCode}.`)); + } + return resolve(new Uint8Array(arrayBuffer)); + }) + }); // type Entry = {"type": String, "hash": String} // type File = {"type": String, "data": Uint8Array} @@ -154,9 +157,18 @@ module.exports = ({ // Uploads raw data to Swarm. // Returns a promise with the uploaded hash. const uploadData = swarmUrl => data => - request(`${swarmUrl}/bzz-raw:/`, { - body: typeof data === "string" ? fromString(data) : data, - method: "POST"}); + new Promise((resolve, reject) => { + const params = { + body: typeof data === "string" ? fromString(data) : data, + method: "POST" + }; + request(`${swarmUrl}/bzz-raw:/`, params, (err, data) => { + if (err) { + return reject(err); + } + return resolve(data); + }); + }); // String -> String -> String -> File -> Promise String // Uploads a file to the Swarm manifest at a given hash, under a specific @@ -171,14 +183,17 @@ module.exports = ({ method: "PUT", headers: {"Content-Type": file.type}, body: file.data}; - return request(url, opt) - .then(response => { - if (response.indexOf("error") !== -1) { - throw response; + return new Promise((resolve, reject) => { + request(url, opt, (err, data) => { + if (err) { + return reject(err); + } + if (data.indexOf("error") !== -1) { + return reject(data); } - return response; - }) - .catch(e => n > 0 && attempt(n-1)); + return data; + }); + }).catch(e => n > 0 && attempt(n-1)); }; return attempt(3); };