Skip to content

Commit

Permalink
stream: use readableObjectMode public api for js stream
Browse files Browse the repository at this point in the history
PR-URL: #27655
Refs: #445
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
  • Loading branch information
antsmartian authored and targos committed May 20, 2019
1 parent 4a9af17 commit 32fd0ac
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
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);
}

0 comments on commit 32fd0ac

Please sign in to comment.