Skip to content

Commit 5ca412a

Browse files
committed
Remove express types from deps and add better docs
1 parent e58a8de commit 5ca412a

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

packages/integrations/src/express.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@ export class Express implements Integration {
4444
}
4545

4646
/**
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) { ... })
4857
*/
4958
function wrap(fn: Function, getCurrentHub: () => Hub): RequestHandler | ErrorRequestHandler {
5059
const arrity = fn.length;
@@ -91,7 +100,14 @@ function wrap(fn: Function, getCurrentHub: () => Hub): RequestHandler | ErrorReq
91100
}
92101

93102
/**
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>[])
95111
*/
96112
function wrapUseArgs(args: IArguments, getCurrentHub: () => Hub): unknown[] {
97113
return Array.from(args).map((arg: unknown) => {
@@ -113,9 +129,9 @@ function wrapUseArgs(args: IArguments, getCurrentHub: () => Hub): unknown[] {
113129
}
114130

115131
/**
116-
* JSDoc
132+
* Patches original app.use to utilize our tracing functionality
117133
*/
118-
function instrumentMiddlewares(app: Application, getCurrentHub: () => Hub): any {
134+
function instrumentMiddlewares(app: Application, getCurrentHub: () => Hub): Application {
119135
const originalAppUse = app.use;
120136
app.use = function(): any {
121137
return originalAppUse.apply(this, wrapUseArgs(arguments, getCurrentHub));

0 commit comments

Comments
 (0)