Skip to content

Commit de2b047

Browse files
ronagMylesBorins
authored andcommitted
http2: avoid bind and properly clean up in compat
PR-URL: #20374 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent a41c44a commit de2b047

1 file changed

Lines changed: 46 additions & 32 deletions

File tree

‎lib/internal/http2/compat.js‎

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const {
1919
}=require('internal/errors').codes;
2020
const{ kSocket }=require('internal/http2/util');
2121

22-
constkFinish=Symbol('finish');
2322
constkBeginSend=Symbol('begin-send');
2423
constkState=Symbol('state');
2524
constkStream=Symbol('stream');
@@ -223,6 +222,27 @@ const proxySocketHandler = {
223222
}
224223
};
225224

225+
functiononStreamCloseRequest(){
226+
constreq=this[kRequest];
227+
228+
if(req===undefined)
229+
return;
230+
231+
conststate=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+
226246
classHttp2ServerRequestextendsReadable{
227247
constructor(stream,headers,options,rawHeaders){
228248
super(options);
@@ -246,7 +266,7 @@ class Http2ServerRequest extends Readable {
246266
stream.on('end',onStreamEnd);
247267
stream.on('error',onStreamError);
248268
stream.on('aborted',onStreamAbortedRequest);
249-
stream.on('close',this[kFinish].bind(this));
269+
stream.on('close',onStreamCloseRequest);
250270
this.on('pause',onRequestPause);
251271
this.on('resume',onRequestResume);
252272
}
@@ -349,24 +369,30 @@ class Http2ServerRequest extends Readable {
349369
return;
350370
this[kStream].setTimeout(msecs,callback);
351371
}
352-
353-
[kFinish](){
354-
conststate=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-
}
366372
}
367373

368374
functiononStreamTrailersReady(){
369-
this[kStream].sendTrailers(this[kTrailers]);
375+
this.sendTrailers(this[kResponse][kTrailers]);
376+
}
377+
378+
functiononStreamCloseResponse(){
379+
constres=this[kResponse];
380+
381+
if(res===undefined)
382+
return;
383+
384+
conststate=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');
370396
}
371397

372398
classHttp2ServerResponseextendsStream{
@@ -387,8 +413,8 @@ class Http2ServerResponse extends Stream {
387413
this.writable=true;
388414
stream.on('drain',onStreamDrain);
389415
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);
392418
}
393419

394420
// User land modules such as finalhandler just check truthiness of this
@@ -619,7 +645,7 @@ class Http2ServerResponse extends Stream {
619645
this.writeHead(this[kState].statusCode);
620646

621647
if(isFinished)
622-
this[kFinish]();
648+
onStreamCloseResponse.call(stream);
623649
else
624650
stream.end();
625651

@@ -665,18 +691,6 @@ class Http2ServerResponse extends Stream {
665691
this[kStream].respond(headers,options);
666692
}
667693

668-
[kFinish](){
669-
conststream=this[kStream];
670-
conststate=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-
680694
// TODO doesn't support callbacks
681695
writeContinue(){
682696
conststream=this[kStream];

0 commit comments

Comments
 (0)