Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Aug 2, 2021
1 parent 3bd9a53 commit 3d31a46
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
12 changes: 12 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,18 @@ added: REPLACEME
* `signal` {AbortSignal}
* Returns: {stream.Readable}

### `stream.Readable.isDisturbed(stream)`
<!-- YAML
added: REPLACEME
-->

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

Returns whether the stream has been read from.

### `stream.Readable.toWeb(streamReadable)`
<!-- YAML
added: REPLACEME
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ ObjectDefineProperties(Readable.prototype, {
readableDidRead: {
enumerable: false,
get: function() {
return (
return !!(
this._readableState.dataEmitted ||
this._readableState.endEmitted ||
this._readableState.errorEmitted ||
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ function willEmitClose(stream) {

function isDisturbed(stream) {
const state = stream && stream._readableState;
return stream && (
return !!(stream && (
stream.readableDidRead ||
isDestroyed(stream) ||
stream[kIsDisturbed] ||
(state && state.endEmitted)
);
));
}

module.exports = {
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-stream-readable-didRead.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const Readable = require('stream').Readable;
const { isDisturbed, Readable } = require('stream');

function noop() {}

function check(readable, data, fn) {
assert.strictEqual(readable.readableDidRead, false);
assert.strictEqual(isDisturbed(readable), false);
if (data === -1) {
readable.on('error', common.mustCall());
readable.on('data', common.mustNotCall());
Expand All @@ -28,6 +29,7 @@ function check(readable, data, fn) {
fn();
setImmediate(() => {
assert.strictEqual(readable.readableDidRead, true);
assert.strictEqual(isDisturbed(readable), true);
});
}

Expand Down
50 changes: 50 additions & 0 deletions test/parallel/test-whatwg-readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

const common = require('../common');
const { isDisturbed } = require('stream');
const assert = require('assert');
const {
isPromise,
Expand Down Expand Up @@ -1520,3 +1521,52 @@ class Source {
readableByteStreamControllerClose(controller);
readableByteStreamControllerEnqueue(controller);
}

{
const stream = new ReadableStream({
start(controller) {
controller.enqueue('a');
controller.close();
},
pull: common.mustNotCall(),
});

const reader = stream.getReader();
(async () => {
isDisturbed(stream, false);
await reader.read();
isDisturbed(stream, true);
})().then(common.mustCall());
}

{
const stream = new ReadableStream({
start(controller) {
controller.close();
},
pull: common.mustNotCall(),
});

const reader = stream.getReader();
(async () => {
isDisturbed(stream, false);
await reader.read();
isDisturbed(stream, true);
})().then(common.mustCall());
}

{
const stream = new ReadableStream({
start(controller) {
},
pull: common.mustNotCall(),
});
stream.cancel();

const reader = stream.getReader();
(async () => {
isDisturbed(stream, false);
await reader.read();
isDisturbed(stream, true);
})().then(common.mustCall());
}

0 comments on commit 3d31a46

Please sign in to comment.