-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsw.js
307 lines (304 loc) · 11 KB
/
sw.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
var CACHE_NAME = 'GBA-WASM';
var CACHE_PATH = self.registration.scope;
var CACHE_LIST = [
"",
"assets/css/style.css",
"assets/js/common.js",
"assets/js/NengeDisk.js",
"assets/js/gamepad.js",
"assets/js/nipplejs.js",
"assets/js/vbanext.js",
"assets/images/gba2.png",
"assets/images/zan.jpg"
].map(v => CACHE_PATH + v);
var isLocal = location.host == '127.0.0.1';
var version = '2023 09/22 19:40';
var myIDB;
var PromiseList = {};
var NowClient;
function Check(obj, k) {
for (let a in k) if (obj[a] != k[a]) return !1;
return !0;
}
function getName(url) {
return (url || '').split('/').pop().split('?')[0].split('#')[0];
}
function getExt(url) {
return getName(url).split('.').pop();
}
function getMime(url) {
var ext = getExt(url).toLowerCase();
switch (ext) {
case 'js':
return 'application/javascript';
case 'json':
return 'application/json';
case 'html':
case 'xml':
case 'css':
return 'text/' + ext;
case 'jpg':
case 'jpeg':
case 'png':
case 'gif':
case 'webp':
case 'avif':
case 'apng':
case 'heic':
return 'image/' + ext;
case 'icon':
return 'image/x-' + ext;
case 'svg':
return 'svg+xml';
default:
return 'application/octet-stream';
}
}
function getHeaders(headers) {
var objs = {};
headers.forEach((a, b) => objs[b] = a);
return objs;
}
async function getClient() {
if(NowClient&&NowClient.visibilityState=='visible'){
return NowClient;
}
var clients = await self.clients.matchAll();
return clients.filter(v => v.visibilityState == 'visible')[0] || NowClient||clients[0];
}
function postMessage(str) {
getClient().then(client => {
if (client) {
if (str && str.constructor === Object) {
Object.assign(str, {
from: ServiceWorker.name,
origin: CACHE_PATH,
scriptURL: serviceWorker.scriptURL,
});
}
client.postMessage(str);
}
});
}
function getResponse(url, action) {
let id = crypto.randomUUID();
postMessage({ url, action, id });
return new Promise(back => {
PromiseList[id] = function (data) {
back(data.result);
}
});
}
function LoadDB(name) {
return new (class {
constructor(name) {
this.name = name;
}
async transaction(table, ReadMode) {
return (await this.db).transaction([table], ReadMode ? undefined : "readwrite").objectStore(table);
}
open() {
return new Promise(re => {
var req = indexedDB.open(this.name);
req.onsuccess = function (e) {
re(req.result);
}
});
}
get db() {
if (!this._DB) this._DB = this.open();
return this._DB;
}
tables = {}
table(table) {
if (this.tables[table]) return this.tables[table];
return new (class {
constructor(IDB, table) {
Object.assign(this, { IDB, table });
IDB.tables[table] = this;
}
async transaction(ReadMode) {
return this.IDB.transaction(table, ReadMode)
}
get read() {
return this.transaction(!0)
}
get write() {
return this.transaction(!1)
}
put(data, path) {
return new Promise(async re => {
(await this.write).put(data, path).onsuccess = function (e) { re(e.target.result) };
});
}
delete(path) {
return new Promise(async re => {
(await this.write).delete(data, path).onsuccess = function (e) { re(e.target.result) };
});
}
get(path) {
return new Promise(async re => {
(await this.read).get(data, path).onsuccess = function (e) { re(e.target.result) };
});
}
cursor(keyname, query, direction) {
return new Promise(async re => {
var data = {}, db = await this.read, odb;
if (keyname) odb = db.index(keyname).openKeyCursor(query, direction);
else odb = db.openCursor(query, direction);
odb.onsuccess = function (e) {
var result = e.target.result;
if (result) {
var { primaryKey, key, value } = result;
data[primaryKey] = value === undefined ? key : value;
result.continue();
} else {
re(data);
}
};
});
}
})(this, table)
}
})(name)
}
function saveCaches(cache_name, result) {
caches.open(cache_name).then(DB => {
if (result) {
Object.entries(result).forEach(entry => {
var filename = getName(entry[0]);
var file = entry[1] instanceof Blob ? file : new File([entry[1].buffer || entry[1]], filename, { type: getMime(filename) });
DB.put(CACHE_PATH + 'cache/file/' + entry[0], new Response(), { status: 200, 'Content-Length': file.size });
})
}
});
}
async function updateCaches(action) {
var myCACHE = await caches.open(CACHE_NAME);
await Promise.all(CACHE_LIST.map(async v => {
if (isLocal) console.log(v);
var re = await fetch(v + '?' + Date.now());
myCACHE.put(v, re);
}));
postMessage({ action });
}
Object.entries({
install(event) {
console.log('serviceWorker install');
//if (!0 || !isLocal&& navigator.onLine) {
//return event.waitUntil(updateCaches().then(()=>self.skipWaiting()));
//.map(v=>new Request(v,{cache:'no-cache'}))
//}
postMessage({ action: 'pwa_install', cachewrite: !1 });
return self.skipWaiting(); //跳过等待
},
activate(event) {
console.log('serviceWorker activate');
//!myIDB && postMessage({ action: 'GETDBNAME' });
if (!isLocal && navigator.onLine) {
return event.waitUntil(updateCaches('pwa_activate').then(e => self.skipWaiting()));
} else {
postMessage({ action: 'pwa_activate', cachewrite: !1 });
}
return self.skipWaiting(); //跳过等待
},
fetch(event) {
if(isLocal) return;
/**
* 返回数据
*/
var response;
/**
* 请求地址
*/
var url = event.request.url;
if (event.request.method == 'GET') {
/**
* 本地地址
*/
var urlLocal = url.includes(location.host);
/**
* 是否缓存列表文件
*/
var isCache = urlLocal && CACHE_LIST.includes(url);
/**
* 是否需要前端解压的虚假地址
*/
var isPack = urlLocal && /(pack=|\.wasm)/i.test(url);
/**
* 是否CDN
*/
var isCDN = !urlLocal && /(cdn|code)/i.test(url);
if (isCache || isPack || isCDN) {
//拦截请求 event.request 一个请求对象
return event.respondWith(new Promise(async resolve => {
var URL_NAME = getName(url);
var DB = await caches.open(CACHE_NAME);
response = await DB.match(event.request);
if (!response) {
if (navigator.onLine) {
if (isPack) {
//分析本地虚假地址 进行虚假worker缓存
action = url.match(/pack=([^&]+)/);
if (action && action[1]) {
var result = await getResponse(url, action[1]);
if (result) {
return resolve(await DB.match(event.request));
}
}
}
//联网状态
/***
if(response){
var headers = getHeaders(response.headers);
var time = headers['date'];
if(time&&Date.now() - Date.parse(time)>24*3600){
var response = await fetch(event.request);
if(response){
DB.put(event.request, response.clone());
}
}
}
*/
if (!response) {
response = await fetch(event.request);
if (!isLocal && response && response.status == 200 && isCDN) {
DB.put(event.request, response.clone());
}
}
}
}
resolve(response);
}));
}
}
return response;
},
notificationclick(event) {
console.log("On notification click: ", event.notification.tag);
},
push(event) {
console.log("On notification click: ", event.data);
},
message(event) {
let data = event.data;
if (data && data.constructor === Object) {
if (isLocal) console.log(data);
var { action, result, id } = data;
if (action == 'CLIENT') {
NowClient = event.source;
} else if (action == 'UPDATE CACHES') {
updateCaches('pwa_upatecaches')
} else if (action == 'WOKERDBNAME') {
!myIDB && (myIDB = LoadDB(result));
} else if (action == 'WRITECACHE') {
saveCaches(data.cachename || CACHE_NAME, result);
} else if (id && PromiseList[id]) {
PromiseList[id](data);
delete PromiseList[id];
}
}
}
}).forEach(
entry => self.addEventListener(entry[0], entry[1])
);