@contrast/route-coverage 1.0.0

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.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright: 2022 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
+
16
+ export interface RouteCoverage {
17
+ discover: (infos: { signature: string, url: string, method: string }) => void
18
+ observe: (data: { url: string, verb: string }) => void
19
+ }
20
+
21
+ declare function init(core: any): RouteCoverage;
22
+
23
+ export = init;
package/lib/index.js ADDED
@@ -0,0 +1,53 @@
1
+ /*
2
+ * Copyright: 2022 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 { callChildComponentMethodsSync, Event } = require('@contrast/common');
18
+ const { routeIdentifier } = require('./utils/route-info');
19
+
20
+ module.exports = function(core) {
21
+ const { logger, messages } = core;
22
+
23
+ const routeCoverage = core.routeCoverage = {};
24
+ const routeInfo = new Map();
25
+
26
+ const discover = (info) => {
27
+ routeInfo.set(routeIdentifier(info), info);
28
+ messages.emit(Event.ROUTE_COVERAGE_DISCOVERY, info);
29
+ };
30
+
31
+ const observe = ({ url, verb }) => {
32
+ const info = { url, verb };
33
+
34
+ const route = routeInfo.get(routeIdentifier({ url, method: verb }));
35
+ if (!route) {
36
+ logger.debug('Unable to observe routes not discovered %j', info);
37
+ return null;
38
+ }
39
+
40
+ messages.emit(Event.ROUTE_COVERAGE_OBSERVATION, route);
41
+ };
42
+
43
+ routeCoverage.discover = discover;
44
+ routeCoverage.observe = observe;
45
+
46
+ require('./install/fastify')(core);
47
+
48
+ routeCoverage.install = function () {
49
+ callChildComponentMethodsSync(routeCoverage, 'install');
50
+ };
51
+
52
+ return routeCoverage;
53
+ };
@@ -0,0 +1,78 @@
1
+ /*
2
+ * Copyright: 2022 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 { createSignature } = require('./../utils/route-info');
18
+
19
+ module.exports = function (core) {
20
+ const { patcher, depHooks, routeCoverage } = core;
21
+
22
+ const routeCoverageFastify = core.routeCoverage.fastify = {};
23
+
24
+ function registerRouteHandler(routeOptions) {
25
+ if (!routeOptions || !routeOptions.method || !routeOptions.url) return;
26
+
27
+ const url = `${routeOptions.prefix || ''}${routeOptions.url}`;
28
+ if (Array.isArray(routeOptions.method)) {
29
+ routeOptions.method.forEach((method) => {
30
+ emitRouteCoverage(url, method);
31
+ });
32
+ } else {
33
+ emitRouteCoverage(url, routeOptions.method);
34
+ }
35
+
36
+ // TODO
37
+ observationListener(routeOptions, url);
38
+ }
39
+
40
+ function observationListener(routeOptions, url) {
41
+ patcher.patch(routeOptions, 'handler', {
42
+ name: 'fastify.routeOptions.handler',
43
+ patchType: 'route-coverage',
44
+ pre(data) {
45
+ const [request] = data.args;
46
+ const { method } = request.raw;
47
+ emitObservation(method, url);
48
+ },
49
+ });
50
+ }
51
+
52
+ function emitRouteCoverage(url, method) {
53
+ method = method.toLowerCase();
54
+ const event = { signature: createSignature(url, method), url, method };
55
+ routeCoverage.discover(event);
56
+ }
57
+
58
+ function emitObservation(verb, url) {
59
+ verb = verb.toLowerCase();
60
+ routeCoverage.observe({ verb, url });
61
+ }
62
+
63
+ routeCoverageFastify.install = function() {
64
+ depHooks.resolve({ name: 'fastify', version: '>=3.0.0' }, (fastify) =>
65
+ patcher.patch(fastify, {
66
+ name: 'fastify.build',
67
+ patchType: 'route-coverage',
68
+ post({ orig, hooked, result: server }) {
69
+ server.addHook('onRoute', function (routeOptions) {
70
+ registerRouteHandler(routeOptions);
71
+ });
72
+ },
73
+ })
74
+ );
75
+ };
76
+
77
+ return routeCoverageFastify;
78
+ };
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Copyright: 2022 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
+ /**
18
+ * Creates a formatted "signature" for a route
19
+ * @param {string} path
20
+ * @param {Array} [methods=['get']]
21
+ * @return {string} formatted signature
22
+ */
23
+ function createSignature(path, method) {
24
+ return `Router.${method}('${path}', [Function])`;
25
+ }
26
+
27
+ /**
28
+ * Creates a route identifier based on the method name and the url
29
+ * @param {Object} Route Information
30
+ * @return {string}
31
+ */
32
+ const routeIdentifier = (info) => `${info.method}.${info.url}`;
33
+
34
+ module.exports = { createSignature, routeIdentifier };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@contrast/route-coverage",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "license": "SEE LICENSE IN LICENSE",
6
+ "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
7
+ "main": "lib/index.js",
8
+ "types": "lib/index.d.ts",
9
+ "engines": {
10
+ "npm": ">=6.13.7 <7 || >= 8.3.1",
11
+ "node": ">= 14.15.0"
12
+ },
13
+ "scripts": {
14
+ "test": "../scripts/test.sh"
15
+ },
16
+ "dependencies": {
17
+ "@contrast/common": "1.4.0"
18
+ }
19
+ }