@pluve/logger-sdk 0.0.12 → 0.0.13

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.
Files changed (45) hide show
  1. package/dist/esm/capture/jsError.js +3 -24
  2. package/dist/esm/capture/promiseError.js +3 -24
  3. package/dist/esm/capture/wechatError.js +9 -31
  4. package/dist/esm/compress/compression.js +21 -44
  5. package/dist/esm/core/httpClient.js +53 -76
  6. package/dist/esm/core/loggerSDK.js +201 -234
  7. package/dist/esm/core/queueManager.js +58 -112
  8. package/dist/esm/core/retryManager.js +44 -70
  9. package/dist/esm/stack/stacktrace.js +8 -31
  10. package/dist/esm/transport/beaconTransport.js +20 -61
  11. package/dist/esm/transport/pixelImageTransport.js +54 -77
  12. package/dist/esm/transport/transportAdapter.js +13 -74
  13. package/dist/esm/transport/wechatTransport.js +53 -94
  14. package/dist/esm/utils/tools.js +12 -35
  15. package/package.json +1 -1
  16. package/dist/cjs/capture/jsError.js +0 -48
  17. package/dist/cjs/capture/promiseError.js +0 -49
  18. package/dist/cjs/capture/resourceError.js +0 -48
  19. package/dist/cjs/capture/wechatError.js +0 -95
  20. package/dist/cjs/compress/compression.js +0 -105
  21. package/dist/cjs/config/index.js +0 -55
  22. package/dist/cjs/core/fingerprint.js +0 -36
  23. package/dist/cjs/core/httpClient.js +0 -96
  24. package/dist/cjs/core/loggerSDK.js +0 -643
  25. package/dist/cjs/core/queueManager.js +0 -263
  26. package/dist/cjs/core/retryManager.js +0 -127
  27. package/dist/cjs/index.js +0 -34
  28. package/dist/cjs/stack/stacktrace.js +0 -48
  29. package/dist/cjs/transport/beaconTransport.js +0 -64
  30. package/dist/cjs/transport/pixelImageTransport.js +0 -99
  31. package/dist/cjs/transport/transport.js +0 -17
  32. package/dist/cjs/transport/transportAdapter.js +0 -73
  33. package/dist/cjs/transport/wechatTransport.js +0 -103
  34. package/dist/cjs/types/api.js +0 -17
  35. package/dist/cjs/types/env.js +0 -17
  36. package/dist/cjs/types/external.d.ts +0 -21
  37. package/dist/cjs/types/logEvent.js +0 -17
  38. package/dist/cjs/types/logEventLevel.js +0 -17
  39. package/dist/cjs/types/sdkOptions.js +0 -17
  40. package/dist/cjs/types/trackOptions.js +0 -17
  41. package/dist/cjs/utils/environment.js +0 -183
  42. package/dist/cjs/utils/session.js +0 -31
  43. package/dist/cjs/utils/tools.js +0 -82
  44. package/dist/cjs/utils/uuid.js +0 -35
  45. package/dist/umd/logger-sdk.min.js +0 -1
@@ -1,24 +1,3 @@
1
- var __async = (__this, __arguments, generator) => {
2
- return new Promise((resolve, reject) => {
3
- var fulfilled = (value) => {
4
- try {
5
- step(generator.next(value));
6
- } catch (e) {
7
- reject(e);
8
- }
9
- };
10
- var rejected = (value) => {
11
- try {
12
- step(generator.throw(value));
13
- } catch (e) {
14
- reject(e);
15
- }
16
- };
17
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
- step((generator = generator.apply(__this, __arguments)).next());
19
- });
20
- };
21
-
22
1
  // src/core/loggerSDK.ts
23
2
  import { registerJsErrorCapture } from "../capture/jsError";
24
3
  import { registerPromiseErrorCapture } from "../capture/promiseError";
@@ -196,65 +175,63 @@ var LoggerSDK = class {
196
175
  flattenEnvTags() {
197
176
  return Object.entries(this.envTags || {}).map(([k, v]) => `${k}=${v}`).join(",");
198
177
  }
199
- track(_0) {
200
- return __async(this, arguments, function* ({ message, error, traceId, logLevel = "INFO" }) {
201
- var _a, _b, _c;
202
- logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "track", { message, error, traceId, logLevel });
203
- if (this.closed)
204
- return;
205
- if (!this.initialized) {
206
- logDebug(!!((_b = this.opts) == null ? void 0 : _b.debug), "SDK not initialized, skip track");
207
- return;
208
- }
209
- if (!this.shouldSend(logLevel, "custom", getCurrentUrl(), traceId, message)) {
210
- logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "Event sampled out", { logLevel, traceId, message });
211
- return;
212
- }
213
- this.seq += 1;
214
- let errorLocation = "";
215
- let throwableStack = "";
216
- if (error instanceof Error) {
217
- try {
218
- const { location, throwable } = yield flattenStack(error);
219
- errorLocation = location || "";
220
- throwableStack = throwable || error.stack || "";
221
- } catch (e) {
222
- errorLocation = "";
223
- throwableStack = error.stack || "";
224
- }
178
+ async track({ message, error, traceId, logLevel = "INFO" }) {
179
+ var _a, _b, _c;
180
+ logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "track", { message, error, traceId, logLevel });
181
+ if (this.closed)
182
+ return;
183
+ if (!this.initialized) {
184
+ logDebug(!!((_b = this.opts) == null ? void 0 : _b.debug), "SDK not initialized, skip track");
185
+ return;
186
+ }
187
+ if (!this.shouldSend(logLevel, "custom", getCurrentUrl(), traceId, message)) {
188
+ logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "Event sampled out", { logLevel, traceId, message });
189
+ return;
190
+ }
191
+ this.seq += 1;
192
+ let errorLocation = "";
193
+ let throwableStack = "";
194
+ if (error instanceof Error) {
195
+ try {
196
+ const { location, throwable } = await flattenStack(error);
197
+ errorLocation = location || "";
198
+ throwableStack = throwable || error.stack || "";
199
+ } catch {
200
+ errorLocation = "";
201
+ throwableStack = error.stack || "";
225
202
  }
226
- const logEvent = {
227
- /** 日志 ID */
228
- logId: `${this.opts.appId}${uuid()}${now()}`,
229
- // UUID
230
- seq: this.seq,
231
- /** 应用标识 */
232
- appId: this.opts.appId || "unknown",
233
- /** 环境标识 */
234
- stage: this.opts.logStage,
235
- /** 日志级别:info/warn/error/fatal */
236
- level: logLevel,
237
- /** 可选:跟踪 ID(跨服务调用链跟踪) */
238
- traceId,
239
- /** 会话标识,同 sessionId */
240
- frontendId: this.sessionId,
241
- // sessionId
242
- /** 发生页面 URL */
243
- url: getCurrentUrl(),
244
- location: errorLocation,
245
- /** 异常信息 */
246
- message: `[message]: ${message}; [envTags]: ${this.flattenEnvTags()}; [t]: ${now()}`,
247
- /** 可选:堆栈信息(长字符串) */
248
- throwable: throwableStack,
249
- /** 可选:用户 ID(脱敏) */
250
- userId: this.opts.userId,
251
- /** 可选:店铺编码 */
252
- storeCode: this.opts.storeCode
253
- /** 可选的结构化额外信息 */
254
- // tags: this.envTags,
255
- };
256
- this.doTrack(logEvent);
257
- });
203
+ }
204
+ const logEvent = {
205
+ /** 日志 ID */
206
+ logId: `${this.opts.appId}${uuid()}${now()}`,
207
+ // UUID
208
+ seq: this.seq,
209
+ /** 应用标识 */
210
+ appId: this.opts.appId || "unknown",
211
+ /** 环境标识 */
212
+ stage: this.opts.logStage,
213
+ /** 日志级别:info/warn/error/fatal */
214
+ level: logLevel,
215
+ /** 可选:跟踪 ID(跨服务调用链跟踪) */
216
+ traceId,
217
+ /** 会话标识,同 sessionId */
218
+ frontendId: this.sessionId,
219
+ // sessionId
220
+ /** 发生页面 URL */
221
+ url: getCurrentUrl(),
222
+ location: errorLocation,
223
+ /** 异常信息 */
224
+ message: `[message]: ${message}; [envTags]: ${this.flattenEnvTags()}; [t]: ${now()}`,
225
+ /** 可选:堆栈信息(长字符串) */
226
+ throwable: throwableStack,
227
+ /** 可选:用户 ID(脱敏) */
228
+ userId: this.opts.userId,
229
+ /** 可选:店铺编码 */
230
+ storeCode: this.opts.storeCode
231
+ /** 可选的结构化额外信息 */
232
+ // tags: this.envTags,
233
+ };
234
+ this.doTrack(logEvent);
258
235
  }
259
236
  trackInner(error) {
260
237
  var _a, _b, _c, _d, _e, _f, _g, _h;
@@ -363,179 +340,169 @@ var LoggerSDK = class {
363
340
  /**
364
341
  * 记录事件
365
342
  */
366
- doTrack(logEvent) {
367
- return __async(this, null, function* () {
368
- var _a, _b, _c;
369
- logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "track", logEvent);
370
- if (this.opts.enableBatch && this.queueManager) {
371
- this.queueManager.enqueue(logEvent);
372
- if (this.queueManager.size() >= this.opts.batchSize) {
373
- logDebug(!!((_b = this.opts) == null ? void 0 : _b.debug), "Queue size reached batch size, flushing immediately");
374
- yield this.flush();
375
- }
376
- } else {
377
- try {
378
- yield this.sendEvent(logEvent);
379
- } catch (e) {
380
- logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "track failed", e);
381
- }
343
+ async doTrack(logEvent) {
344
+ var _a, _b, _c;
345
+ logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "track", logEvent);
346
+ if (this.opts.enableBatch && this.queueManager) {
347
+ this.queueManager.enqueue(logEvent);
348
+ if (this.queueManager.size() >= this.opts.batchSize) {
349
+ logDebug(!!((_b = this.opts) == null ? void 0 : _b.debug), "Queue size reached batch size, flushing immediately");
350
+ await this.flush();
351
+ }
352
+ } else {
353
+ try {
354
+ await this.sendEvent(logEvent);
355
+ } catch (e) {
356
+ logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "track failed", e);
382
357
  }
383
- });
358
+ }
384
359
  }
385
360
  /**
386
361
  * 手动刷新队列,立即上报所有待发送日志
387
362
  */
388
- flush() {
389
- return __async(this, null, function* () {
390
- var _a, _b, _c, _d;
391
- if (!this.queueManager || this.queueManager.size() === 0) {
392
- return;
393
- }
394
- if (this.isSending) {
395
- logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "Already sending, skip flush");
363
+ async flush() {
364
+ var _a, _b, _c, _d;
365
+ if (!this.queueManager || this.queueManager.size() === 0) {
366
+ return;
367
+ }
368
+ if (this.isSending) {
369
+ logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "Already sending, skip flush");
370
+ return;
371
+ }
372
+ this.isSending = true;
373
+ logDebug(!!((_b = this.opts) == null ? void 0 : _b.debug), `Flushing ${this.queueManager.size()} events`);
374
+ try {
375
+ const events = this.queueManager.peek(this.queueManager.size());
376
+ if (events.length === 0) {
396
377
  return;
397
378
  }
398
- this.isSending = true;
399
- logDebug(!!((_b = this.opts) == null ? void 0 : _b.debug), `Flushing ${this.queueManager.size()} events`);
400
- try {
401
- const events = this.queueManager.peek(this.queueManager.size());
402
- if (events.length === 0) {
403
- return;
404
- }
405
- yield this.sendBatch(events);
406
- this.queueManager.dequeue(events.length);
407
- logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), `Flushed ${events.length} events successfully`);
408
- } catch (error) {
409
- logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "Flush failed", error);
410
- } finally {
411
- this.isSending = false;
412
- }
413
- });
379
+ await this.sendBatch(events);
380
+ this.queueManager.dequeue(events.length);
381
+ logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), `Flushed ${events.length} events successfully`);
382
+ } catch (error) {
383
+ logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "Flush failed", error);
384
+ } finally {
385
+ this.isSending = false;
386
+ }
414
387
  }
415
388
  /**
416
389
  * 销毁实例
417
390
  */
418
- destroy() {
419
- return __async(this, null, function* () {
420
- this.closed = true;
421
- if (this.batchTimer) {
422
- clearInterval(this.batchTimer);
423
- this.batchTimer = null;
424
- }
425
- yield this.flush();
426
- if (this.queueManager) {
427
- this.queueManager.clear();
428
- }
429
- if (this.retryManager) {
430
- this.retryManager.clear();
431
- }
432
- if (this.offJs) {
433
- this.offJs();
434
- this.offJs = void 0;
435
- }
436
- if (this.offPromise) {
437
- this.offPromise();
438
- this.offPromise = void 0;
439
- }
440
- if (this.offResource) {
441
- this.offResource();
442
- this.offResource = void 0;
443
- }
444
- if (this.offWxError) {
445
- this.offWxError();
446
- this.offWxError = void 0;
447
- }
448
- if (this.offWxUnhandled) {
449
- this.offWxUnhandled();
450
- this.offWxUnhandled = void 0;
451
- }
452
- this.initialized = false;
453
- this.sessionId = void 0;
454
- LoggerSDK.instance = void 0;
455
- });
391
+ async destroy() {
392
+ this.closed = true;
393
+ if (this.batchTimer) {
394
+ clearInterval(this.batchTimer);
395
+ this.batchTimer = null;
396
+ }
397
+ await this.flush();
398
+ if (this.queueManager) {
399
+ this.queueManager.clear();
400
+ }
401
+ if (this.retryManager) {
402
+ this.retryManager.clear();
403
+ }
404
+ if (this.offJs) {
405
+ this.offJs();
406
+ this.offJs = void 0;
407
+ }
408
+ if (this.offPromise) {
409
+ this.offPromise();
410
+ this.offPromise = void 0;
411
+ }
412
+ if (this.offResource) {
413
+ this.offResource();
414
+ this.offResource = void 0;
415
+ }
416
+ if (this.offWxError) {
417
+ this.offWxError();
418
+ this.offWxError = void 0;
419
+ }
420
+ if (this.offWxUnhandled) {
421
+ this.offWxUnhandled();
422
+ this.offWxUnhandled = void 0;
423
+ }
424
+ this.initialized = false;
425
+ this.sessionId = void 0;
426
+ LoggerSDK.instance = void 0;
456
427
  }
457
428
  // ========== 内部方法 ===========
458
429
  /**
459
430
  * 发送单个事件(带重试)
460
431
  */
461
- sendEvent(event) {
462
- return __async(this, null, function* () {
463
- const sendFn = () => __async(this, null, function* () {
464
- const transporter = this.transporter || (yield TransportAdapter.getInstance(this.opts).getTransporter());
465
- this.transporter = transporter;
466
- yield transporter.send({
467
- appId: event.appId,
468
- appStage: event.stage,
469
- items: [
470
- {
471
- level: event.level === "FATAL" ? "ERROR" : event.level,
472
- traceId: event.traceId,
473
- frontendId: event.frontendId,
474
- location: event.location,
475
- message: event.message,
476
- throwable: event.throwable
477
- }
478
- ]
479
- });
432
+ async sendEvent(event) {
433
+ const sendFn = async () => {
434
+ const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
435
+ this.transporter = transporter;
436
+ await transporter.send({
437
+ appId: event.appId,
438
+ appStage: event.stage,
439
+ items: [
440
+ {
441
+ level: event.level === "FATAL" ? "ERROR" : event.level,
442
+ traceId: event.traceId,
443
+ frontendId: event.frontendId,
444
+ location: event.location,
445
+ message: event.message,
446
+ throwable: event.throwable
447
+ }
448
+ ]
480
449
  });
481
- if (this.opts.enableRetry && this.retryManager) {
482
- yield this.retryManager.executeWithRetry(event.logId, sendFn);
483
- } else {
484
- yield sendFn();
485
- }
486
- });
450
+ };
451
+ if (this.opts.enableRetry && this.retryManager) {
452
+ await this.retryManager.executeWithRetry(event.logId, sendFn);
453
+ } else {
454
+ await sendFn();
455
+ }
487
456
  }
488
457
  /**
489
458
  * 批量发送事件(带重试)
490
459
  */
491
- sendBatch(events) {
492
- return __async(this, null, function* () {
493
- var _a;
494
- if (events.length === 0)
495
- return;
496
- const groups = /* @__PURE__ */ new Map();
497
- for (let i = 0; i < events.length; i += 1) {
498
- const e = events[i];
499
- const key = `${e.appId}|${e.stage}`;
500
- const arr = groups.get(key);
501
- if (arr)
502
- arr.push(e);
503
- else
504
- groups.set(key, [e]);
505
- }
506
- const batchSize = Math.min(200, ((_a = this.opts) == null ? void 0 : _a.batchSize) || 50);
507
- const entries = Array.from(groups.entries());
508
- const tasks = [];
509
- for (let gi = 0; gi < entries.length; gi += 1) {
510
- const [key, group] = entries[gi];
511
- group.sort((a, b) => a.seq - b.seq);
512
- for (let i = 0; i < group.length; i += batchSize) {
513
- const chunk = group.slice(i, i + batchSize);
514
- if (chunk.length > 0) {
515
- const batchId = `batch_${now()}_${Math.random().toString(36).substring(2, 9)}_${key}_${i}`;
516
- const sendFn = () => __async(this, null, function* () {
517
- const transporter = this.transporter || (yield TransportAdapter.getInstance(this.opts).getTransporter());
518
- this.transporter = transporter;
519
- yield transporter.send({
520
- appId: chunk[0].appId,
521
- appStage: chunk[0].stage,
522
- items: chunk.map((event) => ({
523
- level: event.level === "FATAL" ? "ERROR" : event.level,
524
- traceId: event.traceId,
525
- frontendId: event.frontendId,
526
- location: event.location,
527
- message: event.message,
528
- throwable: event.throwable
529
- }))
530
- });
460
+ async sendBatch(events) {
461
+ var _a;
462
+ if (events.length === 0)
463
+ return;
464
+ const groups = /* @__PURE__ */ new Map();
465
+ for (let i = 0; i < events.length; i += 1) {
466
+ const e = events[i];
467
+ const key = `${e.appId}|${e.stage}`;
468
+ const arr = groups.get(key);
469
+ if (arr)
470
+ arr.push(e);
471
+ else
472
+ groups.set(key, [e]);
473
+ }
474
+ const batchSize = Math.min(200, ((_a = this.opts) == null ? void 0 : _a.batchSize) || 50);
475
+ const entries = Array.from(groups.entries());
476
+ const tasks = [];
477
+ for (let gi = 0; gi < entries.length; gi += 1) {
478
+ const [key, group] = entries[gi];
479
+ group.sort((a, b) => a.seq - b.seq);
480
+ for (let i = 0; i < group.length; i += batchSize) {
481
+ const chunk = group.slice(i, i + batchSize);
482
+ if (chunk.length > 0) {
483
+ const batchId = `batch_${now()}_${Math.random().toString(36).substring(2, 9)}_${key}_${i}`;
484
+ const sendFn = async () => {
485
+ const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
486
+ this.transporter = transporter;
487
+ await transporter.send({
488
+ appId: chunk[0].appId,
489
+ appStage: chunk[0].stage,
490
+ items: chunk.map((event) => ({
491
+ level: event.level === "FATAL" ? "ERROR" : event.level,
492
+ traceId: event.traceId,
493
+ frontendId: event.frontendId,
494
+ location: event.location,
495
+ message: event.message,
496
+ throwable: event.throwable
497
+ }))
531
498
  });
532
- const task = this.opts.enableRetry && this.retryManager ? this.retryManager.executeWithRetry(batchId, sendFn) : sendFn();
533
- tasks.push(task);
534
- }
499
+ };
500
+ const task = this.opts.enableRetry && this.retryManager ? this.retryManager.executeWithRetry(batchId, sendFn) : sendFn();
501
+ tasks.push(task);
535
502
  }
536
503
  }
537
- yield Promise.all(tasks);
538
- });
504
+ }
505
+ await Promise.all(tasks);
539
506
  }
540
507
  /**
541
508
  * 启动批量上报定时器
@@ -574,7 +541,7 @@ var LoggerSDK = class {
574
541
  });
575
542
  logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "Page hidden, flushed queue");
576
543
  }
577
- } catch (e) {
544
+ } catch {
578
545
  }
579
546
  });
580
547
  win.addEventListener && win.addEventListener("pagehide", () => {
@@ -584,7 +551,7 @@ var LoggerSDK = class {
584
551
  this.flush().catch(() => {
585
552
  });
586
553
  logDebug(!!((_b = this.opts) == null ? void 0 : _b.debug), "Page hide, flushed queue");
587
- } catch (e) {
554
+ } catch {
588
555
  }
589
556
  });
590
557
  win.addEventListener && win.addEventListener("beforeunload", () => {
@@ -594,7 +561,7 @@ var LoggerSDK = class {
594
561
  this.flush().catch(() => {
595
562
  });
596
563
  logDebug(!!((_b = this.opts) == null ? void 0 : _b.debug), "Page unload, flushed queue");
597
- } catch (e) {
564
+ } catch {
598
565
  }
599
566
  });
600
567
  }
@@ -614,7 +581,7 @@ var LoggerSDK = class {
614
581
  storeCode: opts.storeCode
615
582
  },
616
583
  opts.token
617
- ).then((response) => __async(this, null, function* () {
584
+ ).then(async (response) => {
618
585
  var _a2, _b, _c;
619
586
  logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "Register success", response);
620
587
  if (!response) {
@@ -627,7 +594,7 @@ var LoggerSDK = class {
627
594
  isSuccess = true;
628
595
  data = res.data.data;
629
596
  } else if (type === "fetch") {
630
- const responseData = yield (_b = res == null ? void 0 : res.json) == null ? void 0 : _b.call(res);
597
+ const responseData = await ((_b = res == null ? void 0 : res.json) == null ? void 0 : _b.call(res));
631
598
  logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "Register fetch response", responseData);
632
599
  if ((responseData == null ? void 0 : responseData.code) === 0) {
633
600
  isSuccess = true;
@@ -639,7 +606,7 @@ var LoggerSDK = class {
639
606
  if (isSuccess && data) {
640
607
  onSuccess(data);
641
608
  }
642
- })).catch((err) => {
609
+ }).catch((err) => {
643
610
  var _a2;
644
611
  logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "Failed to initialize SDK", err);
645
612
  });