Files
orca/src/shared/browser-url.ts
T

36 lines
1.0 KiB
TypeScript

import { ORCA_BROWSER_BLANK_URL } from './constants'
const LOCAL_ADDRESS_PATTERN =
/^(?:localhost|127(?:\.\d{1,3}){3}|0\.0\.0\.0|\[[0-9a-f:]+\])(?::\d+)?(?:\/.*)?$/i
export function normalizeBrowserNavigationUrl(rawUrl: string): string | null {
const trimmed = rawUrl.trim()
if (trimmed.length === 0 || trimmed === 'about:blank' || trimmed === ORCA_BROWSER_BLANK_URL) {
return ORCA_BROWSER_BLANK_URL
}
if (LOCAL_ADDRESS_PATTERN.test(trimmed)) {
try {
return new URL(`http://${trimmed}`).toString()
} catch {
return null
}
}
try {
const parsed = new URL(trimmed)
return parsed.protocol === 'http:' || parsed.protocol === 'https:' ? parsed.toString() : null
} catch {
try {
return new URL(`https://${trimmed}`).toString()
} catch {
return null
}
}
}
export function normalizeExternalBrowserUrl(rawUrl: string): string | null {
const normalized = normalizeBrowserNavigationUrl(rawUrl)
return normalized === ORCA_BROWSER_BLANK_URL ? null : normalized
}