Skip to content

Commit

Permalink
fixed issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
phal0r committed Apr 3, 2017
1 parent 674cafa commit 9f8829b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 87 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

## v1.3.0
- Fixed broken Promise chain to be able to catch error during assignment calls
102 changes: 49 additions & 53 deletions lib/weedfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,86 +21,82 @@ WeedFSClient.prototype = {
return new Promise(function(resolve, reject) {
var req = http.request(url.parse(self.baseURL + "dir/assign?" + qs.stringify(opts)), function(res) {
let body = "";

res.setEncoding('utf8');
res.on("data", function(chunk) {
body += chunk;
});
res.on("end", function() {
var json = JSON.parse(body);
resolve(json);
return resolve(json);
});
});
req.on("error", function(err) {
reject(err);
return reject(err);
});
req.end();
});
},

write: function(file, opts) {
opts = opts || {};
var self = this;
return new Promise(function(resolve, reject) {
opts = opts || {};
if (file instanceof Array) {
opts.count = file.length;

if (file instanceof Array) {
opts.count = file.length;

for (var i = 0; i < opts.count; i++) {
if (typeof file[i] === "string") {
file[i] = path.resolve(process.cwd(), file[i]);
}
for (var i = 0; i < opts.count; i++) {
if (typeof file[i] === "string") {
file[i] = path.resolve(process.cwd(), file[i]);
}
} else {
opts.count = 1
if (typeof file === "string") {
file = path.resolve(process.cwd(), file);
}
file = [file];
}
} else {
opts.count = 1
if (typeof file === "string") {
file = path.resolve(process.cwd(), file);
}
file = [file];
}

var ins = this;
return self._assign(opts).then(function(finfo) {
if (finfo.error) {
return Promise.reject(finfo.error);
}

self._assign(opts).then(function(finfo) {
if (finfo.error) {
return reject(finfo.error);
}
var proms = [];
for (var i = 0; i < opts.count; i++) {
proms.push(new Promise(function(resolve, reject) {
var form = new FormData();
var url = "http://" + (self.usePublicUrl ? finfo.publicUrl : finfo.url) + "/" + finfo.fid + (opts.count == 1 ? "" : "_" + i);
var stream = typeof file[i] === "string" ? fs.createReadStream(file[i]) : null;
form.append("file", stream ? stream : file[i]);
var req = form.submit(url, function(err, res) {
if (err) {
return reject(err);
}
resolve(res);
});

var proms = [];
for (var i = 0; i < opts.count; i++) {
proms.push(new Promise(function(resolve, reject) {
var form = new FormData();
var url = "http://" + (self.usePublicUrl ? finfo.publicUrl : finfo.url) + "/" + finfo.fid + (opts.count == 1 ? "" : "_" + i);
var stream = typeof file[i] === "string" ? fs.createReadStream(file[i]) : null;
form.append("file", stream ? stream : file[i]);
var req = form.submit(url, function(err, res) {
if (err) {
return reject(err);
}
resolve(res);
//we only check for self created streams, stream errors from outside streams should be handled outside
if (stream) {
stream.on("error", function(err) {
reject(err);
});
}

//we only check for self created streams, stream errors from outside streams should be handled outside
if (stream) {
stream.on("error", function(err) {
reject(err);
});
}
req.on("error", function(err) {
reject(err);
});

req.on("error", function(err) {
req.on("socket", function(socket) {
socket.on("error", function(err) {
reject(err);
});
})
}));
}

req.on("socket", function(socket) {
socket.on("error", function(err) {
reject(err);
});
})
}));
}
Promise.all(proms).then(function() {
resolve(finfo);
}).catch(function(err) {
reject(err);
});
return Promise.all(proms).then(function() {
return Promise.resolve(finfo);
});

});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"name": "node-seaweedfs",
"description": "Node.js client for seaweed-fs, a distributed file store",
"homepage": "https://github.com/atroo/node-weedfs",
"version": "1.2.0",
"version": "1.3.0",
"main": "./index",
"repository": {
"type": "git",
Expand Down
79 changes: 46 additions & 33 deletions test/tests/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,50 @@ var client = new weedfs(config);

var testFileBytes = 280072;

describe("seaweed write api",function() {
describe("seaweed write api", function() {
it("should write a single file from a path", function(done) {
client.write("./test/tests/test.jpg").then(function (fileInfo) {
client.write("./test/tests/test.jpg").then(function(fileInfo) {
expect(fileInfo).to.be.an("object");

return client.remove(fileInfo.fid);
}).then(function (res) {
}).then(function(res) {
expect(res).to.be.an("object");
expect(res.count).to.be.greaterThan(0);
done();
}).catch(function(err) {
console.log(err);
});

});

it("should write a single buffer", function(done) {
var fileInfo;
client.write(new Buffer("Hallo")).then(function (finfo) {
client.write(new Buffer("Hallo")).then(function(finfo) {
fileInfo = finfo;
expect(fileInfo).to.be.an("object");

return client.read(fileInfo.fid);
}).then(function (buffer) {
}).then(function(buffer) {
expect(buffer.toString("utf8")).to.equal("Hallo");
return client.remove(fileInfo.fid);
}).then(function (res) {
}).then(function(res) {
expect(res).to.be.an("object");
expect(res.count).to.be.greaterThan(0);
done();
}).catch(function(err) {
console.log(err);
});

});

it("should write a single stream", function(done) {
var fileInfo;
client.write(fs.createReadStream("./test/tests/test.jpg")).then(function (finfo) {
client.write(fs.createReadStream("./test/tests/test.jpg")).then(function(finfo) {
fileInfo = finfo;
expect(fileInfo).to.be.an("object");

return client.read(fileInfo.fid);
}).then(function (buffer) {
}).then(function(buffer) {
expect(buffer.length).to.equal(testFileBytes);
return client.remove(fileInfo.fid);
}).then(function(res) {
Expand All @@ -60,52 +60,52 @@ describe("seaweed write api",function() {
}).catch(function(err) {
console.log(err);
});

});

it("should write two files from path as array", function(done) {
client.write(["./test/tests/test.jpg", "./test/tests/test1.jpg"]).then(function (fileInfo) {
client.write(["./test/tests/test.jpg", "./test/tests/test1.jpg"]).then(function(fileInfo) {
expect(fileInfo).to.be.an("object");

return client.remove(fileInfo.fid);
}).then(function (res) {
}).then(function(res) {
expect(res).to.be.an("object");
expect(res.count).to.be.greaterThan(0);
done();
}).catch(function(err) {
console.log(err);
});

});

it("should write two buffers", function(done) {
var fileInfo;
client.write([new Buffer("Hallo"), new Buffer("Hallo2")]).then(function (finfo) {
client.write([new Buffer("Hallo"), new Buffer("Hallo2")]).then(function(finfo) {
fileInfo = finfo;
expect(fileInfo).to.be.an("object");

return client.read(fileInfo.fid);
}).then(function (buffer) {
}).then(function(buffer) {
expect(buffer.toString("utf8")).to.equal("Hallo");
return client.remove(fileInfo.fid);
}).then(function (res) {
}).then(function(res) {
expect(res).to.be.an("object");
expect(res.count).to.be.greaterThan(0);
done();
}).catch(function(err) {
console.log(err);
});

});

it("should write two streams", function(done) {
var fileInfo;
client.write([fs.createReadStream("./test/tests/test.jpg"), fs.createReadStream("./test/tests/test1.jpg")]).then(function (finfo) {
client.write([fs.createReadStream("./test/tests/test.jpg"), fs.createReadStream("./test/tests/test1.jpg")]).then(function(finfo) {
fileInfo = finfo;
expect(fileInfo).to.be.an("object");

return client.read(fileInfo.fid);
}).then(function (buffer) {
}).then(function(buffer) {
expect(buffer.length).to.equal(testFileBytes);
return client.remove(fileInfo.fid);
}).then(function(res) {
Expand All @@ -115,17 +115,30 @@ describe("seaweed write api",function() {
}).catch(function(err) {
console.log(err);
});

});

it("should reject on invalid filepath", function(done) {
client.write("./test/tests/test25.jpg").then(function (fileInfo) {
client.write("./test/tests/test25.jpg").then(function(fileInfo) {
return client.remove(fileInfo.fid);
}).then(function (res) {
}).catch(function(err) {
}).then(function(res) {}).catch(function(err) {
expect(err).to.be.an.instanceof(Error);
done();
});

});

it("should reject the returned promise if host is not reachable", function(done) {
this.timeout(10000);
var wronglyConfiguredClient = new weedfs({
host: "wonghost",
port: 4000
});

wronglyConfiguredClient.write("./test/tests/test.jpg").catch(function(err) {
expect(err).to.be.an.instanceof(Error);
expect(err.code).to.equal("ENOTFOUND");
done();
});

});
});

0 comments on commit 9f8829b

Please sign in to comment.