GeezerVison is showing github.com — stream: reject operators on empty streams with aborted signal · nodejs/node@10ffe22
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/streams/operators.js +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -153,6 +153,10 @@ function map(fn, options) { | 153 | 153 | pump(); | 154 | 154 |
| 155 | 155 | try { | | 156 | + if (signal.aborted) { | | 157 | + throw new AbortError(); | | 158 | + } | | 159 | + | 156 | 160 | while (true) { | 157 | 161 | while (queue.length > 0) { | 158 | 162 | const val = await queue[0]; | @@ -293,6 +297,13 @@ async function toArray(options) { | 293 | 297 | validateAbortSignal(options.signal, 'options.signal'); | 294 | 298 | } | 295 | 299 |
| | 300 | + if (options?.signal?.aborted) { | | 301 | + const err = new AbortError(undefined, { cause: options.signal.reason }); | | 302 | + this.once('error', () => {}); // The error is already propagated | | 303 | + await finished(this.destroy(err)); | | 304 | + throw err; | | 305 | + } | | 306 | + | 296 | 307 | const result = []; | 297 | 308 | for await (const val of this) { | 298 | 309 | if (options?.signal?.aborted) { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-filter.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -174,3 +174,13 @@ const { setTimeout } = require('timers/promises'); | 174 | 174 | // Check that map isn't getting called. | 175 | 175 | stream.filter(() => true); | 176 | 176 | } | | 177 | + | | 178 | +{ | | 179 | + // Already aborted AbortSignal on an empty stream | | 180 | + const stream = Readable.from([]).filter(common.mustNotCall(), { | | 181 | + signal: AbortSignal.abort(), | | 182 | + }); | | 183 | + assert.rejects(stream.toArray(), { | | 184 | + name: 'AbortError', | | 185 | + }).then(common.mustCall()); | | 186 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-flatMap.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -138,3 +138,13 @@ function oneTo5() { | 138 | 138 | // Check that map isn't getting called. | 139 | 139 | stream.flatMap(() => true); | 140 | 140 | } | | 141 | + | | 142 | +{ | | 143 | + // Already aborted AbortSignal on an empty stream | | 144 | + const stream = Readable.from([]).flatMap(common.mustNotCall(), { | | 145 | + signal: AbortSignal.abort(), | | 146 | + }); | | 147 | + assert.rejects(stream.toArray(), { | | 148 | + name: 'AbortError', | | 149 | + }).then(common.mustCall()); | | 150 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-forEach.js +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -137,3 +137,12 @@ const { once } = require('events'); | 137 | 137 | // Check that map isn't getting called. | 138 | 138 | stream.forEach(() => true); | 139 | 139 | } | | 140 | + | | 141 | +{ | | 142 | + // Already aborted AbortSignal on an empty stream | | 143 | + assert.rejects(Readable.from([]).forEach(common.mustNotCall(), { | | 144 | + signal: AbortSignal.abort(), | | 145 | + }), { | | 146 | + name: 'AbortError', | | 147 | + }).then(common.mustCall()); | | 148 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-map.js +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -358,3 +358,13 @@ function createDependentPromises(n) { | 358 | 358 | const stream = Readable.from([1, 2, 3, 4, 5]).map((x) => x); | 359 | 359 | assert.strictEqual(stream.readable, true); | 360 | 360 | } | | 361 | + | | 362 | +{ | | 363 | + // Already aborted AbortSignal on an empty stream | | 364 | + const stream = Readable.from([]).map(common.mustNotCall(), { | | 365 | + signal: AbortSignal.abort(), | | 366 | + }); | | 367 | + assert.rejects(stream.toArray(), { | | 368 | + name: 'AbortError', | | 369 | + }).then(common.mustCall()); | | 370 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-some-find-every.mjs +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -167,6 +167,15 @@ function oneTo5Async() { | 167 | 167 | name: 'AbortError', | 168 | 168 | }, `${op} should abort with pre-aborted abort controller`).then(common.mustCall()); | 169 | 169 | } | | 170 | + { | | 171 | + // Support for pre-aborted AbortSignal on an empty stream | | 172 | + assert.rejects(Readable.from([])[op]( | | 173 | + common.mustNotCall(), | | 174 | + { signal: AbortSignal.abort() } | | 175 | + ), { | | 176 | + name: 'AbortError', | | 177 | + }, `${op} should abort with pre-aborted signal on empty stream`).then(common.mustCall()); | | 178 | + } | 170 | 179 | } | 171 | 180 | } | 172 | 181 | { | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-stream-toArray.js +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -91,3 +91,15 @@ const assert = require('assert'); | 91 | 91 | }); | 92 | 92 | }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); | 93 | 93 | } | | 94 | + | | 95 | +{ | | 96 | + // Already aborted AbortSignal on an empty stream | | 97 | + const reason = new Error('aborted'); | | 98 | + const stream = Readable.from([]); | | 99 | + assert.rejects(stream.toArray({ signal: AbortSignal.abort(reason) }), { | | 100 | + name: 'AbortError', | | 101 | + cause: reason, | | 102 | + }).then(common.mustCall(() => { | | 103 | + assert.strictEqual(stream.destroyed, true); | | 104 | + })); | | 105 | +} |
|