Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
streams: make the pipeline callback mandatory
Right now when not adding a callback to the pipeline it could cause
an uncaught exception if there is an error. Instead, just make the
callback mandatory as mostly done in all other Node.js callback APIs
so users explicitly have to decide what to do in such situations.
  • Loading branch information
BridgeAR committed May 31, 2018
commit e9a29f3aae6ae0f95bc8ca209cd280c1ed888b71
13 changes: 6 additions & 7 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
let eos;

const {
ERR_INVALID_CALLBACK,
ERR_MISSING_ARGS,
ERR_STREAM_DESTROYED
} = require('internal/errors').codes;
Expand All @@ -19,11 +20,6 @@ function once(callback) {
};
}

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';
}
Expand Down Expand Up @@ -66,8 +62,11 @@ function pipe(from, to) {
}

function popCallback(streams) {
if (!streams.length) return noop;
if (typeof streams[streams.length - 1] !== 'function') return noop;
// Streams should never be an empty array. It should always contain at least
// a single stream. Therefore optimize for the average case instead of
// checking for length === 0 as well.
if (typeof streams[streams.length - 1] !== 'function')
throw new ERR_INVALID_CALLBACK();
return streams.pop();
}

Expand Down
19 changes: 5 additions & 14 deletions test/parallel/test-stream-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ common.crashOnUnhandledRejection();
}, /ERR_MISSING_ARGS/);
assert.throws(() => {
pipeline();
}, /ERR_MISSING_ARGS/);
}, /ERR_INVALID_CALLBACK/);
}

{
Expand Down Expand Up @@ -499,17 +499,8 @@ common.crashOnUnhandledRejection();
}
});

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');
assert.throws(
() => pipeline(read, transform, write),
{ code: 'ERR_INVALID_CALLBACK' }
);
}