forked from sveltejs/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.js
More file actions
39 lines (36 loc) · 807 Bytes
/
env.js
File metadata and controls
39 lines (36 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* global ENV_PREFIX */
const expected = new Set([
'SOCKET_PATH',
'HOST',
'PORT',
'ORIGIN',
'XFF_DEPTH',
'ADDRESS_HEADER',
'PROTOCOL_HEADER',
'HOST_HEADER',
'PORT_HEADER',
'BODY_SIZE_LIMIT',
'LISTEN_PID',
'LISTEN_FDS',
'TIMEOUT'
]);
if (ENV_PREFIX) {
for (const name in process.env) {
if (name.startsWith(ENV_PREFIX)) {
const unprefixed = name.slice(ENV_PREFIX.length);
if (!expected.has(unprefixed)) {
throw new Error(
`You should change envPrefix (${ENV_PREFIX}) to avoid conflicts with existing environment variables — unexpectedly saw ${name}`
);
}
}
}
}
/**
* @param {string} name
* @param {any} fallback
*/
export function env(name, fallback) {
const prefixed = ENV_PREFIX + name;
return prefixed in process.env ? process.env[prefixed] : fallback;
}