Skip to content

Commit

Permalink
lib: promise version of streams.finished call clean up
Browse files Browse the repository at this point in the history
ref: #44556
src: add autoCleanup logic to finished
docs: add autoCleanup true as default
  • Loading branch information
ntedgi committed Oct 2, 2022
1 parent 2a4452a commit b48145b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/api/stream.md
Expand Up @@ -2376,6 +2376,9 @@ 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.
**Default:** `true`.

* `callback` {Function} A callback function that takes an optional error
argument.
* Returns: {Function} A cleanup function which removes all registered
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 = true;
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

0 comments on commit b48145b

Please sign in to comment.