6
6
'use strict'
7
7
8
8
const { MiddlewareSpec, MiddlewareMounterSpec, RenderSpec } = require ( '../../lib/shim/specs' )
9
- const { MIDDLEWARE_TYPE_NAMES } = require ( '../../lib/shim/webframework-shim/common' )
10
9
11
10
/**
12
11
* Express middleware generates traces where middleware are considered siblings
@@ -25,35 +24,30 @@ module.exports = function initialize(agent, express, moduleName, shim) {
25
24
return err !== 'route' && err !== 'router'
26
25
} )
27
26
28
- if ( express . Router . use ) {
29
- wrapExpress4 ( shim , express )
30
- } else {
31
- wrapExpress3 ( shim , express )
32
- }
33
- }
34
-
35
- function wrapExpress4 ( shim , express ) {
36
- // Wrap `use` and `route` which are hung off `Router` directly, not on a
37
- // prototype.
38
27
shim . wrapMiddlewareMounter (
39
- express . Router ,
28
+ express . application ,
40
29
'use' ,
41
30
new MiddlewareMounterSpec ( {
42
31
route : shim . FIRST ,
43
32
wrapper : wrapMiddleware
44
33
} )
45
34
)
46
35
36
+ wrapExpressRouter ( shim , express . Router . use ? express . Router : express . Router . prototype )
37
+ wrapResponse ( shim , express . response )
38
+ }
39
+
40
+ function wrapExpressRouter ( shim , router ) {
47
41
shim . wrapMiddlewareMounter (
48
- express . application ,
42
+ router ,
49
43
'use' ,
50
44
new MiddlewareMounterSpec ( {
51
45
route : shim . FIRST ,
52
46
wrapper : wrapMiddleware
53
47
} )
54
48
)
55
49
56
- shim . wrap ( express . Router , 'route' , function wrapRoute ( shim , fn ) {
50
+ shim . wrap ( router , 'route' , function wrapRoute ( shim , fn ) {
57
51
if ( ! shim . isFunction ( fn ) ) {
58
52
return fn
59
53
}
@@ -89,7 +83,7 @@ function wrapExpress4(shim, express) {
89
83
} )
90
84
91
85
shim . wrapMiddlewareMounter (
92
- express . Router ,
86
+ router ,
93
87
'param' ,
94
88
new MiddlewareMounterSpec ( {
95
89
route : shim . FIRST ,
@@ -105,56 +99,6 @@ function wrapExpress4(shim, express) {
105
99
}
106
100
} )
107
101
)
108
-
109
- wrapResponse ( shim , express . response )
110
- }
111
-
112
- function wrapExpress3 ( shim , express ) {
113
- // In Express 3 the app returned from `express()` is actually a `connect` app
114
- // which we have no access to before creation. We can not easily wrap the app
115
- // because there are a lot of methods dangling on it that act on the app itself.
116
- // Really we just care about apps being used as `request` event listeners on
117
- // `http.Server` instances so we'll wrap that instead.
118
-
119
- shim . wrapMiddlewareMounter (
120
- express . Router . prototype ,
121
- 'param' ,
122
- new MiddlewareMounterSpec ( {
123
- route : shim . FIRST ,
124
- wrapper : function wrapParamware ( shim , middleware , fnName , route ) {
125
- return shim . recordParamware (
126
- middleware ,
127
- new MiddlewareSpec ( {
128
- name : route ,
129
- req : shim . FIRST ,
130
- next : shim . THIRD ,
131
- type : MIDDLEWARE_TYPE_NAMES . PARAMWARE
132
- } )
133
- )
134
- }
135
- } )
136
- )
137
- shim . wrapMiddlewareMounter (
138
- express . Router . prototype ,
139
- 'use' ,
140
- new MiddlewareMounterSpec ( {
141
- route : shim . FIRST ,
142
- wrapper : wrapMiddleware
143
- } )
144
- )
145
- shim . wrapMiddlewareMounter (
146
- express . application ,
147
- 'use' ,
148
- new MiddlewareMounterSpec ( {
149
- route : shim . FIRST ,
150
- wrapper : wrapMiddleware
151
- } )
152
- )
153
-
154
- // NOTE: Do not wrap application route methods in Express 3, they all just
155
- // forward their arguments to the router.
156
- wrapRouteMethods ( shim , express . Router . prototype , shim . FIRST )
157
- wrapResponse ( shim , express . response )
158
102
}
159
103
160
104
function wrapRouteMethods ( shim , route , path ) {
0 commit comments