Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
stream: add isReadable helper
  • Loading branch information
ronag committed Dec 16, 2021
commit 57b412c00818114e702a79571e4ef7618b8069ad
13 changes: 13 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,19 @@ added: REPLACEME

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
Expand Down
7 changes: 5 additions & 2 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -116,6 +117,7 @@ function isReadableFinished(stream, strict) {
}

function isReadable(stream) {
if (stream[kIsReadable] != null) return stream[kIsReadable];
const r = isReadableNodeStream(stream);
if (r === null || typeof stream?.readable !== 'boolean') return null;
if (isDestroyed(stream)) return false;
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const {
const {
kIsDisturbed,
kIsErrored,
kIsReadable,
} = require('internal/streams/utils');

const {
Expand Down Expand Up @@ -246,6 +247,10 @@ class ReadableStream {
return this[kState].state === 'errored';
}

get [kIsReadable]() {
return this[kState].state === 'readable';
}

/**
* @readonly
* @type {boolean}
Expand Down
18 changes: 16 additions & 2 deletions test/parallel/test-whatwg-readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1573,7 +1573,6 @@ class Source {
})().then(common.mustCall());
}


{
const stream = new ReadableStream({
pull: common.mustCall((controller) => {
Expand All @@ -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());
}