import fs from "node:fs"; import path from "node:path"; import type dotenv from "@dotenvx/dotenvx"; // dotenvx adds ~40MB to every process, so it is only loaded when an env file // exists, which is rare in container and PaaS deployments. function parseEnvFile(filePath: string): Record { // oxlint-disable-next-line typescript/no-require-imports -- kept lazy so dotenvx stays off the startup path const dotenvx = require("@dotenvx/dotenvx") as typeof dotenv; return dotenvx.parse(fs.readFileSync(filePath, "utf8")); } let environment: Record = {}; const envPath = path.resolve(process.cwd(), `.env`); const envDefault = fs.existsSync(envPath) ? parseEnvFile(envPath) : {}; // Load environment specific variables, in reverse order of precedence const environments = ["production", "development", "local", "test"]; for (const env of environments) { const isEnv = process.env.NODE_ENV === env || envDefault.NODE_ENV === env; const isLocalDevelopment = env === "local" && (process.env.NODE_ENV === "development" || envDefault.NODE_ENV === "development"); if (isEnv || isLocalDevelopment) { const resolvedPath = path.resolve(process.cwd(), `.env.${env}`); if (fs.existsSync(resolvedPath)) { environment = { ...environment, ...parseEnvFile(resolvedPath), }; } } } process.env = { ...envDefault, ...environment, ...process.env, }; /** * Wraps an environment record so that Docker-style file secrets are resolved * lazily. When a variable is read through the proxy and has no value, but a * corresponding `_FILE` variable is set, the referenced file is read and * its contents – trimmed of leading/trailing whitespace – are cached on the * underlying record and returned. If the base variable is already set, the * file is not read. * * @param env - the environment record to wrap. * @returns a proxy over the record that resolves `_FILE` secrets on read. */ export function withFileSecrets( env: Record ): Record { return new Proxy(env, { get(target, prop, receiver) { if (typeof prop !== "string" || !prop.length || prop.endsWith("_FILE")) { return Reflect.get(target, prop, receiver); } const value = target[prop]; if (value !== undefined) { return value; } const filePath = target[`${prop}_FILE`]; if (!filePath) { return undefined; } try { target[prop] = fs.readFileSync(filePath, "utf8").trim(); } catch (err) { // oxlint-disable-next-line no-console console.error( `Failed to read file for ${prop}_FILE (${filePath}): ${(err as Error).message}` ); } return target[prop]; }, }); } export default withFileSecrets(process.env);