swpp-backends 0.0.11-alpha → 0.0.12-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.
@@ -23,6 +23,8 @@ function analyze(version) {
23
23
  remove: []
24
24
  }
25
25
  };
26
+ if (!oldVersion)
27
+ return result;
26
28
  if (version.version !== oldVersion.version) {
27
29
  result.force = true;
28
30
  return result;
@@ -89,16 +89,22 @@ function isStable(url) {
89
89
  }
90
90
  exports.isStable = isStable;
91
91
  /**
92
- * 从指定 URL 加载 cache json
92
+ * 从指定 URL 加载 version json
93
93
  *
94
94
  * + **执行该函数前必须调用过 [loadRules]**
95
95
  */
96
96
  async function loadVersionJson(url) {
97
- const response = await (0, Utils_1.fetchFile)(url);
98
- return _oldVersionJson = (await response.json());
97
+ const response = await (0, Utils_1.fetchFile)(url).catch(err => err);
98
+ if (response?.status === 404) {
99
+ (0, Utils_1.warn)('LoadVersionJson', `拉取 ${url} 时出现 404 错误,如果您是第一次构建请忽略这个警告。`);
100
+ return _oldVersionJson = null;
101
+ }
102
+ else {
103
+ return _oldVersionJson = (await response.json());
104
+ }
99
105
  }
100
106
  exports.loadVersionJson = loadVersionJson;
101
- let _oldVersionJson;
107
+ let _oldVersionJson = undefined;
102
108
  let _newVersionJson;
103
109
  let _mergeVersionMap;
104
110
  const event = new Map();
@@ -114,7 +120,7 @@ exports.submitCacheInfo = submitCacheInfo;
114
120
  * + **调用该函数前必须调用过 [loadCacheJson]**
115
121
  */
116
122
  function readOldVersionJson() {
117
- if (!_oldVersionJson) {
123
+ if (_oldVersionJson === undefined) {
118
124
  (0, Utils_1.error)('OldVersionReader', 'version json 尚未初始化');
119
125
  throw 'version json 尚未初始化';
120
126
  }
@@ -149,7 +155,7 @@ function readMergeVersionMap() {
149
155
  if (_mergeVersionMap)
150
156
  return _mergeVersionMap;
151
157
  const map = {};
152
- Object.assign(map, readOldVersionJson().list);
158
+ Object.assign(map, readOldVersionJson()?.list ?? {});
153
159
  Object.assign(map, readNewVersionJson().list);
154
160
  return _mergeVersionMap = map;
155
161
  }
@@ -230,8 +236,8 @@ async function eachAllLinkInUrl(domain, url, result, event) {
230
236
  event?.(url);
231
237
  const stable = isStable(url);
232
238
  if (stable) {
233
- const old = readOldVersionJson().list;
234
- if (Array.isArray(old[url])) {
239
+ const old = readOldVersionJson()?.list;
240
+ if (Array.isArray(old?.[url])) {
235
241
  const copyTree = (key) => {
236
242
  const value = old[key];
237
243
  if (!value)
@@ -248,14 +254,10 @@ async function eachAllLinkInUrl(domain, url, result, event) {
248
254
  return;
249
255
  }
250
256
  }
251
- const response = await (0, Utils_1.fetchFile)(url).catch(err => {
252
- (0, Utils_1.error)('LinkItorInUrl', `拉取文件 [${url}] 时发生异常:${err}`);
253
- });
254
- if (!response)
255
- throw '拉取时异常';
256
- if (![200, 301, 302, 307, 308].includes(response.status)) {
257
- (0, Utils_1.error)('LinkItorInUrl', `拉取文件 [${url}] 时出现错误:${response.status}`);
258
- throw response;
257
+ const response = await (0, Utils_1.fetchFile)(url).catch(err => err);
258
+ if (![200, 301, 302, 307, 308].includes(response?.status ?? 0)) {
259
+ (0, Utils_1.error)('LinkItorInUrl', `拉取文件 [${url}] 时出现错误:${response?.status}`);
260
+ return;
259
261
  }
260
262
  const pathname = new URL(url).pathname;
261
263
  let content;
@@ -274,12 +276,12 @@ async function eachAllLinkInUrl(domain, url, result, event) {
274
276
  case pathname.endsWith('/'):
275
277
  content = await response.text();
276
278
  update();
277
- await eachAllLinkInHtml(domain, content, url.substring(0, url.lastIndexOf('/') + 1), result, nextEvent);
279
+ await eachAllLinkInHtml(domain, url.substring(0, url.lastIndexOf('/') + 1), content, result, nextEvent);
278
280
  break;
279
281
  case pathname.endsWith('.css'):
280
282
  content = await response.text();
281
283
  update();
282
- await eachAllLinkInCss(domain, content, url.substring(0, url.lastIndexOf('/') + 1), result, nextEvent);
284
+ await eachAllLinkInCss(domain, url.substring(0, url.lastIndexOf('/') + 1), content, result, nextEvent);
283
285
  break;
284
286
  case pathname.endsWith('.js'):
285
287
  content = await response.text();
@@ -347,7 +349,15 @@ async function eachAllLinkInHtml(domain, root, content, result, event) {
347
349
  }
348
350
  }
349
351
  };
350
- await each(fast_html_parser_1.default.parse(content, { style: true, script: true }));
352
+ let html;
353
+ try {
354
+ html = fast_html_parser_1.default.parse(content, { style: true, script: true });
355
+ }
356
+ catch (e) {
357
+ (0, Utils_1.error)('HtmlParser', `HTML [root=${root}] 中存在错误语法`);
358
+ }
359
+ if (html)
360
+ await each(html);
351
361
  }
352
362
  exports.eachAllLinkInHtml = eachAllLinkInHtml;
353
363
  /**
@@ -375,10 +385,10 @@ async function eachAllLinkInCss(domain, root, content, result, event) {
375
385
  case 'declaration':
376
386
  const value = rule.value;
377
387
  const list = value.match(/url\(['"]?([^'")]+)['"]?\)/g)
378
- ?.map(it => it.replace(/(^url\(['"])|(['"]\)$)/g, ''));
388
+ ?.map(it => it.replace(/(^url\(['"]?)|(['"]?\)$)/g, ''));
379
389
  if (list) {
380
390
  for (let url of list) {
381
- if (!/^(https?:)|(\/\/)/) {
391
+ if (!/^(https?:)|(\/\/)/.test(url)) {
382
392
  if (url[0] === '/')
383
393
  url = root + url.substring(1);
384
394
  else
@@ -395,7 +405,15 @@ async function eachAllLinkInCss(domain, root, content, result, event) {
395
405
  }
396
406
  }
397
407
  };
398
- await each(css_1.default.parse(content).stylesheet?.rules);
408
+ let css;
409
+ try {
410
+ css = css_1.default.parse(content).stylesheet?.rules;
411
+ }
412
+ catch (e) {
413
+ (0, Utils_1.error)('CssParser', `CSS [root=${root}] 中存在错误语法`);
414
+ }
415
+ if (css)
416
+ await each(css);
399
417
  }
400
418
  exports.eachAllLinkInCss = eachAllLinkInCss;
401
419
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "0.0.11-alpha",
3
+ "version": "0.0.12-alpha",
4
4
  "main": "dist/index.js",
5
5
  "typings": "types/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",
@@ -35,11 +35,11 @@ export declare function isExclude(domain: string, url: string): boolean;
35
35
  */
36
36
  export declare function isStable(url: string): boolean;
37
37
  /**
38
- * 从指定 URL 加载 cache json
38
+ * 从指定 URL 加载 version json
39
39
  *
40
40
  * + **执行该函数前必须调用过 [loadRules]**
41
41
  */
42
- export declare function loadVersionJson(url: string): Promise<VersionJson>;
42
+ export declare function loadVersionJson(url: string): Promise<VersionJson | null>;
43
43
  /** 提交要存储到 version json 的值 */
44
44
  export declare function submitCacheInfo(key: string, value: any): void;
45
45
  /**
@@ -48,7 +48,7 @@ export declare function submitCacheInfo(key: string, value: any): void;
48
48
  * + **执行该函数前必须调用过 [loadRules]**
49
49
  * + **调用该函数前必须调用过 [loadCacheJson]**
50
50
  */
51
- export declare function readOldVersionJson(): VersionJson;
51
+ export declare function readOldVersionJson(): VersionJson | null;
52
52
  /**
53
53
  * 读取最后一次构建的 VersionJson
54
54
  *