Expand dotenv references for Elysia backend

This commit is contained in:
mantrakp04
2026-06-19 09:54:21 -07:00
parent 17f2438803
commit 5abdf26702
3 changed files with 38 additions and 0 deletions
+1
View File
@@ -1,4 +1,5 @@
import "@/polyfills";
import "./server/env-expand";
import "@/instrument";
import { app } from "./server/app";
+36
View File
@@ -0,0 +1,36 @@
/* eslint-disable no-restricted-syntax -- This bootstrap normalizes process.env before getEnvVariable reads it. */
const envReferencePattern = /\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-(.*?))?\}|\$([A-Za-z_][A-Za-z0-9_]*)/g;
function expandEnvValue(value: string): string {
return value.replace(envReferencePattern, (_match, bracedName: string | undefined, defaultValue: string | undefined, bareName: string | undefined) => {
const name = bracedName ?? bareName;
if (name == null) {
return "";
}
const referencedValue = process.env[name];
if (bracedName != null && defaultValue != null && (referencedValue == null || referencedValue === "")) {
return defaultValue;
}
return referencedValue ?? "";
});
}
for (let iteration = 0; iteration < 10; iteration++) {
let changed = false;
for (const [key, value] of Object.entries(process.env)) {
if (value == null || !envReferencePattern.test(value)) {
envReferencePattern.lastIndex = 0;
continue;
}
envReferencePattern.lastIndex = 0;
const expandedValue = expandEnvValue(value);
if (expandedValue !== value) {
process.env[key] = expandedValue;
changed = true;
}
}
if (!changed) {
break;
}
}
+1
View File
@@ -1,4 +1,5 @@
import "@/polyfills";
import "./env-expand";
import "@/instrument";
import { getEnvVariable } from "@hexclave/shared/dist/utils/env";
import { app } from "./app";