hexo-swpp 2.6.0 → 2.6.2
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/lib/sw-template.js +5 -4
- package/lib/swBuilder.js +17 -13
- package/package.json +1 -1
package/lib/sw-template.js
CHANGED
|
@@ -60,14 +60,15 @@
|
|
|
60
60
|
|
|
61
61
|
self.addEventListener('fetch', event => {
|
|
62
62
|
let request = event.request
|
|
63
|
-
if (request.method !== 'GET' || request.url.startsWith('http')) return
|
|
63
|
+
if (request.method !== 'GET' || !request.url.startsWith('http')) return
|
|
64
64
|
// [modifyRequest call]
|
|
65
65
|
const url = new URL(request.url)
|
|
66
66
|
// [blockRequest call]
|
|
67
67
|
if (findCache(url)) {
|
|
68
68
|
const key = `${url.protocol}//${url.host}${url.pathname}`
|
|
69
|
-
event.respondWith(caches.match(key).then(cache =>
|
|
70
|
-
|
|
69
|
+
event.respondWith(caches.match(key).then(cache => {
|
|
70
|
+
// [debug find]
|
|
71
|
+
return cache ?? fetchFile(request, true).then(response => {
|
|
71
72
|
if (checkResponse(response)) {
|
|
72
73
|
const clone = response.clone()
|
|
73
74
|
caches.open(CACHE_NAME).then(it => it.put(key, clone))
|
|
@@ -75,7 +76,7 @@
|
|
|
75
76
|
}
|
|
76
77
|
return response
|
|
77
78
|
})
|
|
78
|
-
))
|
|
79
|
+
}))
|
|
79
80
|
} else {
|
|
80
81
|
const spare = getSpareUrls(request.url)
|
|
81
82
|
if (spare) event.respondWith(fetchFile(request, false, spare))
|
package/lib/swBuilder.js
CHANGED
|
@@ -22,31 +22,33 @@ module.exports = (hexo, config, pluginConfig, rules) => {
|
|
|
22
22
|
/**
|
|
23
23
|
* 获取任意对象(symbol 类型除外)的源码
|
|
24
24
|
* @param any {any} 对象
|
|
25
|
+
* @param separator {string} 分隔符
|
|
25
26
|
* @param whiteList {string[]?} 白名单
|
|
26
27
|
* @return {string}
|
|
27
28
|
*/
|
|
28
|
-
function getSource(any, whiteList = null) {
|
|
29
|
+
function getSource(any, separator = '\n', whiteList = null) {
|
|
29
30
|
switch (typeof any) {
|
|
30
|
-
case 'undefined': return
|
|
31
|
+
case 'undefined': return `undefined${separator}`
|
|
31
32
|
case 'object': {
|
|
32
|
-
let result = ''
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
let result = whiteList ? '' : '{\n'
|
|
34
|
+
result += Object.getOwnPropertyNames(any)
|
|
35
|
+
.filter(key => !whiteList || whiteList.includes(key))
|
|
36
|
+
.map(key => {
|
|
37
|
+
const value = any[key]
|
|
38
|
+
return whiteList ? `const ${key} = ${getSource(value, '')}` : `${key}: ${getSource(value, '')}`
|
|
39
|
+
}).join(whiteList ? '\n' : ',\n')
|
|
40
|
+
return result + (whiteList ? '' : '\n}') + separator
|
|
39
41
|
}
|
|
40
|
-
case 'string': return `'${any}'
|
|
42
|
+
case 'string': return `'${any}'${separator}`
|
|
43
|
+
case 'bigint': return `${any.toString()}n${separator}`
|
|
44
|
+
default: return any.toString() + separator
|
|
41
45
|
case 'symbol':
|
|
42
46
|
logger.error("[sw builder]: 不支持写入 symbol 类型,请从 sw-rules.js 中移除相关内容!")
|
|
43
47
|
throw '不支持写入 symbol 类型'
|
|
44
|
-
case 'bigint': return `${any.toString()}n\n`
|
|
45
|
-
default: return any.toString() + '\n'
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
50
|
// 获取拓展文件
|
|
49
|
-
let cache = getSource(rules, [
|
|
51
|
+
let cache = getSource(rules, '\n\n', [
|
|
50
52
|
'cacheList', 'modifyRequest', 'getCdnList', 'getSpareUrls', 'blockRequest',
|
|
51
53
|
...('external' in rules && Array.isArray(rules.external) ? rules.external : [])
|
|
52
54
|
])
|
|
@@ -164,6 +166,8 @@ module.exports = (hexo, config, pluginConfig, rules) => {
|
|
|
164
166
|
console.debug(\`receive: \${event.data}\`)
|
|
165
167
|
`).replaceAll('// [debug escape]', `
|
|
166
168
|
console.debug(\`escape: \${aid}\`)
|
|
169
|
+
`).replaceAll('// [debug find]', `
|
|
170
|
+
console.debug(\`find: \${cache}\`)
|
|
167
171
|
`)
|
|
168
172
|
}
|
|
169
173
|
return {
|