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