@contrast/rewriter 1.37.0 → 1.37.1

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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright: 2025 Contrast Security, Inc
1
+ Copyright: 2026 Contrast Security, Inc
2
2
  Contact: support@contrastsecurity.com
3
3
  License: Commercial
4
4
 
package/lib/cache.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -142,7 +142,7 @@ module.exports.Cache = class Cache {
142
142
  }
143
143
 
144
144
  /**
145
- * Looks up and returns the source map for a previously rewritten file.
145
+ * Asynchronously looks up and returns the source map for a previously rewritten file.
146
146
  * @param {string} filename
147
147
  * @returns {Promise<string | undefined>}
148
148
  */
@@ -164,6 +164,29 @@ module.exports.Cache = class Cache {
164
164
  }
165
165
  }
166
166
 
167
+
168
+ /**
169
+ * Synchronously looks up and returns the source map for a previously rewritten file.
170
+ * @param {string} filename
171
+ * @returns {string | undefined}
172
+ */
173
+ readMapSync(filename) {
174
+ const filenameCached = this.getCachedFilename(filename);
175
+ const sourceMap = `${filenameCached}.map`;
176
+ try {
177
+ return fs.readFileSync(sourceMap, 'utf8');
178
+ } catch (err) {
179
+ // @ts-expect-error ts treats errors poorly.
180
+ if (err.code !== 'ENOENT') {
181
+ this.logger.warn(
182
+ { err, filename, filenameCached, sourceMap },
183
+ 'An unexpected error occurred finding source map.'
184
+ );
185
+ }
186
+ return undefined;
187
+ }
188
+ }
189
+
167
190
  /**
168
191
  * Looks up and returns the string content of a previously rewritten file
169
192
  * synchronously. Used when we need to block on cache lookups.
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -14,10 +14,14 @@
14
14
  */
15
15
  // @ts-check
16
16
  'use strict';
17
-
18
17
  const { arch, platform } = require('node:os');
19
18
  const path = require('node:path');
19
+ // @ts-expect-error
20
+ const { SourceMapConsumer: SourceMapConsumerSync } = require('@contrast/synchronous-source-maps');
20
21
  const { parseSync, transform, transformSync } = require('@swc/core');
22
+ // @ts-expect-error `@contrast/agent-swc-plugin` .d.ts file doesn't exist.
23
+ const { defaultRewriter, defaultUnwriter } = require('@contrast/agent-swc-plugin');
24
+ const { funcInfo } = require('@contrast/fn-inspect');
21
25
  const { Cache } = require('./cache');
22
26
 
23
27
  /**
@@ -36,9 +40,6 @@ const { Cache } = require('./cache');
36
40
  * @prop {boolean=} minify if true, minifies the output when source maps are enabled
37
41
  */
38
42
 
39
- // @ts-expect-error `@contrast/agent-swc-plugin` .d.ts file doesn't exist.
40
- const { defaultRewriter, defaultUnwriter } = require('@contrast/agent-swc-plugin');
41
-
42
43
  class Rewriter {
43
44
  /**
44
45
  * @param {Core} core
@@ -70,7 +71,6 @@ class Rewriter {
70
71
  const nodeCfg = this.core.config.agent.node;
71
72
  const sourceMaps = nodeCfg.source_maps.enable;
72
73
  const minify = opts.minify && nodeCfg.source_maps.enable && nodeCfg.rewrite.minify;
73
-
74
74
  return {
75
75
  swcrc: false,
76
76
  filename: opts.filename,
@@ -149,6 +149,44 @@ class Rewriter {
149
149
  sourceMaps: false,
150
150
  }).code;
151
151
  }
152
+
153
+ /**
154
+ * TODO implement for async scenarios using newer versions of source-map
155
+ * @param {function} func
156
+ */
157
+ async funcInfo(func) {
158
+ throw new Error('funcInfo is not implemented');
159
+ }
160
+
161
+ /**
162
+ * @param {function} func
163
+ */
164
+ funcInfoSync(func) {
165
+ /** @type any */
166
+ const fnInfo = funcInfo(func);
167
+
168
+ if (
169
+ !fnInfo?.file ||
170
+ !Number.isFinite(fnInfo.lineNumber) ||
171
+ !Number.isFinite(fnInfo.column) ||
172
+ !this.core.config.agent.node.source_maps.enable
173
+ ) return fnInfo;
174
+
175
+ const map = this.cache.readMapSync(fnInfo.file);
176
+ if (!map) return fnInfo;
177
+
178
+ const orig = new SourceMapConsumerSync(map).originalPositionFor({
179
+ line: fnInfo.lineNumber + 1,
180
+ column: fnInfo.column,
181
+ });
182
+ if (orig) {
183
+ fnInfo.file = orig.source ? path.resolve(fnInfo.file, '..', orig.source) : fnInfo.file;
184
+ fnInfo.lineNumber = orig.line;
185
+ fnInfo.column = orig.column;
186
+ }
187
+
188
+ return fnInfo;
189
+ }
152
190
  }
153
191
 
154
192
  /**
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2025 Contrast Security, Inc
2
+ * Copyright: 2026 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/rewriter",
3
- "version": "1.37.0",
3
+ "version": "1.37.1",
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)",
@@ -24,7 +24,9 @@
24
24
  "@contrast/common": "1.39.0",
25
25
  "@contrast/config": "1.55.0",
26
26
  "@contrast/core": "1.60.0",
27
+ "@contrast/fn-inspect": "^5.0.2",
27
28
  "@contrast/logger": "1.33.0",
29
+ "@contrast/synchronous-source-maps": "^1.1.5",
28
30
  "@swc/core": "1.13.3"
29
31
  }
30
32
  }