-
Notifications
You must be signed in to change notification settings - Fork 6
/
storage.ts
233 lines (217 loc) · 6.08 KB
/
storage.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import {
readDirSync,
readFileSync,
removeSync,
statSync,
writeFileSync,
} from './fs';
import { URL } from './polyfills/url';
import { Profile } from './switch/profile';
import { Application } from './switch/ns';
import { INTERNAL_SYMBOL } from './internal';
import {
assertInternalConstructor,
createInternal,
decodeUTF16,
def,
encodeUTF16,
} from './utils';
interface StorageImpl {
clear(): void;
getItem(key: string): string | null;
key(index: number): string | null;
removeItem(key: string): void;
setItem(key: string, value: string): void;
length(): number;
}
const _ = createInternal<Storage, StorageImpl>();
/**
* @see https://developer.mozilla.org/docs/Web/API/Storage
*/
export class Storage implements globalThis.Storage {
/**
* @ignore
*/
constructor() {
assertInternalConstructor(arguments);
_.set(this, arguments[1]);
}
[name: string]: any;
/**
* The number of data items stored in the data store.
*
* @see https://developer.mozilla.org/docs/Web/API/Storage/length
*/
get length() {
return _(this).length();
}
/**
* Clears all keys stored in a data store.
*
* @see https://developer.mozilla.org/docs/Web/API/Storage/clear
*/
clear(): void {
_(this).clear();
}
/**
* Get an item from the provided key from the data store.
*
* @param key The name of the key you want to retrieve the value of.
* @returns A string containing the value of the key. If the key does not exist, `null` is returned.
* @see https://developer.mozilla.org/docs/Web/API/Storage/getItem
*/
getItem(key: string): string | null {
return _(this).getItem(key);
}
/**
* Retrieves the name of the nth key in the data store. The order
* of keys is user-agent defined, so you should not rely on it.
*
* @param index The number of the key you want to get the name of. This is a zero-based index.
* @returns A string containing the name of the key. If the index does not exist, `null` is returned.
* @see https://developer.mozilla.org/docs/Web/API/Storage/key
*/
key(index: number): string | null {
return _(this).key(index);
}
/**
* Removes the provided key from the data store, if it exists.
*
* If there is no item associated with the given key, this method will do nothing.
*
* @param key The name of the key you want to remove.
* @see https://developer.mozilla.org/docs/Web/API/Storage/removeItem
*/
removeItem(key: string): void {
_(this).removeItem(key);
}
/**
* Adds or updates the key to the data store with the provided value.
*
* @param key The name of the key you want to create / update.
* @param value The value you want to give the key you are creating / updating.
* @see https://developer.mozilla.org/docs/Web/API/Storage/setItem
*/
setItem(key: string, value: string): void {
_(this).setItem(key, value);
}
}
def(Storage);
/**
* `localStorage` is a {@link Storage} instance which persists to the system's Save Data filesystem.
*
* When `localStorage` is accessed for the first time, the currently selected user profile is checked
* to determine the profile which the data will be stored for. If no user profile is currently selected,
* then the profile selection interface is shown.
*
* @see https://developer.mozilla.org/docs/Web/API/Window/localStorage
*/
export declare var localStorage: Storage;
Object.defineProperty(globalThis, 'localStorage', {
enumerable: true,
configurable: true,
get() {
const { self } = Application;
let profile = Profile.current;
while (!profile) {
profile = Profile.current = Profile.select();
}
let saveData = self.findSaveData({
type: 1 /* FsSaveDataType_Account */,
uid: profile.uid,
});
if (!saveData) {
saveData = self.createProfileSaveDataSync(profile);
}
const base = new URL('localStorage/', saveData.mount());
const keyToPath = (key: any) => {
const url = `${base.href}_${Array.from(String(key))
.map((l) => l.charCodeAt(0).toString(16))
.join('_')}`;
return url;
};
const pathToKey = (path: string) => {
const key = String.fromCharCode(
...path
.slice(1)
.split('_')
.map((h) => parseInt(h, 16)),
);
return key;
};
const impl: StorageImpl = {
clear() {
removeSync(base);
saveData!.commit();
},
getItem(key: string): string | null {
const b = readFileSync(keyToPath(key));
if (!b) return null;
return decodeUTF16(b);
},
key(index: number): string | null {
if (index < 0) return null;
const i = index % 0x100000000;
const keys = readDirSync(base) || [];
if (i >= keys.length) return null;
return pathToKey(keys[i]);
},
removeItem(key: string): void {
removeSync(keyToPath(key));
saveData!.commit();
},
setItem(key: string, value: string): void {
writeFileSync(keyToPath(key), encodeUTF16(String(value)));
saveData!.commit();
},
length(): number {
const keys = readDirSync(base);
return keys ? keys.length : 0;
},
};
// @ts-expect-error internal constructor
const storage = new Storage(INTERNAL_SYMBOL, impl);
const proxy = new Proxy(storage, {
has(_, p) {
if (typeof p !== 'string') return false;
const s = statSync(keyToPath(p));
return s !== null;
},
get(target, p) {
if (typeof p !== 'string') return undefined;
if (typeof target[p] !== 'undefined') {
return target[p];
}
return target.getItem(p) ?? undefined;
},
set(target, p, newValue) {
if (typeof p !== 'string') return false;
target.setItem(p, newValue);
return true;
},
deleteProperty(target, p) {
if (typeof p !== 'string') return false;
target.removeItem(p);
return true;
},
ownKeys() {
return (readDirSync(base) || []).map((p) => pathToKey(p));
},
getOwnPropertyDescriptor(target, p) {
if (typeof p !== 'string') return;
if (typeof target[p] !== 'undefined') return;
const v = this.get!(target, p, target);
if (typeof v === 'undefined') return;
return {
enumerable: true,
configurable: true,
writable: true,
value: v,
};
},
});
_.set(proxy, impl);
Object.defineProperty(globalThis, 'localStorage', { value: proxy });
return proxy;
},
});