Skip to content

Commit 9f8d834

Browse files
logaretmclaude
andcommitted
refactor(core): Lazy-init integer literal regex
Cache the regex in a module-scope let and build it on first call. Evaluating it at module scope would still crash Safari <16.4 at import time. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 72afd61 commit 9f8d834

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

packages/core/src/integrations/postgresjs.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ export function _reconstructQuery(strings: string[] | undefined): string | undef
342342
return strings.reduce((acc, str, i) => (i === 0 ? str : `${acc}$${i}${str}`), '');
343343
}
344344

345+
let integerLiteralRE: RegExp | undefined;
346+
345347
/**
346348
* Sanitize SQL query as per the OTEL semantic conventions
347349
* https://opentelemetry.io/docs/specs/semconv/database/database-spans/#sanitization-of-dbquerytext
@@ -356,6 +358,11 @@ export function _sanitizeSqlQuery(sqlQuery: string | undefined): string {
356358
return 'Unknown SQL Query';
357359
}
358360

361+
// Lazy init: constructing this at module scope would evaluate the lookbehind
362+
// on import and crash Safari <16.4 browser bundles that reach this file via
363+
// the core barrel. Building it on first call keeps the cost off the import path.
364+
integerLiteralRE ??= new RegExp('(?<!\\$)-?\\b\\d+\\b', 'g');
365+
359366
return (
360367
sqlQuery
361368
// Remove comments first (they may contain newlines and extra spaces)
@@ -378,10 +385,7 @@ export function _sanitizeSqlQuery(sqlQuery: string | undefined): string {
378385
.replace(/-?\b\d+\.?\d*[eE][+-]?\d+\b/g, '?') // Scientific notation
379386
.replace(/-?\b\d+\.\d+\b/g, '?') // Decimals
380387
.replace(/-?\.\d+\b/g, '?') // Decimals starting with dot
381-
// Constructed via `new RegExp` so the negative lookbehind is evaluated at
382-
// runtime. As a literal, it is a parse-time SyntaxError on Safari <16.4 —
383-
// which breaks any browser bundle that reaches this module via the core barrel.
384-
.replace(new RegExp('(?<!\\$)-?\\b\\d+\\b', 'g'), '?') // Integers (NOT $n placeholders)
388+
.replace(integerLiteralRE, '?') // Integers (NOT $n placeholders)
385389
// Collapse IN clauses for cardinality (both ? and $n variants)
386390
.replace(/\bIN\b\s*\(\s*\?(?:\s*,\s*\?)*\s*\)/gi, 'IN (?)')
387391
.replace(/\bIN\b\s*\(\s*\$\d+(?:\s*,\s*\$\d+)*\s*\)/gi, 'IN ($?)')

0 commit comments

Comments
 (0)