@contrast/rewriter 1.32.0 → 1.34.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 +5 -43
- package/package.json +5 -5
package/lib/index.js
CHANGED
|
@@ -15,11 +15,9 @@
|
|
|
15
15
|
// @ts-check
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
|
-
const Module = require('node:module');
|
|
19
18
|
const { arch, platform } = require('node:os');
|
|
20
19
|
const path = require('node:path');
|
|
21
20
|
const { parseSync, transform, transformSync } = require('@swc/core');
|
|
22
|
-
const { primordials: { StringPrototypeReplace, StringPrototypeSubstring } } = require('@contrast/common');
|
|
23
21
|
const { Cache } = require('./cache');
|
|
24
22
|
|
|
25
23
|
/**
|
|
@@ -34,43 +32,13 @@ const { Cache } = require('./cache');
|
|
|
34
32
|
/**
|
|
35
33
|
* @typedef {Object} RewriteOpts
|
|
36
34
|
* @prop {string=} filename e.g. 'index.js'
|
|
37
|
-
* @prop {boolean=} isModule if true, file is parsed as an ES module instead of a CJS script
|
|
38
35
|
* @prop {boolean=} inject if true, injects ContrastMethods on the global object
|
|
39
|
-
* @prop {boolean=} wrap if true, wraps the content with a modified module wrapper IIFE
|
|
40
36
|
* @prop {boolean=} minify if true, minifies the output when source maps are enabled
|
|
41
37
|
*/
|
|
42
38
|
|
|
43
|
-
// @ts-expect-error `wrapper` is missing from @types/node.
|
|
44
|
-
const prefix = Module.wrapper[0];
|
|
45
|
-
// @ts-expect-error `wrapper` is missing from @types/node, primordials overloads are poorly handled.
|
|
46
|
-
const suffix = StringPrototypeReplace.call(Module.wrapper[1], /;$/, '.apply(this, arguments);');
|
|
47
|
-
|
|
48
39
|
// @ts-expect-error `@contrast/agent-swc-plugin` .d.ts file doesn't exist.
|
|
49
40
|
const { defaultRewriter, defaultUnwriter } = require('@contrast/agent-swc-plugin');
|
|
50
41
|
|
|
51
|
-
/**
|
|
52
|
-
* Wraps the source content as necessary to support rewriting.
|
|
53
|
-
* Wrapping must occur before rewriting since the underlying rewriter cannot
|
|
54
|
-
* parse certain valid statements such as `return` statements in a CJS script.
|
|
55
|
-
* @param {string} content
|
|
56
|
-
* @returns {string}
|
|
57
|
-
*/
|
|
58
|
-
const wrap = (content) => {
|
|
59
|
-
let shebang = '';
|
|
60
|
-
|
|
61
|
-
// The shebang will be commented out since it cannot be present in a
|
|
62
|
-
// function body. swc doesn't include the commented shebang in the generated
|
|
63
|
-
// code despite including comments otherwise.
|
|
64
|
-
if (content.charAt(0) === '#') {
|
|
65
|
-
shebang = StringPrototypeSubstring.call(content, 0, content.indexOf('\n') + 1);
|
|
66
|
-
content = `//${content}`;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
content = `${shebang}${prefix}${content}${suffix}`;
|
|
70
|
-
|
|
71
|
-
return content;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
42
|
class Rewriter {
|
|
75
43
|
/**
|
|
76
44
|
* @param {Core} core
|
|
@@ -106,7 +74,6 @@ class Rewriter {
|
|
|
106
74
|
return {
|
|
107
75
|
swcrc: false,
|
|
108
76
|
filename: opts.filename,
|
|
109
|
-
isModule: opts.isModule,
|
|
110
77
|
env: {
|
|
111
78
|
targets: {
|
|
112
79
|
node: process.versions.node,
|
|
@@ -120,6 +87,11 @@ class Rewriter {
|
|
|
120
87
|
}]],
|
|
121
88
|
cacheRoot: path.join(nodeCfg.rewrite.cache.path, '.swc'),
|
|
122
89
|
},
|
|
90
|
+
parser: {
|
|
91
|
+
syntax: 'ecmascript',
|
|
92
|
+
// prevents the parser from throwing without wrapping when encountering `return`.
|
|
93
|
+
allowReturnOutsideFunction: true,
|
|
94
|
+
},
|
|
123
95
|
// disable most of the more invasive options.
|
|
124
96
|
// see: https://terser.org/docs/options/#compress-options
|
|
125
97
|
minify: minify ? { compress: { defaults: false } } : undefined
|
|
@@ -139,11 +111,6 @@ class Rewriter {
|
|
|
139
111
|
*/
|
|
140
112
|
async rewrite(content, opts = {}) {
|
|
141
113
|
this.logger.trace({ opts }, 'rewriting %s', opts.filename);
|
|
142
|
-
|
|
143
|
-
if (opts.wrap) {
|
|
144
|
-
content = wrap(content);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
114
|
return transform(content, this.rewriteConfig(opts));
|
|
148
115
|
}
|
|
149
116
|
|
|
@@ -158,11 +125,6 @@ class Rewriter {
|
|
|
158
125
|
*/
|
|
159
126
|
rewriteSync(content, opts = {}) {
|
|
160
127
|
this.logger.trace({ opts }, 'rewriting %s', opts.filename);
|
|
161
|
-
|
|
162
|
-
if (opts.wrap) {
|
|
163
|
-
content = wrap(content);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
128
|
return transformSync(content, this.rewriteConfig(opts));
|
|
167
129
|
}
|
|
168
130
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrast/rewriter",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.34.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)",
|
|
@@ -21,10 +21,10 @@
|
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@contrast/agent-swc-plugin": "3.2.0",
|
|
24
|
-
"@contrast/common": "1.
|
|
25
|
-
"@contrast/config": "1.
|
|
26
|
-
"@contrast/core": "1.
|
|
27
|
-
"@contrast/logger": "1.
|
|
24
|
+
"@contrast/common": "1.37.0",
|
|
25
|
+
"@contrast/config": "1.52.1",
|
|
26
|
+
"@contrast/core": "1.57.1",
|
|
27
|
+
"@contrast/logger": "1.30.1",
|
|
28
28
|
"@swc/core": "1.13.3"
|
|
29
29
|
}
|
|
30
30
|
}
|