-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathresource.ts
More file actions
93 lines (80 loc) · 2.38 KB
/
resource.ts
File metadata and controls
93 lines (80 loc) · 2.38 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
90
91
92
93
import { type URLRuleEntry } from "@App/pkg/utils/url_matcher";
import { deletesStorage, loadCache, Repo } from "./repo";
import { v5 as uuidv5 } from "uuid";
export type ResourceType = "require" | "require-css" | "resource";
export interface Resource {
url: string; // key
content: string;
base64: string;
hash: ResourceHash;
type: ResourceType;
link: { [key: string]: boolean }; // 关联的脚本
contentType: string; // 下载成功的话必定有 contentType. 下载失败的话则没有 (空Resource)
createtime: number;
updatetime?: number;
}
export interface ResourceHash {
md5: string;
sha1: string;
sha256: string;
sha384: string;
sha512: string;
integrity?: {
md5: string;
sha1: string;
sha256: string;
sha384: string;
sha512: string;
};
}
export type CompiledResource = {
name: string;
flag: string;
uuid: string;
require: string[]; // 仅存储url,节省空间
matches: string[]; // primary
includeGlobs: string[]; // includeGlobs applied after matches
excludeMatches: string[];
excludeGlobs: string[];
allFrames: boolean;
world: string;
runAt: string;
scriptUrlPatterns: URLRuleEntry[];
originalUrlPatterns: URLRuleEntry[] | null;
};
export const ResourceNamespace = "76f45084-91b1-42c1-8be8-cbcc54b171f0";
export class ResourceDAO extends Repo<Resource> {
constructor() {
super("resource");
}
protected joinKey(key: string) {
return this.prefix + uuidv5(key, ResourceNamespace);
}
save(resource: Resource) {
return super._save(resource.url, resource);
}
}
// CompiledResource结构变更时,建议修改 CompiledResourceNamespace 以删除旧Cache
export const CompiledResourceNamespace = "57d79c56-231a-42d3-b6e3-d2004ba0866f";
export class CompiledResourceDAO extends Repo<CompiledResource> {
constructor() {
super(`compiled_resource`);
this.enableCache();
}
protected joinKey(key: string) {
return this.prefix + CompiledResourceNamespace + ":" + key;
}
save(resource: CompiledResource) {
return super._save(resource.uuid, resource);
}
}
// 清理无效的key
export const cleanInvalidKeys = async () => {
loadCache().then((cache) => {
const invalidKeys = Object.keys(cache).filter(
(key) =>
key.startsWith("compiled_resource:") && !key.startsWith("compiled_resource:" + CompiledResourceNamespace + ":")
);
deletesStorage(invalidKeys);
});
};