2023-08-01 11:56:47 +02:00
|
|
|
import { env as coreEnv } from "@budibase/backend-core"
|
|
|
|
import { ServiceType } from "@budibase/types"
|
2024-08-09 11:30:27 +02:00
|
|
|
import cloneDeep from "lodash/cloneDeep"
|
2023-11-20 21:52:29 +01:00
|
|
|
|
2023-08-01 11:56:47 +02:00
|
|
|
coreEnv._set("SERVICE_TYPE", ServiceType.APPS)
|
2022-11-25 20:57:07 +01:00
|
|
|
import { join } from "path"
|
2022-05-22 19:29:02 +02:00
|
|
|
|
2020-10-26 18:49:33 +01:00
|
|
|
let LOADED = false
|
2023-09-07 15:37:22 +02:00
|
|
|
if (!LOADED && coreEnv.isDev() && !coreEnv.isTest()) {
|
2022-05-22 19:29:02 +02:00
|
|
|
require("dotenv").config({
|
|
|
|
path: join(__dirname, "..", ".env"),
|
|
|
|
})
|
2020-10-26 18:49:33 +01:00
|
|
|
LOADED = true
|
|
|
|
}
|
|
|
|
|
2022-11-25 20:57:07 +01:00
|
|
|
function parseIntSafe(number?: string) {
|
2022-04-08 16:17:19 +02:00
|
|
|
if (number) {
|
|
|
|
return parseInt(number)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 16:04:08 +01:00
|
|
|
const DEFAULTS = {
|
2024-02-22 19:46:04 +01:00
|
|
|
QUERY_THREAD_TIMEOUT: 15000,
|
2024-05-13 13:53:44 +02:00
|
|
|
AUTOMATION_THREAD_TIMEOUT: 15000,
|
2024-02-14 18:15:42 +01:00
|
|
|
AUTOMATION_SYNC_TIMEOUT: 120000,
|
2024-02-14 16:04:08 +01:00
|
|
|
AUTOMATION_MAX_ITERATIONS: 200,
|
2024-02-20 20:35:34 +01:00
|
|
|
JS_PER_EXECUTION_TIME_LIMIT_MS: 1500,
|
2024-02-14 16:04:08 +01:00
|
|
|
TEMPLATE_REPOSITORY: "app",
|
|
|
|
PLUGINS_DIR: "/plugins",
|
|
|
|
FORKED_PROCESS_NAME: "main",
|
|
|
|
JS_RUNNER_MEMORY_LIMIT: 64,
|
|
|
|
}
|
|
|
|
|
|
|
|
const QUERY_THREAD_TIMEOUT =
|
|
|
|
parseIntSafe(process.env.QUERY_THREAD_TIMEOUT) ||
|
|
|
|
DEFAULTS.QUERY_THREAD_TIMEOUT
|
2024-05-13 13:53:44 +02:00
|
|
|
const DEFAULT_AUTOMATION_TIMEOUT =
|
|
|
|
QUERY_THREAD_TIMEOUT > DEFAULTS.AUTOMATION_THREAD_TIMEOUT
|
|
|
|
? QUERY_THREAD_TIMEOUT
|
|
|
|
: DEFAULTS.AUTOMATION_THREAD_TIMEOUT
|
2022-11-25 20:57:07 +01:00
|
|
|
const environment = {
|
2023-08-17 17:39:25 +02:00
|
|
|
// features
|
|
|
|
APP_FEATURES: process.env.APP_FEATURES,
|
2022-09-12 16:17:39 +02:00
|
|
|
// important - prefer app port to generic port
|
|
|
|
PORT: process.env.APP_PORT || process.env.PORT,
|
2020-05-14 16:12:30 +02:00
|
|
|
COUCH_DB_URL: process.env.COUCH_DB_URL,
|
2024-07-11 12:11:28 +02:00
|
|
|
COUCH_DB_SQL_URL: process.env.COUCH_DB_SQL_URL,
|
2021-03-23 16:37:11 +01:00
|
|
|
MINIO_URL: process.env.MINIO_URL,
|
|
|
|
WORKER_URL: process.env.WORKER_URL,
|
|
|
|
AWS_REGION: process.env.AWS_REGION,
|
2024-06-07 17:57:54 +02:00
|
|
|
AWS_SESSION_TOKEN: process.env.AWS_SESSION_TOKEN,
|
2021-03-24 13:54:59 +01:00
|
|
|
MINIO_ACCESS_KEY: process.env.MINIO_ACCESS_KEY,
|
|
|
|
MINIO_SECRET_KEY: process.env.MINIO_SECRET_KEY,
|
2021-04-27 18:29:05 +02:00
|
|
|
REDIS_URL: process.env.REDIS_URL,
|
|
|
|
REDIS_PASSWORD: process.env.REDIS_PASSWORD,
|
2023-04-28 21:23:39 +02:00
|
|
|
REDIS_CLUSTERED: process.env.REDIS_CLUSTERED,
|
2022-01-24 11:48:59 +01:00
|
|
|
HTTP_MIGRATIONS: process.env.HTTP_MIGRATIONS,
|
2023-10-31 21:34:02 +01:00
|
|
|
CLUSTER_MODE: process.env.CLUSTER_MODE,
|
2022-03-03 13:03:29 +01:00
|
|
|
API_REQ_LIMIT_PER_SEC: process.env.API_REQ_LIMIT_PER_SEC,
|
2022-03-28 17:44:33 +02:00
|
|
|
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
|
|
|
|
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,
|
2021-03-23 16:37:11 +01:00
|
|
|
// environment
|
|
|
|
NODE_ENV: process.env.NODE_ENV,
|
|
|
|
JEST_WORKER_ID: process.env.JEST_WORKER_ID,
|
|
|
|
BUDIBASE_ENVIRONMENT: process.env.BUDIBASE_ENVIRONMENT,
|
2021-09-29 17:55:59 +02:00
|
|
|
DISABLE_ACCOUNT_PORTAL: process.env.DISABLE_ACCOUNT_PORTAL,
|
2024-02-14 16:04:08 +01:00
|
|
|
TEMPLATE_REPOSITORY:
|
|
|
|
process.env.TEMPLATE_REPOSITORY || DEFAULTS.TEMPLATE_REPOSITORY,
|
2022-05-22 18:11:05 +02:00
|
|
|
DISABLE_AUTO_PROD_APP_SYNC: process.env.DISABLE_AUTO_PROD_APP_SYNC,
|
2022-08-05 18:13:03 +02:00
|
|
|
SESSION_UPDATE_PERIOD: process.env.SESSION_UPDATE_PERIOD,
|
2021-03-23 16:37:11 +01:00
|
|
|
// minor
|
2020-05-14 16:12:30 +02:00
|
|
|
SALT_ROUNDS: process.env.SALT_ROUNDS,
|
|
|
|
LOGGER: process.env.LOGGER,
|
2022-06-28 23:45:05 +02:00
|
|
|
ACCOUNT_PORTAL_URL: process.env.ACCOUNT_PORTAL_URL,
|
2022-06-23 17:08:25 +02:00
|
|
|
AUTOMATION_MAX_ITERATIONS:
|
2024-02-14 16:04:08 +01:00
|
|
|
parseIntSafe(process.env.AUTOMATION_MAX_ITERATIONS) ||
|
|
|
|
DEFAULTS.AUTOMATION_MAX_ITERATIONS,
|
2020-10-06 20:13:41 +02:00
|
|
|
DYNAMO_ENDPOINT: process.env.DYNAMO_ENDPOINT,
|
2024-02-14 16:04:08 +01:00
|
|
|
QUERY_THREAD_TIMEOUT: QUERY_THREAD_TIMEOUT,
|
2024-02-14 12:44:07 +01:00
|
|
|
AUTOMATION_THREAD_TIMEOUT:
|
2024-02-14 16:04:08 +01:00
|
|
|
parseIntSafe(process.env.AUTOMATION_THREAD_TIMEOUT) ||
|
2024-05-13 13:53:44 +02:00
|
|
|
DEFAULT_AUTOMATION_TIMEOUT,
|
2024-02-14 16:04:08 +01:00
|
|
|
PLUGINS_DIR: process.env.PLUGINS_DIR || DEFAULTS.PLUGINS_DIR,
|
2023-12-01 20:37:43 +01:00
|
|
|
MAX_IMPORT_SIZE_MB: process.env.MAX_IMPORT_SIZE_MB,
|
2024-01-25 17:22:39 +01:00
|
|
|
SESSION_EXPIRY_SECONDS: process.env.SESSION_EXPIRY_SECONDS,
|
2024-02-23 18:31:45 +01:00
|
|
|
// SQL
|
|
|
|
SQL_MAX_ROWS: process.env.SQL_MAX_ROWS,
|
|
|
|
SQL_LOGGING_ENABLE: process.env.SQL_LOGGING_ENABLE,
|
|
|
|
SQL_ALIASING_DISABLE: process.env.SQL_ALIASING_DISABLE,
|
2022-05-17 18:44:40 +02:00
|
|
|
// flags
|
2021-09-14 12:18:02 +02:00
|
|
|
ALLOW_DEV_AUTOMATIONS: process.env.ALLOW_DEV_AUTOMATIONS,
|
2021-11-22 18:42:41 +01:00
|
|
|
DISABLE_THREADING: process.env.DISABLE_THREADING,
|
2022-06-15 18:07:17 +02:00
|
|
|
DISABLE_AUTOMATION_LOGS: process.env.DISABLE_AUTOMATION_LOGS,
|
2023-11-08 19:41:15 +01:00
|
|
|
DISABLE_RATE_LIMITING: process.env.DISABLE_RATE_LIMITING,
|
2024-06-06 18:20:25 +02:00
|
|
|
DISABLE_APP_MIGRATIONS: process.env.SKIP_APP_MIGRATIONS || false,
|
2022-05-17 18:44:40 +02:00
|
|
|
MULTI_TENANCY: process.env.MULTI_TENANCY,
|
|
|
|
ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS,
|
|
|
|
SELF_HOSTED: process.env.SELF_HOSTED,
|
2023-02-14 17:40:57 +01:00
|
|
|
HTTP_MB_LIMIT: process.env.HTTP_MB_LIMIT,
|
2024-02-14 16:04:08 +01:00
|
|
|
FORKED_PROCESS_NAME:
|
|
|
|
process.env.FORKED_PROCESS_NAME || DEFAULTS.FORKED_PROCESS_NAME,
|
2024-02-08 16:20:33 +01:00
|
|
|
JS_PER_INVOCATION_TIMEOUT_MS:
|
2024-02-14 16:04:08 +01:00
|
|
|
parseIntSafe(process.env.JS_PER_EXECUTION_TIME_LIMIT_MS) ||
|
|
|
|
DEFAULTS.JS_PER_EXECUTION_TIME_LIMIT_MS,
|
2024-02-08 16:23:27 +01:00
|
|
|
JS_PER_REQUEST_TIMEOUT_MS: parseIntSafe(
|
2023-12-18 16:29:56 +01:00
|
|
|
process.env.JS_PER_REQUEST_TIME_LIMIT_MS
|
|
|
|
),
|
2024-02-14 16:04:08 +01:00
|
|
|
TOP_LEVEL_PATH:
|
|
|
|
process.env.TOP_LEVEL_PATH || process.env.SERVER_TOP_LEVEL_PATH,
|
|
|
|
APP_MIGRATION_TIMEOUT: parseIntSafe(process.env.APP_MIGRATION_TIMEOUT),
|
|
|
|
JS_RUNNER_MEMORY_LIMIT:
|
|
|
|
parseIntSafe(process.env.JS_RUNNER_MEMORY_LIMIT) ||
|
|
|
|
DEFAULTS.JS_RUNNER_MEMORY_LIMIT,
|
|
|
|
LOG_JS_ERRORS: process.env.LOG_JS_ERRORS,
|
2024-08-30 14:44:23 +02:00
|
|
|
DISABLE_USER_SYNC: process.env.DISABLE_USER_SYNC,
|
2022-05-17 18:44:40 +02:00
|
|
|
// old
|
|
|
|
CLIENT_ID: process.env.CLIENT_ID,
|
2022-11-25 20:57:07 +01:00
|
|
|
_set(key: string, value: any) {
|
2020-10-28 21:35:06 +01:00
|
|
|
process.env[key] = value
|
2022-11-28 18:54:04 +01:00
|
|
|
// @ts-ignore
|
|
|
|
environment[key] = value
|
2024-02-19 17:27:01 +01:00
|
|
|
cleanVariables()
|
2020-10-28 21:35:06 +01:00
|
|
|
},
|
2023-09-07 15:37:22 +02:00
|
|
|
isTest: coreEnv.isTest,
|
|
|
|
isJest: coreEnv.isJest,
|
|
|
|
isDev: coreEnv.isDev,
|
2021-03-24 19:21:23 +01:00
|
|
|
isProd: () => {
|
2023-09-07 15:37:22 +02:00
|
|
|
return !coreEnv.isDev()
|
2021-03-24 19:21:23 +01:00
|
|
|
},
|
2021-11-22 18:42:41 +01:00
|
|
|
isInThread: () => {
|
2023-05-04 18:05:36 +02:00
|
|
|
return process.env.FORKED_PROCESS
|
2021-11-22 18:42:41 +01:00
|
|
|
},
|
2024-02-14 16:04:08 +01:00
|
|
|
getDefaults: () => {
|
|
|
|
return DEFAULTS
|
|
|
|
},
|
2020-05-14 16:12:30 +02:00
|
|
|
}
|
2021-09-20 19:24:09 +02:00
|
|
|
|
2024-08-09 11:30:27 +02:00
|
|
|
export function setEnv(newEnvVars: Partial<typeof environment>): () => void {
|
|
|
|
const oldEnv = cloneDeep(environment)
|
|
|
|
|
|
|
|
let key: keyof typeof newEnvVars
|
|
|
|
for (key in newEnvVars) {
|
|
|
|
environment._set(key, newEnvVars[key])
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
for (const [key, value] of Object.entries(oldEnv)) {
|
|
|
|
environment._set(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function withEnv<T>(envVars: Partial<typeof environment>, f: () => T) {
|
|
|
|
const cleanup = setEnv(envVars)
|
|
|
|
const result = f()
|
|
|
|
if (result instanceof Promise) {
|
|
|
|
return result.finally(cleanup)
|
|
|
|
} else {
|
|
|
|
cleanup()
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 17:27:01 +01:00
|
|
|
function cleanVariables() {
|
|
|
|
// clean up any environment variable edge cases
|
|
|
|
for (let [key, value] of Object.entries(environment)) {
|
|
|
|
// handle the edge case of "0" to disable an environment variable
|
|
|
|
if (value === "0") {
|
|
|
|
// @ts-ignore
|
|
|
|
environment[key] = 0
|
|
|
|
}
|
|
|
|
// handle the edge case of "false" to disable an environment variable
|
|
|
|
if (value === "false") {
|
|
|
|
// @ts-ignore
|
|
|
|
environment[key] = 0
|
|
|
|
}
|
2022-12-15 12:35:22 +01:00
|
|
|
}
|
2021-09-20 19:24:09 +02:00
|
|
|
}
|
2022-11-25 20:57:07 +01:00
|
|
|
|
2024-02-19 17:27:01 +01:00
|
|
|
cleanVariables()
|
|
|
|
|
2023-01-11 10:37:37 +01:00
|
|
|
export default environment
|