Untitled

mail@pastecode.io avatar
unknown
plain_text
17 days ago
1.1 kB
1
Indexable
Never
const isOPFSPersistenceSupported = async (): Promise<boolean> => {
  try {
    const storage = await navigator.storage.getDirectory();
    const fileHandle = await storage.getFileHandle(`test.db`, { create: true });
    const sampleData = new ArrayBuffer(1000);
    const syncAccessFileHandle =
      fileHandle as unknown as CustomFileSystemFileHandle;
    const writable = await syncAccessFileHandle.createSyncAccessHandle();
    writable.write(sampleData, { at: 0 });
    writable.flush();
    writable.close();
    const fileHandle2 = await storage.getFileHandle(`test.db`, {
      create: true,
    });
    const syncAccessFileHandle2 =
      fileHandle2 as unknown as CustomFileSystemFileHandle;
    const readable = await syncAccessFileHandle2.createSyncAccessHandle();
    const dataFromPreviousTest = await readable.getSize();
    console.log(
      "dataFromPreviousTest",
      dataFromPreviousTest,
      "sampleData.byteLength",
      sampleData.byteLength,
    );
    return dataFromPreviousTest === sampleData.byteLength;
  } catch (error) {
    console.log("isOPFSPersistenceSupported error", error);
    return false;
  }
};
Leave a Comment