Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: align OutgoingMessage.writable with streams #33655

Closed
wants to merge 2 commits into from
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
14 changes: 13 additions & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const HIGH_WATER_MARK = getDefaultHighWaterMark();
const { CRLF, debug } = common;

const kCorked = Symbol('corked');
const kWritable = Symbol('writable');

function nop() {}

Expand Down Expand Up @@ -97,7 +98,6 @@ function OutgoingMessage() {
// TCP socket and HTTP Parser and thus handle the backpressure.
this.outputSize = 0;

this.writable = true;
this.destroyed = false;

this._last = false;
Expand Down Expand Up @@ -127,6 +127,18 @@ function OutgoingMessage() {
ObjectSetPrototypeOf(OutgoingMessage.prototype, Stream.prototype);
ObjectSetPrototypeOf(OutgoingMessage, Stream);

ObjectDefineProperty(OutgoingMessage.prototype, 'writable', {
get() {
// TODO (ronag): Add errored state? write/end might error/destroy one
// tick later.
return this[kWritable] !== false && !this.destroyed && !this.finished;
},
set(val) {
// Backwards compatible.
this[kWritable] = !!val;
}
});

ObjectDefineProperty(OutgoingMessage.prototype, 'writableFinished', {
get() {
return (
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-http-outgoing-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const OutgoingMessage = http.OutgoingMessage;
{
const msg = new OutgoingMessage();
assert.strictEqual(msg.destroyed, false);
assert.strictEqual(msg.writable, true);
msg.destroy();
assert.strictEqual(msg.destroyed, true);
assert.strictEqual(msg.writable, false);
let callbackCalled = false;
msg.write('asd', common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
Expand Down
11 changes: 2 additions & 9 deletions test/parallel/test-http-outgoing-finish-writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ const common = require('../common');
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

const server = http.createServer(common.mustCall(function(req, res) {
assert.strictEqual(res.writable, true);
assert.strictEqual(res.finished, false);
Expand All @@ -14,7 +11,7 @@ const server = http.createServer(common.mustCall(function(req, res) {

// 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);
assert.strictEqual(res.writableEnded, true);

Expand All @@ -32,9 +29,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);
}));
1 change: 1 addition & 0 deletions test/parallel/test-http-outgoing-finish.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ function write(out) {
console.log(`ok - ${name} endCb`);
});
}));
assert.strictEqual(out.writable, false);
}
3 changes: 3 additions & 0 deletions test/parallel/test-http-outgoing-writableFinished.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ server.on('listening', common.mustCall(function() {
path: '/'
});

assert.strictEqual(clientRequest.writable, true);
assert.strictEqual(clientRequest.writableFinished, false);
clientRequest
.on('finish', common.mustCall(() => {
assert.strictEqual(clientRequest.writable, false);
assert.strictEqual(clientRequest.writableFinished, true);
}))
.end();
assert.strictEqual(clientRequest.writable, false);
assert.strictEqual(clientRequest.writableFinished, false);
}));
5 changes: 1 addition & 4 deletions test/parallel/test-http-writable-true-after-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ const common = require('../common');
const assert = require('assert');
const { get, createServer } = require('http');

// res.writable should not be set to false after it has finished sending
// Ref: https://github.com/nodejs/node/issues/15029
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please read through the issue to make sure you agree with the consequences of this change.


let internal;
let external;

// Proxy server
const server = createServer(common.mustCall((req, res) => {
const listener = common.mustCall(() => {
assert.strictEqual(res.writable, true);
assert.strictEqual(res.writable, false);
});

// on CentOS 5, 'finish' is emitted
Expand Down