-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.ts
81 lines (73 loc) · 2.13 KB
/
env.ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import "dotenv/config"
// This file is responsible for handling environment variables.
// It imports all environment variables from process.env and performs necessary type conversions.
// For some values, it's better to use the converted values from this file instead of process.env directly.
const {
ENABLE_CAPTCHA,
DISABLE_CHALLENGES,
MAX_BALANCE,
MIN_TEZ,
MAX_TEZ,
MIN_CHALLENGES,
MAX_CHALLENGES,
MAX_CHALLENGES_WITH_CAPTCHA,
CHALLENGE_SIZE,
DIFFICULTY,
} = process.env
const env = {
...process.env,
DISABLE_CHALLENGES: DISABLE_CHALLENGES === "true",
ENABLE_CAPTCHA: ENABLE_CAPTCHA !== "false",
MAX_BALANCE: MAX_BALANCE ? Number(MAX_BALANCE) : null,
MIN_TEZ: MIN_TEZ ? Number(MIN_TEZ) : 1,
MAX_TEZ: MAX_TEZ ? Number(MAX_TEZ) : 6000,
CHALLENGE_SIZE: CHALLENGE_SIZE ? Number(CHALLENGE_SIZE) : 2048,
DIFFICULTY: DIFFICULTY ? Number(DIFFICULTY) : 4,
MIN_CHALLENGES: MIN_CHALLENGES ? Number(MIN_CHALLENGES) : 1,
MAX_CHALLENGES: MAX_CHALLENGES ? Number(MAX_CHALLENGES) : 550,
MAX_CHALLENGES_WITH_CAPTCHA: MAX_CHALLENGES_WITH_CAPTCHA
? Number(MAX_CHALLENGES_WITH_CAPTCHA)
: 66,
}
const vars: (keyof typeof env)[] = [
"MAX_BALANCE",
"MAX_TEZ",
"MIN_TEZ",
"CHALLENGE_SIZE",
"DIFFICULTY",
"MIN_CHALLENGES",
"MAX_CHALLENGES",
"MAX_CHALLENGES_WITH_CAPTCHA",
]
vars.forEach((v) => {
const value: any = env[v]
if (isNaN(value)) throw new Error(`Env var ${v} must be a number.`)
if (
[
"CHALLENGE_SIZE",
"DIFFICULTY",
"MIN_CHALLENGES",
"MAX_CHALLENGES",
"MAX_CHALLENGES_WITH_CAPTCHA",
].includes(v)
) {
if (value <= 0) {
throw new Error(`Env var ${v} must be greater than 0.`)
}
}
})
if (
env.MAX_CHALLENGES < env.MIN_CHALLENGES ||
env.MAX_CHALLENGES_WITH_CAPTCHA < env.MIN_CHALLENGES
) {
throw new Error(
`Env vars MAX_CHALLENGES and MAX_CHALLENGES_WITH_CAPTCHA must be
greater than or equal to MIN_CHALLENGES.`
)
}
if (env.MAX_TEZ < env.MIN_TEZ || env.MIN_TEZ <= 0 || env.MAX_TEZ <= 0) {
throw new Error(
"Env vars MAX_TEZ and MIN_TEZ must be greater than 0 and MAX_TEZ must be greater than or equal to MIN_TEZ."
)
}
export default env