Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: use readableObjectMode public api for js stream #27655

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,13 @@ This property contains the number of bytes (or objects) in the queue
ready to be written. The value provides introspection data regarding
the status of the `highWaterMark`.

##### writable.writableObjectMode
<!-- YAML
added: REPLACEME
-->

Getter for the property `objectMode` of a given `Writable` stream.

##### writable.write(chunk[, encoding][, callback])
<!-- YAML
added: v0.9.4
Expand Down Expand Up @@ -1089,6 +1096,13 @@ This property contains the number of bytes (or objects) in the queue
ready to be read. The value provides introspection data regarding
the status of the `highWaterMark`.

##### readable.readableObjectMode
<!-- YAML
added: REPLACEME
-->

Getter for the property `objectMode` of a given `Readable` stream.

##### readable.resume()
<!-- YAML
added: v0.9.4
Expand Down
7 changes: 7 additions & 0 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,13 @@ Object.defineProperty(Readable.prototype, 'readableLength', {
}
});

Object.defineProperty(Readable.prototype, 'readableObjectMode', {
enumerable: false,
get() {
return this._readableState ? this._readableState.objectMode : false;
}
});

// Pluck off n bytes from an array of buffers.
// Length is the combined lengths of all the buffers in the list.
// This function is designed to be inlinable, so please take care when making
Expand Down
7 changes: 7 additions & 0 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,13 @@ Object.defineProperty(Writable.prototype, 'destroyed', {
}
});

Object.defineProperty(Writable.prototype, 'writableObjectMode', {
enumerable: false,
get() {
return this._writableState ? this._writableState.objectMode : false;
}
});

Writable.prototype.destroy = destroyImpl.destroy;
Writable.prototype._undestroy = destroyImpl.undestroy;
Writable.prototype._destroy = function(err, cb) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/js_stream_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class JSStreamSocket extends Socket {
stream.on('error', (err) => this.emit('error', err));
const ondata = (chunk) => {
if (typeof chunk === 'string' ||
stream._readableState.objectMode === true) {
stream.readableObjectMode === true) {
// Make sure that no further `data` events will happen.
stream.pause();
stream.removeListener('data', ondata);
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-stream2-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

const common = require('../common');
const R = require('_stream_readable');
const W = require('_stream_writable');
const assert = require('assert');

const EE = require('events').EventEmitter;
Expand Down Expand Up @@ -420,3 +421,15 @@ class TestWriter extends EE {
const r2 = r.setEncoding('utf8').pause().resume().pause();
assert.strictEqual(r, r2);
}

{
// Verify readableObjectMode property
const r = new R({ objectMode: true });
assert.strictEqual(r.readableObjectMode, true);
}

{
// Verify writableObjectMode property
const w = new W({ objectMode: true });
assert.strictEqual(w.writableObjectMode, true);
}