swpp-backends 0.0.7-alpha → 0.0.9-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.
@@ -41,13 +41,15 @@ const Utils_1 = require("./Utils");
41
41
  * @param root 根目录
42
42
  * @param cb 回调函数(接收的参数是文件的相对路径)
43
43
  */
44
- function eachAllFile(root, cb) {
44
+ async function eachAllFile(root, cb) {
45
45
  const stats = fs_1.default.statSync(root);
46
46
  if (stats.isFile())
47
- cb(root);
47
+ await cb(root);
48
48
  else {
49
49
  const files = fs_1.default.readdirSync(root);
50
- files.forEach(it => eachAllFile(path_1.default.join(root, it), cb));
50
+ for (let it of files) {
51
+ await eachAllFile(path_1.default.join(root, it), cb);
52
+ }
51
53
  }
52
54
  }
53
55
  /**
@@ -165,7 +167,7 @@ exports.readMergeVersionMap = readMergeVersionMap;
165
167
  */
166
168
  async function buildVersionJson(protocol, domain, root) {
167
169
  const list = {};
168
- eachAllFile(root, async (path) => {
170
+ await eachAllFile(root, async (path) => {
169
171
  const endIndex = path.length - (path.endsWith('/index.html') ? 10 : 0);
170
172
  const url = new URL(protocol + path_1.default.join(domain, path.substring(root.length, endIndex)));
171
173
  const pathname = url.pathname;
@@ -237,9 +239,6 @@ async function eachAllLinkInUrl(domain, url, result, event) {
237
239
  copyTree(url);
238
240
  }
239
241
  }
240
- else {
241
- event?.(value);
242
- }
243
242
  };
244
243
  copyTree(url);
245
244
  return;
@@ -256,23 +255,31 @@ async function eachAllLinkInUrl(domain, url, result, event) {
256
255
  }
257
256
  const pathname = new URL(url).pathname;
258
257
  let content;
259
- const relay = [];
260
258
  const nextEvent = (it) => {
261
- relay.push(it);
262
- event?.(it);
259
+ if (stable)
260
+ result[url].push(it);
261
+ };
262
+ const update = () => {
263
+ if (stable)
264
+ result[url] = [];
265
+ else
266
+ result[url] = crypto.createHash('md5').update(content).digest('hex');
263
267
  };
264
268
  switch (true) {
265
269
  case pathname.endsWith('.html'):
266
270
  case pathname.endsWith('/'):
267
271
  content = await response.text();
272
+ update();
268
273
  await eachAllLinkInHtml(domain, content, result, nextEvent);
269
274
  break;
270
275
  case pathname.endsWith('.css'):
271
276
  content = await response.text();
277
+ update();
272
278
  await eachAllLinkInCss(domain, content, result, nextEvent);
273
279
  break;
274
280
  case pathname.endsWith('.js'):
275
281
  content = await response.text();
282
+ update();
276
283
  await eachAllLinkInJavaScript(domain, content, result, nextEvent);
277
284
  break;
278
285
  default:
@@ -285,14 +292,6 @@ async function eachAllLinkInUrl(domain, url, result, event) {
285
292
  }
286
293
  break;
287
294
  }
288
- if (content) {
289
- if (stable) {
290
- result[url] = relay;
291
- }
292
- else {
293
- result[url] = crypto.createHash('md5').update(content).digest('hex');
294
- }
295
- }
296
295
  }
297
296
  exports.eachAllLinkInUrl = eachAllLinkInUrl;
298
297
  /**
@@ -337,8 +336,10 @@ async function eachAllLinkInHtml(domain, content, result, event) {
337
336
  else if (node.tagName === 'style') {
338
337
  await eachAllLinkInCss(domain, node.rawText, result, event);
339
338
  }
340
- for (let childNode of node.childNodes) {
341
- await each(childNode);
339
+ if (node.childNodes) {
340
+ for (let childNode of node.childNodes) {
341
+ await each(childNode);
342
+ }
342
343
  }
343
344
  };
344
345
  await each(fast_html_parser_1.default.parse(content, { style: true, script: true }));
@@ -429,7 +430,9 @@ async function eachAllLinkInJavaScript(domain, content, result, event) {
429
430
  exports.eachAllLinkInJavaScript = eachAllLinkInJavaScript;
430
431
  /** 判断一个 URL 是否是外部链接 */
431
432
  function isExternalLink(domain, url) {
432
- return new RegExp(`^(https?:)?\\/\\/${domain}`).test(url);
433
+ if (url[0] === '/' && url[1] !== '/')
434
+ return false;
435
+ return !new RegExp(`^(https?:)?\\/\\/${domain}`).test(url);
433
436
  }
434
437
  /**
435
438
  * 查询指定 URL 对应的缓存规则
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "0.0.7-alpha",
3
+ "version": "0.0.9-alpha",
4
4
  "main": "dist/index.js",
5
5
  "typings": "types/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",