-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.ts
More file actions
175 lines (161 loc) · 5.23 KB
/
Copy pathstats.ts
File metadata and controls
175 lines (161 loc) · 5.23 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
import { log } from './log';
export type Stats = {
counters: Record<string, number>;
events: Record<string, any>;
};
export const getStats = (port) => {
const stats: Stats = {
counters: {},
events: {},
};
const addToList = (category, field, data) => {
if (!stats[category]) {
stats[category] = {};
}
if (!stats[category][field]) {
stats[category][field] = [];
}
stats[category][field].push(data);
};
const logDefaultMiddlewares = (
statsInstance: ReturnType<typeof getStats>
) => {
const logMessages = [];
if (statsInstance.getCounter('cors')) {
logMessages.push('cors');
}
if (statsInstance.getCounter('jsonBodyParser')) {
logMessages.push('bodyParser.json');
}
if (statsInstance.getCounter('cookieParser')) {
logMessages.push('cookie-parser');
}
if (statsInstance.getCounter('helmet')) {
logMessages.push('helmet');
}
if (logMessages.length) {
log.stats(` Used built-in middlewares: ${logMessages.join(', ')}`);
}
const notFoundMessages = [];
if (statsInstance.getCounter('cors-not-found')) {
notFoundMessages.push('cors');
}
if (statsInstance.getCounter('jsonBodyParser-not-found')) {
notFoundMessages.push('bodyParser.json');
}
if (statsInstance.getCounter('cookieParser-not-found')) {
notFoundMessages.push('cookie-parser');
}
if (statsInstance.getCounter('helmet-not-found')) {
notFoundMessages.push('helmet');
}
if (notFoundMessages.length) {
log.stats(
` Corresponding libraries for built-in middlewares were not installed: ${notFoundMessages.join(', ')}. To enable them, install the corresponding npm packages.`
);
}
};
const statsInstance = {
set: (field, number = 1) => (stats.counters[field] = number),
add: (field, number = 1) =>
(stats.counters[field] = stats.counters[field]
? stats.counters[field] + number
: number),
getCounter: (field) => stats.counters[field],
registerEvent: (eventName, data) => {
switch (eventName) {
case 'registeringRoute':
statsInstance.add('routes');
statsInstance.add('routeHandlers', data.numberOfHandlers);
addToList('events', eventName, { timestamp: Date.now(), ...data });
break;
default:
addToList('events', eventName, { timestamp: Date.now(), ...data });
}
},
getEvents: (eventName) => stats.events[eventName],
logStartup: () => {
if (port) {
log.stats(`-->Stats for simpleExpress app on ${port} port:<--`);
} else {
log.stats(`-->Stats for simpleExpress app (no port):<--`);
}
logDefaultMiddlewares(statsInstance);
if (statsInstance.getCounter('expressMiddleware')) {
log.stats(
` Registered ${statsInstance.getCounter(
'expressMiddleware'
)} expressMiddleware${
statsInstance.getCounter('expressMiddleware') > 1 ? 's' : ''
}`
);
}
if (statsInstance.getCounter('middleware')) {
log.stats(
` Registered ${statsInstance.getCounter('middleware')} middleware${
statsInstance.getCounter('middleware') > 1 ? 's' : ''
}`
);
}
if (statsInstance.getCounter('errorHandlers')) {
log.stats(
` Registered ${statsInstance.getCounter(
'errorHandlers'
)} errorHandlers${
statsInstance.getCounter('errorHandlers') > 1 ? 's' : ''
}`
);
}
if (!statsInstance.getCounter('routes')) {
return log.stats(` No routes registered`);
}
log.stats(
` Registered ${statsInstance.getCounter(
'routes'
)} routes with ${statsInstance.getCounter('routeHandlers')} handlers:`
);
const mappedRoutes = new Set<string>();
const mappedMethods: Record<string, any[]> = {};
stats.events.registeringRoute.forEach((routeEvent) => {
const { path, method, numberOfHandlers, names } = routeEvent;
if (!mappedMethods[path]) {
mappedMethods[path] = [];
}
mappedMethods[path].push({ method, numberOfHandlers, names });
mappedRoutes.add(path);
});
mappedRoutes.forEach((path) => {
const invalidRoute = path.indexOf('/') !== 0 && path !== '*';
log.stats(
` ${path}${
invalidRoute ? ' - WARNING: Route not starting with "/"!' : ''
}`
);
mappedMethods[path].forEach(
({ method, numberOfHandlers, names = [] }) => {
let foundNames = false;
names.forEach((name) => {
if (name !== 'anonymous') {
foundNames = true;
}
});
log.stats(
` ${method}${
numberOfHandlers > 1 || foundNames
? `, ${numberOfHandlers} handler${
numberOfHandlers === 1 ? '' : 's'
}`
: ''
}${
names && names.length && foundNames
? `: ${names.join(', ')}`
: ''
}`
);
}
);
});
},
};
return statsInstance;
};