Skip to content

Commit

Permalink
http: OutgoingMessage change writable after end
Browse files Browse the repository at this point in the history
When an OutgoingMessage is closed (for example, using the `end`
method), its 'writable' property should be changed to false - since it
is not writable anymore. The 'writable' property should have the
opposite value of the 'finished' property.
  • Loading branch information
Kasher committed Jul 7, 2017
1 parent 6bf4c23 commit 20b6326
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
this.connection.uncork();

this.finished = true;
this.writable = false;

// There is the first message on the outgoing queue, and we've sent
// everything to the socket.
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-http-outgoing-finish-writable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer(common.mustCall(function(req, res) {
assert(res.writable, 'Res should be writable when it is received \
and opened.');
assert(!res.finished, 'Res shouldn\'t be finished when it is received \
and opened.');
res.end();
assert(!res.writable, 'Res shouldn\'t be writable after it was closed.');
assert(res.finished, 'Res should be finished after it was closed.');

server.close();
}));

server.listen(0);

server.on('listening', common.mustCall(function() {
http.request({
port: server.address().port,
method: 'GET',
path: '/'
}).end();
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';
const common = require('../common');
const http = require('http');
const OutgoingMessage = require('_http_outgoing').OutgoingMessage;

const server = http.createServer(common.mustCall(function(req, res) {
console.log('Got a request, piping an OutgoingMessage to it.');
const outgointMessage = new OutgoingMessage();
outgointMessage.pipe(res);

setTimeout(() => {
console.log('Closing response.');
res.end();
outgointMessage.emit('data', 'some data');

// If we got here - 'write after end' wasn't raised and the test passed.
setTimeout(() => server.close(), 10);
}, 10);

setInterval(() => {
console.log('Emitting some data to outgointMessage');
outgointMessage.emit('data', 'some data');
}, 1).unref();

}));

server.listen(0);

server.on('listening', common.mustCall(function() {
http.request({
port: server.address().port,
method: 'GET',
path: '/'
}).end();
}));

0 comments on commit 20b6326

Please sign in to comment.