|
| 1 | +import type { Storage } from 'webextension-polyfill'; |
| 2 | + |
| 3 | +type StorageLocal = Pick<Storage.StorageArea, 'get' | 'set' | 'remove'>; |
| 4 | + |
| 5 | +type MetadataItem = { |
| 6 | + accessTime: number; |
| 7 | +}; |
| 8 | + |
| 9 | +export type Metadata = Record<string, MetadataItem>; |
| 10 | + |
| 11 | +const sizeOfChunkToBePurged = 0.1; |
| 12 | + |
| 13 | +export const makeMetadataKey = (resourceName: string) => `${resourceName}-metadata`; |
| 14 | + |
| 15 | +export const makeItemKey = (resourceName: string, key: string) => `${resourceName}-item-${key}`; |
| 16 | + |
| 17 | +const isGetBytesInUsePresent = ( |
| 18 | + storageLocal: StorageLocal |
| 19 | +): storageLocal is StorageLocal & { getBytesInUse: (keys?: string | string[]) => Promise<number> } => |
| 20 | + 'getBytesInUse' in storageLocal; |
| 21 | + |
| 22 | +export const makePersistentCacheStorageFactory = |
| 23 | + (extensionLocalStorage: StorageLocal, createVolatileCache: <T>() => Map<string, T>) => |
| 24 | + <T>({ |
| 25 | + fallbackMaxCollectionItemsGuard, |
| 26 | + resourceName, |
| 27 | + quotaInBytes |
| 28 | + }: { |
| 29 | + fallbackMaxCollectionItemsGuard: number; |
| 30 | + resourceName: string; |
| 31 | + quotaInBytes: number; |
| 32 | + }) => { |
| 33 | + const loaded = createVolatileCache<T>(); |
| 34 | + const metadataKey = makeMetadataKey(resourceName); |
| 35 | + const getItemKey = (key: string) => makeItemKey(resourceName, key); |
| 36 | + |
| 37 | + const getMetadata = async () => { |
| 38 | + const result = await extensionLocalStorage.get(metadataKey); |
| 39 | + return result[metadataKey] as Metadata; |
| 40 | + }; |
| 41 | + |
| 42 | + const updateAccessTime = async (key: string) => { |
| 43 | + const metadata = await getMetadata(); |
| 44 | + const nextMetadata: Metadata = { |
| 45 | + ...metadata, |
| 46 | + [key]: { |
| 47 | + accessTime: Date.now() |
| 48 | + } |
| 49 | + }; |
| 50 | + await extensionLocalStorage.set({ [metadataKey]: nextMetadata }); |
| 51 | + }; |
| 52 | + |
| 53 | + const isQuotaExceeded = async () => { |
| 54 | + const metadata = await getMetadata(); |
| 55 | + const allCollectionKeys = [metadataKey, ...Object.keys(metadata)]; |
| 56 | + |
| 57 | + // Polyfill we use does not list the getBytesInUse method but that method exists in chrome API |
| 58 | + if (isGetBytesInUsePresent(extensionLocalStorage)) { |
| 59 | + console.log('allCollectionKeys', allCollectionKeys); |
| 60 | + const bytesInUse = await extensionLocalStorage.getBytesInUse(allCollectionKeys); |
| 61 | + return bytesInUse > quotaInBytes; |
| 62 | + } |
| 63 | + |
| 64 | + return allCollectionKeys.length > fallbackMaxCollectionItemsGuard; |
| 65 | + }; |
| 66 | + |
| 67 | + const evict = async () => { |
| 68 | + let metadata = await getMetadata(); |
| 69 | + const mostDatedKeysToPurge = Object.entries(metadata) |
| 70 | + .map(([key, { accessTime }]) => ({ accessTime, key })) |
| 71 | + .sort((a, b) => a.accessTime - b.accessTime) |
| 72 | + .filter((_, index, self) => { |
| 73 | + const numberOfItemsToPurge = Math.abs(self.length * sizeOfChunkToBePurged); |
| 74 | + return index < numberOfItemsToPurge; |
| 75 | + }) |
| 76 | + .map((i) => i.key); |
| 77 | + |
| 78 | + await extensionLocalStorage.remove(mostDatedKeysToPurge); |
| 79 | + metadata = await getMetadata(); |
| 80 | + for (const key of mostDatedKeysToPurge) { |
| 81 | + delete metadata[key]; |
| 82 | + } |
| 83 | + await extensionLocalStorage.set({ [metadataKey]: metadata }); |
| 84 | + }; |
| 85 | + |
| 86 | + return { |
| 87 | + async get(key: string) { |
| 88 | + const itemKey = getItemKey(key); |
| 89 | + |
| 90 | + let value = loaded.get(itemKey); |
| 91 | + if (!value) { |
| 92 | + const result = await extensionLocalStorage.get(itemKey); |
| 93 | + value = result[itemKey]; |
| 94 | + } |
| 95 | + |
| 96 | + if (value) { |
| 97 | + void updateAccessTime(itemKey); |
| 98 | + } |
| 99 | + |
| 100 | + return value; |
| 101 | + }, |
| 102 | + async set(key: string, value: T) { |
| 103 | + const itemKey = getItemKey(key); |
| 104 | + loaded.set(itemKey, value); |
| 105 | + await extensionLocalStorage.set({ [itemKey]: value }); |
| 106 | + |
| 107 | + void (async () => { |
| 108 | + await updateAccessTime(itemKey); |
| 109 | + if (await isQuotaExceeded()) await evict(); |
| 110 | + })(); |
| 111 | + } |
| 112 | + }; |
| 113 | + }; |
0 commit comments