-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathclassic.ts
More file actions
89 lines (80 loc) · 2.43 KB
/
Copy pathclassic.ts
File metadata and controls
89 lines (80 loc) · 2.43 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
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
/**
* Copyright (c) 2021 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { CATE_NOT_SET_ID } from '@util/site'
import BaseDatabase from '../common/base-database'
import { REMAIN_WORD_PREFIX } from '../common/constant'
type SiteEntry = {
/**
* Alias
*/
a?: string
/**
* Icon url
*/
i?: string
/**
* Category ID
*/
c?: number
/**
* Run time
*/
r?: boolean
}
const DB_KEY_PREFIX = REMAIN_WORD_PREFIX + 'SITE_'
const HOST_KEY_PREFIX = DB_KEY_PREFIX + 'h'
const VIRTUAL_KEY_PREFIX = DB_KEY_PREFIX + 'v'
const MERGED_FLAG = 'm'
function cvt2Key({ host, type }: tt4b.site.SiteKey): string {
switch (type) {
case 'virtual': return VIRTUAL_KEY_PREFIX + host
case 'merged': return HOST_KEY_PREFIX + MERGED_FLAG + host
case 'normal': return HOST_KEY_PREFIX + '_' + host
}
}
function cvt2SiteKey(key: string): tt4b.site.SiteKey {
if (key.startsWith(VIRTUAL_KEY_PREFIX)) {
return {
host: key.substring(VIRTUAL_KEY_PREFIX.length),
type: 'virtual',
}
}
if (key.startsWith(HOST_KEY_PREFIX)) {
return {
host: key.substring(HOST_KEY_PREFIX.length + 1),
type: key.charAt(HOST_KEY_PREFIX.length) === MERGED_FLAG ? 'merged' : 'normal',
}
}
return { host: key, type: 'normal' }
}
function cvt2SiteInfo(key: tt4b.site.SiteKey, entry: SiteEntry | undefined): tt4b.site.SiteInfo {
const { a, i, c, r } = entry ?? {}
const siteInfo: tt4b.site.SiteInfo = { ...key }
siteInfo.alias = a
siteInfo.cate = c ?? CATE_NOT_SET_ID
siteInfo.iconUrl = i
siteInfo.options = { run: r }
return siteInfo
}
function isSiteKey(key: string): boolean {
return key.startsWith(HOST_KEY_PREFIX) || key.startsWith(VIRTUAL_KEY_PREFIX)
}
/**
* @deprecated Use IDBSiteDatabase instead, this will be removed in the future
*/
export class ClassicSiteDatabase extends BaseDatabase {
async select(): Promise<tt4b.site.SiteInfo[]> {
const data = await this.storage.get()
return Object.entries(data)
.filter(([key]) => isSiteKey(key))
.map(([key, value]) => cvt2SiteInfo(cvt2SiteKey(key), value as SiteEntry))
}
async remove(siteKeys: tt4b.site.SiteKey[]): Promise<void> {
if (!siteKeys.length) return
await this.storage.remove(siteKeys.map(cvt2Key))
}
}