@pluve/logger-sdk 0.0.17 → 0.0.19
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 +1 -2
- package/dist/esm/core/httpClient.js +1 -26
- package/dist/esm/core/loggerSDK.js +20 -25
- package/dist/esm/core/queueManager.js +4 -18
- package/dist/esm/transport/beaconTransport.js +13 -6
- package/dist/esm/transport/pixelImageTransport.js +8 -8
- package/dist/esm/transport/transportAdapter.js +0 -5
- package/dist/esm/types/securityType.js +11 -0
- package/dist/esm/utils/environment.js +1 -46
- package/dist/types/core/httpClient.d.ts +2 -2
- package/dist/types/core/loggerSDK.d.ts +2 -3
- package/dist/types/types/env.d.ts +1 -1
- package/dist/types/types/logEvent.d.ts +6 -0
- package/dist/types/types/sdkOptions.d.ts +3 -3
- package/dist/types/types/securityType.d.ts +6 -0
- package/dist/types/utils/environment.d.ts +0 -1
- package/package.json +1 -9
- package/dist/esm/capture/wechatError.js +0 -70
- package/dist/esm/transport/wechatTransport.js +0 -79
package/README.md
CHANGED
|
@@ -50,7 +50,6 @@ sdk.setStage('testing');
|
|
|
50
50
|
## 配置项(节选)
|
|
51
51
|
|
|
52
52
|
- enableGzip:是否启用 gzip 压缩(默认 true)
|
|
53
|
-
- gzipBatchMinSize:开启批量压缩的最小 items 数量(默认 2)
|
|
54
53
|
- enableBatch:是否启用批量上报(默认需显式开启)
|
|
55
54
|
- batchSize:批量阈值(默认 10)
|
|
56
55
|
- batchInterval:批量定时器间隔(默认 15000ms)
|
|
@@ -63,7 +62,7 @@ sdk.setStage('testing');
|
|
|
63
62
|
|
|
64
63
|
说明:
|
|
65
64
|
|
|
66
|
-
- enableGzip
|
|
65
|
+
- enableGzip 仅在启用批量模式时生效;
|
|
67
66
|
- enableStorage 默认在开启批量时启用;即使未开启批量,若 enableStorage 未显式设为 false,也会启用
|
|
68
67
|
- autoCapture 默认全部开启;可按需关闭各子项,或关闭总开关
|
|
69
68
|
|
|
@@ -1,36 +1,11 @@
|
|
|
1
1
|
// src/core/httpClient.ts
|
|
2
|
-
import { isWeChatMiniProgram } from "../utils/environment";
|
|
3
2
|
import { logDebug } from "../utils/tools";
|
|
4
3
|
var HttpClient = class {
|
|
5
4
|
/**
|
|
6
|
-
* 发送 POST
|
|
5
|
+
* 发送 POST 请求,需要兼容浏览器
|
|
7
6
|
*/
|
|
8
7
|
static async post(url, data, token) {
|
|
9
8
|
logDebug(true, "post", url, data, token);
|
|
10
|
-
if (isWeChatMiniProgram()) {
|
|
11
|
-
return new Promise((resolve, reject) => {
|
|
12
|
-
wx.request({
|
|
13
|
-
url,
|
|
14
|
-
method: "POST",
|
|
15
|
-
data: JSON.stringify(data),
|
|
16
|
-
header: {
|
|
17
|
-
"Content-Type": "application/json",
|
|
18
|
-
token
|
|
19
|
-
},
|
|
20
|
-
success: (res) => resolve({
|
|
21
|
-
type: "wechat",
|
|
22
|
-
response: res
|
|
23
|
-
}),
|
|
24
|
-
fail: (err) => (
|
|
25
|
-
// eslint-disable-next-line prefer-promise-reject-errors
|
|
26
|
-
reject({
|
|
27
|
-
type: "wechat",
|
|
28
|
-
error: err
|
|
29
|
-
})
|
|
30
|
-
)
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
9
|
if (typeof fetch === "undefined") {
|
|
35
10
|
return new Promise((resolve, reject) => {
|
|
36
11
|
const xhr = new XMLHttpRequest();
|
|
@@ -5,7 +5,6 @@ import { registerResourceErrorCapture } from "../capture/resourceError";
|
|
|
5
5
|
import { getRegisterApi } from "../config";
|
|
6
6
|
import { TransportAdapter } from "../transport/transportAdapter";
|
|
7
7
|
import { collectEnvironmentTags, getCurrentUrl, isBrowser } from "../utils/environment";
|
|
8
|
-
import { registerWechatErrorCapture, registerWechatUnhandledCapture } from "../capture/wechatError";
|
|
9
8
|
import { getSessionId } from "../utils/session";
|
|
10
9
|
import { flattenStack, hashToProb, logDebug, now, normalizeMessage } from "../utils/tools";
|
|
11
10
|
import { uuid } from "../utils/uuid";
|
|
@@ -13,6 +12,7 @@ import { HttpClient } from "./httpClient";
|
|
|
13
12
|
import { QueueManager } from "./queueManager";
|
|
14
13
|
import { RetryManager } from "./retryManager";
|
|
15
14
|
import { Md5 } from "../utils/innerMD5";
|
|
15
|
+
import { SecurityType } from "../types/securityType";
|
|
16
16
|
var LoggerSDK = class {
|
|
17
17
|
constructor() {
|
|
18
18
|
/** 事件序列编号,用于事件去重 */
|
|
@@ -53,8 +53,6 @@ var LoggerSDK = class {
|
|
|
53
53
|
/** 是否启用 gzip 压缩,默认 true */
|
|
54
54
|
enableGzip: options.enableGzip !== false,
|
|
55
55
|
// 默认启用,但仅在批量模式开启情况下有效
|
|
56
|
-
gzipBatchMinSize: options.gzipBatchMinSize || 2,
|
|
57
|
-
// 默认 2 条数据开启 gzip 压缩
|
|
58
56
|
/** 最大像素图 URL 长度,默认 1900 */
|
|
59
57
|
maxPixelUrlLen: options.maxPixelUrlLen || 8192,
|
|
60
58
|
// 批量上报配置(默认关闭,需显式开启)
|
|
@@ -98,8 +96,7 @@ var LoggerSDK = class {
|
|
|
98
96
|
autoCapture: options.autoCapture || {
|
|
99
97
|
js: true,
|
|
100
98
|
promise: true,
|
|
101
|
-
resource: true
|
|
102
|
-
wechat: true
|
|
99
|
+
resource: true
|
|
103
100
|
}
|
|
104
101
|
};
|
|
105
102
|
if (this.opts.enableBatch) {
|
|
@@ -122,7 +119,7 @@ var LoggerSDK = class {
|
|
|
122
119
|
this.sessionId = getSessionId();
|
|
123
120
|
this.envTags = collectEnvironmentTags();
|
|
124
121
|
this.initSDK(this.opts, (data) => {
|
|
125
|
-
var _a, _b, _c, _d, _e, _f
|
|
122
|
+
var _a, _b, _c, _d, _e, _f;
|
|
126
123
|
this.collectSwitch = data.collectSwitch;
|
|
127
124
|
this.collectLogLevel = data.collectLogLevel;
|
|
128
125
|
this.opts.sampleRate = data.samplingRate;
|
|
@@ -142,10 +139,6 @@ var LoggerSDK = class {
|
|
|
142
139
|
if (enableAuto && ac.resource !== false) {
|
|
143
140
|
this.offResource = registerResourceErrorCapture(!!((_f = this.opts) == null ? void 0 : _f.debug), this.trackInner.bind(this));
|
|
144
141
|
}
|
|
145
|
-
if (enableAuto && ac.wechat !== false) {
|
|
146
|
-
this.offWxError = registerWechatErrorCapture(!!((_g = this.opts) == null ? void 0 : _g.debug), this.trackInner.bind(this));
|
|
147
|
-
this.offWxUnhandled = registerWechatUnhandledCapture(!!((_h = this.opts) == null ? void 0 : _h.debug), this.trackInner.bind(this));
|
|
148
|
-
}
|
|
149
142
|
});
|
|
150
143
|
}
|
|
151
144
|
/**
|
|
@@ -159,11 +152,12 @@ var LoggerSDK = class {
|
|
|
159
152
|
}
|
|
160
153
|
}
|
|
161
154
|
/** 设置店铺编码 */
|
|
162
|
-
setStoreCode(storeCode) {
|
|
155
|
+
setStoreCode(storeCode, securityType) {
|
|
163
156
|
var _a;
|
|
164
|
-
logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "setStoreCode", storeCode);
|
|
157
|
+
logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "setStoreCode", storeCode, "securityType", securityType);
|
|
165
158
|
if (this.opts) {
|
|
166
159
|
this.opts.storeCode = storeCode;
|
|
160
|
+
this.opts.securityType = securityType;
|
|
167
161
|
}
|
|
168
162
|
}
|
|
169
163
|
/** 设置日志 stage(env) */
|
|
@@ -233,7 +227,7 @@ var LoggerSDK = class {
|
|
|
233
227
|
/** 可选的结构化额外信息 */
|
|
234
228
|
// tags: this.envTags,
|
|
235
229
|
};
|
|
236
|
-
this.doTrack(logEvent);
|
|
230
|
+
await this.doTrack(logEvent);
|
|
237
231
|
}
|
|
238
232
|
trackInner(error) {
|
|
239
233
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
@@ -415,14 +409,6 @@ var LoggerSDK = class {
|
|
|
415
409
|
this.offResource();
|
|
416
410
|
this.offResource = void 0;
|
|
417
411
|
}
|
|
418
|
-
if (this.offWxError) {
|
|
419
|
-
this.offWxError();
|
|
420
|
-
this.offWxError = void 0;
|
|
421
|
-
}
|
|
422
|
-
if (this.offWxUnhandled) {
|
|
423
|
-
this.offWxUnhandled();
|
|
424
|
-
this.offWxUnhandled = void 0;
|
|
425
|
-
}
|
|
426
412
|
this.initialized = false;
|
|
427
413
|
this.sessionId = void 0;
|
|
428
414
|
LoggerSDK.instance = void 0;
|
|
@@ -437,11 +423,17 @@ var LoggerSDK = class {
|
|
|
437
423
|
*/
|
|
438
424
|
async sendEvent(event) {
|
|
439
425
|
const sendFn = async () => {
|
|
426
|
+
var _a, _b, _c, _d;
|
|
440
427
|
const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
|
|
441
428
|
this.transporter = transporter;
|
|
442
429
|
await transporter.send(this.generateToken(), {
|
|
443
430
|
appId: event.appId,
|
|
444
431
|
appStage: event.stage,
|
|
432
|
+
token: this.generateToken(),
|
|
433
|
+
securityType: ((_a = this.opts) == null ? void 0 : _a.securityType) || SecurityType.BASE,
|
|
434
|
+
gzip: ((_b = this.opts) == null ? void 0 : _b.enableGzip) ? 1 : 0,
|
|
435
|
+
userId: ((_c = this.opts) == null ? void 0 : _c.userId) || "",
|
|
436
|
+
storeCode: ((_d = this.opts) == null ? void 0 : _d.storeCode) || "",
|
|
445
437
|
items: [
|
|
446
438
|
{
|
|
447
439
|
level: event.level === "FATAL" ? "ERROR" : event.level,
|
|
@@ -488,11 +480,17 @@ var LoggerSDK = class {
|
|
|
488
480
|
if (chunk.length > 0) {
|
|
489
481
|
const batchId = `batch_${now()}_${Math.random().toString(36).substring(2, 9)}_${key}_${i}`;
|
|
490
482
|
const sendFn = async () => {
|
|
483
|
+
var _a2, _b, _c, _d;
|
|
491
484
|
const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
|
|
492
485
|
this.transporter = transporter;
|
|
493
486
|
await transporter.send(this.generateToken(), {
|
|
494
487
|
appId: chunk[0].appId,
|
|
495
488
|
appStage: chunk[0].stage,
|
|
489
|
+
token: this.generateToken(),
|
|
490
|
+
securityType: ((_a2 = this.opts) == null ? void 0 : _a2.securityType) || SecurityType.BASE,
|
|
491
|
+
gzip: ((_b = this.opts) == null ? void 0 : _b.enableGzip) ? 1 : 0,
|
|
492
|
+
userId: ((_c = this.opts) == null ? void 0 : _c.userId) || "",
|
|
493
|
+
storeCode: ((_d = this.opts) == null ? void 0 : _d.storeCode) || "",
|
|
496
494
|
items: chunk.map((event) => ({
|
|
497
495
|
level: event.level === "FATAL" ? "ERROR" : event.level,
|
|
498
496
|
traceId: event.traceId,
|
|
@@ -596,10 +594,7 @@ var LoggerSDK = class {
|
|
|
596
594
|
const { type, response: res, error } = response;
|
|
597
595
|
let isSuccess = false;
|
|
598
596
|
let data;
|
|
599
|
-
if (type === "
|
|
600
|
-
isSuccess = true;
|
|
601
|
-
data = res.data.data;
|
|
602
|
-
} else if (type === "fetch") {
|
|
597
|
+
if (type === "fetch") {
|
|
603
598
|
const responseData = await ((_b = res == null ? void 0 : res.json) == null ? void 0 : _b.call(res));
|
|
604
599
|
logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "Register fetch response", responseData);
|
|
605
600
|
if ((responseData == null ? void 0 : responseData.code) === 0) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/core/queueManager.ts
|
|
2
|
-
import { isBrowser
|
|
2
|
+
import { isBrowser } from "../utils/environment";
|
|
3
3
|
import { logDebug, safeStringify } from "../utils/tools";
|
|
4
4
|
var QueueManager = class {
|
|
5
5
|
constructor(options) {
|
|
@@ -90,10 +90,7 @@ var QueueManager = class {
|
|
|
90
90
|
*/
|
|
91
91
|
async loadFromStorage() {
|
|
92
92
|
try {
|
|
93
|
-
|
|
94
|
-
if (isWeChatMiniProgram()) {
|
|
95
|
-
stored = wx.getStorageSync(this.storageKey);
|
|
96
|
-
} else if (isBrowser()) {
|
|
93
|
+
if (isBrowser()) {
|
|
97
94
|
if (!this.localforage) {
|
|
98
95
|
await this.loadLocalForage();
|
|
99
96
|
}
|
|
@@ -122,13 +119,6 @@ var QueueManager = class {
|
|
|
122
119
|
}
|
|
123
120
|
return;
|
|
124
121
|
}
|
|
125
|
-
if (stored) {
|
|
126
|
-
const parsed = JSON.parse(stored);
|
|
127
|
-
if (Array.isArray(parsed)) {
|
|
128
|
-
this.queue = parsed.slice(0, this.opts.maxSize);
|
|
129
|
-
logDebug(!!this.opts.debug, `Loaded ${this.queue.length} events from storage`);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
122
|
} catch (error) {
|
|
133
123
|
logDebug(!!this.opts.debug, "Failed to load queue from storage", error);
|
|
134
124
|
}
|
|
@@ -139,9 +129,7 @@ var QueueManager = class {
|
|
|
139
129
|
saveToStorage() {
|
|
140
130
|
try {
|
|
141
131
|
const data = safeStringify(this.queue);
|
|
142
|
-
if (
|
|
143
|
-
wx.setStorageSync(this.storageKey, data);
|
|
144
|
-
} else if (isBrowser()) {
|
|
132
|
+
if (isBrowser()) {
|
|
145
133
|
const saveToLocalStorage = () => {
|
|
146
134
|
if (typeof localStorage !== "undefined") {
|
|
147
135
|
localStorage.setItem(this.storageKey, data);
|
|
@@ -182,9 +170,7 @@ var QueueManager = class {
|
|
|
182
170
|
*/
|
|
183
171
|
removeFromStorage() {
|
|
184
172
|
try {
|
|
185
|
-
if (
|
|
186
|
-
wx.removeStorageSync(this.storageKey);
|
|
187
|
-
} else if (isBrowser()) {
|
|
173
|
+
if (isBrowser()) {
|
|
188
174
|
const removeFromLocalStorage = () => {
|
|
189
175
|
if (typeof localStorage !== "undefined") {
|
|
190
176
|
localStorage.removeItem(this.storageKey);
|
|
@@ -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";
|
|
@@ -15,18 +15,25 @@ var BeaconTransport = class {
|
|
|
15
15
|
}
|
|
16
16
|
async send(token, payload) {
|
|
17
17
|
var _a, _b, _c, _d, _e;
|
|
18
|
-
let
|
|
19
|
-
const endpoint = `${getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}
|
|
18
|
+
let sendBody = "";
|
|
19
|
+
const endpoint = `${getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}`;
|
|
20
20
|
let contentType = "application/json";
|
|
21
|
-
if ((
|
|
21
|
+
if ((_b = this.opts) == null ? void 0 : _b.enableGzip) {
|
|
22
22
|
const compressedItems = await gzipCompress(safeStringify(payload.items));
|
|
23
|
-
|
|
23
|
+
sendBody = safeStringify({
|
|
24
24
|
...payload,
|
|
25
25
|
items: compressedItems
|
|
26
26
|
});
|
|
27
27
|
contentType = "application/json; charset=utf-8";
|
|
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
|
+
});
|
|
28
35
|
}
|
|
29
|
-
const blob = new Blob([
|
|
36
|
+
const blob = new Blob([sendBody], { type: contentType });
|
|
30
37
|
const success = navigator.sendBeacon(endpoint, blob);
|
|
31
38
|
logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "sendBeacon result", success);
|
|
32
39
|
if (!success) {
|
|
@@ -14,27 +14,27 @@ var PixelImageTransport = class {
|
|
|
14
14
|
return isBrowser() && typeof Image !== "undefined";
|
|
15
15
|
}
|
|
16
16
|
async send(token, payload) {
|
|
17
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k
|
|
17
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
18
18
|
const body = safeStringify(payload.items);
|
|
19
19
|
const endpoint = `${getPixelBatchApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}?token=${token}`;
|
|
20
20
|
const param = "items";
|
|
21
21
|
const maxLen = ((_b = this.opts) == null ? void 0 : _b.maxPixelUrlLen) || 8192;
|
|
22
22
|
let compressedBody;
|
|
23
|
-
if ((
|
|
23
|
+
if ((_c = this.opts) == null ? void 0 : _c.enableGzip) {
|
|
24
24
|
const t = now();
|
|
25
|
-
logDebug(!!((
|
|
25
|
+
logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "PixelImage request gzip compress body: ", body);
|
|
26
26
|
compressedBody = await gzipCompress(body);
|
|
27
|
-
logDebug(!!((
|
|
28
|
-
logDebug(!!((
|
|
29
|
-
logDebug(!!((
|
|
27
|
+
logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "PixelImage request gzip compress cost: ", now() - t);
|
|
28
|
+
logDebug(!!((_f = this.opts) == null ? void 0 : _f.debug), `original body size: ${body.length}, compressed body size: ${compressedBody.length}`);
|
|
29
|
+
logDebug(!!((_g = this.opts) == null ? void 0 : _g.debug), "PixelImage request gzip compress body: ", compressedBody);
|
|
30
30
|
} else {
|
|
31
31
|
compressedBody = await convert2Base64(body);
|
|
32
32
|
}
|
|
33
33
|
const cacheBuster = `_=${Date.now()}`;
|
|
34
|
-
const qs = `appId=${((
|
|
34
|
+
const qs = `appId=${((_h = this.opts) == null ? void 0 : _h.appId) || ""}&appStage=${((_i = this.opts) == null ? void 0 : _i.logStage) || ""}&${param}=${encodeURIComponent(compressedBody)}&gzip=${((_j = this.opts) == null ? void 0 : _j.enableGzip) ? 1 : 0}&${cacheBuster}`;
|
|
35
35
|
const url = endpoint.includes("?") ? `${endpoint}&${qs}` : `${endpoint}?${qs}`;
|
|
36
36
|
if (url.length > maxLen) {
|
|
37
|
-
logDebug(!!((
|
|
37
|
+
logDebug(!!((_k = this.opts) == null ? void 0 : _k.debug), `URL too long (${url.length} > ${maxLen})`);
|
|
38
38
|
}
|
|
39
39
|
return new Promise((resolve, reject) => {
|
|
40
40
|
const img = new Image();
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// src/transport/transportAdapter.ts
|
|
2
|
-
import { isWeChatMiniProgram } from "../utils/environment";
|
|
3
2
|
var TransportAdapter = class {
|
|
4
3
|
constructor(opts) {
|
|
5
4
|
this.opts = opts;
|
|
@@ -11,10 +10,6 @@ var TransportAdapter = class {
|
|
|
11
10
|
return TransportAdapter.instance;
|
|
12
11
|
}
|
|
13
12
|
async getTransporter() {
|
|
14
|
-
if (isWeChatMiniProgram()) {
|
|
15
|
-
const mod = await import("./wechatTransport");
|
|
16
|
-
return new mod.WechatTransport(this.opts);
|
|
17
|
-
}
|
|
18
13
|
const beaconMod = await import("./beaconTransport");
|
|
19
14
|
const pixelMod = await import("./pixelImageTransport");
|
|
20
15
|
const beacon = new beaconMod.BeaconTransport(this.opts);
|
|
@@ -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
|
+
};
|
|
@@ -6,25 +6,7 @@ function isBrowser() {
|
|
|
6
6
|
return false;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
-
function isWeChatMiniProgram() {
|
|
10
|
-
try {
|
|
11
|
-
return typeof wx !== "undefined" && typeof wx.getSystemInfo === "function";
|
|
12
|
-
} catch (e) {
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
9
|
function getCurrentUrl() {
|
|
17
|
-
if (isWeChatMiniProgram()) {
|
|
18
|
-
try {
|
|
19
|
-
const pages = getCurrentPages();
|
|
20
|
-
if (pages && pages.length > 0) {
|
|
21
|
-
const currentPage = pages[pages.length - 1];
|
|
22
|
-
return currentPage.route || "";
|
|
23
|
-
}
|
|
24
|
-
} catch (e) {
|
|
25
|
-
return "";
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
10
|
if (isBrowser()) {
|
|
29
11
|
return window.location.href;
|
|
30
12
|
}
|
|
@@ -34,24 +16,7 @@ function getEnvironmentInfo() {
|
|
|
34
16
|
const envInfo = {
|
|
35
17
|
platform: "unknown"
|
|
36
18
|
};
|
|
37
|
-
if (
|
|
38
|
-
envInfo.platform = "wechat";
|
|
39
|
-
try {
|
|
40
|
-
const systemInfo = wx.getSystemInfoSync();
|
|
41
|
-
envInfo.systemInfo = {
|
|
42
|
-
brand: systemInfo.brand,
|
|
43
|
-
model: systemInfo.model,
|
|
44
|
-
system: systemInfo.system,
|
|
45
|
-
platform: systemInfo.platform,
|
|
46
|
-
version: systemInfo.version,
|
|
47
|
-
SDKVersion: systemInfo.SDKVersion
|
|
48
|
-
};
|
|
49
|
-
envInfo.screenWidth = systemInfo.screenWidth;
|
|
50
|
-
envInfo.screenHeight = systemInfo.screenHeight;
|
|
51
|
-
envInfo.language = systemInfo.language;
|
|
52
|
-
} catch (e) {
|
|
53
|
-
}
|
|
54
|
-
} else if (isBrowser()) {
|
|
19
|
+
if (isBrowser()) {
|
|
55
20
|
envInfo.platform = "browser";
|
|
56
21
|
envInfo.userAgent = navigator.userAgent;
|
|
57
22
|
envInfo.screenWidth = window.screen.width;
|
|
@@ -132,15 +97,6 @@ function collectEnvironmentTags() {
|
|
|
132
97
|
tags.screenWidth = envInfo.screenWidth;
|
|
133
98
|
tags.screenHeight = envInfo.screenHeight;
|
|
134
99
|
tags.language = envInfo.language;
|
|
135
|
-
} else if (envInfo.platform === "wechat" && envInfo.systemInfo) {
|
|
136
|
-
tags.brand = envInfo.systemInfo.brand;
|
|
137
|
-
tags.model = envInfo.systemInfo.model;
|
|
138
|
-
tags.system = envInfo.systemInfo.system;
|
|
139
|
-
tags.wechatVersion = envInfo.systemInfo.version;
|
|
140
|
-
tags.SDKVersion = envInfo.systemInfo.SDKVersion;
|
|
141
|
-
tags.screenWidth = envInfo.screenWidth;
|
|
142
|
-
tags.screenHeight = envInfo.screenHeight;
|
|
143
|
-
tags.language = envInfo.language;
|
|
144
100
|
}
|
|
145
101
|
return tags;
|
|
146
102
|
}
|
|
@@ -149,6 +105,5 @@ export {
|
|
|
149
105
|
getCurrentUrl,
|
|
150
106
|
getEnvironmentInfo,
|
|
151
107
|
isBrowser,
|
|
152
|
-
isWeChatMiniProgram,
|
|
153
108
|
parseBrowserInfo
|
|
154
109
|
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
export type HttpClientResponse = {
|
|
2
|
-
type: 'xhr' | 'fetch'
|
|
2
|
+
type: 'xhr' | 'fetch';
|
|
3
3
|
response?: any;
|
|
4
4
|
error?: any;
|
|
5
5
|
};
|
|
6
6
|
export declare class HttpClient {
|
|
7
7
|
/**
|
|
8
|
-
* 发送 POST
|
|
8
|
+
* 发送 POST 请求,需要兼容浏览器
|
|
9
9
|
*/
|
|
10
10
|
static post(url: string, data: any, token: string): Promise<HttpClientResponse>;
|
|
11
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?;
|
|
@@ -29,8 +30,6 @@ export declare class LoggerSDK {
|
|
|
29
30
|
private offJs?;
|
|
30
31
|
private offPromise?;
|
|
31
32
|
private offResource?;
|
|
32
|
-
private offWxError?;
|
|
33
|
-
private offWxUnhandled?;
|
|
34
33
|
private constructor();
|
|
35
34
|
static getInstance(): LoggerSDK;
|
|
36
35
|
/**
|
|
@@ -42,7 +41,7 @@ export declare class LoggerSDK {
|
|
|
42
41
|
*/
|
|
43
42
|
identify(userId: string): void;
|
|
44
43
|
/** 设置店铺编码 */
|
|
45
|
-
setStoreCode(storeCode: string): void;
|
|
44
|
+
setStoreCode(storeCode: string, securityType: SecurityType): void;
|
|
46
45
|
/** 设置日志 stage(env) */
|
|
47
46
|
setStage(stage: Env): void;
|
|
48
47
|
private flattenEnvTags;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
export type Env = 'develop' | 'testing' | 'product';
|
|
3
3
|
export type Stage = 'develop' | 'testing' | 'sit' | 'yace' | 'product';
|
|
4
4
|
/** 平台类型 */
|
|
5
|
-
export type PlatformType = 'browser' | '
|
|
5
|
+
export type PlatformType = 'browser' | 'unknown';
|
|
6
6
|
/** 环境信息 */
|
|
7
7
|
export interface EnvironmentInfo {
|
|
8
8
|
platform: PlatformType;
|
|
@@ -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,11 @@ 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;
|
|
48
54
|
items: ReportItem[];
|
|
49
55
|
};
|
|
50
56
|
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 */
|
|
@@ -17,8 +18,6 @@ export interface SDKOptions {
|
|
|
17
18
|
batchSize?: number;
|
|
18
19
|
/** 是否启用 gzip 压缩,默认 true (仅在批量模式下有效) */
|
|
19
20
|
enableGzip?: boolean;
|
|
20
|
-
/** 批量模式下启用 gzip 压缩的最小 items 数量,默认 2 */
|
|
21
|
-
gzipBatchMinSize?: number;
|
|
22
21
|
/** 最大像素图 URL 长度,默认 1900 */
|
|
23
22
|
maxPixelUrlLen?: number;
|
|
24
23
|
/** 批量上报时间间隔(毫秒),默认 5000 */
|
|
@@ -41,6 +40,8 @@ export interface SDKOptions {
|
|
|
41
40
|
userId?: string;
|
|
42
41
|
/** 可选:店铺编码 */
|
|
43
42
|
storeCode?: string;
|
|
43
|
+
/** 安全类型 */
|
|
44
|
+
securityType?: SecurityType;
|
|
44
45
|
/** token */
|
|
45
46
|
token: string;
|
|
46
47
|
/** 全局采样率 0.0-1.0,默认 1.0(全量) */
|
|
@@ -55,7 +56,6 @@ export interface SDKOptions {
|
|
|
55
56
|
js?: boolean;
|
|
56
57
|
promise?: boolean;
|
|
57
58
|
resource?: boolean;
|
|
58
|
-
wechat?: boolean;
|
|
59
59
|
};
|
|
60
60
|
enableAutoCapture?: boolean;
|
|
61
61
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluve/logger-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"description": "logger sdk",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"logger"
|
|
@@ -12,14 +12,6 @@
|
|
|
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
|
-
},
|
|
23
15
|
"typesVersions": {
|
|
24
16
|
"*": {
|
|
25
17
|
"index": ["dist/types/index.d.ts"],
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
// src/capture/wechatError.ts
|
|
2
|
-
import { parseStack } from "../stack/stacktrace";
|
|
3
|
-
import { isWeChatMiniProgram } from "../utils/environment";
|
|
4
|
-
import { logDebug } from "../utils/tools";
|
|
5
|
-
function registerWechatErrorCapture(debug, callback) {
|
|
6
|
-
if (!isWeChatMiniProgram())
|
|
7
|
-
return void 0;
|
|
8
|
-
try {
|
|
9
|
-
const wxAny = globalThis.wx;
|
|
10
|
-
if (wxAny && typeof wxAny.onError === "function") {
|
|
11
|
-
const handler = async (error) => {
|
|
12
|
-
logDebug(debug, "registerWechatErrorCapture onError", error);
|
|
13
|
-
const msg = String((error == null ? void 0 : error.message) ?? error);
|
|
14
|
-
const err = error instanceof Error ? error : new Error(msg);
|
|
15
|
-
const stack = await parseStack(err);
|
|
16
|
-
callback({
|
|
17
|
-
type: "js",
|
|
18
|
-
message: err.message,
|
|
19
|
-
stack,
|
|
20
|
-
throwable: err.stack || ""
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
wxAny.onError(handler);
|
|
24
|
-
return () => {
|
|
25
|
-
try {
|
|
26
|
-
wxAny.offError && wxAny.offError(handler);
|
|
27
|
-
} catch {
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
} catch (e) {
|
|
32
|
-
logDebug(debug, "registerWechatErrorCapture attach failed", e);
|
|
33
|
-
}
|
|
34
|
-
return void 0;
|
|
35
|
-
}
|
|
36
|
-
function registerWechatUnhandledCapture(debug, callback) {
|
|
37
|
-
if (!isWeChatMiniProgram())
|
|
38
|
-
return void 0;
|
|
39
|
-
try {
|
|
40
|
-
const wxAny = globalThis.wx;
|
|
41
|
-
if (wxAny && typeof wxAny.onUnhandledRejection === "function") {
|
|
42
|
-
const handler = async (res) => {
|
|
43
|
-
const reason = res == null ? void 0 : res.reason;
|
|
44
|
-
logDebug(debug, "registerWechatUnhandledCapture onUnhandledRejection", reason);
|
|
45
|
-
const err = reason instanceof Error ? reason : new Error(String(reason));
|
|
46
|
-
const stack = await parseStack(err);
|
|
47
|
-
callback({
|
|
48
|
-
type: "promise",
|
|
49
|
-
message: err.message,
|
|
50
|
-
stack,
|
|
51
|
-
throwable: err.stack || ""
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
wxAny.onUnhandledRejection(handler);
|
|
55
|
-
return () => {
|
|
56
|
-
try {
|
|
57
|
-
wxAny.offUnhandledRejection && wxAny.offUnhandledRejection(handler);
|
|
58
|
-
} catch {
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
} catch (e) {
|
|
63
|
-
logDebug(debug, "registerWechatUnhandledCapture attach failed", e);
|
|
64
|
-
}
|
|
65
|
-
return void 0;
|
|
66
|
-
}
|
|
67
|
-
export {
|
|
68
|
-
registerWechatErrorCapture,
|
|
69
|
-
registerWechatUnhandledCapture
|
|
70
|
-
};
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
// src/transport/wechatTransport.ts
|
|
2
|
-
import { gzipCompress } from "../compress/compression";
|
|
3
|
-
import { getReportApi } from "../config";
|
|
4
|
-
import { isWeChatMiniProgram } from "../utils/environment";
|
|
5
|
-
import { logDebug, now, safeStringify } from "../utils/tools";
|
|
6
|
-
var WechatTransport = class {
|
|
7
|
-
constructor(opts) {
|
|
8
|
-
/** 传输器名称 */
|
|
9
|
-
this.name = "wechat";
|
|
10
|
-
this.opts = opts;
|
|
11
|
-
}
|
|
12
|
-
// eslint-disable-next-line class-methods-use-this
|
|
13
|
-
isSupported() {
|
|
14
|
-
return isWeChatMiniProgram();
|
|
15
|
-
}
|
|
16
|
-
async send(token, payload) {
|
|
17
|
-
var _a, _b, _c, _d, _e;
|
|
18
|
-
let body = typeof payload === "string" ? payload : safeStringify(payload);
|
|
19
|
-
const endpoint = `${getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}?token=${token}`;
|
|
20
|
-
const timeout = 1e4;
|
|
21
|
-
let contentType = "application/json";
|
|
22
|
-
if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && payload.items.length >= ((_c = this.opts) == null ? void 0 : _c.gzipBatchMinSize)) {
|
|
23
|
-
const t = now();
|
|
24
|
-
logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "WeChat request enable gzip compress: ", t);
|
|
25
|
-
const compressedItems = await gzipCompress(safeStringify(payload.items));
|
|
26
|
-
body = safeStringify({
|
|
27
|
-
...payload,
|
|
28
|
-
items: compressedItems
|
|
29
|
-
});
|
|
30
|
-
logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "WeChat request gzip compress cost: ", now() - t);
|
|
31
|
-
contentType = "application/json; charset=utf-8";
|
|
32
|
-
}
|
|
33
|
-
return new Promise((resolve, reject) => {
|
|
34
|
-
var _a2;
|
|
35
|
-
let timeoutId = null;
|
|
36
|
-
let settled = false;
|
|
37
|
-
timeoutId = setTimeout(() => {
|
|
38
|
-
if (!settled) {
|
|
39
|
-
settled = true;
|
|
40
|
-
reject(new Error(`WeChat request timeout after ${timeout}ms`));
|
|
41
|
-
}
|
|
42
|
-
}, timeout);
|
|
43
|
-
wx.request({
|
|
44
|
-
url: endpoint,
|
|
45
|
-
method: "POST",
|
|
46
|
-
data: body,
|
|
47
|
-
header: {
|
|
48
|
-
"Content-Type": contentType,
|
|
49
|
-
token: ((_a2 = this.opts) == null ? void 0 : _a2.token) || ""
|
|
50
|
-
},
|
|
51
|
-
success(res) {
|
|
52
|
-
if (timeoutId)
|
|
53
|
-
clearTimeout(timeoutId);
|
|
54
|
-
if (!settled) {
|
|
55
|
-
settled = true;
|
|
56
|
-
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
57
|
-
resolve();
|
|
58
|
-
} else {
|
|
59
|
-
reject(new Error(`HTTP ${res.statusCode}`));
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
fail(err) {
|
|
64
|
-
var _a3;
|
|
65
|
-
if (timeoutId)
|
|
66
|
-
clearTimeout(timeoutId);
|
|
67
|
-
if (!settled) {
|
|
68
|
-
settled = true;
|
|
69
|
-
logDebug(!!((_a3 = this.opts) == null ? void 0 : _a3.debug), "WeChat request failed", err);
|
|
70
|
-
reject(new Error(`WeChat request failed: ${err.errMsg || "unknown error"}`));
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
export {
|
|
78
|
-
WechatTransport
|
|
79
|
-
};
|