Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pumpify "premature close" fix #46

Merged
merged 2 commits into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ var eosConfig = {
error: false,
};

function rethrowAsync(err) {
process.nextTick(rethrow);

function rethrow() {
throw err;
}
}

function tryCatch(fn, args) {
try {
return fn.apply(null, args);
} catch (err) {
rethrowAsync(err);
}
}

function asyncDone(fn, cb) {
cb = once(cb);

Expand All @@ -21,18 +37,18 @@ function asyncDone(fn, cb) {
function done() {
d.removeListener('error', onError);
d.exit();
return cb.apply(null, arguments);
return tryCatch(cb, arguments);
}

function onSuccess(result) {
tick(done, null, result);
done(null, result);
}

function onError(error) {
if (!error) {
error = new Error('Promise rejected without Error');
}
tick(done, error);
done(error);
}

function asyncRunner() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"jscs": "^2.3.5",
"jscs-preset-gulp": "^1.0.0",
"mocha": "^2.4.5",
"pumpify": "^1.3.6",
"rx": "^4.0.6",
"through2": "^2.0.0",
"when": "^3.7.3"
Expand Down
1 change: 1 addition & 0 deletions test/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('promises', function() {
var d = domain.create();
d.once('error', function(err) {
expect(err).toExist();
expect(err.message).toContain('Boom');
done();
});
d.run(function() {
Expand Down
24 changes: 24 additions & 0 deletions test/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var expect = require('expect');
var fs = require('fs');
var path = require('path');
var through = require('through2');
var pumpify = require('pumpify');

var asyncDone = require('../');

Expand All @@ -29,6 +30,21 @@ function failure() {
return read.pipe(new EndStream());
}

function withErr(chunk, _, cb) {
cb(new Error('Fail'));
}

function pumpifyError() {
var read = fs.createReadStream(exists);
var pipeline = pumpify(
through(),
through(withErr),
through()
);

return read.pipe(pipeline);
}

function unpiped() {
return fs.createReadStream(exists);
}
Expand All @@ -48,6 +64,14 @@ describe('streams', function() {
});
});

it('should handle an errored pipeline', function(done) {
asyncDone(pumpifyError, function(err) {
expect(err).toBeAn(Error);
expect(err.message).toNotBe('premature close');
done();
});
});

it('handle a returned stream and cb by only calling callback once', function(done) {
asyncDone(function(cb) {
return success().on('end', function() {
Expand Down