Skip to content

Commit

Permalink
Getting updates from master
Browse files Browse the repository at this point in the history
  • Loading branch information
evertonfraga committed Aug 31, 2018
1 parent b7169e4 commit 3e7bea6
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 43 deletions.
2 changes: 1 addition & 1 deletion lib/api-browser.js
Expand Up @@ -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");

Expand Down
6 changes: 3 additions & 3 deletions lib/api-node.js
Expand Up @@ -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/";

Expand All @@ -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) {
Expand All @@ -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({
Expand Down
54 changes: 38 additions & 16 deletions lib/swarm.js
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/api-browser.js
Expand Up @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions src/api-node.js
Expand Up @@ -5,15 +5,15 @@ 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");
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];
Expand All @@ -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({
Expand Down
51 changes: 33 additions & 18 deletions src/swarm.js
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -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);
};
Expand Down

0 comments on commit 3e7bea6

Please sign in to comment.