Skip to content

Commit

Permalink
fixup: don't read more if failed
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 17, 2021
1 parent aabfd75 commit debd387
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/internal/streams/operators.js
Expand Up @@ -20,6 +20,7 @@ module.exports.map = function map(stream, fn, options) {
const queue = [];

let reading = false;
let failed = false;

// TODO: What about hwm? This will cause some unnecessary buffering.
const ret = new Readable({
Expand Down Expand Up @@ -70,6 +71,7 @@ module.exports.map = function map(stream, fn, options) {
try {
return [null, await val];
} catch (err) {
failed = true;
return [err, null];
}
}
Expand All @@ -82,21 +84,25 @@ module.exports.map = function map(stream, fn, options) {
}

function pump () {
while (queue.length < concurrency) {
let val = stream.read();
if (val === null) {
return;
}
try {
if (failed) {
return;
}
try {
while (queue.length < concurrency) {
let val = stream.read();
if (val === null) {
return;
}
val = fn(val, { signal });
if (val && typeof val.then === 'function') {
enqueue(wrap(val));
} else {
enqueue([null, val]);
}
} catch (err) {
enqueue([err, null]);
}
} catch (err) {
failed = true;
enqueue([err, null]);
}
}

Expand Down

0 comments on commit debd387

Please sign in to comment.