101 lines
2.9 KiB
TypeScript
101 lines
2.9 KiB
TypeScript
export type ServerConfig = {
|
|
/** Raw hostname the user entered, e.g. `music.example.com` or `192.168.86.25:3000` */
|
|
hostname: string
|
|
/** Base URL for the Next.js server, e.g. `https://music.example.com` */
|
|
serverUrl: string
|
|
/** Convex deployment URL, e.g. `https://convex.music.example.com` */
|
|
convexUrl: string
|
|
}
|
|
|
|
const SERVER_CONFIG_KEY = "otunes.server-config"
|
|
const LEGACY_SERVER_CONFIG_KEY = "otunes:server-config"
|
|
const LOCAL_CONVEX_PORT = "3210"
|
|
|
|
function parseHostPort(hostname: string): { host: string; port?: string } {
|
|
const colonIndex = hostname.lastIndexOf(":")
|
|
if (colonIndex === -1) {
|
|
return { host: hostname }
|
|
}
|
|
const host = hostname.slice(0, colonIndex)
|
|
const port = hostname.slice(colonIndex + 1)
|
|
if (!/^\d+$/.test(port)) {
|
|
return { host: hostname }
|
|
}
|
|
return { host, port }
|
|
}
|
|
|
|
function isLocalHost(hostname: string): boolean {
|
|
const host = parseHostPort(hostname).host.toLowerCase()
|
|
return (
|
|
host === "localhost" ||
|
|
host === "127.0.0.1" ||
|
|
host.endsWith(".local") ||
|
|
/^\d{1,3}(\.\d{1,3}){3}$/.test(host)
|
|
)
|
|
}
|
|
|
|
function normalizeHostnameInput(input: string): string {
|
|
return input.trim().replace(/^https?:\/\//i, "").replace(/\/+$/, "")
|
|
}
|
|
|
|
function buildServerUrl(hostname: string): string {
|
|
const protocol = isLocalHost(hostname) ? "http" : "https"
|
|
return `${protocol}://${hostname}`
|
|
}
|
|
|
|
function buildConvexUrl(hostname: string): string {
|
|
const { host, port } = parseHostPort(hostname)
|
|
const protocol = isLocalHost(hostname) ? "http" : "https"
|
|
if (isLocalHost(hostname)) {
|
|
return `${protocol}://${host}:${LOCAL_CONVEX_PORT}`
|
|
}
|
|
const convexHost = `convex.${host}`
|
|
return port ? `${protocol}://${convexHost}:${port}` : `${protocol}://${convexHost}`
|
|
}
|
|
|
|
export function parseServerHostname(input: string): ServerConfig | null {
|
|
const hostname = normalizeHostnameInput(input)
|
|
if (!hostname) return null
|
|
|
|
try {
|
|
const serverUrl = buildServerUrl(hostname)
|
|
const convexUrl = buildConvexUrl(hostname)
|
|
new URL(serverUrl)
|
|
new URL(convexUrl)
|
|
return { hostname, serverUrl, convexUrl }
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export async function loadServerConfig(
|
|
getItem: (key: string) => Promise<string | null>
|
|
): Promise<ServerConfig | null> {
|
|
const raw = await getItem(SERVER_CONFIG_KEY)
|
|
if (!raw) return null
|
|
try {
|
|
const parsed = JSON.parse(raw) as ServerConfig
|
|
if (parsed.hostname && parsed.serverUrl && parsed.convexUrl) {
|
|
return parsed
|
|
}
|
|
} catch {
|
|
return null
|
|
}
|
|
return null
|
|
}
|
|
|
|
export async function saveServerConfig(
|
|
setItem: (key: string, value: string) => Promise<void>,
|
|
config: ServerConfig
|
|
): Promise<void> {
|
|
await setItem(SERVER_CONFIG_KEY, JSON.stringify(config))
|
|
}
|
|
|
|
export async function clearServerConfig(
|
|
removeItem: (key: string) => Promise<void>
|
|
): Promise<void> {
|
|
await removeItem(SERVER_CONFIG_KEY)
|
|
}
|
|
|
|
export { SERVER_CONFIG_KEY, LEGACY_SERVER_CONFIG_KEY }
|