@nodesecure/js-x-ray 7.2.0 → 7.3.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/README.md CHANGED
@@ -307,7 +307,7 @@ $ yarn add @nodesecure/estree-ast-util
307
307
  ## Contributors ✨
308
308
 
309
309
  <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
310
- [![All Contributors](https://img.shields.io/badge/all_contributors-17-orange.svg?style=flat-square)](#contributors-)
310
+ [![All Contributors](https://img.shields.io/badge/all_contributors-18-orange.svg?style=flat-square)](#contributors-)
311
311
  <!-- ALL-CONTRIBUTORS-BADGE:END -->
312
312
 
313
313
  Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
@@ -339,6 +339,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
339
339
  <td align="center" valign="top" width="14.28%"><a href="https://github.com/FredGuiou"><img src="https://avatars.githubusercontent.com/u/99122562?v=4?s=100" width="100px;" alt="FredGuiou"/><br /><sub><b>FredGuiou</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=FredGuiou" title="Documentation">📖</a> <a href="https://github.com/NodeSecure/js-x-ray/commits?author=FredGuiou" title="Code">💻</a></td>
340
340
  <td align="center" valign="top" width="14.28%"><a href="https://github.com/madina0801"><img src="https://avatars.githubusercontent.com/u/101329759?v=4?s=100" width="100px;" alt="Madina"/><br /><sub><b>Madina</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=madina0801" title="Code">💻</a></td>
341
341
  <td align="center" valign="top" width="14.28%"><a href="https://github.com/sairuss7"><img src="https://avatars.githubusercontent.com/u/87803528?v=4?s=100" width="100px;" alt="SairussDev"/><br /><sub><b>SairussDev</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=sairuss7" title="Code">💻</a></td>
342
+ <td align="center" valign="top" width="14.28%"><a href="https://github.com/fless-lab"><img src="https://avatars.githubusercontent.com/u/71844440?v=4?s=100" width="100px;" alt="Abdou-Raouf ATARMLA"/><br /><sub><b>Abdou-Raouf ATARMLA</b></sub></a><br /><a href="https://github.com/NodeSecure/js-x-ray/commits?author=fless-lab" title="Code">💻</a></td>
342
343
  </tr>
343
344
  </tbody>
344
345
  </table>
package/index.js CHANGED
@@ -8,6 +8,14 @@ function runASTAnalysis(
8
8
  str,
9
9
  options = Object.create(null)
10
10
  ) {
11
+ process.emitWarning(
12
+ 'The runASTAnalysis API is deprecated and will be removed in v8. Please use the AstAnalyser class instead.',
13
+ {
14
+ code: 'DeprecationWarning',
15
+ detail: 'The runASTAnalysis API is deprecated and will be removed in v8. Please use the AstAnalyser class instead.'
16
+ }
17
+ );
18
+
11
19
  const {
12
20
  customParser = new JsSourceParser(),
13
21
  customProbes = [],
@@ -28,6 +36,14 @@ async function runASTAnalysisOnFile(
28
36
  pathToFile,
29
37
  options = {}
30
38
  ) {
39
+ process.emitWarning(
40
+ 'The runASTAnalysisOnFile API is deprecated and will be removed in v8. Please use the AstAnalyser class instead.',
41
+ {
42
+ code: 'DeprecationWarning',
43
+ detail: 'The runASTAnalysisOnFile API is deprecated and will be removed in v8. Please use the AstAnalyser class instead.'
44
+ }
45
+ );
46
+
31
47
  const {
32
48
  customProbes = [],
33
49
  customParser = new JsSourceParser(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nodesecure/js-x-ray",
3
- "version": "7.2.0",
3
+ "version": "7.3.0",
4
4
  "description": "JavaScript AST XRay analysis",
5
5
  "type": "module",
6
6
  "exports": "./index.js",
@@ -56,10 +56,10 @@
56
56
  "devDependencies": {
57
57
  "@nodesecure/eslint-config": "^1.6.0",
58
58
  "@types/node": "^20.6.2",
59
- "c8": "^9.0.0",
59
+ "c8": "^10.1.2",
60
60
  "cross-env": "^7.0.3",
61
61
  "eslint": "^9.0.0",
62
- "glob": "^10.3.4",
62
+ "glob": "^11.0.0",
63
63
  "iterator-matcher": "^2.1.0",
64
64
  "pkg-ok": "^3.0.0"
65
65
  }
@@ -1,5 +1,6 @@
1
1
  // Import Node.js Dependencies
2
2
  import fs from "node:fs/promises";
3
+ import fsSync from "node:fs";
3
4
  import path from "node:path";
4
5
 
5
6
  // Import Third-party Dependencies
@@ -127,6 +128,52 @@ export class AstAnalyser {
127
128
  }
128
129
  }
129
130
 
131
+ analyseFileSync(
132
+ pathToFile,
133
+ options = {}
134
+ ) {
135
+ try {
136
+ const {
137
+ packageName = null,
138
+ module = true,
139
+ removeHTMLComments = false,
140
+ initialize,
141
+ finalize
142
+ } = options;
143
+
144
+ const str = fsSync.readFileSync(pathToFile, "utf-8");
145
+ const filePathString = pathToFile instanceof URL ? pathToFile.href : pathToFile;
146
+
147
+ const isMin = filePathString.includes(".min") || isMinified(str);
148
+ const data = this.analyse(str, {
149
+ isMinified: isMin,
150
+ module: path.extname(filePathString) === ".mjs" ? true : module,
151
+ removeHTMLComments,
152
+ initialize,
153
+ finalize
154
+ });
155
+
156
+ if (packageName !== null) {
157
+ data.dependencies.delete(packageName);
158
+ }
159
+
160
+ return {
161
+ ok: true,
162
+ dependencies: data.dependencies,
163
+ warnings: data.warnings,
164
+ isMinified: !data.isOneLineRequire && isMin
165
+ };
166
+ }
167
+ catch (error) {
168
+ return {
169
+ ok: false,
170
+ warnings: [
171
+ { kind: "parsing-error", value: error.message, location: [[0, 0], [0, 0]] }
172
+ ]
173
+ };
174
+ }
175
+ }
176
+
130
177
  /**
131
178
  * @param {!string} source
132
179
  * @param {object} options
@@ -2,17 +2,23 @@
2
2
  import { exportAssignmentHasRequireLeave } from "./exportAssignmentHasRequireLeave.js";
3
3
 
4
4
  export function isOneLineExpressionExport(body) {
5
- if (body.length > 1) {
5
+ if (body.length === 0 || body.length > 1) {
6
6
  return false;
7
7
  }
8
8
 
9
- if (body[0].type !== "ExpressionStatement") {
9
+ const [firstNode] = body;
10
+ if (firstNode.type !== "ExpressionStatement") {
10
11
  return false;
11
12
  }
12
13
 
13
- if (body[0].expression.type !== "AssignmentExpression") {
14
- return false;
14
+ switch (firstNode.expression.type) {
15
+ // module.exports = require('...');
16
+ case "AssignmentExpression":
17
+ return exportAssignmentHasRequireLeave(firstNode.expression.right);
18
+ // require('...');
19
+ case "CallExpression":
20
+ return exportAssignmentHasRequireLeave(firstNode.expression);
21
+ default:
22
+ return false;
15
23
  }
16
-
17
- return exportAssignmentHasRequireLeave(body[0].expression.right);
18
24
  }