README.md (5982B)
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 # Debug Stream 22 23 > [Transform stream][transform-stream] for [debugging][node-debug] stream pipelines. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var debugStream = require( '@stdlib/streams/node/debug' ); 31 ``` 32 33 <a name="debug-stream"></a> 34 35 #### debugStream( \[options,] \[clbk] ) 36 37 Creates a [transform stream][transform-stream] for [debugging][node-debug] stream pipelines. 38 39 ```javascript 40 var ENV = require( '@stdlib/process/env' ); 41 42 // Set the `DEBUG` environment variable... 43 ENV.DEBUG = '*'; 44 45 var stream = debugStream({ 46 'name': 'my-stream' 47 }); 48 49 stream.write( 'a' ); 50 stream.write( 'b' ); 51 stream.write( 'c' ); 52 stream.end(); 53 ``` 54 55 The function accepts the following `options`: 56 57 - **name**: [debug][node-debug] namespace. 58 - **objectMode**: specifies whether a [stream][stream] should operate in [objectMode][object-mode]. Default: `false`. 59 - **highWaterMark**: specifies the `Buffer` level at which `write()` calls start returning `false`. 60 - **allowHalfOpen**: specifies whether a [stream][stream] should remain open even if one side ends. Default: `false`. 61 - **readableObjectMode**: specifies whether the readable side should be in [objectMode][object-mode]. Default: `false`. 62 63 To set [stream][stream] `options`, 64 65 ```javascript 66 var opts = { 67 'name': 'my-app', 68 'objectMode': true, 69 'highWaterMark': 64, 70 'allowHalfOpen': true, 71 'readableObjectMode': false // overridden by `objectMode` option when `objectMode=true` 72 }; 73 74 var stream = debugStream( opts ); 75 ``` 76 77 By default, each `chunk` is logged as a JSON stringified `string`, along with the `chunk` index. For more control over logging behavior, provide a `callback`. 78 79 ```javascript 80 function logger( debug, chunk, idx ) { 81 debug( 'Received a new chunk...' ); 82 debug( 'Beep: %s', chunk.beep ); 83 debug( 'Boop: %s', chunk.boop ); 84 } 85 86 var opts = { 87 'name': 'my-pipeline' 88 }; 89 90 var stream = debugStream( opts, logger ); 91 ``` 92 93 #### debugStream.factory( \[options] ) 94 95 Returns a `function` for creating [streams][transform-stream] which are identically configured according to provided `options`. 96 97 ```javascript 98 var opts = { 99 'objectMode': true, 100 'highWaterMark': 64 101 }; 102 103 var factory = debugStream.factory( opts ); 104 ``` 105 106 This method accepts the same `options` as [`debugStream()`](#debug-stream), **except** for `name`, which must be provided **explicitly**. 107 108 ##### factory( name\[, clbk] ) 109 110 Creates a [debug][node-debug] stream. 111 112 ```javascript 113 var factory = debugStream.factory(); 114 115 var streams = []; 116 var i; 117 118 // Assign each stream to a separate debug namespace... 119 for ( i = 0; i < 10; i++ ) { 120 streams.push( factory( 'stream '+i ) ); 121 } 122 ``` 123 124 #### debugStream.objectMode( \[options,] \[clbk] ) 125 126 This method is a convenience function to create [streams][stream] which **always** operate in [objectMode][object-mode]. 127 128 ```javascript 129 var stream = debugStream.objectMode({ 130 'name': 'beep-boop' 131 }); 132 133 stream.write({ 134 'value': 'a' 135 }); 136 stream.write({ 137 'value': 'b' 138 }); 139 stream.write({ 140 'value': 'c' 141 }); 142 stream.end(); 143 ``` 144 145 This method accepts the same `options` as [`debugStream()`](#debug-stream); however, the method will **always** override the [objectMode][object-mode] option in `options`. 146 147 </section> 148 149 <!-- /.usage --> 150 151 <section class="notes"> 152 153 ## Notes 154 155 - If the [`DEBUG`][node-debug] environment variable is **not** set, no data is logged. 156 - Providing a `name` option is **strongly** encouraged, as the [`DEBUG`][node-debug] environment variable can be used to filter debuggers. 157 158 </section> 159 160 <!-- /.notes --> 161 162 <section class="examples"> 163 164 ## Examples 165 166 <!-- eslint no-undef: "error" --> 167 168 ```javascript 169 var parseJSON = require( '@stdlib/utils/parse-json' ); 170 var stdout = require( '@stdlib/streams/node/stdout' ); 171 var transformFactory = require( '@stdlib/streams/node/transform' ).factory; 172 var debug = require( '@stdlib/streams/node/debug' ).objectMode; 173 174 function parse( chunk, enc, clbk ) { 175 clbk( null, parseJSON( chunk ) ); 176 } 177 178 function pluck( chunk, enc, clbk ) { 179 clbk( null, chunk.value ); 180 } 181 182 function square( chunk, enc, clbk ) { 183 var v = +chunk; 184 clbk( null, v*v ); 185 } 186 187 function toStr( chunk, enc, clbk ) { 188 clbk( null, chunk.toString() ); 189 } 190 191 function join( chunk, enc, clbk ) { 192 clbk( null, chunk+'\n' ); 193 } 194 195 // Create a factory for generating streams running in `objectMode`: 196 var tStream = transformFactory({ 197 'objectMode': true 198 }); 199 200 // Create streams for each transform: 201 var s1 = tStream( parse ); 202 var d1 = debug({ 203 'name': 'parse' 204 }); 205 var s2 = tStream( pluck ); 206 var d2 = debug({ 207 'name': 'pluck' 208 }); 209 var s3 = tStream( square ); 210 var d3 = debug({ 211 'name': 'square' 212 }); 213 var s4 = tStream( toStr ); 214 var d4 = debug({ 215 'name': 'toString' 216 }); 217 var s5 = tStream( join ); 218 var d5 = debug({ 219 'name': 'join' 220 }); 221 222 // Create the pipeline: 223 s1.pipe( d1 ) 224 .pipe( s2 ) 225 .pipe( d2 ) 226 .pipe( s3 ) 227 .pipe( d3 ) 228 .pipe( s4 ) 229 .pipe( d4 ) 230 .pipe( s5 ) 231 .pipe( d5 ) 232 .pipe( stdout ); 233 234 // Write data to the pipeline... 235 var v; 236 var i; 237 for ( i = 0; i < 100; i++ ) { 238 v = '{"value":'+i+'}'; 239 s1.write( v, 'utf8' ); 240 } 241 s1.end(); 242 ``` 243 244 </section> 245 246 <!-- /.examples --> 247 248 <section class="links"> 249 250 [stream]: https://nodejs.org/api/stream.html 251 252 [object-mode]: https://nodejs.org/api/stream.html#stream_object_mode 253 254 [transform-stream]: https://nodejs.org/api/stream.html 255 256 [node-debug]: https://www.npmjs.com/package/debug 257 258 </section> 259 260 <!-- /.links -->