@@ -66,6 +66,7 @@ class IncomingForm extends EventEmitter {
66
66
'bytesExpected' ,
67
67
'bytesReceived' ,
68
68
'_parser' ,
69
+ 'req' ,
69
70
] . forEach ( ( key ) => {
70
71
this [ key ] = null ;
71
72
} ) ;
@@ -110,41 +111,42 @@ class IncomingForm extends EventEmitter {
110
111
return this ;
111
112
}
112
113
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 ) ;
124
122
}
125
- return true ;
126
- } ;
123
+ return false ;
124
+ }
125
+ return true ;
126
+ }
127
127
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 ) ;
138
136
}
137
+ return false ;
138
+ }
139
139
140
- return true ;
141
- } ;
140
+ return true ;
141
+ }
142
+
143
+ parse ( req , cb ) {
144
+ this . req = req ;
142
145
143
146
// Setup callback first, so we don't miss anything from data events emitted immediately.
144
147
if ( cb ) {
145
148
const callback = once ( dezalgo ( cb ) ) ;
146
149
this . fields = { } ;
147
- let mockFields = '' ;
148
150
const files = { } ;
149
151
150
152
this . on ( 'field' , ( name , value ) => {
@@ -245,16 +247,6 @@ class IncomingForm extends EventEmitter {
245
247
return this . bytesReceived ;
246
248
}
247
249
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
-
258
250
onPart ( part ) {
259
251
// this method can be overwritten by the user
260
252
this . _handlePart ( part ) ;
@@ -412,17 +404,12 @@ class IncomingForm extends EventEmitter {
412
404
return ;
413
405
}
414
406
415
- const results = [ ] ;
416
- const _dummyParser = new DummyParser ( this , this . options ) ;
417
407
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 ) ;
423
409
410
+ this . _plugins . forEach ( ( plugin , idx ) => {
424
411
try {
425
- pluginReturn = plugin ( this , this . options ) || this ;
412
+ plugin ( this , this . options ) || this ;
426
413
} catch ( err ) {
427
414
// directly throw from the `form.parse` method;
428
415
// there is no other better way, except a handle through options
@@ -435,42 +422,23 @@ class IncomingForm extends EventEmitter {
435
422
throw error ;
436
423
}
437
424
438
- Object . assign ( this , pluginReturn ) ;
439
-
440
425
// 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
+ } ) ;
455
428
}
456
429
457
430
_error ( err , eventName = 'error' ) {
458
- // if (!err && this.error) {
459
- // this.emit('error', this.error);
460
- // return;
461
- // }
462
431
if ( this . error || this . ended ) {
463
432
return ;
464
433
}
465
434
435
+ this . req = null ;
466
436
this . error = err ;
467
437
this . emit ( eventName , err ) ;
468
438
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
+ } ) ;
474
442
}
475
443
476
444
_parseContentLength ( ) {
@@ -585,7 +553,7 @@ class IncomingForm extends EventEmitter {
585
553
}
586
554
587
555
_setUpMaxFields ( ) {
588
- if ( this . options . maxFields !== 0 ) {
556
+ if ( this . options . maxFields !== Infinity ) {
589
557
let fieldsCount = 0 ;
590
558
this . on ( 'field' , ( ) => {
591
559
fieldsCount += 1 ;
@@ -621,13 +589,10 @@ class IncomingForm extends EventEmitter {
621
589
}
622
590
623
591
_maybeEnd ( ) {
624
- // console.log('ended', this.ended);
625
- // console.log('_flushing', this._flushing);
626
- // console.log('error', this.error);
627
592
if ( ! this . ended || this . _flushing || this . error ) {
628
593
return ;
629
594
}
630
-
595
+ this . req = null ;
631
596
this . emit ( 'end' ) ;
632
597
}
633
598
}
0 commit comments