@contrast/rewriter 1.5.0 → 1.6.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/cache.js CHANGED
@@ -86,12 +86,12 @@ module.exports.Cache = class Cache {
86
86
  }
87
87
 
88
88
  /**
89
- * Looks up and returns the string content of a previously rewritten file
89
+ * Looks up and returns the cached filename for a previously rewritten file
90
90
  * asynchronously.
91
91
  * @param {string} filename
92
92
  * @returns {Promise<string | undefined>}
93
93
  */
94
- async read(filename) {
94
+ async find(filename) {
95
95
  const filenameCached = this.getCachedFilename(filename);
96
96
 
97
97
  try {
@@ -120,7 +120,7 @@ module.exports.Cache = class Cache {
120
120
  'Cache current.'
121
121
  );
122
122
 
123
- return fsPromises.readFile(filenameCached, 'utf8');
123
+ return filenameCached;
124
124
  } catch (err) {
125
125
  // @ts-expect-error ts treats errors poorly.
126
126
  if (err.code !== 'ENOENT') {
package/lib/index.js CHANGED
@@ -18,6 +18,14 @@
18
18
  const { transform, transformSync } = require('@swc/core');
19
19
  const Module = require('node:module');
20
20
  const { Cache } = require('./cache');
21
+ const INJECTION_FIXES = [
22
+ // assigning to global breaks things like `"string".constructor === global.String`
23
+ { search: 'global.Function = global.ContrastMethods.Function', replace: 'var Function = global.ContrastMethods?.Function' },
24
+ { search: 'global.Number = global.ContrastMethods.Number', replace: 'var Number = global.ContrastMethods?.Number' },
25
+ { search: 'global.Object = global.ContrastMethods.Object', replace: 'var Object = global.ContrastMethods?.Object' },
26
+ { search: 'global.String = global.ContrastMethods.String', replace: 'var String = global.ContrastMethods?.String' },
27
+ ];
28
+
21
29
 
22
30
  /**
23
31
  * @typedef {Object} Core
@@ -166,6 +174,12 @@ class Rewriter {
166
174
  result = trim(content, result);
167
175
  }
168
176
 
177
+ for (const { search, replace } of INJECTION_FIXES) {
178
+ if (result.code.indexOf(search) >= 0) {
179
+ result.code = result.code.replace(search, replace);
180
+ }
181
+ }
182
+
169
183
  return result;
170
184
  }
171
185
 
@@ -189,6 +203,12 @@ class Rewriter {
189
203
  result = trim(content, result);
190
204
  }
191
205
 
206
+ for (const { search, replace } of INJECTION_FIXES) {
207
+ if (result.code.indexOf(search) >= 0) {
208
+ result.code = result.code.replace(search, replace);
209
+ }
210
+ }
211
+
192
212
  return result;
193
213
  }
194
214
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/rewriter",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
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)",
@@ -14,9 +14,9 @@
14
14
  "test": "../scripts/test.sh"
15
15
  },
16
16
  "dependencies": {
17
- "@contrast/agent-swc-plugin": "^1.3.0",
18
- "@contrast/agent-swc-plugin-unwrite": "^1.3.0",
19
- "@contrast/common": "^1.16.0",
17
+ "@contrast/agent-swc-plugin": "1.3.0",
18
+ "@contrast/agent-swc-plugin-unwrite": "1.3.0",
19
+ "@contrast/common": "1.20.0",
20
20
  "@contrast/synchronous-source-maps": "^1.1.3",
21
21
  "@swc/core": "1.3.39",
22
22
  "multi-stage-sourcemap": "^0.3.1"