Skip to content

Commit ae50653

Browse files
committed
http2: compat avoid bind and propertly cleanup stream.
1 parent ad6a65b commit ae50653

File tree

1 file changed

+46
-32
lines changed

1 file changed

+46
-32
lines changed

lib/internal/http2/compat.js

+46-32
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-
const kFinish = Symbol('finish');
2322
const kBeginSend = Symbol('begin-send');
2423
const kState = Symbol('state');
2524
const kStream = Symbol('stream');
@@ -223,6 +222,27 @@ const proxySocketHandler = {
223222
}
224223
};
225224

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+
226246
class Http2ServerRequest extends Readable {
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-
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-
}
366372
}
367373

368374
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');
370396
}
371397

372398
class Http2ServerResponse extends Stream {
@@ -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-
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-
680694
// TODO doesn't support callbacks
681695
writeContinue() {
682696
const stream = this[kStream];

0 commit comments

Comments
 (0)