@pluve/logger-sdk 0.0.12 → 0.0.13

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.
Files changed (45) hide show
  1. package/dist/esm/capture/jsError.js +3 -24
  2. package/dist/esm/capture/promiseError.js +3 -24
  3. package/dist/esm/capture/wechatError.js +9 -31
  4. package/dist/esm/compress/compression.js +21 -44
  5. package/dist/esm/core/httpClient.js +53 -76
  6. package/dist/esm/core/loggerSDK.js +201 -234
  7. package/dist/esm/core/queueManager.js +58 -112
  8. package/dist/esm/core/retryManager.js +44 -70
  9. package/dist/esm/stack/stacktrace.js +8 -31
  10. package/dist/esm/transport/beaconTransport.js +20 -61
  11. package/dist/esm/transport/pixelImageTransport.js +54 -77
  12. package/dist/esm/transport/transportAdapter.js +13 -74
  13. package/dist/esm/transport/wechatTransport.js +53 -94
  14. package/dist/esm/utils/tools.js +12 -35
  15. package/package.json +1 -1
  16. package/dist/cjs/capture/jsError.js +0 -48
  17. package/dist/cjs/capture/promiseError.js +0 -49
  18. package/dist/cjs/capture/resourceError.js +0 -48
  19. package/dist/cjs/capture/wechatError.js +0 -95
  20. package/dist/cjs/compress/compression.js +0 -105
  21. package/dist/cjs/config/index.js +0 -55
  22. package/dist/cjs/core/fingerprint.js +0 -36
  23. package/dist/cjs/core/httpClient.js +0 -96
  24. package/dist/cjs/core/loggerSDK.js +0 -643
  25. package/dist/cjs/core/queueManager.js +0 -263
  26. package/dist/cjs/core/retryManager.js +0 -127
  27. package/dist/cjs/index.js +0 -34
  28. package/dist/cjs/stack/stacktrace.js +0 -48
  29. package/dist/cjs/transport/beaconTransport.js +0 -64
  30. package/dist/cjs/transport/pixelImageTransport.js +0 -99
  31. package/dist/cjs/transport/transport.js +0 -17
  32. package/dist/cjs/transport/transportAdapter.js +0 -73
  33. package/dist/cjs/transport/wechatTransport.js +0 -103
  34. package/dist/cjs/types/api.js +0 -17
  35. package/dist/cjs/types/env.js +0 -17
  36. package/dist/cjs/types/external.d.ts +0 -21
  37. package/dist/cjs/types/logEvent.js +0 -17
  38. package/dist/cjs/types/logEventLevel.js +0 -17
  39. package/dist/cjs/types/sdkOptions.js +0 -17
  40. package/dist/cjs/types/trackOptions.js +0 -17
  41. package/dist/cjs/utils/environment.js +0 -183
  42. package/dist/cjs/utils/session.js +0 -31
  43. package/dist/cjs/utils/tools.js +0 -82
  44. package/dist/cjs/utils/uuid.js +0 -35
  45. package/dist/umd/logger-sdk.min.js +0 -1
@@ -1,53 +1,3 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
- }) : x)(function(x) {
10
- if (typeof require !== "undefined")
11
- return require.apply(this, arguments);
12
- throw new Error('Dynamic require of "' + x + '" is not supported');
13
- });
14
- var __copyProps = (to, from, except, desc) => {
15
- if (from && typeof from === "object" || typeof from === "function") {
16
- for (let key of __getOwnPropNames(from))
17
- if (!__hasOwnProp.call(to, key) && key !== except)
18
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
19
- }
20
- return to;
21
- };
22
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
23
- // If the importer is in node compatibility mode or this is not an ESM
24
- // file that has been converted to a CommonJS file using a Babel-
25
- // compatible transform (i.e. "__esModule" has not been set), then set
26
- // "default" to the CommonJS "module.exports" for node compatibility.
27
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
28
- mod
29
- ));
30
- var __async = (__this, __arguments, generator) => {
31
- return new Promise((resolve, reject) => {
32
- var fulfilled = (value) => {
33
- try {
34
- step(generator.next(value));
35
- } catch (e) {
36
- reject(e);
37
- }
38
- };
39
- var rejected = (value) => {
40
- try {
41
- step(generator.throw(value));
42
- } catch (e) {
43
- reject(e);
44
- }
45
- };
46
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
47
- step((generator = generator.apply(__this, __arguments)).next());
48
- });
49
- };
50
-
51
1
  // src/core/queueManager.ts
52
2
  import { isBrowser, isWeChatMiniProgram } from "../utils/environment";
53
3
  import { logDebug, safeStringify } from "../utils/tools";
@@ -63,34 +13,32 @@ var QueueManager = class {
63
13
  }
64
14
  });
65
15
  }
66
- loadLocalForage() {
67
- return __async(this, null, function* () {
68
- if (!isBrowser())
69
- return null;
16
+ async loadLocalForage() {
17
+ if (!isBrowser())
18
+ return null;
19
+ try {
20
+ const g = globalThis;
21
+ if (g && g.localforage && typeof g.localforage.getItem === "function") {
22
+ this.localforage = g.localforage;
23
+ return this.localforage;
24
+ }
70
25
  try {
71
- const g = globalThis;
72
- if (g && g.localforage && typeof g.localforage.getItem === "function") {
73
- this.localforage = g.localforage;
74
- return this.localforage;
75
- }
76
- try {
77
- const mod = yield Promise.resolve().then(() => __toESM(__require(
78
- /* @vite-ignore */
79
- "localforage"
80
- )));
81
- this.localforage = mod;
82
- return this.localforage;
83
- } catch (e) {
84
- logDebug(!!this.opts.debug, "localforage dynamic import failed, fallback to localStorage", e);
85
- this.localforage = null;
86
- return null;
87
- }
26
+ const mod = await import(
27
+ /* @vite-ignore */
28
+ "localforage"
29
+ );
30
+ this.localforage = mod;
31
+ return this.localforage;
88
32
  } catch (e) {
89
- logDebug(!!this.opts.debug, "localforage load error", e);
33
+ logDebug(!!this.opts.debug, "localforage dynamic import failed, fallback to localStorage", e);
90
34
  this.localforage = null;
91
35
  return null;
92
36
  }
93
- });
37
+ } catch (e) {
38
+ logDebug(!!this.opts.debug, "localforage load error", e);
39
+ this.localforage = null;
40
+ return null;
41
+ }
94
42
  }
95
43
  /**
96
44
  * 添加日志到队列
@@ -143,52 +91,50 @@ var QueueManager = class {
143
91
  /**
144
92
  * 从持久化存储加载队列
145
93
  */
146
- loadFromStorage() {
147
- return __async(this, null, function* () {
148
- try {
149
- let stored = null;
150
- if (isWeChatMiniProgram()) {
151
- stored = wx.getStorageSync(this.storageKey);
152
- } else if (isBrowser()) {
153
- if (!this.localforage) {
154
- yield this.loadLocalForage();
155
- }
156
- try {
157
- const items = this.localforage ? yield this.localforage.getItem(this.storageKey) : null;
158
- if (Array.isArray(items)) {
159
- this.queue = items.slice(0, this.opts.maxSize);
160
- logDebug(!!this.opts.debug, `Loaded ${this.queue.length} events from IndexedDB`);
161
- return;
162
- }
163
- } catch (err) {
164
- logDebug(!!this.opts.debug, "IndexedDB load error, falling back to localStorage", err);
94
+ async loadFromStorage() {
95
+ try {
96
+ let stored = null;
97
+ if (isWeChatMiniProgram()) {
98
+ stored = wx.getStorageSync(this.storageKey);
99
+ } else if (isBrowser()) {
100
+ if (!this.localforage) {
101
+ await this.loadLocalForage();
102
+ }
103
+ try {
104
+ const items = this.localforage ? await this.localforage.getItem(this.storageKey) : null;
105
+ if (Array.isArray(items)) {
106
+ this.queue = items.slice(0, this.opts.maxSize);
107
+ logDebug(!!this.opts.debug, `Loaded ${this.queue.length} events from IndexedDB`);
108
+ return;
165
109
  }
166
- if (typeof localStorage !== "undefined") {
167
- const ls = localStorage.getItem(this.storageKey);
168
- if (ls) {
169
- try {
170
- const parsed = JSON.parse(ls);
171
- if (Array.isArray(parsed)) {
172
- this.queue = parsed.slice(0, this.opts.maxSize);
173
- logDebug(!!this.opts.debug, `Loaded ${this.queue.length} events from localStorage`);
174
- }
175
- } catch (e) {
110
+ } catch (err) {
111
+ logDebug(!!this.opts.debug, "IndexedDB load error, falling back to localStorage", err);
112
+ }
113
+ if (typeof localStorage !== "undefined") {
114
+ const ls = localStorage.getItem(this.storageKey);
115
+ if (ls) {
116
+ try {
117
+ const parsed = JSON.parse(ls);
118
+ if (Array.isArray(parsed)) {
119
+ this.queue = parsed.slice(0, this.opts.maxSize);
120
+ logDebug(!!this.opts.debug, `Loaded ${this.queue.length} events from localStorage`);
176
121
  }
122
+ } catch {
177
123
  }
178
124
  }
179
- return;
180
125
  }
181
- if (stored) {
182
- const parsed = JSON.parse(stored);
183
- if (Array.isArray(parsed)) {
184
- this.queue = parsed.slice(0, this.opts.maxSize);
185
- logDebug(!!this.opts.debug, `Loaded ${this.queue.length} events from storage`);
186
- }
126
+ return;
127
+ }
128
+ if (stored) {
129
+ const parsed = JSON.parse(stored);
130
+ if (Array.isArray(parsed)) {
131
+ this.queue = parsed.slice(0, this.opts.maxSize);
132
+ logDebug(!!this.opts.debug, `Loaded ${this.queue.length} events from storage`);
187
133
  }
188
- } catch (error) {
189
- logDebug(!!this.opts.debug, "Failed to load queue from storage", error);
190
134
  }
191
- });
135
+ } catch (error) {
136
+ logDebug(!!this.opts.debug, "Failed to load queue from storage", error);
137
+ }
192
138
  }
193
139
  /**
194
140
  * 保存队列到持久化存储
@@ -1,25 +1,3 @@
1
- var __pow = Math.pow;
2
- var __async = (__this, __arguments, generator) => {
3
- return new Promise((resolve, reject) => {
4
- var fulfilled = (value) => {
5
- try {
6
- step(generator.next(value));
7
- } catch (e) {
8
- reject(e);
9
- }
10
- };
11
- var rejected = (value) => {
12
- try {
13
- step(generator.throw(value));
14
- } catch (e) {
15
- reject(e);
16
- }
17
- };
18
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
19
- step((generator = generator.apply(__this, __arguments)).next());
20
- });
21
- };
22
-
23
1
  // src/core/retryManager.ts
24
2
  import { logDebug } from "../utils/tools";
25
3
  var RetryManager = class {
@@ -33,59 +11,55 @@ var RetryManager = class {
33
11
  * @param fn - 要执行的异步函数
34
12
  * @returns Promise
35
13
  */
36
- executeWithRetry(taskId, fn) {
37
- return __async(this, null, function* () {
38
- if (this.retryingTasks.has(taskId)) {
39
- logDebug(!!this.opts.debug, `Task ${taskId} already retrying, skipped`);
40
- throw new Error(`Task ${taskId} already retrying`);
41
- }
42
- const task = {
43
- id: taskId,
44
- fn,
45
- retries: 0,
46
- maxRetries: this.opts.maxRetries,
47
- baseDelay: this.opts.baseDelay,
48
- useBackoff: this.opts.useBackoff
49
- };
50
- this.retryingTasks.set(taskId, task);
51
- try {
52
- const result = yield this.executeTask(task);
53
- this.retryingTasks.delete(taskId);
54
- return result;
55
- } catch (error) {
56
- this.retryingTasks.delete(taskId);
57
- throw error;
58
- }
59
- });
14
+ async executeWithRetry(taskId, fn) {
15
+ if (this.retryingTasks.has(taskId)) {
16
+ logDebug(!!this.opts.debug, `Task ${taskId} already retrying, skipped`);
17
+ throw new Error(`Task ${taskId} already retrying`);
18
+ }
19
+ const task = {
20
+ id: taskId,
21
+ fn,
22
+ retries: 0,
23
+ maxRetries: this.opts.maxRetries,
24
+ baseDelay: this.opts.baseDelay,
25
+ useBackoff: this.opts.useBackoff
26
+ };
27
+ this.retryingTasks.set(taskId, task);
28
+ try {
29
+ const result = await this.executeTask(task);
30
+ this.retryingTasks.delete(taskId);
31
+ return result;
32
+ } catch (error) {
33
+ this.retryingTasks.delete(taskId);
34
+ throw error;
35
+ }
60
36
  }
61
37
  /**
62
38
  * 执行任务(带重试逻辑)
63
39
  */
64
- executeTask(task) {
65
- return __async(this, null, function* () {
66
- while (task.retries <= task.maxRetries) {
67
- try {
68
- const result = yield task.fn();
69
- if (task.retries > 0) {
70
- logDebug(!!this.opts.debug, `Task ${task.id} succeeded after ${task.retries} retries`);
71
- }
72
- return result;
73
- } catch (error) {
74
- task.retries += 1;
75
- if (task.retries > task.maxRetries) {
76
- logDebug(!!this.opts.debug, `Task ${task.id} failed after ${task.maxRetries} retries`);
77
- throw error;
78
- }
79
- const delay = RetryManager.calculateDelay(task.retries, task.baseDelay, task.useBackoff);
80
- logDebug(
81
- !!this.opts.debug,
82
- `Task ${task.id} failed (attempt ${task.retries}/${task.maxRetries}), retrying in ${delay}ms`
83
- );
84
- yield RetryManager.sleep(delay);
40
+ async executeTask(task) {
41
+ while (task.retries <= task.maxRetries) {
42
+ try {
43
+ const result = await task.fn();
44
+ if (task.retries > 0) {
45
+ logDebug(!!this.opts.debug, `Task ${task.id} succeeded after ${task.retries} retries`);
85
46
  }
47
+ return result;
48
+ } catch (error) {
49
+ task.retries += 1;
50
+ if (task.retries > task.maxRetries) {
51
+ logDebug(!!this.opts.debug, `Task ${task.id} failed after ${task.maxRetries} retries`);
52
+ throw error;
53
+ }
54
+ const delay = RetryManager.calculateDelay(task.retries, task.baseDelay, task.useBackoff);
55
+ logDebug(
56
+ !!this.opts.debug,
57
+ `Task ${task.id} failed (attempt ${task.retries}/${task.maxRetries}), retrying in ${delay}ms`
58
+ );
59
+ await RetryManager.sleep(delay);
86
60
  }
87
- throw new Error(`Task ${task.id} exceeded max retries`);
88
- });
61
+ }
62
+ throw new Error(`Task ${task.id} exceeded max retries`);
89
63
  }
90
64
  /**
91
65
  * 计算延迟时间
@@ -98,7 +72,7 @@ var RetryManager = class {
98
72
  if (!useBackoff) {
99
73
  return baseDelay;
100
74
  }
101
- const factor = __pow(2, attempt - 1);
75
+ const factor = 2 ** (attempt - 1);
102
76
  const delay = baseDelay * factor;
103
77
  const jitterRatio = 0.3;
104
78
  const maxDelay = 3e4;
@@ -1,36 +1,13 @@
1
- var __async = (__this, __arguments, generator) => {
2
- return new Promise((resolve, reject) => {
3
- var fulfilled = (value) => {
4
- try {
5
- step(generator.next(value));
6
- } catch (e) {
7
- reject(e);
8
- }
9
- };
10
- var rejected = (value) => {
11
- try {
12
- step(generator.throw(value));
13
- } catch (e) {
14
- reject(e);
15
- }
16
- };
17
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
- step((generator = generator.apply(__this, __arguments)).next());
19
- });
20
- };
21
-
22
1
  // src/stack/stacktrace.ts
23
2
  import StackTrace from "stacktrace-js";
24
- function parseStack(error) {
25
- return __async(this, null, function* () {
26
- const frames = yield StackTrace.fromError(error);
27
- return frames.slice(0, 5).map((f) => ({
28
- file: f.fileName || "",
29
- line: f.lineNumber || 0,
30
- column: f.columnNumber || 0,
31
- function: f.functionName || void 0
32
- }));
33
- });
3
+ async function parseStack(error) {
4
+ const frames = await StackTrace.fromError(error);
5
+ return frames.slice(0, 5).map((f) => ({
6
+ file: f.fileName || "",
7
+ line: f.lineNumber || 0,
8
+ column: f.columnNumber || 0,
9
+ function: f.functionName || void 0
10
+ }));
34
11
  }
35
12
  export {
36
13
  parseStack
@@ -1,43 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- var __async = (__this, __arguments, generator) => {
21
- return new Promise((resolve, reject) => {
22
- var fulfilled = (value) => {
23
- try {
24
- step(generator.next(value));
25
- } catch (e) {
26
- reject(e);
27
- }
28
- };
29
- var rejected = (value) => {
30
- try {
31
- step(generator.throw(value));
32
- } catch (e) {
33
- reject(e);
34
- }
35
- };
36
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
- step((generator = generator.apply(__this, __arguments)).next());
38
- });
39
- };
40
-
41
1
  // src/transport/beaconTransport.ts
42
2
  import { gzipCompress } from "../compress/compression";
43
3
  import { getReportApi } from "../config";
@@ -53,27 +13,26 @@ var BeaconTransport = class {
53
13
  isSupported() {
54
14
  return isBrowser() && typeof navigator !== "undefined" && navigator.sendBeacon && typeof navigator.sendBeacon === "function";
55
15
  }
56
- send(payload) {
57
- return __async(this, null, function* () {
58
- var _a, _b, _c, _d, _e;
59
- let body = typeof payload === "string" ? payload : safeStringify(payload);
60
- const endpoint = getReportApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop");
61
- let contentType = "application/json";
62
- if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && (!((_c = this.opts) == null ? void 0 : _c.gzipOnlyInBatchMode) || payload.items.length > 0)) {
63
- const compressedItems = yield gzipCompress(safeStringify(payload.items));
64
- body = safeStringify(__spreadProps(__spreadValues({}, payload), {
65
- items: compressedItems
66
- }));
67
- contentType = "application/json; charset=utf-8";
68
- }
69
- const blob = new Blob([body], { type: contentType });
70
- const success = navigator.sendBeacon(endpoint, blob);
71
- logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "sendBeacon result", success);
72
- if (!success) {
73
- logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "sendBeacon failed (queue full or other error)");
74
- }
75
- return Promise.resolve();
76
- });
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
+ let contentType = "application/json";
21
+ if (((_b = this.opts) == null ? void 0 : _b.enableGzip) && (!((_c = this.opts) == null ? void 0 : _c.gzipOnlyInBatchMode) || payload.items.length > 0)) {
22
+ const compressedItems = await gzipCompress(safeStringify(payload.items));
23
+ body = safeStringify({
24
+ ...payload,
25
+ items: compressedItems
26
+ });
27
+ contentType = "application/json; charset=utf-8";
28
+ }
29
+ const blob = new Blob([body], { type: contentType });
30
+ const success = navigator.sendBeacon(endpoint, blob);
31
+ logDebug(!!((_d = this.opts) == null ? void 0 : _d.debug), "sendBeacon result", success);
32
+ if (!success) {
33
+ logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "sendBeacon failed (queue full or other error)");
34
+ }
35
+ return Promise.resolve();
77
36
  }
78
37
  };
79
38
  export {
@@ -1,24 +1,3 @@
1
- var __async = (__this, __arguments, generator) => {
2
- return new Promise((resolve, reject) => {
3
- var fulfilled = (value) => {
4
- try {
5
- step(generator.next(value));
6
- } catch (e) {
7
- reject(e);
8
- }
9
- };
10
- var rejected = (value) => {
11
- try {
12
- step(generator.throw(value));
13
- } catch (e) {
14
- reject(e);
15
- }
16
- };
17
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
- step((generator = generator.apply(__this, __arguments)).next());
19
- });
20
- };
21
-
22
1
  // src/transport/pixelImageTransport.ts
23
2
  import { isBrowser } from "../utils/environment";
24
3
  import { logDebug, now, safeStringify } from "../utils/tools";
@@ -34,62 +13,60 @@ var PixelImageTransport = class {
34
13
  isSupported() {
35
14
  return isBrowser() && typeof Image !== "undefined";
36
15
  }
37
- send(payload) {
38
- return __async(this, null, function* () {
39
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
40
- const body = safeStringify(payload.items);
41
- const endpoint = getPixelBatchApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop");
42
- const param = "items";
43
- const maxLen = ((_b = this.opts) == null ? void 0 : _b.maxPixelUrlLen) || 8192;
44
- let compressedBody;
45
- if (((_c = this.opts) == null ? void 0 : _c.enableGzip) && (!((_d = this.opts) == null ? void 0 : _d.gzipOnlyInBatchMode) || payload.items.length > 0)) {
46
- const t = now();
47
- logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "PixelImage request gzip compress body: ", body);
48
- compressedBody = yield gzipCompress(body);
49
- logDebug(!!((_f = this.opts) == null ? void 0 : _f.debug), "PixelImage request gzip compress cost: ", now() - t);
50
- logDebug(!!((_g = this.opts) == null ? void 0 : _g.debug), `original body size: ${body.length}, compressed body size: ${compressedBody.length}`);
51
- logDebug(!!((_h = this.opts) == null ? void 0 : _h.debug), "PixelImage request gzip compress body: ", compressedBody);
52
- } else {
53
- compressedBody = convert2Base64(body);
54
- }
55
- const cacheBuster = `_=${Date.now()}`;
56
- const qs = `appId=${((_i = this.opts) == null ? void 0 : _i.appId) || ""}&appStage=${((_j = this.opts) == null ? void 0 : _j.logStage) || ""}&${param}=${encodeURIComponent(compressedBody)}&gzip=${((_k = this.opts) == null ? void 0 : _k.enableGzip) ? 1 : 0}&${cacheBuster}`;
57
- const url = endpoint.includes("?") ? `${endpoint}&${qs}` : `${endpoint}?${qs}`;
58
- if (url.length > maxLen) {
59
- logDebug(!!((_l = this.opts) == null ? void 0 : _l.debug), `URL too long (${url.length} > ${maxLen})`);
60
- }
61
- return new Promise((resolve, reject) => {
62
- const img = new Image();
63
- let timeoutId = null;
64
- let settled = false;
65
- timeoutId = setTimeout(() => {
66
- if (!settled) {
67
- settled = true;
68
- img.src = "";
69
- reject(new Error("Image request timeout after 5000ms"));
70
- }
71
- }, 5e3);
72
- img.onload = () => {
73
- if (timeoutId)
74
- clearTimeout(timeoutId);
75
- if (!settled) {
76
- settled = true;
77
- resolve();
78
- }
79
- };
80
- img.onerror = (event, source, lineno, colno, error) => {
81
- var _a2, _b2;
82
- logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "Image request failed", event, source, lineno, colno, error);
83
- if (timeoutId)
84
- clearTimeout(timeoutId);
85
- if (!settled) {
86
- settled = true;
87
- logDebug(!!((_b2 = this.opts) == null ? void 0 : _b2.debug), "Image request failed", event, source, lineno, colno, error);
88
- resolve();
89
- }
90
- };
91
- img.src = url;
92
- });
16
+ async send(payload) {
17
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
18
+ const body = safeStringify(payload.items);
19
+ const endpoint = getPixelBatchApi(((_a = this.opts) == null ? void 0 : _a.env) || "develop");
20
+ const param = "items";
21
+ const maxLen = ((_b = this.opts) == null ? void 0 : _b.maxPixelUrlLen) || 8192;
22
+ let compressedBody;
23
+ if (((_c = this.opts) == null ? void 0 : _c.enableGzip) && (!((_d = this.opts) == null ? void 0 : _d.gzipOnlyInBatchMode) || payload.items.length > 0)) {
24
+ const t = now();
25
+ logDebug(!!((_e = this.opts) == null ? void 0 : _e.debug), "PixelImage request gzip compress body: ", body);
26
+ compressedBody = await gzipCompress(body);
27
+ logDebug(!!((_f = this.opts) == null ? void 0 : _f.debug), "PixelImage request gzip compress cost: ", now() - t);
28
+ logDebug(!!((_g = this.opts) == null ? void 0 : _g.debug), `original body size: ${body.length}, compressed body size: ${compressedBody.length}`);
29
+ logDebug(!!((_h = this.opts) == null ? void 0 : _h.debug), "PixelImage request gzip compress body: ", compressedBody);
30
+ } else {
31
+ compressedBody = convert2Base64(body);
32
+ }
33
+ const cacheBuster = `_=${Date.now()}`;
34
+ const qs = `appId=${((_i = this.opts) == null ? void 0 : _i.appId) || ""}&appStage=${((_j = this.opts) == null ? void 0 : _j.logStage) || ""}&${param}=${encodeURIComponent(compressedBody)}&gzip=${((_k = this.opts) == null ? void 0 : _k.enableGzip) ? 1 : 0}&${cacheBuster}`;
35
+ const url = endpoint.includes("?") ? `${endpoint}&${qs}` : `${endpoint}?${qs}`;
36
+ if (url.length > maxLen) {
37
+ logDebug(!!((_l = this.opts) == null ? void 0 : _l.debug), `URL too long (${url.length} > ${maxLen})`);
38
+ }
39
+ return new Promise((resolve, reject) => {
40
+ const img = new Image();
41
+ let timeoutId = null;
42
+ let settled = false;
43
+ timeoutId = setTimeout(() => {
44
+ if (!settled) {
45
+ settled = true;
46
+ img.src = "";
47
+ reject(new Error("Image request timeout after 5000ms"));
48
+ }
49
+ }, 5e3);
50
+ img.onload = () => {
51
+ if (timeoutId)
52
+ clearTimeout(timeoutId);
53
+ if (!settled) {
54
+ settled = true;
55
+ resolve();
56
+ }
57
+ };
58
+ img.onerror = (event, source, lineno, colno, error) => {
59
+ var _a2, _b2;
60
+ logDebug(!!((_a2 = this.opts) == null ? void 0 : _a2.debug), "Image request failed", event, source, lineno, colno, error);
61
+ if (timeoutId)
62
+ clearTimeout(timeoutId);
63
+ if (!settled) {
64
+ settled = true;
65
+ logDebug(!!((_b2 = this.opts) == null ? void 0 : _b2.debug), "Image request failed", event, source, lineno, colno, error);
66
+ resolve();
67
+ }
68
+ };
69
+ img.src = url;
93
70
  });
94
71
  }
95
72
  };