swpp-backends 1.1.0 → 1.1.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/README.md CHANGED
@@ -1,5 +1,41 @@
1
1
  ## 欢迎使用 SwppBackends
2
2
 
3
-   这是 Service Worker Plus Plus 的后端仓库,代码正在开发中,开发完毕后会在这里补充文档并上传到 npm。
3
+ swpp-backends(以下简称 swpp)插件的功能是为网站生成一个高度可用的 ServiceWorker(以下简称 SW),为网站优化二次加载、提供离线体验、提高可靠性,并为此附带了一些其它的功能。
4
4
 
5
-   有兴趣的可以看 [hexo-swpp | 山岳库博](https://kmar.top/posts/73014407/)。
5
+ swpp 的全拼为“ServiceWorkerPlusPlus”(或“ServiceWorker++”),但是其与已有的插件“hexo-service-worker”并没有关系,插件中所有代码均为我个人开发,这一点请不要误解。
6
+
7
+ swpp 生成的 SW 与其它插件的对比:
8
+
9
+ | | swpp | hexo-offline |
10
+ |:----------:|:-----:|:------------:|
11
+ | 本地缓存 | ✔️ | ✔️ |
12
+ | 缓存增量更新 | ✔️ | ❌ |
13
+ | 缓存过期时间 | ❌^1^ | ✔️ |
14
+ | 缓存大小限制 | ❌ | ✔️ |
15
+ | 预缓存 | ❌ | ✔️ |
16
+ | Request 篡改 | ✔️ | ❌ |
17
+ | URL 竞速 | ✔️ | ❌ |
18
+ | 备用 URL | ✔️ | ❌ |
19
+ | 阻塞响应 | ✔️ | ❌ |
20
+ | 逃生门 | ✔️ | ❌ |
21
+ | 跨平台 | ✔️^2^ | ❌ |
22
+ | 高度自由 | ✔️ | ❌ |
23
+ | 更新 | 非常频繁 | 超过两年没有更新 |
24
+
25
+ + ✔️:支持
26
+ + ❌:不支持
27
+
28
+ 1. 因为有增量更新,所以没提供过期的实现,没必要
29
+ 2. 目前只有 hexo 平台存在具体实现,其它平台的暂时还没有
30
+
31
+ 注:上面提到的跨平台是指跨越框架(比如 Hexo、WordPress 等)。
32
+
33
+ 目前支持的平台:
34
+
35
+ | 平台 | 插件名 | 文档 | 作者 |
36
+ |:----:|:-----------:|:---------------------------------------------------------:|:-----------------------:|
37
+ | hexo | `hexo-swpp` | [github](https://github.com/EmptyDreams/hexo-swpp#readme) | [空梦](https://kmar.top/) |
38
+
39
+ 如果你为某一个平台做了适配,可以在 gh 上发布 issue 或者在文档页面发布评论~
40
+
41
+ 文档:[Swpp Backends 官方文档 | 山岳库博](https://kmar.top/posts/b70ec88f/)
@@ -57,7 +57,7 @@ function buildServiceWorker() {
57
57
  if (blockRequest) {
58
58
  content = content.replace('// [blockRequest call]', `
59
59
  if (blockRequest(url))
60
- return event.respondWith(new Response(null, {status: 208}))
60
+ return event.respondWith(new Response(null, {status: 204}))
61
61
  `);
62
62
  }
63
63
  // noinspection JSUnresolvedVariable
@@ -128,14 +128,16 @@ const JS_CODE_GET_SPARE_URLS = `
128
128
  Object.assign({
129
129
  signal: (controllers[index] = new AbortController()).signal
130
130
  }, fetchArgs)
131
- ).then(response => checkResponse(response) ? {r: response, i: index} : Promise.reject(response.status))
131
+ ).then(response => checkResponse(response) ? {r: response, i: index} : Promise.reject())
132
132
  return new Promise((resolve, reject) => {
133
+ let flag = true
133
134
  const startAll = () => {
135
+ flag = false
134
136
  Promise.any([
135
137
  first,
136
138
  ...Array.from({
137
139
  length: list.length - 1
138
- }, (_, index) => index + 2).map(index => startFetch(index))
140
+ }, (_, index) => index + 1).map(index => startFetch(index))
139
141
  ]).then(res => {
140
142
  for (let i = 0; i !== list.length; ++i) {
141
143
  if (i !== res.i)
@@ -147,11 +149,16 @@ const JS_CODE_GET_SPARE_URLS = `
147
149
  const id = setTimeout(startAll, spare.timeout)
148
150
  const first = startFetch(0)
149
151
  .then(res => {
150
- clearTimeout(id)
151
- resolve(res.r)
152
+ if (flag) {
153
+ clearTimeout(id)
154
+ resolve(res.r)
155
+ }
152
156
  }).catch(() => {
153
- clearTimeout(id)
154
- startAll()
157
+ if (flag) {
158
+ clearTimeout(id)
159
+ startAll()
160
+ }
161
+ return Promise.reject()
155
162
  })
156
163
  })
157
164
  }
package/dist/SwppRules.js CHANGED
@@ -104,6 +104,7 @@ function loadRules(root, fileName, selects) {
104
104
  event(rootRules);
105
105
  }
106
106
  eventList = null;
107
+ (0, Utils_1.deepFreeze)(rootRules);
107
108
  return _rules = rootRules;
108
109
  }
109
110
  exports.loadRules = loadRules;
package/dist/Utils.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.replaceDevRequest = exports.fetchFile = exports.getSource = exports.readEjectData = exports.calcEjectValues = exports.warn = exports.error = void 0;
6
+ exports.deepFreeze = exports.replaceDevRequest = exports.fetchFile = exports.getSource = exports.readEjectData = exports.calcEjectValues = exports.warn = exports.error = void 0;
7
7
  const node_fetch_1 = __importDefault(require("node-fetch"));
8
8
  const SwppRules_1 = require("./SwppRules");
9
9
  const logger = require('hexo-log').default({
@@ -190,3 +190,15 @@ async function fetchSpeed(list) {
190
190
  }
191
191
  return result.response;
192
192
  }
193
+ /** 深度冻结一个对象,这将使得无法修改对象中的任何值,也无法添加新的值 */
194
+ function deepFreeze(obj) {
195
+ if (!obj)
196
+ return;
197
+ Object.freeze(obj);
198
+ for (let key in obj) {
199
+ const value = obj[key];
200
+ if (typeof value === 'object')
201
+ deepFreeze(value);
202
+ }
203
+ }
204
+ exports.deepFreeze = deepFreeze;
package/dist/index.js CHANGED
@@ -30,6 +30,6 @@ exports.default = {
30
30
  getSource: Utils_1.getSource, getShorthand: UpdateJsonBuilder_1.getShorthand, findCache: FileAnalyzer_1.findCache,
31
31
  fetchFile: Utils_1.fetchFile, replaceDevRequest: Utils_1.replaceDevRequest, replaceRequest: FileAnalyzer_1.replaceRequest,
32
32
  isStable: FileAnalyzer_1.isStable, isExclude: FileAnalyzer_1.isExclude, findFileHandler: FileAnalyzer_1.findFileHandler,
33
- eachAllLinkInUrl: FileAnalyzer_1.eachAllLinkInUrl
33
+ eachAllLinkInUrl: FileAnalyzer_1.eachAllLinkInUrl, deepFreeze: Utils_1.deepFreeze
34
34
  }
35
35
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "main": "dist/index.js",
5
5
  "typings": "types/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",
package/types/Utils.d.ts CHANGED
@@ -39,3 +39,5 @@ export declare function fetchFile(link: string): Promise<import("node-fetch").Re
39
39
  * **调用该函数前必须调用过 [loadRules]**
40
40
  */
41
41
  export declare function replaceDevRequest(link: string): string[] | string;
42
+ /** 深度冻结一个对象,这将使得无法修改对象中的任何值,也无法添加新的值 */
43
+ export declare function deepFreeze(obj: any): void;
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { readEjectData, getSource, fetchFile, replaceDevRequest, calcEjectValues } from './Utils';
1
+ import { readEjectData, getSource, fetchFile, replaceDevRequest, calcEjectValues, deepFreeze } from './Utils';
2
2
  import { isExclude, isStable, loadVersionJson, readOldVersionJson, readNewVersionJson, readMergeVersionMap, buildVersionJson, eachAllLinkInUrl, findCache, findFileHandler, replaceRequest, submitCacheInfo, submitExternalUrl, registryFileHandler } from './FileAnalyzer';
3
3
  import { buildServiceWorker } from './ServiceWorkerBuilder';
4
4
  import { readRules, loadRules, addRulesMapEvent } from './SwppRules';
@@ -44,6 +44,7 @@ declare const _default: {
44
44
  isExclude: typeof isExclude;
45
45
  findFileHandler: typeof findFileHandler;
46
46
  eachAllLinkInUrl: typeof eachAllLinkInUrl;
47
+ deepFreeze: typeof deepFreeze;
47
48
  };
48
49
  };
49
50
  export default _default;