@contrast/rewriter 1.37.1 → 1.37.2
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/index.js +53 -7
- package/package.json +5 -5
package/lib/index.js
CHANGED
|
@@ -15,15 +15,17 @@
|
|
|
15
15
|
// @ts-check
|
|
16
16
|
'use strict';
|
|
17
17
|
const { arch, platform } = require('node:os');
|
|
18
|
-
const path = require('node:path');
|
|
19
18
|
// @ts-expect-error
|
|
20
19
|
const { SourceMapConsumer: SourceMapConsumerSync } = require('@contrast/synchronous-source-maps');
|
|
21
20
|
const { parseSync, transform, transformSync } = require('@swc/core');
|
|
22
21
|
// @ts-expect-error `@contrast/agent-swc-plugin` .d.ts file doesn't exist.
|
|
23
22
|
const { defaultRewriter, defaultUnwriter } = require('@contrast/agent-swc-plugin');
|
|
24
23
|
const { funcInfo } = require('@contrast/fn-inspect');
|
|
24
|
+
const { primordials } = require('@contrast/common');
|
|
25
25
|
const { Cache } = require('./cache');
|
|
26
26
|
|
|
27
|
+
const MAGIC_COMMENT_PREFIX = '//# sourceMappingURL=';
|
|
28
|
+
|
|
27
29
|
/**
|
|
28
30
|
* @typedef {Object} Core
|
|
29
31
|
* @prop {import('@contrast/common').AppInfo} appInfo
|
|
@@ -39,7 +41,6 @@ const { Cache } = require('./cache');
|
|
|
39
41
|
* @prop {boolean=} inject if true, injects ContrastMethods on the global object
|
|
40
42
|
* @prop {boolean=} minify if true, minifies the output when source maps are enabled
|
|
41
43
|
*/
|
|
42
|
-
|
|
43
44
|
class Rewriter {
|
|
44
45
|
/**
|
|
45
46
|
* @param {Core} core
|
|
@@ -67,7 +68,7 @@ class Rewriter {
|
|
|
67
68
|
* @param {RewriteOpts} opts
|
|
68
69
|
* @returns {import('@swc/core').Options}
|
|
69
70
|
*/
|
|
70
|
-
|
|
71
|
+
swcOptions(opts) {
|
|
71
72
|
const nodeCfg = this.core.config.agent.node;
|
|
72
73
|
const sourceMaps = nodeCfg.source_maps.enable;
|
|
73
74
|
const minify = opts.minify && nodeCfg.source_maps.enable && nodeCfg.rewrite.minify;
|
|
@@ -85,7 +86,7 @@ class Rewriter {
|
|
|
85
86
|
assess: this.modes.has('assess'),
|
|
86
87
|
inject: opts.inject,
|
|
87
88
|
}]],
|
|
88
|
-
cacheRoot:
|
|
89
|
+
cacheRoot: primordials.PathJoin(nodeCfg.rewrite.cache.path, '.swc'),
|
|
89
90
|
},
|
|
90
91
|
parser: {
|
|
91
92
|
syntax: 'ecmascript',
|
|
@@ -101,6 +102,45 @@ class Rewriter {
|
|
|
101
102
|
};
|
|
102
103
|
}
|
|
103
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Make all of the source map paths absolute.
|
|
107
|
+
* Mutates Output result:
|
|
108
|
+
* - magic sourceMappingURL comment in result.code
|
|
109
|
+
* - result.map.sources array
|
|
110
|
+
* @param {RewriteOpts} rewriteOpts
|
|
111
|
+
* @param {import('@swc/core').Output} result
|
|
112
|
+
*/
|
|
113
|
+
adjustMap(rewriteOpts, result) {
|
|
114
|
+
if (rewriteOpts.filename && result.map) {
|
|
115
|
+
const map = JSON.parse(result.map);
|
|
116
|
+
const cachedFilename = this.cache.getCachedFilename(rewriteOpts.filename);
|
|
117
|
+
const absoluteCachedMapName = `${cachedFilename}.map`;
|
|
118
|
+
const origMapURL = `${primordials.PathBasename(rewriteOpts.filename)}.map`;
|
|
119
|
+
const comment = `${MAGIC_COMMENT_PREFIX}${origMapURL}`;
|
|
120
|
+
|
|
121
|
+
// 1) magic comment
|
|
122
|
+
// https://github.com/nodejs/node/blob/main/lib/internal/source_map/source_map_cache.js#L134-L136
|
|
123
|
+
const i = result.code.lastIndexOf(comment);
|
|
124
|
+
if (i >= 0) {
|
|
125
|
+
let code = primordials.StringPrototypeSubstr.call(result.code, 0, i);
|
|
126
|
+
code += `${MAGIC_COMMENT_PREFIX}${absoluteCachedMapName}`;
|
|
127
|
+
result.code = code;
|
|
128
|
+
}
|
|
129
|
+
// 2) map.sources array
|
|
130
|
+
for (let i = 0; i < map.sources?.length; i++) {
|
|
131
|
+
const resolved = primordials.PathResolve(rewriteOpts.filename, '..', map.sources[i]);
|
|
132
|
+
map.sources[i] = resolved;
|
|
133
|
+
}
|
|
134
|
+
// 3) map.file
|
|
135
|
+
if (map.file) {
|
|
136
|
+
map.file = cachedFilename;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// reasign modified map
|
|
140
|
+
result.map = primordials.JSONStringify(map);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
104
144
|
/**
|
|
105
145
|
* Rewrites the provided source code string asynchronously. this is used in an ESM
|
|
106
146
|
* context. CJS cannot use this because `require` is synchronous.
|
|
@@ -111,7 +151,10 @@ class Rewriter {
|
|
|
111
151
|
*/
|
|
112
152
|
async rewrite(content, opts = {}) {
|
|
113
153
|
this.logger.trace({ opts }, 'rewriting %s', opts.filename);
|
|
114
|
-
|
|
154
|
+
const rewriteOpts = this.swcOptions(opts);
|
|
155
|
+
const ret = await transform(content, rewriteOpts);
|
|
156
|
+
this.adjustMap(rewriteOpts, ret);
|
|
157
|
+
return ret;
|
|
115
158
|
}
|
|
116
159
|
|
|
117
160
|
/**
|
|
@@ -125,7 +168,10 @@ class Rewriter {
|
|
|
125
168
|
*/
|
|
126
169
|
rewriteSync(content, opts = {}) {
|
|
127
170
|
this.logger.trace({ opts }, 'rewriting %s', opts.filename);
|
|
128
|
-
|
|
171
|
+
const rewriteOpts = this.swcOptions(opts);
|
|
172
|
+
const result = transformSync(content, rewriteOpts);
|
|
173
|
+
this.adjustMap(rewriteOpts, result);
|
|
174
|
+
return result;
|
|
129
175
|
}
|
|
130
176
|
|
|
131
177
|
/**
|
|
@@ -180,7 +226,7 @@ class Rewriter {
|
|
|
180
226
|
column: fnInfo.column,
|
|
181
227
|
});
|
|
182
228
|
if (orig) {
|
|
183
|
-
fnInfo.file = orig.source ?
|
|
229
|
+
fnInfo.file = orig.source ? primordials.PathResolve(fnInfo.file, '..', orig.source) : fnInfo.file;
|
|
184
230
|
fnInfo.lineNumber = orig.line;
|
|
185
231
|
fnInfo.column = orig.column;
|
|
186
232
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrast/rewriter",
|
|
3
|
-
"version": "1.37.
|
|
3
|
+
"version": "1.37.2",
|
|
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)",
|
|
@@ -21,11 +21,11 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@contrast/agent-swc-plugin": "3.2.0",
|
|
24
|
-
"@contrast/common": "1.39.
|
|
25
|
-
"@contrast/config": "1.55.
|
|
26
|
-
"@contrast/core": "1.60.
|
|
24
|
+
"@contrast/common": "1.39.1",
|
|
25
|
+
"@contrast/config": "1.55.1",
|
|
26
|
+
"@contrast/core": "1.60.1",
|
|
27
27
|
"@contrast/fn-inspect": "^5.0.2",
|
|
28
|
-
"@contrast/logger": "1.33.
|
|
28
|
+
"@contrast/logger": "1.33.1",
|
|
29
29
|
"@contrast/synchronous-source-maps": "^1.1.5",
|
|
30
30
|
"@swc/core": "1.13.3"
|
|
31
31
|
}
|