@@ -26,6 +26,7 @@ const {
26
26
NumberIsInteger,
27
27
NumberIsNaN,
28
28
ObjectDefineProperties,
29
+ ObjectKeys,
29
30
ObjectSetPrototypeOf,
30
31
Set,
31
32
SymbolAsyncIterator,
@@ -1007,83 +1008,49 @@ function flow(stream) {
1007
1008
// This is *not* part of the readable stream interface.
1008
1009
// It is an ugly unfortunate mess of history.
1009
1010
Readable . prototype . wrap = function ( stream ) {
1010
- const state = this . _readableState ;
1011
1011
let paused = false ;
1012
1012
1013
- stream . on ( 'end' , ( ) => {
1014
- debug ( 'wrapped end' ) ;
1015
- if ( state . decoder && ! state . ended ) {
1016
- const chunk = state . decoder . end ( ) ;
1017
- if ( chunk && chunk . length )
1018
- this . push ( chunk ) ;
1019
- }
1020
-
1021
- this . push ( null ) ;
1022
- } ) ;
1013
+ // TODO (ronag): Should this.destroy(err) emit
1014
+ // 'error' on the wrapped stream? Would require
1015
+ // a static factory method, e.g. Readable.wrap(stream).
1023
1016
1024
1017
stream . on ( 'data' , ( chunk ) => {
1025
- debug ( 'wrapped data' ) ;
1026
- if ( state . decoder )
1027
- chunk = state . decoder . write ( chunk ) ;
1028
-
1029
- // Don't skip over falsy values in objectMode.
1030
- if ( state . objectMode && ( chunk === null || chunk === undefined ) )
1031
- return ;
1032
- else if ( ! state . objectMode && ( ! chunk || ! chunk . length ) )
1033
- return ;
1034
-
1035
- const ret = this . push ( chunk ) ;
1036
- if ( ! ret ) {
1018
+ if ( ! this . push ( chunk ) && stream . pause ) {
1037
1019
paused = true ;
1038
1020
stream . pause ( ) ;
1039
1021
}
1040
1022
} ) ;
1041
1023
1042
- // Proxy all the other methods. Important when wrapping filters and duplexes.
1043
- for ( const i in stream ) {
1044
- if ( this [ i ] === undefined && typeof stream [ i ] === 'function' ) {
1045
- this [ i ] = function methodWrap ( method ) {
1046
- return function methodWrapReturnFunction ( ) {
1047
- return stream [ method ] . apply ( stream , arguments ) ;
1048
- } ;
1049
- } ( i ) ;
1050
- }
1051
- }
1024
+ stream . on ( 'end' , ( ) => {
1025
+ this . push ( null ) ;
1026
+ } ) ;
1052
1027
1053
1028
stream . on ( 'error' , ( err ) => {
1054
1029
errorOrDestroy ( this , err ) ;
1055
1030
} ) ;
1056
1031
1057
1032
stream . on ( 'close' , ( ) => {
1058
- // TODO(ronag): Update readable state?
1059
- this . emit ( 'close' ) ;
1033
+ this . destroy ( ) ;
1060
1034
} ) ;
1061
1035
1062
1036
stream . on ( 'destroy' , ( ) => {
1063
- // TODO(ronag): this.destroy()?
1064
- this . emit ( 'destroy' ) ;
1037
+ this . destroy ( ) ;
1065
1038
} ) ;
1066
1039
1067
- stream . on ( 'pause' , ( ) => {
1068
- // TODO(ronag): this.pause()?
1069
- this . emit ( 'pause' ) ;
1070
- } ) ;
1071
-
1072
- stream . on ( 'resume' , ( ) => {
1073
- // TODO(ronag): this.resume()?
1074
- this . emit ( 'resume' ) ;
1075
- } ) ;
1076
-
1077
- // When we try to consume some more bytes, simply unpause the
1078
- // underlying stream.
1079
- this . _read = ( n ) => {
1080
- debug ( 'wrapped _read' , n ) ;
1081
- if ( paused ) {
1040
+ this . _read = ( ) => {
1041
+ if ( paused && stream . resume ) {
1082
1042
paused = false ;
1083
1043
stream . resume ( ) ;
1084
1044
}
1085
1045
} ;
1086
1046
1047
+ // Proxy all the other methods. Important when wrapping filters and duplexes.
1048
+ for ( const i of ObjectKeys ( stream ) ) {
1049
+ if ( this [ i ] === undefined && typeof stream [ i ] === 'function' ) {
1050
+ this [ i ] = stream [ i ] . bind ( stream ) ;
1051
+ }
1052
+ }
1053
+
1087
1054
return this ;
1088
1055
} ;
1089
1056
0 commit comments