entry.ts

2.6 KB · raw

1import { setupMessageHandler, setRestoreReady } from "./messages";2import { setupAutoLock } from "./alarms";3import { setupPasskeyProxy } from "./passkey";4import { setupCommands } from "./commands";5import { restoreFromSession, isUnlocked, lock } from "./vault";6import { ensureSettingsLoaded } from "./vault";7import { isRecoveryConfirmPending } from "./recovery-pending";8import { denyAllPendingPasskeyRequests } from "./passkey";9import { hydratePersistedSyncState, setSyncRunner } from "./sync-trigger";10import { productionSyncRunner } from "./chirp-sync-runner";1112// Keep alive for MV3 service worker13chrome.runtime.onInstalled.addListener(() => {});1415// The chrome.idle.onStateChanged listener lives in ./alarms (the architectural16// home for auto-lock timing). Keeping it in only one place avoids drift.1718// Lock-on-close via port disconnect. The popup opens a "popup" named port;19// when that port disconnects (popup closed), lock if lockOnClose is enabled.20// Previously declared in index.ts which was never bundled — moved here.21chrome.runtime.onConnect.addListener((port) => {22  if (port.name !== "popup") return;23  port.onDisconnect.addListener(async () => {24    await denyAllPendingPasskeyRequests();25    const s = await ensureSettingsLoaded();26    if (!isUnlocked() || s.lockOnClose === false) return;27    if (await isRecoveryConfirmPending()) return;28    lock();29  });30});3132export function start(): void {33  // MV3: the chrome.runtime.onMessage listener MUST be registered during the34  // worker's initial SYNCHRONOUS evaluation. If registration is deferred behind35  // an await, a popup/content message that wakes a cold worker is dispatched36  // before the listener exists → "receiving end does not exist", and the popup37  // misreads the failed HAS_VAULT as "no vault" and shows onboarding.38  //39  // So: kick off session-restore, hand its promise to the message layer as a40  // gate, and register the handler synchronously. Unlock-dependent handlers41  // await the gate (see handleMessage), which preserves the SEV-3-B fix — no42  // spurious lock screen — without delaying listener registration.43  const restored = restoreFromSession();44  setRestoreReady(restored);45  setupMessageHandler();46  void setupAutoLock();47  setupCommands();48  void setupPasskeyProxy();49  // Wire the chirp-sync production runner into the dispatcher. Without this50  // `triggerSyncIfReachable` is a no-op (the runner slot starts null) — the51  // pairing/transport/coordinator stack ships dormant until this assignment.52  setSyncRunner(productionSyncRunner);53  void restored.then(() => hydratePersistedSyncState()).catch(() => undefined);54}5556start();