@contrast/route-coverage 1.20.3 → 1.20.5

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.
@@ -28,7 +28,7 @@ const METHODS = [
28
28
 
29
29
  const fnInspect = require('@contrast/fn-inspect');
30
30
  const { createSignature, patchType } = require('../utils/route-info');
31
- const { join, toLowerCase, isString } = require('@contrast/common');
31
+ const { ArrayPrototypeJoin, StringPrototypeToLowerCase, isString } = require('@contrast/common');
32
32
 
33
33
  module.exports = function init(core) {
34
34
  const { patcher, depHooks, routeCoverage } = core;
@@ -36,10 +36,10 @@ 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) => layer.name && toLowerCase(layer.name) === 'router';
39
+ const isRouter = (layer) => layer.name && StringPrototypeToLowerCase.call(layer.name) === 'router';
40
40
  const isValidPath = (path) => isString(path) || Array.isArray(path) || path instanceof RegExp;
41
- const regExpToPath = (regex) => regex?.source?.split('/?')[0].replace(/\\/g, '').replace('^', ''); //TODO: replaceAll when v14 deprecated
42
- const format = (url) => Array.isArray(url) ? `/[${join(url)}]` : url instanceof RegExp ? `/{${url.toString().slice(1, -1)}}` : url;
41
+ const regExpToPath = (regex) => regex?.source?.split('/?')[0].replaceAll('\\', '').replace('^', '');
42
+ const format = (url) => Array.isArray(url) ? `/[${ArrayPrototypeJoin.call(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
44
  const getLastLayer = (router) => router?.stack[router.stack.length - 1];
45
45
 
@@ -68,7 +68,7 @@ module.exports = function init(core) {
68
68
  const [url] = req.originalUrl.split('?');
69
69
  const { method } = req;
70
70
  if (url && method) {
71
- observe({ ...routeInfo, url, method: toLowerCase(method) });
71
+ observe({ ...routeInfo, url, method: StringPrototypeToLowerCase.call(method) });
72
72
  }
73
73
  }
74
74
  });
@@ -102,17 +102,13 @@ module.exports = function init(core) {
102
102
  post({ args, result }) {
103
103
  const len = args.length;
104
104
  const fn = args[len - 1];
105
-
106
- let path;
107
- if (len > 1) {
108
- path = args[0];
109
- if (!isValidPath(path)) return;
110
- }
105
+ const path = len > 1 ? args[0] : undefined;
106
+ if (path && !isValidPath(path)) return;
111
107
  const handlers = Array.isArray(fn) ? fn : [fn];
112
108
  handlers.forEach((layer) => {
113
109
  if (isRouter(layer)) {
114
110
  traverse(path, layer.stack);
115
- } else {
111
+ } else if (path) {
116
112
  const routeInfo = createRouteInfo(format(path), 'use', 'App');
117
113
  discover(routeInfo);
118
114
  const lastLayer = getLastLayer(result._router);
@@ -16,7 +16,7 @@
16
16
  'use strict';
17
17
 
18
18
  const { createSignature } = require('./../utils/route-info');
19
- const { toLowerCase } = require('@contrast/common');
19
+ const { StringPrototypeToLowerCase } = require('@contrast/common');
20
20
 
21
21
  /** @typedef {Parameters<import('fastify-3.0.0').onRouteHookHandler>[0]} RouteOptions */
22
22
 
@@ -69,7 +69,7 @@ module.exports = function init(core) {
69
69
  * @param {string} method
70
70
  */
71
71
  function emitRouteCoverage(url, method) {
72
- method = toLowerCase(method);
72
+ method = StringPrototypeToLowerCase.call(method);
73
73
  const event = { signature: createSignature(url, method), url, method, normalizedUrl: url };
74
74
  routeCoverage.discover(event);
75
75
  }
@@ -80,7 +80,7 @@ module.exports = function init(core) {
80
80
  * @param {string=} method
81
81
  */
82
82
  function emitObservation(url, normalizedUrl, method) {
83
- method = method && toLowerCase(method);
83
+ method = method && StringPrototypeToLowerCase.call(method);
84
84
  routeCoverage.observe({ method, url, normalizedUrl });
85
85
  }
86
86
 
@@ -14,14 +14,14 @@
14
14
  */
15
15
  'use strict';
16
16
 
17
- const { toLowerCase } = require('@contrast/common');
17
+ const { StringPrototypeToLowerCase } = require('@contrast/common');
18
18
  const { createSignature, patchType } = require('./../utils/route-info');
19
19
 
20
20
  module.exports = function init(core) {
21
21
  const { patcher, depHooks, routeCoverage } = core;
22
22
 
23
23
  function emitRouteCoverage(url, method) {
24
- method = toLowerCase(method);
24
+ method = StringPrototypeToLowerCase.call(method);
25
25
  const event = { signature: createSignature(url, method), url, method, normalizedUrl: url };
26
26
  routeCoverage.discover(event);
27
27
  }
@@ -56,7 +56,7 @@ module.exports = function init(core) {
56
56
  patchType,
57
57
  post({ args }) {
58
58
  const [{ method, path, route }] = args;
59
- routeCoverage.observe({ url: path, method: toLowerCase(method), normalizedUrl: route.path });
59
+ routeCoverage.observe({ url: path, method: StringPrototypeToLowerCase.call(method), normalizedUrl: route.path });
60
60
  }
61
61
  });
62
62
  }
@@ -14,7 +14,7 @@
14
14
  */
15
15
  'use strict';
16
16
 
17
- const { toLowerCase } = require('@contrast/common');
17
+ const { StringPrototypeToLowerCase } = require('@contrast/common');
18
18
  const { createSignature, patchType } = require('./../utils/route-info');
19
19
 
20
20
  module.exports = function init(core) {
@@ -32,7 +32,7 @@ module.exports = function init(core) {
32
32
  if (req) {
33
33
  const { url: reqUrl, method } = req;
34
34
  const [url] = reqUrl.split(/\?/);
35
- routeCoverage.observe({ url, method: toLowerCase(method || ''), normalizedUrl: path });
35
+ routeCoverage.observe({ url, method: StringPrototypeToLowerCase.call(method || ''), normalizedUrl: path });
36
36
  }
37
37
 
38
38
  await next();
@@ -57,7 +57,7 @@ module.exports = function init(core) {
57
57
  emitRouteCoverage(path, 'use');
58
58
  } else {
59
59
  methods.forEach((method) => {
60
- emitRouteCoverage(path, toLowerCase(method || ''));
60
+ emitRouteCoverage(path, StringPrototypeToLowerCase.call(method || ''));
61
61
  });
62
62
  }
63
63
 
@@ -14,7 +14,7 @@
14
14
  */
15
15
  'use strict';
16
16
 
17
- const { toLowerCase, isString } = require('@contrast/common');
17
+ const { StringPrototypeToLowerCase, isString } = require('@contrast/common');
18
18
  const { createSignature, patchType } = require('../utils/route-info');
19
19
 
20
20
  module.exports = function init(core) {
@@ -23,7 +23,7 @@ module.exports = function init(core) {
23
23
  const observe = (route) => routeCoverage.observe(route);
24
24
 
25
25
  function createRoute(url, method) {
26
- method = toLowerCase(method);
26
+ method = StringPrototypeToLowerCase.call(method);
27
27
  return {
28
28
  signature: createSignature(url, method, 'Server'),
29
29
  method,
@@ -56,7 +56,7 @@ module.exports = function init(core) {
56
56
  const [req] = args;
57
57
  const { url: reqUrl, method } = req;
58
58
  const [url] = reqUrl.split('?');
59
- observe({ ...routeInfo, method: toLowerCase(method), url });
59
+ observe({ ...routeInfo, method: StringPrototypeToLowerCase.call(method), url });
60
60
  }
61
61
  });
62
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/route-coverage",
3
- "version": "1.20.3",
3
+ "version": "1.20.5",
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)",
@@ -11,13 +11,13 @@
11
11
  "types": "lib/index.d.ts",
12
12
  "engines": {
13
13
  "npm": ">=6.13.7 <7 || >= 8.3.1",
14
- "node": ">= 14.18.0"
14
+ "node": ">= 16.9.1"
15
15
  },
16
16
  "scripts": {
17
17
  "test": "../scripts/test.sh"
18
18
  },
19
19
  "dependencies": {
20
- "@contrast/common": "1.21.0",
20
+ "@contrast/common": "1.21.1",
21
21
  "@contrast/fn-inspect": "^4.0.0"
22
22
  }
23
23
  }