From 63502131a32e6fb3a12b987e6163e967134849d9 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 PR-URL: https://github.com/nodejs/node/pull/37739 Fixes: https://github.com/nodejs/node/issues/37731 Reviewed-By: Luigi Pinca Reviewed-By: Matteo Collina Reviewed-By: Robert Nagy Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell --- 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 714a1945d78a7d..428f0fe89abe60 100644 --- a/test/parallel/test-stream-pipeline.js +++ b/test/parallel/test-stream-pipeline.js @@ -1247,3 +1247,19 @@ const net = require('net'); () => common.mustNotCall(), ); } + +{ + 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); + })); +}