@contrast/esm-hooks 2.3.0 → 2.4.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/hooks.mjs CHANGED
@@ -33,6 +33,8 @@ let loaderAgent;
33
33
  * @param {{
34
34
  * modes: string[],
35
35
  * port: import('node:worker_threads').MessagePort,
36
+ * appInfo: import('@contrast/common').AppInfo,
37
+ * agentVersion: string,
36
38
  * }} data
37
39
  */
38
40
  async function initialize(data = {}) {
@@ -129,7 +131,7 @@ async function load(url, context, nextLoad) {
129
131
  inject: true,
130
132
  wrap: type !== 'module', // cannot wrap modules
131
133
  };
132
- result = loaderAgent.rewriter.rewrite(source, rewriteOptions);
134
+ result = await loaderAgent.rewriter.rewrite(source, rewriteOptions);
133
135
 
134
136
  if (process.env.CSI_EXPOSE_CORE) {
135
137
  // only do this in testing scenarios (todo: compose this functionality into test agents instead)
package/lib/index.mjs CHANGED
@@ -46,6 +46,8 @@ export default function init(core) {
46
46
  const data = {
47
47
  port: workerPort,
48
48
  modes: esmHooks.getActiveModes(),
49
+ appInfo: core.appInfo,
50
+ agentVersion: core.agentVersion,
49
51
  };
50
52
 
51
53
  // Instantiate loader agent via register (if available) or manually.
@@ -14,17 +14,26 @@
14
14
  */
15
15
 
16
16
  import Module from 'node:module';
17
+ import { IntentionalError } from '@contrast/common';
17
18
  import { default as debugMethods } from './debug-methods.mjs';
18
19
  import { default as portClient } from './post-message/loader-client.mjs';
19
20
  const require = Module.createRequire(import.meta.url);
20
- const messages = require('@contrast/core/lib/messages');
21
- const config = require('@contrast/config');
22
- const logger = require('@contrast/logger').default;
23
- const rewriter = require('@contrast/rewriter');
24
21
 
22
+ // TODO: language
23
+ const ERROR_MESSAGE = 'An error prevented the Contrast agent from initializing in the loader thread.';
25
24
 
26
- export default function init({ port, modes }) {
25
+ /**
26
+ * @param {{
27
+ * modes: string[],
28
+ * port: import('node:worker_threads').MessagePort,
29
+ * appInfo: import('@contrast/common').AppInfo,
30
+ * agentVersion: string,
31
+ * }} data
32
+ */
33
+ export default function init({ appInfo, agentVersion, port, modes }) {
27
34
  const core = {
35
+ appInfo,
36
+ agentVersion,
28
37
  // this will toggle functionality in hooks.mjs e.g. redirects/rewriting
29
38
  enable: true,
30
39
  // stub for debugMethods
@@ -46,11 +55,24 @@ export default function init({ port, modes }) {
46
55
  },
47
56
  };
48
57
 
49
- messages(core);
50
- config(core);
51
- logger(core);
52
- rewriter(core);
53
- portClient(core, { port });
58
+ try {
59
+ require('@contrast/core/lib/messages')(core);
60
+ require('@contrast/config')(core);
61
+ require('@contrast/logger').default(core);
62
+ require('@contrast/rewriter')(core);
63
+ portClient(core, { port });
64
+
65
+ return core;
66
+ } catch (err) {
67
+ core.enable = false;
54
68
 
55
- return core;
69
+ // ignore intentional errors
70
+ if (!(err instanceof IntentionalError)) {
71
+ if (core.logger) {
72
+ core.logger.error({ err }, ERROR_MESSAGE);
73
+ } else {
74
+ console.error(new Error(ERROR_MESSAGE, { cause: err }));
75
+ }
76
+ }
77
+ }
56
78
  }
@@ -13,13 +13,17 @@
13
13
  * way not consistent with the End User License Agreement.
14
14
  */
15
15
 
16
+ import { Event } from '@contrast/common';
16
17
  import { send } from './send.mjs';
17
18
 
18
19
  /**
19
20
  * Handles loader agent communication with the main agent's port client.
20
21
  * Has API to send status messages to main agent e.g. for when it installs/uninstalls.
21
22
  * Also listens for for TS settings updates and messages from main agent instructing it to disable.
22
- */
23
+ * @param {any} core
24
+ * @param {Object} opts
25
+ * @param {import('node:worker_threads').MessagePort} opts.port
26
+ */
23
27
  export default function init(core, { port }) {
24
28
  const portClient = core.esmHooks.portClient = {
25
29
  _port: port,
@@ -13,19 +13,23 @@
13
13
  * way not consistent with the End User License Agreement.
14
14
  */
15
15
 
16
+ import { Event } from '@contrast/common';
16
17
  import { send } from './send.mjs';
17
18
 
18
19
  /**
19
20
  * Handles main agent communication with the loader agent's port client.
20
21
  * Has API to forward events, and for sending RPC-like messages e.g. for instructing it to disable.
21
22
  * Will listen for status messages from the loader agent and log them.
23
+ * @param {any} core
24
+ * @param {Object} opts
25
+ * @param {import('node:worker_threads').MessagePort} opts.port
22
26
  */
23
27
  export default function init(core, { port }) {
24
28
  const portClient = core.esmHooks.portClient = {
25
29
  _port: port,
26
30
  _handlers: {
27
- status(msg) {
28
- core.logger.debug(msg, 'loader agent status report');
31
+ status(msg, data) {
32
+ core.logger.trace({ name: 'contrast:esm:loader', data }, msg);
29
33
  },
30
34
  },
31
35
  /**
@@ -45,7 +49,7 @@ export default function init(core, { port }) {
45
49
  // handling messages from loader agent
46
50
  portClient._port.on('message', (raw) => {
47
51
  if (raw?.type == 'status') {
48
- portClient._handlers.status(raw.msg);
52
+ portClient._handlers.status(raw.msg, raw.data);
49
53
  }
50
54
  });
51
55
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/esm-hooks",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "type": "module",
5
5
  "description": "Support for loading and instrumenting ECMAScript modules",
6
6
  "license": "SEE LICENSE IN LICENSE",