Skip to content

Commit 8c1bdf1

Browse files
committed
fixup
1 parent 3bd9a53 commit 8c1bdf1

File tree

6 files changed

+69
-5
lines changed

6 files changed

+69
-5
lines changed

doc/api/stream.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,18 @@ added: REPLACEME
20462046
* `signal` {AbortSignal}
20472047
* Returns: {stream.Readable}
20482048

2049+
### `stream.Readable.isDisturbed(stream)`
2050+
<!-- YAML
2051+
added: REPLACEME
2052+
-->
2053+
2054+
> Stability: 1 - Experimental
2055+
2056+
* `stream` {stream.Readable|ReadableStream}
2057+
* Returns: `boolean`
2058+
2059+
Returns whether the stream has been read from.
2060+
20492061
### `stream.Readable.toWeb(streamReadable)`
20502062
<!-- YAML
20512063
added: REPLACEME

lib/internal/streams/readable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ ObjectDefineProperties(Readable.prototype, {
11851185
readableDidRead: {
11861186
enumerable: false,
11871187
get: function() {
1188-
return (
1188+
return !!(
11891189
this._readableState.dataEmitted ||
11901190
this._readableState.endEmitted ||
11911191
this._readableState.errorEmitted ||

lib/internal/streams/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ function willEmitClose(stream) {
198198

199199
function isDisturbed(stream) {
200200
const state = stream && stream._readableState;
201-
return stream && (
201+
return !!(stream && (
202202
stream.readableDidRead ||
203203
isDestroyed(stream) ||
204204
stream[kIsDisturbed] ||
205205
(state && state.endEmitted)
206-
);
206+
));
207207
}
208208

209209
module.exports = {

lib/internal/webstreams/readablestream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const {
8282

8383
const {
8484
kIsDisturbed,
85-
} = require('internal/stream/utils');
85+
} = require('internal/streams/utils');
8686

8787
const {
8888
ArrayBufferViewGetBuffer,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use strict';
22
const common = require('../common');
33
const assert = require('assert');
4-
const Readable = require('stream').Readable;
4+
const { isDisturbed, Readable } = require('stream');
55

66
function noop() {}
77

88
function check(readable, data, fn) {
99
assert.strictEqual(readable.readableDidRead, false);
10+
assert.strictEqual(isDisturbed(readable), false);
1011
if (data === -1) {
1112
readable.on('error', common.mustCall());
1213
readable.on('data', common.mustNotCall());
@@ -28,6 +29,7 @@ function check(readable, data, fn) {
2829
fn();
2930
setImmediate(() => {
3031
assert.strictEqual(readable.readableDidRead, true);
32+
assert.strictEqual(isDisturbed(readable), true);
3133
});
3234
}
3335

test/parallel/test-whatwg-readablestream.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict';
33

44
const common = require('../common');
5+
const { isDisturbed } = require('stream');
56
const assert = require('assert');
67
const {
78
isPromise,
@@ -1520,3 +1521,52 @@ class Source {
15201521
readableByteStreamControllerClose(controller);
15211522
readableByteStreamControllerEnqueue(controller);
15221523
}
1524+
1525+
{
1526+
const stream = new ReadableStream({
1527+
start(controller) {
1528+
controller.enqueue('a');
1529+
controller.close();
1530+
},
1531+
pull: common.mustNotCall(),
1532+
});
1533+
1534+
const reader = stream.getReader();
1535+
(async () => {
1536+
isDisturbed(stream, false);
1537+
await reader.read();
1538+
isDisturbed(stream, true);
1539+
})().then(common.mustCall());
1540+
}
1541+
1542+
{
1543+
const stream = new ReadableStream({
1544+
start(controller) {
1545+
controller.close();
1546+
},
1547+
pull: common.mustNotCall(),
1548+
});
1549+
1550+
const reader = stream.getReader();
1551+
(async () => {
1552+
isDisturbed(stream, false);
1553+
await reader.read();
1554+
isDisturbed(stream, true);
1555+
})().then(common.mustCall());
1556+
}
1557+
1558+
{
1559+
const stream = new ReadableStream({
1560+
start(controller) {
1561+
},
1562+
pull: common.mustNotCall(),
1563+
});
1564+
stream.cancel();
1565+
1566+
const reader = stream.getReader();
1567+
(async () => {
1568+
isDisturbed(stream, false);
1569+
await reader.read();
1570+
isDisturbed(stream, true);
1571+
})().then(common.mustCall());
1572+
}

0 commit comments

Comments
 (0)