(url: string, maxLength = 120)
| 4 | * Sanitize URLs for logging by stripping query/hash and truncating. |
| 5 | */ |
| 6 | export function sanitizeUrlForLog(url: string, maxLength = 120): string { |
| 7 | if (!url) return '' |
| 8 | |
| 9 | const trimmed = url.trim() |
| 10 | try { |
| 11 | const hasProtocol = /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(trimmed) |
| 12 | const parsed = new URL(trimmed, hasProtocol ? undefined : 'http://localhost') |
| 13 | const origin = parsed.origin === 'null' ? '' : parsed.origin |
| 14 | const sanitized = `${origin}${parsed.pathname}` |
| 15 | const result = sanitized || parsed.pathname || trimmed |
| 16 | return truncate(result, maxLength) |
| 17 | } catch { |
| 18 | const withoutQuery = trimmed.split('?')[0].split('#')[0] |
| 19 | return truncate(withoutQuery, maxLength) |
| 20 | } |
| 21 | } |
no test coverage detected