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