Skip to content

Commit 65f209c

Browse files
jasnellMylesBorins
authored andcommitted
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 d3e2bf0 commit 65f209c

25 files changed

+45
-47
lines changed

doc/api/http2.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ All [`Http2Stream`][] instances are destroyed either when:
631631
When an `Http2Stream` instance is destroyed, an attempt will be made to send an
632632
`RST_STREAM` frame will be sent to the connected peer.
633633

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

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

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

lib/internal/http2/compat.js

+2-2
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

+3-5
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,8 @@ function onStreamTrailers() {
224224
return headersList;
225225
}
226226

227-
// Called when the stream is closed. The streamClosed event is emitted on the
228-
// Http2Stream instance. Note that this event is distinctly different than the
229-
// require('stream') interface 'close' event which deals with the state of the
230-
// Readable and Writable sides of the Duplex.
227+
// Called when the stream is closed. The close event is emitted on the
228+
// Http2Stream instance
231229
function onStreamClose(code) {
232230
const stream = this[kOwner];
233231
stream[kUpdateTimer]();
@@ -1473,7 +1471,7 @@ function continueStreamDestroy(err, callback) {
14731471
abort(this);
14741472
this.push(null); // Close the readable side
14751473
this.end(); // Close the writable side
1476-
process.nextTick(emit, this, 'streamClosed', code);
1474+
process.nextTick(emit, this, 'close', code);
14771475
}
14781476

14791477
function finishStreamDestroy() {

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

+1-1
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

+1-1
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

+1-1
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

+1-1
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

+2-2
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

+2-2
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

+1-1
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
}));

test/parallel/test-http2-multiheaders.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ server.listen(0, common.mustCall(() => {
5454
const client = http2.connect(`http://localhost:${server.address().port}`);
5555
const req = client.request(src);
5656
req.on('response', common.mustCall(checkHeaders));
57-
req.on('streamClosed', common.mustCall(() => {
57+
req.on('close', common.mustCall(() => {
5858
server.close();
5959
client.destroy();
6060
}));

test/parallel/test-http2-options-max-reserved-streams.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ server.on('stream', common.mustCall((stream) => {
3434
pushedStream.respond({ ':status': 200 });
3535
pushedStream.on('aborted', common.mustCall());
3636
pushedStream.on('error', common.mustNotCall());
37-
pushedStream.on('streamClosed',
37+
pushedStream.on('close',
3838
common.mustCall((code) => assert.strictEqual(code, 8)));
3939
}));
4040

@@ -67,12 +67,12 @@ server.on('listening', common.mustCall(() => {
6767
client.on('stream', common.mustCall((stream) => {
6868
stream.resume();
6969
stream.on('end', common.mustCall());
70-
stream.on('streamClosed', common.mustCall(maybeClose));
70+
stream.on('close', common.mustCall(maybeClose));
7171
}));
7272

7373
req.on('response', common.mustCall());
7474

7575
req.resume();
7676
req.on('end', common.mustCall());
77-
req.on('streamClosed', common.mustCall(maybeClose));
77+
req.on('close', common.mustCall(maybeClose));
7878
}));

test/parallel/test-http2-respond-file-errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ server.listen(0, common.mustCall(() => {
117117
const client = http2.connect(`http://localhost:${server.address().port}`);
118118
const req = client.request();
119119

120-
req.on('streamClosed', common.mustCall(() => {
120+
req.on('close', common.mustCall(() => {
121121
client.destroy();
122122
server.close();
123123
}));

test/parallel/test-http2-respond-file-fd-errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ server.listen(0, common.mustCall(() => {
144144
const client = http2.connect(`http://localhost:${server.address().port}`);
145145
const req = client.request();
146146

147-
req.on('streamClosed', common.mustCall(() => {
147+
req.on('close', common.mustCall(() => {
148148
client.destroy();
149149
server.close();
150150
}));

test/parallel/test-http2-respond-file-fd-range.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ server.listen(0, () => {
7171
req.on('end', common.mustCall(() => {
7272
assert.strictEqual(check, data.toString('utf8', 8, 11));
7373
}));
74-
req.on('streamClosed', common.mustCall(maybeClose));
74+
req.on('close', common.mustCall(maybeClose));
7575
req.end();
7676
}
7777

@@ -88,7 +88,7 @@ server.listen(0, () => {
8888
req.on('end', common.mustCall(() => {
8989
assert.strictEqual(check, data.toString('utf8', 8, 28));
9090
}));
91-
req.on('streamClosed', common.mustCall(maybeClose));
91+
req.on('close', common.mustCall(maybeClose));
9292
req.end();
9393
}
9494

test/parallel/test-http2-respond-no-data.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const server = http2.createServer();
1313
const status = [204, 205, 304];
1414

1515
server.on('stream', common.mustCall((stream) => {
16-
stream.on('streamClosed', common.mustCall(() => {
16+
stream.on('close', common.mustCall(() => {
1717
assert.strictEqual(stream.destroyed, true);
1818
}));
1919
stream.respond({ ':status': status.shift() });

test/parallel/test-http2-response-splitting.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ server.listen(0, common.mustCall(() => {
6767
}));
6868
req.resume();
6969
req.on('end', common.mustCall());
70-
req.on('streamClosed', common.mustCall(maybeClose));
70+
req.on('close', common.mustCall(maybeClose));
7171
}
7272

7373
doTest(str, 'location', str);

test/parallel/test-http2-server-rst-before-respond.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ server.on('listening', common.mustCall(() => {
3535

3636
req.on('headers', common.mustNotCall());
3737

38-
req.on('streamClosed', common.mustCall((code) => {
38+
req.on('close', common.mustCall((code) => {
3939
assert.strictEqual(h2.constants.NGHTTP2_NO_ERROR, code);
4040
server.close();
4141
client.destroy();

test/parallel/test-http2-server-rst-stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ server.listen(0, common.mustCall(() => {
4343
':method': 'POST',
4444
rstmethod: test[0]
4545
});
46-
req.on('streamClosed', common.mustCall((code) => {
46+
req.on('close', common.mustCall((code) => {
4747
assert.strictEqual(code, test[1]);
4848
countdown.dec();
4949
}));

test/parallel/test-http2-server-socketerror.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ server.listen(0, common.mustCall(() => {
4949
const req = client.request();
5050
req.resume();
5151
req.on('end', common.mustCall());
52-
req.on('streamClosed', common.mustCall(() => {
52+
req.on('close', common.mustCall(() => {
5353
client.destroy();
5454
server.close();
5555
}));

test/parallel/test-http2-stream-client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ server.listen(0, common.mustCall(() => {
2121
const client = http2.connect(`http://localhost:${server.address().port}`);
2222
const req = client.request();
2323
req.resume();
24-
req.on('streamClosed', common.mustCall(() => {
24+
req.on('close', common.mustCall(() => {
2525
client.destroy();
2626
server.close();
2727
}));

test/parallel/test-http2-stream-destroy-event-order.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let req;
1111
const server = http2.createServer();
1212
server.on('stream', common.mustCall((stream) => {
1313
stream.on('error', common.mustCall(() => {
14-
stream.on('streamClosed', common.mustCall((code) => {
14+
stream.on('close', common.mustCall((code) => {
1515
assert.strictEqual(code, 2);
1616
client.destroy();
1717
server.close();

test/parallel/test-http2-too-large-headers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ server.listen(0, common.mustCall(() => {
2222
type: Error,
2323
message: 'Stream closed with error code 11'
2424
}));
25-
req.on('streamClosed', common.mustCall((code) => {
25+
req.on('close', common.mustCall((code) => {
2626
assert.strictEqual(code, NGHTTP2_ENHANCE_YOUR_CALM);
2727
server.close();
2828
client.destroy();

test/parallel/test-http2-too-many-headers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ server.listen(0, common.mustCall(() => {
2525
type: Error,
2626
message: 'Stream closed with error code 11'
2727
}));
28-
req.on('streamClosed', common.mustCall((code) => {
28+
req.on('close', common.mustCall((code) => {
2929
assert.strictEqual(code, NGHTTP2_ENHANCE_YOUR_CALM);
3030
server.close();
3131
client.destroy();

test/parallel/test-http2-write-callbacks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ server.listen(0, common.mustCall(() => {
3030
req.setEncoding('utf8');
3131
req.on('data', (chunk) => actual += chunk);
3232
req.on('end', common.mustCall(() => assert.strictEqual(actual, 'abcxyz')));
33-
req.on('streamClosed', common.mustCall(() => {
33+
req.on('close', common.mustCall(() => {
3434
client.destroy();
3535
server.close();
3636
}));

0 commit comments

Comments
 (0)