Skip to content

Commit 1b99542

Browse files
committed
http2: use 'close' event instead of 'streamClosed'
PR-URL: #17328 Fixes: #15303 Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Sebastiaan Deckers <[email protected]>
1 parent df33d8d commit 1b99542

25 files changed

+45
-47
lines changed

doc/api/http2.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ All [`Http2Stream`][] instances are destroyed either when:
633633
When an `Http2Stream` instance is destroyed, an attempt will be made to send an
634634
`RST_STREAM` frame will be sent to the connected peer.
635635

636-
Once the `Http2Stream` instance is destroyed, the `'streamClosed'` event will
636+
When the `Http2Stream` instance is destroyed, the `'close'` event will
637637
be emitted. Because `Http2Stream` is an instance of `stream.Duplex`, the
638638
`'end'` event will also be emitted if the stream data is currently flowing.
639639
The `'error'` event may also be emitted if `http2stream.destroy()` was called
@@ -655,6 +655,18 @@ abnormally aborted in mid-communication.
655655
*Note*: The `'aborted'` event will only be emitted if the `Http2Stream`
656656
writable side has not been ended.
657657

658+
#### Event: 'close'
659+
<!-- YAML
660+
added: v8.4.0
661+
-->
662+
663+
The `'close'` event is emitted when the `Http2Stream` is destroyed. Once
664+
this event is emitted, the `Http2Stream` instance is no longer usable.
665+
666+
The listener callback is passed a single argument specifying the HTTP/2 error
667+
code specified when closing the stream. If the code is any value other than
668+
`NGHTTP2_NO_ERROR` (`0`), an `'error'` event will also be emitted.
669+
658670
#### Event: 'error'
659671
<!-- YAML
660672
added: v8.4.0
@@ -674,18 +686,6 @@ argument identifying the frame type, and an integer argument identifying the
674686
error code. The `Http2Stream` instance will be destroyed immediately after the
675687
`'frameError'` event is emitted.
676688

677-
#### Event: 'streamClosed'
678-
<!-- YAML
679-
added: v8.4.0
680-
-->
681-
682-
The `'streamClosed'` event is emitted when the `Http2Stream` is destroyed. Once
683-
this event is emitted, the `Http2Stream` instance is no longer usable.
684-
685-
The listener callback is passed a single argument specifying the HTTP/2 error
686-
code specified when closing the stream. If the code is any value other than
687-
`NGHTTP2_NO_ERROR` (`0`), an `'error'` event will also be emitted.
688-
689689
#### Event: 'timeout'
690690
<!-- YAML
691691
added: v8.4.0

lib/internal/http2/compat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class Http2ServerRequest extends Readable {
250250
stream.on('close', onStreamClosedRequest);
251251
stream.on('aborted', onStreamAbortedRequest);
252252
const onfinish = this[kFinish].bind(this);
253-
stream.on('streamClosed', onfinish);
253+
stream.on('close', onfinish);
254254
stream.on('finish', onfinish);
255255
this.on('pause', onRequestPause);
256256
this.on('resume', onRequestResume);
@@ -383,7 +383,7 @@ class Http2ServerResponse extends Stream {
383383
stream.on('close', onStreamClosedResponse);
384384
stream.on('aborted', onStreamAbortedResponse);
385385
const onfinish = this[kFinish].bind(this);
386-
stream.on('streamClosed', onfinish);
386+
stream.on('close', onfinish);
387387
stream.on('finish', onfinish);
388388
}
389389

lib/internal/http2/core.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,8 @@ function onStreamTrailers() {
226226
return headersList;
227227
}
228228

229-
// Called when the stream is closed. The streamClosed event is emitted on the
230-
// Http2Stream instance. Note that this event is distinctly different than the
231-
// require('stream') interface 'close' event which deals with the state of the
232-
// Readable and Writable sides of the Duplex.
229+
// Called when the stream is closed. The close event is emitted on the
230+
// Http2Stream instance
233231
function onStreamClose(code) {
234232
const stream = this[kOwner];
235233
stream[kUpdateTimer]();
@@ -1485,7 +1483,7 @@ function continueStreamDestroy(err, callback) {
14851483
abort(this);
14861484
this.push(null); // Close the readable side
14871485
this.end(); // Close the writable side
1488-
process.nextTick(emit, this, 'streamClosed', code);
1486+
process.nextTick(emit, this, 'close', code);
14891487
}
14901488

14911489
function finishStreamDestroy() {

test/parallel/test-http2-client-http1-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ server.listen(0, common.mustCall(() => {
1313
const client = http2.connect(`http://localhost:${server.address().port}`);
1414

1515
const req = client.request();
16-
req.on('streamClosed', common.mustCall());
16+
req.on('close', common.mustCall());
1717

1818
client.on('error', common.expectsError({
1919
code: 'ERR_HTTP2_ERROR',

test/parallel/test-http2-client-rststream-before-connect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ server.on('listening', common.mustCall(() => {
2727
// second call doesn't do anything
2828
assert.doesNotThrow(() => req.rstStream(8));
2929

30-
req.on('streamClosed', common.mustCall((code) => {
30+
req.on('close', common.mustCall((code) => {
3131
assert.strictEqual(req.destroyed, true);
3232
assert.strictEqual(code, 0);
3333
server.close();

test/parallel/test-http2-client-stream-destroy-before-connect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ server.on('listening', common.mustCall(() => {
4141
})(err);
4242
}));
4343

44-
req.on('streamClosed', common.mustCall((code) => {
44+
req.on('close', common.mustCall((code) => {
4545
assert.strictEqual(req.rstCode, NGHTTP2_INTERNAL_ERROR);
4646
assert.strictEqual(code, NGHTTP2_INTERNAL_ERROR);
4747
server.close();

test/parallel/test-http2-client-unescaped-path.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ server.listen(0, common.mustCall(() => {
3030
type: Error,
3131
message: 'Stream closed with error code 1'
3232
}));
33-
req.on('streamClosed', common.mustCall(maybeClose));
33+
req.on('close', common.mustCall(maybeClose));
3434
}
3535

3636
for (let i = 0; i <= count; i += 1)

test/parallel/test-http2-compat-serverresponse-end.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ const {
183183

184184

185185
{
186-
// Should be able to call .end with cb from stream 'streamClosed'
186+
// Should be able to call .end with cb from stream 'close'
187187
const server = createServer(mustCall((request, response) => {
188188
response.writeHead(HTTP_STATUS_OK, { foo: 'bar' });
189-
response.stream.on('streamClosed', mustCall(() => {
189+
response.stream.on('close', mustCall(() => {
190190
response.end(mustCall());
191191
}));
192192
}));

test/parallel/test-http2-compat-socket.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ server.on('request', common.mustCall(function(request, response) {
6363
assert.strictEqual(request.socket.connecting, false);
6464

6565
// socket events are bound and emitted on Http2Stream
66-
request.socket.on('streamClosed', common.mustCall());
67-
request.socket.once('streamClosed', common.mustCall());
66+
request.socket.on('close', common.mustCall());
67+
request.socket.once('close', common.mustCall());
6868
request.socket.on('testEvent', common.mustCall());
6969
request.socket.emit('testEvent');
7070
}));

test/parallel/test-http2-multiheaders-raw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ server.on('stream', common.mustCall((stream, headers, flags, rawHeaders) => {
4242
server.listen(0, common.mustCall(() => {
4343
const client = http2.connect(`http://localhost:${server.address().port}`);
4444
const req = client.request(src);
45-
req.on('streamClosed', common.mustCall(() => {
45+
req.on('close', common.mustCall(() => {
4646
server.close();
4747
client.destroy();
4848
}));

0 commit comments

Comments
 (0)