This repository has been archived by the owner on May 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathrunTime.js
270 lines (262 loc) · 8.74 KB
/
runTime.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright 2019 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
const { preprocessEnvVar } = require('@americanexpress/env-config-utils');
const isFetchableUrlInNode = require('@americanexpress/env-config-utils/isFetchableUrlInNode');
const isFetchableUrlInBrowser = require('@americanexpress/env-config-utils/isFetchableUrlInBrowser');
const { argv } = require('yargs');
const bytes = require('bytes');
const { getIp } = require('../../utils/getIP');
const isPositiveIntegerIfDefined = (input) => {
if (input === undefined) {
return;
}
const value = Number(input);
if (value > 0 && value % 1 === 0) {
return;
}
throw new Error(`Expected ${input} to be a positive integer`);
};
// the set of env vars to validate and normalize
// order matters, ex: ONE_CLIENT_REPORTING_URL uses HTTP_PORT
const runTime = [
{
name: 'NODE_ENV',
normalize: (input) => input && input.toLowerCase(),
valid: ['development', 'production'],
defaultValue: 'production',
},
{
name: 'ONE_DANGEROUSLY_DISABLE_CSP',
normalize: (input) => input.toLowerCase(),
validate: (input) => {
if (input === 'true' && process.env.NODE_ENV !== 'development') {
throw new Error('If you are trying to bypass CSP requirement, NODE_ENV must also be set to development.');
}
if (input === 'true' && process.env.NODE_ENV === 'development') {
console.warn('ONE_DANGEROUSLY_DISABLE_CSP is true and NODE_ENV is set to development. Content-Security-Policy header will not be set.');
}
},
valid: ['true', 'false'],
defaultValue: 'false',
},
// IPv4 port to bind on
{
name: 'HTTP_PORT',
normalize: (input) => {
const parsed = Number.parseInt(input, 10);
// make sure the parsed value is the same value as input
// eslint-disable-next-line eqeqeq -- input may be a string or a number, we don't want === in this case, just ==
if (Number.isNaN(parsed) || parsed != input) {
throw new Error(`env var HTTP_PORT needs to be a valid integer, given "${input}"`);
} else {
return parsed;
}
},
defaultValue: () => {
if (process.env.PORT) { return process.env.PORT; }
return 3000;
},
},
// IPv4 port for the health and metrics server to bind on
{
name: 'HTTP_METRICS_PORT',
normalize: (input) => {
const parsed = Number.parseInt(input, 10);
// make sure the parsed value is the same value as input
// eslint-disable-next-line eqeqeq -- input may be a string or a number, we don't want === in this case, just ==
if (Number.isNaN(parsed) || parsed != input) {
throw new Error(`env var HTTP_METRICS_PORT needs to be a valid integer, given "${input}"`);
} else {
return parsed;
}
},
defaultValue: () => 3005,
},
{
name: 'HTTP_ONE_APP_DEV_CDN_PORT',
normalize: (input) => {
if (input) {
const parsed = Number.parseInt(input, 10);
// make sure the parsed value is the same value as input
// eslint-disable-next-line eqeqeq -- input may be a string or a number, we don't want === in this case, just ==
if (Number.isNaN(parsed) || parsed != input) {
throw new Error(`env var HTTP_ONE_APP_DEV_CDN_PORT needs to be a valid integer, given "${input}"`);
} else {
return parsed;
}
}
return undefined;
},
defaultValue: () => (process.env.NODE_ENV === 'development'
? 3001
: undefined),
},
{
name: 'HTTP_ONE_APP_DEV_PROXY_SERVER_PORT',
normalize: (input) => {
if (input) {
const parsed = Number.parseInt(input, 10);
// make sure the parsed value is the same value as input
// eslint-disable-next-line eqeqeq -- input may be a string or a number, we don't want === in this case, just ==
if (Number.isNaN(parsed) || parsed != input) {
throw new Error(`env var HTTP_ONE_APP_DEV_PROXY_SERVER_PORT needs to be a valid integer, given "${input}"`);
} else {
return parsed;
}
}
return undefined;
},
defaultValue: () => (process.env.NODE_ENV === 'development'
? 3002
: undefined),
},
// holocron config, the modules to use
{
name: 'HOLOCRON_MODULE_MAP_URL',
defaultValue: () => (process.env.NODE_ENV === 'development'
? `http://${getIp()}:${process.env.HTTP_ONE_APP_DEV_CDN_PORT}/static/module-map.json`
: undefined),
validate: isFetchableUrlInNode,
},
{
name: 'HOLOCRON_SERVER_MAX_MODULES_RETRY',
validate: isPositiveIntegerIfDefined,
},
{
name: 'HOLOCRON_SERVER_MAX_SIM_MODULES_FETCH',
validate: isPositiveIntegerIfDefined,
},
// where to send/report client errors
{
name: 'ONE_CLIENT_REPORTING_URL',
defaultValue: () => (process.env.NODE_ENV === 'development'
? '/_/report/errors'
: undefined),
validate: isFetchableUrlInBrowser,
},
// where to send/report csp violations
{
name: 'ONE_CLIENT_CSP_REPORTING_URL',
defaultValue: () => (process.env.NODE_ENV === 'development'
? '/_/report/security/csp-violation'
: undefined),
validate: isFetchableUrlInBrowser,
},
// allow hosting of static assets externally
{
name: 'ONE_CLIENT_CDN_URL',
defaultValue: () => (process.env.NODE_ENV === 'development'
? '/_/static/'
: undefined),
validate: (value) => {
isFetchableUrlInBrowser(value);
if (!/\/$/.test(value)) {
throw new Error('ONE_CDN_URL must have a trailing slash');
}
},
},
// locale folder env level
{
name: 'ONE_CLIENT_LOCALE_FILENAME',
valid: ['integration', 'qa', undefined],
normalize: (input) => input || undefined,
defaultValue: () => (process.env.NODE_ENV === 'development' ? 'integration' : undefined),
},
{
name: 'ONE_CLIENT_ROOT_MODULE_NAME',
validate: (value) => { if (!value) { throw new Error('The `ONE_CLIENT_ROOT_MODULE_NAME` environment variable must be defined.'); } },
defaultValue: () => (process.env.NODE_ENV === 'development' ? argv.rootModuleName : undefined),
},
{
name: 'ONE_CONFIG_USE_NATIVE_INTL',
defaultValue: 'false',
normalize: (input) => {
if (input?.toLowerCase() === 'true') {
return 'true';
}
return 'false';
},
},
{
name: 'ONE_REFERRER_POLICY_OVERRIDE',
defaultValue: () => '',
validate: (value) => {
const approvedPolicies = [
'no-referrer',
'no-referrer-when-downgrade',
'same-origin',
'strict-origin',
];
if (value && !approvedPolicies.includes(value)) {
throw new Error(`"${value}" is not an approved policy. Please use: ${approvedPolicies.join(',')}.`);
}
},
},
{
// feature flag for service worker, required to be enabled for `appConfig.pwa` to take effect
// TODO: Expires on 2020-10-30
name: 'ONE_SERVICE_WORKER',
normalize: (value) => value === 'true',
defaultValue: () => false,
},
// Enable POSTing to module routes
{
name: 'ONE_ENABLE_POST_TO_MODULE_ROUTES',
defaultValue: 'false',
normalize: (input) => {
if (input.toLowerCase() === 'false') {
return 'false';
}
return `${!!input}`;
},
validate: (input) => {
if (input !== 'true' && input !== 'false') {
throw new Error(`Expected "${input}" to be "true" or "false"`);
}
},
},
// Customize max payload for POST requests
{
name: 'ONE_MAX_POST_REQUEST_PAYLOAD',
defaultValue: '15kb',
validate: (input) => {
const parsed = bytes.parse(input);
if (parsed === null) {
throw new Error(`Expected "${input}" to be parseable by bytes utility https://www.npmjs.com/package/bytes`);
}
},
},
// OpenTelemetry Configuration
{
name: 'OTEL_EXPORTER_OTLP_LOGS_ENDPOINT',
validate: (input) => {
if (!input) return;
// eslint-disable-next-line no-new -- intentionally using new for side effect of validation
new URL(input);
},
},
{
name: 'OTEL_EXPORTER_OTLP_TRACES_ENDPOINT',
validate: (input) => {
if (!input) return;
// eslint-disable-next-line no-new -- intentionally using new for side effect of validation
new URL(input);
},
},
];
runTime.forEach(preprocessEnvVar);
export { getIp };
export default runTime;