Skip to content

Commit e2643af

Browse files
committed
Merge pull request #56 from AnyFetch/no-more-black-magic
No more black magic
2 parents 2ff8eb6 + 64b4686 commit e2643af

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

lib/helpers/child-process.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,25 @@ process.on('message', function(task) {
4848
function downloadFile(cb) {
4949
if(task.file_path) {
5050
// Download the file
51-
var stream = fs.createWriteStream(path);
52-
53-
// Store error if statusCode !== 200
54-
var err;
55-
stream.on("finish", function() {
56-
cb(err);
57-
});
58-
5951
var apiUrl = url.parse(task.file_path, false, true);
60-
var req = request(apiUrl.protocol + "//" + apiUrl.host)
61-
.get(apiUrl.path);
62-
63-
// Warning, Black magic.
64-
// Streaming and checking for status code is no easy task...
65-
req.end(function() {}).req.once('response', function(res) {
66-
if(res.statusCode !== 200) {
67-
if(res.statusCode === 410) {
68-
err = new Error('410 Gone');
69-
err.skip = true;
52+
request(apiUrl.protocol + "//" + apiUrl.host)
53+
.get(apiUrl.path)
54+
.expect(200)
55+
.end(function(err, res) {
56+
if(err) {
57+
if(err.toString().match(/410/)) {
58+
err.skip = true;
59+
}
60+
else {
61+
err = new Error('Error downloading file: ' + err.toString());
62+
}
63+
return cb(err);
7064
}
71-
else {
72-
err = new Error('Error downloading file, got status ' + res.statusCode);
73-
}
74-
75-
stream.end();
76-
this.abort();
77-
}
78-
});
79-
req.pipe(stream);
65+
fs.writeFile(path, res.text, {encoding: 'binary'}, cb);
66+
});
8067
}
8168
else {
82-
cb(null);
69+
cb();
8370
}
8471
},
8572
function startHydration(cb) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "anyfetch-hydrater",
33
"description": "Create hydrater for AnyFetch.",
4-
"version": "2.0.8",
4+
"version": "2.0.9",
55
"author": "Matthieu Bacconnier <[email protected]>",
66
"scripts": {
77
"test": "NODE_ENV=test mocha --recursive -R spec test/",

test/errors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ describe('Errors', function() {
6666
fakeApi.patch('/result', function(req, res, next) {
6767
res.send(204);
6868
next();
69-
if(req.params.hydration_error.toString() === 'Error: Error downloading file, got status 404') {
69+
70+
if(req.params.hydration_error.toString() === 'Error: Error downloading file: Error: expected 200 "OK", got 404 "Not Found"') {
7071
done();
7172
}
7273
else {

0 commit comments

Comments
 (0)