@contrast/rewriter 1.26.0 → 1.28.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/index.js +23 -13
- package/package.json +4 -4
package/lib/index.js
CHANGED
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
// @ts-check
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
|
-
|
|
19
18
|
const Module = require('node:module');
|
|
20
19
|
const { arch, platform } = require('node:os');
|
|
21
20
|
const path = require('node:path');
|
|
@@ -38,11 +37,12 @@ const { Cache } = require('./cache');
|
|
|
38
37
|
* @prop {boolean=} isModule if true, file is parsed as an ES module instead of a CJS script
|
|
39
38
|
* @prop {boolean=} inject if true, injects ContrastMethods on the global object
|
|
40
39
|
* @prop {boolean=} wrap if true, wraps the content with a modified module wrapper IIFE
|
|
40
|
+
* @prop {boolean=} minify if true, minifies the output when source maps are enabled
|
|
41
41
|
*/
|
|
42
42
|
|
|
43
|
-
// @ts-expect-error `wrapper` is missing from @types/node
|
|
43
|
+
// @ts-expect-error `wrapper` is missing from @types/node.
|
|
44
44
|
const prefix = Module.wrapper[0];
|
|
45
|
-
// @ts-expect-error `wrapper` is missing from @types/node
|
|
45
|
+
// @ts-expect-error `wrapper` is missing from @types/node, primordials overloads are poorly handled.
|
|
46
46
|
const suffix = StringPrototypeReplace.call(Module.wrapper[1], /;$/, '.apply(this, arguments);');
|
|
47
47
|
|
|
48
48
|
// @ts-expect-error `@contrast/agent-swc-plugin` .d.ts file doesn't exist.
|
|
@@ -99,14 +99,17 @@ class Rewriter {
|
|
|
99
99
|
* @returns {import('@swc/core').Options}
|
|
100
100
|
*/
|
|
101
101
|
rewriteConfig(opts) {
|
|
102
|
+
const nodeCfg = this.core.config.agent.node;
|
|
103
|
+
const minify = opts.minify && nodeCfg.source_maps.enable && nodeCfg.rewrite.minify;
|
|
104
|
+
|
|
102
105
|
return {
|
|
103
106
|
swcrc: false,
|
|
104
107
|
filename: opts.filename,
|
|
105
108
|
isModule: opts.isModule,
|
|
106
109
|
env: {
|
|
107
110
|
targets: {
|
|
108
|
-
node: process.versions.node
|
|
109
|
-
}
|
|
111
|
+
node: process.versions.node,
|
|
112
|
+
},
|
|
110
113
|
},
|
|
111
114
|
jsc: {
|
|
112
115
|
experimental: {
|
|
@@ -114,10 +117,14 @@ class Rewriter {
|
|
|
114
117
|
assess: this.modes.has('assess'),
|
|
115
118
|
inject: opts.inject,
|
|
116
119
|
}]],
|
|
117
|
-
cacheRoot: path.join(
|
|
120
|
+
cacheRoot: path.join(nodeCfg.rewrite.cache.path, '.swc'),
|
|
118
121
|
},
|
|
122
|
+
// disable most of the more invasive options.
|
|
123
|
+
// see: https://terser.org/docs/options/#compress-options
|
|
124
|
+
minify: minify ? { compress: { defaults: false } } : undefined
|
|
119
125
|
},
|
|
120
|
-
|
|
126
|
+
minify,
|
|
127
|
+
sourceMaps: nodeCfg.source_maps.enable,
|
|
121
128
|
};
|
|
122
129
|
}
|
|
123
130
|
|
|
@@ -168,8 +175,8 @@ class Rewriter {
|
|
|
168
175
|
swcrc: false,
|
|
169
176
|
env: {
|
|
170
177
|
targets: {
|
|
171
|
-
node: process.versions.node
|
|
172
|
-
}
|
|
178
|
+
node: process.versions.node,
|
|
179
|
+
},
|
|
173
180
|
},
|
|
174
181
|
jsc: {
|
|
175
182
|
experimental: {
|
|
@@ -191,10 +198,13 @@ module.exports = function init(core) {
|
|
|
191
198
|
try {
|
|
192
199
|
parseSync('');
|
|
193
200
|
} catch (cause) {
|
|
194
|
-
// @ts-expect-error TS hates errors
|
|
195
|
-
throw
|
|
196
|
-
|
|
197
|
-
|
|
201
|
+
// @ts-expect-error TS hates errors.
|
|
202
|
+
throw cause.message === 'Bindings not found.'
|
|
203
|
+
? new Error(
|
|
204
|
+
`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.`,
|
|
205
|
+
// @ts-expect-error `cause` requires ts to target es2022 or above, which corresponds to Node 17+, despite being added to Node in 16.9.
|
|
206
|
+
{ cause },
|
|
207
|
+
)
|
|
198
208
|
: cause;
|
|
199
209
|
}
|
|
200
210
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrast/rewriter",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.28.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)",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@contrast/agent-swc-plugin": "^2.0.0",
|
|
24
24
|
"@contrast/common": "1.32.0",
|
|
25
|
-
"@contrast/config": "1.
|
|
26
|
-
"@contrast/core": "1.
|
|
27
|
-
"@contrast/logger": "1.
|
|
25
|
+
"@contrast/config": "1.47.0",
|
|
26
|
+
"@contrast/core": "1.52.0",
|
|
27
|
+
"@contrast/logger": "1.25.0",
|
|
28
28
|
"@swc/core": "1.5.29"
|
|
29
29
|
}
|
|
30
30
|
}
|