Skip to content

Commit

Permalink
stream: add isErrored helper
Browse files Browse the repository at this point in the history
Refs: nodejs/undici#1134

PR-URL: #41121
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
ronag authored and danielleadams committed Jan 31, 2022
1 parent a2c4fa8 commit 2568ac9
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 4 deletions.
51 changes: 51 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,57 @@ added: v16.8.0

Returns whether the stream has been read from or cancelled.

### `stream.isErrored(stream)`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
* `stream` {Readable|Writable|Duplex|WritableStream|ReadableStream}
* Returns: {boolean}

Returns whether the stream has encountered an error.

### `stream.Readable.toWeb(streamReadable)`

<!-- YAML
added: v17.0.0
-->

> Stability: 1 - Experimental
* `streamReadable` {stream.Readable}
* Returns: {ReadableStream}

### `stream.Writable.fromWeb(writableStream[, options])`

<!-- YAML
added: v17.0.0
-->

> Stability: 1 - Experimental
* `writableStream` {WritableStream}
* `options` {Object}
* `decodeStrings` {boolean}
* `highWaterMark` {number}
* `objectMode` {boolean}
* `signal` {AbortSignal}
* Returns: {stream.Writable}

### `stream.Writable.toWeb(streamWritable)`

<!-- YAML
added: v17.0.0
-->

> Stability: 1 - Experimental
* `streamWritable` {stream.Writable}
* Returns: {WritableStream}

### `stream.Duplex.from(src)`

<!-- YAML
Expand Down
21 changes: 21 additions & 0 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
SymbolIterator,
} = primordials;

const kIsErrored = Symbol('kIsErrored');
const kIsDisturbed = Symbol('kIsDisturbed');

function isReadableNodeStream(obj) {
Expand Down Expand Up @@ -212,10 +213,30 @@ function willEmitClose(stream) {
);
}

function isDisturbed(stream) {
return !!(stream && (
stream[kIsDisturbed] ??
(stream.readableDidRead || stream.readableAborted)
));
}

function isErrored(stream) {
return !!(stream && (
stream[kIsErrored] ??
stream.readableErrored ??
stream.writableErrored ??
stream._readableState?.errorEmitted ??
stream._writableState?.errorEmitted ??
stream._readableState?.errored ??
stream._writableState?.errored
));
}

module.exports = {
isDisturbed,
isErrored,
kIsDisturbed,
kIsErrored,
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 @@ -82,6 +82,7 @@ const {

const {
kIsDisturbed,
kIsErrored,
} = require('internal/streams/utils');

const {
Expand Down Expand Up @@ -241,6 +242,10 @@ class ReadableStream {
return this[kState].disturbed;
}

get [kIsErrored]() {
return this[kState].state === 'errored';
}

/**
* @readonly
* @type {boolean}
Expand Down
4 changes: 3 additions & 1 deletion lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ const eos = require('internal/streams/end-of-stream');
const internalBuffer = require('internal/buffer');

const promises = require('stream/promises');
const utils = require('internal/streams/utils');

const Stream = module.exports = require('internal/streams/legacy').Stream;
Stream.isDisturbed = require('internal/streams/utils').isDisturbed;
Stream.isDisturbed = utils.isDisturbed;
Stream.isErrored = utils.isErrored;
Stream.Readable = require('internal/streams/readable');
Stream.Writable = require('internal/streams/writable');
Stream.Duplex = require('internal/streams/duplex');
Expand Down
7 changes: 5 additions & 2 deletions test/parallel/test-stream-readable-didRead.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { isDisturbed, Readable } = require('stream');
const { isDisturbed, isErrored, Readable } = require('stream');

function noop() {}

function check(readable, data, fn) {
assert.strictEqual(readable.readableDidRead, false);
assert.strictEqual(isDisturbed(readable), false);
assert.strictEqual(isErrored(readable), false);
if (data === -1) {
readable.on('error', common.mustCall());
readable.on('error', common.mustCall(() => {
assert.strictEqual(isErrored(readable), true);
}));
readable.on('data', common.mustNotCall());
readable.on('end', common.mustNotCall());
} else {
Expand Down
18 changes: 17 additions & 1 deletion 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 } = require('stream');
const { isDisturbed, isErrored } = require('stream');
const assert = require('assert');
const {
isPromise,
Expand Down Expand Up @@ -1572,3 +1572,19 @@ class Source {
isDisturbed(stream, true);
})().then(common.mustCall());
}


{
const stream = new ReadableStream({
pull: common.mustCall((controller) => {
controller.error(new Error());
}),
});

const reader = stream.getReader();
(async () => {
isErrored(stream, false);
await reader.read().catch(common.mustCall());
isErrored(stream, true);
})().then(common.mustCall());
}
4 changes: 4 additions & 0 deletions tools/doc/type-parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ const customTypesMap = {
'stream.Readable': 'stream.html#class-streamreadable',
'stream.Transform': 'stream.html#class-streamtransform',
'stream.Writable': 'stream.html#class-streamwritable',
'Duplex': 'stream.html#class-streamduplex',
'Readable': 'stream.html#class-streamreadable',
'Transform': 'stream.html#class-streamtransform',
'Writable': 'stream.html#class-streamwritable',

'Immediate': 'timers.html#class-immediate',
'Timeout': 'timers.html#class-timeout',
Expand Down

0 comments on commit 2568ac9

Please sign in to comment.