61 lines
3.3 KiB
JavaScript
61 lines
3.3 KiB
JavaScript
// Inventory compiled Nest controllers without booting the app or accessing a database.
|
|
require('reflect-metadata');
|
|
const { readdirSync, mkdirSync, writeFileSync } = require('node:fs');
|
|
const { join, resolve, relative, dirname } = require('node:path');
|
|
const { RequestMethod } = require('@nestjs/common');
|
|
const { PATH_METADATA, METHOD_METADATA, GUARDS_METADATA } = require('@nestjs/common/constants');
|
|
|
|
function files(directory) {
|
|
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) =>
|
|
entry.isDirectory() ? files(join(directory, entry.name)) : [join(directory, entry.name)]);
|
|
}
|
|
|
|
function catalog() {
|
|
const root = resolve('dist/legacy/src');
|
|
const routes = [];
|
|
for (const file of files(root).filter((name) => name.endsWith('.controller.js'))) {
|
|
for (const controller of Object.values(require(file))) {
|
|
if (typeof controller !== 'function' || !Reflect.hasMetadata(PATH_METADATA, controller)) continue;
|
|
for (const name of Object.getOwnPropertyNames(controller.prototype)) {
|
|
const handler = controller.prototype[name];
|
|
if (typeof handler !== 'function' || !Reflect.hasMetadata(METHOD_METADATA, handler)) continue;
|
|
const guards = [...(Reflect.getMetadata(GUARDS_METADATA, controller) || []),
|
|
...(Reflect.getMetadata(GUARDS_METADATA, handler) || [])].map((guard) => guard.name);
|
|
const roles = Reflect.getMetadata('roles', handler) ?? Reflect.getMetadata('roles', controller) ?? [];
|
|
const auth = guards.some((guard) => /SuperAdmin/i.test(guard)) ? 'superadmin'
|
|
: guards.some((guard) => /Jwt/i.test(guard)) ? (roles.includes('admin') ? 'admin' : 'user') : 'public';
|
|
for (const prefix of [Reflect.getMetadata(PATH_METADATA, controller)].flat()) {
|
|
for (const path of [Reflect.getMetadata(PATH_METADATA, handler)].flat()) {
|
|
routes.push({ method: RequestMethod[Reflect.getMetadata(METHOD_METADATA, handler)],
|
|
path: `/api/v1/${prefix}/${path}`.replace(/\/{2,}/g, '/').replace(/\/$/, ''),
|
|
auth, roles, controller: controller.name, handler: name, source: relative(root, file).replace(/\\/g, '/') });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return routes.sort((a, b) => a.path.localeCompare(b.path) || a.method.localeCompare(b.method));
|
|
}
|
|
|
|
function routeFor(routes, method, url) {
|
|
const path = new URL(url, 'http://127.0.0.1').pathname;
|
|
return routes.filter((route) => route.method === method).sort((a, b) =>
|
|
(a.path.match(/:/g) || []).length - (b.path.match(/:/g) || []).length).find((route) => {
|
|
const pattern = route.path.split('/').map((part) => part.startsWith(':') ? '[^/]+'
|
|
: part.includes('*') ? '.+' : part.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('/');
|
|
return new RegExp(`^${pattern}/?$`).test(path);
|
|
});
|
|
}
|
|
|
|
if (require.main === module) {
|
|
const output = resolve(process.argv[2] || 'docs/performance/route-catalog.json');
|
|
const routes = catalog();
|
|
mkdirSync(dirname(output), { recursive: true });
|
|
writeFileSync(output, JSON.stringify({ capturedAt: new Date().toISOString(),
|
|
scope: 'Gateway controller routes; Socket.IO namespaces and static media are separate protocols.', routes }, null, 2) + '\n');
|
|
console.log(`Inventoried ${routes.length} routes: ${JSON.stringify(routes.reduce((counts, r) => {
|
|
counts[r.method] = (counts[r.method] || 0) + 1; return counts;
|
|
}, {}))}`);
|
|
}
|
|
module.exports = { catalog, routeFor };
|