@pluve/mp-logger-sdk 0.0.1 → 0.0.2
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/dist/cjs/core/httpClient.js +2 -2
- package/dist/cjs/core/loggerSDK.js +13 -3
- package/dist/cjs/transport/wechatTransport.js +5 -7
- package/dist/cjs/types/securityType.js +7 -10
- package/dist/esm/core/httpClient.js +2 -2
- package/dist/esm/core/loggerSDK.js +13 -3
- package/dist/esm/transport/wechatTransport.js +5 -7
- package/dist/esm/types/securityType.js +7 -10
- package/dist/types/core/loggerSDK.d.ts +2 -1
- package/dist/types/transport/transport.d.ts +1 -1
- package/dist/types/transport/wechatTransport.d.ts +1 -1
- package/dist/types/types/logEvent.d.ts +3 -0
- package/dist/types/types/sdkOptions.d.ts +3 -0
- package/dist/types/types/securityType.d.ts +6 -6
- package/package.json +1 -1
|
@@ -34,6 +34,7 @@ var import_httpClient = require("./httpClient");
|
|
|
34
34
|
var import_queueManager = require("./queueManager");
|
|
35
35
|
var import_retryManager = require("./retryManager");
|
|
36
36
|
var import_innerMD5 = require("../utils/innerMD5");
|
|
37
|
+
var import_securityType = require("../types/securityType");
|
|
37
38
|
var LoggerSDK = class {
|
|
38
39
|
constructor() {
|
|
39
40
|
/** 事件序列编号,用于事件去重 */
|
|
@@ -93,10 +94,12 @@ var LoggerSDK = class {
|
|
|
93
94
|
// 默认启用
|
|
94
95
|
maxRetries: options.maxRetries || 3,
|
|
95
96
|
retryDelay: options.retryDelay || 1e3,
|
|
97
|
+
// 默认 1 秒重试一次
|
|
96
98
|
retryBackoff: options.retryBackoff !== false,
|
|
97
99
|
// 默认启用
|
|
98
100
|
userId: options.userId,
|
|
99
101
|
storeCode: options.storeCode,
|
|
102
|
+
securityType: options.securityType || import_securityType.SecurityType.BASE,
|
|
100
103
|
token: options.token,
|
|
101
104
|
sampleRate: typeof options.sampleRate === "number" ? options.sampleRate : 1,
|
|
102
105
|
levelSampleRate: options.levelSampleRate || {
|
|
@@ -163,11 +166,12 @@ var LoggerSDK = class {
|
|
|
163
166
|
* 设置 token 信息
|
|
164
167
|
* @param token token
|
|
165
168
|
*/
|
|
166
|
-
setToken(token) {
|
|
169
|
+
setToken(token, securityType) {
|
|
167
170
|
var _a;
|
|
168
171
|
(0, import_tools.logDebug)(!!((_a = this.opts) == null ? void 0 : _a.debug), "setToken", token);
|
|
169
172
|
if (this.opts) {
|
|
170
173
|
this.opts.token = token;
|
|
174
|
+
this.opts.securityType = securityType;
|
|
171
175
|
}
|
|
172
176
|
}
|
|
173
177
|
/**
|
|
@@ -447,11 +451,14 @@ var LoggerSDK = class {
|
|
|
447
451
|
*/
|
|
448
452
|
async sendEvent(event) {
|
|
449
453
|
const sendFn = async () => {
|
|
454
|
+
var _a;
|
|
450
455
|
const transporter = this.transporter || await import_transportAdapter.TransportAdapter.getInstance(this.opts).getTransporter();
|
|
451
456
|
this.transporter = transporter;
|
|
452
|
-
await transporter.send(
|
|
457
|
+
await transporter.send({
|
|
453
458
|
appId: event.appId,
|
|
454
459
|
appStage: event.stage,
|
|
460
|
+
token: this.generateToken(),
|
|
461
|
+
securityType: ((_a = this.opts) == null ? void 0 : _a.securityType) || import_securityType.SecurityType.BASE,
|
|
455
462
|
items: [
|
|
456
463
|
{
|
|
457
464
|
level: event.level === "FATAL" ? "ERROR" : event.level,
|
|
@@ -498,11 +505,14 @@ var LoggerSDK = class {
|
|
|
498
505
|
if (chunk.length > 0) {
|
|
499
506
|
const batchId = `batch_${(0, import_tools.now)()}_${Math.random().toString(36).substring(2, 9)}_${key}_${i}`;
|
|
500
507
|
const sendFn = async () => {
|
|
508
|
+
var _a2;
|
|
501
509
|
const transporter = this.transporter || await import_transportAdapter.TransportAdapter.getInstance(this.opts).getTransporter();
|
|
502
510
|
this.transporter = transporter;
|
|
503
|
-
await transporter.send(
|
|
511
|
+
await transporter.send({
|
|
504
512
|
appId: chunk[0].appId,
|
|
505
513
|
appStage: chunk[0].stage,
|
|
514
|
+
token: this.generateToken(),
|
|
515
|
+
securityType: ((_a2 = this.opts) == null ? void 0 : _a2.securityType) || import_securityType.SecurityType.BASE,
|
|
506
516
|
items: chunk.map((event) => ({
|
|
507
517
|
level: event.level === "FATAL" ? "ERROR" : event.level,
|
|
508
518
|
traceId: event.traceId,
|
|
@@ -35,10 +35,10 @@ var WechatTransport = class {
|
|
|
35
35
|
isSupported() {
|
|
36
36
|
return true;
|
|
37
37
|
}
|
|
38
|
-
async send(
|
|
38
|
+
async send(payload) {
|
|
39
39
|
var _a, _b, _c, _d, _e;
|
|
40
40
|
let body = typeof payload === "string" ? payload : (0, import_tools.safeStringify)(payload);
|
|
41
|
-
const endpoint =
|
|
41
|
+
const endpoint = (0, import_config.getReportApi)(((_a = this.opts) == null ? void 0 : _a.env) || "develop");
|
|
42
42
|
const timeout = 1e4;
|
|
43
43
|
let contentType = "application/json";
|
|
44
44
|
if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && payload.items.length >= ((_c = this.opts) == null ? void 0 : _c.gzipBatchMinSize)) {
|
|
@@ -53,7 +53,6 @@ var WechatTransport = class {
|
|
|
53
53
|
contentType = "application/json; charset=utf-8";
|
|
54
54
|
}
|
|
55
55
|
return new Promise((resolve, reject) => {
|
|
56
|
-
var _a2;
|
|
57
56
|
let timeoutId = null;
|
|
58
57
|
let settled = false;
|
|
59
58
|
timeoutId = setTimeout(() => {
|
|
@@ -67,8 +66,7 @@ var WechatTransport = class {
|
|
|
67
66
|
method: "POST",
|
|
68
67
|
data: body,
|
|
69
68
|
header: {
|
|
70
|
-
"Content-Type": contentType
|
|
71
|
-
token: ((_a2 = this.opts) == null ? void 0 : _a2.token) || ""
|
|
69
|
+
"Content-Type": contentType
|
|
72
70
|
},
|
|
73
71
|
success(res) {
|
|
74
72
|
if (timeoutId)
|
|
@@ -83,12 +81,12 @@ var WechatTransport = class {
|
|
|
83
81
|
}
|
|
84
82
|
},
|
|
85
83
|
fail(err) {
|
|
86
|
-
var
|
|
84
|
+
var _a2;
|
|
87
85
|
if (timeoutId)
|
|
88
86
|
clearTimeout(timeoutId);
|
|
89
87
|
if (!settled) {
|
|
90
88
|
settled = true;
|
|
91
|
-
(0, import_tools.logDebug)(!!((
|
|
89
|
+
(0, import_tools.logDebug)(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "WeChat request failed", err);
|
|
92
90
|
reject(new Error(`WeChat request failed: ${err.errMsg || "unknown error"}`));
|
|
93
91
|
}
|
|
94
92
|
}
|
|
@@ -22,13 +22,10 @@ __export(securityType_exports, {
|
|
|
22
22
|
SecurityType: () => SecurityType
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(securityType_exports);
|
|
25
|
-
var SecurityType = {
|
|
26
|
-
ACCOUNT_CENTER
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
BASE: "BASE"
|
|
33
|
-
// BASE(兜底MD5)
|
|
34
|
-
};
|
|
25
|
+
var SecurityType = /* @__PURE__ */ ((SecurityType2) => {
|
|
26
|
+
SecurityType2["ACCOUNT_CENTER"] = "2B";
|
|
27
|
+
SecurityType2["EHP"] = "EHP";
|
|
28
|
+
SecurityType2["CUSTOMER"] = "CUSTOMER";
|
|
29
|
+
SecurityType2["BASE"] = "BASE";
|
|
30
|
+
return SecurityType2;
|
|
31
|
+
})(SecurityType || {});
|
|
@@ -10,6 +10,7 @@ import { HttpClient } from "./httpClient";
|
|
|
10
10
|
import { QueueManager } from "./queueManager";
|
|
11
11
|
import { RetryManager } from "./retryManager";
|
|
12
12
|
import { md5 } from "../utils/innerMD5";
|
|
13
|
+
import { SecurityType } from "../types/securityType";
|
|
13
14
|
var LoggerSDK = class {
|
|
14
15
|
constructor() {
|
|
15
16
|
/** 事件序列编号,用于事件去重 */
|
|
@@ -69,10 +70,12 @@ var LoggerSDK = class {
|
|
|
69
70
|
// 默认启用
|
|
70
71
|
maxRetries: options.maxRetries || 3,
|
|
71
72
|
retryDelay: options.retryDelay || 1e3,
|
|
73
|
+
// 默认 1 秒重试一次
|
|
72
74
|
retryBackoff: options.retryBackoff !== false,
|
|
73
75
|
// 默认启用
|
|
74
76
|
userId: options.userId,
|
|
75
77
|
storeCode: options.storeCode,
|
|
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 || {
|
|
@@ -139,11 +142,12 @@ var LoggerSDK = class {
|
|
|
139
142
|
* 设置 token 信息
|
|
140
143
|
* @param token token
|
|
141
144
|
*/
|
|
142
|
-
setToken(token) {
|
|
145
|
+
setToken(token, securityType) {
|
|
143
146
|
var _a;
|
|
144
147
|
logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "setToken", token);
|
|
145
148
|
if (this.opts) {
|
|
146
149
|
this.opts.token = token;
|
|
150
|
+
this.opts.securityType = securityType;
|
|
147
151
|
}
|
|
148
152
|
}
|
|
149
153
|
/**
|
|
@@ -423,11 +427,14 @@ var LoggerSDK = class {
|
|
|
423
427
|
*/
|
|
424
428
|
async sendEvent(event) {
|
|
425
429
|
const sendFn = async () => {
|
|
430
|
+
var _a;
|
|
426
431
|
const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
|
|
427
432
|
this.transporter = transporter;
|
|
428
|
-
await transporter.send(
|
|
433
|
+
await transporter.send({
|
|
429
434
|
appId: event.appId,
|
|
430
435
|
appStage: event.stage,
|
|
436
|
+
token: this.generateToken(),
|
|
437
|
+
securityType: ((_a = this.opts) == null ? void 0 : _a.securityType) || SecurityType.BASE,
|
|
431
438
|
items: [
|
|
432
439
|
{
|
|
433
440
|
level: event.level === "FATAL" ? "ERROR" : event.level,
|
|
@@ -474,11 +481,14 @@ var LoggerSDK = class {
|
|
|
474
481
|
if (chunk.length > 0) {
|
|
475
482
|
const batchId = `batch_${now()}_${Math.random().toString(36).substring(2, 9)}_${key}_${i}`;
|
|
476
483
|
const sendFn = async () => {
|
|
484
|
+
var _a2;
|
|
477
485
|
const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
|
|
478
486
|
this.transporter = transporter;
|
|
479
|
-
await transporter.send(
|
|
487
|
+
await transporter.send({
|
|
480
488
|
appId: chunk[0].appId,
|
|
481
489
|
appStage: chunk[0].stage,
|
|
490
|
+
token: this.generateToken(),
|
|
491
|
+
securityType: ((_a2 = this.opts) == null ? void 0 : _a2.securityType) || SecurityType.BASE,
|
|
482
492
|
items: chunk.map((event) => ({
|
|
483
493
|
level: event.level === "FATAL" ? "ERROR" : event.level,
|
|
484
494
|
traceId: event.traceId,
|
|
@@ -12,10 +12,10 @@ var WechatTransport = class {
|
|
|
12
12
|
isSupported() {
|
|
13
13
|
return true;
|
|
14
14
|
}
|
|
15
|
-
async send(
|
|
15
|
+
async send(payload) {
|
|
16
16
|
var _a, _b, _c, _d, _e;
|
|
17
17
|
let body = typeof payload === "string" ? payload : safeStringify(payload);
|
|
18
|
-
const endpoint =
|
|
18
|
+
const endpoint = getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop");
|
|
19
19
|
const timeout = 1e4;
|
|
20
20
|
let contentType = "application/json";
|
|
21
21
|
if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && payload.items.length >= ((_c = this.opts) == null ? void 0 : _c.gzipBatchMinSize)) {
|
|
@@ -30,7 +30,6 @@ var WechatTransport = class {
|
|
|
30
30
|
contentType = "application/json; charset=utf-8";
|
|
31
31
|
}
|
|
32
32
|
return new Promise((resolve, reject) => {
|
|
33
|
-
var _a2;
|
|
34
33
|
let timeoutId = null;
|
|
35
34
|
let settled = false;
|
|
36
35
|
timeoutId = setTimeout(() => {
|
|
@@ -44,8 +43,7 @@ var WechatTransport = class {
|
|
|
44
43
|
method: "POST",
|
|
45
44
|
data: body,
|
|
46
45
|
header: {
|
|
47
|
-
"Content-Type": contentType
|
|
48
|
-
token: ((_a2 = this.opts) == null ? void 0 : _a2.token) || ""
|
|
46
|
+
"Content-Type": contentType
|
|
49
47
|
},
|
|
50
48
|
success(res) {
|
|
51
49
|
if (timeoutId)
|
|
@@ -60,12 +58,12 @@ var WechatTransport = class {
|
|
|
60
58
|
}
|
|
61
59
|
},
|
|
62
60
|
fail(err) {
|
|
63
|
-
var
|
|
61
|
+
var _a2;
|
|
64
62
|
if (timeoutId)
|
|
65
63
|
clearTimeout(timeoutId);
|
|
66
64
|
if (!settled) {
|
|
67
65
|
settled = true;
|
|
68
|
-
logDebug(!!((
|
|
66
|
+
logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "WeChat request failed", err);
|
|
69
67
|
reject(new Error(`WeChat request failed: ${err.errMsg || "unknown error"}`));
|
|
70
68
|
}
|
|
71
69
|
}
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
// src/types/securityType.ts
|
|
2
|
-
var SecurityType = {
|
|
3
|
-
ACCOUNT_CENTER
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
BASE: "BASE"
|
|
10
|
-
// BASE(兜底MD5)
|
|
11
|
-
};
|
|
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 || {});
|
|
12
9
|
export {
|
|
13
10
|
SecurityType
|
|
14
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?;
|
|
@@ -38,7 +39,7 @@ export declare class LoggerSDK {
|
|
|
38
39
|
* 设置 token 信息
|
|
39
40
|
* @param token token
|
|
40
41
|
*/
|
|
41
|
-
setToken(token: string): void;
|
|
42
|
+
setToken(token: string, securityType: SecurityType): void;
|
|
42
43
|
/**
|
|
43
44
|
* 设置用户信息
|
|
44
45
|
*/
|
|
@@ -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,8 @@ export type BaseErrorInfo = {
|
|
|
45
46
|
export type ReportData = {
|
|
46
47
|
appId: string;
|
|
47
48
|
appStage: Stage;
|
|
49
|
+
token: string;
|
|
50
|
+
securityType: SecurityType;
|
|
48
51
|
items: ReportItem[];
|
|
49
52
|
};
|
|
50
53
|
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 */
|
|
@@ -41,6 +42,8 @@ export interface SDKOptions {
|
|
|
41
42
|
userId?: string;
|
|
42
43
|
/** 可选:店铺编码 */
|
|
43
44
|
storeCode?: string;
|
|
45
|
+
/** 安全类型 */
|
|
46
|
+
securityType?: SecurityType;
|
|
44
47
|
/** token */
|
|
45
48
|
token: string;
|
|
46
49
|
/** 全局采样率 0.0-1.0,默认 1.0(全量) */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export declare const SecurityType
|
|
2
|
-
ACCOUNT_CENTER
|
|
3
|
-
EHP
|
|
4
|
-
CUSTOMER
|
|
5
|
-
BASE
|
|
6
|
-
}
|
|
1
|
+
export declare const enum SecurityType {
|
|
2
|
+
ACCOUNT_CENTER = "2B",// 2B(账号中心)
|
|
3
|
+
EHP = "EHP",// EHP(医疗)
|
|
4
|
+
CUSTOMER = "CUSTOMER",// 2C(会员,这期不涉及先预留)
|
|
5
|
+
BASE = "BASE"
|
|
6
|
+
}
|