swpp-backends 2.1.1 → 2.1.3

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.
@@ -27,12 +27,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.replaceRequest = exports.findCache = exports.eachAllLinkInUrl = exports.findFileHandler = exports.registryFileHandler = exports.submitExternalUrl = exports.submitCacheInfo = exports.loadVersionJson = exports.isStable = exports.isExclude = exports.buildVersionJson = void 0;
30
- const fs_1 = __importDefault(require("fs"));
31
- const path_1 = __importDefault(require("path"));
32
- const node_fetch_1 = require("node-fetch");
33
- const crypto = __importStar(require("crypto"));
34
30
  const buffer_1 = require("buffer");
31
+ const crypto = __importStar(require("crypto"));
35
32
  const fast_html_parser_1 = __importDefault(require("fast-html-parser"));
33
+ const fs_1 = __importDefault(require("fs"));
34
+ const node_fetch_1 = require("node-fetch");
35
+ const path_1 = __importDefault(require("path"));
36
+ const SwppConfig_1 = require("./SwppConfig");
36
37
  const Utils_1 = require("./Utils");
37
38
  const Variant_1 = require("./Variant");
38
39
  /**
@@ -172,20 +173,22 @@ exports.isStable = isStable;
172
173
  *
173
174
  * + **执行该函数前必须调用过 [loadRules]**
174
175
  */
175
- async function loadVersionJson(url) {
176
+ async function loadVersionJson(url, level = SwppConfig_1.FileFetchModeLevel.NORMAL) {
176
177
  const key = 'oldVersionJson';
177
178
  const response = await (0, Utils_1.fetchFile)(url).catch(err => err);
178
- if (response.status === 404 || response?.code === 'ENOTFOUND') {
179
- (0, Utils_1.warn)('VersionJsonLoader', `拉取 ${url} 时出现 404 错误,如果您是第一次构建请忽略这个警告。`);
180
- return (0, Variant_1.writeVariant)(key, null);
181
- }
182
- else if (![200, 301, 302, 307, 308].includes(response.status)) {
183
- (0, Utils_1.error)('VersionJsonLoader', `拉取 ${url} 时出现 ${response.status} 错误!`);
184
- if ('status' in response)
185
- throw `拉取时出现 ${response.status} 异常`;
186
- throw response;
179
+ switch (true) {
180
+ case response.status == 404 && level >= SwppConfig_1.FileFetchModeLevel.NORMAL:
181
+ case response.code == 'ENOTFOUND' && level == SwppConfig_1.FileFetchModeLevel.LOOSE:
182
+ (0, Utils_1.warn)('VersionJsonLoader', `拉取 ${url} 时出现 404 错误,如果您是第一次构建请忽略这个警告。`);
183
+ return (0, Variant_1.writeVariant)(key, null);
184
+ default:
185
+ (0, Utils_1.error)('VersionJsonLoader', `拉取 ${url} 时出现 ${response.status} 错误!`);
186
+ if ('status' in response)
187
+ throw `拉取时出现 ${response.status} 异常`;
188
+ throw response;
189
+ case [200, 301, 302, 307, 308].includes(response.status):
190
+ return (0, Variant_1.writeVariant)(key, await response.json());
187
191
  }
188
- return (0, Variant_1.writeVariant)(key, await response.json());
189
192
  }
190
193
  exports.loadVersionJson = loadVersionJson;
191
194
  /** 提交要存储到 version json 的值 */
@@ -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(fetchFile(request, false))
62
+ else if (modify) handleFetch(fetch(request).catch(err => new Response(err, {status: -1})))
63
63
  `);
64
64
  }
65
65
  if (blockRequest) {
@@ -1,2 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileFetchModeLevel = void 0;
4
+ /** 文件拉取模式等级 */
5
+ var FileFetchModeLevel;
6
+ (function (FileFetchModeLevel) {
7
+ /** 严格,不允许 404 */
8
+ FileFetchModeLevel[FileFetchModeLevel["STRICT"] = 0] = "STRICT";
9
+ /** 普通,允许 404,但仅允许服务器返回 404 状态码 */
10
+ FileFetchModeLevel[FileFetchModeLevel["NORMAL"] = 1] = "NORMAL";
11
+ /** 宽松,允许任何形式的 404,包括服务器返回 404、DNS 解析失败等 */
12
+ FileFetchModeLevel[FileFetchModeLevel["LOOSE"] = 2] = "LOOSE";
13
+ })(FileFetchModeLevel || (exports.FileFetchModeLevel = FileFetchModeLevel = {}));
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getShorthand = exports.loadUpdateJson = exports.submitChange = exports.buildUpdateJson = void 0;
4
+ const SwppConfig_1 = require("./SwppConfig");
4
5
  const Utils_1 = require("./Utils");
5
6
  const Variant_1 = require("./Variant");
6
7
  /**
@@ -112,20 +113,22 @@ exports.submitChange = submitChange;
112
113
  *
113
114
  * + **调用该函数前必须调用过 [loadRules]**
114
115
  */
115
- async function loadUpdateJson(url) {
116
+ async function loadUpdateJson(url, level = SwppConfig_1.FileFetchModeLevel.NORMAL) {
116
117
  const key = 'oldUpdateJson';
117
118
  const response = await (0, Utils_1.fetchFile)(url).catch(err => err);
118
- if (response.status === 404 || response.code === 'ENOTFOUND') {
119
- (0, Utils_1.warn)('UpdateJsonLoader', `拉取 ${url} 时出现 404 错误,如果您是第一次构建请忽略这个警告。`);
120
- return (0, Variant_1.writeVariant)(key, null);
119
+ switch (true) {
120
+ case response.status == 404 && (level >= SwppConfig_1.FileFetchModeLevel.NORMAL):
121
+ case response.code == 'ENOTFOUND' && (level == SwppConfig_1.FileFetchModeLevel.LOOSE):
122
+ (0, Utils_1.warn)('UpdateJsonLoader', `拉取 ${url} 时出现 404 错误,如果您是第一次构建请忽略这个警告。`);
123
+ return (0, Variant_1.writeVariant)(key, null);
124
+ default:
125
+ (0, Utils_1.error)('UpdateJsonLoader', `拉取 ${url} 时出现 ${response.status} 错误!`);
126
+ if ('status' in response)
127
+ throw `拉取时出现 ${response.status} 异常`;
128
+ throw response;
129
+ case [200, 301, 302, 307, 308].includes(response.status):
130
+ return (0, Variant_1.writeVariant)(key, await response.json());
121
131
  }
122
- else if (![200, 301, 302, 307, 308].includes(response.status)) {
123
- (0, Utils_1.error)('UpdateJsonLoader', `拉取 ${url} 时出现 ${response.status} 错误!`);
124
- if ('status' in response)
125
- throw `拉取时出现 ${response.status} 异常`;
126
- throw response;
127
- }
128
- return (0, Variant_1.writeVariant)(key, await response.json());
129
132
  }
130
133
  exports.loadUpdateJson = loadUpdateJson;
131
134
  function zipJson(json) {
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.1',
13
+ version: '2.1.3',
14
14
  cache: {
15
15
  readEjectData: Utils_1.readEjectData, readUpdateJson: Variant_1.readUpdateJson,
16
16
  readRules: Variant_1.readRules, readMergeVersionMap: Variant_1.readMergeVersionMap,
@@ -121,7 +121,7 @@
121
121
  const spare = getSpareUrls(request.url)
122
122
  if (spare) handleFetch(fetchFile(request, false, spare))
123
123
  // [modifyRequest else-if]
124
- else handleFetch(fetch(request))
124
+ else handleFetch(fetch(request).catch(err => new Response(err, {status: -1})))
125
125
  }
126
126
  })
127
127
 
@@ -134,6 +134,7 @@
134
134
  type: 'update',
135
135
  update: info.list,
136
136
  version: info.version,
137
+ old: info.old
137
138
  })
138
139
  )
139
140
  })
@@ -152,7 +153,7 @@
152
153
 
153
154
  /**
154
155
  * 根据JSON删除缓存
155
- * @returns {Promise<{version, list}>}
156
+ * @returns {Promise<{version, list, old}>}
156
157
  */
157
158
  const updateJson = () => {
158
159
  /**
@@ -171,14 +172,17 @@
171
172
  // 跨版本幅度过大,直接清理全站
172
173
  return true
173
174
  }
174
- /** 解析字符串 */
175
+ /**
176
+ * 解析字符串
177
+ * @return {Promise<any>}
178
+ */
175
179
  const parseJson = json => dbVersion.read().then(oldVersion => {
176
180
  const {info, global} = json
177
181
  const newVersion = {global, local: info[0].version, escape: oldVersion?.escape ?? 0}
178
182
  //新用户不进行更新操作
179
183
  if (!oldVersion) {
180
184
  dbVersion.write(newVersion)
181
- return newVersion
185
+ return {version: newVersion, old: oldVersion}
182
186
  }
183
187
  let list = new VersionList()
184
188
  let refresh = parseChange(list, info, oldVersion.local)
@@ -189,19 +193,20 @@
189
193
  if (global !== oldVersion.global) list.force = true
190
194
  else list.refresh = true
191
195
  }
192
- return {list, version: newVersion}
196
+ return {list, version: newVersion, old: oldVersion}
193
197
  })
194
198
  return fetchFile(new Request('/update.json'), false)
195
199
  .then(response => {
196
200
  if (checkResponse(response))
197
201
  return response.json().then(json =>
198
202
  parseJson(json).then(result => {
199
- return result.list ? deleteCache(result.list)
203
+ const info = {version: result.version, old: result.old}
204
+ if (result.list)
205
+ return deleteCache(result.list)
200
206
  .then(list => list.length === 0 ? null : list)
201
- .then(list => ({list, version: result.version}))
202
- : {version: result}
203
- }
204
- )
207
+ .then(list => Object.assign({list}, info))
208
+ else return info
209
+ })
205
210
  )
206
211
  else throw `加载 update.json 时遇到异常,状态码:${response.status}`
207
212
  })
@@ -232,7 +237,10 @@
232
237
  if (this.force) return true
233
238
  // noinspection JSValidateTypes
234
239
  url = new URL(url)
235
- if (this.refresh) return findCache(url).clean
240
+ if (this.refresh) {
241
+ // noinspection JSCheckFunctionSignatures
242
+ return findCache(url).clean
243
+ }
236
244
  else {
237
245
  for (let it of list) {
238
246
  if (it.match(url)) return true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "main": "dist/index.js",
5
5
  "typings": "types/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",
@@ -1,3 +1,4 @@
1
+ import { FileFetchModeLevel } from './SwppConfig';
1
2
  /**
2
3
  * 构建一个 version json
3
4
  *
@@ -51,7 +52,7 @@ export declare function isStable(url: string): boolean;
51
52
  *
52
53
  * + **执行该函数前必须调用过 [loadRules]**
53
54
  */
54
- export declare function loadVersionJson(url: string): Promise<VersionJson | null>;
55
+ export declare function loadVersionJson(url: string, level?: FileFetchModeLevel): Promise<VersionJson | null>;
55
56
  /** 提交要存储到 version json 的值 */
56
57
  export declare function submitCacheInfo(key: string, value: any): void;
57
58
  /** 添加一个要监听的 URL */
@@ -127,3 +127,12 @@ export interface SwppConfigTemplate {
127
127
  replacer?: (srcUrl: string) => string[] | string;
128
128
  };
129
129
  }
130
+ /** 文件拉取模式等级 */
131
+ export declare enum FileFetchModeLevel {
132
+ /** 严格,不允许 404 */
133
+ STRICT = 0,
134
+ /** 普通,允许 404,但仅允许服务器返回 404 状态码 */
135
+ NORMAL = 1,
136
+ /** 宽松,允许任何形式的 404,包括服务器返回 404、DNS 解析失败等 */
137
+ LOOSE = 2
138
+ }
@@ -1,3 +1,4 @@
1
+ import { FileFetchModeLevel } from './SwppConfig';
1
2
  import { AnalyzeResult } from './VersionAnalyzer';
2
3
  /**
3
4
  * 构建新的 update json
@@ -18,7 +19,7 @@ export declare function submitChange(...change: ChangeExpression[]): void;
18
19
  *
19
20
  * + **调用该函数前必须调用过 [loadRules]**
20
21
  */
21
- export declare function loadUpdateJson(url: string): Promise<UpdateJson | null>;
22
+ export declare function loadUpdateJson(url: string, level?: FileFetchModeLevel): Promise<UpdateJson | null>;
22
23
  /**
23
24
  * 获取 URL 的缩写形式
24
25
  *