swpp-backends 2.2.2 → 2.3.0-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 +2 -2
- package/dist/SwppConfig.js +42 -1
- package/dist/SwppRules.js +11 -41
- package/dist/index.js +1 -1
- package/dist/resources/sw-template.js +33 -29
- package/package.json +1 -1
- package/types/SwppConfig.d.ts +1 -0
- package/types/SwppRules.d.ts +17 -0
|
@@ -26,7 +26,7 @@ function buildServiceWorker() {
|
|
|
26
26
|
const templatePath = path_1.default.resolve('./', module.path, 'resources/sw-template.js');
|
|
27
27
|
// 获取拓展文件
|
|
28
28
|
let cache = (0, Utils_1.getSource)(rules, undefined, [
|
|
29
|
-
'cacheRules', 'modifyRequest', 'getRaceUrls', 'getSpareUrls', 'blockRequest', 'fetchFile', 'skipRequest',
|
|
29
|
+
'cacheRules', 'modifyRequest', 'getRaceUrls', 'getSpareUrls', 'blockRequest', 'fetchFile', 'skipRequest', 'isCors', 'isMemoryQueue',
|
|
30
30
|
...('external' in rules && Array.isArray(rules.external) ? rules.external : [])
|
|
31
31
|
], true) + '\n';
|
|
32
32
|
if (!fetchFile) {
|
|
@@ -44,7 +44,7 @@ function buildServiceWorker() {
|
|
|
44
44
|
cache += `(${(0, Utils_1.getSource)(rules['afterJoin'])})()\n`;
|
|
45
45
|
if ('afterTheme' in rules)
|
|
46
46
|
cache += `(${(0, Utils_1.getSource)(rules['afterTheme'])})()\n`;
|
|
47
|
-
const keyword = "const { cacheRules, fetchFile, getSpareUrls } = require('../sw-rules')";
|
|
47
|
+
const keyword = "const { cacheRules, fetchFile, getSpareUrls, isCors, isMemoryQueue } = require('../sw-rules')";
|
|
48
48
|
// noinspection JSUnresolvedVariable
|
|
49
49
|
let content = fs_1.default.readFileSync(templatePath, 'utf8')
|
|
50
50
|
.replaceAll("// [insertion site] values", eject?.strValue ?? '')
|
package/dist/SwppConfig.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FileFetchModeLevel = void 0;
|
|
3
|
+
exports.defConfig = exports.FileFetchModeLevel = void 0;
|
|
4
4
|
/** 文件拉取模式等级 */
|
|
5
5
|
var FileFetchModeLevel;
|
|
6
6
|
(function (FileFetchModeLevel) {
|
|
@@ -11,3 +11,44 @@ var FileFetchModeLevel;
|
|
|
11
11
|
/** 宽松,允许任何形式的 404,包括服务器返回 404、DNS 解析失败等 */
|
|
12
12
|
FileFetchModeLevel[FileFetchModeLevel["LOOSE"] = 2] = "LOOSE";
|
|
13
13
|
})(FileFetchModeLevel || (exports.FileFetchModeLevel = FileFetchModeLevel = {}));
|
|
14
|
+
exports.defConfig = {
|
|
15
|
+
serviceWorker: {
|
|
16
|
+
escape: 0,
|
|
17
|
+
cacheName: 'kmarBlogCache',
|
|
18
|
+
debug: false
|
|
19
|
+
},
|
|
20
|
+
register: {
|
|
21
|
+
onerror: () => console.error('Service Worker 注册失败!可能是由于您的浏览器不支持该功能!'),
|
|
22
|
+
builder: (root, _, pluginConfig) => {
|
|
23
|
+
const registerConfig = pluginConfig.register;
|
|
24
|
+
const { onerror, onsuccess } = registerConfig;
|
|
25
|
+
return `<script>
|
|
26
|
+
(() => {
|
|
27
|
+
let sw = navigator.serviceWorker
|
|
28
|
+
let error = ${onerror.toString()}
|
|
29
|
+
if (!sw?.register('${new URL(root).pathname}sw.js')
|
|
30
|
+
${onsuccess ? '?.then(' + onsuccess.toString() + ')' : ''}
|
|
31
|
+
?.catch(error)
|
|
32
|
+
) error()
|
|
33
|
+
})()
|
|
34
|
+
</script>`;
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
dom: {},
|
|
38
|
+
json: {
|
|
39
|
+
maxHtml: 15,
|
|
40
|
+
charLimit: 1024,
|
|
41
|
+
merge: [],
|
|
42
|
+
exclude: {
|
|
43
|
+
localhost: [/^\/sw\.js$/],
|
|
44
|
+
other: []
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
external: {
|
|
48
|
+
timeout: 5000,
|
|
49
|
+
concurrencyLimit: 100,
|
|
50
|
+
js: [],
|
|
51
|
+
stable: [],
|
|
52
|
+
replacer: (it) => it
|
|
53
|
+
}
|
|
54
|
+
};
|
package/dist/SwppRules.js
CHANGED
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.addRulesMapEvent = exports.loadRules = void 0;
|
|
7
7
|
const fs_1 = __importDefault(require("fs"));
|
|
8
8
|
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const SwppConfig_1 = require("./SwppConfig");
|
|
9
10
|
const Utils_1 = require("./Utils");
|
|
10
11
|
const Variant_1 = require("./Variant");
|
|
11
12
|
/**
|
|
@@ -32,7 +33,7 @@ function loadRules(root, fileName, selects) {
|
|
|
32
33
|
const selectRules = selectPath ? require(selectPath) : {};
|
|
33
34
|
const config = rootRules.config ?? {};
|
|
34
35
|
mergeConfig(config, selectRules.config ?? {});
|
|
35
|
-
mergeConfig(config, defConfig);
|
|
36
|
+
mergeConfig(config, SwppConfig_1.defConfig);
|
|
36
37
|
for (let key in selectRules) {
|
|
37
38
|
if (!(key in rootRules))
|
|
38
39
|
rootRules[key] = selectRules[key];
|
|
@@ -41,6 +42,10 @@ function loadRules(root, fileName, selects) {
|
|
|
41
42
|
for (let event of eventList) {
|
|
42
43
|
event(rootRules);
|
|
43
44
|
}
|
|
45
|
+
for (let key in defRules) {
|
|
46
|
+
if (!(key in rootRules))
|
|
47
|
+
rootRules[key] = defRules;
|
|
48
|
+
}
|
|
44
49
|
return (0, Variant_1.writeVariant)('swppRules', (0, Utils_1.deepFreeze)(rootRules));
|
|
45
50
|
}
|
|
46
51
|
exports.loadRules = loadRules;
|
|
@@ -72,44 +77,9 @@ function addRulesMapEvent(mapper) {
|
|
|
72
77
|
(0, Variant_1.readEvent)('addRulesMapEvent').push(mapper);
|
|
73
78
|
}
|
|
74
79
|
exports.addRulesMapEvent = addRulesMapEvent;
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
},
|
|
81
|
-
register: {
|
|
82
|
-
onerror: () => console.error('Service Worker 注册失败!可能是由于您的浏览器不支持该功能!'),
|
|
83
|
-
builder: (root, _, pluginConfig) => {
|
|
84
|
-
const registerConfig = pluginConfig.register;
|
|
85
|
-
const { onerror, onsuccess } = registerConfig;
|
|
86
|
-
return `<script>
|
|
87
|
-
(() => {
|
|
88
|
-
let sw = navigator.serviceWorker
|
|
89
|
-
let error = ${onerror.toString()}
|
|
90
|
-
if (!sw?.register('${new URL(root).pathname}sw.js')
|
|
91
|
-
${onsuccess ? '?.then(' + onsuccess.toString() + ')' : ''}
|
|
92
|
-
?.catch(error)
|
|
93
|
-
) error()
|
|
94
|
-
})()
|
|
95
|
-
</script>`;
|
|
96
|
-
}
|
|
97
|
-
},
|
|
98
|
-
dom: {},
|
|
99
|
-
json: {
|
|
100
|
-
maxHtml: 15,
|
|
101
|
-
charLimit: 1024,
|
|
102
|
-
merge: [],
|
|
103
|
-
exclude: {
|
|
104
|
-
localhost: [/^\/sw\.js$/],
|
|
105
|
-
other: []
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
external: {
|
|
109
|
-
timeout: 5000,
|
|
110
|
-
concurrencyLimit: 100,
|
|
111
|
-
js: [],
|
|
112
|
-
stable: [],
|
|
113
|
-
replacer: (it) => it
|
|
114
|
-
}
|
|
80
|
+
const defRules = {
|
|
81
|
+
config: {},
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
isCors: request => request.headers.get('Content-Type').startsWith('image/'),
|
|
84
|
+
isMemoryQueue: () => false
|
|
115
85
|
};
|
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.
|
|
13
|
+
version: '2.3.0-beta.0',
|
|
14
14
|
cache: {
|
|
15
15
|
readEjectData: Utils_1.readEjectData, readUpdateJson: Variant_1.readUpdateJson,
|
|
16
16
|
readRules: Variant_1.readRules, readMergeVersionMap: Variant_1.readMergeVersionMap,
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
self.addEventListener('activate', event => event.waitUntil(clients.claim()))
|
|
46
46
|
|
|
47
47
|
// noinspection JSFileReferences
|
|
48
|
-
const { cacheRules, fetchFile, getSpareUrls } = require('../sw-rules')
|
|
48
|
+
const { cacheRules, fetchFile, getSpareUrls, isCors, isMemoryQueue } = require('../sw-rules')
|
|
49
49
|
|
|
50
50
|
// 检查请求是否成功
|
|
51
51
|
// noinspection JSUnusedLocalSymbols
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
|
|
74
74
|
/**
|
|
75
75
|
* 缓存列表
|
|
76
|
-
* @type {Map<string,
|
|
76
|
+
* @type {Map<string, function(any)[]>}
|
|
77
77
|
*/
|
|
78
78
|
const cacheMap = new Map()
|
|
79
79
|
|
|
@@ -85,30 +85,35 @@
|
|
|
85
85
|
// [modifyRequest call]
|
|
86
86
|
// [skipRequest call]
|
|
87
87
|
let cacheKey = url.hostname + url.pathname + url.search
|
|
88
|
-
let cache
|
|
89
|
-
if (
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
88
|
+
let cache
|
|
89
|
+
if (isMemoryQueue(request)) {
|
|
90
|
+
cache = cacheMap.get(cacheKey)
|
|
91
|
+
if (cache) {
|
|
92
|
+
return event.respondWith(
|
|
93
|
+
new Promise((resolve, reject) => {
|
|
94
|
+
cacheMap.get(cacheKey).push(arg => arg.body ? resolve(arg) : reject(arg))
|
|
95
|
+
})
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
cacheMap.set(cacheKey, cache = [])
|
|
95
99
|
}
|
|
96
|
-
cacheMap.set(cacheKey, cache = [])
|
|
97
100
|
/** 处理拉取 */
|
|
98
|
-
const handleFetch = promise =>
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
item
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
item
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
101
|
+
const handleFetch = promise => {
|
|
102
|
+
event.respondWith(
|
|
103
|
+
cache ? promise.then(response => {
|
|
104
|
+
for (let item of cache) {
|
|
105
|
+
item(response.clone())
|
|
106
|
+
}
|
|
107
|
+
}).catch(err => {
|
|
108
|
+
for (let item of cache) {
|
|
109
|
+
item(err)
|
|
110
|
+
}
|
|
111
|
+
}).then(() => {
|
|
112
|
+
cacheMap.delete(cacheKey)
|
|
113
|
+
return promise
|
|
114
|
+
}) : promise
|
|
115
|
+
)
|
|
116
|
+
}
|
|
112
117
|
const cacheRule = findCache(url)
|
|
113
118
|
if (cacheRule) {
|
|
114
119
|
let key = `https://${url.host}${url.pathname}`
|
|
@@ -139,10 +144,9 @@
|
|
|
139
144
|
// [debug message]
|
|
140
145
|
if (event.data === 'update')
|
|
141
146
|
updateJson().then(info => {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
)
|
|
147
|
+
info.type = 'update'
|
|
148
|
+
event.source.postMessage(info)
|
|
149
|
+
})
|
|
146
150
|
})
|
|
147
151
|
|
|
148
152
|
/**
|
|
@@ -155,7 +159,7 @@
|
|
|
155
159
|
const fetchWithCors = (request, banCache, optional) => {
|
|
156
160
|
if (!optional) optional = {}
|
|
157
161
|
optional.cache = banCache ? 'no-store' : 'default'
|
|
158
|
-
if (request
|
|
162
|
+
if (isCors(request)) {
|
|
159
163
|
optional.mode = 'cors'
|
|
160
164
|
optional.credentials = 'same-origin'
|
|
161
165
|
}
|
package/package.json
CHANGED
package/types/SwppConfig.d.ts
CHANGED
package/types/SwppRules.d.ts
CHANGED
|
@@ -20,6 +20,23 @@ export interface SwppRules {
|
|
|
20
20
|
cacheRules?: {
|
|
21
21
|
[propName: string]: CacheRules;
|
|
22
22
|
};
|
|
23
|
+
/**
|
|
24
|
+
* 判断指定请求是否使用 CORS
|
|
25
|
+
*
|
|
26
|
+
* **注意:对于需要使用缓存、竞速等功能的 URL 必须使用 CORS,否则无法判断状态码从而使得功能失效或工作异常**
|
|
27
|
+
*
|
|
28
|
+
* @param request 当前请求的 request
|
|
29
|
+
*/
|
|
30
|
+
isCors?: (request: Request) => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* 判断指定请求的是否启用内存队列(即请求合并)
|
|
33
|
+
*
|
|
34
|
+
* 当返回 true 时,SW 会将该请求放置到队列中,如果队列中已有 URL 相同的请求,
|
|
35
|
+
* 则不会发起新的网络请求,而是等待上一个请求结束后直接使用上一个请求的结果。
|
|
36
|
+
*
|
|
37
|
+
* @param request 当前请求的 request
|
|
38
|
+
*/
|
|
39
|
+
isMemoryQueue?: (request: Request) => boolean;
|
|
23
40
|
/**
|
|
24
41
|
* 修改 Request
|
|
25
42
|
* @param request 原始 request
|