-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathconfig.js
219 lines (202 loc) · 6.03 KB
/
config.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import {
modes as MaintenanceModes,
DEFAULT_MODE,
} from './middleware/maintenance.js'
/**
* @typedef {import('./bindings').RawEnvConfiguration} RawEnvConfiguration
* @typedef {import('./bindings').ServiceConfiguration} ServiceConfiguration
* @typedef {import('./bindings').RuntimeEnvironmentName} RuntimeEnvironmentName
*/
/**
* If the CLUSTER_SERVICE variable is set, the service URL will be resolved from here.
*
* @type Record<string, string> */
const CLUSTER_SERVICE_URLS = {
IpfsCluster: 'https://nft.storage.ipfscluster.io/api/',
IpfsCluster2: 'https://nft2.storage.ipfscluster.io/api/',
IpfsCluster3: 'https://nft3.storage.ipfscluster.io/api/',
}
/**
* Load a {@link ServiceConfiguration} from the global environment.
* @returns {ServiceConfiguration}
*/
export const getServiceConfig = () => {
const vars = loadConfigVariables()
return serviceConfigFromVariables(vars)
}
/**
* Parse a {@link ServiceConfiguration} out of the given `configVars` map.
* @param {RawEnvConfiguration} vars map of variable names to values.
*
* Exported for testing. See {@link getServiceConfig} for main public accessor.
*
* @returns {ServiceConfiguration}
*/
export function serviceConfigFromVariables(vars) {
let clusterUrl
if (vars.CLUSTER_SERVICE) {
clusterUrl = CLUSTER_SERVICE_URLS[vars.CLUSTER_SERVICE]
if (!clusterUrl) {
throw new Error(`unknown cluster service: ${vars.CLUSTER_SERVICE}`)
}
}
if (vars.CLUSTER_API_URL) {
clusterUrl = vars.CLUSTER_API_URL
}
if (!clusterUrl || (vars.CLUSTER_SERVICE && vars.CLUSTER_API_URL)) {
throw new Error(
`One of CLUSTER_SERVICE or CLUSTER_API_URL must be set in ENV`
)
}
return {
ENV: parseRuntimeEnv(vars.ENV),
DEBUG: boolValue(vars.DEBUG),
MAINTENANCE_MODE: maintenanceModeFromString(vars.MAINTENANCE_MODE),
SALT: vars.SALT,
SATNAV: vars.SATNAV,
DUDEWHERE: vars.DUDEWHERE,
CARPARK: vars.CARPARK,
CARPARK_URL: vars.CARPARK_URL,
DATABASE_URL: vars.DATABASE_URL,
DATABASE_TOKEN: vars.DATABASE_TOKEN,
CLUSTER_API_URL: clusterUrl,
CLUSTER_BASIC_AUTH_TOKEN: vars.CLUSTER_BASIC_AUTH_TOKEN,
MAGIC_SECRET_KEY: vars.MAGIC_SECRET_KEY,
SENTRY_DSN: vars.SENTRY_DSN,
METAPLEX_AUTH_TOKEN: vars.METAPLEX_AUTH_TOKEN,
MAILCHIMP_API_KEY: vars.MAILCHIMP_API_KEY,
LINKDEX_URL: vars.LINKDEX_URL,
LOGTAIL_TOKEN: vars.LOGTAIL_TOKEN,
S3_ENDPOINT: vars.S3_ENDPOINT,
S3_REGION: vars.S3_REGION,
S3_ACCESS_KEY_ID: vars.S3_ACCESS_KEY_ID,
S3_SECRET_ACCESS_KEY: vars.S3_SECRET_ACCESS_KEY,
S3_BUCKET_NAME: vars.S3_BUCKET_NAME,
SLACK_USER_REQUEST_WEBHOOK_URL: vars.SLACK_USER_REQUEST_WEBHOOK_URL,
PRIVATE_KEY: vars.PRIVATE_KEY,
// These are injected in esbuild
// @ts-ignore
BRANCH: vars.NFT_STORAGE_BRANCH || NFT_STORAGE_BRANCH,
// @ts-ignore
VERSION: vars.NFT_STORAGE_VERSION || NFT_STORAGE_VERSION,
// @ts-ignore
COMMITHASH: vars.NFT_STORAGE_COMMITHASH || NFT_STORAGE_COMMITHASH,
}
}
/**
* Loads configuration variables from the global environment and returns a JS object
* keyed by variable names.
*
* Exported for testing. See {@link getServiceConfig} for main config accessor.
*
* @returns { RawEnvConfiguration } an object with `vars` containing all config variables and their values. guaranteed to have a value for each key defined in DEFAULT_CONFIG_VALUES
* @throws if a config variable is missing, unless ENV is 'test' or 'dev', in which case the default value will be used for missing vars.
*/
export function loadConfigVariables() {
/** @type RawEnvConfiguration */
const vars = {}
/** @type Record<string, unknown> */
const globals = globalThis
const required = [
'ENV',
'DEBUG',
'SALT',
'SATNAV',
'DUDEWHERE',
'CARPARK',
'CARPARK_URL',
'DATABASE_URL',
'DATABASE_TOKEN',
'MAGIC_SECRET_KEY',
'MAILCHIMP_API_KEY',
'METAPLEX_AUTH_TOKEN',
'LOGTAIL_TOKEN',
'PRIVATE_KEY',
'SENTRY_DSN',
'CLUSTER_BASIC_AUTH_TOKEN',
'MAINTENANCE_MODE',
'S3_REGION',
'S3_ACCESS_KEY_ID',
'S3_SECRET_ACCESS_KEY',
'S3_BUCKET_NAME',
]
for (const name of required) {
const val = globals[name]
if (typeof val === 'string') {
vars[name] = val
} else if (val !== null && typeof val === 'object') {
// some globals are objects like an R2Bucket, bound for us by Cloudflare
vars[name] = val
} else {
throw new Error(
`Missing required config variables: ${name}. Check your .env, testing globals or cloudflare vars.`
)
}
}
const optional = [
'CLUSTER_SERVICE',
'CLUSTER_API_URL',
'LINKDEX_URL',
'S3_ENDPOINT',
'SLACK_USER_REQUEST_WEBHOOK_URL',
]
for (const name of optional) {
const val = globals[name]
if (typeof val === 'string') {
vars[name] = val
} else {
if (globals.DEBUG === 'true') {
console.warn(`Missing optional config variables: ${name}`)
}
}
}
return vars
}
/**
* Validates that `s` is a defined runtime environment name and returns it.
*
* @param {unknown} s
* @returns {RuntimeEnvironmentName}
*/
function parseRuntimeEnv(s) {
if (!s) {
return 'test'
}
switch (s) {
case 'test':
case 'dev':
case 'staging':
case 'production':
return s
default:
throw new Error('invalid runtime environment name: ' + s)
}
}
/**
* If `s` is undefined, return the default maintenance mode. Otherwise, make sure it's a valid mode and return.
*
* @param {string|undefined} s
* @returns {import('./middleware/maintenance').Mode}
*/
function maintenanceModeFromString(s) {
if (s === undefined) {
return DEFAULT_MODE
}
for (const m of MaintenanceModes) {
if (s === m) {
return m
}
}
throw new Error(
`invalid maintenance mode value "${s}". valid choices: ${MaintenanceModes}`
)
}
/**
* Returns `true` if the string `s` is equal to `"true"` (case-insensitive) or `"1", and false for `"false"`, `"0"` or an empty value.
*
* @param {string} s
* @returns {boolean}
*/
function boolValue(s) {
return Boolean(s && JSON.parse(String(s).toLowerCase()))
}