swpp-backends 2.3.3 → 2.3.7

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/README.md CHANGED
@@ -8,29 +8,28 @@ swpp 的全拼为“Service Worker Plus Plus”(或“Service Worker++”)
8
8
 
9
9
  swpp 生成的 SW 与其它插件的对比:
10
10
 
11
- | | swpp | hexo-offline |
12
- |:----------:|:-------------:|:------------:|
13
- | 本地缓存 | ✔️ | ✔️ |
14
- | 缓存增量更新 | ✔️ | ❌ |
15
- | 缓存过期时间 | ❌ | ✔️ |
16
- | 缓存大小限制 | ❌ | ✔️ |
17
- | 预缓存 | ❌<sup>1</sup> | ✔️ |
18
- | Request 篡改 | ✔️ | ❌ |
19
- | URL 竞速 | ✔️ | ❌ |
20
- | 备用 URL | ✔️ | ❌ |
21
- | 204 阻塞响应 | ✔️ | ❌ |
22
- | 逃生门 | ✔️ | ❌ |
23
- | 请求合并 | ✔️ | ❌ |
24
- | 跨平台 | ✔️ | ❌ |
25
- | 高度自由 | ✔️ | ❌ |
26
- | 更新 | 非常频繁 | 超过两年没有更新 |
27
-
28
- <small>&emsp;注:上面提到的跨平台是指跨越框架(比如 Hexo、WordPress 等)。</small>
11
+ | | swpp | hexo-offline |
12
+ |:---------------:|:-------------:|:------------:|
13
+ | 本地缓存 | ✔️ | ✔️ |
14
+ | 缓存增量更新 | ✔️ | ❌ |
15
+ | 缓存过期时间 | ❌ | ✔️ |
16
+ | 缓存大小限制 | ❌ | ✔️ |
17
+ | 预缓存 | ❌<sup>1</sup> | ✔️ |
18
+ | Request 篡改 | ✔️ | ❌ |
19
+ | URL 竞速 | ✔️ | ❌ |
20
+ | 备用 URL | ✔️ | ❌ |
21
+ | 204 阻塞响应 | ✔️ | ❌ |
22
+ | 逃生门 | ✔️ | ❌ |
23
+ | 请求合并 | ✔️ | ❌ |
24
+ | 跨平台<sup>2</sup> | ✔️ | ❌ |
25
+ | 高度自由 | ✔️ | ❌ |
26
+ | 更新 | 非常频繁 | 超过两年没有更新 |
29
27
 
30
28
  + ✔️:支持
31
29
  + ❌:不支持
32
30
 
33
31
  1. 预缓存可以在前端实现,SW 实现这个功能容易拖延注册时间
32
+ 2. 跨平台是指跨越框架(可在 NodeJS 平台中的任意框架下工作)
34
33
 
35
34
  目前支持的平台:
36
35
 
@@ -29,14 +29,23 @@ function buildServiceWorker() {
29
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
+ let fetchFileReplaced;
32
33
  if (!fetchFile) {
33
34
  const { JS_CODE_GET_CDN_LIST, JS_CODE_GET_SPARE_URLS, JS_CODE_DEF_FETCH_FILE } = require('./resources/sw-fetch.js');
34
- if (getRaceUrls)
35
- cache += JS_CODE_GET_CDN_LIST;
36
- else if (getSpareUrls)
37
- cache += JS_CODE_GET_SPARE_URLS;
38
- else
39
- cache += JS_CODE_DEF_FETCH_FILE;
35
+ let selected, replaced;
36
+ if (getRaceUrls) {
37
+ selected = JS_CODE_GET_CDN_LIST;
38
+ fetchFileReplaced = 'getRaceUrls(request.url)';
39
+ }
40
+ else if (getSpareUrls) {
41
+ selected = JS_CODE_GET_SPARE_URLS;
42
+ fetchFileReplaced = 'getSpareUrls(request.url)';
43
+ }
44
+ else {
45
+ selected = JS_CODE_DEF_FETCH_FILE;
46
+ fetchFileReplaced = 'null';
47
+ }
48
+ cache += selected;
40
49
  }
41
50
  if (!getSpareUrls)
42
51
  cache += `\nconst getSpareUrls = _ => {}`;
@@ -44,13 +53,16 @@ function buildServiceWorker() {
44
53
  cache += `(${(0, Utils_1.getSource)(rules['afterJoin'])})()\n`;
45
54
  if ('afterTheme' in rules)
46
55
  cache += `(${(0, Utils_1.getSource)(rules['afterTheme'])})()\n`;
47
- const keyword = "const { cacheRules, fetchFile, getSpareUrls, isCors, isMemoryQueue } = require('../sw-rules')";
56
+ const keyword = "const { cacheRules, fetchFile, isCors, isMemoryQueue } = require('../sw-rules')";
48
57
  // noinspection JSUnresolvedVariable
49
58
  let content = fs_1.default.readFileSync(templatePath, 'utf8')
50
59
  .replaceAll("// [insertion site] values", eject?.strValue ?? '')
51
60
  .replaceAll(keyword, cache)
52
61
  .replaceAll("'@$$[escape]'", (serviceWorkerConfig.escape).toString())
53
62
  .replaceAll("'@$$[cacheName]'", `'${serviceWorkerConfig.cacheName}'`);
63
+ if (fetchFileReplaced) {
64
+ content = content.replaceAll('[] // [spareUrls or raceUrls call]', fetchFileReplaced);
65
+ }
54
66
  if (modifyRequest) {
55
67
  content = content.replaceAll('// [modifyRequest call]', `
56
68
  const modify = modifyRequest(request)
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.3.3',
13
+ version: '2.3.7',
14
14
  cache: {
15
15
  readEjectData: Utils_1.readEjectData, readUpdateJson: Variant_1.readUpdateJson,
16
16
  readRules: Variant_1.readRules, readMergeVersionMap: Variant_1.readMergeVersionMap,
@@ -4,10 +4,13 @@ let getRaceUrls, checkResponse, getSpareUrls, fetchWithCors
4
4
  module.exports.JS_CODE_DEF_FETCH_FILE = 'const fetchFile = fetchWithCors;'
5
5
 
6
6
  module.exports.JS_CODE_GET_CDN_LIST= 'const fetchFile = ' + (
7
- (request, banCache) => {
8
- const list = getRaceUrls(request.url)
9
- if (!list || !Promise.any) return fetchWithCors(request, banCache)
10
- const res = list.map(url => new Request(url, request))
7
+ (request, banCache, urls) => {
8
+ if (!urls) {
9
+ urls = getRaceUrls(request.url)
10
+ if (!urls) return fetchWithCors(request, banCache)
11
+ }
12
+ if (!urls || !Promise.any) return fetchWithCors(request, banCache)
13
+ const res = urls.map(url => new Request(url, request))
11
14
  const controllers = []
12
15
  // noinspection JSCheckFunctionSignatures
13
16
  return Promise.any(
@@ -25,16 +28,15 @@ module.exports.JS_CODE_GET_CDN_LIST= 'const fetchFile = ' + (
25
28
  ).toString()
26
29
 
27
30
  module.exports.JS_CODE_GET_SPARE_URLS = 'const fetchFile = ' + (
28
- (request, banCache, spare = null) => {
29
- if (!spare) {
30
- spare = getSpareUrls(request.url)
31
- if (!spare) return fetchWithCors(request, banCache)
31
+ (request, banCache, urls = null) => {
32
+ if (!urls) {
33
+ urls = getSpareUrls(request.url)
34
+ if (!urls) return fetchWithCors(request, banCache)
32
35
  }
33
- const list = spare.list
36
+ const list = urls.list
34
37
  const controllers = new Array(list.length)
35
38
  // noinspection JSCheckFunctionSignatures
36
- const startFetch =
37
- index => fetchWithCors(
39
+ const startFetch = index => fetchWithCors(
38
40
  new Request(list[index], request),
39
41
  banCache,
40
42
  {signal: (controllers[index] = new AbortController()).signal}
@@ -56,7 +58,7 @@ module.exports.JS_CODE_GET_SPARE_URLS = 'const fetchFile = ' + (
56
58
  resolve(res.r)
57
59
  }).catch(() => reject(`请求 ${request.url} 失败`))
58
60
  }
59
- const id = setTimeout(startAll, spare.timeout)
61
+ const id = setTimeout(startAll, urls.timeout)
60
62
  const first = startFetch(0)
61
63
  .then(res => {
62
64
  if (flag) {
@@ -48,7 +48,7 @@
48
48
  self.addEventListener('activate', event => event.waitUntil(clients.claim()))
49
49
 
50
50
  // noinspection JSFileReferences
51
- const { cacheRules, fetchFile, getSpareUrls, isCors, isMemoryQueue } = require('../sw-rules')
51
+ const { cacheRules, fetchFile, isCors, isMemoryQueue } = require('../sw-rules')
52
52
 
53
53
  // 检查请求是否成功
54
54
  // noinspection JSUnusedLocalSymbols
@@ -136,8 +136,8 @@
136
136
  )
137
137
  )
138
138
  } else {
139
- const spare = getSpareUrls(request.url)
140
- if (spare) handleFetch(fetchFile(request, false, spare))
139
+ const urls = [] // [spareUrls or raceUrls call]
140
+ if (urls) handleFetch(fetchFile(request, false, urls))
141
141
  // [modifyRequest else-if]
142
142
  else handleFetch(fetchWithCache(request).catch(err => new Response(err, {status: 499})))
143
143
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "2.3.3",
3
+ "version": "2.3.7",
4
4
  "main": "dist/index.js",
5
5
  "typings": "types/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",
@@ -87,9 +87,9 @@ export interface SwppRules {
87
87
  *
88
88
  * @param request 请求信息
89
89
  * @param banCache 是否禁用缓存
90
- * @param spare 备用 URL
90
+ * @param urls 备用 URL 或 URL 竞速的 URL 列表,为空表示需要手动获取
91
91
  */
92
- fetchFile?: (request: Request, banCache: boolean, spare?: SpareURLs) => Promise<Response>;
92
+ fetchFile?: (request: Request, banCache: boolean, urls?: SpareURLs | string[]) => Promise<Response>;
93
93
  /** 第三方添加的值 */
94
94
  [propName: string]: any;
95
95
  }