@contrast/route-coverage 1.53.0 → 1.53.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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright: 2025 Contrast Security, Inc
1
+ Copyright: 2026 Contrast Security, Inc
2
2
  Contact: support@contrastsecurity.com
3
3
  License: Commercial
4
4
 
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -18,17 +18,20 @@ import { Config } from '@contrast/config';
18
18
  import { DepHooks } from '@contrast/dep-hooks';
19
19
  import { Logger } from '@contrast/logger';
20
20
  import { Patcher } from '@contrast/patcher';
21
+ import { Rewriter } from '@contrast/rewriter';
21
22
  import { Scopes } from '@contrast/scopes';
22
23
 
23
24
  export { RouteInfo };
24
25
 
25
26
  export interface RouteCoverage extends Installable {
27
+ MAX_FILE_LENGTH: number,
26
28
  DISCOVERY_QUEUE_EMPTY_MS: number;
27
29
  discover(info: RouteInfo): void;
28
30
  discoveryFinished(): void;
29
31
  queue(info: RouteInfo): void;
30
32
  queuingFinished(): void;
31
33
  observe(info: RouteInfo): void;
34
+ formatHandlerSync(fn: Function, appDir?: string) : string;
32
35
  }
33
36
 
34
37
  export interface Core {
@@ -37,6 +40,7 @@ export interface Core {
37
40
  readonly logger: Logger;
38
41
  readonly messages: Messages;
39
42
  readonly patcher: Patcher;
43
+ readonly rewriter: Rewriter,
40
44
  readonly scopes: Scopes;
41
45
  initComponentSync(c: any): void;
42
46
  }
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -19,6 +19,10 @@ const {
19
19
  callChildComponentMethodsSync,
20
20
  Event,
21
21
  RouteType,
22
+ primordials: {
23
+ StringPrototypeSubstring,
24
+ StringPrototypeReplace,
25
+ }
22
26
  } = require('@contrast/common');
23
27
 
24
28
  /**
@@ -41,6 +45,7 @@ module.exports = function init(core) {
41
45
  const routeIdentifier = (method, signature) => `${method}.${signature}`;
42
46
 
43
47
  const routeCoverage = core.routeCoverage = {
48
+ MAX_FILE_LENGTH: 40,
44
49
  DISCOVERY_QUEUE_EMPTY_MS: 10_000,
45
50
  discover(info) {
46
51
  const id = routeIdentifier(info.method, info.signature);
@@ -120,6 +125,24 @@ module.exports = function init(core) {
120
125
  callChildComponentMethodsSync(this, 'install');
121
126
  setInterval(() => recentlyObserved.clear(), 10000).unref();
122
127
  },
128
+
129
+ formatHandlerSync(handler, appDir) {
130
+ const info = core.rewriter.funcInfoSync(handler);
131
+ if (!info) return '[Function]';
132
+
133
+ let file = info.file ?
134
+ StringPrototypeReplace.call(info.file, appDir, '') :
135
+ '';
136
+
137
+ if (file.length > this.MAX_FILE_LENGTH) {
138
+ file = `...${StringPrototypeSubstring.call(file, file.length - this.MAX_FILE_LENGTH)}`;
139
+ }
140
+ const handlerName = info.method || handler.name || 'anonymous';
141
+ const formattedHandler = (file && Number.isFinite(info.lineNumber) && Number.isFinite(info.column)) ?
142
+ `[${handlerName} ${file} ${info.lineNumber}:${info.column}]` :
143
+ `[Function: ${handlerName}]`; // what util.inspect(handler) would return
144
+ return formattedHandler;
145
+ }
123
146
  };
124
147
 
125
148
  core.initComponentSync(require('./install/express'));
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -30,7 +30,6 @@ const {
30
30
  }
31
31
  } = require('@contrast/common');
32
32
  const Core = require('@contrast/core/lib/ioc/core');
33
- const { formatHandler } = require('../utils/route-info');
34
33
 
35
34
  const METHODS = [
36
35
  'all',
@@ -88,7 +87,6 @@ class ExpressInstrumentation {
88
87
 
89
88
  core.messages.on(Event.SERVER_LISTENING, () => {
90
89
  let router;
91
-
92
90
  self.listenFlag = true;
93
91
 
94
92
  try {
@@ -482,7 +480,7 @@ class ExpressInstrumentation {
482
480
  }
483
481
  let template = ArrayPrototypeJoin.call(templates, '');
484
482
  if (template == '') template = '/';
485
- const signature = `${type}.${method}(${template}, ${formatHandler(handler)})`;
483
+ const signature = `${type}.${method}(${template}, ${core.routeCoverage.formatHandlerSync(handler)})`;
486
484
 
487
485
  // this gets merged into meta.observables if same route handler is mounted at multiple paths
488
486
  return {
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -15,57 +15,61 @@
15
15
  'use strict';
16
16
 
17
17
  const { RouteType } = require('@contrast/common');
18
- const { patchType, formatHandler } = require('./../../utils/route-info');
19
- const isArray = (arr) => Array.isArray(arr);
20
- module.exports = function init(core) {
21
- const { patcher, depHooks, routeCoverage, scopes } = core;
18
+ const { Core } = require('@contrast/core/lib/ioc/core');
19
+ const { patchType } = require('./../../utils/route-info');
22
20
 
23
- return core.routeCoverage.fastifyExpress = {
24
- install() {
25
- const name = 'fastifyExpress';
26
- depHooks.resolve({ name: '@fastify/express', version: '*' }, (_xport) => patcher.patch(_xport, {
27
- name,
28
- patchType,
29
- post(data) {
30
- const store = { lock: true, name };
31
- patcher.patch(data.args[0], 'use', {
32
- name: 'use',
33
- patchType,
34
- around(next, data) {
35
- const [url, fn] = data.args;
36
- if (!url || !fn || !core.config.getEffectiveValue('assess.report_middleware_routes')) return next();
21
+ module.exports = Core.makeComponent({
22
+ name: 'core.routeCoverage.fastifyExpress',
23
+ factory: function init(core) {
24
+ const { patcher, depHooks, routeCoverage, scopes } = core;
37
25
 
38
- const middleware = isArray(fn) ? fn : [fn];
39
- const formattedPath = isArray(url) ? `[${url.join(', ')}]` : url;
40
- const patchedMiddleware = middleware.map((f) => {
41
- const formattedHandler = formatHandler(f);
42
- const signature = `fastify.use(${formattedPath}, ${formattedHandler})`;
26
+ return core.routeCoverage.fastifyExpress = {
27
+ install() {
28
+ const name = 'fastifyExpress';
29
+ depHooks.resolve({ name: '@fastify/express', version: '*' }, (_xport) => patcher.patch(_xport, {
30
+ name,
31
+ patchType,
32
+ post(data) {
33
+ const store = { lock: true, name };
34
+ patcher.patch(data.args[0], 'use', {
35
+ name: 'use',
36
+ patchType,
37
+ around(next, data) {
38
+ const [url, fn] = data.args;
39
+ if (!url || !fn || !core.config.getEffectiveValue('assess.report_middleware_routes')) return next();
43
40
 
44
- const routeInfo = {
45
- signature,
46
- url: formattedPath,
47
- method: 'use',
48
- normalizedUrl: formattedPath,
49
- type: RouteType.MIDDLEWARE,
50
- framework: 'fastify'
51
- };
52
- routeCoverage.discover(routeInfo);
41
+ const middleware = Array.isArray(fn) ? fn : [fn];
42
+ const formattedPath = Array.isArray(url) ? `[${url.join(', ')}]` : url;
43
+ const patchedMiddleware = middleware.map((f) => {
44
+ const formattedHandler = core.routeCoverage.formatHandlerSync(f);
45
+ const signature = `fastify.use(${formattedPath}, ${formattedHandler})`;
53
46
 
54
- return patcher.patch(f, {
55
- name: 'middleware',
56
- patchType,
57
- post() {
58
- routeCoverage.observe(routeInfo);
59
- }
47
+ const routeInfo = {
48
+ signature,
49
+ url: formattedPath,
50
+ method: 'use',
51
+ normalizedUrl: formattedPath,
52
+ type: RouteType.MIDDLEWARE,
53
+ framework: 'fastify'
54
+ };
55
+ routeCoverage.discover(routeInfo);
56
+
57
+ return patcher.patch(f, {
58
+ name: 'middleware',
59
+ patchType,
60
+ post() {
61
+ routeCoverage.observe(routeInfo);
62
+ }
63
+ });
60
64
  });
61
- });
62
- data.args[1] = patchedMiddleware;
65
+ data.args[1] = patchedMiddleware;
63
66
 
64
- return !scopes.instrumentation.isLocked() ? scopes.instrumentation.run(store, next) : next();
65
- }
66
- });
67
- }
68
- }));
69
- }
70
- };
71
- };
67
+ return !scopes.instrumentation.isLocked() ? scopes.instrumentation.run(store, next) : next();
68
+ }
69
+ });
70
+ }
71
+ }));
72
+ }
73
+ };
74
+ }
75
+ });
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -15,8 +15,8 @@
15
15
  'use strict';
16
16
 
17
17
  const { RouteType } = require('@contrast/common');
18
- const { patchType, formatHandler } = require('./../../utils/route-info');
19
- const isArray = (arr) => Array.isArray(arr);
18
+ const { patchType } = require('./../../utils/route-info');
19
+
20
20
  module.exports = function init(core) {
21
21
  const { patcher, depHooks, routeCoverage } = core;
22
22
 
@@ -33,10 +33,10 @@ module.exports = function init(core) {
33
33
  const [url, fn] = data.args;
34
34
  if (!url || !fn || !core.config.getEffectiveValue('assess.report_middleware_routes')) return;
35
35
 
36
- const middleware = isArray(fn) ? fn : [fn];
37
- const formattedPath = isArray(url) ? `[${url.join(', ')}]` : url;
36
+ const middleware = Array.isArray(fn) ? fn : [fn];
37
+ const formattedPath = Array.isArray(url) ? `[${url.join(', ')}]` : url;
38
38
  const patchedMiddleware = middleware.map((f) => {
39
- const formattedHandler = formatHandler(f);
39
+ const formattedHandler = core.routeCoverage.formatHandlerSync(f);
40
40
  const signature = `fastify.use(${formattedPath}, ${formattedHandler})`;
41
41
 
42
42
  const routeInfo = {
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -19,7 +19,7 @@ const {
19
19
  primordials: { StringPrototypeToLowerCase, StringPrototypeSplit },
20
20
  RouteType,
21
21
  } = require('@contrast/common');
22
- const { patchType, formatHandler } = require('./../../utils/route-info');
22
+ const { patchType } = require('./../../utils/route-info');
23
23
 
24
24
  // Spec: https://contrast.atlassian.net/wiki/spaces/NOD/pages/3454861621/Node.js+Agent+Route+Signatures#Fastify
25
25
  module.exports = function init(core) {
@@ -41,9 +41,10 @@ module.exports = function init(core) {
41
41
  function createRouteInfo(method, url, fullyDeclared, type, handler) {
42
42
  method = StringPrototypeToLowerCase.call(method);
43
43
 
44
+ const formattedHandler = core.routeCoverage.formatHandlerSync(handler);
44
45
  const signature = fullyDeclared
45
- ? `fastify.route({ method: ${method}, url: ${url}, handler: ${formatHandler(handler)} })`
46
- : `fastify.${method}(${url}, ${formatHandler(handler)})`;
46
+ ? `fastify.route({ method: ${method}, url: ${url}, handler: ${formattedHandler} })`
47
+ : `fastify.${method}(${url}, ${formattedHandler})`;
47
48
 
48
49
  const routeInfo = {
49
50
  signature,
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -18,7 +18,7 @@
18
18
  const { AsyncLocalStorage } = require('node:async_hooks');
19
19
  const { RouteType, set } = require('@contrast/common');
20
20
  const { Core } = require('@contrast/core/lib/ioc/core');
21
- const { formatHandler, patchType } = require('../utils/route-info');
21
+ const { patchType } = require('../utils/route-info');
22
22
 
23
23
  /**
24
24
  * The hapi `Route` class from lib/route.js is not defined or exported.
@@ -63,7 +63,7 @@ class HapiRouteCoverage {
63
63
  * @param {Route} route
64
64
  */
65
65
  createSignature(route) {
66
- const handler = formatHandler(this.patcher.unwrap(route.settings.handler));
66
+ const handler = this.core.routeCoverage.formatHandlerSync(this.patcher.unwrap(route.settings.handler));
67
67
  return `server.route({ method: '${route.method}', path: '${route.path}', handler: ${handler} })`;
68
68
  }
69
69
 
@@ -121,6 +121,7 @@ class HapiRouteCoverage {
121
121
  patchType,
122
122
  post({ result: route }) {
123
123
  if (route._special) return; // skip special internal routes
124
+
124
125
  const signature = self.createSignature(route);
125
126
  const type = self.registerScope.getStore()?.isMiddleware ? RouteType.MIDDLEWARE : RouteType.HTTP;
126
127
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -16,7 +16,7 @@
16
16
 
17
17
  const { METHODS } = require('./../utils/methods');
18
18
  const { isString, RouteType, primordials: { StringPrototypeToLowerCase, StringPrototypeSplit } } = require('@contrast/common');
19
- const { patchType, formatHandler } = require('./../utils/route-info');
19
+ const { patchType } = require('./../utils/route-info');
20
20
 
21
21
  // Spec: https://contrast.atlassian.net/wiki/spaces/NOD/pages/3454861621/Node.js+Agent+Route+Signatures#Koa
22
22
  module.exports = function init(core) {
@@ -49,7 +49,7 @@ module.exports = function init(core) {
49
49
  const method = methods.length === 0 ? 'use' : METHODS.every(m => methods.includes(m)) ? 'all' : StringPrototypeToLowerCase.call(methods[methods.length - 1]);
50
50
  if (method === 'use' && !core.config.getEffectiveValue('assess.report_middleware_routes')) return;
51
51
  const routeInfo = {
52
- signature: `Router.${method}(${path}, ${formatHandler(handler)})`,
52
+ signature: `Router.${method}(${path}, ${core.routeCoverage.formatHandlerSync(handler)})`,
53
53
  method,
54
54
  url: path,
55
55
  normalizedUrl: path,
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -26,7 +26,7 @@ const {
26
26
  RouteType,
27
27
  } = require('@contrast/common');
28
28
  const { Core } = require('@contrast/core/lib/ioc/core');
29
- const { formatHandler, patchType } = require('../utils/route-info');
29
+ const { patchType } = require('../utils/route-info');
30
30
 
31
31
  const COMPONENT_NAME = 'routeCoverage.restify';
32
32
  const FRAMEWORK = 'restify';
@@ -45,7 +45,7 @@ class RestifyInstrumentation {
45
45
  }
46
46
 
47
47
  formatHandler(fn) {
48
- return formatHandler(this.core.patcher.unwrap(fn));
48
+ return this.core.routeCoverage.formatHandlerSync(this.core.patcher.unwrap(fn));
49
49
  }
50
50
 
51
51
  install() {
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -15,8 +15,6 @@
15
15
  'use strict';
16
16
 
17
17
  const patchType = 'route-coverage';
18
- const { funcInfo } = require('@contrast/fn-inspect');
19
- const { primordials: { StringPrototypeReplace, StringPrototypeSubstring } } = require('@contrast/common');
20
18
 
21
19
  /**
22
20
  * Creates a formatted "signature" for a route
@@ -30,27 +28,4 @@ function createSignature(path, method = '', obj = 'Router', handler = '[Function
30
28
  return `${obj}.${method}('${path}', ${handler})`;
31
29
  }
32
30
 
33
- /**
34
- * Creates a formatted handler signature for a route
35
- * @param {function} handler
36
- * @param {string=} appDir
37
- * @return {string} formatted handler
38
- */
39
- function formatHandler(handler, appDir) {
40
- const info = funcInfo(handler);
41
- if (!info) return '[Function]';
42
-
43
- let file = info.file ?
44
- StringPrototypeReplace.call(info.file, appDir, '') :
45
- '';
46
- if (file.length > 30) {
47
- file = `...${StringPrototypeSubstring.call(file, file.length - 40)}`;
48
- }
49
- const handlerName = info.method || handler.name || 'anonymous';
50
- const formattedHandler = (file && Number.isFinite(info.lineNumber) && Number.isFinite(info.column)) ?
51
- `[${handlerName} ${file} ${info.lineNumber}:${info.column}]` :
52
- `[Function: ${handlerName}]`; // what util.inspect(handler) would return
53
- return formattedHandler;
54
- }
55
-
56
- module.exports = { createSignature, patchType, formatHandler };
31
+ module.exports = { createSignature, patchType };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/route-coverage",
3
- "version": "1.53.0",
3
+ "version": "1.53.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)",
@@ -20,14 +20,15 @@
20
20
  "test": "bash ../scripts/test.sh"
21
21
  },
22
22
  "dependencies": {
23
- "@contrast/common": "1.39.0",
24
- "@contrast/config": "1.55.0",
25
- "@contrast/core": "1.60.0",
26
- "@contrast/dep-hooks": "1.29.0",
23
+ "@contrast/common": "1.39.1",
24
+ "@contrast/config": "1.55.1",
25
+ "@contrast/core": "1.60.1",
26
+ "@contrast/dep-hooks": "1.29.1",
27
27
  "@contrast/fn-inspect": "^5.0.2",
28
- "@contrast/logger": "1.33.0",
29
- "@contrast/patcher": "1.32.0",
30
- "@contrast/scopes": "1.30.0",
28
+ "@contrast/logger": "1.33.1",
29
+ "@contrast/patcher": "1.32.1",
30
+ "@contrast/rewriter": "1.37.2",
31
+ "@contrast/scopes": "1.30.1",
31
32
  "semver": "^7.6.0"
32
33
  }
33
34
  }