@contrast/rewriter 1.5.0 → 1.7.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
@@ -19,6 +19,7 @@ const fs = require('node:fs');
19
19
  const fsPromises = require('node:fs/promises');
20
20
  const os = require('node:os');
21
21
  const path = require('node:path');
22
+ const { version } = require('../package.json');
22
23
 
23
24
  /**
24
25
  * Returns the modification time of a file as a number.
@@ -47,7 +48,7 @@ module.exports.Cache = class Cache {
47
48
  this.cacheDir = path.join(
48
49
  core.config.agent.node.rewrite.cache.path,
49
50
  core.appInfo.name.replace('/', '_'),
50
- core.agentVersion,
51
+ version,
51
52
  );
52
53
  }
53
54
 
@@ -86,12 +87,12 @@ module.exports.Cache = class Cache {
86
87
  }
87
88
 
88
89
  /**
89
- * Looks up and returns the string content of a previously rewritten file
90
+ * Looks up and returns the cached filename for a previously rewritten file
90
91
  * asynchronously.
91
92
  * @param {string} filename
92
93
  * @returns {Promise<string | undefined>}
93
94
  */
94
- async read(filename) {
95
+ async find(filename) {
95
96
  const filenameCached = this.getCachedFilename(filename);
96
97
 
97
98
  try {
@@ -120,7 +121,7 @@ module.exports.Cache = class Cache {
120
121
  'Cache current.'
121
122
  );
122
123
 
123
- return fsPromises.readFile(filenameCached, 'utf8');
124
+ return filenameCached;
124
125
  } catch (err) {
125
126
  // @ts-expect-error ts treats errors poorly.
126
127
  if (err.code !== 'ENOENT') {
package/lib/index.js CHANGED
@@ -18,11 +18,18 @@
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
24
32
  * @prop {import('@contrast/common').AppInfo} appInfo
25
- * @prop {string} agentVersion
26
33
  * @prop {import('@contrast/config').Config} config
27
34
  * @prop {import('@contrast/logger').Logger} logger
28
35
  */
@@ -166,6 +173,12 @@ class Rewriter {
166
173
  result = trim(content, result);
167
174
  }
168
175
 
176
+ for (const { search, replace } of INJECTION_FIXES) {
177
+ if (result.code.indexOf(search) >= 0) {
178
+ result.code = result.code.replace(search, replace);
179
+ }
180
+ }
181
+
169
182
  return result;
170
183
  }
171
184
 
@@ -189,6 +202,12 @@ class Rewriter {
189
202
  result = trim(content, result);
190
203
  }
191
204
 
205
+ for (const { search, replace } of INJECTION_FIXES) {
206
+ if (result.code.indexOf(search) >= 0) {
207
+ result.code = result.code.replace(search, replace);
208
+ }
209
+ }
210
+
192
211
  return result;
193
212
  }
194
213
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/rewriter",
3
- "version": "1.5.0",
3
+ "version": "1.7.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"