]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/root-helpers/peertube-web-storage.ts
Update translations
[github/Chocobozzz/PeerTube.git] / client / src / root-helpers / peertube-web-storage.ts
index 0db1301bd4fa4846b1e6d3096e0417d49849c527..3622cdc44e74f275408ac4afcebe9c07e8952bbd 100644 (file)
@@ -4,28 +4,30 @@ const valuesMap = new Map()
 
 function proxify (instance: MemoryStorage) {
   return new Proxy(instance, {
-    set: function (obj, prop: string | number, value) {
-      if (MemoryStorage.prototype.hasOwnProperty(prop)) {
-        instance[prop] = value
+    set: function (obj, prop: string | symbol, value) {
+      if (Object.prototype.hasOwnProperty.call(MemoryStorage, prop)) {
+        // FIXME: remove cast on typescript upgrade
+        instance[prop as any] = value
       } else {
         instance.setItem(prop, value)
       }
+
       return true
     },
-    get: function (target, name: string | number) {
-      if (MemoryStorage.prototype.hasOwnProperty(name)) {
-        return instance[name]
-      }
-      if (valuesMap.has(name)) {
+    get: function (target, name: string | symbol | number) {
+      // FIXME: remove cast on typescript upgrade
+      if (typeof instance[name as any] === 'function') {
+        // FIXME: remove cast on typescript upgrade
+        return instance[name as any]
+      } else if (valuesMap.has(name)) {
         return instance.getItem(name)
       }
     }
   })
 }
 
-class MemoryStorage {
+class MemoryStorage implements Storage {
   [key: string]: any
-  [index: number]: string
 
   getItem (key: any) {
     const stringKey = String(key)
@@ -64,10 +66,8 @@ class MemoryStorage {
 
 let peertubeLocalStorage: Storage
 let peertubeSessionStorage: Storage
-try {
-  peertubeLocalStorage = localStorage
-  peertubeSessionStorage = sessionStorage
-} catch (err) {
+
+function reinitStorage () {
   const instanceLocalStorage = new MemoryStorage()
   const instanceSessionStorage = new MemoryStorage()
 
@@ -75,6 +75,19 @@ try {
   peertubeSessionStorage = proxify(instanceSessionStorage)
 }
 
+try {
+  peertubeLocalStorage = localStorage
+  peertubeSessionStorage = sessionStorage
+} catch (err) {
+  // support Firefox and other browsers using an exception rather than null
+  reinitStorage()
+}
+
+// support Brave and other browsers using null rather than an exception
+if (!peertubeLocalStorage || !peertubeSessionStorage) {
+  reinitStorage()
+}
+
 export {
   peertubeLocalStorage,
   peertubeSessionStorage