From 44f0733db48ae00716bd19d16da8360237015cc3 Mon Sep 17 00:00:00 2001 From: Nitzan Uziely Date: Sat, 13 Mar 2021 16:44:48 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/37731 --- lib/internal/streams/utils.js | 6 ++++-- test/parallel/test-stream-pipeline.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/internal/streams/utils.js b/lib/internal/streams/utils.js index 08c196802780b8..b6e744250799c6 100644 --- a/lib/internal/streams/utils.js +++ b/lib/internal/streams/utils.js @@ -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) { diff --git a/test/parallel/test-stream-pipeline.js b/test/parallel/test-stream-pipeline.js index 3ad8d82e785b95..31d879b5c05ab0 100644 --- a/test/parallel/test-stream-pipeline.js +++ b/test/parallel/test-stream-pipeline.js @@ -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); + })); +}