| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 17ef1bb commit cc2393c
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2363,6 +2363,7 @@ changes: | |||
| 2363 | 2363 | --> | |
| 2364 | 2364 | ||
| 2365 | 2365 | * `stream` {Stream} A readable and/or writable stream. | |
| 2366 | + | ||
| 2366 | 2367 | * `options` {Object} | |
| 2367 | 2368 | * `error` {boolean} If set to `false`, then a call to `emit('error', err)` is | |
| 2368 | 2369 | not treated as finished. **Default:** `true`. | |
@@ -2376,8 +2377,12 @@ changes: | |||
| 2376 | 2377 | underlying stream will _not_ be aborted if the signal is aborted. The | |
| 2377 | 2378 | callback will get called with an `AbortError`. All registered | |
| 2378 | 2379 | listeners added by this function will also be removed. | |
| 2380 | + * `cleanup` {boolean} remove all registered stream listeners. | ||
| 2381 | + **Default:** `false`. | ||
| 2382 | + | ||
| 2379 | 2383 | * `callback` {Function} A callback function that takes an optional error | |
| 2380 | 2384 | argument. | |
| 2385 | + | ||
| 2381 | 2386 | * Returns: {Function} A cleanup function which removes all registered | |
| 2382 | 2387 | listeners. | |
| 2383 | 2388 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,7 @@ const { | |||
| 19 | 19 | validateAbortSignal, | |
| 20 | 20 | validateFunction, | |
| 21 | 21 | validateObject, | |
| 22 | + validateBoolean | ||
| 22 | 23 | } = require('internal/validators'); | |
| 23 | 24 | ||
| 24 | 25 | const { Promise } = primordials; | |
@@ -243,8 +244,19 @@ function eos(stream, options, callback) { | |||
| 243 | 244 | } | |
| 244 | 245 | ||
| 245 | 246 | function finished(stream, opts) { | |
| 247 | + let autoCleanup = false; | ||
| 248 | + if (opts === null) { | ||
| 249 | + opts = kEmptyObject; | ||
| 250 | + } | ||
| 251 | + if (opts?.cleanup) { | ||
| 252 | + validateBoolean(opts.cleanup, 'cleanup'); | ||
| 253 | + autoCleanup = opts.cleanup; | ||
| 254 | + } | ||
| 246 | 255 | return new Promise((resolve, reject) => { | |
| 247 | - eos(stream, opts, (err) => { | ||
| 256 | + const cleanup = eos(stream, opts, (err) => { | ||
| 257 | + if (autoCleanup) { | ||
| 258 | + cleanup(); | ||
| 259 | + } | ||
| 248 | 260 | if (err) { | |
| 249 | 261 | reject(err); | |
| 250 | 262 | } else { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,13 +3,10 @@ | |||
| 3 | 3 | const common = require('../common'); | |
| 4 | 4 | const stream = require('stream'); | |
| 5 | 5 | const { | |
| 6 | - Readable, | ||
| 7 | - Writable, | ||
| 8 | - promises, | ||
| 6 | + Readable, Writable, promises, | ||
| 9 | 7 | } = stream; | |
| 10 | 8 | const { | |
| 11 | - finished, | ||
| 12 | - pipeline, | ||
| 9 | + finished, pipeline, | ||
| 13 | 10 | } = require('stream/promises'); | |
| 14 | 11 | const fs = require('fs'); | |
| 15 | 12 | const assert = require('assert'); | |
@@ -24,14 +21,11 @@ assert.strictEqual(finished, promisify(stream.finished)); | |||
| 24 | 21 | { | |
| 25 | 22 | let finished = false; | |
| 26 | 23 | const processed = []; | |
| 27 | - const expected = [ | ||
| 28 | - Buffer.from('a'), | ||
| 29 | - Buffer.from('b'), | ||
| 30 | - Buffer.from('c'), | ||
| 31 | - ]; | ||
| 24 | + const expected = [Buffer.from('a'), Buffer.from('b'), Buffer.from('c')]; | ||
| 32 | 25 | ||
| 33 | 26 | const read = new Readable({ | |
| 34 | - read() { } | ||
| 27 | + read() { | ||
| 28 | + } | ||
| 35 | 29 | }); | |
| 36 | 30 | ||
| 37 | 31 | const write = new Writable({ | |
@@ -59,7 +53,8 @@ assert.strictEqual(finished, promisify(stream.finished)); | |||
| 59 | 53 | // pipeline error | |
| 60 | 54 | { | |
| 61 | 55 | const read = new Readable({ | |
| 62 | - read() { } | ||
| 56 | + read() { | ||
| 57 | + } | ||
| 63 | 58 | }); | |
| 64 | 59 | ||
| 65 | 60 | const write = new Writable({ | |
@@ -101,3 +96,50 @@ assert.strictEqual(finished, promisify(stream.finished)); | |||
| 101 | 96 | code: 'ENOENT' | |
| 102 | 97 | }).then(common.mustCall()); | |
| 103 | 98 | } | |
| 99 | + | ||
| 100 | + { | ||
| 101 | + const streamObj = new Readable(); | ||
| 102 | + assert.throws(() => { | ||
| 103 | + // Passing cleanup option not as boolean | ||
| 104 | + // should throw error | ||
| 105 | + finished(streamObj, { cleanup: 2 }); | ||
| 106 | + }, { code: 'ERR_INVALID_ARG_TYPE' }); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + // Below code should not throw any errors as the | ||
| 110 | + // streamObj is `Stream` and cleanup is boolean | ||
| 111 | + { | ||
| 112 | + const streamObj = new Readable(); | ||
| 113 | + finished(streamObj, { cleanup: true }); | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + | ||
| 117 | + // Cleanup function should not be called when cleanup is set to false | ||
| 118 | + // listenerCount should be 1 after calling finish | ||
| 119 | + { | ||
| 120 | + const streamObj = new Writable(); | ||
| 121 | + assert.strictEqual(streamObj.listenerCount('end'), 0); | ||
| 122 | + finished(streamObj, { cleanup: false }).then(() => { | ||
| 123 | + assert.strictEqual(streamObj.listenerCount('end'), 1); | ||
| 124 | + }); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + // Cleanup function should be called when cleanup is set to true | ||
| 128 | + // listenerCount should be 0 after calling finish | ||
| 129 | + { | ||
| 130 | + const streamObj = new Writable(); | ||
| 131 | + assert.strictEqual(streamObj.listenerCount('end'), 0); | ||
| 132 | + finished(streamObj, { cleanup: true }).then(() => { | ||
| 133 | + assert.strictEqual(streamObj.listenerCount('end'), 0); | ||
| 134 | + }); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + // Cleanup function should not be called when cleanup has not been set | ||
| 138 | + // listenerCount should be 1 after calling finish | ||
| 139 | + { | ||
| 140 | + const streamObj = new Writable(); | ||
| 141 | + assert.strictEqual(streamObj.listenerCount('end'), 0); | ||
| 142 | + finished(streamObj).then(() => { | ||
| 143 | + assert.strictEqual(streamObj.listenerCount('end'), 1); | ||
| 144 | + }); | ||
| 145 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments