@contrast/route-coverage 1.20.1 → 1.20.3

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.
@@ -41,6 +41,7 @@ module.exports = function init(core) {
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;
43
43
  const getHandleMethod = (layer) => fnInspect.funcInfo(layer.__handle)?.file.includes('express-async-errors') ? '__handle' : 'handle';
44
+ const getLastLayer = (router) => router?.stack[router.stack.length - 1];
44
45
 
45
46
  function parseRoute(route) {
46
47
  const { path } = route;
@@ -73,7 +74,7 @@ module.exports = function init(core) {
73
74
  });
74
75
  }
75
76
 
76
- function traverse(path, stack, depth = 0) {
77
+ function traverse(path = '', stack, depth = 0) {
77
78
  path = format(path);
78
79
  stack.forEach((layer) => {
79
80
  if (isRoute(layer)) {
@@ -101,8 +102,12 @@ module.exports = function init(core) {
101
102
  post({ args, result }) {
102
103
  const len = args.length;
103
104
  const fn = args[len - 1];
104
- const path = len > 1 ? args[0] : '';
105
- if (!isValidPath(path)) return;
105
+
106
+ let path;
107
+ if (len > 1) {
108
+ path = args[0];
109
+ if (!isValidPath(path)) return;
110
+ }
106
111
  const handlers = Array.isArray(fn) ? fn : [fn];
107
112
  handlers.forEach((layer) => {
108
113
  if (isRouter(layer)) {
@@ -110,7 +115,8 @@ module.exports = function init(core) {
110
115
  } else {
111
116
  const routeInfo = createRouteInfo(format(path), 'use', 'App');
112
117
  discover(routeInfo);
113
- patchHandle(result._router, routeInfo);
118
+ const lastLayer = getLastLayer(result._router);
119
+ if (lastLayer) patchHandle(lastLayer, routeInfo);
114
120
  }
115
121
  });
116
122
  }
@@ -125,7 +131,8 @@ module.exports = function init(core) {
125
131
  if (!url || !fn || !isValidPath(url)) return;
126
132
  const routeInfo = createRouteInfo(format(url), method, 'App');
127
133
  discover(routeInfo);
128
- patchHandle(result._router, routeInfo);
134
+ const lastLayer = getLastLayer(result._router);
135
+ if (lastLayer) patchHandle(lastLayer, routeInfo);
129
136
  }
130
137
  });
131
138
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/route-coverage",
3
- "version": "1.20.1",
3
+ "version": "1.20.3",
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)",