Select Git revision
dimension.mjs
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}