@contrast/agentify 1.21.0 → 1.22.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.
@@ -78,7 +78,7 @@ module.exports = function (deps) {
78
78
  // cannot parse lone function expressions that are not part of an assignment.
79
79
  try {
80
80
  let unwritten = functionHooks.contextualizeFunction(code);
81
- unwritten = rewriter.unwrite(unwritten);
81
+ unwritten = rewriter.unwriteSync(unwritten);
82
82
  unwritten = unwritten.replace(METHOD_CONTEXT, '');
83
83
  unwritten = unwritten.replace(FUNCTION_CONTEXT, '');
84
84
  unwritten = unwritten.replace(/;\s*$/, ''); // removes trailing semicolon/whitespace
package/lib/index.d.ts CHANGED
@@ -13,21 +13,36 @@
13
13
  * way not consistent with the End User License Agreement.
14
14
  */
15
15
 
16
- import { Config } from '@contrast/config';
17
16
  import { Installable } from '@contrast/common';
17
+ import { Config } from '@contrast/config';
18
+ import { Core as _Core } from '@contrast/core';
18
19
  import { Logger } from '@contrast/logger';
19
20
  import { Rewriter } from '@contrast/rewriter';
20
21
  import RequireHook from '@contrast/require-hook';
22
+ import { Patcher } from '@contrast/patcher';
23
+ import { Scopes } from '@contrast/scopes';
24
+ import { Deadzones } from '@contrast/deadzones';
25
+ import { ReporterBus } from '@contrast/reporter';
21
26
 
22
27
  declare module 'module' {
23
28
  class Module {
24
29
  /**
30
+ * @see https://github.com/nodejs/node/blob/main/lib/internal/modules/cjs/loader.js
31
+ * @param content The source code of the module
32
+ * @param filename The file path of the module
33
+ */
34
+ _compile(content: string, filename: string);
35
+
36
+ static _extensions: {
37
+ /**
25
38
  * @see https://github.com/nodejs/node/blob/main/lib/internal/modules/cjs/loader.js
26
- * @param content The source code of the module
39
+ * @param module The module to compile
27
40
  * @param filename The file path of the module
28
41
  */
29
- _compile(content: string, filename: string);
42
+ ['.js'](module: import('module'), filename: string);
43
+ };
30
44
  }
45
+
31
46
  export = Module;
32
47
  }
33
48
 
@@ -36,13 +51,17 @@ declare module 'node:module' {
36
51
  export = Module;
37
52
  }
38
53
 
39
- // TODO: this is now much larger with all of the existing core deps
40
- export interface Core {
41
- readonly config: Config;
42
- readonly depHooks: RequireHook;
43
- readonly logger: Logger;
44
- readonly rewriter: Rewriter;
45
- readonly threadInfo: any;
54
+ export interface Core extends _Core {
55
+ config: Config;
56
+ logger: Logger;
57
+ depHooks: RequireHook;
58
+ patcher: Patcher
59
+ rewriter: Rewriter;
60
+ scopes: Scopes;
61
+ deadzones: Deadzones;
62
+ reporter: ReporterBus;
63
+ instrumentation: any;
64
+ metrics: any;
46
65
  }
47
66
 
48
67
  export interface AgentifyOptions {
@@ -50,13 +69,13 @@ export interface AgentifyOptions {
50
69
  }
51
70
 
52
71
  export interface PreRunMain {
53
- (core: any): Installable | void | Promise<Installable | void>;
72
+ (core: Core): Installable | void | Promise<Installable | void>;
54
73
  }
55
74
 
56
75
  export interface Agentify {
57
- (preRunMain: PreRunMain, opts?: AgentifyOptions): any;
76
+ (preRunMain: PreRunMain, opts?: AgentifyOptions): Installable | void;
58
77
  }
59
78
 
60
- declare function init(core: any): Agentify;
79
+ declare function init(core: Partial<Core>): Agentify;
61
80
 
62
81
  export = init;
package/lib/index.js CHANGED
@@ -55,6 +55,8 @@ const DEFAULT_INSTALL_ORDER = [
55
55
  * @returns {import('.').Agentify}
56
56
  */
57
57
  module.exports = function init(core = {}) {
58
+ core.startTime = process.hrtime.bigint();
59
+
58
60
  let _callback;
59
61
  let _opts;
60
62
 
@@ -16,7 +16,7 @@
16
16
 
17
17
  'use strict';
18
18
 
19
- const Module = require('module');
19
+ const Module = require('node:module');
20
20
 
21
21
  /**
22
22
  * @param {import('.').Core & {
@@ -25,11 +25,12 @@ const Module = require('module');
25
25
  * @returns {import('@contrast/common').Installable}
26
26
  */
27
27
  module.exports = function init(core) {
28
+ const js = Module._extensions['.js'];
28
29
  const { _compile } = Module.prototype;
29
30
 
30
31
  core.rewriteHooks = {
31
32
  install() {
32
- if (!core.config.agent.node.enable_rewrite) return;
33
+ if (!core.config.agent.node.rewrite.enable) return;
33
34
 
34
35
  /**
35
36
  * @see https://github.com/nodejs/node/blob/main/lib/internal/modules/cjs/loader.js
@@ -37,33 +38,58 @@ module.exports = function init(core) {
37
38
  * @param {string} filename The file path of the module
38
39
  */
39
40
  Module.prototype._compile = function (content, filename) {
40
- let result;
41
+ /** @type {import('@contrast/rewriter').RewriteOpts} */
41
42
  const options = {
42
43
  filename,
43
44
  isModule: false,
44
45
  inject: true,
45
46
  wrap: true,
47
+ trim: false,
46
48
  };
47
49
 
48
- const { code } = core.rewriter.rewrite(content, options);
50
+ const result = core.rewriter.rewriteSync(content, options);
49
51
 
50
52
  try {
51
- result = _compile.call(this, code, filename);
53
+ const compiled = Reflect.apply(_compile, this, [result.code, filename]);
54
+
55
+ if (core.config.agent.node.rewrite.cache.enable) {
56
+ core.rewriter.cache.write(filename, result);
57
+ }
58
+
59
+ return compiled;
52
60
  } catch (err) {
53
61
  core.logger.warn(
54
62
  { err },
55
63
  'Failed to compile rewritten code for %s, compiling original code.',
56
64
  filename,
57
65
  );
58
- result = _compile.call(this, content, filename);
66
+
67
+ return Reflect.apply(_compile, this, [content, filename]);
59
68
  }
69
+ };
70
+
71
+ /**
72
+ * @see https://github.com/nodejs/node/blob/main/lib/internal/modules/cjs/loader.js
73
+ * @param {Module} module The module to compile
74
+ * @param {string} filename The file path of the module
75
+ */
76
+ Module._extensions['.js'] = function (module, filename) {
77
+ if (!core.config.agent.node.rewrite.cache.enable) {
78
+ return Reflect.apply(js, this, [module, filename]);
79
+ }
80
+
81
+ const cached = core.rewriter.cache.readSync(filename);
60
82
 
61
- return result;
83
+ // If cached, short circuit the _extensions method and go straight to compile.
84
+ return cached
85
+ ? Reflect.apply(_compile, module, [cached, filename])
86
+ : Reflect.apply(js, this, [module, filename]);
62
87
  };
63
88
  },
64
89
 
65
90
  uninstall() {
66
91
  Module.prototype._compile = _compile;
92
+ Module._extensions['.js'] = js;
67
93
  }
68
94
  };
69
95
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/agentify",
3
- "version": "1.21.0",
3
+ "version": "1.22.0",
4
4
  "description": "Configures Contrast agent services and instrumentation within an application",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
@@ -22,13 +22,13 @@
22
22
  "@contrast/core": "1.30.0",
23
23
  "@contrast/deadzones": "1.1.2",
24
24
  "@contrast/dep-hooks": "1.3.1",
25
- "@contrast/esm-hooks": "2.3.0",
25
+ "@contrast/esm-hooks": "2.4.0",
26
26
  "@contrast/instrumentation": "1.6.0",
27
27
  "@contrast/logger": "1.8.0",
28
- "@contrast/metrics": "1.5.0",
28
+ "@contrast/metrics": "1.6.0",
29
29
  "@contrast/patcher": "1.7.1",
30
- "@contrast/reporter": "1.25.0",
31
- "@contrast/rewriter": "1.4.2",
30
+ "@contrast/reporter": "1.25.1",
31
+ "@contrast/rewriter": "1.5.0",
32
32
  "@contrast/scopes": "1.4.0"
33
33
  }
34
34
  }