]>
Commit | Line | Data |
---|---|---|
0bd78bf3 C |
1 | // Thanks: https://github.com/capaj/localstorage-polyfill |
2 | ||
3 | const valuesMap = new Map() | |
4 | ||
fd8f73cf C |
5 | function proxify (instance: MemoryStorage) { |
6 | return new Proxy(instance, { | |
0f01a8ba | 7 | set: function (obj, prop: string | symbol, value) { |
9df52d66 | 8 | if (Object.prototype.hasOwnProperty.call(MemoryStorage, prop)) { |
0f01a8ba C |
9 | // FIXME: symbol typing issue https://github.com/microsoft/TypeScript/issues/1863 |
10 | instance[prop as any] = value | |
fd8f73cf C |
11 | } else { |
12 | instance.setItem(prop, value) | |
13 | } | |
14 | return true | |
15 | }, | |
0f01a8ba | 16 | get: function (target, name: string | symbol | number) { |
9df52d66 | 17 | if (Object.prototype.hasOwnProperty.call(MemoryStorage, name)) { |
0f01a8ba C |
18 | // FIXME: symbol typing issue https://github.com/microsoft/TypeScript/issues/1863 |
19 | return instance[name as any] | |
fd8f73cf C |
20 | } |
21 | if (valuesMap.has(name)) { | |
22 | return instance.getItem(name) | |
23 | } | |
24 | } | |
25 | }) | |
26 | } | |
27 | ||
0f01a8ba | 28 | class MemoryStorage implements Storage { |
0bd78bf3 | 29 | [key: string]: any |
0bd78bf3 | 30 | |
244b4ae3 | 31 | getItem (key: any) { |
0bd78bf3 C |
32 | const stringKey = String(key) |
33 | if (valuesMap.has(key)) { | |
34 | return String(valuesMap.get(stringKey)) | |
35 | } | |
36 | ||
37 | return null | |
38 | } | |
39 | ||
244b4ae3 | 40 | setItem (key: any, val: any) { |
0bd78bf3 C |
41 | valuesMap.set(String(key), String(val)) |
42 | } | |
43 | ||
244b4ae3 | 44 | removeItem (key: any) { |
0bd78bf3 C |
45 | valuesMap.delete(key) |
46 | } | |
47 | ||
48 | clear () { | |
49 | valuesMap.clear() | |
50 | } | |
51 | ||
52 | key (i: any) { | |
53 | if (arguments.length === 0) { | |
54 | throw new TypeError('Failed to execute "key" on "Storage": 1 argument required, but only 0 present.') | |
55 | } | |
56 | ||
57 | const arr = Array.from(valuesMap.keys()) | |
58 | return arr[i] | |
59 | } | |
60 | ||
61 | get length () { | |
62 | return valuesMap.size | |
63 | } | |
64 | } | |
65 | ||
66 | let peertubeLocalStorage: Storage | |
88a7f93f | 67 | let peertubeSessionStorage: Storage |
61379e43 RK |
68 | |
69 | function reinitStorage () { | |
6189b699 C |
70 | const instanceLocalStorage = new MemoryStorage() |
71 | const instanceSessionStorage = new MemoryStorage() | |
0bd78bf3 | 72 | |
6189b699 C |
73 | peertubeLocalStorage = proxify(instanceLocalStorage) |
74 | peertubeSessionStorage = proxify(instanceSessionStorage) | |
0bd78bf3 C |
75 | } |
76 | ||
61379e43 RK |
77 | try { |
78 | peertubeLocalStorage = localStorage | |
79 | peertubeSessionStorage = sessionStorage | |
80 | } catch (err) { | |
81 | // support Firefox and other browsers using an exception rather than null | |
82 | reinitStorage() | |
83 | } | |
84 | ||
85 | // support Brave and other browsers using null rather than an exception | |
86 | if (peertubeLocalStorage === null || peertubeSessionStorage === null) { | |
87 | reinitStorage() | |
88 | } | |
89 | ||
88a7f93f RK |
90 | export { |
91 | peertubeLocalStorage, | |
92 | peertubeSessionStorage | |
93 | } |