-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·92 lines (80 loc) · 3.86 KB
/
Copy pathcli.js
File metadata and controls
executable file
·92 lines (80 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env node
const fastscan = require('./src/index');
const path = require('path');
const { performance } = require('perf_hooks');
const args = process.argv.slice(2);
if (args.length < 2) {
console.log("Usage: fastscan <filepath> <pattern(s)> [maxMatches] [contextSize]");
console.log("Tip: Pass multiple patterns separated by comma: fastscan app.log \"ERROR,CRITICAL,FATAL\"");
process.exit(1);
}
const filepath = args[0];
const rawPattern = args[1];
const maxMatches = parseInt(args[2], 10) || 100;
const contextSize = parseInt(args[3], 10) || 50;
console.log(`\x1b[36m⚡ FastScan Ultra v2.0 — Hardware-Accelerated Multi-Platform Scanner\x1b[0m`);
try {
const cpu = fastscan.getCpuFeatures();
const hwList = [];
if (cpu.avx512) hwList.push("AVX-512 (512-bit SIMD, 128B Loop)");
else if (cpu.avx2) hwList.push("AVX2 (256-bit SIMD, 64B Loop)");
else if (cpu.sse2) hwList.push("SSE2 (128-bit SIMD)");
else if (cpu.neon) hwList.push("ARM NEON");
console.log(`\x1b[33m[Hardware]\x1b[0m Engine: ${hwList.join(', ') || 'Scalar'} + Persistent Thread Pool`);
} catch (e) {}
console.log(`[*] Target: ${filepath}`);
const isMulti = rawPattern.includes(',');
if (isMulti) {
const patterns = rawPattern.split(',').map(s => s.trim()).filter(Boolean);
console.log(`[*] Multi-Pattern Mode (${patterns.length} patterns): [${patterns.join(', ')}]`);
console.log(`[*] Max Matches: ${maxMatches}`);
console.log(`[*] Single-Pass Scanning...\n`);
const startTime = performance.now();
try {
const results = fastscan.scanFileMulti(filepath, patterns, maxMatches);
const elapsed = (performance.now() - startTime).toFixed(2);
console.log(`\x1b[32m✅ Scan Finished in ${elapsed} ms\x1b[0m`);
console.log(` Found ${results.length} matches`);
const previewCount = Math.min(results.length, 10);
if (previewCount > 0) {
console.log(`\n--- Results Preview (First ${previewCount} matches) ---`);
for (let i = 0; i < previewCount; i++) {
const item = results[i];
console.log(`\x1b[34m[Match #${i + 1}]\x1b[0m Pattern: \x1b[33m"${item.pattern}"\x1b[0m | Offset: ${item.offset}`);
}
}
} catch (err) {
console.error("\x1b[31m❌ Error:\x1b[0m", err.message);
process.exit(1);
}
} else {
console.log(`[*] Pattern: "${rawPattern}"`);
console.log(`[*] Max Matches: ${maxMatches}`);
console.log(`[*] Scanning with Vectorized Line Indexing...\n`);
const startTime = performance.now();
fastscan.scanWithPositions(filepath, rawPattern, { maxMatches, contextSize })
.then((results) => {
const elapsed = (performance.now() - startTime).toFixed(2);
console.log(`\x1b[32m✅ Scan Finished in ${elapsed} ms\x1b[0m`);
console.log(` Found ${results.length} matches`);
const previewCount = Math.min(results.length, 10);
if (previewCount > 0) {
console.log(`\n--- Results Preview (First ${previewCount} matches) ---`);
for (let i = 0; i < previewCount; i++) {
const item = results[i];
console.log(`\x1b[34m[Match #${i + 1}]\x1b[0m Line ${item.line}:${item.column} (Offset: ${item.offset})`);
console.log(` ... ${item.snippet.replace(/[\r\n]+/g, ' ')} ...\n`);
}
}
})
.catch((err) => {
if (err.name === 'FileNotFoundError') {
console.error("\x1b[31m❌ Error: File not found.\x1b[0m");
} else if (err.name === 'MemoryError') {
console.error("\x1b[31m❌ Error: Memory allocation failed. Try reducing maxMatches.\x1b[0m");
} else {
console.error("\x1b[31m❌ Error:\x1b[0m", err.message);
}
process.exit(1);
});
}