swpp-backends 0.0.1-alpha.1 → 0.0.2-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.
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.replaceRequest = exports.findCache = exports.eachAllLinkInJavaScript = exports.eachAllLinkInCss = exports.eachAllLinkInHtml = exports.eachAllLinkInUrl = exports.buildVersionJson = exports.readMergeVersionMap = exports.readNewVersionJson = exports.readOldVersionJson = exports.loadVersionJson = exports.isStable = exports.isExclude = void 0;
29
+ exports.replaceRequest = exports.findCache = exports.eachAllLinkInJavaScript = exports.eachAllLinkInCss = exports.eachAllLinkInHtml = exports.eachAllLinkInUrl = exports.buildVersionJson = exports.readMergeVersionMap = exports.readNewVersionJson = exports.readOldVersionJson = exports.submitCacheInfo = exports.loadVersionJson = exports.isStable = exports.isExclude = void 0;
30
30
  const fs_1 = __importDefault(require("fs"));
31
31
  const path_1 = __importDefault(require("path"));
32
32
  const node_fetch_1 = require("node-fetch");
@@ -55,14 +55,14 @@ function eachAllFile(root, cb) {
55
55
  *
56
56
  * + **执行该函数前必须调用过 [loadRules]**
57
57
  *
58
- * @param webRoot 网站域名
58
+ * @param domain 网站域名
59
59
  * @param url 要判断的 URL
60
60
  */
61
- function isExclude(webRoot, url) {
61
+ function isExclude(domain, url) {
62
62
  const exclude = (0, SwppRules_1.readRules)().config?.json?.exclude;
63
63
  if (!exclude)
64
64
  throw 'exclude 为空';
65
- const list = isExternalLink(webRoot, url) ? exclude.other : exclude.localhost;
65
+ const list = isExternalLink(domain, url) ? exclude.other : exclude.localhost;
66
66
  for (let reg of list) {
67
67
  if (url.match(reg))
68
68
  return true;
@@ -99,6 +99,12 @@ exports.loadVersionJson = loadVersionJson;
99
99
  let _oldVersionJson;
100
100
  let _newVersionJson;
101
101
  let _mergeVersionMap;
102
+ const event = [];
103
+ /** 提交要存储到 version json 的值 */
104
+ function submitCacheInfo(value) {
105
+ event.push(value);
106
+ }
107
+ exports.submitCacheInfo = submitCacheInfo;
102
108
  /**
103
109
  * 读取最后一次加载的 version json
104
110
  *
@@ -143,23 +149,23 @@ function readMergeVersionMap() {
143
149
  }
144
150
  exports.readMergeVersionMap = readMergeVersionMap;
145
151
  /**
146
- * 构建一个 cache json
152
+ * 构建一个 version json
147
153
  *
148
154
  * + **执行该函数前必须调用过 [loadRules]**
149
155
  * + **调用该函数前必须调用过 [loadCacheJson]**
150
156
  * + **执行该函数前必须调用过 [calcEjectValues]**
151
157
  *
152
158
  * @param protocol 网站的网络协议
153
- * @param webRoot 网站域名(包括二级域名)
159
+ * @param domain 网站域名(包括二级域名)
154
160
  * @param root 网页根目录(首页 index.html 所在目录)
155
161
  */
156
- async function buildVersionJson(protocol, webRoot, root) {
162
+ async function buildVersionJson(protocol, domain, root) {
157
163
  const list = {};
158
164
  eachAllFile(root, async (path) => {
159
165
  const endIndex = path.length - (path.endsWith('/index.html') ? 10 : 0);
160
- const url = new URL(protocol + path_1.default.join(webRoot, path.substring(root.length, endIndex)));
166
+ const url = new URL(protocol + path_1.default.join(domain, path.substring(root.length, endIndex)));
161
167
  const pathname = url.pathname;
162
- if (isExclude(webRoot, pathname))
168
+ if (isExclude(domain, pathname))
163
169
  return;
164
170
  let content = null;
165
171
  if (findCache(url)) {
@@ -170,17 +176,17 @@ async function buildVersionJson(protocol, webRoot, root) {
170
176
  if (pathname.endsWith('/') || pathname.endsWith('.html')) {
171
177
  if (!content)
172
178
  content = fs_1.default.readFileSync(path, 'utf-8');
173
- await eachAllLinkInHtml(webRoot, content, list);
179
+ await eachAllLinkInHtml(domain, content, list);
174
180
  }
175
181
  else if (pathname.endsWith('.css')) {
176
182
  if (!content)
177
183
  content = fs_1.default.readFileSync(path, 'utf-8');
178
- await eachAllLinkInCss(webRoot, content, list);
184
+ await eachAllLinkInCss(domain, content, list);
179
185
  }
180
186
  else if (pathname.endsWith('.js')) {
181
187
  if (!content)
182
188
  content = fs_1.default.readFileSync(path, 'utf-8');
183
- await eachAllLinkInJavaScript(webRoot, content, list);
189
+ await eachAllLinkInJavaScript(domain, content, list);
184
190
  }
185
191
  });
186
192
  return _newVersionJson = {
@@ -197,19 +203,19 @@ exports.buildVersionJson = buildVersionJson;
197
203
  * + **调用该函数前必须调用过 [loadCacheJson]**
198
204
  * + **执行该函数前必须调用过 [calcEjectValues]**
199
205
  *
200
- * @param webRoot 网站域名
206
+ * @param domain 网站域名
201
207
  * @param url 要检索的 URL
202
208
  * @param result 存放结果的对象
203
209
  * @param event 检索到一个 URL 时触发的事件
204
210
  */
205
- async function eachAllLinkInUrl(webRoot, url, result, event) {
211
+ async function eachAllLinkInUrl(domain, url, result, event) {
206
212
  if (url.startsWith('//'))
207
213
  url = 'http' + url;
208
214
  if (url in result)
209
215
  return event?.(url);
210
- if (!url.startsWith('http') || isExclude(webRoot, url))
216
+ if (!url.startsWith('http') || isExclude(domain, url))
211
217
  return;
212
- if (!(isExternalLink(webRoot, url) && findCache(new URL(url))))
218
+ if (!(isExternalLink(domain, url) && findCache(new URL(url))))
213
219
  return;
214
220
  event?.(url);
215
221
  const stable = isStable(url);
@@ -249,15 +255,15 @@ async function eachAllLinkInUrl(webRoot, url, result, event) {
249
255
  case pathname.endsWith('.html'):
250
256
  case pathname.endsWith('/'):
251
257
  content = await response.text();
252
- await eachAllLinkInHtml(webRoot, content, result, nextEvent);
258
+ await eachAllLinkInHtml(domain, content, result, nextEvent);
253
259
  break;
254
260
  case pathname.endsWith('.css'):
255
261
  content = await response.text();
256
- await eachAllLinkInCss(webRoot, content, result, nextEvent);
262
+ await eachAllLinkInCss(domain, content, result, nextEvent);
257
263
  break;
258
264
  case pathname.endsWith('.js'):
259
265
  content = await response.text();
260
- await eachAllLinkInJavaScript(webRoot, content, result, nextEvent);
266
+ await eachAllLinkInJavaScript(domain, content, result, nextEvent);
261
267
  break;
262
268
  default:
263
269
  if (stable) {
@@ -287,12 +293,12 @@ exports.eachAllLinkInUrl = eachAllLinkInUrl;
287
293
  * + **执行该函数前必须调用过 [loadRules]**
288
294
  * + **调用该函数前必须调用过 [loadCacheJson]**
289
295
  *
290
- * @param webRoot 网站域名
296
+ * @param domain 网站域名
291
297
  * @param content HTML 文件内容
292
298
  * @param result 存放结果的对象
293
299
  * @param event 检索到 URL 时触发的事件
294
300
  */
295
- async function eachAllLinkInHtml(webRoot, content, result, event) {
301
+ async function eachAllLinkInHtml(domain, content, result, event) {
296
302
  const each = async (node) => {
297
303
  let url = undefined;
298
304
  switch (node.tagName) {
@@ -311,13 +317,13 @@ async function eachAllLinkInHtml(webRoot, content, result, event) {
311
317
  break;
312
318
  }
313
319
  if (url) {
314
- await eachAllLinkInUrl(webRoot, url, result, event);
320
+ await eachAllLinkInUrl(domain, url, result, event);
315
321
  }
316
322
  else if (node.tagName === 'script') {
317
- await eachAllLinkInJavaScript(webRoot, node.rawText, result, event);
323
+ await eachAllLinkInJavaScript(domain, node.rawText, result, event);
318
324
  }
319
325
  else if (node.tagName === 'style') {
320
- await eachAllLinkInCss(webRoot, node.rawText, result, event);
326
+ await eachAllLinkInCss(domain, node.rawText, result, event);
321
327
  }
322
328
  for (let childNode of node.childNodes) {
323
329
  await each(childNode);
@@ -334,12 +340,12 @@ exports.eachAllLinkInHtml = eachAllLinkInHtml;
334
340
  * + **执行该函数前必须调用过 [loadRules]**
335
341
  * + **调用该函数前必须调用过 [loadCacheJson]**
336
342
  *
337
- * @param webRoot 网站域名
343
+ * @param domain 网站域名
338
344
  * @param content CSS 文件内容
339
345
  * @param result 存放结果的对象
340
346
  * @param event 当检索到一个 URL 后触发的事件
341
347
  */
342
- async function eachAllLinkInCss(webRoot, content, result, event) {
348
+ async function eachAllLinkInCss(domain, content, result, event) {
343
349
  const each = async (any) => {
344
350
  if (!any)
345
351
  return;
@@ -354,13 +360,13 @@ async function eachAllLinkInCss(webRoot, content, result, event) {
354
360
  ?.map(it => it.replace(/(^url\(['"])|(['"]\)$)/g, ''));
355
361
  if (list) {
356
362
  for (let url of list) {
357
- await eachAllLinkInUrl(webRoot, url, result, event);
363
+ await eachAllLinkInUrl(domain, url, result, event);
358
364
  }
359
365
  }
360
366
  break;
361
367
  case 'import':
362
368
  const url = rule.import.trim().replace(/^["']|["']$/g, '');
363
- await eachAllLinkInUrl(webRoot, url, result, event);
369
+ await eachAllLinkInUrl(domain, url, result, event);
364
370
  break;
365
371
  }
366
372
  }
@@ -376,12 +382,12 @@ exports.eachAllLinkInCss = eachAllLinkInCss;
376
382
  * + **执行该函数前必须调用过 [loadRules]**
377
383
  * + **调用该函数前必须调用过 [loadCacheJson]**
378
384
  *
379
- * @param webRoot 网站域名
385
+ * @param domain 网站域名
380
386
  * @param content JS 文件内容
381
387
  * @param result 存放结果的对象
382
388
  * @param event 当检索到一个 URL 后触发的事件
383
389
  */
384
- async function eachAllLinkInJavaScript(webRoot, content, result, event) {
390
+ async function eachAllLinkInJavaScript(domain, content, result, event) {
385
391
  const ruleList = (0, SwppRules_1.readRules)().config?.external?.js;
386
392
  if (!ruleList)
387
393
  throw 'ruleList 为空';
@@ -389,7 +395,7 @@ async function eachAllLinkInJavaScript(webRoot, content, result, event) {
389
395
  if (typeof value === 'function') {
390
396
  const urls = value(content);
391
397
  for (let url of urls) {
392
- await eachAllLinkInUrl(webRoot, url, result, event);
398
+ await eachAllLinkInUrl(domain, url, result, event);
393
399
  }
394
400
  }
395
401
  else {
@@ -400,7 +406,7 @@ async function eachAllLinkInJavaScript(webRoot, content, result, event) {
400
406
  ?.map(it => it.replace(/^['"`]|['"`]$/g, ''));
401
407
  if (list) {
402
408
  for (let url of list) {
403
- await eachAllLinkInUrl(webRoot, url, result, event);
409
+ await eachAllLinkInUrl(domain, url, result, event);
404
410
  }
405
411
  }
406
412
  }
@@ -408,8 +414,8 @@ async function eachAllLinkInJavaScript(webRoot, content, result, event) {
408
414
  }
409
415
  exports.eachAllLinkInJavaScript = eachAllLinkInJavaScript;
410
416
  /** 判断一个 URL 是否是外部链接 */
411
- function isExternalLink(webRoot, url) {
412
- return new RegExp(`^(https?:)?\\/\\/${webRoot}`).test(url);
417
+ function isExternalLink(domain, url) {
418
+ return new RegExp(`^(https?:)?\\/\\/${domain}`).test(url);
413
419
  }
414
420
  /**
415
421
  * 查询指定 URL 对应的缓存规则
package/dist/index.js CHANGED
@@ -24,7 +24,7 @@ exports.default = {
24
24
  loadRules: SwppRules_1.loadRules, loadUpdateJson: UpdateJsonBuilder_1.loadUpdateJson, loadVersionJson: FileAnalyzer_1.loadVersionJson
25
25
  },
26
26
  event: {
27
- addRulesMapEvent: SwppRules_1.addRulesMapEvent, refreshUrl: VersionAnalyzer_1.refreshUrl, submitChange: UpdateJsonBuilder_1.submitChange
27
+ addRulesMapEvent: SwppRules_1.addRulesMapEvent, refreshUrl: VersionAnalyzer_1.refreshUrl, submitChange: UpdateJsonBuilder_1.submitChange, submitCacheInfo: FileAnalyzer_1.submitCacheInfo
28
28
  },
29
29
  utils: {
30
30
  getSource: Utils_1.getSource, getShorthand: UpdateJsonBuilder_1.getShorthand, findCache: FileAnalyzer_1.findCache,
package/package.json CHANGED
@@ -1,34 +1,37 @@
1
- {
2
- "name": "swpp-backends",
3
- "version": "0.0.1-alpha.1",
4
- "main": "dist/index.js",
5
- "typings": "types/index.d.ts",
6
- "scripts": {
7
- "prepublishOnly": "tsc && gulp copy-resources"
8
- },
9
- "description": "Generate a powerful ServiceWorker for your website.",
10
- "author": "kmar",
11
- "license": "AGPL-3.0",
12
- "files": [
13
- "dist", "types"
14
- ],
15
- "devDependencies": {
16
- "@types/css": "^0.0.33",
17
- "@types/fast-html-parser": "^1.0.2",
18
- "@types/node": "^20.4.5",
19
- "gulp": "^4.0.2",
20
- "typescript": "^5.1.6"
21
- },
22
- "dependencies": {
23
- "css": "^3.0.0",
24
- "fast-html-parser": "^1.0.1",
25
- "hexo-log": "^4.1.0",
26
- "node-fetch": "^3.3.2"
27
- },
28
- "repository": {
29
- "type": "git",
30
- "url": "git@github.com:EmptyDreams/swpp-backends.git"
31
- },
32
- "homepage": "https://github.com/EmptyDreams/swpp-backends",
33
- "keywords": ["ServiceWorker", "sw"]
1
+ {
2
+ "name": "swpp-backends",
3
+ "version": "0.0.2-alpha",
4
+ "main": "dist/index.js",
5
+ "typings": "types/index.d.ts",
6
+ "description": "Generate a powerful ServiceWorker for your website.",
7
+ "author": "kmar",
8
+ "license": "AGPL-3.0",
9
+ "files": [
10
+ "dist",
11
+ "types"
12
+ ],
13
+ "devDependencies": {
14
+ "@types/css": "^0.0.33",
15
+ "@types/fast-html-parser": "^1.0.2",
16
+ "@types/node": "^20.4.5",
17
+ "gulp": "^4.0.2",
18
+ "typescript": "^5.1.6"
19
+ },
20
+ "dependencies": {
21
+ "@types/node-fetch": "^2.6.4",
22
+ "css": "^3.0.0",
23
+ "fast-html-parser": "^1.0.1",
24
+ "hexo-log": "^4.1.0",
25
+ "node-fetch": "^2.6.12"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+ssh://git@github.com/EmptyDreams/swpp-backends.git"
30
+ },
31
+ "homepage": "https://github.com/EmptyDreams/swpp-backends",
32
+ "keywords": [
33
+ "ServiceWorker",
34
+ "sw"
35
+ ],
36
+ "scripts": {}
34
37
  }
@@ -21,10 +21,10 @@ export interface VersionMap {
21
21
  *
22
22
  * + **执行该函数前必须调用过 [loadRules]**
23
23
  *
24
- * @param webRoot 网站域名
24
+ * @param domain 网站域名
25
25
  * @param url 要判断的 URL
26
26
  */
27
- export declare function isExclude(webRoot: string, url: string): boolean;
27
+ export declare function isExclude(domain: string, url: string): boolean;
28
28
  /**
29
29
  * 判断指定 URL 是否是 stable 的
30
30
  *
@@ -37,6 +37,8 @@ export declare function isStable(url: string): boolean;
37
37
  * + **执行该函数前必须调用过 [loadRules]**
38
38
  */
39
39
  export declare function loadVersionJson(url: string): Promise<VersionJson>;
40
+ /** 提交要存储到 version json 的值 */
41
+ export declare function submitCacheInfo(value: any): void;
40
42
  /**
41
43
  * 读取最后一次加载的 version json
42
44
  *
@@ -63,17 +65,17 @@ export declare function readNewVersionJson(): VersionJson;
63
65
  */
64
66
  export declare function readMergeVersionMap(): VersionMap;
65
67
  /**
66
- * 构建一个 cache json
68
+ * 构建一个 version json
67
69
  *
68
70
  * + **执行该函数前必须调用过 [loadRules]**
69
71
  * + **调用该函数前必须调用过 [loadCacheJson]**
70
72
  * + **执行该函数前必须调用过 [calcEjectValues]**
71
73
  *
72
74
  * @param protocol 网站的网络协议
73
- * @param webRoot 网站域名(包括二级域名)
75
+ * @param domain 网站域名(包括二级域名)
74
76
  * @param root 网页根目录(首页 index.html 所在目录)
75
77
  */
76
- export declare function buildVersionJson(protocol: ('https://' | 'http://'), webRoot: string, root: string): Promise<VersionJson>;
78
+ export declare function buildVersionJson(protocol: ('https://' | 'http://'), domain: string, root: string): Promise<VersionJson>;
77
79
  /**
78
80
  * 检索一个 URL 指向的文件中所有地外部链接
79
81
  *
@@ -83,12 +85,12 @@ export declare function buildVersionJson(protocol: ('https://' | 'http://'), web
83
85
  * + **调用该函数前必须调用过 [loadCacheJson]**
84
86
  * + **执行该函数前必须调用过 [calcEjectValues]**
85
87
  *
86
- * @param webRoot 网站域名
88
+ * @param domain 网站域名
87
89
  * @param url 要检索的 URL
88
90
  * @param result 存放结果的对象
89
91
  * @param event 检索到一个 URL 时触发的事件
90
92
  */
91
- export declare function eachAllLinkInUrl(webRoot: string, url: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
93
+ export declare function eachAllLinkInUrl(domain: string, url: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
92
94
  /**
93
95
  * 检索 HTML 文件中的所有外部链接
94
96
  *
@@ -97,12 +99,12 @@ export declare function eachAllLinkInUrl(webRoot: string, url: string, result: V
97
99
  * + **执行该函数前必须调用过 [loadRules]**
98
100
  * + **调用该函数前必须调用过 [loadCacheJson]**
99
101
  *
100
- * @param webRoot 网站域名
102
+ * @param domain 网站域名
101
103
  * @param content HTML 文件内容
102
104
  * @param result 存放结果的对象
103
105
  * @param event 检索到 URL 时触发的事件
104
106
  */
105
- export declare function eachAllLinkInHtml(webRoot: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
107
+ export declare function eachAllLinkInHtml(domain: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
106
108
  /**
107
109
  * 检索 CSS 文件中的所有外部链
108
110
  *
@@ -111,12 +113,12 @@ export declare function eachAllLinkInHtml(webRoot: string, content: string, resu
111
113
  * + **执行该函数前必须调用过 [loadRules]**
112
114
  * + **调用该函数前必须调用过 [loadCacheJson]**
113
115
  *
114
- * @param webRoot 网站域名
116
+ * @param domain 网站域名
115
117
  * @param content CSS 文件内容
116
118
  * @param result 存放结果的对象
117
119
  * @param event 当检索到一个 URL 后触发的事件
118
120
  */
119
- export declare function eachAllLinkInCss(webRoot: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
121
+ export declare function eachAllLinkInCss(domain: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
120
122
  /**
121
123
  * 遍历 JS 文件中地所有外部链接
122
124
  *
@@ -125,12 +127,12 @@ export declare function eachAllLinkInCss(webRoot: string, content: string, resul
125
127
  * + **执行该函数前必须调用过 [loadRules]**
126
128
  * + **调用该函数前必须调用过 [loadCacheJson]**
127
129
  *
128
- * @param webRoot 网站域名
130
+ * @param domain 网站域名
129
131
  * @param content JS 文件内容
130
132
  * @param result 存放结果的对象
131
133
  * @param event 当检索到一个 URL 后触发的事件
132
134
  */
133
- export declare function eachAllLinkInJavaScript(webRoot: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
135
+ export declare function eachAllLinkInJavaScript(domain: string, content: string, result: VersionMap, event?: (url: string) => void): Promise<void>;
134
136
  /**
135
137
  * 查询指定 URL 对应的缓存规则
136
138
  *
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { readEjectData, getSource, fetchFile, replaceDevRequest, calcEjectValues } from './Utils';
2
- import { isExclude, isStable, loadVersionJson, readOldVersionJson, readNewVersionJson, readMergeVersionMap, buildVersionJson, eachAllLinkInUrl, eachAllLinkInHtml, eachAllLinkInCss, eachAllLinkInJavaScript, findCache, replaceRequest } from './FileAnalyzer';
2
+ import { isExclude, isStable, loadVersionJson, readOldVersionJson, readNewVersionJson, readMergeVersionMap, buildVersionJson, eachAllLinkInUrl, eachAllLinkInHtml, eachAllLinkInCss, eachAllLinkInJavaScript, findCache, replaceRequest, submitCacheInfo } from './FileAnalyzer';
3
3
  import { buildServiceWorker } from './ServiceWorkerBuilder';
4
4
  import { readRules, loadRules, addRulesMapEvent } from './SwppRules';
5
5
  import { readUpdateJson, loadUpdateJson, submitChange, getShorthand, buildNewInfo } from './UpdateJsonBuilder';
@@ -29,6 +29,7 @@ declare const _default: {
29
29
  addRulesMapEvent: typeof addRulesMapEvent;
30
30
  refreshUrl: typeof refreshUrl;
31
31
  submitChange: typeof submitChange;
32
+ submitCacheInfo: typeof submitCacheInfo;
32
33
  };
33
34
  utils: {
34
35
  getSource: typeof getSource;