diff --git a/js/src/anisette.ts b/js/src/anisette.ts index c4dd264..0842904 100644 --- a/js/src/anisette.ts +++ b/js/src/anisette.ts @@ -149,10 +149,12 @@ export class Anisette { async provision(): Promise { await this.provisioning.provision(this.dsid); // Sync provisioning state to IndexedDB (browser only) - try { - await this.bridge.syncIdbfsToStorage(); - } catch { - // Ignore errors in Node.js or if IDBFS unavailable + if (this.bridge.isIdbfsAvailable()) { + try { + await this.bridge.syncIdbfsToStorage(); + } catch (err) { + console.error("[anisette] Failed to sync to IDBFS:", err); + } } } @@ -171,11 +173,15 @@ export class Anisette { const deviceJsonBytes = encodeUtf8(JSON.stringify(this.device.toJson(), null, 2)); this.bridge = new WasmBridge(this.wasmModule); - mountIdbfsPaths(this.bridge, this.libraryPath, this.provisioningPath); - try { - await this.bridge.syncIdbfsFromStorage(); - } catch { - // Ignore errors - might be first run or Node.js + + // Mount + load persisted IDBFS in browser environment + if (this.bridge.isIdbfsAvailable()) { + mountIdbfsPaths(this.bridge, this.libraryPath, this.provisioningPath); + try { + await this.bridge.syncIdbfsFromStorage(); + } catch { + // Ignore errors - might be first run or no existing data + } } if (adiPb) { diff --git a/js/src/wasm-bridge.ts b/js/src/wasm-bridge.ts index ec50d09..a83456b 100644 --- a/js/src/wasm-bridge.ts +++ b/js/src/wasm-bridge.ts @@ -216,13 +216,24 @@ export class WasmBridge { }; } + /** + * Check if IDBFS is available (browser environment only). + */ + isIdbfsAvailable(): boolean { + try { + return !!(this.m.FS && this.m.FS.filesystems?.IDBFS); + } catch { + return false; + } + } + /** * Initialize IDBFS for browser persistence. * Only works in browser environments with IDBFS available. */ initIdbfs(path: string): void { // Check if FS and IDBFS are available (browser only) - if (!this.m.FS || !this.m.FS.filesystems?.IDBFS) { + if (!this.isIdbfsAvailable()) { return; // Node.js or environment without IDBFS } @@ -248,10 +259,11 @@ export class WasmBridge { /** * Sync IDBFS from IndexedDB to memory (async). * Must be called after initIdbfs to load existing data from IndexedDB. + * Only works in browser environments with IDBFS available. */ async syncIdbfsFromStorage(): Promise { - if (!this.m.FS) { - return; // FS not available + if (!this.isIdbfsAvailable()) { + return; // IDBFS not available, skip silently } return new Promise((resolve, reject) => { @@ -269,10 +281,11 @@ export class WasmBridge { /** * Sync IDBFS from memory to IndexedDB (async). * Must be called after modifying files to persist them. + * Only works in browser environments with IDBFS available. */ async syncIdbfsToStorage(): Promise { - if (!this.m.FS) { - return; // FS not available + if (!this.isIdbfsAvailable()) { + return; // IDBFS not available, skip silently } return new Promise((resolve, reject) => {