swpp-backends 0.0.4-alpha → 0.0.6-alpha

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.
@@ -34,8 +34,10 @@ exports.loadUpdateJson = loadUpdateJson;
34
34
  * + **调用该函数前必须调用过 [loadUpdateJson]**
35
35
  */
36
36
  function readUpdateJson() {
37
- if (_oldJson === undefined)
37
+ if (_oldJson === undefined) {
38
+ (0, Utils_1.error)('UpdateJsonReader', 'UpdateJson 尚未初始化');
38
39
  throw 'UpdateJson 未初始化';
40
+ }
39
41
  return _oldJson;
40
42
  }
41
43
  exports.readUpdateJson = readUpdateJson;
@@ -52,8 +54,10 @@ exports.readUpdateJson = readUpdateJson;
52
54
  */
53
55
  function buildNewInfo(root, dif) {
54
56
  const config = (0, SwppRules_1.readRules)().config.json;
55
- if (!config)
57
+ if (!config) {
58
+ (0, Utils_1.error)('NewInfoBuilder', '功能未开启');
56
59
  throw '功能未开启';
60
+ }
57
61
  const old = readUpdateJson();
58
62
  let global = old?.global ?? 0;
59
63
  if (dif.force)
@@ -274,6 +278,7 @@ function getShorthand(url, offset = 0) {
274
278
  removeSet.forEach(it => collide.delete(it));
275
279
  break;
276
280
  default:
281
+ (0, Utils_1.error)('Shorthand', '意料之外的错误:' + count);
277
282
  throw '意料之外的错误:' + count;
278
283
  }
279
284
  }
@@ -61,7 +61,7 @@ function eachAllFile(root, cb) {
61
61
  function isExclude(domain, url) {
62
62
  const exclude = (0, SwppRules_1.readRules)().config?.json?.exclude;
63
63
  if (!exclude)
64
- throw 'exclude 为空';
64
+ return false;
65
65
  const list = isExternalLink(domain, url) ? exclude.other : exclude.localhost;
66
66
  for (let reg of list) {
67
67
  if (url.match(reg))
@@ -78,7 +78,7 @@ exports.isExclude = isExclude;
78
78
  function isStable(url) {
79
79
  const stable = (0, SwppRules_1.readRules)().config?.external?.stable;
80
80
  if (!stable)
81
- throw 'stable 为空';
81
+ return false;
82
82
  for (let reg of stable) {
83
83
  if (url.match(reg))
84
84
  return true;
@@ -112,8 +112,10 @@ exports.submitCacheInfo = submitCacheInfo;
112
112
  * + **调用该函数前必须调用过 [loadCacheJson]**
113
113
  */
114
114
  function readOldVersionJson() {
115
- if (!_oldVersionJson)
116
- throw 'cache json 尚未初始化';
115
+ if (!_oldVersionJson) {
116
+ (0, Utils_1.error)('OldVersionReader', 'version json 尚未初始化');
117
+ throw 'version json 尚未初始化';
118
+ }
117
119
  return _oldVersionJson;
118
120
  }
119
121
  exports.readOldVersionJson = readOldVersionJson;
@@ -126,8 +128,10 @@ exports.readOldVersionJson = readOldVersionJson;
126
128
  * + **执行该函数前必须调用过 [calcEjectValues]**
127
129
  */
128
130
  function readNewVersionJson() {
129
- if (!_newVersionJson)
130
- throw 'cache json 尚未初始化';
131
+ if (!_newVersionJson) {
132
+ (0, Utils_1.error)('NewVersionReader', 'version json 尚未初始化');
133
+ throw 'version json 尚未初始化';
134
+ }
131
135
  return _newVersionJson;
132
136
  }
133
137
  exports.readNewVersionJson = readNewVersionJson;
@@ -241,9 +245,15 @@ async function eachAllLinkInUrl(domain, url, result, event) {
241
245
  return;
242
246
  }
243
247
  }
244
- const response = await (0, Utils_1.fetchFile)(url);
245
- if (![200, 301, 302, 307, 308].includes(response.status))
248
+ const response = await (0, Utils_1.fetchFile)(url).catch(err => {
249
+ (0, Utils_1.error)('LinkItorInUrl', `拉取文件 [${url}] 时发生异常:${err}`);
250
+ });
251
+ if (!response)
252
+ throw '拉取时异常';
253
+ if (![200, 301, 302, 307, 308].includes(response.status)) {
254
+ (0, Utils_1.error)('LinkItorInUrl', `拉取文件 [${url}] 时出现错误:${response.status}`);
246
255
  throw response;
256
+ }
247
257
  const pathname = new URL(url).pathname;
248
258
  let content;
249
259
  const relay = [];
@@ -389,8 +399,10 @@ exports.eachAllLinkInCss = eachAllLinkInCss;
389
399
  */
390
400
  async function eachAllLinkInJavaScript(domain, content, result, event) {
391
401
  const ruleList = (0, SwppRules_1.readRules)().config?.external?.js;
392
- if (!ruleList)
402
+ if (!ruleList) {
403
+ (0, Utils_1.error)('LinkItorInJS', '不应发生的异常');
393
404
  throw 'ruleList 为空';
405
+ }
394
406
  for (let value of ruleList) {
395
407
  if (typeof value === 'function') {
396
408
  const urls = value(content);
@@ -431,7 +443,7 @@ function findCache(url) {
431
443
  url = new URL(replaceRequest(url.href));
432
444
  for (let key in cacheRules) {
433
445
  const value = cacheRules[key];
434
- if (value.match(url, eject.nodeEject))
446
+ if (value.match(url, eject?.nodeEject))
435
447
  return value;
436
448
  }
437
449
  return null;
@@ -449,6 +461,6 @@ function replaceRequest(url) {
449
461
  return url;
450
462
  const { modifyRequest } = rules;
451
463
  const request = new node_fetch_1.Request(url);
452
- return modifyRequest?.(request, (0, Utils_1.readEjectData)().nodeEject)?.url ?? url;
464
+ return modifyRequest?.(request, (0, Utils_1.readEjectData)()?.nodeEject)?.url ?? url;
453
465
  }
454
466
  exports.replaceRequest = replaceRequest;
package/dist/index.js CHANGED
@@ -18,7 +18,7 @@ exports.default = {
18
18
  buildVersionJson: FileAnalyzer_1.buildVersionJson,
19
19
  buildNewInfo: UpdateJsonBuilder_1.buildNewInfo,
20
20
  calcEjectValues: Utils_1.calcEjectValues,
21
- analyzer: VersionAnalyzer_1.analyze
21
+ analyze: VersionAnalyzer_1.analyze
22
22
  },
23
23
  loader: {
24
24
  loadRules: SwppRules_1.loadRules, loadUpdateJson: UpdateJsonBuilder_1.loadUpdateJson, loadVersionJson: FileAnalyzer_1.loadVersionJson
@@ -19,7 +19,7 @@ function buildServiceWorker() {
19
19
  const eject = (0, Utils_1.readEjectData)();
20
20
  const { modifyRequest, fetchFile, getRaceUrls, getSpareUrls, blockRequest, config } = rules;
21
21
  const serviceWorkerConfig = config.serviceWorker;
22
- const templatePath = path_1.default.resolve('./', module.path, 'sw-template.js');
22
+ const templatePath = path_1.default.resolve('./', module.path, 'resources/sw-template.js');
23
23
  // 获取拓展文件
24
24
  let cache = (0, Utils_1.getSource)(rules, undefined, [
25
25
  'cacheList', 'modifyRequest', 'getCdnList', 'getSpareUrls', 'blockRequest', 'fetchFile',
@@ -42,7 +42,7 @@ function buildServiceWorker() {
42
42
  const keyword = "const { cacheList, fetchFile, getSpareUrls } = require('../sw-rules')";
43
43
  // noinspection JSUnresolvedVariable
44
44
  let content = fs_1.default.readFileSync(templatePath, 'utf8')
45
- .replaceAll("// [insertion site] values", eject.strValue ?? '')
45
+ .replaceAll("// [insertion site] values", eject?.strValue ?? '')
46
46
  .replaceAll(keyword, cache)
47
47
  .replaceAll("'@$$[escape]'", (serviceWorkerConfig.escape).toString())
48
48
  .replaceAll("'@$$[cacheName]'", `'${serviceWorkerConfig.cacheName}'`);
package/dist/swppRules.js CHANGED
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.loadRules = exports.addRulesMapEvent = exports.readRules = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
+ const Utils_1 = require("./Utils");
9
10
  const defConfig = {
10
11
  serviceWorker: {
11
12
  escape: 0,
@@ -54,8 +55,10 @@ let _rules;
54
55
  * **执行该函数前必须调用过 [loadRules]**
55
56
  */
56
57
  function readRules() {
57
- if (!_rules)
58
+ if (!_rules) {
59
+ (0, Utils_1.error)('RulesReader', 'rules 尚未初始化');
58
60
  throw 'rules 尚未初始化';
61
+ }
59
62
  return _rules;
60
63
  }
61
64
  exports.readRules = readRules;
@@ -82,8 +85,10 @@ function loadRules(root, fileName, selects) {
82
85
  .find(it => fs_1.default.existsSync(it));
83
86
  // 其它可选目录下的 rules 文件
84
87
  const selectPath = selects.flatMap(value => extensions.map(it => path_1.default.resolve(value, `${fileName}.${it}`))).find(it => fs_1.default.existsSync(it));
85
- if (!(rootPath || selectPath))
88
+ if (!(rootPath || selectPath)) {
89
+ (0, Utils_1.error)('RulesLoader', '未查询到 rules 文件');
86
90
  throw '未查询到 rules 文件';
91
+ }
87
92
  const rootRules = rootPath ? { ...require(rootPath) } : {};
88
93
  const selectRules = selectPath ? require(selectPath) : {};
89
94
  const config = rootRules.config ?? {};
package/dist/utils.js CHANGED
@@ -28,8 +28,10 @@ let ejectData = undefined;
28
28
  */
29
29
  function calcEjectValues(framework) {
30
30
  const rules = (0, SwppRules_1.readRules)();
31
- if (!('ejectValues' in rules))
31
+ if (!('ejectValues' in rules)) {
32
+ ejectData = null;
32
33
  return;
34
+ }
33
35
  // noinspection JSUnresolvedReference
34
36
  const eject = rules.ejectValues?.(framework, rules);
35
37
  const nodeEject = {};
@@ -62,8 +64,10 @@ exports.calcEjectValues = calcEjectValues;
62
64
  * + **执行该函数前必须调用过 [calcEjectValues]**
63
65
  */
64
66
  function readEjectData() {
65
- if (!ejectData)
67
+ if (ejectData === undefined) {
68
+ error('EjectReader', 'eject data 尚未初始化');
66
69
  throw 'eject data 尚未初始化';
70
+ }
67
71
  return ejectData;
68
72
  }
69
73
  exports.readEjectData = readEjectData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "0.0.4-alpha",
3
+ "version": "0.0.6-alpha",
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/index.d.ts CHANGED
@@ -18,7 +18,7 @@ declare const _default: {
18
18
  buildVersionJson: typeof buildVersionJson;
19
19
  buildNewInfo: typeof buildNewInfo;
20
20
  calcEjectValues: typeof calcEjectValues;
21
- analyzer: typeof analyze;
21
+ analyze: typeof analyze;
22
22
  };
23
23
  loader: {
24
24
  loadRules: typeof loadRules;
package/types/utils.d.ts CHANGED
@@ -18,7 +18,7 @@ export declare function calcEjectValues(framework: any): void;
18
18
  * + **执行该函数前必须调用过 [loadRules]**
19
19
  * + **执行该函数前必须调用过 [calcEjectValues]**
20
20
  */
21
- export declare function readEjectData(): EjectCache;
21
+ export declare function readEjectData(): EjectCache | null;
22
22
  /**
23
23
  * 获取指定值的 js 源码表达形式
24
24
  * @param obj 要转换的对象