-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (94 loc) · 3.03 KB
/
index.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
/**
* express-route-audit - Audit declared Express routes with usage data.
* Copyright (C) 2019 Jon Peck
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const listEndpoints = require('express-list-endpoints');
module.exports = class ERA {
constructor(value) {
// todo: validate;
this.storage = value;
}
/**
* Given a route and method, generate a key used for lookups.
*
* @param {string} route - Express route
* @param {string} method - HTTP method
* @return {string} normalized key
*/
static generateKey(route, method) {
if (!route || typeof route !== 'string') {
throw new TypeError('generateKey: invalid route!');
}
if (!method || typeof method !== 'string') {
throw new TypeError('generateKey: invalid method!');
}
return `${route.toLowerCase()}|${method.toLowerCase()}`;
}
/**
* Middleware that records the route paths upon a finished request.
*
* @param {Object} request - Express request object
* @param {Object} response - Express response object
* @param {Function} next - Express next middleware function
* @return {*} next
*/
middleware(request, response, next) {
response.on('finish', () => {
if (request.route && request.route.path) {
const key = ERA.generateKey(request.route.path, request.method);
this.storage.get(key)
.then(value => this.storage.set(key, value + 1))
.catch(error => next(error));
}
});
return next();
}
/**
* Parse an Express app and combine with path/method counts to produce a consumable report.
*
* @param {Object} app - express app
* @return {Promise<Array>} objects containing count, method and path
*/
report(app) {
const routes = [];
const promises = [];
listEndpoints(app).forEach((endpoint) => {
endpoint.methods.forEach((method) => {
const { path } = endpoint;
const key = ERA.generateKey(path, method);
routes.push({
path,
method,
});
promises.push(this.storage.get(key));
});
});
return Promise.all(promises)
.then((counts) => {
routes.forEach((value, index) => {
routes[index].count = counts[index];
});
// Highest counts first.
routes.sort((a, b) => {
if (a.count > b.count) {
return -1;
}
return (a.count < b.count) ? 1 : 0;
});
return routes;
});
}
};