-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.worker.js
More file actions
185 lines (161 loc) · 7.18 KB
/
scanner.worker.js
File metadata and controls
185 lines (161 loc) · 7.18 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { SuperDB } from './index.js';
import { getData, getDataAsStream } from './db.js';
let isCancelled = false;
let superdbWorkerInstance = null;
async function initializeSuperDB(wasmPath) {
if (superdbWorkerInstance) return true;
if (typeof SuperDB === 'undefined') {
postMessage({ type: 'init_error', message: 'SuperDB class not found in worker after import.' });
return false;
}
try {
superdbWorkerInstance = await SuperDB.instantiate(wasmPath);
postMessage({ type: 'init_done' });
return true;
} catch (error) {
console.error("Worker: Failed to instantiate SuperDB Wasm:", error);
postMessage({ type: 'init_error', message: `Worker failed to load Wasm: ${error.message}` });
return false;
}
}
async function runScan(dataPayload) {
const { rules, inputFormat, data, dataLocation } = dataPayload;
if (!superdbWorkerInstance) {
postMessage({ type: 'error', ruleName: 'Setup', message: 'SuperDB not initialized in worker.' });
postMessage({ type: 'complete', hitsFound: 0, errorsOccurred: true });
return;
}
if (!rules || rules.length === 0) {
postMessage({ type: 'error', ruleName: 'Setup', message: 'No rules provided to worker.' });
postMessage({ type: 'complete', hitsFound: 0, errorsOccurred: true });
return;
}
isCancelled = false;
let totalHitsFound = 0;
let totalErrorsOccurred = false;
let rulesProcessed = 0;
postMessage({ type: 'progress', processed: 0, total: rules.length });
const isLargeDataFromDB = dataLocation && dataLocation.type === 'indexeddb' && dataLocation.key;
const CONCURRENCY_LIMIT = isLargeDataFromDB ? 50 : 150;
const ruleChunks = [];
for (let i = 0; i < rules.length; i += CONCURRENCY_LIMIT) {
ruleChunks.push(rules.slice(i, i + CONCURRENCY_LIMIT));
}
postMessage({ type: 'progress_detail', message: `Processing in batches of ${CONCURRENCY_LIMIT}...` });
for (const ruleChunk of ruleChunks) {
if (isCancelled) break;
try {
let batchOpts;
if (isLargeDataFromDB) {
const sourceStream = await getDataAsStream(dataLocation.key);
const teedStreams = [];
let currentStream = sourceStream;
for (let i = 0; i < ruleChunk.length; i++) {
if (i < ruleChunk.length - 1) {
const [s1, s2] = currentStream.tee();
teedStreams.push(s1);
currentStream = s2;
} else {
teedStreams.push(currentStream);
}
}
batchOpts = ruleChunk.map((rule, i) => ({
program: rule.query,
input: teedStreams[i],
inputFormat: inputFormat,
outputFormat: 'line'
}));
} else {
if (!data) {
postMessage({ type: 'error', ruleName: 'Setup', message: 'No input data available for scan.' });
continue;
}
batchOpts = ruleChunk.map(rule => ({
program: rule.query,
input: data,
inputFormat: inputFormat,
outputFormat: 'line'
}));
}
const batchResults = await superdbWorkerInstance.zqBatch(batchOpts);
const hits = [];
const errors = [];
batchResults.forEach((batchItem, i) => {
const rule = ruleChunk[i];
const success = batchItem.success;
const dataResult = batchItem.data;
const dataHasData = batchItem.hasData;
const errorMsg = batchItem.error;
const index = batchItem.index;
const query = batchItem.query;
console.log(`--- Processing Rule: "${rule.name}" (Index: ${index}) ---`);
console.log(` Success: ${success} (type: ${typeof success})`);
console.log(` Has Data: ${dataHasData} (type: ${typeof dataHasData})`);
console.log(` (Data length: ${dataResult ? dataResult.length : 0})`);
console.log(` Error: "${errorMsg}"`);
if (success && dataResult && dataResult.trim() !== "") {
console.log(` -> Detected HIT for rule "${rule.name}".`);
hits.push({
ruleName: rule.name,
query: query,
result: `${dataResult.trim().substring(0, 500)}.... Use 'Investigate' to see fully results.`
});
} else if (!success) {
console.log(` -> Entered FAILURE branch for rule "${rule.name}".`);
console.error(` -> Rule "${rule.name}" FAILED. Error: ${errorMsg || 'Unknown error.'}`);
errors.push({
ruleName: rule.name,
message: errorMsg || 'Unknown error during query execution.'
});
} else {
console.log(` -> Rule "${rule.name}" successful but NO DATA (no hit).`);
}
});
if (hits.length > 0 || errors.length > 0) {
postMessage({ type: 'scanner_batch_results', hits: hits, errors: errors });
}
totalHitsFound += hits.length;
if (errors.length > 0) totalErrorsOccurred = true;
} catch (e) {
totalErrorsOccurred = true;
ruleChunk.forEach(rule => {
postMessage({ type: 'scanner_error', error: { ruleName: rule.name, message: `Batch failed: ${e.message || String(e)}` } });
});
}
rulesProcessed += ruleChunk.length;
postMessage({ type: 'progress', processed: rulesProcessed, total: rules.length });
await new Promise(resolve => setTimeout(resolve, 0));
}
if (!isCancelled) {
postMessage({ type: 'complete', hitsFound: totalHitsFound, errorsOccurred: totalErrorsOccurred });
} else {
postMessage({ type: 'cancelled' });
}
}
self.onmessage = async (event) => {
try {
const { type, ...dataPayload } = event.data;
if (type === 'init') {
await initializeSuperDB(dataPayload.wasmPath || "superdb.wasm");
} else if (type === 'start') {
if (!superdbWorkerInstance) {
const initialized = await initializeSuperDB(dataPayload.wasmPath || "superdb.wasm");
if (initialized) {
await runScan(dataPayload);
}
} else {
await runScan(dataPayload);
}
} else if (type === 'cancel') {
isCancelled = true;
console.log("Worker: Received cancel signal.");
}
} catch (error) {
console.error("Worker: Critical Error in onmessage:", error);
postMessage({
type: 'critical_error',
message: `Critical worker error: ${error.message}`,
stack: error.stack
});
}
};