Skip to content

Commit d17c784

Browse files
authored
refactor(js-x-ray): remove is-minified-code from dependencies and re-implement it in utils (#415)
1 parent a98fe96 commit d17c784

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

.changeset/two-points-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/js-x-ray": minor
3+
---
4+
5+
Remove is-minified-code from dependencies and re-implement function in utils

workspaces/js-x-ray/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"@nodesecure/tracer": "^3.0.0",
5151
"digraph-js": "2.2.4",
5252
"frequency-set": "^2.1.0",
53-
"is-minified-code": "^2.0.0",
5453
"meriyah": "^6.0.0",
5554
"safe-regex": "^2.1.1",
5655
"ts-pattern": "^5.0.6"

workspaces/js-x-ray/src/AstAnalyser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import path from "node:path";
55

66
// Import Third-party Dependencies
77
import type { ESTree } from "meriyah";
8-
import isMinified from "is-minified-code";
98

109
// Import Internal Dependencies
1110
import {
@@ -22,7 +21,8 @@ import { ProbeRunner, type Probe } from "./ProbeRunner.js";
2221
import { walkEnter } from "./walker/index.js";
2322
import * as trojan from "./obfuscators/trojan-source.js";
2423
import {
25-
isOneLineExpressionExport
24+
isOneLineExpressionExport,
25+
isMinifiedCode
2626
} from "./utils/index.js";
2727
import {
2828
PipelineRunner,
@@ -221,7 +221,7 @@ export class AstAnalyser {
221221
const str = await fs.readFile(pathToFile, "utf-8");
222222
const filePathString = pathToFile instanceof URL ? pathToFile.href : pathToFile;
223223

224-
const isMin = filePathString.includes(".min") || isMinified(str);
224+
const isMin = filePathString.includes(".min") || isMinifiedCode(str);
225225
const data = this.analyse(str, {
226226
isMinified: isMin,
227227
module: path.extname(filePathString) === ".mjs" ? true : module,
@@ -274,7 +274,7 @@ export class AstAnalyser {
274274
const str = fsSync.readFileSync(pathToFile, "utf-8");
275275
const filePathString = pathToFile instanceof URL ? pathToFile.href : pathToFile;
276276

277-
const isMin = filePathString.includes(".min") || isMinified(str);
277+
const isMin = filePathString.includes(".min") || isMinifiedCode(str);
278278
const data = this.analyse(str, {
279279
isMinified: isMin,
280280
module: path.extname(filePathString) === ".mjs" ? true : module,

workspaces/js-x-ray/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from "./extractNode.js";
22
export * from "./isOneLineExpressionExport.js";
33
export * from "./notNullOrUndefined.js";
44
export * from "./toArrayLocation.js";
5+
export * from "./isMinifiedCode.js";
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// CONSTANTS
2+
const kCommentPattern = /\/\*[\s\S]*?\*\/\r?\n?|\/\/.{0,200}?(?:\r?\n|$)/g;
3+
const kTrailingLfPattern = /\r?\n$/;
4+
5+
/**
6+
* This code has been imported from:
7+
* https://github.com/MartinKolarik/is-minified-code
8+
*/
9+
export function isMinifiedCode(
10+
code: string
11+
): boolean {
12+
const lines = code
13+
.replace(kCommentPattern, "")
14+
.replace(kTrailingLfPattern, "")
15+
.split("\n")
16+
.flatMap((line) => (line.length > 0 ? [line.length] : []));
17+
18+
return lines.length <= 1 || median(lines) > 200;
19+
}
20+
21+
function median(
22+
values: number[]
23+
): number {
24+
const toSorted = [...values].sort((a, b) => a - b);
25+
const half = Math.floor(toSorted.length / 2);
26+
27+
if (toSorted.length % 2) {
28+
return toSorted[half];
29+
}
30+
31+
return (toSorted[half - 1] + toSorted[half]) / 2;
32+
}

0 commit comments

Comments
 (0)