@@ -44,7 +44,16 @@ export class Express implements Integration {
44
44
}
45
45
46
46
/**
47
- * JSDoc
47
+ * Wraps original middleware function in a tracing call, which stores the info about the call as a span,
48
+ * and finishes it once the middleware is done invoking.
49
+ *
50
+ * Express middlewares have 3 various forms, thus we have to take care of all of them:
51
+ * // sync
52
+ * app.use(function (req, res) { ... })
53
+ * // async
54
+ * app.use(function (req, res, next) { ... })
55
+ * // error handler
56
+ * app.use(function (err, req, res, next) { ... })
48
57
*/
49
58
function wrap ( fn : Function , getCurrentHub : ( ) => Hub ) : RequestHandler | ErrorRequestHandler {
50
59
const arrity = fn . length ;
@@ -91,7 +100,14 @@ function wrap(fn: Function, getCurrentHub: () => Hub): RequestHandler | ErrorReq
91
100
}
92
101
93
102
/**
94
- * JSDoc
103
+ * Takes all the function arguments passed to the original `app.use` call
104
+ * and wraps every function, as well as array of functions with a call to our `wrap` method.
105
+ * We have to take care of the arrays as well as iterate over all of the arguments,
106
+ * as `app.use` can accept middlewares in few various forms.
107
+ *
108
+ * app.use([<path>], <fn>)
109
+ * app.use([<path>], <fn>, ...<fn>)
110
+ * app.use([<path>], ...<fn>[])
95
111
*/
96
112
function wrapUseArgs ( args : IArguments , getCurrentHub : ( ) => Hub ) : unknown [ ] {
97
113
return Array . from ( args ) . map ( ( arg : unknown ) => {
@@ -113,9 +129,9 @@ function wrapUseArgs(args: IArguments, getCurrentHub: () => Hub): unknown[] {
113
129
}
114
130
115
131
/**
116
- * JSDoc
132
+ * Patches original app.use to utilize our tracing functionality
117
133
*/
118
- function instrumentMiddlewares ( app : Application , getCurrentHub : ( ) => Hub ) : any {
134
+ function instrumentMiddlewares ( app : Application , getCurrentHub : ( ) => Hub ) : Application {
119
135
const originalAppUse = app . use ;
120
136
app . use = function ( ) : any {
121
137
return originalAppUse . apply ( this , wrapUseArgs ( arguments , getCurrentHub ) ) ;
0 commit comments