This commit is contained in:
Samuel Newman
2025-05-10 00:06:06 +03:00
committed by GitHub
parent 2e80fa3dac
commit a0bd804262
45 changed files with 2021 additions and 451 deletions
+30
View File
@@ -372,3 +372,33 @@ export function getServiceAuthAudFromUrl(url: string | URL): string | null {
}
return `did:web:${hostname}`
}
// passes URL.parse, and has a TLD etc
export function definitelyUrl(maybeUrl: string) {
try {
if (maybeUrl.endsWith('.')) return null
// Prepend 'https://' if the input doesn't start with a protocol
if (!maybeUrl.startsWith('https://') && !maybeUrl.startsWith('http://')) {
maybeUrl = 'https://' + maybeUrl
}
const url = new URL(maybeUrl)
// Extract the hostname and split it into labels
const hostname = url.hostname
const labels = hostname.split('.')
// Ensure there are at least two labels (e.g., 'example' and 'com')
if (labels.length < 2) return null
const tld = labels[labels.length - 1]
// Check that the TLD is at least two characters long and contains only letters
if (!/^[a-z]{2,}$/i.test(tld)) return null
return url.toString()
} catch {
return null
}
}