@pluve/logger-sdk 0.0.16 → 0.0.18
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 +3 -28
- package/dist/esm/core/loggerSDK.js +12 -26
- package/dist/esm/core/queueManager.js +4 -18
- package/dist/esm/transport/beaconTransport.js +7 -6
- package/dist/esm/transport/pixelImageTransport.js +10 -10
- package/dist/esm/transport/transportAdapter.js +0 -5
- package/dist/esm/utils/environment.js +1 -46
- package/dist/esm/utils/innerMD5.js +380 -0
- package/dist/types/core/httpClient.d.ts +3 -3
- package/dist/types/core/loggerSDK.d.ts +1 -2
- package/dist/types/core/queueManager.d.ts +1 -1
- package/dist/types/transport/beaconTransport.d.ts +1 -1
- package/dist/types/transport/pixelImageTransport.d.ts +1 -1
- package/dist/types/transport/transport.d.ts +1 -1
- package/dist/types/transport/wechatTransport.d.ts +1 -1
- package/dist/types/types/env.d.ts +3 -3
- package/dist/types/types/logEvent.d.ts +3 -3
- package/dist/types/types/logEventLevel.d.ts +1 -1
- package/dist/types/types/sdkOptions.d.ts +0 -3
- package/dist/types/types/trackOptions.d.ts +1 -1
- package/dist/types/utils/environment.d.ts +0 -1
- package/dist/types/utils/innerMD5.d.ts +70 -0
- package/package.json +3 -11
- 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,42 +1,17 @@
|
|
|
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
|
-
Authorization: `Bearer ${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();
|
|
37
12
|
xhr.open("POST", url, true);
|
|
38
13
|
xhr.setRequestHeader("Content-Type", "application/json");
|
|
39
|
-
xhr.setRequestHeader("
|
|
14
|
+
xhr.setRequestHeader("token", token);
|
|
40
15
|
xhr.onreadystatechange = () => {
|
|
41
16
|
if (xhr.readyState === 4) {
|
|
42
17
|
if (xhr.status === 200) {
|
|
@@ -61,7 +36,7 @@ var HttpClient = class {
|
|
|
61
36
|
body: JSON.stringify(data),
|
|
62
37
|
headers: {
|
|
63
38
|
"Content-Type": "application/json",
|
|
64
|
-
|
|
39
|
+
token
|
|
65
40
|
}
|
|
66
41
|
}).then((res) => resolve({ type: "fetch", response: res })).catch((err) => reject({ type: "fetch", error: err }));
|
|
67
42
|
});
|
|
@@ -5,13 +5,13 @@ 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";
|
|
12
11
|
import { HttpClient } from "./httpClient";
|
|
13
12
|
import { QueueManager } from "./queueManager";
|
|
14
13
|
import { RetryManager } from "./retryManager";
|
|
14
|
+
import { Md5 } from "../utils/innerMD5";
|
|
15
15
|
var LoggerSDK = class {
|
|
16
16
|
constructor() {
|
|
17
17
|
/** 事件序列编号,用于事件去重 */
|
|
@@ -52,8 +52,6 @@ var LoggerSDK = class {
|
|
|
52
52
|
/** 是否启用 gzip 压缩,默认 true */
|
|
53
53
|
enableGzip: options.enableGzip !== false,
|
|
54
54
|
// 默认启用,但仅在批量模式开启情况下有效
|
|
55
|
-
gzipBatchMinSize: options.gzipBatchMinSize || 2,
|
|
56
|
-
// 默认 2 条数据开启 gzip 压缩
|
|
57
55
|
/** 最大像素图 URL 长度,默认 1900 */
|
|
58
56
|
maxPixelUrlLen: options.maxPixelUrlLen || 8192,
|
|
59
57
|
// 批量上报配置(默认关闭,需显式开启)
|
|
@@ -97,8 +95,7 @@ var LoggerSDK = class {
|
|
|
97
95
|
autoCapture: options.autoCapture || {
|
|
98
96
|
js: true,
|
|
99
97
|
promise: true,
|
|
100
|
-
resource: true
|
|
101
|
-
wechat: true
|
|
98
|
+
resource: true
|
|
102
99
|
}
|
|
103
100
|
};
|
|
104
101
|
if (this.opts.enableBatch) {
|
|
@@ -121,7 +118,7 @@ var LoggerSDK = class {
|
|
|
121
118
|
this.sessionId = getSessionId();
|
|
122
119
|
this.envTags = collectEnvironmentTags();
|
|
123
120
|
this.initSDK(this.opts, (data) => {
|
|
124
|
-
var _a, _b, _c, _d, _e, _f
|
|
121
|
+
var _a, _b, _c, _d, _e, _f;
|
|
125
122
|
this.collectSwitch = data.collectSwitch;
|
|
126
123
|
this.collectLogLevel = data.collectLogLevel;
|
|
127
124
|
this.opts.sampleRate = data.samplingRate;
|
|
@@ -141,10 +138,6 @@ var LoggerSDK = class {
|
|
|
141
138
|
if (enableAuto && ac.resource !== false) {
|
|
142
139
|
this.offResource = registerResourceErrorCapture(!!((_f = this.opts) == null ? void 0 : _f.debug), this.trackInner.bind(this));
|
|
143
140
|
}
|
|
144
|
-
if (enableAuto && ac.wechat !== false) {
|
|
145
|
-
this.offWxError = registerWechatErrorCapture(!!((_g = this.opts) == null ? void 0 : _g.debug), this.trackInner.bind(this));
|
|
146
|
-
this.offWxUnhandled = registerWechatUnhandledCapture(!!((_h = this.opts) == null ? void 0 : _h.debug), this.trackInner.bind(this));
|
|
147
|
-
}
|
|
148
141
|
});
|
|
149
142
|
}
|
|
150
143
|
/**
|
|
@@ -232,7 +225,7 @@ var LoggerSDK = class {
|
|
|
232
225
|
/** 可选的结构化额外信息 */
|
|
233
226
|
// tags: this.envTags,
|
|
234
227
|
};
|
|
235
|
-
this.doTrack(logEvent);
|
|
228
|
+
await this.doTrack(logEvent);
|
|
236
229
|
}
|
|
237
230
|
trackInner(error) {
|
|
238
231
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
@@ -414,18 +407,14 @@ var LoggerSDK = class {
|
|
|
414
407
|
this.offResource();
|
|
415
408
|
this.offResource = void 0;
|
|
416
409
|
}
|
|
417
|
-
if (this.offWxError) {
|
|
418
|
-
this.offWxError();
|
|
419
|
-
this.offWxError = void 0;
|
|
420
|
-
}
|
|
421
|
-
if (this.offWxUnhandled) {
|
|
422
|
-
this.offWxUnhandled();
|
|
423
|
-
this.offWxUnhandled = void 0;
|
|
424
|
-
}
|
|
425
410
|
this.initialized = false;
|
|
426
411
|
this.sessionId = void 0;
|
|
427
412
|
LoggerSDK.instance = void 0;
|
|
428
413
|
}
|
|
414
|
+
generateToken() {
|
|
415
|
+
var _a;
|
|
416
|
+
return ((_a = this.opts) == null ? void 0 : _a.token) || Md5.hashStr(`${this.opts.appId}${this.opts.logStage}${now()}`);
|
|
417
|
+
}
|
|
429
418
|
// ========== 内部方法 ===========
|
|
430
419
|
/**
|
|
431
420
|
* 发送单个事件(带重试)
|
|
@@ -434,7 +423,7 @@ var LoggerSDK = class {
|
|
|
434
423
|
const sendFn = async () => {
|
|
435
424
|
const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
|
|
436
425
|
this.transporter = transporter;
|
|
437
|
-
await transporter.send({
|
|
426
|
+
await transporter.send(this.generateToken(), {
|
|
438
427
|
appId: event.appId,
|
|
439
428
|
appStage: event.stage,
|
|
440
429
|
items: [
|
|
@@ -485,7 +474,7 @@ var LoggerSDK = class {
|
|
|
485
474
|
const sendFn = async () => {
|
|
486
475
|
const transporter = this.transporter || await TransportAdapter.getInstance(this.opts).getTransporter();
|
|
487
476
|
this.transporter = transporter;
|
|
488
|
-
await transporter.send({
|
|
477
|
+
await transporter.send(this.generateToken(), {
|
|
489
478
|
appId: chunk[0].appId,
|
|
490
479
|
appStage: chunk[0].stage,
|
|
491
480
|
items: chunk.map((event) => ({
|
|
@@ -581,7 +570,7 @@ var LoggerSDK = class {
|
|
|
581
570
|
userId: opts.userId,
|
|
582
571
|
storeCode: opts.storeCode
|
|
583
572
|
},
|
|
584
|
-
|
|
573
|
+
this.generateToken()
|
|
585
574
|
).then(async (response) => {
|
|
586
575
|
var _a2, _b, _c;
|
|
587
576
|
logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "Register success", response);
|
|
@@ -591,10 +580,7 @@ var LoggerSDK = class {
|
|
|
591
580
|
const { type, response: res, error } = response;
|
|
592
581
|
let isSuccess = false;
|
|
593
582
|
let data;
|
|
594
|
-
if (type === "
|
|
595
|
-
isSuccess = true;
|
|
596
|
-
data = res.data.data;
|
|
597
|
-
} else if (type === "fetch") {
|
|
583
|
+
if (type === "fetch") {
|
|
598
584
|
const responseData = await ((_b = res == null ? void 0 : res.json) == null ? void 0 : _b.call(res));
|
|
599
585
|
logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "Register fetch response", responseData);
|
|
600
586
|
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);
|
|
@@ -13,24 +13,25 @@ var BeaconTransport = class {
|
|
|
13
13
|
isSupported() {
|
|
14
14
|
return isBrowser() && typeof navigator !== "undefined" && navigator.sendBeacon && typeof navigator.sendBeacon === "function";
|
|
15
15
|
}
|
|
16
|
-
async send(payload) {
|
|
17
|
-
var _a, _b, _c, _d
|
|
16
|
+
async send(token, payload) {
|
|
17
|
+
var _a, _b, _c, _d;
|
|
18
18
|
let body = typeof payload === "string" ? payload : safeStringify(payload);
|
|
19
|
-
const endpoint = getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")
|
|
19
|
+
const endpoint = `${getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}?token=${token}`;
|
|
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
|
body = safeStringify({
|
|
24
24
|
...payload,
|
|
25
25
|
items: compressedItems
|
|
26
26
|
});
|
|
27
27
|
contentType = "application/json; charset=utf-8";
|
|
28
|
+
} else {
|
|
28
29
|
}
|
|
29
30
|
const blob = new Blob([body], { type: contentType });
|
|
30
31
|
const success = navigator.sendBeacon(endpoint, blob);
|
|
31
|
-
logDebug(!!((
|
|
32
|
+
logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "sendBeacon result", success);
|
|
32
33
|
if (!success) {
|
|
33
|
-
logDebug(!!((
|
|
34
|
+
logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "sendBeacon failed (queue full or other error)");
|
|
34
35
|
}
|
|
35
36
|
return Promise.resolve();
|
|
36
37
|
}
|
|
@@ -13,28 +13,28 @@ var PixelImageTransport = class {
|
|
|
13
13
|
isSupported() {
|
|
14
14
|
return isBrowser() && typeof Image !== "undefined";
|
|
15
15
|
}
|
|
16
|
-
async send(payload) {
|
|
17
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k
|
|
16
|
+
async send(token, payload) {
|
|
17
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
18
18
|
const body = safeStringify(payload.items);
|
|
19
|
-
const endpoint = getPixelBatchApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")
|
|
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);
|
|
@@ -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
|
};
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
// src/utils/innerMD5.ts
|
|
2
|
+
var EMPTY_STATE = new Int32Array(4);
|
|
3
|
+
var _Md5 = class {
|
|
4
|
+
constructor() {
|
|
5
|
+
this._dataLength = 0;
|
|
6
|
+
this._bufferLength = 0;
|
|
7
|
+
this._state = new Int32Array(4);
|
|
8
|
+
this._buffer = new ArrayBuffer(68);
|
|
9
|
+
this._buffer8 = new Uint8Array(this._buffer, 0, 68);
|
|
10
|
+
this._buffer32 = new Uint32Array(this._buffer, 0, 17);
|
|
11
|
+
this.start();
|
|
12
|
+
}
|
|
13
|
+
static hashStr(str, raw = false) {
|
|
14
|
+
return this.onePassHasher.start().appendStr(str).end(raw);
|
|
15
|
+
}
|
|
16
|
+
static hashAsciiStr(str, raw = false) {
|
|
17
|
+
return this.onePassHasher.start().appendAsciiStr(str).end(raw);
|
|
18
|
+
}
|
|
19
|
+
static _hex(x) {
|
|
20
|
+
const hc = _Md5.hexChars;
|
|
21
|
+
const ho = _Md5.hexOut;
|
|
22
|
+
let n;
|
|
23
|
+
let offset;
|
|
24
|
+
let j;
|
|
25
|
+
let i;
|
|
26
|
+
for (i = 0; i < 4; i += 1) {
|
|
27
|
+
offset = i * 8;
|
|
28
|
+
n = x[i];
|
|
29
|
+
for (j = 0; j < 8; j += 2) {
|
|
30
|
+
ho[offset + 1 + j] = hc.charAt(n & 15);
|
|
31
|
+
n >>>= 4;
|
|
32
|
+
ho[offset + 0 + j] = hc.charAt(n & 15);
|
|
33
|
+
n >>>= 4;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return ho.join("");
|
|
37
|
+
}
|
|
38
|
+
static _md5cycle(x, k) {
|
|
39
|
+
let a = x[0];
|
|
40
|
+
let b = x[1];
|
|
41
|
+
let c = x[2];
|
|
42
|
+
let d = x[3];
|
|
43
|
+
a += (b & c | ~b & d) + k[0] - 680876936 | 0;
|
|
44
|
+
a = (a << 7 | a >>> 25) + b | 0;
|
|
45
|
+
d += (a & b | ~a & c) + k[1] - 389564586 | 0;
|
|
46
|
+
d = (d << 12 | d >>> 20) + a | 0;
|
|
47
|
+
c += (d & a | ~d & b) + k[2] + 606105819 | 0;
|
|
48
|
+
c = (c << 17 | c >>> 15) + d | 0;
|
|
49
|
+
b += (c & d | ~c & a) + k[3] - 1044525330 | 0;
|
|
50
|
+
b = (b << 22 | b >>> 10) + c | 0;
|
|
51
|
+
a += (b & c | ~b & d) + k[4] - 176418897 | 0;
|
|
52
|
+
a = (a << 7 | a >>> 25) + b | 0;
|
|
53
|
+
d += (a & b | ~a & c) + k[5] + 1200080426 | 0;
|
|
54
|
+
d = (d << 12 | d >>> 20) + a | 0;
|
|
55
|
+
c += (d & a | ~d & b) + k[6] - 1473231341 | 0;
|
|
56
|
+
c = (c << 17 | c >>> 15) + d | 0;
|
|
57
|
+
b += (c & d | ~c & a) + k[7] - 45705983 | 0;
|
|
58
|
+
b = (b << 22 | b >>> 10) + c | 0;
|
|
59
|
+
a += (b & c | ~b & d) + k[8] + 1770035416 | 0;
|
|
60
|
+
a = (a << 7 | a >>> 25) + b | 0;
|
|
61
|
+
d += (a & b | ~a & c) + k[9] - 1958414417 | 0;
|
|
62
|
+
d = (d << 12 | d >>> 20) + a | 0;
|
|
63
|
+
c += (d & a | ~d & b) + k[10] - 42063 | 0;
|
|
64
|
+
c = (c << 17 | c >>> 15) + d | 0;
|
|
65
|
+
b += (c & d | ~c & a) + k[11] - 1990404162 | 0;
|
|
66
|
+
b = (b << 22 | b >>> 10) + c | 0;
|
|
67
|
+
a += (b & c | ~b & d) + k[12] + 1804603682 | 0;
|
|
68
|
+
a = (a << 7 | a >>> 25) + b | 0;
|
|
69
|
+
d += (a & b | ~a & c) + k[13] - 40341101 | 0;
|
|
70
|
+
d = (d << 12 | d >>> 20) + a | 0;
|
|
71
|
+
c += (d & a | ~d & b) + k[14] - 1502002290 | 0;
|
|
72
|
+
c = (c << 17 | c >>> 15) + d | 0;
|
|
73
|
+
b += (c & d | ~c & a) + k[15] + 1236535329 | 0;
|
|
74
|
+
b = (b << 22 | b >>> 10) + c | 0;
|
|
75
|
+
a += (b & d | c & ~d) + k[1] - 165796510 | 0;
|
|
76
|
+
a = (a << 5 | a >>> 27) + b | 0;
|
|
77
|
+
d += (a & c | b & ~c) + k[6] - 1069501632 | 0;
|
|
78
|
+
d = (d << 9 | d >>> 23) + a | 0;
|
|
79
|
+
c += (d & b | a & ~b) + k[11] + 643717713 | 0;
|
|
80
|
+
c = (c << 14 | c >>> 18) + d | 0;
|
|
81
|
+
b += (c & a | d & ~a) + k[0] - 373897302 | 0;
|
|
82
|
+
b = (b << 20 | b >>> 12) + c | 0;
|
|
83
|
+
a += (b & d | c & ~d) + k[5] - 701558691 | 0;
|
|
84
|
+
a = (a << 5 | a >>> 27) + b | 0;
|
|
85
|
+
d += (a & c | b & ~c) + k[10] + 38016083 | 0;
|
|
86
|
+
d = (d << 9 | d >>> 23) + a | 0;
|
|
87
|
+
c += (d & b | a & ~b) + k[15] - 660478335 | 0;
|
|
88
|
+
c = (c << 14 | c >>> 18) + d | 0;
|
|
89
|
+
b += (c & a | d & ~a) + k[4] - 405537848 | 0;
|
|
90
|
+
b = (b << 20 | b >>> 12) + c | 0;
|
|
91
|
+
a += (b & d | c & ~d) + k[9] + 568446438 | 0;
|
|
92
|
+
a = (a << 5 | a >>> 27) + b | 0;
|
|
93
|
+
d += (a & c | b & ~c) + k[14] - 1019803690 | 0;
|
|
94
|
+
d = (d << 9 | d >>> 23) + a | 0;
|
|
95
|
+
c += (d & b | a & ~b) + k[3] - 187363961 | 0;
|
|
96
|
+
c = (c << 14 | c >>> 18) + d | 0;
|
|
97
|
+
b += (c & a | d & ~a) + k[8] + 1163531501 | 0;
|
|
98
|
+
b = (b << 20 | b >>> 12) + c | 0;
|
|
99
|
+
a += (b & d | c & ~d) + k[13] - 1444681467 | 0;
|
|
100
|
+
a = (a << 5 | a >>> 27) + b | 0;
|
|
101
|
+
d += (a & c | b & ~c) + k[2] - 51403784 | 0;
|
|
102
|
+
d = (d << 9 | d >>> 23) + a | 0;
|
|
103
|
+
c += (d & b | a & ~b) + k[7] + 1735328473 | 0;
|
|
104
|
+
c = (c << 14 | c >>> 18) + d | 0;
|
|
105
|
+
b += (c & a | d & ~a) + k[12] - 1926607734 | 0;
|
|
106
|
+
b = (b << 20 | b >>> 12) + c | 0;
|
|
107
|
+
a += (b ^ c ^ d) + k[5] - 378558 | 0;
|
|
108
|
+
a = (a << 4 | a >>> 28) + b | 0;
|
|
109
|
+
d += (a ^ b ^ c) + k[8] - 2022574463 | 0;
|
|
110
|
+
d = (d << 11 | d >>> 21) + a | 0;
|
|
111
|
+
c += (d ^ a ^ b) + k[11] + 1839030562 | 0;
|
|
112
|
+
c = (c << 16 | c >>> 16) + d | 0;
|
|
113
|
+
b += (c ^ d ^ a) + k[14] - 35309556 | 0;
|
|
114
|
+
b = (b << 23 | b >>> 9) + c | 0;
|
|
115
|
+
a += (b ^ c ^ d) + k[1] - 1530992060 | 0;
|
|
116
|
+
a = (a << 4 | a >>> 28) + b | 0;
|
|
117
|
+
d += (a ^ b ^ c) + k[4] + 1272893353 | 0;
|
|
118
|
+
d = (d << 11 | d >>> 21) + a | 0;
|
|
119
|
+
c += (d ^ a ^ b) + k[7] - 155497632 | 0;
|
|
120
|
+
c = (c << 16 | c >>> 16) + d | 0;
|
|
121
|
+
b += (c ^ d ^ a) + k[10] - 1094730640 | 0;
|
|
122
|
+
b = (b << 23 | b >>> 9) + c | 0;
|
|
123
|
+
a += (b ^ c ^ d) + k[13] + 681279174 | 0;
|
|
124
|
+
a = (a << 4 | a >>> 28) + b | 0;
|
|
125
|
+
d += (a ^ b ^ c) + k[0] - 358537222 | 0;
|
|
126
|
+
d = (d << 11 | d >>> 21) + a | 0;
|
|
127
|
+
c += (d ^ a ^ b) + k[3] - 722521979 | 0;
|
|
128
|
+
c = (c << 16 | c >>> 16) + d | 0;
|
|
129
|
+
b += (c ^ d ^ a) + k[6] + 76029189 | 0;
|
|
130
|
+
b = (b << 23 | b >>> 9) + c | 0;
|
|
131
|
+
a += (b ^ c ^ d) + k[9] - 640364487 | 0;
|
|
132
|
+
a = (a << 4 | a >>> 28) + b | 0;
|
|
133
|
+
d += (a ^ b ^ c) + k[12] - 421815835 | 0;
|
|
134
|
+
d = (d << 11 | d >>> 21) + a | 0;
|
|
135
|
+
c += (d ^ a ^ b) + k[15] + 530742520 | 0;
|
|
136
|
+
c = (c << 16 | c >>> 16) + d | 0;
|
|
137
|
+
b += (c ^ d ^ a) + k[2] - 995338651 | 0;
|
|
138
|
+
b = (b << 23 | b >>> 9) + c | 0;
|
|
139
|
+
a += (c ^ (b | ~d)) + k[0] - 198630844 | 0;
|
|
140
|
+
a = (a << 6 | a >>> 26) + b | 0;
|
|
141
|
+
d += (b ^ (a | ~c)) + k[7] + 1126891415 | 0;
|
|
142
|
+
d = (d << 10 | d >>> 22) + a | 0;
|
|
143
|
+
c += (a ^ (d | ~b)) + k[14] - 1416354905 | 0;
|
|
144
|
+
c = (c << 15 | c >>> 17) + d | 0;
|
|
145
|
+
b += (d ^ (c | ~a)) + k[5] - 57434055 | 0;
|
|
146
|
+
b = (b << 21 | b >>> 11) + c | 0;
|
|
147
|
+
a += (c ^ (b | ~d)) + k[12] + 1700485571 | 0;
|
|
148
|
+
a = (a << 6 | a >>> 26) + b | 0;
|
|
149
|
+
d += (b ^ (a | ~c)) + k[3] - 1894986606 | 0;
|
|
150
|
+
d = (d << 10 | d >>> 22) + a | 0;
|
|
151
|
+
c += (a ^ (d | ~b)) + k[10] - 1051523 | 0;
|
|
152
|
+
c = (c << 15 | c >>> 17) + d | 0;
|
|
153
|
+
b += (d ^ (c | ~a)) + k[1] - 2054922799 | 0;
|
|
154
|
+
b = (b << 21 | b >>> 11) + c | 0;
|
|
155
|
+
a += (c ^ (b | ~d)) + k[8] + 1873313359 | 0;
|
|
156
|
+
a = (a << 6 | a >>> 26) + b | 0;
|
|
157
|
+
d += (b ^ (a | ~c)) + k[15] - 30611744 | 0;
|
|
158
|
+
d = (d << 10 | d >>> 22) + a | 0;
|
|
159
|
+
c += (a ^ (d | ~b)) + k[6] - 1560198380 | 0;
|
|
160
|
+
c = (c << 15 | c >>> 17) + d | 0;
|
|
161
|
+
b += (d ^ (c | ~a)) + k[13] + 1309151649 | 0;
|
|
162
|
+
b = (b << 21 | b >>> 11) + c | 0;
|
|
163
|
+
a += (c ^ (b | ~d)) + k[4] - 145523070 | 0;
|
|
164
|
+
a = (a << 6 | a >>> 26) + b | 0;
|
|
165
|
+
d += (b ^ (a | ~c)) + k[11] - 1120210379 | 0;
|
|
166
|
+
d = (d << 10 | d >>> 22) + a | 0;
|
|
167
|
+
c += (a ^ (d | ~b)) + k[2] + 718787259 | 0;
|
|
168
|
+
c = (c << 15 | c >>> 17) + d | 0;
|
|
169
|
+
b += (d ^ (c | ~a)) + k[9] - 343485551 | 0;
|
|
170
|
+
b = (b << 21 | b >>> 11) + c | 0;
|
|
171
|
+
x[0] = a + x[0] | 0;
|
|
172
|
+
x[1] = b + x[1] | 0;
|
|
173
|
+
x[2] = c + x[2] | 0;
|
|
174
|
+
x[3] = d + x[3] | 0;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Initialise buffer to be hashed
|
|
178
|
+
*/
|
|
179
|
+
start() {
|
|
180
|
+
this._dataLength = 0;
|
|
181
|
+
this._bufferLength = 0;
|
|
182
|
+
this._state.set(_Md5.stateIdentity);
|
|
183
|
+
return this;
|
|
184
|
+
}
|
|
185
|
+
// Char to code point to to array conversion:
|
|
186
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
|
|
187
|
+
// #Example.3A_Fixing_charCodeAt_to_handle_non-Basic-Multilingual-Plane_characters_if_their_presence_earlier_in_the_string_is_unknown
|
|
188
|
+
/**
|
|
189
|
+
* Append a UTF-8 string to the hash buffer
|
|
190
|
+
* @param str String to append
|
|
191
|
+
*/
|
|
192
|
+
appendStr(str) {
|
|
193
|
+
const buf8 = this._buffer8;
|
|
194
|
+
const buf32 = this._buffer32;
|
|
195
|
+
let bufLen = this._bufferLength;
|
|
196
|
+
let code;
|
|
197
|
+
let i;
|
|
198
|
+
for (i = 0; i < str.length; i += 1) {
|
|
199
|
+
code = str.charCodeAt(i);
|
|
200
|
+
if (code < 128) {
|
|
201
|
+
buf8[bufLen++] = code;
|
|
202
|
+
} else if (code < 2048) {
|
|
203
|
+
buf8[bufLen++] = (code >>> 6) + 192;
|
|
204
|
+
buf8[bufLen++] = code & 63 | 128;
|
|
205
|
+
} else if (code < 55296 || code > 56319) {
|
|
206
|
+
buf8[bufLen++] = (code >>> 12) + 224;
|
|
207
|
+
buf8[bufLen++] = code >>> 6 & 63 | 128;
|
|
208
|
+
buf8[bufLen++] = code & 63 | 128;
|
|
209
|
+
} else {
|
|
210
|
+
code = (code - 55296) * 1024 + (str.charCodeAt(++i) - 56320) + 65536;
|
|
211
|
+
if (code > 1114111) {
|
|
212
|
+
throw new Error(
|
|
213
|
+
"Unicode standard supports code points up to U+10FFFF"
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
buf8[bufLen++] = (code >>> 18) + 240;
|
|
217
|
+
buf8[bufLen++] = code >>> 12 & 63 | 128;
|
|
218
|
+
buf8[bufLen++] = code >>> 6 & 63 | 128;
|
|
219
|
+
buf8[bufLen++] = code & 63 | 128;
|
|
220
|
+
}
|
|
221
|
+
if (bufLen >= 64) {
|
|
222
|
+
this._dataLength += 64;
|
|
223
|
+
_Md5._md5cycle(this._state, buf32);
|
|
224
|
+
bufLen -= 64;
|
|
225
|
+
buf32[0] = buf32[16];
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
this._bufferLength = bufLen;
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Append an ASCII string to the hash buffer
|
|
233
|
+
* @param str String to append
|
|
234
|
+
*/
|
|
235
|
+
appendAsciiStr(str) {
|
|
236
|
+
const buf8 = this._buffer8;
|
|
237
|
+
const buf32 = this._buffer32;
|
|
238
|
+
let bufLen = this._bufferLength;
|
|
239
|
+
let i;
|
|
240
|
+
let j = 0;
|
|
241
|
+
for (; ; ) {
|
|
242
|
+
i = Math.min(str.length - j, 64 - bufLen);
|
|
243
|
+
while (i--) {
|
|
244
|
+
buf8[bufLen++] = str.charCodeAt(j++);
|
|
245
|
+
}
|
|
246
|
+
if (bufLen < 64) {
|
|
247
|
+
break;
|
|
248
|
+
}
|
|
249
|
+
this._dataLength += 64;
|
|
250
|
+
_Md5._md5cycle(this._state, buf32);
|
|
251
|
+
bufLen = 0;
|
|
252
|
+
}
|
|
253
|
+
this._bufferLength = bufLen;
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Append a byte array to the hash buffer
|
|
258
|
+
* @param input array to append
|
|
259
|
+
*/
|
|
260
|
+
appendByteArray(input) {
|
|
261
|
+
const buf8 = this._buffer8;
|
|
262
|
+
const buf32 = this._buffer32;
|
|
263
|
+
let bufLen = this._bufferLength;
|
|
264
|
+
let i;
|
|
265
|
+
let j = 0;
|
|
266
|
+
for (; ; ) {
|
|
267
|
+
i = Math.min(input.length - j, 64 - bufLen);
|
|
268
|
+
while (i--) {
|
|
269
|
+
buf8[bufLen++] = input[j++];
|
|
270
|
+
}
|
|
271
|
+
if (bufLen < 64) {
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
this._dataLength += 64;
|
|
275
|
+
_Md5._md5cycle(this._state, buf32);
|
|
276
|
+
bufLen = 0;
|
|
277
|
+
}
|
|
278
|
+
this._bufferLength = bufLen;
|
|
279
|
+
return this;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Get the state of the hash buffer
|
|
283
|
+
*/
|
|
284
|
+
getState() {
|
|
285
|
+
const s = this._state;
|
|
286
|
+
return {
|
|
287
|
+
buffer: String.fromCharCode.apply(null, Array.from(this._buffer8)),
|
|
288
|
+
buflen: this._bufferLength,
|
|
289
|
+
length: this._dataLength,
|
|
290
|
+
state: [s[0], s[1], s[2], s[3]]
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Override the current state of the hash buffer
|
|
295
|
+
* @param state New hash buffer state
|
|
296
|
+
*/
|
|
297
|
+
setState(state) {
|
|
298
|
+
const buf = state.buffer;
|
|
299
|
+
const x = state.state;
|
|
300
|
+
const s = this._state;
|
|
301
|
+
let i;
|
|
302
|
+
this._dataLength = state.length;
|
|
303
|
+
this._bufferLength = state.buflen;
|
|
304
|
+
s[0] = x[0];
|
|
305
|
+
s[1] = x[1];
|
|
306
|
+
s[2] = x[2];
|
|
307
|
+
s[3] = x[3];
|
|
308
|
+
for (i = 0; i < buf.length; i += 1) {
|
|
309
|
+
this._buffer8[i] = buf.charCodeAt(i);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Hash the current state of the hash buffer and return the result
|
|
314
|
+
* @param raw Whether to return the value as an `Int32Array`
|
|
315
|
+
*/
|
|
316
|
+
end(raw = false) {
|
|
317
|
+
const bufLen = this._bufferLength;
|
|
318
|
+
const buf8 = this._buffer8;
|
|
319
|
+
const buf32 = this._buffer32;
|
|
320
|
+
const i = (bufLen >> 2) + 1;
|
|
321
|
+
this._dataLength += bufLen;
|
|
322
|
+
const dataBitsLen = this._dataLength * 8;
|
|
323
|
+
buf8[bufLen] = 128;
|
|
324
|
+
buf8[bufLen + 1] = buf8[bufLen + 2] = buf8[bufLen + 3] = 0;
|
|
325
|
+
buf32.set(_Md5.buffer32Identity.subarray(i), i);
|
|
326
|
+
if (bufLen > 55) {
|
|
327
|
+
_Md5._md5cycle(this._state, buf32);
|
|
328
|
+
buf32.set(_Md5.buffer32Identity);
|
|
329
|
+
}
|
|
330
|
+
if (dataBitsLen <= 4294967295) {
|
|
331
|
+
buf32[14] = dataBitsLen;
|
|
332
|
+
} else {
|
|
333
|
+
const matches = dataBitsLen.toString(16).match(/(.*?)(.{0,8})$/);
|
|
334
|
+
if (matches === null)
|
|
335
|
+
return raw ? EMPTY_STATE : "";
|
|
336
|
+
const lo = parseInt(matches[2], 16);
|
|
337
|
+
const hi = parseInt(matches[1], 16) || 0;
|
|
338
|
+
buf32[14] = lo;
|
|
339
|
+
buf32[15] = hi;
|
|
340
|
+
}
|
|
341
|
+
_Md5._md5cycle(this._state, buf32);
|
|
342
|
+
return raw ? this._state : _Md5._hex(this._state);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
var Md5 = _Md5;
|
|
346
|
+
// Private Static Variables
|
|
347
|
+
Md5.stateIdentity = new Int32Array([
|
|
348
|
+
1732584193,
|
|
349
|
+
-271733879,
|
|
350
|
+
-1732584194,
|
|
351
|
+
271733878
|
|
352
|
+
]);
|
|
353
|
+
Md5.buffer32Identity = new Int32Array([
|
|
354
|
+
0,
|
|
355
|
+
0,
|
|
356
|
+
0,
|
|
357
|
+
0,
|
|
358
|
+
0,
|
|
359
|
+
0,
|
|
360
|
+
0,
|
|
361
|
+
0,
|
|
362
|
+
0,
|
|
363
|
+
0,
|
|
364
|
+
0,
|
|
365
|
+
0,
|
|
366
|
+
0,
|
|
367
|
+
0,
|
|
368
|
+
0,
|
|
369
|
+
0
|
|
370
|
+
]);
|
|
371
|
+
Md5.hexChars = "0123456789abcdef";
|
|
372
|
+
Md5.hexOut = [];
|
|
373
|
+
// Permanent instance is to use for one-call hashing
|
|
374
|
+
Md5.onePassHasher = new _Md5();
|
|
375
|
+
if (Md5.hashStr("hello") !== "5d41402abc4b2a76b9719d911017c592") {
|
|
376
|
+
throw new Error("Md5 self test failed.");
|
|
377
|
+
}
|
|
378
|
+
export {
|
|
379
|
+
Md5
|
|
380
|
+
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export
|
|
2
|
-
type: 'xhr' | 'fetch'
|
|
1
|
+
export type HttpClientResponse = {
|
|
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
|
}
|
|
@@ -29,8 +29,6 @@ export declare class LoggerSDK {
|
|
|
29
29
|
private offJs?;
|
|
30
30
|
private offPromise?;
|
|
31
31
|
private offResource?;
|
|
32
|
-
private offWxError?;
|
|
33
|
-
private offWxUnhandled?;
|
|
34
32
|
private constructor();
|
|
35
33
|
static getInstance(): LoggerSDK;
|
|
36
34
|
/**
|
|
@@ -64,6 +62,7 @@ export declare class LoggerSDK {
|
|
|
64
62
|
* 销毁实例
|
|
65
63
|
*/
|
|
66
64
|
destroy(): Promise<void>;
|
|
65
|
+
private generateToken;
|
|
67
66
|
/**
|
|
68
67
|
* 发送单个事件(带重试)
|
|
69
68
|
*/
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/** 环境类型 */
|
|
2
|
-
export
|
|
3
|
-
export
|
|
2
|
+
export type Env = 'develop' | 'testing' | 'product';
|
|
3
|
+
export type Stage = 'develop' | 'testing' | 'sit' | 'yace' | 'product';
|
|
4
4
|
/** 平台类型 */
|
|
5
|
-
export
|
|
5
|
+
export type PlatformType = 'browser' | 'unknown';
|
|
6
6
|
/** 环境信息 */
|
|
7
7
|
export interface EnvironmentInfo {
|
|
8
8
|
platform: PlatformType;
|
|
@@ -36,18 +36,18 @@ export interface StackFrame {
|
|
|
36
36
|
column: number;
|
|
37
37
|
function?: string;
|
|
38
38
|
}
|
|
39
|
-
export
|
|
39
|
+
export type BaseErrorInfo = {
|
|
40
40
|
type: 'js' | 'promise' | 'resource';
|
|
41
41
|
message: string;
|
|
42
42
|
stack: StackFrame[];
|
|
43
43
|
throwable?: string;
|
|
44
44
|
};
|
|
45
|
-
export
|
|
45
|
+
export type ReportData = {
|
|
46
46
|
appId: string;
|
|
47
47
|
appStage: Stage;
|
|
48
48
|
items: ReportItem[];
|
|
49
49
|
};
|
|
50
|
-
export
|
|
50
|
+
export type ReportItem = {
|
|
51
51
|
level: LogEventLevel;
|
|
52
52
|
traceId?: string;
|
|
53
53
|
frontendId: string;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/** 日志级别 */
|
|
2
|
-
export
|
|
2
|
+
export type LogEventLevel = 'INFO' | 'WARN' | 'ERROR' | 'FATAL';
|
|
@@ -17,8 +17,6 @@ export interface SDKOptions {
|
|
|
17
17
|
batchSize?: number;
|
|
18
18
|
/** 是否启用 gzip 压缩,默认 true (仅在批量模式下有效) */
|
|
19
19
|
enableGzip?: boolean;
|
|
20
|
-
/** 批量模式下启用 gzip 压缩的最小 items 数量,默认 2 */
|
|
21
|
-
gzipBatchMinSize?: number;
|
|
22
20
|
/** 最大像素图 URL 长度,默认 1900 */
|
|
23
21
|
maxPixelUrlLen?: number;
|
|
24
22
|
/** 批量上报时间间隔(毫秒),默认 5000 */
|
|
@@ -55,7 +53,6 @@ export interface SDKOptions {
|
|
|
55
53
|
js?: boolean;
|
|
56
54
|
promise?: boolean;
|
|
57
55
|
resource?: boolean;
|
|
58
|
-
wechat?: boolean;
|
|
59
56
|
};
|
|
60
57
|
enableAutoCapture?: boolean;
|
|
61
58
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
interface HasherState {
|
|
2
|
+
buffer: string;
|
|
3
|
+
buflen: number;
|
|
4
|
+
length: number;
|
|
5
|
+
state: number[];
|
|
6
|
+
}
|
|
7
|
+
export declare class Md5 {
|
|
8
|
+
/**
|
|
9
|
+
* Hash a UTF-8 string on the spot
|
|
10
|
+
* @param str String to hash
|
|
11
|
+
* @param raw Whether to return the value as an `Int32Array`
|
|
12
|
+
*/
|
|
13
|
+
static hashStr(str: string, raw?: false): string;
|
|
14
|
+
static hashStr(str: string, raw: true): Int32Array;
|
|
15
|
+
/**
|
|
16
|
+
* Hash a ASCII string on the spot
|
|
17
|
+
* @param str String to hash
|
|
18
|
+
* @param raw Whether to return the value as an `Int32Array`
|
|
19
|
+
*/
|
|
20
|
+
static hashAsciiStr(str: string, raw?: false): string;
|
|
21
|
+
static hashAsciiStr(str: string, raw: true): Int32Array;
|
|
22
|
+
private static stateIdentity;
|
|
23
|
+
private static buffer32Identity;
|
|
24
|
+
private static hexChars;
|
|
25
|
+
private static hexOut;
|
|
26
|
+
private static onePassHasher;
|
|
27
|
+
private static _hex;
|
|
28
|
+
private static _md5cycle;
|
|
29
|
+
private _dataLength;
|
|
30
|
+
private _bufferLength;
|
|
31
|
+
private _state;
|
|
32
|
+
private _buffer;
|
|
33
|
+
private _buffer8;
|
|
34
|
+
private _buffer32;
|
|
35
|
+
constructor();
|
|
36
|
+
/**
|
|
37
|
+
* Initialise buffer to be hashed
|
|
38
|
+
*/
|
|
39
|
+
start(): this;
|
|
40
|
+
/**
|
|
41
|
+
* Append a UTF-8 string to the hash buffer
|
|
42
|
+
* @param str String to append
|
|
43
|
+
*/
|
|
44
|
+
appendStr(str: string): this;
|
|
45
|
+
/**
|
|
46
|
+
* Append an ASCII string to the hash buffer
|
|
47
|
+
* @param str String to append
|
|
48
|
+
*/
|
|
49
|
+
appendAsciiStr(str: string): this;
|
|
50
|
+
/**
|
|
51
|
+
* Append a byte array to the hash buffer
|
|
52
|
+
* @param input array to append
|
|
53
|
+
*/
|
|
54
|
+
appendByteArray(input: Uint8Array): this;
|
|
55
|
+
/**
|
|
56
|
+
* Get the state of the hash buffer
|
|
57
|
+
*/
|
|
58
|
+
getState(): HasherState;
|
|
59
|
+
/**
|
|
60
|
+
* Override the current state of the hash buffer
|
|
61
|
+
* @param state New hash buffer state
|
|
62
|
+
*/
|
|
63
|
+
setState(state: HasherState): void;
|
|
64
|
+
/**
|
|
65
|
+
* Hash the current state of the hash buffer and return the result
|
|
66
|
+
* @param raw Whether to return the value as an `Int32Array`
|
|
67
|
+
*/
|
|
68
|
+
end(raw?: boolean): Int32Array | string;
|
|
69
|
+
}
|
|
70
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluve/logger-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
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"],
|
|
@@ -44,10 +36,10 @@
|
|
|
44
36
|
"url": "https://gitlab.pharmacyyf.com/frontend-common/pluve-lib.git"
|
|
45
37
|
},
|
|
46
38
|
"scripts": {
|
|
47
|
-
"init-install": "
|
|
39
|
+
"init-install": "pnpm install",
|
|
48
40
|
"build-types": "tsc -p tsconfig.types.json",
|
|
49
41
|
"typecheck": "tsc -p tsconfig.json",
|
|
50
|
-
"build": "
|
|
42
|
+
"build": "pnpm run build-types && father build",
|
|
51
43
|
"test": "vitest run",
|
|
52
44
|
"test:watch": "vitest",
|
|
53
45
|
"test:coverage": "vitest run --coverage",
|
|
@@ -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(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");
|
|
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
|
-
};
|