swpp-backends 3.0.0 → 3.1.0-beta1

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.d.ts CHANGED
@@ -5,7 +5,6 @@ export { utils, RuntimeException } from './swpp/untils';
5
5
  export { ResourcesScanner, FileUpdateTracker, } from './swpp/ResourcesScanner';
6
6
  export { JsonBuilder, UpdateJson, UpdateChangeExp, TrackerHeaderDiff } from './swpp/JsonBuilder';
7
7
  export { SwCompiler, CompilationData, RuntimeData, BrowserVersion } from './swpp/SwCompiler';
8
- export { NetworkFileHandler, FiniteConcurrencyFetcher } from './swpp/NetworkFileHandler';
9
8
  export { KeyValueDatabase, readThisValue } from './swpp/database/KeyValueDatabase';
10
9
  export { RuntimeKeyValueDatabase } from './swpp/database/RuntimeKeyValueDatabase';
11
10
  export { RuntimeEventCode } from './swpp/database/RuntimeEventCode';
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  // noinspection JSUnusedGlobalSymbols
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.defineLazyInitConfig = exports.defineNoCacheConfig = exports.defineContextConfig = exports.defineIndivisibleConfig = exports.NoCacheConfig = exports.IndivisibleConfig = exports.SpecialConfig = exports.defineModifier = exports.defineCompilationFP = exports.defineCompilationEnv = exports.defineCrossEnv = exports.defineRuntimeDep = exports.defineCrossDep = exports.defineRuntimeCore = exports.defineDomConfig = exports.defineRuntimeEvent = exports.defineConfig = exports.ConfigLoader = exports.DomCode = exports.AllowNotFoundEnum = exports.CompilationEnv = exports.CrossDepCode = exports.CrossEnv = exports.RuntimeDepCode = exports.RuntimeCoreCode = exports.RuntimeEventCode = exports.RuntimeKeyValueDatabase = exports.readThisValue = exports.KeyValueDatabase = exports.FiniteConcurrencyFetcher = exports.RuntimeData = exports.CompilationData = exports.SwCompiler = exports.JsonBuilder = exports.FileUpdateTracker = exports.ResourcesScanner = exports.RuntimeException = exports.utils = exports.BasicActions = exports.swppVersion = void 0;
4
+ exports.defineLazyInitConfig = exports.defineNoCacheConfig = exports.defineContextConfig = exports.defineIndivisibleConfig = exports.NoCacheConfig = exports.IndivisibleConfig = exports.SpecialConfig = exports.defineModifier = exports.defineCompilationFP = exports.defineCompilationEnv = exports.defineCrossEnv = exports.defineRuntimeDep = exports.defineCrossDep = exports.defineRuntimeCore = exports.defineDomConfig = exports.defineRuntimeEvent = exports.defineConfig = exports.ConfigLoader = exports.DomCode = exports.AllowNotFoundEnum = exports.CompilationEnv = exports.CrossDepCode = exports.CrossEnv = exports.RuntimeDepCode = exports.RuntimeCoreCode = exports.RuntimeEventCode = exports.RuntimeKeyValueDatabase = exports.readThisValue = exports.KeyValueDatabase = exports.RuntimeData = exports.CompilationData = exports.SwCompiler = exports.JsonBuilder = exports.FileUpdateTracker = exports.ResourcesScanner = exports.RuntimeException = exports.utils = exports.BasicActions = exports.swppVersion = void 0;
5
5
  /** 版本号 */
6
6
  exports.swppVersion = require('../package.json').version;
7
7
  var BasicActions_1 = require("./swpp/BasicActions");
@@ -18,8 +18,6 @@ var SwCompiler_1 = require("./swpp/SwCompiler");
18
18
  Object.defineProperty(exports, "SwCompiler", { enumerable: true, get: function () { return SwCompiler_1.SwCompiler; } });
19
19
  Object.defineProperty(exports, "CompilationData", { enumerable: true, get: function () { return SwCompiler_1.CompilationData; } });
20
20
  Object.defineProperty(exports, "RuntimeData", { enumerable: true, get: function () { return SwCompiler_1.RuntimeData; } });
21
- var NetworkFileHandler_1 = require("./swpp/NetworkFileHandler");
22
- Object.defineProperty(exports, "FiniteConcurrencyFetcher", { enumerable: true, get: function () { return NetworkFileHandler_1.FiniteConcurrencyFetcher; } });
23
21
  var KeyValueDatabase_1 = require("./swpp/database/KeyValueDatabase");
24
22
  Object.defineProperty(exports, "KeyValueDatabase", { enumerable: true, get: function () { return KeyValueDatabase_1.KeyValueDatabase; } });
25
23
  Object.defineProperty(exports, "readThisValue", { enumerable: true, get: function () { return KeyValueDatabase_1.readThisValue; } });
@@ -1,4 +1,3 @@
1
- import { CompilationData } from './SwCompiler';
2
1
  export declare class FilePath {
3
2
  readonly absPath: string;
4
3
  readonly baseProject: string | null;
@@ -27,10 +26,6 @@ export declare class FilePath {
27
26
  * 检查路径是否指向一个目录
28
27
  */
29
28
  isDirectory(): Promise<boolean>;
30
- /**
31
- * 检查路径是否指向一个文件
32
- */
33
- isFile(): Promise<boolean>;
34
29
  /**
35
30
  * 将指定目录拼接到当前路径之后
36
31
  */
@@ -44,10 +39,6 @@ export declare class FilePath {
44
39
  * @param consumer
45
40
  */
46
41
  walkAllFile(consumer: (filePath: FilePath) => Promise<void> | void): Promise<void>;
47
- /**
48
- * 获取文件名
49
- */
50
- fileName(): string;
51
42
  /**
52
43
  * 获取上一级目录
53
44
  */
@@ -60,12 +51,6 @@ export declare class FilePath {
60
51
  mkdirs(): Promise<void>;
61
52
  /** 空目录 */
62
53
  static EMPTY: FilePath;
63
- /**
64
- * 从绝对路径获取 FilePath
65
- * @param absPath 绝对路径
66
- * @param compilation 编译期数据
67
- */
68
- static fromAbsPath(absPath: string, compilation: CompilationData): FilePath;
69
54
  /**
70
55
  * 构建网站根目录的 FilePath
71
56
  * @param path 绝对路径或相对路径(相对于项目根目录),以 `/` 开头或 `x:/` 开头判定为绝对路径
@@ -48,14 +48,6 @@ class FilePath {
48
48
  this.statCache = await fs_1.default.promises.stat(this.absPath);
49
49
  return this.statCache.isDirectory();
50
50
  }
51
- /**
52
- * 检查路径是否指向一个文件
53
- */
54
- async isFile() {
55
- if (!this.statCache)
56
- this.statCache = await fs_1.default.promises.stat(this.absPath);
57
- return this.statCache.isFile();
58
- }
59
51
  /**
60
52
  * 将指定目录拼接到当前路径之后
61
53
  */
@@ -99,12 +91,6 @@ class FilePath {
99
91
  }
100
92
  } while (queue.length);
101
93
  }
102
- /**
103
- * 获取文件名
104
- */
105
- fileName() {
106
- return path_1.default.posix.basename(this.absPath);
107
- }
108
94
  /**
109
95
  * 获取上一级目录
110
96
  */
@@ -130,25 +116,6 @@ class FilePath {
130
116
  await fs_1.default.promises.mkdir(parent.absPath, { recursive: true });
131
117
  }
132
118
  }
133
- /**
134
- * 从绝对路径获取 FilePath
135
- * @param absPath 绝对路径
136
- * @param compilation 编译期数据
137
- */
138
- static fromAbsPath(absPath, compilation) {
139
- absPath = path_1.default.posix.normalize(absPath);
140
- const projectRoot = compilation.compilationEnv.read('PROJECT_PATH');
141
- const publicRoot = compilation.compilationEnv.read('PUBLIC_PATH');
142
- if (projectRoot.absPath.startsWith(absPath)) {
143
- return projectRoot.join(absPath.substring(projectRoot.absPath.length));
144
- }
145
- else if (publicRoot.absPath.startsWith(absPath)) {
146
- return publicRoot.join(absPath.substring(publicRoot.absPath.length));
147
- }
148
- else {
149
- return new FilePath(absPath, null, null);
150
- }
151
- }
152
119
  /**
153
120
  * 构建网站根目录的 FilePath
154
121
  * @param path 绝对路径或相对路径(相对于项目根目录),以 `/` 开头或 `x:/` 开头判定为绝对路径
@@ -10,8 +10,6 @@ export declare class ResourcesScanner {
10
10
  constructor(compilation: CompilationData, oldTracker?: FileUpdateTracker | undefined);
11
11
  /** 扫描指定目录下的所有文件 */
12
12
  scanLocalFile(path: FilePath): Promise<FileUpdateTracker>;
13
- /** 扫描网络文件 */
14
- private scanNetworkFile;
15
13
  }
16
14
  /**
17
15
  * 文件更新监听器
@@ -26,11 +24,6 @@ export declare class FileUpdateTracker {
26
24
  constructor(compilation: CompilationData, oldTracker?: FileUpdateTracker | undefined);
27
25
  /** 更新一个文件的标识符 */
28
26
  update(uri: string, value: string | Set<string> | string[]): void;
29
- /**
30
- * 同步指定的稳定资源(同步时会连同同步其连接的稳定资源)
31
- * @return 直接或间接连接的一些需要扫描的资源
32
- */
33
- syncStable(uri: URL, value: string[], oldTracker: FileUpdateTracker): string[];
34
27
  /** 读取一个文件的标识符 */
35
28
  get(uri: string): string | string[] | undefined;
36
29
  /** 归一化 uri */
@@ -79,46 +79,8 @@ class ResourcesScanner {
79
79
  }, isCached);
80
80
  set.forEach(it => urls.add(it));
81
81
  });
82
- await this.scanNetworkFile(tracker, urls);
83
82
  return tracker;
84
83
  }
85
- /** 扫描网络文件 */
86
- async scanNetworkFile(tracker, urls, record = new Set()) {
87
- const matchCacheRule = this.compilation.crossDep.read('matchCacheRule');
88
- const registry = this.compilation.fileParser;
89
- const isStable = this.compilation.compilationEnv.read('isStable');
90
- const appendedUrls = new Set();
91
- const taskList = new Array(urls.size);
92
- let i = 0;
93
- for (let url of urls) {
94
- const normalizeUri = tracker.normalizeUri(url);
95
- if (record.has(normalizeUri.href))
96
- continue;
97
- record.add(normalizeUri.href);
98
- const isCached = matchCacheRule.runOnNode(normalizeUri);
99
- if (isCached) {
100
- tracker.addUrl(normalizeUri.href);
101
- }
102
- if (isStable(normalizeUri)) {
103
- const oldValue = this.oldTracker?.get?.(normalizeUri.href);
104
- if (Array.isArray(oldValue)) {
105
- const list = tracker.syncStable(normalizeUri, oldValue, this.oldTracker);
106
- list.forEach(it => appendedUrls.add(it));
107
- continue;
108
- }
109
- }
110
- taskList[i++] = registry.parserUrlFile(normalizeUri.href, !!isCached)
111
- .then(value => {
112
- if (isCached) {
113
- tracker.update(value.file, value.mark);
114
- }
115
- value.urls.forEach(it => appendedUrls.add(it));
116
- }).catch(err => untils_1.utils.printError('SCAN NETWORK FILE', err));
117
- }
118
- await Promise.all(taskList);
119
- if (appendedUrls.size !== 0)
120
- await this.scanNetworkFile(tracker, appendedUrls, record);
121
- }
122
84
  }
123
85
  exports.ResourcesScanner = ResourcesScanner;
124
86
  /**
@@ -148,30 +110,6 @@ class FileUpdateTracker {
148
110
  this.map.set(uri, JSON.stringify(Array.from(value)));
149
111
  }
150
112
  }
151
- /**
152
- * 同步指定的稳定资源(同步时会连同同步其连接的稳定资源)
153
- * @return 直接或间接连接的一些需要扫描的资源
154
- */
155
- syncStable(uri, value, oldTracker) {
156
- const isStable = this.compilation.compilationEnv.read('isStable');
157
- this.update(uri.href, value);
158
- this.addUrl(uri.href);
159
- const result = [];
160
- for (let item of value) {
161
- this.addUrl(item);
162
- const itemUrl = new URL(item);
163
- if (isStable(itemUrl)) {
164
- const oldValue = oldTracker.get(item);
165
- if (Array.isArray(oldValue)) {
166
- const son = this.syncStable(itemUrl, oldValue, oldTracker);
167
- result.push(...son);
168
- continue;
169
- }
170
- }
171
- result.push(item);
172
- }
173
- return result;
174
- }
175
113
  /** 读取一个文件的标识符 */
176
114
  get(uri) {
177
115
  const value = this.map.get(this.normalizeUri(uri).href);
@@ -287,7 +225,7 @@ class FileUpdateTracker {
287
225
  let error;
288
226
  const result = await (async () => {
289
227
  try {
290
- const response = await fetcher.fetch(url);
228
+ const response = await fetcher(url);
291
229
  if (isNotFound.response(response)) {
292
230
  if (notFoundLevel == CompilationEnv_1.AllowNotFoundEnum.REJECT_ALL) {
293
231
  error = new untils_1.RuntimeException(untils_1.exceptionNames.notFound, `拉取 ${url} 时出现 404 错误`);
@@ -1,6 +1,5 @@
1
1
  import { FilePath } from '../FilePath';
2
2
  import { UpdateJson } from '../JsonBuilder';
3
- import { FiniteConcurrencyFetcher } from '../NetworkFileHandler';
4
3
  import { FileUpdateTracker } from '../ResourcesScanner';
5
4
  import { CompilationData } from '../SwCompiler';
6
5
  import { KeyValueDatabase } from './KeyValueDatabase';
@@ -62,7 +61,7 @@ declare function buildCommon(): {
62
61
  /**
63
62
  * 拉取网络文件
64
63
  */
65
- readonly NETWORK_FILE_FETCHER: import("./KeyValueDatabase").DatabaseValue<FiniteConcurrencyFetcher>;
64
+ readonly NETWORK_FILE_FETCHER: import("./KeyValueDatabase").DatabaseValue<(url: string | URL) => Promise<Response>>;
66
65
  /**
67
66
  * 判断文件是否是 404
68
67
  */
@@ -74,9 +73,5 @@ declare function buildCommon(): {
74
73
  * 是否允许 404
75
74
  */
76
75
  readonly ALLOW_NOT_FOUND: import("./KeyValueDatabase").DatabaseValue<AllowNotFoundEnum>;
77
- /**
78
- * 检查一个链接是否是稳定的(也就是 URL 不变其返回的结果永远不变)
79
- */
80
- readonly isStable: import("./KeyValueDatabase").DatabaseValue<(_url: URL) => boolean>;
81
76
  };
82
77
  export {};
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AllowNotFoundEnum = exports.CompilationEnv = void 0;
4
4
  const FilePath_1 = require("../FilePath");
5
- const NetworkFileHandler_1 = require("../NetworkFileHandler");
6
5
  const ResourcesScanner_1 = require("../ResourcesScanner");
7
6
  const untils_1 = require("../untils");
8
7
  const KeyValueDatabase_1 = require("./KeyValueDatabase");
@@ -117,7 +116,7 @@ function buildCommon() {
117
116
  try {
118
117
  const swppPath = (0, KeyValueDatabase_1.readThisValue)(this, 'swppPath');
119
118
  const versionPath = (0, KeyValueDatabase_1.readThisValue)(this, 'versionPath');
120
- const response = await fetcher.fetch(untils_1.utils.splicingUrl(baseUrl, swppPath, versionPath));
119
+ const response = await fetcher(untils_1.utils.splicingUrl(baseUrl, swppPath, versionPath));
121
120
  if (!isNotFound.response(response)) {
122
121
  if (isFetchSuccessful(response)) {
123
122
  const json = await response.json().catch(err => {
@@ -159,7 +158,7 @@ function buildCommon() {
159
158
  * 拉取网络文件
160
159
  */
161
160
  NETWORK_FILE_FETCHER: (0, KeyValueDatabase_1.buildEnv)({
162
- default: new NetworkFileHandler_1.FiniteConcurrencyFetcher()
161
+ default: (url) => fetch(url)
163
162
  }),
164
163
  /**
165
164
  * 判断文件是否是 404
@@ -186,11 +185,5 @@ function buildCommon() {
186
185
  }
187
186
  }
188
187
  }),
189
- /**
190
- * 检查一个链接是否是稳定的(也就是 URL 不变其返回的结果永远不变)
191
- */
192
- isStable: (0, KeyValueDatabase_1.buildEnv)({
193
- default: (_url) => false
194
- })
195
188
  };
196
189
  }
@@ -7,14 +7,6 @@ export declare class CompilationFileParser extends KeyValueDatabase<FileParser<c
7
7
  constructor();
8
8
  /** 解析本地文件 */
9
9
  parserLocalFile(path: FilePath, cb?: (content: crypto.BinaryLike) => void, force?: boolean): Promise<Set<string>>;
10
- /** 解析网络文件 */
11
- parserNetworkFile(response: Response, callback?: (content: crypto.BinaryLike) => Promise<void> | void): Promise<Set<string>>;
12
- /**
13
- * 解析指定的 URL
14
- * @param url 链接
15
- * @param isCached 该链接指向的资源是否需要缓存
16
- */
17
- parserUrlFile(url: string, isCached: boolean): Promise<FileMark>;
18
10
  /**
19
11
  * 解析指定类型的文件内容
20
12
  * @param type 文件类型
@@ -60,69 +60,6 @@ class CompilationFileParser extends KeyValueDatabase_1.KeyValueDatabase {
60
60
  return new Set();
61
61
  }
62
62
  }
63
- /** 解析网络文件 */
64
- async parserNetworkFile(response, callback) {
65
- const url = response.url;
66
- const fileHandler = this.compilation.compilationEnv.read('NETWORK_FILE_FETCHER');
67
- const contentType = fileHandler.getUrlContentType(url, response);
68
- if (this.hasKey(contentType)) {
69
- const parser = this.read(contentType);
70
- const content = await parser.readFromNetwork(this.compilation, response);
71
- if (callback)
72
- await callback(content);
73
- return await parser.extractUrls(this.compilation, content, new URL(url));
74
- }
75
- else {
76
- if (callback) {
77
- const buffer = await response.arrayBuffer();
78
- const array = new Uint8Array(buffer);
79
- callback(array);
80
- }
81
- return new Set();
82
- }
83
- }
84
- /**
85
- * 解析指定的 URL
86
- * @param url 链接
87
- * @param isCached 该链接指向的资源是否需要缓存
88
- */
89
- async parserUrlFile(url, isCached) {
90
- const fileHandler = this.compilation.compilationEnv.read('NETWORK_FILE_FETCHER');
91
- const contentType = fileHandler.getUrlContentType(url);
92
- if (!contentType && !isCached)
93
- return { file: url, mark: '', urls: new Set() };
94
- const parser = this.hasKey(contentType) ? this.read(contentType) : undefined;
95
- if (!parser && !isCached)
96
- return { file: url, mark: '', urls: new Set() };
97
- if (parser?.calcUrl) {
98
- const result = await parser.calcUrl(url);
99
- if (result)
100
- return {
101
- file: url,
102
- ...result
103
- };
104
- }
105
- const fetcher = this.compilation.compilationEnv.read('NETWORK_FILE_FETCHER');
106
- const urls = new Set();
107
- let mark = '';
108
- await fetcher.fetch(url)
109
- .then(response => this.parserNetworkFile(response, isCached ? content => {
110
- mark = untils_1.utils.calcHash(content);
111
- } : undefined))
112
- .then(urls => urls.forEach(it => urls.add(it)))
113
- .catch(err => new Response(JSON.stringify({
114
- type: err.type,
115
- message: err.message,
116
- stack: err.stack,
117
- addition: err
118
- }), {
119
- status: 599,
120
- headers: {
121
- 'Content-Type': 'application/json'
122
- }
123
- }));
124
- return { file: url, mark, urls };
125
- }
126
63
  /**
127
64
  * 解析指定类型的文件内容
128
65
  * @param type 文件类型
@@ -9,6 +9,8 @@ export declare class CrossEnv extends RuntimeKeyValueDatabase<any, COMMON_TYPE_C
9
9
  declare function buildCommon(): {
10
10
  /** 缓存库名称 */
11
11
  readonly CACHE_NAME: import("./KeyValueDatabase").DatabaseValue<string>;
12
+ /** 网站的基准域名 */
13
+ readonly BASE_URL: import("./KeyValueDatabase").DatabaseValue<string>;
12
14
  /** 永久缓存标记 */
13
15
  readonly INFINITE_CACHE: import("./KeyValueDatabase").DatabaseValue<symbol>;
14
16
  /** 存储版本号的 URL */
@@ -32,6 +32,18 @@ function buildCommon() {
32
32
  return {
33
33
  /** 缓存库名称 */
34
34
  CACHE_NAME: (0, KeyValueDatabase_1.buildEnv)({ default: 'kmarBlogCache' }),
35
+ /** 网站的基准域名 */
36
+ BASE_URL: (0, KeyValueDatabase_1.buildEnv)({
37
+ default: (0, ConfigCluster_1.defineLazyInitConfig)((_, compilation) => {
38
+ const url = compilation.compilationEnv.read('DOMAIN_HOST');
39
+ return url.href;
40
+ }),
41
+ checker(value) {
42
+ if (this.manual)
43
+ return { value, message: '不应当手动设置该项!' };
44
+ return false;
45
+ },
46
+ }),
35
47
  /** 永久缓存标记 */
36
48
  INFINITE_CACHE: (0, KeyValueDatabase_1.buildEnv)({
37
49
  default: Symbol(),
@@ -84,8 +84,14 @@ function buildCommon() {
84
84
  const headers = response.headers;
85
85
  if (headers.has(INVALID_KEY))
86
86
  return false;
87
- if (rule === INFINITE_CACHE)
88
- return true;
87
+ // 只有本站资源允许永久缓存
88
+ if (rule === INFINITE_CACHE) {
89
+ const url = response.url;
90
+ const baseLength = BASE_URL.length;
91
+ if (url.startsWith(BASE_URL) && (url.length === baseLength || url[baseLength] === '/')) {
92
+ return true;
93
+ }
94
+ }
89
95
  const storage = headers.get(STORAGE_TIMESTAMP);
90
96
  if (!storage)
91
97
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "3.0.0",
3
+ "version": "3.1.0-beta1",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",