Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ function OutgoingMessage() {
Object.setPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
Object.setPrototypeOf(OutgoingMessage, Stream);

Object.defineProperty(OutgoingMessage.prototype, '_writableState', {
get: function() {
if (!this._writableStateProxy) {
const msg = this;
this._writableStateProxy = {
get finished() {
return (
msg.finished &&
msg.outputSize === 0 &&
(!msg.socket || msg.socket.writableLength === 0)
);
},
get ended() {
return msg.finished;
}
};
}
return this._writableStateProxy;
}
});

Object.defineProperty(OutgoingMessage.prototype, '_headers', {
get: internalUtil.deprecate(function() {
Expand Down Expand Up @@ -699,6 +719,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
12 changes: 3 additions & 9 deletions test/parallel/test-http-outgoing-finish-writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ const assert = require('assert');
const http = require('http');

// Verify that after calling end() on an `OutgoingMessage` (or a type that
// inherits from `OutgoingMessage`), its `writable` property is not set to false
// inherits from `OutgoingMessage`), its `writable` property is set to false

const server = http.createServer(common.mustCall(function(req, res) {
assert.strictEqual(res.writable, true);
assert.strictEqual(res.finished, false);
res.end();

// res.writable is set to false after it has finished sending
// Ref: https://github.com/nodejs/node/issues/15029
assert.strictEqual(res.writable, true);
assert.strictEqual(res.writable, false);
assert.strictEqual(res.finished, true);

server.close();
Expand All @@ -30,9 +28,5 @@ server.on('listening', common.mustCall(function() {

assert.strictEqual(clientRequest.writable, true);
clientRequest.end();

// Writable is still true when close
// THIS IS LEGACY, we cannot change it
// unless we break error detection
assert.strictEqual(clientRequest.writable, true);
assert.strictEqual(clientRequest.writable, false);
}));
42 changes: 0 additions & 42 deletions test/parallel/test-http-writable-true-after-close.js

This file was deleted.

29 changes: 29 additions & 0 deletions test/parallel/test-stream-pipeline-http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const common = require('../common');
const { get, createServer } = require('http');
const { pipeline } = require('stream');
const assert = require('assert');

let innerRequest;

// Http server
createServer(common.mustCall((req, res) => {
res.writeHead(200);
setTimeout(() => {
innerRequest.abort();
res.end('Hello World\n');
}, 1000);
})).listen(3000);

// Proxy server
createServer(common.mustCall((req, res) => {
get('http://127.0.0.1:3000', (inner) => {
pipeline(inner, res, (err) => {
assert(err);
});
});
}))
.listen(3001, common.mustCall(() => {
innerRequest = get('http://127.0.0.1:3001');
}));