Skip to content

Commit eeab6d4

Browse files
committed
stream: add isErrored helper
Refs: nodejs/undici#1134
1 parent f92d60a commit eeab6d4

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed

doc/api/stream.md

+13
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,19 @@ added: v16.8.0
22252225

22262226
Returns whether the stream has been read from or cancelled.
22272227

2228+
### `stream.Readable.isErrored(stream)`
2229+
2230+
<!-- YAML
2231+
added: v16.8.0
2232+
-->
2233+
2234+
> Stability: 1 - Experimental
2235+
2236+
* `stream` {stream.Readable|ReadableStream}
2237+
* Returns: `boolean`
2238+
2239+
Returns whether the stream has been errored.
2240+
22282241
### `stream.Readable.toWeb(streamReadable)`
22292242

22302243
<!-- YAML

lib/internal/streams/utils.js

+14
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,24 @@ function isDisturbed(stream) {
245245
));
246246
}
247247

248+
function isErrored(stream) {
249+
return !!(stream && (
250+
stream.readableErrored ||
251+
stream.writableErrored ||
252+
stream._readableState?.errorEmitted ||
253+
stream._writableState?.errorEmitted ||
254+
stream._readableState?.errored ||
255+
stream._writableState?.errored ||
256+
stream[kIsErrored]
257+
));
258+
}
259+
248260
module.exports = {
249261
kDestroyed,
250262
isDisturbed,
263+
isErrored,
251264
kIsDisturbed,
265+
kIsErrored,
252266
isClosed,
253267
isDestroyed,
254268
isDuplexNodeStream,

lib/internal/webstreams/readablestream.js

+4
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ class ReadableStream {
241241
return this[kState].disturbed;
242242
}
243243

244+
get [kIsErrored]() {
245+
return this[kState].state === 'errored';
246+
}
247+
244248
/**
245249
* @readonly
246250
* @type {boolean}

lib/stream.js

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const promises = require('stream/promises');
3939

4040
const Stream = module.exports = require('internal/streams/legacy').Stream;
4141
Stream.isDisturbed = require('internal/streams/utils').isDisturbed;
42+
Stream.isErrored = require('internal/streams/utils').isErrored;
4243
Stream.Readable = require('internal/streams/readable');
4344
Stream.Writable = require('internal/streams/writable');
4445
Stream.Duplex = require('internal/streams/duplex');

test/parallel/test-stream-readable-didRead.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4-
const { isDisturbed, Readable } = require('stream');
4+
const { isDisturbed, isErrored, Readable } = require('stream');
55

66
function noop() {}
77

88
function check(readable, data, fn) {
99
assert.strictEqual(readable.readableDidRead, false);
1010
assert.strictEqual(isDisturbed(readable), false);
11+
assert.strictEqual(isErrored(readable), false);
1112
if (data === -1) {
12-
readable.on('error', common.mustCall());
13+
readable.on('error', common.mustCall(() => {
14+
assert.strictEqual(isErrored(readable), true)
15+
}));
1316
readable.on('data', common.mustNotCall());
1417
readable.on('end', common.mustNotCall());
1518
} else {

test/parallel/test-whatwg-readablestream.js

+17
Original file line numberDiff line numberDiff line change
@@ -1572,3 +1572,20 @@ class Source {
15721572
isDisturbed(stream, true);
15731573
})().then(common.mustCall());
15741574
}
1575+
1576+
1577+
{
1578+
const stream = new ReadableStream({
1579+
start(controller) {
1580+
controller.error(new Error());
1581+
},
1582+
pull: common.mustNotCall(),
1583+
});
1584+
1585+
const reader = stream.getReader();
1586+
(async () => {
1587+
isErrored(stream, false);
1588+
await reader.read();
1589+
isErrored(stream, true);
1590+
})().then(common.mustCall());
1591+
}

0 commit comments

Comments
 (0)