time-to-botec

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

main.js (2871B)


      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 proc = require( 'process' );
     24 var getGlobal = require( '@stdlib/utils/global' );
     25 var nativeClass = require( '@stdlib/utils/native-class' );
     26 var isObject = require( './../../is-plain-object' );
     27 var isString = require( './../../is-string' ).isPrimitive;
     28 var globalScope = require( './global_scope.js' );
     29 
     30 
     31 // VARIABLES //
     32 
     33 var Global = getGlobal();
     34 var RE = /node|io\.js/;
     35 
     36 
     37 // MAIN //
     38 
     39 /**
     40 * Returns a boolean indicating if the runtime is Node.js.
     41 *
     42 * @returns {boolean} boolean indicating if runtime is Node.js
     43 *
     44 * @example
     45 * var bool = isNode();
     46 * // returns <boolean>
     47 */
     48 function isNode() {
     49 	return (
     50 		// Check for presence of `global` variable:
     51 		typeof global === 'object' &&
     52 
     53 		// Check that the `global` variable matches the determined global variable:
     54 		global === Global &&
     55 
     56 		// Check for a circular reference to the global variable:
     57 		Global === Global.global &&
     58 
     59 		// Check that the global variable has the expected internal class:
     60 		(
     61 			// Node < v7
     62 			nativeClass( Global ) === '[object global]' ||
     63 
     64 			// Node >= v7 (https://github.com/nodejs/node/issues/9274)
     65 			nativeClass( Global ) === '[object Object]'
     66 		) &&
     67 
     68 		// Check that the `global` variable is equal to the global scope:
     69 		globalScope === true &&
     70 
     71 		// Check for a `require` global variable:
     72 		typeof require === 'function' &&
     73 
     74 		// Check for a `process` global variable:
     75 		typeof proc === 'object' &&
     76 
     77 		// Check that the `process` global variable has the expected internal class:
     78 		(
     79 			// Node < v14.6.0
     80 			nativeClass( proc ) === '[object process]' ||
     81 
     82 			// Node >= v14.6.0
     83 			nativeClass( proc ) === '[object Object]'
     84 		) &&
     85 
     86 		// Check for a `versions` property:
     87 		isObject( proc.versions ) &&
     88 
     89 		// Check for a `node` property:
     90 		isString( proc.versions.node ) &&
     91 
     92 		(
     93 			// `process.release` was added in Node v3.0.0 via io.js:
     94 			typeof proc.release === 'undefined' ||
     95 
     96 			(
     97 				// Check for a `release` property:
     98 				isObject( proc.release ) &&
     99 
    100 				// Check for a `name` property:
    101 				isString( proc.release.name ) &&
    102 
    103 				// Check that the release name contains either `node` or `io.js` (in Node.js/io.js, the release name is read-only):
    104 				RE.test( proc.release.name )
    105 			)
    106 		)
    107 	);
    108 }
    109 
    110 
    111 // EXPORTS //
    112 
    113 module.exports = isNode;