main.js (2287B)
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 /* global WorkerGlobalScope, WorkerNavigator, WorkerLocation, self, importScripts, navigator, location */ 20 21 'use strict'; 22 23 // MODULES // 24 25 var getGlobal = require( '@stdlib/utils/global' ); 26 var IS_NODE = require( './../../is-node' ); 27 var isObject = require( './../../is-plain-object' ); 28 var globalScope = require( './global_scope.js' ); 29 30 31 // VARIABLES // 32 33 var Global = getGlobal(); 34 35 36 // MAIN // 37 38 /** 39 * Returns a boolean indicating if the runtime is a web worker. 40 * 41 * @returns {boolean} boolean indicating if runtime is a web worker 42 * 43 * @example 44 * var bool = isWebWorker(); 45 * // returns <boolean> 46 */ 47 function isWebWorker() { 48 return ( 49 // Check that we are not running in a Node.js runtime: 50 IS_NODE === false && 51 52 // Check for presence of `WorkerGlobalScope` global variable: 53 typeof WorkerGlobalScope === 'object' && 54 55 // Check for presence of `WorkerNavigator` global variable: 56 isObject( WorkerNavigator ) && 57 58 // Check that the `navigator` global object is an instance of `WorkerNavigator`: 59 navigator instanceof WorkerNavigator && 60 61 // Check for presence of `WorkerLocation` global variable: 62 isObject( WorkerLocation ) && 63 64 // Check that the `location` global object is an instance of `WorkerLocation`: 65 location instanceof WorkerLocation && 66 67 // Check for presence of `self` variable: 68 typeof self === 'object' && 69 70 // Check that the `self` variable matches the determined global variable: 71 self === Global && 72 73 // Check that the `self` variable is equal to the global scope: 74 globalScope === true && 75 76 // Check for presence of `importScripts` function: 77 typeof importScripts === 'function' 78 ); 79 } 80 81 82 // EXPORTS // 83 84 module.exports = isWebWorker;