@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.
Files changed (108) hide show
  1. package/CHANGELOG.md +248 -0
  2. package/LICENSE +17 -0
  3. package/NOTICE +58 -0
  4. package/README.md +81 -2
  5. package/dist/api-BAFm4Lxj.d.ts +558 -0
  6. package/dist/api-DNrctwJu.d.cts +558 -0
  7. package/dist/browser/index.cjs +168 -0
  8. package/dist/browser/index.d.cts +131 -0
  9. package/dist/browser/index.d.ts +131 -0
  10. package/dist/browser/index.js +7 -0
  11. package/dist/build/rollup.cjs +22 -0
  12. package/dist/build/rollup.d.cts +34 -0
  13. package/dist/build/rollup.d.ts +34 -0
  14. package/dist/build/rollup.js +3 -0
  15. package/dist/build/vite.cjs +51 -0
  16. package/dist/build/vite.d.cts +23 -0
  17. package/dist/build/vite.d.ts +23 -0
  18. package/dist/build/vite.js +46 -0
  19. package/dist/build/webpack.cjs +18 -0
  20. package/dist/build/webpack.d.cts +39 -0
  21. package/dist/build/webpack.d.ts +39 -0
  22. package/dist/build/webpack.js +3 -0
  23. package/dist/chunk-2PM4FRM5.cjs +431 -0
  24. package/dist/chunk-335HNCOJ.js +249 -0
  25. package/dist/chunk-3AW5B2WW.js +115 -0
  26. package/dist/chunk-3R5TQAWD.js +216 -0
  27. package/dist/chunk-6H5MOPTQ.cjs +1230 -0
  28. package/dist/chunk-7TKNT3QJ.cjs +2487 -0
  29. package/dist/chunk-A4WLNFMU.cjs +220 -0
  30. package/dist/chunk-ALYFFOPR.cjs +134 -0
  31. package/dist/chunk-BVVFVRMO.js +1113 -0
  32. package/dist/chunk-CIHNKN62.cjs +15 -0
  33. package/dist/chunk-CMGCH6HD.js +1212 -0
  34. package/dist/chunk-D6QAPQ7L.cjs +56 -0
  35. package/dist/chunk-DUO5V6WZ.js +213 -0
  36. package/dist/chunk-EC2WZ6PG.js +90 -0
  37. package/dist/chunk-EFURLFJT.cjs +187 -0
  38. package/dist/chunk-EROUORMY.js +53 -0
  39. package/dist/chunk-LXBXFOAP.cjs +96 -0
  40. package/dist/chunk-MF6SA6DB.js +563 -0
  41. package/dist/chunk-N5R6ORIB.js +413 -0
  42. package/dist/chunk-N6R6BWHK.cjs +142 -0
  43. package/dist/chunk-QAGGN2T7.cjs +225 -0
  44. package/dist/chunk-QV4HRIDD.cjs +659 -0
  45. package/dist/chunk-TKNDYQB3.cjs +583 -0
  46. package/dist/chunk-UI5YC4TH.js +131 -0
  47. package/dist/chunk-VEHZYLK6.cjs +1151 -0
  48. package/dist/chunk-VNCWOGTI.js +12 -0
  49. package/dist/chunk-VWNIOGE6.js +180 -0
  50. package/dist/chunk-WDKRKGYF.js +608 -0
  51. package/dist/chunk-XOZIXBUK.js +2462 -0
  52. package/dist/chunk-ZS3K3SDR.cjs +258 -0
  53. package/dist/cli/index.cjs +2780 -0
  54. package/dist/common-DLFAqL2d.d.cts +36 -0
  55. package/dist/common-DbSXkaw6.d.ts +36 -0
  56. package/dist/dedupe-Bh5riD8Z.d.cts +13 -0
  57. package/dist/dedupe-DoYiSx-9.d.ts +13 -0
  58. package/dist/handler-BcnsnvhF.d.cts +82 -0
  59. package/dist/handler-qHzilC9T.d.ts +82 -0
  60. package/dist/index.cjs +309 -0
  61. package/dist/index.d.cts +187 -0
  62. package/dist/index.d.ts +187 -0
  63. package/dist/index.js +4 -0
  64. package/dist/next/client.cjs +113 -0
  65. package/dist/next/client.d.cts +26 -0
  66. package/dist/next/client.d.ts +26 -0
  67. package/dist/next/client.js +41 -0
  68. package/dist/next/config.cjs +277 -0
  69. package/dist/next/config.d.cts +193 -0
  70. package/dist/next/config.d.ts +193 -0
  71. package/dist/next/config.js +263 -0
  72. package/dist/next/index.cjs +148 -0
  73. package/dist/next/index.d.cts +32 -0
  74. package/dist/next/index.d.ts +32 -0
  75. package/dist/next/index.js +59 -0
  76. package/dist/node/index.cjs +257 -0
  77. package/dist/node/index.d.cts +145 -0
  78. package/dist/node/index.d.ts +145 -0
  79. package/dist/node/index.js +83 -0
  80. package/dist/options-BUhWuWJX.d.cts +79 -0
  81. package/dist/options-BUhWuWJX.d.ts +79 -0
  82. package/dist/react/index.cjs +107 -0
  83. package/dist/react/index.d.cts +84 -0
  84. package/dist/react/index.d.ts +84 -0
  85. package/dist/react/index.js +103 -0
  86. package/dist/scope-D1ouJm2h.d.cts +628 -0
  87. package/dist/scope-D1ouJm2h.d.ts +628 -0
  88. package/dist/server-edge-7624N3WF.cjs +51 -0
  89. package/dist/server-edge-BEEXWL7T.js +48 -0
  90. package/dist/server-node-ET4LIPEC.cjs +27 -0
  91. package/dist/server-node-TQUVMZUC.js +24 -0
  92. package/dist/session-C_WzbEN2.d.cts +4 -0
  93. package/dist/session-C_WzbEN2.d.ts +4 -0
  94. package/dist/session-propagation-Pr0Zb9Zn.d.cts +30 -0
  95. package/dist/session-propagation-RM5py1bp.d.ts +30 -0
  96. package/dist/starko.min.js +6 -0
  97. package/docs/browser.md +386 -0
  98. package/docs/configuration.md +310 -0
  99. package/docs/nextjs.md +488 -0
  100. package/docs/node.md +427 -0
  101. package/docs/other-backends.md +232 -0
  102. package/docs/privacy.md +112 -0
  103. package/docs/react.md +187 -0
  104. package/docs/source-maps.md +482 -0
  105. package/docs/troubleshooting.md +263 -0
  106. package/docs/widget.md +84 -0
  107. package/docs/wire-format.md +547 -0
  108. package/package.json +191 -4
@@ -0,0 +1,608 @@
1
+ import { version, LIMITS } from './chunk-3AW5B2WW.js';
2
+
3
+ // src/core/instrument.ts
4
+ var KEY = /* @__PURE__ */ Symbol.for("starko.sdk.instrument.v1");
5
+ function registry() {
6
+ const g = globalThis;
7
+ return g[KEY] || (g[KEY] = /* @__PURE__ */ new WeakMap());
8
+ }
9
+ var slotOf = (target, name) => {
10
+ const per = registry().get(target);
11
+ return per && per[name];
12
+ };
13
+ function originalOf(target, name) {
14
+ const s = slotOf(target, name);
15
+ return s ? s.o : target[name];
16
+ }
17
+ function addHook(target, name, hook) {
18
+ try {
19
+ const rec = target;
20
+ const reg = registry();
21
+ let per = reg.get(rec);
22
+ if (!per) reg.set(rec, per = {});
23
+ let s = per[name];
24
+ if (!s) {
25
+ const o = rec[name];
26
+ if (typeof o !== "function") return noop;
27
+ const slot2 = {
28
+ o,
29
+ h: [],
30
+ w: function() {
31
+ const args = Array.prototype.slice.call(arguments);
32
+ const after = [];
33
+ for (const h of slot2.h.slice()) {
34
+ try {
35
+ const a = h(this, args);
36
+ if (a) after.push(a);
37
+ } catch {
38
+ }
39
+ }
40
+ const ret = slot2.o.apply(this, args);
41
+ for (const a of after) {
42
+ try {
43
+ a(ret);
44
+ } catch {
45
+ }
46
+ }
47
+ return ret;
48
+ }
49
+ };
50
+ rec[name] = slot2.w;
51
+ if (rec[name] !== slot2.w) return noop;
52
+ s = per[name] = slot2;
53
+ }
54
+ const slot = s;
55
+ slot.h.push(hook);
56
+ return () => {
57
+ try {
58
+ const i = slot.h.indexOf(hook);
59
+ if (i >= 0) slot.h.splice(i, 1);
60
+ if (slot.h.length || per[name] !== slot) return;
61
+ per[name] = void 0;
62
+ if (rec[name] === slot.w) rec[name] = slot.o;
63
+ } catch {
64
+ }
65
+ };
66
+ } catch {
67
+ return noop;
68
+ }
69
+ }
70
+ var noop = () => void 0;
71
+
72
+ // src/core/utils.ts
73
+ var SDK_VERSION = version;
74
+ var toStr = (v) => Object.prototype.toString.call(v);
75
+ var isTagged = (v, tag) => toStr(v) === `[object ${tag}]`;
76
+ var isString = (v) => typeof v === "string";
77
+ var isPlainObject = (v) => toStr(v) === "[object Object]";
78
+ var isPrimitive = (v) => v === null || typeof v !== "object" && typeof v !== "function";
79
+ function isError(v) {
80
+ if (/^\[object (?:Error|Exception|DOMException|WebAssembly\.Exception)\]$/.test(toStr(v))) return true;
81
+ try {
82
+ return v instanceof Error;
83
+ } catch {
84
+ return false;
85
+ }
86
+ }
87
+ function isEventLike(v) {
88
+ if (!v || typeof v !== "object") return false;
89
+ try {
90
+ return typeof Event !== "undefined" && v instanceof Event;
91
+ } catch {
92
+ return false;
93
+ }
94
+ }
95
+ var isThenable = (v) => !!v && (typeof v === "object" || typeof v === "function") && typeof v.then === "function";
96
+ function truncate(s, max) {
97
+ if (s.length <= max) return s;
98
+ let end = max;
99
+ const c = s.charCodeAt(end - 1);
100
+ if (c >= 55296 && c <= 56319) end--;
101
+ return s.slice(0, end);
102
+ }
103
+ function utf8Length(s) {
104
+ let n = 0;
105
+ for (let i = 0; i < s.length; i++) {
106
+ const c = s.charCodeAt(i);
107
+ if (c < 128) n += 1;
108
+ else if (c < 2048) n += 2;
109
+ else if (c >= 55296 && c <= 56319) {
110
+ n += 4;
111
+ i++;
112
+ } else n += 3;
113
+ }
114
+ return n;
115
+ }
116
+ function uuid4() {
117
+ const c = globalThis.crypto;
118
+ try {
119
+ if (c && typeof c.randomUUID === "function") return c.randomUUID();
120
+ } catch {
121
+ }
122
+ let bytes;
123
+ try {
124
+ if (c && typeof c.getRandomValues === "function") bytes = c.getRandomValues(new Uint8Array(16));
125
+ } catch {
126
+ }
127
+ let i = 0;
128
+ return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (d) => {
129
+ const r = bytes ? bytes[i++ >> 1] >> (i & 1) * 4 : Math.random() * 16;
130
+ return (+d ^ (r & 15) >> +d / 4).toString(16);
131
+ });
132
+ }
133
+ function safeString(v) {
134
+ try {
135
+ return String(v);
136
+ } catch {
137
+ return "[unserializable]";
138
+ }
139
+ }
140
+ var LOG_PREFIX = "[starko]";
141
+ var originalConsole = {};
142
+ try {
143
+ if (typeof console !== "undefined") {
144
+ for (const k of ["log", "warn"]) originalConsole[k] = originalOf(console, k).bind(console);
145
+ }
146
+ } catch {
147
+ }
148
+ function warnAlways(...args) {
149
+ try {
150
+ const f = originalConsole.warn;
151
+ if (f) f(LOG_PREFIX, ...args);
152
+ } catch {
153
+ }
154
+ }
155
+ function createLogger(enabled = false) {
156
+ const alerted = /* @__PURE__ */ new Set();
157
+ const out = (k, args) => {
158
+ if (!logger2.enabled) return;
159
+ try {
160
+ const f = originalConsole[k];
161
+ if (f) f(LOG_PREFIX, ...args);
162
+ } catch {
163
+ }
164
+ };
165
+ const logger2 = {
166
+ enabled,
167
+ log: (...a) => out("log", a),
168
+ warn: (...a) => out("warn", a),
169
+ alert: (key, ...a) => {
170
+ if (alerted.has(key)) return;
171
+ alerted.add(key);
172
+ warnAlways(...a);
173
+ }
174
+ };
175
+ return logger2;
176
+ }
177
+ function safely(logger2, what, fn, fallback) {
178
+ try {
179
+ return fn();
180
+ } catch (err) {
181
+ if (logger2) logger2.warn(`${what} failed`, err);
182
+ return fallback;
183
+ }
184
+ }
185
+ function matchesAny(value, patterns) {
186
+ if (!patterns) return false;
187
+ for (const p of patterns) {
188
+ try {
189
+ if (isString(p) ? value.includes(p) : p.test(value)) return true;
190
+ } catch {
191
+ }
192
+ }
193
+ return false;
194
+ }
195
+ function unref(t) {
196
+ const u = t && t.unref;
197
+ if (typeof u === "function") u.call(t);
198
+ }
199
+ var clamp = (n, lo, hi) => Math.min(hi, Math.max(lo, n));
200
+ var num = (v, fallback) => typeof v === "number" && isFinite(v) ? v : fallback;
201
+
202
+ // src/core/normalize.ts
203
+ var MAX_KEYS = 100;
204
+ var MAX_STRING = 2e3;
205
+ function normalize(value, depth = 5) {
206
+ return walk(value, depth, /* @__PURE__ */ new WeakSet());
207
+ }
208
+ function walk(v, depth, seen) {
209
+ try {
210
+ if (v === null || typeof v === "boolean") return v;
211
+ if (typeof v === "string") return truncate(v, MAX_STRING);
212
+ if (typeof v === "number") return isFinite(v) ? v : `[${safeString(v)}]`;
213
+ if (typeof v === "undefined") return "[undefined]";
214
+ if (typeof v === "bigint") return `[BigInt: ${v.toString()}]`;
215
+ if (typeof v === "symbol") return `[${safeString(v)}]`;
216
+ if (typeof v === "function") return `[Function: ${v.name || "<anonymous>"}]`;
217
+ const o = v;
218
+ if (seen.has(o)) return "[Circular]";
219
+ const node = o;
220
+ if (typeof node.nodeType === "number" && typeof node.nodeName === "string") return `[${node.nodeName}]`;
221
+ if (o.window === o) return "[Window]";
222
+ if (o instanceof Date) return isNaN(o.getTime()) ? "[Invalid Date]" : o.toISOString();
223
+ if (o instanceof RegExp) return safeString(o);
224
+ if (depth <= 0) return Array.isArray(o) ? "[Array]" : "[Object]";
225
+ seen.add(o);
226
+ let out;
227
+ if (Array.isArray(o)) {
228
+ const arr = [];
229
+ for (let i = 0; i < o.length && i < MAX_KEYS; i++) arr.push(walk(o[i], depth - 1, seen));
230
+ if (o.length > MAX_KEYS) arr.push(`[\u2026 ${o.length - MAX_KEYS} more]`);
231
+ out = arr;
232
+ } else if (isError(o)) {
233
+ const e = o;
234
+ const rec = { name: walk(e.name, 1, seen), message: walk(e.message, 1, seen) };
235
+ for (const k of Object.keys(e).slice(0, MAX_KEYS)) rec[k] = walk(e[k], depth - 1, seen);
236
+ out = rec;
237
+ } else {
238
+ const toJSON = o.toJSON;
239
+ if (typeof toJSON === "function" && !isPlainObject(o)) {
240
+ out = walk(toJSON.call(o), depth - 1, seen);
241
+ } else {
242
+ const rec = {};
243
+ let n = 0;
244
+ for (const k in o) {
245
+ if (!Object.prototype.hasOwnProperty.call(o, k)) continue;
246
+ if (n++ >= MAX_KEYS) {
247
+ rec["\u2026"] = "[more keys]";
248
+ break;
249
+ }
250
+ let val;
251
+ try {
252
+ val = o[k];
253
+ } catch {
254
+ val = "[unserializable]";
255
+ }
256
+ if (val === void 0) continue;
257
+ rec[k] = walk(val, depth - 1, seen);
258
+ }
259
+ out = rec;
260
+ }
261
+ }
262
+ seen.delete(o);
263
+ return out;
264
+ } catch {
265
+ return "[unserializable]";
266
+ }
267
+ }
268
+ function safeStringify(v) {
269
+ try {
270
+ return JSON.stringify(v) ?? "null";
271
+ } catch {
272
+ return '"[unserializable]"';
273
+ }
274
+ }
275
+ function normalizeToSize(value, maxBytes, depth = 5) {
276
+ for (let d = depth; d >= 1; d--) {
277
+ const n = normalize(value, d);
278
+ if (utf8Length(safeStringify(n)) <= maxBytes) return n;
279
+ }
280
+ return { _truncated: `[over ${maxBytes} bytes]` };
281
+ }
282
+
283
+ // src/core/scope.ts
284
+ var MAX_BREADCRUMB_ITEM_BYTES = 4 * 1024;
285
+ var BreadcrumbBuffer = class _BreadcrumbBuffer {
286
+ constructor(maxItems = LIMITS.MAX_BREADCRUMBS, maxBytes = LIMITS.MAX_BREADCRUMB_BYTES) {
287
+ this.maxItems = maxItems;
288
+ this.maxBytes = maxBytes;
289
+ this.items = [];
290
+ this.bytes = 0;
291
+ this.maxItems = clamp(Math.floor(maxItems), 0, LIMITS.MAX_BREADCRUMBS);
292
+ }
293
+ add(input) {
294
+ if (this.maxItems === 0) return;
295
+ const crumb = sanitizeBreadcrumb(input);
296
+ let size = utf8Length(safeStringify(crumb)) + 1;
297
+ if (size > MAX_BREADCRUMB_ITEM_BYTES && crumb.data) {
298
+ crumb.data = normalizeToSize(crumb.data, MAX_BREADCRUMB_ITEM_BYTES / 2, 3);
299
+ size = utf8Length(safeStringify(crumb)) + 1;
300
+ }
301
+ if (size > MAX_BREADCRUMB_ITEM_BYTES) {
302
+ delete crumb.data;
303
+ if (crumb.message) crumb.message = truncate(crumb.message, 1024);
304
+ size = utf8Length(safeStringify(crumb)) + 1;
305
+ }
306
+ this.items.push({ crumb, bytes: size });
307
+ this.bytes += size;
308
+ while (this.items.length > this.maxItems || this.bytes > this.maxBytes) {
309
+ const old = this.items.shift();
310
+ if (!old) break;
311
+ this.bytes -= old.bytes;
312
+ }
313
+ }
314
+ /** Change the count cap (0..50); the oldest are dropped when it shrinks. */
315
+ setMaxItems(n) {
316
+ this.maxItems = clamp(Math.floor(n), 0, LIMITS.MAX_BREADCRUMBS);
317
+ while (this.items.length > this.maxItems) this.bytes -= this.items.shift().bytes;
318
+ }
319
+ /** Oldest first. */
320
+ get() {
321
+ return this.items.map((i) => i.crumb);
322
+ }
323
+ get byteSize() {
324
+ return this.bytes;
325
+ }
326
+ clear() {
327
+ this.items = [];
328
+ this.bytes = 0;
329
+ }
330
+ clone() {
331
+ const b = new _BreadcrumbBuffer(this.maxItems, this.maxBytes);
332
+ b.items = this.items.slice();
333
+ b.bytes = this.bytes;
334
+ return b;
335
+ }
336
+ };
337
+ var LEVELS = ["fatal", "error", "warning", "info", "debug"];
338
+ var isLevel = (v) => LEVELS.includes(v);
339
+ function sanitizeBreadcrumb(b) {
340
+ const out = {
341
+ timestamp: typeof b.timestamp === "number" && isFinite(b.timestamp) ? b.timestamp : Date.now(),
342
+ type: typeof b.type === "string" && b.type ? truncate(b.type, 32) : "custom"
343
+ };
344
+ if (b.category != null) out.category = truncate(safeString(b.category), 64);
345
+ if (b.message != null) out.message = truncate(safeString(b.message), 2048);
346
+ if (isLevel(b.level)) out.level = b.level;
347
+ if (b.data && typeof b.data === "object") out.data = normalizeToSize(b.data, MAX_BREADCRUMB_ITEM_BYTES, 3);
348
+ return out;
349
+ }
350
+ var Scope = class _Scope {
351
+ constructor(maxBreadcrumbs = LIMITS.MAX_BREADCRUMBS) {
352
+ this.user = null;
353
+ this.tags = {};
354
+ this.extra = {};
355
+ this.contexts = {};
356
+ this.breadcrumbs = new BreadcrumbBuffer(maxBreadcrumbs);
357
+ }
358
+ setUser(user) {
359
+ this.user = user ? { ...user } : null;
360
+ return this;
361
+ }
362
+ setTag(key, value) {
363
+ this.tags = { ...this.tags, [key]: safeString(value) };
364
+ return this;
365
+ }
366
+ setTags(tags) {
367
+ for (const k of Object.keys(tags || {})) this.setTag(k, tags[k]);
368
+ return this;
369
+ }
370
+ setExtra(key, value) {
371
+ this.extra = { ...this.extra, [key]: value };
372
+ return this;
373
+ }
374
+ setExtras(extras) {
375
+ this.extra = { ...this.extra, ...extras };
376
+ return this;
377
+ }
378
+ /** `null` removes the context. */
379
+ setContext(name, context) {
380
+ const next = { ...this.contexts };
381
+ if (context === null) delete next[name];
382
+ else next[name] = context;
383
+ this.contexts = next;
384
+ return this;
385
+ }
386
+ setFingerprint(fingerprint) {
387
+ this.fingerprint = fingerprint;
388
+ return this;
389
+ }
390
+ setLevel(level) {
391
+ this.level = level;
392
+ return this;
393
+ }
394
+ addBreadcrumb(b) {
395
+ this.breadcrumbs.add(b);
396
+ return this;
397
+ }
398
+ clearBreadcrumbs() {
399
+ this.breadcrumbs.clear();
400
+ return this;
401
+ }
402
+ clear() {
403
+ this.user = null;
404
+ this.tags = {};
405
+ this.extra = {};
406
+ this.contexts = {};
407
+ this.fingerprint = void 0;
408
+ this.level = void 0;
409
+ this.breadcrumbs.clear();
410
+ return this;
411
+ }
412
+ /** A fork: changes to it never reach this scope. */
413
+ clone() {
414
+ const s = new _Scope(0);
415
+ s.user = this.user ? { ...this.user } : null;
416
+ s.tags = { ...this.tags };
417
+ s.extra = { ...this.extra };
418
+ s.contexts = { ...this.contexts };
419
+ s.fingerprint = this.fingerprint;
420
+ s.level = this.level;
421
+ s.breadcrumbs = this.breadcrumbs.clone();
422
+ return s;
423
+ }
424
+ /** Writes the scope's data into `event`; the event's own values win. */
425
+ applyToEvent(event) {
426
+ if (this.user && Object.keys(this.user).length) event.user = { ...this.user, ...event.user };
427
+ if (Object.keys(this.tags).length) event.tags = { ...this.tags, ...event.tags };
428
+ if (Object.keys(this.extra).length) event.extra = { ...this.extra, ...event.extra };
429
+ if (Object.keys(this.contexts).length) event.contexts = { ...this.contexts, ...event.contexts };
430
+ if (this.fingerprint && !event.fingerprint) event.fingerprint = this.fingerprint;
431
+ if (this.level) event.level = this.level;
432
+ const crumbs = this.breadcrumbs.get();
433
+ if (crumbs.length) event.breadcrumbs = [...crumbs, ...event.breadcrumbs || []];
434
+ return event;
435
+ }
436
+ };
437
+
438
+ // src/core/carrier.ts
439
+ function compatLine(version2) {
440
+ const m = /^(\d+)\.(\d+)/.exec(version2);
441
+ if (!m) return version2;
442
+ return m[1] === "0" ? `0.${m[2]}` : m[1];
443
+ }
444
+ var CARRIER_KEY = /* @__PURE__ */ Symbol.for(`starko.sdk.carrier.v2/${compatLine(SDK_VERSION)}`);
445
+ function state() {
446
+ const g = globalThis;
447
+ let s = g[CARRIER_KEY];
448
+ if (!s || typeof s !== "object") {
449
+ s = { version: SDK_VERSION, client: void 0, globalScope: new Scope(), stack: [], manager: void 0 };
450
+ g[CARRIER_KEY] = s;
451
+ }
452
+ return s;
453
+ }
454
+ var stackManager = {
455
+ getScope: () => {
456
+ const s = state();
457
+ return s.stack[s.stack.length - 1] || s.globalScope;
458
+ },
459
+ withScope(callback) {
460
+ const stack = state().stack;
461
+ const fork = stackManager.getScope().clone();
462
+ stack.push(fork);
463
+ try {
464
+ return callback(fork);
465
+ } finally {
466
+ const i = stack.lastIndexOf(fork);
467
+ if (i >= 0) stack.splice(i, 1);
468
+ }
469
+ }
470
+ };
471
+ var manager = () => state().manager || stackManager;
472
+ function getClient() {
473
+ return state().client;
474
+ }
475
+ function setCurrentClient(client) {
476
+ state().client = client;
477
+ }
478
+ function clientMethod(client, name) {
479
+ try {
480
+ const f = client && client[name];
481
+ return typeof f === "function" ? f.bind(client) : void 0;
482
+ } catch {
483
+ return void 0;
484
+ }
485
+ }
486
+ function getCurrentScope() {
487
+ return manager().getScope();
488
+ }
489
+ function getGlobalScope() {
490
+ return state().globalScope;
491
+ }
492
+ function setScopeManager(m) {
493
+ state().manager = m;
494
+ }
495
+ function withScopeInternal(callback) {
496
+ return manager().withScope(callback);
497
+ }
498
+ function getEventScope() {
499
+ const m = manager();
500
+ return m.peekScope ? m.peekScope() : m.getScope();
501
+ }
502
+ function getUserScope(user) {
503
+ const m = manager();
504
+ return m.scopeForUser ? m.scopeForUser(user) : m.getScope();
505
+ }
506
+
507
+ // src/core/api.ts
508
+ var logger = () => {
509
+ const c = getClient();
510
+ return c && c.logger;
511
+ };
512
+ var warn = (what, err) => {
513
+ const l = logger();
514
+ if (l) l.warn(what, err);
515
+ };
516
+ var guard = (fn, fallback) => safely(logger(), "API call", fn, fallback);
517
+ var copy = (hint) => safely(void 0, "", () => hint && typeof hint === "object" ? { ...hint } : {}, {});
518
+ function captureException(exception, hint) {
519
+ let syn;
520
+ try {
521
+ if (!(hint && hint.syntheticException) && !isError(exception) && !isEventLike(exception)) syn = new Error();
522
+ } catch {
523
+ }
524
+ return guard(() => {
525
+ const capture = clientMethod(getClient(), "captureException");
526
+ if (!capture) return "";
527
+ const h = copy(hint);
528
+ return capture(exception, syn ? { ...h, syntheticException: syn, syntheticSkip: 1 } : h);
529
+ }, "");
530
+ }
531
+ function captureMessage(message, levelOrHint) {
532
+ const syn = new Error();
533
+ return guard(() => {
534
+ const capture = clientMethod(getClient(), "captureMessage");
535
+ if (!capture) return "";
536
+ const hint = copy(levelOrHint);
537
+ const level = typeof levelOrHint === "string" ? levelOrHint : hint.level || "info";
538
+ return capture(safeString(message), level, { ...hint, syntheticException: syn, syntheticSkip: 1 });
539
+ }, "");
540
+ }
541
+ function setUser(user) {
542
+ guard(() => {
543
+ const u = user && typeof user === "object" ? user : null;
544
+ const scope = getUserScope(u);
545
+ if (scope) scope.setUser(u);
546
+ }, void 0);
547
+ }
548
+ function identify(user, userHash) {
549
+ guard(() => {
550
+ const u = typeof user === "string" ? { id: user } : { ...user };
551
+ if (userHash) u.hash = userHash;
552
+ const scope = getUserScope(u);
553
+ if (scope) scope.setUser(u);
554
+ }, void 0);
555
+ }
556
+ function setTag(key, value) {
557
+ guard(() => void getCurrentScope().setTag(key, value), void 0);
558
+ }
559
+ function setTags(tags) {
560
+ guard(() => void getCurrentScope().setTags(tags), void 0);
561
+ }
562
+ function setExtra(key, value) {
563
+ guard(() => void getCurrentScope().setExtra(key, value), void 0);
564
+ }
565
+ function setExtras(extras) {
566
+ guard(() => void getCurrentScope().setExtras(extras), void 0);
567
+ }
568
+ function setContext(name, context) {
569
+ guard(() => void getCurrentScope().setContext(name, context), void 0);
570
+ }
571
+ function addBreadcrumb(breadcrumb, hint) {
572
+ guard(() => {
573
+ const crumb = { timestamp: Date.now(), type: "custom", ...breadcrumb };
574
+ const add = clientMethod(getClient(), "addBreadcrumb");
575
+ if (add) add(crumb, hint);
576
+ else getCurrentScope().addBreadcrumb(crumb);
577
+ }, void 0);
578
+ }
579
+ function withScope(callback) {
580
+ if (typeof callback !== "function") return void 0;
581
+ let entered = false;
582
+ try {
583
+ return withScopeInternal((scope) => {
584
+ entered = true;
585
+ return callback(scope);
586
+ });
587
+ } catch (err) {
588
+ if (entered) throw err;
589
+ warn("withScope failed", err);
590
+ return callback(getCurrentScope());
591
+ }
592
+ }
593
+ function flush(timeoutMs) {
594
+ return guard(() => {
595
+ const c = getClient();
596
+ const f = clientMethod(c, "flush");
597
+ return f ? f(timeoutMs) : Promise.resolve(!c);
598
+ }, Promise.resolve(false));
599
+ }
600
+ function close(timeoutMs) {
601
+ return guard(() => {
602
+ const c = getClient();
603
+ const f = clientMethod(c, "close");
604
+ return f ? f(timeoutMs) : Promise.resolve(!c);
605
+ }, Promise.resolve(false));
606
+ }
607
+
608
+ export { BreadcrumbBuffer, LOG_PREFIX, SDK_VERSION, Scope, addBreadcrumb, addHook, captureException, captureMessage, clamp, clientMethod, close, compatLine, createLogger, flush, getClient, getCurrentScope, getEventScope, getGlobalScope, identify, isError, isEventLike, isLevel, isPlainObject, isPrimitive, isString, isTagged, isThenable, matchesAny, normalize, normalizeToSize, num, originalOf, safeString, safeStringify, safely, sanitizeBreadcrumb, setContext, setCurrentClient, setExtra, setExtras, setScopeManager, setTag, setTags, setUser, truncate, unref, utf8Length, uuid4, warnAlways, withScope };