-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.js
More file actions
496 lines (431 loc) · 18.1 KB
/
Copy pathserve.js
File metadata and controls
496 lines (431 loc) · 18.1 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
const path = require('path');
const fs = require('fs');
const url = require('url');
const express = require('express');
const resolve = require('resolve');
const chalk = require('chalk');
const utils = require('./shared/utils');
const bootstrap = require('./shared/bootstrap');
const getCacheFilename = require('./shared/get-cache-filename');
const discoveryDir = require('./shared/discovery-dir');
const libs = require(path.join(discoveryDir, 'libs'));
const gen = require('./shared/gen');
const ROUTE_DATA = '/data.json';
const ROUTE_RESET_DATA = '/drop-cache';
const ROUTE_SETUP = '/gen/setup.js';
const ROUTE_MODEL_PREPARE = '/gen/model-prepare.js';
const ROUTE_MODEL_VIEW_JS = '/gen/model-view.js';
const ROUTE_MODEL_LIBS_JS = '/gen/model-libs.js';
const ROUTE_MODEL_VIEW_CSS = '/gen/model-view.css';
const ROUTE_MODEL_LIBS_CSS = '/gen/model-libs.css';
const ROUTE_MODEL_BUILD = '/gen/build.zip';
const defaultRoutes = {
[ROUTE_DATA]: (req, res) => res.send({ name: 'Model free mode' }),
[ROUTE_RESET_DATA]: (req, res) => res.send(null),
[ROUTE_SETUP]: generate(ROUTE_SETUP, null, {}, { name: 'Discovery', mode: 'modelfree' }, null),
[ROUTE_MODEL_PREPARE]: generate(ROUTE_MODEL_PREPARE),
[ROUTE_MODEL_VIEW_JS]: generate(ROUTE_MODEL_VIEW_JS),
[ROUTE_MODEL_LIBS_JS]: generate(ROUTE_MODEL_LIBS_JS),
[ROUTE_MODEL_VIEW_CSS]: generate(ROUTE_MODEL_VIEW_CSS),
[ROUTE_MODEL_LIBS_CSS]: generate(ROUTE_MODEL_LIBS_CSS),
[ROUTE_MODEL_BUILD]: (req, res) => res.status(400).send(null)
};
const stubApi = new Proxy({}, {
get: () => function() {
return this;
}
});
const serverLog = (...args) => console.log(chalk.grey(utils.time()), ...args);
const serverError = (...args) => console.error(chalk.grey(utils.time()), ...args);
const routeLog = (route, ...args) => serverLog(chalk.cyan(route), ...args);
const routeError = (route, ...args) => serverError(chalk.cyan(route), ...args);
function ensureTrailingSlash(req, res, next) {
const parsedUrl = url.parse(req.originalUrl);
if (req.path === '/' &&
!parsedUrl.pathname.endsWith('/') &&
['GET', 'HEAD', 'OPTIONS'].includes(req.method)) {
res.redirect(301, parsedUrl.pathname + '/' + (parsedUrl.search || ''));
} else {
next();
}
}
function generate(filename, ...args) {
return (req, res, next) => gen[filename](...args)
.then(content => {
res.type(path.extname(filename));
res.send(content);
})
.catch(next);
}
function generateStream(filename, ...args) {
return (req, res, next) => gen[filename](...args)
.then(({ stream }) => {
res.set('Content-Type', 'application/zip');
return new Promise((resolve, reject) =>
stream
.pipe(res)
.on('finish', resolve)
.on('error', reject)
);
})
.catch(next);
}
function faviconIfSpecified(router, modelConfig, config) {
const favicon = (modelConfig ? modelConfig.favicon : null) || config.favicon;
if (favicon) {
router.get('/favicon' + path.extname(favicon), (req, res) => res.sendFile(favicon));
}
}
function generateDataJson(modelConfig, options) {
const { slug } = modelConfig;
const prefix = `/${slug}/data.json`;
const bgCacheEnabled = Boolean(getCacheFilename(modelConfig) && modelConfig.cacheBgUpdate);
let bgUpdateTimer = null;
let request = null;
function tryCacheBgUpdate() {
if (bgCacheEnabled && !bgUpdateTimer) {
const updateOptions = {
...options,
background: true,
rewriteCache: true
};
routeLog(prefix, `Schedule update cache in background in ${utils.prettyDuration(modelConfig.cacheBgUpdate, true)}`);
bgUpdateTimer = setTimeout(
() => {
const bgUpdateStartTime = Date.now();
routeLog(prefix, 'Start background cache update');
gen['/data.json'](modelConfig, updateOptions)
.catch(error => routeError(prefix, `Cache update in background error: ${error}`))
.then(() => {
bgUpdateTimer = null;
routeLog(prefix, `Background cache update done in ${utils.prettyDuration(Date.now() - bgUpdateStartTime)}`);
tryCacheBgUpdate();
});
},
modelConfig.cacheBgUpdate
);
}
}
function cutPaths(text) {
const home = process.env.HOME;
const rx = new RegExp(home.replace(/\[\]\(\)\{\}\.\+\*\?/g, '\\$1'), 'g');
return home ? text.replace(rx, '~') : text;
}
return function getData(_, res) {
const startTime = Date.now();
if (res === stubApi) {
return {
scheduleBgUpdate: bgCacheEnabled ? tryCacheBgUpdate : null,
warmupDataCache: () => gen['/data.json'](modelConfig, { ...options, background: true })
};
}
if (!request) {
request = gen['/data.json'](modelConfig, options);
request
.catch(error => routeError(prefix, `Collect data error: ${error}`))
.then(() => {
request = null;
tryCacheBgUpdate();
});
}
return request
.then(({ stream, size, brotli }) => {
res.set('Content-Type', 'application/json');
if (brotli) {
res.set('Content-Encoding', 'br');
}
if (size) {
res.set('Content-Length', size);
}
return new Promise((resolve, reject) =>
stream.pipe(res)
.on('finish', resolve)
.on('error', reject)
);
})
.catch(error => {
res.status(500).json({
error: cutPaths(String(error.stack || error)),
data: null
});
routeError(prefix, `[ERROR] ${String(error).split(/\r\n?|\n/)[0]}`);
})
.then(() => {
routeLog(prefix, '[OK] Responsed in', utils.prettyDuration(Date.now() - startTime));
});
};
}
function dropDataCache(modelConfig) {
return (req, res) => {
const { slug } = modelConfig;
const cacheFile = getCacheFilename(modelConfig);
if (cacheFile) {
try {
fs.unlinkSync(cacheFile);
routeLog(`/${slug}/`, 'Drop cache');
} catch (e) {
routeLog(`/${slug}/`, `Drop cache ERROR: ${e}`);
}
}
res.status(200).send('OK');
};
}
function prebuild(options) {
const args = [];
if (options.configFile) {
args.push(options.configFile);
}
if (options.model) {
args.push('--model', options.model);
}
args.push('--output', options.prebuild);
args.push('--no-data');
// args.push('--cleanup');
console.log('='.repeat(40));
console.log(' PREBUILD START: ');
console.log('='.repeat(40));
return utils.runScript(path.join(__dirname, '../bin/build'), args)
.then(() => {
console.log('='.repeat(40));
console.log(' PREBUILD END');
console.log('='.repeat(40));
});
}
function createModelRouter(modelConfig, options, config, addBeforeReadyTask, addScheduleTask, routes = {}) {
const { slug } = modelConfig;
const cacheFilename = getCacheFilename(modelConfig);
const router = express.Router();
utils.sectionStart(chalk.cyan(slug));
if (typeof modelConfig.extendRouter === 'function') {
utils.process('Extend with custom routes', () => {
modelConfig.extendRouter(router, modelConfig, options);
});
}
utils.process('Define default routes', () => {
// set up routes
Object.keys(defaultRoutes).forEach(path =>
router.get(path, routes[path] || defaultRoutes[path])
);
if (options.prebuild) {
router.use(express.static(
config.mode === 'single' ? options.prebuild : path.join(options.prebuild, slug)
));
} else {
// favicon
faviconIfSpecified(router, modelConfig, config);
// main files
router.get('/', generate('/model-index.html', modelConfig, options, config));
router.get('/model.js', (_, res) => res.sendFile(path.join(__dirname, 'static/model.js')));
router.get('/model.css', (_, res) => res.sendFile(path.join(__dirname, 'static/model.css')));
}
});
if (cacheFilename) {
utils.sectionStart('Cache:');
utils.println(`File: ${path.relative(process.cwd(), cacheFilename)}`);
if (modelConfig.cacheBgUpdate) {
utils.println(`Background update every ${utils.prettyDuration(modelConfig.cacheBgUpdate, true)}`);
}
if (options.warmup && ROUTE_DATA in routes) {
utils.process('Warming up', () => {
const { warmupDataCache, scheduleBgUpdate } = routes[ROUTE_DATA](stubApi, stubApi);
addBeforeReadyTask(`Model '${slug}'`, warmupDataCache);
if (scheduleBgUpdate !== null) {
addScheduleTask(scheduleBgUpdate);
}
});
}
utils.sectionEnd();
} else {
utils.println('Cache: NO');
}
if (Array.isArray(modelConfig.plugins) && modelConfig.plugins.length) {
utils.sectionStart('Plugins:');
modelConfig.plugins.forEach(plugin => utils.println(path.relative(process.cwd(), plugin)));
utils.sectionEnd();
} else {
utils.println('Plugins: NO');
}
utils.sectionEnd();
return router;
}
module.exports = bootstrap(function createServer(options, config, configFile) {
const scheduleTasks = [];
const addScheduleTask = fn => scheduleTasks.push(fn);
const beforeReadyTasks = [];
const addBeforeReadyTask = (name, fn) => beforeReadyTasks.push({
name: name || 'Untitled',
status: 'pending',
fn
});
let beforeReadyTasksDone = 0;
let beforeReadyStartTime;
let beforeReadyTimeElapsed;
const app = express();
console.log(configFile
? `Load config from ${chalk.yellow(configFile)}`
: 'No config is used'
);
if (!options.prebuild) {
// use random isolation marker to avoid mixing with styles of other builds, e.g. JsonDiscovery browser plugin
options.isolateStyles = 'discovery-server-isolated-' + (new Date().toISOString().replace(/\D/g, ''));
}
// check up models
if (!config.models || !config.models.length) {
if (options.model) {
// looks like a user mistake
console.error(` Model \`${options.model}\` is not found`);
process.exit(2);
}
// model free mode
utils.println(' Models are not defined (model free mode is enabled)');
utils.silent(() =>
app.use(createModelRouter({ name: 'Discovery' }, options, config, addBeforeReadyTask, addScheduleTask))
);
} else {
config.models = config.models.map(modelConfig => ({
...modelConfig,
download: options.prebuild ? modelConfig.download : false // disable download for not prebuilded mode
}));
const routers = utils.section(
config.mode === 'single' ? 'Init single model' : 'Init models',
() => config.models.map(modelConfig =>
createModelRouter(modelConfig, options, config, addBeforeReadyTask, addScheduleTask,
options.prebuild
? {
[ROUTE_DATA]: generateDataJson(modelConfig, options),
[ROUTE_RESET_DATA]: dropDataCache(modelConfig),
[ROUTE_MODEL_BUILD]: generateStream(ROUTE_MODEL_BUILD, modelConfig, options)
}
: {
[ROUTE_DATA]: generateDataJson(modelConfig, options),
[ROUTE_RESET_DATA]: dropDataCache(modelConfig),
[ROUTE_SETUP]: generate(ROUTE_SETUP, modelConfig, options, config, `.${ROUTE_DATA}`),
[ROUTE_MODEL_PREPARE]: generate(ROUTE_MODEL_PREPARE, modelConfig, options),
[ROUTE_MODEL_VIEW_JS]: generate(ROUTE_MODEL_VIEW_JS, modelConfig, options),
[ROUTE_MODEL_LIBS_JS]: generate(ROUTE_MODEL_LIBS_JS, modelConfig, options),
[ROUTE_MODEL_VIEW_CSS]: generate(ROUTE_MODEL_VIEW_CSS, modelConfig, options),
[ROUTE_MODEL_LIBS_CSS]: generate(ROUTE_MODEL_LIBS_CSS, modelConfig, options)
}
)
)
);
if (typeof config.extendRouter === 'function') {
utils.process('Extend with custom routes', () => {
config.extendRouter(app, config, options);
});
}
if (config.mode === 'single') {
app.use(routers[0]);
// add the same routing to slug to compliment multi model mode urls
app.use('/' + config.models[0].slug, ensureTrailingSlash, routers[0]);
} else {
faviconIfSpecified(app, null, config);
app.get('/', generate('/index.html', null, options, config));
app.get('/gen/index-view.js', generate('/gen/index-view.js', options, config, null));
app.get('/gen/index-libs.js', generate('/gen/index-libs.js', options, config, null));
app.get('/gen/index-view.css', generate('/gen/index-view.css', options, config, null));
app.get('/gen/index-libs.css', generate('/gen/index-libs.css', options, config, null));
config.models.forEach((model, idx) =>
app.use('/' + model.slug, ensureTrailingSlash, routers[idx])
);
}
}
// common static files
utils.process('Init common routes', () => {
if (options.prebuild) {
app.use(express.static(options.prebuild));
return;
}
app.use(express.static(path.join(__dirname, 'static')));
app.use('/dist', express.static(path.join(discoveryDir, 'dist')));
app.get('/gen/setup.js', generate('/gen/setup.js', null, options, config, null));
app.use('/@discoveryjs/discovery', express.static(path.join(discoveryDir, 'src')));
for (let [name, lib] of Object.entries(libs)) {
app.get(`/gen/${lib.filename}`, function(req, res) {
res.type('.js');
res.send(lib.source);
});
app.use(
'/node_modules/' + name,
express.static(
lib.path ||
// for backward compability (prior discovery beta.39)
path.dirname(resolve.sync(name + '/package.json', { basedir: discoveryDir }))
)
);
}
});
// special routes
app.get('/healthz', (_, res) => {
res.status(200);
res.send({ status: 'OK' });
});
app.get('/readyz', (_, res) => {
const warmupStatus = {};
if (beforeReadyTasks.length > 0) {
warmupStatus.warmup = {
tasksTotal: beforeReadyTasks.length,
tasksDone: beforeReadyTasksDone,
tasks: beforeReadyTasks.map(task => ({
...task,
startTime: task.startTime && typeof task.startTime === 'number'
? new Date(task.startTime).toISOString()
: task.startTime
})),
time: beforeReadyTimeElapsed || Date.now() - beforeReadyStartTime
};
}
if (beforeReadyTasksDone < beforeReadyTasks.length) {
res.status(500);
res.send({
status: `Await ready for ${beforeReadyTasks.length - beforeReadyTasksDone} of ${beforeReadyTasks.length} tasks`,
...warmupStatus
});
} else {
res.status(200);
res.send({
status: 'Ready',
...warmupStatus
});
}
});
if (options.prebuild) {
addBeforeReadyTask('Prebuild static', () => prebuild(options, config, configFile));
}
if (scheduleTasks.length) {
addBeforeReadyTask('Schedule data cache background update', () => scheduleTasks.forEach(fn => fn()));
}
// start server
app.listen(options.port, function() {
console.log();
console.log(`Server listen on ${chalk.green(`http://localhost:${this.address().port}`)}`);
console.log();
// Await warmup tasks if any
// Don't use Promise.all() since we need to count tasks left
if (beforeReadyTasks.length > 0) {
serverLog(`Await ${beforeReadyTasks.length} tasks before ready (warmup)`);
beforeReadyStartTime = Date.now();
beforeReadyTasks.reduce(
(pipeline, task) => pipeline.then(() => serverLog('==== Task:', chalk.yellow(task.name)) || Object.assign(task, {
status: 'processing',
startTime: Date.now()
}).fn())
.catch(error => {
console.error(`Warmup task "${task.name}" error:`, task.error = error);
if (options.bail) {
console.log('Exit due to --bail option');
process.exit(2);
}
})
.finally(() => (beforeReadyTasksDone++, Object.assign(task, {
status: task.error ? 'failed' : 'ok',
duration: Date.now() - task.startTime
}))),
Promise.resolve()
).finally(() => {
beforeReadyTimeElapsed = Date.now() - beforeReadyStartTime;
serverLog('Warmup is DONE in', utils.prettyDuration(beforeReadyTimeElapsed));
});
}
});
});