@pisell/core 1.1.4 → 1.1.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.
@@ -81,10 +81,10 @@ declare class LoggerManager {
81
81
  private createFileName;
82
82
  /**
83
83
  * 创建AWS日志文件名
84
- * @param isManual 是否手动上传
84
+ * @param isManual 紧急上传
85
85
  * @returns 日志文件名
86
86
  */
87
- createAWSFileName(isManual?: boolean): Promise<any>;
87
+ createAWSFileName(urgent?: boolean): Promise<any>;
88
88
  /**
89
89
  * 创建日志文件
90
90
  * @param _fileName 文件名
@@ -94,7 +94,7 @@ declare class LoggerManager {
94
94
  /**
95
95
  * 存储日志到持久化存储
96
96
  */
97
- storeLog(): Promise<void>;
97
+ storeLog(urgent?: boolean): Promise<void>;
98
98
  private storeLogToIndexDB;
99
99
  /**
100
100
  * 清理旧日志,只保留最近指定天数的日志
@@ -159,12 +159,14 @@ var LoggerManager = /*#__PURE__*/function () {
159
159
  logId: dayjs().valueOf()
160
160
  });
161
161
  console.log("---- 行为日志", logItem);
162
+ this.logBuffer.push(logItem);
162
163
  if (log.type === "error") {
163
164
  this.sendFeishuNotification(_objectSpread(_objectSpread({}, logItem || {}), {}, {
164
165
  feishu: feishu
165
166
  }));
167
+ // error 级别日志需要立即存储
168
+ this.storeLog(true);
166
169
  }
167
- this.logBuffer.push(logItem);
168
170
  }
169
171
 
170
172
  /**
@@ -221,34 +223,34 @@ var LoggerManager = /*#__PURE__*/function () {
221
223
 
222
224
  /**
223
225
  * 创建AWS日志文件名
224
- * @param isManual 是否手动上传
226
+ * @param isManual 紧急上传
225
227
  * @returns 日志文件名
226
228
  */
227
229
  }, {
228
230
  key: "createAWSFileName",
229
231
  value: (function () {
230
- var _createAWSFileName = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(isManual) {
232
+ var _createAWSFileName = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(urgent) {
231
233
  var _this$app;
232
234
  var _date, _hour, _this$app2, fileName;
233
235
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
234
236
  while (1) switch (_context2.prev = _context2.next) {
235
237
  case 0:
236
238
  _date = dayjs().format("YYYY-MM-DD");
237
- _hour = dayjs().format("HH:mm"); // 自定义上传名称
239
+ _hour = dayjs().format("HH:mm"); // 紧急上传时,在小时后面加上当前时间戳 避免文件名重复覆盖
240
+ if (urgent) {
241
+ _hour = "".concat(dayjs().format("HH:mm"), " [ urgent ] ").concat(dayjs().valueOf());
242
+ }
238
243
  if (!((_this$app = this.app) !== null && _this$app !== void 0 && (_this$app = _this$app.getPlugin("aws")) !== null && _this$app !== void 0 && _this$app.createFileName)) {
239
- _context2.next = 4;
244
+ _context2.next = 5;
240
245
  break;
241
246
  }
242
247
  return _context2.abrupt("return", (_this$app2 = this.app) === null || _this$app2 === void 0 || (_this$app2 = _this$app2.getPlugin("aws")) === null || _this$app2 === void 0 ? void 0 : _this$app2.createFileName({
243
248
  date: _date,
244
249
  hour: _hour,
245
- isManual: isManual
250
+ isManual: false
246
251
  }));
247
- case 4:
252
+ case 5:
248
253
  fileName = "logs/".concat("pisell", "/", _date, "/").concat(_hour);
249
- if (isManual) {
250
- fileName += "-manual";
251
- }
252
254
  return _context2.abrupt("return", "".concat(fileName, ".json"));
253
255
  case 7:
254
256
  case "end":
@@ -288,7 +290,7 @@ var LoggerManager = /*#__PURE__*/function () {
288
290
  }, {
289
291
  key: "storeLog",
290
292
  value: (function () {
291
- var _storeLog = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
293
+ var _storeLog = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(urgent) {
292
294
  var _this$logBuffer, fileName, buffer, logs;
293
295
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
294
296
  while (1) switch (_context3.prev = _context3.next) {
@@ -301,7 +303,7 @@ var LoggerManager = /*#__PURE__*/function () {
301
303
  case 2:
302
304
  _context3.prev = 2;
303
305
  _context3.next = 5;
304
- return this.createAWSFileName();
306
+ return this.createAWSFileName(urgent);
305
307
  case 5:
306
308
  fileName = _context3.sent;
307
309
  console.log("-------- 存储日志到AWS 开始", fileName);
@@ -346,7 +348,7 @@ var LoggerManager = /*#__PURE__*/function () {
346
348
  }
347
349
  }, _callee3, this, [[2, 15], [18, 23]]);
348
350
  }));
349
- function storeLog() {
351
+ function storeLog(_x2) {
350
352
  return _storeLog.apply(this, arguments);
351
353
  }
352
354
  return storeLog;
@@ -561,7 +563,7 @@ var LoggerManager = /*#__PURE__*/function () {
561
563
  }
562
564
  }, _callee7, this, [[2, 8]]);
563
565
  }));
564
- function getLogFile(_x2) {
566
+ function getLogFile(_x3) {
565
567
  return _getLogFile.apply(this, arguments);
566
568
  }
567
569
  return getLogFile;
@@ -611,7 +613,7 @@ var LoggerManager = /*#__PURE__*/function () {
611
613
  }
612
614
  }, _callee8, this, [[2, 13]]);
613
615
  }));
614
- function clearLogs(_x3) {
616
+ function clearLogs(_x4) {
615
617
  return _clearLogs.apply(this, arguments);
616
618
  }
617
619
  return clearLogs;
@@ -3,7 +3,7 @@ import App from "../app";
3
3
  * IndexDB 管理器模块
4
4
  *
5
5
  * 这个模块提供了对浏览器 IndexedDB 的简单封装,并在不支持 IndexedDB 的环境中
6
- * 自动降级使用 localStorage 作为备选存储方案。
6
+ * 自动降级使用内存存储作为备选存储方案。
7
7
  */
8
8
  /**
9
9
  * 数据库配置选项接口
@@ -30,10 +30,11 @@ export interface DBOptions {
30
30
  options?: IDBIndexParameters;
31
31
  }[];
32
32
  }[];
33
+ timeout?: number;
33
34
  }
34
35
  /**
35
36
  * IndexDB 管理器类
36
- * 提供对 IndexedDB 的简单封装,支持自动降级到 localStorage
37
+ * 提供对 IndexedDB 的简单封装,支持自动降级到内存存储
37
38
  * @class IndexDBManager
38
39
  */
39
40
  declare class IndexDBManager {
@@ -49,25 +50,34 @@ declare class IndexDBManager {
49
50
  private stores;
50
51
  private useIndexDB;
51
52
  private app;
53
+ private memoryStorage;
54
+ private timeout;
52
55
  /**
53
56
  * 创建 IndexDBManager 实例
54
57
  * @param {DBOptions} options - 数据库配置选项
55
58
  */
56
59
  constructor(app: App, options: DBOptions);
60
+ /**
61
+ * 超时包装器 - 为 Promise 添加超时控制
62
+ * @param {Promise<T>} promise - 需要包装的 Promise
63
+ * @param {string} operation - 操作名称(用于错误提示)
64
+ * @returns {Promise<T>} 带超时控制的 Promise
65
+ * @private
66
+ */
67
+ private withTimeout;
57
68
  /**
58
69
  * 初始化数据库连接
59
- * 如果环境不支持 IndexedDB,将自动使用 localStorage
70
+ * 如果环境不支持 IndexedDB,将自动使用内存存储
60
71
  * @returns {Promise<boolean>} 连接是否成功
61
72
  */
62
73
  connect(): Promise<boolean>;
63
74
  /**
64
- * 生成用于 localStorage 的存储键
75
+ * 获取内存存储中指定 store 的 Map
65
76
  * @param {string} storeName - 存储对象名称
66
- * @param {string|number} [key] - 可选的键值
67
- * @returns {string} 格式化的存储键
77
+ * @returns {Map<string | number, any>} 存储 Map
68
78
  * @private
69
79
  */
70
- private getStorageKey;
80
+ private getMemoryStore;
71
81
  /**
72
82
  * 添加数据到指定的存储对象
73
83
  * @param {string} storeName - 存储对象名称
@@ -169,9 +179,9 @@ declare class IndexDBManager {
169
179
  close(): void;
170
180
  /**
171
181
  * 获取当前使用的存储方式
172
- * @returns {'indexDB'|'localStorage'} 当前使用的存储方式
182
+ * @returns {'indexDB'|'memory'} 当前使用的存储方式
173
183
  */
174
- getCurrentStorage(): 'indexDB' | 'localStorage';
184
+ getCurrentStorage(): 'indexDB' | 'memory';
175
185
  }
176
186
  /**
177
187
  * 使用示例:
@@ -240,12 +250,18 @@ declare class IndexDBManager {
240
250
  * db.close();
241
251
  *
242
252
  * // 15. 检查当前使用的存储方式
243
- * const storageType = db.getCurrentStorage(); // 'indexDB' 或 'localStorage'
253
+ * const storageType = db.getCurrentStorage(); // 'indexDB' 或 'memory'
244
254
  *
245
255
  * // 性能优化建议:
246
256
  * // - 使用 exists() 而不是 get() 来检查数据是否存在
247
257
  * // - 为常用查询字段创建索引
248
258
  * // - 使用索引查询方法来提高查询效率
249
259
  * // - 批量操作时使用事务(可以考虑后续扩展)
260
+ *
261
+ * // 降级方案说明:
262
+ * // - 当浏览器不支持 IndexedDB 时,会自动使用内存存储 (Map) 作为降级方案
263
+ * // - 内存存储的数据在页面刷新后会丢失,仅适合临时数据存储
264
+ * // - 内存存储支持所有 IndexedDB API,包括索引查询
265
+ * // - 对于需要持久化的数据,请确保浏览器支持 IndexedDB
250
266
  */
251
267
  export default IndexDBManager;