Skip to content

Commit

Permalink
stream: add isReadable helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Dec 16, 2021
1 parent 752d75d commit 3b19515
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
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
6 changes: 5 additions & 1 deletion 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,9 +262,11 @@ function isErrored(stream) {
module.exports = {
kDestroyed,
isDisturbed,
isErrored,
kIsDisturbed,
isErrored,
kIsErrored,
isReadable,
kIsReadable,
isClosed,
isDestroyed,
isDuplexNodeStream,
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());
}

0 comments on commit 3b19515

Please sign in to comment.