time-to-botec

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

main.js (2336B)


      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 IS_NODE = require( './../../is-node' );
     24 var p = require( './parent.js' );
     25 var stacktrace = require( './stacktrace.js' );
     26 
     27 
     28 // VARIABLES //
     29 
     30 // The module id for the builtin REPL was `repl` in Node versions <4 and then changed to `<repl>` for Node versions >=4 (see https://github.com/nodejs/node/commit/ee72ee753118f2576bfd1ccf58fb2ff651e8bfb0#diff-b13d72249263845d8e8341db0426f9d3R527).
     31 var RE_MODULE_ID = /^repl$|^<repl>$/;
     32 
     33 // This is V8 specific (!!), as this assumes the V8 stacktrace API. TODO: rely on an engine agnostic approach to determining callsites.
     34 var RE_ERROR_STACK = /at REPLServer/;
     35 
     36 
     37 // MAIN //
     38 
     39 /**
     40 * Returns a boolean indicating if the function is called from a Node.js REPL environment.
     41 *
     42 * ## Notes
     43 *
     44 * -   False positives are possible due to the existence of a userland package having the same module `id` (see [repl][repl-template-lib]) as the builtin Node.js `repl`.
     45 *
     46 * [repl-template-lib]: https://www.npmjs.com/package/repl
     47 *
     48 * @returns {boolean} boolean indicating if the function is called from a Node.js REPL environment
     49 *
     50 * @example
     51 * var bool = isNodeREPL();
     52 * // returns <boolean>
     53 */
     54 function isNodeREPL() {
     55 	var stack;
     56 	var m;
     57 	if ( !IS_NODE ) {
     58 		return false;
     59 	}
     60 	// If this module was required in a REPL environment, we can walk up the module dependency tree to find a `repl` ancestor...
     61 	m = p();
     62 	while ( m ) {
     63 		if ( RE_MODULE_ID.test( m.id ) ) {
     64 			return true;
     65 		}
     66 		m = m.parent;
     67 	}
     68 	// Unable to find a `repl` ancestor, so try determining if this function was called from a REPL environment...
     69 	stack = stacktrace();
     70 	if ( stack ) {
     71 		return RE_ERROR_STACK.test( stack );
     72 	}
     73 	return false;
     74 }
     75 
     76 
     77 // EXPORTS //
     78 
     79 module.exports = isNodeREPL;