mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
stream: reject operators on empty streams with aborted signal
Stream operators given an already-aborted signal resolved instead of rejecting when the stream was empty, because the signal was only checked once a chunk arrived. Check it up front in map() and toArray(), which the other operators are built on. Signed-off-by: vedchaudhari <vedc2853@gmail.com> PR-URL: https://github.com/nodejs/node/pull/67637 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
This commit is contained in:
1 parent
8857717d81
commit
10ffe227bf
7 files changed
+71
No files matched your search
@@ -153,6 +153,10 @@ function map(fn, options) {
|
||||
pump();
|
||||
|
||||
try {
|
||||
if (signal.aborted) {
|
||||
throw new AbortError();
|
||||
}
|
||||
|
||||
while (true) {
|
||||
while (queue.length > 0) {
|
||||
const val = await queue[0];
|
||||
@@ -293,6 +297,13 @@ async function toArray(options) {
|
||||
validateAbortSignal(options.signal, 'options.signal');
|
||||
}
|
||||
|
||||
if (options?.signal?.aborted) {
|
||||
const err = new AbortError(undefined, { cause: options.signal.reason });
|
||||
this.once('error', () => {}); // The error is already propagated
|
||||
await finished(this.destroy(err));
|
||||
throw err;
|
||||
}
|
||||
|
||||
const result = [];
|
||||
for await (const val of this) {
|
||||
if (options?.signal?.aborted) {
|
||||
|
||||
@@ -174,3 +174,13 @@ const { setTimeout } = require('timers/promises');
|
||||
// Check that map isn't getting called.
|
||||
stream.filter(() => true);
|
||||
}
|
||||
|
||||
{
|
||||
// Already aborted AbortSignal on an empty stream
|
||||
const stream = Readable.from([]).filter(common.mustNotCall(), {
|
||||
signal: AbortSignal.abort(),
|
||||
});
|
||||
assert.rejects(stream.toArray(), {
|
||||
name: 'AbortError',
|
||||
}).then(common.mustCall());
|
||||
}
|
||||
@@ -138,3 +138,13 @@ function oneTo5() {
|
||||
// Check that map isn't getting called.
|
||||
stream.flatMap(() => true);
|
||||
}
|
||||
|
||||
{
|
||||
// Already aborted AbortSignal on an empty stream
|
||||
const stream = Readable.from([]).flatMap(common.mustNotCall(), {
|
||||
signal: AbortSignal.abort(),
|
||||
});
|
||||
assert.rejects(stream.toArray(), {
|
||||
name: 'AbortError',
|
||||
}).then(common.mustCall());
|
||||
}
|
||||
@@ -137,3 +137,12 @@ const { once } = require('events');
|
||||
// Check that map isn't getting called.
|
||||
stream.forEach(() => true);
|
||||
}
|
||||
|
||||
{
|
||||
// Already aborted AbortSignal on an empty stream
|
||||
assert.rejects(Readable.from([]).forEach(common.mustNotCall(), {
|
||||
signal: AbortSignal.abort(),
|
||||
}), {
|
||||
name: 'AbortError',
|
||||
}).then(common.mustCall());
|
||||
}
|
||||
@@ -358,3 +358,13 @@ function createDependentPromises(n) {
|
||||
const stream = Readable.from([1, 2, 3, 4, 5]).map((x) => x);
|
||||
assert.strictEqual(stream.readable, true);
|
||||
}
|
||||
|
||||
{
|
||||
// Already aborted AbortSignal on an empty stream
|
||||
const stream = Readable.from([]).map(common.mustNotCall(), {
|
||||
signal: AbortSignal.abort(),
|
||||
});
|
||||
assert.rejects(stream.toArray(), {
|
||||
name: 'AbortError',
|
||||
}).then(common.mustCall());
|
||||
}
|
||||
@@ -167,6 +167,15 @@ function oneTo5Async() {
|
||||
name: 'AbortError',
|
||||
}, `${op} should abort with pre-aborted abort controller`).then(common.mustCall());
|
||||
}
|
||||
{
|
||||
// Support for pre-aborted AbortSignal on an empty stream
|
||||
assert.rejects(Readable.from([])[op](
|
||||
common.mustNotCall(),
|
||||
{ signal: AbortSignal.abort() }
|
||||
), {
|
||||
name: 'AbortError',
|
||||
}, `${op} should abort with pre-aborted signal on empty stream`).then(common.mustCall());
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
|
||||
@@ -91,3 +91,15 @@ const assert = require('assert');
|
||||
});
|
||||
}, /ERR_INVALID_ARG_TYPE/).then(common.mustCall());
|
||||
}
|
||||
|
||||
{
|
||||
// Already aborted AbortSignal on an empty stream
|
||||
const reason = new Error('aborted');
|
||||
const stream = Readable.from([]);
|
||||
assert.rejects(stream.toArray({ signal: AbortSignal.abort(reason) }), {
|
||||
name: 'AbortError',
|
||||
cause: reason,
|
||||
}).then(common.mustCall(() => {
|
||||
assert.strictEqual(stream.destroyed, true);
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user