time-to-botec

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

factory.js (3064B)


      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 isObject = require( '@stdlib/assert/is-plain-object' );
     24 var copy = require( '@stdlib/utils/copy' );
     25 var Stream = require( './main.js' );
     26 
     27 
     28 // MAIN //
     29 
     30 /**
     31 * Creates a reusable transform stream factory.
     32 *
     33 * @param {Options} [options] - stream options
     34 * @param {boolean} [options.objectMode=false] - specifies whether a stream should operate in object mode
     35 * @param {(string|null)} [options.encoding=null] - specifies how `Buffer` objects should be decoded to `strings`
     36 * @param {NonNegativeNumber} [options.highWaterMark] - specifies the `Buffer` level for when `write()` starts returning `false`
     37 * @param {boolean} [options.allowHalfOpen=false] - specifies whether the stream should remain open even if one side ends
     38 * @param {boolean} [options.decodeStrings=true] - specifies whether to decode `strings` into `Buffer` objects when writing
     39 * @throws {TypeError} options argument must be an object
     40 * @returns {Function} transform stream factory
     41 *
     42 * @example
     43 * function transform( chunk, enc, clbk ) {
     44 *     clbk( null, chunk.toString()+'\n' );
     45 * }
     46 *
     47 * var opts = {
     48 *     'objectMode': true,
     49 *     'encoding': 'utf8',
     50 *     'highWaterMark': 64,
     51 *     'decodeStrings': false
     52 * };
     53 *
     54 * var factory = streamFactory( opts );
     55 *
     56 * // Create 10 identically configured streams...
     57 * var streams = [];
     58 * var i;
     59 * for ( i = 0; i < 10; i++ ) {
     60 *     streams.push( factory( transform ) );
     61 * }
     62 */
     63 function streamFactory( options ) {
     64 	var opts;
     65 	if ( arguments.length ) {
     66 		if ( !isObject( options ) ) {
     67 			throw new TypeError( 'invalid argument. Options argument must be an object. Value: `' + options + '`.' );
     68 		}
     69 		opts = copy( options );
     70 	} else {
     71 		opts = {};
     72 	}
     73 	return createStream;
     74 
     75 	/**
     76 	* Creates a transform stream.
     77 	*
     78 	* @private
     79 	* @param {Function} transform - callback to invoke upon receiving a new chunk
     80 	* @param {Function} [flush] - callback to invoke after receiving all chunks and prior to the stream closing
     81 	* @throws {TypeError} must provide valid options
     82 	* @throws {TypeError} transform callback must be a function
     83 	* @throws {TypeError} flush callback must be a function
     84 	* @returns {TransformStream} transform stream
     85 	*/
     86 	function createStream( transform, flush ) {
     87 		opts.transform = transform;
     88 		if ( arguments.length > 1 ) {
     89 			opts.flush = flush;
     90 		} else {
     91 			delete opts.flush; // clear any previous `flush`
     92 		}
     93 		return new Stream( opts );
     94 	}
     95 }
     96 
     97 
     98 // EXPORTS //
     99 
    100 module.exports = streamFactory;