chrome.ts

2.9 KB · raw

1// Shared chrome shim — uses real Chrome APIs on extension builds,2// Capacitor-backed shim on mobile builds.3// Background/vault code all imports from here so storage calls work on both platforms.4import { Capacitor } from "@capacitor/core";56export const PeachChrome = Capacitor.isNativePlatform()7  ? {8      storage: {9        local: {10          async get(keys: string | string[] | Record<string, unknown>): Promise<Record<string, unknown>> {11            const { Preferences } = await import("@capacitor/preferences");12            const keyArray = Array.isArray(keys) ? keys : typeof keys === "string" ? [keys] : Object.keys(keys);13            const result: Record<string, unknown> = {};14            for (const k of keyArray) {15              const { value } = await Preferences.get({ key: k });16              if (value !== undefined) result[k] = value;17            }18            return result;19          },20          async set(items: Record<string, unknown>): Promise<void> {21            const { Preferences } = await import("@capacitor/preferences");22            for (const [k, v] of Object.entries(items)) {23              await Preferences.set({ key: k, value: String(v) });24            }25          },26          async remove(keys: string | string[]): Promise<void> {27            const { Preferences } = await import("@capacitor/preferences");28            const keyArray = Array.isArray(keys) ? keys : [keys];29            for (const k of keyArray) await Preferences.remove({ key: k });30          },31          async clear(): Promise<void> {32            const { Preferences } = await import("@capacitor/preferences");33            await Preferences.clear();34          },35        },36        session: {37          _s: new Map<string, unknown>(),38          async get(keys: string | string[] | Record<string, unknown>): Promise<Record<string, unknown>> {39            const keyArray = Array.isArray(keys) ? keys : typeof keys === "string" ? [keys] : Object.keys(keys);40            const result: Record<string, unknown> = {};41            for (const k of keyArray) { if (this._s.has(k)) result[k] = this._s.get(k); }42            return result;43          },44          async set(items: Record<string, unknown>): Promise<void> {45            for (const [k, v] of Object.entries(items)) this._s.set(k, v);46          },47          async remove(keys: string | string[]): Promise<void> {48            const keyArray = Array.isArray(keys) ? keys : [keys];49            for (const k of keyArray) this._s.delete(k);50          },51        },52        onChanged: { addListener: () => {}, removeListener: () => {} },53      },54      runtime: {55        sendMessage: (_msg: unknown, cb?: (r: unknown) => void) => { if (cb) cb({ ok: false, error: "NO_SERVICE_WORKER" }); },56        onMessage: { addListener: () => {}, removeListener: () => {} },57        connect: () => ({ disconnect: () => {} }),58        lastError: null,59      },60    }61  : undefined; // Use real chrome on extension builds