@pisell/core 1.0.9 → 1.0.11

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.
@@ -1,3 +1,4 @@
1
+ var _excluded = ["feishu"];
1
2
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
3
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
3
4
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
@@ -8,6 +9,8 @@ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToAr
8
9
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
9
10
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
10
11
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
12
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
13
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
11
14
  function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
12
15
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
13
16
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
@@ -27,6 +30,7 @@ import { sendWarningLog } from "./feishu";
27
30
  * 日志管理器类
28
31
  */
29
32
  var LoggerManager = /*#__PURE__*/function () {
33
+ // 日志管理器状态
30
34
  /**
31
35
  * 构造函数
32
36
  * @param prefix 日志前缀
@@ -51,6 +55,7 @@ var LoggerManager = /*#__PURE__*/function () {
51
55
  _defineProperty(this, "retentionDays", void 0);
52
56
  // 日志保留天数
53
57
  _defineProperty(this, "metadataFunction", void 0);
58
+ _defineProperty(this, "status", "running");
54
59
  this.prefix = (options === null || options === void 0 ? void 0 : options.prefix) || "app";
55
60
  this.checkInterval = (options === null || options === void 0 ? void 0 : options.checkInterval) || 5 * 60 * 1000;
56
61
  this.app = app;
@@ -61,6 +66,7 @@ var LoggerManager = /*#__PURE__*/function () {
61
66
  _createClass(LoggerManager, [{
62
67
  key: "init",
63
68
  value: function init() {
69
+ this.setStatus("running");
64
70
  this.initTimer();
65
71
  // 定期清理旧日志
66
72
  this.cleanupOldLogs();
@@ -119,6 +125,19 @@ var LoggerManager = /*#__PURE__*/function () {
119
125
  }
120
126
  }, this.checkInterval);
121
127
  }
128
+ }, {
129
+ key: "setStatus",
130
+ value: function setStatus(status) {
131
+ this.status = status;
132
+ }
133
+ }, {
134
+ key: "stop",
135
+ value: function stop() {
136
+ this.setStatus("stop");
137
+ if (this.timer) {
138
+ clearInterval(this.timer);
139
+ }
140
+ }
122
141
 
123
142
  /**
124
143
  * 添加日志
@@ -127,13 +146,23 @@ var LoggerManager = /*#__PURE__*/function () {
127
146
  }, {
128
147
  key: "addLog",
129
148
  value: function addLog(log) {
130
- var logItem = _objectSpread(_objectSpread({}, log), {}, {
149
+ if (this.status === "stop") {
150
+ return;
151
+ }
152
+ var _ref = log || {},
153
+ feishu = _ref.feishu,
154
+ restLog = _objectWithoutProperties(_ref, _excluded);
155
+ var logItem = _objectSpread(_objectSpread({}, restLog), {}, {
131
156
  date: log.date || dayjs().format("YYYY-MM-DD HH:mm:ss"),
132
- metadata: JSON.stringify(log.metadata || "{}")
157
+ metadata: JSON.stringify(log.metadata || {}),
158
+ // 以时间戳生成logId
159
+ logId: dayjs().valueOf()
133
160
  });
134
161
  console.log("---- 行为日志", logItem);
135
162
  if (log.type === "error") {
136
- this.sendFeishuNotification(logItem);
163
+ this.sendFeishuNotification(_objectSpread(_objectSpread({}, logItem || {}), {}, {
164
+ feishu: feishu
165
+ }));
137
166
  }
138
167
  this.logBuffer.push(logItem);
139
168
  }
@@ -148,21 +177,30 @@ var LoggerManager = /*#__PURE__*/function () {
148
177
  if (this.feishuConfig) {
149
178
  var _this$metadataFunctio;
150
179
  var _metadata = ((_this$metadataFunctio = this.metadataFunction) === null || _this$metadataFunctio === void 0 ? void 0 : _this$metadataFunctio.call(this)) || {};
180
+ var content = [{
181
+ key: "日志类型",
182
+ value: log.type
183
+ }, {
184
+ key: "日志时间",
185
+ value: log.date || dayjs().format("YYYY-MM-DD HH:mm:ss")
186
+ }, {
187
+ key: "日志来源",
188
+ value: JSON.stringify(_objectSpread(_objectSpread({}, this.metadata || {}), _metadata))
189
+ }];
190
+ if (log.feishu) {
191
+ content.push({
192
+ key: "日志内容",
193
+ value: JSON.stringify(log.feishu || {})
194
+ });
195
+ } else {
196
+ content.push({
197
+ key: "日志内容",
198
+ value: JSON.stringify(log.metadata || {})
199
+ });
200
+ }
151
201
  sendWarningLog({
152
202
  title: log.title,
153
- content: [{
154
- key: "日志类型",
155
- value: log.type
156
- }, {
157
- key: "日志时间",
158
- value: log.date || dayjs().format("YYYY-MM-DD HH:mm:ss")
159
- }, {
160
- key: "日志来源",
161
- value: JSON.stringify(_objectSpread(_objectSpread({}, this.metadata || {}), _metadata))
162
- }, {
163
- key: "日志内容",
164
- value: JSON.stringify(log.metadata)
165
- }],
203
+ content: content,
166
204
  webhook: this.feishuConfig.webhook
167
205
  });
168
206
  console.log("-------- 发送飞书通知", log);
@@ -181,11 +219,54 @@ var LoggerManager = /*#__PURE__*/function () {
181
219
  return fileName;
182
220
  }
183
221
 
222
+ /**
223
+ * 创建AWS日志文件名
224
+ * @param isManual 是否手动上传
225
+ * @returns 日志文件名
226
+ */
227
+ }, {
228
+ key: "createAWSFileName",
229
+ value: (function () {
230
+ var _createAWSFileName = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(isManual) {
231
+ var _this$app;
232
+ var _date, _hour, _this$app2, fileName;
233
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
234
+ while (1) switch (_context2.prev = _context2.next) {
235
+ case 0:
236
+ _date = dayjs().format("YYYY-MM-DD");
237
+ _hour = dayjs().format("HH:mm"); // 自定义上传名称
238
+ if (!((_this$app = this.app) !== null && _this$app !== void 0 && (_this$app = _this$app.getPlugin("aws")) !== null && _this$app !== void 0 && _this$app.getFileName)) {
239
+ _context2.next = 4;
240
+ break;
241
+ }
242
+ 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.getFileName({
243
+ date: _date,
244
+ hour: _hour,
245
+ isManual: isManual
246
+ }));
247
+ case 4:
248
+ fileName = "logs/".concat("pisell", "/", _date, "/").concat(_hour);
249
+ if (isManual) {
250
+ fileName += "-manual";
251
+ }
252
+ return _context2.abrupt("return", "".concat(fileName, ".json"));
253
+ case 7:
254
+ case "end":
255
+ return _context2.stop();
256
+ }
257
+ }, _callee2, this);
258
+ }));
259
+ function createAWSFileName(_x) {
260
+ return _createAWSFileName.apply(this, arguments);
261
+ }
262
+ return createAWSFileName;
263
+ }()
184
264
  /**
185
265
  * 创建日志文件
186
266
  * @param _fileName 文件名
187
267
  * @returns 日志文件对象
188
268
  */
269
+ )
189
270
  }, {
190
271
  key: "createFile",
191
272
  value: function createFile(_fileName) {
@@ -207,85 +288,149 @@ var LoggerManager = /*#__PURE__*/function () {
207
288
  }, {
208
289
  key: "storeLog",
209
290
  value: (function () {
210
- var _storeLog = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
211
- var fileName, logFile;
212
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
213
- while (1) switch (_context2.prev = _context2.next) {
291
+ var _storeLog = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
292
+ var _this$logBuffer, fileName, buffer, logs;
293
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
294
+ while (1) switch (_context3.prev = _context3.next) {
214
295
  case 0:
215
296
  if (!(this.logBuffer.length === 0 || !this.db)) {
216
- _context2.next = 2;
297
+ _context3.next = 2;
217
298
  break;
218
299
  }
219
- return _context2.abrupt("return");
300
+ return _context3.abrupt("return");
220
301
  case 2:
302
+ _context3.prev = 2;
303
+ _context3.next = 5;
304
+ return this.createAWSFileName();
305
+ case 5:
306
+ fileName = _context3.sent;
307
+ console.log("-------- 存储日志到AWS 开始", fileName);
308
+
309
+ // 将buffer中的metadata转换为对象
310
+ buffer = (_this$logBuffer = this.logBuffer) === null || _this$logBuffer === void 0 ? void 0 : _this$logBuffer.map(function (item) {
311
+ item.metadata = JSON.parse(item.metadata || "{}");
312
+ return item;
313
+ });
314
+ logs = JSON.stringify(buffer, null, 2);
315
+ _context3.next = 11;
316
+ return this.app.aws.upload({
317
+ Bucket: "",
318
+ Key: fileName,
319
+ Body: logs
320
+ });
321
+ case 11:
322
+ console.log("-------- 存储日志到AWS 成功");
323
+ // 上传成功后需要清空缓冲区,避免重复上传
324
+ this.logBuffer = [];
325
+ _context3.next = 26;
326
+ break;
327
+ case 15:
328
+ _context3.prev = 15;
329
+ _context3.t0 = _context3["catch"](2);
330
+ console.error("存储日志上传AWS失败:", _context3.t0);
331
+
332
+ // 将日志存储到IndexDB
333
+ _context3.prev = 18;
334
+ _context3.next = 21;
335
+ return this.storeLogToIndexDB();
336
+ case 21:
337
+ _context3.next = 26;
338
+ break;
339
+ case 23:
340
+ _context3.prev = 23;
341
+ _context3.t1 = _context3["catch"](18);
342
+ console.error("存储日志到IndexDB也失败:", _context3.t1);
343
+ case 26:
344
+ case "end":
345
+ return _context3.stop();
346
+ }
347
+ }, _callee3, this, [[2, 15], [18, 23]]);
348
+ }));
349
+ function storeLog() {
350
+ return _storeLog.apply(this, arguments);
351
+ }
352
+ return storeLog;
353
+ }())
354
+ }, {
355
+ key: "storeLogToIndexDB",
356
+ value: function () {
357
+ var _storeLogToIndexDB = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
358
+ var _this$db, _this$db3, fileName, logFile, _this$db2;
359
+ return _regeneratorRuntime().wrap(function _callee4$(_context4) {
360
+ while (1) switch (_context4.prev = _context4.next) {
361
+ case 0:
362
+ _context4.prev = 0;
221
363
  fileName = this.createFileName();
222
- _context2.prev = 3;
223
- _context2.next = 6;
224
- return this.db.get("logs", fileName);
225
- case 6:
226
- logFile = _context2.sent;
364
+ console.log("storeLog", fileName);
365
+
366
+ // 获取现有日志文件
367
+ _context4.next = 5;
368
+ return (_this$db = this.db) === null || _this$db === void 0 ? void 0 : _this$db.get("logs", fileName);
369
+ case 5:
370
+ logFile = _context4.sent;
227
371
  if (logFile) {
228
- _context2.next = 11;
372
+ _context4.next = 10;
229
373
  break;
230
374
  }
231
375
  logFile = this.createFile(fileName);
232
- _context2.next = 11;
233
- return this.db.add("logs", logFile);
234
- case 11:
376
+ _context4.next = 10;
377
+ return (_this$db2 = this.db) === null || _this$db2 === void 0 ? void 0 : _this$db2.add("logs", logFile);
378
+ case 10:
235
379
  // 添加日志到文件内容
236
380
  logFile.fileContent.logs = [].concat(_toConsumableArray(logFile.fileContent.logs), _toConsumableArray(this.logBuffer));
237
381
 
238
382
  // 更新日志文件
239
- _context2.next = 14;
240
- return this.db.update("logs", logFile);
241
- case 14:
242
- console.log("-------- 存储日志", {
383
+ _context4.next = 13;
384
+ return (_this$db3 = this.db) === null || _this$db3 === void 0 ? void 0 : _this$db3.update("logs", logFile);
385
+ case 13:
386
+ console.log("-------- 存储日志到IndexDB", {
243
387
  fileName: fileName,
244
388
  logFile: logFile
245
389
  });
246
- _context2.next = 20;
390
+
391
+ // 存储成功后需要清空缓冲区,避免重复存储
392
+ this.logBuffer = [];
393
+ _context4.next = 21;
247
394
  break;
248
395
  case 17:
249
- _context2.prev = 17;
250
- _context2.t0 = _context2["catch"](3);
251
- console.error("存储日志到IndexDB失败:", _context2.t0);
252
- case 20:
253
- // 清空缓冲区
254
- this.logBuffer = [];
396
+ _context4.prev = 17;
397
+ _context4.t0 = _context4["catch"](0);
398
+ console.log("-------- 存储日志到IndexDB 失败", _context4.t0);
399
+ // 重新抛出错误,让外层 catch 能够捕获
400
+ throw _context4.t0;
255
401
  case 21:
256
402
  case "end":
257
- return _context2.stop();
403
+ return _context4.stop();
258
404
  }
259
- }, _callee2, this, [[3, 17]]);
405
+ }, _callee4, this, [[0, 17]]);
260
406
  }));
261
- function storeLog() {
262
- return _storeLog.apply(this, arguments);
407
+ function storeLogToIndexDB() {
408
+ return _storeLogToIndexDB.apply(this, arguments);
263
409
  }
264
- return storeLog;
410
+ return storeLogToIndexDB;
265
411
  }()
266
412
  /**
267
413
  * 清理旧日志,只保留最近指定天数的日志
268
414
  */
269
- )
270
415
  }, {
271
416
  key: "cleanupOldLogs",
272
417
  value: (function () {
273
- var _cleanupOldLogs = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
418
+ var _cleanupOldLogs = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5() {
274
419
  var logFiles, cutoffDate, filesToDelete, _iterator, _step, file;
275
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
276
- while (1) switch (_context3.prev = _context3.next) {
420
+ return _regeneratorRuntime().wrap(function _callee5$(_context5) {
421
+ while (1) switch (_context5.prev = _context5.next) {
277
422
  case 0:
278
423
  if (this.db) {
279
- _context3.next = 2;
424
+ _context5.next = 2;
280
425
  break;
281
426
  }
282
- return _context3.abrupt("return");
427
+ return _context5.abrupt("return");
283
428
  case 2:
284
- _context3.prev = 2;
285
- _context3.next = 5;
429
+ _context5.prev = 2;
430
+ _context5.next = 5;
286
431
  return this.getLogFiles();
287
432
  case 5:
288
- logFiles = _context3.sent;
433
+ logFiles = _context5.sent;
289
434
  // 计算保留日志的截止日期
290
435
  cutoffDate = dayjs().subtract(this.retentionDays, "day").format("YYYY-MM-DD"); // 筛选出需要删除的日志文件
291
436
  filesToDelete = logFiles.filter(function (file) {
@@ -295,47 +440,47 @@ var LoggerManager = /*#__PURE__*/function () {
295
440
 
296
441
  // 删除旧日志文件
297
442
  _iterator = _createForOfIteratorHelper(filesToDelete);
298
- _context3.prev = 10;
443
+ _context5.prev = 10;
299
444
  _iterator.s();
300
445
  case 12:
301
446
  if ((_step = _iterator.n()).done) {
302
- _context3.next = 19;
447
+ _context5.next = 19;
303
448
  break;
304
449
  }
305
450
  file = _step.value;
306
- _context3.next = 16;
451
+ _context5.next = 16;
307
452
  return this.db.delete("logs", file.fileName);
308
453
  case 16:
309
454
  console.log("-------- \u5220\u9664\u65E7\u65E5\u5FD7\u6587\u4EF6: ".concat(file.fileName, ", \u65E5\u671F: ").concat(file.date));
310
455
  case 17:
311
- _context3.next = 12;
456
+ _context5.next = 12;
312
457
  break;
313
458
  case 19:
314
- _context3.next = 24;
459
+ _context5.next = 24;
315
460
  break;
316
461
  case 21:
317
- _context3.prev = 21;
318
- _context3.t0 = _context3["catch"](10);
319
- _iterator.e(_context3.t0);
462
+ _context5.prev = 21;
463
+ _context5.t0 = _context5["catch"](10);
464
+ _iterator.e(_context5.t0);
320
465
  case 24:
321
- _context3.prev = 24;
466
+ _context5.prev = 24;
322
467
  _iterator.f();
323
- return _context3.finish(24);
468
+ return _context5.finish(24);
324
469
  case 27:
325
470
  if (filesToDelete.length > 0) {
326
471
  console.log("-------- \u5171\u6E05\u7406 ".concat(filesToDelete.length, " \u4E2A\u65E7\u65E5\u5FD7\u6587\u4EF6"));
327
472
  }
328
- _context3.next = 33;
473
+ _context5.next = 33;
329
474
  break;
330
475
  case 30:
331
- _context3.prev = 30;
332
- _context3.t1 = _context3["catch"](2);
333
- console.error("清理旧日志失败:", _context3.t1);
476
+ _context5.prev = 30;
477
+ _context5.t1 = _context5["catch"](2);
478
+ console.error("清理旧日志失败:", _context5.t1);
334
479
  case 33:
335
480
  case "end":
336
- return _context3.stop();
481
+ return _context5.stop();
337
482
  }
338
- }, _callee3, this, [[2, 30], [10, 21, 24, 27]]);
483
+ }, _callee5, this, [[2, 30], [10, 21, 24, 27]]);
339
484
  }));
340
485
  function cleanupOldLogs() {
341
486
  return _cleanupOldLogs.apply(this, arguments);
@@ -350,31 +495,31 @@ var LoggerManager = /*#__PURE__*/function () {
350
495
  }, {
351
496
  key: "getLogFiles",
352
497
  value: (function () {
353
- var _getLogFiles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
354
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
355
- while (1) switch (_context4.prev = _context4.next) {
498
+ var _getLogFiles = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6() {
499
+ return _regeneratorRuntime().wrap(function _callee6$(_context6) {
500
+ while (1) switch (_context6.prev = _context6.next) {
356
501
  case 0:
357
502
  if (this.db) {
358
- _context4.next = 2;
503
+ _context6.next = 2;
359
504
  break;
360
505
  }
361
- return _context4.abrupt("return", []);
506
+ return _context6.abrupt("return", []);
362
507
  case 2:
363
- _context4.prev = 2;
364
- _context4.next = 5;
508
+ _context6.prev = 2;
509
+ _context6.next = 5;
365
510
  return this.db.getAll("logs");
366
511
  case 5:
367
- return _context4.abrupt("return", _context4.sent);
512
+ return _context6.abrupt("return", _context6.sent);
368
513
  case 8:
369
- _context4.prev = 8;
370
- _context4.t0 = _context4["catch"](2);
371
- console.error("获取日志文件列表失败:", _context4.t0);
372
- return _context4.abrupt("return", []);
514
+ _context6.prev = 8;
515
+ _context6.t0 = _context6["catch"](2);
516
+ console.error("获取日志文件列表失败:", _context6.t0);
517
+ return _context6.abrupt("return", []);
373
518
  case 12:
374
519
  case "end":
375
- return _context4.stop();
520
+ return _context6.stop();
376
521
  }
377
- }, _callee4, this, [[2, 8]]);
522
+ }, _callee6, this, [[2, 8]]);
378
523
  }));
379
524
  function getLogFiles() {
380
525
  return _getLogFiles.apply(this, arguments);
@@ -390,33 +535,33 @@ var LoggerManager = /*#__PURE__*/function () {
390
535
  }, {
391
536
  key: "getLogFile",
392
537
  value: (function () {
393
- var _getLogFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(fileName) {
394
- return _regeneratorRuntime().wrap(function _callee5$(_context5) {
395
- while (1) switch (_context5.prev = _context5.next) {
538
+ var _getLogFile = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(fileName) {
539
+ return _regeneratorRuntime().wrap(function _callee7$(_context7) {
540
+ while (1) switch (_context7.prev = _context7.next) {
396
541
  case 0:
397
542
  if (this.db) {
398
- _context5.next = 2;
543
+ _context7.next = 2;
399
544
  break;
400
545
  }
401
- return _context5.abrupt("return", null);
546
+ return _context7.abrupt("return", null);
402
547
  case 2:
403
- _context5.prev = 2;
404
- _context5.next = 5;
548
+ _context7.prev = 2;
549
+ _context7.next = 5;
405
550
  return this.db.get("logs", fileName);
406
551
  case 5:
407
- return _context5.abrupt("return", _context5.sent);
552
+ return _context7.abrupt("return", _context7.sent);
408
553
  case 8:
409
- _context5.prev = 8;
410
- _context5.t0 = _context5["catch"](2);
411
- console.error("获取日志文件内容失败:", _context5.t0);
412
- return _context5.abrupt("return", null);
554
+ _context7.prev = 8;
555
+ _context7.t0 = _context7["catch"](2);
556
+ console.error("获取日志文件内容失败:", _context7.t0);
557
+ return _context7.abrupt("return", null);
413
558
  case 12:
414
559
  case "end":
415
- return _context5.stop();
560
+ return _context7.stop();
416
561
  }
417
- }, _callee5, this, [[2, 8]]);
562
+ }, _callee7, this, [[2, 8]]);
418
563
  }));
419
- function getLogFile(_x) {
564
+ function getLogFile(_x2) {
420
565
  return _getLogFile.apply(this, arguments);
421
566
  }
422
567
  return getLogFile;
@@ -430,43 +575,43 @@ var LoggerManager = /*#__PURE__*/function () {
430
575
  }, {
431
576
  key: "clearLogs",
432
577
  value: (function () {
433
- var _clearLogs = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(fileName) {
434
- return _regeneratorRuntime().wrap(function _callee6$(_context6) {
435
- while (1) switch (_context6.prev = _context6.next) {
578
+ var _clearLogs = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee8(fileName) {
579
+ return _regeneratorRuntime().wrap(function _callee8$(_context8) {
580
+ while (1) switch (_context8.prev = _context8.next) {
436
581
  case 0:
437
582
  if (this.db) {
438
- _context6.next = 2;
583
+ _context8.next = 2;
439
584
  break;
440
585
  }
441
- return _context6.abrupt("return", false);
586
+ return _context8.abrupt("return", false);
442
587
  case 2:
443
- _context6.prev = 2;
588
+ _context8.prev = 2;
444
589
  if (!fileName) {
445
- _context6.next = 8;
590
+ _context8.next = 8;
446
591
  break;
447
592
  }
448
- _context6.next = 6;
593
+ _context8.next = 6;
449
594
  return this.db.delete("logs", fileName);
450
595
  case 6:
451
- _context6.next = 10;
596
+ _context8.next = 10;
452
597
  break;
453
598
  case 8:
454
- _context6.next = 10;
599
+ _context8.next = 10;
455
600
  return this.db.clear("logs");
456
601
  case 10:
457
- return _context6.abrupt("return", true);
602
+ return _context8.abrupt("return", true);
458
603
  case 13:
459
- _context6.prev = 13;
460
- _context6.t0 = _context6["catch"](2);
461
- console.error("清空日志失败:", _context6.t0);
462
- return _context6.abrupt("return", false);
604
+ _context8.prev = 13;
605
+ _context8.t0 = _context8["catch"](2);
606
+ console.error("清空日志失败:", _context8.t0);
607
+ return _context8.abrupt("return", false);
463
608
  case 17:
464
609
  case "end":
465
- return _context6.stop();
610
+ return _context8.stop();
466
611
  }
467
- }, _callee6, this, [[2, 13]]);
612
+ }, _callee8, this, [[2, 13]]);
468
613
  }));
469
- function clearLogs(_x2) {
614
+ function clearLogs(_x3) {
470
615
  return _clearLogs.apply(this, arguments);
471
616
  }
472
617
  return clearLogs;
@@ -493,17 +638,17 @@ var LoggerManager = /*#__PURE__*/function () {
493
638
  }, {
494
639
  key: "manualCleanup",
495
640
  value: (function () {
496
- var _manualCleanup = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7() {
497
- return _regeneratorRuntime().wrap(function _callee7$(_context7) {
498
- while (1) switch (_context7.prev = _context7.next) {
641
+ var _manualCleanup = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee9() {
642
+ return _regeneratorRuntime().wrap(function _callee9$(_context9) {
643
+ while (1) switch (_context9.prev = _context9.next) {
499
644
  case 0:
500
- _context7.next = 2;
645
+ _context9.next = 2;
501
646
  return this.cleanupOldLogs();
502
647
  case 2:
503
648
  case "end":
504
- return _context7.stop();
649
+ return _context9.stop();
505
650
  }
506
- }, _callee7, this);
651
+ }, _callee9, this);
507
652
  }));
508
653
  function manualCleanup() {
509
654
  return _manualCleanup.apply(this, arguments);
@@ -11,7 +11,7 @@ export interface SocketHistoryItem {
11
11
  /** 消息方向(发送/接收) */
12
12
  direction: 'in' | 'out';
13
13
  /** 消息内容 */
14
- message: SocketMessage | Partial<SocketMessage>;
14
+ message: SocketMessage;
15
15
  /** 消息大小(字节) */
16
16
  size?: number;
17
17
  }
@@ -291,7 +291,6 @@ export var SocketMonitor = /*#__PURE__*/function () {
291
291
  }, {
292
292
  key: "removeEventListeners",
293
293
  value: function removeEventListeners() {
294
- // @ts-ignore
295
294
  var _iterator = _createForOfIteratorHelper(this.eventHandlers.entries()),
296
295
  _step;
297
296
  try {
@@ -1,4 +1,4 @@
1
- import { ReconnectOptions } from './types';
1
+ import { ReconnectOptions } from "./types";
2
2
  /**
3
3
  * Socket重连管理器
4
4
  * 处理断线重连逻辑