@hanzo/event 0.3.32 → 0.3.34

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanzo/event",
3
- "version": "0.3.32",
3
+ "version": "0.3.34",
4
4
  "description": "Hanzo Event — the ONE telemetry client. Emits pageview/event/identify/group to the Hanzo Cloud event stream (POST /v1/event), AND reports errors to Sentry as real Sentry envelopes — the error plane needs a DSN, without one nothing reaches Sentry. First-touch attribution, beacon-on-unload, auto error capture, client-side secret/PII scrubbing, a shared event + goal vocabulary. Subsumes @sentry.",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/",
@@ -17,7 +17,6 @@
17
17
  "files": [
18
18
  "dist",
19
19
  "src",
20
- "hz.js",
21
20
  "README.md",
22
21
  "TAXONOMY.md"
23
22
  ],
@@ -82,7 +81,7 @@
82
81
  "build": "tsup",
83
82
  "dev": "tsup --watch",
84
83
  "test": "vitest run",
85
- "typecheck": "tsc --noEmit",
84
+ "typecheck": "tsgo --noEmit -p tsconfig.json",
86
85
  "clean": "rm -rf dist"
87
86
  }
88
87
  }
package/src/anon.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // Types for anon.js, which is hand-written ES5 rather than TypeScript because
2
- // hz.js and the door's hosted tag inline it VERBATIM and neither has a compiler.
2
+ // The door's hosted tag inlines it VERBATIM and has no compiler.
3
3
  // The declarations are here so the bundled client still imports it typed.
4
4
 
5
5
  /** Mints a time-ordered UUIDv7 (RFC 9562 §5.7) for `now` in epoch milliseconds. */
@@ -8,7 +8,7 @@ export declare function hzUuidv7(now?: number): string
8
8
  /**
9
9
  * The stable anonymous id for this browser, or '' during SSR.
10
10
  *
11
- * cookie · localStorage `hz_anon_id` · localStorage `hz_id` · in-memory · mint —
11
+ * cookie · localStorage `iam-anon-id` · adopted keys · in-memory · mint —
12
12
  * every existing id is adopted, and only a browser holding none is given a new one.
13
13
  */
14
14
  export declare function hzAnonId(): string
package/src/anon.js CHANGED
@@ -1,35 +1,42 @@
1
- /*! anon.js — THE anonymous-identity chain. ONE implementation, three distributions.
1
+ /*! anon.js — THE anonymous-identity chain. ONE implementation, two distributions.
2
2
  *
3
3
  * One browser is ONE person on every Hanzo surface, whichever client a page
4
- * happens to have loaded. There were three implementations writing TWO keys —
5
- * `hz_anon_id` (the npm client, the hosted tag) and `hz_id` (hz.js) — so the same
6
- * visitor was several people depending on which snippet the surface shipped.
4
+ * happens to have loaded. There were once three implementations writing TWO keys —
5
+ * `hz_anon_id` and `hz_id` — so the same visitor was several people depending on
6
+ * which snippet the surface shipped.
7
7
  *
8
- * The three call sites:
8
+ * The two call sites:
9
9
  * 1. src/storage.ts — the bundled npm client; IMPORTS this file.
10
- * 2. hz.js — the no-build script tag; INLINES the marked region.
11
- * 3. hanzoai/cloud apps/analytics/tag.js — the tag the door hosts at
10
+ * 2. hanzoai/cloud apps/analytics/tag.js — the tag the door hosts at
12
11
  * /v1/event.js; vendors this file and its tag.go serves the marked region
13
12
  * with the tag as one asset, so the door holds no second copy either.
14
13
  *
15
- * (2) and (3) have no bundler and cannot import anything, which is why the chain
16
- * lives in a file that is plain ES5 rather than in a .ts: the region between the
17
- * BEGIN and END markers is COPIED VERBATIM, and src/anon.test.ts fails if hz.js's
18
- * copy is so much as a byte different. Keep the region ES5, dependency-free,
19
- * `hz`-prefixed (it is spliced into other people's scopes) and unformatted — a
20
- * reformat of one copy is a diff against the other.
14
+ * (2) has no bundler and cannot import anything, which is why the chain lives in a
15
+ * file that is plain ES5 rather than in a .ts: the region between the BEGIN and END
16
+ * markers is COPIED VERBATIM. Keep it ES5, dependency-free, `hz`-prefixed (it is
17
+ * spliced into other people's scopes) and unformatted a reformat here is a diff
18
+ * against the vendored copy. After editing, resync the door:
19
+ *
20
+ * curl -fsSL https://unpkg.com/@hanzo/event/src/anon.js -o apps/analytics/anon.js
21
21
  */
22
22
 
23
- /* ── BEGIN hz anon chain — copied VERBATIM into hz.js and hanzoai/cloud ────── */
23
+ /* ── BEGIN hz anon chain — copied VERBATIM into hanzoai/cloud ──────────────── */
24
24
 
25
- /** The ONE anonymous-id key, on every surface and in every distribution. */
26
- var HZ_ANON_KEY = 'hz_anon_id'
25
+ /** The ONE anonymous-id key, on every surface and in every distribution.
26
+ *
27
+ * It names what it is, not who ships it: this value is visible to anyone who opens
28
+ * their own cookie jar, and a vendor's name has no business there. Identity is IAM's
29
+ * concern whether or not the visitor has said who they are. */
30
+ var HZ_ANON_KEY = 'iam-anon-id'
27
31
 
28
- /** hz.js used to write `hz_id` a SECOND identity space, so the one-paste tag
29
- * and the npm client were two different people on one page. It is READ and never
30
- * written: an id already in the wild is ADOPTED into the shared identity, because
31
- * minting over one detaches a returning visitor from their own history. */
32
- var HZ_ANON_LEGACY_KEY = 'hz_id'
32
+ /** Keys an id may already be living under. READ, never written.
33
+ *
34
+ * Each was canonical once `hz_anon_id` most recently, and `hz_id` before it, when a
35
+ * no-build tag wrote a second identity space and one page could hold two people.
36
+ * Adopting an id already in the wild is what makes this a rename rather than an
37
+ * erasure: minting over one detaches a returning visitor from their own history.
38
+ * They are never written, so they leave with the cookies that hold them. */
39
+ var HZ_ANON_ADOPT = ['hz_anon_id', 'hz_id']
33
40
 
34
41
  /** The registrable domain the cookie is scoped to, so docs, cloud, console,
35
42
  * studio, pay, id and www all read the ONE id. localStorage cannot do this: it is
@@ -165,7 +172,7 @@ function hzAnonWrite(name, value) {
165
172
  * Resolution is strictly ADDITIVE — every id that already exists is ADOPTED, and
166
173
  * only a browser holding none of them is given a new one:
167
174
  *
168
- * cookie · localStorage hz_anon_id · localStorage hz_id · in-memory · mint
175
+ * cookie · localStorage iam-anon-id · adopted keys · in-memory · mint
169
176
  *
170
177
  * Minting over an id resets a returning visitor and detaches them from their own
171
178
  * history, so the order is the migration: the cookie is the shared home, the two
@@ -175,13 +182,26 @@ function hzAnonWrite(name, value) {
175
182
  * localStorage keeps being written, so a rollback finds everyone where it left
176
183
  * them, and a browser that refuses cookies still holds one id per origin.
177
184
  */
185
+ /**
186
+ * hzAnonAdopted returns an id already living under a key that used to be canonical,
187
+ * from the cookie jar first — that is the copy shared across subdomains, so a visitor
188
+ * who arrived on one host is the same person on the next.
189
+ */
190
+ function hzAnonAdopted(s) {
191
+ for (var i = 0; i < HZ_ANON_ADOPT.length; i++) {
192
+ var id = hzAnonCookie(HZ_ANON_ADOPT[i]) || hzAnonItem(s, HZ_ANON_ADOPT[i])
193
+ if (id) return id
194
+ }
195
+ return ''
196
+ }
197
+
178
198
  function hzAnonId() {
179
199
  if (typeof window === 'undefined') return '' // SSR / prerender: no browser to identify
180
200
  var s = hzAnonStore()
181
201
  var id =
182
202
  hzAnonCookie(HZ_ANON_KEY) ||
183
203
  hzAnonItem(s, HZ_ANON_KEY) ||
184
- hzAnonItem(s, HZ_ANON_LEGACY_KEY) ||
204
+ hzAnonAdopted(s) ||
185
205
  hzAnonMemo ||
186
206
  hzUuidv7()
187
207
  hzAnonMemo = id
package/src/anon.test.ts CHANGED
@@ -1,15 +1,13 @@
1
- // The anonymous-identity chain is ONE implementation with three call sites, and
2
- // two of them cannot import it: hz.js and the tag hanzoai/cloud hosts at
3
- // /v1/event.js have no bundler, so they carry the marked region of anon.js
4
- // VERBATIM. That is the drift risk this file exists to remove — three snippets
5
- // that agree the day they are written and disagree a quarter later, which is
6
- // exactly how the same browser came to hold two different anonymous ids under two
7
- // different key names.
1
+ // The anonymous-identity chain is ONE implementation with two call sites, and one
2
+ // of them cannot import it: the tag hanzoai/cloud hosts at /v1/event.js has no
3
+ // bundler, so it carries the marked region of anon.js VERBATIM. That is the drift
4
+ // risk this file exists to remove — snippets that agree the day they are written
5
+ // and disagree a quarter later, which is exactly how the same browser came to hold
6
+ // two different anonymous ids under two different key names.
8
7
  //
9
- // The behavioural contract is proven where each distribution runs it:
10
- // storage.test.ts (the npm client) and hz.test.ts (the script tag). This file
11
- // proves the copies are the SAME TEXT, which is the only thing that keeps those
12
- // two suites testing one implementation instead of two.
8
+ // The behavioural contract is proven where the chain runs: storage.test.ts. This
9
+ // file proves the region is SPLICEABLE and that it ships, which is what lets the
10
+ // door vendor it instead of writing a second one.
13
11
 
14
12
  import { describe, expect, it } from 'vitest'
15
13
  import { readFileSync } from 'node:fs'
@@ -32,37 +30,19 @@ function span(src: string, what: string): [number, number] {
32
30
  /** The shared region, markers included. */
33
31
  const region = (src: string, what: string): string => src.slice(...span(src, what))
34
32
 
35
- /** Everything that is NOT the shared region. */
36
- function outside(src: string, what: string): string {
37
- const [b, e] = span(src, what)
38
- return src.slice(0, b) + src.slice(e)
39
- }
40
-
41
33
  const ANON = read('./anon.js')
42
- const HZ = read('../hz.js')
43
34
 
44
35
  describe('the shared anon chain', () => {
45
- it('is in hz.js as the same text, byte for byte', () => {
46
- // Not "equivalent" IDENTICAL. Anything weaker (normalising whitespace,
47
- // comparing behaviour) permits the copies to say different things about which
48
- // key they read, which is the bug. To change the chain, edit src/anon.js and
49
- // re-splice; never patch a copy.
50
- expect(region(HZ, 'hz.js')).toBe(region(ANON, 'anon.js'))
51
- })
52
-
53
- it('names one key, and hz.js holds no identity code of its own', () => {
54
- // hz.js used to mint into hz_id — a second identity space, so the one-paste
55
- // tag and the npm client were two people on one page. Neither key may be
56
- // NAMED outside the shared region: a snippet that spells a key is a snippet
57
- // that has an opinion about identity, and there is one opinion now.
58
- const rest = outside(HZ, 'hz.js')
59
- expect(rest).not.toContain("'hz_anon_id'")
60
- expect(rest).not.toContain("'hz_id'")
36
+ it('is marked, so the door can splice exactly it', () => {
37
+ // cloud's tag.go slices between these markers and PANICS if it cannot find
38
+ // them, so losing a marker ships a tag whose every event carries an undefined
39
+ // identity. To change the chain, edit this file; never patch a copy.
40
+ expect(region(ANON, 'anon.js')).toContain(BEGIN)
61
41
  })
62
42
 
63
43
  it('is inlineable: the region imports nothing and declares no ES6', () => {
64
- // It is spliced into two files that have no bundler and into pages the client
65
- // does not control, so it must be ES5, self-contained and hz-prefixed.
44
+ // It is spliced into a file that has no bundler and into pages the client does
45
+ // not control, so it must be ES5, self-contained and hz-prefixed.
66
46
  const r = region(ANON, 'anon.js')
67
47
  expect(r).not.toMatch(/\b(?:import|export|const|let|class|=>)\b|=>/)
68
48
  for (const [, name] of r.matchAll(/^(?:function|var) ([A-Za-z_$][\w$]*)/gm)) {
@@ -72,9 +52,8 @@ describe('the shared anon chain', () => {
72
52
 
73
53
  it('ships in the published package, for the door to vendor', () => {
74
54
  // hanzoai/cloud vendors this file whole and its tag.go serves the region with
75
- // the tag as one asset, so the third call site holds no second copy either.
55
+ // the tag as one asset, so the second call site holds no copy of its own.
76
56
  const pkg = JSON.parse(read('../package.json')) as { files: string[] }
77
57
  expect(pkg.files).toContain('src')
78
- expect(pkg.files).toContain('hz.js')
79
58
  })
80
59
  })
package/src/core.ts CHANGED
@@ -46,6 +46,7 @@ import {
46
46
  deriveChannel,
47
47
  } from './attribution'
48
48
  import { dsnForProduct } from './dsn'
49
+ import { keyForPage } from './org'
49
50
  import { EXCEPTION, PAGEVIEW } from './events'
50
51
  import { exceptionProperties } from './exception'
51
52
  import { scrubText } from './scrub'
@@ -283,12 +284,21 @@ export class Analytics {
283
284
  // carries end to end — KMS `deploy/PUBLISHABLE_KEY` -> the PUBLISHABLE_KEY
284
285
  // build-arg -> the NEXT_PUBLIC_ prefix Next inlines.
285
286
  //
286
- // A surface that provides neither has no key, and its anonymous traffic
287
- // files under `$public` (which drops track/identify/group and answers 200);
288
- // the fix is to give it the KMS-sourced env, not to hardcode the org key
289
- // here. The key is a credential-class value: its home is KMS, and the ONE
290
- // build reads it from there.
291
- ingestKey: config.ingestKey ?? readEnv('NEXT_PUBLIC_PUBLISHABLE_KEY'),
287
+ // 3. the org that owns the HOST this page is served from (`org.ts`).
288
+ //
289
+ // Step 3 fires only when a surface configured NOTHING, so it never overrides
290
+ // the KMS chain above it replaces going dark with reporting to the right
291
+ // brand. That is what a static export needs: `output: export` inlines no env,
292
+ // so before this the only way to report was to commit a key literal, and the
293
+ // fleet grew one copy per site of a value with a single source.
294
+ //
295
+ // A surface no brand claims still has no key, and the door REFUSES an
296
+ // unattributed event (401 `ingest_key_required`) — the reserved `$public`
297
+ // tenant that once caught keyless beacons is retired, and anonymous ingest is
298
+ // refused at every door on every brand host. So there is no quiet fallback to
299
+ // rely on: an event lands in the org a credential names, or it does not land.
300
+ ingestKey:
301
+ config.ingestKey ?? readEnv('NEXT_PUBLIC_PUBLISHABLE_KEY') ?? keyForPage(),
292
302
  }
293
303
  this.transport = config.transport ?? new DefaultTransport()
294
304
  // Error plane, most specific source first: an explicit DSN wins, then the
package/src/index.ts CHANGED
@@ -44,3 +44,10 @@ export type {
44
44
  Transport,
45
45
  WireEvent,
46
46
  } from './types'
47
+
48
+ /** Which org owns a host's telemetry — the resolution that lets a surface report
49
+ * correctly while configuring nothing. `keyFor` takes an optional keyring so a
50
+ * runtime that receives one (hanzo.id serves every brand from one image) resolves
51
+ * through this same function rather than a second copy of it. */
52
+ export { ORG_DOMAIN, ORG_KEY, orgOf, keyFor, keyForPage } from './org'
53
+ export type { Keyring } from './org'
@@ -0,0 +1,113 @@
1
+ import { describe, it, expect, afterEach } from 'vitest'
2
+
3
+ import { createAnalytics } from './core'
4
+ import { ORG_KEY, keyFor, keyForPage, orgOf } from './org'
5
+
6
+ const ENV = 'NEXT_PUBLIC_PUBLISHABLE_KEY'
7
+
8
+ /** Stand on a host the way a browser does, for the duration of one assertion. */
9
+ function onHost<T>(hostname: string, run: () => T): T {
10
+ const had = 'location' in globalThis
11
+ const prev = (globalThis as { location?: unknown }).location
12
+ Object.defineProperty(globalThis, 'location', {
13
+ value: { hostname },
14
+ configurable: true,
15
+ writable: true,
16
+ })
17
+ try {
18
+ return run()
19
+ } finally {
20
+ if (had) Object.defineProperty(globalThis, 'location', { value: prev, configurable: true, writable: true })
21
+ else delete (globalThis as { location?: unknown }).location
22
+ }
23
+ }
24
+
25
+ afterEach(() => {
26
+ delete process.env[ENV]
27
+ })
28
+
29
+ describe('the org that owns a host', () => {
30
+ it('claims a brand domain and every subdomain of it', () => {
31
+ expect(orgOf('hanzo.ai')).toBe('hanzo')
32
+ expect(orgOf('lux.network')).toBe('lux')
33
+ expect(orgOf('zoo.ngo')).toBe('zoo')
34
+ // an alias inherits by naming its brand's domain — no entry of its own
35
+ expect(orgOf('explore.lux.network')).toBe('lux')
36
+ expect(orgOf('id.zoo.network')).toBe('zoo')
37
+ })
38
+
39
+ it('reads a hostname the way a domain compares', () => {
40
+ expect(orgOf('LUX.NETWORK')).toBe('lux')
41
+ expect(orgOf('lux.network:3000')).toBe('lux')
42
+ expect(orgOf('lux.network.')).toBe('lux')
43
+ expect(orgOf(' hanzo.ai ')).toBe('hanzo')
44
+ })
45
+
46
+ it('claims nothing it does not own, rather than defaulting to Hanzo', () => {
47
+ // The defect this exists to prevent: an unrecognised host filed under a brand
48
+ // that is not its own is silent, reads as working, and surfaces a week later
49
+ // in someone else's warehouse.
50
+ for (const host of ['osage.id', 'pars.id', 'id.bootno.de', 'example.com', '']) {
51
+ expect(orgOf(host)).toBeUndefined()
52
+ expect(keyFor(host)).toBeUndefined()
53
+ }
54
+ // and it must not be fooled by a suffix that merely ENDS with a brand domain
55
+ expect(orgOf('notlux.network')).toBeUndefined()
56
+ expect(orgOf('evil-hanzo.ai')).toBeUndefined()
57
+ })
58
+ })
59
+
60
+ describe('the key a host resolves to', () => {
61
+ it('gives each brand its OWN key', () => {
62
+ const hanzo = keyFor('hanzo.ai')
63
+ const lux = keyFor('lux.network')
64
+ const zoo = keyFor('zoo.ngo')
65
+ for (const k of [hanzo, lux, zoo]) expect(k).toMatch(/^pk-/)
66
+ // The bug on record: one build-time key filed every brand's visitors into
67
+ // Hanzo's project. Three brands must resolve to three DIFFERENT keys.
68
+ expect(new Set([hanzo, lux, zoo]).size).toBe(3)
69
+ expect(lux).not.toBe(hanzo)
70
+ expect(zoo).not.toBe(hanzo)
71
+ })
72
+
73
+ it('refuses anything that is not publishable, failing closed', () => {
74
+ expect(keyFor('hanzo.ai', { hanzo: 'sk-live-secret' })).toBeUndefined()
75
+ expect(keyFor('hanzo.ai', { hanzo: '' })).toBeUndefined()
76
+ expect(keyFor('hanzo.ai', {})).toBeUndefined()
77
+ })
78
+
79
+ it('resolves a runtime keyring through the same function, not a second copy', () => {
80
+ // hanzo.id serves every brand from ONE image and gets its keyring at runtime.
81
+ expect(keyFor('id.lux.network', { lux: 'pk-runtime-lux' })).toBe('pk-runtime-lux')
82
+ })
83
+
84
+ it('is undefined off a browser, where there is no host to read', () => {
85
+ expect(keyForPage()).toBeUndefined()
86
+ })
87
+ })
88
+
89
+ describe('a surface that configures nothing', () => {
90
+ it('still reports, to its own brand', () => {
91
+ const lux = onHost('lux.network', () => createAnalytics({ product: 'site' }))
92
+ expect((lux as unknown as { cfg: { ingestKey?: string } }).cfg.ingestKey).toBe(ORG_KEY.lux)
93
+
94
+ const zoo = onHost('zoo.ngo', () => createAnalytics({ product: 'site' }))
95
+ expect((zoo as unknown as { cfg: { ingestKey?: string } }).cfg.ingestKey).toBe(ORG_KEY.zoo)
96
+ })
97
+
98
+ it('never overrides what the surface or the build already resolved', () => {
99
+ const explicit = onHost('lux.network', () =>
100
+ createAnalytics({ product: 'site', ingestKey: 'pk-explicit' })
101
+ )
102
+ expect((explicit as unknown as { cfg: { ingestKey?: string } }).cfg.ingestKey).toBe('pk-explicit')
103
+
104
+ process.env[ENV] = 'pk-from-the-build'
105
+ const env = onHost('lux.network', () => createAnalytics({ product: 'site' }))
106
+ expect((env as unknown as { cfg: { ingestKey?: string } }).cfg.ingestKey).toBe('pk-from-the-build')
107
+ })
108
+
109
+ it('has no key on a host no brand claims, and invents none', () => {
110
+ const none = onHost('example.com', () => createAnalytics({ product: 'site' }))
111
+ expect((none as unknown as { cfg: { ingestKey?: string } }).cfg.ingestKey).toBeUndefined()
112
+ })
113
+ })
package/src/org.ts ADDED
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Which org owns a host's telemetry.
3
+ *
4
+ * A surface should not have to LEARN its key. It already knows the domain it is
5
+ * served from, and a domain belongs to exactly one brand, so the key is derivable
6
+ * and asking a surface to carry one is asking it to restate something already
7
+ * true. This module answers `orgOf(location.hostname)` and hands back that org's
8
+ * publishable key, which is what lets a static export report correctly while
9
+ * configuring nothing.
10
+ *
11
+ * This is the SAME question `hanzo.id` answers at runtime for the identity hosts
12
+ * (`pkgs/shared/src/ingest.ts`): one image serves every brand, so the key cannot
13
+ * be a property of the build. It was, and the bill is on record — Lux's, Zoo's,
14
+ * Osage's and Pars' visitors were all filed in HANZO's project, because one
15
+ * build-time key was inlined for every brand. That is the white-label boundary
16
+ * crossed in the direction that shows up latest. The marketing sites carry the
17
+ * identical defect in its other form: each commits its OWN key literal, so the
18
+ * fleet holds N copies of a value with one source, and a site added tomorrow
19
+ * reports nothing until somebody remembers to paste one in.
20
+ *
21
+ * A `pk-` is PUBLISHABLE: it authorizes a write into one org and mints no reading
22
+ * principal. It is readable in devtools on every deployed page that sends an
23
+ * event, so stating it here exposes nothing that serving the page did not. What
24
+ * it does buy is that correct attribution becomes the DEFAULT rather than
25
+ * something each surface opts into.
26
+ *
27
+ * Keyed by ORG, never by host: a brand's facts repeat on every alias it owns
28
+ * (`lux.id`, `id.lux.network`), and a key stated per host would have to be
29
+ * repeated too — so the day someone adds an alias and forgets the key, that host
30
+ * silently stops reporting. Stated once per org, a new alias inherits its brand's
31
+ * key by naming its brand's domain, which is all an alias ever says.
32
+ */
33
+
34
+ /** One publishable key per org. */
35
+ export type Keyring = Readonly<Record<string, string>>
36
+
37
+ /**
38
+ * Each org's registrable domains. A host matches a domain when it IS that domain
39
+ * or a subdomain of it, so `explore.lux.network` needs no entry of its own.
40
+ *
41
+ * An org absent from this table resolves to undefined, and that is deliberate:
42
+ * `osage`, `pars` and `bootnode` have no project of their own yet, and reporting
43
+ * NOTHING is the honest answer. Filing them under a brand that is not theirs is
44
+ * the defect this module exists to prevent — it is silent, it reads as working,
45
+ * and it is only visible later in someone else's warehouse.
46
+ */
47
+ export const ORG_DOMAIN: Readonly<Record<string, readonly string[]>> = Object.freeze({
48
+ hanzo: Object.freeze(['hanzo.ai', 'hanzo.app', 'hanzo.chat', 'hanzo.id', 'hanzo.bot', 'hanzo.sh']),
49
+ lux: Object.freeze([
50
+ 'lux.network',
51
+ 'lux.exchange',
52
+ 'lux.market',
53
+ 'lux.finance',
54
+ 'lux.financial',
55
+ 'lux.credit',
56
+ 'lux.fund',
57
+ 'lux.id',
58
+ ]),
59
+ zoo: Object.freeze(['zoo.ngo', 'zoo.network', 'zoolabs.id']),
60
+ })
61
+
62
+ /**
63
+ * Each org's publishable key — the same values `universe`'s `SPA_INGEST_KEYRING`
64
+ * serves to the identity hosts, which are each brand's own insights team token.
65
+ * Add an org here the day its project exists, never before.
66
+ */
67
+ export const ORG_KEY: Keyring = Object.freeze({
68
+ hanzo: 'pk-rM_CdaF2MQckGCrla113SrR1oH4zvqN8xh2I95Z9tY8',
69
+ lux: 'pk-gUZp6ZVfhJzSwK-rb4oLbVkpCnMBx5uSCpxf_5yEhQk',
70
+ zoo: 'pk-3TKpKnERV9AQSsBUERWkZejC1O1mUxc1jRzsP3MPbs4',
71
+ })
72
+
73
+ /** `pk-` is publishable; `sk-` is not, and there is no third thing. Checked at the
74
+ * one point a key becomes something a page will send, so a mistyped entry fails
75
+ * closed rather than putting a secret in every visitor's tab. */
76
+ const PUBLISHABLE = 'pk-'
77
+
78
+ /** Strip the port and case a hostname the way a domain compares. */
79
+ const normalize = (host: string): string =>
80
+ host.trim().toLowerCase().replace(/\.$/, '').split(':')[0] ?? ''
81
+
82
+ /**
83
+ * The org that owns a host, or undefined when no brand claims it.
84
+ *
85
+ * Longest match wins, so a brand owning both a domain and a subdomain of another
86
+ * brand's cannot be decided by table order. Pure and total.
87
+ */
88
+ export function orgOf(host: string): string | undefined {
89
+ const h = normalize(host)
90
+ if (!h) return undefined
91
+ let best: string | undefined
92
+ let bestLen = 0
93
+ for (const [org, domains] of Object.entries(ORG_DOMAIN)) {
94
+ for (const d of domains) {
95
+ if ((h === d || h.endsWith(`.${d}`)) && d.length > bestLen) {
96
+ best = org
97
+ bestLen = d.length
98
+ }
99
+ }
100
+ }
101
+ return best
102
+ }
103
+
104
+ /**
105
+ * The publishable key for a host, or undefined when there is not exactly one to
106
+ * give.
107
+ *
108
+ * Deliberately WITHOUT a fallback: returning Hanzo's key for an unrecognised host
109
+ * is precisely the defect described above. Undefined is the honest answer and the
110
+ * caller reports nothing.
111
+ *
112
+ * `keyring` is a parameter so the identity runtime — which receives its keyring
113
+ * from `/config.json` because one image serves every brand — resolves through
114
+ * this SAME function rather than a second copy of it.
115
+ */
116
+ export function keyFor(host: string, keyring: Keyring = ORG_KEY): string | undefined {
117
+ const org = orgOf(host)
118
+ if (!org) return undefined
119
+ const key = keyring[org]
120
+ if (typeof key !== 'string') return undefined
121
+ const trimmed = key.trim()
122
+ return trimmed.startsWith(PUBLISHABLE) ? trimmed : undefined
123
+ }
124
+
125
+ /** The key for the page this code is running on; undefined off a browser. */
126
+ export function keyForPage(keyring: Keyring = ORG_KEY): string | undefined {
127
+ if (typeof location === 'undefined') return undefined
128
+ return keyFor(location.hostname, keyring)
129
+ }