From 2bcf3bce4ec4fd38ed6e813f11f43bd5ebbf950f Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Wed, 24 Feb 2021 11:30:50 -0800 Subject: [PATCH] stream: move duplicated code to an internal module Create a utils module for isIterable(), isReadable(), and isStream(). PR-URL: https://github.com/nodejs/node/pull/37508 Reviewed-By: Antoine du Hamel Reviewed-By: Benjamin Gruenbaum Reviewed-By: Darshan Sen Reviewed-By: Robert Nagy Reviewed-By: Matteo Collina --- lib/internal/streams/pipeline.js | 27 +++++---------------- lib/internal/streams/utils.js | 32 +++++++++++++++++++++++++ node.gyp | 1 + test/parallel/test-bootstrap-modules.js | 1 + 4 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 lib/internal/streams/utils.js diff --git a/lib/internal/streams/pipeline.js b/lib/internal/streams/pipeline.js index 134e9ea94fa797..6483e9829eddc3 100644 --- a/lib/internal/streams/pipeline.js +++ b/lib/internal/streams/pipeline.js @@ -7,7 +7,6 @@ const { ArrayIsArray, ReflectApply, SymbolAsyncIterator, - SymbolIterator, } = primordials; let eos; @@ -22,6 +21,12 @@ const { ERR_STREAM_DESTROYED } = require('internal/errors').codes; +const { + isIterable, + isReadable, + isStream, +} = require('internal/streams/utils'); + let EE; let PassThrough; let Readable; @@ -78,26 +83,6 @@ function popCallback(streams) { return streams.pop(); } -function isReadable(obj) { - return !!(obj && typeof obj.pipe === 'function'); -} - -function isWritable(obj) { - return !!(obj && typeof obj.write === 'function'); -} - -function isStream(obj) { - return isReadable(obj) || isWritable(obj); -} - -function isIterable(obj, isAsync) { - if (!obj) return false; - if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function'; - if (isAsync === false) return typeof obj[SymbolIterator] === 'function'; - return typeof obj[SymbolAsyncIterator] === 'function' || - typeof obj[SymbolIterator] === 'function'; -} - function makeAsyncIterable(val) { if (isIterable(val)) { return val; diff --git a/lib/internal/streams/utils.js b/lib/internal/streams/utils.js new file mode 100644 index 00000000000000..08c196802780b8 --- /dev/null +++ b/lib/internal/streams/utils.js @@ -0,0 +1,32 @@ +'use strict'; + +const { + SymbolAsyncIterator, + SymbolIterator, +} = primordials; + +function isReadable(obj) { + return !!(obj && typeof obj.pipe === 'function'); +} + +function isWritable(obj) { + return !!(obj && typeof obj.write === 'function'); +} + +function isStream(obj) { + return isReadable(obj) || isWritable(obj); +} + +function isIterable(obj, isAsync) { + if (!obj) return false; + if (isAsync === true) return typeof obj[SymbolAsyncIterator] === 'function'; + if (isAsync === false) return typeof obj[SymbolIterator] === 'function'; + return typeof obj[SymbolAsyncIterator] === 'function' || + typeof obj[SymbolIterator] === 'function'; +} + +module.exports = { + isIterable, + isReadable, + isStream, +}; diff --git a/node.gyp b/node.gyp index 2197f0fd755a15..35b7912afa786c 100644 --- a/node.gyp +++ b/node.gyp @@ -245,6 +245,7 @@ 'lib/internal/streams/state.js', 'lib/internal/streams/pipeline.js', 'lib/internal/streams/end-of-stream.js', + 'lib/internal/streams/utils.js', 'deps/v8/tools/splaytree.js', 'deps/v8/tools/codemap.js', 'deps/v8/tools/consarray.js', diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index 0544f8eec4b54e..683171fe3c7b20 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -79,6 +79,7 @@ const expectedModules = new Set([ 'NativeModule internal/process/warning', 'NativeModule internal/querystring', 'NativeModule internal/source_map/source_map_cache', + 'NativeModule internal/streams/utils', 'NativeModule internal/timers', 'NativeModule internal/url', 'NativeModule internal/util',