Files
streamyfin/utils/serverUrl/resolve.ts
Gauvain 0f29457ff8 feat(settings): unified server-URL resolver + field; adopt in Jellyseerr
Type a loose address (media.example.com, https://…, host:port) and the app finds the working, canonical URL.

- utils/serverUrl: generic candidate generator (https-first, port/path preserved, no Jellyfin-specific ports), parallel-probe resolver, numeric semver compare, and a Jellyseerr probe (/api/v1/status, min 2.0.0).

- useServerUrlResolver: idle -> resolving -> ok | error state machine with cancellation.

- ServerUrlField: shared input that auto-resolves on blur, inline status chip (tap to re-test) + resolved URL, persists the canonical URL.

- Jellyseerr settings adopt the field and log in with the resolved URL. Probe contract makes Streamystats/Jellyfin/Merlin a drop-in follow-up.
2026-06-04 20:13:10 +02:00

95 lines
2.8 KiB
TypeScript

import { getServerUrlCandidates } from "./candidates";
import type { ServerProbe, ServerProbeOutcome } from "./types";
export type ResolveFailureReason =
| "empty"
| "invalid"
| "version-too-low"
| "wrong-service"
| "unreachable";
export type ResolveResult =
| { ok: true; url: string; meta?: Record<string, unknown> }
| { ok: false; reason: ResolveFailureReason; version?: string };
export interface ResolveOptions {
/** Per-candidate probe timeout in ms. Default 5000. */
timeoutMs?: number;
/** Abort the whole resolution (cancels every in-flight probe). */
signal?: AbortSignal;
}
// Order in which to surface a failure when no candidate validated:
// the more specific/actionable the reason, the earlier it is reported.
const FAILURE_PRIORITY = [
"version-too-low",
"wrong-service",
"unreachable",
] as const satisfies ReadonlyArray<ResolveFailureReason>;
/**
* Resolve loose user input to a single working, canonical server URL.
*
* Generates candidates (https-first), probes them in parallel with a per-candidate
* timeout, and returns the first candidate (in preference order) the probe
* accepted. When none work, the most actionable failure is returned.
*/
export async function resolveServerUrl(
input: string,
probe: ServerProbe,
options: ResolveOptions = {},
): Promise<ResolveResult> {
const { timeoutMs = 5000, signal } = options;
if (!input.trim()) return { ok: false, reason: "empty" };
const candidates = getServerUrlCandidates(input);
if (candidates.length === 0) return { ok: false, reason: "invalid" };
const outcomes = await Promise.all(
candidates.map((url) => runProbe(url, probe, timeoutMs, signal)),
);
// Prefer the first candidate (https-first) that validated.
for (let i = 0; i < candidates.length; i++) {
const outcome = outcomes[i];
if (outcome.status === "ok") {
return { ok: true, url: candidates[i], meta: outcome.meta };
}
}
// Nothing validated: report the most useful failure.
for (const reason of FAILURE_PRIORITY) {
const hit = outcomes.find((outcome) => outcome.status === reason);
if (hit) {
return {
ok: false,
reason,
version: hit.status === "version-too-low" ? hit.version : undefined,
};
}
}
return { ok: false, reason: "unreachable" };
}
async function runProbe(
url: string,
probe: ServerProbe,
timeoutMs: number,
parentSignal?: AbortSignal,
): Promise<ServerProbeOutcome> {
const controller = new AbortController();
const abort = () => controller.abort();
parentSignal?.addEventListener("abort", abort);
const timer = setTimeout(abort, timeoutMs);
try {
return await probe(url, controller.signal);
} catch {
return { status: "unreachable" };
} finally {
clearTimeout(timer);
parentSignal?.removeEventListener("abort", abort);
}
}