@pluve/logger-sdk 0.0.7 → 0.0.8

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 (103) hide show
  1. package/README.md +60 -20
  2. package/dist/cjs/capture/jsError.js +48 -0
  3. package/dist/cjs/capture/promiseError.js +49 -0
  4. package/dist/cjs/capture/resourceError.js +48 -0
  5. package/dist/cjs/capture/wechatError.js +95 -0
  6. package/dist/cjs/compress/compression.js +84 -0
  7. package/dist/cjs/config/index.js +55 -0
  8. package/dist/cjs/core/fingerprint.js +36 -0
  9. package/dist/cjs/core/httpClient.js +96 -0
  10. package/dist/cjs/core/loggerSDK.js +641 -0
  11. package/dist/cjs/core/queueManager.js +249 -0
  12. package/dist/cjs/core/retryManager.js +127 -0
  13. package/dist/cjs/index.js +29 -0
  14. package/dist/cjs/logger-sdk.mermaid +84 -0
  15. package/dist/cjs/logger-sdk.svg +1 -0
  16. package/dist/cjs/stack/stacktrace.js +48 -0
  17. package/dist/cjs/transport/beaconTransport.js +64 -0
  18. package/dist/cjs/transport/pixelImageTransport.js +100 -0
  19. package/dist/cjs/transport/transport.js +17 -0
  20. package/dist/cjs/transport/transportAdapter.js +56 -0
  21. package/dist/cjs/transport/wechatTransport.js +103 -0
  22. package/dist/cjs/types/api.js +17 -0
  23. package/dist/cjs/types/env.js +17 -0
  24. package/dist/cjs/types/external.d.ts +21 -0
  25. package/dist/cjs/types/logEvent.js +17 -0
  26. package/dist/cjs/types/logEventLevel.js +17 -0
  27. package/dist/cjs/types/sdkOptions.js +17 -0
  28. package/dist/cjs/types/trackOptions.js +17 -0
  29. package/dist/cjs/utils/environment.js +183 -0
  30. package/dist/cjs/utils/session.js +31 -0
  31. package/dist/cjs/utils/tools.js +82 -0
  32. package/dist/cjs/utils/uuid.js +35 -0
  33. package/dist/esm/capture/jsError.js +45 -0
  34. package/dist/esm/capture/promiseError.js +46 -0
  35. package/dist/esm/capture/resourceError.js +24 -0
  36. package/dist/esm/capture/wechatError.js +92 -0
  37. package/dist/esm/compress/compression.js +82 -0
  38. package/dist/esm/config/index.js +28 -0
  39. package/dist/esm/core/fingerprint.js +12 -0
  40. package/dist/esm/core/httpClient.js +95 -0
  41. package/dist/esm/core/loggerSDK.js +650 -0
  42. package/dist/esm/core/queueManager.js +269 -0
  43. package/dist/esm/core/retryManager.js +129 -0
  44. package/dist/esm/index.js +5 -0
  45. package/dist/esm/logger-sdk.mermaid +84 -0
  46. package/dist/esm/logger-sdk.svg +1 -0
  47. package/dist/esm/stack/stacktrace.js +37 -0
  48. package/dist/esm/transport/beaconTransport.js +81 -0
  49. package/dist/esm/transport/pixelImageTransport.js +99 -0
  50. package/dist/esm/transport/transport.js +0 -0
  51. package/dist/esm/transport/transportAdapter.js +32 -0
  52. package/dist/esm/transport/wechatTransport.js +120 -0
  53. package/dist/esm/types/api.js +0 -0
  54. package/dist/esm/types/env.js +0 -0
  55. package/dist/esm/types/external.d.ts +21 -0
  56. package/dist/esm/types/logEvent.js +0 -0
  57. package/dist/esm/types/logEventLevel.js +0 -0
  58. package/dist/esm/types/sdkOptions.js +0 -0
  59. package/dist/esm/types/trackOptions.js +0 -0
  60. package/dist/esm/utils/environment.js +154 -0
  61. package/dist/esm/utils/session.js +7 -0
  62. package/dist/esm/utils/tools.js +76 -0
  63. package/dist/esm/utils/uuid.js +11 -0
  64. package/dist/types/capture/jsError.d.ts +2 -0
  65. package/dist/types/capture/promiseError.d.ts +2 -0
  66. package/dist/types/capture/resourceError.d.ts +2 -0
  67. package/dist/types/capture/wechatError.d.ts +3 -0
  68. package/dist/types/compress/compression.d.ts +10 -0
  69. package/dist/types/config/index.d.ts +9 -0
  70. package/dist/types/core/fingerprint.d.ts +8 -0
  71. package/dist/types/core/httpClient.d.ts +11 -0
  72. package/dist/{loggerSDK.d.ts → types/core/loggerSDK.d.ts} +27 -2
  73. package/dist/{queueManager.d.ts → types/core/queueManager.d.ts} +9 -1
  74. package/dist/{retryManager.d.ts → types/core/retryManager.d.ts} +0 -4
  75. package/dist/types/index.d.ts +1 -0
  76. package/dist/types/stack/stacktrace.d.ts +2 -0
  77. package/dist/types/transport/beaconTransport.d.ts +11 -0
  78. package/dist/types/transport/pixelImageTransport.d.ts +11 -0
  79. package/dist/types/transport/transport.d.ts +14 -0
  80. package/dist/types/transport/transportAdapter.d.ts +10 -0
  81. package/dist/types/transport/wechatTransport.d.ts +11 -0
  82. package/dist/types/types/api.d.ts +12 -0
  83. package/dist/types/types/env.d.ts +14 -0
  84. package/dist/types/types/logEvent.d.ts +57 -0
  85. package/dist/types/types/logEventLevel.d.ts +2 -0
  86. package/dist/{types.d.ts → types/types/sdkOptions.d.ts} +23 -42
  87. package/dist/types/types/trackOptions.d.ts +7 -0
  88. package/dist/types/utils/environment.d.ts +21 -0
  89. package/dist/types/utils/session.d.ts +1 -0
  90. package/dist/types/utils/tools.d.ts +12 -0
  91. package/dist/types/utils/uuid.d.ts +7 -0
  92. package/dist/umd/logger-sdk.min.js +1 -0
  93. package/package.json +29 -5
  94. package/dist/index.d.ts +0 -10
  95. package/dist/index.js +0 -13
  96. package/dist/loggerSDK.js +0 -560
  97. package/dist/queueManager.js +0 -186
  98. package/dist/retryManager.js +0 -224
  99. package/dist/transportAdapter.d.ts +0 -51
  100. package/dist/transportAdapter.js +0 -315
  101. package/dist/types.js +0 -1
  102. package/dist/utils.d.ts +0 -52
  103. package/dist/utils.js +0 -348
package/README.md CHANGED
@@ -1,17 +1,20 @@
1
1
  # @pluve/logger-sdk 使用说明
2
2
 
3
3
  ## 概览
4
+
4
5
  - 轻量级前端日志采集 SDK,支持浏览器与微信小程序
5
6
  - 传输优先级:Beacon(页面卸载可靠)→ Image(像素降级);小程序使用 wx.request
6
7
  - 支持:批量上报(默认关闭)、重试与指数退避、持久化队列、自动卸载刷新
7
8
  - 会话锁定:init 时生成并锁定 sessionId,destroy 前所有日志使用同一会话
8
9
 
9
10
  ## 安装与引入
11
+
10
12
  - 安装:`yarn add @pluve/logger-sdk`
11
13
  - ESM:`import { LoggerSDK } from '@pluve/logger-sdk'`
12
14
  - UMD:构建后全局为 `LoggerSDK`
13
15
 
14
16
  ## 快速开始(单例 + init)
17
+
15
18
  ```ts
16
19
  import { LoggerSDK } from '@pluve/logger-sdk';
17
20
 
@@ -19,7 +22,7 @@ const sdk = LoggerSDK.getInstance();
19
22
  sdk.init({
20
23
  endpoint: '/api/log',
21
24
  appId: 'web-shop',
22
- env: 'prod', // prod/stage/dev
25
+ env: 'prod', // prod/stage/dev
23
26
  debug: true,
24
27
  // enableBatch: true, // 默认 false,如需开启批量
25
28
  });
@@ -32,8 +35,8 @@ sdk.setStoreCode('STORE_001');
32
35
  await sdk.track({
33
36
  message: 'TypeError: Cannot read property',
34
37
  error: new Error('TypeError: Cannot read property'),
35
- traceId: 'trace-abc-123', // 可选
36
- logLevel: 'error', // 可选,默认 'info'
38
+ traceId: 'trace-abc-123', // 可选
39
+ logLevel: 'error', // 可选,默认 'info'
37
40
  });
38
41
 
39
42
  // 记录普通信息
@@ -44,6 +47,7 @@ await sdk.track({
44
47
  ```
45
48
 
46
49
  ## 事件数据结构(实际实现)
50
+
47
51
  ```ts
48
52
  {
49
53
  // 唯一日志标识:由 appId + UUID + 时间戳组成(非纯 v4)
@@ -69,35 +73,39 @@ await sdk.track({
69
73
  ```
70
74
 
71
75
  ## API 与行为
76
+
72
77
  ### 单例与初始化
78
+
73
79
  - `LoggerSDK.getInstance(): LoggerSDK` 获取全局唯一实例
74
80
  - `init(options: SDKOptions)` 初始化、锁定 `sessionId`、收集环境标签
75
81
  - `new LoggerSDK(options?: SDKOptions)` 构造时传入 options 会自动 init
76
82
 
77
83
  ### SDKOptions(实际默认值)
84
+
78
85
  ```ts
79
86
  interface SDKOptions {
80
- endpoint: string; // 必填
81
- appId: string; // 必填
82
- env?: 'prod' | 'stage' | 'dev'; // 默认 'dev'
83
- debug?: boolean; // 默认 false
84
- pixelParam?: string; // 默认 'data'
85
- maxPixelUrlLen?: number; // 默认 4096
86
- enableGzip?: boolean; // 默认 true(传输适配器内部处理)
87
- enableBatch?: boolean; // 默认 false(需显式开启)
88
- batchSize?: number; // 默认 10
89
- batchInterval?: number; // 默认 5000 ms
90
- maxQueueSize?: number; // 默认 100
91
- enableStorage?: boolean; // 默认 true(队列持久化)
92
- storagePrefix?: string; // 默认 'logger_sdk'
93
- enableRetry?: boolean; // 默认 true
94
- maxRetries?: number; // 默认 3
95
- retryDelay?: number; // 默认 1000 ms
96
- retryBackoff?: boolean; // 默认 true(指数退避 + 0-30% 抖动,上限 30s)
87
+ endpoint: string; // 必填
88
+ appId: string; // 必填
89
+ env?: 'prod' | 'stage' | 'dev'; // 默认 'dev'
90
+ debug?: boolean; // 默认 false
91
+ pixelParam?: string; // 默认 'data'
92
+ maxPixelUrlLen?: number; // 默认 4096
93
+ enableGzip?: boolean; // 默认 true(传输适配器内部处理)
94
+ enableBatch?: boolean; // 默认 false(需显式开启)
95
+ batchSize?: number; // 默认 10
96
+ batchInterval?: number; // 默认 5000 ms
97
+ maxQueueSize?: number; // 默认 100
98
+ enableStorage?: boolean; // 默认 true(队列持久化)
99
+ storagePrefix?: string; // 默认 'logger_sdk'
100
+ enableRetry?: boolean; // 默认 true
101
+ maxRetries?: number; // 默认 3
102
+ retryDelay?: number; // 默认 1000 ms
103
+ retryBackoff?: boolean; // 默认 true(指数退避 + 0-30% 抖动,上限 30s)
97
104
  }
98
105
  ```
99
106
 
100
107
  ### 事件上报
108
+
101
109
  - `track(options: TrackOptions)`
102
110
  - `message: string`(必填)
103
111
  - `error?: Error`(推荐在错误场景传入)
@@ -110,10 +118,12 @@ interface SDKOptions {
110
118
  - `destroy()` 停止定时器与重试,刷新队列,清理单例并重置会话锁定
111
119
 
112
120
  ### 自动采集
121
+
113
122
  - 浏览器:visibilitychange/pagehide/beforeunload 触发 `flush()`,确保卸载时尽量发送
114
123
  - 微信小程序:建议在 App 生命周期中调用(SDK 内部识别环境)
115
124
 
116
125
  ## 传输适配器
126
+
117
127
  - 默认选择:
118
128
  - 小程序:`wx.request`
119
129
  - 浏览器:`navigator.sendBeacon` → 降级到 Image 像素
@@ -123,12 +133,14 @@ interface SDKOptions {
123
133
  - Image:通过 query 拼接,受 URL 长度限制(默认 4096)
124
134
 
125
135
  ## 重试策略(实际实现)
136
+
126
137
  - 启用时对单事件与批量发送采用统一重试
127
138
  - 指数退避:`baseDelay * (2 ** (attempt - 1))`
128
139
  - 抖动:`delay * [0, 0.3]`,最大不超过 30000ms
129
140
  - 防重复:同一 taskId 重试中会拒绝重复执行
130
141
 
131
142
  ## 场景示例
143
+
132
144
  ```ts
133
145
  // 错误监控(浏览器)
134
146
  window.addEventListener('error', (event) => {
@@ -158,11 +170,39 @@ sdk.track({
158
170
  ```
159
171
 
160
172
  ## 最佳实践与安全
173
+
161
174
  - 初始化后统一设置用户与店铺信息(`identify`/`setStoreCode`)
162
175
  - userId/message/stack/tags 建议在业务侧脱敏与裁剪
163
176
  - 避免在 tags 中上传 cookie/localStorage 原始值、账户/手机号/身份证等敏感信息
164
177
 
165
178
  ## 迁移指南(多实例 → 单例 + init)
179
+
166
180
  - 统一在应用入口:`LoggerSDK.getInstance().init(opts)`
167
181
  - 其他模块直接使用该单例进行 `track/trace/flush`
168
182
  - 会话边界:登出/身份切换/子应用卸载时调用 `destroy()`,随后重新 `init()`
183
+
184
+ ## 发布与类型构建
185
+
186
+ - 多产物发布
187
+ - ESM:dist/esm(现代浏览器与打包器)
188
+ - CJS:dist/cjs(Node/工具链)
189
+ - UMD(压缩):dist/umd/logger-sdk.min.js(CDN/script 标签,全局 `LoggerSDK`)
190
+ - 包入口与导出
191
+ - main 指向 CJS,module 指向 ESM,browser 指向 ESM
192
+ - exports 同时声明 import/require/default,现代打包器自动选择
193
+ - unpkg/jsdelivr 指向 UMD 压缩产物
194
+ - tsconfig 策略
195
+ - tsconfig.base.json:通用编译选项(target、module、lib、skipLibCheck 等)
196
+ - tsconfig.json:仅类型检查(noEmit: true)
197
+ - tsconfig.types.json:仅声明构建(emitDeclarationOnly,declarationDir: dist/types)
198
+ - 脚本
199
+ - 类型检查:`yarn typecheck`
200
+ - 声明构建:`yarn build-types`
201
+ - 代码产物:`yarn build`
202
+ - 使用建议
203
+ - CDN:`https://unpkg.com/@pluve/logger-sdk/dist/umd/logger-sdk.min.js`,全局变量 `LoggerSDK`
204
+ - 打包器:优先使用 ESM(import { LoggerSDK } from '@pluve/logger-sdk')
205
+
206
+ ## 其他
207
+
208
+ 生成 mermaid 图:`yarn generate-mermaid`
@@ -0,0 +1,48 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/capture/jsError.ts
20
+ var jsError_exports = {};
21
+ __export(jsError_exports, {
22
+ registerJsErrorCapture: () => registerJsErrorCapture
23
+ });
24
+ module.exports = __toCommonJS(jsError_exports);
25
+ var import_stacktrace = require("../stack/stacktrace");
26
+ var import_tools = require("../utils/tools");
27
+ function registerJsErrorCapture(debug, callback) {
28
+ if (typeof window === "undefined")
29
+ return void 0;
30
+ const handler = async (e) => {
31
+ (0, import_tools.logDebug)(debug, "registerJsErrorCapture error", e);
32
+ if (!e.error)
33
+ return;
34
+ const stack = await (0, import_stacktrace.parseStack)(e.error);
35
+ callback({
36
+ type: "js",
37
+ message: e.error.message,
38
+ stack,
39
+ throwable: e.error.stack || ""
40
+ });
41
+ };
42
+ window.addEventListener("error", handler);
43
+ return () => window.removeEventListener("error", handler);
44
+ }
45
+ // Annotate the CommonJS export names for ESM import in node:
46
+ 0 && (module.exports = {
47
+ registerJsErrorCapture
48
+ });
@@ -0,0 +1,49 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/capture/promiseError.ts
20
+ var promiseError_exports = {};
21
+ __export(promiseError_exports, {
22
+ registerPromiseErrorCapture: () => registerPromiseErrorCapture
23
+ });
24
+ module.exports = __toCommonJS(promiseError_exports);
25
+ var import_stacktrace = require("../stack/stacktrace");
26
+ var import_tools = require("../utils/tools");
27
+ function registerPromiseErrorCapture(debug, callback) {
28
+ if (typeof window === "undefined")
29
+ return void 0;
30
+ const handler = async (e) => {
31
+ (0, import_tools.logDebug)(debug, "registerPromiseErrorCapture unhandledrejection", e);
32
+ const { reason } = e;
33
+ if (reason instanceof Error) {
34
+ const stack = await (0, import_stacktrace.parseStack)(reason);
35
+ callback({
36
+ type: "promise",
37
+ message: reason.message,
38
+ stack,
39
+ throwable: reason.stack || ""
40
+ });
41
+ }
42
+ };
43
+ window.addEventListener("unhandledrejection", handler);
44
+ return () => window.removeEventListener("unhandledrejection", handler);
45
+ }
46
+ // Annotate the CommonJS export names for ESM import in node:
47
+ 0 && (module.exports = {
48
+ registerPromiseErrorCapture
49
+ });
@@ -0,0 +1,48 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/capture/resourceError.ts
20
+ var resourceError_exports = {};
21
+ __export(resourceError_exports, {
22
+ registerResourceErrorCapture: () => registerResourceErrorCapture
23
+ });
24
+ module.exports = __toCommonJS(resourceError_exports);
25
+ var import_tools = require("../utils/tools");
26
+ function registerResourceErrorCapture(debug, callback) {
27
+ if (typeof window === "undefined")
28
+ return void 0;
29
+ const handler = (e) => {
30
+ var _a;
31
+ (0, import_tools.logDebug)(debug, "registerResourceErrorCapture error", e);
32
+ const target = e.target;
33
+ if ((target == null ? void 0 : target.src) || (target == null ? void 0 : target.href)) {
34
+ callback({
35
+ type: "resource",
36
+ message: `Resource load failed: ${(target == null ? void 0 : target.src) || (target == null ? void 0 : target.href)}`,
37
+ stack: [],
38
+ throwable: ((_a = e == null ? void 0 : e.error) == null ? void 0 : _a.stack) || ""
39
+ });
40
+ }
41
+ };
42
+ window.addEventListener("error", handler, true);
43
+ return () => window.removeEventListener("error", handler, true);
44
+ }
45
+ // Annotate the CommonJS export names for ESM import in node:
46
+ 0 && (module.exports = {
47
+ registerResourceErrorCapture
48
+ });
@@ -0,0 +1,95 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/capture/wechatError.ts
20
+ var wechatError_exports = {};
21
+ __export(wechatError_exports, {
22
+ registerWechatErrorCapture: () => registerWechatErrorCapture,
23
+ registerWechatUnhandledCapture: () => registerWechatUnhandledCapture
24
+ });
25
+ module.exports = __toCommonJS(wechatError_exports);
26
+ var import_stacktrace = require("../stack/stacktrace");
27
+ var import_environment = require("../utils/environment");
28
+ var import_tools = require("../utils/tools");
29
+ function registerWechatErrorCapture(debug, callback) {
30
+ if (!(0, import_environment.isWeChatMiniProgram)())
31
+ return void 0;
32
+ try {
33
+ const wxAny = globalThis.wx;
34
+ if (wxAny && typeof wxAny.onError === "function") {
35
+ const handler = async (error) => {
36
+ (0, import_tools.logDebug)(debug, "registerWechatErrorCapture onError", error);
37
+ const msg = String((error == null ? void 0 : error.message) ?? error);
38
+ const err = error instanceof Error ? error : new Error(msg);
39
+ const stack = await (0, import_stacktrace.parseStack)(err);
40
+ callback({
41
+ type: "js",
42
+ message: err.message,
43
+ stack,
44
+ throwable: err.stack || ""
45
+ });
46
+ };
47
+ wxAny.onError(handler);
48
+ return () => {
49
+ try {
50
+ wxAny.offError && wxAny.offError(handler);
51
+ } catch {
52
+ }
53
+ };
54
+ }
55
+ } catch (e) {
56
+ (0, import_tools.logDebug)(debug, "registerWechatErrorCapture attach failed", e);
57
+ }
58
+ return void 0;
59
+ }
60
+ function registerWechatUnhandledCapture(debug, callback) {
61
+ if (!(0, import_environment.isWeChatMiniProgram)())
62
+ return void 0;
63
+ try {
64
+ const wxAny = globalThis.wx;
65
+ if (wxAny && typeof wxAny.onUnhandledRejection === "function") {
66
+ const handler = async (res) => {
67
+ const reason = res == null ? void 0 : res.reason;
68
+ (0, import_tools.logDebug)(debug, "registerWechatUnhandledCapture onUnhandledRejection", reason);
69
+ const err = reason instanceof Error ? reason : new Error(String(reason));
70
+ const stack = await (0, import_stacktrace.parseStack)(err);
71
+ callback({
72
+ type: "promise",
73
+ message: err.message,
74
+ stack,
75
+ throwable: err.stack || ""
76
+ });
77
+ };
78
+ wxAny.onUnhandledRejection(handler);
79
+ return () => {
80
+ try {
81
+ wxAny.offUnhandledRejection && wxAny.offUnhandledRejection(handler);
82
+ } catch {
83
+ }
84
+ };
85
+ }
86
+ } catch (e) {
87
+ (0, import_tools.logDebug)(debug, "registerWechatUnhandledCapture attach failed", e);
88
+ }
89
+ return void 0;
90
+ }
91
+ // Annotate the CommonJS export names for ESM import in node:
92
+ 0 && (module.exports = {
93
+ registerWechatErrorCapture,
94
+ registerWechatUnhandledCapture
95
+ });
@@ -0,0 +1,84 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/compress/compression.ts
20
+ var compression_exports = {};
21
+ __export(compression_exports, {
22
+ gzipCompress: () => gzipCompress,
23
+ isGzipSupported: () => isGzipSupported
24
+ });
25
+ module.exports = __toCommonJS(compression_exports);
26
+ var import_fflate = require("fflate");
27
+ var import_js_base64 = require("js-base64");
28
+ var import_environment = require("../utils/environment");
29
+ function toUtf8Bytes(s) {
30
+ if (typeof TextEncoder !== "undefined") {
31
+ return new TextEncoder().encode(s);
32
+ }
33
+ const enc = encodeURIComponent(s);
34
+ const out = [];
35
+ for (let i = 0; i < enc.length; i += 1) {
36
+ const c = enc[i];
37
+ if (c === "%") {
38
+ out.push(parseInt(enc.slice(i + 1, i + 3), 16));
39
+ i += 2;
40
+ } else {
41
+ out.push(c.charCodeAt(0));
42
+ }
43
+ }
44
+ return new Uint8Array(out);
45
+ }
46
+ async function gzipCompress(data) {
47
+ if ((0, import_environment.isBrowser)() && typeof CompressionStream !== "undefined") {
48
+ try {
49
+ const encoder = new TextEncoder();
50
+ const input = encoder.encode(data);
51
+ const gzip = new CompressionStream("gzip");
52
+ const readable = new Blob([input]).stream();
53
+ const compressedStream = readable.pipeThrough(gzip);
54
+ const compressedBuffer = await new Response(compressedStream).arrayBuffer();
55
+ const bytes = new Uint8Array(compressedBuffer);
56
+ let binary = "";
57
+ for (let i = 0; i < bytes.byteLength; i += 1) {
58
+ binary += String.fromCharCode(bytes[i]);
59
+ }
60
+ if (typeof btoa !== "undefined") {
61
+ return btoa(binary);
62
+ }
63
+ return Buffer.from(binary, "base64").toString("base64");
64
+ } catch (e) {
65
+ console.log("gzipCompress 压缩失败,尝试使用 fflate 库", e);
66
+ }
67
+ }
68
+ try {
69
+ const input = toUtf8Bytes(data);
70
+ const compressed = (0, import_fflate.gzipSync)(input);
71
+ return import_js_base64.Base64.fromUint8Array(compressed, false);
72
+ } catch (e) {
73
+ console.log("gzipCompress 压缩失败", e);
74
+ }
75
+ return import_js_base64.Base64.encode(data, false);
76
+ }
77
+ function isGzipSupported() {
78
+ return (0, import_environment.isBrowser)() && typeof CompressionStream !== "undefined";
79
+ }
80
+ // Annotate the CommonJS export names for ESM import in node:
81
+ 0 && (module.exports = {
82
+ gzipCompress,
83
+ isGzipSupported
84
+ });
@@ -0,0 +1,55 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/config/index.ts
20
+ var config_exports = {};
21
+ __export(config_exports, {
22
+ getPixelBatchApi: () => getPixelBatchApi,
23
+ getRegisterApi: () => getRegisterApi,
24
+ getReportApi: () => getReportApi,
25
+ getSDKBaseApi: () => getSDKBaseApi
26
+ });
27
+ module.exports = __toCommonJS(config_exports);
28
+ var getSDKBaseApi = (env) => {
29
+ switch (env) {
30
+ case "product":
31
+ return "https://apm.pharmacyyf.com";
32
+ case "testing":
33
+ return "https://apm-te.pharmacyyf.com";
34
+ case "develop":
35
+ return "https://chief-dev.yifengx.com";
36
+ default:
37
+ return "https://apm.pharmacyyf.com";
38
+ }
39
+ };
40
+ var getRegisterApi = (env) => {
41
+ return `${getSDKBaseApi(env)}/yfcloud-apm/log/front/init`;
42
+ };
43
+ var getReportApi = (env) => {
44
+ return `${getSDKBaseApi(env)}/yfcloud-apm/log/front/batchSave`;
45
+ };
46
+ var getPixelBatchApi = (env) => {
47
+ return `${getSDKBaseApi(env)}/yfcloud-apm/log/front/pixelBatchSave`;
48
+ };
49
+ // Annotate the CommonJS export names for ESM import in node:
50
+ 0 && (module.exports = {
51
+ getPixelBatchApi,
52
+ getRegisterApi,
53
+ getReportApi,
54
+ getSDKBaseApi
55
+ });
@@ -0,0 +1,36 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/core/fingerprint.ts
20
+ var fingerprint_exports = {};
21
+ __export(fingerprint_exports, {
22
+ generateFingerprint: () => generateFingerprint
23
+ });
24
+ module.exports = __toCommonJS(fingerprint_exports);
25
+ var import_tools = require("../utils/tools");
26
+ function generateFingerprint(error) {
27
+ var _a;
28
+ const msg = (0, import_tools.normalizeMessage)(error.message);
29
+ const frame = (_a = error.stack) == null ? void 0 : _a[0];
30
+ const frameKey = frame ? `${frame.file}:${frame.line}` : "no_frame";
31
+ return `${error.type}|${msg}|${frameKey}`;
32
+ }
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ generateFingerprint
36
+ });
@@ -0,0 +1,96 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/core/httpClient.ts
20
+ var httpClient_exports = {};
21
+ __export(httpClient_exports, {
22
+ HttpClient: () => HttpClient
23
+ });
24
+ module.exports = __toCommonJS(httpClient_exports);
25
+ var import_environment = require("../utils/environment");
26
+ var import_tools = require("../utils/tools");
27
+ var HttpClient = class {
28
+ /**
29
+ * 发送 POST 请求,需要兼容浏览器和微信小程序
30
+ */
31
+ static async post(url, data, token) {
32
+ (0, import_tools.logDebug)(true, "post", url, data, token);
33
+ if ((0, import_environment.isWeChatMiniProgram)()) {
34
+ return new Promise((resolve, reject) => {
35
+ wx.request({
36
+ url,
37
+ method: "POST",
38
+ data: JSON.stringify(data),
39
+ header: {
40
+ "Content-Type": "application/json",
41
+ Authorization: `Bearer ${token}`
42
+ },
43
+ success: (res) => resolve({
44
+ type: "wechat",
45
+ response: res
46
+ }),
47
+ fail: (err) => (
48
+ // eslint-disable-next-line prefer-promise-reject-errors
49
+ reject({
50
+ type: "wechat",
51
+ error: err
52
+ })
53
+ )
54
+ });
55
+ });
56
+ }
57
+ if (typeof fetch === "undefined") {
58
+ return new Promise((resolve, reject) => {
59
+ const xhr = new XMLHttpRequest();
60
+ xhr.open("POST", url, true);
61
+ xhr.setRequestHeader("Content-Type", "application/json");
62
+ xhr.setRequestHeader("Authorization", `Bearer ${token}`);
63
+ xhr.onreadystatechange = () => {
64
+ if (xhr.readyState === 4) {
65
+ if (xhr.status === 200) {
66
+ resolve({
67
+ type: "xhr",
68
+ response: xhr.responseText
69
+ });
70
+ } else {
71
+ reject({
72
+ type: "xhr",
73
+ error: new Error(xhr.statusText)
74
+ });
75
+ }
76
+ }
77
+ };
78
+ xhr.send(JSON.stringify(data));
79
+ });
80
+ }
81
+ return new Promise((resolve, reject) => {
82
+ fetch(url, {
83
+ method: "POST",
84
+ body: JSON.stringify(data),
85
+ headers: {
86
+ "Content-Type": "application/json",
87
+ Authorization: `Bearer ${token}`
88
+ }
89
+ }).then((res) => resolve({ type: "fetch", response: res })).catch((err) => reject({ type: "fetch", error: err }));
90
+ });
91
+ }
92
+ };
93
+ // Annotate the CommonJS export names for ESM import in node:
94
+ 0 && (module.exports = {
95
+ HttpClient
96
+ });