@hanzo/event 0.3.13 → 0.3.14

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/index.cjs CHANGED
@@ -286,7 +286,7 @@ function uuidv7Time(id) {
286
286
  }
287
287
 
288
288
  // src/version.ts
289
- var VERSION = "0.3.13";
289
+ var VERSION = "0.3.14";
290
290
 
291
291
  // src/sentry.ts
292
292
  var MAX_FRAMES = 50;
@@ -487,6 +487,8 @@ var KEY = {
487
487
  cohort: "hz_cohort"
488
488
  };
489
489
  var SESSION_TTL_MS = 30 * 60 * 1e3;
490
+ var ANON_DOMAIN = "hanzo.ai";
491
+ var ANON_MAX_AGE_S = 2 * 365 * 24 * 60 * 60;
490
492
  function ls() {
491
493
  try {
492
494
  if (typeof window === "undefined" || !window.localStorage) return void 0;
@@ -495,15 +497,69 @@ function ls() {
495
497
  return void 0;
496
498
  }
497
499
  }
500
+ function jar() {
501
+ try {
502
+ if (typeof document === "undefined" || typeof document.cookie !== "string") return void 0;
503
+ return document;
504
+ } catch {
505
+ return void 0;
506
+ }
507
+ }
508
+ function getCookie(name) {
509
+ const d = jar();
510
+ if (!d) return void 0;
511
+ for (const part of d.cookie.split(";")) {
512
+ const eq = part.indexOf("=");
513
+ if (eq < 0 || part.slice(0, eq).trim() !== name) continue;
514
+ const v = part.slice(eq + 1).trim();
515
+ if (!v) continue;
516
+ try {
517
+ return decodeURIComponent(v);
518
+ } catch {
519
+ return v;
520
+ }
521
+ }
522
+ return void 0;
523
+ }
524
+ function setCookie(name, value) {
525
+ const d = jar();
526
+ if (!d) return;
527
+ let host = "";
528
+ let secure = false;
529
+ try {
530
+ if (typeof window !== "undefined" && window.location) {
531
+ host = window.location.hostname || "";
532
+ secure = window.location.protocol === "https:";
533
+ }
534
+ } catch {
535
+ }
536
+ const attrs = [
537
+ // encodeURIComponent leaves a UUID byte-identical while making any value that
538
+ // is not one unable to forge a `;` and inject an attribute.
539
+ `${name}=${encodeURIComponent(value)}`,
540
+ "Path=/",
541
+ `Max-Age=${ANON_MAX_AGE_S}`,
542
+ "SameSite=Lax"
543
+ ];
544
+ if (host === ANON_DOMAIN || host.endsWith(`.${ANON_DOMAIN}`)) attrs.push(`Domain=${ANON_DOMAIN}`);
545
+ if (secure) attrs.push("Secure");
546
+ try {
547
+ d.cookie = attrs.join("; ");
548
+ } catch {
549
+ }
550
+ }
551
+ var memAnon;
498
552
  function anonId() {
553
+ if (typeof window === "undefined") return void 0;
499
554
  const s2 = ls();
500
- if (!s2) return void 0;
501
- let v = s2.getItem(KEY.anon);
502
- if (!v) {
503
- v = uuidv7();
504
- s2.setItem(KEY.anon, v);
555
+ const id = getCookie(KEY.anon) || s2?.getItem(KEY.anon) || memAnon || uuidv7();
556
+ memAnon = id;
557
+ setCookie(KEY.anon, id);
558
+ try {
559
+ if (s2 && s2.getItem(KEY.anon) !== id) s2.setItem(KEY.anon, id);
560
+ } catch {
505
561
  }
506
- return v;
562
+ return id;
507
563
  }
508
564
  function sessionId(now = Date.now()) {
509
565
  const s2 = ls();