27 lines
880 B
JavaScript
27 lines
880 B
JavaScript
// Match mobile's public-domain and local-network Convex conventions.
|
|
function parseServer(input) {
|
|
if (typeof input !== "string" || !input.trim())
|
|
throw new Error("Enter your server address.")
|
|
const raw = input
|
|
.trim()
|
|
.replace(/^https?:\/\//i, "")
|
|
.replace(/\/+$/, "")
|
|
if (/[\s/@?#\\]/.test(raw))
|
|
throw new Error("Enter a hostname, without a path or credentials.")
|
|
const parsed = new URL(`https://${raw}`)
|
|
const local =
|
|
parsed.hostname === "localhost" ||
|
|
parsed.hostname.endsWith(".local") ||
|
|
/^\d{1,3}(\.\d{1,3}){3}$/.test(parsed.hostname)
|
|
const protocol = local ? "http:" : "https:"
|
|
const hostname = parsed.host
|
|
return {
|
|
hostname,
|
|
serverUrl: `${protocol}//${hostname}`,
|
|
convexUrl: local
|
|
? `${protocol}//${parsed.hostname}:3210`
|
|
: `${protocol}//convex.${hostname}`,
|
|
}
|
|
}
|
|
module.exports = { parseServer }
|