@pluve/logger-sdk 0.0.1 → 0.0.3
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 +195 -76
- package/dist/index.js +9 -0
- package/dist/loggerSDK.d.ts +36 -0
- package/dist/{esm/dbQueue.js → loggerSDK.js} +195 -161
- package/dist/transportAdapter.d.ts +51 -0
- package/dist/{esm/transportAdapter.js → transportAdapter.js} +260 -111
- package/dist/types.d.ts +46 -0
- package/dist/utils.d.ts +30 -0
- package/dist/utils.js +229 -0
- package/lib/dbQueue.js +133 -0
- package/{dist/esm → lib}/index.js +1 -1
- package/lib/loggerSDK.js +571 -0
- package/lib/storeAdapter.js +99 -0
- package/lib/transportAdapter.d.ts +66 -0
- package/lib/transportAdapter.js +406 -0
- package/lib/types.js +1 -0
- package/lib/utils.js +50 -0
- package/package.json +8 -2
- package/dist/cjs/dbQueue.js +0 -88
- package/dist/cjs/index.js +0 -29
- package/dist/cjs/loggerSDK.js +0 -426
- package/dist/cjs/storeAdapter.js +0 -64
- package/dist/cjs/transportAdapter.d.ts +0 -5
- package/dist/cjs/transportAdapter.js +0 -109
- package/dist/cjs/types.js +0 -17
- package/dist/cjs/utils.js +0 -69
- package/dist/esm/dbQueue.d.ts +0 -10
- package/dist/esm/loggerSDK.d.ts +0 -29
- package/dist/esm/loggerSDK.js +0 -761
- package/dist/esm/storeAdapter.d.ts +0 -7
- package/dist/esm/storeAdapter.js +0 -139
- package/dist/esm/transportAdapter.d.ts +0 -5
- package/dist/esm/types.d.ts +0 -35
- package/dist/esm/utils.d.ts +0 -5
- package/dist/esm/utils.js +0 -53
- package/dist/umd/logger-sdk.min.js +0 -1
- /package/dist/{cjs/index.d.ts → index.d.ts} +0 -0
- /package/dist/{esm/types.js → types.js} +0 -0
- /package/{dist/cjs → lib}/dbQueue.d.ts +0 -0
- /package/{dist/esm → lib}/index.d.ts +0 -0
- /package/{dist/cjs → lib}/loggerSDK.d.ts +0 -0
- /package/{dist/cjs → lib}/storeAdapter.d.ts +0 -0
- /package/{dist/cjs → lib}/types.d.ts +0 -0
- /package/{dist/cjs → lib}/utils.d.ts +0 -0
package/dist/cjs/loggerSDK.js
DELETED
|
@@ -1,426 +0,0 @@
|
|
|
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 __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
|
|
29
|
-
// src/loggerSDK.ts
|
|
30
|
-
var loggerSDK_exports = {};
|
|
31
|
-
__export(loggerSDK_exports, {
|
|
32
|
-
LoggerSDK: () => LoggerSDK,
|
|
33
|
-
default: () => loggerSDK_default
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(loggerSDK_exports);
|
|
36
|
-
var import_dbQueue = __toESM(require("./dbQueue"));
|
|
37
|
-
var import_transportAdapter = require("./transportAdapter");
|
|
38
|
-
var import_utils = require("./utils");
|
|
39
|
-
var import_storeAdapter = require("./storeAdapter");
|
|
40
|
-
var LoggerSDK = class {
|
|
41
|
-
constructor(options) {
|
|
42
|
-
this.inMemoryQueue = [];
|
|
43
|
-
this.seq = 0;
|
|
44
|
-
this.timerId = null;
|
|
45
|
-
this.storage = null;
|
|
46
|
-
this.closed = false;
|
|
47
|
-
this.idbQueue = null;
|
|
48
|
-
this.flushing = false;
|
|
49
|
-
const defaults = {
|
|
50
|
-
endpoints: options.endpoints || {},
|
|
51
|
-
appId: options.appId || "",
|
|
52
|
-
env: options.env || (() => {
|
|
53
|
-
if ((0, import_utils.isWeChatMiniProgram)())
|
|
54
|
-
return "wechat";
|
|
55
|
-
if ((0, import_utils.isBrowser)())
|
|
56
|
-
return "h5";
|
|
57
|
-
return "unknown";
|
|
58
|
-
})(),
|
|
59
|
-
batchSize: options.batchSize || 10,
|
|
60
|
-
flushInterval: options.flushInterval || 5e3,
|
|
61
|
-
retryCount: options.retryCount || 3,
|
|
62
|
-
retryBase: options.retryBase || 300,
|
|
63
|
-
storageKey: options.storageKey || "logger_sdk_cache_v2",
|
|
64
|
-
maxCacheSize: options.maxCacheSize || 2e3,
|
|
65
|
-
timeout: options.timeout || 1e4,
|
|
66
|
-
debug: !!options.debug,
|
|
67
|
-
transport: options.transport || import_transportAdapter.defaultTransport,
|
|
68
|
-
globalHeaders: options.globalHeaders || {},
|
|
69
|
-
enableAutoPV: options.enableAutoPV !== false,
|
|
70
|
-
enablePerf: options.enablePerf !== false,
|
|
71
|
-
usePixel: options.usePixel || false,
|
|
72
|
-
pixelParam: options.pixelParam || "data",
|
|
73
|
-
maxPixelUrlLen: options.maxPixelUrlLen || 1900
|
|
74
|
-
};
|
|
75
|
-
this.opts = Object.assign(defaults, options);
|
|
76
|
-
this.env = this.opts.env;
|
|
77
|
-
if (this.env === "wechat")
|
|
78
|
-
this.storage = (0, import_storeAdapter.wechatStorage)(this.opts.storageKey);
|
|
79
|
-
else if (this.env === "h5")
|
|
80
|
-
this.storage = (0, import_storeAdapter.browserStorage)(this.opts.storageKey);
|
|
81
|
-
else
|
|
82
|
-
this.storage = null;
|
|
83
|
-
if ((0, import_utils.isBrowser)() && (0, import_utils.isIndexedDBAvailable)()) {
|
|
84
|
-
this.idbQueue = new import_dbQueue.default("logger_sdk_db", "queue");
|
|
85
|
-
this.idbQueue.open().catch(() => {
|
|
86
|
-
this.idbQueue = null;
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
this.loadFromStorage().then(() => {
|
|
90
|
-
if (this.opts.flushInterval > 0)
|
|
91
|
-
this.startTimer();
|
|
92
|
-
});
|
|
93
|
-
this.attachGlobalHandlers();
|
|
94
|
-
if (this.opts.enableAutoPV && this.env === "h5")
|
|
95
|
-
this.installAutoPV();
|
|
96
|
-
if (this.opts.enablePerf && this.env === "h5") {
|
|
97
|
-
if (document.readyState === "complete")
|
|
98
|
-
this.collectPerf();
|
|
99
|
-
else
|
|
100
|
-
window.addEventListener("load", () => this.collectPerf());
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
logDebug(...args) {
|
|
104
|
-
if (this.opts.debug)
|
|
105
|
-
console.debug("[LoggerSDK]", ...args);
|
|
106
|
-
}
|
|
107
|
-
async loadFromStorage() {
|
|
108
|
-
if (!this.storage)
|
|
109
|
-
return;
|
|
110
|
-
try {
|
|
111
|
-
const raw = await this.storage.get();
|
|
112
|
-
if (raw) {
|
|
113
|
-
const arr = JSON.parse(raw);
|
|
114
|
-
this.inMemoryQueue = arr.concat(this.inMemoryQueue).slice(-this.opts.maxCacheSize);
|
|
115
|
-
this.logDebug("loaded persisted queue", this.inMemoryQueue.length);
|
|
116
|
-
}
|
|
117
|
-
} catch (e) {
|
|
118
|
-
this.logDebug("load persisted fail", e);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
async persistToStorage() {
|
|
122
|
-
if (!this.storage)
|
|
123
|
-
return;
|
|
124
|
-
try {
|
|
125
|
-
await this.storage.set((0, import_utils.safeStringify)(this.inMemoryQueue));
|
|
126
|
-
this.logDebug("persisted queue", this.inMemoryQueue.length);
|
|
127
|
-
} catch (e) {
|
|
128
|
-
this.logDebug("persist fail", e);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
startTimer() {
|
|
132
|
-
if (this.timerId)
|
|
133
|
-
return;
|
|
134
|
-
this.timerId = setInterval(() => this.flush().catch(() => {
|
|
135
|
-
}), this.opts.flushInterval);
|
|
136
|
-
}
|
|
137
|
-
stopTimer() {
|
|
138
|
-
if (!this.timerId)
|
|
139
|
-
return;
|
|
140
|
-
clearInterval(this.timerId);
|
|
141
|
-
this.timerId = null;
|
|
142
|
-
}
|
|
143
|
-
// 将事件入队(先到内存,再持久化)
|
|
144
|
-
async track(event, headers) {
|
|
145
|
-
if (this.closed)
|
|
146
|
-
return;
|
|
147
|
-
this.seq += 1;
|
|
148
|
-
const e = {
|
|
149
|
-
type: event.type || "custom",
|
|
150
|
-
time: event.time || (0, import_utils.now)(),
|
|
151
|
-
user: event.user || void 0,
|
|
152
|
-
ctx: event.ctx || {},
|
|
153
|
-
level: event.level || "info",
|
|
154
|
-
seq: this.seq
|
|
155
|
-
};
|
|
156
|
-
this.inMemoryQueue.push(e);
|
|
157
|
-
this.logDebug("enqueue", e);
|
|
158
|
-
await this.persistToStorage();
|
|
159
|
-
if (this.idbQueue) {
|
|
160
|
-
try {
|
|
161
|
-
await this.idbQueue.add(e);
|
|
162
|
-
} catch (err) {
|
|
163
|
-
this.logDebug("idb add fail", err);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
if (this.inMemoryQueue.length >= this.opts.batchSize) {
|
|
167
|
-
await this.flush(headers).catch(() => {
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
// flush:从队列中取一批发送(按 level 分 endpoint,支持部分成功剔除)
|
|
172
|
-
async flush(extraHeaders) {
|
|
173
|
-
if (this.closed)
|
|
174
|
-
return;
|
|
175
|
-
if (this.inMemoryQueue.length === 0)
|
|
176
|
-
return;
|
|
177
|
-
if (this.flushing)
|
|
178
|
-
return;
|
|
179
|
-
this.flushing = true;
|
|
180
|
-
try {
|
|
181
|
-
const batch = this.inMemoryQueue.slice(0, this.opts.batchSize);
|
|
182
|
-
const groups = {};
|
|
183
|
-
batch.forEach((ev) => {
|
|
184
|
-
const level = ev.level || "info";
|
|
185
|
-
(groups[level] = groups[level] || []).push(ev);
|
|
186
|
-
});
|
|
187
|
-
const delay = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
188
|
-
const sendGroup = (level, events) => {
|
|
189
|
-
const endpoint = this.opts.endpoints[level] || this.opts.endpoints.default;
|
|
190
|
-
const payload = { appId: this.opts.appId, env: this.env, ts: (0, import_utils.now)(), level, events };
|
|
191
|
-
const maxTry = this.opts.retryCount;
|
|
192
|
-
const attemptOnce = (attempt) => {
|
|
193
|
-
const transportOpts = { ...this.opts, endpoint, headers: { ...this.opts.globalHeaders || {}, ...extraHeaders || {} } };
|
|
194
|
-
return this.opts.transport(payload, transportOpts).then(() => true).catch(async (err) => {
|
|
195
|
-
const next = attempt + 1;
|
|
196
|
-
this.logDebug("send fail", level, next, err);
|
|
197
|
-
if (next > maxTry)
|
|
198
|
-
return false;
|
|
199
|
-
const wait = (this.opts.retryBase || 300) * 2 ** (next - 1);
|
|
200
|
-
await delay(wait);
|
|
201
|
-
return attemptOnce(next);
|
|
202
|
-
});
|
|
203
|
-
};
|
|
204
|
-
return attemptOnce(0).then((ok) => ({ ok, level, events }));
|
|
205
|
-
};
|
|
206
|
-
const results = await Promise.all(Object.keys(groups).map((level) => sendGroup(level, groups[level])));
|
|
207
|
-
const successLevels = new Set(results.filter((r) => r.ok).map((r) => r.level));
|
|
208
|
-
const allOk = results.length > 0 && results.every((r) => r.ok);
|
|
209
|
-
if (!allOk && successLevels.size === 0) {
|
|
210
|
-
this.logDebug("batch send failed, keep batch in queue");
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
const removeSeq = new Set(batch.filter((e) => successLevels.has(e.level || "info")).map((e) => e.seq));
|
|
214
|
-
this.inMemoryQueue = this.inMemoryQueue.filter((x) => !removeSeq.has(x.seq));
|
|
215
|
-
await this.persistToStorage();
|
|
216
|
-
if (allOk && this.idbQueue) {
|
|
217
|
-
try {
|
|
218
|
-
await this.idbQueue.clear();
|
|
219
|
-
} catch (e) {
|
|
220
|
-
this.logDebug("idb clear fail", e);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
} finally {
|
|
224
|
-
this.flushing = false;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
// 将队列尽力全部发送(分批轮询 + 安全阈值)
|
|
228
|
-
async flushAll() {
|
|
229
|
-
let safety = 0;
|
|
230
|
-
return new Promise((resolve) => {
|
|
231
|
-
const tick = async () => {
|
|
232
|
-
safety += 1;
|
|
233
|
-
if (this.inMemoryQueue.length === 0 || safety >= 200)
|
|
234
|
-
return resolve();
|
|
235
|
-
if (!this.flushing) {
|
|
236
|
-
try {
|
|
237
|
-
await this.flush();
|
|
238
|
-
} catch {
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
setTimeout(tick, 50);
|
|
242
|
-
};
|
|
243
|
-
tick();
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
async identify(user) {
|
|
247
|
-
this.opts.appId = user && user.appId || this.opts.appId;
|
|
248
|
-
this.logDebug("identify", user);
|
|
249
|
-
}
|
|
250
|
-
async setCommon(params) {
|
|
251
|
-
Object.assign(this.opts, params);
|
|
252
|
-
this.logDebug("setCommon", params);
|
|
253
|
-
}
|
|
254
|
-
destroy() {
|
|
255
|
-
this.stopTimer();
|
|
256
|
-
this.closed = true;
|
|
257
|
-
}
|
|
258
|
-
// ========== 自动采集与错误处理 ===========
|
|
259
|
-
attachGlobalHandlers() {
|
|
260
|
-
if (this.env === "h5" && typeof window !== "undefined") {
|
|
261
|
-
const win = window;
|
|
262
|
-
win.addEventListener && win.addEventListener("error", (ev) => {
|
|
263
|
-
try {
|
|
264
|
-
this.track({ type: "error", level: "error", ctx: { message: ev.message, filename: ev.filename, lineno: ev.lineno, colno: ev.colno, stack: ev.error && ev.error.stack } });
|
|
265
|
-
} catch {
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
win.addEventListener && win.addEventListener("unhandledrejection", (ev) => {
|
|
269
|
-
try {
|
|
270
|
-
this.track({ type: "error", level: "error", ctx: { reason: ev.reason && (ev.reason.stack || ev.reason) } });
|
|
271
|
-
} catch {
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
win.addEventListener && win.addEventListener(
|
|
275
|
-
"error",
|
|
276
|
-
(ev) => {
|
|
277
|
-
if (ev.target && (ev.target.src || ev.target.href)) {
|
|
278
|
-
try {
|
|
279
|
-
this.track({ type: "error", level: "warn", ctx: { resource: ev.target.src || ev.target.href, tag: ev.target.tagName } });
|
|
280
|
-
} catch {
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
},
|
|
284
|
-
true
|
|
285
|
-
);
|
|
286
|
-
document.addEventListener && document.addEventListener("visibilitychange", () => {
|
|
287
|
-
try {
|
|
288
|
-
if (document.visibilityState === "hidden")
|
|
289
|
-
this.flushBeacon();
|
|
290
|
-
} catch {
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
win.addEventListener && win.addEventListener("pagehide", () => {
|
|
294
|
-
try {
|
|
295
|
-
this.flushBeacon();
|
|
296
|
-
} catch {
|
|
297
|
-
}
|
|
298
|
-
});
|
|
299
|
-
win.addEventListener && win.addEventListener("beforeunload", () => {
|
|
300
|
-
try {
|
|
301
|
-
this.flushBeacon();
|
|
302
|
-
} catch {
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
// 自动 PV(single-page 支持简单的 history 池监听)
|
|
308
|
-
installAutoPV() {
|
|
309
|
-
if (!(0, import_utils.isBrowser)())
|
|
310
|
-
return;
|
|
311
|
-
const { history, location } = window;
|
|
312
|
-
const { pushState } = history;
|
|
313
|
-
const { replaceState } = history;
|
|
314
|
-
const onUrlChange = () => {
|
|
315
|
-
try {
|
|
316
|
-
this.track({ type: "pageview", level: "info", ctx: { path: location.pathname + location.search, title: document.title } });
|
|
317
|
-
} catch {
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
history.pushState = function(...args) {
|
|
321
|
-
pushState.apply(this, args);
|
|
322
|
-
window.dispatchEvent(new Event("locationchange"));
|
|
323
|
-
};
|
|
324
|
-
history.replaceState = function(...args) {
|
|
325
|
-
replaceState.apply(this, args);
|
|
326
|
-
window.dispatchEvent(new Event("locationchange"));
|
|
327
|
-
};
|
|
328
|
-
window.addEventListener("popstate", () => window.dispatchEvent(new Event("locationchange")));
|
|
329
|
-
window.addEventListener("locationchange", onUrlChange);
|
|
330
|
-
onUrlChange();
|
|
331
|
-
}
|
|
332
|
-
// 性能采集(Navigation timing + Paint)
|
|
333
|
-
collectPerf() {
|
|
334
|
-
if (!(0, import_utils.isBrowser)() || !("performance" in window))
|
|
335
|
-
return;
|
|
336
|
-
try {
|
|
337
|
-
const perf = window.performance;
|
|
338
|
-
const nav = perf.getEntriesByType && perf.getEntriesByType("navigation") && perf.getEntriesByType("navigation")[0];
|
|
339
|
-
const paints = perf.getEntriesByType ? perf.getEntriesByType("paint") : [];
|
|
340
|
-
const data = {};
|
|
341
|
-
if (nav) {
|
|
342
|
-
data.ttfb = nav.responseStart - nav.requestStart;
|
|
343
|
-
data.domContentLoaded = nav.domContentLoadedEventEnd - nav.startTime;
|
|
344
|
-
data.load = nav.loadEventEnd - nav.startTime;
|
|
345
|
-
} else if (perf.timing) {
|
|
346
|
-
const t = perf.timing;
|
|
347
|
-
data.ttfb = t.responseStart - t.requestStart;
|
|
348
|
-
data.domContentLoaded = t.domContentLoadedEventEnd - t.navigationStart;
|
|
349
|
-
data.load = t.loadEventEnd - t.navigationStart;
|
|
350
|
-
}
|
|
351
|
-
if (paints && paints.length) {
|
|
352
|
-
paints.forEach((p) => {
|
|
353
|
-
data[p.name] = p.startTime;
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
this.track({ type: "perf", level: "info", ctx: data });
|
|
357
|
-
} catch (e) {
|
|
358
|
-
this.logDebug("collect perf fail", e);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
flushBeacon() {
|
|
362
|
-
if (!(0, import_utils.isBrowser)())
|
|
363
|
-
return;
|
|
364
|
-
if (this.inMemoryQueue.length === 0)
|
|
365
|
-
return;
|
|
366
|
-
const groups = {};
|
|
367
|
-
this.inMemoryQueue.forEach((ev) => {
|
|
368
|
-
const level = ev.level || "info";
|
|
369
|
-
(groups[level] = groups[level] || []).push(ev);
|
|
370
|
-
});
|
|
371
|
-
Object.keys(groups).forEach((level) => {
|
|
372
|
-
const events = groups[level];
|
|
373
|
-
const endpoint = this.opts.endpoints[level] || this.opts.endpoints.default;
|
|
374
|
-
const payload = { appId: this.opts.appId, env: this.env, ts: (0, import_utils.now)(), level, events };
|
|
375
|
-
const transportOpts = {
|
|
376
|
-
...this.opts,
|
|
377
|
-
endpoint,
|
|
378
|
-
headers: { ...this.opts.globalHeaders || {} },
|
|
379
|
-
useBeacon: true,
|
|
380
|
-
usePixel: this.opts.usePixel,
|
|
381
|
-
pixelParam: this.opts.pixelParam,
|
|
382
|
-
maxPixelUrlLen: this.opts.maxPixelUrlLen
|
|
383
|
-
};
|
|
384
|
-
try {
|
|
385
|
-
this.opts.transport(payload, transportOpts);
|
|
386
|
-
} catch {
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
// 生成简单 Demo 页面(HTML 字符串),方便生成 demo 文档站点
|
|
391
|
-
// public generateDemoHtml(opts?: { scriptUrl?: string }) {
|
|
392
|
-
// const scriptUrl = (opts && opts.scriptUrl) || 'dist/index.umd.js';
|
|
393
|
-
// const html = ```
|
|
394
|
-
// <!doctype html>
|
|
395
|
-
// <html>
|
|
396
|
-
// <head>
|
|
397
|
-
// <meta charset="utf-8" />
|
|
398
|
-
// <title>LoggerSDK Demo</title>
|
|
399
|
-
// </head>
|
|
400
|
-
// <body>
|
|
401
|
-
// <h1>LoggerSDK Demo</h1>
|
|
402
|
-
// <script src="${scriptUrl}"></script>
|
|
403
|
-
// <script>
|
|
404
|
-
// // 通过 UMD 全局 MyLogger 使用
|
|
405
|
-
// const sdk = new MyLogger.LoggerSDK({
|
|
406
|
-
// endpoints: { default: '/api/collect', info: '/api/collect/info', error: '/api/collect/error' },
|
|
407
|
-
// appId: 'demo-app',
|
|
408
|
-
// debug: true,
|
|
409
|
-
// enableAutoPV: true,
|
|
410
|
-
// enablePerf: true
|
|
411
|
-
// });
|
|
412
|
-
// // 手动埋点
|
|
413
|
-
// sdk.track({ type: 'custom', level: 'info', ctx: { action: 'click_demo' } });
|
|
414
|
-
// // 模拟错误
|
|
415
|
-
// setTimeout(() => { throw new Error('demo error'); }, 2000);
|
|
416
|
-
// </script>
|
|
417
|
-
// </body>
|
|
418
|
-
// </html>```;
|
|
419
|
-
// return html;
|
|
420
|
-
// }
|
|
421
|
-
};
|
|
422
|
-
var loggerSDK_default = LoggerSDK;
|
|
423
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
424
|
-
0 && (module.exports = {
|
|
425
|
-
LoggerSDK
|
|
426
|
-
});
|
package/dist/cjs/storeAdapter.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// src/storeAdapter.ts
|
|
20
|
-
var storeAdapter_exports = {};
|
|
21
|
-
__export(storeAdapter_exports, {
|
|
22
|
-
browserStorage: () => browserStorage,
|
|
23
|
-
wechatStorage: () => wechatStorage
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(storeAdapter_exports);
|
|
26
|
-
function browserStorage(key) {
|
|
27
|
-
return {
|
|
28
|
-
async get() {
|
|
29
|
-
return Promise.resolve(localStorage.getItem(key));
|
|
30
|
-
},
|
|
31
|
-
async set(val) {
|
|
32
|
-
localStorage.setItem(key, val);
|
|
33
|
-
return Promise.resolve();
|
|
34
|
-
},
|
|
35
|
-
async remove() {
|
|
36
|
-
localStorage.removeItem(key);
|
|
37
|
-
return Promise.resolve();
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
function wechatStorage(key) {
|
|
42
|
-
return {
|
|
43
|
-
async get() {
|
|
44
|
-
return new Promise((res) => {
|
|
45
|
-
wx.getStorage({ key, success: (r) => res(r.data), fail: () => res(null) });
|
|
46
|
-
});
|
|
47
|
-
},
|
|
48
|
-
async set(val) {
|
|
49
|
-
return new Promise((res) => {
|
|
50
|
-
wx.setStorage({ key, data: val, success: () => res(void 0), fail: () => res(void 0) });
|
|
51
|
-
});
|
|
52
|
-
},
|
|
53
|
-
async remove() {
|
|
54
|
-
return new Promise((res) => {
|
|
55
|
-
wx.removeStorage({ key, success: () => res(void 0), fail: () => res(void 0) });
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
61
|
-
0 && (module.exports = {
|
|
62
|
-
browserStorage,
|
|
63
|
-
wechatStorage
|
|
64
|
-
});
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// src/transportAdapter.ts
|
|
20
|
-
var transportAdapter_exports = {};
|
|
21
|
-
__export(transportAdapter_exports, {
|
|
22
|
-
defaultTransport: () => defaultTransport
|
|
23
|
-
});
|
|
24
|
-
module.exports = __toCommonJS(transportAdapter_exports);
|
|
25
|
-
var import_utils = require("./utils");
|
|
26
|
-
async function defaultTransport(payload, opts) {
|
|
27
|
-
var _a;
|
|
28
|
-
const body = typeof payload === "string" ? payload : (0, import_utils.safeStringify)(payload);
|
|
29
|
-
const timeout = (opts == null ? void 0 : opts.timeout) || 1e4;
|
|
30
|
-
const endpoint = opts && opts.endpoint ? opts.endpoint : ((_a = opts.endpoints) == null ? void 0 : _a.default) || "";
|
|
31
|
-
if ((0, import_utils.isBrowser)() && typeof navigator !== "undefined" && typeof navigator.sendBeacon === "function" && (opts == null ? void 0 : opts.useBeacon)) {
|
|
32
|
-
const blob = new Blob([body], { type: "application/json" });
|
|
33
|
-
const ok = navigator.sendBeacon(endpoint || "", blob);
|
|
34
|
-
if (ok)
|
|
35
|
-
return Promise.resolve();
|
|
36
|
-
return Promise.reject(new Error("sendBeacon failed"));
|
|
37
|
-
}
|
|
38
|
-
if ((0, import_utils.isWeChatMiniProgram)()) {
|
|
39
|
-
return new Promise((resolve, reject) => {
|
|
40
|
-
let timer = null;
|
|
41
|
-
wx.request({
|
|
42
|
-
url: endpoint || "",
|
|
43
|
-
method: "POST",
|
|
44
|
-
data: body,
|
|
45
|
-
header: { "Content-Type": "application/json", ...opts && opts.globalHeaders ? opts.globalHeaders : {} },
|
|
46
|
-
success() {
|
|
47
|
-
if (timer)
|
|
48
|
-
clearTimeout(timer);
|
|
49
|
-
resolve();
|
|
50
|
-
},
|
|
51
|
-
fail(err) {
|
|
52
|
-
if (timer)
|
|
53
|
-
clearTimeout(timer);
|
|
54
|
-
reject(err);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
timer = setTimeout(() => reject(new Error("timeout")), timeout);
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
if ((0, import_utils.isBrowser)() && (opts == null ? void 0 : opts.usePixel)) {
|
|
61
|
-
const param = (opts == null ? void 0 : opts.pixelParam) || "data";
|
|
62
|
-
const cacheBuster = `_=${(0, import_utils.now)()}`;
|
|
63
|
-
const urlBase = endpoint || "";
|
|
64
|
-
const qs = `${param}=${encodeURIComponent(body)}&${cacheBuster}`;
|
|
65
|
-
const url = urlBase.includes("?") ? `${urlBase}&${qs}` : `${urlBase}?${qs}`;
|
|
66
|
-
const maxLen = (opts == null ? void 0 : opts.maxPixelUrlLen) || 1900;
|
|
67
|
-
if (url.length > maxLen) {
|
|
68
|
-
} else {
|
|
69
|
-
const img = new Image();
|
|
70
|
-
return new Promise((resolve, reject) => {
|
|
71
|
-
let timer = null;
|
|
72
|
-
img.onload = () => {
|
|
73
|
-
if (timer)
|
|
74
|
-
clearTimeout(timer);
|
|
75
|
-
resolve();
|
|
76
|
-
};
|
|
77
|
-
img.onerror = () => {
|
|
78
|
-
if (timer)
|
|
79
|
-
clearTimeout(timer);
|
|
80
|
-
reject(new Error("pixel error"));
|
|
81
|
-
};
|
|
82
|
-
timer = setTimeout(() => reject(new Error("timeout")), timeout);
|
|
83
|
-
img.src = url;
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (typeof fetch === "function") {
|
|
88
|
-
const controller = typeof AbortController !== "undefined" ? new AbortController() : null;
|
|
89
|
-
if (controller)
|
|
90
|
-
setTimeout(() => controller.abort(), timeout);
|
|
91
|
-
return fetch(endpoint || "", {
|
|
92
|
-
method: "POST",
|
|
93
|
-
headers: { "Content-Type": "application/json", ...opts && opts.globalHeaders ? opts.globalHeaders : {}, ...opts && opts.headers ? opts.headers : {} },
|
|
94
|
-
body,
|
|
95
|
-
// 在页面关闭场景提供尽力传输能力(非标准环境忽略)
|
|
96
|
-
// @ts-ignore
|
|
97
|
-
keepalive: (opts == null ? void 0 : opts.useBeacon) ? true : void 0,
|
|
98
|
-
signal: controller ? controller.signal : void 0
|
|
99
|
-
}).then((res) => {
|
|
100
|
-
if (!res.ok)
|
|
101
|
-
throw new Error("network response not ok");
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
return Promise.reject(new Error("no transport available"));
|
|
105
|
-
}
|
|
106
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
107
|
-
0 && (module.exports = {
|
|
108
|
-
defaultTransport
|
|
109
|
-
});
|
package/dist/cjs/types.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __copyProps = (to, from, except, desc) => {
|
|
6
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
-
for (let key of __getOwnPropNames(from))
|
|
8
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
-
}
|
|
11
|
-
return to;
|
|
12
|
-
};
|
|
13
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
-
|
|
15
|
-
// src/types.ts
|
|
16
|
-
var types_exports = {};
|
|
17
|
-
module.exports = __toCommonJS(types_exports);
|
package/dist/cjs/utils.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
|
|
19
|
-
// src/utils.ts
|
|
20
|
-
var utils_exports = {};
|
|
21
|
-
__export(utils_exports, {
|
|
22
|
-
isBrowser: () => isBrowser,
|
|
23
|
-
isIndexedDBAvailable: () => isIndexedDBAvailable,
|
|
24
|
-
isWeChatMiniProgram: () => isWeChatMiniProgram,
|
|
25
|
-
now: () => now,
|
|
26
|
-
safeStringify: () => safeStringify
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(utils_exports);
|
|
29
|
-
var now = () => Date.now();
|
|
30
|
-
function isWeChatMiniProgram() {
|
|
31
|
-
try {
|
|
32
|
-
return typeof wx !== "undefined" && typeof wx.request === "function";
|
|
33
|
-
} catch (e) {
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
function isBrowser() {
|
|
38
|
-
try {
|
|
39
|
-
return typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
40
|
-
} catch (e) {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function safeStringify(obj) {
|
|
45
|
-
const seen = /* @__PURE__ */ new WeakSet();
|
|
46
|
-
return JSON.stringify(obj, function(_k, v) {
|
|
47
|
-
if (v && typeof v === "object") {
|
|
48
|
-
if (seen.has(v))
|
|
49
|
-
return "[Circular]";
|
|
50
|
-
seen.add(v);
|
|
51
|
-
}
|
|
52
|
-
return v;
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
function isIndexedDBAvailable() {
|
|
56
|
-
try {
|
|
57
|
-
return typeof indexedDB !== "undefined";
|
|
58
|
-
} catch (e) {
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
63
|
-
0 && (module.exports = {
|
|
64
|
-
isBrowser,
|
|
65
|
-
isIndexedDBAvailable,
|
|
66
|
-
isWeChatMiniProgram,
|
|
67
|
-
now,
|
|
68
|
-
safeStringify
|
|
69
|
-
});
|