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 <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Rich Trott 2021-02-24 11:30:50 -08:00
parent 7a4c2c8d86
commit 1e34df139c
5 changed files with 45 additions and 43 deletions

View File

@ -11,7 +11,6 @@ const {
FunctionPrototypeCall,
ReflectApply,
SymbolAsyncIterator,
SymbolIterator,
} = primordials;
let eos;
@ -27,6 +26,12 @@ const {
const { validateCallback } = require('internal/validators');
const {
isIterable,
isReadable,
isStream,
} = require('internal/streams/utils');
let EE;
let PassThrough;
let Readable;
@ -82,26 +87,6 @@ function popCallback(streams) {
return ArrayPrototypePop(streams);
}
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;

View File

@ -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,
};

View File

@ -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) => {

View File

@ -263,6 +263,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',

View File

@ -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',