swpp-backends 3.0.0-alpha.3 → 3.0.0-alpha.5

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.
@@ -17,7 +17,7 @@ class JsonBuilder {
17
17
  }
18
18
  // noinspection JSUnusedGlobalSymbols
19
19
  async buildJson() {
20
- const json = await this.compilation.compilationEnv.read('VERSION_FILE').fetcher();
20
+ const json = await this.compilation.compilationEnv.read('SWPP_JSON_FILE').fetcher();
21
21
  if (json.info.length == 0) {
22
22
  json.info.push({ version: 1 });
23
23
  return json;
@@ -248,7 +248,10 @@ class FileUpdateTracker {
248
248
  }
249
249
  // noinspection JSUnusedGlobalSymbols
250
250
  /** 从网络拉取并解析 tracker */
251
- static async parserJsonFromNetwork(compilation, url) {
251
+ static async parserJsonFromNetwork(compilation) {
252
+ const domain = compilation.compilationEnv.read('DOMAIN_HOST');
253
+ const jsonInfo = compilation.compilationEnv.read('SWPP_JSON_FILE');
254
+ const url = new URL(jsonInfo.trackerPath, `https://${domain}`);
252
255
  const fetcher = compilation.compilationEnv.read('FETCH_NETWORK_FILE');
253
256
  const isNotFound = compilation.compilationEnv.read('IS_NOT_FOUND');
254
257
  const notFoundLevel = compilation.compilationEnv.read('ALLOW_NOT_FOUND');
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RuntimeData = exports.SwCompiler = void 0;
4
4
  const CrossDepCode_1 = require("./database/CrossDepCode");
5
+ const DomCode_1 = require("./database/DomCode");
5
6
  const RuntimeCoreCode_1 = require("./database/RuntimeCoreCode");
6
7
  const RuntimeDepCode_1 = require("./database/RuntimeDepCode");
7
8
  const CrossEnv_1 = require("./database/CrossEnv");
@@ -44,6 +45,8 @@ class RuntimeData {
44
45
  this.runtimeEvent = new RuntimeEventCode_1.RuntimeEventCode();
45
46
  /** 运行时/编译时工具函数 */
46
47
  this.crossDep = new CrossDepCode_1.CrossDepCode();
48
+ /** DOM 相关设置 */
49
+ this.domConfig = new DomCode_1.DomCode();
47
50
  }
48
51
  getDatabase(key) {
49
52
  if (!(key in this))
@@ -67,7 +67,7 @@ class ConfigLoader {
67
67
  const config = this.config;
68
68
  // 写入运行时信息
69
69
  const writeRuntime = () => {
70
- const insertList = ['runtimeDep', 'runtimeCore', 'runtimeEvent'];
70
+ const insertList = ['runtimeDep', 'runtimeCore', 'runtimeEvent', 'domConfig'];
71
71
  for (let str of insertList) {
72
72
  const configValue = config[str];
73
73
  if (!configValue)
@@ -174,8 +174,5 @@ ConfigLoader.extensions = [
174
174
  'js', 'ts', 'cjs', 'cts', 'mjs', 'mjs'
175
175
  ];
176
176
  ConfigLoader.jiti = (0, jiti_1.createJiti)(__filename, {
177
- fsCache: false,
178
- alias: {
179
- 'swpp-backends': path_1.default.join(__dirname, '..', '..', 'index')
180
- }
177
+ fsCache: false
181
178
  });
@@ -97,15 +97,16 @@ function buildCommon(_env, crossEnv, crossCode) {
97
97
  }
98
98
  }),
99
99
  /** 获取已经上线的版本文件 */
100
- VERSION_FILE: (0, KeyValueDatabase_1.buildEnv)({
100
+ SWPP_JSON_FILE: (0, KeyValueDatabase_1.buildEnv)({
101
101
  default: {
102
- path: 'update.json',
102
+ trackerPath: 'swpp/tracker.json',
103
+ versionPath: 'swpp/update.json',
103
104
  async fetcher() {
104
105
  const host = env.read('DOMAIN_HOST');
105
106
  const fetcher = env.read('FETCH_NETWORK_FILE');
106
107
  const isNotFound = env.read('IS_NOT_FOUND');
107
108
  try {
108
- const response = await fetcher.fetch(`https://${host}/${this.path}`);
109
+ const response = await fetcher.fetch(new URL(this.versionPath, `https://${host}`));
109
110
  if (!isNotFound.response(response)) {
110
111
  const json = await response.json();
111
112
  return json;
@@ -16,6 +16,7 @@ class DomCode extends RuntimeKeyValueDatabase_1.RuntimeKeyValueDatabase {
16
16
  }
17
17
  buildInnerSource() {
18
18
  const map = this.entries();
19
+ delete map['registry'];
19
20
  const inlineCode = Object.keys(map)
20
21
  .filter(it => it.startsWith('_inline'))
21
22
  .map(it => `${it}()`);
@@ -38,6 +39,19 @@ let pjaxUpdate;
38
39
  let postMessage2Sw;
39
40
  function buildCommon() {
40
41
  return {
42
+ registry: {
43
+ default: () => {
44
+ const sw = navigator.serviceWorker;
45
+ if (sw) {
46
+ sw.register('sw.js')
47
+ .then(() => console.log('SWPP 注册成功'))
48
+ .catch(err => console.error('SWPP 注册失败', err));
49
+ }
50
+ else {
51
+ console.warn('当前浏览器不支持 SW');
52
+ }
53
+ }
54
+ },
41
55
  postMessage2Sw: {
42
56
  default: (type) => controller.postMessage(type)
43
57
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "3.0.0-alpha.3",
3
+ "version": "3.0.0-alpha.5",
4
4
  "main": "dist/index.js",
5
5
  "typings": "types/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",
@@ -65,5 +65,5 @@ export declare class FileUpdateTracker {
65
65
  /** 解序列化数据 */
66
66
  static unJson(compilation: CompilationData, jsonStr: string): FileUpdateTracker;
67
67
  /** 从网络拉取并解析 tracker */
68
- static parserJsonFromNetwork(compilation: CompilationData, url: RequestInfo | URL): Promise<FileUpdateTracker>;
68
+ static parserJsonFromNetwork(compilation: CompilationData): Promise<FileUpdateTracker>;
69
69
  }
@@ -1,5 +1,6 @@
1
1
  import { CompilationEnv } from './database/CompilationEnv';
2
2
  import { CrossDepCode } from './database/CrossDepCode';
3
+ import { DomCode } from './database/DomCode';
3
4
  import { RuntimeCoreCode } from './database/RuntimeCoreCode';
4
5
  import { RuntimeDepCode } from './database/RuntimeDepCode';
5
6
  import { CrossEnv } from './database/CrossEnv';
@@ -15,17 +16,19 @@ export declare class SwCompiler {
15
16
  /** 运行时数据 */
16
17
  export declare class RuntimeData {
17
18
  /** 控制插入顺序 */
18
- readonly insertOrder: (Exclude<keyof RuntimeData, 'insertOrder'> | string)[];
19
+ readonly insertOrder: (Exclude<keyof RuntimeData, 'insertOrder' | 'domConfig'> | string)[];
19
20
  /** 运行时环境变量 */
20
- readonly crossEnv: CrossEnv;
21
+ crossEnv: CrossEnv;
21
22
  /** 运行时工具函数 */
22
- readonly runtimeDep: RuntimeDepCode;
23
+ runtimeDep: RuntimeDepCode;
23
24
  /** 运行时核心功能函数 */
24
- readonly runtimeCore: RuntimeCoreCode;
25
+ runtimeCore: RuntimeCoreCode;
25
26
  /** 运行时事件注册 */
26
- readonly runtimeEvent: RuntimeEventCode;
27
+ runtimeEvent: RuntimeEventCode;
27
28
  /** 运行时/编译时工具函数 */
28
- readonly crossDep: CrossDepCode;
29
+ crossDep: CrossDepCode;
30
+ /** DOM 相关设置 */
31
+ domConfig: DomCode;
29
32
  getDatabase(key: string): RuntimeKeyValueDatabase<any, {}>;
30
33
  }
31
34
  /** 编译时数据 */
@@ -27,8 +27,9 @@ declare function buildCommon(_env: any, crossEnv: CrossEnv, crossCode: CrossDepC
27
27
  /** 版本信息长度限制 */
28
28
  readonly VERSION_LENGTH_LIMIT: import("./KeyValueDatabase").DatabaseValue<number>;
29
29
  /** 获取已经上线的版本文件 */
30
- readonly VERSION_FILE: import("./KeyValueDatabase").DatabaseValue<{
31
- path: string;
30
+ readonly SWPP_JSON_FILE: import("./KeyValueDatabase").DatabaseValue<{
31
+ trackerPath: string;
32
+ versionPath: string;
32
33
  fetcher(): Promise<UpdateJson>;
33
34
  }>;
34
35
  /** 读取一个本地文件 */
@@ -6,6 +6,9 @@ export declare class DomCode extends RuntimeKeyValueDatabase<any, COMMON_TYPE_DO
6
6
  buildInnerSource(): string;
7
7
  }
8
8
  declare function buildCommon(): {
9
+ readonly registry: {
10
+ readonly default: () => void;
11
+ };
9
12
  readonly postMessage2Sw: {
10
13
  readonly default: (type: string) => void;
11
14
  };