(pattern: string)
| 68 | } |
| 69 | |
| 70 | private compilePattern(pattern: string): RegExp { |
| 71 | if (!pattern) { |
| 72 | throw new Error('Regex pattern is required') |
| 73 | } |
| 74 | |
| 75 | if (pattern.length > MAX_PATTERN_LENGTH) { |
| 76 | throw new Error(`Regex pattern exceeds maximum length of ${MAX_PATTERN_LENGTH} characters`) |
| 77 | } |
| 78 | |
| 79 | try { |
| 80 | const regex = new RegExp(toNonCapturing(pattern), 'g') |
| 81 | |
| 82 | const testStrings = [ |
| 83 | 'a'.repeat(10000), |
| 84 | ' '.repeat(10000), |
| 85 | 'a '.repeat(5000), |
| 86 | 'aB1 xY2\n'.repeat(1250), |
| 87 | `${'a'.repeat(30)}!`, |
| 88 | `${'a b '.repeat(25)}!`, |
| 89 | ] |
| 90 | for (const testStr of testStrings) { |
| 91 | regex.lastIndex = 0 |
| 92 | const start = Date.now() |
| 93 | regex.test(testStr) |
| 94 | const elapsed = Date.now() - start |
| 95 | if (elapsed > 50) { |
| 96 | throw new Error('Regex pattern appears to have catastrophic backtracking') |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | regex.lastIndex = 0 |
| 101 | return regex |
| 102 | } catch (error) { |
| 103 | if (error instanceof Error && error.message.includes('catastrophic')) { |
| 104 | throw error |
| 105 | } |
| 106 | throw new Error(`Invalid regex pattern "${pattern}": ${toError(error).message}`) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | async chunk(content: string): Promise<Chunk[]> { |
| 111 | if (!content?.trim()) { |
no test coverage detected