@starko-one/sdk 0.0.0-stage → 0.1.0-beta.1
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/CHANGELOG.md +248 -0
- package/LICENSE +17 -0
- package/NOTICE +58 -0
- package/README.md +81 -2
- package/dist/api-BAFm4Lxj.d.ts +558 -0
- package/dist/api-DNrctwJu.d.cts +558 -0
- package/dist/browser/index.cjs +168 -0
- package/dist/browser/index.d.cts +131 -0
- package/dist/browser/index.d.ts +131 -0
- package/dist/browser/index.js +7 -0
- package/dist/build/rollup.cjs +22 -0
- package/dist/build/rollup.d.cts +34 -0
- package/dist/build/rollup.d.ts +34 -0
- package/dist/build/rollup.js +3 -0
- package/dist/build/vite.cjs +51 -0
- package/dist/build/vite.d.cts +23 -0
- package/dist/build/vite.d.ts +23 -0
- package/dist/build/vite.js +46 -0
- package/dist/build/webpack.cjs +18 -0
- package/dist/build/webpack.d.cts +39 -0
- package/dist/build/webpack.d.ts +39 -0
- package/dist/build/webpack.js +3 -0
- package/dist/chunk-2PM4FRM5.cjs +431 -0
- package/dist/chunk-335HNCOJ.js +249 -0
- package/dist/chunk-3AW5B2WW.js +115 -0
- package/dist/chunk-3R5TQAWD.js +216 -0
- package/dist/chunk-6H5MOPTQ.cjs +1230 -0
- package/dist/chunk-7TKNT3QJ.cjs +2487 -0
- package/dist/chunk-A4WLNFMU.cjs +220 -0
- package/dist/chunk-ALYFFOPR.cjs +134 -0
- package/dist/chunk-BVVFVRMO.js +1113 -0
- package/dist/chunk-CIHNKN62.cjs +15 -0
- package/dist/chunk-CMGCH6HD.js +1212 -0
- package/dist/chunk-D6QAPQ7L.cjs +56 -0
- package/dist/chunk-DUO5V6WZ.js +213 -0
- package/dist/chunk-EC2WZ6PG.js +90 -0
- package/dist/chunk-EFURLFJT.cjs +187 -0
- package/dist/chunk-EROUORMY.js +53 -0
- package/dist/chunk-LXBXFOAP.cjs +96 -0
- package/dist/chunk-MF6SA6DB.js +563 -0
- package/dist/chunk-N5R6ORIB.js +413 -0
- package/dist/chunk-N6R6BWHK.cjs +142 -0
- package/dist/chunk-QAGGN2T7.cjs +225 -0
- package/dist/chunk-QV4HRIDD.cjs +659 -0
- package/dist/chunk-TKNDYQB3.cjs +583 -0
- package/dist/chunk-UI5YC4TH.js +131 -0
- package/dist/chunk-VEHZYLK6.cjs +1151 -0
- package/dist/chunk-VNCWOGTI.js +12 -0
- package/dist/chunk-VWNIOGE6.js +180 -0
- package/dist/chunk-WDKRKGYF.js +608 -0
- package/dist/chunk-XOZIXBUK.js +2462 -0
- package/dist/chunk-ZS3K3SDR.cjs +258 -0
- package/dist/cli/index.cjs +2780 -0
- package/dist/common-DLFAqL2d.d.cts +36 -0
- package/dist/common-DbSXkaw6.d.ts +36 -0
- package/dist/dedupe-Bh5riD8Z.d.cts +13 -0
- package/dist/dedupe-DoYiSx-9.d.ts +13 -0
- package/dist/handler-BcnsnvhF.d.cts +82 -0
- package/dist/handler-qHzilC9T.d.ts +82 -0
- package/dist/index.cjs +309 -0
- package/dist/index.d.cts +187 -0
- package/dist/index.d.ts +187 -0
- package/dist/index.js +4 -0
- package/dist/next/client.cjs +113 -0
- package/dist/next/client.d.cts +26 -0
- package/dist/next/client.d.ts +26 -0
- package/dist/next/client.js +41 -0
- package/dist/next/config.cjs +277 -0
- package/dist/next/config.d.cts +193 -0
- package/dist/next/config.d.ts +193 -0
- package/dist/next/config.js +263 -0
- package/dist/next/index.cjs +148 -0
- package/dist/next/index.d.cts +32 -0
- package/dist/next/index.d.ts +32 -0
- package/dist/next/index.js +59 -0
- package/dist/node/index.cjs +257 -0
- package/dist/node/index.d.cts +145 -0
- package/dist/node/index.d.ts +145 -0
- package/dist/node/index.js +83 -0
- package/dist/options-BUhWuWJX.d.cts +79 -0
- package/dist/options-BUhWuWJX.d.ts +79 -0
- package/dist/react/index.cjs +107 -0
- package/dist/react/index.d.cts +84 -0
- package/dist/react/index.d.ts +84 -0
- package/dist/react/index.js +103 -0
- package/dist/scope-D1ouJm2h.d.cts +628 -0
- package/dist/scope-D1ouJm2h.d.ts +628 -0
- package/dist/server-edge-7624N3WF.cjs +51 -0
- package/dist/server-edge-BEEXWL7T.js +48 -0
- package/dist/server-node-ET4LIPEC.cjs +27 -0
- package/dist/server-node-TQUVMZUC.js +24 -0
- package/dist/session-C_WzbEN2.d.cts +4 -0
- package/dist/session-C_WzbEN2.d.ts +4 -0
- package/dist/session-propagation-Pr0Zb9Zn.d.cts +30 -0
- package/dist/session-propagation-RM5py1bp.d.ts +30 -0
- package/dist/starko.min.js +6 -0
- package/docs/browser.md +386 -0
- package/docs/configuration.md +310 -0
- package/docs/nextjs.md +488 -0
- package/docs/node.md +427 -0
- package/docs/other-backends.md +232 -0
- package/docs/privacy.md +112 -0
- package/docs/react.md +187 -0
- package/docs/source-maps.md +482 -0
- package/docs/troubleshooting.md +263 -0
- package/docs/widget.md +84 -0
- package/docs/wire-format.md +547 -0
- package/package.json +191 -4
|
@@ -0,0 +1,1212 @@
|
|
|
1
|
+
import { capturedAs, unwrapThrown, markCaptured, buildExceptions, getInjectedRelease } from './chunk-335HNCOJ.js';
|
|
2
|
+
import { createLogger, utf8Length, unref, clamp, safeStringify, isLevel, num, truncate, normalize, safely, getGlobalScope, uuid4, getCurrentScope, safeString, SDK_VERSION, getEventScope, matchesAny, isThenable, warnAlways, isError, isEventLike, sanitizeBreadcrumb, normalizeToSize } from './chunk-WDKRKGYF.js';
|
|
3
|
+
import { SENSITIVE_QUERY_PARAMS, LIMITS, REDACTED, SESSION_ID_PATTERN, DEFAULT_HOST, WIRE_VERSION } from './chunk-3AW5B2WW.js';
|
|
4
|
+
|
|
5
|
+
// src/core/queue.ts
|
|
6
|
+
var OFF_REASONS = ["unknown_workspace", "disabled", "origin_not_allowed"];
|
|
7
|
+
var ENVELOPE_OVERHEAD = 1024;
|
|
8
|
+
var EventQueue = class {
|
|
9
|
+
constructor(opts) {
|
|
10
|
+
this.items = [];
|
|
11
|
+
/** The sequential send loop. */
|
|
12
|
+
this.running = null;
|
|
13
|
+
/** Every request in flight. */
|
|
14
|
+
this.sends = /* @__PURE__ */ new Set();
|
|
15
|
+
/** Batches in flight without keepalive (a page-hide flush sends them again). */
|
|
16
|
+
this.flying = /* @__PURE__ */ new Set();
|
|
17
|
+
/** 429: nothing goes out before this time. */
|
|
18
|
+
this.retryAt = 0;
|
|
19
|
+
/** 5xx / network: back-off; a hide-time flush ignores it (last chance). */
|
|
20
|
+
this.backoffUntil = 0;
|
|
21
|
+
/** Repeated failures: nothing goes out before this time. */
|
|
22
|
+
this.pausedUntil = 0;
|
|
23
|
+
/** Batches given up in a row. */
|
|
24
|
+
this.failures = 0;
|
|
25
|
+
this.disabled = false;
|
|
26
|
+
/** Batches given up on (dropped, out of attempts) since the queue started. */
|
|
27
|
+
this.dropped = 0;
|
|
28
|
+
this.o = {
|
|
29
|
+
transport: opts.transport,
|
|
30
|
+
envelope: opts.envelope,
|
|
31
|
+
maxQueueSize: 100,
|
|
32
|
+
flushInterval: 1e3,
|
|
33
|
+
maxAttempts: 3,
|
|
34
|
+
pauseAfter: 3,
|
|
35
|
+
failurePauseMs: 5 * 6e4,
|
|
36
|
+
backoffMs: 1e3,
|
|
37
|
+
maxBackoffMs: 6e4,
|
|
38
|
+
sendTimeoutMs: 3e4,
|
|
39
|
+
random: Math.random,
|
|
40
|
+
logger: createLogger()
|
|
41
|
+
};
|
|
42
|
+
for (const k of Object.keys(opts)) if (opts[k] !== void 0) this.o[k] = opts[k];
|
|
43
|
+
}
|
|
44
|
+
get size() {
|
|
45
|
+
return this.items.length;
|
|
46
|
+
}
|
|
47
|
+
/** A request is in flight. */
|
|
48
|
+
get isSending() {
|
|
49
|
+
return this.sends.size > 0;
|
|
50
|
+
}
|
|
51
|
+
get isDisabled() {
|
|
52
|
+
return this.disabled;
|
|
53
|
+
}
|
|
54
|
+
/** Waiting out a 429, a back-off or a failure pause: a flush now would send nothing. */
|
|
55
|
+
get isBlocked() {
|
|
56
|
+
return this.blockedFor(false) > 0;
|
|
57
|
+
}
|
|
58
|
+
/** Keep only the unsent events `keep` accepts (a remote config that arrived after they were queued). */
|
|
59
|
+
filter(keep) {
|
|
60
|
+
this.items = this.items.filter((i) => {
|
|
61
|
+
try {
|
|
62
|
+
return keep(i.event);
|
|
63
|
+
} catch {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/** Queue an event. One that cannot be serialised is dropped here, alone, instead of spoiling the batch it would travel in. */
|
|
69
|
+
add(event) {
|
|
70
|
+
if (this.disabled) return;
|
|
71
|
+
let json;
|
|
72
|
+
try {
|
|
73
|
+
json = JSON.stringify(event);
|
|
74
|
+
} catch {
|
|
75
|
+
}
|
|
76
|
+
if (typeof json !== "string") {
|
|
77
|
+
this.o.logger.warn("unserializable event dropped");
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const bytes = utf8Length(json);
|
|
81
|
+
if (bytes > LIMITS.MAX_BODY_BYTES - ENVELOPE_OVERHEAD) {
|
|
82
|
+
this.o.logger.warn("event too large", event.event_id);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
this.items.push({ event, bytes, attempts: 0, maxBatch: LIMITS.MAX_EVENTS_PER_ENVELOPE });
|
|
86
|
+
this.bound();
|
|
87
|
+
this.schedule(this.items.length >= LIMITS.MAX_EVENTS_PER_ENVELOPE ? 0 : this.o.flushInterval);
|
|
88
|
+
}
|
|
89
|
+
/** Drop everything and stop accepting events. */
|
|
90
|
+
disable() {
|
|
91
|
+
this.disabled = true;
|
|
92
|
+
this.items = [];
|
|
93
|
+
this.clearTimer();
|
|
94
|
+
}
|
|
95
|
+
/** Send nothing for `ms` (a shutdown gave up: no new connection attempt may keep the process alive). */
|
|
96
|
+
holdOff(ms) {
|
|
97
|
+
this.backoffUntil = Math.max(this.backoffUntil, Date.now() + ms);
|
|
98
|
+
this.clearTimer();
|
|
99
|
+
this.schedule(this.o.flushInterval);
|
|
100
|
+
}
|
|
101
|
+
/** End a failure pause or back-off now (the network is back). A 429 `Retry-After` still holds. */
|
|
102
|
+
resume() {
|
|
103
|
+
if (this.disabled) return;
|
|
104
|
+
this.pausedUntil = this.backoffUntil = this.failures = 0;
|
|
105
|
+
this.schedule(0);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Send everything now. Resolves true when the queue is empty and nothing
|
|
109
|
+
* is in flight within `timeoutMs`. With `keepalive` the requests are made
|
|
110
|
+
* for a page that is going away: all of them start before this returns
|
|
111
|
+
* (back-off is ignored, 429 and a failure pause are not).
|
|
112
|
+
*/
|
|
113
|
+
async flush(timeoutMs = 2e3, keepalive = false) {
|
|
114
|
+
this.clearTimer();
|
|
115
|
+
const droppedBefore = this.dropped;
|
|
116
|
+
if (keepalive) this.sendAll();
|
|
117
|
+
const work = (async () => {
|
|
118
|
+
for (let i = 0; i < 50; i++) {
|
|
119
|
+
while (this.sends.size) await Promise.all(this.sends);
|
|
120
|
+
if (!this.items.length || this.disabled || this.blockedFor(keepalive) > 0) break;
|
|
121
|
+
if (keepalive) this.sendAll();
|
|
122
|
+
else await this.drain();
|
|
123
|
+
}
|
|
124
|
+
return !this.items.length && !this.sends.size && this.dropped === droppedBefore;
|
|
125
|
+
})();
|
|
126
|
+
let t;
|
|
127
|
+
const timeout = new Promise((r) => {
|
|
128
|
+
t = setTimeout(() => r(false), Math.max(0, timeoutMs));
|
|
129
|
+
});
|
|
130
|
+
try {
|
|
131
|
+
return await Promise.race([work, timeout]);
|
|
132
|
+
} finally {
|
|
133
|
+
clearTimeout(t);
|
|
134
|
+
this.schedule(this.o.flushInterval);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
bound() {
|
|
138
|
+
const over = this.items.length - this.o.maxQueueSize;
|
|
139
|
+
if (over > 0) {
|
|
140
|
+
this.items.splice(0, over);
|
|
141
|
+
this.dropped++;
|
|
142
|
+
this.o.logger.alert("queue", `events dropped: queue full (maxQueueSize ${this.o.maxQueueSize})`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
blockedFor(keepalive) {
|
|
146
|
+
const now = Date.now();
|
|
147
|
+
return Math.max(0, this.retryAt - now, this.pausedUntil - now, keepalive ? 0 : this.backoffUntil - now);
|
|
148
|
+
}
|
|
149
|
+
clearTimer() {
|
|
150
|
+
clearTimeout(this.timer);
|
|
151
|
+
this.timer = void 0;
|
|
152
|
+
}
|
|
153
|
+
schedule(ms) {
|
|
154
|
+
if (this.disabled || !this.items.length) return;
|
|
155
|
+
const wait = Math.max(ms, this.blockedFor(false));
|
|
156
|
+
if (!(wait < 2 ** 31 - 1)) return;
|
|
157
|
+
if (this.timer !== void 0) {
|
|
158
|
+
if (wait > 0) return;
|
|
159
|
+
this.clearTimer();
|
|
160
|
+
}
|
|
161
|
+
this.timer = setTimeout(() => {
|
|
162
|
+
this.timer = void 0;
|
|
163
|
+
void this.drain();
|
|
164
|
+
}, wait);
|
|
165
|
+
unref(this.timer);
|
|
166
|
+
}
|
|
167
|
+
/** The sequential loop: one request at a time until the queue is empty or blocked. */
|
|
168
|
+
drain() {
|
|
169
|
+
if (this.running) return this.running;
|
|
170
|
+
this.running = (async () => {
|
|
171
|
+
if (this.o.gate) {
|
|
172
|
+
try {
|
|
173
|
+
await this.o.gate();
|
|
174
|
+
} catch {
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
while (this.items.length && !this.disabled && !this.blockedFor(false) && await this.send(this.take(), false)) ;
|
|
178
|
+
})().catch((err) => this.o.logger.warn("send failed", err)).then(() => {
|
|
179
|
+
this.running = null;
|
|
180
|
+
this.schedule(this.items.length >= LIMITS.MAX_EVENTS_PER_ENVELOPE ? 0 : this.o.flushInterval);
|
|
181
|
+
});
|
|
182
|
+
return this.running;
|
|
183
|
+
}
|
|
184
|
+
/** Page going away: every batch, and the events of requests in flight, leave now with keepalive. */
|
|
185
|
+
sendAll() {
|
|
186
|
+
const again = [];
|
|
187
|
+
for (const batch of this.flying) {
|
|
188
|
+
for (const item of batch) {
|
|
189
|
+
if (item.done || item.rescued || this.items.includes(item)) continue;
|
|
190
|
+
item.rescued = true;
|
|
191
|
+
again.push(item);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
this.items.unshift(...again);
|
|
195
|
+
while (this.items.length && !this.disabled && !this.blockedFor(true)) void this.send(this.take(), true);
|
|
196
|
+
}
|
|
197
|
+
take() {
|
|
198
|
+
const batch = [];
|
|
199
|
+
let bytes = ENVELOPE_OVERHEAD;
|
|
200
|
+
let limit = LIMITS.MAX_EVENTS_PER_ENVELOPE;
|
|
201
|
+
for (const item of this.items) {
|
|
202
|
+
limit = Math.min(limit, item.maxBatch);
|
|
203
|
+
if (batch.length >= limit) break;
|
|
204
|
+
if (batch.length && bytes + item.bytes + 1 > LIMITS.MAX_BODY_BYTES) break;
|
|
205
|
+
batch.push(item);
|
|
206
|
+
bytes += item.bytes + 1;
|
|
207
|
+
}
|
|
208
|
+
this.items.splice(0, batch.length);
|
|
209
|
+
return batch;
|
|
210
|
+
}
|
|
211
|
+
/** One request. The transport is called synchronously; resolves with `handle`'s verdict (false stops a loop). */
|
|
212
|
+
send(batch, keepalive) {
|
|
213
|
+
if (!keepalive) this.flying.add(batch);
|
|
214
|
+
let res;
|
|
215
|
+
try {
|
|
216
|
+
res = this.o.transport.send(this.o.envelope(batch.map((b) => b.event)), { keepalive });
|
|
217
|
+
} catch (err) {
|
|
218
|
+
this.o.logger.warn("send failed", err);
|
|
219
|
+
res = Promise.resolve({ status: 0 });
|
|
220
|
+
}
|
|
221
|
+
let t;
|
|
222
|
+
const p = Promise.race([
|
|
223
|
+
Promise.resolve(res).then(void 0, (err) => {
|
|
224
|
+
this.o.logger.warn("send failed", err);
|
|
225
|
+
return { status: 0 };
|
|
226
|
+
}),
|
|
227
|
+
new Promise((r) => {
|
|
228
|
+
t = setTimeout(() => r({ status: 0 }), this.o.sendTimeoutMs);
|
|
229
|
+
unref(t);
|
|
230
|
+
})
|
|
231
|
+
]).then((r) => {
|
|
232
|
+
clearTimeout(t);
|
|
233
|
+
this.flying.delete(batch);
|
|
234
|
+
this.sends.delete(p);
|
|
235
|
+
return this.handle(batch, r && typeof r.status === "number" ? r : { status: 0 });
|
|
236
|
+
}).catch(() => false);
|
|
237
|
+
this.sends.add(p);
|
|
238
|
+
return p;
|
|
239
|
+
}
|
|
240
|
+
/** Items of `batch` that may go again: not delivered, not already back in the queue. */
|
|
241
|
+
requeue(batch, keep) {
|
|
242
|
+
this.items.unshift(...batch.filter((b) => !b.done && !this.items.includes(b) && keep(b)));
|
|
243
|
+
this.bound();
|
|
244
|
+
}
|
|
245
|
+
/** Applies a response; false stops the current loop. */
|
|
246
|
+
handle(batch, res) {
|
|
247
|
+
const { status, reason } = res;
|
|
248
|
+
const log = this.o.logger;
|
|
249
|
+
const now = Date.now();
|
|
250
|
+
const retryAfter = typeof res.retryAfter === "number" && isFinite(res.retryAfter) && res.retryAfter >= 0 ? res.retryAfter * 1e3 : void 0;
|
|
251
|
+
if (status >= 200 && status < 300) {
|
|
252
|
+
this.failures = 0;
|
|
253
|
+
for (const b of batch) b.done = true;
|
|
254
|
+
this.items = this.items.filter((b) => !b.done);
|
|
255
|
+
log.log(`sent ${batch.length}`);
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
if ((status === 401 || status === 403) && OFF_REASONS.includes(reason)) {
|
|
259
|
+
log.warn(`${status} ${reason}: off`);
|
|
260
|
+
this.disable();
|
|
261
|
+
try {
|
|
262
|
+
if (this.o.onDisable) this.o.onDisable(status);
|
|
263
|
+
} catch {
|
|
264
|
+
}
|
|
265
|
+
return false;
|
|
266
|
+
}
|
|
267
|
+
if (status >= 300 && status < 400) {
|
|
268
|
+
this.dropped++;
|
|
269
|
+
const where = res.location ? ` to ${res.location}` : "";
|
|
270
|
+
log.alert("redirect", `host answered ${status}, a redirect${where}: not followed, events not delivered. Set \`host\` to the final address.`);
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
if (status === 413) {
|
|
274
|
+
if (batch.length > 1 && !batch.some((b) => b.halved)) {
|
|
275
|
+
const half = Math.ceil(batch.length / 2);
|
|
276
|
+
for (const b of batch) {
|
|
277
|
+
b.halved = true;
|
|
278
|
+
b.maxBatch = half;
|
|
279
|
+
}
|
|
280
|
+
this.requeue(batch, () => true);
|
|
281
|
+
log.warn("413: halving");
|
|
282
|
+
} else {
|
|
283
|
+
this.dropped++;
|
|
284
|
+
log.warn("413: dropped");
|
|
285
|
+
}
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
if (status === 429 && reason === "quota") {
|
|
289
|
+
this.retryAt = now + (retryAfter === void 0 ? 36e5 : retryAfter);
|
|
290
|
+
this.dropped++;
|
|
291
|
+
log.warn("429 quota: dropped");
|
|
292
|
+
this.schedule(0);
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
if ((status === 401 || status === 403) && reason === "invalid") {
|
|
296
|
+
this.dropped++;
|
|
297
|
+
log.alert("credentials", `${status}: secret key refused, events dropped. Check \`apiKey\`.`);
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
const retryable = status === 0 || status === 401 || status === 403 || status === 408 || status === 429 || status >= 500;
|
|
301
|
+
if (!retryable) {
|
|
302
|
+
this.dropped++;
|
|
303
|
+
log.warn(`${status}: dropped`);
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
let given = false;
|
|
307
|
+
let attempt = 1;
|
|
308
|
+
this.requeue(batch, (b) => {
|
|
309
|
+
if (++b.attempts < this.o.maxAttempts) return !!(attempt = b.attempts);
|
|
310
|
+
given = true;
|
|
311
|
+
return false;
|
|
312
|
+
});
|
|
313
|
+
if (given) this.dropped++;
|
|
314
|
+
if (given && ++this.failures >= this.o.pauseAfter) {
|
|
315
|
+
this.pausedUntil = now + this.o.failurePauseMs;
|
|
316
|
+
log.warn("repeated failures: paused");
|
|
317
|
+
}
|
|
318
|
+
const base = Math.min(this.o.maxBackoffMs, this.o.backoffMs * 2 ** (attempt - 1));
|
|
319
|
+
const wait = Math.round(base * (0.5 + this.o.random()));
|
|
320
|
+
if (retryAfter !== void 0 || status === 429) this.retryAt = now + Math.max(wait, retryAfter === void 0 ? 6e4 : retryAfter);
|
|
321
|
+
else this.backoffUntil = now + wait;
|
|
322
|
+
log.warn(`${status}: retry`);
|
|
323
|
+
this.schedule(0);
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// src/core/rate-limiter.ts
|
|
329
|
+
var KEY_MESSAGE_CHARS = 120;
|
|
330
|
+
function rateKey(type, message, topFrame) {
|
|
331
|
+
const t = typeof type === "string" && type ? type : "Error";
|
|
332
|
+
const m = typeof message === "string" ? message.slice(0, KEY_MESSAGE_CHARS) : "";
|
|
333
|
+
if (m) {
|
|
334
|
+
return `${t}: ${m.replace(/[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}|\b(?=[0-9a-f]*\d)[0-9a-f]{8,}\b/gi, "<id>").replace(/\d+/g, "<n>")}`;
|
|
335
|
+
}
|
|
336
|
+
return topFrame ? `${t} @ ${topFrame.filename}:${topFrame.lineno}` : t;
|
|
337
|
+
}
|
|
338
|
+
var MAX_BUCKETS = 200;
|
|
339
|
+
var MINUTE = 6e4;
|
|
340
|
+
var RateLimiter = class {
|
|
341
|
+
constructor(opts = {}) {
|
|
342
|
+
this.buckets = /* @__PURE__ */ new Map();
|
|
343
|
+
this.sent = 0;
|
|
344
|
+
/** Send times within the last minute (only kept when there is a per-minute budget). */
|
|
345
|
+
this.recent = [];
|
|
346
|
+
const pos = (v) => typeof v === "number" && !isNaN(v) ? Math.max(0, v) : Infinity;
|
|
347
|
+
this.burst = Math.max(1, opts.burst ?? 10);
|
|
348
|
+
this.refillMs = Math.max(1, opts.refillMs ?? 1e4);
|
|
349
|
+
this.lifetimeCap = pos(opts.lifetimeCap);
|
|
350
|
+
this.perMinute = pos(opts.perMinute);
|
|
351
|
+
}
|
|
352
|
+
/** `null` when the event may go; otherwise why it may not. Consumes a token when allowed. */
|
|
353
|
+
check(key, now = Date.now()) {
|
|
354
|
+
return this.test(key, now, true);
|
|
355
|
+
}
|
|
356
|
+
/** What `check` would answer, without consuming anything. */
|
|
357
|
+
peek(key, now = Date.now()) {
|
|
358
|
+
return this.test(key, now, false);
|
|
359
|
+
}
|
|
360
|
+
test(key, now, consume) {
|
|
361
|
+
if (this.sent >= this.lifetimeCap) return "lifetime cap";
|
|
362
|
+
if (this.perMinute !== Infinity) {
|
|
363
|
+
while (this.recent.length && this.recent[0] <= now - MINUTE) this.recent.shift();
|
|
364
|
+
if (this.recent.length >= this.perMinute) return "per-minute budget";
|
|
365
|
+
}
|
|
366
|
+
let b = this.buckets.get(key);
|
|
367
|
+
if (!b) {
|
|
368
|
+
if (this.buckets.size >= MAX_BUCKETS) this.buckets.delete(this.buckets.keys().next().value);
|
|
369
|
+
b = { tokens: this.burst, at: now };
|
|
370
|
+
this.buckets.set(key, b);
|
|
371
|
+
} else {
|
|
372
|
+
this.buckets.delete(key);
|
|
373
|
+
this.buckets.set(key, b);
|
|
374
|
+
const refills = Math.floor((now - b.at) / this.refillMs);
|
|
375
|
+
if (refills > 0) {
|
|
376
|
+
b.tokens = Math.min(this.burst, b.tokens + refills);
|
|
377
|
+
b.at += refills * this.refillMs;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
if (b.tokens <= 0) return "rate limited";
|
|
381
|
+
if (!consume) return null;
|
|
382
|
+
b.tokens--;
|
|
383
|
+
if (b.tokens === this.burst - 1) b.at = now;
|
|
384
|
+
this.sent++;
|
|
385
|
+
if (this.perMinute !== Infinity) this.recent.push(now);
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
get count() {
|
|
389
|
+
return this.sent;
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
// src/core/remote-config.ts
|
|
394
|
+
var DEFAULT_REMOTE_CONFIG = { enabled: true, sample_rate: 1, suppress: [] };
|
|
395
|
+
function normalizeRemoteConfig(raw) {
|
|
396
|
+
if (!raw || typeof raw !== "object") return { ...DEFAULT_REMOTE_CONFIG };
|
|
397
|
+
const r = raw;
|
|
398
|
+
const suppress = [];
|
|
399
|
+
if (Array.isArray(r.suppress)) {
|
|
400
|
+
for (const s of r.suppress.slice(0, 100)) {
|
|
401
|
+
if (!s || typeof s !== "object") continue;
|
|
402
|
+
const rule = {};
|
|
403
|
+
if (typeof s.type === "string" && s.type) rule.type = s.type;
|
|
404
|
+
if (typeof s.value_contains === "string" && s.value_contains) rule.value_contains = s.value_contains;
|
|
405
|
+
if (rule.type || rule.value_contains) suppress.push(rule);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return {
|
|
409
|
+
enabled: r.enabled !== false,
|
|
410
|
+
sample_rate: typeof r.sample_rate === "number" && isFinite(r.sample_rate) ? clamp(r.sample_rate, 0, 1) : 1,
|
|
411
|
+
suppress
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
function isSuppressed(event, rules) {
|
|
415
|
+
const ex = event.exception_list[0];
|
|
416
|
+
if (!ex) return false;
|
|
417
|
+
const value = (ex.value || "").toLowerCase();
|
|
418
|
+
return rules.some(
|
|
419
|
+
(r) => (r.type === void 0 || r.type === ex.type) && (r.value_contains === void 0 || value.includes(r.value_contains.toLowerCase()))
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// src/core/url.ts
|
|
424
|
+
var SENSITIVE = new Set(SENSITIVE_QUERY_PARAMS);
|
|
425
|
+
function isSensitive(rawName) {
|
|
426
|
+
let name = rawName;
|
|
427
|
+
try {
|
|
428
|
+
name = decodeURIComponent(rawName.replace(/\+/g, " "));
|
|
429
|
+
} catch {
|
|
430
|
+
}
|
|
431
|
+
if (SENSITIVE.has(name.toLowerCase())) return true;
|
|
432
|
+
return name.replace(/([a-z0-9])([A-Z])/g, "$1 $2").toLowerCase().split(/[\s_-]+/).some((part) => SENSITIVE.has(part));
|
|
433
|
+
}
|
|
434
|
+
function sanitizeUrl(url) {
|
|
435
|
+
if (typeof url !== "string") return url;
|
|
436
|
+
const hash = url.indexOf("#");
|
|
437
|
+
let s = hash >= 0 ? url.slice(0, hash) : url;
|
|
438
|
+
const auth = s.indexOf("//");
|
|
439
|
+
const q0 = s.indexOf("?");
|
|
440
|
+
if (auth >= 0 && (q0 < 0 || auth < q0) && (auth === 0 || s[auth - 1] === ":")) {
|
|
441
|
+
const start = auth + 2;
|
|
442
|
+
let end = start;
|
|
443
|
+
while (end < s.length && s[end] !== "/" && s[end] !== "?") end++;
|
|
444
|
+
const at = s.lastIndexOf("@", end - 1);
|
|
445
|
+
if (at >= start) s = s.slice(0, start) + s.slice(at + 1);
|
|
446
|
+
}
|
|
447
|
+
const q = s.indexOf("?");
|
|
448
|
+
if (q < 0) return s;
|
|
449
|
+
const query = s.slice(q + 1).split("&").map((pair) => {
|
|
450
|
+
const eq = pair.indexOf("=");
|
|
451
|
+
return eq > 0 && isSensitive(pair.slice(0, eq)) ? `${pair.slice(0, eq)}=${REDACTED}` : pair;
|
|
452
|
+
}).join("&");
|
|
453
|
+
return `${s.slice(0, q)}?${query}`;
|
|
454
|
+
}
|
|
455
|
+
var URL_END = " \n\r\f\v\"'`<>()[]{}|\\^";
|
|
456
|
+
function sanitizeUrlsInText(text) {
|
|
457
|
+
if (typeof text !== "string" || text.indexOf("://") < 0) return text;
|
|
458
|
+
const lower = text.toLowerCase();
|
|
459
|
+
let out = "";
|
|
460
|
+
let from = 0;
|
|
461
|
+
let i = 0;
|
|
462
|
+
for (; ; ) {
|
|
463
|
+
const p = lower.indexOf("http", i);
|
|
464
|
+
if (p < 0) break;
|
|
465
|
+
const len = lower.startsWith("://", p + 4) ? 7 : lower.startsWith("s://", p + 4) ? 8 : 0;
|
|
466
|
+
if (!len) {
|
|
467
|
+
i = p + 4;
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
let end = p + len;
|
|
471
|
+
while (end < text.length && URL_END.indexOf(text[end]) < 0) end++;
|
|
472
|
+
out += text.slice(from, p) + sanitizeUrl(text.slice(p, end));
|
|
473
|
+
from = i = end;
|
|
474
|
+
}
|
|
475
|
+
return out + text.slice(from);
|
|
476
|
+
}
|
|
477
|
+
function sanitizeFileName(name) {
|
|
478
|
+
return /^[a-z][a-z0-9+.-]*:\/\//i.test(name) ? sanitizeUrl(name) : name;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// src/core/trim.ts
|
|
482
|
+
var MAX_EVENT_BYTES = 200 * 1024;
|
|
483
|
+
var str = (v, max) => v == null ? void 0 : truncate(safeString(v), max);
|
|
484
|
+
var posInt = (v) => typeof v === "number" && isFinite(v) && v > 0 ? Math.floor(v) : void 0;
|
|
485
|
+
var isObj = (v) => !!v && typeof v === "object";
|
|
486
|
+
function copyStrings(to, from, fields) {
|
|
487
|
+
for (const [k, max] of fields) if (from[k] != null) to[k] = str(from[k], max);
|
|
488
|
+
return to;
|
|
489
|
+
}
|
|
490
|
+
var FRAME_STRINGS = [
|
|
491
|
+
["lang", 32],
|
|
492
|
+
["filename", LIMITS.MAX_FILENAME],
|
|
493
|
+
["abs_path", LIMITS.MAX_FILENAME],
|
|
494
|
+
["function", LIMITS.MAX_FUNCTION],
|
|
495
|
+
["module", LIMITS.MAX_FUNCTION],
|
|
496
|
+
["chunk_id", 128],
|
|
497
|
+
["context_line", LIMITS.MAX_CONTEXT_LINE]
|
|
498
|
+
];
|
|
499
|
+
var lines = (a, from, to) => a.slice(from, to).map((l) => str(l, LIMITS.MAX_CONTEXT_LINE) || "");
|
|
500
|
+
function trimFrame(f) {
|
|
501
|
+
const out = copyStrings({ platform: str(f.platform, 32) }, f, FRAME_STRINGS);
|
|
502
|
+
if (out.filename) out.filename = sanitizeFileName(out.filename);
|
|
503
|
+
if (out.abs_path) out.abs_path = sanitizeFileName(out.abs_path);
|
|
504
|
+
const lineno = posInt(f.lineno);
|
|
505
|
+
const colno = posInt(f.colno);
|
|
506
|
+
if (lineno) out.lineno = lineno;
|
|
507
|
+
if (colno) out.colno = colno;
|
|
508
|
+
if (typeof f.in_app === "boolean") out.in_app = f.in_app;
|
|
509
|
+
if (Array.isArray(f.pre_context)) out.pre_context = lines(f.pre_context, -LIMITS.MAX_CONTEXT_LINES);
|
|
510
|
+
if (Array.isArray(f.post_context)) out.post_context = lines(f.post_context, 0, LIMITS.MAX_CONTEXT_LINES);
|
|
511
|
+
if (typeof f.resolved === "boolean") out.resolved = f.resolved;
|
|
512
|
+
return out;
|
|
513
|
+
}
|
|
514
|
+
function trimMechanism(m) {
|
|
515
|
+
const out = { type: str(m.type, 64) || "generic", handled: m.handled !== false };
|
|
516
|
+
if (typeof m.synthetic === "boolean") out.synthetic = m.synthetic;
|
|
517
|
+
if (m.source != null) out.source = str(m.source, 64);
|
|
518
|
+
for (const k of ["exception_id", "parent_id"]) {
|
|
519
|
+
const n = m[k];
|
|
520
|
+
if (typeof n === "number" && n >= 0 && n < 1e3) out[k] = Math.floor(n);
|
|
521
|
+
}
|
|
522
|
+
return out;
|
|
523
|
+
}
|
|
524
|
+
function trimException(e, maxFrames) {
|
|
525
|
+
const out = { type: str(e.type, LIMITS.MAX_TYPE) || "Error", value: sanitizeUrlsInText(str(e.value, LIMITS.MAX_VALUE) || "") };
|
|
526
|
+
if (e.module != null) out.module = str(e.module, LIMITS.MAX_TYPE);
|
|
527
|
+
if (isObj(e.mechanism)) out.mechanism = trimMechanism(e.mechanism);
|
|
528
|
+
const frames = e.stacktrace && e.stacktrace.frames;
|
|
529
|
+
if (Array.isArray(frames) && frames.length) {
|
|
530
|
+
const half = maxFrames >> 1;
|
|
531
|
+
const kept = frames.length > maxFrames ? [...frames.slice(0, half), ...frames.slice(-(maxFrames - half))] : frames;
|
|
532
|
+
out.stacktrace = { type: "raw", frames: kept.filter(isObj).map(trimFrame) };
|
|
533
|
+
}
|
|
534
|
+
return out;
|
|
535
|
+
}
|
|
536
|
+
var URL_KEYS = ["url", "from", "to"];
|
|
537
|
+
function trimCrumb(c) {
|
|
538
|
+
const out = sanitizeBreadcrumb(c);
|
|
539
|
+
if (out.message) out.message = sanitizeUrlsInText(out.message);
|
|
540
|
+
const d = out.data;
|
|
541
|
+
if (d) {
|
|
542
|
+
for (const k of URL_KEYS) if (typeof d[k] === "string") d[k] = sanitizeUrl(d[k]);
|
|
543
|
+
}
|
|
544
|
+
return out;
|
|
545
|
+
}
|
|
546
|
+
function trimBreadcrumbs(crumbs) {
|
|
547
|
+
let list = [];
|
|
548
|
+
for (const c of crumbs.slice(-LIMITS.MAX_BREADCRUMBS)) {
|
|
549
|
+
try {
|
|
550
|
+
if (isObj(c)) list.push(trimCrumb(c));
|
|
551
|
+
} catch {
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
let sizes = list.map((c) => utf8Length(safeStringify(c)) + 1);
|
|
555
|
+
let total = sizes.reduce((a, b) => a + b, 2);
|
|
556
|
+
while (list.length && total > LIMITS.MAX_BREADCRUMB_BYTES) {
|
|
557
|
+
total -= sizes[0];
|
|
558
|
+
list = list.slice(1);
|
|
559
|
+
sizes = sizes.slice(1);
|
|
560
|
+
}
|
|
561
|
+
return list;
|
|
562
|
+
}
|
|
563
|
+
function record(v, maxBytes) {
|
|
564
|
+
if (!isObj(v)) return void 0;
|
|
565
|
+
const n = normalizeToSize(v, maxBytes);
|
|
566
|
+
return isObj(n) && !Array.isArray(n) && Object.keys(n).length ? n : void 0;
|
|
567
|
+
}
|
|
568
|
+
function trimEvent(input) {
|
|
569
|
+
const list = Array.isArray(input.exception_list) ? input.exception_list.filter(isObj).slice(0, LIMITS.MAX_EXCEPTIONS) : [];
|
|
570
|
+
const e = {
|
|
571
|
+
event_id: str(input.event_id, 64) || "",
|
|
572
|
+
timestamp: num(input.timestamp, Date.now()),
|
|
573
|
+
platform: str(input.platform, 32),
|
|
574
|
+
level: isLevel(input.level) ? input.level : "error",
|
|
575
|
+
exception_list: list.map((x) => trimException(x, LIMITS.MAX_FRAMES))
|
|
576
|
+
};
|
|
577
|
+
if (!e.exception_list.length) e.exception_list = [{ type: "Error", value: "" }];
|
|
578
|
+
copyStrings(e, input, [
|
|
579
|
+
["fingerprint", LIMITS.MAX_FINGERPRINT],
|
|
580
|
+
["release", LIMITS.MAX_RELEASE],
|
|
581
|
+
["environment", LIMITS.MAX_ENVIRONMENT],
|
|
582
|
+
["client_token", 2048],
|
|
583
|
+
["user_agent", 512],
|
|
584
|
+
["server_name", 256]
|
|
585
|
+
]);
|
|
586
|
+
if (typeof input.session_id === "string" && SESSION_ID_PATTERN.test(input.session_id)) e.session_id = input.session_id;
|
|
587
|
+
const sub = (k, fields) => {
|
|
588
|
+
const v = input[k];
|
|
589
|
+
if (!isObj(v)) return;
|
|
590
|
+
const out = copyStrings({}, v, fields);
|
|
591
|
+
if (Object.keys(out).length) e[k] = out;
|
|
592
|
+
};
|
|
593
|
+
sub("user", [["id", 256], ["email", 256], ["name", 256], ["hash", 256]]);
|
|
594
|
+
sub("request", [["url", LIMITS.MAX_URL], ["method", 16], ["route", LIMITS.MAX_URL], ["referrer", LIMITS.MAX_URL]]);
|
|
595
|
+
sub("trace", [["trace_id", 64], ["span_id", 64]]);
|
|
596
|
+
if (e.request) {
|
|
597
|
+
if (e.request.url) e.request.url = truncate(sanitizeUrl(e.request.url), LIMITS.MAX_URL);
|
|
598
|
+
if (e.request.referrer) e.request.referrer = truncate(sanitizeUrl(e.request.referrer), LIMITS.MAX_URL);
|
|
599
|
+
}
|
|
600
|
+
const tags = isObj(input.tags) ? normalize(input.tags, 1) : void 0;
|
|
601
|
+
if (isObj(tags)) {
|
|
602
|
+
const t = {};
|
|
603
|
+
let n = 0;
|
|
604
|
+
for (const k of Object.keys(tags)) {
|
|
605
|
+
if (n++ >= LIMITS.MAX_TAGS) break;
|
|
606
|
+
t[truncate(k, LIMITS.MAX_TAG_KEY)] = str(tags[k], LIMITS.MAX_TAG_VALUE) || "";
|
|
607
|
+
}
|
|
608
|
+
if (n) e.tags = t;
|
|
609
|
+
}
|
|
610
|
+
const extra = record(input.extra, LIMITS.MAX_EXTRA_BYTES);
|
|
611
|
+
if (extra) e.extra = extra;
|
|
612
|
+
const c = record(input.contexts, LIMITS.MAX_EXTRA_BYTES);
|
|
613
|
+
if (c) {
|
|
614
|
+
const ctx = {};
|
|
615
|
+
for (const k of Object.keys(c)) {
|
|
616
|
+
const v = c[k];
|
|
617
|
+
if (isObj(v) && !Array.isArray(v)) ctx[k] = v;
|
|
618
|
+
}
|
|
619
|
+
if (Object.keys(ctx).length) e.contexts = ctx;
|
|
620
|
+
}
|
|
621
|
+
if (Array.isArray(input.breadcrumbs) && input.breadcrumbs.length) {
|
|
622
|
+
const crumbs = trimBreadcrumbs(input.breadcrumbs);
|
|
623
|
+
if (crumbs.length) e.breadcrumbs = crumbs;
|
|
624
|
+
}
|
|
625
|
+
return fitEvent(e);
|
|
626
|
+
}
|
|
627
|
+
function fitEvent(e) {
|
|
628
|
+
const size = () => utf8Length(safeStringify(e));
|
|
629
|
+
if (size() <= MAX_EVENT_BYTES) return e;
|
|
630
|
+
for (const x of e.exception_list) for (const f of x.stacktrace ? x.stacktrace.frames : []) {
|
|
631
|
+
delete f.pre_context;
|
|
632
|
+
delete f.post_context;
|
|
633
|
+
}
|
|
634
|
+
if (size() <= MAX_EVENT_BYTES) return e;
|
|
635
|
+
for (const x of e.exception_list) if (x.stacktrace) x.stacktrace.frames = x.stacktrace.frames.slice(0, 20);
|
|
636
|
+
if (size() <= MAX_EVENT_BYTES) return e;
|
|
637
|
+
delete e.extra;
|
|
638
|
+
delete e.contexts;
|
|
639
|
+
delete e.breadcrumbs;
|
|
640
|
+
if (size() <= MAX_EVENT_BYTES) return e;
|
|
641
|
+
e.exception_list = e.exception_list.slice(0, 1);
|
|
642
|
+
return e;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// src/core/client.ts
|
|
646
|
+
var MAX_PENDING = 30;
|
|
647
|
+
var REMOTE_CONFIG_TIMEOUT_MS = 1e4;
|
|
648
|
+
var MAX_INFLIGHT = 1e3;
|
|
649
|
+
var TIMED_OUT = {};
|
|
650
|
+
var LIMIT_MESSAGES = {
|
|
651
|
+
"rate limited": "rate limit per error (10, then 1 per 10 s)",
|
|
652
|
+
"per-minute budget": "maxEventsPerMinute",
|
|
653
|
+
"lifetime cap": "maxEventsPerLifetime"
|
|
654
|
+
};
|
|
655
|
+
var DROP_LOG_WINDOW_MS = 6e4;
|
|
656
|
+
var NON_URL_FILES = /^(?:<anonymous>|\[native code\]|native)$/;
|
|
657
|
+
var Client = class {
|
|
658
|
+
constructor(options) {
|
|
659
|
+
this.integrations = [];
|
|
660
|
+
this.state = "idle";
|
|
661
|
+
this.remote = null;
|
|
662
|
+
this.remoteReady = Promise.resolve();
|
|
663
|
+
this.pending = [];
|
|
664
|
+
/** Async `beforeSend` results not settled yet: `flush()` waits for them. */
|
|
665
|
+
this.inflight = /* @__PURE__ */ new Set();
|
|
666
|
+
/** Rate-limit drops per limit, for the log line (see `limitDrop`). */
|
|
667
|
+
this.drops = {};
|
|
668
|
+
const o = { ...options, host: (options.host || DEFAULT_HOST).replace(/\/+$/, "") };
|
|
669
|
+
this.options = o;
|
|
670
|
+
this.logger = createLogger(!!o.debug);
|
|
671
|
+
const limit = (v, fallback) => typeof v === "number" && v >= 0 ? v : fallback;
|
|
672
|
+
this.limiter = new RateLimiter({
|
|
673
|
+
lifetimeCap: limit(o.maxEventsPerLifetime, limit(o.defaultMaxEventsPerLifetime, 100)),
|
|
674
|
+
perMinute: limit(o.maxEventsPerMinute, o.defaultMaxEventsPerMinute)
|
|
675
|
+
});
|
|
676
|
+
this.transport = o.transport({ host: o.host, workspaceKey: o.workspaceKey, timeoutMs: o.requestTimeoutMs });
|
|
677
|
+
this.queue = new EventQueue({
|
|
678
|
+
transport: this.transport,
|
|
679
|
+
envelope: (events) => this.envelope(events),
|
|
680
|
+
logger: this.logger,
|
|
681
|
+
maxQueueSize: num(o.maxQueueSize, num(o.defaultMaxQueueSize, 100)),
|
|
682
|
+
flushInterval: num(o.flushInterval, 1e3),
|
|
683
|
+
failurePauseMs: o.failurePauseMs,
|
|
684
|
+
onDisable: () => this.shutdown("key refused"),
|
|
685
|
+
gate: this.configGate()
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
/** Starts the client: integrations, remote config. Called once by the platform `init()`. */
|
|
689
|
+
init(integrations) {
|
|
690
|
+
safely(
|
|
691
|
+
this.logger,
|
|
692
|
+
"init",
|
|
693
|
+
() => {
|
|
694
|
+
if (this.state !== "idle") return;
|
|
695
|
+
if (!this.options.workspaceKey || typeof this.options.workspaceKey !== "string") {
|
|
696
|
+
this.logger.warn("no workspaceKey");
|
|
697
|
+
this.state = "disabled";
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
if (this.options.enabled === false) {
|
|
701
|
+
this.state = "disabled";
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
this.state = "active";
|
|
705
|
+
if (this.options.maxBreadcrumbs !== void 0) getGlobalScope().breadcrumbs.setMaxItems(num(this.options.maxBreadcrumbs, 50));
|
|
706
|
+
if (this.options.user) getGlobalScope().setUser({ ...this.options.user, ...this.options.userHash ? { hash: this.options.userHash } : {} });
|
|
707
|
+
this.loadRemoteConfig();
|
|
708
|
+
const byName = /* @__PURE__ */ new Map();
|
|
709
|
+
for (const i of integrations) if (i && i.name) byName.set(i.name, i);
|
|
710
|
+
for (const i of byName.values()) {
|
|
711
|
+
try {
|
|
712
|
+
if (i.setup) i.setup(this);
|
|
713
|
+
this.integrations.push(i);
|
|
714
|
+
} catch (err) {
|
|
715
|
+
this.logger.warn(i.name, err);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
this.logger.log("started");
|
|
719
|
+
},
|
|
720
|
+
void 0
|
|
721
|
+
);
|
|
722
|
+
return this;
|
|
723
|
+
}
|
|
724
|
+
isEnabled() {
|
|
725
|
+
return this.state === "active" && !this.queue.isDisabled;
|
|
726
|
+
}
|
|
727
|
+
getIntegration(name) {
|
|
728
|
+
return this.integrations.find((i) => i.name === name);
|
|
729
|
+
}
|
|
730
|
+
/** The session id stamped on events (browser: the widget recorder's, or minted). */
|
|
731
|
+
getSessionId() {
|
|
732
|
+
return safely(this.logger, "session", () => {
|
|
733
|
+
const f = this.options.getSessionId;
|
|
734
|
+
return f && f();
|
|
735
|
+
}, void 0);
|
|
736
|
+
}
|
|
737
|
+
/** The remote config once loaded (`null` while loading). */
|
|
738
|
+
getRemoteConfig() {
|
|
739
|
+
return this.remote;
|
|
740
|
+
}
|
|
741
|
+
/** Capture a thrown value. Returns the event id (the earlier id when this error was already captured). */
|
|
742
|
+
captureException(exception, hint, scope) {
|
|
743
|
+
const syn = needsSyntheticStack(exception) ? new Error() : void 0;
|
|
744
|
+
return this.capture(exception, hint, scope, syn, false);
|
|
745
|
+
}
|
|
746
|
+
/** Capture a message (an exception of type "Message", with the caller's stack). */
|
|
747
|
+
captureMessage(message, level, hint, scope) {
|
|
748
|
+
const syn = new Error();
|
|
749
|
+
return this.capture(message, hint, scope, syn, true, level);
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Send a ready-made event (other platforms, custom producers). Missing
|
|
753
|
+
* `event_id` / `timestamp` / `platform` / `level` are filled in.
|
|
754
|
+
*/
|
|
755
|
+
captureEvent(event, hint, scope) {
|
|
756
|
+
return safely(
|
|
757
|
+
this.logger,
|
|
758
|
+
"capture",
|
|
759
|
+
() => {
|
|
760
|
+
const id = event && typeof event.event_id === "string" && event.event_id || uuid4();
|
|
761
|
+
if (!this.isEnabled() || !event || typeof event !== "object") return id;
|
|
762
|
+
const e = { ...this.baseEvent(id, isLevel(event.level) ? event.level : "error"), ...event, event_id: id };
|
|
763
|
+
if (!Array.isArray(e.exception_list)) e.exception_list = [];
|
|
764
|
+
this.process(e, toHint(hint), scope);
|
|
765
|
+
return id;
|
|
766
|
+
},
|
|
767
|
+
""
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
addBreadcrumb(breadcrumb, hint, scope) {
|
|
771
|
+
safely(
|
|
772
|
+
this.logger,
|
|
773
|
+
"breadcrumb",
|
|
774
|
+
() => {
|
|
775
|
+
if (this.state === "closed" || this.state === "disabled") return;
|
|
776
|
+
if (!breadcrumb || typeof breadcrumb !== "object") return;
|
|
777
|
+
let crumb = { ...breadcrumb, timestamp: breadcrumb.timestamp || Date.now() };
|
|
778
|
+
if (this.options.beforeBreadcrumb) crumb = this.options.beforeBreadcrumb(crumb, hint);
|
|
779
|
+
if (crumb) (scope || getCurrentScope()).addBreadcrumb(crumb);
|
|
780
|
+
},
|
|
781
|
+
void 0
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
/**
|
|
785
|
+
* True while an event is on its way: waiting for the remote config, in an
|
|
786
|
+
* async `beforeSend`, or queued. Lets a server skip a flush that has
|
|
787
|
+
* nothing to send.
|
|
788
|
+
*/
|
|
789
|
+
hasPending() {
|
|
790
|
+
return this.pending.length > 0 || this.inflight.size > 0 || this.queue.size > 0 || this.queue.isSending;
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Send what is queued. True when everything went out within `timeoutMs`.
|
|
794
|
+
* With `keepalive` (the page is going away) the requests start before this
|
|
795
|
+
* returns, in the caller's turn: events held for the remote config are
|
|
796
|
+
* released as if the config were the default, and nothing waits for a
|
|
797
|
+
* request already in flight.
|
|
798
|
+
*/
|
|
799
|
+
async flush(timeoutMs, keepalive = false) {
|
|
800
|
+
try {
|
|
801
|
+
const ms = Math.max(0, num(timeoutMs, 2e3));
|
|
802
|
+
const start = Date.now();
|
|
803
|
+
if (keepalive) {
|
|
804
|
+
this.release();
|
|
805
|
+
void this.queue.flush(ms, true);
|
|
806
|
+
}
|
|
807
|
+
let t;
|
|
808
|
+
const deadline = new Promise((r) => t = setTimeout(r, ms));
|
|
809
|
+
try {
|
|
810
|
+
if (!this.remote && this.state === "active" && !keepalive) await Promise.race([this.remoteReady, deadline]);
|
|
811
|
+
if (this.inflight.size) await Promise.race([Promise.all(this.inflight), deadline]);
|
|
812
|
+
} finally {
|
|
813
|
+
clearTimeout(t);
|
|
814
|
+
}
|
|
815
|
+
return await this.queue.flush(Math.max(0, ms - (Date.now() - start)), keepalive);
|
|
816
|
+
} catch (err) {
|
|
817
|
+
this.logger.warn("flush failed", err);
|
|
818
|
+
return false;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
/** End a pause after repeated send failures (the browser calls it when the network is back). */
|
|
822
|
+
resume() {
|
|
823
|
+
safely(this.logger, "resume", () => this.queue.resume(), void 0);
|
|
824
|
+
}
|
|
825
|
+
/** Flush, then remove every hook. The client sends nothing afterwards. */
|
|
826
|
+
async close(timeoutMs) {
|
|
827
|
+
try {
|
|
828
|
+
this.teardown();
|
|
829
|
+
const ok = this.state === "active" ? await this.flush(timeoutMs) : true;
|
|
830
|
+
this.state = "closed";
|
|
831
|
+
this.queue.disable();
|
|
832
|
+
return ok;
|
|
833
|
+
} catch (err) {
|
|
834
|
+
this.logger.warn("close failed", err);
|
|
835
|
+
return false;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
// --- internals ------------------------------------------------------------
|
|
839
|
+
/** The guarded body of `captureException` / `captureMessage`; `syn` was made in the public method (skip 1 drops its frame). */
|
|
840
|
+
capture(value, hint, scope, syn, message, level) {
|
|
841
|
+
try {
|
|
842
|
+
const h = toHint(hint);
|
|
843
|
+
const own = h.syntheticException;
|
|
844
|
+
if (!message) {
|
|
845
|
+
const prev = capturedAs(value) || capturedAs(unwrapThrown(value));
|
|
846
|
+
if (prev) return prev;
|
|
847
|
+
}
|
|
848
|
+
const id = uuid4();
|
|
849
|
+
if (!this.isEnabled()) return id;
|
|
850
|
+
if (!message) markCaptured(value, id);
|
|
851
|
+
const built = buildExceptions(message ? safeString(value) : value, { type: message ? "message" : "generic", handled: true, ...h.mechanism }, this.buildContext(own || syn, own ? num(h.syntheticSkip, 0) : 1));
|
|
852
|
+
const event = this.baseEvent(id, message ? isLevel(level) ? level : "info" : isLevel(h.level) ? h.level : "error");
|
|
853
|
+
event.exception_list = built.exceptions;
|
|
854
|
+
if (message) built.exceptions[0].type = "Message";
|
|
855
|
+
else if (built.serialized !== void 0) event.extra = { __serialized__: built.serialized };
|
|
856
|
+
this.process(event, { ...h, originalException: value }, scope);
|
|
857
|
+
return id;
|
|
858
|
+
} catch (err) {
|
|
859
|
+
this.logger.warn("capture failed", err);
|
|
860
|
+
return "";
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
buildContext(syn, skip) {
|
|
864
|
+
const o = this.options;
|
|
865
|
+
let stack;
|
|
866
|
+
try {
|
|
867
|
+
stack = syn && typeof syn.stack === "string" ? syn.stack : void 0;
|
|
868
|
+
} catch {
|
|
869
|
+
}
|
|
870
|
+
return { parser: o.stackParser, framePlatform: o.framePlatform, isInApp: o.isInApp, synthetic: stack ? { stack, skip } : void 0 };
|
|
871
|
+
}
|
|
872
|
+
baseEvent(id, level) {
|
|
873
|
+
return { event_id: id, timestamp: Date.now(), platform: this.options.platform, level, exception_list: [] };
|
|
874
|
+
}
|
|
875
|
+
envelope(events) {
|
|
876
|
+
return {
|
|
877
|
+
v: WIRE_VERSION,
|
|
878
|
+
workspace: this.options.workspaceKey,
|
|
879
|
+
sdk: { name: this.options.sdkName, version: this.options.sdkVersion || SDK_VERSION },
|
|
880
|
+
sent_at: Date.now(),
|
|
881
|
+
events
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
/** Steps up to the URL filters (synchronous), then the rest. */
|
|
885
|
+
process(event, hint, scope) {
|
|
886
|
+
const o = this.options;
|
|
887
|
+
if (!event.release) {
|
|
888
|
+
const release = o.release || getInjectedRelease();
|
|
889
|
+
if (release) event.release = release;
|
|
890
|
+
}
|
|
891
|
+
if (o.environment && !event.environment) event.environment = o.environment;
|
|
892
|
+
if (!event.session_id) {
|
|
893
|
+
const sid = this.getSessionId();
|
|
894
|
+
if (sid) event.session_id = sid;
|
|
895
|
+
}
|
|
896
|
+
const current = scope || getEventScope();
|
|
897
|
+
current.applyToEvent(event);
|
|
898
|
+
if (hint.tags) event.tags = { ...event.tags, ...hint.tags };
|
|
899
|
+
if (hint.extra) event.extra = { ...event.extra, ...hint.extra };
|
|
900
|
+
if (hint.contexts) event.contexts = { ...event.contexts, ...hint.contexts };
|
|
901
|
+
if (hint.fingerprint) event.fingerprint = safeString(hint.fingerprint);
|
|
902
|
+
let e = event;
|
|
903
|
+
for (const i of this.integrations) {
|
|
904
|
+
if (!i.processEvent) continue;
|
|
905
|
+
try {
|
|
906
|
+
e = i.processEvent(e, hint, this);
|
|
907
|
+
} catch (err) {
|
|
908
|
+
this.logger.warn(i.name, err);
|
|
909
|
+
}
|
|
910
|
+
if (!e) {
|
|
911
|
+
this.logger.log("dropped", i.name);
|
|
912
|
+
return;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
const why = this.localFilter(e);
|
|
916
|
+
if (why) {
|
|
917
|
+
this.logger.log("dropped", why);
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
if (!this.remote) {
|
|
921
|
+
if (this.pending.length < MAX_PENDING) this.pending.push([e, hint]);
|
|
922
|
+
else this.logger.alert("pending", `events dropped: over ${MAX_PENDING} before the remote config`);
|
|
923
|
+
return;
|
|
924
|
+
}
|
|
925
|
+
this.finish(e, hint);
|
|
926
|
+
}
|
|
927
|
+
localFilter(e) {
|
|
928
|
+
const o = this.options;
|
|
929
|
+
const ex = e.exception_list[0];
|
|
930
|
+
const ignore = o.ignoreErrors || [];
|
|
931
|
+
const allow = o.allowUrls || [];
|
|
932
|
+
const deny = o.denyUrls || [];
|
|
933
|
+
if (ignore.length && ex) {
|
|
934
|
+
const msg = ex.value || "";
|
|
935
|
+
if (matchesAny(`${ex.type}: ${msg}`, ignore) || matchesAny(msg, ignore)) return "ignoreErrors";
|
|
936
|
+
}
|
|
937
|
+
if (allow.length || deny.length) {
|
|
938
|
+
const url = eventUrl(e);
|
|
939
|
+
if (url && matchesAny(url, deny)) return "denyUrls";
|
|
940
|
+
if (url && allow.length && !matchesAny(url, allow)) return "allowUrls";
|
|
941
|
+
}
|
|
942
|
+
return null;
|
|
943
|
+
}
|
|
944
|
+
/** Remote suppression → sample rate → rate limiter → beforeSend → trim → queue. */
|
|
945
|
+
finish(event, hint) {
|
|
946
|
+
const rc = this.remote || DEFAULT_REMOTE_CONFIG;
|
|
947
|
+
if (!this.isEnabled() || !rc.enabled) return;
|
|
948
|
+
if (isSuppressed(event, rc.suppress)) return this.logger.log("dropped", "suppress");
|
|
949
|
+
const rate = Math.min(1, Math.max(0, num(this.options.sampleRate, 1))) * rc.sample_rate;
|
|
950
|
+
if (rate < 1 && Math.random() >= rate) return this.logger.log("dropped", "sample");
|
|
951
|
+
const first = event.exception_list[0];
|
|
952
|
+
const key = rateKey(first && first.type, first && first.value, first && first.stacktrace ? first.stacktrace.frames[0] : void 0);
|
|
953
|
+
const limited = this.limiter.peek(key);
|
|
954
|
+
if (limited) return this.limitDrop(limited, key);
|
|
955
|
+
const enqueue = (e) => {
|
|
956
|
+
if (!e || typeof e !== "object" || !Array.isArray(e.exception_list)) return this.logger.log("dropped", "beforeSend");
|
|
957
|
+
if (!this.isEnabled()) return;
|
|
958
|
+
const why = this.limiter.check(key);
|
|
959
|
+
if (why) return this.limitDrop(why, key);
|
|
960
|
+
this.queue.add(trimEvent(e));
|
|
961
|
+
};
|
|
962
|
+
const before = this.options.beforeSend;
|
|
963
|
+
if (!before) return enqueue(event);
|
|
964
|
+
let out;
|
|
965
|
+
try {
|
|
966
|
+
out = before(event, hint);
|
|
967
|
+
} catch (err) {
|
|
968
|
+
this.logger.warn("beforeSend", err);
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
if (isThenable(out)) {
|
|
972
|
+
if (this.inflight.size >= MAX_INFLIGHT) {
|
|
973
|
+
this.logger.alert("inflight", `events dropped: ${MAX_INFLIGHT} async beforeSend calls pending`);
|
|
974
|
+
return;
|
|
975
|
+
}
|
|
976
|
+
let timer;
|
|
977
|
+
const ms = Math.max(0, num(this.options.beforeSendTimeoutMs, 5e3));
|
|
978
|
+
const timeout = new Promise((r) => {
|
|
979
|
+
timer = setTimeout(() => r(TIMED_OUT), ms);
|
|
980
|
+
unref(timer);
|
|
981
|
+
});
|
|
982
|
+
const p = Promise.race([out, timeout]).then(
|
|
983
|
+
(r) => {
|
|
984
|
+
clearTimeout(timer);
|
|
985
|
+
if (r === TIMED_OUT) return this.logger.alert("beforeSend", `event dropped: beforeSend did not settle in ${ms} ms`);
|
|
986
|
+
safely(this.logger, "enqueue", () => enqueue(r), void 0);
|
|
987
|
+
},
|
|
988
|
+
(err) => {
|
|
989
|
+
clearTimeout(timer);
|
|
990
|
+
this.logger.warn("beforeSend", err);
|
|
991
|
+
}
|
|
992
|
+
);
|
|
993
|
+
const done = () => this.inflight.delete(p);
|
|
994
|
+
this.inflight.add(p);
|
|
995
|
+
p.then(done, done);
|
|
996
|
+
} else enqueue(out);
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* An event a rate limit dropped. Said whatever `debug` says, at most once
|
|
1000
|
+
* a minute per limit: the first drop at once, then how many were dropped
|
|
1001
|
+
* since the previous line, naming the key of the drop that prints it.
|
|
1002
|
+
*/
|
|
1003
|
+
limitDrop(why, key) {
|
|
1004
|
+
this.logger.log("dropped", why, key);
|
|
1005
|
+
const d = this.drops[why] || (this.drops[why] = { at: 0, n: 0 });
|
|
1006
|
+
d.n++;
|
|
1007
|
+
const now = Date.now();
|
|
1008
|
+
if (d.at && now - d.at < DROP_LOG_WINDOW_MS) return;
|
|
1009
|
+
this.notice(`events dropped by ${LIMIT_MESSAGES[why]}: ${d.n} ${d.at ? `in ${Math.round((now - d.at) / 1e3)} s` : "so far"}${why === "rate limited" ? `, latest "${truncate(key, 160)}"` : ""}`);
|
|
1010
|
+
d.at = now;
|
|
1011
|
+
d.n = 0;
|
|
1012
|
+
}
|
|
1013
|
+
/** A line printed whatever `debug` says (no credential ever goes in). */
|
|
1014
|
+
notice(...args) {
|
|
1015
|
+
warnAlways(...args);
|
|
1016
|
+
}
|
|
1017
|
+
/** What the queue awaits before its first request (servers: the lazy remote config). None here. */
|
|
1018
|
+
configGate() {
|
|
1019
|
+
return void 0;
|
|
1020
|
+
}
|
|
1021
|
+
/** Eager mode: read at `init`, events held until it arrives. Servers override it (src/node/server-client.ts). */
|
|
1022
|
+
loadRemoteConfig() {
|
|
1023
|
+
if (this.options.remoteConfig === false || !this.transport.fetchConfig) return this.applyRemoteConfig(DEFAULT_REMOTE_CONFIG);
|
|
1024
|
+
this.remoteReady = this.readConfig(REMOTE_CONFIG_TIMEOUT_MS, (rc) => this.applyRemoteConfig(rc));
|
|
1025
|
+
}
|
|
1026
|
+
/** GET the remote config (given up after `ms`: enabled, defaults) and `apply` it. Never rejects. */
|
|
1027
|
+
readConfig(ms, apply) {
|
|
1028
|
+
return new Promise((resolve) => {
|
|
1029
|
+
let settled = false;
|
|
1030
|
+
const done = (raw) => {
|
|
1031
|
+
if (settled) return;
|
|
1032
|
+
settled = true;
|
|
1033
|
+
clearTimeout(timer);
|
|
1034
|
+
safely(this.logger, "config", () => apply(normalizeRemoteConfig(raw)), void 0);
|
|
1035
|
+
resolve();
|
|
1036
|
+
};
|
|
1037
|
+
const timer = setTimeout(() => done(null), ms);
|
|
1038
|
+
unref(timer);
|
|
1039
|
+
try {
|
|
1040
|
+
Promise.resolve(this.transport.fetchConfig()).then(done, (err) => {
|
|
1041
|
+
this.logger.warn("config", err);
|
|
1042
|
+
done(null);
|
|
1043
|
+
});
|
|
1044
|
+
} catch (err) {
|
|
1045
|
+
this.logger.warn("config", err);
|
|
1046
|
+
done(null);
|
|
1047
|
+
}
|
|
1048
|
+
});
|
|
1049
|
+
}
|
|
1050
|
+
/** Page going away before the remote config arrived: held events continue as if it were the default (enabled). */
|
|
1051
|
+
release() {
|
|
1052
|
+
if (this.remote || !this.pending.length) return;
|
|
1053
|
+
const held = this.pending;
|
|
1054
|
+
this.pending = [];
|
|
1055
|
+
for (const [e, h] of held) safely(this.logger, "capture", () => this.finish(e, h), void 0);
|
|
1056
|
+
}
|
|
1057
|
+
applyRemoteConfig(rc) {
|
|
1058
|
+
this.remote = rc;
|
|
1059
|
+
const held = this.pending;
|
|
1060
|
+
this.pending = [];
|
|
1061
|
+
if (!rc.enabled) {
|
|
1062
|
+
this.logger.log("config: disabled");
|
|
1063
|
+
this.shutdown("config");
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
for (const [e, h] of held) safely(this.logger, "capture", () => this.finish(e, h), void 0);
|
|
1067
|
+
}
|
|
1068
|
+
/** Off for good (remote config disabled, 401/403): no hooks, nothing queued. */
|
|
1069
|
+
shutdown(reason) {
|
|
1070
|
+
if (this.state === "disabled" || this.state === "closed") return;
|
|
1071
|
+
this.logger.log("off", reason);
|
|
1072
|
+
this.state = "disabled";
|
|
1073
|
+
this.pending = [];
|
|
1074
|
+
this.queue.disable();
|
|
1075
|
+
this.teardown();
|
|
1076
|
+
}
|
|
1077
|
+
/** Last set up, first torn down: an integration that wrapped a function another one wrapped first unwraps before it. */
|
|
1078
|
+
teardown() {
|
|
1079
|
+
for (const i of this.integrations.splice(0).reverse()) {
|
|
1080
|
+
try {
|
|
1081
|
+
if (i.teardown) i.teardown();
|
|
1082
|
+
} catch (err) {
|
|
1083
|
+
this.logger.warn(i.name, err);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
};
|
|
1088
|
+
function toHint(hint) {
|
|
1089
|
+
try {
|
|
1090
|
+
const out = { ...hint };
|
|
1091
|
+
for (const k of ["mechanism", "tags", "extra", "contexts", "data"]) if (out[k] && typeof out[k] === "object") out[k] = { ...out[k] };
|
|
1092
|
+
if (out.syntheticException && !isError(out.syntheticException)) delete out.syntheticException;
|
|
1093
|
+
return out;
|
|
1094
|
+
} catch {
|
|
1095
|
+
return {};
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
function needsSyntheticStack(v) {
|
|
1099
|
+
try {
|
|
1100
|
+
return !isError(v) && !isEventLike(v);
|
|
1101
|
+
} catch {
|
|
1102
|
+
return false;
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
function eventUrl(e) {
|
|
1106
|
+
for (const ex of e.exception_list) {
|
|
1107
|
+
for (const f of ex.stacktrace ? ex.stacktrace.frames : []) if (f.filename && !NON_URL_FILES.test(f.filename)) return f.filename;
|
|
1108
|
+
}
|
|
1109
|
+
return void 0;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
// src/core/stack-parsers.ts
|
|
1113
|
+
var MAX_LINE = 2048;
|
|
1114
|
+
var MAX_PARSED = 1e3;
|
|
1115
|
+
var LOCATION = /^(.*?):(\d+)(?::(\d+))?$/;
|
|
1116
|
+
function location(loc) {
|
|
1117
|
+
const m = LOCATION.exec(loc);
|
|
1118
|
+
if (m) return { filename: m[1] || void 0, lineno: +m[2], colno: m[3] ? +m[3] : void 0 };
|
|
1119
|
+
return { filename: loc || void 0 };
|
|
1120
|
+
}
|
|
1121
|
+
function frame(fn, loc) {
|
|
1122
|
+
const f = {};
|
|
1123
|
+
if (loc.filename) f.filename = loc.filename;
|
|
1124
|
+
if (fn && fn !== "<anonymous>" && fn !== "?") f.function = fn;
|
|
1125
|
+
if (loc.lineno) f.lineno = loc.lineno;
|
|
1126
|
+
if (loc.colno) f.colno = loc.colno;
|
|
1127
|
+
return f;
|
|
1128
|
+
}
|
|
1129
|
+
var V8_LINE = /^\s*at (?:(.+?) \((.+)\)|(.+))\s*$/;
|
|
1130
|
+
var V8_EVAL = /\((\S+?):(\d+):(\d+)\)/;
|
|
1131
|
+
var v8LineParser = (line) => {
|
|
1132
|
+
const m = V8_LINE.exec(line);
|
|
1133
|
+
if (!m) return void 0;
|
|
1134
|
+
let fn = m[1];
|
|
1135
|
+
let loc = (m[2] || m[3] || "").trim();
|
|
1136
|
+
if (!fn && /^async /.test(loc)) loc = loc.slice(6);
|
|
1137
|
+
if (fn) fn = fn.replace(/^async /, "");
|
|
1138
|
+
if (/^eval at /.test(loc)) {
|
|
1139
|
+
const e = V8_EVAL.exec(loc);
|
|
1140
|
+
if (e) return frame(fn, { filename: e[1], lineno: +e[2], colno: +e[3] });
|
|
1141
|
+
return frame(fn, { filename: "<anonymous>" });
|
|
1142
|
+
}
|
|
1143
|
+
if (/^data:/.test(loc)) return frame(fn, { filename: "<data:" + loc.slice(5, loc.indexOf(",") > 0 ? loc.indexOf(",") : 40) + ">" });
|
|
1144
|
+
if (/^index \d+$/.test(loc)) return frame(fn, {});
|
|
1145
|
+
return frame(fn, location(loc));
|
|
1146
|
+
};
|
|
1147
|
+
var GECKO_EVAL = /^(.*?) line (\d+)(?: > [A-Za-z]+(?: line \d+)?)* > (?:eval|Function)/;
|
|
1148
|
+
var geckoLineParser = (line) => {
|
|
1149
|
+
const s = line.trim();
|
|
1150
|
+
if (!s) return void 0;
|
|
1151
|
+
const at = s.indexOf("@");
|
|
1152
|
+
let fn = at >= 0 ? s.slice(0, at) : void 0;
|
|
1153
|
+
let loc = at >= 0 ? s.slice(at + 1) : s;
|
|
1154
|
+
if (at < 0 && !LOCATION.test(loc) && loc !== "[native code]") return void 0;
|
|
1155
|
+
if (fn) fn = fn.replace(/^async\*/, "");
|
|
1156
|
+
const e = GECKO_EVAL.exec(loc);
|
|
1157
|
+
if (e) {
|
|
1158
|
+
return frame(fn || "eval", { filename: e[1], lineno: +e[2] });
|
|
1159
|
+
}
|
|
1160
|
+
if (loc === "[native code]") return frame(fn, { filename: "[native code]" });
|
|
1161
|
+
return frame(fn, location(loc));
|
|
1162
|
+
};
|
|
1163
|
+
function createStackParser(v8 = v8LineParser, gecko = geckoLineParser) {
|
|
1164
|
+
return (stack, skipFirst = 0) => {
|
|
1165
|
+
const frames = [];
|
|
1166
|
+
if (typeof stack !== "string" || !stack) return frames;
|
|
1167
|
+
const lines2 = stack.split("\n");
|
|
1168
|
+
const isV8 = lines2.some((l) => /^\s*at /.test(l));
|
|
1169
|
+
for (const raw of lines2) {
|
|
1170
|
+
if (frames.length >= MAX_PARSED + skipFirst) break;
|
|
1171
|
+
const line = raw.replace(/\r$/, "");
|
|
1172
|
+
if (line.length > MAX_LINE) continue;
|
|
1173
|
+
if (isV8 && !/^\s*at /.test(line)) continue;
|
|
1174
|
+
const f = (isV8 ? v8 : gecko)(line);
|
|
1175
|
+
if (f && (f.filename || f.function)) frames.push(f);
|
|
1176
|
+
}
|
|
1177
|
+
return frames.slice(skipFirst);
|
|
1178
|
+
};
|
|
1179
|
+
}
|
|
1180
|
+
var browserStackParser = createStackParser();
|
|
1181
|
+
var nodeStackParser = createStackParser(v8LineParser, () => void 0);
|
|
1182
|
+
|
|
1183
|
+
// src/core/integrations/dedupe.ts
|
|
1184
|
+
function signature(e) {
|
|
1185
|
+
const parts = [e.fingerprint || "", JSON.stringify([e.user, e.session_id])];
|
|
1186
|
+
for (const x of e.exception_list) {
|
|
1187
|
+
parts.push(x.type, x.value);
|
|
1188
|
+
for (const f of x.stacktrace ? x.stacktrace.frames : []) parts.push(`${f.filename}|${f.function}|${f.lineno}|${f.colno}`);
|
|
1189
|
+
}
|
|
1190
|
+
return parts.join("\n");
|
|
1191
|
+
}
|
|
1192
|
+
function dedupeIntegration(options = {}) {
|
|
1193
|
+
let previous;
|
|
1194
|
+
let previousKey;
|
|
1195
|
+
return {
|
|
1196
|
+
name: "Dedupe",
|
|
1197
|
+
processEvent(event) {
|
|
1198
|
+
const sig = signature(event);
|
|
1199
|
+
const key = options.requestKey && options.requestKey();
|
|
1200
|
+
if (sig === previous && key === previousKey) return null;
|
|
1201
|
+
previous = sig;
|
|
1202
|
+
previousKey = key;
|
|
1203
|
+
return event;
|
|
1204
|
+
},
|
|
1205
|
+
teardown() {
|
|
1206
|
+
previous = void 0;
|
|
1207
|
+
previousKey = void 0;
|
|
1208
|
+
}
|
|
1209
|
+
};
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
export { Client, DEFAULT_REMOTE_CONFIG, EventQueue, MAX_EVENT_BYTES, RateLimiter, browserStackParser, createStackParser, dedupeIntegration, eventUrl, geckoLineParser, isSuppressed, nodeStackParser, normalizeRemoteConfig, sanitizeUrl, trimBreadcrumbs, trimEvent, v8LineParser };
|