Files
hermeven/apps/even-client/src/device/hostDetect.ts
T
DanandClaude Fable 5.2 65260911ae Address Cody review round 6: executable runbook, static client install, README
CODY-011 deploy/install.sh performs the ownership-sensitive sequence: the
         release is copied to app.new, that one directory is given to the
         hermeven user, npm ci runs unprivileged, and the finished tree is
         handed back to root:root with group/other write removed. Secrets,
         state and model directories are never touched. The guide now gives
         exact commands, including the C/C++ toolchain a clean host needs
         (found by running it on one).
CODY-012 install.sh builds the phone page with the bridge address preset
         and installs it root-owned into /opt/hermeven/www for Caddy. The
         guide explains the web build versus the .ehpk package and when
         each is needed.
CODY-013 README states which commit was deployed, what was checked, and
         what remains unverified.

Also corrects a wrong diagnosis recorded in the previous commit: the phone
page that showed "Browser preview" had been opened in Chrome, not in the
Even app. Host detection keeps a short precautionary wait (2.5 s) and its
comments no longer claim an observed Even-app delay.

tests/deploy/runbook-proof.sh proves the guide on a disposable Ubuntu 24.04
host with systemd.

Not reviewed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-21 12:28:07 -08:00

83 lines
3.0 KiB
TypeScript

/**
* Decide whether this page is running inside the Even app.
*
* The Even app's WebView provides `window.flutter_inappwebview.callHandler` (per the SDK's
* README). The simulator provides it before the page's script runs. Whether the real Even app
* always does is NOT known: this wait is a precaution, not a fix for an observed problem.
* (It was written on 2026-11-21 after a wrong diagnosis: a page that showed "Browser preview"
* on a phone had simply been opened in Chrome, not in the Even app.) The page waits briefly
* for the host, and listens for the WebView's own "platform ready" event, before concluding
* it is in an ordinary browser.
*/
export interface HostWindow {
flutter_inappwebview?: { callHandler?: unknown };
addEventListener(type: string, listener: () => void): void;
removeEventListener(type: string, listener: () => void): void;
}
export interface HostDetection {
found: boolean;
/** How long the page waited before deciding. */
waitedMs: number;
}
/** Short, because in an ordinary browser this is pure delay before the preview appears. */
export const HOST_WAIT_MS = 2500;
/** The event flutter_inappwebview dispatches on `window` once its JavaScript bridge is usable. */
export const PLATFORM_READY_EVENT = 'flutterInAppWebViewPlatformReady';
export function hostPresent(win: HostWindow): boolean {
return typeof win.flutter_inappwebview?.callHandler === 'function';
}
export function waitForEvenHost(
win: HostWindow,
options: { timeoutMs?: number; pollMs?: number; now?: () => number } = {},
): Promise<HostDetection> {
const timeoutMs = options.timeoutMs ?? HOST_WAIT_MS;
const pollMs = options.pollMs ?? 50;
const now = options.now ?? Date.now;
const started = now();
return new Promise((resolve) => {
let done = false;
const finish = (found: boolean) => {
if (done) return;
done = true;
clearInterval(poll);
clearTimeout(deadline);
win.removeEventListener(PLATFORM_READY_EVENT, onReady);
resolve({ found, waitedMs: now() - started });
};
const check = () => {
if (hostPresent(win)) finish(true);
};
// The event alone is not proof (anything can dispatch it); it only prompts a re-check.
const onReady = () => check();
const poll = setInterval(check, pollMs);
const deadline = setTimeout(() => finish(hostPresent(win)), timeoutMs);
win.addEventListener(PLATFORM_READY_EVENT, onReady);
check();
});
}
/**
* After the page has settled on browser-preview mode, keep watching: if the Even host turns
* up after all, call `onLate` once (the page reloads itself into the real mode).
*/
export function watchForLateHost(win: HostWindow, onLate: () => void, pollMs = 500): () => void {
let fired = false;
const check = () => {
if (fired || !hostPresent(win)) return;
fired = true;
stop();
onLate();
};
const timer = setInterval(check, pollMs);
win.addEventListener(PLATFORM_READY_EVENT, check);
const stop = () => {
clearInterval(timer);
win.removeEventListener(PLATFORM_READY_EVENT, check);
};
return stop;
}