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

http2: use getter replace use directly _witableState.finished #28007

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
10 changes: 10 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,16 @@ This property contains the number of bytes (or objects) in the queue
ready to be written. The value provides introspection data regarding
the status of the `highWaterMark`.

##### writable.writableFinished
<!-- YAML
added: v12.4.0
-->

* {boolean}

Is `true` if all data has been flushed to the underlying system. After
the [`'finish'`][] event has been emitted.

##### writable.writableObjectMode
<!-- YAML
added: v12.3.0
Expand Down
10 changes: 10 additions & 0 deletions lib/_stream_duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ Object.defineProperty(Duplex.prototype, 'writableLength', {
}
});

Object.defineProperty(Duplex.prototype, 'writableFinished', {
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
enumerable: false,
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
get() {
return this._writableState.finished;
}
});

// The no-half-open enforcer
function onend() {
// If the writable side ended, then we're ok.
Expand Down
10 changes: 10 additions & 0 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,16 @@ Object.defineProperty(Writable.prototype, 'writableObjectMode', {
}
});

Object.defineProperty(Writable.prototype, 'writableFinished', {
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
enumerable: false,
get() {
return this._writableState.finished;
}
});

Writable.prototype.destroy = destroyImpl.destroy;
Writable.prototype._undestroy = destroyImpl.undestroy;
Writable.prototype._destroy = function(err, cb) {
Expand Down
7 changes: 3 additions & 4 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ function closeStream(stream, code, rstStreamStatus = kSubmitRstStream) {
stream.setTimeout(0);
stream.removeAllListeners('timeout');

const { ending, finished } = stream._writableState;
const { ending } = stream._writableState;

if (!ending) {
// If the writable side of the Http2Stream is still open, emit the
Expand All @@ -1572,7 +1572,7 @@ function closeStream(stream, code, rstStreamStatus = kSubmitRstStream) {

if (rstStreamStatus !== kNoRstStream) {
const finishFn = finishCloseStream.bind(stream, code);
if (!ending || finished || code !== NGHTTP2_NO_ERROR ||
if (!ending || stream.writableFinished || code !== NGHTTP2_NO_ERROR ||
rstStreamStatus === kForceRstStream)
finishFn();
else
Expand Down Expand Up @@ -1982,8 +1982,7 @@ class Http2Stream extends Duplex {
return;
}

// TODO(mcollina): remove usage of _*State properties
if (this._writableState.finished) {
if (this.writableFinished) {
if (!this.readable && this.closed) {
this.destroy();
return;
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-stream-duplex-writable-finished.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common');
const { Duplex } = require('stream');
const assert = require('assert');

// basic
{
// Find it on Duplex.prototype
assert(Duplex.prototype.hasOwnProperty('writableFinished'));
}

// event
{
const duplex = new Duplex();

duplex._write = (chunk, encoding, cb) => {
// The state finished should start in false.
assert.strictEqual(duplex.writableFinished, false);
cb();
};

duplex.on('finish', common.mustCall(() => {
assert.strictEqual(duplex.writableFinished, true);
}));

duplex.end('testing finished state', common.mustCall(() => {
assert.strictEqual(duplex.writableFinished, true);
}));
}
30 changes: 30 additions & 0 deletions test/parallel/test-stream-writable-finished.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common');
const { Writable } = require('stream');
const assert = require('assert');

// basic
{
// Find it on Writable.prototype
assert(Writable.prototype.hasOwnProperty('writableFinished'));
}

// event
{
const writable = new Writable();

writable._write = (chunk, encoding, cb) => {
// The state finished should start in false.
assert.strictEqual(writable.writableFinished, false);
cb();
};

writable.on('finish', common.mustCall(() => {
assert.strictEqual(writable.writableFinished, true);
}));

writable.end('testing finished state', common.mustCall(() => {
assert.strictEqual(writable.writableFinished, true);
}));
}