index.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { logError } from "@ente/shared/sentry";
  2. import { CacheStorageFactory } from "./factory";
  3. const SecurityError = "SecurityError";
  4. const INSECURE_OPERATION = "The operation is insecure.";
  5. async function openCache(cacheName: string, cacheLimit?: number) {
  6. try {
  7. return await CacheStorageFactory.getCacheStorage().open(
  8. cacheName,
  9. cacheLimit,
  10. );
  11. } catch (e) {
  12. // ignoring insecure operation error, as it is thrown in incognito mode in firefox
  13. if (e.name === SecurityError && e.message === INSECURE_OPERATION) {
  14. // no-op
  15. } else {
  16. // log and ignore, we don't want to break the caller flow, when cache is not available
  17. logError(e, "openCache failed");
  18. }
  19. }
  20. }
  21. async function deleteCache(cacheName: string) {
  22. try {
  23. return await CacheStorageFactory.getCacheStorage().delete(cacheName);
  24. } catch (e) {
  25. // ignoring insecure operation error, as it is thrown in incognito mode in firefox
  26. if (e.name === SecurityError && e.message === INSECURE_OPERATION) {
  27. // no-op
  28. } else {
  29. // log and ignore, we don't want to break the caller flow, when cache is not available
  30. logError(e, "deleteCache failed");
  31. }
  32. }
  33. }
  34. export const CacheStorageService = { open: openCache, delete: deleteCache };