-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
64 lines (56 loc) · 1.66 KB
/
app.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
class AppBoot {
constructor(app) {
this.app = app;
}
didReady() {
const app = this.app;
const {
options: { serverScope },
config: { basicAuthHooks, ignoreRouter, customStorage },
router,
} = app;
// custom console
if (serverScope === 'console') {
// auth hooks
if (basicAuthHooks) {
let index = 0;
const middlewares = app.middleware;
for (const length = middlewares.length; index < length; index++) {
const { _name } = middlewares[index];
if (_name && _name.includes('basicAuth')) {
break;
}
}
if (basicAuthHooks.before) {
middlewares.splice(index, 0, basicAuthHooks.before);
}
if (basicAuthHooks.after) {
middlewares.splice(index + 1, 0, basicAuthHooks.after);
}
}
// ignore routes
if (ignoreRouter.length) {
const layers = router.stack;
for (const layer of layers) {
if (ignoreRouter.every(router => (typeof router === 'string'
? router !== layer.path
: router.path !== layer.path || !layer.methods.includes(router.method.toUpperCase())))) {
continue;
}
this.app.logger.info('[devtoolx-console] ignore router: %s, methods: %s', layer.path, layer.methods.join(','));
layer.stack.pop();
}
}
// app extension
if (customStorage) {
const originStorage = app.storage;
Object.defineProperty(app, 'storage', {
get() {
return customStorage.call(app, originStorage);
},
});
}
}
}
}
module.exports = AppBoot;