This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
decorator.js
139 lines (110 loc) · 4.42 KB
/
decorator.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
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
'use strict';
const trim = require('lodash.trim');
const rtrim = require('lodash.trimend');
const tools = require("@knuckleswtf/scribe/dist/tools");
module.exports = decorator;
function decorator() {
if (!process.env.SCRIBE_GENERATE) {
return;
}
decorateExpress();
}
decorator.decorated = false;
decorator.subRouters = new Map();
const httpMethods = ['get', 'post', 'put', 'patch', 'head', 'delete'];
function decorateExpress() {
const hook = require('require-in-the-middle');
const shimmer = require('shimmer');
hook(['express'], function (exports, name, basedir) {
if (decorator.decorated) {
return exports;
}
httpMethods.forEach(function shimHttpMethod(httpMethod) {
shimmer.wrap(exports.Route.prototype, httpMethod, original => {
return patchHttpVerbMethod(original, httpMethod);
});
});
// Handle sub-routers and sub-apps (ie app.use('/path', routerOrApp);
shimmer.wrap(exports.application, 'use', patchAppUseMethod);
shimmer.wrap(exports.Router, 'use', patchRouterUseMethod);
// Handle app.route(path).get()
shimmer.wrap(exports.Router, 'route', original => {
return function (...args) {
const routeObject = original.apply(this, args);
routeObject.___router = this;
return routeObject;
}
});
decorator.decorated = true;
return exports;
});
}
function addRouteToExpressRouter(router, details) {
let routesOnThisRouter = decorator.subRouters.get(router);
if (!Array.isArray(routesOnThisRouter)) {
decorator.subRouters.set(router, []);
routesOnThisRouter = [];
}
routesOnThisRouter.push(details);
decorator.subRouters.set(router, routesOnThisRouter);
}
function patchAppUseMethod(originalMethod) {
return function use(...args) {
const returnVal = originalMethod.apply(this, args);
const routerOrApp = args[args.length - 1];
if (args.length < 2 || typeof args[0] != 'string'
|| typeof routerOrApp !== 'function' || !('handle' in routerOrApp)) {
return returnVal;
}
const isApp = '_router' in routerOrApp;
const routes = decorator.subRouters.get(isApp ? routerOrApp._router : routerOrApp);
routes.forEach((r) => {
r.uri = rtrim(args[0], '/') + '/' + trim(r.uri, '/');
addRouteToExpressRouter(this._router, r);
});
decorator.subRouters.delete(isApp ? routerOrApp._router : routerOrApp);
return returnVal;
};
}
function patchRouterUseMethod(originalMethod) {
return function use(...args) {
const returnVal = originalMethod.apply(this, args);
let frameAtCallSite = tools.getFrameAtCallSite();
const insideAppDotUse = frameAtCallSite.includes("Array.forEach");
if (insideAppDotUse) {
// app.use() calls router.use(),
// but not with the original args, so we can't access them
// Instead, the instrumentation is handled from our patched app.use()
return returnVal;
}
const routerOrApp = args[args.length - 1];
if (args.length < 2 || typeof args[0] != 'string'
|| typeof routerOrApp !== 'function' || !('handle' in routerOrApp)) {
return returnVal;
}
const isApp = '_router' in routerOrApp;
const routes = decorator.subRouters.get(isApp ? routerOrApp._router : routerOrApp);
routes.forEach((r) => {
r.uri = rtrim(args[0], '/') + '/' + trim(r.uri, '/');
addRouteToExpressRouter(this, r);
});
decorator.subRouters.delete(isApp ? routerOrApp._router : routerOrApp);
return returnVal;
};
}
function patchHttpVerbMethod(originalMethod, method) {
return function (...args) {
const returnVal = originalMethod.apply(this, args);
const router = this.___router; // Set by the patched ".route" method
let frameAtCallSite = tools.getFrameAtCallSite();
const {filePath, lineNumber} = tools.getFilePathAndLineNumberFromCallStackFrame(frameAtCallSite);
const route = {
methods: [method.toUpperCase()],
uri: this.path,
declaredAt: [filePath, lineNumber],
handler: args[args.length - 1]
};
addRouteToExpressRouter(router, route);
return returnVal;
};
}