swpp-backends 3.0.0-alpha.0 → 3.0.0-alpha.2

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.
package/dist/index.js CHANGED
@@ -1,7 +1,52 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.version = void 0;
3
+ exports.defineCompilationEnv = exports.defineCrossEnv = exports.defineRuntimeDep = exports.defineCrossDep = exports.defineRuntimeCore = exports.defineDomConfig = exports.defineRuntimeEvent = exports.defineConfig = exports.defineIndivisibleConfig = exports.DomCode = exports.AllowNotFoundEnum = exports.CompilationEnv = exports.CrossDepCode = exports.CrossEnv = exports.RuntimeDepCode = exports.RuntimeCoreCode = exports.RuntimeEventCode = exports.RuntimeKeyValueDatabase = exports.KeyValueDatabase = exports.ConfigLoader = exports.FiniteConcurrencyFetcher = exports.RuntimeData = exports.SwCompiler = exports.FileParserRegistry = exports.JsonBuilder = exports.FileUpdateTracker = exports.ResourcesScanner = exports.utils = exports.swppVersion = void 0;
4
4
  const untils_1 = require("./swpp/untils");
5
5
  /** 版本号 */
6
- exports.version = require('../package.json').version;
7
- untils_1.utils.printInfo('INDEX', `欢迎使用 swpp@${exports.version}`);
6
+ exports.swppVersion = require('../package.json').version;
7
+ var untils_2 = require("./swpp/untils");
8
+ Object.defineProperty(exports, "utils", { enumerable: true, get: function () { return untils_2.utils; } });
9
+ var ResourcesScanner_1 = require("./swpp/ResourcesScanner");
10
+ Object.defineProperty(exports, "ResourcesScanner", { enumerable: true, get: function () { return ResourcesScanner_1.ResourcesScanner; } });
11
+ Object.defineProperty(exports, "FileUpdateTracker", { enumerable: true, get: function () { return ResourcesScanner_1.FileUpdateTracker; } });
12
+ var JsonBuilder_1 = require("./swpp/JsonBuilder");
13
+ Object.defineProperty(exports, "JsonBuilder", { enumerable: true, get: function () { return JsonBuilder_1.JsonBuilder; } });
14
+ var FileParser_1 = require("./swpp/FileParser");
15
+ Object.defineProperty(exports, "FileParserRegistry", { enumerable: true, get: function () { return FileParser_1.FileParserRegistry; } });
16
+ var SwCompiler_1 = require("./swpp/SwCompiler");
17
+ Object.defineProperty(exports, "SwCompiler", { enumerable: true, get: function () { return SwCompiler_1.SwCompiler; } });
18
+ Object.defineProperty(exports, "RuntimeData", { enumerable: true, get: function () { return SwCompiler_1.RuntimeData; } });
19
+ var NetworkFileHandler_1 = require("./swpp/NetworkFileHandler");
20
+ Object.defineProperty(exports, "FiniteConcurrencyFetcher", { enumerable: true, get: function () { return NetworkFileHandler_1.FiniteConcurrencyFetcher; } });
21
+ var ConfigLoader_1 = require("./swpp/config/ConfigLoader");
22
+ Object.defineProperty(exports, "ConfigLoader", { enumerable: true, get: function () { return ConfigLoader_1.ConfigLoader; } });
23
+ var KeyValueDatabase_1 = require("./swpp/database/KeyValueDatabase");
24
+ Object.defineProperty(exports, "KeyValueDatabase", { enumerable: true, get: function () { return KeyValueDatabase_1.KeyValueDatabase; } });
25
+ var RuntimeKeyValueDatabase_1 = require("./swpp/database/RuntimeKeyValueDatabase");
26
+ Object.defineProperty(exports, "RuntimeKeyValueDatabase", { enumerable: true, get: function () { return RuntimeKeyValueDatabase_1.RuntimeKeyValueDatabase; } });
27
+ var RuntimeEventCode_1 = require("./swpp/database/RuntimeEventCode");
28
+ Object.defineProperty(exports, "RuntimeEventCode", { enumerable: true, get: function () { return RuntimeEventCode_1.RuntimeEventCode; } });
29
+ var RuntimeCoreCode_1 = require("./swpp/database/RuntimeCoreCode");
30
+ Object.defineProperty(exports, "RuntimeCoreCode", { enumerable: true, get: function () { return RuntimeCoreCode_1.RuntimeCoreCode; } });
31
+ var RuntimeDepCode_1 = require("./swpp/database/RuntimeDepCode");
32
+ Object.defineProperty(exports, "RuntimeDepCode", { enumerable: true, get: function () { return RuntimeDepCode_1.RuntimeDepCode; } });
33
+ var CrossEnv_1 = require("./swpp/database/CrossEnv");
34
+ Object.defineProperty(exports, "CrossEnv", { enumerable: true, get: function () { return CrossEnv_1.CrossEnv; } });
35
+ var CrossDepCode_1 = require("./swpp/database/CrossDepCode");
36
+ Object.defineProperty(exports, "CrossDepCode", { enumerable: true, get: function () { return CrossDepCode_1.CrossDepCode; } });
37
+ var CompilationEnv_1 = require("./swpp/database/CompilationEnv");
38
+ Object.defineProperty(exports, "CompilationEnv", { enumerable: true, get: function () { return CompilationEnv_1.CompilationEnv; } });
39
+ Object.defineProperty(exports, "AllowNotFoundEnum", { enumerable: true, get: function () { return CompilationEnv_1.AllowNotFoundEnum; } });
40
+ var DomCode_1 = require("./swpp/database/DomCode");
41
+ Object.defineProperty(exports, "DomCode", { enumerable: true, get: function () { return DomCode_1.DomCode; } });
42
+ untils_1.utils.printInfo('INDEX', `欢迎使用 swpp@${exports.swppVersion}`);
43
+ var ConfigCluster_1 = require("./swpp/config/ConfigCluster");
44
+ Object.defineProperty(exports, "defineIndivisibleConfig", { enumerable: true, get: function () { return ConfigCluster_1.defineIndivisibleConfig; } });
45
+ Object.defineProperty(exports, "defineConfig", { enumerable: true, get: function () { return ConfigCluster_1.defineConfig; } });
46
+ Object.defineProperty(exports, "defineRuntimeEvent", { enumerable: true, get: function () { return ConfigCluster_1.defineRuntimeEvent; } });
47
+ Object.defineProperty(exports, "defineDomConfig", { enumerable: true, get: function () { return ConfigCluster_1.defineDomConfig; } });
48
+ Object.defineProperty(exports, "defineRuntimeCore", { enumerable: true, get: function () { return ConfigCluster_1.defineRuntimeCore; } });
49
+ Object.defineProperty(exports, "defineCrossDep", { enumerable: true, get: function () { return ConfigCluster_1.defineCrossDep; } });
50
+ Object.defineProperty(exports, "defineRuntimeDep", { enumerable: true, get: function () { return ConfigCluster_1.defineRuntimeDep; } });
51
+ Object.defineProperty(exports, "defineCrossEnv", { enumerable: true, get: function () { return ConfigCluster_1.defineCrossEnv; } });
52
+ Object.defineProperty(exports, "defineCompilationEnv", { enumerable: true, get: function () { return ConfigCluster_1.defineCompilationEnv; } });
@@ -3,7 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.buildFileParser = exports.FileParserRegistry = void 0;
6
+ exports.FileParserRegistry = void 0;
7
+ exports.buildFileParser = buildFileParser;
7
8
  const path_1 = __importDefault(require("path"));
8
9
  const untils_1 = require("./untils");
9
10
  class FileParserRegistry {
@@ -82,4 +83,3 @@ exports.FileParserRegistry = FileParserRegistry;
82
83
  function buildFileParser(parser) {
83
84
  return parser;
84
85
  }
85
- exports.buildFileParser = buildFileParser;
@@ -15,6 +15,7 @@ class JsonBuilder {
15
15
  putHeader(key, value) {
16
16
  this.headers.set(key, value);
17
17
  }
18
+ // noinspection JSUnusedGlobalSymbols
18
19
  async buildJson() {
19
20
  const json = await this.compilation.compilationEnv.read('VERSION_FILE')();
20
21
  if (json.info.length == 0) {
@@ -30,6 +30,7 @@ exports.FileUpdateTracker = exports.ResourcesScanner = void 0;
30
30
  const fs_1 = __importDefault(require("fs"));
31
31
  const crypto = __importStar(require("node:crypto"));
32
32
  const path_1 = __importDefault(require("path"));
33
+ const CompilationEnv_1 = require("./database/CompilationEnv");
33
34
  const JsonBuilder_1 = require("./JsonBuilder");
34
35
  const untils_1 = require("./untils");
35
36
  /**
@@ -39,6 +40,7 @@ class ResourcesScanner {
39
40
  constructor(compilation) {
40
41
  this.compilation = compilation;
41
42
  }
43
+ // noinspection JSUnusedGlobalSymbols
42
44
  /** 扫描指定目录下的所有文件 */
43
45
  async scanLocalFile(path) {
44
46
  const matchCacheRule = this.compilation.crossDep.read('matchCacheRule');
@@ -159,6 +161,7 @@ class FileUpdateTracker {
159
161
  addUrl(url) {
160
162
  this.allUrl.add(url);
161
163
  }
164
+ // noinspection JSUnusedGlobalSymbols
162
165
  /**
163
166
  * 判断两个 tracker 的差异
164
167
  *
@@ -186,6 +189,7 @@ class FileUpdateTracker {
186
189
  });
187
190
  return diff;
188
191
  }
192
+ // noinspection JSUnusedGlobalSymbols
189
193
  /**
190
194
  * 将数据序列化为 JSON
191
195
  *
@@ -242,5 +246,45 @@ class FileUpdateTracker {
242
246
  }
243
247
  return tracker;
244
248
  }
249
+ // noinspection JSUnusedGlobalSymbols
250
+ /** 从网络拉取并解析 tracker */
251
+ static async parserJsonFromNetwork(compilation, url) {
252
+ const fetcher = compilation.compilationEnv.read('FETCH_NETWORK_FILE');
253
+ const isNotFound = compilation.compilationEnv.read('IS_NOT_FOUND');
254
+ const notFoundLevel = compilation.compilationEnv.read('ALLOW_NOT_FOUND');
255
+ let error;
256
+ const result = await (async () => {
257
+ try {
258
+ const response = await fetcher.fetch(url);
259
+ if (isNotFound.response(response)) {
260
+ if (notFoundLevel == CompilationEnv_1.AllowNotFoundEnum.REJECT_ALL) {
261
+ error = {
262
+ code: untils_1.exceptionNames.notFound,
263
+ message: `拉取 ${url} 时出现 404 错误`
264
+ };
265
+ return;
266
+ }
267
+ untils_1.utils.printWarning('SCANNER', '拉取 tracker 时服务器返回了 404,如果是第一次携带 swpp v3 构建网站请忽视这条信息');
268
+ return new FileUpdateTracker(compilation);
269
+ }
270
+ const text = await response.text();
271
+ return FileUpdateTracker.unJson(compilation, text);
272
+ }
273
+ catch (e) {
274
+ if (isNotFound.error(e) && notFoundLevel == CompilationEnv_1.AllowNotFoundEnum.ALLOW_ALL) {
275
+ untils_1.utils.printWarning('SCANNER', '拉取 tracker 时 DNS 解析失败,如果是第一次携带 swpp v3 构建网站且网站暂时无法解析请忽视这条信息');
276
+ return new FileUpdateTracker(compilation);
277
+ }
278
+ untils_1.utils.printError('SCANNER', e);
279
+ throw {
280
+ code: untils_1.exceptionNames.error,
281
+ message: `拉取或解析历史 Tracker 时出现错误`
282
+ };
283
+ }
284
+ })();
285
+ if (result)
286
+ return result;
287
+ throw error;
288
+ }
245
289
  }
246
290
  exports.FileUpdateTracker = FileUpdateTracker;
@@ -11,6 +11,7 @@ class SwCompiler {
11
11
  constructor() {
12
12
  this.swCode = '';
13
13
  }
14
+ // noinspection JSUnusedGlobalSymbols
14
15
  /**
15
16
  * 构建 sw 代码,该函数结果会被缓存
16
17
  */
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defineConfig = defineConfig;
4
+ exports.defineCompilationEnv = defineCompilationEnv;
5
+ exports.defineCrossEnv = defineCrossEnv;
6
+ exports.defineRuntimeDep = defineRuntimeDep;
7
+ exports.defineCrossDep = defineCrossDep;
8
+ exports.defineRuntimeCore = defineRuntimeCore;
9
+ exports.defineDomConfig = defineDomConfig;
10
+ exports.defineRuntimeEvent = defineRuntimeEvent;
11
+ exports.defineIndivisibleConfig = defineIndivisibleConfig;
12
+ const ConfigLoader_1 = require("./ConfigLoader");
13
+ /** 定义一个通过 `export default` 导出的配置 */
14
+ function defineConfig(config) {
15
+ return config;
16
+ }
17
+ /** 定义一个通过 `export const compilationEnv` 导出的配置 */
18
+ function defineCompilationEnv(config) {
19
+ return config;
20
+ }
21
+ /** 定义一个通过 `export const crossEnv` 导出的配置 */
22
+ function defineCrossEnv(config) {
23
+ return config;
24
+ }
25
+ /** 定义一个通过 `export const runtimeDep` 导出的配置 */
26
+ function defineRuntimeDep(config) {
27
+ return config;
28
+ }
29
+ /** 定义一个通过 `export const crossDep` 导出的配置 */
30
+ function defineCrossDep(config) {
31
+ return config;
32
+ }
33
+ /** 定义一个通过 `export const runtimeCore` 导出的配置 */
34
+ function defineRuntimeCore(config) {
35
+ return config;
36
+ }
37
+ /** 定义一个通过 `export const domConfig` 导出的配置 */
38
+ function defineDomConfig(config) {
39
+ return config;
40
+ }
41
+ /** 定义一个通过 `export const runtimeEvent` 导出的配置 */
42
+ function defineRuntimeEvent(config) {
43
+ return config;
44
+ }
45
+ /**
46
+ * 定义一个无法分割的对象配置,这对一些强依赖对象内部属性的设置很有用,可以避免对象被错误地拼接。
47
+ *
48
+ * 默认情况下,当定义一个对象配置时,将允许从其它配置文件中合并一部分配置到对象中,比如:
49
+ *
50
+ * ```typescript
51
+ * // 当前配置
52
+ * exampleConfig.obj = {
53
+ * value1: 'hello world'
54
+ * }
55
+ * // 如果还有一个配置文件中也声明了这个配置
56
+ * exampleConfig.obj = {
57
+ * value2: 'hello swpp'
58
+ * }
59
+ * // 最终将合并生成如下配置
60
+ * exampleConfig.obj = {
61
+ * value1: 'hello world',
62
+ * value2: 'hello swpp'
63
+ * }
64
+ * ```
65
+ *
66
+ * 通过该函数,可以禁止 swpp 合并配置时仅选取对象的部分字段,要么全部使用 [value] 的值,要么完全不使用 [value] 的值。
67
+ *
68
+ * 放入到上述例子中,假如两个 obj 任意一个或多个通过 `defineIndivisibleConfig({ xxx: xxx })` 设置,最终的值将取决于两个配置文件的优先级,
69
+ * 若 `value2` 的优先级高将产生:
70
+ *
71
+ * ```typescript
72
+ * // 最终结果
73
+ * exampleConfig.obj = {
74
+ * value2: 'hello swpp'
75
+ * }
76
+ * ```
77
+ */
78
+ function defineIndivisibleConfig(value) {
79
+ Object.defineProperty(value, ConfigLoader_1.IndivisibleName, {
80
+ value: true,
81
+ writable: false,
82
+ configurable: false,
83
+ enumerable: false
84
+ });
85
+ return value;
86
+ }
@@ -3,14 +3,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.defineRuntimeEvent = exports.defineRuntimeCore = exports.defineCrossDep = exports.defineRuntimeDep = exports.defineCrossEnv = exports.defineCompilationEnv = exports.defineConfig = exports.ConfigLoader = void 0;
6
+ exports.ConfigLoader = exports.IndivisibleName = void 0;
7
7
  const jiti_1 = require("jiti");
8
8
  const path_1 = __importDefault(require("path"));
9
+ const CompilationEnv_1 = require("../database/CompilationEnv");
10
+ const SwCompiler_1 = require("../SwCompiler");
9
11
  const untils_1 = require("../untils");
12
+ exports.IndivisibleName = '1indivisible__';
10
13
  class ConfigLoader {
11
14
  constructor() {
15
+ this.modifierList = [];
12
16
  this.isBuilt = false;
13
17
  }
18
+ // noinspection JSUnusedGlobalSymbols
14
19
  /**
15
20
  * 加载一个配置文件,越早加载的优先级越高
16
21
  * @param file
@@ -33,18 +38,32 @@ class ConfigLoader {
33
38
  // @ts-ignore
34
39
  const content = await ConfigLoader.jiti.import(file);
35
40
  let newConfig = 'default' in content ? content.default : content;
41
+ if ('modifier' in newConfig) {
42
+ this.modifierList.push(newConfig.modifier);
43
+ }
36
44
  if (this.config)
37
45
  this.mergeConfig(newConfig);
38
46
  else
39
47
  this.config = newConfig;
40
48
  }
49
+ // noinspection JSUnusedGlobalSymbols
41
50
  /** 将配置项的内容写入到环境中 */
42
- write(runtime, compilation) {
51
+ generate() {
43
52
  if (!this.config)
44
53
  throw {
45
54
  code: untils_1.exceptionNames.nullPoint,
46
55
  message: '构建配之前必须至少加载一个配置文件'
47
56
  };
57
+ // 构建属性集
58
+ const { runtime, compilation } = (this.modifierList.find(it => it.build)?.build ?? (() => {
59
+ const runtime = new SwCompiler_1.RuntimeData();
60
+ const compilation = {
61
+ compilationEnv: new CompilationEnv_1.CompilationEnv(runtime.crossEnv, runtime.crossDep),
62
+ crossDep: runtime.crossDep,
63
+ crossEnv: runtime.crossEnv
64
+ };
65
+ return { runtime, compilation };
66
+ }))();
48
67
  const config = this.config;
49
68
  // 写入运行时信息
50
69
  const writeRuntime = () => {
@@ -107,14 +126,27 @@ class ConfigLoader {
107
126
  }
108
127
  }
109
128
  };
129
+ // 运行 registry
130
+ for (let i = this.modifierList.length - 1; i >= 0; i--) {
131
+ const modifier = this.modifierList[i];
132
+ modifier.registry?.(runtime, compilation);
133
+ }
110
134
  writeRuntime();
111
135
  writeCompilation();
112
136
  writeCross();
137
+ // 运行 dynamic
138
+ for (let i = this.modifierList.length - 1; i >= 0; i--) {
139
+ const modifier = this.modifierList[i];
140
+ modifier.dynamic?.(runtime, compilation);
141
+ }
142
+ return Object.freeze({ runtime, compilation });
113
143
  }
114
144
  /** 将新配置合并到已有配置中 */
115
145
  mergeConfig(other) {
116
- function mergeHelper(high, low) {
146
+ function mergeHelper(high, low, skip) {
117
147
  for (let key in low) {
148
+ if (skip && key == 'modifier')
149
+ continue;
118
150
  const lowValue = low[key];
119
151
  if (key in high) {
120
152
  const highValue = high[key];
@@ -124,8 +156,8 @@ class ConfigLoader {
124
156
  }
125
157
  if (typeof highValue != typeof lowValue)
126
158
  continue;
127
- if (typeof highValue == 'object') {
128
- mergeHelper(highValue, lowValue);
159
+ if (typeof highValue == 'object' && !highValue[exports.IndivisibleName] && !lowValue[exports.IndivisibleName]) {
160
+ mergeHelper(highValue, lowValue, false);
129
161
  }
130
162
  }
131
163
  else {
@@ -133,7 +165,7 @@ class ConfigLoader {
133
165
  }
134
166
  }
135
167
  }
136
- mergeHelper(this.config, other);
168
+ mergeHelper(this.config, other, true);
137
169
  }
138
170
  }
139
171
  exports.ConfigLoader = ConfigLoader;
@@ -143,43 +175,7 @@ ConfigLoader.extensions = [
143
175
  ];
144
176
  ConfigLoader.jiti = (0, jiti_1.createJiti)(__filename, {
145
177
  fsCache: false,
146
- moduleCache: false,
147
178
  alias: {
148
- 'swpp-backends': path_1.default.join(__dirname, '..')
179
+ 'swpp-backends': path_1.default.join(__dirname, '..', '..', 'index')
149
180
  }
150
181
  });
151
- /** 定义一个通过 `export default` 导出的配置 */
152
- function defineConfig(config) {
153
- return config;
154
- }
155
- exports.defineConfig = defineConfig;
156
- /** 定义一个通过 `export const compilationEnv` 导出的配置 */
157
- function defineCompilationEnv(config) {
158
- return config;
159
- }
160
- exports.defineCompilationEnv = defineCompilationEnv;
161
- /** 定义一个通过 `export const crossEnv` 导出的配置 */
162
- function defineCrossEnv(config) {
163
- return config;
164
- }
165
- exports.defineCrossEnv = defineCrossEnv;
166
- /** 定义一个通过 `export const runtimeDep` 导出的配置 */
167
- function defineRuntimeDep(config) {
168
- return config;
169
- }
170
- exports.defineRuntimeDep = defineRuntimeDep;
171
- /** 定义一个通过 `export const crossDep` 导出的配置 */
172
- function defineCrossDep(config) {
173
- return config;
174
- }
175
- exports.defineCrossDep = defineCrossDep;
176
- /** 定义一个通过 `export const runtimeCore` 导出的配置 */
177
- function defineRuntimeCore(config) {
178
- return config;
179
- }
180
- exports.defineRuntimeCore = defineRuntimeCore;
181
- /** 定义一个通过 `export const runtimeEvent` 导出的配置 */
182
- function defineRuntimeEvent(config) {
183
- return config;
184
- }
185
- exports.defineRuntimeEvent = defineRuntimeEvent;
@@ -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.CompilationEnv = void 0;
29
+ exports.AllowNotFoundEnum = exports.CompilationEnv = void 0;
30
30
  const fs_1 = __importDefault(require("fs"));
31
31
  const FileParser_1 = require("../FileParser");
32
32
  const NetworkFileHandler_1 = require("../NetworkFileHandler");
@@ -43,6 +43,16 @@ class CompilationEnv extends KeyValueDatabase_1.KeyValueDatabase {
43
43
  }
44
44
  }
45
45
  exports.CompilationEnv = CompilationEnv;
46
+ /** 拉取版本信息和 tracker 时的 404 等级 */
47
+ var AllowNotFoundEnum;
48
+ (function (AllowNotFoundEnum) {
49
+ /** 允许任意形式的 404,包含 DNS 解析失败 */
50
+ AllowNotFoundEnum[AllowNotFoundEnum["ALLOW_ALL"] = 0] = "ALLOW_ALL";
51
+ /** 允许服务器返回 404 */
52
+ AllowNotFoundEnum[AllowNotFoundEnum["ALLOW_STATUS"] = 1] = "ALLOW_STATUS";
53
+ /** 拒绝任意形式的 404 */
54
+ AllowNotFoundEnum[AllowNotFoundEnum["REJECT_ALL"] = 2] = "REJECT_ALL";
55
+ })(AllowNotFoundEnum || (exports.AllowNotFoundEnum = AllowNotFoundEnum = {}));
46
56
  function buildCommon(_env, crossEnv, crossCode) {
47
57
  const env = _env;
48
58
  return {
@@ -130,6 +140,19 @@ function buildCommon(_env, crossEnv, crossCode) {
130
140
  error: (err) => err?.cause?.code === 'ENOTFOUND'
131
141
  }
132
142
  }),
143
+ ALLOW_NOT_FOUND: (0, KeyValueDatabase_1.buildEnv)({
144
+ default: AllowNotFoundEnum.ALLOW_STATUS,
145
+ checker(value) {
146
+ switch (value) {
147
+ case AllowNotFoundEnum.ALLOW_ALL:
148
+ case AllowNotFoundEnum.ALLOW_STATUS:
149
+ case AllowNotFoundEnum.REJECT_ALL:
150
+ return false;
151
+ default:
152
+ return { value, message: '填写了非法的 ALLOW_NOT_FOUND 值' };
153
+ }
154
+ }
155
+ }),
133
156
  FILE_PARSER: (0, KeyValueDatabase_1.buildEnv)({
134
157
  default: createRegister(env, crossEnv, crossCode)
135
158
  })
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DomCode = void 0;
4
+ const untils_1 = require("../untils");
5
+ const RuntimeKeyValueDatabase_1 = require("./RuntimeKeyValueDatabase");
6
+ class DomCode extends RuntimeKeyValueDatabase_1.RuntimeKeyValueDatabase {
7
+ constructor() {
8
+ super(buildCommon());
9
+ }
10
+ buildJsSource() {
11
+ return `
12
+ document.addEventListener('DOMContentLoaded', () => {
13
+ ${this.buildInnerSource()}
14
+ }
15
+ `;
16
+ }
17
+ buildInnerSource() {
18
+ const map = this.entries();
19
+ const inlineCode = Object.keys(map)
20
+ .filter(it => it.startsWith('_inline'))
21
+ .map(it => `${it}()`);
22
+ return `
23
+ const controller = navigator.serviceWorker?.controller
24
+ if (!controller) return
25
+ ${untils_1.utils.anyToSource(map, false, 'const')};
26
+ ${inlineCode.join(';\n')}
27
+ navigator.serviceWorker.addEventListener('message', event => {
28
+ messageEvent()
29
+ })
30
+ `;
31
+ }
32
+ }
33
+ exports.DomCode = DomCode;
34
+ let controller;
35
+ let SESSION_KEY;
36
+ let onSuccess;
37
+ let pjaxUpdate;
38
+ let postMessage2Sw;
39
+ function buildCommon() {
40
+ return {
41
+ postMessage2Sw: {
42
+ default: (type) => controller.postMessage(type)
43
+ },
44
+ pjaxUpdate: {
45
+ default: (url) => {
46
+ const type = url.endsWith('js') ? 'script' : 'link';
47
+ const name = type === 'link' ? 'href' : 'src';
48
+ for (let item of document.getElementsByTagName(type)) {
49
+ // @ts-ignore
50
+ const itUrl = item[name];
51
+ if (url.length > itUrl ? url.endsWith(itUrl) : itUrl.endsWith(url)) {
52
+ const newEle = document.createElement(type);
53
+ const content = item.textContent || item.innerHTML || '';
54
+ Array.from(item.attributes).forEach(attr => newEle.setAttribute(attr.name, attr.value));
55
+ newEle.appendChild(document.createTextNode(content));
56
+ item.parentNode.replaceChildren(newEle, item);
57
+ return;
58
+ }
59
+ }
60
+ }
61
+ },
62
+ SESSION_KEY: {
63
+ default: 'updated'
64
+ },
65
+ onSuccess: {
66
+ default: () => console.log('版本更新成功')
67
+ },
68
+ _inlineA: {
69
+ default: () => {
70
+ if (sessionStorage.getItem(SESSION_KEY)) {
71
+ onSuccess();
72
+ sessionStorage.removeItem(SESSION_KEY);
73
+ }
74
+ else
75
+ postMessage2Sw('update');
76
+ }
77
+ },
78
+ messageEvent: {
79
+ default: async (event) => {
80
+ const data = event.data;
81
+ sessionStorage.setItem(SESSION_KEY, data.type);
82
+ const list = data.data?.filter((url) => /\.(js|css)$/.test(url));
83
+ if (list?.length) {
84
+ // @ts-ignore
85
+ if (window.Pjax?.isSupported?.())
86
+ list.forEach(pjaxUpdate);
87
+ location.reload();
88
+ }
89
+ else {
90
+ onSuccess();
91
+ sessionStorage.removeItem(SESSION_KEY);
92
+ }
93
+ }
94
+ }
95
+ };
96
+ }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildEnv = exports.KeyValueDatabase = void 0;
3
+ exports.KeyValueDatabase = void 0;
4
+ exports.buildEnv = buildEnv;
4
5
  /** 键值对存储器 */
5
6
  class KeyValueDatabase {
6
7
  constructor(map) {
@@ -73,4 +74,3 @@ exports.KeyValueDatabase = KeyValueDatabase;
73
74
  function buildEnv(env) {
74
75
  return env;
75
76
  }
76
- exports.buildEnv = buildEnv;
@@ -52,11 +52,10 @@ function buildCommon() {
52
52
  /**
53
53
  * 缓存增量更新功能实现
54
54
  * @param force 是否强制更新
55
- * @return 标记缓存是否更新,-1 - 新访客,1 - 仅更新版本号,2 - 更新了缓存,否则 - 没有进行任何更新
55
+ * @return 标记缓存是否更新,-1 - 新访客,1 - 仅更新版本号,2 - 更新了缓存,string[] - 更新了部分缓存,否则 - 没有进行任何更新
56
56
  */
57
57
  handleUpdate: {
58
- default: async (force) => {
59
- const oldVersion = await readVersion();
58
+ default: async (oldVersion, force) => {
60
59
  if (!force && oldVersion && Date.now() - oldVersion.tp < UPDATE_CD)
61
60
  return;
62
61
  const json = await (await fetch(UPDATE_JSON_URL, {
@@ -68,7 +67,7 @@ function buildCommon() {
68
67
  // 新访客或触发了逃生门
69
68
  if (!oldVersion || (ESCAPE && ESCAPE !== oldVersion.escape)) {
70
69
  await writeVersion(newVersion);
71
- return oldVersion ? 2 : -1;
70
+ return oldVersion ? 1 : -1;
72
71
  }
73
72
  // 已是最新版本时跳过剩余步骤
74
73
  if (oldVersion.global === global && oldVersion.local === newVersion.local)
@@ -89,7 +88,7 @@ function buildCommon() {
89
88
  }
90
89
  }
91
90
  });
92
- return postMessage('update', urlList);
91
+ return urlList;
93
92
  }
94
93
  const changeList = infoElement.change;
95
94
  if (changeList) {
@@ -101,7 +100,7 @@ function buildCommon() {
101
100
  // 运行到这里说明版本号丢失
102
101
  await caches.delete(CACHE_NAME)
103
102
  .then(() => writeVersion(newVersion));
104
- return postMessage('reset', null);
103
+ return 2;
105
104
  }
106
105
  },
107
106
  handleFetchEvent: {
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RuntimeEventCode = void 0;
4
4
  const RuntimeKeyValueDatabase_1 = require("./RuntimeKeyValueDatabase");
5
5
  let handleFetchEvent;
6
+ let handleUpdate;
7
+ let postMessage;
8
+ let readVersion;
6
9
  class RuntimeEventCode extends RuntimeKeyValueDatabase_1.RuntimeKeyValueDatabase {
7
10
  constructor() {
8
11
  super(buildCommon());
@@ -39,14 +42,27 @@ function buildCommon() {
39
42
  }
40
43
  },
41
44
  message: {
42
- default: (event) => {
45
+ default: async (event) => {
43
46
  // @ts-ignore
44
47
  const data = event.data;
45
48
  switch (data.type) {
46
49
  case 'update':
47
- // @ts-ignore
48
- handleUpdate();
49
- break;
50
+ const oldVersion = await readVersion();
51
+ const updateResult = await handleUpdate(oldVersion);
52
+ if (!updateResult)
53
+ return;
54
+ switch (updateResult) {
55
+ case -1:
56
+ return postMessage('new', null);
57
+ case 1:
58
+ return postMessage('revise', null);
59
+ case 2:
60
+ return postMessage('update', null);
61
+ default:
62
+ if (Array.isArray(updateResult)) {
63
+ return postMessage('update', updateResult);
64
+ }
65
+ }
50
66
  }
51
67
  }
52
68
  }
@@ -177,7 +177,7 @@ exports.utils = Object.freeze({
177
177
  return `${hours}:${minutes}:${seconds}`;
178
178
  },
179
179
  printError(title, err) {
180
- console.error(`[${this.time()}] [ERR] [SWPP] [${title}]: ${JSON.stringify(err, null, 2)}`);
180
+ console.error(`[${this.time()}] [ERR] [SWPP] [${title}]: `, err);
181
181
  },
182
182
  printInfo(title, info) {
183
183
  console.info(`[${this.time()}] [INFO] [SWPP] [${title}]: ${JSON.stringify(info, null, 2)}`);
@@ -252,5 +252,9 @@ exports.exceptionNames = {
252
252
  /** 空指针 */
253
253
  nullPoint: 'null_point',
254
254
  /** 配置文件已经完成构建 */
255
- configBuilt: 'config_built'
255
+ configBuilt: 'config_built',
256
+ /** 404 错误 */
257
+ notFound: 'not_found',
258
+ /** 未知分类错误 */
259
+ error: 'error'
256
260
  };