swpp-backends 3.1.0-beta2 → 3.1.1

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.
@@ -7,7 +7,6 @@ export declare class BasicActions {
7
7
  private readonly isBuildServiceWorker;
8
8
  private readonly domJsPath;
9
9
  private readonly diffJsonPath;
10
- private readonly disableTrack;
11
10
  /**
12
11
  * 构建一个基础的 swpp 行为封装器
13
12
  */
@@ -67,8 +66,6 @@ export interface BasicActionOptions {
67
66
  domJsPath?: string;
68
67
  /** diff.json 文件路径(相对于网站根目录,留空表示不生成) */
69
68
  diffJsonPath?: string;
70
- /** 是否进行引用的静态分析,留空表示不进行(禁用静态分析后不能使用无限期缓存) */
71
- trackLink?: boolean;
72
69
  }
73
70
  interface BuildFileInfo {
74
71
  key: BasicActionKey;
@@ -12,7 +12,7 @@ class BasicActions {
12
12
  * 构建一个基础的 swpp 行为封装器
13
13
  */
14
14
  static async build(optional) {
15
- const actions = new BasicActions(optional.context, optional.isServiceWorker, optional.domJsPath, optional.diffJsonPath, optional.trackLink !== true);
15
+ const actions = new BasicActions(optional.context, optional.isServiceWorker, optional.domJsPath, optional.diffJsonPath);
16
16
  await actions.configLoader.loadFromCode({
17
17
  compilationEnv: {
18
18
  PUBLIC_PATH: (0, ConfigCluster_1.defineLazyInitConfig)((_, compilation) => {
@@ -22,12 +22,11 @@ class BasicActions {
22
22
  });
23
23
  return actions;
24
24
  }
25
- constructor(context, isBuildServiceWorker, domJsPath, diffJsonPath, disableTrack) {
25
+ constructor(context, isBuildServiceWorker, domJsPath, diffJsonPath) {
26
26
  this.context = context;
27
27
  this.isBuildServiceWorker = isBuildServiceWorker;
28
28
  this.domJsPath = domJsPath;
29
29
  this.diffJsonPath = diffJsonPath;
30
- this.disableTrack = disableTrack;
31
30
  this.configLoader = new ConfigLoader_1.ConfigLoader(this.context);
32
31
  /**
33
32
  * 各个文件的生成路径,为空则表示不生成(或配置未初始化)
@@ -40,12 +39,6 @@ class BasicActions {
40
39
  diffJson: null
41
40
  };
42
41
  this.buildCaches = {};
43
- if (disableTrack) {
44
- untils_1.utils.printInfo('Track', '链接追踪(静态分析)已被关闭,缓存主动更新已自动禁用,请勿使用永久缓存');
45
- }
46
- else {
47
- untils_1.utils.printInfo('Track', '链接追踪(静态分析)已开启,如果发现行为异常请及时反馈');
48
- }
49
42
  }
50
43
  /**
51
44
  * 加载一个配置文件或配置
@@ -82,57 +75,6 @@ class BasicActions {
82
75
  this.runtimeData = runtime;
83
76
  this.compilationData = compilation;
84
77
  this.configLoader = undefined;
85
- if (this.disableTrack) {
86
- const matcher = runtime.crossDep.read('matchCacheRule');
87
- const checker = (text) => {
88
- if (!text.includes('INFINITE_CACHE'))
89
- return true;
90
- const list = text.replaceAll('\r', '').split('\n');
91
- let inStr = '';
92
- let inComment = false;
93
- o: for (let string of list) {
94
- for (let i = 0; i < string.length; i++) {
95
- if (inStr) {
96
- if (string[i] == inStr)
97
- inStr = '';
98
- }
99
- else if (inComment) {
100
- if (string[i] == '*' && string[i + 1] === '/') {
101
- inComment = false;
102
- ++i;
103
- }
104
- }
105
- else {
106
- switch (string[i]) {
107
- case '"':
108
- case '`':
109
- case `"`:
110
- inStr = string[i];
111
- continue;
112
- case '/':
113
- if (string[i + 1] === '*') {
114
- inComment = true;
115
- }
116
- else if (string[i + 1] === '/') {
117
- continue o;
118
- }
119
- break;
120
- default:
121
- if (string.startsWith('INFINITE_CACHE', i)) {
122
- return false;
123
- }
124
- break;
125
- }
126
- }
127
- }
128
- }
129
- return true;
130
- };
131
- if (!checker(matcher.runOnBrowser.toString())) {
132
- throw new untils_1.RuntimeException(untils_1.exceptionNames.unsupportedOperate, '禁用引用链分析时禁止在 matchCacheRule 返回 INFINITE_CACHE。' +
133
- '如果您没有返回该值,可能是由于存在与该字段同名的变量或其它内容,从而导致 SWPP 误判,请您修改与 INFINITE_CACHE 重复的名称。');
134
- }
135
- }
136
78
  const compilationEnv = compilation.compilationEnv;
137
79
  const publicRoot = compilationEnv.read('PUBLIC_PATH');
138
80
  const jsonInfo = compilationEnv.read('SWPP_JSON_FILE');
@@ -220,17 +162,6 @@ class BasicActions {
220
162
  * @param excludeFilter 需排除的文件
221
163
  */
222
164
  async saveFiles(excludeFilter = []) {
223
- if (this.disableTrack) {
224
- if (!excludeFilter.includes('tracker')) {
225
- excludeFilter.push('tracker');
226
- }
227
- if (!excludeFilter.includes('version')) {
228
- excludeFilter.push('version');
229
- }
230
- if (!excludeFilter.includes('diffJson')) {
231
- excludeFilter.push('diffJson');
232
- }
233
- }
234
165
  const fileList = await this.buildFiles(excludeFilter);
235
166
  for (let item of fileList) {
236
167
  if (await item.path.exists()) {
@@ -26,9 +26,15 @@ declare function buildCommon(): {
26
26
  readonly isFetchSuccessful: {
27
27
  readonly default: FunctionInBrowserAndNode<[response: Response], boolean>;
28
28
  };
29
- /** 缓存规则 */
29
+ /**
30
+ * 缓存规则
31
+ *
32
+ * + 返回转换为 false 的值表示不缓存;
33
+ * + 返回正数表示缓存指定毫秒数;
34
+ * + 返回负数表示永久缓存(仅对本站资源有效,非本站资源返回负数等价于 24h)
35
+ */
30
36
  readonly matchCacheRule: {
31
- readonly default: FunctionInBrowserAndNode<[_url: URL], number | false | symbol | null | undefined>;
37
+ readonly default: FunctionInBrowserAndNode<[_url: URL], number | false | null | undefined>;
32
38
  };
33
39
  /** 归一化 URL */
34
40
  readonly normalizeUrl: {
@@ -42,7 +42,13 @@ function buildCommon() {
42
42
  isFetchSuccessful: {
43
43
  default: CrossDepCode.buildBothFunction((response) => [200, 301, 302, 307, 308].includes(response.status))
44
44
  },
45
- /** 缓存规则 */
45
+ /**
46
+ * 缓存规则
47
+ *
48
+ * + 返回转换为 false 的值表示不缓存;
49
+ * + 返回正数表示缓存指定毫秒数;
50
+ * + 返回负数表示永久缓存(仅对本站资源有效,非本站资源返回负数等价于 24h)
51
+ */
46
52
  matchCacheRule: {
47
53
  default: CrossDepCode.buildBothFunction((_url) => false)
48
54
  },
@@ -11,8 +11,6 @@ declare function buildCommon(): {
11
11
  readonly CACHE_NAME: import("./KeyValueDatabase").DatabaseValue<string>;
12
12
  /** 网站的基准域名 */
13
13
  readonly BASE_URL: import("./KeyValueDatabase").DatabaseValue<string>;
14
- /** 永久缓存标记 */
15
- readonly INFINITE_CACHE: import("./KeyValueDatabase").DatabaseValue<symbol>;
16
14
  /** 存储版本号的 URL */
17
15
  readonly VERSION_PATH: import("./KeyValueDatabase").DatabaseValue<string>;
18
16
  /** 逃生门版本号 */
@@ -44,17 +44,6 @@ function buildCommon() {
44
44
  return false;
45
45
  },
46
46
  }),
47
- /** 永久缓存标记 */
48
- INFINITE_CACHE: (0, KeyValueDatabase_1.buildEnv)({
49
- default: Symbol(),
50
- checker(value) {
51
- // noinspection SuspiciousTypeOfGuard
52
- if (typeof value !== 'symbol') {
53
- return { value, message: '填写的值应当为一个 Symbol' };
54
- }
55
- return false;
56
- }
57
- }),
58
47
  /** 存储版本号的 URL */
59
48
  VERSION_PATH: (0, KeyValueDatabase_1.buildEnv)({
60
49
  default: 'https://id.v3/',
@@ -30,7 +30,7 @@ declare function buildCommon(): {
30
30
  };
31
31
  /** 判断指定的缓存是否是有效缓存 */
32
32
  readonly isValidCache: {
33
- readonly default: (response: Response, rule: number | symbol) => boolean;
33
+ readonly default: (response: Response, rule: number) => boolean;
34
34
  };
35
35
  /** 读取版本号 */
36
36
  readonly readVersion: {
@@ -85,12 +85,14 @@ function buildCommon() {
85
85
  if (headers.has(INVALID_KEY))
86
86
  return false;
87
87
  // 只有本站资源允许永久缓存
88
- if (rule === INFINITE_CACHE) {
88
+ if (rule < 0) {
89
89
  const url = response.url;
90
90
  const baseLength = BASE_URL.length;
91
91
  if (url.startsWith(BASE_URL) && (url.length === baseLength || url[baseLength] === '/')) {
92
92
  return true;
93
93
  }
94
+ // 将rule设置为一天(24小时)
95
+ rule = 24 * 60 * 60 * 1000;
94
96
  }
95
97
  const storage = headers.get(STORAGE_TIMESTAMP);
96
98
  if (!storage)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "3.1.0-beta2",
3
+ "version": "3.1.1",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",