-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathlocalStorage.ts
203 lines (179 loc) · 6.43 KB
/
localStorage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
namespace pxt.storage {
interface IStorage {
removeItem(key: string): void;
getItem(key: string): string;
setItem(key: string, value: string): void;
clear(): void;
}
class MemoryStorage implements IStorage {
type: "memory";
items: pxt.Map<string> = {};
removeItem(key: string) {
delete this.items[key];
}
getItem(key: string): string {
return this.items[key];
}
setItem(key: string, value: string): void {
this.items[key] = value;
}
clear() {
this.items = {};
}
}
class LocalStorage implements IStorage {
type = "localstorage";
constructor(private storageId: string) {
}
targetKey(key: string): string {
return this.storageId + '/' + key;
}
removeItem(key: string) {
window.localStorage.removeItem(this.targetKey(key));
}
getItem(key: string): string {
return window.localStorage[this.targetKey(key)];
}
setItem(key: string, value: string): void {
window.localStorage[this.targetKey(key)] = value;
}
clear() {
let prefix = this.targetKey('');
let keys: string[] = [];
for (let i = 0; i < window.localStorage.length; ++i) {
let key = window.localStorage.key(i);
if (key.indexOf(prefix) == 0)
keys.push(key);
}
keys.forEach(key => window.localStorage.removeItem(key));
}
}
export function storageId(): string {
if (pxt.appTarget) return pxt.appTarget.id;
const cfg = (<any>window).pxtConfig;
if (cfg) return cfg.targetId;
const bndl = (<any>window).pxtTargetBundle;
if (bndl) return bndl.id;
return '';
}
let impl: IStorage;
function init() {
if (impl) return;
// test if local storage is supported
const sid = storageId();
let supported = false;
// no local storage in sandbox mode
if (!pxt.shell.isSandboxMode()) {
try {
const rand = pxt.Util.guidGen();
window.localStorage[sid] = rand;
const v = window.localStorage[sid];
supported = v === rand;
} catch (e) { }
}
if (!supported) {
impl = new MemoryStorage();
pxt.debug('storage: in memory');
} else {
impl = new LocalStorage(sid);
pxt.debug(`storage: local under ${sid}`)
}
}
export function setLocal(key: string, value: string) {
init()
impl.setItem(key, value);
}
export function getLocal(key: string): string {
init()
return impl.getItem(key);
}
export function removeLocal(key: string) {
init()
impl.removeItem(key);
}
export function clearLocal() {
init()
impl.clear();
}
}
/**
* Storage that will be shared across localhost frames when developing locally. Uses regular browser storage in production.
* One side effect: Localhost storage will be shared between different browsers and incognito tabs as well. To disable this
* behavior, set the `routingEnabled` switch below to `false`.
*/
namespace pxt.storage.shared {
/**
* Override switch. Setting this to `false` will stop routing calls to the pxt server, using browser storage instead.
*/
const routingEnabled = true;
// Specify host and port explicitly so that localhost frames not served on the default port (e.g. skillmap) can access it.
const localhostStoreUrl = "http://localhost:3232/api/store/";
function useSharedLocalStorage(): boolean {
return routingEnabled && pxt.BrowserUtils.isLocalHostDev() && !pxt.BrowserUtils.noSharedLocalStorage();
}
function sharedStorageNamespace(): string {
if (pxt.BrowserUtils.isChromiumEdge()) { return "chromium-edge"; }
return pxt.BrowserUtils.browser();
}
export async function getAsync<T>(container: string, key: string): Promise<T> {
if (useSharedLocalStorage()) {
container += '-' + sharedStorageNamespace();
const resp = await pxt.Util.requestAsync({
url: `${localhostStoreUrl}${encodeURIComponent(container)}/${encodeURIComponent(key)}`,
method: "GET",
allowHttpErrors: true
});
if (resp.statusCode === 204) {
throw new Error(`Missing ${key} not available in ${container}`);
} else if (resp.json) {
return resp.json as T;
} else if (resp.text) {
return resp.text as any as T;
} else {
return undefined;
}
} else {
const sval = pxt.storage.getLocal(`${container}:${key}`);
const val = pxt.Util.jsonTryParse(sval);
return val ? val : sval;
}
}
export async function setAsync(container: string, key: string, val: any): Promise<void> {
if (typeof val == "undefined") {
await pxt.storage.shared.delAsync(container, key);
return;
}
let sval = "";
if (typeof val === "object")
sval = JSON.stringify(val);
else
sval = val.toString();
if (useSharedLocalStorage()) {
container += '-' + sharedStorageNamespace();
const data = {
type: (typeof val === "object") ? "json" : "text",
val: sval
};
const sdata = JSON.stringify(data);
await pxt.Util.requestAsync({
url: `${localhostStoreUrl}${encodeURIComponent(container)}/${encodeURIComponent(key)}`,
method: "POST",
data: sdata
});
} else {
pxt.storage.setLocal(`${container}:${key}`, sval);
}
}
export async function delAsync(container: string, key: string): Promise<void> {
if (useSharedLocalStorage()) {
container += '-' + sharedStorageNamespace();
await pxt.Util.requestAsync({
url: `${localhostStoreUrl}${encodeURIComponent(container)}/${encodeURIComponent(key)}`,
method: "DELETE",
allowHttpErrors: true
});
} else {
pxt.storage.removeLocal(`${container}:${key}`);
}
}
}