Run user-supplied regexes in worker threads with a timeout

This commit is contained in:
Dragory 2020-08-05 01:15:36 +03:00
parent 19b97bc32b
commit a7fa258f2a
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
15 changed files with 237 additions and 43 deletions

View file

@ -0,0 +1,37 @@
import { RegExpRunner } from "./RegExpRunner";
interface RunnerInfo {
users: number;
runner: RegExpRunner;
}
const runners: Map<string, RunnerInfo> = new Map();
export function getRegExpRunner(key: string) {
if (!runners.has(key)) {
const runner = new RegExpRunner();
runners.set(key, {
users: 0,
runner,
});
}
const info = runners.get(key);
info.users++;
return info.runner;
}
export function discardRegExpRunner(key: string) {
if (!runners.has(key)) {
throw new Error(`No runners with key ${key}, cannot discard`);
}
const info = runners.get(key);
info.users--;
if (info.users <= 0) {
info.runner.dispose();
runners.delete(key);
}
}