swpp-backends 1.0.2 → 1.0.4

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.
@@ -47,9 +47,7 @@ async function eachAllFile(root, cb) {
47
47
  await cb(root);
48
48
  else {
49
49
  const files = fs_1.default.readdirSync(root);
50
- for (let it of files) {
51
- await eachAllFile(path_1.default.join(root, it), cb);
52
- }
50
+ await Promise.all(files.map(it => eachAllFile(path_1.default.join(root, it), cb)));
53
51
  }
54
52
  }
55
53
  /**
@@ -277,18 +275,15 @@ async function eachAllLinkInUrl(domain, url, result, event) {
277
275
  case pathname.endsWith('/'):
278
276
  content = await response.text();
279
277
  update();
280
- await eachAllLinkInHtml(domain, url.substring(0, url.lastIndexOf('/') + 1), content, result, nextEvent);
281
- break;
278
+ return eachAllLinkInHtml(domain, url.substring(0, url.lastIndexOf('/') + 1), content, result, nextEvent);
282
279
  case pathname.endsWith('.css'):
283
280
  content = await response.text();
284
281
  update();
285
- await eachAllLinkInCss(domain, url.substring(0, url.lastIndexOf('/') + 1), content, result, nextEvent);
286
- break;
282
+ return eachAllLinkInCss(domain, url.substring(0, url.lastIndexOf('/') + 1), content, result, nextEvent);
287
283
  case pathname.endsWith('.js'):
288
284
  content = await response.text();
289
285
  update();
290
- await eachAllLinkInJavaScript(domain, content, result, nextEvent);
291
- break;
286
+ return eachAllLinkInJavaScript(domain, content, result, nextEvent);
292
287
  default:
293
288
  if (stable) {
294
289
  result[url] = [];
@@ -316,7 +311,8 @@ exports.eachAllLinkInUrl = eachAllLinkInUrl;
316
311
  * @param event 检索到 URL 时触发的事件
317
312
  */
318
313
  async function eachAllLinkInHtml(domain, root, content, result, event) {
319
- const each = async (node) => {
314
+ const taskList = [];
315
+ const each = (node) => {
320
316
  let url = undefined;
321
317
  switch (node.tagName) {
322
318
  case 'link':
@@ -336,17 +332,17 @@ async function eachAllLinkInHtml(domain, root, content, result, event) {
336
332
  break;
337
333
  }
338
334
  if (url) {
339
- await eachAllLinkInUrl(domain, url, result, event);
335
+ taskList.push(eachAllLinkInUrl(domain, url, result, event));
340
336
  }
341
337
  else if (node.tagName === 'script') {
342
- await eachAllLinkInJavaScript(domain, node.rawText, result, event);
338
+ taskList.push(eachAllLinkInJavaScript(domain, node.rawText, result, event));
343
339
  }
344
340
  else if (node.tagName === 'style') {
345
- await eachAllLinkInCss(domain, root, node.rawText, result, event);
341
+ taskList.push(eachAllLinkInCss(domain, root, node.rawText, result, event));
346
342
  }
347
343
  if (node.childNodes) {
348
344
  for (let childNode of node.childNodes) {
349
- await each(childNode);
345
+ each(childNode);
350
346
  }
351
347
  }
352
348
  };
@@ -358,7 +354,8 @@ async function eachAllLinkInHtml(domain, root, content, result, event) {
358
354
  (0, Utils_1.error)('HtmlParser', `HTML [root=${root}] 中存在错误语法`);
359
355
  }
360
356
  if (html)
361
- await each(html);
357
+ each(html);
358
+ return Promise.all(taskList);
362
359
  }
363
360
  exports.eachAllLinkInHtml = eachAllLinkInHtml;
364
361
  /**
@@ -376,12 +373,13 @@ exports.eachAllLinkInHtml = eachAllLinkInHtml;
376
373
  * @param event 当检索到一个 URL 后触发的事件
377
374
  */
378
375
  async function eachAllLinkInCss(domain, root, content, result, event) {
379
- const each = async (any) => {
376
+ const taskList = [];
377
+ const each = (any) => {
380
378
  if (!any)
381
379
  return;
382
380
  for (let rule of any) {
383
381
  if (rule.declarations)
384
- await each(rule.declarations);
382
+ each(rule.declarations);
385
383
  switch (rule.type) {
386
384
  case 'declaration':
387
385
  const value = rule.value;
@@ -395,13 +393,13 @@ async function eachAllLinkInCss(domain, root, content, result, event) {
395
393
  else
396
394
  url = root + url;
397
395
  }
398
- await eachAllLinkInUrl(domain, url, result, event);
396
+ taskList.push(eachAllLinkInUrl(domain, url, result, event));
399
397
  }
400
398
  }
401
399
  break;
402
400
  case 'import':
403
401
  const url = rule.import.trim().replace(/^["']|["']$/g, '');
404
- await eachAllLinkInUrl(domain, url, result, event);
402
+ taskList.push(eachAllLinkInUrl(domain, url, result, event));
405
403
  break;
406
404
  }
407
405
  }
@@ -414,7 +412,8 @@ async function eachAllLinkInCss(domain, root, content, result, event) {
414
412
  (0, Utils_1.error)('CssParser', `CSS [root=${root}] 中存在错误语法`);
415
413
  }
416
414
  if (css)
417
- await each(css);
415
+ each(css);
416
+ return Promise.all(taskList);
418
417
  }
419
418
  exports.eachAllLinkInCss = eachAllLinkInCss;
420
419
  /**
@@ -430,7 +429,8 @@ exports.eachAllLinkInCss = eachAllLinkInCss;
430
429
  * @param result 存放结果的对象
431
430
  * @param event 当检索到一个 URL 后触发的事件
432
431
  */
433
- async function eachAllLinkInJavaScript(domain, content, result, event) {
432
+ function eachAllLinkInJavaScript(domain, content, result, event) {
433
+ const taskList = [];
434
434
  const ruleList = (0, SwppRules_1.readRules)().config?.external?.js;
435
435
  if (!ruleList) {
436
436
  (0, Utils_1.error)('LinkItorInJS', '不应发生的异常');
@@ -440,7 +440,7 @@ async function eachAllLinkInJavaScript(domain, content, result, event) {
440
440
  if (typeof value === 'function') {
441
441
  const urls = value(content);
442
442
  for (let url of urls) {
443
- await eachAllLinkInUrl(domain, url, result, event);
443
+ taskList.push(eachAllLinkInUrl(domain, url, result, event));
444
444
  }
445
445
  }
446
446
  else {
@@ -451,11 +451,12 @@ async function eachAllLinkInJavaScript(domain, content, result, event) {
451
451
  ?.map(it => it.replace(/^['"`]|['"`]$/g, ''));
452
452
  if (list) {
453
453
  for (let url of list) {
454
- await eachAllLinkInUrl(domain, url, result, event);
454
+ taskList.push(eachAllLinkInUrl(domain, url, result, event));
455
455
  }
456
456
  }
457
457
  }
458
458
  }
459
+ return Promise.all(taskList);
459
460
  }
460
461
  exports.eachAllLinkInJavaScript = eachAllLinkInJavaScript;
461
462
  /** 判断一个 URL 是否是外部链接 */
@@ -22,7 +22,7 @@ function buildServiceWorker() {
22
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
- 'cacheList', 'modifyRequest', 'getCdnList', 'getSpareUrls', 'blockRequest', 'fetchFile',
25
+ 'cacheRules', 'modifyRequest', 'getRaceUrls', 'getSpareUrls', 'blockRequest', 'fetchFile',
26
26
  ...('external' in rules && Array.isArray(rules.external) ? rules.external : [])
27
27
  ], true) + '\n';
28
28
  if (!fetchFile) {
@@ -39,7 +39,7 @@ function buildServiceWorker() {
39
39
  cache += `(${(0, Utils_1.getSource)(rules['afterJoin'])})()\n`;
40
40
  if ('afterTheme' in rules)
41
41
  cache += `(${(0, Utils_1.getSource)(rules['afterTheme'])})()\n`;
42
- const keyword = "const { cacheList, fetchFile, getSpareUrls } = require('../sw-rules')";
42
+ const keyword = "const { cacheRules, fetchFile, getRaceUrls } = require('../sw-rules')";
43
43
  // noinspection JSUnresolvedVariable
44
44
  let content = fs_1.default.readFileSync(templatePath, 'utf8')
45
45
  .replaceAll("// [insertion site] values", eject?.strValue ?? '')
@@ -83,7 +83,7 @@ const JS_CODE_DEF_FETCH_FILE = `
83
83
  credentials: 'same-origin'
84
84
  })
85
85
  `;
86
- // getCdnList 函数的代码
86
+ // getRaceUrls 函数的代码
87
87
  const JS_CODE_GET_CDN_LIST = `
88
88
  const fetchFile = (request, banCache) => {
89
89
  const fetchArgs = {
@@ -91,7 +91,7 @@ const JS_CODE_GET_CDN_LIST = `
91
91
  mode: 'cors',
92
92
  credentials: 'same-origin'
93
93
  }
94
- const list = getCdnList(request.url)
94
+ const list = getRaceUrls(request.url)
95
95
  if (!list || !Promise.any) return fetch(request, fetchArgs)
96
96
  const res = list.map(url => new Request(url, request))
97
97
  const controllers = []
package/dist/Utils.js CHANGED
@@ -105,7 +105,7 @@ function getSource(obj, typeChecker = undefined, whiteList = undefined, isTop =
105
105
  let str = getSource(value, typeChecker);
106
106
  if (str.length === 0)
107
107
  return '';
108
- if (isTop && whiteList && ['cacheList', 'modifyRequest'].includes(key)) {
108
+ if (isTop && whiteList && ['cacheRules', 'modifyRequest'].includes(key)) {
109
109
  str = str
110
110
  .replace(/\(\s*(.*?)\s*,\s*\$eject\s*\)/g, "$1") // 去掉箭头函数参数表中的 $eject
111
111
  .replaceAll(/\$eject\.(\w+)/g, (_, match) => `eject${match[0].toUpperCase()}${match.substring(1)}`); // 将函数体中的 $eject.xxx 替换为 ejectXxx
@@ -34,7 +34,7 @@
34
34
  self.addEventListener('activate', event => event.waitUntil(clients.claim()))
35
35
 
36
36
  // noinspection JSFileReferences
37
- const { cacheList, fetchFile, getSpareUrls } = require('../sw-rules')
37
+ const { cacheRules, fetchFile, getSpareUrls } = require('../sw-rules')
38
38
 
39
39
  // 检查请求是否成功
40
40
  // noinspection JSUnusedLocalSymbols
@@ -106,8 +106,8 @@
106
106
  /** 判断指定url击中了哪一种缓存,都没有击中则返回null */
107
107
  function findCache(url) {
108
108
  if (url.hostname === 'localhost') return
109
- for (let key in cacheList) {
110
- const value = cacheList[key]
109
+ for (let key in cacheRules) {
110
+ const value = cacheRules[key]
111
111
  if (value.match(url)) return value
112
112
  }
113
113
  }
@@ -219,10 +219,6 @@
219
219
  * @constructor
220
220
  */
221
221
  function CacheChangeExpression(json) {
222
- const checkCache = url => {
223
- const cache = findCache(new URL(url))
224
- return !cache || cache.clean
225
- }
226
222
  /**
227
223
  * 遍历所有value
228
224
  * @param action {function(string): boolean} 接受value并返回bool的函数
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "main": "dist/index.js",
5
5
  "typings": "types/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",
@@ -93,7 +93,7 @@ export declare function buildVersionJson(protocol: ('https://' | 'http://'), dom
93
93
  * @param result 存放结果的对象
94
94
  * @param event 检索到一个 URL 时触发的事件
95
95
  */
96
- export declare function eachAllLinkInUrl(domain: string, url: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
96
+ export declare function eachAllLinkInUrl(domain: string, url: string, result: VersionMap, event?: (url: string) => void): Promise<void | any[]>;
97
97
  /**
98
98
  * 检索 HTML 文件中的所有外部链接
99
99
  *
@@ -108,7 +108,7 @@ export declare function eachAllLinkInUrl(domain: string, url: string, result: Ve
108
108
  * @param result 存放结果的对象
109
109
  * @param event 检索到 URL 时触发的事件
110
110
  */
111
- export declare function eachAllLinkInHtml(domain: string, root: 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<any[]>;
112
112
  /**
113
113
  * 检索 CSS 文件中的所有外部链
114
114
  *
@@ -123,7 +123,7 @@ export declare function eachAllLinkInHtml(domain: string, root: string, content:
123
123
  * @param result 存放结果的对象
124
124
  * @param event 当检索到一个 URL 后触发的事件
125
125
  */
126
- export declare function eachAllLinkInCss(domain: string, root: 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[]>;
127
127
  /**
128
128
  * 遍历 JS 文件中地所有外部链接
129
129
  *
@@ -137,7 +137,7 @@ export declare function eachAllLinkInCss(domain: string, root: string, content:
137
137
  * @param result 存放结果的对象
138
138
  * @param event 当检索到一个 URL 后触发的事件
139
139
  */
140
- export declare function eachAllLinkInJavaScript(domain: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
140
+ export declare function eachAllLinkInJavaScript(domain: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<any[]>;
141
141
  /**
142
142
  * 查询指定 URL 对应的缓存规则
143
143
  *
@@ -42,7 +42,7 @@ export interface VersionJsonConfig {
42
42
  charLimit: number;
43
43
  /** 是否合并指定项目 */
44
44
  merge: string[];
45
- /** 生成 cacheList.json 时忽略的文件 */
45
+ /** 生成版本信息时忽略的文件 */
46
46
  exclude: {
47
47
  localhost: RegExp[];
48
48
  other: RegExp[];
@@ -100,7 +100,7 @@ export interface SwppConfigTemplate {
100
100
  charLimit?: number;
101
101
  /** 是否合并指定项目 */
102
102
  merge?: string[];
103
- /** 生成 cacheList.json 时忽略的文件 */
103
+ /** 生成版本信息时忽略的文件 */
104
104
  exclude?: {
105
105
  /** 当前网站的 URL */
106
106
  localhost?: RegExp[];
File without changes
File without changes