@@ -70,41 +70,29 @@ const util = require('util');
70
70
util . inherits ( Transform , Duplex ) ;
71
71
72
72
73
- function TransformState ( stream ) {
74
- this . afterTransform = function ( er , data ) {
75
- return afterTransform ( stream , er , data ) ;
76
- } ;
77
-
78
- this . needTransform = false ;
79
- this . transforming = false ;
80
- this . writecb = null ;
81
- this . writechunk = null ;
82
- this . writeencoding = null ;
83
- }
84
-
85
- function afterTransform ( stream , er , data ) {
86
- var ts = stream . _transformState ;
73
+ function afterTransform ( er , data ) {
74
+ var ts = this . _transformState ;
87
75
ts . transforming = false ;
88
76
89
77
var cb = ts . writecb ;
90
78
91
79
if ( ! cb ) {
92
- return stream . emit ( 'error' ,
93
- new Error ( 'write callback called multiple times' ) ) ;
80
+ return this . emit ( 'error' ,
81
+ new Error ( 'write callback called multiple times' ) ) ;
94
82
}
95
83
96
84
ts . writechunk = null ;
97
85
ts . writecb = null ;
98
86
99
- if ( data !== null && data !== undefined )
100
- stream . push ( data ) ;
87
+ if ( data != null ) // single equals check for both `null` and ` undefined`
88
+ this . push ( data ) ;
101
89
102
90
cb ( er ) ;
103
91
104
- var rs = stream . _readableState ;
92
+ var rs = this . _readableState ;
105
93
rs . reading = false ;
106
94
if ( rs . needReadable || rs . length < rs . highWaterMark ) {
107
- stream . _read ( rs . highWaterMark ) ;
95
+ this . _read ( rs . highWaterMark ) ;
108
96
}
109
97
}
110
98
@@ -115,9 +103,14 @@ function Transform(options) {
115
103
116
104
Duplex . call ( this , options ) ;
117
105
118
- this . _transformState = new TransformState ( this ) ;
119
-
120
- var stream = this ;
106
+ this . _transformState = {
107
+ afterTransform : afterTransform . bind ( this ) ,
108
+ needTransform : false ,
109
+ transforming : false ,
110
+ writecb : null ,
111
+ writechunk : null ,
112
+ writeencoding : null
113
+ } ;
121
114
122
115
// start out asking for a readable event once data is transformed.
123
116
this . _readableState . needReadable = true ;
@@ -136,14 +129,17 @@ function Transform(options) {
136
129
}
137
130
138
131
// When the writable side finishes, then flush out anything remaining.
139
- this . once ( 'prefinish' , function ( ) {
140
- if ( typeof this . _flush === 'function' )
141
- this . _flush ( function ( er , data ) {
142
- done ( stream , er , data ) ;
143
- } ) ;
144
- else
145
- done ( stream ) ;
146
- } ) ;
132
+ this . on ( 'prefinish' , prefinish ) ;
133
+ }
134
+
135
+ function prefinish ( ) {
136
+ if ( typeof this . _flush === 'function' ) {
137
+ this . _flush ( ( er , data ) => {
138
+ done ( this , er , data ) ;
139
+ } ) ;
140
+ } else {
141
+ done ( this , null , null ) ;
142
+ }
147
143
}
148
144
149
145
Transform . prototype . push = function ( chunk , encoding ) {
@@ -208,18 +204,15 @@ function done(stream, er, data) {
208
204
if ( er )
209
205
return stream . emit ( 'error' , er ) ;
210
206
211
- if ( data !== null && data !== undefined )
207
+ if ( data != null ) // single equals check for both `null` and ` undefined`
212
208
stream . push ( data ) ;
213
209
214
210
// if there's nothing in the write buffer, then that means
215
211
// that nothing more will ever be provided
216
- var ws = stream . _writableState ;
217
- var ts = stream . _transformState ;
218
-
219
- if ( ws . length )
212
+ if ( stream . _writableState . length )
220
213
throw new Error ( 'Calling transform done when ws.length != 0' ) ;
221
214
222
- if ( ts . transforming )
215
+ if ( stream . _transformState . transforming )
223
216
throw new Error ( 'Calling transform done when still transforming' ) ;
224
217
225
218
return stream . push ( null ) ;
0 commit comments