swpp-backends 0.0.10-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
  }
@@ -182,12 +188,12 @@ async function buildVersionJson(protocol, domain, root) {
182
188
  if (pathname.endsWith('/') || pathname.endsWith('.html')) {
183
189
  if (!content)
184
190
  content = fs_1.default.readFileSync(path, 'utf-8');
185
- await eachAllLinkInHtml(domain, content, list);
191
+ await eachAllLinkInHtml(domain, protocol + domain, content, list);
186
192
  }
187
193
  else if (pathname.endsWith('.css')) {
188
194
  if (!content)
189
195
  content = fs_1.default.readFileSync(path, 'utf-8');
190
- await eachAllLinkInCss(domain, content, list);
196
+ await eachAllLinkInCss(domain, protocol + domain, content, list);
191
197
  }
192
198
  else if (pathname.endsWith('.js')) {
193
199
  if (!content)
@@ -195,8 +201,12 @@ async function buildVersionJson(protocol, domain, root) {
195
201
  await eachAllLinkInJavaScript(domain, content, list);
196
202
  }
197
203
  });
204
+ const external = {};
205
+ event.forEach((value, key) => {
206
+ external[key] = value;
207
+ });
198
208
  return _newVersionJson = {
199
- version: 3, list, external: event
209
+ version: 3, list, external
200
210
  };
201
211
  }
202
212
  exports.buildVersionJson = buildVersionJson;
@@ -226,8 +236,8 @@ async function eachAllLinkInUrl(domain, url, result, event) {
226
236
  event?.(url);
227
237
  const stable = isStable(url);
228
238
  if (stable) {
229
- const old = readOldVersionJson().list;
230
- if (url in old) {
239
+ const old = readOldVersionJson()?.list;
240
+ if (Array.isArray(old?.[url])) {
231
241
  const copyTree = (key) => {
232
242
  const value = old[key];
233
243
  if (!value)
@@ -244,14 +254,10 @@ async function eachAllLinkInUrl(domain, url, result, event) {
244
254
  return;
245
255
  }
246
256
  }
247
- const response = await (0, Utils_1.fetchFile)(url).catch(err => {
248
- (0, Utils_1.error)('LinkItorInUrl', `拉取文件 [${url}] 时发生异常:${err}`);
249
- });
250
- if (!response)
251
- throw '拉取时异常';
252
- if (![200, 301, 302, 307, 308].includes(response.status)) {
253
- (0, Utils_1.error)('LinkItorInUrl', `拉取文件 [${url}] 时出现错误:${response.status}`);
254
- 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;
255
261
  }
256
262
  const pathname = new URL(url).pathname;
257
263
  let content;
@@ -270,12 +276,12 @@ async function eachAllLinkInUrl(domain, url, result, event) {
270
276
  case pathname.endsWith('/'):
271
277
  content = await response.text();
272
278
  update();
273
- await eachAllLinkInHtml(domain, content, result, nextEvent);
279
+ await eachAllLinkInHtml(domain, url.substring(0, url.lastIndexOf('/') + 1), content, result, nextEvent);
274
280
  break;
275
281
  case pathname.endsWith('.css'):
276
282
  content = await response.text();
277
283
  update();
278
- await eachAllLinkInCss(domain, content, result, nextEvent);
284
+ await eachAllLinkInCss(domain, url.substring(0, url.lastIndexOf('/') + 1), content, result, nextEvent);
279
285
  break;
280
286
  case pathname.endsWith('.js'):
281
287
  content = await response.text();
@@ -303,11 +309,12 @@ exports.eachAllLinkInUrl = eachAllLinkInUrl;
303
309
  * + **调用该函数前必须调用过 [loadCacheJson]**
304
310
  *
305
311
  * @param domain 网站域名
312
+ * @param root 当前资源的根
306
313
  * @param content HTML 文件内容
307
314
  * @param result 存放结果的对象
308
315
  * @param event 检索到 URL 时触发的事件
309
316
  */
310
- async function eachAllLinkInHtml(domain, content, result, event) {
317
+ async function eachAllLinkInHtml(domain, root, content, result, event) {
311
318
  const each = async (node) => {
312
319
  let url = undefined;
313
320
  switch (node.tagName) {
@@ -334,7 +341,7 @@ async function eachAllLinkInHtml(domain, content, result, event) {
334
341
  await eachAllLinkInJavaScript(domain, node.rawText, result, event);
335
342
  }
336
343
  else if (node.tagName === 'style') {
337
- await eachAllLinkInCss(domain, node.rawText, result, event);
344
+ await eachAllLinkInCss(domain, root, node.rawText, result, event);
338
345
  }
339
346
  if (node.childNodes) {
340
347
  for (let childNode of node.childNodes) {
@@ -342,7 +349,15 @@ async function eachAllLinkInHtml(domain, content, result, event) {
342
349
  }
343
350
  }
344
351
  };
345
- 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);
346
361
  }
347
362
  exports.eachAllLinkInHtml = eachAllLinkInHtml;
348
363
  /**
@@ -354,11 +369,12 @@ exports.eachAllLinkInHtml = eachAllLinkInHtml;
354
369
  * + **调用该函数前必须调用过 [loadCacheJson]**
355
370
  *
356
371
  * @param domain 网站域名
372
+ * @param root 当前资源的 URL 的根
357
373
  * @param content CSS 文件内容
358
374
  * @param result 存放结果的对象
359
375
  * @param event 当检索到一个 URL 后触发的事件
360
376
  */
361
- async function eachAllLinkInCss(domain, content, result, event) {
377
+ async function eachAllLinkInCss(domain, root, content, result, event) {
362
378
  const each = async (any) => {
363
379
  if (!any)
364
380
  return;
@@ -369,9 +385,15 @@ async function eachAllLinkInCss(domain, content, result, event) {
369
385
  case 'declaration':
370
386
  const value = rule.value;
371
387
  const list = value.match(/url\(['"]?([^'")]+)['"]?\)/g)
372
- ?.map(it => it.replace(/(^url\(['"])|(['"]\)$)/g, ''));
388
+ ?.map(it => it.replace(/(^url\(['"]?)|(['"]?\)$)/g, ''));
373
389
  if (list) {
374
390
  for (let url of list) {
391
+ if (!/^(https?:)|(\/\/)/.test(url)) {
392
+ if (url[0] === '/')
393
+ url = root + url.substring(1);
394
+ else
395
+ url = root + url;
396
+ }
375
397
  await eachAllLinkInUrl(domain, url, result, event);
376
398
  }
377
399
  }
@@ -383,7 +405,15 @@ async function eachAllLinkInCss(domain, content, result, event) {
383
405
  }
384
406
  }
385
407
  };
386
- 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);
387
417
  }
388
418
  exports.eachAllLinkInCss = eachAllLinkInCss;
389
419
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "0.0.10-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
  *
@@ -103,11 +103,12 @@ export declare function eachAllLinkInUrl(domain: string, url: string, result: Ve
103
103
  * + **调用该函数前必须调用过 [loadCacheJson]**
104
104
  *
105
105
  * @param domain 网站域名
106
+ * @param root 当前资源的根
106
107
  * @param content HTML 文件内容
107
108
  * @param result 存放结果的对象
108
109
  * @param event 检索到 URL 时触发的事件
109
110
  */
110
- export declare function eachAllLinkInHtml(domain: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
111
+ export declare function eachAllLinkInHtml(domain: string, root: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
111
112
  /**
112
113
  * 检索 CSS 文件中的所有外部链
113
114
  *
@@ -117,11 +118,12 @@ export declare function eachAllLinkInHtml(domain: string, content: string, resul
117
118
  * + **调用该函数前必须调用过 [loadCacheJson]**
118
119
  *
119
120
  * @param domain 网站域名
121
+ * @param root 当前资源的 URL 的根
120
122
  * @param content CSS 文件内容
121
123
  * @param result 存放结果的对象
122
124
  * @param event 当检索到一个 URL 后触发的事件
123
125
  */
124
- export declare function eachAllLinkInCss(domain: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
126
+ export declare function eachAllLinkInCss(domain: string, root: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
125
127
  /**
126
128
  * 遍历 JS 文件中地所有外部链接
127
129
  *