Skip to content

Commit

Permalink
stream: pipeline accept Buffer as a valid first argument
Browse files Browse the repository at this point in the history
change isStream to also check existence of on, so it
wont mistake buffers as Streams.

fixes: #37731
  • Loading branch information
Linkgoron committed Mar 13, 2021
1 parent c2a792f commit fd7bab1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/internal/streams/add-abort-signal.js
Expand Up @@ -18,13 +18,13 @@ const validateAbortSignal = (signal, name) => {
}
};

function isStream(obj) {
function isReadable(obj) {
return !!(obj && typeof obj.pipe === 'function');
}

module.exports.addAbortSignal = function addAbortSignal(signal, stream) {
validateAbortSignal(signal, 'signal');
if (!isStream(stream)) {
if (!isReadable(stream)) {
throw new ERR_INVALID_ARG_TYPE('stream', 'stream.Stream', stream);
}
return module.exports.addAbortSignalNoValidate(signal, stream);
Expand Down
6 changes: 4 additions & 2 deletions lib/internal/streams/utils.js
Expand Up @@ -6,11 +6,13 @@ const {
} = primordials;

function isReadable(obj) {
return !!(obj && typeof obj.pipe === 'function');
return !!(obj && typeof obj.pipe === 'function' &&
typeof obj.on === 'function');
}

function isWritable(obj) {
return !!(obj && typeof obj.write === 'function');
return !!(obj && typeof obj.write === 'function' &&
typeof obj.on === 'function');
}

function isStream(obj) {
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-stream-pipeline.js
Expand Up @@ -1339,3 +1339,19 @@ const net = require('net');
assert.strictEqual(res, '123');
}));
}

{
const content = 'abc';
pipeline(Buffer.from(content), PassThrough({ objectMode: true }),
common.mustSucceed(() => {}));

let res = '';
pipeline(Buffer.from(content), async function*(previous) {
for await (const val of previous) {
res += String.fromCharCode(val);
yield val;
}
}, common.mustSucceed(() => {
assert.strictEqual(res, content);
}));
}

0 comments on commit fd7bab1

Please sign in to comment.