Skip to content
Snippets Groups Projects
Select Git revision
  • 9eed9cea28e841db66c6bab508647832d4c57b37
  • master default protected
  • 1.31
  • 4.38.3
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
  • 4.31.0
  • 4.30.1
  • 4.30.0
  • 4.29.1
23 results

runtime.mjs

Blame
  • runtime.mjs 2.48 KiB
    /**
     * Copyright schukai GmbH and contributors 2023. All Rights Reserved.
     * Node module: @schukai/monster
     * This file is licensed under the AGPLv3 License.
     * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
     */
    
    const ENV_AWS_LAMBDA = 'aws-lambda';
    const ENV_GOOGLE_FUNCTIONS = 'google-functions';
    const ENV_ELECTRON = 'electron';
    const ENV_NODE = 'node';
    const ENV_BROWSER = 'browser';
    const ENV_WEB_WORKER = 'web-worker';
    const ENV_DENO = 'deno';
    const ENV_UNKNOWN = 'unknown';
    
    /**
     * Detects and returns the current runtime environment.
     * 
     * - 'aws-lambda': AWS Lambda environment
     * - 'google-functions': Google Cloud Functions environment
     * - 'electron': Electron environment
     * - 'node': Node.js environment
     * - 'browser': Browser environment
     * - 'web-worker': Web Worker environment
     * - 'deno': Deno environment
     * - 'react-native': React Native environment
     * - 'unknown': Unknown environment
     * 
     * @returns {string} The detected runtime environment. Possible values are:
     */
    function detectRuntimeEnvironment() {
        // AWS Lambda environment
        if (
            typeof process !== 'undefined' &&
            process.env != null &&
            process.env.AWS_LAMBDA_FUNCTION_NAME
        ) {
            return ENV_AWS_LAMBDA;
        }
    
        // Google Cloud Functions environment
        if (
            typeof process !== 'undefined' &&
            process.env != null &&
            process.env.FUNCTION_NAME
        ) {
            return ENV_GOOGLE_FUNCTIONS;
        }
    
        // Node.js environment
        if (
            typeof process !== 'undefined' &&
            process.versions != null &&
            process.versions.node != null
        ) {
            // Electron environment
            if (process.versions.electron != null) {
                return ENV_ELECTRON;
            }
            return ENV_NODE;
        }
    
        // Browser environment
        if (
            typeof window !== 'undefined' &&
            typeof window.document !== 'undefined' &&
            typeof navigator !== 'undefined' &&
            typeof navigator.userAgent === 'string'
        ) {
            // Web Worker environment
            if (typeof self === 'object' && typeof importScripts === 'function') {
                return ENV_WEB_WORKER;
            }
            return ENV_BROWSER;
        }
    
        // Deno environment
        if (typeof Deno !== 'undefined') {
            return ENV_DENO;
        }
    
        // Unknown environment
        return ENV_UNKNOWN;
    }
    
    export {
        ENV_AWS_LAMBDA,
        ENV_GOOGLE_FUNCTIONS,
        ENV_ELECTRON,
        ENV_NODE,
        ENV_BROWSER,
        ENV_WEB_WORKER,
        ENV_DENO,
        ENV_UNKNOWN,
        detectRuntimeEnvironment}