feat: fix
This commit is contained in:
@@ -215,4 +215,89 @@ export class WasmBridge {
|
||||
machineId: this.readBytes(midPtr, midLen),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
return; // Node.js or environment without IDBFS
|
||||
}
|
||||
|
||||
const normalizedPath = this.normalizeMountPath(path);
|
||||
|
||||
// Create directory structure
|
||||
if (normalizedPath !== "/") {
|
||||
try {
|
||||
this.m.FS.mkdirTree(normalizedPath);
|
||||
} catch {
|
||||
// Directory already exists, ignore
|
||||
}
|
||||
}
|
||||
|
||||
// Mount IDBFS
|
||||
try {
|
||||
this.m.FS.mount(this.m.FS.filesystems.IDBFS, {}, normalizedPath);
|
||||
} catch {
|
||||
// Already mounted, ignore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync IDBFS from IndexedDB to memory (async).
|
||||
* Must be called after initIdbfs to load existing data from IndexedDB.
|
||||
*/
|
||||
async syncIdbfsFromStorage(): Promise<void> {
|
||||
if (!this.m.FS) {
|
||||
return; // FS not available
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.m.FS.syncfs(true, (err: Error | null) => {
|
||||
if (err) {
|
||||
console.error("[anisette] IDBFS sync from storage failed:", err);
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync IDBFS from memory to IndexedDB (async).
|
||||
* Must be called after modifying files to persist them.
|
||||
*/
|
||||
async syncIdbfsToStorage(): Promise<void> {
|
||||
if (!this.m.FS) {
|
||||
return; // FS not available
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
this.m.FS.syncfs(false, (err: Error | null) => {
|
||||
if (err) {
|
||||
console.error("[anisette] IDBFS sync to storage failed:", err);
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private normalizeMountPath(path: string): string {
|
||||
const trimmed = path.trim();
|
||||
const noSlash = trimmed.replace(/\/+$/, "");
|
||||
const noDot = noSlash.startsWith("./") ? noSlash.slice(2) : noSlash;
|
||||
|
||||
if (!noDot || noDot === ".") {
|
||||
return "/";
|
||||
} else if (noDot.startsWith("/")) {
|
||||
return noDot;
|
||||
} else {
|
||||
return "/" + noDot;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user