@contrast/route-coverage 1.11.0 → 1.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -73,6 +73,7 @@ module.exports = function init(core) {
73
73
  },
74
74
  };
75
75
 
76
+ require('./install/http')(core);
76
77
  require('./install/express')(core);
77
78
  require('./install/fastify')(core);
78
79
  require('./install/koa')(core);
@@ -117,82 +117,64 @@ module.exports = function init(core) {
117
117
  }
118
118
  }
119
119
 
120
- core.routeCoverage.express = {
120
+ return core.routeCoverage.express = {
121
121
  install() {
122
- depHooks.resolve(
123
- { name: 'express' },
124
- (express) => {
122
+ depHooks.resolve({ name: 'express' }, (express) => {
123
+ patcher.patch(express.Router, 'use', {
124
+ name: 'express.Router.use',
125
+ patchType,
126
+ post({ args, result }) {
127
+ const [prefix, router] = args;
128
+ if (typeof prefix === 'string' && prefix !== '/') {
129
+ updateRoutes(prefix, router, result);
130
+ }
131
+ }
132
+ });
125
133
 
126
- patcher.patch(express.Router, 'use', {
127
- name: 'express.Router.use',
128
- patchType,
129
- post({ args, result }) {
130
- const [prefix, router] = args;
131
- if (typeof prefix === 'string' && prefix !== '/') {
132
- updateRoutes(prefix, router, result);
133
- }
134
+ patcher.patch(express.application, 'use', {
135
+ name: 'express.application.use',
136
+ patchType,
137
+ post({ args }) {
138
+ const idx = args.length;
139
+ const router = args[idx - 1];
140
+ const routes = routers.get(router);
141
+ if (routes) {
142
+ routes.forEach((route) => {
143
+ discoverRoute(route);
144
+ });
134
145
  }
135
- });
146
+ }
147
+ });
136
148
 
137
- patcher.patch(express.application, 'use', {
138
- name: 'express.application.use',
149
+ METHODS.forEach((method) => {
150
+ patcher.patch(express.application, method, {
151
+ name: `express.application.${method}`,
139
152
  patchType,
140
- post({ args }) {
141
- const idx = args.length;
142
- const router = args[idx - 1];
143
- const routes = routers.get(router);
144
- if (routes) {
145
- routes.forEach((route) => {
146
- discoverRoute(route);
147
- });
148
- }
153
+ post({ args, result }) {
154
+ const [url, fn] = args;
155
+ if (!url || !fn) return;
156
+ const route = createRoute(formatUrl(url), method);
157
+ instrumentRoute(result?._router, route);
158
+ discoverRoute(route);
149
159
  }
150
160
  });
151
161
 
152
- METHODS.forEach((method) => {
153
- patcher.patch(express.application, method, {
154
- name: `express.application.${method}`,
155
- patchType,
156
- post({ args, result }) {
157
- const [url, fn] = args;
158
- if (!url || !fn) return;
159
- const route = createRoute(formatUrl(url), method);
160
- instrumentRoute(result?._router, route);
161
- discoverRoute(route);
162
- }
163
- });
164
-
165
- patcher.patch(express.Router, method, {
166
- name: `express.Router.${method}`,
167
- patchType,
168
- post({ args, obj: router }) {
169
- const [url, fn] = args;
170
- if (!url || !fn) return;
171
- const route = createRoute(formatUrl(url), method);
172
- const routes = routers.get(router) || [];
173
-
174
- instrumentRoute(router, route);
175
- routes.push(route);
176
- routers.set(router, routes);
177
- }
178
- });
179
- });
180
- }
181
- );
182
-
183
- ['http', 'https', 'spdy'].forEach((name) => {
184
- depHooks.resolve({ name }, (module) => {
185
- patcher.patch(module.Server.prototype, 'listen', {
186
- name: `${name}.Server.prototype.listen`,
162
+ patcher.patch(express.Router, method, {
163
+ name: `express.Router.${method}`,
187
164
  patchType,
188
- post() {
189
- routeCoverage.discoveryFinished();
165
+ post({ args, obj: router }) {
166
+ const [url, fn] = args;
167
+ if (!url || !fn) return;
168
+ const route = createRoute(formatUrl(url), method);
169
+ const routes = routers.get(router) || [];
170
+
171
+ instrumentRoute(router, route);
172
+ routes.push(route);
173
+ routers.set(router, routes);
190
174
  }
191
175
  });
192
176
  });
193
177
  });
194
178
  }
195
179
  };
196
-
197
- return core.routeCoverage.express;
198
180
  };
@@ -82,7 +82,7 @@ module.exports = function init(core) {
82
82
  routeCoverage.observe({ method, url });
83
83
  }
84
84
 
85
- core.routeCoverage.fastify = {
85
+ return core.routeCoverage.fastify = {
86
86
  install() {
87
87
  depHooks.resolve(
88
88
  { name: 'fastify', version: '>=3.0.0' },
@@ -105,6 +105,4 @@ module.exports = function init(core) {
105
105
  );
106
106
  }
107
107
  };
108
-
109
- return core.routeCoverage.fastify;
110
108
  };
@@ -0,0 +1,44 @@
1
+ /*
2
+ * Copyright: 2023 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+ 'use strict';
16
+
17
+ module.exports = function init(core) {
18
+ const { depHooks, patcher, routeCoverage } = core;
19
+
20
+ let handled;
21
+
22
+ return core.routeCoverage.http = {
23
+ install() {
24
+ ['http', 'https', 'spdy'].forEach((name) => {
25
+ depHooks.resolve({ name }, (_export) => {
26
+ if (!_export?.Server?.prototype) return;
27
+
28
+ patcher.patch(_export.Server.prototype, 'listen', {
29
+ name: `${name}.Server.prototype.listen`,
30
+ patchType: 'route-coverage',
31
+ post(data) {
32
+ data.result?.on('listening', () => {
33
+ if (!handled) {
34
+ handled = true;
35
+ routeCoverage.discoveryFinished();
36
+ }
37
+ });
38
+ }
39
+ });
40
+ });
41
+ });
42
+ }
43
+ };
44
+ };
@@ -14,8 +14,8 @@
14
14
  */
15
15
  'use strict';
16
16
 
17
- const { createSignature, patchType } = require('./../utils/route-info');
18
17
  const { toLowerCase } = require('@contrast/common');
18
+ const { createSignature, patchType } = require('./../utils/route-info');
19
19
 
20
20
  module.exports = function init(core) {
21
21
  const { patcher, depHooks, routeCoverage } = core;
@@ -37,46 +37,34 @@ module.exports = function init(core) {
37
37
  await next();
38
38
  };
39
39
 
40
- function registerRouteHandler(Router) {
41
- patcher.patch(Router.prototype, 'register', {
42
- name: 'koaRouter.prototype',
43
- patchType,
44
- post: ({ args, result: layer }) => {
45
- const [path, methods] = args;
46
- if (!path || !methods || Array.isArray(path)) {
47
- return;
48
- }
40
+ return core.routeCoverage.koa = {
41
+ install() {
42
+ ['@koa/router', 'koa-router'].forEach((name) => {
43
+ depHooks.resolve({ name }, (_export) => {
44
+ if (!_export?.prototype?.register) return;
49
45
 
50
- if (methods.length === 0) {
51
- emitRouteCoverage(path, 'use');
52
- } else {
53
- methods.forEach((method) => {
54
- emitRouteCoverage(path, toLowerCase(method || ''));
55
- });
56
- }
46
+ patcher.patch(_export.prototype, 'register', {
47
+ name: 'koaRouter.prototype',
48
+ patchType,
49
+ post: ({ args, result: layer }) => {
50
+ const [path, methods] = args;
51
+ if (!path || !methods || Array.isArray(path)) {
52
+ return;
53
+ }
57
54
 
58
- layer.stack.unshift(routeObservationPathClosure({ path }).bind(this));
59
- }
60
- });
55
+ if (methods.length === 0) {
56
+ emitRouteCoverage(path, 'use');
57
+ } else {
58
+ methods.forEach((method) => {
59
+ emitRouteCoverage(path, toLowerCase(method || ''));
60
+ });
61
+ }
61
62
 
62
- patcher.patch(Router.prototype, 'routes', {
63
- name: 'koaRouter.prototype',
64
- patchType,
65
- post: () => {
66
- routeCoverage.discoveryFinished();
67
- }
68
- });
69
- }
70
-
71
- core.routeCoverage.koa = {
72
- install() {
73
- ['@koa/router', 'koa-router'].forEach((name) => {
74
- depHooks.resolve(
75
- { name }, registerRouteHandler.bind(this)
76
- );
63
+ layer.stack.unshift(routeObservationPathClosure({ path }).bind(this));
64
+ }
65
+ });
66
+ });
77
67
  });
78
68
  }
79
69
  };
80
-
81
- return core.routeCoverage.koa;
82
70
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/route-coverage",
3
- "version": "1.11.0",
3
+ "version": "1.11.2",
4
4
  "description": "Handles route discovery and observation",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
@@ -17,6 +17,6 @@
17
17
  "test": "../scripts/test.sh"
18
18
  },
19
19
  "dependencies": {
20
- "@contrast/common": "1.15.0"
20
+ "@contrast/common": "1.15.1"
21
21
  }
22
22
  }