@pluve/logger-sdk 0.0.18 → 0.0.20

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.
@@ -12,6 +12,7 @@ import { HttpClient } from "./httpClient";
12
12
  import { QueueManager } from "./queueManager";
13
13
  import { RetryManager } from "./retryManager";
14
14
  import { Md5 } from "../utils/innerMD5";
15
+ import { SecurityType } from "../types/securityType";
15
16
  var LoggerSDK = class {
16
17
  constructor() {
17
18
  /** 事件序列编号,用于事件去重 */
@@ -73,6 +74,8 @@ var LoggerSDK = class {
73
74
  // 默认启用
74
75
  userId: options.userId,
75
76
  storeCode: options.storeCode,
77
+ /** 安全类型 */
78
+ securityType: options.securityType || SecurityType.BASE,
76
79
  token: options.token,
77
80
  sampleRate: typeof options.sampleRate === "number" ? options.sampleRate : 1,
78
81
  levelSampleRate: options.levelSampleRate || {
@@ -151,11 +154,12 @@ var LoggerSDK = class {
151
154
  }
152
155
  }
153
156
  /** 设置店铺编码 */
154
- setStoreCode(storeCode) {
157
+ setStoreCode(storeCode, securityType) {
155
158
  var _a;
156
- logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "setStoreCode", storeCode);
159
+ logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "setStoreCode", storeCode, "securityType", securityType);
157
160
  if (this.opts) {
158
161
  this.opts.storeCode = storeCode;
162
+ this.opts.securityType = securityType;
159
163
  }
160
164
  }
161
165
  /** 设置日志 stage(env) */
@@ -413,7 +417,12 @@ var LoggerSDK = class {
413
417
  }
414
418
  generateToken() {
415
419
  var _a;
416
- return ((_a = this.opts) == null ? void 0 : _a.token) || Md5.hashStr(`${this.opts.appId}${this.opts.logStage}${now()}`);
420
+ const logTime = Date.now();
421
+ const token = ((_a = this.opts) == null ? void 0 : _a.token) || Md5.hashStr(`${this.opts.appId}${this.opts.logStage}${logTime}`);
422
+ return {
423
+ token,
424
+ logTime
425
+ };
417
426
  }
418
427
  // ========== 内部方法 ===========
419
428
  /**
@@ -421,11 +430,19 @@ var LoggerSDK = class {
421
430
  */
422
431
  async sendEvent(event) {
423
432
  const sendFn = async () => {
433
+ var _a, _b, _c, _d;
424
434
  const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
425
435
  this.transporter = transporter;
426
- await transporter.send(this.generateToken(), {
436
+ const { token, logTime } = this.generateToken();
437
+ await transporter.send(token, {
427
438
  appId: event.appId,
428
439
  appStage: event.stage,
440
+ token,
441
+ securityType: ((_a = this.opts) == null ? void 0 : _a.securityType) || SecurityType.BASE,
442
+ gzip: ((_b = this.opts) == null ? void 0 : _b.enableGzip) ? 1 : 0,
443
+ userId: ((_c = this.opts) == null ? void 0 : _c.userId) || "",
444
+ storeCode: ((_d = this.opts) == null ? void 0 : _d.storeCode) || "",
445
+ logTime,
429
446
  items: [
430
447
  {
431
448
  level: event.level === "FATAL" ? "ERROR" : event.level,
@@ -472,11 +489,19 @@ var LoggerSDK = class {
472
489
  if (chunk.length > 0) {
473
490
  const batchId = `batch_${now()}_${Math.random().toString(36).substring(2, 9)}_${key}_${i}`;
474
491
  const sendFn = async () => {
492
+ var _a2, _b, _c, _d;
475
493
  const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
476
494
  this.transporter = transporter;
477
- await transporter.send(this.generateToken(), {
495
+ const { token, logTime } = this.generateToken();
496
+ await transporter.send(token, {
478
497
  appId: chunk[0].appId,
479
498
  appStage: chunk[0].stage,
499
+ token,
500
+ securityType: ((_a2 = this.opts) == null ? void 0 : _a2.securityType) || SecurityType.BASE,
501
+ gzip: ((_b = this.opts) == null ? void 0 : _b.enableGzip) ? 1 : 0,
502
+ userId: ((_c = this.opts) == null ? void 0 : _c.userId) || "",
503
+ storeCode: ((_d = this.opts) == null ? void 0 : _d.storeCode) || "",
504
+ logTime,
480
505
  items: chunk.map((event) => ({
481
506
  level: event.level === "FATAL" ? "ERROR" : event.level,
482
507
  traceId: event.traceId,
@@ -560,6 +585,7 @@ var LoggerSDK = class {
560
585
  */
561
586
  initSDK(opts, onSuccess) {
562
587
  var _a;
588
+ const { token } = this.generateToken();
563
589
  HttpClient.post(
564
590
  getRegisterApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop"),
565
591
  {
@@ -570,7 +596,7 @@ var LoggerSDK = class {
570
596
  userId: opts.userId,
571
597
  storeCode: opts.storeCode
572
598
  },
573
- this.generateToken()
599
+ token
574
600
  ).then(async (response) => {
575
601
  var _a2, _b, _c;
576
602
  logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "Register success", response);
@@ -1,5 +1,5 @@
1
1
  // src/transport/beaconTransport.ts
2
- import { gzipCompress } from "../compress/compression";
2
+ import { convert2Base64, gzipCompress } from "../compress/compression";
3
3
  import { getReportApi } from "../config";
4
4
  import { isBrowser } from "../utils/environment";
5
5
  import { logDebug, safeStringify } from "../utils/tools";
@@ -14,24 +14,30 @@ var BeaconTransport = class {
14
14
  return isBrowser() && typeof navigator !== "undefined" && navigator.sendBeacon && typeof navigator.sendBeacon === "function";
15
15
  }
16
16
  async send(token, payload) {
17
- var _a, _b, _c, _d;
18
- let body = typeof payload === "string" ? payload : safeStringify(payload);
19
- const endpoint = `${getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}?token=${token}`;
17
+ var _a, _b, _c, _d, _e;
18
+ let sendBody = "";
19
+ const endpoint = `${getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}`;
20
20
  let contentType = "application/json";
21
21
  if ((_b = this.opts) == null ? void 0 : _b.enableGzip) {
22
22
  const compressedItems = await gzipCompress(safeStringify(payload.items));
23
- body = safeStringify({
23
+ sendBody = safeStringify({
24
24
  ...payload,
25
25
  items: compressedItems
26
26
  });
27
27
  contentType = "application/json; charset=utf-8";
28
28
  } else {
29
+ logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "WeChat request no gzip mode: ");
30
+ const compressedItems = convert2Base64(safeStringify(payload.items));
31
+ sendBody = safeStringify({
32
+ ...payload,
33
+ items: compressedItems
34
+ });
29
35
  }
30
- const blob = new Blob([body], { type: contentType });
36
+ const blob = new Blob([sendBody], { type: contentType });
31
37
  const success = navigator.sendBeacon(endpoint, blob);
32
- logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "sendBeacon result", success);
38
+ logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "sendBeacon result", success);
33
39
  if (!success) {
34
- logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "sendBeacon failed (queue full or other error)");
40
+ logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "sendBeacon failed (queue full or other error)");
35
41
  }
36
42
  return Promise.resolve();
37
43
  }
@@ -0,0 +1,11 @@
1
+ // src/types/securityType.ts
2
+ var SecurityType = /* @__PURE__ */ ((SecurityType2) => {
3
+ SecurityType2["ACCOUNT_CENTER"] = "2B";
4
+ SecurityType2["EHP"] = "EHP";
5
+ SecurityType2["CUSTOMER"] = "CUSTOMER";
6
+ SecurityType2["BASE"] = "BASE";
7
+ return SecurityType2;
8
+ })(SecurityType || {});
9
+ export {
10
+ SecurityType
11
+ };
@@ -1,6 +1,7 @@
1
1
  import { Env } from '../types/env';
2
2
  import { SDKOptions } from '../types/sdkOptions';
3
3
  import { TrackOptions } from '../types/trackOptions';
4
+ import { SecurityType } from '../types/securityType';
4
5
  export declare class LoggerSDK {
5
6
  private static instance;
6
7
  private opts?;
@@ -40,7 +41,7 @@ export declare class LoggerSDK {
40
41
  */
41
42
  identify(userId: string): void;
42
43
  /** 设置店铺编码 */
43
- setStoreCode(storeCode: string): void;
44
+ setStoreCode(storeCode: string, securityType: SecurityType): void;
44
45
  /** 设置日志 stage(env) */
45
46
  setStage(stage: Env): void;
46
47
  private flattenEnvTags;
@@ -1,5 +1,6 @@
1
1
  import { Stage } from './env';
2
2
  import { LogEventLevel } from './logEventLevel';
3
+ import { SecurityType } from './securityType';
3
4
  /** 标准化日志上报格式 */
4
5
  export interface LogEvent {
5
6
  /** 日志 ID */
@@ -45,6 +46,12 @@ export type BaseErrorInfo = {
45
46
  export type ReportData = {
46
47
  appId: string;
47
48
  appStage: Stage;
49
+ token: string;
50
+ securityType: SecurityType;
51
+ gzip: number;
52
+ userId: string;
53
+ storeCode: string;
54
+ logTime: number;
48
55
  items: ReportItem[];
49
56
  };
50
57
  export type ReportItem = {
@@ -1,5 +1,6 @@
1
1
  import { Env, Stage } from './env';
2
2
  import { LogEventLevel } from './logEventLevel';
3
+ import { SecurityType } from './securityType';
3
4
  /** SDK 配置选项 */
4
5
  export interface SDKOptions {
5
6
  /** 上报端点 URL */
@@ -39,6 +40,8 @@ export interface SDKOptions {
39
40
  userId?: string;
40
41
  /** 可选:店铺编码 */
41
42
  storeCode?: string;
43
+ /** 安全类型 */
44
+ securityType?: SecurityType;
42
45
  /** token */
43
46
  token: string;
44
47
  /** 全局采样率 0.0-1.0,默认 1.0(全量) */
@@ -0,0 +1,6 @@
1
+ export declare const enum SecurityType {
2
+ ACCOUNT_CENTER = "2B",// 2B(账号中心)
3
+ EHP = "EHP",// EHP(医疗)
4
+ CUSTOMER = "CUSTOMER",// 2C(会员,这期不涉及先预留)
5
+ BASE = "BASE"
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluve/logger-sdk",
3
- "version": "0.0.18",
3
+ "version": "0.0.20",
4
4
  "description": "logger sdk",
5
5
  "keywords": [
6
6
  "logger"
@@ -12,6 +12,14 @@
12
12
  "module": "dist/esm/index.js",
13
13
  "browser": "dist/esm/index.js",
14
14
  "types": "dist/types/index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/types/index.d.ts",
18
+ "import": "./dist/esm/index.js",
19
+ "require": "./dist/cjs/index.js",
20
+ "default": "./dist/esm/index.js"
21
+ }
22
+ },
15
23
  "typesVersions": {
16
24
  "*": {
17
25
  "index": ["dist/types/index.d.ts"],