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

streams: make the pipeline callback mandatory #21054

Closed
wants to merge 2 commits into from
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
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' }
);
}