| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 617819e commit 3d3df0c
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1578,6 +1578,9 @@ further errors except from `_destroy()` may be emitted as `'error'`. | |||
| 1578 | 1578 | <!-- YAML | |
| 1579 | 1579 | added: v10.0.0 | |
| 1580 | 1580 | changes: | |
| 1581 | + - version: REPLACEME | ||
| 1582 | + pr-url: https://github.com/nodejs/node/pull/37354 | ||
| 1583 | + description: The `signal` option was added. | ||
| 1581 | 1584 | - version: v14.0.0 | |
| 1582 | 1585 | pr-url: https://github.com/nodejs/node/pull/32158 | |
| 1583 | 1586 | description: The `finished(stream, cb)` will wait for the `'close'` event | |
@@ -1604,6 +1607,10 @@ changes: | |||
| 1604 | 1607 | * `writable` {boolean} When set to `false`, the callback will be called when | |
| 1605 | 1608 | the stream ends even though the stream might still be writable. | |
| 1606 | 1609 | **Default:** `true`. | |
| 1610 | + * `signal` {AbortSignal} allows aborting the wait for the stream finish. The | ||
| 1611 | + underlying stream will *not* be aborted if the signal is aborted. The | ||
| 1612 | + callback will get called with an `AbortError`. All registered | ||
| 1613 | + listeners added by this function will also be removed. | ||
| 1607 | 1614 | * `callback` {Function} A callback function that takes an optional error | |
| 1608 | 1615 | argument. | |
| 1609 | 1616 | * Returns: {Function} A cleanup function which removes all registered | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,11 +3,20 @@ | |||
| 3 | 3 | ||
| 4 | 4 | 'use strict'; | |
| 5 | 5 | ||
| 6 | + const { | ||
| 7 | + FunctionPrototypeCall, | ||
| 8 | + ReflectApply, | ||
| 9 | + } = primordials; | ||
| 10 | + const { | ||
| 11 | + AbortError, | ||
| 12 | + codes, | ||
| 13 | + } = require('internal/errors'); | ||
| 6 | 14 | const { | |
| 7 | 15 | ERR_STREAM_PREMATURE_CLOSE | |
| 8 | - } = require('internal/errors').codes; | ||
| 16 | + } = codes; | ||
| 9 | 17 | const { once } = require('internal/util'); | |
| 10 | 18 | const { | |
| 19 | + validateAbortSignal, | ||
| 11 | 20 | validateFunction, | |
| 12 | 21 | validateObject, | |
| 13 | 22 | } = require('internal/validators'); | |
@@ -64,6 +73,7 @@ function eos(stream, options, callback) { | |||
| 64 | 73 | validateObject(options, 'options'); | |
| 65 | 74 | } | |
| 66 | 75 | validateFunction(callback, 'callback'); | |
| 76 | + validateAbortSignal(options.signal, 'options.signal'); | ||
| 67 | 77 | ||
| 68 | 78 | callback = once(callback); | |
| 69 | 79 | ||
@@ -185,7 +195,7 @@ function eos(stream, options, callback) { | |||
| 185 | 195 | }); | |
| 186 | 196 | } | |
| 187 | 197 | ||
| 188 | - return function() { | ||
| 198 | + const cleanup = () => { | ||
| 189 | 199 | callback = nop; | |
| 190 | 200 | stream.removeListener('aborted', onclose); | |
| 191 | 201 | stream.removeListener('complete', onfinish); | |
@@ -199,6 +209,27 @@ function eos(stream, options, callback) { | |||
| 199 | 209 | stream.removeListener('error', onerror); | |
| 200 | 210 | stream.removeListener('close', onclose); | |
| 201 | 211 | }; | |
| 212 | + | ||
| 213 | + if (options.signal && !closed) { | ||
| 214 | + const abort = () => { | ||
| 215 | + // Keep it because cleanup removes it. | ||
| 216 | + const endCallback = callback; | ||
| 217 | + cleanup(); | ||
| 218 | + FunctionPrototypeCall(endCallback, stream, new AbortError()); | ||
| 219 | + }; | ||
| 220 | + if (options.signal.aborted) { | ||
| 221 | + process.nextTick(abort); | ||
| 222 | + } else { | ||
| 223 | + const originalCallback = callback; | ||
| 224 | + callback = once((...args) => { | ||
| 225 | + options.signal.removeEventListener('abort', abort); | ||
| 226 | + ReflectApply(originalCallback, stream, args); | ||
| 227 | + }); | ||
| 228 | + options.signal.addEventListener('abort', abort); | ||
| 229 | + } | ||
| 230 | + } | ||
| 231 | + | ||
| 232 | + return cleanup; | ||
| 202 | 233 | } | |
| 203 | 234 | ||
| 204 | 235 | module.exports = eos; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -92,6 +92,83 @@ const http = require('http'); | |||
| 92 | 92 | run(); | |
| 93 | 93 | } | |
| 94 | 94 | ||
| 95 | + { | ||
| 96 | + // Check pre-cancelled | ||
| 97 | + const signal = new EventTarget(); | ||
| 98 | + signal.aborted = true; | ||
| 99 | + | ||
| 100 | + const rs = Readable.from((function* () {})()); | ||
| 101 | + finished(rs, { signal }, common.mustCall((err) => { | ||
| 102 | + assert.strictEqual(err.name, 'AbortError'); | ||
| 103 | + })); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + { | ||
| 107 | + // Check cancelled before the stream ends sync. | ||
| 108 | + const ac = new AbortController(); | ||
| 109 | + const { signal } = ac; | ||
| 110 | + | ||
| 111 | + const rs = Readable.from((function* () {})()); | ||
| 112 | + finished(rs, { signal }, common.mustCall((err) => { | ||
| 113 | + assert.strictEqual(err.name, 'AbortError'); | ||
| 114 | + })); | ||
| 115 | + | ||
| 116 | + ac.abort(); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + { | ||
| 120 | + // Check cancelled before the stream ends async. | ||
| 121 | + const ac = new AbortController(); | ||
| 122 | + const { signal } = ac; | ||
| 123 | + | ||
| 124 | + const rs = Readable.from((function* () {})()); | ||
| 125 | + setTimeout(() => ac.abort(), 1); | ||
| 126 | + finished(rs, { signal }, common.mustCall((err) => { | ||
| 127 | + assert.strictEqual(err.name, 'AbortError'); | ||
| 128 | + })); | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + { | ||
| 132 | + // Check cancelled after doesn't throw. | ||
| 133 | + const ac = new AbortController(); | ||
| 134 | + const { signal } = ac; | ||
| 135 | + | ||
| 136 | + const rs = Readable.from((function* () { | ||
| 137 | + yield 5; | ||
| 138 | + setImmediate(() => ac.abort()); | ||
| 139 | + })()); | ||
| 140 | + rs.resume(); | ||
| 141 | + finished(rs, { signal }, common.mustSucceed()); | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + { | ||
| 145 | + // Promisified abort works | ||
| 146 | + const finishedPromise = promisify(finished); | ||
| 147 | + async function run() { | ||
| 148 | + const ac = new AbortController(); | ||
| 149 | + const { signal } = ac; | ||
| 150 | + const rs = Readable.from((function* () {})()); | ||
| 151 | + setImmediate(() => ac.abort()); | ||
| 152 | + await finishedPromise(rs, { signal }); | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + assert.rejects(run, { name: 'AbortError' }).then(common.mustCall()); | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + { | ||
| 159 | + // Promisified pre-aborted works | ||
| 160 | + const finishedPromise = promisify(finished); | ||
| 161 | + async function run() { | ||
| 162 | + const signal = new EventTarget(); | ||
| 163 | + signal.aborted = true; | ||
| 164 | + const rs = Readable.from((function* () {})()); | ||
| 165 | + await finishedPromise(rs, { signal }); | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + assert.rejects(run, { name: 'AbortError' }).then(common.mustCall()); | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + | ||
| 95 | 172 | { | |
| 96 | 173 | const rs = fs.createReadStream('file-does-not-exist'); | |
| 97 | 174 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments