forked from webrecorder/behaviors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf.js
231 lines (214 loc) · 6.08 KB
/
conf.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
'use strict';
const program = require('commander');
const qs = require('qs');
const chalk = require('chalk').default;
const pkg = require('../package');
const internalPaths = require('../internal/paths');
const Utils = require('../internal/utils');
/**
* The default api server port
* @type {number}
*/
const DefaultPort = 3030;
/**
* The default api server host
* @type {string}
*/
const DefaultHost = '127.0.0.1';
/**
* Prints a warning for invalid environment config values
* @param {string} key
* @param {string} value
* @param {*} defaultValue
*/
function invalidValue(key, value, defaultValue) {
console.log(chalk.bold.red(`Invalid value for ${key}: ${value}`));
console.log(chalk.bold.red(`Using default value: ${defaultValue}`));
}
function convertEnvInt(key, defaultValue) {
const envValue = process.env[key];
let value = defaultValue;
if (envValue != null) {
try {
value = parseInt(envValue, 10);
} catch (e) {
invalidValue(key, envValue, defaultValue);
}
if (isNaN(value)) {
invalidValue(key, envValue, defaultValue);
value = defaultValue;
}
}
return value;
}
/**
* Returns the default port the api server will listen on.
* If the env variable BEHAVIOR_API_PORT is set returns it's value
* otherwise returns 3030
* @return {number}
*/
const getDefaultPort = () => convertEnvInt('BEHAVIOR_API_PORT', DefaultPort);
/**
* Returns the default port the api server will listen on.
* If the env variable WR_BEHAVIOR_HOST is set returns it's value
* otherwise returns 127.0.0.1
* @return {string}
*/
function getDefaultHost() {
if (process.env.BEHAVIOR_API_HOST != null) {
return process.env.BEHAVIOR_API_HOST;
}
return DefaultHost;
}
/**
* Returns the default path to the behaviors.
* If the env key `WR_BEHAVIOR_DIR` is defined that value is used
* otherwise `internalPaths.distDir`
* @return {string}
*/
function getDefaultBehaviorDir() {
if (process.env.WR_BEHAVIOR_DIR != null) {
return process.env.WR_BEHAVIOR_DIR;
}
return internalPaths.distDir;
}
/**
* Returns the default path to the behavior metadata file.
* If the env key `WR_BEHAVIOR_METADATA_PATH` is defined that value is used
* otherwise `internalPaths.defaultBehaviorMetadataPath`
* @return {string}
*/
function getDefaultMetadataPath() {
if (process.env.WR_BEHAVIOR_METADATA_PATH != null) {
return process.env.WR_BEHAVIOR_METADATA_PATH;
}
return internalPaths.defaultBehaviorMetadataPath;
}
/**
* Returns the default value for the build behaviors flag.
* If the env key `BUILD_BEHAVIORS` is defined that value is used
* otherwise false.
* @return {boolean}
*/
function getDefaultBuildBehaviorsFlag() {
if (process.env.BUILD_BEHAVIORS) {
return Utils.envFlagToBool(process.env.BUILD_BEHAVIORS);
}
return false;
}
/**
* Returns the default value for the number of behavior lookup workers.
* If the env key `NUM_WORKERS`
* @return {number}
*/
function defaultNumWorkers() {
const numWorkers = convertEnvInt('NUM_WORKERS', 2);
if (numWorkers >= 1) return ensureNumWorkers(numWorkers);
console.log(
chalk.bold.red(
`The number of behavior lookup workers cannot be less than 1, got "${numWorkers}". Using default value of 2`
)
);
return 2;
}
function ensureNumWorkers(value) {
let numWorkers = value;
if (typeof value === 'string') {
try {
numWorkers = parseInt(value, 10);
} catch (e) {
invalidValue('workers', value, 2);
numWorkers = 2;
}
}
if (isNaN(numWorkers)) {
invalidValue('workers', numWorkers, 2);
numWorkers = 2;
} else if (numWorkers < 1) {
console.log(
chalk.bold.red(
`The number of behavior lookup workers must be 1 <= workers <= 4, got "${numWorkers}". Using default value of 2`
)
);
numWorkers = 2;
} else if (numWorkers > 4) {
console.log(
chalk.bold.red(
`The number of behavior lookup workers must be 1 <= workers <= 4, got "${numWorkers}". Using default value of 2`
)
);
numWorkers = 2;
}
return numWorkers;
}
program
.version(pkg.version)
.usage('[options]')
.option(
'-p, --port [port]',
'The port the api server is to bind to',
getDefaultPort()
)
.option(
'-h, --host [host]',
'The host address the server is listen on',
getDefaultHost()
)
.option(
'-b, --behaviorDir [behaviorDir]',
'The path to the directory containing the build behaviors',
getDefaultBehaviorDir()
)
.option(
'-m, --behaviorMetadata [medataPath]',
'The path to the behavior metadata',
getDefaultMetadataPath()
)
.option(
'--build-behaviors',
'Should the api server build the behaviors for starting up',
getDefaultBuildBehaviorsFlag()
)
.option(
'-w, --workers [numWorkers]',
'How many behavior lookup workers should be spawned',
ensureNumWorkers,
defaultNumWorkers()
)
.parse(process.argv);
const enableLogging = Utils.envFlagToBool(process.env.BEHAVIOR_API_LOGGING);
const qsOpts = {
charset: 'iso-8859-1',
interpretNumericEntities: true,
charsetSentinel: true,
};
// {fastifyOpts: {trustProxy: boolean, maxParamLength: number, logger: boolean, querystringParser: (function(...args: *): *)}, port: number, host: string, numLookupWorkers: number, behaviorInfo: {build: boolean, mdataPath: string, behaviorDir: string}}
/**
* @typedef {Object} APIConfig
* @property {string} host
* @property {number} port
* @property {number} numLookupWorkers
* @property {{trustProxy: boolean, maxParamLength: number, logger: boolean, querystringParser: *}} fastifyOpts
* @property {{build: boolean, mdataPath: string, behaviorDir: string}} behaviorInfo
*/
/**
* The behavior API server config object
* @type {APIConfig}
*/
const config = {
host: program.host,
port: program.port,
numLookupWorkers: program.workers,
behaviorInfo: {
behaviorDir: program.behaviorDir,
mdataPath: program.behaviorMetadata,
build: program.buildBehaviors,
},
fastifyOpts: {
trustProxy: true,
maxParamLength: 5e12,
logger: enableLogging || process.env.LOG != null,
querystringParser: url => qs.parse(url, qsOpts),
},
};
module.exports = config;