@pluve/logger-sdk 0.0.22 → 0.0.24

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.
@@ -11,7 +11,7 @@ function registerJsErrorCapture(debug, callback) {
11
11
  const stack = await parseStack(e.error);
12
12
  callback({
13
13
  type: "js",
14
- message: e.error.message,
14
+ message: JSON.stringify(e),
15
15
  stack,
16
16
  throwable: e.error.stack || ""
17
17
  });
@@ -11,9 +11,9 @@ function registerPromiseErrorCapture(debug, callback) {
11
11
  const stack = await parseStack(reason);
12
12
  callback({
13
13
  type: "promise",
14
- message: reason.message,
14
+ message: JSON.stringify(e),
15
15
  stack,
16
- throwable: reason.stack || ""
16
+ throwable: ""
17
17
  });
18
18
  }
19
19
  };
@@ -43,6 +43,14 @@ async function gzipCompress(data) {
43
43
  throw new Error("gzipCompress 不支持压缩");
44
44
  }
45
45
  function convert2Base64FromArray(data) {
46
+ if (typeof Buffer !== "undefined") {
47
+ if (data instanceof Uint8Array) {
48
+ return Buffer.from(data).toString("base64");
49
+ }
50
+ if (Array.isArray(data)) {
51
+ return Buffer.from(Uint8Array.from(data)).toString("base64");
52
+ }
53
+ }
46
54
  let binary = "";
47
55
  for (let i = 0; i < data.length; i += 1) {
48
56
  binary += String.fromCharCode(data[i]);
@@ -50,20 +58,16 @@ function convert2Base64FromArray(data) {
50
58
  if (typeof btoa !== "undefined") {
51
59
  return btoa(binary);
52
60
  }
53
- if (typeof Buffer !== "undefined") {
54
- return Buffer.from(binary, "base64").toString("base64");
55
- }
61
+ if (typeof Buffer !== "undefined")
62
+ return Buffer.from(binary, "binary").toString("base64");
56
63
  throw new Error("convert2Base64FromArray 不支持转换");
57
64
  }
58
65
  async function convert2Base64(data) {
59
- if (typeof btoa !== "undefined") {
60
- try {
61
- return btoa(data);
62
- } catch {
63
- }
64
- }
65
- if (typeof Buffer !== "undefined") {
66
- return Buffer.from(data, "base64").toString("base64");
66
+ try {
67
+ const bytes = toUtf8Bytes(data);
68
+ return convert2Base64FromArray(bytes);
69
+ } catch (e) {
70
+ console.log("convert2Base64 UTF-8 转换失败", e);
67
71
  }
68
72
  try {
69
73
  const mod = await import("js-base64");
@@ -136,10 +136,16 @@ var LoggerSDK = class {
136
136
  this.offJs = registerJsErrorCapture(!!((_d = this.opts) == null ? void 0 : _d.debug), this.trackInner.bind(this));
137
137
  }
138
138
  if (enableAuto && ac.promise !== false) {
139
- this.offPromise = registerPromiseErrorCapture(!!((_e = this.opts) == null ? void 0 : _e.debug), this.trackInner.bind(this));
139
+ this.offPromise = registerPromiseErrorCapture(
140
+ !!((_e = this.opts) == null ? void 0 : _e.debug),
141
+ this.trackInner.bind(this)
142
+ );
140
143
  }
141
144
  if (enableAuto && ac.resource !== false) {
142
- this.offResource = registerResourceErrorCapture(!!((_f = this.opts) == null ? void 0 : _f.debug), this.trackInner.bind(this));
145
+ this.offResource = registerResourceErrorCapture(
146
+ !!((_f = this.opts) == null ? void 0 : _f.debug),
147
+ this.trackInner.bind(this)
148
+ );
143
149
  }
144
150
  });
145
151
  }
@@ -154,9 +160,9 @@ var LoggerSDK = class {
154
160
  }
155
161
  }
156
162
  /**
157
- * 设置 token 信息
158
- * @param token token
159
- */
163
+ * 设置 token 信息
164
+ * @param token token
165
+ */
160
166
  setToken(token, securityType) {
161
167
  var _a;
162
168
  logDebug(!!((_a = this.opts) == null ? void 0 : _a.debug), "setToken", token, securityType);
@@ -427,9 +433,9 @@ var LoggerSDK = class {
427
433
  LoggerSDK.instance = void 0;
428
434
  }
429
435
  generateToken() {
430
- var _a;
436
+ var _a, _b, _c;
431
437
  const logTime = Date.now();
432
- const token = ((_a = this.opts) == null ? void 0 : _a.token) || md5(`${this.opts.appId}${this.opts.logStage}${logTime}`);
438
+ const token = ((_a = this.opts) == null ? void 0 : _a.token) || md5(`${(_b = this.opts) == null ? void 0 : _b.appId}${(_c = this.opts) == null ? void 0 : _c.logStage}${logTime}`);
433
439
  return {
434
440
  token,
435
441
  logTime
@@ -27,7 +27,11 @@ var QueueManager = class {
27
27
  this.localforage = mod;
28
28
  return this.localforage;
29
29
  } catch (e) {
30
- logDebug(!!this.opts.debug, "localforage dynamic import failed, fallback to localStorage", e);
30
+ logDebug(
31
+ !!this.opts.debug,
32
+ "localforage dynamic import failed, fallback to localStorage",
33
+ e
34
+ );
31
35
  this.localforage = null;
32
36
  return null;
33
37
  }
@@ -142,21 +146,33 @@ var QueueManager = class {
142
146
  return this.localforage.setItem(this.storageKey, this.queue).then(() => {
143
147
  logDebug(!!this.opts.debug, `Saved ${this.queue.length} events to IndexedDB`);
144
148
  }).catch((err) => {
145
- logDebug(!!this.opts.debug, "IndexedDB save error, falling back to localStorage", err);
149
+ logDebug(
150
+ !!this.opts.debug,
151
+ "IndexedDB save error, falling back to localStorage",
152
+ err
153
+ );
146
154
  saveToLocalStorage();
147
155
  });
148
156
  }
149
157
  saveToLocalStorage();
150
158
  return null;
151
159
  }).catch((err) => {
152
- logDebug(!!this.opts.debug, "IndexedDB save error, falling back to localStorage", err);
160
+ logDebug(
161
+ !!this.opts.debug,
162
+ "IndexedDB save error, falling back to localStorage",
163
+ err
164
+ );
153
165
  saveToLocalStorage();
154
166
  });
155
167
  } else {
156
168
  this.localforage.setItem(this.storageKey, this.queue).then(() => {
157
169
  logDebug(!!this.opts.debug, `Saved ${this.queue.length} events to IndexedDB`);
158
170
  }).catch((err) => {
159
- logDebug(!!this.opts.debug, "IndexedDB save error, falling back to localStorage", err);
171
+ logDebug(
172
+ !!this.opts.debug,
173
+ "IndexedDB save error, falling back to localStorage",
174
+ err
175
+ );
160
176
  saveToLocalStorage();
161
177
  });
162
178
  }
@@ -183,21 +199,33 @@ var QueueManager = class {
183
199
  return this.localforage.removeItem(this.storageKey).then(() => {
184
200
  logDebug(!!this.opts.debug, "Removed queue from IndexedDB");
185
201
  }).catch((err) => {
186
- logDebug(!!this.opts.debug, "IndexedDB remove error, falling back to localStorage", err);
202
+ logDebug(
203
+ !!this.opts.debug,
204
+ "IndexedDB remove error, falling back to localStorage",
205
+ err
206
+ );
187
207
  removeFromLocalStorage();
188
208
  });
189
209
  }
190
210
  removeFromLocalStorage();
191
211
  return null;
192
212
  }).catch((err) => {
193
- logDebug(!!this.opts.debug, "IndexedDB remove error, falling back to localStorage", err);
213
+ logDebug(
214
+ !!this.opts.debug,
215
+ "IndexedDB remove error, falling back to localStorage",
216
+ err
217
+ );
194
218
  removeFromLocalStorage();
195
219
  });
196
220
  } else {
197
221
  this.localforage.removeItem(this.storageKey).then(() => {
198
222
  logDebug(!!this.opts.debug, "Removed queue from IndexedDB");
199
223
  }).catch((err) => {
200
- logDebug(!!this.opts.debug, "IndexedDB remove error, falling back to localStorage", err);
224
+ logDebug(
225
+ !!this.opts.debug,
226
+ "IndexedDB remove error, falling back to localStorage",
227
+ err
228
+ );
201
229
  removeFromLocalStorage();
202
230
  });
203
231
  }
@@ -27,7 +27,7 @@ var BeaconTransport = class {
27
27
  contentType = "application/json; charset=utf-8";
28
28
  } else {
29
29
  logDebug(!!((_c = this.opts) == null ? void 0 : _c.debug), "WeChat request no gzip mode: ");
30
- const compressedItems = convert2Base64(safeStringify(payload.items));
30
+ const compressedItems = await convert2Base64(safeStringify(payload.items));
31
31
  sendBody = safeStringify({
32
32
  ...payload,
33
33
  items: compressedItems
@@ -3,6 +3,7 @@ import { isBrowser } from "../utils/environment";
3
3
  import { logDebug, now, safeStringify } from "../utils/tools";
4
4
  import { getPixelBatchApi } from "../config";
5
5
  import { convert2Base64, gzipCompress } from "../compress/compression";
6
+ import { SecurityType } from "../types/securityType";
6
7
  var PixelImageTransport = class {
7
8
  constructor(opts) {
8
9
  /** 传输器名称 */
@@ -14,7 +15,7 @@ var PixelImageTransport = class {
14
15
  return isBrowser() && typeof Image !== "undefined";
15
16
  }
16
17
  async send(token, payload) {
17
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
18
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
18
19
  const body = safeStringify(payload.items);
19
20
  const endpoint = `${getPixelBatchApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop")}?token=${token}`;
20
21
  const param = "items";
@@ -31,10 +32,10 @@ var PixelImageTransport = class {
31
32
  compressedBody = await convert2Base64(body);
32
33
  }
33
34
  const cacheBuster = `_=${Date.now()}`;
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
+ const qs = `appId=${((_h = this.opts) == null ? void 0 : _h.appId) || ""}&appStage=${((_i = this.opts) == null ? void 0 : _i.logStage) || ""}&${param}=${encodeURIComponent(compressedBody)}&securityType=${((_j = this.opts) == null ? void 0 : _j.securityType) || SecurityType.BASE}&logTime=${payload.logTime}&gzip=${((_k = this.opts) == null ? void 0 : _k.enableGzip) ? 1 : 0}&${cacheBuster}`;
35
36
  const url = endpoint.includes("?") ? `${endpoint}&${qs}` : `${endpoint}?${qs}`;
36
37
  if (url.length > maxLen) {
37
- logDebug(!!((_k = this.opts) == null ? void 0 : _k.debug), `URL too long (${url.length} > ${maxLen})`);
38
+ logDebug(!!((_l = this.opts) == null ? void 0 : _l.debug), `URL too long (${url.length} > ${maxLen})`);
38
39
  }
39
40
  return new Promise((resolve, reject) => {
40
41
  const img = new Image();
@@ -1,2 +1,2 @@
1
- import { BaseErrorInfo } from '../types/logEvent';
1
+ import type { BaseErrorInfo } from '../types/logEvent';
2
2
  export declare function registerJsErrorCapture(debug: boolean, callback: (error: BaseErrorInfo) => void): (() => void) | undefined;
@@ -1,2 +1,2 @@
1
- import { BaseErrorInfo } from '../types/logEvent';
1
+ import type { BaseErrorInfo } from '../types/logEvent';
2
2
  export declare function registerPromiseErrorCapture(debug: boolean, callback: (error: BaseErrorInfo) => void): (() => void) | undefined;
@@ -1,2 +1,2 @@
1
- import { BaseErrorInfo } from '../types/logEvent';
1
+ import type { BaseErrorInfo } from '../types/logEvent';
2
2
  export declare function registerResourceErrorCapture(debug: boolean, callback: (error: BaseErrorInfo) => void): (() => void) | undefined;
@@ -1,4 +1,4 @@
1
- import { Env } from '../types/env';
1
+ import type { Env } from '../types/env';
2
2
  /** 获取 SDK 基础 API */
3
3
  export declare const getSDKBaseApi: (env: Env) => "https://apm.pharmacyyf.com" | "https://apm-te.pharmacyyf.com" | "https://chief-dev.yifengx.com";
4
4
  /** 获取 SDK 注册 API */
@@ -1,6 +1,6 @@
1
- import { Env } from '../types/env';
2
- import { SDKOptions } from '../types/sdkOptions';
3
- import { TrackOptions } from '../types/trackOptions';
1
+ import type { Env } from '../types/env';
2
+ import type { SDKOptions } from '../types/sdkOptions';
3
+ import type { TrackOptions } from '../types/trackOptions';
4
4
  import { SecurityType } from '../types/securityType';
5
5
  export declare class LoggerSDK {
6
6
  private static instance;
@@ -41,9 +41,9 @@ export declare class LoggerSDK {
41
41
  */
42
42
  identify(userId: string): void;
43
43
  /**
44
- * 设置 token 信息
45
- * @param token token
46
- */
44
+ * 设置 token 信息
45
+ * @param token token
46
+ */
47
47
  setToken(token: string, securityType: SecurityType): void;
48
48
  /** 设置店铺编码 */
49
49
  setStoreCode(storeCode: string): void;
@@ -1,4 +1,4 @@
1
- import { LogEvent } from '../types/logEvent';
1
+ import type { LogEvent } from '../types/logEvent';
2
2
  type LocalForageLike = {
3
3
  getItem: (key: string) => Promise<any>;
4
4
  setItem: (key: string, value: any) => Promise<any>;
@@ -1,2 +1,2 @@
1
- import { StackFrame } from '../types/logEvent';
1
+ import type { StackFrame } from '../types/logEvent';
2
2
  export declare function parseStack(error: Error): Promise<StackFrame[]>;
@@ -1,5 +1,5 @@
1
- import { ReportData } from '../types/logEvent';
2
- import { Transporter, TransportOptions } from './transport';
1
+ import type { ReportData } from '../types/logEvent';
2
+ import type { Transporter, TransportOptions } from './transport';
3
3
  export declare class BeaconTransport implements Transporter {
4
4
  /** 传输器名称 */
5
5
  name: string;
@@ -1,5 +1,5 @@
1
- import { Transporter, TransportOptions } from './transport';
2
- import { ReportData } from '../types/logEvent';
1
+ import type { Transporter, TransportOptions } from './transport';
2
+ import type { ReportData } from '../types/logEvent';
3
3
  export declare class PixelImageTransport implements Transporter {
4
4
  /** 传输器名称 */
5
5
  name: string;
@@ -1,5 +1,5 @@
1
- import { ReportData } from '../types/logEvent';
2
- import { SDKOptions } from '../types/sdkOptions';
1
+ import type { ReportData } from '../types/logEvent';
2
+ import type { SDKOptions } from '../types/sdkOptions';
3
3
  /** 传输选项接口 */
4
4
  export interface TransportOptions extends SDKOptions {
5
5
  }
@@ -1,6 +1,6 @@
1
- import { Stage } from './env';
2
- import { LogEventLevel } from './logEventLevel';
3
- import { SecurityType } from './securityType';
1
+ import type { Stage } from './env';
2
+ import type { LogEventLevel } from './logEventLevel';
3
+ import type { SecurityType } from './securityType';
4
4
  /** 标准化日志上报格式 */
5
5
  export interface LogEvent {
6
6
  /** 日志 ID */
@@ -1,6 +1,6 @@
1
- import { Env, Stage } from './env';
2
- import { LogEventLevel } from './logEventLevel';
3
- import { SecurityType } from './securityType';
1
+ import type { Env, Stage } from './env';
2
+ import type { LogEventLevel } from './logEventLevel';
3
+ import type { SecurityType } from './securityType';
4
4
  /** SDK 配置选项 */
5
5
  export interface SDKOptions {
6
6
  /** 上报端点 URL */
@@ -1,4 +1,4 @@
1
- import { LogEventLevel } from './logEventLevel';
1
+ import type { LogEventLevel } from './logEventLevel';
2
2
  export type TrackOptions = {
3
3
  message: string;
4
4
  error?: Error | unknown;
@@ -1,4 +1,4 @@
1
- import { EnvironmentInfo } from '../types/env';
1
+ import type { EnvironmentInfo } from '../types/env';
2
2
  export declare function isBrowser(): boolean;
3
3
  export declare function getCurrentUrl(): string;
4
4
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pluve/logger-sdk",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "description": "logger sdk",
5
5
  "keywords": [
6
6
  "logger"