Live (#8354)
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user