Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/runtime/internal/utils.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,24 @@ export type EnvOptions = {
envExpansion?: boolean;
};

// `useRuntimeConfig(event)` applies env overrides to the whole runtime config on every request, and the
// env name only depends on the key, so it is converted once instead of once per key per request
const envKeys = new Map<string, string>();
const MAX_ENV_KEYS = 5000;

function toEnvKey(key: string) {
let envKey = envKeys.get(key);
if (envKey === undefined) {
envKey = snakeCase(key).toUpperCase();
if (envKeys.size < MAX_ENV_KEYS) {
envKeys.set(key, envKey);
}
}
return envKey;
}

export function getEnv(key: string, opts: EnvOptions) {
const envKey = snakeCase(key).toUpperCase();
const envKey = toEnvKey(key);
return destr(
process.env[opts.prefix + envKey] ?? process.env[opts.altPrefix + envKey]
);
Expand Down
45 changes: 44 additions & 1 deletion test/unit/utils.env.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { afterEach, describe, expect, it } from "vitest";
import { applyEnv } from "../../src/runtime/internal/utils.env";

describe("env utils", () => {
Expand Down Expand Up @@ -39,6 +39,49 @@ describe("env utils", () => {
}
});

// Env names of config keys are cached, env values must not be
describe("env changes between calls", () => {
const envKeys = [
"NITRO_API_BASE_URL",
"APP_API_BASE_URL",
"NITRO_PUBLIC_NESTED",
"NITRO_PUBLIC_NESTED_DEEP_KEY",
];
const opts = { prefix: "NITRO_", altPrefix: "APP_" };
const config = () => ({
apiBaseUrl: "default",
public: { nested: { deepKey: "default" } },
});

afterEach(() => {
for (const key of envKeys) {
delete process.env[key];
}
});

it("reads the current env value on every call", () => {
process.env.NITRO_API_BASE_URL = "first";
expect(applyEnv(config(), opts).apiBaseUrl).toBe("first");
process.env.NITRO_API_BASE_URL = "second";
expect(applyEnv(config(), opts).apiBaseUrl).toBe("second");
delete process.env.NITRO_API_BASE_URL;
process.env.APP_API_BASE_URL = "alt";
expect(applyEnv(config(), opts).apiBaseUrl).toBe("alt");
delete process.env.APP_API_BASE_URL;
expect(applyEnv(config(), opts)).toEqual(config());
});

it("does not keep nested or object overrides after env is removed", () => {
process.env.NITRO_PUBLIC_NESTED_DEEP_KEY = "deep";
expect(applyEnv(config(), opts).public.nested.deepKey).toBe("deep");
delete process.env.NITRO_PUBLIC_NESTED_DEEP_KEY;
process.env.NITRO_PUBLIC_NESTED = JSON.stringify({ deepKey: "obj" });
expect(applyEnv(config(), opts).public.nested.deepKey).toBe("obj");
delete process.env.NITRO_PUBLIC_NESTED;
expect(applyEnv(config(), opts)).toEqual(config());
});
});

describe("envExpansion", () => {
const tests = [
// envExpansion is false by default
Expand Down