time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (5779B)


      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 > [Writable stream][writable-stream] for [debugging][node-debug] stream pipelines.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var debugSinkStream = require( '@stdlib/streams/node/debug-sink' );
     31 ```
     32 
     33 <a name="debug-sink-stream"></a>
     34 
     35 #### debugSinkStream( \[options,] \[clbk] )
     36 
     37 Creates a [writable stream][writable-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 = debugSinkStream({
     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 -   **decodeStrings**: specifies whether to encode strings as `Buffer` objects before writing data to a returned [stream][stream]. Default: `true`.
     61 -   **defaultEncoding**: default encoding when not explicitly specified when writing data. Default: `'utf8'`.
     62 
     63 To set [stream][stream] `options`,
     64 
     65 ```javascript
     66 var opts = {
     67     'name': 'my-app',
     68     'objectMode': true,
     69     'highWaterMark': 64,
     70     'decodeStrings': false,
     71     'defaultEncoding': 'utf8'
     72 };
     73 
     74 var stream = debugSinkStream( 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 = debugSinkStream( opts, logger );
     91 ```
     92 
     93 #### debugSinkStream.factory( \[options] )
     94 
     95 Returns a `function` for creating [streams][writable-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 = debugSinkStream.factory( opts );
    104 ```
    105 
    106 This method accepts the same `options` as [`debugSinkStream()`](#debug-sink-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 = debugSinkStream.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 #### debugSinkStream.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 = debugSinkStream.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 [`debugSinkStream()`](#debug-sink-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 transformFactory = require( '@stdlib/streams/node/transform' ).factory;
    171 var debug = require( '@stdlib/streams/node/debug-sink' ).objectMode;
    172 
    173 function parse( chunk, enc, clbk ) {
    174     clbk( null, parseJSON( chunk ) );
    175 }
    176 
    177 function pluck( chunk, enc, clbk ) {
    178     clbk( null, chunk.value );
    179 }
    180 
    181 function square( chunk, enc, clbk ) {
    182     var v = +chunk;
    183     clbk( null, v*v );
    184 }
    185 
    186 function toStr( chunk, enc, clbk ) {
    187     clbk( null, chunk.toString() );
    188 }
    189 
    190 function join( chunk, enc, clbk ) {
    191     clbk( null, chunk+'\n' );
    192 }
    193 
    194 // Create a factory for generating streams running in `objectMode`:
    195 var tStream = transformFactory({
    196     'objectMode': true
    197 });
    198 
    199 // Create streams for each transform:
    200 var s1 = tStream( parse );
    201 var s2 = tStream( pluck );
    202 var s3 = tStream( square );
    203 var s4 = tStream( toStr );
    204 var s5 = tStream( join );
    205 
    206 // Create a writable stream for debugging the result of the transformations:
    207 var ds = debug({
    208     'name': 'debugger'
    209 });
    210 
    211 // Create the pipeline:
    212 s1.pipe( s2 )
    213     .pipe( s3 )
    214     .pipe( s4 )
    215     .pipe( s5 )
    216     .pipe( ds );
    217 
    218 // Write data to the pipeline...
    219 var v;
    220 var i;
    221 for ( i = 0; i < 100; i++ ) {
    222     v = '{"value":'+i+'}';
    223     s1.write( v, 'utf8' );
    224 }
    225 s1.end();
    226 ```
    227 
    228 </section>
    229 
    230 <!-- /.examples -->
    231 
    232 <section class="links">
    233 
    234 [stream]: https://nodejs.org/api/stream.html
    235 
    236 [object-mode]: https://nodejs.org/api/stream.html#stream_object_mode
    237 
    238 [writable-stream]: https://nodejs.org/api/stream.html
    239 
    240 [node-debug]: https://www.npmjs.com/package/debug
    241 
    242 </section>
    243 
    244 <!-- /.links -->