diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2020-08-03 18:06:49 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2020-08-06 15:08:58 +0200 |
commit | 4504f09f6e85f09b0489debb547a17209d7176ea (patch) | |
tree | 1e2fdcdd3a0982f6e873205f69bdf90de7f4a883 /client/src/root-helpers/peertube-web-storage.ts | |
parent | 71ab65d02f359000f9ca6a00f163d66d56a33955 (diff) | |
download | PeerTube-4504f09f6e85f09b0489debb547a17209d7176ea.tar.gz PeerTube-4504f09f6e85f09b0489debb547a17209d7176ea.tar.zst PeerTube-4504f09f6e85f09b0489debb547a17209d7176ea.zip |
deal with refresh token in embed
Diffstat (limited to 'client/src/root-helpers/peertube-web-storage.ts')
-rw-r--r-- | client/src/root-helpers/peertube-web-storage.ts | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/client/src/root-helpers/peertube-web-storage.ts b/client/src/root-helpers/peertube-web-storage.ts new file mode 100644 index 000000000..0db1301bd --- /dev/null +++ b/client/src/root-helpers/peertube-web-storage.ts | |||
@@ -0,0 +1,81 @@ | |||
1 | // Thanks: https://github.com/capaj/localstorage-polyfill | ||
2 | |||
3 | const valuesMap = new Map() | ||
4 | |||
5 | function proxify (instance: MemoryStorage) { | ||
6 | return new Proxy(instance, { | ||
7 | set: function (obj, prop: string | number, value) { | ||
8 | if (MemoryStorage.prototype.hasOwnProperty(prop)) { | ||
9 | instance[prop] = value | ||
10 | } else { | ||
11 | instance.setItem(prop, value) | ||
12 | } | ||
13 | return true | ||
14 | }, | ||
15 | get: function (target, name: string | number) { | ||
16 | if (MemoryStorage.prototype.hasOwnProperty(name)) { | ||
17 | return instance[name] | ||
18 | } | ||
19 | if (valuesMap.has(name)) { | ||
20 | return instance.getItem(name) | ||
21 | } | ||
22 | } | ||
23 | }) | ||
24 | } | ||
25 | |||
26 | class MemoryStorage { | ||
27 | [key: string]: any | ||
28 | [index: number]: string | ||
29 | |||
30 | getItem (key: any) { | ||
31 | const stringKey = String(key) | ||
32 | if (valuesMap.has(key)) { | ||
33 | return String(valuesMap.get(stringKey)) | ||
34 | } | ||
35 | |||
36 | return null | ||
37 | } | ||
38 | |||
39 | setItem (key: any, val: any) { | ||
40 | valuesMap.set(String(key), String(val)) | ||
41 | } | ||
42 | |||
43 | removeItem (key: any) { | ||
44 | valuesMap.delete(key) | ||
45 | } | ||
46 | |||
47 | clear () { | ||
48 | valuesMap.clear() | ||
49 | } | ||
50 | |||
51 | key (i: any) { | ||
52 | if (arguments.length === 0) { | ||
53 | throw new TypeError('Failed to execute "key" on "Storage": 1 argument required, but only 0 present.') | ||
54 | } | ||
55 | |||
56 | const arr = Array.from(valuesMap.keys()) | ||
57 | return arr[i] | ||
58 | } | ||
59 | |||
60 | get length () { | ||
61 | return valuesMap.size | ||
62 | } | ||
63 | } | ||
64 | |||
65 | let peertubeLocalStorage: Storage | ||
66 | let peertubeSessionStorage: Storage | ||
67 | try { | ||
68 | peertubeLocalStorage = localStorage | ||
69 | peertubeSessionStorage = sessionStorage | ||
70 | } catch (err) { | ||
71 | const instanceLocalStorage = new MemoryStorage() | ||
72 | const instanceSessionStorage = new MemoryStorage() | ||
73 | |||
74 | peertubeLocalStorage = proxify(instanceLocalStorage) | ||
75 | peertubeSessionStorage = proxify(instanceSessionStorage) | ||
76 | } | ||
77 | |||
78 | export { | ||
79 | peertubeLocalStorage, | ||
80 | peertubeSessionStorage | ||
81 | } | ||