@contrast/rewriter 1.21.3 → 1.21.4

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.
Files changed (2) hide show
  1. package/lib/index.js +16 -2
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -15,11 +15,13 @@
15
15
  // @ts-check
16
16
  'use strict';
17
17
 
18
+
18
19
  const Module = require('node:module');
20
+ const { arch, platform } = require('node:os');
19
21
  const path = require('node:path');
20
- const { transform, transformSync } = require('@swc/core');
21
- const { Cache } = require('./cache');
22
+ const { parseSync, transform, transformSync } = require('@swc/core');
22
23
  const { primordials: { StringPrototypeReplace, StringPrototypeSubstring } } = require('@contrast/common');
24
+ const { Cache } = require('./cache');
23
25
 
24
26
  /**
25
27
  * @typedef {Object} Core
@@ -184,6 +186,18 @@ class Rewriter {
184
186
  * @returns {Rewriter}
185
187
  */
186
188
  module.exports = function init(core) {
189
+ // Check that the correct bindings for `swc` are installed, throwing and
190
+ // stopping instrumentation if not.
191
+ try {
192
+ parseSync('');
193
+ } catch (cause) {
194
+ // @ts-expect-error TS hates errors
195
+ throw (cause.message === 'Bindings not found.') ?
196
+ // @ts-expect-error `cause` requires ts to target es2022 or above, which corresponds to Node 17+, despite being added to Node in 16.9
197
+ new Error(`Contrast cannot detect the correct precompiled dependencies for the current environment: ${platform()}-${arch()}. This typically occurs when deploying an installation from one environment to a different execution environment.`, { cause })
198
+ : cause;
199
+ }
200
+
187
201
  core.rewriter = new Rewriter(core);
188
202
  return core.rewriter;
189
203
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/rewriter",
3
- "version": "1.21.3",
3
+ "version": "1.21.4",
4
4
  "description": "A transpilation tool mainly used for instrumentation",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",