-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathconfig.js
More file actions
228 lines (207 loc) · 6.22 KB
/
config.js
File metadata and controls
228 lines (207 loc) · 6.22 KB
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
220
221
222
223
224
225
226
227
228
import {
modes as MaintenanceModes,
DEFAULT_MODE,
} from './middleware/maintenance.js'
/**
* @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/',
}
const DEFAULT_PSA_QUOTA = 100
/** @type ServiceConfiguration|undefined */
let _globalConfig
/**
* Returns a {@link ServiceConfiguration} object containing the runtime config options for the API service.
* Includes anything injected by the environment (secrets, URLs for services we call out to, maintenance flag, etc).
*
* Loaded from global variables injected by CloudFlare Worker runtime.
*
* Lazily loaded and cached on first access.
*/
export const getServiceConfig = () => {
if (_globalConfig) {
return _globalConfig
}
_globalConfig = loadServiceConfig()
return _globalConfig
}
/**
* Override the global service configuration for testing purposes.
* Note that some files call {@link getServiceConfig} at module scope,
* so they may not pick up the overriden config.
*
* @param {ServiceConfiguration} config
*/
export const overrideServiceConfigForTesting = (config) => {
_globalConfig = config
}
/**
* Load a {@link ServiceConfiguration} from the global environment.
*
* Exported for testing. See {@link getServiceConfig} for main public accessor.
*
* @returns {ServiceConfiguration}
*/
export function loadServiceConfig() {
const vars = loadConfigVariables()
return serviceConfigFromVariables(vars)
}
/**
* Parse a {@link ServiceConfiguration} out of the given `configVars` map.
* @param {Record<string, string>} 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 = vars.CLUSTER_API_URL
} else {
clusterUrl = CLUSTER_SERVICE_URLS[vars.CLUSTER_SERVICE]
if (!clusterUrl) {
throw new Error(`unknown cluster service: ${vars.CLUSTER_SERVICE}`)
}
}
return {
ENV: parseRuntimeEnv(vars.ENV),
DEBUG: boolValue(vars.DEBUG),
MAINTENANCE_MODE: maintenanceModeFromString(vars.MAINTENANCE_MODE),
SALT: vars.SALT,
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,
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,
PSA_QUOTA: vars.PSA_QUOTA ? Number(vars.PSA_QUOTA) : DEFAULT_PSA_QUOTA,
PRIVATE_KEY: vars.PRIVATE_KEY,
// These are injected in esbuild
// @ts-ignore
BRANCH: NFT_STORAGE_BRANCH,
// @ts-ignore
VERSION: NFT_STORAGE_VERSION,
// @ts-ignore
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 { Record<string, string>} 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 Record<string, string> */
const vars = {}
/** @type Record<string, unknown> */
const globals = globalThis
const required = [
'ENV',
'DEBUG',
'SALT',
'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',
'PSA_QUOTA',
]
for (const name of required) {
const val = globals[name]
if (typeof val === 'string') {
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', 'S3_ENDPOINT']
for (const name of optional) {
const val = globals[name]
if (typeof val === 'string') {
vars[name] = val
} else {
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()))
}