Skip to content

Commit 32fd0ac

Browse files
antsmartiantargos
authored andcommitted
stream: use readableObjectMode public api for js stream
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>
1 parent 4a9af17 commit 32fd0ac

5 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎doc/api/stream.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,13 @@ This property contains the number of bytes (or objects) in the queue
502502
ready to be written. The value provides introspection data regarding
503503
the status of the `highWaterMark`.
504504

505+
##### writable.writableObjectMode
506+
<!-- YAML
507+
added: REPLACEME
508+
-->
509+
510+
Getter for the property `objectMode` of a given `Writable` stream.
511+
505512
##### writable.write(chunk[, encoding][, callback])
506513
<!-- YAML
507514
added: v0.9.4
@@ -1089,6 +1096,13 @@ This property contains the number of bytes (or objects) in the queue
10891096
ready to be read. The value provides introspection data regarding
10901097
the status of the `highWaterMark`.
10911098

1099+
##### readable.readableObjectMode
1100+
<!-- YAML
1101+
added: REPLACEME
1102+
-->
1103+
1104+
Getter for the property `objectMode` of a given `Readable` stream.
1105+
10921106
##### readable.resume()
10931107
<!-- YAML
10941108
added: v0.9.4

‎lib/_stream_readable.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,13 @@ Object.defineProperty(Readable.prototype, 'readableLength', {
10811081
}
10821082
});
10831083

1084+
Object.defineProperty(Readable.prototype,'readableObjectMode',{
1085+
enumerable: false,
1086+
get(){
1087+
returnthis._readableState ? this._readableState.objectMode : false;
1088+
}
1089+
});
1090+
10841091
// Pluck off n bytes from an array of buffers.
10851092
// Length is the combined lengths of all the buffers in the list.
10861093
// This function is designed to be inlinable, so please take care when making

‎lib/_stream_writable.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,13 @@ Object.defineProperty(Writable.prototype, 'destroyed', {
707707
}
708708
});
709709

710+
Object.defineProperty(Writable.prototype,'writableObjectMode',{
711+
enumerable: false,
712+
get(){
713+
returnthis._writableState ? this._writableState.objectMode : false;
714+
}
715+
});
716+
710717
Writable.prototype.destroy=destroyImpl.destroy;
711718
Writable.prototype._undestroy=destroyImpl.undestroy;
712719
Writable.prototype._destroy=function(err,cb){

‎lib/internal/js_stream_socket.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class JSStreamSocket extends Socket {
5050
stream.on('error',(err)=>this.emit('error',err));
5151
constondata=(chunk)=>{
5252
if(typeofchunk==='string'||
53-
stream._readableState.objectMode===true){
53+
stream.readableObjectMode===true){
5454
// Make sure that no further `data` events will happen.
5555
stream.pause();
5656
stream.removeListener('data',ondata);

‎test/parallel/test-stream2-basic.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
constcommon=require('../common');
2525
constR=require('_stream_readable');
26+
constW=require('_stream_writable');
2627
constassert=require('assert');
2728

2829
constEE=require('events').EventEmitter;
@@ -420,3 +421,15 @@ class TestWriter extends EE {
420421
constr2=r.setEncoding('utf8').pause().resume().pause();
421422
assert.strictEqual(r,r2);
422423
}
424+
425+
{
426+
// Verify readableObjectMode property
427+
constr=newR({objectMode: true});
428+
assert.strictEqual(r.readableObjectMode,true);
429+
}
430+
431+
{
432+
// Verify writableObjectMode property
433+
constw=newW({objectMode: true});
434+
assert.strictEqual(w.writableObjectMode,true);
435+
}

0 commit comments

Comments
 (0)