@contrast/agentify 1.43.0 → 1.45.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.js CHANGED
@@ -16,12 +16,14 @@
16
16
  'use strict';
17
17
 
18
18
  const Module = require('module');
19
-
19
+ const path = require('path');
20
+ const { IntentionalError, safeConsoleError } = require('@contrast/common');
21
+ const { Core } = require('@contrast/core/lib/ioc/core');
20
22
  const {
21
23
  assertValidOpts,
22
24
  preStartupValidation,
23
25
  } = require('./utils');
24
- const { IntentionalError } = require('@contrast/common');
26
+
25
27
 
26
28
  const ERROR_MESSAGE = 'An error prevented the Contrast agent from installing. The application will be run without instrumentation.';
27
29
  /**
@@ -38,6 +40,7 @@ const DEFAULT_INSTALL_ORDER = [
38
40
  'contrastMethods',
39
41
  'deadzones',
40
42
  'scopes',
43
+ 'secObs',
41
44
  'sources',
42
45
  'architectureComponents',
43
46
  'assess',
@@ -62,6 +65,10 @@ const DEFAULT_INSTALL_ORDER = [
62
65
  * @returns {import('.').Agentify}
63
66
  */
64
67
  module.exports = function init(core = {}) {
68
+ if (!(core instanceof Core)) core = new Core(core);
69
+
70
+ core.agentPath = path.resolve(__dirname, '..', '..');
71
+
65
72
  // used for data that needs to be transferred to threads. originally added
66
73
  // for the file descriptor so the main and esm thread logger instances can
67
74
  // share the same FD. over time, other module-specific data that needs to
@@ -74,6 +81,7 @@ module.exports = function init(core = {}) {
74
81
  if (!core.threadTransferData) {
75
82
  core.threadTransferData = Object.create(null);
76
83
  }
84
+
77
85
  core.startTime = process.hrtime.bigint();
78
86
  if (!core.Perf) {
79
87
  core.Perf = require('@contrast/perf');
@@ -83,7 +91,6 @@ module.exports = function init(core = {}) {
83
91
 
84
92
  let _callback;
85
93
  let _opts;
86
- const _perfAgentify = new core.Perf('agentify');
87
94
 
88
95
  core.agentify = async function agentify(callback, opts = {}) {
89
96
  // options are hardcoded, so if this throws it's going to be in testing/dev situation
@@ -133,6 +140,8 @@ module.exports = function init(core = {}) {
133
140
 
134
141
  const plugin = await _callback?.(core);
135
142
 
143
+ core.messages.emit('ext:core.pre-install');
144
+
136
145
  for (const svcName of _opts.installOrder ?? []) {
137
146
  const svc = core[svcName];
138
147
  if (svc?.install) {
@@ -144,12 +153,15 @@ module.exports = function init(core = {}) {
144
153
  if (plugin?.install) {
145
154
  await plugin.install();
146
155
  }
147
- } catch (err) {
148
- console.error(err);
149
- console.error(ERROR_MESSAGE);
150
156
 
157
+ core.messages.emit('ext:core.post-install');
158
+ } catch (err) {
151
159
  // TODO: Consider proper UNINSTALLATION and normal startup w/o agent
152
- logger.error({ err }, ERROR_MESSAGE);
160
+ if (logger) {
161
+ logger.error({ err }, ERROR_MESSAGE);
162
+ } else {
163
+ safeConsoleError(new Error(ERROR_MESSAGE, { cause: err }));
164
+ }
153
165
  }
154
166
  }
155
167
 
@@ -158,7 +170,6 @@ module.exports = function init(core = {}) {
158
170
  preStartupValidation(core);
159
171
 
160
172
  const modules = [
161
- { name: 'messages', spec: '@contrast/core/lib/messages' },
162
173
  { name: 'config', spec: '@contrast/config' },
163
174
  { name: 'logger', spec: '@contrast/logger', default: true },
164
175
  // call all configValidation functions here
@@ -180,7 +191,6 @@ module.exports = function init(core = {}) {
180
191
  { name: 'reporter', spec: '@contrast/reporter', default: true },
181
192
  { name: 'instrumentation', spec: '@contrast/instrumentation' },
182
193
  { name: 'metrics', spec: '@contrast/metrics' },
183
-
184
194
  // compose additional local services
185
195
  { name: 'heap-snapshots', spec: './heap-snapshots' },
186
196
  { name: 'sources', spec: './sources' },
@@ -213,12 +223,16 @@ module.exports = function init(core = {}) {
213
223
  // now load each module and, if a validator is available, run it.
214
224
  for (const { name, spec, default: isDefault } of modules) {
215
225
  // typescript inserts a default export for esm modules.
216
- let mod = require(spec);
226
+ let component = require(spec);
217
227
  if (isDefault) {
218
- mod = mod.default;
228
+ component = component.default;
219
229
  }
220
230
 
221
- _perfAgentify.wrapInit(mod, spec)(core);
231
+ if (Core.isComponent(component)) {
232
+ core.initComponentSync(component);
233
+ } else {
234
+ core.initComponentFactory(component, spec);
235
+ }
222
236
 
223
237
  // perform any validations that can take place now that this module is loaded.
224
238
  if (name === 'logger') {
@@ -239,10 +253,10 @@ module.exports = function init(core = {}) {
239
253
  // IntentionalError is used when the agent is disabled by config. We want
240
254
  // to abort the installation but not issue any messages.
241
255
  if (!(err instanceof IntentionalError)) {
242
- console.error(err);
243
- console.error(ERROR_MESSAGE);
244
256
  if (core.logger) {
245
257
  core.logger?.error?.({ err }, ERROR_MESSAGE);
258
+ } else {
259
+ safeConsoleError(new Error(ERROR_MESSAGE, { cause: err }));
246
260
  }
247
261
  }
248
262
  }
package/lib/validators.js CHANGED
@@ -15,7 +15,11 @@
15
15
 
16
16
  'use strict';
17
17
 
18
- const { IntentionalError, primordials: { ArrayPrototypeSlice, StringPrototypeToLowerCase } } = require('@contrast/common');
18
+ const {
19
+ IntentionalError,
20
+ primordials: { ArrayPrototypeSlice, StringPrototypeToLowerCase },
21
+ safeConsoleWarn,
22
+ } = require('@contrast/common');
19
23
 
20
24
  // v4 accepted `-c` or `--configFile` option in argv, but v5 does not. so if
21
25
  // something that looks like a config flag is present on the command line, we
@@ -31,7 +35,7 @@ function config(core) {
31
35
  if (core.logger) {
32
36
  core.logger.warn(msg);
33
37
  } else {
34
- console.warn(msg);
38
+ safeConsoleWarn(msg);
35
39
  }
36
40
  }
37
41
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/agentify",
3
- "version": "1.43.0",
3
+ "version": "1.45.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)",
@@ -20,21 +20,21 @@
20
20
  "test": "../scripts/test.sh"
21
21
  },
22
22
  "dependencies": {
23
- "@contrast/common": "1.30.0",
24
- "@contrast/config": "1.41.0",
25
- "@contrast/core": "1.46.0",
26
- "@contrast/deadzones": "1.18.0",
27
- "@contrast/dep-hooks": "1.15.0",
28
- "@contrast/esm-hooks": "2.20.0",
23
+ "@contrast/common": "1.32.0",
24
+ "@contrast/config": "1.43.0",
25
+ "@contrast/core": "1.48.0",
26
+ "@contrast/deadzones": "1.20.0",
27
+ "@contrast/dep-hooks": "1.17.0",
28
+ "@contrast/esm-hooks": "2.22.0",
29
29
  "@contrast/find-package-json": "^1.1.0",
30
- "@contrast/instrumentation": "1.25.0",
31
- "@contrast/logger": "1.19.0",
32
- "@contrast/metrics": "1.23.0",
33
- "@contrast/patcher": "1.18.0",
30
+ "@contrast/instrumentation": "1.27.0",
31
+ "@contrast/logger": "1.21.0",
32
+ "@contrast/metrics": "1.25.0",
33
+ "@contrast/patcher": "1.20.0",
34
34
  "@contrast/perf": "1.3.1",
35
- "@contrast/reporter": "1.42.0",
36
- "@contrast/rewriter": "1.22.0",
37
- "@contrast/scopes": "1.16.0",
35
+ "@contrast/reporter": "1.44.0",
36
+ "@contrast/rewriter": "1.24.0",
37
+ "@contrast/scopes": "1.18.0",
38
38
  "on-finished": "^2.4.1",
39
39
  "semver": "^7.6.0"
40
40
  }