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.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
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 });
|
||||
}
|
||||
Reference in New Issue
Block a user