README.md (5702B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Inspect Stream 22 23 > [Transform stream][transform-stream] for inspecting streamed data. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var inspectStream = require( '@stdlib/streams/node/inspect' ); 31 ``` 32 33 <a name="inspect-stream"></a> 34 35 #### inspectStream( \[options,] clbk ) 36 37 Creates a [transform stream][transform-stream] for inspecting streamed data. 38 39 ```javascript 40 function log( chunk, idx ) { 41 console.log( 'index: %d', idx ); 42 console.log( chunk ); 43 } 44 45 var stream = inspectStream( log ); 46 47 stream.write( 'a' ); 48 stream.write( 'b' ); 49 stream.write( 'c' ); 50 51 stream.end(); 52 /* => 53 'index: 0' 54 'a' 55 'index: 1' 56 'b' 57 'index: 2' 58 'c' 59 */ 60 ``` 61 62 The function accepts the following `options`: 63 64 - **objectMode**: specifies whether a [stream][stream] should operate in [objectMode][object-mode]. Default: `false`. 65 - **highWaterMark**: specifies the `Buffer` level at which `write()` calls start returning `false`. 66 - **allowHalfOpen**: specifies whether a [stream][stream] should remain open even if one side ends. Default: `false`. 67 - **readableObjectMode**: specifies whether the readable side should be in [objectMode][object-mode]. Default: `false`. 68 69 To set [stream][stream] `options`, 70 71 ```javascript 72 function log( chunk, idx ) { 73 console.log( 'index: %d', idx ); 74 console.log( chunk ); 75 } 76 77 var opts = { 78 'objectMode': true, 79 'highWaterMark': 64, 80 'allowHalfOpen': true, 81 'readableObjectMode': false // overridden by `objectMode` option when `objectMode=true` 82 }; 83 84 var stream = inspectStream( opts, log ); 85 ``` 86 87 #### inspectStream.factory( \[options] ) 88 89 Returns a `function` for creating [streams][transform-stream] which are identically configured according to provided `options`. 90 91 ```javascript 92 var opts = { 93 'objectMode': true, 94 'highWaterMark': 64 95 }; 96 97 var factory = inspectStream.factory( opts ); 98 ``` 99 100 This method accepts the same `options` as [`inspectStream()`](#inspect-stream). 101 102 ##### factory( clbk ) 103 104 Creates a [transform stream][transform-stream] for inspecting streamed data. 105 106 ```javascript 107 function log( chunk, idx ) { 108 console.log( 'index: %d', idx ); 109 console.log( chunk ); 110 } 111 112 var factory = inspectStream.factory(); 113 114 // Create 10 identically configured streams... 115 var streams = []; 116 var i; 117 for ( i = 0; i < 10; i++ ) { 118 streams.push( factory( log ) ); 119 } 120 ``` 121 122 #### inspectStream.objectMode( \[options,] clbk ) 123 124 This method is a convenience function to create [streams][stream] which **always** operate in [objectMode][object-mode]. 125 126 <!-- eslint-disable object-curly-newline --> 127 128 ```javascript 129 function log( chunk, idx ) { 130 console.log( 'index: %d', idx ); 131 console.log( chunk ); 132 } 133 134 var stream = inspectStream.objectMode( log ); 135 136 stream.write( { 'value': 'a' } ); 137 stream.write( { 'value': 'b' } ); 138 stream.write( { 'value': 'c' } ); 139 140 stream.end(); 141 /* => 142 'index: 0' 143 {'value': 'a'} 144 'index: 1' 145 {'value': 'b'} 146 'index: 2' 147 {'value': 'c'} 148 */ 149 ``` 150 151 This method accepts the same `options` as [`inspectStream()`](#inspect-stream); however, the method will **always** override the [objectMode][object-mode] option in `options`. 152 153 </section> 154 155 <!-- /.usage --> 156 157 <section class="examples"> 158 159 ## Examples 160 161 <!-- eslint no-undef: "error" --> 162 163 ```javascript 164 var parseJSON = require( '@stdlib/utils/parse-json' ); 165 var stdout = require( '@stdlib/streams/node/stdout' ); 166 var transformFactory = require( '@stdlib/streams/node/transform' ).factory; 167 var inspect = require( '@stdlib/streams/node/inspect' ).objectMode; 168 169 function parse( chunk, enc, clbk ) { 170 clbk( null, parseJSON( chunk ) ); 171 } 172 173 function pluck( chunk, enc, clbk ) { 174 clbk( null, chunk.value ); 175 } 176 177 function square( chunk, enc, clbk ) { 178 var v = +chunk; 179 clbk( null, v*v ); 180 } 181 182 function toStr( chunk, enc, clbk ) { 183 clbk( null, chunk.toString() ); 184 } 185 186 function join( chunk, enc, clbk ) { 187 clbk( null, chunk+'\n' ); 188 } 189 190 function logger( name ) { 191 return log; 192 193 function log( chunk, idx ) { 194 console.log( 'name: %s', name ); 195 console.log( 'index: %d', idx ); 196 console.log( chunk ); 197 } 198 } 199 200 // Create a factory for generating streams running in `objectMode`: 201 var tStream = transformFactory({ 202 'objectMode': true 203 }); 204 205 // Create streams for each transform: 206 var s1 = tStream( parse ); 207 var i1 = inspect( logger( 'parse' ) ); 208 var s2 = tStream( pluck ); 209 var i2 = inspect( logger( 'pluck' ) ); 210 var s3 = tStream( square ); 211 var i3 = inspect( logger( 'square' ) ); 212 var s4 = tStream( toStr ); 213 var i4 = inspect( logger( 'toString' ) ); 214 var s5 = tStream( join ); 215 var i5 = inspect( logger( 'join' ) ); 216 217 // Create the pipeline: 218 s1.pipe( i1 ) 219 .pipe( s2 ) 220 .pipe( i2 ) 221 .pipe( s3 ) 222 .pipe( i3 ) 223 .pipe( s4 ) 224 .pipe( i4 ) 225 .pipe( s5 ) 226 .pipe( i5 ) 227 .pipe( stdout ); 228 229 // Write data to the pipeline... 230 var v; 231 var i; 232 for ( i = 0; i < 100; i++ ) { 233 v = '{"value":'+i+'}'; 234 s1.write( v, 'utf8' ); 235 } 236 s1.end(); 237 ``` 238 239 </section> 240 241 <!-- /.examples --> 242 243 <section class="links"> 244 245 [stream]: https://nodejs.org/api/stream.html 246 247 [object-mode]: https://nodejs.org/api/stream.html#stream_object_mode 248 249 [transform-stream]: https://nodejs.org/api/stream.html 250 251 </section> 252 253 <!-- /.links -->