Files
rea/src/hopper/HopperRegexWorker.ts
T
morluto f201b04fbb fix(hopper)!: isolate regex matching from the native API thread
BREAKING CHANGE: Hopper regex mode uses ECMAScript Unicode syntax instead of Python re. Matching runs in a cancellable worker with a five-second deadline; literal mode keeps Unicode casefold matching.
2026-10-09 05:12:36 +08:00

28 lines
756 B
TypeScript

import { parentPort, workerData } from "node:worker_threads";
import { z } from "zod";
const input = z
.strictObject({
pattern: z.string().min(1),
caseSensitive: z.boolean(),
values: z.array(z.string()),
})
.parse(workerData);
let expression: RegExp | undefined;
try {
expression = new RegExp(input.pattern, input.caseSensitive ? "u" : "iu");
} catch (cause: unknown) {
parentPort?.postMessage({
status: "invalid_pattern",
reason: cause instanceof Error ? cause.message : String(cause),
});
}
if (expression !== undefined) {
const indices = [];
for (const [index, value] of input.values.entries()) {
if (expression.test(value)) indices.push(index);
}
parentPort?.postMessage({ status: "matched", indices });
}