swpp-backends 2.1.1 → 2.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.
@@ -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 的值 */
@@ -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.2',
14
14
  cache: {
15
15
  readEjectData: Utils_1.readEjectData, readUpdateJson: Variant_1.readUpdateJson,
16
16
  readRules: Variant_1.readRules, readMergeVersionMap: Variant_1.readMergeVersionMap,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "2.1.1",
3
+ "version": "2.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.",
@@ -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
  *