stream: add isReadable helper

PR-URL: https://github.com/nodejs/node/pull/41199
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Robert Nagy
2021-12-16 14:32:02 +01:00
parent b97b81d4ec
commit a698c49993
5 changed files with 40 additions and 4 deletions

View File

@@ -2282,6 +2282,19 @@ added: v17.3.0
Returns whether the stream has encountered an error.
### `stream.isReadable(stream)`
<!-- YAML
added: REPLACEME
-->
> Stability: 1 - Experimental
* `stream` {Readable|Duplex|ReadableStream}
* Returns: {boolean}
Returns whether the stream is readable.
### `stream.Readable.toWeb(streamReadable)`
<!-- YAML

View File

@@ -8,6 +8,7 @@ const {
const kDestroyed = Symbol('kDestroyed');
const kIsErrored = Symbol('kIsErrored');
const kIsReadable = Symbol('kIsReadable');
const kIsDisturbed = Symbol('kIsDisturbed');
function isReadableNodeStream(obj, strict = false) {
@@ -116,6 +117,7 @@ function isReadableFinished(stream, strict) {
}
function isReadable(stream) {
if (stream && stream[kIsReadable] != null) return stream[kIsReadable];
const r = isReadableNodeStream(stream);
if (r === null || typeof stream?.readable !== 'boolean') return null;
if (isDestroyed(stream)) return false;
@@ -260,15 +262,16 @@ function isErrored(stream) {
module.exports = {
kDestroyed,
isDisturbed,
isErrored,
kIsDisturbed,
isErrored,
kIsErrored,
isReadable,
kIsReadable,
isClosed,
isDestroyed,
isDuplexNodeStream,
isFinished,
isIterable,
isReadable,
isReadableNodeStream,
isReadableEnded,
isReadableFinished,

View File

@@ -83,6 +83,7 @@ const {
const {
kIsDisturbed,
kIsErrored,
kIsReadable,
} = require('internal/streams/utils');
const {
@@ -261,6 +262,10 @@ class ReadableStream {
return this[kState].state === 'errored';
}
get [kIsReadable]() {
return this[kState].state === 'readable';
}
/**
* @readonly
* @type {boolean}

View File

@@ -44,6 +44,7 @@ const utils = require('internal/streams/utils');
const Stream = module.exports = require('internal/streams/legacy').Stream;
Stream.isDisturbed = utils.isDisturbed;
Stream.isErrored = utils.isErrored;
Stream.isReadable = utils.isReadable;
Stream.Readable = require('internal/streams/readable');
for (const key of ObjectKeys(operators)) {
const op = operators[key];

View File

@@ -2,7 +2,7 @@
'use strict';
const common = require('../common');
const { isDisturbed, isErrored } = require('stream');
const { isDisturbed, isErrored, isReadable } = require('stream');
const assert = require('assert');
const {
isPromise,
@@ -1573,7 +1573,6 @@ class Source {
})().then(common.mustCall());
}
{
const stream = new ReadableStream({
pull: common.mustCall((controller) => {
@@ -1588,3 +1587,18 @@ class Source {
isErrored(stream, true);
})().then(common.mustCall());
}
{
const stream = new ReadableStream({
pull: common.mustCall((controller) => {
controller.error(new Error());
}),
});
const reader = stream.getReader();
(async () => {
isReadable(stream, true);
await reader.read().catch(common.mustCall());
isReadable(stream, false);
})().then(common.mustCall());
}