swpp-backends 0.0.9-alpha → 0.0.11-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.
@@ -101,10 +101,10 @@ exports.loadVersionJson = loadVersionJson;
101
101
  let _oldVersionJson;
102
102
  let _newVersionJson;
103
103
  let _mergeVersionMap;
104
- const event = [];
104
+ const event = new Map();
105
105
  /** 提交要存储到 version json 的值 */
106
- function submitCacheInfo(value) {
107
- event.push(value);
106
+ function submitCacheInfo(key, value) {
107
+ event.set(key, value);
108
108
  }
109
109
  exports.submitCacheInfo = submitCacheInfo;
110
110
  /**
@@ -168,7 +168,7 @@ exports.readMergeVersionMap = readMergeVersionMap;
168
168
  async function buildVersionJson(protocol, domain, root) {
169
169
  const list = {};
170
170
  await eachAllFile(root, async (path) => {
171
- const endIndex = path.length - (path.endsWith('/index.html') ? 10 : 0);
171
+ const endIndex = path.length - (/[\/\\]index\.html$/.test(path) ? 10 : 0);
172
172
  const url = new URL(protocol + path_1.default.join(domain, path.substring(root.length, endIndex)));
173
173
  const pathname = url.pathname;
174
174
  if (isExclude(domain, pathname))
@@ -182,12 +182,12 @@ async function buildVersionJson(protocol, domain, root) {
182
182
  if (pathname.endsWith('/') || pathname.endsWith('.html')) {
183
183
  if (!content)
184
184
  content = fs_1.default.readFileSync(path, 'utf-8');
185
- await eachAllLinkInHtml(domain, content, list);
185
+ await eachAllLinkInHtml(domain, protocol + domain, content, list);
186
186
  }
187
187
  else if (pathname.endsWith('.css')) {
188
188
  if (!content)
189
189
  content = fs_1.default.readFileSync(path, 'utf-8');
190
- await eachAllLinkInCss(domain, content, list);
190
+ await eachAllLinkInCss(domain, protocol + domain, content, list);
191
191
  }
192
192
  else if (pathname.endsWith('.js')) {
193
193
  if (!content)
@@ -195,8 +195,12 @@ async function buildVersionJson(protocol, domain, root) {
195
195
  await eachAllLinkInJavaScript(domain, content, list);
196
196
  }
197
197
  });
198
+ const external = {};
199
+ event.forEach((value, key) => {
200
+ external[key] = value;
201
+ });
198
202
  return _newVersionJson = {
199
- version: 3, list, external: event
203
+ version: 3, list, external
200
204
  };
201
205
  }
202
206
  exports.buildVersionJson = buildVersionJson;
@@ -227,7 +231,7 @@ async function eachAllLinkInUrl(domain, url, result, event) {
227
231
  const stable = isStable(url);
228
232
  if (stable) {
229
233
  const old = readOldVersionJson().list;
230
- if (url in old) {
234
+ if (Array.isArray(old[url])) {
231
235
  const copyTree = (key) => {
232
236
  const value = old[key];
233
237
  if (!value)
@@ -270,12 +274,12 @@ async function eachAllLinkInUrl(domain, url, result, event) {
270
274
  case pathname.endsWith('/'):
271
275
  content = await response.text();
272
276
  update();
273
- await eachAllLinkInHtml(domain, content, result, nextEvent);
277
+ await eachAllLinkInHtml(domain, content, url.substring(0, url.lastIndexOf('/') + 1), result, nextEvent);
274
278
  break;
275
279
  case pathname.endsWith('.css'):
276
280
  content = await response.text();
277
281
  update();
278
- await eachAllLinkInCss(domain, content, result, nextEvent);
282
+ await eachAllLinkInCss(domain, content, url.substring(0, url.lastIndexOf('/') + 1), result, nextEvent);
279
283
  break;
280
284
  case pathname.endsWith('.js'):
281
285
  content = await response.text();
@@ -303,11 +307,12 @@ exports.eachAllLinkInUrl = eachAllLinkInUrl;
303
307
  * + **调用该函数前必须调用过 [loadCacheJson]**
304
308
  *
305
309
  * @param domain 网站域名
310
+ * @param root 当前资源的根
306
311
  * @param content HTML 文件内容
307
312
  * @param result 存放结果的对象
308
313
  * @param event 检索到 URL 时触发的事件
309
314
  */
310
- async function eachAllLinkInHtml(domain, content, result, event) {
315
+ async function eachAllLinkInHtml(domain, root, content, result, event) {
311
316
  const each = async (node) => {
312
317
  let url = undefined;
313
318
  switch (node.tagName) {
@@ -334,7 +339,7 @@ async function eachAllLinkInHtml(domain, content, result, event) {
334
339
  await eachAllLinkInJavaScript(domain, node.rawText, result, event);
335
340
  }
336
341
  else if (node.tagName === 'style') {
337
- await eachAllLinkInCss(domain, node.rawText, result, event);
342
+ await eachAllLinkInCss(domain, root, node.rawText, result, event);
338
343
  }
339
344
  if (node.childNodes) {
340
345
  for (let childNode of node.childNodes) {
@@ -354,25 +359,31 @@ exports.eachAllLinkInHtml = eachAllLinkInHtml;
354
359
  * + **调用该函数前必须调用过 [loadCacheJson]**
355
360
  *
356
361
  * @param domain 网站域名
362
+ * @param root 当前资源的 URL 的根
357
363
  * @param content CSS 文件内容
358
364
  * @param result 存放结果的对象
359
365
  * @param event 当检索到一个 URL 后触发的事件
360
366
  */
361
- async function eachAllLinkInCss(domain, content, result, event) {
367
+ async function eachAllLinkInCss(domain, root, content, result, event) {
362
368
  const each = async (any) => {
363
369
  if (!any)
364
370
  return;
365
371
  for (let rule of any) {
372
+ if (rule.declarations)
373
+ await each(rule.declarations);
366
374
  switch (rule.type) {
367
- case 'rule':
368
- await each(rule.declarations);
369
- break;
370
375
  case 'declaration':
371
376
  const value = rule.value;
372
377
  const list = value.match(/url\(['"]?([^'")]+)['"]?\)/g)
373
378
  ?.map(it => it.replace(/(^url\(['"])|(['"]\)$)/g, ''));
374
379
  if (list) {
375
380
  for (let url of list) {
381
+ if (!/^(https?:)|(\/\/)/) {
382
+ if (url[0] === '/')
383
+ url = root + url.substring(1);
384
+ else
385
+ url = root + url;
386
+ }
376
387
  await eachAllLinkInUrl(domain, url, result, event);
377
388
  }
378
389
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "0.0.9-alpha",
3
+ "version": "0.0.11-alpha",
4
4
  "main": "dist/index.js",
5
5
  "typings": "types/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",
@@ -41,7 +41,7 @@ export declare function isStable(url: string): boolean;
41
41
  */
42
42
  export declare function loadVersionJson(url: string): Promise<VersionJson>;
43
43
  /** 提交要存储到 version json 的值 */
44
- export declare function submitCacheInfo(value: any): void;
44
+ export declare function submitCacheInfo(key: string, value: any): void;
45
45
  /**
46
46
  * 读取最后一次加载的 version json
47
47
  *
@@ -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
  *