time-to-botec

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

env.js (2208B)


      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 objectKeys = require( './../../../keys' );
     24 var ENV = require( '@stdlib/process/env' );
     25 
     26 
     27 // FUNCTIONS //
     28 
     29 /**
     30 * Copies `process.env`.
     31 *
     32 * ## Notes
     33 *
     34 * -   This implementation accommodates `process.env` on older Node.js versions (<=v0.10.x), where `process.env` was object-like, but would show unexpected behavior when attempting to get own property descriptors.
     35 *
     36 * @private
     37 * @returns {Object} copy of `process.env`
     38 */
     39 function copy() {
     40 	var keys;
     41 	var env;
     42 	var out;
     43 	var i;
     44 
     45 	keys = objectKeys( ENV );
     46 	out = {};
     47 	for ( i = 0; i < keys.length; i++ ) {
     48 		env = keys[ i ];
     49 		out[ env ] = ENV[ env ];
     50 	}
     51 	return out;
     52 }
     53 
     54 
     55 // MAIN //
     56 
     57 /**
     58 * Returns worker environment variables.
     59 *
     60 * @private
     61 * @param {Options} opts - options
     62 * @param {string} opts.cmd - executable file/command
     63 * @param {boolean} opts.ordered - boolean indicating whether to preserve order of script output
     64 * @param {(NonNegativeInteger|null)} opts.uid - process user identity
     65 * @param {(NonNegativeInteger|null)} opts.gid - process group identity
     66 * @param {string} opts.encoding - `stdio` encoding
     67 * @param {NonNegativeInteger} opts.maxBuffer - max child process `stdio` buffer size
     68 * @returns {Object} environment variables
     69 */
     70 function env( opts ) {
     71 	var out = copy();
     72 
     73 	out.WORKER_CMD = opts.cmd;
     74 	out.WORKER_ENCODING = opts.encoding;
     75 	out.WORKER_MAX_BUFFER = opts.maxBuffer;
     76 
     77 	if ( opts.ordered ) {
     78 		out.WORKER_ORDERED = '1';
     79 	}
     80 	if ( opts.uid ) {
     81 		out.WORKER_UID = opts.uid;
     82 	}
     83 	if ( opts.gid ) {
     84 		out.WORKER_GID = opts.gid;
     85 	}
     86 	return out;
     87 }
     88 
     89 
     90 // EXPORTS //
     91 
     92 module.exports = env;