Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

streams: add null check in Readable.from #32873

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/api/stream.md
Expand Up @@ -1698,7 +1698,8 @@ added:
-->

* `iterable` {Iterable} Object implementing the `Symbol.asyncIterator` or
`Symbol.iterator` iterable protocol.
`Symbol.iterator` iterable protocol. Emits an 'error' event if a null
value is passed.
* `options` {Object} Options provided to `new stream.Readable([options])`.
By default, `Readable.from()` will set `options.objectMode` to `true`, unless
this is explicitly opted out by setting `options.objectMode` to `false`.
Expand Down
16 changes: 11 additions & 5 deletions lib/internal/streams/from.js
Expand Up @@ -7,7 +7,8 @@ const {
const { Buffer } = require('buffer');

const {
ERR_INVALID_ARG_TYPE
ERR_INVALID_ARG_TYPE,
ERR_STREAM_NULL_VALUES
} = require('internal/errors').codes;

function from(Readable, iterable, opts) {
Expand Down Expand Up @@ -73,15 +74,20 @@ function from(Readable, iterable, opts) {
needToClose = false;
const { value, done } = await iterator.next();
needToClose = !done;
const resolved = await value;
ronag marked this conversation as resolved.
Show resolved Hide resolved
if (done) {
readable.push(null);
} else if (readable.destroyed) {
await close();
rexagod marked this conversation as resolved.
Show resolved Hide resolved
} else if (readable.push(resolved)) {
next();
} else {
reading = false;
ronag marked this conversation as resolved.
Show resolved Hide resolved
const res = await value;
ronag marked this conversation as resolved.
Show resolved Hide resolved
if (res === null) {
reading = false;
rexagod marked this conversation as resolved.
Show resolved Hide resolved
throw new ERR_STREAM_NULL_VALUES();
} else if (readable.push(res)) {
next();
} else {
reading = false;
}
}
} catch (err) {
readable.destroy(err);
Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-readable-from-iterator-closing.js
Expand Up @@ -168,18 +168,17 @@ async function closeAfterNullYielded() {
const finallyMustCall = mustCall();
const dataMustCall = mustCall(3);

function* infiniteGenerate() {
function* generate() {
try {
yield 'a';
yield 'a';
yield 'a';
while (true) yield null;
} finally {
finallyMustCall();
}
}

const stream = Readable.from(infiniteGenerate());
const stream = Readable.from(generate());

stream.on('data', (chunk) => {
dataMustCall();
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-stream-readable-next-no-null.js
@@ -0,0 +1,19 @@
'use strict';
const { mustNotCall, expectsError } = require('../common');
const { Readable } = require('stream');

async function* generate() {
yield null;
}

const stream = Readable.from(generate());

stream.on('error', expectsError({
code: 'ERR_STREAM_NULL_VALUES',
name: 'TypeError',
message: 'May not write null values to stream'
}));

stream.on('data', mustNotCall((chunk) => {}));

stream.on('end', mustNotCall());