Skip to content

Commit

Permalink
http2: Do not imply a callback to compat res.write
Browse files Browse the repository at this point in the history
Fixes: nodejs#135
PR-URL: nodejs#137
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
mcollina authored and jasnell committed Jul 14, 2017
1 parent f5062cd commit 619ce80
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,18 @@ class Http2ServerResponse extends Stream {

write(chunk, encoding, cb) {
var stream = this[kStream];

if (typeof encoding === 'function') {
cb = encoding;
encoding = 'utf8';
}

if (stream === undefined) {
cb(new Error('HTTP/2 Stream has been closed'));
var err = new Error('HTTP/2 Stream has been closed');
if (cb)
cb(err);
else
this.emit('error', err);
return;
}
var beginSend = this[kBeginSend];
Expand Down
95 changes: 95 additions & 0 deletions test/parallel/test-http2-compat-serverresponse-write-no-cb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const h2 = require('http2');

// Http2ServerResponse.write does not imply there is a callback

{
const server = h2.createServer();
server.listen(0, common.mustCall(function() {
const port = server.address().port;
const url = `http://localhost:${port}`;
const client = h2.connect(url, common.mustCall(function() {
const headers = {
':path': '/',
':method': 'GET',
':scheme': 'http',
':authority': `localhost:${port}`
};
const request = client.request(headers);
request.end();
request.resume();
}));

server.once('request', common.mustCall(function(request, response) {
client.destroy();
response.stream.session.on('close', common.mustCall(function() {
response.on('error', common.mustCall(function(err) {
assert.strictEqual(err.message, 'HTTP/2 Stream has been closed');
}));
response.write('muahaha');
server.close();
}));
}));
}));
}

{
const server = h2.createServer();
server.listen(0, common.mustCall(function() {
const port = server.address().port;
const url = `http://localhost:${port}`;
const client = h2.connect(url, common.mustCall(function() {
const headers = {
':path': '/',
':method': 'get',
':scheme': 'http',
':authority': `localhost:${port}`
};
const request = client.request(headers);
request.end();
request.resume();
}));

server.once('request', common.mustCall(function(request, response) {
client.destroy();
response.stream.session.on('close', common.mustCall(function() {
response.write('muahaha', common.mustCall(function(err) {
assert.strictEqual(err.message, 'HTTP/2 Stream has been closed');
}));
server.close();
}));
}));
}));
}

{
const server = h2.createServer();
server.listen(0, common.mustCall(function() {
const port = server.address().port;
const url = `http://localhost:${port}`;
const client = h2.connect(url, common.mustCall(function() {
const headers = {
':path': '/',
':method': 'get',
':scheme': 'http',
':authority': `localhost:${port}`
};
const request = client.request(headers);
request.end();
request.resume();
}));

server.once('request', common.mustCall(function(request, response) {
response.stream.session.on('close', common.mustCall(function() {
response.write('muahaha', 'utf8', common.mustCall(function(err) {
assert.strictEqual(err.message, 'HTTP/2 Stream has been closed');
}));
server.close();
}));
client.destroy();
}));
}));
}

0 comments on commit 619ce80

Please sign in to comment.