diff --git a/lib/internal/streams/pipeline.js b/lib/internal/streams/pipeline.js index 65ef0c8f672bbf..573f32d1421bf8 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; @@ -23,6 +22,12 @@ const { const { validateCallback } = require('internal/validators'); +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/lib/stream/promises.js b/lib/stream/promises.js index 90d6b1244bd380..027579b878dbf7 100644 --- a/lib/stream/promises.js +++ b/lib/stream/promises.js @@ -3,8 +3,6 @@ const { ArrayPrototypePop, Promise, - SymbolAsyncIterator, - SymbolIterator, } = primordials; const { @@ -15,29 +13,14 @@ const { validateAbortSignal, } = require('internal/validators'); +const { + isIterable, + isStream, +} = require('internal/streams/utils'); + let pl; let eos; -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 pipeline(...streams) { if (!pl) pl = require('internal/streams/pipeline'); return new Promise((resolve, reject) => { diff --git a/node.gyp b/node.gyp index 5d2e63db5d1cab..6691de8422e940 100644 --- a/node.gyp +++ b/node.gyp @@ -256,6 +256,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 db8d9eaa434b96..27202dee504880 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -98,6 +98,7 @@ const expectedModules = new Set([ 'NativeModule internal/streams/readable', 'NativeModule internal/streams/state', 'NativeModule internal/streams/transform', + 'NativeModule internal/streams/utils', 'NativeModule internal/streams/writable', 'NativeModule internal/timers', 'NativeModule internal/url',