@pluve/logger-sdk 0.0.11 → 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 (47) 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 +47 -51
  5. package/dist/esm/core/httpClient.js +53 -76
  6. package/dist/esm/core/loggerSDK.js +202 -233
  7. package/dist/esm/core/queueManager.js +59 -99
  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 +55 -79
  12. package/dist/esm/transport/transportAdapter.js +11 -13
  13. package/dist/esm/transport/wechatTransport.js +53 -94
  14. package/dist/esm/utils/tools.js +12 -35
  15. package/dist/types/compress/compression.d.ts +2 -0
  16. package/dist/types/transport/transportAdapter.d.ts +3 -5
  17. package/package.json +1 -2
  18. package/dist/cjs/capture/jsError.js +0 -48
  19. package/dist/cjs/capture/promiseError.js +0 -49
  20. package/dist/cjs/capture/resourceError.js +0 -48
  21. package/dist/cjs/capture/wechatError.js +0 -95
  22. package/dist/cjs/compress/compression.js +0 -84
  23. package/dist/cjs/config/index.js +0 -55
  24. package/dist/cjs/core/fingerprint.js +0 -36
  25. package/dist/cjs/core/httpClient.js +0 -96
  26. package/dist/cjs/core/loggerSDK.js +0 -641
  27. package/dist/cjs/core/queueManager.js +0 -249
  28. package/dist/cjs/core/retryManager.js +0 -127
  29. package/dist/cjs/index.js +0 -34
  30. package/dist/cjs/stack/stacktrace.js +0 -48
  31. package/dist/cjs/transport/beaconTransport.js +0 -64
  32. package/dist/cjs/transport/pixelImageTransport.js +0 -100
  33. package/dist/cjs/transport/transport.js +0 -17
  34. package/dist/cjs/transport/transportAdapter.js +0 -56
  35. package/dist/cjs/transport/wechatTransport.js +0 -103
  36. package/dist/cjs/types/api.js +0 -17
  37. package/dist/cjs/types/env.js +0 -17
  38. package/dist/cjs/types/external.d.ts +0 -21
  39. package/dist/cjs/types/logEvent.js +0 -17
  40. package/dist/cjs/types/logEventLevel.js +0 -17
  41. package/dist/cjs/types/sdkOptions.js +0 -17
  42. package/dist/cjs/types/trackOptions.js +0 -17
  43. package/dist/cjs/utils/environment.js +0 -183
  44. package/dist/cjs/utils/session.js +0 -31
  45. package/dist/cjs/utils/tools.js +0 -82
  46. package/dist/cjs/utils/uuid.js +0 -35
  47. 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";
@@ -137,7 +116,7 @@ var LoggerSDK = class {
137
116
  debug: this.opts.debug
138
117
  });
139
118
  }
140
- this.transporter = TransportAdapter.getInstance(this.opts).getTransporter();
119
+ this.transporter = void 0;
141
120
  this.sessionId = getSessionId();
142
121
  this.envTags = collectEnvironmentTags();
143
122
  this.initSDK(this.opts, (data) => {
@@ -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,177 +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
- var _a;
465
- yield (_a = this.transporter) == null ? void 0 : _a.send({
466
- appId: event.appId,
467
- appStage: event.stage,
468
- items: [
469
- {
470
- level: event.level === "FATAL" ? "ERROR" : event.level,
471
- traceId: event.traceId,
472
- frontendId: event.frontendId,
473
- location: event.location,
474
- message: event.message,
475
- throwable: event.throwable
476
- }
477
- ]
478
- });
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
+ ]
479
449
  });
480
- if (this.opts.enableRetry && this.retryManager) {
481
- yield this.retryManager.executeWithRetry(event.logId, sendFn);
482
- } else {
483
- yield sendFn();
484
- }
485
- });
450
+ };
451
+ if (this.opts.enableRetry && this.retryManager) {
452
+ await this.retryManager.executeWithRetry(event.logId, sendFn);
453
+ } else {
454
+ await sendFn();
455
+ }
486
456
  }
487
457
  /**
488
458
  * 批量发送事件(带重试)
489
459
  */
490
- sendBatch(events) {
491
- return __async(this, null, function* () {
492
- var _a;
493
- if (events.length === 0)
494
- return;
495
- const groups = /* @__PURE__ */ new Map();
496
- for (let i = 0; i < events.length; i += 1) {
497
- const e = events[i];
498
- const key = `${e.appId}|${e.stage}`;
499
- const arr = groups.get(key);
500
- if (arr)
501
- arr.push(e);
502
- else
503
- groups.set(key, [e]);
504
- }
505
- const batchSize = Math.min(200, ((_a = this.opts) == null ? void 0 : _a.batchSize) || 50);
506
- const entries = Array.from(groups.entries());
507
- const tasks = [];
508
- for (let gi = 0; gi < entries.length; gi += 1) {
509
- const [key, group] = entries[gi];
510
- group.sort((a, b) => a.seq - b.seq);
511
- for (let i = 0; i < group.length; i += batchSize) {
512
- const chunk = group.slice(i, i + batchSize);
513
- if (chunk.length > 0) {
514
- const batchId = `batch_${now()}_${Math.random().toString(36).substring(2, 9)}_${key}_${i}`;
515
- const sendFn = () => __async(this, null, function* () {
516
- var _a2;
517
- yield (_a2 = this.transporter) == null ? void 0 : _a2.send({
518
- appId: chunk[0].appId,
519
- appStage: chunk[0].stage,
520
- items: chunk.map((event) => ({
521
- level: event.level === "FATAL" ? "ERROR" : event.level,
522
- traceId: event.traceId,
523
- frontendId: event.frontendId,
524
- location: event.location,
525
- message: event.message,
526
- throwable: event.throwable
527
- }))
528
- });
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
+ }))
529
498
  });
530
- const task = this.opts.enableRetry && this.retryManager ? this.retryManager.executeWithRetry(batchId, sendFn) : sendFn();
531
- tasks.push(task);
532
- }
499
+ };
500
+ const task = this.opts.enableRetry && this.retryManager ? this.retryManager.executeWithRetry(batchId, sendFn) : sendFn();
501
+ tasks.push(task);
533
502
  }
534
503
  }
535
- yield Promise.all(tasks);
536
- });
504
+ }
505
+ await Promise.all(tasks);
537
506
  }
538
507
  /**
539
508
  * 启动批量上报定时器
@@ -572,7 +541,7 @@ var LoggerSDK = class {
572
541
  });
573
542
  logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "Page hidden, flushed queue");
574
543
  }
575
- } catch (e) {
544
+ } catch {
576
545
  }
577
546
  });
578
547
  win.addEventListener && win.addEventListener("pagehide", () => {
@@ -582,7 +551,7 @@ var LoggerSDK = class {
582
551
  this.flush().catch(() => {
583
552
  });
584
553
  logDebug(!!((_b = this.opts) == null ? void 0 : _b.debug), "Page hide, flushed queue");
585
- } catch (e) {
554
+ } catch {
586
555
  }
587
556
  });
588
557
  win.addEventListener && win.addEventListener("beforeunload", () => {
@@ -592,7 +561,7 @@ var LoggerSDK = class {
592
561
  this.flush().catch(() => {
593
562
  });
594
563
  logDebug(!!((_b = this.opts) == null ? void 0 : _b.debug), "Page unload, flushed queue");
595
- } catch (e) {
564
+ } catch {
596
565
  }
597
566
  });
598
567
  }
@@ -612,7 +581,7 @@ var LoggerSDK = class {
612
581
  storeCode: opts.storeCode
613
582
  },
614
583
  opts.token
615
- ).then((response) => __async(this, null, function* () {
584
+ ).then(async (response) => {
616
585
  var _a2, _b, _c;
617
586
  logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "Register success", response);
618
587
  if (!response) {
@@ -625,7 +594,7 @@ var LoggerSDK = class {
625
594
  isSuccess = true;
626
595
  data = res.data.data;
627
596
  } else if (type === "fetch") {
628
- 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));
629
598
  logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "Register fetch response", responseData);
630
599
  if ((responseData == null ? void 0 : responseData.code) === 0) {
631
600
  isSuccess = true;
@@ -637,7 +606,7 @@ var LoggerSDK = class {
637
606
  if (isSuccess && data) {
638
607
  onSuccess(data);
639
608
  }
640
- })).catch((err) => {
609
+ }).catch((err) => {
641
610
  var _a2;
642
611
  logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "Failed to initialize SDK", err);
643
612
  });