@pluve/logger-sdk 0.0.14 → 0.0.16
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.
package/README.md
CHANGED
|
@@ -50,6 +50,7 @@ sdk.setStage('testing');
|
|
|
50
50
|
## 配置项(节选)
|
|
51
51
|
|
|
52
52
|
- enableGzip:是否启用 gzip 压缩(默认 true)
|
|
53
|
+
- gzipBatchMinSize:开启批量压缩的最小 items 数量(默认 2)
|
|
53
54
|
- enableBatch:是否启用批量上报(默认需显式开启)
|
|
54
55
|
- batchSize:批量阈值(默认 10)
|
|
55
56
|
- batchInterval:批量定时器间隔(默认 15000ms)
|
|
@@ -60,6 +61,12 @@ sdk.setStage('testing');
|
|
|
60
61
|
- enableAutoCapture:自动采集总开关(默认开启)
|
|
61
62
|
- autoCapture:细粒度开关 { js, promise, resource, wechat }
|
|
62
63
|
|
|
64
|
+
说明:
|
|
65
|
+
|
|
66
|
+
- enableGzip 仅在启用批量模式时生效;当 items 数量 ≥ gzipBatchMinSize 时自动压缩,否则走未压缩编码
|
|
67
|
+
- enableStorage 默认在开启批量时启用;即使未开启批量,若 enableStorage 未显式设为 false,也会启用
|
|
68
|
+
- autoCapture 默认全部开启;可按需关闭各子项,或关闭总开关
|
|
69
|
+
|
|
63
70
|
详细定义参考类型文件:[sdkOptions.ts](./src/types/sdkOptions.ts).
|
|
64
71
|
|
|
65
72
|
## 压缩实现
|
|
@@ -147,3 +154,59 @@ sdk.setStage('testing');
|
|
|
147
154
|
- 类型入口:package.json exports.types 与 typesVersions 指向 dist/types
|
|
148
155
|
- TypeScript 解析:推荐在应用设置 moduleResolution: "bundler" 或 "nodenext"/"node16"
|
|
149
156
|
- 声明构建:`yarn build-types`(tsconfig.types.json 禁用默认 @types 扫描,避免冲突)
|
|
157
|
+
|
|
158
|
+
## 依赖说明
|
|
159
|
+
|
|
160
|
+
- peerDependencies(由应用安装或 CDN 提供)
|
|
161
|
+
- js-base64(^3.7.5)
|
|
162
|
+
- localforage(^1.10.0)
|
|
163
|
+
- 安装示例(应用侧)
|
|
164
|
+
- `yarn add js-base64 localforage`
|
|
165
|
+
- CDN 全局(UMD 产物)
|
|
166
|
+
- 先引入 js-base64 提供全局 Base64,再引入 logger-sdk UMD
|
|
167
|
+
- localforage 可选(IndexedDB 持久化),未提供时回退到 localStorage
|
|
168
|
+
- 构建外部化(UMD)
|
|
169
|
+
- .fatherrc.ts 已将 js-base64 与 localforage 外部化映射到全局变量
|
|
170
|
+
- 运行时回退策略
|
|
171
|
+
- PixelImageTransport:优先使用全局 Base64,其次 btoa,再次 encodeURIComponent
|
|
172
|
+
- 压缩:CompressionStream 优先;回退 fflate.gzipSync + btoa/Buffer
|
|
173
|
+
- 队列:优先 localforage(IndexedDB);失败回退 localStorage(仅浏览器启用)
|
|
174
|
+
|
|
175
|
+
## 编码细节(传输 × 压缩)
|
|
176
|
+
|
|
177
|
+
- WechatTransport(POST)
|
|
178
|
+
- 未压缩
|
|
179
|
+
- Body:JSON(safeStringify(payload))
|
|
180
|
+
- Header:Content-Type: application/json;token: <opts.token>
|
|
181
|
+
- items:字符串化后的数组(message/throwable 等为字符串)
|
|
182
|
+
- Endpoint:[config.getReportApi](.src/config/index.ts)
|
|
183
|
+
- 启用压缩
|
|
184
|
+
- items:safeStringify(items) 后使用 [gzipCompress](.src/compress/compression.ts) 生成 Base64
|
|
185
|
+
- Body:JSON(...payload, items: Base64)
|
|
186
|
+
- Header:Content-Type: application/json; charset=utf-8;token: <opts.token>
|
|
187
|
+
|
|
188
|
+
- BeaconTransport(sendBeacon)
|
|
189
|
+
- 未压缩
|
|
190
|
+
- Body:JSON(safeStringify(payload))
|
|
191
|
+
- Content-Type:application/json(通过 Blob 指定)
|
|
192
|
+
- 发送方式:navigator.sendBeacon(endpoint, blob)
|
|
193
|
+
- 启用压缩(满足批量条件或显式开启)
|
|
194
|
+
- items:safeStringify(items) 后 gzipCompress → Base64
|
|
195
|
+
- Body:JSON(...payload, items: Base64)
|
|
196
|
+
- Content-Type:application/json(Blob)
|
|
197
|
+
|
|
198
|
+
- PixelImageTransport(Image GET)
|
|
199
|
+
- 未压缩
|
|
200
|
+
- items:safeStringify(items)
|
|
201
|
+
- 编码:优先使用 Base64.encode(items),其次 encodeURIComponent(items)
|
|
202
|
+
- Query 参数:items=encodeURIComponent(编码结果);gzip=0;携带 appId、appStage、cacheBuster
|
|
203
|
+
- 启用压缩
|
|
204
|
+
- items:safeStringify(items) 后 gzipCompress → Base64
|
|
205
|
+
- Query 参数:items=encodeURIComponent(Base64);gzip=1;携带 appId、appStage、cacheBuster
|
|
206
|
+
- URL 长度检查:默认最大 8192,超长打印警告
|
|
207
|
+
|
|
208
|
+
说明:
|
|
209
|
+
|
|
210
|
+
- Base64 来源优先级:全局 Base64(CDN 或应用注入) > 原生 btoa > encodeURIComponent 回退
|
|
211
|
+
- 压缩实现优先级:CompressionStream(浏览器) > fflate.gzipSync(通用),均返回 Base64 字符串便于传输
|
|
212
|
+
- 所有传输模式的 items 都基于统一结构(level/traceId/frontendId/location/message/throwable)
|
|
@@ -43,16 +43,17 @@ var LoggerSDK = class {
|
|
|
43
43
|
init(options) {
|
|
44
44
|
this.opts = {
|
|
45
45
|
// endpoint: options.endpoint,
|
|
46
|
-
appId: `${options.appId}
|
|
46
|
+
appId: `${options.appId}`,
|
|
47
|
+
// 统一与 zadig 部署应用 APPID 保持一致
|
|
47
48
|
env: options.env || "develop",
|
|
48
49
|
/** 日志环境 */
|
|
49
50
|
logStage: options.logStage,
|
|
50
51
|
debug: !!options.debug,
|
|
51
52
|
/** 是否启用 gzip 压缩,默认 true */
|
|
52
53
|
enableGzip: options.enableGzip !== false,
|
|
53
|
-
//
|
|
54
|
-
|
|
55
|
-
//
|
|
54
|
+
// 默认启用,但仅在批量模式开启情况下有效
|
|
55
|
+
gzipBatchMinSize: options.gzipBatchMinSize || 2,
|
|
56
|
+
// 默认 2 条数据开启 gzip 压缩
|
|
56
57
|
/** 最大像素图 URL 长度,默认 1900 */
|
|
57
58
|
maxPixelUrlLen: options.maxPixelUrlLen || 8192,
|
|
58
59
|
// 批量上报配置(默认关闭,需显式开启)
|
|
@@ -18,7 +18,7 @@ var BeaconTransport = class {
|
|
|
18
18
|
let body = typeof payload === "string" ? payload : safeStringify(payload);
|
|
19
19
|
const endpoint = getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop");
|
|
20
20
|
let contentType = "application/json";
|
|
21
|
-
if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && (
|
|
21
|
+
if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && payload.items.length >= ((_c = this.opts) == null ? void 0 : _c.gzipBatchMinSize)) {
|
|
22
22
|
const compressedItems = await gzipCompress(safeStringify(payload.items));
|
|
23
23
|
body = safeStringify({
|
|
24
24
|
...payload,
|
|
@@ -20,11 +20,10 @@ var PixelImageTransport = class {
|
|
|
20
20
|
const param = "items";
|
|
21
21
|
const maxLen = ((_b = this.opts) == null ? void 0 : _b.maxPixelUrlLen) || 8192;
|
|
22
22
|
let compressedBody;
|
|
23
|
-
if (((_c = this.opts) == null ? void 0 : _c.enableGzip) && (
|
|
23
|
+
if (((_c = this.opts) == null ? void 0 : _c.enableGzip) && payload.items.length >= ((_d = this.opts) == null ? void 0 : _d.gzipBatchMinSize)) {
|
|
24
24
|
const t = now();
|
|
25
25
|
logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "PixelImage request gzip compress body: ", body);
|
|
26
26
|
compressedBody = await gzipCompress(body);
|
|
27
|
-
compressedBody = await convert2Base64(compressedBody);
|
|
28
27
|
logDebug(!!((_f = this.opts) == null ? void 0 : _f.debug), "PixelImage request gzip compress cost: ", now() - t);
|
|
29
28
|
logDebug(!!((_g = this.opts) == null ? void 0 : _g.debug), `original body size: ${body.length}, compressed body size: ${compressedBody.length}`);
|
|
30
29
|
logDebug(!!((_h = this.opts) == null ? void 0 : _h.debug), "PixelImage request gzip compress body: ", compressedBody);
|
|
@@ -19,7 +19,7 @@ var WechatTransport = class {
|
|
|
19
19
|
const endpoint = getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop");
|
|
20
20
|
const timeout = 1e4;
|
|
21
21
|
let contentType = "application/json";
|
|
22
|
-
if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && (
|
|
22
|
+
if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && payload.items.length >= ((_c = this.opts) == null ? void 0 : _c.gzipBatchMinSize)) {
|
|
23
23
|
const t = now();
|
|
24
24
|
logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "WeChat request enable gzip compress: ", t);
|
|
25
25
|
const compressedItems = await gzipCompress(safeStringify(payload.items));
|
|
@@ -15,10 +15,10 @@ export interface SDKOptions {
|
|
|
15
15
|
enableBatch?: boolean;
|
|
16
16
|
/** 批量上报最大数量,默认 10 */
|
|
17
17
|
batchSize?: number;
|
|
18
|
-
/** 是否启用 gzip 压缩,默认 true */
|
|
18
|
+
/** 是否启用 gzip 压缩,默认 true (仅在批量模式下有效) */
|
|
19
19
|
enableGzip?: boolean;
|
|
20
|
-
/**
|
|
21
|
-
|
|
20
|
+
/** 批量模式下启用 gzip 压缩的最小 items 数量,默认 2 */
|
|
21
|
+
gzipBatchMinSize?: number;
|
|
22
22
|
/** 最大像素图 URL 长度,默认 1900 */
|
|
23
23
|
maxPixelUrlLen?: number;
|
|
24
24
|
/** 批量上报时间间隔(毫秒),默认 5000 */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluve/logger-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"description": "logger sdk",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"logger"
|
|
@@ -58,8 +58,7 @@
|
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
60
|
"jsdom": "^27.4.0",
|
|
61
|
-
"vitest": "^4.0.15"
|
|
62
|
-
"@mermaid-js/mermaid-cli": "^10.9.1"
|
|
61
|
+
"vitest": "^4.0.15"
|
|
63
62
|
},
|
|
64
63
|
"dependencies": {
|
|
65
64
|
"fflate": "^0.8.2",
|