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

lib: promise version of streams.finished call cleanup after eos #44862

Merged
merged 4 commits into from Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api/stream.md
Expand Up @@ -2363,6 +2363,7 @@ changes:
-->

* `stream` {Stream} A readable and/or writable stream.

* `options` {Object}
* `error` {boolean} If set to `false`, then a call to `emit('error', err)` is
not treated as finished. **Default:** `true`.
Expand All @@ -2376,8 +2377,12 @@ changes:
underlying stream will _not_ be aborted if the signal is aborted. The
callback will get called with an `AbortError`. All registered
listeners added by this function will also be removed.
* `autoCleanup` {boolean} remove all registered stream listeners.
ntedgi marked this conversation as resolved.
Show resolved Hide resolved
**Default:** `false`.

* `callback` {Function} A callback function that takes an optional error
argument.

* Returns: {Function} A cleanup function which removes all registered
listeners.

Expand Down
14 changes: 13 additions & 1 deletion lib/internal/streams/end-of-stream.js
Expand Up @@ -19,6 +19,7 @@ const {
validateAbortSignal,
validateFunction,
validateObject,
validateBoolean
} = require('internal/validators');

const { Promise } = primordials;
Expand Down Expand Up @@ -243,8 +244,19 @@ function eos(stream, options, callback) {
}

function finished(stream, opts) {
let autoCleanup = false;
if (opts === null) {
opts = kEmptyObject;
}
if (opts?.autoCleanup) {
validateBoolean(opts.autoCleanup, 'autoCleanup');
autoCleanup = opts.autoCleanup;
}
return new Promise((resolve, reject) => {
eos(stream, opts, (err) => {
const cleanup = eos(stream, opts, (err) => {
if (autoCleanup) {
cleanup();
}
if (err) {
reject(err);
} else {
Expand Down
66 changes: 54 additions & 12 deletions test/parallel/test-stream-promises.js
Expand Up @@ -3,13 +3,10 @@
const common = require('../common');
const stream = require('stream');
const {
Readable,
Writable,
promises,
Readable, Writable, promises,
} = stream;
const {
finished,
pipeline,
finished, pipeline,
} = require('stream/promises');
const fs = require('fs');
const assert = require('assert');
Expand All @@ -24,14 +21,11 @@ assert.strictEqual(finished, promisify(stream.finished));
{
let finished = false;
const processed = [];
const expected = [
Buffer.from('a'),
Buffer.from('b'),
Buffer.from('c'),
];
const expected = [Buffer.from('a'), Buffer.from('b'), Buffer.from('c')];

const read = new Readable({
read() { }
read() {
}
});

const write = new Writable({
Expand Down Expand Up @@ -59,7 +53,8 @@ assert.strictEqual(finished, promisify(stream.finished));
// pipeline error
{
const read = new Readable({
read() { }
read() {
}
});

const write = new Writable({
Expand Down Expand Up @@ -101,3 +96,50 @@ assert.strictEqual(finished, promisify(stream.finished));
code: 'ENOENT'
}).then(common.mustCall());
}

{
const streamObj = new Readable();
assert.throws(() => {
// Passing autoCleanup option not as boolean
// should throw error
finished(streamObj, { autoCleanup: 2 });
}, { code: 'ERR_INVALID_ARG_TYPE' });
}

// Below code should not throw any errors as the
// streamObj is `Stream` and autoCleanup is boolean
{
const streamObj = new Readable();
finished(streamObj, { autoCleanup: true });
}


// Cleanup function should not be called when autoCleanup is set to false
// listenerCount should be 1 after calling finish
{
const streamObj = new Writable();
assert.strictEqual(streamObj.listenerCount('end'), 0);
finished(streamObj, { autoCleanup: false }).then(() => {
assert.strictEqual(streamObj.listenerCount('end'), 1);
});
}

// Cleanup function should be called when autoCleanup is set to true
// listenerCount should be 0 after calling finish
{
const streamObj = new Writable();
assert.strictEqual(streamObj.listenerCount('end'), 0);
finished(streamObj, { autoCleanup: true }).then(() => {
assert.strictEqual(streamObj.listenerCount('end'), 0);
});
}

// Cleanup function should not be called when autoCleanup has not been set
// listenerCount should be 1 after calling finish
{
const streamObj = new Writable();
assert.strictEqual(streamObj.listenerCount('end'), 0);
finished(streamObj).then(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Same

assert.strictEqual(streamObj.listenerCount('end'), 1);
});
}