@contrast/route-coverage 1.19.0 → 1.20.1

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
@@ -85,6 +85,7 @@ module.exports = function init(core) {
85
85
  require('./install/fastify')(core);
86
86
  require('./install/hapi')(core);
87
87
  require('./install/koa')(core);
88
+ require('./install/restify')(core);
88
89
 
89
90
  return routeCoverage;
90
91
  };
@@ -36,7 +36,7 @@ module.exports = function init(core) {
36
36
  const observe = (route) => routeCoverage.observe(route);
37
37
 
38
38
  const isRoute = (layer) => !!layer.route;
39
- const isRouter = (layer) => toLowerCase(layer.name) === 'router';
39
+ const isRouter = (layer) => layer.name && toLowerCase(layer.name) === 'router';
40
40
  const isValidPath = (path) => isString(path) || Array.isArray(path) || path instanceof RegExp;
41
41
  const regExpToPath = (regex) => regex?.source?.split('/?')[0].replace(/\\/g, '').replace('^', ''); //TODO: replaceAll when v14 deprecated
42
42
  const format = (url) => Array.isArray(url) ? `/[${join(url)}]` : url instanceof RegExp ? `/{${url.toString().slice(1, -1)}}` : url;
@@ -103,13 +103,16 @@ module.exports = function init(core) {
103
103
  const fn = args[len - 1];
104
104
  const path = len > 1 ? args[0] : '';
105
105
  if (!isValidPath(path)) return;
106
- if (isRouter(fn)) {
107
- traverse(path, fn.stack);
108
- } else {
109
- const routeInfo = createRouteInfo(path, 'use', 'App');
110
- discover(routeInfo);
111
- patchHandle(result._router, routeInfo);
112
- }
106
+ const handlers = Array.isArray(fn) ? fn : [fn];
107
+ handlers.forEach((layer) => {
108
+ if (isRouter(layer)) {
109
+ traverse(path, layer.stack);
110
+ } else {
111
+ const routeInfo = createRouteInfo(format(path), 'use', 'App');
112
+ discover(routeInfo);
113
+ patchHandle(result._router, routeInfo);
114
+ }
115
+ });
113
116
  }
114
117
  });
115
118
 
@@ -0,0 +1,69 @@
1
+ /*
2
+ * Copyright: 2024 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
+ const { toLowerCase, isString } = require('@contrast/common');
18
+ const { createSignature, patchType } = require('../utils/route-info');
19
+
20
+ module.exports = function init(core) {
21
+ const { patcher, depHooks, routeCoverage } = core;
22
+ const discover = (route) => routeCoverage.discover(route);
23
+ const observe = (route) => routeCoverage.observe(route);
24
+
25
+ function createRoute(url, method) {
26
+ method = toLowerCase(method);
27
+ return {
28
+ signature: createSignature(url, method, 'Server'),
29
+ method,
30
+ url,
31
+ normalizedUrl: url
32
+ };
33
+ }
34
+
35
+ return core.routeCoverage.restify = {
36
+ install() {
37
+ depHooks.resolve({ name: 'restify', version: '>= 8.0.0' }, (restify) => {
38
+ patcher.patch(restify, 'createServer', {
39
+ name: 'restify.createServer',
40
+ patchType,
41
+ post({ result: server }) {
42
+ patcher.patch(server.router, 'mount', {
43
+ name: 'restify.router.mount',
44
+ patchType,
45
+ post({ result: route }) {
46
+ const { path, method } = route;
47
+ if (!path || !method || !isString(path)) return;
48
+ const routeInfo = createRoute(path, method);
49
+ discover(routeInfo);
50
+
51
+ const [handler] = route.chain._stack;
52
+ route.chain._stack[0] = patcher.patch(handler, {
53
+ name: 'route.chain._stack[0].handler',
54
+ patchType,
55
+ post({ args }) {
56
+ const [req] = args;
57
+ const { url: reqUrl, method } = req;
58
+ const [url] = reqUrl.split('?');
59
+ observe({ ...routeInfo, method: toLowerCase(method), url });
60
+ }
61
+ });
62
+ }
63
+ });
64
+ }
65
+ });
66
+ });
67
+ }
68
+ };
69
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/route-coverage",
3
- "version": "1.19.0",
3
+ "version": "1.20.1",
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,7 +17,7 @@
17
17
  "test": "../scripts/test.sh"
18
18
  },
19
19
  "dependencies": {
20
- "@contrast/common": "1.20.1",
20
+ "@contrast/common": "1.21.0",
21
21
  "@contrast/fn-inspect": "^4.0.0"
22
22
  }
23
23
  }