1 // Thanks: https://github.com/capaj/localstorage-polyfill
3 const valuesMap = new Map()
5 function proxify (instance: MemoryStorage) {
6 return new Proxy(instance, {
7 set: function (obj, prop: string | symbol, value) {
8 if (Object.prototype.hasOwnProperty.call(MemoryStorage, prop)) {
9 // FIXME: remove cast on typescript upgrade
10 instance[prop as any] = value
12 instance.setItem(prop, value)
17 get: function (target, name: string | symbol | number) {
18 // FIXME: remove cast on typescript upgrade
19 if (typeof instance[name as any] === 'function') {
20 // FIXME: remove cast on typescript upgrade
21 return instance[name as any]
22 } else if (valuesMap.has(name)) {
23 return instance.getItem(name)
29 class MemoryStorage implements Storage {
33 const stringKey = String(key)
34 if (valuesMap.has(key)) {
35 return String(valuesMap.get(stringKey))
41 setItem (key: any, val: any) {
42 valuesMap.set(String(key), String(val))
45 removeItem (key: any) {
54 if (arguments.length === 0) {
55 throw new TypeError('Failed to execute "key" on "Storage": 1 argument required, but only 0 present.')
58 const arr = Array.from(valuesMap.keys())
67 let peertubeLocalStorage: Storage
68 let peertubeSessionStorage: Storage
70 function reinitStorage () {
71 const instanceLocalStorage = new MemoryStorage()
72 const instanceSessionStorage = new MemoryStorage()
74 peertubeLocalStorage = proxify(instanceLocalStorage)
75 peertubeSessionStorage = proxify(instanceSessionStorage)
79 peertubeLocalStorage = localStorage
80 peertubeSessionStorage = sessionStorage
82 // support Firefox and other browsers using an exception rather than null
86 // support Brave and other browsers using null rather than an exception
87 if (!peertubeLocalStorage || !peertubeSessionStorage) {
93 peertubeSessionStorage