@contrast/cli 1.55.0 → 1.57.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/bin/rewrite.mjs +12 -13
- package/lib/{rewrite.mjs → rewrite/index.mjs} +3 -33
- package/lib/rewrite/program.mjs +28 -0
- package/package.json +7 -7
package/bin/rewrite.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
1
|
+
#!/usr/bin/env node
|
|
2
2
|
/*
|
|
3
3
|
* Copyright: 2025 Contrast Security, Inc
|
|
4
4
|
* Contact: support@contrastsecurity.com
|
|
@@ -14,16 +14,15 @@
|
|
|
14
14
|
* way not consistent with the End User License Agreement.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
import {
|
|
18
|
-
import
|
|
19
|
-
import { action, version } from '../lib/rewrite.mjs';
|
|
17
|
+
import { execFileSync } from 'node:child_process';
|
|
18
|
+
import { fileURLToPath } from 'node:url';
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
execFileSync(
|
|
21
|
+
process.argv[0],
|
|
22
|
+
[
|
|
23
|
+
'--experimental-import-meta-resolve',
|
|
24
|
+
fileURLToPath(new URL('../lib/rewrite/program.mjs', import.meta.url)),
|
|
25
|
+
...process.argv.slice(2),
|
|
26
|
+
],
|
|
27
|
+
{ stdio: 'inherit' },
|
|
28
|
+
);
|
|
@@ -17,7 +17,6 @@ import { primordials } from '@contrast/common';
|
|
|
17
17
|
import configInit from '@contrast/config';
|
|
18
18
|
import appInfoInit from '@contrast/core/lib/app-info.js';
|
|
19
19
|
import coreMessagesInit from '@contrast/core/lib/messages.js';
|
|
20
|
-
import { findPackageJson } from '@contrast/find-package-json';
|
|
21
20
|
import loggerInit from '@contrast/logger';
|
|
22
21
|
import Perf from '@contrast/perf';
|
|
23
22
|
import rewriterInit from '@contrast/rewriter';
|
|
@@ -29,10 +28,10 @@ import { createRequire } from 'node:module';
|
|
|
29
28
|
import path from 'node:path';
|
|
30
29
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
31
30
|
|
|
32
|
-
const { RegExpPrototypeTest
|
|
31
|
+
const { RegExpPrototypeTest } = primordials;
|
|
33
32
|
const JS_FILE_REGEX = /\.[cm]?js$/;
|
|
34
33
|
|
|
35
|
-
const packageJson = JSON.parse((await readFile(new URL('
|
|
34
|
+
const packageJson = JSON.parse((await readFile(new URL('../../package.json', import.meta.url))).toString());
|
|
36
35
|
const { name: agentName, version: agentVersion } = packageJson;
|
|
37
36
|
|
|
38
37
|
/** @type {any} */
|
|
@@ -59,31 +58,6 @@ const rewriter = rewriterInit(core);
|
|
|
59
58
|
*/
|
|
60
59
|
const visited = new Set();
|
|
61
60
|
|
|
62
|
-
/** @param {string} filename absolute path */
|
|
63
|
-
async function isModuleFile(filename) {
|
|
64
|
-
// if the file extension specifies the type, there's no need to do extra IO
|
|
65
|
-
const ext = path.extname(filename);
|
|
66
|
-
if (ext === '.mjs') {
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (ext === '.cjs') {
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// check for type: 'module' in a file's package json, otherwise assume CJS.
|
|
75
|
-
try {
|
|
76
|
-
const pkg = await findPackageJson({ cwd: filename });
|
|
77
|
-
if (pkg && JSONParse((await readFile(pkg)).toString()).type === 'module') {
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
} catch {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
61
|
class RewriteVisitor extends Visitor {
|
|
88
62
|
/** @param {string} filename */
|
|
89
63
|
constructor(filename) {
|
|
@@ -172,20 +146,16 @@ async function rewriteFile(filename) {
|
|
|
172
146
|
|
|
173
147
|
try {
|
|
174
148
|
const content = (await readFile(filename)).toString();
|
|
175
|
-
const isModule = await isModuleFile(filename);
|
|
176
149
|
|
|
177
150
|
const result = await rewriter.rewrite(content, {
|
|
178
151
|
filename,
|
|
179
|
-
isModule,
|
|
180
152
|
inject: true,
|
|
181
|
-
wrap: !isModule,
|
|
182
153
|
minify: true,
|
|
183
154
|
});
|
|
184
155
|
rewriter.cache.write(filename, result);
|
|
185
156
|
|
|
186
157
|
/** @type {swc.Module | swc.Script} */
|
|
187
|
-
|
|
188
|
-
const program = await swc.parse(content, { isModule });
|
|
158
|
+
const program = await swc.parse(content);
|
|
189
159
|
const visitor = new RewriteVisitor(filename);
|
|
190
160
|
visitor.visitProgram(program);
|
|
191
161
|
} catch (err) {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2025 Contrast Security, Inc
|
|
3
|
+
* Contact: support@contrastsecurity.com
|
|
4
|
+
* License: Commercial
|
|
5
|
+
|
|
6
|
+
* NOTICE: This Software and the patented inventions embodied within may only be
|
|
7
|
+
* used as part of Contrast Security’s commercial offerings. Even though it is
|
|
8
|
+
* made available through public repositories, use of this Software is subject to
|
|
9
|
+
* the applicable End User Licensing Agreement found at
|
|
10
|
+
* https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
|
|
11
|
+
* between Contrast Security and the End User. The Software may not be reverse
|
|
12
|
+
* engineered, modified, repackaged, sold, redistributed or otherwise used in a
|
|
13
|
+
* way not consistent with the End User License Agreement.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { program } from 'commander';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
import { action, version } from './index.mjs';
|
|
19
|
+
|
|
20
|
+
program
|
|
21
|
+
.name('npx -p @contrast/cli rewrite')
|
|
22
|
+
.version(version)
|
|
23
|
+
.description('Rewrites application files, caching them so that rewriting does not need to occur when the application runs.')
|
|
24
|
+
.argument('<entrypoint>', 'The entrypoint for the application', entrypoint => path.resolve(entrypoint))
|
|
25
|
+
.option('-a, --assess', 'rewrite in assess mode')
|
|
26
|
+
.option('-p, --protect', 'rewrite in protect mode')
|
|
27
|
+
.action(action)
|
|
28
|
+
.parse(process.argv);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrast/cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.57.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A collection of agent related CLI utilities",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE",
|
|
@@ -26,14 +26,14 @@
|
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@contrast/find-package-json": "^1.1.0",
|
|
29
|
-
"@contrast/rewriter": "1.
|
|
29
|
+
"@contrast/rewriter": "1.35.0",
|
|
30
30
|
"@contrast/common": "1.37.0",
|
|
31
|
-
"@contrast/config": "1.
|
|
32
|
-
"@contrast/core": "1.
|
|
33
|
-
"@contrast/logger": "1.
|
|
31
|
+
"@contrast/config": "1.53.0",
|
|
32
|
+
"@contrast/core": "1.58.0",
|
|
33
|
+
"@contrast/logger": "1.31.0",
|
|
34
34
|
"@contrast/perf": "1.4.0",
|
|
35
|
-
"@contrast/reporter": "1.
|
|
36
|
-
"@contrast/scopes": "1.
|
|
35
|
+
"@contrast/reporter": "1.56.0",
|
|
36
|
+
"@contrast/scopes": "1.28.0",
|
|
37
37
|
"@swc/core": "1.13.3",
|
|
38
38
|
"commander": "^9.4.1"
|
|
39
39
|
}
|