time-to-botec

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

main.js (3932B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var InspectSinkStream = require( './../../../node/inspect-sink' );
     24 var isFunction = require( '@stdlib/assert/is-function' );
     25 var isBuffer = require( '@stdlib/assert/is-buffer' );
     26 var copy = require( '@stdlib/utils/copy' );
     27 var inherit = require( '@stdlib/utils/inherit' );
     28 var debug = require( './debug.js' );
     29 var DEFAULTS = require( './defaults.json' );
     30 var NAMESPACE = require( './namespace.js' );
     31 var validate = require( './validate.js' );
     32 var logger = require( './logger.js' );
     33 
     34 
     35 // MAIN //
     36 
     37 /**
     38 * Debug stream constructor.
     39 *
     40 * @constructor
     41 * @param {Options} [options] - stream options
     42 * @param {string} [options.name] - debug namespace
     43 * @param {boolean} [options.objectMode=false] - specifies whether the stream should operate in object mode
     44 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the `Buffer` level for when `write()` starts returning `false`
     45 * @param {boolean} [options.decodeStrings=true] - specifies whether to encode strings as `Buffer` objects before writing data to a returned stream
     46 * @param {string} [options.defaultEncoding='utf8'] - default encoding when not explicitly specified when writing data
     47 * @param {Callback} [clbk] - callback to invoke upon receiving data
     48 * @throws {TypeError} must provide valid options
     49 * @throws {TypeError} must a valid callback argument
     50 * @returns {DebugSinkStream} debug stream
     51 *
     52 * @example
     53 * var stream = new DebugSinkStream({
     54 *     'name': 'my-stream'
     55 * });
     56 *
     57 * stream.write( 'a' );
     58 * stream.write( 'b' );
     59 * stream.write( 'c' );
     60 * stream.end();
     61 */
     62 function DebugSinkStream( options, clbk ) {
     63 	var opts;
     64 	var name;
     65 	var log;
     66 	var err;
     67 	var cb;
     68 	if ( !(this instanceof DebugSinkStream) ) {
     69 		if ( arguments.length > 1 ) {
     70 			return new DebugSinkStream( options, clbk );
     71 		}
     72 		if ( arguments.length === 1 ) {
     73 			return new DebugSinkStream( options );
     74 		}
     75 		return new DebugSinkStream();
     76 	}
     77 	opts = copy( DEFAULTS );
     78 	if ( arguments.length > 1 ) {
     79 		if ( !isFunction( clbk ) ) {
     80 			throw new TypeError( 'invalid argument. Callback argument must be a function. Value: `' + clbk + '`.' );
     81 		}
     82 		cb = clbk;
     83 		err = validate( opts, options );
     84 	} else if ( arguments.length ) {
     85 		if ( isFunction( options ) ) {
     86 			cb = options;
     87 		} else {
     88 			err = validate( opts, options );
     89 		}
     90 	}
     91 	if ( err ) {
     92 		throw err;
     93 	}
     94 	if ( opts.name ) {
     95 		name = NAMESPACE + ':' + opts.name;
     96 	} else {
     97 		name = NAMESPACE;
     98 	}
     99 	log = logger( name );
    100 
    101 	debug( 'Creating a writable stream configured with the following options: %s.', JSON.stringify( opts ) );
    102 	InspectSinkStream.call( this, opts, inspect );
    103 
    104 	return this;
    105 
    106 	/**
    107 	* Callback invoked upon receiving a new chunk.
    108 	*
    109 	* @private
    110 	* @param {*} chunk - received chunk
    111 	* @param {NonNegativeInteger} idx - chunk index
    112 	* @returns {void}
    113 	*/
    114 	function inspect( chunk, idx ) {
    115 		if ( cb ) {
    116 			debug( 'Received a new chunk. Chunk: %s. Index: %d.', chunk.toString(), idx );
    117 			return cb( log, chunk, idx );
    118 		}
    119 		if ( isBuffer( chunk ) ) {
    120 			chunk = chunk.toString();
    121 		}
    122 		chunk = JSON.stringify( chunk );
    123 		debug( 'Received a new chunk. Chunk: %s. Index: %d.', chunk, idx );
    124 		log( 'Chunk: %s. Index: %d.', chunk, idx );
    125 	}
    126 }
    127 
    128 /*
    129 * Inherit from the `InspectSinkStream` prototype.
    130 */
    131 inherit( DebugSinkStream, InspectSinkStream );
    132 
    133 
    134 // EXPORTS //
    135 
    136 module.exports = DebugSinkStream;