Skip to content
This repository was archived by the owner on Jul 6, 2018. It is now read-only.

Commit cf8b5da

Browse files
committed
http2: Do not imply a callback to compat res.write
Fixes: #135 PR-URL: #137 Reviewed-By: Colin Ihrig <[email protected]>
1 parent 797f94f commit cf8b5da

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

lib/internal/http2/compat.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,18 @@ class Http2ServerResponse extends Stream {
416416

417417
write(chunk, encoding, cb) {
418418
var stream = this[kStream];
419+
420+
if (typeof encoding === 'function') {
421+
cb = encoding;
422+
encoding = 'utf8';
423+
}
424+
419425
if (stream === undefined) {
420-
cb(new Error('HTTP/2 Stream has been closed'));
426+
var err = new Error('HTTP/2 Stream has been closed');
427+
if (cb)
428+
cb(err);
429+
else
430+
this.emit('error', err);
421431
return;
422432
}
423433
var beginSend = this[kBeginSend];
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const h2 = require('http2');
6+
7+
// Http2ServerResponse.write does not imply there is a callback
8+
9+
{
10+
const server = h2.createServer();
11+
server.listen(0, common.mustCall(function() {
12+
const port = server.address().port;
13+
const url = `http://localhost:${port}`;
14+
const client = h2.connect(url, common.mustCall(function() {
15+
const headers = {
16+
':path': '/',
17+
':method': 'GET',
18+
':scheme': 'http',
19+
':authority': `localhost:${port}`
20+
};
21+
const request = client.request(headers);
22+
request.end();
23+
request.resume();
24+
}));
25+
26+
server.once('request', common.mustCall(function(request, response) {
27+
client.destroy();
28+
response.stream.session.on('close', common.mustCall(function() {
29+
response.on('error', common.mustCall(function(err) {
30+
assert.strictEqual(err.message, 'HTTP/2 Stream has been closed');
31+
}));
32+
response.write('muahaha');
33+
server.close();
34+
}));
35+
}));
36+
}));
37+
}
38+
39+
{
40+
const server = h2.createServer();
41+
server.listen(0, common.mustCall(function() {
42+
const port = server.address().port;
43+
const url = `http://localhost:${port}`;
44+
const client = h2.connect(url, common.mustCall(function() {
45+
const headers = {
46+
':path': '/',
47+
':method': 'get',
48+
':scheme': 'http',
49+
':authority': `localhost:${port}`
50+
};
51+
const request = client.request(headers);
52+
request.end();
53+
request.resume();
54+
}));
55+
56+
server.once('request', common.mustCall(function(request, response) {
57+
client.destroy();
58+
response.stream.session.on('close', common.mustCall(function() {
59+
response.write('muahaha', common.mustCall(function(err) {
60+
assert.strictEqual(err.message, 'HTTP/2 Stream has been closed');
61+
}));
62+
server.close();
63+
}));
64+
}));
65+
}));
66+
}
67+
68+
{
69+
const server = h2.createServer();
70+
server.listen(0, common.mustCall(function() {
71+
const port = server.address().port;
72+
const url = `http://localhost:${port}`;
73+
const client = h2.connect(url, common.mustCall(function() {
74+
const headers = {
75+
':path': '/',
76+
':method': 'get',
77+
':scheme': 'http',
78+
':authority': `localhost:${port}`
79+
};
80+
const request = client.request(headers);
81+
request.end();
82+
request.resume();
83+
}));
84+
85+
server.once('request', common.mustCall(function(request, response) {
86+
response.stream.session.on('close', common.mustCall(function() {
87+
response.write('muahaha', 'utf8', common.mustCall(function(err) {
88+
assert.strictEqual(err.message, 'HTTP/2 Stream has been closed');
89+
}));
90+
server.close();
91+
}));
92+
client.destroy();
93+
}));
94+
}));
95+
}

0 commit comments

Comments
 (0)