@lynker-desktop/electron-sdk 0.0.9-alpha.7 → 0.0.9-alpha.72
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/README.md +160 -1
- package/common/index.d.ts +96 -0
- package/common/index.d.ts.map +1 -1
- package/common/index.js.map +1 -1
- package/esm/common/index.d.ts +96 -0
- package/esm/common/index.d.ts.map +1 -1
- package/esm/common/index.js.map +1 -1
- package/esm/main/clipboard.d.ts +32 -0
- package/esm/main/clipboard.d.ts.map +1 -0
- package/esm/main/clipboard.js +1208 -0
- package/esm/main/clipboard.js.map +1 -0
- package/esm/main/downloader.d.ts +212 -0
- package/esm/main/downloader.d.ts.map +1 -0
- package/esm/main/downloader.js +674 -0
- package/esm/main/downloader.js.map +1 -0
- package/esm/main/index.d.ts +20 -67
- package/esm/main/index.d.ts.map +1 -1
- package/esm/main/index.js +51 -202
- package/esm/main/index.js.map +1 -1
- package/esm/main/resource-cache.d.ts +245 -0
- package/esm/main/resource-cache.d.ts.map +1 -0
- package/esm/main/resource-cache.js +857 -0
- package/esm/main/resource-cache.js.map +1 -0
- package/esm/main/shortcut.d.ts +14 -0
- package/esm/main/shortcut.d.ts.map +1 -0
- package/esm/main/shortcut.js +173 -0
- package/esm/main/shortcut.js.map +1 -0
- package/esm/main/store.d.ts +10 -0
- package/esm/main/store.d.ts.map +1 -0
- package/esm/main/store.js +62 -0
- package/esm/main/store.js.map +1 -0
- package/esm/main/video-downloader.d.ts +39 -0
- package/esm/main/video-downloader.d.ts.map +1 -0
- package/esm/main/video-downloader.js +505 -0
- package/esm/main/video-downloader.js.map +1 -0
- package/esm/preload/index.js +19 -1
- package/esm/preload/index.js.map +1 -1
- package/esm/renderer/index.d.ts +8 -0
- package/esm/renderer/index.d.ts.map +1 -1
- package/esm/renderer/index.js +25 -0
- package/esm/renderer/index.js.map +1 -1
- package/main/clipboard.d.ts +32 -0
- package/main/clipboard.d.ts.map +1 -0
- package/main/clipboard.js +1208 -0
- package/main/clipboard.js.map +1 -0
- package/main/downloader.d.ts +212 -0
- package/main/downloader.d.ts.map +1 -0
- package/main/downloader.js +674 -0
- package/main/downloader.js.map +1 -0
- package/main/index.d.ts +20 -67
- package/main/index.d.ts.map +1 -1
- package/main/index.js +54 -205
- package/main/index.js.map +1 -1
- package/main/resource-cache.d.ts +245 -0
- package/main/resource-cache.d.ts.map +1 -0
- package/main/resource-cache.js +857 -0
- package/main/resource-cache.js.map +1 -0
- package/main/shortcut.d.ts +14 -0
- package/main/shortcut.d.ts.map +1 -0
- package/main/shortcut.js +173 -0
- package/main/shortcut.js.map +1 -0
- package/main/store.d.ts +10 -0
- package/main/store.d.ts.map +1 -0
- package/main/store.js +64 -0
- package/main/store.js.map +1 -0
- package/main/video-downloader.d.ts +39 -0
- package/main/video-downloader.d.ts.map +1 -0
- package/main/video-downloader.js +510 -0
- package/main/video-downloader.js.map +1 -0
- package/package.json +9 -5
- package/preload/index.js +19 -1
- package/preload/index.js.map +1 -1
- package/renderer/index.d.ts +8 -0
- package/renderer/index.d.ts.map +1 -1
- package/renderer/index.js +25 -0
- package/renderer/index.js.map +1 -1
|
@@ -0,0 +1,857 @@
|
|
|
1
|
+
const fs = require('node:fs');
|
|
2
|
+
const path = require('node:path');
|
|
3
|
+
const md5 = require('md5');
|
|
4
|
+
const mime = require('mime-types');
|
|
5
|
+
const electron = require('electron');
|
|
6
|
+
const nodeDownloaderHelper = require('node-downloader-helper');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 默认文件扩展名
|
|
10
|
+
*/
|
|
11
|
+
const DEFAULT_EXT = '.res';
|
|
12
|
+
/**
|
|
13
|
+
* 默认 MIME 类型
|
|
14
|
+
*/
|
|
15
|
+
const DEFAULT_MIME_TYPE = 'application/octet-stream';
|
|
16
|
+
/**
|
|
17
|
+
* 最大重定向次数
|
|
18
|
+
*/
|
|
19
|
+
const MAX_REDIRECTS = 5;
|
|
20
|
+
/**
|
|
21
|
+
* 默认配置
|
|
22
|
+
*/
|
|
23
|
+
const DEFAULT_OPTIONS = {
|
|
24
|
+
cacheDir: '',
|
|
25
|
+
cacheTTL: 24 * 60 * 60 * 1000,
|
|
26
|
+
// 图片格式:png, jpg/jpeg, webp, gif, svg, ico, bmp, avif, heic, heif, tiff, tif
|
|
27
|
+
// 字体格式:woff, woff2, ttf, eot, otf
|
|
28
|
+
// 视频格式:mp4, webm, ogg, mov, avi, mkv, flv, m4v, 3gp
|
|
29
|
+
// 音频格式:mp3, wav, aac, m4a, flac, opus, wma
|
|
30
|
+
// 样式和脚本:css, js, json, xml, txt
|
|
31
|
+
// Web资源:wasm, map (source map)
|
|
32
|
+
// 文档格式:pdf
|
|
33
|
+
// 压缩文件:zip, 7z, rar, tar, gz, bz2
|
|
34
|
+
match: /\.(png|jpe?g|webp|gif|svg|ico|bmp|avif|heic|heif|tiff?|woff2?|ttf|eot|otf|mp4|webm|ogg|mov|avi|mkv|flv|m4v|3gp|mp3|wav|aac|m4a|flac|opus|wma|css|js|json|xml|txt|wasm|map|pdf|zip|7z|rar|tar|gz|bz2)(\?.*)?$/i,
|
|
35
|
+
allowedOrigins: null,
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* 资源缓存类:拦截并缓存静态资源,提升加载性能
|
|
39
|
+
*/
|
|
40
|
+
class ResourceCache {
|
|
41
|
+
/**
|
|
42
|
+
* 构造函数
|
|
43
|
+
* @param session Electron session
|
|
44
|
+
* @param options 缓存配置
|
|
45
|
+
*/
|
|
46
|
+
constructor(session, options) {
|
|
47
|
+
this.cacheHost = `${ResourceCache.scheme}://-`;
|
|
48
|
+
/** 正在下载的 URL 和对应的下载信息(避免重复下载,允许多个请求等待同一个下载任务) */
|
|
49
|
+
this._downloadingUrls = new Map();
|
|
50
|
+
if (!session)
|
|
51
|
+
throw new Error('ResourceCache: session is required');
|
|
52
|
+
this.session = session;
|
|
53
|
+
// 合并配置,保证类型安全
|
|
54
|
+
this.options = {
|
|
55
|
+
...DEFAULT_OPTIONS,
|
|
56
|
+
...options,
|
|
57
|
+
cacheDir: options.cacheDir,
|
|
58
|
+
cacheTTL: options.cacheTTL ?? DEFAULT_OPTIONS.cacheTTL,
|
|
59
|
+
match: options.match ?? DEFAULT_OPTIONS.match,
|
|
60
|
+
allowedOrigins: options.allowedOrigins ?? DEFAULT_OPTIONS.allowedOrigins,
|
|
61
|
+
};
|
|
62
|
+
if (!this.options.cacheDir) {
|
|
63
|
+
throw new Error('ResourceCache: cacheDir is required');
|
|
64
|
+
}
|
|
65
|
+
// 确保缓存目录存在
|
|
66
|
+
if (!fs.existsSync(this.options.cacheDir)) {
|
|
67
|
+
fs.mkdirSync(this.options.cacheDir, { recursive: true });
|
|
68
|
+
}
|
|
69
|
+
this._registerInterceptor();
|
|
70
|
+
// 异步清理过期缓存,不阻塞初始化
|
|
71
|
+
this._cleanOldCache().catch(err => {
|
|
72
|
+
console.log('初始化时清理过期缓存失败:', err);
|
|
73
|
+
});
|
|
74
|
+
electron.ipcMain.handle('core:cache', async (event, options) => {
|
|
75
|
+
try {
|
|
76
|
+
switch (options.method) {
|
|
77
|
+
case 'clear':
|
|
78
|
+
return await this.clearCache();
|
|
79
|
+
case 'add': {
|
|
80
|
+
const { id } = options;
|
|
81
|
+
const sender = event.sender;
|
|
82
|
+
const urls = Array.isArray(options.urls) ? options.urls : options.urls ? [options.urls] : [];
|
|
83
|
+
const data = await this.addCacheUrls(urls, options.force ?? false, true, (data) => {
|
|
84
|
+
try {
|
|
85
|
+
if (sender) {
|
|
86
|
+
if (typeof sender?.send === 'function') {
|
|
87
|
+
sender?.send?.(`core:cache:progress`, {
|
|
88
|
+
id,
|
|
89
|
+
data
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
console.log('发送缓存进度回调失败:', error);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
return Array.isArray(options.urls) ? data : data[0];
|
|
99
|
+
}
|
|
100
|
+
case 'delete': {
|
|
101
|
+
const urls = Array.isArray(options.urls) ? options.urls : options.urls ? [options.urls] : [];
|
|
102
|
+
const data = await this.deleteCacheUrls(urls);
|
|
103
|
+
return Array.isArray(options.urls) ? data : data[0];
|
|
104
|
+
}
|
|
105
|
+
case 'get': {
|
|
106
|
+
const urls = Array.isArray(options.urls) ? options.urls : options.urls ? [options.urls] : [];
|
|
107
|
+
const data = await this.getCacheUrls(urls);
|
|
108
|
+
return Array.isArray(options.urls) ? data : data[0];
|
|
109
|
+
}
|
|
110
|
+
case 'stats':
|
|
111
|
+
return await this.getCacheStats();
|
|
112
|
+
default:
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
return { success: false, error: error instanceof Error ? error.message : '未知错误' };
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 获取缓存统计信息(异步版本,性能更好)
|
|
123
|
+
*/
|
|
124
|
+
async getCacheStats() {
|
|
125
|
+
try {
|
|
126
|
+
const files = await fs.promises.readdir(this.options.cacheDir);
|
|
127
|
+
if (files.length === 0) {
|
|
128
|
+
return { size: 0, totalSize: 0 };
|
|
129
|
+
}
|
|
130
|
+
// 并行获取文件信息,提升性能
|
|
131
|
+
const fileInfos = await Promise.allSettled(files.map(async (file) => {
|
|
132
|
+
const filePath = path.join(this.options.cacheDir, file);
|
|
133
|
+
try {
|
|
134
|
+
const stats = await fs.promises.stat(filePath);
|
|
135
|
+
return { file, filePath, size: stats.size };
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
return { file, filePath, size: 0, error };
|
|
139
|
+
}
|
|
140
|
+
}));
|
|
141
|
+
const validInfos = fileInfos
|
|
142
|
+
.filter((r) => r.status === 'fulfilled')
|
|
143
|
+
.map(r => r.value);
|
|
144
|
+
return {
|
|
145
|
+
size: validInfos.length,
|
|
146
|
+
totalSize: validInfos.reduce((sum, info) => sum + info.size, 0)
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
console.log('获取缓存统计信息失败:', error);
|
|
151
|
+
return { size: 0, totalSize: 0 };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* 获取资源匹配函数(带缓存,避免重复创建)
|
|
156
|
+
*/
|
|
157
|
+
_getMatchFunction() {
|
|
158
|
+
if (this._cachedMatchFunction) {
|
|
159
|
+
return this._cachedMatchFunction;
|
|
160
|
+
}
|
|
161
|
+
const matcher = this.options.match;
|
|
162
|
+
if (typeof matcher === 'function') {
|
|
163
|
+
this._cachedMatchFunction = matcher;
|
|
164
|
+
}
|
|
165
|
+
else if (matcher instanceof RegExp) {
|
|
166
|
+
this._cachedMatchFunction = (url) => matcher.test(url);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
this._cachedMatchFunction = () => false;
|
|
170
|
+
}
|
|
171
|
+
return this._cachedMatchFunction;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 获取来源校验函数(带缓存,避免重复创建)
|
|
175
|
+
*/
|
|
176
|
+
_getOriginAllowFunction() {
|
|
177
|
+
if (this._cachedOriginFunction) {
|
|
178
|
+
return this._cachedOriginFunction;
|
|
179
|
+
}
|
|
180
|
+
const origins = this.options.allowedOrigins;
|
|
181
|
+
if (!origins) {
|
|
182
|
+
this._cachedOriginFunction = () => true;
|
|
183
|
+
}
|
|
184
|
+
else if (typeof origins === 'function') {
|
|
185
|
+
this._cachedOriginFunction = origins;
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
const prefixList = origins.map(o => o.toLowerCase());
|
|
189
|
+
this._cachedOriginFunction = (url) => {
|
|
190
|
+
try {
|
|
191
|
+
const origin = new URL(url).origin.toLowerCase();
|
|
192
|
+
return prefixList.some(prefix => origin.startsWith(prefix));
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
return this._cachedOriginFunction;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* 获取缓存文件路径
|
|
203
|
+
* @param url 资源URL
|
|
204
|
+
* @param customExt 自定义文件扩展名(可选,用于 base64 URL)
|
|
205
|
+
*/
|
|
206
|
+
getCachedPath(url, customExt) {
|
|
207
|
+
const md5Str = md5(url);
|
|
208
|
+
let ext = '.res';
|
|
209
|
+
if (customExt) {
|
|
210
|
+
// 如果提供了自定义扩展名,使用它
|
|
211
|
+
ext = customExt.startsWith('.') ? customExt : `.${customExt}`;
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
// 尝试从 URL 中提取扩展名
|
|
215
|
+
try {
|
|
216
|
+
const urlObj = new URL(url);
|
|
217
|
+
ext = path.extname(urlObj.pathname) || DEFAULT_EXT;
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
// 如果 URL 解析失败(可能是 base64 data URL),使用默认扩展名
|
|
221
|
+
ext = DEFAULT_EXT;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
filePath: path.join(this.options.cacheDir, `${md5Str}${ext}`),
|
|
226
|
+
hostPath: `${this.cacheHost}/${md5Str}${ext}`,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* 判断缓存是否有效(同步版本,用于拦截器)
|
|
231
|
+
* @param filePath 缓存文件路径
|
|
232
|
+
*/
|
|
233
|
+
isCacheValid(filePath) {
|
|
234
|
+
try {
|
|
235
|
+
if (!fs.existsSync(filePath))
|
|
236
|
+
return false;
|
|
237
|
+
const stat = fs.statSync(filePath);
|
|
238
|
+
return Date.now() - stat.mtimeMs < this.options.cacheTTL;
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* 判断缓存是否有效(异步版本,性能更好)
|
|
246
|
+
* @param filePath 缓存文件路径
|
|
247
|
+
*/
|
|
248
|
+
async isCacheValidAsync(filePath) {
|
|
249
|
+
try {
|
|
250
|
+
const stat = await fs.promises.stat(filePath);
|
|
251
|
+
return Date.now() - stat.mtimeMs < this.options.cacheTTL;
|
|
252
|
+
}
|
|
253
|
+
catch {
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* 下载资源到本地缓存(异步版本,返回 Promise)
|
|
259
|
+
* @param url 资源URL
|
|
260
|
+
* @param filePath 本地缓存路径
|
|
261
|
+
* @param redirectCount 当前重定向次数(内部使用)
|
|
262
|
+
* @param onProgress 下载进度回调函数(可选)
|
|
263
|
+
* @returns Promise<void> 下载完成或失败
|
|
264
|
+
*/
|
|
265
|
+
downloadResourceAsync(url, filePath, redirectCount = 0, onProgress) {
|
|
266
|
+
// 检查是否正在下载,如果正在下载则返回已存在的 Promise(允许多个请求等待同一个下载任务)
|
|
267
|
+
const existingDownload = this._downloadingUrls.get(url);
|
|
268
|
+
if (existingDownload) {
|
|
269
|
+
// 如果提供了新的进度回调,添加到回调列表中
|
|
270
|
+
if (onProgress) {
|
|
271
|
+
existingDownload.progressCallbacks.add(onProgress);
|
|
272
|
+
}
|
|
273
|
+
return existingDownload.promise;
|
|
274
|
+
}
|
|
275
|
+
// 检查重定向次数限制
|
|
276
|
+
if (redirectCount >= MAX_REDIRECTS) {
|
|
277
|
+
return Promise.reject(new Error(`重定向次数超过限制 (${MAX_REDIRECTS}): ${url}`));
|
|
278
|
+
}
|
|
279
|
+
// 创建新的下载 Promise
|
|
280
|
+
let downloader;
|
|
281
|
+
// 创建进度回调集合(在 Promise 外部,以便后续可以访问)
|
|
282
|
+
const progressCallbacks = new Set();
|
|
283
|
+
if (onProgress) {
|
|
284
|
+
progressCallbacks.add(onProgress);
|
|
285
|
+
}
|
|
286
|
+
const downloadPromise = new Promise((resolve, reject) => {
|
|
287
|
+
const tempFilePath = `${filePath}.cache`;
|
|
288
|
+
const downloadDir = path.dirname(tempFilePath);
|
|
289
|
+
const downloadFileName = path.basename(tempFilePath);
|
|
290
|
+
// 确保目录存在
|
|
291
|
+
if (!fs.existsSync(downloadDir)) {
|
|
292
|
+
fs.mkdirSync(downloadDir, { recursive: true });
|
|
293
|
+
}
|
|
294
|
+
// 创建下载器实例
|
|
295
|
+
const dl = new nodeDownloaderHelper.DownloaderHelper(url, downloadDir, {
|
|
296
|
+
fileName: downloadFileName,
|
|
297
|
+
retry: { maxRetries: 3, delay: 1000 },
|
|
298
|
+
// 超时设置:10 分钟(600000 毫秒)
|
|
299
|
+
// 对于视频等大文件,需要更长的超时时间
|
|
300
|
+
// 如果 10 分钟内没有任何数据传输,才会超时
|
|
301
|
+
timeout: 600000,
|
|
302
|
+
override: true, // 覆盖已存在的文件
|
|
303
|
+
httpRequestOptions: {
|
|
304
|
+
// 允许重定向
|
|
305
|
+
followRedirect: true,
|
|
306
|
+
maxRedirects: MAX_REDIRECTS
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
// 将下载器实例赋值给外部变量(Promise 构造函数是同步执行的)
|
|
310
|
+
downloader = dl;
|
|
311
|
+
// 监听下载进度 - 必须在 start() 之前设置
|
|
312
|
+
// 注意:我们需要在 start() 之前设置事件监听器,这样后续的请求可以正确绑定进度回调
|
|
313
|
+
dl.on('progress', (stats) => {
|
|
314
|
+
// 调用所有注册的进度回调
|
|
315
|
+
const progressData = {
|
|
316
|
+
url,
|
|
317
|
+
downloaded: stats.downloaded || 0,
|
|
318
|
+
total: stats.total || 0,
|
|
319
|
+
percentage: stats.progress !== undefined ? Math.round(stats.progress) : -1,
|
|
320
|
+
speed: stats.speed || 0
|
|
321
|
+
};
|
|
322
|
+
progressCallbacks.forEach((callback) => {
|
|
323
|
+
try {
|
|
324
|
+
callback(progressData);
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
// 忽略回调中的错误,避免影响下载
|
|
328
|
+
console.error('进度回调执行失败:', error);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
// 监听下载完成
|
|
333
|
+
dl.on('end', () => {
|
|
334
|
+
// 将临时文件重命名为最终文件
|
|
335
|
+
fs.rename(tempFilePath, filePath, (renameErr) => {
|
|
336
|
+
// 无论成功还是失败,都要从下载列表中删除
|
|
337
|
+
this._downloadingUrls.delete(url);
|
|
338
|
+
if (renameErr) {
|
|
339
|
+
reject(new Error(`缓存文件重命名失败 from ${tempFilePath} to ${filePath}: ${renameErr.message}`));
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
resolve();
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
// 监听下载错误
|
|
347
|
+
dl.on('error', (err) => {
|
|
348
|
+
// 从下载列表中删除
|
|
349
|
+
this._downloadingUrls.delete(url);
|
|
350
|
+
// 清理临时文件
|
|
351
|
+
fs.promises.unlink(tempFilePath).catch(() => {
|
|
352
|
+
// 忽略删除失败
|
|
353
|
+
});
|
|
354
|
+
reject(err instanceof Error ? err : new Error(`下载失败: ${url}`));
|
|
355
|
+
});
|
|
356
|
+
// 监听下载停止(取消)
|
|
357
|
+
dl.on('stop', () => {
|
|
358
|
+
// 从下载列表中删除
|
|
359
|
+
this._downloadingUrls.delete(url);
|
|
360
|
+
// 清理临时文件
|
|
361
|
+
fs.promises.unlink(tempFilePath).catch(() => {
|
|
362
|
+
// 忽略删除失败
|
|
363
|
+
});
|
|
364
|
+
reject(new Error(`下载已停止: ${url}`));
|
|
365
|
+
});
|
|
366
|
+
// 开始下载 - 必须在所有事件监听器设置完成后调用
|
|
367
|
+
dl.start().catch((err) => {
|
|
368
|
+
// 从下载列表中删除
|
|
369
|
+
this._downloadingUrls.delete(url);
|
|
370
|
+
reject(err instanceof Error ? err : new Error(`启动下载失败: ${url}`));
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
// 立即将 Promise、下载器实例和进度回调集合存储到 Map 中,供后续相同 URL 的请求复用
|
|
374
|
+
// 注意:必须在 start() 调用之前存储,以便后续请求可以绑定进度回调
|
|
375
|
+
// 由于 Promise 构造函数是同步执行的,此时 downloader 已经被赋值,事件监听器也已设置完成
|
|
376
|
+
this._downloadingUrls.set(url, {
|
|
377
|
+
promise: downloadPromise,
|
|
378
|
+
downloader: downloader,
|
|
379
|
+
progressCallbacks
|
|
380
|
+
});
|
|
381
|
+
return downloadPromise;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* 下载资源到本地缓存(同步版本,不返回 Promise,用于拦截器)
|
|
385
|
+
* @param url 资源URL
|
|
386
|
+
* @param filePath 本地缓存路径
|
|
387
|
+
*/
|
|
388
|
+
downloadResource(url, filePath) {
|
|
389
|
+
// 异步执行,不等待完成
|
|
390
|
+
// 如果正在下载,跳过(避免重复下载)
|
|
391
|
+
if (this._downloadingUrls.has(url)) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
// 调用 downloadResourceAsync,它会自动处理重复下载的情况
|
|
395
|
+
this.downloadResourceAsync(url, filePath).catch(() => {
|
|
396
|
+
// 静默处理错误,避免日志过多
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* 从文件扩展名获取 MIME 类型
|
|
401
|
+
* @param ext 文件扩展名(带或不带点)
|
|
402
|
+
* @returns MIME 类型
|
|
403
|
+
*/
|
|
404
|
+
_getMimeTypeFromExt(ext) {
|
|
405
|
+
const cleanExt = ext.replace(/^\./, '').toLowerCase();
|
|
406
|
+
const mimeType = mime.lookup(cleanExt);
|
|
407
|
+
return mimeType || DEFAULT_MIME_TYPE;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* 检测并处理 base64 data URL
|
|
411
|
+
* @param url 资源URL
|
|
412
|
+
* @returns 如果是 base64 URL,返回 true、文件扩展名、MIME 类型和数据;否则返回 false
|
|
413
|
+
*/
|
|
414
|
+
_isBase64DataUrl(url) {
|
|
415
|
+
if (!url.startsWith('data:')) {
|
|
416
|
+
return { isBase64: false };
|
|
417
|
+
}
|
|
418
|
+
try {
|
|
419
|
+
// 解析 data URL 格式:data:[<mediatype>][;base64],<data>
|
|
420
|
+
const commaIndex = url.indexOf(',');
|
|
421
|
+
if (commaIndex === -1) {
|
|
422
|
+
return { isBase64: false };
|
|
423
|
+
}
|
|
424
|
+
const header = url.substring(0, commaIndex);
|
|
425
|
+
const data = url.substring(commaIndex + 1);
|
|
426
|
+
// 检查是否包含 base64 标识
|
|
427
|
+
if (!header.includes('base64')) {
|
|
428
|
+
return { isBase64: false };
|
|
429
|
+
}
|
|
430
|
+
// 从 mediatype 中提取文件扩展名和 MIME 类型
|
|
431
|
+
// 例如:data:image/png;base64 -> png, image/png
|
|
432
|
+
// 例如:data:image/jpeg;base64 -> jpeg, image/jpeg
|
|
433
|
+
let ext = DEFAULT_EXT.replace(/^\./, '');
|
|
434
|
+
let mimeType = DEFAULT_MIME_TYPE;
|
|
435
|
+
const mimeMatch = header.match(/data:([^;]+)/);
|
|
436
|
+
if (mimeMatch && mimeMatch[1]) {
|
|
437
|
+
mimeType = mimeMatch[1];
|
|
438
|
+
// 使用 mime-types 包从 MIME 类型获取扩展名
|
|
439
|
+
const extension = mime.extension(mimeType);
|
|
440
|
+
if (extension) {
|
|
441
|
+
ext = extension;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return { isBase64: true, ext, mimeType, data };
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
return { isBase64: false };
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* 保存 base64 数据到文件
|
|
452
|
+
* @param base64Data base64 编码的数据
|
|
453
|
+
* @param filePath 目标文件路径
|
|
454
|
+
*/
|
|
455
|
+
async _saveBase64ToFile(base64Data, filePath) {
|
|
456
|
+
try {
|
|
457
|
+
// 解码 base64 数据
|
|
458
|
+
const buffer = Buffer.from(base64Data, 'base64');
|
|
459
|
+
// 使用 Promise 版本的 writeFile
|
|
460
|
+
// Buffer 继承自 Uint8Array,可以直接使用
|
|
461
|
+
await fs.promises.writeFile(filePath, buffer);
|
|
462
|
+
}
|
|
463
|
+
catch (error) {
|
|
464
|
+
throw new Error(`保存 base64 文件失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* 手动缓存指定 URL 的资源
|
|
469
|
+
* @param url 要缓存的资源 URL(支持普通 URL 和 base64 data URL)
|
|
470
|
+
* @param force 是否强制重新下载,即使缓存有效(默认 false)
|
|
471
|
+
* @param ignoreOrigin 是否忽略来源检查(默认 false)
|
|
472
|
+
* @param onDownloadProgress 下载进度回调函数(可选)
|
|
473
|
+
* @returns Promise<{ filePath: string, hostPath: string, mimeType: string, size: number }> 返回缓存文件路径、主机路径、MIME 类型和文件大小
|
|
474
|
+
* @throws 如果 URL 不匹配缓存规则或来源不允许,会抛出错误
|
|
475
|
+
*/
|
|
476
|
+
async cacheUrl(url, force = false, ignoreOrigin = false, onDownloadProgress) {
|
|
477
|
+
// 检查是否是 base64 data URL
|
|
478
|
+
const base64Info = this._isBase64DataUrl(url);
|
|
479
|
+
if (base64Info.isBase64) {
|
|
480
|
+
// 处理 base64 data URL
|
|
481
|
+
if (!base64Info.data || !base64Info.ext) {
|
|
482
|
+
throw new Error(`无效的 base64 data URL: ${url}`);
|
|
483
|
+
}
|
|
484
|
+
// 获取缓存路径(使用检测到的扩展名)
|
|
485
|
+
const cachePath = this.getCachedPath(url, base64Info.ext);
|
|
486
|
+
const mimeType = base64Info.mimeType || this._getMimeTypeFromExt(base64Info.ext);
|
|
487
|
+
// 如果缓存有效且不强制重新下载,直接返回
|
|
488
|
+
if (!force && await this.isCacheValidAsync(cachePath.filePath)) {
|
|
489
|
+
const stats = await fs.promises.stat(cachePath.filePath);
|
|
490
|
+
return {
|
|
491
|
+
...cachePath,
|
|
492
|
+
mimeType,
|
|
493
|
+
size: stats.size
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
// 保存 base64 数据到文件
|
|
497
|
+
await this._saveBase64ToFile(base64Info.data, cachePath.filePath);
|
|
498
|
+
// 获取文件大小
|
|
499
|
+
const stats = await fs.promises.stat(cachePath.filePath);
|
|
500
|
+
return {
|
|
501
|
+
...cachePath,
|
|
502
|
+
mimeType,
|
|
503
|
+
size: stats.size
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
// 处理普通 URL
|
|
507
|
+
const shouldCache = ignoreOrigin ? () => true : this._getMatchFunction();
|
|
508
|
+
const isAllowedOrigin = ignoreOrigin ? () => true : this._getOriginAllowFunction();
|
|
509
|
+
// 检查是否匹配缓存规则
|
|
510
|
+
if (!shouldCache(url)) {
|
|
511
|
+
throw new Error(`URL 不匹配缓存规则: ${url}`);
|
|
512
|
+
}
|
|
513
|
+
// 检查来源是否允许
|
|
514
|
+
if (!isAllowedOrigin(url)) {
|
|
515
|
+
throw new Error(`URL 来源不允许缓存: ${url}`);
|
|
516
|
+
}
|
|
517
|
+
// 获取缓存路径
|
|
518
|
+
const cachePath = this.getCachedPath(url);
|
|
519
|
+
// 从文件扩展名获取 MIME 类型
|
|
520
|
+
const ext = path.extname(cachePath.filePath).replace(/^\./, '') || DEFAULT_EXT.replace(/^\./, '');
|
|
521
|
+
const mimeType = this._getMimeTypeFromExt(ext);
|
|
522
|
+
// 如果缓存有效且不强制重新下载,直接返回
|
|
523
|
+
if (!force && await this.isCacheValidAsync(cachePath.filePath)) {
|
|
524
|
+
const stats = await fs.promises.stat(cachePath.filePath);
|
|
525
|
+
return {
|
|
526
|
+
...cachePath,
|
|
527
|
+
mimeType,
|
|
528
|
+
size: stats.size
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
// 下载资源
|
|
532
|
+
await this.downloadResourceAsync(url, cachePath.filePath, 0, onDownloadProgress);
|
|
533
|
+
// 获取文件大小
|
|
534
|
+
const stats = await fs.promises.stat(cachePath.filePath);
|
|
535
|
+
return {
|
|
536
|
+
...cachePath,
|
|
537
|
+
mimeType,
|
|
538
|
+
size: stats.size
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* 批量缓存多个 URL 的资源
|
|
543
|
+
* @param urls 要缓存的资源 URL 数组
|
|
544
|
+
* @param force 是否强制重新下载,即使缓存有效(默认 false)
|
|
545
|
+
* @param ignoreOrigin 是否忽略来源检查(默认 false)
|
|
546
|
+
* @param onProgress 进度回调函数(可选)
|
|
547
|
+
* @returns Promise<Array<{ url: string, success: boolean, filePath?: string, hostPath?: string, mimeType?: string, size?: number, error?: string }>> 返回每个 URL 的缓存结果
|
|
548
|
+
*/
|
|
549
|
+
async addCacheUrls(urls, force = false, ignoreOrigin = false, onProgress) {
|
|
550
|
+
const total = urls.length;
|
|
551
|
+
const results = [];
|
|
552
|
+
let completed = 0;
|
|
553
|
+
// 存储每个 URL 的下载进度
|
|
554
|
+
const downloadProgressMap = new Map();
|
|
555
|
+
// 进度回调的包装函数,确保线程安全
|
|
556
|
+
const reportProgress = (url, success, result) => {
|
|
557
|
+
if (onProgress) {
|
|
558
|
+
completed++;
|
|
559
|
+
const downloadProgress = downloadProgressMap.get(url);
|
|
560
|
+
onProgress({
|
|
561
|
+
current: completed,
|
|
562
|
+
total,
|
|
563
|
+
url,
|
|
564
|
+
success,
|
|
565
|
+
result,
|
|
566
|
+
percentage: Math.round((completed / total) * 100),
|
|
567
|
+
downloadProgress: downloadProgress ? {
|
|
568
|
+
downloaded: downloadProgress.downloaded,
|
|
569
|
+
total: downloadProgress.total,
|
|
570
|
+
percentage: downloadProgress.percentage,
|
|
571
|
+
speed: downloadProgress.speed
|
|
572
|
+
} : undefined
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
// 并行处理所有 URL,但跟踪每个的完成状态
|
|
577
|
+
const promises = urls.map(async (url, index) => {
|
|
578
|
+
// 为每个 URL 创建下载进度回调
|
|
579
|
+
const onDownloadProgress = onProgress
|
|
580
|
+
? (progress) => {
|
|
581
|
+
// 更新下载进度
|
|
582
|
+
downloadProgressMap.set(url, {
|
|
583
|
+
downloaded: progress.downloaded,
|
|
584
|
+
total: progress.total,
|
|
585
|
+
percentage: progress.percentage,
|
|
586
|
+
speed: progress.speed
|
|
587
|
+
});
|
|
588
|
+
// 实时报告下载进度(不增加 completed 计数)
|
|
589
|
+
if (onProgress) {
|
|
590
|
+
onProgress({
|
|
591
|
+
current: completed,
|
|
592
|
+
total,
|
|
593
|
+
url,
|
|
594
|
+
success: true, // 下载中视为进行中
|
|
595
|
+
result: undefined,
|
|
596
|
+
percentage: Math.round((completed / total) * 100),
|
|
597
|
+
downloadProgress: {
|
|
598
|
+
downloaded: progress.downloaded,
|
|
599
|
+
total: progress.total,
|
|
600
|
+
percentage: progress.percentage,
|
|
601
|
+
speed: progress.speed
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
: undefined;
|
|
607
|
+
try {
|
|
608
|
+
const result = await this.cacheUrl(url, force, ignoreOrigin, onDownloadProgress);
|
|
609
|
+
const item = {
|
|
610
|
+
url,
|
|
611
|
+
success: true,
|
|
612
|
+
filePath: result.filePath,
|
|
613
|
+
hostPath: result.hostPath,
|
|
614
|
+
mimeType: result.mimeType,
|
|
615
|
+
size: result.size
|
|
616
|
+
};
|
|
617
|
+
results[index] = item;
|
|
618
|
+
// 清除下载进度(已完成)
|
|
619
|
+
downloadProgressMap.delete(url);
|
|
620
|
+
// 调用进度回调
|
|
621
|
+
reportProgress(url, true, {
|
|
622
|
+
filePath: result.filePath,
|
|
623
|
+
hostPath: result.hostPath,
|
|
624
|
+
mimeType: result.mimeType,
|
|
625
|
+
size: result.size
|
|
626
|
+
});
|
|
627
|
+
return item;
|
|
628
|
+
}
|
|
629
|
+
catch (error) {
|
|
630
|
+
const item = {
|
|
631
|
+
url,
|
|
632
|
+
success: false,
|
|
633
|
+
error: error instanceof Error ? error.message : '未知错误'
|
|
634
|
+
};
|
|
635
|
+
results[index] = item;
|
|
636
|
+
// 清除下载进度(失败)
|
|
637
|
+
downloadProgressMap.delete(url);
|
|
638
|
+
// 调用进度回调
|
|
639
|
+
reportProgress(url, false, {
|
|
640
|
+
error: item.error
|
|
641
|
+
});
|
|
642
|
+
return item;
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
// 等待所有请求完成
|
|
646
|
+
await Promise.allSettled(promises);
|
|
647
|
+
return results;
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* 删除多个 URL 的资源
|
|
651
|
+
* @param urls 要删除的资源 URL 数组
|
|
652
|
+
* @returns Promise<Array<{ url: string, success: boolean, error?: string }>> 返回每个 URL 的删除结果
|
|
653
|
+
*/
|
|
654
|
+
async deleteCacheUrls(urls) {
|
|
655
|
+
const results = await Promise.allSettled(urls.map(url => this.deleteUrl(url)));
|
|
656
|
+
return results.map((result, index) => {
|
|
657
|
+
const url = urls[index] || '';
|
|
658
|
+
if (result.status === 'fulfilled') {
|
|
659
|
+
return { url, success: true };
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
return { url, success: false, error: result.reason?.message || '未知错误' };
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* 获取单个 URL 的缓存信息
|
|
668
|
+
* @param url 资源 URL
|
|
669
|
+
* @returns Promise<{ url: string, exists: boolean, valid?: boolean, filePath?: string, hostPath?: string, mimeType?: string, size?: number, error?: string }> 返回缓存信息
|
|
670
|
+
*/
|
|
671
|
+
async getCacheUrl(url) {
|
|
672
|
+
try {
|
|
673
|
+
const cachePath = this.getCachedPath(url);
|
|
674
|
+
const exists = fs.existsSync(cachePath.filePath);
|
|
675
|
+
if (!exists) {
|
|
676
|
+
return { url, exists: false };
|
|
677
|
+
}
|
|
678
|
+
// 检查缓存是否有效
|
|
679
|
+
const valid = await this.isCacheValidAsync(cachePath.filePath);
|
|
680
|
+
// 获取文件信息
|
|
681
|
+
const stats = await fs.promises.stat(cachePath.filePath);
|
|
682
|
+
// 从文件扩展名获取 MIME 类型
|
|
683
|
+
const ext = path.extname(cachePath.filePath).replace(/^\./, '') || DEFAULT_EXT.replace(/^\./, '');
|
|
684
|
+
const mimeType = this._getMimeTypeFromExt(ext);
|
|
685
|
+
return {
|
|
686
|
+
url,
|
|
687
|
+
exists: true,
|
|
688
|
+
valid,
|
|
689
|
+
filePath: cachePath.filePath,
|
|
690
|
+
hostPath: cachePath.hostPath,
|
|
691
|
+
mimeType,
|
|
692
|
+
size: stats.size
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
catch (error) {
|
|
696
|
+
return {
|
|
697
|
+
url,
|
|
698
|
+
exists: false,
|
|
699
|
+
error: error instanceof Error ? error.message : '未知错误'
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
* 获取多个 URL 的缓存信息
|
|
705
|
+
* @param urls 资源 URL 数组
|
|
706
|
+
* @returns Promise<Array<{ url: string, exists: boolean, valid?: boolean, filePath?: string, hostPath?: string, mimeType?: string, size?: number, error?: string }>> 返回每个 URL 的缓存信息
|
|
707
|
+
*/
|
|
708
|
+
async getCacheUrls(urls) {
|
|
709
|
+
const results = await Promise.allSettled(urls.map(url => this.getCacheUrl(url)));
|
|
710
|
+
return results.map((result, index) => {
|
|
711
|
+
const url = urls[index] || '';
|
|
712
|
+
if (result.status === 'fulfilled') {
|
|
713
|
+
return result.value;
|
|
714
|
+
}
|
|
715
|
+
else {
|
|
716
|
+
return {
|
|
717
|
+
url,
|
|
718
|
+
exists: false,
|
|
719
|
+
error: result.reason?.message || '未知错误'
|
|
720
|
+
};
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
/**
|
|
725
|
+
* 删除单个 URL 的资源(异步版本,性能更好)
|
|
726
|
+
* @param url 要删除的资源 URL
|
|
727
|
+
* @returns Promise<{ url: string, success: boolean, error?: string }> 返回删除结果
|
|
728
|
+
*/
|
|
729
|
+
async deleteUrl(url) {
|
|
730
|
+
try {
|
|
731
|
+
const cachePath = this.getCachedPath(url);
|
|
732
|
+
try {
|
|
733
|
+
await fs.promises.unlink(cachePath.filePath);
|
|
734
|
+
return { url, success: true };
|
|
735
|
+
}
|
|
736
|
+
catch (error) {
|
|
737
|
+
if (error.code === 'ENOENT') {
|
|
738
|
+
return { url, success: false, error: '文件不存在' };
|
|
739
|
+
}
|
|
740
|
+
throw error;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
catch (error) {
|
|
744
|
+
return {
|
|
745
|
+
url,
|
|
746
|
+
success: false,
|
|
747
|
+
error: error instanceof Error ? error.message : '未知错误'
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* 删除单个 URL 的资源(别名,保持向后兼容)
|
|
753
|
+
* @deprecated 使用 deleteUrl 代替
|
|
754
|
+
*/
|
|
755
|
+
async deleteCache(url) {
|
|
756
|
+
return this.deleteUrl(url);
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* 清理过期缓存文件(异步并行处理,性能更好)
|
|
760
|
+
*/
|
|
761
|
+
async _cleanOldCache() {
|
|
762
|
+
try {
|
|
763
|
+
const files = await fs.promises.readdir(this.options.cacheDir);
|
|
764
|
+
if (files.length === 0)
|
|
765
|
+
return;
|
|
766
|
+
const now = Date.now();
|
|
767
|
+
// 并行处理,提升性能
|
|
768
|
+
await Promise.allSettled(files.map(async (file) => {
|
|
769
|
+
const fullPath = path.join(this.options.cacheDir, file);
|
|
770
|
+
try {
|
|
771
|
+
const stat = await fs.promises.stat(fullPath);
|
|
772
|
+
if (now - stat.mtimeMs > this.options.cacheTTL) {
|
|
773
|
+
await fs.promises.unlink(fullPath);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
catch {
|
|
777
|
+
// 忽略单个文件异常
|
|
778
|
+
}
|
|
779
|
+
}));
|
|
780
|
+
}
|
|
781
|
+
catch (error) {
|
|
782
|
+
// 忽略目录读取错误
|
|
783
|
+
console.log('清理过期缓存时发生错误:', error);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* 清理所有缓存文件(完全异步版本,性能更好)
|
|
788
|
+
* @returns Promise<{ success: number, failed: number, totalSize: number }> 返回清理统计信息
|
|
789
|
+
*/
|
|
790
|
+
async clearCache() {
|
|
791
|
+
try {
|
|
792
|
+
const files = await fs.promises.readdir(this.options.cacheDir);
|
|
793
|
+
if (files.length === 0) {
|
|
794
|
+
return { success: 0, failed: 0, totalSize: 0 };
|
|
795
|
+
}
|
|
796
|
+
// 并行获取所有文件信息(避免并行时的竞态条件)
|
|
797
|
+
const fileInfos = await Promise.allSettled(files.map(async (file) => {
|
|
798
|
+
const filePath = path.join(this.options.cacheDir, file);
|
|
799
|
+
try {
|
|
800
|
+
const stats = await fs.promises.stat(filePath);
|
|
801
|
+
return { file, filePath, size: stats.size };
|
|
802
|
+
}
|
|
803
|
+
catch (error) {
|
|
804
|
+
return { file, filePath, size: 0, error };
|
|
805
|
+
}
|
|
806
|
+
}));
|
|
807
|
+
const validInfos = fileInfos
|
|
808
|
+
.filter((r) => r.status === 'fulfilled')
|
|
809
|
+
.map(r => r.value);
|
|
810
|
+
// 计算总大小
|
|
811
|
+
const totalSize = validInfos.reduce((sum, info) => sum + info.size, 0);
|
|
812
|
+
// 并行删除文件,提升性能
|
|
813
|
+
const deleteResults = await Promise.allSettled(validInfos.map(async (info) => {
|
|
814
|
+
try {
|
|
815
|
+
await fs.promises.unlink(info.filePath);
|
|
816
|
+
return { success: true, file: info.file };
|
|
817
|
+
}
|
|
818
|
+
catch (error) {
|
|
819
|
+
throw error;
|
|
820
|
+
}
|
|
821
|
+
}));
|
|
822
|
+
// 统计成功和失败数量
|
|
823
|
+
const success = deleteResults.filter(r => r.status === 'fulfilled').length;
|
|
824
|
+
const failed = deleteResults.filter(r => r.status === 'rejected').length;
|
|
825
|
+
return { success, failed, totalSize };
|
|
826
|
+
}
|
|
827
|
+
catch (error) {
|
|
828
|
+
console.log('清理缓存时发生错误:', error);
|
|
829
|
+
throw error;
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* 注册 Electron 请求拦截器,实现资源缓存
|
|
834
|
+
*/
|
|
835
|
+
_registerInterceptor() {
|
|
836
|
+
const shouldCache = this._getMatchFunction();
|
|
837
|
+
const isAllowedOrigin = this._getOriginAllowFunction();
|
|
838
|
+
this.session.webRequest.onBeforeRequest({ urls: ['http://*/*', 'https://*/*'] }, (details, callback) => {
|
|
839
|
+
const url = details.url;
|
|
840
|
+
// 不匹配或来源不允许,直接放行
|
|
841
|
+
if (details.method !== 'GET' || !shouldCache(url) || !isAllowedOrigin(url))
|
|
842
|
+
return callback({});
|
|
843
|
+
const cachePath = this.getCachedPath(url);
|
|
844
|
+
// 命中缓存,直接重定向到本地文件
|
|
845
|
+
if (this.isCacheValid(cachePath.filePath)) {
|
|
846
|
+
return callback({ redirectURL: cachePath.hostPath });
|
|
847
|
+
}
|
|
848
|
+
// 未命中则异步下载,当前请求正常放行
|
|
849
|
+
this.downloadResource(url, cachePath.filePath);
|
|
850
|
+
return callback({});
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
ResourceCache.scheme = 'cachefile';
|
|
855
|
+
|
|
856
|
+
exports.ResourceCache = ResourceCache;
|
|
857
|
+
//# sourceMappingURL=resource-cache.js.map
|