swpp-backends 2.1.7 → 2.1.10-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ServiceWorkerBuilder.js +1 -1
- package/dist/index.js +1 -1
- package/dist/resources/sw-dom.js +7 -9
- package/dist/resources/sw-fetch.js +13 -30
- package/dist/resources/sw-template.js +31 -14
- package/package.json +1 -1
|
@@ -59,7 +59,7 @@ function buildServiceWorker() {
|
|
|
59
59
|
url = new URL(request.url)
|
|
60
60
|
}
|
|
61
61
|
`).replaceAll('// [modifyRequest else-if]', `
|
|
62
|
-
else if (modify) handleFetch(
|
|
62
|
+
else if (modify) handleFetch(fetchWithCors(request, false).catch(err => new Response(err, {status: 499})))
|
|
63
63
|
`);
|
|
64
64
|
}
|
|
65
65
|
if (blockRequest) {
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ const VersionAnalyzer_1 = require("./VersionAnalyzer");
|
|
|
10
10
|
const DomBuilder_1 = require("./DomBuilder");
|
|
11
11
|
// noinspection JSUnusedGlobalSymbols
|
|
12
12
|
exports.default = {
|
|
13
|
-
version: '2.1.
|
|
13
|
+
version: '2.1.8',
|
|
14
14
|
cache: {
|
|
15
15
|
readEjectData: Utils_1.readEjectData, readUpdateJson: Variant_1.readUpdateJson,
|
|
16
16
|
readRules: Variant_1.readRules, readMergeVersionMap: Variant_1.readMergeVersionMap,
|
package/dist/resources/sw-dom.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
document.addEventListener('DOMContentLoaded', () => {
|
|
2
|
-
|
|
3
|
-
const checkServiceWorker = () => 'serviceWorker' in navigator && navigator.serviceWorker.controller
|
|
2
|
+
if (!navigator.serviceWorker?.controller) return
|
|
4
3
|
/** 发送信息到 sw */
|
|
5
4
|
const postMessage2SW = type => navigator.serviceWorker.controller.postMessage(type)
|
|
6
5
|
const pjaxUpdate = url => {
|
|
@@ -19,20 +18,19 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
|
19
18
|
}
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
|
-
|
|
23
|
-
if (sessionStorage.getItem(
|
|
24
|
-
sessionStorage.removeItem(
|
|
21
|
+
const SESSION_KEY = 'updated'
|
|
22
|
+
if (sessionStorage.getItem(SESSION_KEY)) {
|
|
23
|
+
sessionStorage.removeItem(SESSION_KEY);
|
|
25
24
|
// ${onSuccess}
|
|
26
25
|
} else postMessage2SW('update')
|
|
27
26
|
navigator.serviceWorker.addEventListener('message', event => {
|
|
27
|
+
sessionStorage.setItem(SESSION_KEY, '1')
|
|
28
28
|
const data = event.data
|
|
29
29
|
switch (data.type) {
|
|
30
30
|
case 'update':
|
|
31
|
-
const list = data.
|
|
32
|
-
if (!list) break
|
|
33
|
-
sessionStorage.setItem('updated', '1')
|
|
31
|
+
const list = data.list
|
|
34
32
|
// noinspection JSUnresolvedVariable,JSUnresolvedFunction
|
|
35
|
-
if (window.Pjax?.isSupported()) {
|
|
33
|
+
if (list && window.Pjax?.isSupported()) {
|
|
36
34
|
list.filter(url => /\.(js|css)$/.test(url))
|
|
37
35
|
.forEach(pjaxUpdate)
|
|
38
36
|
}
|
|
@@ -1,32 +1,21 @@
|
|
|
1
1
|
/** @type function */
|
|
2
|
-
let getRaceUrls, checkResponse, getSpareUrls
|
|
2
|
+
let getRaceUrls, checkResponse, getSpareUrls, fetchWithCors
|
|
3
3
|
|
|
4
|
-
module.exports.JS_CODE_DEF_FETCH_FILE = 'const fetchFile = '
|
|
5
|
-
(request, banCache) => fetch(request, {
|
|
6
|
-
cache: banCache ? "no-store" : "default",
|
|
7
|
-
mode: 'cors',
|
|
8
|
-
credentials: 'same-origin'
|
|
9
|
-
})
|
|
10
|
-
).toString()
|
|
4
|
+
module.exports.JS_CODE_DEF_FETCH_FILE = 'const fetchFile = fetchWithCors;'
|
|
11
5
|
|
|
12
6
|
module.exports.JS_CODE_GET_CDN_LIST= 'const fetchFile = ' + (
|
|
13
7
|
(request, banCache) => {
|
|
14
|
-
const fetchArgs = {
|
|
15
|
-
cache: banCache ? 'no-store' : 'default',
|
|
16
|
-
mode: 'cors',
|
|
17
|
-
credentials: 'same-origin'
|
|
18
|
-
}
|
|
19
8
|
const list = getRaceUrls(request.url)
|
|
20
|
-
if (!list || !Promise.any) return
|
|
9
|
+
if (!list || !Promise.any) return fetchWithCors(request, banCache)
|
|
21
10
|
const res = list.map(url => new Request(url, request))
|
|
22
11
|
const controllers = []
|
|
23
12
|
// noinspection JSCheckFunctionSignatures
|
|
24
|
-
return Promise.any(
|
|
25
|
-
(it, index) =>
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
)
|
|
29
|
-
)
|
|
13
|
+
return Promise.any(
|
|
14
|
+
res.map((it, index) => fetchWithCors(
|
|
15
|
+
it, banCache,
|
|
16
|
+
{signal: (controllers[index] = new AbortController()).signal}
|
|
17
|
+
).then(response => checkResponse(response) ? {index, response} : Promise.reject()))
|
|
18
|
+
).then(it => {
|
|
30
19
|
for (let i in controllers) {
|
|
31
20
|
if (i !== it.index) controllers[i].abort()
|
|
32
21
|
}
|
|
@@ -37,24 +26,18 @@ module.exports.JS_CODE_GET_CDN_LIST= 'const fetchFile = ' + (
|
|
|
37
26
|
|
|
38
27
|
module.exports.JS_CODE_GET_SPARE_URLS = 'const fetchFile = ' + (
|
|
39
28
|
(request, banCache, spare = null) => {
|
|
40
|
-
const fetchArgs = {
|
|
41
|
-
cache: banCache ? 'no-store' : 'default',
|
|
42
|
-
mode: 'cors',
|
|
43
|
-
credentials: 'same-origin'
|
|
44
|
-
}
|
|
45
29
|
if (!spare) {
|
|
46
30
|
spare = getSpareUrls(request.url)
|
|
47
|
-
if (!spare) return
|
|
31
|
+
if (!spare) return fetchWithCors(request, banCache)
|
|
48
32
|
}
|
|
49
33
|
const list = spare.list
|
|
50
34
|
const controllers = new Array(list.length)
|
|
51
35
|
// noinspection JSCheckFunctionSignatures
|
|
52
36
|
const startFetch =
|
|
53
|
-
index =>
|
|
37
|
+
index => fetchWithCors(
|
|
54
38
|
new Request(list[index], request),
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}, fetchArgs)
|
|
39
|
+
banCache,
|
|
40
|
+
{signal: (controllers[index] = new AbortController()).signal}
|
|
58
41
|
).then(response => checkResponse(response) ? {r: response, i: index} : Promise.reject())
|
|
59
42
|
return new Promise((resolve, reject) => {
|
|
60
43
|
let flag = true
|
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
const CACHE_NAME = '@$$[cacheName]'
|
|
6
6
|
/** 控制信息存储地址(必须以`/`结尾) */
|
|
7
7
|
const CTRL_PATH = 'https://id.v3/'
|
|
8
|
-
let escapeTrigger
|
|
9
8
|
|
|
10
9
|
// [insertion site] values
|
|
11
10
|
|
|
@@ -26,13 +25,16 @@
|
|
|
26
25
|
self.skipWaiting()
|
|
27
26
|
const escape = '@$$[escape]'
|
|
28
27
|
if (escape) {
|
|
29
|
-
readVersion().then(oldVersion => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
// noinspection
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
readVersion().then(async oldVersion => {
|
|
29
|
+
// noinspection JSIncompatibleTypesComparison
|
|
30
|
+
if (oldVersion && oldVersion.escape !== escape) {
|
|
31
|
+
// noinspection JSValidateTypes
|
|
32
|
+
oldVersion.escape = escape
|
|
33
|
+
await caches.delete(CACHE_NAME)
|
|
34
|
+
await writeVersion(oldVersion)
|
|
35
|
+
// noinspection JSUnresolvedReference
|
|
36
|
+
const list = await clients.matchAll()
|
|
37
|
+
list.forEach(client => client.postMessage({type: 'escape'}))
|
|
36
38
|
}
|
|
37
39
|
})
|
|
38
40
|
}
|
|
@@ -92,8 +94,8 @@
|
|
|
92
94
|
}
|
|
93
95
|
cacheMap.set(cacheKey, cache = [])
|
|
94
96
|
/** 处理拉取 */
|
|
95
|
-
const handleFetch = promise =>
|
|
96
|
-
|
|
97
|
+
const handleFetch = promise => event.respondWith(
|
|
98
|
+
promise.then(response => {
|
|
97
99
|
for (let item of cache) {
|
|
98
100
|
item.s(response.clone())
|
|
99
101
|
}
|
|
@@ -104,7 +106,8 @@
|
|
|
104
106
|
}).then(() => {
|
|
105
107
|
cacheMap.delete(cacheKey)
|
|
106
108
|
return promise
|
|
107
|
-
})
|
|
109
|
+
})
|
|
110
|
+
)
|
|
108
111
|
const cacheRule = findCache(url)
|
|
109
112
|
if (cacheRule) {
|
|
110
113
|
let key = `https://${url.host}${url.pathname}`
|
|
@@ -127,7 +130,7 @@
|
|
|
127
130
|
const spare = getSpareUrls(request.url)
|
|
128
131
|
if (spare) handleFetch(fetchFile(request, false, spare))
|
|
129
132
|
// [modifyRequest else-if]
|
|
130
|
-
else handleFetch(
|
|
133
|
+
else handleFetch(fetchWithCors(request, false).catch(err => new Response(err, {status: 499})))
|
|
131
134
|
}
|
|
132
135
|
})
|
|
133
136
|
|
|
@@ -141,6 +144,21 @@
|
|
|
141
144
|
)
|
|
142
145
|
})
|
|
143
146
|
|
|
147
|
+
/**
|
|
148
|
+
* 添加 cors 配置请求指定资源
|
|
149
|
+
* @param request {Request|string}
|
|
150
|
+
* @param banCache {boolean} 是否禁用 HTTP 缓存
|
|
151
|
+
* @param optional {RequestInit?} 额外的配置项
|
|
152
|
+
* @return {Promise<Response>}
|
|
153
|
+
*/
|
|
154
|
+
const fetchWithCors = (request, banCache, optional) => fetch(
|
|
155
|
+
request,
|
|
156
|
+
Object.assign({
|
|
157
|
+
cache: optional ? "no-store" : "default",
|
|
158
|
+
mode: 'cors'
|
|
159
|
+
}, optional || {})
|
|
160
|
+
)
|
|
161
|
+
|
|
144
162
|
/**
|
|
145
163
|
* 判断指定 url 击中了哪一种缓存,都没有击中则返回 null
|
|
146
164
|
* @param url {URL}
|
|
@@ -187,10 +205,9 @@
|
|
|
187
205
|
/** @type {BrowserVersion} */
|
|
188
206
|
const newVersion = {global, local: info[0].version, escape: oldVersion?.escape ?? 0}
|
|
189
207
|
// 新用户和刚进行过逃逸操作的用户不进行更新操作
|
|
190
|
-
if (!oldVersion
|
|
208
|
+
if (!oldVersion) {
|
|
191
209
|
// noinspection JSValidateTypes
|
|
192
210
|
newVersion.escape = '@$$[escape]'
|
|
193
|
-
escapeTrigger = false
|
|
194
211
|
writeVersion(newVersion)
|
|
195
212
|
return {new: newVersion, old: oldVersion}
|
|
196
213
|
}
|