|
@@ -1,5 +1,3 @@
|
|
|
-import log from "@/next/log";
|
|
|
-
|
|
|
export enum CACHES {
|
|
|
THUMBS = "thumbs",
|
|
|
FACE_CROPS = "face-crops",
|
|
@@ -7,7 +5,7 @@ export enum CACHES {
|
|
|
FILES = "files",
|
|
|
}
|
|
|
|
|
|
-export interface LimitedCacheStorage {
|
|
|
+interface LimitedCacheStorage {
|
|
|
open: (
|
|
|
cacheName: string,
|
|
|
cacheLimitInBytes?: number,
|
|
@@ -26,62 +24,33 @@ export interface LimitedCache {
|
|
|
|
|
|
class cacheStorageFactory {
|
|
|
getCacheStorage(): LimitedCacheStorage {
|
|
|
- return transformBrowserCacheStorageToLimitedCacheStorage(caches);
|
|
|
+ return {
|
|
|
+ async open(cacheName) {
|
|
|
+ const cache = await caches.open(cacheName);
|
|
|
+ return {
|
|
|
+ match: (key) => {
|
|
|
+ // options are not supported in the browser
|
|
|
+ return cache.match(key);
|
|
|
+ },
|
|
|
+ put: cache.put.bind(cache),
|
|
|
+ delete: cache.delete.bind(cache),
|
|
|
+ };
|
|
|
+ },
|
|
|
+ delete: caches.delete.bind(caches),
|
|
|
+ };
|
|
|
}
|
|
|
}
|
|
|
|
|
|
export const CacheStorageFactory = new cacheStorageFactory();
|
|
|
|
|
|
-function transformBrowserCacheStorageToLimitedCacheStorage(
|
|
|
- caches: CacheStorage,
|
|
|
-): LimitedCacheStorage {
|
|
|
- return {
|
|
|
- async open(cacheName) {
|
|
|
- const cache = await caches.open(cacheName);
|
|
|
- return {
|
|
|
- match: (key) => {
|
|
|
- // options are not supported in the browser
|
|
|
- return cache.match(key);
|
|
|
- },
|
|
|
- put: cache.put.bind(cache),
|
|
|
- delete: cache.delete.bind(cache),
|
|
|
- };
|
|
|
- },
|
|
|
- delete: caches.delete.bind(caches),
|
|
|
- };
|
|
|
-}
|
|
|
-
|
|
|
-const SecurityError = "SecurityError";
|
|
|
-const INSECURE_OPERATION = "The operation is insecure.";
|
|
|
-
|
|
|
async function openCache(cacheName: string, cacheLimit?: number) {
|
|
|
- try {
|
|
|
- return await CacheStorageFactory.getCacheStorage().open(
|
|
|
- cacheName,
|
|
|
- cacheLimit,
|
|
|
- );
|
|
|
- } catch (e) {
|
|
|
- // ignoring insecure operation error, as it is thrown in incognito mode in firefox
|
|
|
- if (e.name === SecurityError && e.message === INSECURE_OPERATION) {
|
|
|
- // no-op
|
|
|
- } else {
|
|
|
- // log and ignore, we don't want to break the caller flow, when cache is not available
|
|
|
- log.error("openCache failed", e);
|
|
|
- }
|
|
|
- }
|
|
|
+ return await CacheStorageFactory.getCacheStorage().open(
|
|
|
+ cacheName,
|
|
|
+ cacheLimit,
|
|
|
+ );
|
|
|
}
|
|
|
async function deleteCache(cacheName: string) {
|
|
|
- try {
|
|
|
- return await CacheStorageFactory.getCacheStorage().delete(cacheName);
|
|
|
- } catch (e) {
|
|
|
- // ignoring insecure operation error, as it is thrown in incognito mode in firefox
|
|
|
- if (e.name === SecurityError && e.message === INSECURE_OPERATION) {
|
|
|
- // no-op
|
|
|
- } else {
|
|
|
- // log and ignore, we don't want to break the caller flow, when cache is not available
|
|
|
- log.error("deleteCache failed", e);
|
|
|
- }
|
|
|
- }
|
|
|
+ return await CacheStorageFactory.getCacheStorage().delete(cacheName);
|
|
|
}
|
|
|
|
|
|
export const CacheStorageService = { open: openCache, delete: deleteCache };
|