mirror of
https://github.com/stack-auth/stack.git
synced 2026-07-20 21:29:36 +08:00
Expand dotenv references for Elysia backend
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import "@/polyfills";
|
||||
import "./server/env-expand";
|
||||
import "@/instrument";
|
||||
import { app } from "./server/app";
|
||||
|
||||
|
||||
@@ -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,4 +1,5 @@
|
||||
import "@/polyfills";
|
||||
import "./env-expand";
|
||||
import "@/instrument";
|
||||
import { getEnvVariable } from "@hexclave/shared/dist/utils/env";
|
||||
import { app } from "./app";
|
||||
|
||||
Reference in New Issue
Block a user