mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-30 11:53:55 +01:00
/cso can now search every file in a repository with one helper call, which stops at a hard time limit, and a daily report.md lists its low-confidence hypotheses. Why: audits that started never reached the vulnerable file because each file took its own helper call inside a 10-minute budget (gstack-evals#443). Nothing to do: the next `/cso` run uses it. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
30 lines
1.0 KiB
TypeScript
30 lines
1.0 KiB
TypeScript
const WINDOW = 4096,
|
|
OVERLAP = 256,
|
|
EXCERPT = 300,
|
|
LEAD = 100;
|
|
let pattern: RegExp | undefined;
|
|
|
|
/** Matches one file's lines in bounded windows and posts each hit as it is found, so a search stopped mid-file keeps the hits before the stop. */
|
|
self.onmessage = (event: MessageEvent) => {
|
|
const message = event.data as { pattern?: string; text?: string };
|
|
if (typeof message.pattern === 'string') {
|
|
pattern = new RegExp(message.pattern);
|
|
postMessage({ done: true });
|
|
return;
|
|
}
|
|
let windowed = false;
|
|
(message.text ?? '').split('\n').forEach((line, index) => {
|
|
if (line.length > WINDOW) windowed = true;
|
|
for (let start = 0; ; start += WINDOW - OVERLAP) {
|
|
const found = pattern!.exec(line.slice(start, start + WINDOW));
|
|
if (found) {
|
|
const from = Math.max(0, start + found.index - LEAD);
|
|
postMessage({ hit: { line: index + 1, text: line.slice(from, from + EXCERPT) } });
|
|
return;
|
|
}
|
|
if (start + WINDOW >= line.length) return;
|
|
}
|
|
});
|
|
postMessage({ done: true, windowed });
|
|
};
|