Skip to content

Commit b2fb01a

Browse files
ronagcodebytere
authored andcommitted
stream: make from read one at a time
Currently from will eagerly buffer up items which means that errors are also eagerly encountered and items which are buffer when an error occurs will be discarded, which is inconsistent with how generators work. Fixes: #29428 PR-URL: #33201 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent cf88ed8 commit b2fb01a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/internal/streams/from.js

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ function from(Readable, iterable, opts) {
3333

3434
const readable = new Readable({
3535
objectMode: true,
36+
highWaterMark: 1,
37+
// TODO(ronag): What options should be allowed?
3638
...opts
3739
});
3840

test/parallel/test-readable-from.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,29 @@ async function asTransformStream() {
159159
}
160160
}
161161

162+
async function endWithError() {
163+
async function* generate() {
164+
yield 1;
165+
yield 2;
166+
yield Promise.reject('Boum');
167+
}
168+
169+
const stream = Readable.from(generate());
170+
171+
const expected = [1, 2];
172+
173+
try {
174+
for await (const chunk of stream) {
175+
strictEqual(chunk, expected.shift());
176+
}
177+
throw new Error();
178+
} catch (err) {
179+
strictEqual(expected.length, 0);
180+
strictEqual(err, 'Boum');
181+
}
182+
}
183+
184+
162185
Promise.all([
163186
toReadableBasicSupport(),
164187
toReadableSyncIterator(),
@@ -168,5 +191,6 @@ Promise.all([
168191
toReadableOnData(),
169192
toReadableOnDataNonObject(),
170193
destroysTheStreamWhenThrowing(),
171-
asTransformStream()
194+
asTransformStream(),
195+
endWithError()
172196
]).then(mustCall());

0 commit comments

Comments
 (0)