Skip to content

Commit

Permalink
fix: missing blob mime types in IndexedDB (#3280)
Browse files Browse the repository at this point in the history
  • Loading branch information
fourdim committed Jul 1, 2023
1 parent fcbaac0 commit 1e62fe4
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/store/src/persistence/blob/indexeddb-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,40 @@ import type { BlobStorage } from './types.js';

export const createIndexeddbStorage = (database: string): BlobStorage => {
let dbPromise: ReturnType<typeof createStore>;
let mimeTypeDbPromise: ReturnType<typeof createStore>;
// async import `idb-keyval` to avoid side effect
const idbPromise = import('idb-keyval').then(({ createStore, ...idb }) => {
// don't change the db name, it's for backward compatibility
dbPromise = createStore(`${database}_blob`, 'blob');
mimeTypeDbPromise = createStore(`${database}_blob_mime`, 'blob_mime');
return idb;
});
return {
crud: {
get: async (key: string) => {
const get = (await idbPromise).get;
const db = await dbPromise;
const mimeTypeDb = await mimeTypeDbPromise;
const res = await get<ArrayBuffer>(key, db);
if (res) {
return new Blob([res]);
return new Blob([res], { type: await get(key, mimeTypeDb) });
}
return null;
},
set: async (key: string, value: Blob) => {
const set = (await idbPromise).set;
const db = await dbPromise;
const mimeTypeDb = await mimeTypeDbPromise;
await set(key, await value.arrayBuffer(), db);
await set(key, value.type, mimeTypeDb);
return key;
},
delete: async (key: string) => {
const del = (await idbPromise).del;
const db = await dbPromise;
const mimeTypeDb = await mimeTypeDbPromise;
await del(key, db);
await del(key, mimeTypeDb);
},
list: async () => {
const keys = (await idbPromise).keys;
Expand Down

2 comments on commit 1e62fe4

@vercel
Copy link

@vercel vercel bot commented on 1e62fe4 Jul 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

blocksuite – ./packages/playground

blocksuite-git-master-toeverything.vercel.app
blocksuite-toeverything.vercel.app
blocksuite-five.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 1e62fe4 Jul 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.