/
UrlRegistrationService.ts
81 lines (64 loc) · 2.51 KB
/
UrlRegistrationService.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
/* -*- indent-tabs-mode: nil; tab-width: 2; -*- */
/* vim: set ts=2 sw=2 et ai : */
/**
Container Tab Groups
Copyright (C) 2023 Menhera.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
@license
**/
import { StorageItem } from "weeg-storage";
import { ServiceRegistry } from "./ServiceRegistry";
import { StartupService } from "./StartupService";
import { RandomIdService } from "./RandomIdService";
type StorageType = {
[urlId: string]: string; // url
};
const startupService = StartupService.getInstance();
const randomIdService = RandomIdService.getInstance();
export class UrlRegistrationService {
private static readonly INSTANCE = new UrlRegistrationService();
public static getInstance(): UrlRegistrationService {
return this.INSTANCE;
}
private readonly _storage = new StorageItem<StorageType>('registeredUrls', {}, StorageItem.AREA_LOCAL);
private constructor() {
// nothing
}
public async resetStorage(): Promise<void> {
await this._storage.setValue({});
}
public async registerUrl(url: string): Promise<string> {
new URL(url); // throws for invalid URLs
const storage = await this._storage.getValue();
const urlId = randomIdService.getRandomId();
storage[urlId] = url;
await this._storage.setValue(storage);
return urlId;
}
public async getUrl(urlId: string): Promise<string | null> {
const storage = await this._storage.getValue();
return storage[urlId] ?? null;
}
public async getAndRevokeUrl(urlId: string): Promise<string | null> {
const storage = await this._storage.getValue();
const url = storage[urlId] ?? null;
delete storage[urlId];
await this._storage.setValue(storage);
return url;
}
}
ServiceRegistry.getInstance().registerService('UrlRegistrationService', UrlRegistrationService.getInstance());
startupService.onStartup.addListener(() => {
UrlRegistrationService.getInstance().resetStorage().catch((e) => {
console.error(e);
});
});