sinzmise-cetastories 2.0.0-1731064238567 → 2.0.0-1731114112124
Sign up to get free protection for your applications and to get access to all the features.
- package/404/index.html +4033 -1
- package/about/index.html +4041 -1
- package/album/index.html +4050 -1
- package/archives/2021/12/index.html +3917 -1
- package/archives/2021/index.html +3917 -1
- package/archives/2022/12/index.html +3917 -1
- package/archives/2022/index.html +3917 -1
- package/archives/2023/01/index.html +4072 -1
- package/archives/2023/02/index.html +3917 -1
- package/archives/2023/03/index.html +4063 -1
- package/archives/2023/04/index.html +3992 -1
- package/archives/2023/07/index.html +4217 -1
- package/archives/2023/08/index.html +4067 -1
- package/archives/2023/09/index.html +3917 -1
- package/archives/2023/10/index.html +3917 -1
- package/archives/2023/11/index.html +3917 -1
- package/archives/2023/12/index.html +3918 -1
- package/archives/2023/index.html +4591 -1
- package/archives/2023/page/2/index.html +4572 -1
- package/archives/2023/page/3/index.html +3930 -1
- package/archives/2024/01/index.html +4142 -1
- package/archives/2024/02/index.html +3922 -1
- package/archives/2024/03/index.html +3916 -1
- package/archives/2024/04/index.html +3997 -1
- package/archives/2024/05/index.html +4065 -1
- package/archives/2024/06/index.html +3917 -1
- package/archives/2024/07/index.html +3917 -1
- package/archives/2024/08/index.html +3992 -1
- package/archives/2024/09/index.html +3917 -1
- package/archives/2024/10/index.html +3984 -1
- package/archives/2024/index.html +4576 -1
- package/archives/2024/page/2/index.html +4433 -1
- package/archives/index.html +4560 -1
- package/archives/page/2/index.html +4560 -1
- package/archives/page/3/index.html +4560 -1
- package/archives/page/4/index.html +4560 -1
- package/archives/page/5/index.html +4560 -1
- package/assets/algolia/algoliasearch.js +7182 -3
- package/assets/algolia/algoliasearchLite.js +4445 -3
- package/atom.xml +31 -31
- package/bbs/bbs.html +4012 -1
- package/bbs/bbs.js +12 -2
- package/bbs/index.html +4031 -1
- package/categories/index.html +4050 -1
- package/categories//344/270/252/344/272/272/345/260/217/350/256/260/index.html +4570 -1
- package/categories//344/270/252/344/272/272/345/260/217/350/256/260/page/2/index.html +4485 -1
- package/categories//346/255/214/346/233/262/346/224/266/351/233/206/index.html +3920 -1
- package/categories//346/270/270/346/210/217/347/233/270/345/205/263/index.html +4148 -1
- package/categories//347/253/231/347/202/271/346/212/230/350/205/276/index.html +4610 -1
- package/categories//347/253/231/347/202/271/346/212/230/350/205/276/page/2/index.html +4148 -1
- package/categories//351/241/271/347/233/256/346/212/230/350/205/276/index.html +4072 -1
- package/charts/index.html +4039 -1
- package/comments/index.html +4052 -1
- package/content.json +1 -0
- package/css/custom2.css +3 -2
- package/css/first.css +1868 -2
- package/css/pace/big-counter.css +39 -2
- package/css/pace/corner-indicator.css +72 -2
- package/css/pace/flash.css +76 -2
- package/css/pace/mac-osx.css +86 -2
- package/css/pace/pace.css +50 -2
- package/css/plane.css +59 -2
- package/css/style.css +8585 -2
- package/css/tags.css +3516 -2
- package/essay/index.html +4050 -1
- package/fcircle/index.html +4075 -1
- package/games +61 -19
- package/index.html +4717 -1
- package/js/app.js +1341 -2
- package/js/autoload.js +62 -2
- package/js/ceta/fancybox.css +2 -2
- package/js/ceta/fancybox.umd.js +2 -2
- package/js/ceta/iconfontInkss.js +1 -2
- package/js/ceta/message.css +6 -2
- package/js/ceta/message.js +6 -2
- package/js/clock.js +1 -2
- package/js/kslink.js +38 -2
- package/js/newyear.js +76 -2
- package/js/plane.js +2200 -2
- package/js/plugins/aplayer.js +186 -2
- package/js/plugins/parallax.js +191 -2
- package/js/plugins/rightMenus.js +623 -2
- package/js/plugins/tags/contributors.js +92 -2
- package/js/plugins/tags/friends.js +93 -2
- package/js/plugins/tags/sites.js +96 -2
- package/js/search/algolia.js +239 -2
- package/js/search/hexo.js +195 -2
- package/js/search/meilisearch.js +220 -2
- package/js/txmap.js +248 -2
- package/link/index.html +6180 -1
- package/load.html +294 -1
- package/music/index.html +4033 -1
- package/package.json +1 -1
- package/page/2/index.html +4578 -1
- package/page/3/index.html +4575 -1
- package/page/4/index.html +4572 -1
- package/page/5/index.html +3924 -1
- package/posts/10021/index.html +4130 -5
- package/posts/10045/index.html +4224 -1
- package/posts/10733/index.html +4125 -1
- package/posts/10996/index.html +4127 -5
- package/posts/12779/index.html +4134 -1
- package/posts/13624/index.html +4179 -1
- package/posts/15688/index.html +4133 -1
- package/posts/15748/index.html +4121 -5
- package/posts/15799/index.html +4130 -1
- package/posts/15842/index.html +4121 -5
- package/posts/16107/index.html +4111 -1
- package/posts/18063/index.html +4124 -1
- package/posts/20412/index.html +4134 -1
- package/posts/21375/index.html +4124 -1
- package/posts/22945/index.html +4169 -4
- package/posts/23021/index.html +4104 -1
- package/posts/27531/index.html +4121 -5
- package/posts/28536/index.html +4116 -1
- package/posts/28733/index.html +4168 -1
- package/posts/29196/index.html +4106 -1
- package/posts/38917/index.html +4144 -1
- package/posts/38964/index.html +4208 -1
- package/posts/42487/index.html +4135 -1
- package/posts/42580/index.html +4128 -1
- package/posts/45875/index.html +4118 -1
- package/posts/46640/index.html +4147 -1
- package/posts/48762/index.html +4109 -1
- package/posts/50710/index.html +4114 -1
- package/posts/52677/index.html +4125 -1
- package/posts/53662/index.html +4123 -1
- package/posts/54386/index.html +4116 -1
- package/posts/54481/index.html +4133 -1
- package/posts/54787/index.html +4124 -5
- package/posts/56467/index.html +4121 -1
- package/posts/57692/index.html +4143 -1
- package/posts/58203/index.html +4116 -1
- package/posts/61417/index.html +4117 -1
- package/posts/61712/index.html +4113 -1
- package/posts/646/index.html +4106 -1
- package/posts/64856/index.html +4120 -1
- package/posts/64935/index.html +4120 -1
- package/search.xml +84 -84
- package/seas/index.html +4050 -1
- package/sw.js +798 -2
- package/tags/Flash/347/233/270/345/205/263/index.html +3924 -1
- package/tags/Steam/346/270/270/346/210/217/index.html +3924 -1
- package/tags/Windows/350/275/257/344/273/266/index.html +3997 -1
- package/tags/index.html +4033 -1
- package/tags//344/270/252/344/272/272/345/260/217/350/256/260/index.html +4570 -1
- package/tags//344/270/252/344/272/272/345/260/217/350/256/260/page/2/index.html +4485 -1
- package/tags//345/205/266/345/256/203/346/270/270/346/210/217/index.html +4073 -1
- package/tags//345/212/240/350/275/275/345/212/250/347/224/273/index.html +3924 -1
- package/tags//346/202/254/346/265/256/345/256/240/347/211/251-/347/234/213/346/235/277/345/250/230/index.html +3925 -1
- package/tags//346/255/214/346/233/262/346/224/266/351/233/206/index.html +3920 -1
- package/tags//346/270/270/346/210/217/347/233/270/345/205/263/index.html +4148 -1
- package/tags//347/253/231/347/202/271/345/272/225/351/203/250/351/255/224/346/224/271/index.html +3924 -1
- package/tags//347/253/231/347/202/271/346/212/230/350/205/276/index.html +4610 -1
- package/tags//347/253/231/347/202/271/346/212/230/350/205/276/page/2/index.html +4148 -1
- package/tags//350/207/252/345/273/272/351/203/250/347/275/262/index.html +4224 -1
- package/tags//351/241/265/351/235/242/351/255/224/346/224/271/index.html +4075 -1
- package/tags//351/241/271/347/233/256/346/212/230/350/205/276/index.html +4072 -1
- package/update/index.html +4112 -1
- package/weichuncai/css/style.css +25 -2
- package/weichuncai/js/common.js +449 -2
- package/weichuncai/js/jquery.js +139 -2
- package/weichuncai/wcc-config.js +17 -2
- package/maps/assets/algolia/algoliasearch.js.map +0 -1
- package/maps/assets/algolia/algoliasearchLite.js.map +0 -1
- package/maps/bbs/bbs.js.map +0 -1
- package/maps/css/custom2.css.map +0 -1
- package/maps/css/first.css.map +0 -1
- package/maps/css/pace/big-counter.css.map +0 -1
- package/maps/css/pace/corner-indicator.css.map +0 -1
- package/maps/css/pace/flash.css.map +0 -1
- package/maps/css/pace/mac-osx.css.map +0 -1
- package/maps/css/pace/pace.css.map +0 -1
- package/maps/css/plane.css.map +0 -1
- package/maps/css/style.css.map +0 -1
- package/maps/css/tags.css.map +0 -1
- package/maps/js/app.js.map +0 -1
- package/maps/js/autoload.js.map +0 -1
- package/maps/js/ceta/fancybox.css.map +0 -1
- package/maps/js/ceta/fancybox.umd.js.map +0 -1
- package/maps/js/ceta/iconfontInkss.js.map +0 -1
- package/maps/js/ceta/message.css.map +0 -1
- package/maps/js/ceta/message.js.map +0 -1
- package/maps/js/clock.js.map +0 -1
- package/maps/js/kslink.js.map +0 -1
- package/maps/js/newyear.js.map +0 -1
- package/maps/js/plane.js.map +0 -1
- package/maps/js/plugins/aplayer.js.map +0 -1
- package/maps/js/plugins/parallax.js.map +0 -1
- package/maps/js/plugins/rightMenus.js.map +0 -1
- package/maps/js/plugins/tags/contributors.js.map +0 -1
- package/maps/js/plugins/tags/friends.js.map +0 -1
- package/maps/js/plugins/tags/sites.js.map +0 -1
- package/maps/js/search/algolia.js.map +0 -1
- package/maps/js/search/hexo.js.map +0 -1
- package/maps/js/search/meilisearch.js.map +0 -1
- package/maps/js/txmap.js.map +0 -1
- package/maps/sw.js.map +0 -1
- package/maps/weichuncai/css/style.css.map +0 -1
- package/maps/weichuncai/js/common.js.map +0 -1
- package/maps/weichuncai/js/jquery.js.map +0 -1
- package/maps/weichuncai/wcc-config.js.map +0 -1
package/sw.js
CHANGED
@@ -1,2 +1,798 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
// 全站打包上传 npm,sw 并发请求 cdn
|
2
|
+
const prefix = 'sinzmise-cetastories';
|
3
|
+
const cacheSuffixVersion = '00000018-0.5481051434706005';
|
4
|
+
const CACHE_NAME = prefix + '-v' + cacheSuffixVersion;
|
5
|
+
const PreCachlist = [
|
6
|
+
"/css/style.css",
|
7
|
+
"/js/app.js",
|
8
|
+
"/js/search/hexo.js",
|
9
|
+
];
|
10
|
+
let NPMMirror = true;
|
11
|
+
const NPMPackage = "sinzmise-cetastories";
|
12
|
+
let NPMPackageVersion = "latest";
|
13
|
+
let debug = true;
|
14
|
+
// location.hostname == 'localhost' && (debug = true) && (NPMMirror = false);
|
15
|
+
const handleFetch = async (event) => {
|
16
|
+
const url = event.request.url;
|
17
|
+
if (/nocache/.test(url)) {
|
18
|
+
return NetworkOnly(event)
|
19
|
+
} else if (/@latest/.test(url)) {
|
20
|
+
return CacheFirst(event)
|
21
|
+
} else if (/cdnjs\.cloudflare\.com/.test(url)) {
|
22
|
+
return CacheAlways(event)
|
23
|
+
} else if (/music\.126\.net/.test(url)) {
|
24
|
+
return CacheAlways(event)
|
25
|
+
} else if (/qqmusic\.qq\.com/.test(url)) {
|
26
|
+
return CacheAlways(event)
|
27
|
+
} else if (/jsdelivr\.net/.test(url)) {
|
28
|
+
return CacheAlways(event)
|
29
|
+
} else if (/npm\.elemecdn\.com/.test(url)) {
|
30
|
+
return CacheAlways(event)
|
31
|
+
} else if (/unpkg\.com/.test(url)) {
|
32
|
+
return CacheAlways(event)
|
33
|
+
} else if (/.*\.(?:png|jpg|jpeg|svg|gif|webp|ico|eot|ttf|woff|woff2|mp3)$/.test(url)) {
|
34
|
+
return CacheAlways(event)
|
35
|
+
} else if (/.*\.(css|js)$/.test(url)) {
|
36
|
+
return CacheAlways(event)
|
37
|
+
} else {
|
38
|
+
return CacheFirst(event)
|
39
|
+
}
|
40
|
+
}
|
41
|
+
const cdn = {
|
42
|
+
gh: {
|
43
|
+
jsdelivr: 'https://kkgithub.com/extdomains/cdn.jsdelivr.net/gh',
|
44
|
+
fastly: 'https://fastly.jsdelivr.net/gh',
|
45
|
+
gcore: 'https://gcore.jsdelivr.net/gh',
|
46
|
+
testingcf: 'https://testingcf.jsdelivr.net/gh',
|
47
|
+
test1: 'https://test1.jsdelivr.net/gh',
|
48
|
+
},
|
49
|
+
combine: {
|
50
|
+
jsdelivr: 'https://kkgithub.com/extdomains/cdn.jsdelivr.net/combine',
|
51
|
+
fastly: 'https://fastly.jsdelivr.net/combine',
|
52
|
+
gcore: 'https://gcore.jsdelivr.net/combine',
|
53
|
+
testingcf: 'https://testingcf.jsdelivr.net/combine',
|
54
|
+
test1: 'https://test1.jsdelivr.net/combine',
|
55
|
+
},
|
56
|
+
npm: {
|
57
|
+
jsdelivr: 'https://kkgithub.com/extdomains/cdn.jsdelivr.net/npm',
|
58
|
+
fastly: 'https://fastly.jsdelivr.net/npm',
|
59
|
+
gcore: 'https://gcore.jsdelivr.net/npm',
|
60
|
+
testingcf: 'https://testingcf.jsdelivr.net/npm',
|
61
|
+
test1: 'https://test1.jsdelivr.net/npm',
|
62
|
+
unpkg: 'https://unpkg.com',
|
63
|
+
eleme: 'https://npm.elemecdn.com',
|
64
|
+
storisinz: 'https://jsd.cdn.storisinz.site/npm',
|
65
|
+
webcache: 'https://npm.webcache.cn',
|
66
|
+
},
|
67
|
+
cdnjs: {
|
68
|
+
cdnjs: 'https://kkgithub.com/extdomains/cdnjs.cloudflare.com/ajax/libs',
|
69
|
+
baomitu: 'https://lib.baomitu.com',
|
70
|
+
bootcdn: 'https://cdn.bootcdn.net/ajax/libs',
|
71
|
+
bytedance: 'https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M',
|
72
|
+
sustech: 'https://mirrors.sustech.edu.cn/cdnjs/ajax/libs',
|
73
|
+
}
|
74
|
+
}
|
75
|
+
const cdn_match_list = []
|
76
|
+
for (const type in cdn) {
|
77
|
+
for (const key in cdn[type]) {
|
78
|
+
cdn_match_list.push({ type: type, key: cdn[type][key] })
|
79
|
+
}
|
80
|
+
}
|
81
|
+
const _console = console;
|
82
|
+
const color = {
|
83
|
+
black: '#000000',
|
84
|
+
red: '#FF0000',
|
85
|
+
green: '#008000',
|
86
|
+
yellow: '#FFFF00',
|
87
|
+
blue: '#0000FF',
|
88
|
+
magenta: '#FF00FF',
|
89
|
+
cyan: '#00FFFF',
|
90
|
+
white: '#FFFFFF',
|
91
|
+
};
|
92
|
+
const add = (...arr) => {
|
93
|
+
let fi = [
|
94
|
+
[]
|
95
|
+
];
|
96
|
+
for (let key = 0; key < arr.length; key++) {
|
97
|
+
const [first, ...other] = arr[key];
|
98
|
+
fi[0] += first;
|
99
|
+
fi = fi.concat(other);
|
100
|
+
}
|
101
|
+
return fi;
|
102
|
+
};
|
103
|
+
const createlog = (util) => (...args) => {
|
104
|
+
// const fun = _console[util] ? _console[util] : _console.log;
|
105
|
+
const fun = util == "error" ? _console[util] : _console.log;
|
106
|
+
fun.apply(void 0, args);
|
107
|
+
};
|
108
|
+
const creategroup = (util) => (...args) => {
|
109
|
+
const fun = _console.groupCollapsed;
|
110
|
+
fun.apply(void 0, args);
|
111
|
+
};
|
112
|
+
const colorUtils = {
|
113
|
+
bold: (str) => {
|
114
|
+
if (typeof str === 'string' || typeof str === 'number') {
|
115
|
+
return `${str};font-weight: bold;`;
|
116
|
+
}
|
117
|
+
for (let key = 1; key < str.length; key++) {
|
118
|
+
str[key] += `;font-weight: bold;`;
|
119
|
+
}
|
120
|
+
return str;
|
121
|
+
}
|
122
|
+
};
|
123
|
+
const colorHash = {
|
124
|
+
log: 'black',
|
125
|
+
wait: 'cyan',
|
126
|
+
error: 'red',
|
127
|
+
warn: 'yellow',
|
128
|
+
ready: 'green',
|
129
|
+
info: 'blue',
|
130
|
+
event: 'magenta',
|
131
|
+
};
|
132
|
+
const createChalk = (name) => (...str) => {
|
133
|
+
if (typeof str[0] === 'object') {
|
134
|
+
createlog(name)(...add(colorUtils.bold(colorUtils[colorHash[name]](`[${firstToUpperCase(name)}] `)), ...str));
|
135
|
+
return;
|
136
|
+
}
|
137
|
+
let strArr = str;
|
138
|
+
if (typeof str === 'string' || typeof str === 'number') {
|
139
|
+
strArr = colorUtils[colorHash[name]](str);
|
140
|
+
}
|
141
|
+
createlog(name)(...add(colorUtils.bold(colorUtils[colorHash[name]](`[${firstToUpperCase(name)}] `)), strArr));
|
142
|
+
};
|
143
|
+
const createChalkBg = (name) => (...str) => {
|
144
|
+
if (typeof str[0] === 'object') {
|
145
|
+
createlog(name)(...add(colorUtils.bold(colorUtils[`bg${firstToUpperCase(colorHash[name])}`](`[${firstToUpperCase(name)}] `)), ...str));
|
146
|
+
return;
|
147
|
+
}
|
148
|
+
let strArr = str;
|
149
|
+
if (typeof str === 'string' || typeof str === 'number') {
|
150
|
+
strArr = colorUtils[colorHash[name]](str);
|
151
|
+
}
|
152
|
+
createlog(name)(...add(colorUtils.bold(colorUtils[`bg${firstToUpperCase(colorHash[name])}`](`[${firstToUpperCase(name)}] `)), strArr));
|
153
|
+
};
|
154
|
+
const createChalkGroup = (name) => (...str) => {
|
155
|
+
if (typeof str[0] === 'object') {
|
156
|
+
creategroup(name)(...add(colorUtils.bold(colorUtils[colorHash[name]](`[${firstToUpperCase(name)}] `)), ...str));
|
157
|
+
return;
|
158
|
+
}
|
159
|
+
let strArr = str;
|
160
|
+
if (typeof str === 'string' || typeof str === 'number') {
|
161
|
+
strArr = colorUtils[colorHash[name]](str);
|
162
|
+
}
|
163
|
+
creategroup(name)(...add(colorUtils.bold(colorUtils[colorHash[name]](`[${firstToUpperCase(name)}] `)), strArr));
|
164
|
+
};
|
165
|
+
const chalk = {
|
166
|
+
group: {
|
167
|
+
end: _console.groupEnd
|
168
|
+
},
|
169
|
+
bg: {}
|
170
|
+
};
|
171
|
+
Object.keys(colorHash).forEach(key => {
|
172
|
+
chalk[key] = createChalk(key);
|
173
|
+
chalk.group[key] = createChalkGroup(key);
|
174
|
+
chalk.bg[key] = createChalkBg(key);
|
175
|
+
});
|
176
|
+
const firstToUpperCase = (str) => str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
|
177
|
+
Object.keys(color).forEach(key => {
|
178
|
+
colorUtils[key] = (str) => {
|
179
|
+
if (typeof str === 'string' || typeof str === 'number') {
|
180
|
+
return [`%c${str}`, `color:${color[key]}`];
|
181
|
+
}
|
182
|
+
for (let i = 1; i < str.length; i++) {
|
183
|
+
str[i] += `;color:${color[key]}`;
|
184
|
+
}
|
185
|
+
return str;
|
186
|
+
};
|
187
|
+
colorUtils[`bg${firstToUpperCase(key)}`] = (str) => {
|
188
|
+
if (typeof str === 'string' || typeof str === 'number') {
|
189
|
+
return [`%c${str}`, `padding: 2px 4px; border-radius: 3px; color: ${key === 'white' ? '#000' : '#fff'}; font-weight: bold; background:${color[key]};`];
|
190
|
+
}
|
191
|
+
for (let i = 1; i < str.length; i++) {
|
192
|
+
str[i] += `;padding: 2px 4px; border-radius: 3px; font-weight: bold; background:${color[key]};`;
|
193
|
+
}
|
194
|
+
return str;
|
195
|
+
};
|
196
|
+
});
|
197
|
+
self.logger = {
|
198
|
+
add,
|
199
|
+
...chalk,
|
200
|
+
...colorUtils,
|
201
|
+
};
|
202
|
+
|
203
|
+
if (!debug) {
|
204
|
+
logger = {
|
205
|
+
log: () => { },
|
206
|
+
wait: () => { },
|
207
|
+
error: () => { },
|
208
|
+
warn: () => { },
|
209
|
+
ready: () => { },
|
210
|
+
info: () => { },
|
211
|
+
event: () => { },
|
212
|
+
group: {
|
213
|
+
log: () => { },
|
214
|
+
wait: () => { },
|
215
|
+
error: () => { },
|
216
|
+
warn: () => { },
|
217
|
+
ready: () => { },
|
218
|
+
info: () => { },
|
219
|
+
event: () => { },
|
220
|
+
end: () => { },
|
221
|
+
},
|
222
|
+
bg: {
|
223
|
+
log: () => { },
|
224
|
+
wait: () => { },
|
225
|
+
error: () => { },
|
226
|
+
warn: () => { },
|
227
|
+
ready: () => { },
|
228
|
+
info: () => { },
|
229
|
+
event: () => { },
|
230
|
+
}
|
231
|
+
};
|
232
|
+
console.log = () => { };
|
233
|
+
}
|
234
|
+
|
235
|
+
const generate_uuid = () => {
|
236
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
237
|
+
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
238
|
+
return v.toString(16);
|
239
|
+
});
|
240
|
+
}
|
241
|
+
self.db = {
|
242
|
+
read: (key, config) => {
|
243
|
+
if (!config) { config = { type: "text" } }
|
244
|
+
return new Promise((resolve, reject) => {
|
245
|
+
caches.open(CACHE_NAME).then(cache => {
|
246
|
+
cache.match(new Request(`https://LOCALCACHE/${encodeURIComponent(key)}`)).then(function (res) {
|
247
|
+
if (!res) resolve(null)
|
248
|
+
res.text().then(text => resolve(text))
|
249
|
+
}).catch(() => {
|
250
|
+
resolve(null)
|
251
|
+
})
|
252
|
+
})
|
253
|
+
})
|
254
|
+
},
|
255
|
+
write: (key, value) => {
|
256
|
+
return new Promise((resolve, reject) => {
|
257
|
+
caches.open(CACHE_NAME).then(function (cache) {
|
258
|
+
cache.put(new Request(`https://LOCALCACHE/${encodeURIComponent(key)}`), new Response(value));
|
259
|
+
resolve()
|
260
|
+
}).catch(() => {
|
261
|
+
reject()
|
262
|
+
})
|
263
|
+
})
|
264
|
+
}
|
265
|
+
}
|
266
|
+
const compareVersion = (a, b) => {
|
267
|
+
let v1 = a.split('.');
|
268
|
+
let v2 = b.split('.');
|
269
|
+
const len = Math.max(v1.length, v2.length);
|
270
|
+
while (v1.length < len) {
|
271
|
+
v1.push('0');
|
272
|
+
}
|
273
|
+
while (v2.length < len) {
|
274
|
+
v2.push('0');
|
275
|
+
}
|
276
|
+
for (let i = 0; i < len; i++) {
|
277
|
+
const num1 = parseInt(v1[i]);
|
278
|
+
const num2 = parseInt(v2[i]);
|
279
|
+
if (num1 > num2) {
|
280
|
+
return a;
|
281
|
+
} else if (num1 < num2) {
|
282
|
+
return b;
|
283
|
+
}
|
284
|
+
}
|
285
|
+
return a;
|
286
|
+
}
|
287
|
+
|
288
|
+
const mirrors = [
|
289
|
+
`https://registry.npmjs.org/${NPMPackage}/latest`,
|
290
|
+
`https://registry.npmmirror.com/${NPMPackage}/latest`,
|
291
|
+
`https://mirrors.cloud.tencent.com/npm/${NPMPackage}/latest`
|
292
|
+
]
|
293
|
+
const getLocalVersion = async () => {
|
294
|
+
NPMPackageVersion = await db.read('blog_version') || NPMPackageVersion
|
295
|
+
logger.bg.info(`Local Version: ${NPMPackage}@${NPMPackageVersion}`)
|
296
|
+
}
|
297
|
+
let mirror_time = 0;
|
298
|
+
const setNewestVersion = async () => {
|
299
|
+
if (!NPMMirror) {
|
300
|
+
return
|
301
|
+
}
|
302
|
+
let f = null;
|
303
|
+
if (!(mirror_time % (mirrors.length + 1))) {
|
304
|
+
f = FetchEngine(mirrors);
|
305
|
+
} else {
|
306
|
+
f = fetch(mirrors[(mirror_time % (mirrors.length + 1)) - 1]);
|
307
|
+
}
|
308
|
+
mirror_time++;
|
309
|
+
return f
|
310
|
+
.then(res => res.json())
|
311
|
+
.then(async res => {
|
312
|
+
if (!res.version) throw ('No Version Found!')
|
313
|
+
NPMPackageVersion = compareVersion(res.version, await db.read('blog_version') || NPMPackageVersion)
|
314
|
+
logger.bg.ready(`${NPMPackage}@${NPMPackageVersion}`)
|
315
|
+
await db.write('blog_version', NPMPackageVersion)
|
316
|
+
})
|
317
|
+
.catch(error => {
|
318
|
+
logger.error('[Set Newest Version] ' + (error.stack || error))
|
319
|
+
})
|
320
|
+
}
|
321
|
+
setInterval(async () => {
|
322
|
+
await setNewestVersion()
|
323
|
+
}, 60 * 1000);
|
324
|
+
setTimeout(async () => {
|
325
|
+
await setNewestVersion()
|
326
|
+
}, 5000)
|
327
|
+
const installFunction = async () => {
|
328
|
+
await getLocalVersion();
|
329
|
+
return caches.open(CACHE_NAME + "-precache")
|
330
|
+
.then(async function (cache) {
|
331
|
+
if (!await db.read('uuid')) {
|
332
|
+
await db.write('uuid', generate_uuid())
|
333
|
+
}
|
334
|
+
if (PreCachlist.length) {
|
335
|
+
logger.group.event(`Precaching ${PreCachlist.length} files.`);
|
336
|
+
let index = 0;
|
337
|
+
PreCachlist.forEach(function (url) {
|
338
|
+
cache.match(new Request(url)).then(function (response) {
|
339
|
+
if (response) {
|
340
|
+
logger.ready(`Precaching ${url}`);
|
341
|
+
} else {
|
342
|
+
logger.wait(`Precaching ${url}`);
|
343
|
+
cache.add(new Request(url));
|
344
|
+
}
|
345
|
+
index++;
|
346
|
+
if (index === PreCachlist.length) {
|
347
|
+
logger.ready(`Precached ${PreCachlist.length} files.`);
|
348
|
+
logger.group.end();
|
349
|
+
}
|
350
|
+
})
|
351
|
+
})
|
352
|
+
}
|
353
|
+
}).catch((error) => {
|
354
|
+
logger.error('[install] ' + (error.stack || error));
|
355
|
+
})
|
356
|
+
}
|
357
|
+
self.addEventListener('install', async function (event) {
|
358
|
+
logger.bg.event("service worker install event listening");
|
359
|
+
try {
|
360
|
+
self.skipWaiting();
|
361
|
+
event.waitUntil(installFunction());
|
362
|
+
logger.bg.ready('service worker install sucess!');
|
363
|
+
} catch (error) {
|
364
|
+
logger.error('[install] ' + (error.stack || error));
|
365
|
+
}
|
366
|
+
});
|
367
|
+
self.addEventListener('activate', async event => {
|
368
|
+
logger.bg.event("service worker activate event listening");
|
369
|
+
try {
|
370
|
+
event.waitUntil(
|
371
|
+
caches.keys().then((keys) => {
|
372
|
+
return Promise.all(keys.map((key) => {
|
373
|
+
if (!key.includes(cacheSuffixVersion)) {
|
374
|
+
caches.delete(key);
|
375
|
+
logger.bg.ready('Deleted Outdated Cache: ' + key);
|
376
|
+
}
|
377
|
+
}));
|
378
|
+
}).catch((error) => {
|
379
|
+
logger.error('[activate] ' + (error.stack || error));
|
380
|
+
})
|
381
|
+
);
|
382
|
+
await self.clients.claim()
|
383
|
+
logger.bg.ready('service worker activate sucess!');
|
384
|
+
} catch (error) {
|
385
|
+
logger.error('[activate] ' + (error.stack || error));
|
386
|
+
}
|
387
|
+
})
|
388
|
+
self.addEventListener('fetch', async event => {
|
389
|
+
event.respondWith(
|
390
|
+
handleFetch(event).catch((error) => {
|
391
|
+
logger.error('[fetch] ' + event.request.url + '\n[error] ' + (error.stack || error));
|
392
|
+
})
|
393
|
+
)
|
394
|
+
});
|
395
|
+
|
396
|
+
|
397
|
+
const NetworkOnly = async (event) => {
|
398
|
+
logger.group.info('NetworkOnly: ' + new URL(event.request.url).pathname);
|
399
|
+
logger.wait('service worker fetch: ' + event.request.url)
|
400
|
+
logger.group.end();
|
401
|
+
return fetch(event.request)
|
402
|
+
}
|
403
|
+
const CacheFirst = async (event) => {
|
404
|
+
return caches.match(event.request).then(function (resp) {
|
405
|
+
logger.group.info('CacheFirst: ' + new URL(event.request.url).pathname);
|
406
|
+
logger.wait('service worker fetch: ' + event.request.url)
|
407
|
+
if (resp) {
|
408
|
+
logger.group.ready(`Cache Hit`);
|
409
|
+
console.log(resp)
|
410
|
+
logger.group.end();
|
411
|
+
logger.group.end();
|
412
|
+
event.waitUntil(CacheRuntime(event.request))
|
413
|
+
return resp;
|
414
|
+
} else {
|
415
|
+
logger.warn(`Cache Miss`);
|
416
|
+
logger.group.end();
|
417
|
+
return CacheRuntime(event.request)
|
418
|
+
}
|
419
|
+
})
|
420
|
+
}
|
421
|
+
const CacheAlways = async (event) => {
|
422
|
+
return caches.match(event.request).then(function (resp) {
|
423
|
+
logger.group.info('CacheAlways: ' + new URL(event.request.url).pathname);
|
424
|
+
logger.wait('service worker fetch: ' + event.request.url)
|
425
|
+
if (resp) {
|
426
|
+
logger.group.ready(`Cache Hit`);
|
427
|
+
console.log(resp)
|
428
|
+
logger.group.end();
|
429
|
+
logger.group.end();
|
430
|
+
return resp;
|
431
|
+
} else {
|
432
|
+
logger.warn(`Cache Miss`);
|
433
|
+
logger.group.end();
|
434
|
+
return CacheRuntime(event.request)
|
435
|
+
}
|
436
|
+
})
|
437
|
+
}
|
438
|
+
|
439
|
+
const matchCache = async (event) => {
|
440
|
+
return caches.match(event.request).then(function (resp) {
|
441
|
+
logger.group.info('service worker fetch: ' + event.request.url)
|
442
|
+
if (resp) {
|
443
|
+
logger.group.ready(`Cache Hit`);
|
444
|
+
console.log(resp)
|
445
|
+
logger.group.end();
|
446
|
+
logger.group.end();
|
447
|
+
return resp;
|
448
|
+
} else {
|
449
|
+
logger.warn(`Cache Miss`);
|
450
|
+
logger.group.end();
|
451
|
+
return CacheRuntime(event.request)
|
452
|
+
}
|
453
|
+
})
|
454
|
+
}
|
455
|
+
async function CacheRuntime(request) {
|
456
|
+
const url = new URL(request.url);
|
457
|
+
let response = await matchCDN(request);
|
458
|
+
if (!response) {
|
459
|
+
response = await fetch(request).catch(() => null)
|
460
|
+
}
|
461
|
+
logger.group.event(`Cache Runtime ${url.pathname}`);
|
462
|
+
logger.wait(`Caching url: ${request.url}`);
|
463
|
+
console.log(response);
|
464
|
+
|
465
|
+
if (request.method === "GET" && (url.protocol == "https:")) {
|
466
|
+
const cache = await caches.open(CACHE_NAME + "-runtime");
|
467
|
+
cache.put(request, response.clone()).catch(error => {
|
468
|
+
logger.error('[Cache Runtime] ' + (error.stack || error));
|
469
|
+
if (error.name === 'QuotaExceededError') {
|
470
|
+
caches.delete(CACHE_NAME + "-runtime");
|
471
|
+
logger.ready("deleted cache")
|
472
|
+
}
|
473
|
+
})
|
474
|
+
logger.ready(`Cached url: ${request.url}`);
|
475
|
+
} else {
|
476
|
+
logger.warn(`Not Cached url: ${request.url}`);
|
477
|
+
}
|
478
|
+
logger.group.end();
|
479
|
+
return response;
|
480
|
+
}
|
481
|
+
|
482
|
+
const matchCDN = async (req) => {
|
483
|
+
const nav = navigator
|
484
|
+
const { saveData, effectiveType } = nav.connection || nav.mozConnection || nav.webkitConnection || {}
|
485
|
+
if (saveData || /2g/.test(effectiveType)) {
|
486
|
+
logger.warn("Slow Network: Transparent Proxy");
|
487
|
+
return fetch(req);
|
488
|
+
}
|
489
|
+
const urls = []
|
490
|
+
let urlObj = new URL(req.url)
|
491
|
+
let pathType = urlObj.pathname.split('/')[1]
|
492
|
+
let pathTestRes = "";
|
493
|
+
|
494
|
+
if (NPMMirror && new RegExp(location.origin).test(req.url)) {
|
495
|
+
logger.group.ready(`Match NPM Mirror: ` + req.url);
|
496
|
+
for (const key in cdn.npm) {
|
497
|
+
let url = cdn.npm[key] + "/" + NPMPackage + "@" + NPMPackageVersion + req.url.replace(location.origin, '')
|
498
|
+
url = fullPath(fullPath(url))
|
499
|
+
console.log(url);
|
500
|
+
urls.push(url)
|
501
|
+
}
|
502
|
+
logger.group.end()
|
503
|
+
}
|
504
|
+
if (!urls.length) {
|
505
|
+
for (const item of cdn_match_list) {
|
506
|
+
if (new RegExp(item.key).test(req.url)) {
|
507
|
+
pathType = item.type
|
508
|
+
pathTestRes = new RegExp(item.key).exec(req.url)[0]
|
509
|
+
break;
|
510
|
+
}
|
511
|
+
}
|
512
|
+
for (const type in cdn) {
|
513
|
+
if (type === pathType) {
|
514
|
+
logger.group.ready(`Match CDN ${pathType}: ` + req.url);
|
515
|
+
for (const key in cdn[type]) {
|
516
|
+
const url = cdn[type][key] + req.url.replace(pathTestRes, '')
|
517
|
+
console.log(url);
|
518
|
+
urls.push(url)
|
519
|
+
}
|
520
|
+
logger.group.end()
|
521
|
+
}
|
522
|
+
}
|
523
|
+
}
|
524
|
+
|
525
|
+
let res;
|
526
|
+
if (urls.length)
|
527
|
+
res = FetchEngine(urls)
|
528
|
+
else
|
529
|
+
res = fetch(req)
|
530
|
+
if (res && NPMMirror && new RegExp(location.origin).test(req.url)) {
|
531
|
+
const ext = fullPath(fullPath(req.url)).split('.').pop()
|
532
|
+
const contentType = getContentType(ext)
|
533
|
+
res = res
|
534
|
+
.then(res => res.arrayBuffer())
|
535
|
+
.then(buffer => new Response(buffer, {
|
536
|
+
headers: {
|
537
|
+
"Content-Type": contentType
|
538
|
+
}
|
539
|
+
}))
|
540
|
+
.catch(() => null)
|
541
|
+
}
|
542
|
+
return res
|
543
|
+
}
|
544
|
+
|
545
|
+
const fullPath = (url) => {
|
546
|
+
url = url.split('?')[0].split('#')[0]
|
547
|
+
if (url.endsWith('/')) {
|
548
|
+
url += 'index.html'
|
549
|
+
} else {
|
550
|
+
const list = url.split('/')
|
551
|
+
const last = list[list.length - 1]
|
552
|
+
if (last.indexOf('.') === -1) {
|
553
|
+
url += '.html'
|
554
|
+
}
|
555
|
+
}
|
556
|
+
return url
|
557
|
+
}
|
558
|
+
async function progress(res) {
|
559
|
+
return new Response(await res.arrayBuffer(), {
|
560
|
+
status: res.status,
|
561
|
+
headers: res.headers
|
562
|
+
})
|
563
|
+
}
|
564
|
+
|
565
|
+
function createPromiseAny() {
|
566
|
+
Promise.any = function (promises) {
|
567
|
+
return new Promise((resolve, reject) => {
|
568
|
+
promises = Array.isArray(promises) ? promises : []
|
569
|
+
let len = promises.length
|
570
|
+
let errs = []
|
571
|
+
if (len === 0) return reject(new AggregateError('All promises were rejected'))
|
572
|
+
promises.forEach((p) => {
|
573
|
+
if (p instanceof Promise) {
|
574
|
+
p.then(
|
575
|
+
(res) => resolve(res),
|
576
|
+
(err) => {
|
577
|
+
len--
|
578
|
+
errs.push(err)
|
579
|
+
if (len === 0) reject(new AggregateError(errs))
|
580
|
+
}
|
581
|
+
)
|
582
|
+
} else {
|
583
|
+
reject(p)
|
584
|
+
}
|
585
|
+
})
|
586
|
+
})
|
587
|
+
}
|
588
|
+
}
|
589
|
+
|
590
|
+
function fetchAny(reqs) {
|
591
|
+
const controller = new AbortController()
|
592
|
+
|
593
|
+
return reqs.map(req => {
|
594
|
+
return new Promise((resolve, reject) => {
|
595
|
+
fetch(req, {
|
596
|
+
signal: controller.signal
|
597
|
+
})
|
598
|
+
.then(progress)
|
599
|
+
.then(res => {
|
600
|
+
controller.abort()
|
601
|
+
if (res.status !== 200)
|
602
|
+
reject(null)
|
603
|
+
else
|
604
|
+
resolve(res)
|
605
|
+
})
|
606
|
+
.catch(() => reject(null))
|
607
|
+
})
|
608
|
+
})
|
609
|
+
}
|
610
|
+
|
611
|
+
function fetchParallel(reqs) {
|
612
|
+
const abortEvent = new Event("abortOtherInstance")
|
613
|
+
const eventTarget = new EventTarget();
|
614
|
+
|
615
|
+
return reqs.map(async req => {
|
616
|
+
const controller = new AbortController();
|
617
|
+
let tagged = false;
|
618
|
+
eventTarget.addEventListener(abortEvent.type, () => {
|
619
|
+
if (!tagged) controller.abort();
|
620
|
+
})
|
621
|
+
return new Promise((resolve, reject) => {
|
622
|
+
fetch(req, {
|
623
|
+
signal: controller.signal,
|
624
|
+
}).then(res => {
|
625
|
+
tagged = true;
|
626
|
+
eventTarget.dispatchEvent(abortEvent)
|
627
|
+
if (res.status !== 200)
|
628
|
+
reject(null)
|
629
|
+
else
|
630
|
+
resolve(res)
|
631
|
+
}).catch(() => reject(null))
|
632
|
+
})
|
633
|
+
});
|
634
|
+
}
|
635
|
+
|
636
|
+
const FetchEngine = (reqs) => {
|
637
|
+
if (!Promise.any) createPromiseAny();
|
638
|
+
return Promise.any(fetchParallel(reqs)).then(res => res)
|
639
|
+
.catch((e) => {
|
640
|
+
if (e == "AggregateError: All promises were rejected") {
|
641
|
+
return Promise.any(fetchAny(reqs))
|
642
|
+
.then((res) => res)
|
643
|
+
.catch(() => null);
|
644
|
+
}
|
645
|
+
return null;
|
646
|
+
});
|
647
|
+
};
|
648
|
+
|
649
|
+
const getContentType = (ext) => {
|
650
|
+
switch (ext) {
|
651
|
+
case 'js':
|
652
|
+
return 'text/javascript'
|
653
|
+
case 'html':
|
654
|
+
return 'text/html'
|
655
|
+
case 'css':
|
656
|
+
return 'text/css'
|
657
|
+
case 'json':
|
658
|
+
return 'application/json'
|
659
|
+
case 'webp':
|
660
|
+
return 'image/webp'
|
661
|
+
case 'jpg':
|
662
|
+
return 'image/jpg'
|
663
|
+
case 'jpeg':
|
664
|
+
return 'image/jpeg'
|
665
|
+
case 'png':
|
666
|
+
return 'image/png'
|
667
|
+
case 'gif':
|
668
|
+
return 'image/gif'
|
669
|
+
case 'xml':
|
670
|
+
return 'text/xml'
|
671
|
+
case 'xsl':
|
672
|
+
return 'text/xml'
|
673
|
+
case 'webmanifest':
|
674
|
+
return 'text/webmanifest'
|
675
|
+
case 'map':
|
676
|
+
return 'application/json'
|
677
|
+
case 'bcmap':
|
678
|
+
return 'image/vnd.wap.wbmp'
|
679
|
+
case 'wbmp':
|
680
|
+
return 'image/vnd.wap.wbmp'
|
681
|
+
case 'bmp':
|
682
|
+
return 'image/bmp'
|
683
|
+
case 'ico':
|
684
|
+
return 'image/vnd.microsoft.icon'
|
685
|
+
case 'tiff':
|
686
|
+
return 'image/tiff'
|
687
|
+
case 'tif':
|
688
|
+
return 'image/tiff'
|
689
|
+
case 'svg':
|
690
|
+
return 'image/svg+xml'
|
691
|
+
case 'svgz':
|
692
|
+
return 'image/svg+xml'
|
693
|
+
case 'woff':
|
694
|
+
return 'application/font-woff'
|
695
|
+
case 'woff2':
|
696
|
+
return 'application/font-woff2'
|
697
|
+
case 'ttf':
|
698
|
+
return 'application/font-ttf'
|
699
|
+
case 'otf':
|
700
|
+
return 'application/font-otf'
|
701
|
+
case 'eot':
|
702
|
+
return 'application/vnd.ms-fontobject'
|
703
|
+
case 'zip':
|
704
|
+
return 'application/zip'
|
705
|
+
case 'tar':
|
706
|
+
return 'application/x-tar'
|
707
|
+
case 'gz':
|
708
|
+
return 'application/gzip'
|
709
|
+
case 'bz2':
|
710
|
+
return 'application/x-bzip2'
|
711
|
+
case 'rar':
|
712
|
+
return 'application/x-rar-compressed'
|
713
|
+
case '7z':
|
714
|
+
return 'application/x-7z-compressed'
|
715
|
+
case 'doc':
|
716
|
+
return 'application/msword'
|
717
|
+
case 'docx':
|
718
|
+
return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
|
719
|
+
case 'xls':
|
720
|
+
return 'application/vnd.ms-excel'
|
721
|
+
case 'xlsx':
|
722
|
+
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
723
|
+
case 'ppt':
|
724
|
+
return 'application/vnd.ms-powerpoint'
|
725
|
+
case 'pptx':
|
726
|
+
return 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
|
727
|
+
case 'pdf':
|
728
|
+
return 'application/pdf'
|
729
|
+
case 'txt':
|
730
|
+
return 'text/plain'
|
731
|
+
case 'rtf':
|
732
|
+
return 'application/rtf'
|
733
|
+
case 'mp3':
|
734
|
+
return 'audio/mpeg'
|
735
|
+
case 'wav':
|
736
|
+
return 'audio/x-wav'
|
737
|
+
case 'ogg':
|
738
|
+
return 'audio/ogg'
|
739
|
+
case 'mp4':
|
740
|
+
return 'video/mp4'
|
741
|
+
case 'm4v':
|
742
|
+
return 'video/x-m4v'
|
743
|
+
case 'mov':
|
744
|
+
return 'video/quicktime'
|
745
|
+
case 'avi':
|
746
|
+
return 'video/x-msvideo'
|
747
|
+
case 'wmv':
|
748
|
+
return 'video/x-ms-wmv'
|
749
|
+
case 'flv':
|
750
|
+
return 'video/x-flv'
|
751
|
+
case 'swf':
|
752
|
+
return 'application/x-shockwave-flash'
|
753
|
+
case 'mpg':
|
754
|
+
return 'video/mpeg'
|
755
|
+
case 'mpeg':
|
756
|
+
return 'video/mpeg'
|
757
|
+
case 'mpe':
|
758
|
+
return 'video/mpeg'
|
759
|
+
case 'mpv':
|
760
|
+
return 'video/mpeg'
|
761
|
+
case 'm2v':
|
762
|
+
return 'video/mpeg'
|
763
|
+
case 'm4a':
|
764
|
+
return 'audio/mp4'
|
765
|
+
case 'aac':
|
766
|
+
return 'audio/aac'
|
767
|
+
case 'm3u':
|
768
|
+
return 'audio/x-mpegurl'
|
769
|
+
case 'm3u8':
|
770
|
+
return 'application/vnd.apple.mpegurl'
|
771
|
+
case 'pls':
|
772
|
+
return 'audio/x-scpls'
|
773
|
+
case 'cue':
|
774
|
+
return 'application/x-cue'
|
775
|
+
case 'wma':
|
776
|
+
return 'audio/x-ms-wma'
|
777
|
+
case 'flac':
|
778
|
+
return 'audio/flac'
|
779
|
+
case 'aif':
|
780
|
+
return 'audio/x-aiff'
|
781
|
+
case 'aiff':
|
782
|
+
return 'audio/x-aiff'
|
783
|
+
case 'aifc':
|
784
|
+
return 'audio/x-aiff'
|
785
|
+
case 'au':
|
786
|
+
return 'audio/basic'
|
787
|
+
case 'snd':
|
788
|
+
return 'audio/basic'
|
789
|
+
case 'mid':
|
790
|
+
return 'audio/midi'
|
791
|
+
case 'midi':
|
792
|
+
return 'audio/midi'
|
793
|
+
case 'kar':
|
794
|
+
return 'audio/midi'
|
795
|
+
default:
|
796
|
+
return 'text/plain'
|
797
|
+
}
|
798
|
+
}
|