@hanzo/event 0.3.14 → 0.3.15

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 hanzo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.cjs CHANGED
@@ -259,34 +259,115 @@ function scrubText(s2, capturePII = false) {
259
259
  return s2;
260
260
  }
261
261
 
262
- // src/uid.ts
263
- var HEX = Array.from({ length: 256 }, (_, i) => (i + 256).toString(16).slice(1));
264
- function fill(b) {
265
- const c = typeof crypto !== "undefined" ? crypto : void 0;
266
- if (c && typeof c.getRandomValues === "function") {
267
- c.getRandomValues(b);
268
- return b;
269
- }
270
- for (let i = 0; i < b.length; i++) b[i] = Math.random() * 256 | 0;
271
- return b;
272
- }
273
- function uuidv7(now = Date.now()) {
274
- const b = fill(new Uint8Array(16));
275
- let t = Math.floor(now);
276
- for (let i = 5; i >= 0; i--) {
262
+ // src/anon.js
263
+ var HZ_ANON_KEY = "hz_anon_id";
264
+ var HZ_ANON_LEGACY_KEY = "hz_id";
265
+ var HZ_ANON_DOMAIN = "hanzo.ai";
266
+ var HZ_ANON_MAX_AGE = 2 * 365 * 24 * 60 * 60;
267
+ var hzAnonMemo;
268
+ function hzUuidv7(now) {
269
+ var b = new Uint8Array(16);
270
+ var i;
271
+ var c = typeof crypto !== "undefined" ? crypto : void 0;
272
+ if (c && typeof c.getRandomValues === "function") c.getRandomValues(b);
273
+ else for (i = 0; i < 16; i++) b[i] = Math.random() * 256 | 0;
274
+ var t = Math.floor(now === void 0 ? Date.now() : now);
275
+ for (i = 5; i >= 0; i--) {
277
276
  b[i] = t % 256;
278
277
  t = Math.floor(t / 256);
279
278
  }
280
279
  b[6] = 112 | b[6] & 15;
281
280
  b[8] = 128 | b[8] & 63;
282
- return HEX[b[0]] + HEX[b[1]] + HEX[b[2]] + HEX[b[3]] + "-" + HEX[b[4]] + HEX[b[5]] + "-" + HEX[b[6]] + HEX[b[7]] + "-" + HEX[b[8]] + HEX[b[9]] + "-" + HEX[b[10]] + HEX[b[11]] + HEX[b[12]] + HEX[b[13]] + HEX[b[14]] + HEX[b[15]];
281
+ var h = "";
282
+ for (i = 0; i < 16; i++) {
283
+ h += (b[i] + 256).toString(16).slice(1);
284
+ if (i === 3 || i === 5 || i === 7 || i === 9) h += "-";
285
+ }
286
+ return h;
287
+ }
288
+ function hzAnonJar() {
289
+ try {
290
+ if (typeof document === "undefined" || typeof document.cookie !== "string") return null;
291
+ return document;
292
+ } catch (e) {
293
+ return null;
294
+ }
295
+ }
296
+ function hzAnonStore() {
297
+ try {
298
+ if (typeof window === "undefined" || !window.localStorage) return null;
299
+ return window.localStorage;
300
+ } catch (e) {
301
+ return null;
302
+ }
303
+ }
304
+ function hzAnonItem(store, name) {
305
+ try {
306
+ return store && store.getItem(name) || "";
307
+ } catch (e) {
308
+ return "";
309
+ }
310
+ }
311
+ function hzAnonCookie(name) {
312
+ var d = hzAnonJar();
313
+ if (!d) return "";
314
+ var parts = d.cookie.split(";");
315
+ for (var i = 0; i < parts.length; i++) {
316
+ var eq = parts[i].indexOf("=");
317
+ if (eq < 0 || parts[i].slice(0, eq).trim() !== name) continue;
318
+ var v = parts[i].slice(eq + 1).trim();
319
+ if (!v) continue;
320
+ try {
321
+ return decodeURIComponent(v);
322
+ } catch (e) {
323
+ return v;
324
+ }
325
+ }
326
+ return "";
327
+ }
328
+ function hzAnonWrite(name, value) {
329
+ var d = hzAnonJar();
330
+ if (!d) return;
331
+ var host = "";
332
+ var secure = false;
333
+ try {
334
+ if (typeof window !== "undefined" && window.location) {
335
+ host = window.location.hostname || "";
336
+ secure = window.location.protocol === "https:";
337
+ }
338
+ } catch (e) {
339
+ }
340
+ var c = name + "=" + encodeURIComponent(value);
341
+ c += "; Path=/; Max-Age=" + HZ_ANON_MAX_AGE + "; SameSite=Lax";
342
+ if (("." + host).slice(-(HZ_ANON_DOMAIN.length + 1)) === "." + HZ_ANON_DOMAIN) {
343
+ c += "; Domain=" + HZ_ANON_DOMAIN;
344
+ }
345
+ if (secure) c += "; Secure";
346
+ try {
347
+ d.cookie = c;
348
+ } catch (e) {
349
+ }
283
350
  }
351
+ function hzAnonId() {
352
+ if (typeof window === "undefined") return "";
353
+ var s2 = hzAnonStore();
354
+ var id = hzAnonCookie(HZ_ANON_KEY) || hzAnonItem(s2, HZ_ANON_KEY) || hzAnonItem(s2, HZ_ANON_LEGACY_KEY) || hzAnonMemo || hzUuidv7();
355
+ hzAnonMemo = id;
356
+ hzAnonWrite(HZ_ANON_KEY, id);
357
+ try {
358
+ if (s2 && s2.getItem(HZ_ANON_KEY) !== id) s2.setItem(HZ_ANON_KEY, id);
359
+ } catch (e) {
360
+ }
361
+ return id;
362
+ }
363
+
364
+ // src/uid.ts
284
365
  function uuidv7Time(id) {
285
366
  return parseInt(id.slice(0, 8) + id.slice(9, 13), 16);
286
367
  }
287
368
 
288
369
  // src/version.ts
289
- var VERSION = "0.3.14";
370
+ var VERSION = "0.3.15";
290
371
 
291
372
  // src/sentry.ts
292
373
  var MAX_FRAMES = 50;
@@ -295,7 +376,7 @@ var MAX_LINE_LEN = 2048;
295
376
  var MAX_TAG_LEN = 1024;
296
377
  var MAX_TAGS = 50;
297
378
  function eventId() {
298
- return uuidv7().replace(/-/g, "");
379
+ return hzUuidv7().replace(/-/g, "");
299
380
  }
300
381
  function byteLen(s2) {
301
382
  if (typeof TextEncoder !== "undefined") return new TextEncoder().encode(s2).length;
@@ -481,14 +562,11 @@ ${payload}
481
562
 
482
563
  // src/storage.ts
483
564
  var KEY = {
484
- anon: "hz_anon_id",
485
565
  session: "hz_session",
486
566
  firstTouch: "hz_first_touch",
487
567
  cohort: "hz_cohort"
488
568
  };
489
569
  var SESSION_TTL_MS = 30 * 60 * 1e3;
490
- var ANON_DOMAIN = "hanzo.ai";
491
- var ANON_MAX_AGE_S = 2 * 365 * 24 * 60 * 60;
492
570
  function ls() {
493
571
  try {
494
572
  if (typeof window === "undefined" || !window.localStorage) return void 0;
@@ -497,69 +575,8 @@ function ls() {
497
575
  return void 0;
498
576
  }
499
577
  }
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;
552
578
  function anonId() {
553
- if (typeof window === "undefined") return void 0;
554
- const s2 = ls();
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 {
561
- }
562
- return id;
579
+ return hzAnonId() || void 0;
563
580
  }
564
581
  function sessionId(now = Date.now()) {
565
582
  const s2 = ls();
@@ -571,7 +588,7 @@ function sessionId(now = Date.now()) {
571
588
  state = null;
572
589
  }
573
590
  if (!state || now - state.last > SESSION_TTL_MS) {
574
- state = { id: uuidv7(now), last: now };
591
+ state = { id: hzUuidv7(now), last: now };
575
592
  } else {
576
593
  state.last = now;
577
594
  }
@@ -907,7 +924,7 @@ var Analytics = class {
907
924
  build(kind, event, extra) {
908
925
  const anon = anonId();
909
926
  const wire = {
910
- messageId: uuidv7(),
927
+ messageId: hzUuidv7(),
911
928
  type: kind,
912
929
  event,
913
930
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
@@ -1103,6 +1120,27 @@ var COHORTS = {
1103
1120
  channel: { field: "channel", label: "Acquisition channel" },
1104
1121
  refCode: { field: "ref_code", label: "Referral code" }
1105
1122
  };
1123
+ /*! anon.js — THE anonymous-identity chain. ONE implementation, three distributions.
1124
+ *
1125
+ * One browser is ONE person on every Hanzo surface, whichever client a page
1126
+ * happens to have loaded. There were three implementations writing TWO keys —
1127
+ * `hz_anon_id` (the npm client, the hosted tag) and `hz_id` (hz.js) — so the same
1128
+ * visitor was several people depending on which snippet the surface shipped.
1129
+ *
1130
+ * The three call sites:
1131
+ * 1. src/storage.ts — the bundled npm client; IMPORTS this file.
1132
+ * 2. hz.js — the no-build script tag; INLINES the marked region.
1133
+ * 3. hanzoai/cloud apps/analytics/tag.js — the tag the door hosts at
1134
+ * /v1/event.js; vendors this file and its tag.go serves the marked region
1135
+ * with the tag as one asset, so the door holds no second copy either.
1136
+ *
1137
+ * (2) and (3) have no bundler and cannot import anything, which is why the chain
1138
+ * lives in a file that is plain ES5 rather than in a .ts: the region between the
1139
+ * BEGIN and END markers is COPIED VERBATIM, and src/anon.test.ts fails if hz.js's
1140
+ * copy is so much as a byte different. Keep the region ES5, dependency-free,
1141
+ * `hz`-prefixed (it is spliced into other people's scopes) and unformatted — a
1142
+ * reformat of one copy is a diff against the other.
1143
+ */
1106
1144
 
1107
1145
  exports.Analytics = Analytics;
1108
1146
  exports.COHORTS = COHORTS;
@@ -1130,7 +1168,7 @@ exports.parseDsn = parseDsn;
1130
1168
  exports.redactSecrets = redactSecrets;
1131
1169
  exports.scrubPII = scrubPII;
1132
1170
  exports.scrubText = scrubText;
1133
- exports.uuidv7 = uuidv7;
1171
+ exports.uuidv7 = hzUuidv7;
1134
1172
  exports.uuidv7Time = uuidv7Time;
1135
1173
  //# sourceMappingURL=index.cjs.map
1136
1174
  //# sourceMappingURL=index.cjs.map