@cross-deck/web 1.5.2 → 1.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/vue.cjs CHANGED
@@ -92,7 +92,7 @@ function typeMapForStatus(status) {
92
92
  }
93
93
 
94
94
  // src/_version.ts
95
- var SDK_VERSION = "1.3.0";
95
+ var SDK_VERSION = "1.5.3";
96
96
  var SDK_NAME = "@cross-deck/web";
97
97
 
98
98
  // src/http.ts
@@ -155,7 +155,14 @@ var HttpClient = class {
155
155
  if (timeoutHandle !== null) clearTimeout(timeoutHandle);
156
156
  }
157
157
  if (!response.ok) {
158
- throw await crossdeckErrorFromResponse(response);
158
+ const parsed = await crossdeckErrorFromResponse(response);
159
+ if (this.config.onErrorParsed) {
160
+ try {
161
+ this.config.onErrorParsed(parsed);
162
+ } catch {
163
+ }
164
+ }
165
+ throw parsed;
159
166
  }
160
167
  if (response.status === 204) return void 0;
161
168
  try {
@@ -342,29 +349,258 @@ function randomChars(count) {
342
349
  return out.join("");
343
350
  }
344
351
 
352
+ // src/hash.ts
353
+ var K = new Uint32Array([
354
+ 1116352408,
355
+ 1899447441,
356
+ 3049323471,
357
+ 3921009573,
358
+ 961987163,
359
+ 1508970993,
360
+ 2453635748,
361
+ 2870763221,
362
+ 3624381080,
363
+ 310598401,
364
+ 607225278,
365
+ 1426881987,
366
+ 1925078388,
367
+ 2162078206,
368
+ 2614888103,
369
+ 3248222580,
370
+ 3835390401,
371
+ 4022224774,
372
+ 264347078,
373
+ 604807628,
374
+ 770255983,
375
+ 1249150122,
376
+ 1555081692,
377
+ 1996064986,
378
+ 2554220882,
379
+ 2821834349,
380
+ 2952996808,
381
+ 3210313671,
382
+ 3336571891,
383
+ 3584528711,
384
+ 113926993,
385
+ 338241895,
386
+ 666307205,
387
+ 773529912,
388
+ 1294757372,
389
+ 1396182291,
390
+ 1695183700,
391
+ 1986661051,
392
+ 2177026350,
393
+ 2456956037,
394
+ 2730485921,
395
+ 2820302411,
396
+ 3259730800,
397
+ 3345764771,
398
+ 3516065817,
399
+ 3600352804,
400
+ 4094571909,
401
+ 275423344,
402
+ 430227734,
403
+ 506948616,
404
+ 659060556,
405
+ 883997877,
406
+ 958139571,
407
+ 1322822218,
408
+ 1537002063,
409
+ 1747873779,
410
+ 1955562222,
411
+ 2024104815,
412
+ 2227730452,
413
+ 2361852424,
414
+ 2428436474,
415
+ 2756734187,
416
+ 3204031479,
417
+ 3329325298
418
+ ]);
419
+ function utf8Bytes(input) {
420
+ if (typeof TextEncoder !== "undefined") {
421
+ return new TextEncoder().encode(input);
422
+ }
423
+ const out = [];
424
+ for (let i = 0; i < input.length; i++) {
425
+ let codePoint = input.charCodeAt(i);
426
+ if (codePoint >= 55296 && codePoint <= 56319 && i + 1 < input.length) {
427
+ const next = input.charCodeAt(i + 1);
428
+ if (next >= 56320 && next <= 57343) {
429
+ codePoint = 65536 + (codePoint - 55296 << 10) + (next - 56320);
430
+ i++;
431
+ }
432
+ }
433
+ if (codePoint < 128) {
434
+ out.push(codePoint);
435
+ } else if (codePoint < 2048) {
436
+ out.push(192 | codePoint >> 6);
437
+ out.push(128 | codePoint & 63);
438
+ } else if (codePoint < 65536) {
439
+ out.push(224 | codePoint >> 12);
440
+ out.push(128 | codePoint >> 6 & 63);
441
+ out.push(128 | codePoint & 63);
442
+ } else {
443
+ out.push(240 | codePoint >> 18);
444
+ out.push(128 | codePoint >> 12 & 63);
445
+ out.push(128 | codePoint >> 6 & 63);
446
+ out.push(128 | codePoint & 63);
447
+ }
448
+ }
449
+ return new Uint8Array(out);
450
+ }
451
+ function sha256Hex(input) {
452
+ const bytes = utf8Bytes(input);
453
+ const bitLength = bytes.length * 8;
454
+ const blockCount = Math.floor((bytes.length + 9 + 63) / 64);
455
+ const padded = new Uint8Array(blockCount * 64);
456
+ padded.set(bytes);
457
+ padded[bytes.length] = 128;
458
+ const high = Math.floor(bitLength / 4294967296);
459
+ const low = bitLength >>> 0;
460
+ const lenOffset = padded.length - 8;
461
+ padded[lenOffset + 0] = high >>> 24 & 255;
462
+ padded[lenOffset + 1] = high >>> 16 & 255;
463
+ padded[lenOffset + 2] = high >>> 8 & 255;
464
+ padded[lenOffset + 3] = high & 255;
465
+ padded[lenOffset + 4] = low >>> 24 & 255;
466
+ padded[lenOffset + 5] = low >>> 16 & 255;
467
+ padded[lenOffset + 6] = low >>> 8 & 255;
468
+ padded[lenOffset + 7] = low & 255;
469
+ const H = new Uint32Array([
470
+ 1779033703,
471
+ 3144134277,
472
+ 1013904242,
473
+ 2773480762,
474
+ 1359893119,
475
+ 2600822924,
476
+ 528734635,
477
+ 1541459225
478
+ ]);
479
+ const W = new Uint32Array(64);
480
+ for (let block = 0; block < blockCount; block++) {
481
+ const offset = block * 64;
482
+ for (let t = 0; t < 16; t++) {
483
+ W[t] = (padded[offset + t * 4] << 24 | padded[offset + t * 4 + 1] << 16 | padded[offset + t * 4 + 2] << 8 | padded[offset + t * 4 + 3]) >>> 0;
484
+ }
485
+ for (let t = 16; t < 64; t++) {
486
+ const w15 = W[t - 15];
487
+ const w2 = W[t - 2];
488
+ const s0 = (w15 >>> 7 | w15 << 25) ^ (w15 >>> 18 | w15 << 14) ^ w15 >>> 3;
489
+ const s1 = (w2 >>> 17 | w2 << 15) ^ (w2 >>> 19 | w2 << 13) ^ w2 >>> 10;
490
+ W[t] = W[t - 16] + s0 + W[t - 7] + s1 >>> 0;
491
+ }
492
+ let a = H[0], b = H[1], c = H[2], d = H[3];
493
+ let e = H[4], f = H[5], g = H[6], h = H[7];
494
+ for (let t = 0; t < 64; t++) {
495
+ const S1 = (e >>> 6 | e << 26) ^ (e >>> 11 | e << 21) ^ (e >>> 25 | e << 7);
496
+ const ch = e & f ^ ~e & g;
497
+ const temp1 = h + S1 + ch + K[t] + W[t] >>> 0;
498
+ const S0 = (a >>> 2 | a << 30) ^ (a >>> 13 | a << 19) ^ (a >>> 22 | a << 10);
499
+ const maj = a & b ^ a & c ^ b & c;
500
+ const temp2 = S0 + maj >>> 0;
501
+ h = g;
502
+ g = f;
503
+ f = e;
504
+ e = d + temp1 >>> 0;
505
+ d = c;
506
+ c = b;
507
+ b = a;
508
+ a = temp1 + temp2 >>> 0;
509
+ }
510
+ H[0] = H[0] + a >>> 0;
511
+ H[1] = H[1] + b >>> 0;
512
+ H[2] = H[2] + c >>> 0;
513
+ H[3] = H[3] + d >>> 0;
514
+ H[4] = H[4] + e >>> 0;
515
+ H[5] = H[5] + f >>> 0;
516
+ H[6] = H[6] + g >>> 0;
517
+ H[7] = H[7] + h >>> 0;
518
+ }
519
+ let hex = "";
520
+ for (let i = 0; i < 8; i++) {
521
+ hex += H[i].toString(16).padStart(8, "0");
522
+ }
523
+ return hex;
524
+ }
525
+
345
526
  // src/entitlement-cache.ts
346
527
  var DEFAULT_STALE_AFTER_MS = 24 * 60 * 60 * 1e3;
347
- var EntitlementCache = class {
528
+ var ANON_SUFFIX = "_anon";
529
+ var INDEX_SUFFIX = "_index";
530
+ var EntitlementCache = class _EntitlementCache {
348
531
  /**
349
- * @param storage Device storage adapter. When omitted (tests) or
350
- * a MemoryStorage (strict-consent / no-persistence
351
- * mode) the cache is session-only — durability is
352
- * simply absent, never wrong.
353
- * @param storageKey Full key the persisted blob lives under.
354
- * @param staleAfterMs Age past which last-known-good is flagged stale
355
- * even without a failed refresh. Default 24h.
532
+ * @param storage Device storage adapter. When omitted (tests) or
533
+ * a MemoryStorage (strict-consent / no-persistence
534
+ * mode) the cache is session-only — durability is
535
+ * simply absent, never wrong.
536
+ * @param storageKeyPrefix Prefix used to derive per-user storage keys
537
+ * (`<prefix>:<sha256(userId)>`). Default
538
+ * `crossdeck:entitlements`. The trailing user
539
+ * suffix is filled at identify() / reset()
540
+ * time — see [[setUserKey]].
541
+ * @param staleAfterMs Age past which last-known-good is flagged stale
542
+ * even without a failed refresh. Default 24h.
356
543
  */
357
- constructor(storage, storageKey = "crossdeck:entitlements", staleAfterMs = DEFAULT_STALE_AFTER_MS) {
544
+ constructor(storage, storageKeyPrefix = "crossdeck:entitlements", staleAfterMs = DEFAULT_STALE_AFTER_MS) {
358
545
  this.all = [];
359
546
  this.lastUpdated = 0;
360
547
  this.lastRefreshFailedAt = 0;
361
548
  this.listeners = /* @__PURE__ */ new Set();
362
549
  this.listenerErrorCount = 0;
550
+ this.currentSuffix = ANON_SUFFIX;
363
551
  this.storage = storage;
364
- this.storageKey = storageKey;
552
+ this.storageKeyPrefix = storageKeyPrefix;
365
553
  this.staleAfterMs = staleAfterMs;
366
554
  this.hydrate();
367
555
  }
556
+ /** The full storage key the current-user blob is persisted under. */
557
+ get storageKey() {
558
+ return `${this.storageKeyPrefix}:${this.currentSuffix}`;
559
+ }
560
+ /** Key of the index blob — a JSON array of every suffix we've
561
+ * written. Used by clearAll() to scope a logout-wipe. */
562
+ get indexKey() {
563
+ return `${this.storageKeyPrefix}:${INDEX_SUFFIX}`;
564
+ }
565
+ /** Derive a stable suffix for a developerUserId via SHA-256. The
566
+ * raw userId never lands in the storage key — protects against
567
+ * accidentally leaking email-style identifiers through DevTools
568
+ * inspection. Pass `null` to switch back to the anonymous slot. */
569
+ static suffixForUserId(userId) {
570
+ if (userId == null || userId === "") return ANON_SUFFIX;
571
+ return sha256Hex(userId);
572
+ }
573
+ /**
574
+ * Switch the cache to a different user's storage slot. Bank-grade
575
+ * three-layer isolation:
576
+ * (a) Physical key separation — `<prefix>:<sha256(userId)>` so
577
+ * a user-switch can't physically read prior user's data
578
+ * even if the in-memory clear was skipped.
579
+ * (b) Unconditional in-memory clear — invoked whenever the
580
+ * active suffix changes, even on same-id re-identify.
581
+ * (c) Re-hydrate from the new slot — a returning user observes
582
+ * their last-known-good cache from storage immediately.
583
+ *
584
+ * Caller (identify() / reset()) MUST invoke this BEFORE the next
585
+ * setFromList() so the write lands under the right key.
586
+ */
587
+ setUserKey(userId) {
588
+ const nextSuffix = _EntitlementCache.suffixForUserId(userId);
589
+ if (nextSuffix === this.currentSuffix) {
590
+ this.all = [];
591
+ this.lastUpdated = 0;
592
+ this.lastRefreshFailedAt = 0;
593
+ this.notify();
594
+ this.hydrate();
595
+ return;
596
+ }
597
+ this.currentSuffix = nextSuffix;
598
+ this.all = [];
599
+ this.lastUpdated = 0;
600
+ this.lastRefreshFailedAt = 0;
601
+ this.hydrate();
602
+ this.notify();
603
+ }
368
604
  /**
369
605
  * Sync read — true iff the entitlement is currently granting access.
370
606
  *
@@ -434,12 +670,13 @@ var EntitlementCache = class {
434
670
  this.lastUpdated = Date.now();
435
671
  this.lastRefreshFailedAt = 0;
436
672
  this.persist();
673
+ this.recordSuffixInIndex(this.currentSuffix);
437
674
  this.notify();
438
675
  }
439
676
  /**
440
- * Wipe used on reset() (logout) and on an identity switch. Clears
441
- * BOTH memory and durable storage so a prior user's entitlements can
442
- * never leak to the next person on this device.
677
+ * Wipe the CURRENT user's slot. Used internally when a single
678
+ * user's cache needs to be invalidated without affecting other
679
+ * persisted slots. The full-logout path is [[clearAll]].
443
680
  */
444
681
  clear() {
445
682
  this.all = [];
@@ -451,6 +688,40 @@ var EntitlementCache = class {
451
688
  } catch {
452
689
  }
453
690
  }
691
+ this.removeSuffixFromIndex(this.currentSuffix);
692
+ this.notify();
693
+ }
694
+ /**
695
+ * Logout-grade wipe — bank-grade contract: removes EVERY per-user
696
+ * entitlement slot the SDK has ever written on this device, then
697
+ * clears the index. Used by `Crossdeck.reset()` so a logout on a
698
+ * shared device can never leave another user's entitlements
699
+ * readable (layer (c) of the v1.4.0 isolation fix).
700
+ *
701
+ * After clearAll(), the cache is back to anonymous + empty.
702
+ */
703
+ clearAll() {
704
+ this.all = [];
705
+ this.lastUpdated = 0;
706
+ this.lastRefreshFailedAt = 0;
707
+ this.currentSuffix = ANON_SUFFIX;
708
+ if (this.storage) {
709
+ const suffixes = this.readIndex();
710
+ for (const suffix of suffixes) {
711
+ try {
712
+ this.storage.removeItem(`${this.storageKeyPrefix}:${suffix}`);
713
+ } catch {
714
+ }
715
+ }
716
+ try {
717
+ this.storage.removeItem(`${this.storageKeyPrefix}:${ANON_SUFFIX}`);
718
+ } catch {
719
+ }
720
+ try {
721
+ this.storage.removeItem(this.indexKey);
722
+ } catch {
723
+ }
724
+ }
454
725
  this.notify();
455
726
  }
456
727
  /**
@@ -500,6 +771,47 @@ var EntitlementCache = class {
500
771
  } catch {
501
772
  }
502
773
  }
774
+ /** Read the index of all per-user suffixes the SDK has written. */
775
+ readIndex() {
776
+ if (!this.storage) return [];
777
+ try {
778
+ const raw = this.storage.getItem(this.indexKey);
779
+ if (!raw) return [];
780
+ const parsed = JSON.parse(raw);
781
+ if (Array.isArray(parsed)) {
782
+ return parsed.filter((x) => typeof x === "string");
783
+ }
784
+ return [];
785
+ } catch {
786
+ return [];
787
+ }
788
+ }
789
+ /** Add a suffix to the persisted index. Idempotent. */
790
+ recordSuffixInIndex(suffix) {
791
+ if (!this.storage) return;
792
+ const existing = this.readIndex();
793
+ if (existing.includes(suffix)) return;
794
+ existing.push(suffix);
795
+ try {
796
+ this.storage.setItem(this.indexKey, JSON.stringify(existing));
797
+ } catch {
798
+ }
799
+ }
800
+ /** Remove a suffix from the persisted index. No-op if absent. */
801
+ removeSuffixFromIndex(suffix) {
802
+ if (!this.storage) return;
803
+ const existing = this.readIndex();
804
+ const next = existing.filter((s) => s !== suffix);
805
+ if (next.length === existing.length) return;
806
+ try {
807
+ if (next.length === 0) {
808
+ this.storage.removeItem(this.indexKey);
809
+ } else {
810
+ this.storage.setItem(this.indexKey, JSON.stringify(next));
811
+ }
812
+ } catch {
813
+ }
814
+ }
503
815
  notify() {
504
816
  if (this.listeners.size === 0) return;
505
817
  const snapshot = this.all.slice();
@@ -514,6 +826,34 @@ var EntitlementCache = class {
514
826
  }
515
827
  };
516
828
 
829
+ // src/idempotency-key.ts
830
+ function formatAsUuid(hex) {
831
+ return [
832
+ hex.slice(0, 8),
833
+ hex.slice(8, 12),
834
+ hex.slice(12, 16),
835
+ hex.slice(16, 20),
836
+ hex.slice(20, 32)
837
+ ].join("-");
838
+ }
839
+ function deriveIdempotencyKeyForPurchase(body) {
840
+ let identifier;
841
+ if (body.rail === "apple") {
842
+ identifier = body.signedTransactionInfo ?? "";
843
+ } else if (body.rail === "google") {
844
+ identifier = body.purchaseToken ?? "";
845
+ } else {
846
+ identifier = "";
847
+ }
848
+ if (!identifier) {
849
+ throw new Error(
850
+ `deriveIdempotencyKeyForPurchase: no stable identifier in body (rail=${body.rail}). Apple needs signedTransactionInfo; Google needs purchaseToken.`
851
+ );
852
+ }
853
+ const namespaced = `crossdeck:purchases/sync:${body.rail}:${identifier}`;
854
+ return formatAsUuid(sha256Hex(namespaced));
855
+ }
856
+
517
857
  // src/retry-policy.ts
518
858
  var DEFAULT_BASE = 1e3;
519
859
  var DEFAULT_MAX = 6e4;
@@ -2058,6 +2398,789 @@ var BreadcrumbBuffer = class {
2058
2398
  }
2059
2399
  };
2060
2400
 
2401
+ // src/_diagnostic-telemetry.ts
2402
+ var DIAGNOSTIC_TELEMETRY_ENDPOINT = "https://api.cross-deck.com/v1/sdk/diagnostic";
2403
+ var DIAGNOSTIC_TELEMETRY_PUBLISHABLE_KEY = "cd_pub_live_9490e7aa029c432abf";
2404
+ function isDiagnosticTelemetryEnabled() {
2405
+ return !DIAGNOSTIC_TELEMETRY_PUBLISHABLE_KEY.startsWith(
2406
+ "cd_pub_RELIABILITY_PLACEHOLDER"
2407
+ );
2408
+ }
2409
+ var DIAGNOSTIC_TELEMETRY_ALLOWED_KEYS = /* @__PURE__ */ new Set([
2410
+ "contract_id",
2411
+ "sdk_version",
2412
+ "sdk_platform",
2413
+ "failure_reason",
2414
+ "run_context",
2415
+ "run_id",
2416
+ "test_file",
2417
+ "test_name",
2418
+ "device_class",
2419
+ // verification_phase is set by the runtime contract verifier layer
2420
+ // (sdks/web/src/_contract-verifiers.ts) — values `boot` / `hot_path`.
2421
+ // Absent for failures emitted by external test harnesses
2422
+ // (XCTestObservation, Vitest hooks, JUnit watchers) which carry
2423
+ // test_file + test_name instead. See contracts/diagnostics/
2424
+ // contract-failed-payload-schema-lock.json.
2425
+ "verification_phase"
2426
+ ]);
2427
+ function filterDiagnosticPayload(payload) {
2428
+ const filtered = {};
2429
+ for (const [k, v] of Object.entries(payload)) {
2430
+ if (DIAGNOSTIC_TELEMETRY_ALLOWED_KEYS.has(k) && typeof v === "string") {
2431
+ filtered[k] = v;
2432
+ }
2433
+ }
2434
+ return filtered;
2435
+ }
2436
+ function sendDiagnosticTelemetry(payload) {
2437
+ if (!isDiagnosticTelemetryEnabled()) return;
2438
+ const filtered = filterDiagnosticPayload(payload);
2439
+ if (Object.keys(filtered).length === 0) return;
2440
+ const body = JSON.stringify(filtered);
2441
+ const f = globalThis.fetch;
2442
+ if (typeof f !== "function") return;
2443
+ try {
2444
+ void f(DIAGNOSTIC_TELEMETRY_ENDPOINT, {
2445
+ method: "POST",
2446
+ headers: {
2447
+ "Content-Type": "application/json",
2448
+ Authorization: `Bearer ${DIAGNOSTIC_TELEMETRY_PUBLISHABLE_KEY}`,
2449
+ "Crossdeck-Sdk-Version": `${SDK_NAME}@${SDK_VERSION}`
2450
+ },
2451
+ body,
2452
+ keepalive: true,
2453
+ // No credentials, no cache, no referrer — the reliability
2454
+ // endpoint is the same origin only in tests. In production the
2455
+ // browser never carries anything beyond the request body and
2456
+ // the Authorization header we set explicitly.
2457
+ credentials: "omit",
2458
+ cache: "no-store",
2459
+ referrerPolicy: "no-referrer"
2460
+ }).catch(() => {
2461
+ });
2462
+ } catch {
2463
+ }
2464
+ }
2465
+
2466
+ // src/_contract-verifiers.ts
2467
+ function buildVerifierContext(opts) {
2468
+ return {
2469
+ sdkVersion: `${SDK_NAME}@${SDK_VERSION}`,
2470
+ runId: `cd_verify_${randomHex(8)}`,
2471
+ runContext: opts.runContext ?? "customer-app",
2472
+ logVerifierResults: opts.logVerifierResults,
2473
+ disableContractAssertions: opts.disableContractAssertions,
2474
+ console,
2475
+ emitTelemetry: sendDiagnosticTelemetry
2476
+ };
2477
+ }
2478
+ var VerifierReporter = class {
2479
+ constructor(ctx) {
2480
+ this.ctx = ctx;
2481
+ this.reentrancyDepth = 0;
2482
+ }
2483
+ /**
2484
+ * Report a verifier result. Pass `operation` for hot-path results
2485
+ * to render the prefix as `[crossdeck.identify]` etc.; omit for
2486
+ * boot results which render as `[crossdeck]`.
2487
+ */
2488
+ report(result, phase, operation) {
2489
+ if (this.ctx.disableContractAssertions) return;
2490
+ if (result.ok) {
2491
+ this.reportPass(result, phase, operation);
2492
+ } else {
2493
+ this.reportFail(result, phase, operation);
2494
+ }
2495
+ }
2496
+ reportPass(result, phase, operation) {
2497
+ if (!this.ctx.logVerifierResults) return;
2498
+ const line = formatPassLine(result, operation);
2499
+ if (phase === "boot") {
2500
+ this.ctx.console.info(line);
2501
+ } else {
2502
+ this.ctx.console.debug(line);
2503
+ }
2504
+ }
2505
+ reportFail(result, phase, operation) {
2506
+ this.ctx.console.warn(formatFailLine(result, operation));
2507
+ if (this.reentrancyDepth > 0) return;
2508
+ this.reentrancyDepth += 1;
2509
+ try {
2510
+ this.ctx.emitTelemetry({
2511
+ contract_id: result.contractId,
2512
+ sdk_version: this.ctx.sdkVersion,
2513
+ sdk_platform: "web",
2514
+ failure_reason: truncate(result.failureReason, 128),
2515
+ run_context: this.ctx.runContext,
2516
+ run_id: this.ctx.runId,
2517
+ verification_phase: phase
2518
+ });
2519
+ } finally {
2520
+ this.reentrancyDepth -= 1;
2521
+ }
2522
+ }
2523
+ };
2524
+ function formatPassLine(r, operation) {
2525
+ const prefix = operation ? `[crossdeck.${operation}]` : "[crossdeck]";
2526
+ return `${prefix} \u2713 ${r.contractId} \u2014 ${r.evidence} (${r.durationMs}ms)`;
2527
+ }
2528
+ function formatFailLine(r, operation) {
2529
+ const prefix = operation ? `[crossdeck.${operation}]` : "[crossdeck]";
2530
+ return `${prefix} \u2717 ${r.contractId} \u2014 ${r.failureReason} (${r.durationMs}ms)`;
2531
+ }
2532
+ var VERIFIER_PER_USER_CACHE_ISOLATION = {
2533
+ contractId: "per-user-cache-isolation",
2534
+ bootTest() {
2535
+ const t0 = nowMs();
2536
+ try {
2537
+ const storage = new MemoryStorage2();
2538
+ const cache = new EntitlementCache(storage, "_verifier");
2539
+ cache.setUserKey("user_A");
2540
+ cache.setFromList([
2541
+ {
2542
+ object: "entitlement",
2543
+ key: "pro",
2544
+ isActive: true,
2545
+ validUntil: null,
2546
+ source: {
2547
+ rail: "stripe",
2548
+ productId: "p_verifier",
2549
+ subscriptionId: "s_verifier"
2550
+ },
2551
+ updatedAt: Date.now()
2552
+ }
2553
+ ]);
2554
+ cache.setUserKey("user_B");
2555
+ const inMemoryB = cache.list();
2556
+ if (inMemoryB.length !== 0) {
2557
+ return fail(
2558
+ "per-user-cache-isolation",
2559
+ "in-memory snapshot still carried user_A's entitlements after rotation",
2560
+ nowMs() - t0
2561
+ );
2562
+ }
2563
+ const suffixA = EntitlementCache.suffixForUserId("user_A");
2564
+ const suffixB = EntitlementCache.suffixForUserId("user_B");
2565
+ if (suffixA === suffixB) {
2566
+ return fail(
2567
+ "per-user-cache-isolation",
2568
+ "suffixes for user_A and user_B collided",
2569
+ nowMs() - t0
2570
+ );
2571
+ }
2572
+ const storageA = storage.getItem(`_verifier:${suffixA}`);
2573
+ const storageB = storage.getItem(`_verifier:${suffixB}`);
2574
+ if (!storageA) {
2575
+ return fail(
2576
+ "per-user-cache-isolation",
2577
+ "user_A's storage slot was wiped on rotation (expected isolation, not erasure)",
2578
+ nowMs() - t0
2579
+ );
2580
+ }
2581
+ if (storageB) {
2582
+ return fail(
2583
+ "per-user-cache-isolation",
2584
+ "user_B's storage slot already contained data before any write",
2585
+ nowMs() - t0
2586
+ );
2587
+ }
2588
+ return pass(
2589
+ "per-user-cache-isolation",
2590
+ `slot rotated A:${shortSuffix(suffixA)} \u2192 B:${shortSuffix(suffixB)} (isolated, physically separate)`,
2591
+ nowMs() - t0
2592
+ );
2593
+ } catch (err) {
2594
+ return fail(
2595
+ "per-user-cache-isolation",
2596
+ `boot test threw: ${err.message?.slice(0, 80) ?? "unknown error"}`,
2597
+ nowMs() - t0
2598
+ );
2599
+ }
2600
+ },
2601
+ hooks: {
2602
+ onIdentify(obs) {
2603
+ const t0 = nowMs();
2604
+ const priorSuffix = EntitlementCache.suffixForUserId(obs.priorUserId);
2605
+ const nextSuffix = EntitlementCache.suffixForUserId(obs.nextUserId);
2606
+ if (priorSuffix !== nextSuffix) {
2607
+ if (obs.cache.list().length !== 0) {
2608
+ return fail(
2609
+ "per-user-cache-isolation",
2610
+ "in-memory snapshot still held entitlements after slot rotation",
2611
+ nowMs() - t0
2612
+ );
2613
+ }
2614
+ return pass(
2615
+ "per-user-cache-isolation",
2616
+ `slot rotated ${shortSuffix(priorSuffix)} \u2192 ${shortSuffix(nextSuffix)}`,
2617
+ nowMs() - t0
2618
+ );
2619
+ }
2620
+ return pass(
2621
+ "per-user-cache-isolation",
2622
+ `same-id re-identify (suffix ${shortSuffix(nextSuffix)}); cleared + rehydrated per contract`,
2623
+ nowMs() - t0
2624
+ );
2625
+ }
2626
+ }
2627
+ };
2628
+ var CANONICAL_APPLE_JWS = "eyJ.jws.sig";
2629
+ var CANONICAL_APPLE_IDEMPOTENCY_KEY = "a66b1640-efaf-bb4d-1261-6650033bf111";
2630
+ var VERIFIER_IDEMPOTENCY_KEY_DETERMINISTIC = {
2631
+ contractId: "idempotency-key-deterministic",
2632
+ bootTest() {
2633
+ const t0 = nowMs();
2634
+ try {
2635
+ const derived = deriveIdempotencyKeyForPurchase({
2636
+ rail: "apple",
2637
+ signedTransactionInfo: CANONICAL_APPLE_JWS
2638
+ });
2639
+ if (derived !== CANONICAL_APPLE_IDEMPOTENCY_KEY) {
2640
+ return fail(
2641
+ "idempotency-key-deterministic",
2642
+ `canonical apple JWS derived ${derived} (expected ${CANONICAL_APPLE_IDEMPOTENCY_KEY})`,
2643
+ nowMs() - t0
2644
+ );
2645
+ }
2646
+ const second = deriveIdempotencyKeyForPurchase({
2647
+ rail: "apple",
2648
+ signedTransactionInfo: CANONICAL_APPLE_JWS
2649
+ });
2650
+ if (second !== derived) {
2651
+ return fail(
2652
+ "idempotency-key-deterministic",
2653
+ "same JWS produced different keys on two derivations",
2654
+ nowMs() - t0
2655
+ );
2656
+ }
2657
+ const appleKey = derived;
2658
+ const googleKey = deriveIdempotencyKeyForPurchase({
2659
+ rail: "google",
2660
+ purchaseToken: CANONICAL_APPLE_JWS
2661
+ });
2662
+ if (appleKey === googleKey) {
2663
+ return fail(
2664
+ "idempotency-key-deterministic",
2665
+ "rail namespacing failed \u2014 apple/google identical key",
2666
+ nowMs() - t0
2667
+ );
2668
+ }
2669
+ return pass(
2670
+ "idempotency-key-deterministic",
2671
+ `apple JWS \u2192 ${derived} (canonical vector + determinism + rail isolation)`,
2672
+ nowMs() - t0
2673
+ );
2674
+ } catch (err) {
2675
+ return fail(
2676
+ "idempotency-key-deterministic",
2677
+ `boot test threw: ${err.message?.slice(0, 80) ?? "unknown error"}`,
2678
+ nowMs() - t0
2679
+ );
2680
+ }
2681
+ },
2682
+ hooks: {
2683
+ onSyncPurchases(obs) {
2684
+ const t0 = nowMs();
2685
+ try {
2686
+ const expected = deriveIdempotencyKeyForPurchase(
2687
+ obs.rail === "apple" ? { rail: "apple", signedTransactionInfo: obs.stableIdentifier } : { rail: obs.rail, purchaseToken: obs.stableIdentifier }
2688
+ );
2689
+ if (expected !== obs.derivedKey) {
2690
+ return fail(
2691
+ "idempotency-key-deterministic",
2692
+ `derived key drifted from canonical algorithm`,
2693
+ nowMs() - t0
2694
+ );
2695
+ }
2696
+ return pass(
2697
+ "idempotency-key-deterministic",
2698
+ `${obs.rail} \u2192 ${obs.derivedKey}`,
2699
+ nowMs() - t0
2700
+ );
2701
+ } catch (err) {
2702
+ return fail(
2703
+ "idempotency-key-deterministic",
2704
+ `hot-path derivation threw: ${err.message?.slice(0, 80) ?? "unknown error"}`,
2705
+ nowMs() - t0
2706
+ );
2707
+ }
2708
+ }
2709
+ }
2710
+ };
2711
+ var VERIFIER_ERROR_ENVELOPE_SHAPE = {
2712
+ contractId: "error-envelope-shape",
2713
+ bootTest() {
2714
+ const t0 = nowMs();
2715
+ const wire = {
2716
+ error: {
2717
+ type: "invalid_request_error",
2718
+ code: "missing_customer",
2719
+ message: "Customer identifier is required.",
2720
+ request_id: "req_test1234"
2721
+ }
2722
+ };
2723
+ const ALLOWED_TYPES = /* @__PURE__ */ new Set([
2724
+ "authentication_error",
2725
+ "permission_error",
2726
+ "invalid_request_error",
2727
+ "rate_limit_error",
2728
+ "internal_error"
2729
+ ]);
2730
+ const err = wire.error;
2731
+ const missing = ["type", "code", "message", "request_id"].filter(
2732
+ (k) => !(k in err) || typeof err[k] !== "string"
2733
+ );
2734
+ if (missing.length > 0) {
2735
+ return fail(
2736
+ "error-envelope-shape",
2737
+ `envelope missing required fields: ${missing.join(", ")}`,
2738
+ nowMs() - t0
2739
+ );
2740
+ }
2741
+ if (!ALLOWED_TYPES.has(err.type)) {
2742
+ return fail(
2743
+ "error-envelope-shape",
2744
+ `error.type "${err.type}" not in canonical ApiErrorType set`,
2745
+ nowMs() - t0
2746
+ );
2747
+ }
2748
+ return pass(
2749
+ "error-envelope-shape",
2750
+ "{ type, code, message, request_id } parsed and type \u2208 ApiErrorType",
2751
+ nowMs() - t0
2752
+ );
2753
+ },
2754
+ hooks: {
2755
+ onErrorParse(obs) {
2756
+ const t0 = nowMs();
2757
+ const ALLOWED_TYPES = /* @__PURE__ */ new Set([
2758
+ "authentication_error",
2759
+ "permission_error",
2760
+ "invalid_request_error",
2761
+ "rate_limit_error",
2762
+ "internal_error"
2763
+ ]);
2764
+ if (!ALLOWED_TYPES.has(obs.errorType)) {
2765
+ return fail(
2766
+ "error-envelope-shape",
2767
+ `wire error.type "${obs.errorType}" outside canonical ApiErrorType`,
2768
+ nowMs() - t0
2769
+ );
2770
+ }
2771
+ if (!obs.errorCode || obs.errorCode.length === 0) {
2772
+ return fail(
2773
+ "error-envelope-shape",
2774
+ "wire error.code was empty",
2775
+ nowMs() - t0
2776
+ );
2777
+ }
2778
+ return pass(
2779
+ "error-envelope-shape",
2780
+ `${obs.errorType}/${obs.errorCode} on ${obs.httpStatus}${obs.requestId ? ` (${obs.requestId.slice(0, 12)}\u2026)` : ""}`,
2781
+ nowMs() - t0
2782
+ );
2783
+ }
2784
+ }
2785
+ };
2786
+ var VERIFIER_FLUSH_INTERVAL_PARITY = {
2787
+ contractId: "flush-interval-parity",
2788
+ // This verifier reads the configured value off the live SDK, so
2789
+ // we expose it as a closure-bound bootTest constructed by the SDK
2790
+ // at start(). The bare bootTest below provides the canonical
2791
+ // default-value smoke test against the SDK's source-of-truth
2792
+ // constant.
2793
+ bootTest() {
2794
+ const t0 = nowMs();
2795
+ const CANONICAL_DEFAULT_MS = 2e3;
2796
+ if (CANONICAL_DEFAULT_MS !== 2e3) {
2797
+ return fail(
2798
+ "flush-interval-parity",
2799
+ `canonical default drifted from 2000ms`,
2800
+ nowMs() - t0
2801
+ );
2802
+ }
2803
+ return pass(
2804
+ "flush-interval-parity",
2805
+ "eventFlushIntervalMs default = 2000ms (Web/Node/RN/Swift/Android parity)",
2806
+ nowMs() - t0
2807
+ );
2808
+ }
2809
+ // No hot-path hook — the flush interval is set once at start() and
2810
+ // never changes per-operation.
2811
+ };
2812
+ function buildFlushIntervalVerifier(configuredIntervalMs) {
2813
+ return {
2814
+ contractId: "flush-interval-parity",
2815
+ bootTest() {
2816
+ const t0 = nowMs();
2817
+ const CANONICAL_DEFAULT_MS = 2e3;
2818
+ if (configuredIntervalMs < 100 || configuredIntervalMs > 6e4) {
2819
+ return fail(
2820
+ "flush-interval-parity",
2821
+ `configured eventFlushIntervalMs=${configuredIntervalMs} outside reasonable bounds [100, 60000]`,
2822
+ nowMs() - t0
2823
+ );
2824
+ }
2825
+ if (configuredIntervalMs !== CANONICAL_DEFAULT_MS) {
2826
+ return pass(
2827
+ "flush-interval-parity",
2828
+ `eventFlushIntervalMs = ${configuredIntervalMs}ms (override; canonical default is 2000ms)`,
2829
+ nowMs() - t0
2830
+ );
2831
+ }
2832
+ return pass(
2833
+ "flush-interval-parity",
2834
+ "eventFlushIntervalMs = 2000ms (canonical default)",
2835
+ nowMs() - t0
2836
+ );
2837
+ }
2838
+ };
2839
+ }
2840
+ var VERIFIER_SUPER_PROPERTY_MERGE_PRECEDENCE = {
2841
+ contractId: "super-property-merge-precedence",
2842
+ bootTest() {
2843
+ const t0 = nowMs();
2844
+ const device = { plan: "device_plan", os: "macos" };
2845
+ const superProps = { plan: "super_plan", appVersion: "1.0.0" };
2846
+ const caller = { plan: "caller_plan", eventSpecific: true };
2847
+ const merged = { ...device, ...superProps, ...caller };
2848
+ if (merged.plan !== "caller_plan") {
2849
+ return fail(
2850
+ "super-property-merge-precedence",
2851
+ `merged.plan = "${merged.plan}" (expected "caller_plan"; caller must override super and device)`,
2852
+ nowMs() - t0
2853
+ );
2854
+ }
2855
+ if (merged.appVersion !== "1.0.0") {
2856
+ return fail(
2857
+ "super-property-merge-precedence",
2858
+ "super-property appVersion was clobbered by device or caller",
2859
+ nowMs() - t0
2860
+ );
2861
+ }
2862
+ if (merged.os !== "macos") {
2863
+ return fail(
2864
+ "super-property-merge-precedence",
2865
+ "device property os was dropped from merged result",
2866
+ nowMs() - t0
2867
+ );
2868
+ }
2869
+ return pass(
2870
+ "super-property-merge-precedence",
2871
+ "caller > super > device verified (synthetic merge)",
2872
+ nowMs() - t0
2873
+ );
2874
+ },
2875
+ hooks: {
2876
+ onTrack(obs) {
2877
+ const t0 = nowMs();
2878
+ for (const [k, v] of Object.entries(obs.callerProperties)) {
2879
+ if (obs.mergedProperties[k] !== v) {
2880
+ return fail(
2881
+ "super-property-merge-precedence",
2882
+ `caller key "${k}" did not win in merged output`,
2883
+ nowMs() - t0
2884
+ );
2885
+ }
2886
+ }
2887
+ for (const [k, v] of Object.entries(obs.superProperties)) {
2888
+ if (k in obs.callerProperties) continue;
2889
+ if (obs.mergedProperties[k] !== v) {
2890
+ return fail(
2891
+ "super-property-merge-precedence",
2892
+ `super key "${k}" did not win over device in merged output`,
2893
+ nowMs() - t0
2894
+ );
2895
+ }
2896
+ }
2897
+ return pass(
2898
+ "super-property-merge-precedence",
2899
+ `caller(${Object.keys(obs.callerProperties).length}) > super(${Object.keys(obs.superProperties).length}) > device verified`,
2900
+ nowMs() - t0
2901
+ );
2902
+ }
2903
+ }
2904
+ };
2905
+ var CONTRACT_FAILED_REQUIRED_FIELDS = [
2906
+ "contract_id",
2907
+ "sdk_version",
2908
+ "sdk_platform",
2909
+ "failure_reason",
2910
+ "run_context",
2911
+ "run_id"
2912
+ ];
2913
+ var CONTRACT_FAILED_OPTIONAL_FIELDS = [
2914
+ "test_file",
2915
+ "test_name",
2916
+ "device_class",
2917
+ "verification_phase"
2918
+ ];
2919
+ var CONTRACT_FAILED_FORBIDDEN_FIELDS = [
2920
+ // The legitimate-interest analysis fails the moment any of these
2921
+ // appear on the wire. The list is conservative — anything that
2922
+ // could re-link a payload to an end-user.
2923
+ "anonymousId",
2924
+ "developerUserId",
2925
+ "crossdeckCustomerId",
2926
+ "email",
2927
+ "userId",
2928
+ "ip",
2929
+ "ipAddress",
2930
+ "userAgent",
2931
+ "stack",
2932
+ "stackTrace",
2933
+ "url",
2934
+ "referrer",
2935
+ "deviceId"
2936
+ ];
2937
+ var VERIFIER_CONTRACT_FAILED_PAYLOAD_SCHEMA_LOCK = {
2938
+ contractId: "contract-failed-payload-schema-lock",
2939
+ bootTest() {
2940
+ const t0 = nowMs();
2941
+ const syntheticPayload = {
2942
+ contract_id: "synthetic",
2943
+ sdk_version: SDK_VERSION,
2944
+ sdk_platform: "web",
2945
+ failure_reason: "synthetic",
2946
+ run_context: "customer-app",
2947
+ run_id: "synthetic-run-id",
2948
+ verification_phase: "boot"
2949
+ };
2950
+ const keys = Object.keys(syntheticPayload);
2951
+ const allowed = /* @__PURE__ */ new Set([
2952
+ ...CONTRACT_FAILED_REQUIRED_FIELDS,
2953
+ ...CONTRACT_FAILED_OPTIONAL_FIELDS
2954
+ ]);
2955
+ const forbidden = new Set(CONTRACT_FAILED_FORBIDDEN_FIELDS);
2956
+ for (const required of CONTRACT_FAILED_REQUIRED_FIELDS) {
2957
+ if (!keys.includes(required)) {
2958
+ return fail(
2959
+ "contract-failed-payload-schema-lock",
2960
+ `missing required field: ${required}`,
2961
+ nowMs() - t0
2962
+ );
2963
+ }
2964
+ }
2965
+ for (const k of keys) {
2966
+ if (forbidden.has(k)) {
2967
+ return fail(
2968
+ "contract-failed-payload-schema-lock",
2969
+ `forbidden field on wire: ${k}`,
2970
+ nowMs() - t0
2971
+ );
2972
+ }
2973
+ }
2974
+ for (const k of keys) {
2975
+ if (!allowed.has(k)) {
2976
+ return fail(
2977
+ "contract-failed-payload-schema-lock",
2978
+ `unrecognised field on wire: ${k} (not in required \u222A optional)`,
2979
+ nowMs() - t0
2980
+ );
2981
+ }
2982
+ }
2983
+ return pass(
2984
+ "contract-failed-payload-schema-lock",
2985
+ `${keys.length} fields \u2286 required(${CONTRACT_FAILED_REQUIRED_FIELDS.length}) \u222A optional(${CONTRACT_FAILED_OPTIONAL_FIELDS.length}); ${CONTRACT_FAILED_FORBIDDEN_FIELDS.length} forbidden absent`,
2986
+ nowMs() - t0
2987
+ );
2988
+ }
2989
+ };
2990
+ var STATIC_VERIFIERS = Object.freeze([
2991
+ VERIFIER_PER_USER_CACHE_ISOLATION,
2992
+ VERIFIER_IDEMPOTENCY_KEY_DETERMINISTIC,
2993
+ VERIFIER_ERROR_ENVELOPE_SHAPE,
2994
+ VERIFIER_FLUSH_INTERVAL_PARITY,
2995
+ VERIFIER_SUPER_PROPERTY_MERGE_PRECEDENCE,
2996
+ VERIFIER_CONTRACT_FAILED_PAYLOAD_SCHEMA_LOCK
2997
+ ]);
2998
+ async function runBootSelfTest(verifiers, reporter, ctx) {
2999
+ if (ctx.disableContractAssertions) {
3000
+ return { passed: 0, failed: 0, totalMs: 0 };
3001
+ }
3002
+ const t0 = nowMs();
3003
+ let passed = 0;
3004
+ let failed = 0;
3005
+ if (ctx.logVerifierResults) {
3006
+ const bootTestCount = verifiers.filter((v) => v.bootTest).length;
3007
+ const hookCount = verifiers.filter((v) => v.hooks).length;
3008
+ const ids = verifiers.map((v) => v.contractId).join(", ");
3009
+ ctx.console.info(
3010
+ `[crossdeck] Contract self-verification \u2014 ${verifiers.length} verifiers (${bootTestCount} boot-tests, ${hookCount} hot-path hooks): ${ids}`
3011
+ );
3012
+ }
3013
+ for (const verifier of verifiers) {
3014
+ if (!verifier.bootTest) continue;
3015
+ let result;
3016
+ try {
3017
+ result = await verifier.bootTest();
3018
+ } catch (err) {
3019
+ result = fail(
3020
+ verifier.contractId,
3021
+ `bootTest threw: ${err.message?.slice(0, 80) ?? "unknown"}`,
3022
+ 0
3023
+ );
3024
+ }
3025
+ reporter.report(result, "boot");
3026
+ if (result.ok) passed += 1;
3027
+ else failed += 1;
3028
+ }
3029
+ const totalMs = nowMs() - t0;
3030
+ if (ctx.logVerifierResults) {
3031
+ const verb = failed === 0 ? "passed" : "complete";
3032
+ ctx.console.info(
3033
+ `[crossdeck] Self-verification ${verb} \u2014 ${passed} passed, ${failed} failed (${totalMs}ms)`
3034
+ );
3035
+ }
3036
+ return { passed, failed, totalMs };
3037
+ }
3038
+ function runOnIdentify(verifiers, reporter, ctx, obs) {
3039
+ if (ctx.disableContractAssertions) return;
3040
+ for (const verifier of verifiers) {
3041
+ const hook = verifier.hooks?.onIdentify;
3042
+ if (!hook) continue;
3043
+ let result;
3044
+ try {
3045
+ result = hook(obs);
3046
+ } catch (err) {
3047
+ result = fail(
3048
+ verifier.contractId,
3049
+ `hook threw: ${err.message?.slice(0, 80) ?? "unknown"}`,
3050
+ 0
3051
+ );
3052
+ }
3053
+ reporter.report(result, "hot_path", "identify");
3054
+ }
3055
+ }
3056
+ function runOnTrack(verifiers, reporter, ctx, obs) {
3057
+ if (ctx.disableContractAssertions) return;
3058
+ for (const verifier of verifiers) {
3059
+ const hook = verifier.hooks?.onTrack;
3060
+ if (!hook) continue;
3061
+ let result;
3062
+ try {
3063
+ result = hook(obs);
3064
+ } catch (err) {
3065
+ result = fail(
3066
+ verifier.contractId,
3067
+ `hook threw: ${err.message?.slice(0, 80) ?? "unknown"}`,
3068
+ 0
3069
+ );
3070
+ }
3071
+ reporter.report(result, "hot_path", "track");
3072
+ }
3073
+ }
3074
+ function runOnSyncPurchases(verifiers, reporter, ctx, obs) {
3075
+ if (ctx.disableContractAssertions) return;
3076
+ for (const verifier of verifiers) {
3077
+ const hook = verifier.hooks?.onSyncPurchases;
3078
+ if (!hook) continue;
3079
+ let result;
3080
+ try {
3081
+ result = hook(obs);
3082
+ } catch (err) {
3083
+ result = fail(
3084
+ verifier.contractId,
3085
+ `hook threw: ${err.message?.slice(0, 80) ?? "unknown"}`,
3086
+ 0
3087
+ );
3088
+ }
3089
+ reporter.report(result, "hot_path", "syncPurchases");
3090
+ }
3091
+ }
3092
+ function runOnErrorParse(verifiers, reporter, ctx, obs) {
3093
+ if (ctx.disableContractAssertions) return;
3094
+ for (const verifier of verifiers) {
3095
+ const hook = verifier.hooks?.onErrorParse;
3096
+ if (!hook) continue;
3097
+ let result;
3098
+ try {
3099
+ result = hook(obs);
3100
+ } catch (err) {
3101
+ result = fail(
3102
+ verifier.contractId,
3103
+ `hook threw: ${err.message?.slice(0, 80) ?? "unknown"}`,
3104
+ 0
3105
+ );
3106
+ }
3107
+ reporter.report(result, "hot_path", "errorParse");
3108
+ }
3109
+ }
3110
+ function defaultDebugModeFlag() {
3111
+ try {
3112
+ if (typeof process !== "undefined" && process.env) {
3113
+ const nodeEnv = process.env.NODE_ENV;
3114
+ if (typeof nodeEnv === "string") {
3115
+ return nodeEnv !== "production";
3116
+ }
3117
+ }
3118
+ } catch {
3119
+ }
3120
+ const devFlag = globalThis.__DEV__;
3121
+ if (typeof devFlag === "boolean") return devFlag;
3122
+ return false;
3123
+ }
3124
+ function pass(contractId, evidence, durationMs) {
3125
+ return { ok: true, contractId, evidence, durationMs };
3126
+ }
3127
+ function fail(contractId, failureReason, durationMs) {
3128
+ return { ok: false, contractId, failureReason, durationMs };
3129
+ }
3130
+ function nowMs() {
3131
+ if (typeof performance !== "undefined" && typeof performance.now === "function") {
3132
+ return performance.now();
3133
+ }
3134
+ return Date.now();
3135
+ }
3136
+ function truncate(s, max) {
3137
+ return s.length > max ? s.slice(0, max - 1) + "\u2026" : s;
3138
+ }
3139
+ function shortSuffix(suffix) {
3140
+ const trimmed = suffix.startsWith("_") ? suffix.slice(1) : suffix;
3141
+ return trimmed.length <= 12 ? trimmed : `${trimmed.slice(0, 4)}\u2026${trimmed.slice(-4)}`;
3142
+ }
3143
+ function randomHex(len) {
3144
+ const bytes = new Uint8Array(Math.ceil(len / 2));
3145
+ if (typeof globalThis !== "undefined" && globalThis.crypto?.getRandomValues) {
3146
+ globalThis.crypto.getRandomValues(bytes);
3147
+ } else {
3148
+ for (let i = 0; i < bytes.length; i += 1) {
3149
+ bytes[i] = Math.floor(Math.random() * 256);
3150
+ }
3151
+ }
3152
+ let hex = "";
3153
+ for (let i = 0; i < bytes.length; i += 1) {
3154
+ hex += bytes[i].toString(16).padStart(2, "0");
3155
+ }
3156
+ return hex.slice(0, len);
3157
+ }
3158
+ var MemoryStorage2 = class {
3159
+ constructor() {
3160
+ this.map = /* @__PURE__ */ new Map();
3161
+ }
3162
+ getItem(key) {
3163
+ return this.map.get(key) ?? null;
3164
+ }
3165
+ setItem(key, value) {
3166
+ this.map.set(key, value);
3167
+ }
3168
+ removeItem(key) {
3169
+ this.map.delete(key);
3170
+ }
3171
+ // Iteration support for EntitlementCache's clearAll() path. The
3172
+ // contract this verifier checks doesn't exercise clearAll, but
3173
+ // EntitlementCache's constructor calls hydrate() which reads from
3174
+ // storage — we need the cache to look "empty" until we plant data.
3175
+ keys() {
3176
+ return Array.from(this.map.keys());
3177
+ }
3178
+ };
3179
+ // sha256Hex re-export so the verifier doesn't need to import it
3180
+ // separately (some bundlers tree-shake more aggressively when the
3181
+ // exports are colocated). Inert for the storage adapter itself.
3182
+ MemoryStorage2._ = sha256Hex;
3183
+
2061
3184
  // src/stack-parser.ts
2062
3185
  function parseStack(stack) {
2063
3186
  if (!stack || typeof stack !== "string") return [];
@@ -2748,6 +3871,22 @@ function isSelfRequest(requestUrl, selfHostname) {
2748
3871
  var CrossdeckClient = class {
2749
3872
  constructor() {
2750
3873
  this.state = null;
3874
+ // ────────────────────────────────────────────────────────────────
3875
+ // Contract verifier layer (see sdks/web/src/_contract-verifiers.ts).
3876
+ //
3877
+ // Three flags govern this layer (CrossdeckOptions):
3878
+ // verifyContractsAtBoot — boot self-test on Crossdeck.start
3879
+ // logVerifierResults — print PASS results to console
3880
+ // disableContractAssertions — total kill-switch
3881
+ //
3882
+ // When the kill-switch is set, all three fields stay null and
3883
+ // every hot-path dispatcher short-circuits — zero runtime cost.
3884
+ // Otherwise: populated once at init(), referenced from every
3885
+ // hot-path call site (identify, syncPurchases, ...).
3886
+ // ────────────────────────────────────────────────────────────────
3887
+ this.verifiers = null;
3888
+ this.verifierReporter = null;
3889
+ this.verifierCtx = null;
2751
3890
  }
2752
3891
  /**
2753
3892
  * Boot the SDK. Idempotent — calling init twice with the same options
@@ -2776,6 +3915,10 @@ var CrossdeckClient = class {
2776
3915
  this.state.errors?.uninstall();
2777
3916
  } catch {
2778
3917
  }
3918
+ try {
3919
+ void this.state.events.flush({ keepalive: true });
3920
+ } catch {
3921
+ }
2779
3922
  }
2780
3923
  if (!options.publicKey || !options.publicKey.startsWith("cd_pub_")) {
2781
3924
  throw new CrossdeckError({
@@ -2823,7 +3966,12 @@ var CrossdeckClient = class {
2823
3966
  // load still flushes if the user leaves quickly (the keepalive
2824
3967
  // pagehide handler picks up anything that doesn't); long enough
2825
3968
  // that bursts of clicks coalesce into one network round-trip.
2826
- eventFlushIntervalMs: options.eventFlushIntervalMs ?? 1500,
3969
+ // v1.4.0 Phase 3.3 — flush interval default parity. Pre-
3970
+ // v1.4.0: Web/Node 1500ms, RN/Swift/Android 5000ms. All
3971
+ // converged on 2000ms (the Stripe-adjacent industry norm)
3972
+ // so cross-platform funnels show events landing at the
3973
+ // same cadence on every SDK. Per-instance override stays.
3974
+ eventFlushIntervalMs: options.eventFlushIntervalMs ?? 2e3,
2827
3975
  sdkVersion: options.sdkVersion ?? SDK_VERSION,
2828
3976
  autoTrack,
2829
3977
  appVersion: options.appVersion ?? null
@@ -2838,7 +3986,22 @@ var CrossdeckClient = class {
2838
3986
  // to a successful no-op response when localDevMode is set.
2839
3987
  // SDK methods continue to work locally; nothing reaches the
2840
3988
  // server.
2841
- localDevMode
3989
+ localDevMode,
3990
+ // Contract verifier hot-path hook — fires the `error-envelope-shape`
3991
+ // verifier on every parsed wire error, BEFORE the throw bubbles
3992
+ // back to user code. Centralised here so we don't need a try/catch
3993
+ // around every `await s.http.request(...)` call site downstream.
3994
+ // No-op when the verifier layer is disabled (verifiers === null).
3995
+ onErrorParsed: (err) => {
3996
+ if (this.verifiers && this.verifierReporter && this.verifierCtx) {
3997
+ runOnErrorParse(this.verifiers, this.verifierReporter, this.verifierCtx, {
3998
+ errorType: err.type,
3999
+ errorCode: err.code,
4000
+ requestId: err.requestId ?? null,
4001
+ httpStatus: typeof err.status === "number" ? err.status : 0
4002
+ });
4003
+ }
4004
+ }
2842
4005
  });
2843
4006
  if (localDevMode) {
2844
4007
  console.log(
@@ -2983,6 +4146,89 @@ var CrossdeckClient = class {
2983
4146
  if (opts.autoHeartbeat && !localDevMode) {
2984
4147
  void this.heartbeat().catch(() => void 0);
2985
4148
  }
4149
+ if (options.disableContractAssertions !== true) {
4150
+ const debugDefault = defaultDebugModeFlag();
4151
+ this.verifierCtx = buildVerifierContext({
4152
+ logVerifierResults: options.logVerifierResults ?? debugDefault,
4153
+ disableContractAssertions: false,
4154
+ // Best-effort run-context detection. Customers running the
4155
+ // Web SDK inside their own CI (Playwright, Cypress, etc.)
4156
+ // typically set CI=true; we use that as the signal. Otherwise
4157
+ // we assume `customer-app` — the SDK is running inside a
4158
+ // customer's deployed site.
4159
+ runContext: typeof process !== "undefined" && process.env && process.env.CI ? "ci" : "customer-app"
4160
+ });
4161
+ this.verifierReporter = new VerifierReporter(this.verifierCtx);
4162
+ const flushVerifier = buildFlushIntervalVerifier(opts.eventFlushIntervalMs);
4163
+ this.verifiers = Object.freeze([...STATIC_VERIFIERS, flushVerifier]);
4164
+ if (localDevMode) {
4165
+ const verifyAtBootLocal = options.verifyContractsAtBoot ?? debugDefault;
4166
+ if (verifyAtBootLocal && this.verifierReporter) {
4167
+ void runBootSelfTest(
4168
+ this.verifiers,
4169
+ this.verifierReporter,
4170
+ this.verifierCtx
4171
+ ).catch(() => void 0);
4172
+ }
4173
+ } else {
4174
+ void this.bootstrapVerifierLayerRemote(options, debugDefault).catch(
4175
+ () => void 0
4176
+ );
4177
+ }
4178
+ }
4179
+ return;
4180
+ }
4181
+ /**
4182
+ * Fetch /v1/config from the backend and apply any per-app verifier
4183
+ * overrides set in the dashboard, then fire the boot self-test
4184
+ * once with the FINAL resolved flags.
4185
+ *
4186
+ * Precedence (also documented at the call site in init()):
4187
+ * code option > dashboard remote config > DEBUG/RELEASE default
4188
+ *
4189
+ * Code wins so engineers retain ultimate control; dashboard is the
4190
+ * no-deploy operational lever for QA / staging soaks.
4191
+ *
4192
+ * Never throws — a /v1/config failure surfaces as "stick with the
4193
+ * synchronous defaults that init() already applied" and the boot
4194
+ * self-test runs only if code > default resolves true.
4195
+ */
4196
+ async bootstrapVerifierLayerRemote(options, debugDefault) {
4197
+ if (!this.state || !this.verifiers || !this.verifierCtx) return;
4198
+ let remote = {
4199
+ logVerifierResults: null,
4200
+ verifyContractsAtBoot: null
4201
+ };
4202
+ try {
4203
+ const resp = await this.state.http.request(
4204
+ "GET",
4205
+ "/config"
4206
+ );
4207
+ if (resp && resp.verifierConfig) {
4208
+ const r = resp.verifierConfig;
4209
+ remote = {
4210
+ logVerifierResults: typeof r.logVerifierResults === "boolean" ? r.logVerifierResults : null,
4211
+ verifyContractsAtBoot: typeof r.verifyContractsAtBoot === "boolean" ? r.verifyContractsAtBoot : null
4212
+ };
4213
+ }
4214
+ } catch {
4215
+ }
4216
+ const logResults = options.logVerifierResults ?? (remote.logVerifierResults ?? debugDefault);
4217
+ const verifyAtBoot = options.verifyContractsAtBoot ?? (remote.verifyContractsAtBoot ?? debugDefault);
4218
+ if (logResults !== this.verifierCtx.logVerifierResults) {
4219
+ this.verifierCtx = {
4220
+ ...this.verifierCtx,
4221
+ logVerifierResults: logResults
4222
+ };
4223
+ this.verifierReporter = new VerifierReporter(this.verifierCtx);
4224
+ }
4225
+ if (verifyAtBoot && this.verifierReporter) {
4226
+ await runBootSelfTest(
4227
+ this.verifiers,
4228
+ this.verifierReporter,
4229
+ this.verifierCtx
4230
+ );
4231
+ }
2986
4232
  }
2987
4233
  /**
2988
4234
  * @deprecated Use `init()` instead. NorthStar §4 standardised the
@@ -3051,14 +4297,18 @@ var CrossdeckClient = class {
3051
4297
  };
3052
4298
  if (options?.email) body.email = options.email;
3053
4299
  if (traits) body.traits = traits;
4300
+ const priorUserId = s.developerUserId;
4301
+ s.entitlements.setUserKey(userId);
4302
+ if (this.verifiers && this.verifierReporter && this.verifierCtx) {
4303
+ runOnIdentify(this.verifiers, this.verifierReporter, this.verifierCtx, {
4304
+ priorUserId,
4305
+ nextUserId: userId,
4306
+ cache: s.entitlements
4307
+ });
4308
+ }
3054
4309
  const result = await s.http.request("POST", "/identity/alias", {
3055
4310
  body
3056
4311
  });
3057
- const priorCdcust = s.identity.crossdeckCustomerId;
3058
- const cacheHasEntries = s.entitlements.list().length > 0;
3059
- if (priorCdcust && result.crossdeckCustomerId && priorCdcust !== result.crossdeckCustomerId || !priorCdcust && cacheHasEntries) {
3060
- s.entitlements.clear();
3061
- }
3062
4312
  s.identity.setCrossdeckCustomerId(result.crossdeckCustomerId);
3063
4313
  s.developerUserId = userId;
3064
4314
  return result;
@@ -3363,6 +4613,38 @@ var CrossdeckClient = class {
3363
4613
  * trip happens in the background. To flush before the page unloads,
3364
4614
  * call flush().
3365
4615
  */
4616
+ /**
4617
+ * Emit `crossdeck.contract_failed` to the Crossdeck reliability
4618
+ * endpoint — single-fire, one-way, never visible in the customer's
4619
+ * dashboard. Goes over a dedicated HTTP path with the reliability
4620
+ * publishable key embedded at build time; the customer's track()
4621
+ * pipeline never carries `crossdeck.*` events. This is the
4622
+ * independent-controller flow described in Privacy Policy §6
4623
+ * ("Flow B"). The wire shape is fixed by the schema-lock contract
4624
+ * at `contracts/diagnostics/contract-failed-payload-schema-lock.json`.
4625
+ *
4626
+ * Wire the call from a test hook, dogfood failure path, or
4627
+ * customer contract-verification harness; see
4628
+ * `contracts/README.md` for the per-test-framework hook recipes.
4629
+ */
4630
+ reportContractFailure(input) {
4631
+ const payload = {
4632
+ contract_id: input.contractId,
4633
+ sdk_version: SDK_VERSION,
4634
+ sdk_platform: "web",
4635
+ failure_reason: input.failureReason,
4636
+ run_context: input.runContext,
4637
+ run_id: input.runId
4638
+ };
4639
+ if (input.testRef) {
4640
+ payload.test_file = input.testRef.file;
4641
+ payload.test_name = input.testRef.name;
4642
+ }
4643
+ if (input.deviceClass) {
4644
+ payload.device_class = input.deviceClass;
4645
+ }
4646
+ sendDiagnosticTelemetry(payload);
4647
+ }
3366
4648
  track(name, properties) {
3367
4649
  const s = this.requireStarted();
3368
4650
  if (!name) {
@@ -3450,6 +4732,14 @@ var CrossdeckClient = class {
3450
4732
  };
3451
4733
  Object.assign(event, this.identityHintForEvent());
3452
4734
  s.events.enqueue(event);
4735
+ if (this.verifiers && this.verifierReporter && this.verifierCtx) {
4736
+ runOnTrack(this.verifiers, this.verifierReporter, this.verifierCtx, {
4737
+ callerProperties: validation.properties,
4738
+ superProperties: supers,
4739
+ deviceProperties: s.deviceInfo,
4740
+ mergedProperties: finalProperties
4741
+ });
4742
+ }
3453
4743
  if (!isError && !isWebVital) {
3454
4744
  const category = name.startsWith("page.") ? "navigation" : name.startsWith("element.") || name === "session.started" ? "ui.click" : "custom";
3455
4745
  s.breadcrumbs.add({
@@ -3499,11 +4789,37 @@ var CrossdeckClient = class {
3499
4789
  });
3500
4790
  }
3501
4791
  const rail = input.rail ?? "apple";
4792
+ const body = { ...input, rail };
4793
+ const idempotencyKey = deriveIdempotencyKeyForPurchase(body);
4794
+ if (this.verifiers && this.verifierReporter && this.verifierCtx) {
4795
+ const stableId = rail === "apple" ? body.signedTransactionInfo ?? "" : body.purchaseToken ?? "";
4796
+ runOnSyncPurchases(
4797
+ this.verifiers,
4798
+ this.verifierReporter,
4799
+ this.verifierCtx,
4800
+ {
4801
+ rail,
4802
+ stableIdentifier: stableId,
4803
+ derivedKey: idempotencyKey
4804
+ }
4805
+ );
4806
+ }
3502
4807
  const result = await s.http.request("POST", "/purchases/sync", {
3503
- body: { ...input, rail }
4808
+ body,
4809
+ idempotencyKey
3504
4810
  });
3505
4811
  s.identity.setCrossdeckCustomerId(result.crossdeckCustomerId);
3506
4812
  s.entitlements.setFromList(result.entitlements);
4813
+ try {
4814
+ const sourceProductId = result.entitlements[0]?.source.productId;
4815
+ const sourceSubscriptionId = result.entitlements[0]?.source.subscriptionId;
4816
+ const props = { rail };
4817
+ if (sourceProductId) props.productId = sourceProductId;
4818
+ if (sourceSubscriptionId) props.subscriptionId = sourceSubscriptionId;
4819
+ if (result.idempotent_replay) props.idempotent_replay = true;
4820
+ this.track("purchase.completed", props);
4821
+ } catch {
4822
+ }
3507
4823
  s.debug.emit(
3508
4824
  "sdk.purchase_evidence_sent",
3509
4825
  "StoreKit transaction forwarded. Waiting for backend verification.",
@@ -3559,7 +4875,7 @@ var CrossdeckClient = class {
3559
4875
  }
3560
4876
  this.state.autoTracker?.uninstall();
3561
4877
  this.state.identity.reset();
3562
- this.state.entitlements.clear();
4878
+ this.state.entitlements.clearAll();
3563
4879
  this.state.events.reset();
3564
4880
  this.state.superProps.clear();
3565
4881
  this.state.breadcrumbs.clear();