feat: resolve presentation live target

This commit is contained in:
lda
2026-07-09 06:11:33 +07:00 Verified
parent 5a3070df11
commit c332e58697
2 changed files with 100 additions and 0 deletions
@@ -0,0 +1,46 @@
import { STORAGE_KEY } from "../app/state.js";
export type PresentationTargetState =
| { readonly mode: "live"; readonly target: string; readonly source: "session-storage" | "default" }
| { readonly mode: "replay"; readonly target: null; readonly reason: string };
export const DEFAULT_PRESENTATION_TARGET = "http://127.0.0.1:8765/rpc";
const isHttpUrl = (value: string): boolean => {
try {
const url = new URL(value);
return url.protocol === "http:" || url.protocol === "https:";
} catch {
return false;
}
};
export const resolvePresentationTarget = (
storage: Storage | null = typeof sessionStorage === "undefined" ? null : sessionStorage,
): PresentationTargetState => {
let stored: string | null;
try {
stored = storage?.getItem(STORAGE_KEY) ?? null;
} catch {
return {
mode: "replay",
target: null,
reason: "session storage is unavailable",
};
}
const target = stored ?? DEFAULT_PRESENTATION_TARGET;
if (!isHttpUrl(target)) {
return {
mode: "replay",
target: null,
reason: "presentation target is not an HTTP URL",
};
}
return {
mode: "live",
target,
source: stored ? "session-storage" : "default",
};
};