stream: ensure Stream.pipeline re-throws errors without callback

Fixes an issue where Stream.pipeline wouldn't re-throw errors
on a stream if no callback was specified, thus swallowing
said errors.

Fixes: https://github.com/nodejs/node/issues/20303

PR-URL: https://github.com/nodejs/node/pull/20437
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Blaine Bublitz 2018-04-26 13:49:12 -07:00 committed by Anna Henningsen
parent c2c9c0c3d3
commit c1012b4402
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
2 changed files with 39 additions and 4 deletions

View File

@ -19,7 +19,10 @@ function once(callback) {
};
}
function noop() {}
function noop(err) {
// Rethrow the error if it exists to avoid swallowing it
if (err) throw err;
}
function isRequest(stream) {
return stream.setHeader && typeof stream.abort === 'function';

View File

@ -142,7 +142,7 @@ common.crashOnUnhandledRejection();
}
});
pipeline(rs, res);
pipeline(rs, res, () => {});
});
server.listen(0, () => {
@ -177,7 +177,7 @@ common.crashOnUnhandledRejection();
})
});
pipeline(rs, res);
pipeline(rs, res, () => {});
});
server.listen(0, () => {
@ -206,7 +206,7 @@ common.crashOnUnhandledRejection();
})
});
pipeline(rs, res);
pipeline(rs, res, () => {});
});
let cnt = 10;
@ -481,3 +481,35 @@ common.crashOnUnhandledRejection();
run();
}
{
const read = new Readable({
read() {}
});
const transform = new Transform({
transform(data, enc, cb) {
cb(new Error('kaboom'));
}
});
const write = new Writable({
write(data, enc, cb) {
cb();
}
});
read.on('close', common.mustCall());
transform.on('close', common.mustCall());
write.on('close', common.mustCall());
process.on('uncaughtException', common.mustCall((err) => {
assert.deepStrictEqual(err, new Error('kaboom'));
}));
const dst = pipeline(read, transform, write);
assert.strictEqual(dst, write);
read.push('hello');
}