@@ -19,7 +19,6 @@ const {
19
19
} = require ( 'internal/errors' ) . codes ;
20
20
const { kSocket } = require ( 'internal/http2/util' ) ;
21
21
22
- const kFinish = Symbol ( 'finish' ) ;
23
22
const kBeginSend = Symbol ( 'begin-send' ) ;
24
23
const kState = Symbol ( 'state' ) ;
25
24
const kStream = Symbol ( 'stream' ) ;
@@ -223,6 +222,27 @@ const proxySocketHandler = {
223
222
}
224
223
} ;
225
224
225
+ function onStreamCloseRequest ( ) {
226
+ const req = this [ kRequest ] ;
227
+
228
+ if ( req === undefined )
229
+ return ;
230
+
231
+ const state = req [ kState ] ;
232
+ state . closed = true ;
233
+
234
+ req . push ( null ) ;
235
+ // if the user didn't interact with incoming data and didn't pipe it,
236
+ // dump it for compatibility with http1
237
+ if ( ! state . didRead && ! req . _readableState . resumeScheduled )
238
+ req . resume ( ) ;
239
+
240
+ this [ kProxySocket ] = null ;
241
+ this [ kRequest ] = undefined ;
242
+
243
+ req . emit ( 'close' ) ;
244
+ }
245
+
226
246
class Http2ServerRequest extends Readable {
227
247
constructor ( stream , headers , options , rawHeaders ) {
228
248
super ( options ) ;
@@ -246,7 +266,7 @@ class Http2ServerRequest extends Readable {
246
266
stream . on ( 'end' , onStreamEnd ) ;
247
267
stream . on ( 'error' , onStreamError ) ;
248
268
stream . on ( 'aborted' , onStreamAbortedRequest ) ;
249
- stream . on ( 'close' , this [ kFinish ] . bind ( this ) ) ;
269
+ stream . on ( 'close' , onStreamCloseRequest ) ;
250
270
this . on ( 'pause' , onRequestPause ) ;
251
271
this . on ( 'resume' , onRequestResume ) ;
252
272
}
@@ -349,24 +369,30 @@ class Http2ServerRequest extends Readable {
349
369
return ;
350
370
this [ kStream ] . setTimeout ( msecs , callback ) ;
351
371
}
352
-
353
- [ kFinish ] ( ) {
354
- const state = this [ kState ] ;
355
- if ( state . closed )
356
- return ;
357
- state . closed = true ;
358
- this . push ( null ) ;
359
- this [ kStream ] [ kRequest ] = undefined ;
360
- // if the user didn't interact with incoming data and didn't pipe it,
361
- // dump it for compatibility with http1
362
- if ( ! state . didRead && ! this . _readableState . resumeScheduled )
363
- this . resume ( ) ;
364
- this . emit ( 'close' ) ;
365
- }
366
372
}
367
373
368
374
function onStreamTrailersReady ( ) {
369
- this [ kStream ] . sendTrailers ( this [ kTrailers ] ) ;
375
+ this . sendTrailers ( this [ kResponse ] [ kTrailers ] ) ;
376
+ }
377
+
378
+ function onStreamCloseResponse ( ) {
379
+ const res = this [ kResponse ] ;
380
+
381
+ if ( res === undefined )
382
+ return ;
383
+
384
+ const state = res [ kState ] ;
385
+
386
+ if ( this . headRequest !== state . headRequest )
387
+ return ;
388
+
389
+ state . closed = true ;
390
+
391
+ this [ kProxySocket ] = null ;
392
+ this [ kResponse ] = undefined ;
393
+
394
+ res . emit ( 'finish' ) ;
395
+ res . emit ( 'close' ) ;
370
396
}
371
397
372
398
class Http2ServerResponse extends Stream {
@@ -387,8 +413,8 @@ class Http2ServerResponse extends Stream {
387
413
this . writable = true ;
388
414
stream . on ( 'drain' , onStreamDrain ) ;
389
415
stream . on ( 'aborted' , onStreamAbortedResponse ) ;
390
- stream . on ( 'close' , this [ kFinish ] . bind ( this ) ) ;
391
- stream . on ( 'wantTrailers' , onStreamTrailersReady . bind ( this ) ) ;
416
+ stream . on ( 'close' , onStreamCloseResponse ) ;
417
+ stream . on ( 'wantTrailers' , onStreamTrailersReady ) ;
392
418
}
393
419
394
420
// User land modules such as finalhandler just check truthiness of this
@@ -619,7 +645,7 @@ class Http2ServerResponse extends Stream {
619
645
this . writeHead ( this [ kState ] . statusCode ) ;
620
646
621
647
if ( isFinished )
622
- this [ kFinish ] ( ) ;
648
+ onStreamCloseResponse . call ( stream ) ;
623
649
else
624
650
stream . end ( ) ;
625
651
@@ -665,18 +691,6 @@ class Http2ServerResponse extends Stream {
665
691
this [ kStream ] . respond ( headers , options ) ;
666
692
}
667
693
668
- [ kFinish ] ( ) {
669
- const stream = this [ kStream ] ;
670
- const state = this [ kState ] ;
671
- if ( state . closed || stream . headRequest !== state . headRequest )
672
- return ;
673
- state . closed = true ;
674
- this [ kProxySocket ] = null ;
675
- stream [ kResponse ] = undefined ;
676
- this . emit ( 'finish' ) ;
677
- this . emit ( 'close' ) ;
678
- }
679
-
680
694
// TODO doesn't support callbacks
681
695
writeContinue ( ) {
682
696
const stream = this [ kStream ] ;
0 commit comments