Repository navigation
Expand file tree
/
Copy pathcomputers.ts
More file actions
39 lines (39 loc) · 2.24 KB
/
Copy pathcomputers.ts
File metadata and controls
39 lines (39 loc) · 2.24 KB
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
type Computer = { name: string; url: string; token: string };
type Storage = { read(): Promise<unknown>; write(value: string): Promise<unknown> };
function isComputer(value: unknown): value is Computer {
return typeof value === 'object' && value !== null && 'name' in value && typeof value.name === 'string'
&& 'url' in value && typeof value.url === 'string' && 'token' in value && typeof value.token === 'string';
}
export function createComputers(storage: Storage) {
let entries: Computer[] = [];
let loaded = false;
let queue: Promise<void> = Promise.resolve();
function serial(action: () => Promise<void>) { const next = queue.then(action); queue = next.catch(() => {}); return next; }
return {
load() { return serial(async () => {
const raw = await storage.read();
if (typeof raw !== 'string') throw Error('Could not read app storage.');
const value: unknown = raw ? JSON.parse(raw) : [];
if (!Array.isArray(value) || value.some(x => !isComputer(x))) throw Error('Could not read saved computers.');
entries = value.filter(isComputer); loaded = true;
}); },
list() { return entries.map(x => ({ ...x })); },
save({ name, url, token }: Computer) { return serial(async () => {
if (!loaded) throw Error('Loading saved computers.');
let parsed;
try { parsed = new URL(url.trim()); } catch { throw Error('Enter a valid server address.'); }
if (!['http:', 'https:'].includes(parsed.protocol) || parsed.username || parsed.password || parsed.search || parsed.hash || parsed.pathname !== '/') throw Error('Enter a valid server address.');
name = name.trim(); token = token.trim(); url = parsed.origin;
if (!name || !token) throw Error('Enter a computer name and connection token.');
const index = entries.findIndex(x => x.url === url);
const next = entries.slice();
if (index < 0) next.push({ name, url, token }); else next[index] = { name, url, token };
await storage.write(JSON.stringify(next)); entries = next;
}); },
remove(url: string) { return serial(async () => {
if (!loaded) throw Error('Loading saved computers.');
const next = entries.filter(x => x.url !== url);
await storage.write(JSON.stringify(next)); entries = next;
}); },
};
}