diff --git a/.changeset/two-points-share.md b/.changeset/two-points-share.md new file mode 100644 index 0000000..fe60068 --- /dev/null +++ b/.changeset/two-points-share.md @@ -0,0 +1,5 @@ +--- +"@nodesecure/js-x-ray": minor +--- + +Remove is-minified-code from dependencies and re-implement function in utils diff --git a/workspaces/js-x-ray/package.json b/workspaces/js-x-ray/package.json index 39a6c96..4030aee 100644 --- a/workspaces/js-x-ray/package.json +++ b/workspaces/js-x-ray/package.json @@ -50,7 +50,6 @@ "@nodesecure/tracer": "^3.0.0", "digraph-js": "2.2.4", "frequency-set": "^2.1.0", - "is-minified-code": "^2.0.0", "meriyah": "^6.0.0", "safe-regex": "^2.1.1", "ts-pattern": "^5.0.6" diff --git a/workspaces/js-x-ray/src/AstAnalyser.ts b/workspaces/js-x-ray/src/AstAnalyser.ts index 773db9b..e233506 100644 --- a/workspaces/js-x-ray/src/AstAnalyser.ts +++ b/workspaces/js-x-ray/src/AstAnalyser.ts @@ -5,7 +5,6 @@ import path from "node:path"; // Import Third-party Dependencies import type { ESTree } from "meriyah"; -import isMinified from "is-minified-code"; // Import Internal Dependencies import { @@ -22,7 +21,8 @@ import { ProbeRunner, type Probe } from "./ProbeRunner.js"; import { walkEnter } from "./walker/index.js"; import * as trojan from "./obfuscators/trojan-source.js"; import { - isOneLineExpressionExport + isOneLineExpressionExport, + isMinifiedCode } from "./utils/index.js"; import { PipelineRunner, @@ -221,7 +221,7 @@ export class AstAnalyser { const str = await fs.readFile(pathToFile, "utf-8"); const filePathString = pathToFile instanceof URL ? pathToFile.href : pathToFile; - const isMin = filePathString.includes(".min") || isMinified(str); + const isMin = filePathString.includes(".min") || isMinifiedCode(str); const data = this.analyse(str, { isMinified: isMin, module: path.extname(filePathString) === ".mjs" ? true : module, @@ -274,7 +274,7 @@ export class AstAnalyser { const str = fsSync.readFileSync(pathToFile, "utf-8"); const filePathString = pathToFile instanceof URL ? pathToFile.href : pathToFile; - const isMin = filePathString.includes(".min") || isMinified(str); + const isMin = filePathString.includes(".min") || isMinifiedCode(str); const data = this.analyse(str, { isMinified: isMin, module: path.extname(filePathString) === ".mjs" ? true : module, diff --git a/workspaces/js-x-ray/src/utils/index.ts b/workspaces/js-x-ray/src/utils/index.ts index 2995244..89fff47 100644 --- a/workspaces/js-x-ray/src/utils/index.ts +++ b/workspaces/js-x-ray/src/utils/index.ts @@ -2,3 +2,4 @@ export * from "./extractNode.js"; export * from "./isOneLineExpressionExport.js"; export * from "./notNullOrUndefined.js"; export * from "./toArrayLocation.js"; +export * from "./isMinifiedCode.js"; diff --git a/workspaces/js-x-ray/src/utils/isMinifiedCode.ts b/workspaces/js-x-ray/src/utils/isMinifiedCode.ts new file mode 100644 index 0000000..08df80a --- /dev/null +++ b/workspaces/js-x-ray/src/utils/isMinifiedCode.ts @@ -0,0 +1,32 @@ +// CONSTANTS +const kCommentPattern = /\/\*[\s\S]*?\*\/\r?\n?|\/\/.{0,200}?(?:\r?\n|$)/g; +const kTrailingLfPattern = /\r?\n$/; + +/** + * This code has been imported from: + * https://github.com/MartinKolarik/is-minified-code + */ +export function isMinifiedCode( + code: string +): boolean { + const lines = code + .replace(kCommentPattern, "") + .replace(kTrailingLfPattern, "") + .split("\n") + .flatMap((line) => (line.length > 0 ? [line.length] : [])); + + return lines.length <= 1 || median(lines) > 200; +} + +function median( + values: number[] +): number { + const toSorted = [...values].sort((a, b) => a - b); + const half = Math.floor(toSorted.length / 2); + + if (toSorted.length % 2) { + return toSorted[half]; + } + + return (toSorted[half - 1] + toSorted[half]) / 2; +}