@@ -66,6 +66,7 @@ class IncomingForm extends EventEmitter {
6666 'bytesExpected' ,
6767 'bytesReceived' ,
6868 '_parser' ,
69+ 'req' ,
6970 ] . forEach ( ( key ) => {
7071 this [ key ] = null ;
7172 } ) ;
@@ -110,41 +111,42 @@ class IncomingForm extends EventEmitter {
110111 return this ;
111112 }
112113
113- parse ( req , cb ) {
114- this . pause = ( ) => {
115- try {
116- req . pause ( ) ;
117- } catch ( err ) {
118- // the stream was destroyed
119- if ( ! this . ended ) {
120- // before it was completed, crash & burn
121- this . _error ( err ) ;
122- }
123- return false ;
114+ pause ( ) {
115+ try {
116+ this . req . pause ( ) ;
117+ } catch ( err ) {
118+ // the stream was destroyed
119+ if ( ! this . ended ) {
120+ // before it was completed, crash & burn
121+ this . _error ( err ) ;
124122 }
125- return true ;
126- } ;
123+ return false ;
124+ }
125+ return true ;
126+ }
127127
128- this . resume = ( ) => {
129- try {
130- req . resume ( ) ;
131- } catch ( err ) {
132- // the stream was destroyed
133- if ( ! this . ended ) {
134- // before it was completed, crash & burn
135- this . _error ( err ) ;
136- }
137- return false ;
128+ resume ( ) {
129+ try {
130+ this . req . resume ( ) ;
131+ } catch ( err ) {
132+ // the stream was destroyed
133+ if ( ! this . ended ) {
134+ // before it was completed, crash & burn
135+ this . _error ( err ) ;
138136 }
137+ return false ;
138+ }
139139
140- return true ;
141- } ;
140+ return true ;
141+ }
142+
143+ parse ( req , cb ) {
144+ this . req = req ;
142145
143146 // Setup callback first, so we don't miss anything from data events emitted immediately.
144147 if ( cb ) {
145148 const callback = once ( dezalgo ( cb ) ) ;
146149 this . fields = { } ;
147- let mockFields = '' ;
148150 const files = { } ;
149151
150152 this . on ( 'field' , ( name , value ) => {
@@ -245,16 +247,6 @@ class IncomingForm extends EventEmitter {
245247 return this . bytesReceived ;
246248 }
247249
248- pause ( ) {
249- // this does nothing, unless overwritten in IncomingForm.parse
250- return false ;
251- }
252-
253- resume ( ) {
254- // this does nothing, unless overwritten in IncomingForm.parse
255- return false ;
256- }
257-
258250 onPart ( part ) {
259251 // this method can be overwritten by the user
260252 this . _handlePart ( part ) ;
@@ -412,17 +404,12 @@ class IncomingForm extends EventEmitter {
412404 return ;
413405 }
414406
415- const results = [ ] ;
416- const _dummyParser = new DummyParser ( this , this . options ) ;
417407
418- // eslint-disable-next-line no-plusplus
419- for ( let idx = 0 ; idx < this . _plugins . length ; idx ++ ) {
420- const plugin = this . _plugins [ idx ] ;
421-
422- let pluginReturn = null ;
408+ new DummyParser ( this , this . options ) ;
423409
410+ this . _plugins . forEach ( ( plugin , idx ) => {
424411 try {
425- pluginReturn = plugin ( this , this . options ) || this ;
412+ plugin ( this , this . options ) || this ;
426413 } catch ( err ) {
427414 // directly throw from the `form.parse` method;
428415 // there is no other better way, except a handle through options
@@ -435,42 +422,23 @@ class IncomingForm extends EventEmitter {
435422 throw error ;
436423 }
437424
438- Object . assign ( this , pluginReturn ) ;
439-
440425 // todo: use Set/Map and pass plugin name instead of the `idx` index
441- this . emit ( 'plugin' , idx , pluginReturn ) ;
442- results . push ( pluginReturn ) ;
443- }
444-
445- this . emit ( 'pluginsResults' , results ) ;
446-
447- // NOTE: probably not needed, because we check options.enabledPlugins in the constructor
448- // if (results.length === 0 /* && results.length !== this._plugins.length */) {
449- // this._error(
450- // new Error(
451- // `bad content-type header, unknown content-type: ${this.headers['content-type']}`,
452- // ),
453- // );
454- // }
426+ this . emit ( 'plugin' , idx ) ;
427+ } ) ;
455428 }
456429
457430 _error ( err , eventName = 'error' ) {
458- // if (!err && this.error) {
459- // this.emit('error', this.error);
460- // return;
461- // }
462431 if ( this . error || this . ended ) {
463432 return ;
464433 }
465434
435+ this . req = null ;
466436 this . error = err ;
467437 this . emit ( eventName , err ) ;
468438
469- if ( Array . isArray ( this . openedFiles ) ) {
470- this . openedFiles . forEach ( ( file ) => {
471- file . destroy ( ) ;
472- } ) ;
473- }
439+ this . openedFiles . forEach ( ( file ) => {
440+ file . destroy ( ) ;
441+ } ) ;
474442 }
475443
476444 _parseContentLength ( ) {
@@ -585,7 +553,7 @@ class IncomingForm extends EventEmitter {
585553 }
586554
587555 _setUpMaxFields ( ) {
588- if ( this . options . maxFields !== 0 ) {
556+ if ( this . options . maxFields !== Infinity ) {
589557 let fieldsCount = 0 ;
590558 this . on ( 'field' , ( ) => {
591559 fieldsCount += 1 ;
@@ -621,13 +589,10 @@ class IncomingForm extends EventEmitter {
621589 }
622590
623591 _maybeEnd ( ) {
624- // console.log('ended', this.ended);
625- // console.log('_flushing', this._flushing);
626- // console.log('error', this.error);
627592 if ( ! this . ended || this . _flushing || this . error ) {
628593 return ;
629594 }
630-
595+ this . req = null ;
631596 this . emit ( 'end' ) ;
632597 }
633598}
0 commit comments