@cross-deck/web 0.7.0 → 1.0.0

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/dist/vue.mjs ADDED
@@ -0,0 +1,3324 @@
1
+ // src/vue.ts
2
+ import { ref, onMounted, onScopeDispose } from "vue";
3
+
4
+ // src/errors.ts
5
+ var CrossdeckError = class _CrossdeckError extends Error {
6
+ constructor(payload) {
7
+ super(payload.message);
8
+ this.name = "CrossdeckError";
9
+ this.type = payload.type;
10
+ this.code = payload.code;
11
+ this.requestId = payload.requestId;
12
+ this.status = payload.status;
13
+ this.retryAfterMs = payload.retryAfterMs;
14
+ Object.setPrototypeOf(this, _CrossdeckError.prototype);
15
+ }
16
+ };
17
+ async function crossdeckErrorFromResponse(res) {
18
+ const requestId = res.headers.get("x-request-id") ?? void 0;
19
+ const retryAfterMs = parseRetryAfterHeader(res.headers.get("retry-after"));
20
+ let body;
21
+ try {
22
+ body = await res.json();
23
+ } catch {
24
+ body = null;
25
+ }
26
+ const envelope = body?.error;
27
+ if (envelope && typeof envelope.type === "string" && typeof envelope.code === "string") {
28
+ return new CrossdeckError({
29
+ type: envelope.type,
30
+ code: envelope.code,
31
+ message: envelope.message ?? `HTTP ${res.status}`,
32
+ requestId: envelope.request_id ?? requestId,
33
+ status: res.status,
34
+ retryAfterMs
35
+ });
36
+ }
37
+ return new CrossdeckError({
38
+ type: typeMapForStatus(res.status),
39
+ code: `http_${res.status}`,
40
+ message: `HTTP ${res.status} ${res.statusText || ""}`.trim(),
41
+ requestId,
42
+ status: res.status,
43
+ retryAfterMs
44
+ });
45
+ }
46
+ function parseRetryAfterHeader(value) {
47
+ if (!value) return void 0;
48
+ const trimmed = value.trim();
49
+ if (!trimmed) return void 0;
50
+ if (/^\d+(\.\d+)?$/.test(trimmed)) {
51
+ const secs = Number(trimmed);
52
+ if (!Number.isFinite(secs) || secs < 0) return void 0;
53
+ return Math.round(secs * 1e3);
54
+ }
55
+ if (!/[a-zA-Z,/:]/.test(trimmed)) return void 0;
56
+ const target = Date.parse(trimmed);
57
+ if (!Number.isFinite(target)) return void 0;
58
+ const delta = target - Date.now();
59
+ return delta > 0 ? delta : 0;
60
+ }
61
+ function typeMapForStatus(status) {
62
+ if (status === 401) return "authentication_error";
63
+ if (status === 403) return "permission_error";
64
+ if (status === 429) return "rate_limit_error";
65
+ if (status >= 400 && status < 500) return "invalid_request_error";
66
+ return "internal_error";
67
+ }
68
+
69
+ // src/http.ts
70
+ var SDK_NAME = "@cross-deck/web";
71
+ var SDK_VERSION = "1.0.0";
72
+ var DEFAULT_BASE_URL = "https://api.cross-deck.com/v1";
73
+ var DEFAULT_TIMEOUT_MS = 15e3;
74
+ var HttpClient = class {
75
+ constructor(config) {
76
+ this.config = config;
77
+ }
78
+ /**
79
+ * Issue a request. `path` is relative to the configured baseUrl
80
+ * ("/entitlements", "/identity/alias", etc.).
81
+ *
82
+ * Throws CrossdeckError on:
83
+ * - Network failure (`type: "network_error"`)
84
+ * - Non-2xx response (typed from the body envelope)
85
+ * - JSON parse failure on a 2xx (treated as `internal_error`)
86
+ */
87
+ async request(method, path, options = {}) {
88
+ if (this.config.localDevMode) {
89
+ return synthesizeLocalDevResponse(path);
90
+ }
91
+ const url = this.buildUrl(path, options.query);
92
+ const headers = {
93
+ Authorization: `Bearer ${this.config.publicKey}`,
94
+ "Crossdeck-Sdk-Version": `${SDK_NAME}@${this.config.sdkVersion}`,
95
+ Accept: "application/json"
96
+ };
97
+ if (options.idempotencyKey) {
98
+ headers["Idempotency-Key"] = options.idempotencyKey;
99
+ }
100
+ let bodyInit;
101
+ if (options.body !== void 0) {
102
+ headers["Content-Type"] = "application/json";
103
+ bodyInit = JSON.stringify(options.body);
104
+ }
105
+ const effectiveTimeout = options.timeoutMs ?? this.config.timeoutMs ?? DEFAULT_TIMEOUT_MS;
106
+ const controller = typeof AbortController !== "undefined" && effectiveTimeout > 0 ? new AbortController() : null;
107
+ let timeoutHandle = null;
108
+ if (controller && effectiveTimeout > 0) {
109
+ timeoutHandle = setTimeout(() => controller.abort(), effectiveTimeout);
110
+ }
111
+ let response;
112
+ try {
113
+ response = await fetch(url, {
114
+ method,
115
+ headers,
116
+ body: bodyInit,
117
+ keepalive: options.keepalive === true,
118
+ signal: controller?.signal
119
+ });
120
+ } catch (err) {
121
+ const aborted = controller?.signal?.aborted === true;
122
+ throw new CrossdeckError({
123
+ type: "network_error",
124
+ code: aborted ? "request_timeout" : "fetch_failed",
125
+ message: aborted ? `Request to ${path} aborted after ${effectiveTimeout}ms` : err instanceof Error ? err.message : "fetch failed"
126
+ });
127
+ } finally {
128
+ if (timeoutHandle !== null) clearTimeout(timeoutHandle);
129
+ }
130
+ if (!response.ok) {
131
+ throw await crossdeckErrorFromResponse(response);
132
+ }
133
+ if (response.status === 204) return void 0;
134
+ try {
135
+ return await response.json();
136
+ } catch (err) {
137
+ throw new CrossdeckError({
138
+ type: "internal_error",
139
+ code: "invalid_json_response",
140
+ message: "Server returned a 2xx with an unparseable body.",
141
+ requestId: response.headers.get("x-request-id") ?? void 0,
142
+ status: response.status
143
+ });
144
+ }
145
+ }
146
+ /**
147
+ * Whether this client is in localhost dev-mode short-circuit. Used
148
+ * by other SDK pieces (event-queue) to skip network-bound work
149
+ * entirely rather than going through synthesizeLocalDevResponse.
150
+ */
151
+ get isLocalDevMode() {
152
+ return this.config.localDevMode === true;
153
+ }
154
+ buildUrl(path, query) {
155
+ const base = this.config.baseUrl.replace(/\/+$/, "");
156
+ const cleanPath = path.startsWith("/") ? path : `/${path}`;
157
+ let url = base + cleanPath;
158
+ if (query) {
159
+ const params = new URLSearchParams();
160
+ for (const [k, v] of Object.entries(query)) {
161
+ if (typeof v === "string" && v.length > 0) params.append(k, v);
162
+ }
163
+ const qs = params.toString();
164
+ if (qs) url += (url.includes("?") ? "&" : "?") + qs;
165
+ }
166
+ return url;
167
+ }
168
+ };
169
+ var cachedLocalCdcust = null;
170
+ function synthesizeLocalDevResponse(path) {
171
+ if (path.startsWith("/sdk/heartbeat")) {
172
+ return {
173
+ object: "heartbeat",
174
+ ok: true,
175
+ projectId: "proj_local_dev",
176
+ appId: "app_local_dev",
177
+ platform: "web",
178
+ env: "sandbox",
179
+ serverTime: Date.now()
180
+ };
181
+ }
182
+ if (path.startsWith("/identity/alias")) {
183
+ if (!cachedLocalCdcust) {
184
+ const tail = typeof crypto !== "undefined" && "randomUUID" in crypto ? crypto.randomUUID().replace(/-/g, "").slice(0, 16) : Math.random().toString(36).slice(2, 18);
185
+ cachedLocalCdcust = `cdcust_local_${tail}`;
186
+ }
187
+ return {
188
+ object: "alias_result",
189
+ crossdeckCustomerId: cachedLocalCdcust,
190
+ linked: [],
191
+ mergePending: false,
192
+ env: "sandbox"
193
+ };
194
+ }
195
+ if (path.startsWith("/entitlements")) {
196
+ return {
197
+ object: "list",
198
+ data: [],
199
+ crossdeckCustomerId: cachedLocalCdcust ?? "",
200
+ env: "sandbox"
201
+ };
202
+ }
203
+ if (path.startsWith("/events")) {
204
+ return {
205
+ object: "list",
206
+ received: 0,
207
+ env: "sandbox"
208
+ };
209
+ }
210
+ return {};
211
+ }
212
+
213
+ // src/identity.ts
214
+ var KEY_ANON = "anon_id";
215
+ var KEY_CDCUST = "cdcust_id";
216
+ var IdentityStore = class {
217
+ constructor(primary, prefix, secondary) {
218
+ this.primary = primary;
219
+ this.prefix = prefix;
220
+ this.secondary = secondary ?? null;
221
+ const anonFromPrimary = primary.getItem(prefix + KEY_ANON);
222
+ const cdcustFromPrimary = primary.getItem(prefix + KEY_CDCUST);
223
+ const anonFromSecondary = this.secondary?.getItem(prefix + KEY_ANON) ?? null;
224
+ const cdcustFromSecondary = this.secondary?.getItem(prefix + KEY_CDCUST) ?? null;
225
+ const anon = anonFromPrimary ?? anonFromSecondary;
226
+ const cdcust = cdcustFromPrimary ?? cdcustFromSecondary;
227
+ this.state = {
228
+ anonymousId: anon ?? this.mintAnonymousId(),
229
+ crossdeckCustomerId: cdcust
230
+ };
231
+ if (!anonFromPrimary || !anonFromSecondary) {
232
+ this.writeBoth(prefix + KEY_ANON, this.state.anonymousId);
233
+ }
234
+ if (cdcust && (!cdcustFromPrimary || !cdcustFromSecondary)) {
235
+ this.writeBoth(prefix + KEY_CDCUST, cdcust);
236
+ }
237
+ }
238
+ /** Return the persisted anonymous device ID (always set). */
239
+ get anonymousId() {
240
+ return this.state.anonymousId;
241
+ }
242
+ /** Return the resolved cross­deckCustomerId once we have one, else null. */
243
+ get crossdeckCustomerId() {
244
+ return this.state.crossdeckCustomerId;
245
+ }
246
+ /** Persist a newly-resolved Crossdeck customer ID. */
247
+ setCrossdeckCustomerId(value) {
248
+ this.state.crossdeckCustomerId = value;
249
+ this.writeBoth(this.prefix + KEY_CDCUST, value);
250
+ }
251
+ /**
252
+ * Wipe persisted identity. Called by reset() — used when an end-user
253
+ * logs out. After reset the SDK mints a new anonymousId so the next
254
+ * pre-login session is a fresh customer in the identity graph.
255
+ */
256
+ reset() {
257
+ this.deleteBoth(this.prefix + KEY_ANON);
258
+ this.deleteBoth(this.prefix + KEY_CDCUST);
259
+ this.state = {
260
+ anonymousId: this.mintAnonymousId(),
261
+ crossdeckCustomerId: null
262
+ };
263
+ this.writeBoth(this.prefix + KEY_ANON, this.state.anonymousId);
264
+ }
265
+ /**
266
+ * Generate an anonymousId. Crockford-ish base32 timestamp + random
267
+ * suffix. Same shape Stripe / Segment / others use — sortable, log-
268
+ * friendly, no PII.
269
+ */
270
+ mintAnonymousId() {
271
+ const ts = Date.now().toString(36);
272
+ const rand = randomChars(10);
273
+ return `anon_${ts}${rand}`;
274
+ }
275
+ writeBoth(key, value) {
276
+ try {
277
+ this.primary.setItem(key, value);
278
+ } catch {
279
+ }
280
+ if (this.secondary) {
281
+ try {
282
+ this.secondary.setItem(key, value);
283
+ } catch {
284
+ }
285
+ }
286
+ }
287
+ deleteBoth(key) {
288
+ try {
289
+ this.primary.removeItem(key);
290
+ } catch {
291
+ }
292
+ if (this.secondary) {
293
+ try {
294
+ this.secondary.removeItem(key);
295
+ } catch {
296
+ }
297
+ }
298
+ }
299
+ };
300
+ function randomChars(count) {
301
+ const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
302
+ const out = [];
303
+ const cryptoApi = globalThis.crypto;
304
+ if (cryptoApi?.getRandomValues) {
305
+ const buf = new Uint8Array(count);
306
+ cryptoApi.getRandomValues(buf);
307
+ for (let i = 0; i < count; i++) {
308
+ out.push(alphabet[buf[i] % alphabet.length] ?? "0");
309
+ }
310
+ } else {
311
+ for (let i = 0; i < count; i++) {
312
+ out.push(alphabet[Math.floor(Math.random() * alphabet.length)] ?? "0");
313
+ }
314
+ }
315
+ return out.join("");
316
+ }
317
+
318
+ // src/entitlement-cache.ts
319
+ var EntitlementCache = class {
320
+ constructor() {
321
+ this.active = /* @__PURE__ */ new Set();
322
+ this.all = [];
323
+ this.lastUpdated = 0;
324
+ this.listeners = /* @__PURE__ */ new Set();
325
+ this.listenerErrorCount = 0;
326
+ }
327
+ /** Sync read — true iff the entitlement key is currently active. */
328
+ isEntitled(key) {
329
+ return this.active.has(key);
330
+ }
331
+ /** Full snapshot for callers that need source / validUntil details. */
332
+ list() {
333
+ return this.all.slice();
334
+ }
335
+ /** When the cache was last refreshed. 0 means "never". */
336
+ get freshness() {
337
+ return this.lastUpdated;
338
+ }
339
+ /**
340
+ * Cumulative count of listener invocations that threw. Listener errors
341
+ * are swallowed (a buggy consumer must not crash the SDK) but the
342
+ * counter lets diagnostics() surface "you have a broken subscriber"
343
+ * without putting the developer in a debug session.
344
+ */
345
+ get listenerErrors() {
346
+ return this.listenerErrorCount;
347
+ }
348
+ /**
349
+ * Replace the cache with a fresh server response. The backend already
350
+ * filters to active + env-matching, so we don't re-filter — just trust
351
+ * what we got.
352
+ *
353
+ * Fires listeners AFTER the mutation so each listener sees the new state.
354
+ */
355
+ setFromList(entitlements) {
356
+ this.all = entitlements.slice();
357
+ this.active = new Set(entitlements.filter((e) => e.isActive).map((e) => e.key));
358
+ this.lastUpdated = Date.now();
359
+ this.notify();
360
+ }
361
+ /**
362
+ * Wipe — used on reset() (logout). The SDK forgets everything until
363
+ * the next identify + read.
364
+ *
365
+ * Fires listeners so React/SwiftUI/etc bindings re-render to the
366
+ * logged-out state immediately.
367
+ */
368
+ clear() {
369
+ this.active.clear();
370
+ this.all = [];
371
+ this.lastUpdated = 0;
372
+ this.notify();
373
+ }
374
+ /**
375
+ * Subscribe to cache mutations. Returns an unsubscribe function.
376
+ *
377
+ * The listener is invoked AFTER setFromList() or clear() with the
378
+ * current snapshot. Throwing inside a listener is non-fatal — the
379
+ * error is swallowed and subsequent listeners still run.
380
+ *
381
+ * Used by `@cross-deck/web/react`'s `useEntitlement` hook to
382
+ * trigger re-renders when entitlements change.
383
+ */
384
+ subscribe(listener) {
385
+ this.listeners.add(listener);
386
+ let unsubscribed = false;
387
+ return () => {
388
+ if (unsubscribed) return;
389
+ unsubscribed = true;
390
+ this.listeners.delete(listener);
391
+ };
392
+ }
393
+ notify() {
394
+ if (this.listeners.size === 0) return;
395
+ const snapshot = this.all.slice();
396
+ const listenersSnapshot = [...this.listeners];
397
+ for (const listener of listenersSnapshot) {
398
+ try {
399
+ listener(snapshot);
400
+ } catch {
401
+ this.listenerErrorCount += 1;
402
+ }
403
+ }
404
+ }
405
+ };
406
+
407
+ // src/retry-policy.ts
408
+ var DEFAULT_BASE = 1e3;
409
+ var DEFAULT_MAX = 6e4;
410
+ var DEFAULT_FACTOR = 2;
411
+ var DEFAULT_WARN = 8;
412
+ function computeNextDelay(attempts, retryAfterMs, options = {}, random = Math.random) {
413
+ const base = options.baseMs ?? DEFAULT_BASE;
414
+ const max = options.maxMs ?? DEFAULT_MAX;
415
+ const factor = options.factor ?? DEFAULT_FACTOR;
416
+ const safeAttempts = Math.min(attempts, 30);
417
+ const ceiling = Math.min(max, base * Math.pow(factor, safeAttempts));
418
+ const jittered = ceiling * random();
419
+ if (retryAfterMs !== void 0 && retryAfterMs > jittered) {
420
+ return Math.min(max, retryAfterMs);
421
+ }
422
+ return Math.max(0, Math.round(jittered));
423
+ }
424
+ var RetryPolicy = class {
425
+ constructor(options = {}) {
426
+ this.options = options;
427
+ this.attempts = 0;
428
+ }
429
+ /** How many consecutive failures since the last success. */
430
+ get consecutiveFailures() {
431
+ return this.attempts;
432
+ }
433
+ /** Whether we've crossed the failuresBeforeWarn threshold. */
434
+ get isWarning() {
435
+ return this.attempts >= (this.options.failuresBeforeWarn ?? DEFAULT_WARN);
436
+ }
437
+ /** Schedule-time delay for the NEXT retry. Increments the counter. */
438
+ nextDelay(retryAfterMs, random = Math.random) {
439
+ const delay = computeNextDelay(this.attempts, retryAfterMs, this.options, random);
440
+ this.attempts += 1;
441
+ return delay;
442
+ }
443
+ /** Mark a successful flush — reset the counter. */
444
+ recordSuccess() {
445
+ this.attempts = 0;
446
+ }
447
+ };
448
+
449
+ // src/event-queue.ts
450
+ var HARD_BUFFER_CAP = 1e3;
451
+ var EventQueue = class {
452
+ constructor(cfg) {
453
+ this.cfg = cfg;
454
+ this.buffer = [];
455
+ this.dropped = 0;
456
+ this.inFlight = 0;
457
+ this.lastFlushAt = 0;
458
+ this.lastError = null;
459
+ this.cancelTimer = null;
460
+ this.firstFlushFired = false;
461
+ this.nextRetryAt = null;
462
+ this.retry = new RetryPolicy(cfg.retry ?? {});
463
+ this.persistent = cfg.persistentStore ?? null;
464
+ if (this.persistent) {
465
+ const restored = this.persistent.load();
466
+ if (restored.length > 0) {
467
+ if (restored.length > HARD_BUFFER_CAP) {
468
+ this.dropped += restored.length - HARD_BUFFER_CAP;
469
+ this.buffer = restored.slice(restored.length - HARD_BUFFER_CAP);
470
+ } else {
471
+ this.buffer = restored;
472
+ }
473
+ this.cfg.onBufferChange?.(this.buffer.length);
474
+ this.scheduleIdleFlush();
475
+ }
476
+ }
477
+ }
478
+ enqueue(event) {
479
+ this.buffer.push(event);
480
+ if (this.buffer.length > HARD_BUFFER_CAP) {
481
+ const overflow = this.buffer.length - HARD_BUFFER_CAP;
482
+ this.buffer.splice(0, overflow);
483
+ this.dropped += overflow;
484
+ this.cfg.onDrop?.(overflow);
485
+ }
486
+ this.cfg.onBufferChange?.(this.buffer.length);
487
+ this.persistent?.save(this.buffer);
488
+ if (this.buffer.length >= this.cfg.batchSize) {
489
+ void this.flush();
490
+ } else {
491
+ this.scheduleIdleFlush();
492
+ }
493
+ }
494
+ /**
495
+ * Flush the buffer to /v1/events. Resolves when the network call
496
+ * completes (success or failure). On failure, events stay in the
497
+ * buffer for the next scheduled retry.
498
+ *
499
+ * `options.keepalive` marks the underlying fetch as keepalive so the
500
+ * browser keeps the request alive past page unload. Use this for
501
+ * terminal flushes (pagehide / visibilitychange→hidden / beforeunload).
502
+ */
503
+ async flush(options = {}) {
504
+ if (this.buffer.length === 0) return null;
505
+ this.cancelTimerIfSet();
506
+ this.nextRetryAt = null;
507
+ const batch = this.buffer.splice(0);
508
+ const batchId = this.mintBatchId();
509
+ this.inFlight += batch.length;
510
+ this.persistent?.save(this.buffer);
511
+ this.cfg.onBufferChange?.(this.buffer.length);
512
+ try {
513
+ const env = this.cfg.envelope();
514
+ const result = await this.cfg.http.request("POST", "/events", {
515
+ body: {
516
+ // NorthStar §13.1 batch envelope. The backend validates these
517
+ // against the API-key-resolved app and rejects mismatches
518
+ // loudly (env_mismatch).
519
+ appId: env.appId,
520
+ environment: env.environment,
521
+ sdk: env.sdk,
522
+ events: batch
523
+ },
524
+ keepalive: options.keepalive === true,
525
+ idempotencyKey: batchId
526
+ });
527
+ this.lastFlushAt = Date.now();
528
+ this.lastError = null;
529
+ this.inFlight -= batch.length;
530
+ this.retry.recordSuccess();
531
+ this.persistent?.save(this.buffer);
532
+ if (!this.firstFlushFired) {
533
+ this.firstFlushFired = true;
534
+ this.cfg.onFirstFlushSuccess?.();
535
+ }
536
+ return result;
537
+ } catch (err) {
538
+ this.buffer.unshift(...batch);
539
+ this.inFlight -= batch.length;
540
+ const message = err instanceof Error ? err.message : String(err);
541
+ this.lastError = message;
542
+ this.persistent?.save(this.buffer);
543
+ this.cfg.onBufferChange?.(this.buffer.length);
544
+ const retryAfterMs = extractRetryAfterMs(err);
545
+ const delay = this.retry.nextDelay(retryAfterMs);
546
+ this.scheduleRetry(delay);
547
+ this.cfg.onRetryScheduled?.({
548
+ delayMs: delay,
549
+ consecutiveFailures: this.retry.consecutiveFailures,
550
+ retryAfterMs,
551
+ lastError: message
552
+ });
553
+ return null;
554
+ }
555
+ }
556
+ /** Cancel any pending timer and clear in-memory state. Wipes durable store too. */
557
+ reset() {
558
+ this.cancelTimerIfSet();
559
+ this.nextRetryAt = null;
560
+ this.buffer = [];
561
+ this.dropped = 0;
562
+ this.inFlight = 0;
563
+ this.lastError = null;
564
+ this.retry.recordSuccess();
565
+ this.persistent?.clear();
566
+ this.cfg.onBufferChange?.(0);
567
+ }
568
+ getStats() {
569
+ return {
570
+ buffered: this.buffer.length,
571
+ dropped: this.dropped,
572
+ inFlight: this.inFlight,
573
+ lastFlushAt: this.lastFlushAt,
574
+ lastError: this.lastError,
575
+ consecutiveFailures: this.retry.consecutiveFailures,
576
+ nextRetryAt: this.nextRetryAt
577
+ };
578
+ }
579
+ // ---------- internal scheduling ----------
580
+ scheduleIdleFlush() {
581
+ this.cancelTimerIfSet();
582
+ const sched = this.cfg.scheduler ?? defaultScheduler;
583
+ this.cancelTimer = sched(() => {
584
+ void this.flush();
585
+ }, this.cfg.intervalMs);
586
+ }
587
+ scheduleRetry(delayMs) {
588
+ this.cancelTimerIfSet();
589
+ this.nextRetryAt = Date.now() + delayMs;
590
+ const sched = this.cfg.scheduler ?? defaultScheduler;
591
+ this.cancelTimer = sched(() => {
592
+ void this.flush();
593
+ }, delayMs);
594
+ }
595
+ cancelTimerIfSet() {
596
+ if (this.cancelTimer) {
597
+ this.cancelTimer();
598
+ this.cancelTimer = null;
599
+ }
600
+ }
601
+ mintBatchId() {
602
+ return `batch_${Date.now().toString(36)}${randomChars(10)}`;
603
+ }
604
+ };
605
+ function extractRetryAfterMs(err) {
606
+ if (err && typeof err === "object" && "retryAfterMs" in err) {
607
+ const v = err.retryAfterMs;
608
+ return typeof v === "number" && Number.isFinite(v) && v >= 0 ? v : void 0;
609
+ }
610
+ return void 0;
611
+ }
612
+ function defaultScheduler(fn, ms) {
613
+ const id = setTimeout(fn, ms);
614
+ if (typeof id.unref === "function") {
615
+ try {
616
+ id.unref();
617
+ } catch {
618
+ }
619
+ }
620
+ return () => clearTimeout(id);
621
+ }
622
+
623
+ // src/event-storage.ts
624
+ var PersistentEventStore = class {
625
+ constructor(options) {
626
+ this.options = options;
627
+ this.writeScheduled = false;
628
+ // Pending events captured on the most recent write request. We keep
629
+ // the latest snapshot ref so a debounced write always picks up the
630
+ // freshest buffer state.
631
+ this.pendingSnapshot = null;
632
+ this.key = `${options.prefix}queue.v1`;
633
+ }
634
+ /**
635
+ * Read the persisted queue on boot. Returns an empty array (with no
636
+ * warning) when nothing is stored, the blob is malformed, or storage
637
+ * is unavailable. Caller is responsible for treating duplicates from
638
+ * the persisted queue as the SAME events (eventId-based dedup).
639
+ */
640
+ load() {
641
+ let raw;
642
+ try {
643
+ raw = this.options.storage.getItem(this.key);
644
+ } catch {
645
+ return [];
646
+ }
647
+ if (!raw) return [];
648
+ try {
649
+ const parsed = JSON.parse(raw);
650
+ if (!parsed || parsed.version !== 1 || !Array.isArray(parsed.events)) {
651
+ return [];
652
+ }
653
+ return parsed.events;
654
+ } catch {
655
+ return [];
656
+ }
657
+ }
658
+ /**
659
+ * Schedule a write of the current buffer. Debounced via microtask so
660
+ * a burst of enqueue() calls coalesces into one persistence write.
661
+ * Writes are best-effort: if storage throws (quota, private mode),
662
+ * we swallow and rely on the in-memory buffer.
663
+ */
664
+ save(snapshot) {
665
+ this.pendingSnapshot = snapshot.slice();
666
+ if (this.writeScheduled) return;
667
+ this.writeScheduled = true;
668
+ queueMicrotask(() => this.flushWrite());
669
+ }
670
+ /** Synchronous variant for terminal flushes (pagehide / beforeunload). */
671
+ saveSync(snapshot) {
672
+ this.pendingSnapshot = snapshot.slice();
673
+ this.flushWrite();
674
+ }
675
+ /** Wipe the persisted blob. Used by reset() (logout). */
676
+ clear() {
677
+ this.pendingSnapshot = null;
678
+ this.writeScheduled = false;
679
+ try {
680
+ this.options.storage.removeItem(this.key);
681
+ } catch {
682
+ }
683
+ }
684
+ flushWrite() {
685
+ this.writeScheduled = false;
686
+ const snapshot = this.pendingSnapshot;
687
+ this.pendingSnapshot = null;
688
+ if (snapshot === null) return;
689
+ if (snapshot.length === 0) {
690
+ try {
691
+ this.options.storage.removeItem(this.key);
692
+ } catch {
693
+ }
694
+ return;
695
+ }
696
+ const blob = { version: 1, events: snapshot };
697
+ try {
698
+ this.options.storage.setItem(this.key, JSON.stringify(blob));
699
+ } catch {
700
+ }
701
+ }
702
+ };
703
+
704
+ // src/storage.ts
705
+ var MemoryStorage = class {
706
+ constructor() {
707
+ this.store = /* @__PURE__ */ new Map();
708
+ }
709
+ getItem(key) {
710
+ return this.store.get(key) ?? null;
711
+ }
712
+ setItem(key, value) {
713
+ this.store.set(key, value);
714
+ }
715
+ removeItem(key) {
716
+ this.store.delete(key);
717
+ }
718
+ };
719
+ var CookieStorage = class {
720
+ constructor(options) {
721
+ this.maxAgeSec = options?.maxAgeSec ?? 63072e3;
722
+ this.secure = options?.secure ?? defaultSecure();
723
+ this.sameSite = options?.sameSite ?? "Lax";
724
+ }
725
+ getItem(key) {
726
+ if (!hasDocument()) return null;
727
+ const doc = globalThis.document;
728
+ const cookies = doc.cookie ? doc.cookie.split(/;\s*/) : [];
729
+ const prefix = encodeURIComponent(key) + "=";
730
+ for (const c of cookies) {
731
+ if (c.startsWith(prefix)) {
732
+ try {
733
+ return decodeURIComponent(c.slice(prefix.length));
734
+ } catch {
735
+ return null;
736
+ }
737
+ }
738
+ }
739
+ return null;
740
+ }
741
+ setItem(key, value) {
742
+ if (!hasDocument()) return;
743
+ const doc = globalThis.document;
744
+ const parts = [
745
+ `${encodeURIComponent(key)}=${encodeURIComponent(value)}`,
746
+ "Path=/",
747
+ `Max-Age=${this.maxAgeSec}`,
748
+ `SameSite=${this.sameSite}`
749
+ ];
750
+ if (this.secure) parts.push("Secure");
751
+ try {
752
+ doc.cookie = parts.join("; ");
753
+ } catch {
754
+ }
755
+ }
756
+ removeItem(key) {
757
+ if (!hasDocument()) return;
758
+ const doc = globalThis.document;
759
+ const parts = [
760
+ `${encodeURIComponent(key)}=`,
761
+ "Path=/",
762
+ "Max-Age=0",
763
+ `SameSite=${this.sameSite}`
764
+ ];
765
+ if (this.secure) parts.push("Secure");
766
+ try {
767
+ doc.cookie = parts.join("; ");
768
+ } catch {
769
+ }
770
+ }
771
+ };
772
+ function detectDefaultStorage() {
773
+ try {
774
+ const ls = globalThis.localStorage;
775
+ if (ls) {
776
+ const probe = "__crossdeck_probe__";
777
+ ls.setItem(probe, "1");
778
+ ls.removeItem(probe);
779
+ return ls;
780
+ }
781
+ } catch {
782
+ }
783
+ return new MemoryStorage();
784
+ }
785
+ function defaultSecure() {
786
+ try {
787
+ const loc = globalThis.location;
788
+ return loc?.protocol === "https:";
789
+ } catch {
790
+ return false;
791
+ }
792
+ }
793
+ function hasDocument() {
794
+ return typeof globalThis.document !== "undefined";
795
+ }
796
+
797
+ // src/device-info.ts
798
+ function isBrowser() {
799
+ return typeof globalThis.window !== "undefined" && typeof globalThis.document !== "undefined" && typeof globalThis.navigator !== "undefined";
800
+ }
801
+ function collectDeviceInfo(extra) {
802
+ const info = {};
803
+ if (extra?.appVersion) info.appVersion = extra.appVersion;
804
+ if (!isBrowser()) return info;
805
+ const w = globalThis.window;
806
+ const nav = globalThis.navigator;
807
+ const doc = globalThis.document;
808
+ try {
809
+ if (typeof nav.language === "string") info.locale = nav.language;
810
+ } catch {
811
+ }
812
+ try {
813
+ info.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
814
+ } catch {
815
+ }
816
+ try {
817
+ if (w.screen) {
818
+ info.screenWidth = w.screen.width;
819
+ info.screenHeight = w.screen.height;
820
+ }
821
+ info.viewportWidth = w.innerWidth;
822
+ info.viewportHeight = w.innerHeight;
823
+ info.devicePixelRatio = w.devicePixelRatio;
824
+ } catch {
825
+ }
826
+ try {
827
+ const ua = nav.userAgent ?? "";
828
+ const parsed = parseUserAgent(ua);
829
+ Object.assign(info, parsed);
830
+ } catch {
831
+ }
832
+ try {
833
+ const uaData = nav.userAgentData;
834
+ if (uaData?.platform && !info.os) info.os = uaData.platform;
835
+ if (uaData?.brands && !info.browser) {
836
+ const real = uaData.brands.find(
837
+ (b) => !/Not[ .;A]*Brand/i.test(b.brand) && !/Chromium/i.test(b.brand)
838
+ );
839
+ if (real) {
840
+ info.browser = real.brand;
841
+ info.browserVersion = real.version;
842
+ }
843
+ }
844
+ } catch {
845
+ }
846
+ void doc;
847
+ return info;
848
+ }
849
+ function parseUserAgent(ua) {
850
+ const out = {};
851
+ if (/iPad|iPhone|iPod/.test(ua)) {
852
+ out.os = "iOS";
853
+ const m = ua.match(/OS (\d+[._]\d+(?:[._]\d+)?)/);
854
+ if (m?.[1]) out.osVersion = m[1].replace(/_/g, ".");
855
+ } else if (/Android/.test(ua)) {
856
+ out.os = "Android";
857
+ const m = ua.match(/Android (\d+(?:\.\d+)*)/);
858
+ if (m?.[1]) out.osVersion = m[1];
859
+ } else if (/Windows/.test(ua)) {
860
+ out.os = "Windows";
861
+ const m = ua.match(/Windows NT (\d+\.\d+)/);
862
+ if (m?.[1]) out.osVersion = m[1];
863
+ } else if (/Mac OS X|Macintosh/.test(ua)) {
864
+ out.os = "macOS";
865
+ const m = ua.match(/Mac OS X (\d+[._]\d+(?:[._]\d+)?)/);
866
+ if (m?.[1]) out.osVersion = m[1].replace(/_/g, ".");
867
+ } else if (/Linux/.test(ua)) {
868
+ out.os = "Linux";
869
+ }
870
+ if (/Edg\/(\d+(?:\.\d+)*)/.test(ua)) {
871
+ out.browser = "Edge";
872
+ out.browserVersion = ua.match(/Edg\/(\d+(?:\.\d+)*)/)?.[1];
873
+ } else if (/Firefox\/(\d+(?:\.\d+)*)/.test(ua)) {
874
+ out.browser = "Firefox";
875
+ out.browserVersion = ua.match(/Firefox\/(\d+(?:\.\d+)*)/)?.[1];
876
+ } else if (/OPR\/(\d+(?:\.\d+)*)/.test(ua)) {
877
+ out.browser = "Opera";
878
+ out.browserVersion = ua.match(/OPR\/(\d+(?:\.\d+)*)/)?.[1];
879
+ } else if (/Chrome\/(\d+(?:\.\d+)*)/.test(ua)) {
880
+ out.browser = "Chrome";
881
+ out.browserVersion = ua.match(/Chrome\/(\d+(?:\.\d+)*)/)?.[1];
882
+ } else if (/Version\/(\d+(?:\.\d+)*).*Safari/.test(ua)) {
883
+ out.browser = "Safari";
884
+ out.browserVersion = ua.match(/Version\/(\d+(?:\.\d+)*)/)?.[1];
885
+ }
886
+ return out;
887
+ }
888
+
889
+ // src/auto-track.ts
890
+ var DEFAULT_AUTO_TRACK = {
891
+ sessions: true,
892
+ pageViews: true,
893
+ deviceInfo: true,
894
+ clicks: true,
895
+ webVitals: true,
896
+ errors: true
897
+ };
898
+ var SESSION_RESUME_THRESHOLD_MS = 30 * 60 * 1e3;
899
+ var EMPTY_ACQUISITION = {
900
+ utm_source: "",
901
+ utm_medium: "",
902
+ utm_campaign: "",
903
+ utm_content: "",
904
+ utm_term: "",
905
+ referrer: "",
906
+ gclid: "",
907
+ fbclid: "",
908
+ msclkid: "",
909
+ ttclid: "",
910
+ li_fat_id: "",
911
+ twclid: ""
912
+ };
913
+ var AutoTracker = class {
914
+ constructor(cfg, track) {
915
+ this.cfg = cfg;
916
+ this.track = track;
917
+ this.session = null;
918
+ this.cleanups = [];
919
+ /**
920
+ * Stable per-page-view identifier. Minted at every `page.viewed`
921
+ * emission and attached to every subsequent event until the next
922
+ * `page.viewed`. Lets dashboards correlate "user clicked X" to
923
+ * "user viewed page Y" without timestamp arithmetic — the canonical
924
+ * Mixpanel `$current_url` / Segment `pageId` pattern.
925
+ *
926
+ * Null until the first `page.viewed` fires (which happens at SDK
927
+ * install if `autoTrack.pageViews !== false`).
928
+ */
929
+ this.pageviewId = null;
930
+ }
931
+ install() {
932
+ if (!isBrowserSafe()) return;
933
+ if (this.cfg.sessions) this.installSessionTracking();
934
+ if (this.cfg.pageViews) this.installPageViewTracking();
935
+ if (this.cfg.clicks) this.installClickTracking();
936
+ }
937
+ uninstall() {
938
+ while (this.cleanups.length) {
939
+ const fn = this.cleanups.pop();
940
+ try {
941
+ fn?.();
942
+ } catch {
943
+ }
944
+ }
945
+ if (this.session && !this.session.endedSent) {
946
+ this.emitSessionEnd();
947
+ }
948
+ this.session = null;
949
+ }
950
+ /** Exposed for tests + consumers that want to reset the session manually. */
951
+ resetSession() {
952
+ if (this.session && !this.session.endedSent) this.emitSessionEnd();
953
+ this.session = this.startNewSession();
954
+ this.emitSessionStart();
955
+ }
956
+ /** Exposed for inspection/tests — returns the current sessionId (or null if not in a session). */
957
+ get currentSessionId() {
958
+ return this.session?.sessionId ?? null;
959
+ }
960
+ /** Stable per-page-view ID. Null before the first page.viewed has fired. */
961
+ get currentPageviewId() {
962
+ return this.pageviewId;
963
+ }
964
+ /**
965
+ * Per-session acquisition context — utm_* + referrer, captured once
966
+ * at session start. Returns empty strings when there's no session
967
+ * (Node, before init, after uninstall) so callers can spread without
968
+ * conditional logic. Bank-grade rule: capture once, attach to every
969
+ * event of the session, don't re-read on every track() (the URL
970
+ * changes via SPA pushState; the source-of-record is the URL we
971
+ * landed on).
972
+ */
973
+ get currentAcquisition() {
974
+ return this.session?.acquisition ?? EMPTY_ACQUISITION;
975
+ }
976
+ // ---------- sessions ----------
977
+ installSessionTracking() {
978
+ this.session = this.startNewSession();
979
+ this.emitSessionStart();
980
+ const onVisChange = () => {
981
+ if (!this.session) return;
982
+ const doc2 = globalThis.document;
983
+ if (doc2.visibilityState === "hidden") {
984
+ this.session.hiddenAt = Date.now();
985
+ } else if (doc2.visibilityState === "visible") {
986
+ const hiddenFor = this.session.hiddenAt ? Date.now() - this.session.hiddenAt : 0;
987
+ if (hiddenFor >= SESSION_RESUME_THRESHOLD_MS) {
988
+ this.emitSessionEnd();
989
+ this.session = this.startNewSession();
990
+ this.emitSessionStart();
991
+ } else {
992
+ this.session.hiddenAt = null;
993
+ }
994
+ }
995
+ };
996
+ const onPageHide = () => this.emitSessionEnd();
997
+ const w = globalThis.window;
998
+ const doc = globalThis.document;
999
+ doc.addEventListener("visibilitychange", onVisChange);
1000
+ w.addEventListener("pagehide", onPageHide);
1001
+ w.addEventListener("beforeunload", onPageHide);
1002
+ this.cleanups.push(() => {
1003
+ doc.removeEventListener("visibilitychange", onVisChange);
1004
+ w.removeEventListener("pagehide", onPageHide);
1005
+ w.removeEventListener("beforeunload", onPageHide);
1006
+ });
1007
+ }
1008
+ startNewSession() {
1009
+ return {
1010
+ sessionId: mintSessionId(),
1011
+ startedAt: Date.now(),
1012
+ hiddenAt: null,
1013
+ endedSent: false,
1014
+ acquisition: captureAcquisition()
1015
+ };
1016
+ }
1017
+ emitSessionStart() {
1018
+ if (!this.session) return;
1019
+ this.track("session.started", { sessionId: this.session.sessionId });
1020
+ }
1021
+ emitSessionEnd() {
1022
+ if (!this.session || this.session.endedSent) return;
1023
+ const duration = Date.now() - this.session.startedAt;
1024
+ this.track("session.ended", {
1025
+ sessionId: this.session.sessionId,
1026
+ durationMs: duration
1027
+ });
1028
+ this.session.endedSent = true;
1029
+ }
1030
+ // ---------- page views ----------
1031
+ installPageViewTracking() {
1032
+ const w = globalThis.window;
1033
+ const doc = globalThis.document;
1034
+ let lastFiredAt = 0;
1035
+ let lastFiredUrl = "";
1036
+ const DEDUP_WINDOW_MS = 250;
1037
+ const fire = (force = false) => {
1038
+ const loc = w.location;
1039
+ const url = loc.href;
1040
+ const now = Date.now();
1041
+ if (!force && url === lastFiredUrl && now - lastFiredAt < DEDUP_WINDOW_MS) return;
1042
+ lastFiredAt = now;
1043
+ lastFiredUrl = url;
1044
+ this.pageviewId = `pv_${Date.now().toString(36)}${randomChars(10)}`;
1045
+ this.track("page.viewed", {
1046
+ pageviewId: this.pageviewId,
1047
+ path: loc.pathname,
1048
+ url,
1049
+ search: loc.search || void 0,
1050
+ hash: loc.hash || void 0,
1051
+ title: doc.title,
1052
+ // referrer only on the first hit of the session — afterward it's
1053
+ // always our previous URL, which isn't useful.
1054
+ referrer: doc.referrer || void 0
1055
+ });
1056
+ };
1057
+ fire();
1058
+ const origPush = w.history.pushState;
1059
+ const origReplace = w.history.replaceState;
1060
+ function patchedPush(data, unused, url) {
1061
+ origPush.apply(this, [data, unused, url]);
1062
+ queueMicrotask(fire);
1063
+ }
1064
+ function patchedReplace(data, unused, url) {
1065
+ origReplace.apply(this, [data, unused, url]);
1066
+ queueMicrotask(fire);
1067
+ }
1068
+ w.history.pushState = patchedPush;
1069
+ w.history.replaceState = patchedReplace;
1070
+ const onPopState = () => fire(true);
1071
+ w.addEventListener("popstate", onPopState);
1072
+ this.cleanups.push(() => {
1073
+ if (w.history.pushState === patchedPush) {
1074
+ w.history.pushState = origPush;
1075
+ }
1076
+ if (w.history.replaceState === patchedReplace) {
1077
+ w.history.replaceState = origReplace;
1078
+ }
1079
+ w.removeEventListener("popstate", onPopState);
1080
+ });
1081
+ }
1082
+ // ---------- click autocapture ----------
1083
+ /**
1084
+ * Global click tracking — Mixpanel / Amplitude style autocapture.
1085
+ * Fires `element.clicked` for every interactive click with the
1086
+ * target element's selector path, text content, tag, href, data-*
1087
+ * attributes, and viewport coordinates. Powers the funnel /
1088
+ * attribution USP: "users who clicked X then converted within
1089
+ * 7 days." Default ON because behavioural attribution is the
1090
+ * core product promise.
1091
+ *
1092
+ * Privacy guardrails:
1093
+ * - Skip clicks ON inputs / textareas / selects (form interaction
1094
+ * isn't button telemetry; the dev should track form submits
1095
+ * deliberately via track('form_submitted'))
1096
+ * - Skip clicks INSIDE [type="password"] and password-class
1097
+ * elements
1098
+ * - Skip clicks inside elements opted out via class="cd-noTrack"
1099
+ * or data-cd-noTrack attribute (Mixpanel's exact opt-out
1100
+ * idiom — most devs already know it)
1101
+ * - Capture text content but cap at 64 chars and trim — never
1102
+ * more than what you'd see on a button label
1103
+ *
1104
+ * Volume guardrails:
1105
+ * - Coalesce double-clicks within 100ms (React's synthetic click
1106
+ * pattern + browser's native dblclick can fire twice)
1107
+ * - Listen on document at capture phase so we see the click
1108
+ * before any framework's own handlers stop propagation
1109
+ */
1110
+ installClickTracking() {
1111
+ const w = globalThis.window;
1112
+ const doc = globalThis.document;
1113
+ let lastFiredAt = 0;
1114
+ let lastFiredTarget = null;
1115
+ const COALESCE_MS = 100;
1116
+ const TEXT_CAP = 64;
1117
+ const onClick = (ev) => {
1118
+ const target = ev.target;
1119
+ if (!target || !(target instanceof Element)) return;
1120
+ const now = Date.now();
1121
+ if (target === lastFiredTarget && now - lastFiredAt < COALESCE_MS) return;
1122
+ lastFiredAt = now;
1123
+ lastFiredTarget = target;
1124
+ const actionable = closestActionable(target);
1125
+ const clicked = actionable || target;
1126
+ if (isFormInput(clicked)) return;
1127
+ if (isInOptedOut(clicked)) return;
1128
+ if (isInsidePasswordField(clicked)) return;
1129
+ const tag = clicked.tagName.toLowerCase();
1130
+ const text = trimText(extractText(clicked), TEXT_CAP);
1131
+ const href = clicked.href || void 0;
1132
+ const linkTarget = clicked.target || void 0;
1133
+ const elementId = clicked.id || void 0;
1134
+ const role = clicked.getAttribute("role") || void 0;
1135
+ const ariaLabel = clicked.getAttribute("aria-label") || void 0;
1136
+ const selector = buildSelector(clicked);
1137
+ const dataAttrs = collectDataAttrs(clicked);
1138
+ const isLink = tag === "a" && !!href;
1139
+ const explicitName = clicked.getAttribute("data-cd-event");
1140
+ const props = {
1141
+ selector,
1142
+ tag,
1143
+ text,
1144
+ elementId,
1145
+ role,
1146
+ ariaLabel,
1147
+ href,
1148
+ isLink,
1149
+ linkTarget,
1150
+ viewportX: ev.clientX,
1151
+ viewportY: ev.clientY,
1152
+ pageX: ev.pageX,
1153
+ pageY: ev.pageY,
1154
+ ...dataAttrs
1155
+ };
1156
+ for (const k of Object.keys(props)) {
1157
+ if (props[k] === void 0 || props[k] === null || props[k] === "") delete props[k];
1158
+ }
1159
+ this.track(explicitName || "element.clicked", props);
1160
+ };
1161
+ doc.addEventListener("click", onClick, { capture: true, passive: true });
1162
+ this.cleanups.push(() => {
1163
+ doc.removeEventListener("click", onClick, { capture: true });
1164
+ });
1165
+ }
1166
+ };
1167
+ function closestActionable(el) {
1168
+ return el.closest("[data-cd-event]") || el.closest("[data-cd-noTrack]") || el.closest("button, a, [role='button'], [role='link'], input[type='button'], input[type='submit']") || null;
1169
+ }
1170
+ function isFormInput(el) {
1171
+ if (!(el instanceof HTMLElement)) return false;
1172
+ const tag = el.tagName.toLowerCase();
1173
+ if (tag === "textarea" || tag === "select") return true;
1174
+ if (tag === "input") {
1175
+ const type = (el.type || "").toLowerCase();
1176
+ return type !== "button" && type !== "submit" && type !== "image" && type !== "reset";
1177
+ }
1178
+ return false;
1179
+ }
1180
+ function isInOptedOut(el) {
1181
+ if (el.closest("[data-cd-noTrack], [data-cd-no-track], .cd-noTrack, .cd-no-track")) return true;
1182
+ return false;
1183
+ }
1184
+ function isInsidePasswordField(el) {
1185
+ if (el.closest('input[type="password"]')) return true;
1186
+ return false;
1187
+ }
1188
+ function extractText(el) {
1189
+ const aria = el.getAttribute("aria-label");
1190
+ if (aria) return aria.replace(/\s+/g, " ").trim();
1191
+ if (el instanceof HTMLInputElement && el.value) return el.value;
1192
+ const text = (el.textContent || "").replace(/\s+/g, " ").trim();
1193
+ return text;
1194
+ }
1195
+ function trimText(s, cap) {
1196
+ if (s.length <= cap) return s;
1197
+ return s.slice(0, cap - 1) + "\u2026";
1198
+ }
1199
+ function buildSelector(el) {
1200
+ const parts = [];
1201
+ let cur = el;
1202
+ let depth = 0;
1203
+ while (cur && cur.nodeName.toLowerCase() !== "body" && depth < 5) {
1204
+ let part = cur.nodeName.toLowerCase();
1205
+ if (cur.id) {
1206
+ parts.unshift(`${part}#${cur.id}`);
1207
+ break;
1208
+ }
1209
+ if (cur.classList.length > 0) {
1210
+ const cls = Array.from(cur.classList).filter((c) => !c.startsWith("cd-")).slice(0, 2).join(".");
1211
+ if (cls) part += `.${cls}`;
1212
+ }
1213
+ parts.unshift(part);
1214
+ cur = cur.parentElement;
1215
+ depth++;
1216
+ }
1217
+ return parts.join(" > ");
1218
+ }
1219
+ function collectDataAttrs(el) {
1220
+ const out = {};
1221
+ if (!(el instanceof HTMLElement)) return out;
1222
+ for (const name of el.getAttributeNames()) {
1223
+ if (!name.startsWith("data-")) continue;
1224
+ if (name === "data-cd-noTrack" || name === "data-cd-no-track") continue;
1225
+ if (name === "data-cd-event") continue;
1226
+ const value = el.getAttribute(name) || "";
1227
+ const key = name.replace(/^data-cd-prop-/, "").replace(/^data-/, "");
1228
+ out[key] = value;
1229
+ }
1230
+ return out;
1231
+ }
1232
+ function isBrowserSafe() {
1233
+ return typeof globalThis.window !== "undefined" && typeof globalThis.document !== "undefined";
1234
+ }
1235
+ function mintSessionId() {
1236
+ const ts = Date.now().toString(36);
1237
+ return `sess_${ts}${randomChars(10)}`;
1238
+ }
1239
+ function captureAcquisition() {
1240
+ if (!isBrowserSafe()) return { ...EMPTY_ACQUISITION };
1241
+ const result = { ...EMPTY_ACQUISITION };
1242
+ try {
1243
+ const w = globalThis.window;
1244
+ const params = new URLSearchParams(w.location.search ?? "");
1245
+ result.utm_source = params.get("utm_source") ?? "";
1246
+ result.utm_medium = params.get("utm_medium") ?? "";
1247
+ result.utm_campaign = params.get("utm_campaign") ?? "";
1248
+ result.utm_content = params.get("utm_content") ?? "";
1249
+ result.utm_term = params.get("utm_term") ?? "";
1250
+ result.gclid = params.get("gclid") ?? "";
1251
+ result.fbclid = params.get("fbclid") ?? "";
1252
+ result.msclkid = params.get("msclkid") ?? "";
1253
+ result.ttclid = params.get("ttclid") ?? "";
1254
+ result.li_fat_id = params.get("li_fat_id") ?? "";
1255
+ result.twclid = params.get("twclid") ?? "";
1256
+ } catch {
1257
+ }
1258
+ try {
1259
+ const doc = globalThis.document;
1260
+ if (typeof doc.referrer === "string") result.referrer = doc.referrer;
1261
+ } catch {
1262
+ }
1263
+ return result;
1264
+ }
1265
+
1266
+ // src/debug.ts
1267
+ var SENSITIVE_KEY_PATTERNS = [
1268
+ /^email$/i,
1269
+ /^password$/i,
1270
+ /^token$/i,
1271
+ /^secret$/i,
1272
+ /^card$/i,
1273
+ /^phone$/i,
1274
+ /password/i,
1275
+ /credit_?card/i
1276
+ ];
1277
+ function findSensitivePropertyKeys(properties) {
1278
+ if (!properties) return [];
1279
+ const hits = [];
1280
+ for (const k of Object.keys(properties)) {
1281
+ if (SENSITIVE_KEY_PATTERNS.some((re) => re.test(k))) hits.push(k);
1282
+ }
1283
+ return hits;
1284
+ }
1285
+ var ConsoleDebugLogger = class {
1286
+ constructor() {
1287
+ this.enabled = false;
1288
+ this.seen = /* @__PURE__ */ new Set();
1289
+ }
1290
+ emit(signal, message, context) {
1291
+ if (!this.enabled) return;
1292
+ if (ONCE_SIGNALS.has(signal)) {
1293
+ if (this.seen.has(signal)) return;
1294
+ this.seen.add(signal);
1295
+ }
1296
+ const ctx = context ? ` ${safeJson(context)}` : "";
1297
+ console.info(`[crossdeck:${signal}] ${message}${ctx}`);
1298
+ }
1299
+ };
1300
+ var ONCE_SIGNALS = /* @__PURE__ */ new Set([
1301
+ "sdk.configured",
1302
+ "sdk.first_event_sent",
1303
+ "sdk.environment_mismatch"
1304
+ ]);
1305
+ function safeJson(obj) {
1306
+ try {
1307
+ return JSON.stringify(obj);
1308
+ } catch {
1309
+ return "[unserialisable context]";
1310
+ }
1311
+ }
1312
+
1313
+ // src/event-validation.ts
1314
+ var DEFAULT_MAX_STRING = 1024;
1315
+ var DEFAULT_MAX_BYTES = 8 * 1024;
1316
+ var DEFAULT_MAX_DEPTH = 5;
1317
+ function validateEventProperties(input, options = {}) {
1318
+ const warnings = [];
1319
+ if (!input) return { properties: {}, warnings };
1320
+ const maxStringLength = options.maxStringLength ?? DEFAULT_MAX_STRING;
1321
+ const maxBatchPropertyBytes = options.maxBatchPropertyBytes ?? DEFAULT_MAX_BYTES;
1322
+ const maxDepth = options.maxDepth ?? DEFAULT_MAX_DEPTH;
1323
+ const seen = /* @__PURE__ */ new WeakSet();
1324
+ const visit = (value, key, depth) => {
1325
+ if (depth > maxDepth) {
1326
+ warnings.push({ kind: "depth_exceeded", key });
1327
+ return { keep: true, value: "[depth-exceeded]" };
1328
+ }
1329
+ if (value === null) return { keep: true, value: null };
1330
+ const t = typeof value;
1331
+ if (t === "string") {
1332
+ const s = value;
1333
+ if (s.length > maxStringLength) {
1334
+ warnings.push({ kind: "truncated_string", key });
1335
+ return { keep: true, value: s.slice(0, maxStringLength - 1) + "\u2026" };
1336
+ }
1337
+ return { keep: true, value: s };
1338
+ }
1339
+ if (t === "number") {
1340
+ if (!Number.isFinite(value)) {
1341
+ warnings.push({ kind: "non_serialisable", key });
1342
+ return { keep: true, value: null };
1343
+ }
1344
+ return { keep: true, value };
1345
+ }
1346
+ if (t === "boolean") return { keep: true, value };
1347
+ if (t === "bigint") {
1348
+ warnings.push({ kind: "coerced_bigint", key });
1349
+ return { keep: true, value: value.toString() };
1350
+ }
1351
+ if (t === "function") {
1352
+ warnings.push({ kind: "dropped_function", key });
1353
+ return { keep: false, value: void 0 };
1354
+ }
1355
+ if (t === "symbol") {
1356
+ warnings.push({ kind: "dropped_symbol", key });
1357
+ return { keep: false, value: void 0 };
1358
+ }
1359
+ if (t === "undefined") {
1360
+ warnings.push({ kind: "dropped_undefined", key });
1361
+ return { keep: false, value: void 0 };
1362
+ }
1363
+ if (value instanceof Date) {
1364
+ warnings.push({ kind: "coerced_date", key });
1365
+ const iso = Number.isFinite(value.getTime()) ? value.toISOString() : null;
1366
+ return { keep: true, value: iso };
1367
+ }
1368
+ if (value instanceof Error) {
1369
+ warnings.push({ kind: "coerced_error", key });
1370
+ return {
1371
+ keep: true,
1372
+ value: {
1373
+ name: value.name,
1374
+ message: value.message,
1375
+ stack: typeof value.stack === "string" ? value.stack.slice(0, maxStringLength) : void 0
1376
+ }
1377
+ };
1378
+ }
1379
+ if (value instanceof Map) {
1380
+ warnings.push({ kind: "coerced_map", key });
1381
+ const obj = {};
1382
+ for (const [k, v] of value.entries()) {
1383
+ const subKey = typeof k === "string" ? k : String(k);
1384
+ const result = visit(v, `${key}.${subKey}`, depth + 1);
1385
+ if (result.keep) obj[subKey] = result.value;
1386
+ }
1387
+ return { keep: true, value: obj };
1388
+ }
1389
+ if (value instanceof Set) {
1390
+ warnings.push({ kind: "coerced_set", key });
1391
+ const arr = [];
1392
+ let i = 0;
1393
+ for (const v of value.values()) {
1394
+ const result = visit(v, `${key}[${i}]`, depth + 1);
1395
+ if (result.keep) arr.push(result.value);
1396
+ i++;
1397
+ }
1398
+ return { keep: true, value: arr };
1399
+ }
1400
+ if (Array.isArray(value)) {
1401
+ if (seen.has(value)) {
1402
+ warnings.push({ kind: "circular_reference", key });
1403
+ return { keep: true, value: "[circular]" };
1404
+ }
1405
+ seen.add(value);
1406
+ const out = [];
1407
+ for (let i = 0; i < value.length; i++) {
1408
+ const result = visit(value[i], `${key}[${i}]`, depth + 1);
1409
+ if (result.keep) out.push(result.value);
1410
+ }
1411
+ return { keep: true, value: out };
1412
+ }
1413
+ if (t === "object") {
1414
+ const obj = value;
1415
+ if (seen.has(obj)) {
1416
+ warnings.push({ kind: "circular_reference", key });
1417
+ return { keep: true, value: "[circular]" };
1418
+ }
1419
+ seen.add(obj);
1420
+ const out = {};
1421
+ for (const k of Object.keys(obj)) {
1422
+ const result = visit(obj[k], `${key}.${k}`, depth + 1);
1423
+ if (result.keep) out[k] = result.value;
1424
+ }
1425
+ return { keep: true, value: out };
1426
+ }
1427
+ warnings.push({ kind: "non_serialisable", key });
1428
+ try {
1429
+ return { keep: true, value: String(value) };
1430
+ } catch {
1431
+ return { keep: false, value: void 0 };
1432
+ }
1433
+ };
1434
+ const cleaned = {};
1435
+ for (const k of Object.keys(input)) {
1436
+ const result = visit(input[k], k, 0);
1437
+ if (result.keep) cleaned[k] = result.value;
1438
+ }
1439
+ const serialised = safeStringify(cleaned);
1440
+ if (serialised && byteLength(serialised) > maxBatchPropertyBytes) {
1441
+ warnings.push({ kind: "size_cap_exceeded", key: "*" });
1442
+ const sizes = Object.keys(cleaned).map((k) => ({ k, size: byteLength(safeStringify(cleaned[k]) ?? "") })).sort((a, b) => b.size - a.size);
1443
+ let currentSize = byteLength(serialised);
1444
+ for (const { k } of sizes) {
1445
+ if (currentSize <= maxBatchPropertyBytes) break;
1446
+ currentSize -= sizes.find((s) => s.k === k).size;
1447
+ delete cleaned[k];
1448
+ }
1449
+ cleaned.__truncated = true;
1450
+ }
1451
+ return { properties: cleaned, warnings };
1452
+ }
1453
+ function safeStringify(v) {
1454
+ try {
1455
+ return JSON.stringify(v) ?? null;
1456
+ } catch {
1457
+ return null;
1458
+ }
1459
+ }
1460
+ function byteLength(s) {
1461
+ if (typeof TextEncoder !== "undefined") {
1462
+ return new TextEncoder().encode(s).length;
1463
+ }
1464
+ return s.length * 4;
1465
+ }
1466
+
1467
+ // src/super-properties.ts
1468
+ var KEY_SUPER = "super_props";
1469
+ var KEY_GROUPS = "groups";
1470
+ var SuperPropertyStore = class {
1471
+ constructor(storage, prefix) {
1472
+ this.storage = storage;
1473
+ this.prefix = prefix;
1474
+ this.superProps = {};
1475
+ this.groups = {};
1476
+ this.superProps = readJson(storage, prefix + KEY_SUPER) ?? {};
1477
+ this.groups = readJson(storage, prefix + KEY_GROUPS) ?? {};
1478
+ }
1479
+ // ---------- super properties ----------
1480
+ /**
1481
+ * Merge new keys into the super-property bag. Returns a snapshot of
1482
+ * the resulting bag. Values that are `null` are deleted (Mixpanel
1483
+ * semantics — explicit null = "stop tracking this key").
1484
+ */
1485
+ register(props) {
1486
+ for (const [k, v] of Object.entries(props)) {
1487
+ if (v === null) {
1488
+ delete this.superProps[k];
1489
+ } else if (v !== void 0) {
1490
+ this.superProps[k] = v;
1491
+ }
1492
+ }
1493
+ writeJson(this.storage, this.prefix + KEY_SUPER, this.superProps);
1494
+ return { ...this.superProps };
1495
+ }
1496
+ /** Remove a single super-property key. Idempotent. */
1497
+ unregister(key) {
1498
+ if (key in this.superProps) {
1499
+ delete this.superProps[key];
1500
+ writeJson(this.storage, this.prefix + KEY_SUPER, this.superProps);
1501
+ }
1502
+ }
1503
+ /** Snapshot of the current super-property bag. */
1504
+ getSuperProperties() {
1505
+ return { ...this.superProps };
1506
+ }
1507
+ // ---------- groups ----------
1508
+ /**
1509
+ * Set a group membership. Passing `id: null` clears the membership
1510
+ * for that group type — the SDK stops attaching it to events.
1511
+ */
1512
+ setGroup(type, id, traits) {
1513
+ if (id === null) {
1514
+ delete this.groups[type];
1515
+ } else {
1516
+ this.groups[type] = traits !== void 0 ? { id, traits } : { id };
1517
+ }
1518
+ writeJson(this.storage, this.prefix + KEY_GROUPS, this.groups);
1519
+ }
1520
+ /**
1521
+ * Snapshot of the current groups map, keyed by group type. Returned
1522
+ * shape mirrors what the SDK attaches to every event as
1523
+ * `$groups.{type}`. The `traits` sub-object is the most-recent
1524
+ * traits payload passed to `setGroup` for that type; null when none.
1525
+ */
1526
+ getGroups() {
1527
+ return JSON.parse(JSON.stringify(this.groups));
1528
+ }
1529
+ /**
1530
+ * The flat `{ type: id }` projection used for event-attachment. Stable
1531
+ * for fast every-event merge — we don't want to JSON-clone on each
1532
+ * track() call.
1533
+ */
1534
+ getGroupIds() {
1535
+ const out = {};
1536
+ for (const [type, info] of Object.entries(this.groups)) {
1537
+ out[type] = info.id;
1538
+ }
1539
+ return out;
1540
+ }
1541
+ /** Wipe both bags. Called by Crossdeck.reset() (logout). */
1542
+ clear() {
1543
+ this.superProps = {};
1544
+ this.groups = {};
1545
+ try {
1546
+ this.storage.removeItem(this.prefix + KEY_SUPER);
1547
+ } catch {
1548
+ }
1549
+ try {
1550
+ this.storage.removeItem(this.prefix + KEY_GROUPS);
1551
+ } catch {
1552
+ }
1553
+ }
1554
+ };
1555
+ function readJson(storage, key) {
1556
+ let raw;
1557
+ try {
1558
+ raw = storage.getItem(key);
1559
+ } catch {
1560
+ return null;
1561
+ }
1562
+ if (!raw) return null;
1563
+ try {
1564
+ return JSON.parse(raw);
1565
+ } catch {
1566
+ return null;
1567
+ }
1568
+ }
1569
+ function writeJson(storage, key, value) {
1570
+ try {
1571
+ storage.setItem(key, JSON.stringify(value));
1572
+ } catch {
1573
+ }
1574
+ }
1575
+
1576
+ // src/web-vitals.ts
1577
+ var WebVitalsTracker = class {
1578
+ constructor(cfg, report) {
1579
+ this.cfg = cfg;
1580
+ this.report = report;
1581
+ this.observers = [];
1582
+ this.flushed = /* @__PURE__ */ new Set();
1583
+ this.cls = 0;
1584
+ this.clsEntries = [];
1585
+ this.inp = 0;
1586
+ this.cleanups = [];
1587
+ }
1588
+ install() {
1589
+ if (!this.cfg.enabled) return;
1590
+ if (typeof PerformanceObserver === "undefined") return;
1591
+ if (typeof globalThis === "undefined" || !("document" in globalThis)) return;
1592
+ const doc = globalThis.document;
1593
+ try {
1594
+ const navObserver = new PerformanceObserver((list) => {
1595
+ for (const entry of list.getEntries()) {
1596
+ const e = entry;
1597
+ if (e.responseStart > 0 && !this.flushed.has("ttfb")) {
1598
+ this.flushed.add("ttfb");
1599
+ this.report("webvitals.ttfb", { valueMs: Math.round(e.responseStart - e.startTime) });
1600
+ }
1601
+ }
1602
+ });
1603
+ navObserver.observe({ type: "navigation", buffered: true });
1604
+ this.observers.push(navObserver);
1605
+ } catch {
1606
+ }
1607
+ try {
1608
+ const paintObserver = new PerformanceObserver((list) => {
1609
+ for (const entry of list.getEntries()) {
1610
+ if (entry.name === "first-contentful-paint" && !this.flushed.has("fcp")) {
1611
+ this.flushed.add("fcp");
1612
+ this.report("webvitals.fcp", { valueMs: Math.round(entry.startTime) });
1613
+ }
1614
+ }
1615
+ });
1616
+ paintObserver.observe({ type: "paint", buffered: true });
1617
+ this.observers.push(paintObserver);
1618
+ } catch {
1619
+ }
1620
+ let lcpValue = 0;
1621
+ try {
1622
+ const lcpObserver = new PerformanceObserver((list) => {
1623
+ const entries = list.getEntries();
1624
+ const last = entries[entries.length - 1];
1625
+ if (last) lcpValue = last.startTime;
1626
+ });
1627
+ lcpObserver.observe({ type: "largest-contentful-paint", buffered: true });
1628
+ this.observers.push(lcpObserver);
1629
+ } catch {
1630
+ }
1631
+ try {
1632
+ const clsObserver = new PerformanceObserver((list) => {
1633
+ for (const entry of list.getEntries()) {
1634
+ const e = entry;
1635
+ if (typeof e.value === "number" && !e.hadRecentInput) {
1636
+ this.cls += e.value;
1637
+ this.clsEntries.push(entry);
1638
+ }
1639
+ }
1640
+ });
1641
+ clsObserver.observe({ type: "layout-shift", buffered: true });
1642
+ this.observers.push(clsObserver);
1643
+ } catch {
1644
+ }
1645
+ try {
1646
+ const eventObserver = new PerformanceObserver((list) => {
1647
+ for (const entry of list.getEntries()) {
1648
+ const e = entry;
1649
+ if (e.interactionId && e.duration > this.inp) {
1650
+ this.inp = e.duration;
1651
+ }
1652
+ }
1653
+ });
1654
+ try {
1655
+ eventObserver.observe({ type: "event", buffered: true, durationThreshold: 16 });
1656
+ } catch {
1657
+ eventObserver.observe({ type: "first-input", buffered: true });
1658
+ }
1659
+ this.observers.push(eventObserver);
1660
+ } catch {
1661
+ }
1662
+ const flush = () => {
1663
+ if (lcpValue > 0 && !this.flushed.has("lcp")) {
1664
+ this.flushed.add("lcp");
1665
+ this.report("webvitals.lcp", { valueMs: Math.round(lcpValue) });
1666
+ }
1667
+ if (this.cls > 0 && !this.flushed.has("cls")) {
1668
+ this.flushed.add("cls");
1669
+ this.report("webvitals.cls", { value: Math.round(this.cls * 1e3) / 1e3 });
1670
+ }
1671
+ if (this.inp > 0 && !this.flushed.has("inp")) {
1672
+ this.flushed.add("inp");
1673
+ this.report("webvitals.inp", { valueMs: Math.round(this.inp) });
1674
+ }
1675
+ };
1676
+ const onHidden = () => {
1677
+ if (doc.visibilityState === "hidden") flush();
1678
+ };
1679
+ doc.addEventListener("visibilitychange", onHidden);
1680
+ globalThis.window.addEventListener("pagehide", flush);
1681
+ this.cleanups.push(() => {
1682
+ doc.removeEventListener("visibilitychange", onHidden);
1683
+ globalThis.window.removeEventListener("pagehide", flush);
1684
+ });
1685
+ }
1686
+ uninstall() {
1687
+ for (const o of this.observers) {
1688
+ try {
1689
+ o.disconnect();
1690
+ } catch {
1691
+ }
1692
+ }
1693
+ this.observers = [];
1694
+ for (const fn of this.cleanups.splice(0)) {
1695
+ try {
1696
+ fn();
1697
+ } catch {
1698
+ }
1699
+ }
1700
+ }
1701
+ };
1702
+
1703
+ // src/consent.ts
1704
+ var ALL_GRANTED = {
1705
+ analytics: true,
1706
+ marketing: true,
1707
+ errors: true
1708
+ };
1709
+ var ConsentManager = class {
1710
+ constructor(options) {
1711
+ this.state = { ...ALL_GRANTED };
1712
+ this.dntDenied = false;
1713
+ if (options?.respectDnt && this.detectDnt()) {
1714
+ this.dntDenied = true;
1715
+ this.state = { analytics: false, marketing: false, errors: false };
1716
+ }
1717
+ }
1718
+ /**
1719
+ * Merge new dimensions onto the current state. Returns the resulting
1720
+ * snapshot. DNT-derived denies cannot be flipped back on by a `set`
1721
+ * call — once the browser says "don't track", we don't track even if
1722
+ * the developer code disagrees. That's the contract.
1723
+ */
1724
+ set(partial) {
1725
+ if (this.dntDenied) return { ...this.state };
1726
+ for (const k of Object.keys(partial)) {
1727
+ const v = partial[k];
1728
+ if (typeof v === "boolean") this.state[k] = v;
1729
+ }
1730
+ return { ...this.state };
1731
+ }
1732
+ /** Snapshot of the current state. */
1733
+ get() {
1734
+ return { ...this.state };
1735
+ }
1736
+ /** Convenience getters for hot paths. */
1737
+ get analytics() {
1738
+ return this.state.analytics;
1739
+ }
1740
+ get marketing() {
1741
+ return this.state.marketing;
1742
+ }
1743
+ get errors() {
1744
+ return this.state.errors;
1745
+ }
1746
+ /** True iff the constructor detected and applied DNT. */
1747
+ get isDntDenied() {
1748
+ return this.dntDenied;
1749
+ }
1750
+ detectDnt() {
1751
+ try {
1752
+ const nav = globalThis.navigator;
1753
+ if (!nav) return false;
1754
+ const sources = [
1755
+ nav.doNotTrack,
1756
+ nav.msDoNotTrack,
1757
+ globalThis.doNotTrack
1758
+ ];
1759
+ return sources.some((v) => v === "1" || v === "yes");
1760
+ } catch {
1761
+ return false;
1762
+ }
1763
+ }
1764
+ };
1765
+ var EMAIL_PATTERN = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;
1766
+ var CARD_PATTERN = /\b\d(?:[ -]?\d){12,18}\b/g;
1767
+ var REPLACEMENT_EMAIL = "[email]";
1768
+ var REPLACEMENT_CARD = "[card]";
1769
+ function scrubPii(value) {
1770
+ if (!value) return value;
1771
+ let out = value;
1772
+ if (EMAIL_PATTERN.test(out)) {
1773
+ out = out.replace(EMAIL_PATTERN, REPLACEMENT_EMAIL);
1774
+ }
1775
+ EMAIL_PATTERN.lastIndex = 0;
1776
+ if (CARD_PATTERN.test(out)) {
1777
+ out = out.replace(CARD_PATTERN, REPLACEMENT_CARD);
1778
+ }
1779
+ CARD_PATTERN.lastIndex = 0;
1780
+ return out;
1781
+ }
1782
+ function scrubPiiFromProperties(properties) {
1783
+ const out = {};
1784
+ for (const k of Object.keys(properties)) {
1785
+ const v = properties[k];
1786
+ if (typeof v === "string") {
1787
+ out[k] = scrubPii(v);
1788
+ } else if (Array.isArray(v)) {
1789
+ out[k] = v.map((item) => typeof item === "string" ? scrubPii(item) : item);
1790
+ } else {
1791
+ out[k] = v;
1792
+ }
1793
+ }
1794
+ return out;
1795
+ }
1796
+
1797
+ // src/breadcrumbs.ts
1798
+ var BreadcrumbBuffer = class {
1799
+ constructor(maxSize = 50) {
1800
+ this.maxSize = maxSize;
1801
+ this.items = [];
1802
+ }
1803
+ add(crumb) {
1804
+ this.items.push(crumb);
1805
+ if (this.items.length > this.maxSize) {
1806
+ this.items.shift();
1807
+ }
1808
+ }
1809
+ /** Defensive copy — caller can read freely without mutating buffer state. */
1810
+ snapshot() {
1811
+ return this.items.slice();
1812
+ }
1813
+ clear() {
1814
+ this.items = [];
1815
+ }
1816
+ get size() {
1817
+ return this.items.length;
1818
+ }
1819
+ };
1820
+
1821
+ // src/stack-parser.ts
1822
+ function parseStack(stack) {
1823
+ if (!stack || typeof stack !== "string") return [];
1824
+ const lines = stack.split("\n");
1825
+ const frames = [];
1826
+ for (const line of lines) {
1827
+ const trimmed = line.trim();
1828
+ if (!trimmed) continue;
1829
+ const frame = parseLine(trimmed);
1830
+ if (frame) frames.push(frame);
1831
+ }
1832
+ return frames;
1833
+ }
1834
+ function parseLine(line) {
1835
+ let m = /^at\s+(.+?)\s+\((.+?):(\d+):(\d+)\)$/.exec(line);
1836
+ if (m) {
1837
+ return buildFrame({
1838
+ function: m[1],
1839
+ filename: m[2],
1840
+ lineno: parseInt(m[3], 10),
1841
+ colno: parseInt(m[4], 10),
1842
+ raw: line
1843
+ });
1844
+ }
1845
+ m = /^at\s+(.+?):(\d+):(\d+)$/.exec(line);
1846
+ if (m) {
1847
+ return buildFrame({
1848
+ function: "?",
1849
+ filename: m[1],
1850
+ lineno: parseInt(m[2], 10),
1851
+ colno: parseInt(m[3], 10),
1852
+ raw: line
1853
+ });
1854
+ }
1855
+ m = /^(.*?)@(.+?):(\d+):(\d+)$/.exec(line);
1856
+ if (m) {
1857
+ return buildFrame({
1858
+ function: m[1] || "?",
1859
+ filename: m[2],
1860
+ lineno: parseInt(m[3], 10),
1861
+ colno: parseInt(m[4], 10),
1862
+ raw: line
1863
+ });
1864
+ }
1865
+ if (/^\w*Error/.test(line) || !line.includes(":")) {
1866
+ return null;
1867
+ }
1868
+ return {
1869
+ function: "?",
1870
+ filename: "",
1871
+ lineno: 0,
1872
+ colno: 0,
1873
+ in_app: true,
1874
+ raw: line
1875
+ };
1876
+ }
1877
+ function buildFrame(input) {
1878
+ return {
1879
+ function: input.function || "?",
1880
+ filename: input.filename,
1881
+ lineno: Number.isFinite(input.lineno) ? input.lineno : 0,
1882
+ colno: Number.isFinite(input.colno) ? input.colno : 0,
1883
+ in_app: isInAppFrame(input.filename),
1884
+ raw: input.raw
1885
+ };
1886
+ }
1887
+ function isInAppFrame(filename) {
1888
+ if (!filename) return true;
1889
+ if (/^(?:chrome|moz|safari|webkit)-extension:\/\//.test(filename)) return false;
1890
+ if (/\bcdn\.jsdelivr\.net\b/.test(filename)) return false;
1891
+ if (/\bunpkg\.com\b/.test(filename)) return false;
1892
+ if (/\bgoogletagmanager\.com\b/.test(filename)) return false;
1893
+ if (/\bgoogle-analytics\.com\b/.test(filename)) return false;
1894
+ if (/\b@cross-deck\/web\b/.test(filename)) return false;
1895
+ if (/\/crossdeck\.umd\.min\.js$/.test(filename)) return false;
1896
+ return true;
1897
+ }
1898
+ function fingerprintError(message, frames) {
1899
+ const inAppFrames = frames.filter((f) => f.in_app).slice(0, 3);
1900
+ const key = [
1901
+ (message || "").slice(0, 200),
1902
+ ...inAppFrames.map((f) => `${f.function}@${f.filename}:${f.lineno}`)
1903
+ ].join("|");
1904
+ return djb2Hex(key);
1905
+ }
1906
+ function djb2Hex(input) {
1907
+ let h = 5381;
1908
+ for (let i = 0; i < input.length; i++) {
1909
+ h = (h << 5) + h + input.charCodeAt(i) | 0;
1910
+ }
1911
+ return (h >>> 0).toString(16).padStart(8, "0");
1912
+ }
1913
+
1914
+ // src/error-capture.ts
1915
+ var DEFAULT_ERROR_CAPTURE = {
1916
+ enabled: true,
1917
+ onError: true,
1918
+ onUnhandledRejection: true,
1919
+ wrapFetch: true,
1920
+ wrapXhr: true,
1921
+ captureConsole: false,
1922
+ ignoreErrors: [
1923
+ // Classic browser noise. These aren't application bugs.
1924
+ "ResizeObserver loop limit exceeded",
1925
+ "ResizeObserver loop completed with undelivered notifications",
1926
+ "Non-Error promise rejection captured",
1927
+ // Cross-origin script errors that the browser strips — no info,
1928
+ // no way to act on them, just noise.
1929
+ "Script error.",
1930
+ "Script error"
1931
+ ],
1932
+ allowUrls: [],
1933
+ denyUrls: [
1934
+ // Common third-party extensions that pollute error streams.
1935
+ /^chrome-extension:\/\//,
1936
+ /^moz-extension:\/\//,
1937
+ /^safari-extension:\/\//,
1938
+ /^webkit-extension:\/\//,
1939
+ /^safari-web-extension:\/\//
1940
+ ],
1941
+ sampleRate: 1,
1942
+ maxPerFingerprintPerMinute: 5,
1943
+ maxPerSession: 100
1944
+ };
1945
+ var ErrorTracker = class {
1946
+ constructor(opts) {
1947
+ this.opts = opts;
1948
+ this.installed = false;
1949
+ this.cleanups = [];
1950
+ this._reporting = false;
1951
+ this.sessionCount = 0;
1952
+ this.fingerprintWindow = /* @__PURE__ */ new Map();
1953
+ }
1954
+ install() {
1955
+ if (this.installed) return;
1956
+ if (!this.opts.config.enabled) return;
1957
+ if (typeof globalThis === "undefined" || !("window" in globalThis)) return;
1958
+ const w = globalThis.window;
1959
+ if (this.opts.config.onError) this.installOnErrorListener(w);
1960
+ if (this.opts.config.onUnhandledRejection) this.installRejectionListener(w);
1961
+ if (this.opts.config.wrapFetch) this.installFetchWrap(w);
1962
+ if (this.opts.config.wrapXhr) this.installXhrWrap(w);
1963
+ if (this.opts.config.captureConsole) this.installConsoleWrap();
1964
+ this.installed = true;
1965
+ }
1966
+ uninstall() {
1967
+ for (const fn of this.cleanups.splice(0)) {
1968
+ try {
1969
+ fn();
1970
+ } catch {
1971
+ }
1972
+ }
1973
+ this.installed = false;
1974
+ }
1975
+ /**
1976
+ * Manual API. Either an Error instance or any unknown value (we
1977
+ * coerce). Returns silently — never throws.
1978
+ */
1979
+ captureError(error, options) {
1980
+ if (!this.opts.isConsented()) return;
1981
+ try {
1982
+ const captured = this.buildFromUnknown(error, "error.handled", options?.level ?? "error");
1983
+ if (options?.context) captured.context = { ...captured.context, ...options.context };
1984
+ if (options?.tags) captured.tags = { ...captured.tags, ...options.tags };
1985
+ this.maybeReport(captured);
1986
+ } catch {
1987
+ }
1988
+ }
1989
+ /**
1990
+ * Capture a non-error event as an issue. For "we hit a soft-warning
1991
+ * code path" / "deprecated API used" kinds of signals. Pairs with
1992
+ * Sentry's captureMessage().
1993
+ */
1994
+ captureMessage(message, level = "info") {
1995
+ if (!this.opts.isConsented()) return;
1996
+ try {
1997
+ const captured = {
1998
+ timestamp: Date.now(),
1999
+ kind: "error.message",
2000
+ level,
2001
+ message,
2002
+ errorType: null,
2003
+ frames: [],
2004
+ rawStack: null,
2005
+ filename: null,
2006
+ lineno: null,
2007
+ colno: null,
2008
+ fingerprint: fingerprintError(message, []),
2009
+ breadcrumbs: this.opts.breadcrumbs.snapshot(),
2010
+ context: this.opts.getContext(),
2011
+ tags: this.opts.getTags()
2012
+ };
2013
+ this.maybeReport(captured);
2014
+ } catch {
2015
+ }
2016
+ }
2017
+ // ============================================================
2018
+ // Listener installation
2019
+ // ============================================================
2020
+ installOnErrorListener(w) {
2021
+ const handler = (event) => {
2022
+ if (this._reporting) return;
2023
+ if (!this.opts.isConsented()) return;
2024
+ try {
2025
+ this._reporting = true;
2026
+ const captured = this.buildFromErrorEvent(event);
2027
+ this.maybeReport(captured);
2028
+ } catch {
2029
+ } finally {
2030
+ this._reporting = false;
2031
+ }
2032
+ };
2033
+ w.addEventListener("error", handler, true);
2034
+ this.cleanups.push(() => w.removeEventListener("error", handler, true));
2035
+ }
2036
+ installRejectionListener(w) {
2037
+ const handler = (event) => {
2038
+ if (this._reporting) return;
2039
+ if (!this.opts.isConsented()) return;
2040
+ try {
2041
+ this._reporting = true;
2042
+ const captured = this.buildFromUnknown(
2043
+ event.reason,
2044
+ "error.unhandledrejection",
2045
+ "error"
2046
+ );
2047
+ this.maybeReport(captured);
2048
+ } catch {
2049
+ } finally {
2050
+ this._reporting = false;
2051
+ }
2052
+ };
2053
+ w.addEventListener("unhandledrejection", handler);
2054
+ this.cleanups.push(() => w.removeEventListener("unhandledrejection", handler));
2055
+ }
2056
+ /**
2057
+ * Wrap fetch() so failed HTTP requests get auto-captured. We do NOT
2058
+ * call this an "error" for 4xx (those are often expected — auth
2059
+ * required, validation failed). Only 5xx + network failures fire.
2060
+ */
2061
+ installFetchWrap(w) {
2062
+ const origFetch = w.fetch?.bind(w);
2063
+ if (!origFetch) return;
2064
+ const wrapped = async (...args) => {
2065
+ const input = args[0];
2066
+ const init = args[1] ?? {};
2067
+ const url = typeof input === "string" ? input : input?.url ?? "";
2068
+ const method = (init.method || "GET").toUpperCase();
2069
+ const start = Date.now();
2070
+ this.opts.breadcrumbs.add({
2071
+ timestamp: start,
2072
+ category: "http",
2073
+ message: `${method} ${url}`,
2074
+ data: { url, method }
2075
+ });
2076
+ try {
2077
+ const response = await origFetch(...args);
2078
+ if (response.status >= 500 && this.opts.isConsented()) {
2079
+ if (!url.includes("api.cross-deck.com")) {
2080
+ this.captureHttp({
2081
+ url,
2082
+ method,
2083
+ status: response.status,
2084
+ statusText: response.statusText
2085
+ });
2086
+ }
2087
+ }
2088
+ return response;
2089
+ } catch (err) {
2090
+ if (this.opts.isConsented() && !url.includes("api.cross-deck.com")) {
2091
+ this.captureHttp({
2092
+ url,
2093
+ method,
2094
+ status: 0,
2095
+ statusText: err instanceof Error ? err.message : "network error"
2096
+ });
2097
+ }
2098
+ throw err;
2099
+ }
2100
+ };
2101
+ w.fetch = wrapped;
2102
+ this.cleanups.push(() => {
2103
+ if (w.fetch === wrapped) w.fetch = origFetch;
2104
+ });
2105
+ }
2106
+ /**
2107
+ * Wrap XMLHttpRequest for legacy consumers (jQuery $.ajax under the
2108
+ * hood, older bundlers). Same capture semantics as fetch.
2109
+ */
2110
+ installXhrWrap(w) {
2111
+ const xhrCtor = w.XMLHttpRequest;
2112
+ const proto = xhrCtor?.prototype;
2113
+ if (!proto) return;
2114
+ const origOpen = proto.open;
2115
+ const origSend = proto.send;
2116
+ const tracker = this;
2117
+ proto.open = function(method, url, ...rest) {
2118
+ this._cdMethod = method;
2119
+ this._cdUrl = url;
2120
+ return origOpen.apply(this, [method, url, ...rest]);
2121
+ };
2122
+ proto.send = function(body) {
2123
+ const xhr = this;
2124
+ const onLoad = () => {
2125
+ try {
2126
+ if (xhr.status >= 500 && tracker.opts.isConsented()) {
2127
+ const url = xhr._cdUrl ?? "";
2128
+ if (!url.includes("api.cross-deck.com")) {
2129
+ tracker.captureHttp({
2130
+ url,
2131
+ method: (xhr._cdMethod ?? "GET").toUpperCase(),
2132
+ status: xhr.status,
2133
+ statusText: xhr.statusText
2134
+ });
2135
+ }
2136
+ }
2137
+ } catch {
2138
+ }
2139
+ };
2140
+ xhr.addEventListener("loadend", onLoad);
2141
+ return origSend.apply(this, [body ?? null]);
2142
+ };
2143
+ this.cleanups.push(() => {
2144
+ proto.open = origOpen;
2145
+ proto.send = origSend;
2146
+ });
2147
+ }
2148
+ installConsoleWrap() {
2149
+ const console2 = globalThis.console;
2150
+ if (!console2) return;
2151
+ const orig = console2.error.bind(console2);
2152
+ console2.error = (...args) => {
2153
+ try {
2154
+ if (this.opts.isConsented()) {
2155
+ this.captureMessage(args.map((a) => safeStringify2(a)).join(" "), "error");
2156
+ }
2157
+ } catch {
2158
+ }
2159
+ return orig(...args);
2160
+ };
2161
+ this.cleanups.push(() => {
2162
+ console2.error = orig;
2163
+ });
2164
+ }
2165
+ // ============================================================
2166
+ // Builders
2167
+ // ============================================================
2168
+ buildFromErrorEvent(event) {
2169
+ const err = event.error;
2170
+ const message = event.message || (err instanceof Error ? err.message : "Unknown error");
2171
+ const stack = err instanceof Error ? err.stack ?? null : null;
2172
+ const frames = parseStack(stack);
2173
+ return {
2174
+ timestamp: Date.now(),
2175
+ kind: "error.unhandled",
2176
+ level: "error",
2177
+ message: String(message).slice(0, 1024),
2178
+ errorType: err instanceof Error ? err.name : null,
2179
+ frames,
2180
+ rawStack: stack,
2181
+ filename: event.filename || null,
2182
+ lineno: typeof event.lineno === "number" ? event.lineno : null,
2183
+ colno: typeof event.colno === "number" ? event.colno : null,
2184
+ fingerprint: fingerprintError(message, frames),
2185
+ breadcrumbs: this.opts.breadcrumbs.snapshot(),
2186
+ context: this.opts.getContext(),
2187
+ tags: this.opts.getTags()
2188
+ };
2189
+ }
2190
+ buildFromUnknown(err, kind, level) {
2191
+ if (err instanceof Error) {
2192
+ const frames = parseStack(err.stack);
2193
+ return {
2194
+ timestamp: Date.now(),
2195
+ kind,
2196
+ level,
2197
+ message: String(err.message).slice(0, 1024),
2198
+ errorType: err.name,
2199
+ frames,
2200
+ rawStack: err.stack ?? null,
2201
+ filename: null,
2202
+ lineno: null,
2203
+ colno: null,
2204
+ fingerprint: fingerprintError(err.message, frames),
2205
+ breadcrumbs: this.opts.breadcrumbs.snapshot(),
2206
+ context: this.opts.getContext(),
2207
+ tags: this.opts.getTags()
2208
+ };
2209
+ }
2210
+ const message = safeStringify2(err).slice(0, 1024);
2211
+ return {
2212
+ timestamp: Date.now(),
2213
+ kind,
2214
+ level,
2215
+ message,
2216
+ errorType: null,
2217
+ frames: [],
2218
+ rawStack: null,
2219
+ filename: null,
2220
+ lineno: null,
2221
+ colno: null,
2222
+ fingerprint: fingerprintError(message, []),
2223
+ breadcrumbs: this.opts.breadcrumbs.snapshot(),
2224
+ context: this.opts.getContext(),
2225
+ tags: this.opts.getTags()
2226
+ };
2227
+ }
2228
+ captureHttp(info) {
2229
+ try {
2230
+ const message = `HTTP ${info.status} ${info.method} ${info.url}`;
2231
+ const captured = {
2232
+ timestamp: Date.now(),
2233
+ kind: "error.http",
2234
+ level: "error",
2235
+ message,
2236
+ errorType: `HTTPError`,
2237
+ frames: [],
2238
+ rawStack: null,
2239
+ filename: info.url,
2240
+ lineno: null,
2241
+ colno: null,
2242
+ fingerprint: fingerprintError(`HTTP ${info.status} ${info.method}`, []),
2243
+ breadcrumbs: this.opts.breadcrumbs.snapshot(),
2244
+ context: this.opts.getContext(),
2245
+ tags: this.opts.getTags(),
2246
+ http: info
2247
+ };
2248
+ this.maybeReport(captured);
2249
+ } catch {
2250
+ }
2251
+ }
2252
+ // ============================================================
2253
+ // Reporting pipeline — filter / sample / rate-limit / send
2254
+ // ============================================================
2255
+ maybeReport(err) {
2256
+ if (this.sessionCount >= this.opts.config.maxPerSession) return;
2257
+ if (this.shouldIgnore(err)) return;
2258
+ if (!this.passesUrlGate(err)) return;
2259
+ if (!this.passesSample(err)) return;
2260
+ if (!this.passesRateLimit(err)) return;
2261
+ let finalErr = err;
2262
+ if (this.opts.beforeSend) {
2263
+ try {
2264
+ finalErr = this.opts.beforeSend(err);
2265
+ } catch {
2266
+ finalErr = err;
2267
+ }
2268
+ if (!finalErr) return;
2269
+ }
2270
+ this.sessionCount += 1;
2271
+ try {
2272
+ this.opts.report(finalErr);
2273
+ } catch {
2274
+ }
2275
+ }
2276
+ shouldIgnore(err) {
2277
+ for (const pat of this.opts.config.ignoreErrors) {
2278
+ if (typeof pat === "string" && err.message.includes(pat)) return true;
2279
+ if (pat instanceof RegExp && pat.test(err.message)) return true;
2280
+ }
2281
+ return false;
2282
+ }
2283
+ passesUrlGate(err) {
2284
+ const topFrame = err.frames.find((f) => f.filename) ?? null;
2285
+ const url = topFrame?.filename ?? err.filename ?? "";
2286
+ if (!url) return true;
2287
+ for (const pat of this.opts.config.denyUrls) {
2288
+ if (typeof pat === "string" && url.includes(pat)) return false;
2289
+ if (pat instanceof RegExp && pat.test(url)) return false;
2290
+ }
2291
+ if (this.opts.config.allowUrls.length > 0) {
2292
+ for (const pat of this.opts.config.allowUrls) {
2293
+ if (typeof pat === "string" && url.includes(pat)) return true;
2294
+ if (pat instanceof RegExp && pat.test(url)) return true;
2295
+ }
2296
+ return false;
2297
+ }
2298
+ return true;
2299
+ }
2300
+ passesSample(err) {
2301
+ if (this.opts.config.sampleRate >= 1) return true;
2302
+ if (this.opts.config.sampleRate <= 0) return false;
2303
+ const hashByte = parseInt(err.fingerprint.slice(0, 2), 16);
2304
+ return hashByte / 255 < this.opts.config.sampleRate;
2305
+ }
2306
+ passesRateLimit(err) {
2307
+ const windowMs = 6e4;
2308
+ const now = Date.now();
2309
+ const max = this.opts.config.maxPerFingerprintPerMinute;
2310
+ const arr = this.fingerprintWindow.get(err.fingerprint) ?? [];
2311
+ const fresh = arr.filter((t) => now - t < windowMs);
2312
+ if (fresh.length >= max) {
2313
+ this.fingerprintWindow.set(err.fingerprint, fresh);
2314
+ return false;
2315
+ }
2316
+ fresh.push(now);
2317
+ this.fingerprintWindow.set(err.fingerprint, fresh);
2318
+ return true;
2319
+ }
2320
+ };
2321
+ function safeStringify2(v) {
2322
+ if (v == null) return String(v);
2323
+ if (typeof v === "string") return v;
2324
+ if (typeof v === "number" || typeof v === "boolean") return String(v);
2325
+ try {
2326
+ return JSON.stringify(v);
2327
+ } catch {
2328
+ return Object.prototype.toString.call(v);
2329
+ }
2330
+ }
2331
+
2332
+ // src/crossdeck.ts
2333
+ var CrossdeckClient = class {
2334
+ constructor() {
2335
+ this.state = null;
2336
+ }
2337
+ /**
2338
+ * Boot the SDK. Idempotent — calling init twice with the same options
2339
+ * is a no-op; calling with different options replaces the previous
2340
+ * configuration.
2341
+ *
2342
+ * NorthStar §11.1: signature is `Crossdeck.init({ appId, publicKey,
2343
+ * environment })`. The trio is validated up-front so a typo'd key or a
2344
+ * mismatched env fails fast at boot rather than at first event-flush.
2345
+ */
2346
+ init(options) {
2347
+ if (!options.publicKey || !options.publicKey.startsWith("cd_pub_")) {
2348
+ throw new CrossdeckError({
2349
+ type: "configuration_error",
2350
+ code: "invalid_public_key",
2351
+ message: "Crossdeck.init requires a publishable key starting with cd_pub_."
2352
+ });
2353
+ }
2354
+ if (!options.appId) {
2355
+ throw new CrossdeckError({
2356
+ type: "configuration_error",
2357
+ code: "missing_app_id",
2358
+ message: "Crossdeck.init requires an appId. Find yours in the Crossdeck dashboard."
2359
+ });
2360
+ }
2361
+ if (options.environment !== "production" && options.environment !== "sandbox") {
2362
+ throw new CrossdeckError({
2363
+ type: "configuration_error",
2364
+ code: "invalid_environment",
2365
+ message: 'Crossdeck.init requires environment: "production" | "sandbox".'
2366
+ });
2367
+ }
2368
+ const keyEnv = inferEnvFromKey(options.publicKey);
2369
+ if (keyEnv && keyEnv !== options.environment) {
2370
+ throw new CrossdeckError({
2371
+ type: "configuration_error",
2372
+ code: "environment_mismatch",
2373
+ message: `Crossdeck.init: environment "${options.environment}" disagrees with key prefix (${keyEnv}). Reconcile the publishable key with the environment declaration.`
2374
+ });
2375
+ }
2376
+ const localDevMode = isLocalHostname();
2377
+ const storage = options.storage ?? detectDefaultStorage();
2378
+ const persistIdentity = options.persistIdentity ?? true;
2379
+ const autoTrack = resolveAutoTrack(options.autoTrack);
2380
+ const opts = {
2381
+ appId: options.appId,
2382
+ publicKey: options.publicKey,
2383
+ environment: options.environment,
2384
+ baseUrl: options.baseUrl ?? DEFAULT_BASE_URL,
2385
+ persistIdentity,
2386
+ storagePrefix: options.storagePrefix ?? "crossdeck:",
2387
+ autoHeartbeat: options.autoHeartbeat ?? true,
2388
+ eventFlushBatchSize: options.eventFlushBatchSize ?? 20,
2389
+ // 1500ms idle window. Short enough that an event queued on page
2390
+ // load still flushes if the user leaves quickly (the keepalive
2391
+ // pagehide handler picks up anything that doesn't); long enough
2392
+ // that bursts of clicks coalesce into one network round-trip.
2393
+ eventFlushIntervalMs: options.eventFlushIntervalMs ?? 1500,
2394
+ sdkVersion: options.sdkVersion ?? SDK_VERSION,
2395
+ autoTrack,
2396
+ appVersion: options.appVersion ?? null
2397
+ };
2398
+ const debug = new ConsoleDebugLogger();
2399
+ debug.enabled = options.debug === true;
2400
+ const http = new HttpClient({
2401
+ publicKey: opts.publicKey,
2402
+ baseUrl: opts.baseUrl,
2403
+ sdkVersion: opts.sdkVersion,
2404
+ // Localhost auto-route: HttpClient short-circuits every request
2405
+ // to a successful no-op response when localDevMode is set.
2406
+ // SDK methods continue to work locally; nothing reaches the
2407
+ // server.
2408
+ localDevMode
2409
+ });
2410
+ if (localDevMode) {
2411
+ console.log(
2412
+ "[crossdeck] Localhost detected \u2014 running in dev mode (no network calls). Set publicKey: 'cd_pub_test_\u2026' and deploy to a real domain to test against the Crossdeck Sandbox."
2413
+ );
2414
+ }
2415
+ const effectiveStorage = persistIdentity ? storage : new MemoryStorage();
2416
+ const useCookieRedundancy = persistIdentity && !options.storage && // honour caller's adapter choice
2417
+ typeof globalThis.document !== "undefined";
2418
+ const cookieStore = useCookieRedundancy ? new CookieStorage() : void 0;
2419
+ const identity = new IdentityStore(effectiveStorage, opts.storagePrefix, cookieStore);
2420
+ const entitlements = new EntitlementCache();
2421
+ const persistentEvents = persistIdentity ? new PersistentEventStore({ storage: effectiveStorage, prefix: opts.storagePrefix }) : null;
2422
+ if (persistentEvents) {
2423
+ debug.emit(
2424
+ "sdk.queue_restored",
2425
+ "Restored persisted event queue from a prior session."
2426
+ );
2427
+ }
2428
+ const events = new EventQueue({
2429
+ http,
2430
+ batchSize: opts.eventFlushBatchSize,
2431
+ intervalMs: opts.eventFlushIntervalMs,
2432
+ envelope: () => ({
2433
+ appId: opts.appId,
2434
+ environment: opts.environment,
2435
+ sdk: { name: SDK_NAME, version: opts.sdkVersion }
2436
+ }),
2437
+ persistentStore: persistentEvents ?? void 0,
2438
+ onFirstFlushSuccess: () => {
2439
+ debug.emit(
2440
+ "sdk.first_event_sent",
2441
+ "First telemetry event received. View it in Live Events.",
2442
+ { appId: opts.appId, environment: opts.environment }
2443
+ );
2444
+ },
2445
+ onRetryScheduled: (info) => {
2446
+ debug.emit(
2447
+ "sdk.flush_retry_scheduled",
2448
+ `Event flush failed (${info.lastError}). Retrying in ${info.delayMs}ms (attempt ${info.consecutiveFailures}).`,
2449
+ { ...info }
2450
+ );
2451
+ }
2452
+ });
2453
+ const deviceInfo = autoTrack.deviceInfo ? collectDeviceInfo({ appVersion: opts.appVersion ?? void 0 }) : opts.appVersion ? { appVersion: opts.appVersion } : {};
2454
+ const superProps = new SuperPropertyStore(
2455
+ persistIdentity ? effectiveStorage : new MemoryStorage(),
2456
+ opts.storagePrefix
2457
+ );
2458
+ const consent = new ConsentManager({ respectDnt: options.respectDnt === true });
2459
+ if (consent.isDntDenied) {
2460
+ debug.emit(
2461
+ "sdk.consent_dnt_applied",
2462
+ "Do Not Track detected \u2014 all tracking dimensions denied at init."
2463
+ );
2464
+ }
2465
+ const breadcrumbs = new BreadcrumbBuffer(50);
2466
+ this.state = {
2467
+ http,
2468
+ identity,
2469
+ entitlements,
2470
+ events,
2471
+ autoTracker: null,
2472
+ webVitals: null,
2473
+ errors: null,
2474
+ breadcrumbs,
2475
+ errorContext: {},
2476
+ errorTags: {},
2477
+ errorBeforeSend: null,
2478
+ superProps,
2479
+ consent,
2480
+ scrubPii: options.scrubPii !== false,
2481
+ deviceInfo,
2482
+ options: opts,
2483
+ debug,
2484
+ developerUserId: null,
2485
+ uninstallUnloadFlush: null,
2486
+ lastServerTime: null,
2487
+ lastClientTime: null
2488
+ };
2489
+ debug.emit("sdk.configured", `Crossdeck connected to ${opts.appId} in ${opts.environment} mode.`, {
2490
+ appId: opts.appId,
2491
+ environment: opts.environment,
2492
+ sdkVersion: opts.sdkVersion
2493
+ });
2494
+ if (autoTrack.sessions || autoTrack.pageViews) {
2495
+ const tracker = new AutoTracker(
2496
+ autoTrack,
2497
+ (name, properties) => this.track(name, properties)
2498
+ );
2499
+ this.state.autoTracker = tracker;
2500
+ tracker.install();
2501
+ }
2502
+ if (autoTrack.webVitals) {
2503
+ const vitals = new WebVitalsTracker(
2504
+ { enabled: true },
2505
+ (name, properties) => this.track(name, properties)
2506
+ );
2507
+ this.state.webVitals = vitals;
2508
+ vitals.install();
2509
+ }
2510
+ if (autoTrack.errors) {
2511
+ const tracker = new ErrorTracker({
2512
+ config: { ...DEFAULT_ERROR_CAPTURE, enabled: true },
2513
+ breadcrumbs,
2514
+ report: (err) => this.reportError(err),
2515
+ getContext: () => ({ ...this.state.errorContext }),
2516
+ getTags: () => ({ ...this.state.errorTags }),
2517
+ beforeSend: this.state.errorBeforeSend,
2518
+ isConsented: () => this.state.consent.errors
2519
+ });
2520
+ this.state.errors = tracker;
2521
+ tracker.install();
2522
+ }
2523
+ this.state.uninstallUnloadFlush = installUnloadFlush(() => {
2524
+ void this.flush({ keepalive: true }).catch(() => void 0);
2525
+ });
2526
+ if (opts.autoHeartbeat && !localDevMode) {
2527
+ void this.heartbeat().catch(() => void 0);
2528
+ }
2529
+ }
2530
+ /**
2531
+ * @deprecated Use `init()` instead. NorthStar §4 standardised the
2532
+ * lifecycle method name across SDKs as `init` (formerly `start` /
2533
+ * `configure`). `start` will be removed in a future major version.
2534
+ */
2535
+ start(options) {
2536
+ if (typeof console !== "undefined") {
2537
+ console.warn(
2538
+ "[crossdeck] Crossdeck.start() is deprecated \u2014 use Crossdeck.init() instead. The signature is the same."
2539
+ );
2540
+ }
2541
+ this.init(options);
2542
+ }
2543
+ /**
2544
+ * Link the anonymous device to a developer-supplied user ID. Cache
2545
+ * the resolved Crossdeck customer for follow-up calls.
2546
+ *
2547
+ * v0.9.0+ accepts an optional `traits` bag — profile data (name,
2548
+ * plan, signupDate, role) persisted on the Crossdeck customer record
2549
+ * and queryable from dashboards. Traits are sanitised through the
2550
+ * same validator that gates `track()` properties, so a `{ avatar:
2551
+ * <File>, onSave: () => {} }` payload can't corrupt the alias call.
2552
+ *
2553
+ * Crossdeck.identify("user_847", {
2554
+ * email: "wes@pinet.co.za",
2555
+ * traits: { name: "Wes", plan: "pro", signedUpAt: "2026-05-11" },
2556
+ * });
2557
+ */
2558
+ async identify(userId, options) {
2559
+ const s = this.requireStarted();
2560
+ if (!userId) {
2561
+ throw new CrossdeckError({
2562
+ type: "invalid_request_error",
2563
+ code: "missing_user_id",
2564
+ message: "identify(userId) requires a non-empty userId."
2565
+ });
2566
+ }
2567
+ if (!s.consent.analytics) {
2568
+ s.debug.emit(
2569
+ "sdk.consent_denied",
2570
+ `identify() skipped \u2014 consent denied for analytics.`
2571
+ );
2572
+ return {
2573
+ object: "alias_result",
2574
+ crossdeckCustomerId: s.identity.crossdeckCustomerId ?? "",
2575
+ linked: [],
2576
+ mergePending: false,
2577
+ env: s.options.environment
2578
+ };
2579
+ }
2580
+ const traitsValidation = options?.traits !== void 0 ? validateEventProperties(options.traits) : null;
2581
+ const traits = traitsValidation && Object.keys(traitsValidation.properties).length > 0 ? traitsValidation.properties : void 0;
2582
+ if (s.debug.enabled && traitsValidation && traitsValidation.warnings.length > 0) {
2583
+ for (const w of traitsValidation.warnings) {
2584
+ s.debug.emit(
2585
+ "sdk.property_coerced",
2586
+ `identify() traits key ${JSON.stringify(w.key)} was ${w.kind.replace(/_/g, " ")} during validation.`,
2587
+ { key: w.key, kind: w.kind }
2588
+ );
2589
+ }
2590
+ }
2591
+ const body = {
2592
+ userId,
2593
+ anonymousId: s.identity.anonymousId
2594
+ };
2595
+ if (options?.email) body.email = options.email;
2596
+ if (traits) body.traits = traits;
2597
+ const result = await s.http.request("POST", "/identity/alias", {
2598
+ body
2599
+ });
2600
+ s.identity.setCrossdeckCustomerId(result.crossdeckCustomerId);
2601
+ s.developerUserId = userId;
2602
+ return result;
2603
+ }
2604
+ /**
2605
+ * Register super-properties — Mixpanel pattern. Once set, every
2606
+ * subsequent event of THIS SDK instance carries these keys on its
2607
+ * properties bag automatically.
2608
+ *
2609
+ * Crossdeck.register({ plan: "pro", releaseChannel: "beta" });
2610
+ * Crossdeck.track("paywall_shown"); // includes plan + releaseChannel
2611
+ *
2612
+ * Values that are `null` are deleted (the explicit "stop tracking
2613
+ * this key" idiom). Returns the resulting bag.
2614
+ *
2615
+ * Sanitised through `validateEventProperties` so a `{ avatar: File }`
2616
+ * payload can't poison the queue at flush time.
2617
+ */
2618
+ register(properties) {
2619
+ const s = this.requireStarted();
2620
+ const validation = validateEventProperties(properties);
2621
+ return s.superProps.register(validation.properties);
2622
+ }
2623
+ /** Remove a single super-property key. Idempotent. */
2624
+ unregister(key) {
2625
+ const s = this.requireStarted();
2626
+ s.superProps.unregister(key);
2627
+ }
2628
+ /** Snapshot of the current super-property bag. */
2629
+ getSuperProperties() {
2630
+ if (!this.state) return {};
2631
+ return this.state.superProps.getSuperProperties();
2632
+ }
2633
+ /**
2634
+ * Associate the current user with a group (org, team, account, etc.).
2635
+ * Mixpanel / Segment "Group Analytics" pattern.
2636
+ *
2637
+ * Crossdeck.group("org", "acme_inc");
2638
+ * Crossdeck.group("team", "design", { headcount: 12 });
2639
+ *
2640
+ * Once set, every subsequent event carries `$groups.<type>: id` on
2641
+ * its properties bag, enabling B2B dashboards ("how is Acme using
2642
+ * the product"). Pass `id: null` to clear a group membership.
2643
+ */
2644
+ group(type, id, traits) {
2645
+ const s = this.requireStarted();
2646
+ if (!type) {
2647
+ throw new CrossdeckError({
2648
+ type: "invalid_request_error",
2649
+ code: "missing_group_type",
2650
+ message: "group(type, id) requires a non-empty type."
2651
+ });
2652
+ }
2653
+ const sanitisedTraits = traits ? validateEventProperties(traits).properties : void 0;
2654
+ s.superProps.setGroup(type, id, sanitisedTraits);
2655
+ }
2656
+ /** Snapshot of the current groups map keyed by type. */
2657
+ getGroups() {
2658
+ if (!this.state) return {};
2659
+ return this.state.superProps.getGroups();
2660
+ }
2661
+ /**
2662
+ * Update consent state. Three independent dimensions:
2663
+ *
2664
+ * analytics — track() + identify() + auto-emissions
2665
+ * marketing — paid-traffic click IDs + referrer URL on events
2666
+ * errors — Web Vitals + (future) error reporting
2667
+ *
2668
+ * Each defaults to `true` (granted). Pass partial state — only the
2669
+ * keys you provide are changed.
2670
+ *
2671
+ * Crossdeck.consent({ analytics: false });
2672
+ * Crossdeck.consent({ marketing: true, errors: true });
2673
+ *
2674
+ * DNT-derived denies cannot be flipped back on; if the browser said
2675
+ * "don't track" we don't track even if the developer code disagrees.
2676
+ */
2677
+ consent(state) {
2678
+ const s = this.requireStarted();
2679
+ const next = s.consent.set(state);
2680
+ s.debug.emit("sdk.consent_changed", "Consent state updated.", { ...next });
2681
+ return next;
2682
+ }
2683
+ /** Snapshot of the current consent state. */
2684
+ consentStatus() {
2685
+ if (!this.state) {
2686
+ return { analytics: true, marketing: true, errors: true };
2687
+ }
2688
+ return this.state.consent.get();
2689
+ }
2690
+ // ============================================================
2691
+ // Error capture surface (v1.0.0+)
2692
+ // ============================================================
2693
+ /**
2694
+ * Manually capture an error from a try/catch block.
2695
+ *
2696
+ * try { …risky… } catch (err) {
2697
+ * Crossdeck.captureError(err, { context: { plan: "pro" } });
2698
+ * }
2699
+ *
2700
+ * The error is shipped through the same event queue as analytics
2701
+ * (durable, retried, rate-limited per fingerprint). Sends are gated
2702
+ * by `consent.errors`. Returns silently — never throws, even if the
2703
+ * SDK isn't initialised yet.
2704
+ */
2705
+ captureError(error, options) {
2706
+ if (!this.state?.errors) return;
2707
+ this.state.errors.captureError(error, options);
2708
+ }
2709
+ /**
2710
+ * Capture a non-error event you want to surface as an issue
2711
+ * ("deprecated path hit", "we entered the slow code path"). Sentry
2712
+ * captureMessage pattern. Returns silently if not initialised.
2713
+ */
2714
+ captureMessage(message, level = "info") {
2715
+ if (!this.state?.errors) return;
2716
+ this.state.errors.captureMessage(message, level);
2717
+ }
2718
+ /**
2719
+ * Attach a tag to every subsequent error report. Tags are key/value
2720
+ * strings (Sentry pattern): `setTag("flow", "checkout")` → every
2721
+ * error from this point on carries `tags.flow === "checkout"`.
2722
+ */
2723
+ setTag(key, value) {
2724
+ if (!this.state) return;
2725
+ this.state.errorTags[key] = value;
2726
+ }
2727
+ /** Bulk-set tags. Merges with existing tags. */
2728
+ setTags(tags) {
2729
+ if (!this.state) return;
2730
+ Object.assign(this.state.errorTags, tags);
2731
+ }
2732
+ /**
2733
+ * Attach a structured context blob to every subsequent error report.
2734
+ * Unlike tags (flat key/value), context is a named bag of arbitrary
2735
+ * data: `setContext("cart", { items: 3, total: 42.99 })`.
2736
+ */
2737
+ setContext(name, data) {
2738
+ if (!this.state) return;
2739
+ this.state.errorContext[name] = data;
2740
+ }
2741
+ /**
2742
+ * Add a custom breadcrumb to the rolling buffer. Useful for marking
2743
+ * domain-meaningful moments ("user opened paywall") that aren't
2744
+ * already auto-captured. The buffer caps at 50 entries; old ones
2745
+ * evict.
2746
+ */
2747
+ addBreadcrumb(crumb) {
2748
+ if (!this.state) return;
2749
+ this.state.breadcrumbs.add(crumb);
2750
+ }
2751
+ /**
2752
+ * Install a pre-send hook for errors. Return null to drop, or a
2753
+ * modified CapturedError to scrub / rewrite. Sentry's beforeSend
2754
+ * pattern — the only way to redact app-specific PII (auth tokens
2755
+ * in URLs, etc.) before the report leaves the browser.
2756
+ */
2757
+ setErrorBeforeSend(hook) {
2758
+ if (!this.state) return;
2759
+ this.state.errorBeforeSend = hook;
2760
+ }
2761
+ /**
2762
+ * Internal: turn a CapturedError into a Crossdeck event and enqueue
2763
+ * it. Goes through the same queue / persistence / consent / scrub
2764
+ * pipeline as analytics events.
2765
+ */
2766
+ reportError(err) {
2767
+ const properties = {
2768
+ // Identifiers
2769
+ fingerprint: err.fingerprint,
2770
+ level: err.level,
2771
+ // Error shape
2772
+ errorType: err.errorType,
2773
+ message: err.message,
2774
+ // Stack
2775
+ stack: err.rawStack ?? void 0,
2776
+ frames: err.frames,
2777
+ filename: err.filename ?? void 0,
2778
+ lineno: err.lineno ?? void 0,
2779
+ colno: err.colno ?? void 0,
2780
+ // Context
2781
+ tags: err.tags,
2782
+ context: err.context,
2783
+ breadcrumbs: err.breadcrumbs,
2784
+ // HTTP (only when applicable)
2785
+ http: err.http
2786
+ };
2787
+ for (const k of Object.keys(properties)) {
2788
+ if (properties[k] === void 0) delete properties[k];
2789
+ }
2790
+ this.track(err.kind, properties);
2791
+ }
2792
+ /**
2793
+ * GDPR/CCPA "right to be forgotten" — calls the backend's
2794
+ * /v1/identity/forget endpoint to schedule a server-side deletion of
2795
+ * the customer's events and profile, then wipes all local state
2796
+ * (identity, entitlements, queue, super-props, persistent stores).
2797
+ *
2798
+ * Idempotent. Safe to call when no identity has been established
2799
+ * (it just wipes the empty local state).
2800
+ *
2801
+ * After forget() resolves, the SDK is in the same shape as if the
2802
+ * developer had called `Crossdeck.reset()` — a fresh anonymousId is
2803
+ * minted and the next session is a brand new identity-graph entry.
2804
+ */
2805
+ async forget() {
2806
+ const s = this.requireStarted();
2807
+ const identityQuery = this.identityQueryParams();
2808
+ try {
2809
+ await s.http.request("POST", "/identity/forget", {
2810
+ body: {
2811
+ // Send every identity hint we hold; the server resolves the
2812
+ // canonical customer record and queues deletion. Missing
2813
+ // endpoint (older backend) gracefully degrades — local state
2814
+ // still wipes via the reset() call below.
2815
+ ...identityQuery
2816
+ }
2817
+ });
2818
+ } catch (err) {
2819
+ s.debug.emit(
2820
+ "sdk.consent_denied",
2821
+ `forget() server call failed (${err instanceof Error ? err.message : String(err)}). Local state wiped anyway.`
2822
+ );
2823
+ }
2824
+ this.reset();
2825
+ }
2826
+ /**
2827
+ * Read the current customer's active entitlements from the server.
2828
+ * Updates the local cache so subsequent isEntitled() calls answer
2829
+ * synchronously.
2830
+ */
2831
+ async getEntitlements() {
2832
+ const s = this.requireStarted();
2833
+ const query = this.identityQueryParams();
2834
+ const result = await s.http.request(
2835
+ "GET",
2836
+ "/entitlements",
2837
+ { query }
2838
+ );
2839
+ if (result.crossdeckCustomerId) {
2840
+ s.identity.setCrossdeckCustomerId(result.crossdeckCustomerId);
2841
+ }
2842
+ s.entitlements.setFromList(result.data);
2843
+ return result.data;
2844
+ }
2845
+ /**
2846
+ * Synchronous read from the local cache. Returns false if the cache
2847
+ * has never been populated (call getEntitlements first to warm it).
2848
+ */
2849
+ isEntitled(key) {
2850
+ const s = this.requireStarted();
2851
+ return s.entitlements.isEntitled(key);
2852
+ }
2853
+ /** Snapshot of the local entitlement cache. */
2854
+ listEntitlements() {
2855
+ const s = this.requireStarted();
2856
+ return s.entitlements.list();
2857
+ }
2858
+ /**
2859
+ * Subscribe to entitlement-cache changes. Returns an unsubscribe fn.
2860
+ *
2861
+ * The listener is invoked AFTER the cache mutates — once after a
2862
+ * successful `getEntitlements()` warms it, again after `syncPurchases()`
2863
+ * delivers fresh entitlements, and once on `reset()` to fire the
2864
+ * empty-cache state for logout flows.
2865
+ *
2866
+ * It is NOT invoked synchronously on subscribe. Callers that need
2867
+ * the current state should read it via `isEntitled()` / `listEntitlements()`
2868
+ * inline; the listener fires only on FUTURE changes.
2869
+ *
2870
+ * This is the foundation of the `useEntitlement` React hook in
2871
+ * `@cross-deck/web/react` — without it, React (or SwiftUI / Compose
2872
+ * / Vue) would have no way to re-render when entitlements arrive
2873
+ * asynchronously after init. The naive pattern of calling
2874
+ * `Crossdeck.isEntitled("pro")` directly inside a render path
2875
+ * shows the empty-cache result forever; binding the result to
2876
+ * component state via `onEntitlementsChange` is the correct
2877
+ * pattern.
2878
+ *
2879
+ * Idempotent unsubscribe — calling the returned function multiple
2880
+ * times is safe.
2881
+ *
2882
+ * Listener errors are swallowed (a buggy listener can't crash the
2883
+ * SDK or other listeners).
2884
+ */
2885
+ onEntitlementsChange(listener) {
2886
+ const s = this.requireStarted();
2887
+ return s.entitlements.subscribe(listener);
2888
+ }
2889
+ /**
2890
+ * Queue a telemetry event. Returns immediately — the network round-
2891
+ * trip happens in the background. To flush before the page unloads,
2892
+ * call flush().
2893
+ */
2894
+ track(name, properties) {
2895
+ const s = this.requireStarted();
2896
+ if (!name) {
2897
+ throw new CrossdeckError({
2898
+ type: "invalid_request_error",
2899
+ code: "missing_event_name",
2900
+ message: "track(name) requires a non-empty name."
2901
+ });
2902
+ }
2903
+ const isError = name.startsWith("error.");
2904
+ const isWebVital = name.startsWith("webvitals.");
2905
+ const consentGateOk = isError || isWebVital ? s.consent.errors : s.consent.analytics;
2906
+ if (!consentGateOk) {
2907
+ if (s.debug.enabled) {
2908
+ s.debug.emit(
2909
+ "sdk.consent_denied",
2910
+ `Dropped event "${name}" \u2014 consent denied for ${isWebVital ? "errors" : "analytics"}.`
2911
+ );
2912
+ }
2913
+ return;
2914
+ }
2915
+ if (s.debug.enabled && properties) {
2916
+ const flagged = findSensitivePropertyKeys(properties);
2917
+ if (flagged.length > 0) {
2918
+ s.debug.emit(
2919
+ "sdk.sensitive_property_warning",
2920
+ `Event "${name}" has potentially sensitive property names: ${flagged.join(", ")}. Crossdeck is privacy-first \u2014 avoid sending PII unless intentional.`,
2921
+ { eventName: name, flagged }
2922
+ );
2923
+ }
2924
+ }
2925
+ if (s.debug.enabled && !s.developerUserId && !s.identity.crossdeckCustomerId) {
2926
+ s.debug.emit(
2927
+ "sdk.no_identity",
2928
+ "Using anonymous user until identify(userId) is called."
2929
+ );
2930
+ }
2931
+ const validation = validateEventProperties(properties);
2932
+ if (s.debug.enabled && validation.warnings.length > 0) {
2933
+ for (const w of validation.warnings) {
2934
+ s.debug.emit(
2935
+ "sdk.property_coerced",
2936
+ `Event "${name}" property ${JSON.stringify(w.key)} was ${w.kind.replace(/_/g, " ")} during validation.`,
2937
+ { eventName: name, key: w.key, kind: w.kind }
2938
+ );
2939
+ }
2940
+ }
2941
+ const enriched = { ...s.deviceInfo };
2942
+ const sessionId = s.autoTracker?.currentSessionId;
2943
+ if (sessionId) enriched.sessionId = sessionId;
2944
+ const pageviewId = s.autoTracker?.currentPageviewId;
2945
+ if (pageviewId) enriched.pageviewId = pageviewId;
2946
+ const acquisition = s.autoTracker?.currentAcquisition;
2947
+ if (acquisition) {
2948
+ if (acquisition.utm_source) enriched.utm_source = acquisition.utm_source;
2949
+ if (acquisition.utm_medium) enriched.utm_medium = acquisition.utm_medium;
2950
+ if (acquisition.utm_campaign) enriched.utm_campaign = acquisition.utm_campaign;
2951
+ if (acquisition.utm_content) enriched.utm_content = acquisition.utm_content;
2952
+ if (acquisition.utm_term) enriched.utm_term = acquisition.utm_term;
2953
+ if (acquisition.referrer && s.consent.marketing) enriched.referrer = acquisition.referrer;
2954
+ if (s.consent.marketing) {
2955
+ if (acquisition.gclid) enriched.gclid = acquisition.gclid;
2956
+ if (acquisition.fbclid) enriched.fbclid = acquisition.fbclid;
2957
+ if (acquisition.msclkid) enriched.msclkid = acquisition.msclkid;
2958
+ if (acquisition.ttclid) enriched.ttclid = acquisition.ttclid;
2959
+ if (acquisition.li_fat_id) enriched.li_fat_id = acquisition.li_fat_id;
2960
+ if (acquisition.twclid) enriched.twclid = acquisition.twclid;
2961
+ }
2962
+ }
2963
+ const supers = s.superProps.getSuperProperties();
2964
+ for (const k of Object.keys(supers)) {
2965
+ if (!(k in enriched)) enriched[k] = supers[k];
2966
+ }
2967
+ const groupIds = s.superProps.getGroupIds();
2968
+ if (Object.keys(groupIds).length > 0) {
2969
+ enriched.$groups = groupIds;
2970
+ }
2971
+ Object.assign(enriched, validation.properties);
2972
+ const finalProperties = s.scrubPii ? scrubPiiFromProperties(enriched) : enriched;
2973
+ const event = {
2974
+ eventId: this.mintEventId(),
2975
+ name,
2976
+ timestamp: Date.now(),
2977
+ properties: finalProperties
2978
+ };
2979
+ Object.assign(event, this.identityHintForEvent());
2980
+ s.events.enqueue(event);
2981
+ if (!isError && !isWebVital) {
2982
+ const category = name.startsWith("page.") ? "navigation" : name.startsWith("element.") || name === "session.started" ? "ui.click" : "custom";
2983
+ s.breadcrumbs.add({
2984
+ timestamp: event.timestamp,
2985
+ category,
2986
+ message: name,
2987
+ // Strip the device-info / session bloat from the breadcrumb
2988
+ // payload — only the caller-supplied properties belong in
2989
+ // the user-readable trail.
2990
+ data: properties ? { ...properties } : void 0
2991
+ });
2992
+ }
2993
+ }
2994
+ /**
2995
+ * Force-flush queued events. Useful to call from page-unload handlers.
2996
+ *
2997
+ * Pass `{ keepalive: true }` from terminal handlers (pagehide /
2998
+ * visibilitychange→hidden / beforeunload). The browser keeps the
2999
+ * request alive after the page tears down, so the final batch
3000
+ * actually lands instead of being cancelled with the unload.
3001
+ *
3002
+ * NorthStar §4: standard method name across all Crossdeck SDKs.
3003
+ */
3004
+ async flush(options = {}) {
3005
+ const s = this.requireStarted();
3006
+ await s.events.flush(options);
3007
+ }
3008
+ /** @deprecated Use `flush()` instead. NorthStar §4 standardised the name. */
3009
+ async flushEvents() {
3010
+ return this.flush();
3011
+ }
3012
+ /**
3013
+ * Forward purchase evidence to the backend for verification + entitlement
3014
+ * projection. NorthStar §4 + §13 canonical name.
3015
+ *
3016
+ * Today the web SDK only supports Apple StoreKit 2 forwarding (web apps
3017
+ * that sit alongside an iOS app). Stripe doesn't need this method —
3018
+ * Stripe webhooks deliver evidence server-side without a client round-trip.
3019
+ */
3020
+ async syncPurchases(input) {
3021
+ const s = this.requireStarted();
3022
+ if (!input.signedTransactionInfo) {
3023
+ throw new CrossdeckError({
3024
+ type: "invalid_request_error",
3025
+ code: "missing_signed_transaction_info",
3026
+ message: "syncPurchases requires a signedTransactionInfo string from StoreKit 2."
3027
+ });
3028
+ }
3029
+ const result = await s.http.request("POST", "/purchases/sync", {
3030
+ body: { rail: input.rail ?? "apple", ...input }
3031
+ });
3032
+ s.identity.setCrossdeckCustomerId(result.crossdeckCustomerId);
3033
+ s.entitlements.setFromList(result.entitlements);
3034
+ s.debug.emit(
3035
+ "sdk.purchase_evidence_sent",
3036
+ "StoreKit transaction forwarded. Waiting for backend verification.",
3037
+ { rail: input.rail ?? "apple" }
3038
+ );
3039
+ return result;
3040
+ }
3041
+ /** @deprecated Use `syncPurchases()` instead. NorthStar §4 standardised the name. */
3042
+ async purchaseApple(input) {
3043
+ return this.syncPurchases({ rail: "apple", ...input });
3044
+ }
3045
+ /**
3046
+ * Toggle verbose diagnostic logging — NorthStar §16. When enabled, the
3047
+ * SDK emits a fixed vocabulary of debug signals to console.info that the
3048
+ * dashboard's onboarding checklist can also surface as live events.
3049
+ */
3050
+ setDebugMode(enabled) {
3051
+ const s = this.requireStarted();
3052
+ s.debug.enabled = enabled;
3053
+ if (enabled) {
3054
+ s.debug.emit(
3055
+ "sdk.configured",
3056
+ `Debug mode enabled for ${s.options.appId} in ${s.options.environment} mode.`,
3057
+ { appId: s.options.appId, environment: s.options.environment }
3058
+ );
3059
+ }
3060
+ }
3061
+ /**
3062
+ * Send the boot heartbeat. Called automatically by start() unless
3063
+ * autoHeartbeat:false. Safe to call manually as a "we're still here" ping.
3064
+ */
3065
+ async heartbeat() {
3066
+ const s = this.requireStarted();
3067
+ const result = await s.http.request("GET", "/sdk/heartbeat");
3068
+ if (typeof result?.serverTime === "number" && Number.isFinite(result.serverTime)) {
3069
+ s.lastServerTime = result.serverTime;
3070
+ s.lastClientTime = Date.now();
3071
+ }
3072
+ return result;
3073
+ }
3074
+ /**
3075
+ * Wipe persisted identity + entitlement cache. Use on logout. The
3076
+ * next pre-login session generates a fresh anonymousId and starts a
3077
+ * new identity-graph entry.
3078
+ */
3079
+ reset() {
3080
+ if (!this.state) return;
3081
+ if (this.state.developerUserId) {
3082
+ try {
3083
+ this.track("user.signed_out", { auto: true });
3084
+ } catch {
3085
+ }
3086
+ }
3087
+ this.state.autoTracker?.uninstall();
3088
+ this.state.identity.reset();
3089
+ this.state.entitlements.clear();
3090
+ this.state.events.reset();
3091
+ this.state.superProps.clear();
3092
+ this.state.breadcrumbs.clear();
3093
+ this.state.errorContext = {};
3094
+ this.state.errorTags = {};
3095
+ this.state.developerUserId = null;
3096
+ if (this.state.autoTracker) {
3097
+ const tracker = new AutoTracker(
3098
+ this.state.options.autoTrack,
3099
+ (name, props) => this.track(name, props)
3100
+ );
3101
+ this.state.autoTracker = tracker;
3102
+ tracker.install();
3103
+ }
3104
+ }
3105
+ /**
3106
+ * Diagnostic: current state + queue stats. Useful for the dashboard's
3107
+ * heartbeat row and debugging in dev.
3108
+ *
3109
+ * Returns a stable shape regardless of whether start() has been called —
3110
+ * callers don't need to narrow on `started` to access `events` or
3111
+ * `entitlements`. Pre-start values are sensible empties.
3112
+ */
3113
+ diagnostics() {
3114
+ if (!this.state) {
3115
+ return {
3116
+ started: false,
3117
+ anonymousId: null,
3118
+ crossdeckCustomerId: null,
3119
+ developerUserId: null,
3120
+ sdkVersion: null,
3121
+ baseUrl: null,
3122
+ clock: { lastServerTime: null, lastClientTime: null, skewMs: null },
3123
+ entitlements: { count: 0, lastUpdated: 0, listenerErrors: 0 },
3124
+ events: {
3125
+ buffered: 0,
3126
+ dropped: 0,
3127
+ inFlight: 0,
3128
+ lastFlushAt: 0,
3129
+ lastError: null,
3130
+ consecutiveFailures: 0,
3131
+ nextRetryAt: null
3132
+ }
3133
+ };
3134
+ }
3135
+ const s = this.state;
3136
+ const skewMs = s.lastServerTime !== null && s.lastClientTime !== null ? s.lastClientTime - s.lastServerTime : null;
3137
+ return {
3138
+ started: true,
3139
+ anonymousId: s.identity.anonymousId,
3140
+ crossdeckCustomerId: s.identity.crossdeckCustomerId,
3141
+ developerUserId: s.developerUserId,
3142
+ sdkVersion: s.options.sdkVersion,
3143
+ baseUrl: s.options.baseUrl,
3144
+ clock: {
3145
+ lastServerTime: s.lastServerTime,
3146
+ lastClientTime: s.lastClientTime,
3147
+ skewMs
3148
+ },
3149
+ entitlements: {
3150
+ count: s.entitlements.list().length,
3151
+ lastUpdated: s.entitlements.freshness,
3152
+ listenerErrors: s.entitlements.listenerErrors
3153
+ },
3154
+ events: s.events.getStats()
3155
+ };
3156
+ }
3157
+ // ---------- private helpers ----------
3158
+ requireStarted() {
3159
+ if (!this.state) {
3160
+ throw new CrossdeckError({
3161
+ type: "configuration_error",
3162
+ code: "not_initialized",
3163
+ message: "Call Crossdeck.init({ appId, publicKey, environment }) before any other method."
3164
+ });
3165
+ }
3166
+ return this.state;
3167
+ }
3168
+ /**
3169
+ * Build the identity query for /v1/entitlements. Priority:
3170
+ * crossdeckCustomerId > developerUserId > anonymousId
3171
+ * — matches the resolveCrossdeckCustomerId precedence on the server.
3172
+ */
3173
+ identityQueryParams() {
3174
+ const s = this.requireStarted();
3175
+ if (s.identity.crossdeckCustomerId) {
3176
+ return { customerId: s.identity.crossdeckCustomerId };
3177
+ }
3178
+ if (s.developerUserId) return { userId: s.developerUserId };
3179
+ return { anonymousId: s.identity.anonymousId };
3180
+ }
3181
+ /**
3182
+ * Embed every known identity axis on the event. Earlier this returned
3183
+ * just the highest-priority hint (cdcust → developerUserId → anonymousId)
3184
+ * to keep payloads small, but that leaked into analytics: once a user
3185
+ * was logged in, every subsequent page.viewed shipped without
3186
+ * anonymousId, and `uniqExact(anonymous_id)` on the warehouse side
3187
+ * counted 0 visitors for the entire authenticated app.
3188
+ *
3189
+ * Bank-grade rule: the server is the single source of truth on
3190
+ * dedup. Send everything we know; let CH count by whichever axis
3191
+ * matches the question. Each field is at most 32 bytes — sending
3192
+ * three on every event costs ~80 bytes per request, which is
3193
+ * trivial compared to the analytics correctness it buys.
3194
+ */
3195
+ identityHintForEvent() {
3196
+ const s = this.requireStarted();
3197
+ const hint = {
3198
+ anonymousId: s.identity.anonymousId
3199
+ };
3200
+ if (s.developerUserId) hint.developerUserId = s.developerUserId;
3201
+ if (s.identity.crossdeckCustomerId) {
3202
+ hint.crossdeckCustomerId = s.identity.crossdeckCustomerId;
3203
+ }
3204
+ return hint;
3205
+ }
3206
+ mintEventId() {
3207
+ const ts = Date.now().toString(36);
3208
+ return `evt_${ts}${randomChars(8)}`;
3209
+ }
3210
+ };
3211
+ var Crossdeck = new CrossdeckClient();
3212
+ function inferEnvFromKey(publicKey) {
3213
+ if (publicKey.startsWith("cd_pub_test_")) return "sandbox";
3214
+ if (publicKey.startsWith("cd_pub_live_")) return "production";
3215
+ return null;
3216
+ }
3217
+ function isLocalHostname() {
3218
+ const w = globalThis.window;
3219
+ if (w?.__CROSSDECK_FORCE_LIVE__ === true) return false;
3220
+ const hostname = w?.location?.hostname;
3221
+ if (!hostname) return false;
3222
+ if (hostname === "localhost" || hostname === "127.0.0.1") return true;
3223
+ if (hostname === "::1" || hostname === "[::1]") return true;
3224
+ if (hostname.endsWith(".local")) return true;
3225
+ if (/^10\./.test(hostname)) return true;
3226
+ if (/^192\.168\./.test(hostname)) return true;
3227
+ if (/^172\.(1[6-9]|2\d|3[0-1])\./.test(hostname)) return true;
3228
+ return false;
3229
+ }
3230
+ function resolveAutoTrack(input) {
3231
+ if (input === false) {
3232
+ return {
3233
+ sessions: false,
3234
+ pageViews: false,
3235
+ deviceInfo: false,
3236
+ clicks: false,
3237
+ webVitals: false,
3238
+ errors: false
3239
+ };
3240
+ }
3241
+ if (input === void 0 || input === true) {
3242
+ return { ...DEFAULT_AUTO_TRACK };
3243
+ }
3244
+ return {
3245
+ sessions: input.sessions ?? DEFAULT_AUTO_TRACK.sessions,
3246
+ pageViews: input.pageViews ?? DEFAULT_AUTO_TRACK.pageViews,
3247
+ deviceInfo: input.deviceInfo ?? DEFAULT_AUTO_TRACK.deviceInfo,
3248
+ clicks: input.clicks ?? DEFAULT_AUTO_TRACK.clicks,
3249
+ webVitals: input.webVitals ?? DEFAULT_AUTO_TRACK.webVitals,
3250
+ errors: input.errors ?? DEFAULT_AUTO_TRACK.errors
3251
+ };
3252
+ }
3253
+ function installUnloadFlush(onUnload) {
3254
+ const w = globalThis.window;
3255
+ const doc = globalThis.document;
3256
+ if (!w || !doc) return () => void 0;
3257
+ const onVisChange = () => {
3258
+ if (doc.visibilityState === "hidden") onUnload();
3259
+ };
3260
+ const onTerminal = () => onUnload();
3261
+ doc.addEventListener("visibilitychange", onVisChange);
3262
+ w.addEventListener("pagehide", onTerminal);
3263
+ w.addEventListener("beforeunload", onTerminal);
3264
+ return () => {
3265
+ doc.removeEventListener("visibilitychange", onVisChange);
3266
+ w.removeEventListener("pagehide", onTerminal);
3267
+ w.removeEventListener("beforeunload", onTerminal);
3268
+ };
3269
+ }
3270
+
3271
+ // src/vue.ts
3272
+ function useEntitlement(key) {
3273
+ const r = ref(safeIsEntitled(key));
3274
+ onMounted(() => {
3275
+ r.value = safeIsEntitled(key);
3276
+ let unsubscribe = null;
3277
+ try {
3278
+ unsubscribe = Crossdeck.onEntitlementsChange(() => {
3279
+ r.value = safeIsEntitled(key);
3280
+ });
3281
+ } catch {
3282
+ }
3283
+ onScopeDispose(() => {
3284
+ if (unsubscribe) unsubscribe();
3285
+ });
3286
+ });
3287
+ return r;
3288
+ }
3289
+ function useEntitlements() {
3290
+ const r = ref(safeListKeys());
3291
+ onMounted(() => {
3292
+ r.value = safeListKeys();
3293
+ let unsubscribe = null;
3294
+ try {
3295
+ unsubscribe = Crossdeck.onEntitlementsChange((entitlements) => {
3296
+ r.value = entitlements.filter((e) => e.isActive).map((e) => e.key);
3297
+ });
3298
+ } catch {
3299
+ }
3300
+ onScopeDispose(() => {
3301
+ if (unsubscribe) unsubscribe();
3302
+ });
3303
+ });
3304
+ return r;
3305
+ }
3306
+ function safeIsEntitled(key) {
3307
+ try {
3308
+ return Crossdeck.isEntitled(key);
3309
+ } catch {
3310
+ return false;
3311
+ }
3312
+ }
3313
+ function safeListKeys() {
3314
+ try {
3315
+ return Crossdeck.listEntitlements().filter((e) => e.isActive).map((e) => e.key);
3316
+ } catch {
3317
+ return [];
3318
+ }
3319
+ }
3320
+ export {
3321
+ useEntitlement,
3322
+ useEntitlements
3323
+ };
3324
+ //# sourceMappingURL=vue.mjs.map