( raw: string | undefined | null, onInvalid?: (value: string) => void )
| 122 | * @param onInvalid - Optional callback invoked once per invalid entry |
| 123 | */ |
| 124 | export function parseOriginList( |
| 125 | raw: string | undefined | null, |
| 126 | onInvalid?: (value: string) => void |
| 127 | ): string[] { |
| 128 | if (!raw) return [] |
| 129 | const seen = new Set<string>() |
| 130 | const origins: string[] = [] |
| 131 | for (const candidate of raw.split(',')) { |
| 132 | const trimmed = candidate.trim() |
| 133 | if (!trimmed) continue |
| 134 | try { |
| 135 | const { origin } = new URL(trimmed) |
| 136 | if (!seen.has(origin)) { |
| 137 | seen.add(origin) |
| 138 | origins.push(origin) |
| 139 | } |
| 140 | } catch { |
| 141 | onInvalid?.(trimmed) |
| 142 | } |
| 143 | } |
| 144 | return origins |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns true when the given URL points at a localhost loopback host. |
no test coverage detected