Skip to content

Commit fd7bab1

Browse files
committed
stream: pipeline accept Buffer as a valid first argument
change isStream to also check existence of on, so it wont mistake buffers as Streams. fixes: #37731
1 parent c2a792f commit fd7bab1

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

lib/internal/streams/add-abort-signal.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const validateAbortSignal = (signal, name) => {
1818
}
1919
};
2020

21-
function isStream(obj) {
21+
function isReadable(obj) {
2222
return !!(obj && typeof obj.pipe === 'function');
2323
}
2424

2525
module.exports.addAbortSignal = function addAbortSignal(signal, stream) {
2626
validateAbortSignal(signal, 'signal');
27-
if (!isStream(stream)) {
27+
if (!isReadable(stream)) {
2828
throw new ERR_INVALID_ARG_TYPE('stream', 'stream.Stream', stream);
2929
}
3030
return module.exports.addAbortSignalNoValidate(signal, stream);

lib/internal/streams/utils.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ const {
66
} = primordials;
77

88
function isReadable(obj) {
9-
return !!(obj && typeof obj.pipe === 'function');
9+
return !!(obj && typeof obj.pipe === 'function' &&
10+
typeof obj.on === 'function');
1011
}
1112

1213
function isWritable(obj) {
13-
return !!(obj && typeof obj.write === 'function');
14+
return !!(obj && typeof obj.write === 'function' &&
15+
typeof obj.on === 'function');
1416
}
1517

1618
function isStream(obj) {

test/parallel/test-stream-pipeline.js

+16
Original file line numberDiff line numberDiff line change
@@ -1339,3 +1339,19 @@ const net = require('net');
13391339
assert.strictEqual(res, '123');
13401340
}));
13411341
}
1342+
1343+
{
1344+
const content = 'abc';
1345+
pipeline(Buffer.from(content), PassThrough({ objectMode: true }),
1346+
common.mustSucceed(() => {}));
1347+
1348+
let res = '';
1349+
pipeline(Buffer.from(content), async function*(previous) {
1350+
for await (const val of previous) {
1351+
res += String.fromCharCode(val);
1352+
yield val;
1353+
}
1354+
}, common.mustSucceed(() => {
1355+
assert.strictEqual(res, content);
1356+
}));
1357+
}

0 commit comments

Comments
 (0)