Skip to content

Commit

Permalink
streams: add null check in Readable.from
Browse files Browse the repository at this point in the history
Throws `ERR_STREAM_NULL_VALUES` error if a null value is passed to
`Readable.from`. Also added docs for the same.

Fixes: nodejs#32845
  • Loading branch information
rexagod committed Apr 16, 2020
1 parent 0bd5595 commit 1dd0295
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ added:
-->

* `iterable` {Iterable} Object implementing the `Symbol.asyncIterator` or
`Symbol.iterator` iterable protocol.
`Symbol.iterator` iterable protocol. Throws 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
12 changes: 8 additions & 4 deletions lib/internal/streams/from.js
Original file line number Diff line number Diff line change
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 @@ -48,10 +49,13 @@ function from(Readable, iterable, opts) {
const { value, done } = await iterator.next();
if (done) {
readable.push(null);
} else if (readable.push(await value)) {
next();
} else {
reading = false;
if (value == null) throw new ERR_STREAM_NULL_VALUES();
if (readable.push(await value)) {
next();
} else {
reading = false;
}
}
} catch (err) {
readable.destroy(err);
Expand Down

0 comments on commit 1dd0295

Please sign in to comment.