Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: make opfs safer by allowing only 1 operation at a time

Abdullah Attab5ac9dd43011
diff --git a/apps/web/src/global.d.ts b/apps/web/src/global.d.ts
index 13c709496..418168d85 100644
--- a/apps/web/src/global.d.ts
+++ b/apps/web/src/global.d.ts
@@ -36,4 +36,9 @@ declare global {
os?: () => NodeJS.Platform | "mas";
NativeNNCrypto?: new () => import("@notesnook/crypto").NNCrypto;
}
+ interface FileSystemFileHandle {
+ createSyncAccessHandle(options?: {
+ mode: "read-only" | "readwrite" | "readwrite-unsafe";
+ }): Promise<FileSystemSyncAccessHandle>;
+ }
}
diff --git a/apps/web/src/interfaces/opfs.worker.ts b/apps/web/src/interfaces/opfs.worker.ts
index 81be3e2cc..1f840cd04 100644
--- a/apps/web/src/interfaces/opfs.worker.ts
+++ b/apps/web/src/interfaces/opfs.worker.ts
@@ -24,6 +24,7 @@ import { expose, transfer } from "comlink";
class OriginPrivateFileStore implements IFileStorage {
private storage: IndexedDBKVStore;
+ private locks: Map<string, Promise<any>> = new Map();
constructor(
name: string,
private readonly directory: FileSystemDirectoryHandle
@@ -33,7 +34,9 @@ class OriginPrivateFileStore implements IFileStorage {
async clear(): Promise<void> {
for await (const [name] of this.directory) {
- await this.directory.removeEntry(name, { recursive: true });
+ await this.safeOp(name, () =>
+ this.directory.removeEntry(name, { recursive: true })
+ );
}
await this.storage.clear();
}
@@ -47,33 +50,57 @@ class OriginPrivateFileStore implements IFileStorage {
return this.storage.delete(filename);
}
async writeChunk(chunkName: string, data: Uint8Array): Promise<void> {
- const file = await this.directory.getFileHandle(chunkName, {
- create: true
- });
- const syncHandle = await file.createSyncAccessHandle();
- syncHandle.write(data);
- syncHandle.close();
+ try {
+ await this.safeOp(chunkName, () =>
+ this.directory
+ .getFileHandle(chunkName, {
+ create: true
+ })
+ .then((file) => file.createSyncAccessHandle())
+ .then((handle) => {
+ handle.write(data);
+ handle.close();
+ })
+ );
+ } catch (e) {
+ console.error("Failed to write chunk", e);
+ }
}
+
async deleteChunk(chunkName: string) {
try {
- await this.directory.removeEntry(chunkName);
+ await this.safeOp(chunkName, () => this.directory.removeEntry(chunkName));
} catch (e) {
console.error("Failed to delete chunk", e);
}
}
+
async readChunk(chunkName: string): Promise<Uint8Array | undefined> {
try {
- const file = await this.directory.getFileHandle(chunkName);
- const syncHandle = await file.createSyncAccessHandle();
- const buffer = new Uint8Array(syncHandle.getSize());
- syncHandle.read(buffer);
- syncHandle.close();
- return buffer;
+ if (Object.hasOwn(FileSystemSyncAccessHandle.prototype, "mode")) {
+ return readFile(this.directory, chunkName);
+ }
+
+ // OPFS currently does not support multiple readers on a single file
+ // on all browsers so we wait for the file handle to be released before
+ // continuing. This is temporary until all browsers start supporting
+ // the read-only mode.
+ return this.safeOp(chunkName, () => readFile(this.directory, chunkName));
} catch (e) {
console.error("Failed to read chunk", e);
return;
}
}
+
+ private async safeOp<T>(chunkName: string, createPromise: () => Promise<T>) {
+ const lock = this.locks.get(chunkName);
+ if (lock) await lock;
+
+ const promise = createPromise();
+ this.locks.set(chunkName, promise);
+
+ return promise.finally(() => this.locks.delete(chunkName));
+ }
}
const fileStores: Map<string, OriginPrivateFileStore> = new Map();
@@ -117,3 +144,15 @@ const workerModule = {
expose(workerModule);
export type OriginPrivateFileStoreWorkerType = typeof workerModule;
+
+function readFile(directory: FileSystemDirectoryHandle, name: string) {
+ return directory
+ .getFileHandle(name)
+ .then((file) => file.createSyncAccessHandle({ mode: "read-only" }))
+ .then((handle) => {
+ const buffer = new Uint8Array(handle.getSize());
+ handle.read(buffer);
+ handle.close();
+ return buffer;
+ });
+}