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)) {
11 instance.setItem(prop, value)
16 get: function (target, name: string | symbol | number) {
17 if (typeof instance[name] === 'function') {
19 } else if (valuesMap.has(name)) {
20 return instance.getItem(name)
26 class MemoryStorage implements Storage {
27 [key: string | symbol]: any
30 const stringKey = String(key)
31 if (valuesMap.has(key)) {
32 return String(valuesMap.get(stringKey))
38 setItem (key: any, val: any) {
39 valuesMap.set(String(key), String(val))
42 removeItem (key: any) {
51 if (arguments.length === 0) {
52 throw new TypeError('Failed to execute "key" on "Storage": 1 argument required, but only 0 present.')
55 const arr = Array.from(valuesMap.keys())
64 let peertubeLocalStorage: Storage
65 let peertubeSessionStorage: Storage
67 function reinitStorage () {
68 const instanceLocalStorage = new MemoryStorage()
69 const instanceSessionStorage = new MemoryStorage()
71 peertubeLocalStorage = proxify(instanceLocalStorage)
72 peertubeSessionStorage = proxify(instanceSessionStorage)
76 peertubeLocalStorage = localStorage
77 peertubeSessionStorage = sessionStorage
79 // support Firefox and other browsers using an exception rather than null
83 // support Brave and other browsers using null rather than an exception
84 if (!peertubeLocalStorage || !peertubeSessionStorage) {
90 peertubeSessionStorage