Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/two-points-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/js-x-ray": minor
---

Remove is-minified-code from dependencies and re-implement function in utils
1 change: 0 additions & 1 deletion workspaces/js-x-ray/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions workspaces/js-x-ray/src/AstAnalyser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions workspaces/js-x-ray/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./extractNode.js";
export * from "./isOneLineExpressionExport.js";
export * from "./notNullOrUndefined.js";
export * from "./toArrayLocation.js";
export * from "./isMinifiedCode.js";
32 changes: 32 additions & 0 deletions workspaces/js-x-ray/src/utils/isMinifiedCode.ts
Original file line number Diff line number Diff line change
@@ -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;
}