2323
2424const {
2525 ObjectDefineProperty,
26- ObjectKeys,
27- ReflectApply,
26+ ObjectSetPrototypeOf,
2827} = primordials ;
2928
3029const {
30+ defineLazyProperties,
3131 promisify : { custom : customPromisify } ,
3232} = require ( 'internal/util' ) ;
3333
34- const {
35- streamReturningOperators,
36- promiseReturningOperators,
37- } = require ( 'internal/streams/operators' ) ;
38-
39- const {
40- codes : {
41- ERR_ILLEGAL_CONSTRUCTOR ,
42- } ,
43- } = require ( 'internal/errors' ) ;
44- const compose = require ( 'internal/streams/compose' ) ;
4534const { setDefaultHighWaterMark, getDefaultHighWaterMark } = require ( 'internal/streams/state' ) ;
46- const { pipeline } = require ( 'internal/streams/pipeline' ) ;
4735const { destroyer } = require ( 'internal/streams/destroy' ) ;
4836const { eos } = require ( 'internal/streams/end-of-stream' ) ;
4937const internalBuffer = require ( 'internal/buffer' ) ;
5038
51- const promises = require ( 'stream/promises' ) ;
39+ let promises ;
5240const utils = require ( 'internal/streams/utils' ) ;
5341const { isArrayBufferView, isUint8Array } = require ( 'internal/util/types' ) ;
5442
@@ -61,57 +49,29 @@ Stream.isReadable = utils.isReadable;
6149Stream . isWritable = utils . isWritable ;
6250
6351Stream . Readable = require ( 'internal/streams/readable' ) ;
64- const streamKeys = ObjectKeys ( streamReturningOperators ) ;
65- for ( let i = 0 ; i < streamKeys . length ; i ++ ) {
66- const key = streamKeys [ i ] ;
67- const op = streamReturningOperators [ key ] ;
68- function fn ( ...args ) {
69- if ( new . target ) {
70- throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
71- }
72- return Stream . Readable . from ( ReflectApply ( op , this , args ) ) ;
73- }
74- ObjectDefineProperty ( fn , 'name' , { __proto__ : null , value : op . name } ) ;
75- ObjectDefineProperty ( fn , 'length' , { __proto__ : null , value : op . length } ) ;
76- ObjectDefineProperty ( Stream . Readable . prototype , key , {
77- __proto__ : null ,
78- value : fn ,
79- enumerable : false ,
80- configurable : true ,
81- writable : true ,
82- } ) ;
83- }
84- const promiseKeys = ObjectKeys ( promiseReturningOperators ) ;
85- for ( let i = 0 ; i < promiseKeys . length ; i ++ ) {
86- const key = promiseKeys [ i ] ;
87- const op = promiseReturningOperators [ key ] ;
88- function fn ( ...args ) {
89- if ( new . target ) {
90- throw new ERR_ILLEGAL_CONSTRUCTOR ( ) ;
91- }
92- return ReflectApply ( op , this , args ) ;
93- }
94- ObjectDefineProperty ( fn , 'name' , { __proto__ : null , value : op . name } ) ;
95- ObjectDefineProperty ( fn , 'length' , { __proto__ : null , value : op . length } ) ;
96- ObjectDefineProperty ( Stream . Readable . prototype , key , {
97- __proto__ : null ,
98- value : fn ,
99- enumerable : false ,
100- configurable : true ,
101- writable : true ,
102- } ) ;
103- }
52+ defineLazyProperties ( Stream . Readable . prototype , 'internal/streams/operators' , [
53+ 'drop' , 'filter' , 'flatMap' , 'map' , 'take' ,
54+ 'every' , 'forEach' , 'reduce' , 'toArray' , 'some' , 'find' ,
55+ ] , false ) ;
10456Stream . Writable = require ( 'internal/streams/writable' ) ;
105- Stream . Duplex = require ( 'internal/streams/duplex' ) ;
106- Stream . Transform = require ( 'internal/streams/transform' ) ;
107- Stream . PassThrough = require ( 'internal/streams/passthrough' ) ;
108- Stream . duplexPair = require ( 'internal/streams/duplexpair' ) ;
109- Stream . pipeline = pipeline ;
57+ // These modules export a single function rather than named properties.
58+ // Set the prototype after creation to retain fast properties.
59+ const lazy = ObjectSetPrototypeOf ( {
60+ Duplex : 'internal/streams/duplex' ,
61+ Transform : 'internal/streams/transform' ,
62+ PassThrough : 'internal/streams/passthrough' ,
63+ duplexPair : 'internal/streams/duplexpair' ,
64+ compose : 'internal/streams/compose' ,
65+ } , null ) ;
66+ defineLazyProperties ( Stream , lazy , [
67+ 'Duplex' , 'Transform' , 'PassThrough' , 'duplexPair' ,
68+ ] ) ;
69+ defineLazyProperties ( Stream , 'internal/streams/pipeline' , [ 'pipeline' ] ) ;
11070const { addAbortSignal } = require ( 'internal/streams/add-abort-signal' ) ;
11171Stream . addAbortSignal = addAbortSignal ;
11272Stream . finished = eos ;
11373Stream . destroy = destroyer ;
114- Stream . compose = compose ;
74+ defineLazyProperties ( Stream , lazy , [ ' compose' ] ) ;
11575Stream . setDefaultHighWaterMark = setDefaultHighWaterMark ;
11676Stream . getDefaultHighWaterMark = getDefaultHighWaterMark ;
11777
@@ -120,22 +80,15 @@ ObjectDefineProperty(Stream, 'promises', {
12080 configurable : true ,
12181 enumerable : true ,
12282 get ( ) {
123- return promises ;
124- } ,
125- } ) ;
126-
127- ObjectDefineProperty ( pipeline , customPromisify , {
128- __proto__ : null ,
129- enumerable : true ,
130- get ( ) {
131- return promises . pipeline ;
83+ return promises ??= require ( 'stream/promises' ) ;
13284 } ,
13385} ) ;
13486
13587ObjectDefineProperty ( eos , customPromisify , {
13688 __proto__ : null ,
13789 enumerable : true ,
13890 get ( ) {
91+ promises ??= require ( 'stream/promises' ) ;
13992 return promises . finished ;
14093 } ,
14194} ) ;
0 commit comments