diff --git a/packages/addon/src/addon.ts b/packages/addon/src/addon.ts index 73e4a69b..77dcca01 100644 --- a/packages/addon/src/addon.ts +++ b/packages/addon/src/addon.ts @@ -135,7 +135,7 @@ export class AIOStreams { Settings.DEFAULT_REGEX_EXCLUDE_PATTERN : undefined; const excludeRegex = excludeRegexPattern - ? compileRegex(excludeRegexPattern, 'i') + ? compileRegex(excludeRegexPattern, 'i', true) : undefined; const excludeKeywordsRegex = this.config.excludeFilters @@ -147,7 +147,7 @@ export class AIOStreams { Settings.DEFAULT_REGEX_INCLUDE_PATTERN : undefined; const requiredRegex = requiredRegexPattern - ? compileRegex(requiredRegexPattern, 'i') + ? compileRegex(requiredRegexPattern, 'i', true) : undefined; const requiredKeywordsRegex = this.config.strictIncludeFilters @@ -162,7 +162,7 @@ export class AIOStreams { ? sortRegexPatterns .split(/\s+/) .filter(Boolean) - .map((pattern) => compileRegex(pattern, 'i')) + .map((pattern) => compileRegex(pattern, 'i', true)) : undefined; excludeRegex || @@ -830,12 +830,9 @@ export class AIOStreams { if (!b.filename) return direction === 'asc' ? 1 : -1; // Test patterns in order - for (let i = 0; i < compiledRegexPatterns.length; i++) { - const regex = compiledRegexPatterns[i]; - + for (const regex of compiledRegexPatterns) { const aMatch = safeRegexTest(regex, a.filename); const bMatch = safeRegexTest(regex, b.filename); - // If both match or both don't match, continue to next pattern if ((aMatch && bMatch) || (!aMatch && !bMatch)) continue; diff --git a/packages/utils/src/regex.ts b/packages/utils/src/regex.ts index 01546dce..ed59c00f 100644 --- a/packages/utils/src/regex.ts +++ b/packages/utils/src/regex.ts @@ -5,8 +5,11 @@ import { createLogger } from './logger'; import { Settings } from './settings'; const DEFAULT_TIMEOUT = 1000; // 1 second timeout -const regexCache = Cache.getInstance('regexCache'); -const resultCache = Cache.getInstance('regexResultCache'); +const regexCache = Cache.getInstance('regexCache', 1_000); +const resultCache = Cache.getInstance( + 'regexResultCache', + 1_000_000 +); const logger = createLogger('regex'); @@ -22,41 +25,37 @@ export function safeRegexTest( str: string, timeoutMs: number = DEFAULT_TIMEOUT ): boolean { - // const compiledPattern = - // typeof pattern === 'string' - // ? regexCache.wrap( - // (p: string) => new RegExp(p), - // getTextHash(pattern), - // 60, - // pattern - // ) - // : pattern; const compiledPattern = typeof pattern === 'string' ? compileRegex(pattern) : pattern; try { - const result = resultCache.wrap( + return resultCache.wrap( (p: RegExp, s: string) => isMatch(p, s, { timeout: timeoutMs }), getTextHash(`${compiledPattern.toString()}|${str}`), - 60, + 100, compiledPattern, str ); - return result; } catch (error) { logger.error(`Regex test timed out after ${timeoutMs}ms:`, error); return false; } } -export function compileRegex(pattern: string, flags: string = ''): RegExp { - const compiledPattern = regexCache.wrap( +export function compileRegex( + pattern: string, + flags: string = '', + bypassCache: boolean = false +): RegExp { + if (bypassCache) { + return new RegExp(pattern, flags); + } + return regexCache.wrap( (p: string, f: string) => new RegExp(p, f), getTextHash(`${pattern}|${flags}`), 60, pattern, flags ); - return compiledPattern; } export function formRegexFromKeywords(