@pluve/logger-sdk 0.0.7 → 0.0.8

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 (103) hide show
  1. package/README.md +60 -20
  2. package/dist/cjs/capture/jsError.js +48 -0
  3. package/dist/cjs/capture/promiseError.js +49 -0
  4. package/dist/cjs/capture/resourceError.js +48 -0
  5. package/dist/cjs/capture/wechatError.js +95 -0
  6. package/dist/cjs/compress/compression.js +84 -0
  7. package/dist/cjs/config/index.js +55 -0
  8. package/dist/cjs/core/fingerprint.js +36 -0
  9. package/dist/cjs/core/httpClient.js +96 -0
  10. package/dist/cjs/core/loggerSDK.js +641 -0
  11. package/dist/cjs/core/queueManager.js +249 -0
  12. package/dist/cjs/core/retryManager.js +127 -0
  13. package/dist/cjs/index.js +29 -0
  14. package/dist/cjs/logger-sdk.mermaid +84 -0
  15. package/dist/cjs/logger-sdk.svg +1 -0
  16. package/dist/cjs/stack/stacktrace.js +48 -0
  17. package/dist/cjs/transport/beaconTransport.js +64 -0
  18. package/dist/cjs/transport/pixelImageTransport.js +100 -0
  19. package/dist/cjs/transport/transport.js +17 -0
  20. package/dist/cjs/transport/transportAdapter.js +56 -0
  21. package/dist/cjs/transport/wechatTransport.js +103 -0
  22. package/dist/cjs/types/api.js +17 -0
  23. package/dist/cjs/types/env.js +17 -0
  24. package/dist/cjs/types/external.d.ts +21 -0
  25. package/dist/cjs/types/logEvent.js +17 -0
  26. package/dist/cjs/types/logEventLevel.js +17 -0
  27. package/dist/cjs/types/sdkOptions.js +17 -0
  28. package/dist/cjs/types/trackOptions.js +17 -0
  29. package/dist/cjs/utils/environment.js +183 -0
  30. package/dist/cjs/utils/session.js +31 -0
  31. package/dist/cjs/utils/tools.js +82 -0
  32. package/dist/cjs/utils/uuid.js +35 -0
  33. package/dist/esm/capture/jsError.js +45 -0
  34. package/dist/esm/capture/promiseError.js +46 -0
  35. package/dist/esm/capture/resourceError.js +24 -0
  36. package/dist/esm/capture/wechatError.js +92 -0
  37. package/dist/esm/compress/compression.js +82 -0
  38. package/dist/esm/config/index.js +28 -0
  39. package/dist/esm/core/fingerprint.js +12 -0
  40. package/dist/esm/core/httpClient.js +95 -0
  41. package/dist/esm/core/loggerSDK.js +650 -0
  42. package/dist/esm/core/queueManager.js +269 -0
  43. package/dist/esm/core/retryManager.js +129 -0
  44. package/dist/esm/index.js +5 -0
  45. package/dist/esm/logger-sdk.mermaid +84 -0
  46. package/dist/esm/logger-sdk.svg +1 -0
  47. package/dist/esm/stack/stacktrace.js +37 -0
  48. package/dist/esm/transport/beaconTransport.js +81 -0
  49. package/dist/esm/transport/pixelImageTransport.js +99 -0
  50. package/dist/esm/transport/transport.js +0 -0
  51. package/dist/esm/transport/transportAdapter.js +32 -0
  52. package/dist/esm/transport/wechatTransport.js +120 -0
  53. package/dist/esm/types/api.js +0 -0
  54. package/dist/esm/types/env.js +0 -0
  55. package/dist/esm/types/external.d.ts +21 -0
  56. package/dist/esm/types/logEvent.js +0 -0
  57. package/dist/esm/types/logEventLevel.js +0 -0
  58. package/dist/esm/types/sdkOptions.js +0 -0
  59. package/dist/esm/types/trackOptions.js +0 -0
  60. package/dist/esm/utils/environment.js +154 -0
  61. package/dist/esm/utils/session.js +7 -0
  62. package/dist/esm/utils/tools.js +76 -0
  63. package/dist/esm/utils/uuid.js +11 -0
  64. package/dist/types/capture/jsError.d.ts +2 -0
  65. package/dist/types/capture/promiseError.d.ts +2 -0
  66. package/dist/types/capture/resourceError.d.ts +2 -0
  67. package/dist/types/capture/wechatError.d.ts +3 -0
  68. package/dist/types/compress/compression.d.ts +10 -0
  69. package/dist/types/config/index.d.ts +9 -0
  70. package/dist/types/core/fingerprint.d.ts +8 -0
  71. package/dist/types/core/httpClient.d.ts +11 -0
  72. package/dist/{loggerSDK.d.ts → types/core/loggerSDK.d.ts} +27 -2
  73. package/dist/{queueManager.d.ts → types/core/queueManager.d.ts} +9 -1
  74. package/dist/{retryManager.d.ts → types/core/retryManager.d.ts} +0 -4
  75. package/dist/types/index.d.ts +1 -0
  76. package/dist/types/stack/stacktrace.d.ts +2 -0
  77. package/dist/types/transport/beaconTransport.d.ts +11 -0
  78. package/dist/types/transport/pixelImageTransport.d.ts +11 -0
  79. package/dist/types/transport/transport.d.ts +14 -0
  80. package/dist/types/transport/transportAdapter.d.ts +10 -0
  81. package/dist/types/transport/wechatTransport.d.ts +11 -0
  82. package/dist/types/types/api.d.ts +12 -0
  83. package/dist/types/types/env.d.ts +14 -0
  84. package/dist/types/types/logEvent.d.ts +57 -0
  85. package/dist/types/types/logEventLevel.d.ts +2 -0
  86. package/dist/{types.d.ts → types/types/sdkOptions.d.ts} +23 -42
  87. package/dist/types/types/trackOptions.d.ts +7 -0
  88. package/dist/types/utils/environment.d.ts +21 -0
  89. package/dist/types/utils/session.d.ts +1 -0
  90. package/dist/types/utils/tools.d.ts +12 -0
  91. package/dist/types/utils/uuid.d.ts +7 -0
  92. package/dist/umd/logger-sdk.min.js +1 -0
  93. package/package.json +29 -5
  94. package/dist/index.d.ts +0 -10
  95. package/dist/index.js +0 -13
  96. package/dist/loggerSDK.js +0 -560
  97. package/dist/queueManager.js +0 -186
  98. package/dist/retryManager.js +0 -224
  99. package/dist/transportAdapter.d.ts +0 -51
  100. package/dist/transportAdapter.js +0 -315
  101. package/dist/types.js +0 -1
  102. package/dist/utils.d.ts +0 -52
  103. package/dist/utils.js +0 -348
@@ -1,315 +0,0 @@
1
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
- function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
3
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
5
- /*
6
- * @Author : 黄震 huangzhen@yfpharmacy.com
7
- * @Date : 2025-11-21 14:35:48
8
- * @LastEditors : 黄震 huangzhen@yfpharmacy.com
9
- * @LastEditTime : 2025-12-04 22:00:00
10
- * @Description : 传输适配器 - Beacon、像素图和微信小程序方式
11
- * Copyright (c) 2025 by 益丰大药房连锁股份有限公司, All Rights Reserved.
12
- */
13
-
14
- import { safeStringify, isBrowser, isWeChatMiniProgram, now, gzipCompress } from "./utils";
15
-
16
- /** 传输选项接口 */
17
-
18
- /** 传输适配器接口 */
19
-
20
- // ==================== 辅助函数 ====================
21
-
22
- /** 获取端点 URL */
23
- function getEndpoint(opts) {
24
- return (opts === null || opts === void 0 ? void 0 : opts.endpoint) || '';
25
- }
26
-
27
- // ==================== 1. Beacon 传输适配器(页面卸载场景首选)====================
28
-
29
- /**
30
- * Beacon 传输适配器
31
- * - 兼容性:现代浏览器(Chrome 39+, Firefox 31+, Edge 14+)
32
- * - 健壮性:页面卸载时可靠传输、不阻塞页面卸载
33
- * - 适用场景:页面关闭、visibilitychange、pagehide、beforeunload 事件
34
- * - 限制:无法获取响应、队列有大小限制(通常 64KB)
35
- */
36
- export var beaconTransport = {
37
- name: 'beacon',
38
- isSupported: function isSupported() {
39
- return isBrowser() && typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function';
40
- },
41
- send: function send(payload, opts) {
42
- return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
43
- var body, endpoint, contentType, blob, success;
44
- return _regeneratorRuntime().wrap(function _callee$(_context) {
45
- while (1) switch (_context.prev = _context.next) {
46
- case 0:
47
- body = typeof payload === 'string' ? payload : safeStringify(payload);
48
- endpoint = getEndpoint(opts); // 如果启用 gzip 压缩
49
- contentType = 'application/json';
50
- if (!(opts !== null && opts !== void 0 && opts.enableGzip)) {
51
- _context.next = 8;
52
- break;
53
- }
54
- _context.next = 6;
55
- return gzipCompress(body);
56
- case 6:
57
- body = _context.sent;
58
- contentType = 'application/json; charset=utf-8';
59
- case 8:
60
- // sendBeacon 使用 Blob 确保正确的 Content-Type
61
- blob = new Blob([body], {
62
- type: contentType
63
- });
64
- success = navigator.sendBeacon(endpoint, blob);
65
- if (success) {
66
- _context.next = 12;
67
- break;
68
- }
69
- throw new Error('sendBeacon failed (queue full or other error)');
70
- case 12:
71
- case "end":
72
- return _context.stop();
73
- }
74
- }, _callee);
75
- }))();
76
- }
77
- };
78
-
79
- // ==================== 2. 微信小程序传输适配器(微信环境专用)====================
80
-
81
- /**
82
- * 微信小程序传输适配器
83
- * - 兼容性:微信小程序环境
84
- * - 健壮性:支持超时控制、完善的错误处理
85
- * - 适用场景:微信小程序环境下的日志上报
86
- */
87
- export var wechatTransport = {
88
- name: 'wechat',
89
- isSupported: function isSupported() {
90
- return isWeChatMiniProgram();
91
- },
92
- send: function send(payload, opts) {
93
- return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
94
- var body, endpoint, timeout, contentType;
95
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
96
- while (1) switch (_context2.prev = _context2.next) {
97
- case 0:
98
- body = typeof payload === 'string' ? payload : safeStringify(payload);
99
- endpoint = getEndpoint(opts);
100
- timeout = 10000; // 10秒超时
101
- // 如果启用 gzip 压缩
102
- contentType = 'application/json';
103
- if (!(opts !== null && opts !== void 0 && opts.enableGzip)) {
104
- _context2.next = 9;
105
- break;
106
- }
107
- _context2.next = 7;
108
- return gzipCompress(body);
109
- case 7:
110
- body = _context2.sent;
111
- contentType = 'application/json; charset=utf-8';
112
- case 9:
113
- return _context2.abrupt("return", new Promise(function (resolve, reject) {
114
- var timeoutId = null;
115
- var settled = false;
116
-
117
- // 超时处理
118
- timeoutId = setTimeout(function () {
119
- if (!settled) {
120
- settled = true;
121
- reject(new Error("WeChat request timeout after ".concat(timeout, "ms")));
122
- }
123
- }, timeout);
124
-
125
- // @ts-ignore
126
- wx.request({
127
- url: endpoint,
128
- method: 'POST',
129
- data: body,
130
- header: {
131
- 'Content-Type': contentType
132
- },
133
- success: function success(res) {
134
- if (timeoutId) clearTimeout(timeoutId);
135
- if (!settled) {
136
- settled = true;
137
- if (res.statusCode >= 200 && res.statusCode < 300) {
138
- resolve();
139
- } else {
140
- reject(new Error("HTTP ".concat(res.statusCode)));
141
- }
142
- }
143
- },
144
- fail: function fail(err) {
145
- if (timeoutId) clearTimeout(timeoutId);
146
- if (!settled) {
147
- settled = true;
148
- reject(new Error("WeChat request failed: ".concat(err.errMsg || 'unknown error')));
149
- }
150
- }
151
- });
152
- }));
153
- case 10:
154
- case "end":
155
- return _context2.stop();
156
- }
157
- }, _callee2);
158
- }))();
159
- }
160
- };
161
-
162
- // ==================== 3. Image 像素上报适配器(兼容性最好)====================
163
-
164
- /**
165
- * Image 像素上报适配器
166
- * - 兼容性:所有浏览器
167
- * - 健壮性:轻量级、无跨域限制、支持超时控制
168
- * - 适用场景:数据量小的快速上报、跨域场景、降级方案
169
- * - 限制:URL 长度限制(默认 1900 字符)
170
- */
171
- export var imageTransport = {
172
- name: 'image',
173
- isSupported: function isSupported() {
174
- return isBrowser() && typeof Image !== 'undefined';
175
- },
176
- send: function send(payload, opts) {
177
- return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
178
- var body, endpoint, param, maxLen, cacheBuster, qs, url;
179
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
180
- while (1) switch (_context3.prev = _context3.next) {
181
- case 0:
182
- body = typeof payload === 'string' ? payload : safeStringify(payload);
183
- endpoint = getEndpoint(opts);
184
- param = 'data'; // opts?.pixelParam || 'data';
185
- maxLen = (opts === null || opts === void 0 ? void 0 : opts.maxPixelUrlLen) || 4096; // 构建 URL
186
- cacheBuster = "_=".concat(now());
187
- qs = "".concat(param, "=").concat(encodeURIComponent(body), "&").concat(cacheBuster);
188
- url = endpoint.includes('?') ? "".concat(endpoint, "&").concat(qs) : "".concat(endpoint, "?").concat(qs); // URL 长度检查
189
- if (!(url.length > maxLen)) {
190
- _context3.next = 9;
191
- break;
192
- }
193
- throw new Error("URL too long (".concat(url.length, " > ").concat(maxLen, ")"));
194
- case 9:
195
- return _context3.abrupt("return", new Promise(function (resolve, reject) {
196
- var img = new Image();
197
- var timeoutId = null;
198
- var settled = false;
199
-
200
- // 超时处理(5秒)
201
- timeoutId = setTimeout(function () {
202
- if (!settled) {
203
- settled = true;
204
- img.src = ''; // 取消请求
205
- reject(new Error('Image request timeout after 5000ms'));
206
- }
207
- }, 5000);
208
- img.onload = function () {
209
- if (timeoutId) clearTimeout(timeoutId);
210
- if (!settled) {
211
- settled = true;
212
- resolve();
213
- }
214
- };
215
- img.onerror = function () {
216
- if (timeoutId) clearTimeout(timeoutId);
217
- if (!settled) {
218
- settled = true;
219
- reject(new Error('Image request failed'));
220
- }
221
- };
222
-
223
- // 发起请求
224
- img.src = url;
225
- }));
226
- case 10:
227
- case "end":
228
- return _context3.stop();
229
- }
230
- }, _callee3);
231
- }))();
232
- }
233
- };
234
-
235
- // ==================== 默认传输策略 ====================
236
-
237
- /**
238
- * 默认传输函数 - 按环境选择最佳传输方式
239
- * 浏览器策略:Beacon(可靠) > Image(兼容)
240
- * 微信小程序策略:WeChat Request
241
- */
242
- export function defaultTransport(_x, _x2) {
243
- return _defaultTransport.apply(this, arguments);
244
- }
245
-
246
- /**
247
- * 导出所有适配器,方便自定义使用
248
- */
249
- function _defaultTransport() {
250
- _defaultTransport = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(payload, opts) {
251
- var transports, adapter;
252
- return _regeneratorRuntime().wrap(function _callee4$(_context4) {
253
- while (1) switch (_context4.prev = _context4.next) {
254
- case 0:
255
- if (!isWeChatMiniProgram()) {
256
- _context4.next = 5;
257
- break;
258
- }
259
- if (!wechatTransport.isSupported(opts)) {
260
- _context4.next = 5;
261
- break;
262
- }
263
- _context4.next = 4;
264
- return wechatTransport.send(payload, opts);
265
- case 4:
266
- return _context4.abrupt("return");
267
- case 5:
268
- // 浏览器环境使用 Beacon 或 Image
269
- transports = [beaconTransport, imageTransport]; // 尝试找到第一个支持的适配器
270
- adapter = transports.find(function (t) {
271
- return t.isSupported(opts);
272
- });
273
- if (adapter) {
274
- _context4.next = 9;
275
- break;
276
- }
277
- throw new Error('No supported transport adapter available');
278
- case 9:
279
- _context4.prev = 9;
280
- _context4.next = 12;
281
- return adapter.send(payload, opts);
282
- case 12:
283
- _context4.next = 27;
284
- break;
285
- case 14:
286
- _context4.prev = 14;
287
- _context4.t0 = _context4["catch"](9);
288
- if (!(adapter.name === 'beacon' && imageTransport.isSupported(opts))) {
289
- _context4.next = 26;
290
- break;
291
- }
292
- _context4.prev = 17;
293
- _context4.next = 20;
294
- return imageTransport.send(payload, opts);
295
- case 20:
296
- return _context4.abrupt("return");
297
- case 23:
298
- _context4.prev = 23;
299
- _context4.t1 = _context4["catch"](17);
300
- throw _context4.t0;
301
- case 26:
302
- throw _context4.t0;
303
- case 27:
304
- case "end":
305
- return _context4.stop();
306
- }
307
- }, _callee4, null, [[9, 14], [17, 23]]);
308
- }));
309
- return _defaultTransport.apply(this, arguments);
310
- }
311
- export var TransportAdapters = {
312
- beacon: beaconTransport,
313
- wechat: wechatTransport,
314
- image: imageTransport
315
- };
package/dist/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
package/dist/utils.d.ts DELETED
@@ -1,52 +0,0 @@
1
- export declare const now: () => number;
2
- /**
3
- * 生成 UUID v4
4
- * 符合 RFC 4122 标准
5
- * 格式: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
6
- * 其中 x 是 0-f 的十六进制数字,y 是 8/9/a/b 之一
7
- */
8
- export declare function generateUUID(): string;
9
- export declare function isBrowser(): boolean;
10
- export declare function isWeChatMiniProgram(): boolean;
11
- export declare function safeStringify(obj: any): string;
12
- /**
13
- * gzip 压缩字符串
14
- * @param data - 需要压缩的字符串
15
- * @returns 压缩后的 Base64 字符串
16
- */
17
- export declare function gzipCompress(data: string): Promise<string>;
18
- /**
19
- * 检查是否支持 gzip 压缩
20
- */
21
- export declare function isGzipSupported(): boolean;
22
- export declare function getSessionId(): string;
23
- export declare function getCurrentUrl(): string;
24
- /** 平台类型 */
25
- export type PlatformType = 'browser' | 'wechat' | 'unknown';
26
- /** 环境信息 */
27
- export interface EnvironmentInfo {
28
- platform: PlatformType;
29
- userAgent?: string;
30
- screenWidth?: number;
31
- screenHeight?: number;
32
- language?: string;
33
- systemInfo?: any;
34
- }
35
- /**
36
- * 获取环境信息
37
- */
38
- export declare function getEnvironmentInfo(): EnvironmentInfo;
39
- /**
40
- * 解析浏览器信息
41
- */
42
- export declare function parseBrowserInfo(userAgent: string): {
43
- browser: string;
44
- browserVersion: string;
45
- os: string;
46
- osVersion: string;
47
- };
48
- /**
49
- * 收集环境信息并生成 tags(仅在初始化时执行一次)
50
- */
51
- export declare function collectEnvironmentTags(debug: boolean): Record<string, any>;
52
- export declare function logDebug(debug: boolean, ...args: any[]): void;