Skip to content

Commit 1650bcf

Browse files
zero1fivetargos
authored andcommitted
stream: add writableFinished
add a new getter to duplex stream to replace the property `this .writableState.finished` of the object that inherited duplex. Refs: #445 PR-URL: #28007 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent e55d0ef commit 1650bcf

5 files changed

+90
-0
lines changed

doc/api/stream.md

+10
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,16 @@ This property contains the number of bytes (or objects) in the queue
503503
ready to be written. The value provides introspection data regarding
504504
the status of the `highWaterMark`.
505505

506+
##### writable.writableFinished
507+
<!-- YAML
508+
added: v12.4.0
509+
-->
510+
511+
* {boolean}
512+
513+
Is `true` if all data has been flushed to the underlying system. After
514+
the [`'finish'`][] event has been emitted.
515+
506516
##### writable.writableObjectMode
507517
<!-- YAML
508518
added: v12.3.0

lib/_stream_duplex.js

+10
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ Object.defineProperty(Duplex.prototype, 'writableLength', {
9898
}
9999
});
100100

101+
Object.defineProperty(Duplex.prototype, 'writableFinished', {
102+
// Making it explicit this property is not enumerable
103+
// because otherwise some prototype manipulation in
104+
// userland will fail
105+
enumerable: false,
106+
get() {
107+
return this._writableState.finished;
108+
}
109+
});
110+
101111
// The no-half-open enforcer
102112
function onend() {
103113
// If the writable side ended, then we're ok.

lib/_stream_writable.js

+10
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,16 @@ Object.defineProperty(Writable.prototype, 'writableObjectMode', {
714714
}
715715
});
716716

717+
Object.defineProperty(Writable.prototype, 'writableFinished', {
718+
// Making it explicit this property is not enumerable
719+
// because otherwise some prototype manipulation in
720+
// userland will fail
721+
enumerable: false,
722+
get() {
723+
return this._writableState.finished;
724+
}
725+
});
726+
717727
Writable.prototype.destroy = destroyImpl.destroy;
718728
Writable.prototype._undestroy = destroyImpl.undestroy;
719729
Writable.prototype._destroy = function(err, cb) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { Duplex } = require('stream');
5+
const assert = require('assert');
6+
7+
// basic
8+
{
9+
// Find it on Duplex.prototype
10+
assert(Duplex.prototype.hasOwnProperty('writableFinished'));
11+
}
12+
13+
// event
14+
{
15+
const duplex = new Duplex();
16+
17+
duplex._write = (chunk, encoding, cb) => {
18+
// The state finished should start in false.
19+
assert.strictEqual(duplex.writableFinished, false);
20+
cb();
21+
};
22+
23+
duplex.on('finish', common.mustCall(() => {
24+
assert.strictEqual(duplex.writableFinished, true);
25+
}));
26+
27+
duplex.end('testing finished state', common.mustCall(() => {
28+
assert.strictEqual(duplex.writableFinished, true);
29+
}));
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const { Writable } = require('stream');
5+
const assert = require('assert');
6+
7+
// basic
8+
{
9+
// Find it on Writable.prototype
10+
assert(Writable.prototype.hasOwnProperty('writableFinished'));
11+
}
12+
13+
// event
14+
{
15+
const writable = new Writable();
16+
17+
writable._write = (chunk, encoding, cb) => {
18+
// The state finished should start in false.
19+
assert.strictEqual(writable.writableFinished, false);
20+
cb();
21+
};
22+
23+
writable.on('finish', common.mustCall(() => {
24+
assert.strictEqual(writable.writableFinished, true);
25+
}));
26+
27+
writable.end('testing finished state', common.mustCall(() => {
28+
assert.strictEqual(writable.writableFinished, true);
29+
}));
30+
}

0 commit comments

Comments
 (0)