@cross-deck/buckets 0.4.0 → 0.5.0

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/README.md CHANGED
@@ -388,6 +388,39 @@ reconciles against your provider's invoice instead of drifting from it.
388
388
 
389
389
  ---
390
390
 
391
+ ## One model: resource units
392
+
393
+ Buckets doesn't really measure "reads and writes." It measures **resource units** —
394
+ the raw quantity of whatever a service charges you for. Firestore happens to charge
395
+ in reads, writes, and deletes, so those are its units. Other sources charge
396
+ differently, and each gets its own units:
397
+
398
+ | Source | Resource units |
399
+ |---|---|
400
+ | **Firestore** | `read`, `write`, `delete` |
401
+ | ClickHouse | `clickhouse.query_ms`, `clickhouse.bytes_scanned` |
402
+ | Redis | `redis.memory_mb` |
403
+ | Cloudflare Workers | `cloudflare.invocations` |
404
+ | OpenAI | `openai.tokens` |
405
+
406
+ Two rules keep this honest, and they are the whole of the discipline:
407
+
408
+ 1. **Every resource keeps its own identity and its own unit.** A `read` is a read; a
409
+ `clickhouse.query_ms` is a query-millisecond. They are stored and shown on
410
+ **separate lines** and are **never added together** — there is deliberately no
411
+ "total units" number, because adding a read to a query-millisecond is meaningless.
412
+ 2. **Raw counts only — no money.** Buckets never multiplies units by a price or
413
+ guesses a dollar figure. It tells you *how much of each unit* a feature consumed;
414
+ you verify the cost against your provider's bill, which is the only source of
415
+ truth for money. (Prices change by plan, region, and date; the count doesn't.)
416
+
417
+ Every adapter — present and future — does the same thing: `record(resource, quantity)`
418
+ under a `bucket()`. Same model, many sources. That's why adapters are named for the
419
+ **source**, not the database: Buckets answers *"what did feature X consume,"* and
420
+ Firestore is simply the first place it found the leak.
421
+
422
+ ---
423
+
391
424
  ## Datastore support
392
425
 
393
426
  | Datastore | Status |
package/dist/index.d.mts CHANGED
@@ -1,7 +1,15 @@
1
- import { S as Sink } from './sink-DjFci4Uz.mjs';
2
- export { B as BucketsReport, O as OpCounts, R as ReportSink, a as ReportSinkConfig } from './sink-DjFci4Uz.mjs';
1
+ import { S as Sink } from './sink-Cojdm73l.mjs';
2
+ export { B as BucketsReport, O as OpCounts, R as ReportSink, a as ReportSinkConfig, b as ResourceCounts } from './sink-Cojdm73l.mjs';
3
3
 
4
- type OpType = "read" | "write" | "delete";
4
+ /**
5
+ * A resource unit — what an adapter counts. Firestore emits `read`/`write`/
6
+ * `delete`; other adapters emit their own (`clickhouse.query_ms`, `openai.tokens`).
7
+ * It is a free identifier on purpose, BUT each one is kept entirely distinct: the
8
+ * meter only ever sums quantities WITHIN a single resource, never across two.
9
+ */
10
+ type ResourceUnit = string;
11
+ /** @deprecated — Firestore-era name; use {@link ResourceUnit}. */
12
+ type OpType = ResourceUnit;
5
13
  /** Optional read-site hint — the collection touched, derived at the trap from the
6
14
  * path. Lets an UNtagged read cascade to `col:<collection>` instead of vanishing. */
7
15
  interface CostHint {
@@ -13,6 +21,14 @@ interface MeterConfig {
13
21
  flushIntervalMs?: number;
14
22
  onError?: (e: unknown) => void;
15
23
  }
24
+ /**
25
+ * Count `quantity` of a `resource` against the live tag. THE adapter primitive —
26
+ * a Firestore adapter records "read"; a ClickHouse adapter records
27
+ * "clickhouse.query_ms"; an OpenAI adapter records "openai.tokens". Each resource
28
+ * is bucketed entirely on its own; nothing is ever added across resources. Never
29
+ * throws.
30
+ */
31
+ declare function record(resource: ResourceUnit, quantity: number, hint?: CostHint): void;
16
32
  /** Firestore bills a minimum of one read even for an empty result, so 0 counts as 1. */
17
33
  declare function recordReads(n: number, hint?: CostHint): void;
18
34
  declare function recordWrites(n?: number): void;
@@ -131,4 +147,4 @@ interface InitOptions {
131
147
  */
132
148
  declare function init(options: InitOptions): void;
133
149
 
134
- export { type CostHint, type CostTag, type FirestoreClasses, type InitOptions, type MeterConfig, type OpType, Sink, bucket, currentCostTag, enterCostTag, flush, init, init as initBuckets, installFirestoreMeter, recordDeletes, recordReads, recordWrites, refineCostTag, runWithCostTag };
150
+ export { type CostHint, type CostTag, type FirestoreClasses, type InitOptions, type MeterConfig, type OpType, type ResourceUnit, Sink, bucket, currentCostTag, enterCostTag, flush, init, init as initBuckets, installFirestoreMeter, record, recordDeletes, recordReads, recordWrites, refineCostTag, runWithCostTag };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,15 @@
1
- import { S as Sink } from './sink-DjFci4Uz.js';
2
- export { B as BucketsReport, O as OpCounts, R as ReportSink, a as ReportSinkConfig } from './sink-DjFci4Uz.js';
1
+ import { S as Sink } from './sink-Cojdm73l.js';
2
+ export { B as BucketsReport, O as OpCounts, R as ReportSink, a as ReportSinkConfig, b as ResourceCounts } from './sink-Cojdm73l.js';
3
3
 
4
- type OpType = "read" | "write" | "delete";
4
+ /**
5
+ * A resource unit — what an adapter counts. Firestore emits `read`/`write`/
6
+ * `delete`; other adapters emit their own (`clickhouse.query_ms`, `openai.tokens`).
7
+ * It is a free identifier on purpose, BUT each one is kept entirely distinct: the
8
+ * meter only ever sums quantities WITHIN a single resource, never across two.
9
+ */
10
+ type ResourceUnit = string;
11
+ /** @deprecated — Firestore-era name; use {@link ResourceUnit}. */
12
+ type OpType = ResourceUnit;
5
13
  /** Optional read-site hint — the collection touched, derived at the trap from the
6
14
  * path. Lets an UNtagged read cascade to `col:<collection>` instead of vanishing. */
7
15
  interface CostHint {
@@ -13,6 +21,14 @@ interface MeterConfig {
13
21
  flushIntervalMs?: number;
14
22
  onError?: (e: unknown) => void;
15
23
  }
24
+ /**
25
+ * Count `quantity` of a `resource` against the live tag. THE adapter primitive —
26
+ * a Firestore adapter records "read"; a ClickHouse adapter records
27
+ * "clickhouse.query_ms"; an OpenAI adapter records "openai.tokens". Each resource
28
+ * is bucketed entirely on its own; nothing is ever added across resources. Never
29
+ * throws.
30
+ */
31
+ declare function record(resource: ResourceUnit, quantity: number, hint?: CostHint): void;
16
32
  /** Firestore bills a minimum of one read even for an empty result, so 0 counts as 1. */
17
33
  declare function recordReads(n: number, hint?: CostHint): void;
18
34
  declare function recordWrites(n?: number): void;
@@ -131,4 +147,4 @@ interface InitOptions {
131
147
  */
132
148
  declare function init(options: InitOptions): void;
133
149
 
134
- export { type CostHint, type CostTag, type FirestoreClasses, type InitOptions, type MeterConfig, type OpType, Sink, bucket, currentCostTag, enterCostTag, flush, init, init as initBuckets, installFirestoreMeter, recordDeletes, recordReads, recordWrites, refineCostTag, runWithCostTag };
150
+ export { type CostHint, type CostTag, type FirestoreClasses, type InitOptions, type MeterConfig, type OpType, type ResourceUnit, Sink, bucket, currentCostTag, enterCostTag, flush, init, init as initBuckets, installFirestoreMeter, record, recordDeletes, recordReads, recordWrites, refineCostTag, runWithCostTag };
package/dist/index.js CHANGED
@@ -46,33 +46,33 @@ function ensureFlushLoop() {
46
46
  process.once?.("SIGTERM", () => void flush());
47
47
  process.once?.("beforeExit", () => void flush());
48
48
  }
49
- function recordFirestore(op, n, hint) {
49
+ function record(resource, quantity, hint) {
50
50
  try {
51
- if (!Number.isFinite(n) || n <= 0) return;
51
+ if (!Number.isFinite(quantity) || quantity <= 0) return;
52
52
  const t = currentCostTag();
53
53
  const date = utcDate();
54
54
  const label = t.label || (hint?.collection ? `col:${hint.collection}` : "uncategorized");
55
- const lk = date + SEP + op + SEP + label;
56
- labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + n);
57
- const hk = date + SEP + op + SEP + utcHour();
58
- hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + n);
55
+ const lk = date + SEP + resource + SEP + label;
56
+ labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + quantity);
57
+ const hk = date + SEP + resource + SEP + utcHour();
58
+ hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + quantity);
59
59
  ensureFlushLoop();
60
60
  if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS) void flush();
61
61
  } catch {
62
62
  }
63
63
  }
64
64
  function recordReads(n, hint) {
65
- recordFirestore("read", Math.max(n, 1), hint);
65
+ record("read", Math.max(n, 1), hint);
66
66
  }
67
67
  function recordWrites(n = 1) {
68
- recordFirestore("write", n);
68
+ record("write", n);
69
69
  }
70
70
  function recordDeletes(n = 1) {
71
- recordFirestore("delete", n);
71
+ record("delete", n);
72
72
  }
73
- function add(target, key, op, n) {
73
+ function add(target, key, resource, n) {
74
74
  const bag = target[key] ??= {};
75
- bag[op] = (bag[op] ?? 0) + n;
75
+ bag[resource] = (bag[resource] ?? 0) + n;
76
76
  }
77
77
  async function flush() {
78
78
  if (flushing) return;
@@ -276,6 +276,7 @@ exports.flush = flush;
276
276
  exports.init = init;
277
277
  exports.initBuckets = init;
278
278
  exports.installFirestoreMeter = installFirestoreMeter;
279
+ exports.record = record;
279
280
  exports.recordDeletes = recordDeletes;
280
281
  exports.recordReads = recordReads;
281
282
  exports.recordWrites = recordWrites;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/cost-context.ts","../src/cost-meter.ts","../src/sink.ts","../src/adapters/firestore.ts","../src/index.ts"],"names":["AsyncLocalStorage","sink"],"mappings":";;;;;AAqBA,IAAM,cAAuB,EAAC;AAC9B,IAAM,KAAA,GAAQ,IAAIA,6BAAA,EAA2B;AAGtC,SAAS,cAAA,CAAkB,KAAc,EAAA,EAAgB;AAC9D,EAAA,OAAO,MAAM,GAAA,CAAI,EAAE,GAAG,GAAA,IAAO,EAAE,CAAA;AACjC;AAGO,SAAS,aAAa,GAAA,EAAoB;AAC/C,EAAA,KAAA,CAAM,SAAA,CAAU,EAAE,GAAG,GAAA,EAAK,CAAA;AAC5B;AAGO,SAAS,cAAc,KAAA,EAA+B;AAC3D,EAAA,MAAM,GAAA,GAAM,MAAM,QAAA,EAAS;AAC3B,EAAA,IAAI,GAAA,EAAK,MAAA,CAAO,MAAA,CAAO,GAAA,EAAK,KAAK,CAAA;AACnC;AAGO,SAAS,cAAA,GAA0B;AACxC,EAAA,OAAO,KAAA,CAAM,UAAS,IAAK,WAAA;AAC7B;AAWO,SAAS,MAAA,CAAU,MAAc,EAAA,EAAgB;AACtD,EAAA,OAAO,cAAA,CAAe,EA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Q,OAAA,CAAQ,MAAA,EAAQ,QAAA,EAAU,OAAA,CAAQ,QAAA,EAAU,CAAA;AAClG,EAAA,cAAA,CAAe,EAAE,MAAAA,KAAAA,EAAM,eAAA,EAAiB,QAAQ,eAAA,EAAiB,OAAA,EAAS,OAAA,CAAQ,OAAA,EAAS,CAAA;AAC3F,EAAA,IAAI,OAAA,CAAQ,SAAA,EAAW,qBAAA,CAAsB,OAAA,CAAQ,SAAS,CAAA;AAChE","file":"index.js","sourcesContent":["/**\n * cost-context — the request-scoped tag every counted operation attributes\n * itself to. Set it ONCE at a boundary (or wrap a path with `bucket()`); it\n * rides Node's AsyncLocalStorage down through every async fan-out, so one\n * handler that triggers 15 reads attributes all 15 to the same bucket — with\n * zero per-call-site work.\n *\n * Generic by design: unlike a hardcoded product taxonomy, the only meaningful\n * field a consumer sets is the free-form `label` (the bucket name). `feature`\n * is an optional coarse grouping if you want one; nothing here is\n * Crossdeck-specific.\n */\nimport { AsyncLocalStorage } from \"node:async_hooks\";\n\nexport interface CostTag {\n /** Optional coarse grouping (a caller-defined surface name). */\n feature?: string;\n /** The bucket name — what `bucket()` sets. Drives the report's `byLabel`. */\n label?: string;\n}\n\nconst DEFAULT_TAG: CostTag = {};\nconst store = new AsyncLocalStorage<CostTag>();\n\n/** Run `fn` with `tag` bound for its entire async subtree. */\nexport function runWithCostTag<T>(tag: CostTag, fn: () => T): T {\n return store.run({ ...tag }, fn);\n}\n\n/** Bind a tag for the remainder of the current async context (no closure to wrap). */\nexport function enterCostTag(tag: CostTag): void {\n store.enterWith({ ...tag });\n}\n\n/** Refine the live tag in place (e.g. stamp a feature after the boundary). */\nexport function refineCostTag(patch: Partial<CostTag>): void {\n const cur = store.getStore();\n if (cur) Object.assign(cur, patch);\n}\n\n/** The current tag, or a safe empty default outside any bound context. */\nexport function currentCostTag(): CostTag {\n return store.getStore() ?? DEFAULT_TAG;\n}\n\n/**\n * `bucket(name, fn)` — the headline verb, the `track()` of cost. Run `fn` with\n * every operation inside it attributed to the bucket `name`; the attribution\n * rides the async subtree automatically. The one verb most developers ever touch:\n *\n * await bucket(\"nightly-export\", async () => {\n * const rows = await db.collection(\"events\").where(...).get(); // → \"nightly-export\"\n * });\n */\nexport function bucket<T>(name: string, fn: () => T): T {\n return runWithCostTag({ ...currentCostTag(), label: name }, fn);\n}\n","/**\n * cost-meter — counts operations against the ambient tag and flushes them to the\n * configured Sink cheaply.\n *\n * LOW-OVERHEAD CONTRACT (the thing that warns you about reads must not run them\n * up): counts accumulate in an in-memory buffer and flush periodically — NEVER one\n * network call per counted operation. A flush coalesces the whole window into one\n * report per UTC day and hands it to the Sink. At steady state that is ~1 small\n * request a minute, regardless of how many ops you served.\n *\n * BEST-EFFORT CONTRACT: metering must never throw into your code. Every recorder\n * swallows its own errors; a failed flush drops that window's counts (surfaced via\n * `onError` if you pass one) rather than disturbing the app.\n */\nimport { currentCostTag } from \"./cost-context\";\nimport type { Sink, BucketsReport, OpCounts } from \"./sink\";\n\nexport type OpType = \"read\" | \"write\" | \"delete\";\n\n/** Optional read-site hint — the collection touched, derived at the trap from the\n * path. Lets an UNtagged read cascade to `col:<collection>` instead of vanishing. */\nexport interface CostHint {\n collection?: string;\n projectId?: string;\n}\n\n// NUL separator — a bucket/collection name can contain almost anything except\n// this, so the key never collides with a name that has a \"|\" or \":\" in it.\nconst SEP = \"\\u001f\"; // ASCII Unit Separator\n\n/** key = date <NUL> op <NUL> label → count */\nconst labelBuffer = new Map<string, number>();\n/** key = date <NUL> op <NUL> hour → count */\nconst hourBuffer = new Map<string, number>();\n\nlet sink: Sink | null = null;\nlet flushIntervalMs = 60_000;\nlet onError: ((e: unknown) => void) | null = null;\nlet timer: ReturnType<typeof setInterval> | null = null;\nlet flushing = false;\n/** Safety valve — flush early if a burst fills the buffer between intervals. */\nconst MAX_BUFFER_KEYS = 5_000;\n\nexport interface MeterConfig {\n sink: Sink;\n flushIntervalMs?: number;\n onError?: (e: unknown) => void;\n}\n\n/** Point the meter at a sink. Called by `init()`; pass your own sink to self-host. */\nexport function configureMeter(config: MeterConfig): void {\n sink = config.sink;\n if (config.flushIntervalMs && config.flushIntervalMs > 0) flushIntervalMs = config.flushIntervalMs;\n onError = config.onError ?? null;\n}\n\nconst utcDate = (): string => new Date().toISOString().slice(0, 10);\nconst utcHour = (): string => new Date().toISOString().slice(11, 13);\n\nfunction ensureFlushLoop(): void {\n if (timer) return;\n timer = setInterval(() => void flush(), flushIntervalMs);\n // Don't keep the event loop alive just for metering.\n (timer as { unref?: () => void }).unref?.();\n // Flush the last window on shutdown.\n process.once?.(\"SIGTERM\", () => void flush());\n process.once?.(\"beforeExit\", () => void flush());\n}\n\n/** Count `n` ops of `op` against the live tag. Never throws. */\nexport function recordFirestore(op: OpType, n: number, hint?: CostHint): void {\n try {\n if (!Number.isFinite(n) || n <= 0) return;\n const t = currentCostTag();\n const date = utcDate();\n // CASCADE — every op gets a label, by design (no blind spots): the bucket name\n // wins; else the collection it actually touched (`col:posts`); else\n // \"uncategorized\" as a loud last resort. A read is never invisible.\n const label = t.label || (hint?.collection ? `col:${hint.collection}` : \"uncategorized\");\n const lk = date + SEP + op + SEP + label;\n labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + n);\n const hk = date + SEP + op + SEP + utcHour();\n hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + n);\n ensureFlushLoop();\n if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS) void flush();\n } catch {\n /* metering is best-effort — never disturb the caller */\n }\n}\n\n/** Firestore bills a minimum of one read even for an empty result, so 0 counts as 1. */\nexport function recordReads(n: number, hint?: CostHint): void {\n recordFirestore(\"read\", Math.max(n, 1), hint);\n}\nexport function recordWrites(n = 1): void {\n recordFirestore(\"write\", n);\n}\nexport function recordDeletes(n = 1): void {\n recordFirestore(\"delete\", n);\n}\n\nfunction add(target: Record<string, OpCounts>, key: string, op: OpType, n: number): void {\n const bag = (target[key] ??= {});\n bag[op] = (bag[op] ?? 0) + n;\n}\n\n/**\n * Coalesce the buffer into one report per UTC day and hand each to the Sink.\n * Snapshots + clears up front so concurrent records land in the next window.\n * Never throws; a sink failure drops that window (surfaced via `onError`).\n */\nexport async function flush(): Promise<void> {\n if (flushing) return;\n // Not configured (init not called) — drop, don't grow unbounded.\n if (!sink) {\n labelBuffer.clear();\n hourBuffer.clear();\n return;\n }\n if (labelBuffer.size === 0 && hourBuffer.size === 0) return;\n flushing = true;\n\n const labels = new Map(labelBuffer);\n const hours = new Map(hourBuffer);\n labelBuffer.clear();\n hourBuffer.clear();\n\n try {\n const byDate = new Map<string, BucketsReport>();\n const reportFor = (date: string): BucketsReport => {\n let r = byDate.get(date);\n if (!r) {\n r = { date, byLabel: {}, byHour: {} };\n byDate.set(date, r);\n }\n return r;\n };\n for (const [k, n] of labels) {\n const [date, op, label] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byLabel, label, op, n);\n }\n for (const [k, n] of hours) {\n const [date, op, hour] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byHour!, hour, op, n);\n }\n for (const report of byDate.values()) {\n await sink.flush(report);\n }\n } catch (e) {\n // Drop this window rather than risk a partial/double report on retry.\n onError?.(e);\n } finally {\n flushing = false;\n }\n}\n","/**\n * sink — where the meter sends a coalesced rollup, and the wire shape it sends.\n *\n * Abstracting the sink is what makes Buckets storage-agnostic: the meter never\n * knows where counts go. The DEFAULT sink (`ReportSink`) reports up to Crossdeck's\n * ingest endpoint so the numbers surface on your Crossdeck dashboard. A team that\n * wants to self-host can implement `Sink` against anything (Postgres, a file, your\n * own API) without touching the meter.\n */\n\nexport interface OpCounts {\n read?: number;\n write?: number;\n delete?: number;\n}\n\n/**\n * One coalesced report — the wire contract (see docs/ROLLUP_SCHEMA.md). The meter\n * produces one of these per UTC day in a flush window (usually exactly one).\n */\nexport interface BucketsReport {\n /** UTC day \"YYYY-MM-DD\". */\n date: string;\n /** bucket name → counts. The heart of the report. */\n byLabel: Record<string, OpCounts>;\n /** UTC hour \"HH\" → counts, for the hourly \"did my fix land this hour?\" view. */\n byHour?: Record<string, OpCounts>;\n}\n\n/**\n * A destination for coalesced rollups. `flush` MAY throw on failure — the meter\n * catches it, drops that one window, and never lets it reach your app.\n */\nexport interface Sink {\n flush(report: BucketsReport): Promise<void>;\n}\n\nexport interface ReportSinkConfig {\n /** The project's `cd_sk_` secret key. Server-to-server only. */\n apiKey: string;\n /** Defaults to https://api.cross-deck.com/v1/buckets/report */\n endpoint?: string;\n /** Request timeout (ms); a slow Crossdeck must never stall your flush. */\n timeoutMs?: number;\n}\n\nconst DEFAULT_ENDPOINT = \"https://api.cross-deck.com/v1/buckets/report\";\n\n/**\n * The default sink: POST one coalesced rollup to Crossdeck's ingest endpoint.\n * The ingest folds it into the day's maintained doc with `increment`, so many\n * reports a minute coalesce safely. This path does ZERO database reads — it sends\n * a summary, it does not read. Throws on a non-202 so the meter can log/drop the\n * window; the meter guarantees it never reaches your app.\n */\nexport class ReportSink implements Sink {\n private readonly endpoint: string;\n private readonly apiKey: string;\n private readonly timeoutMs: number;\n\n constructor(config: ReportSinkConfig) {\n this.endpoint = config.endpoint ?? DEFAULT_ENDPOINT;\n this.apiKey = config.apiKey;\n this.timeoutMs = config.timeoutMs ?? 5000;\n }\n\n async flush(report: BucketsReport): Promise<void> {\n const res = await fetch(this.endpoint, {\n method: \"POST\",\n signal: AbortSignal.timeout(this.timeoutMs),\n headers: {\n \"content-type\": \"application/json\",\n authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify(report),\n });\n if (res.status !== 202) {\n throw new Error(`Buckets report rejected: HTTP ${res.status}`);\n }\n }\n}\n","/**\n * adapters/firestore — the universal Firestore read meter (the trap).\n *\n * THE LESSON (learned on a real product): per-call-site `recordReads()`\n * instrumentation MISSES paths. You meter the read sites you're looking at and\n * leave the cron / trigger / ingest path uncounted — often the majority of reads,\n * invisible. Humans tag what they see and miss the path that matters.\n *\n * THE FIX: patch the admin SDK's read methods ONCE. From install onward, EVERY\n * read — anywhere, on any code path — is counted under the ambient tag, with zero\n * per-call-site work and no blind spots.\n *\n * SAFETY CONTRACT — this sits on your production read path, so it is defensive by\n * construction. Each wrapper:\n * 1. calls the REAL method first and captures the result,\n * 2. counts in a try/catch that can never throw into the caller,\n * 3. ALWAYS returns the real result, untouched.\n * It cannot break a read, change a result, or add latency beyond one in-memory\n * counter increment. A wrong count is a measurement error, never a correctness or\n * availability one. Idempotent — calling it twice patches once.\n *\n * COUNTING MODEL — a query returning N docs = N reads (an empty result still bills\n * 1, which the meter enforces). A document get = 1. getAll(...) = the ref count.\n * CollectionReference.get IS Query.get (shared prototype method), so patching Query\n * covers collections with no double-count.\n */\nimport { recordReads, type CostHint } from \"../cost-meter\";\n\nlet installed = false;\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\ntype AnyFn = (...args: any[]) => any;\n\n/**\n * The firebase-admin Firestore classes to patch. Pass the module namespace from\n * `firebase-admin/firestore` — only the prototypes present are patched.\n */\nexport interface FirestoreClasses {\n Query?: { prototype: { get?: AnyFn; onSnapshot?: AnyFn } };\n DocumentReference?: { prototype: { get?: AnyFn; onSnapshot?: AnyFn } };\n Transaction?: { prototype: { get?: AnyFn; getAll?: AnyFn } };\n Firestore?: { prototype: { getAll?: AnyFn } };\n /** count() / sum() / average() — aggregation queries bill reads too. */\n AggregateQuery?: { prototype: { get?: AnyFn } };\n}\n\n/** `projects/{id}/…` → the project id, else undefined. Pure string op. */\nfunction projectFromPath(path: string): string | undefined {\n const parts = path.split(\"/\");\n const i = parts.indexOf(\"projects\");\n return i >= 0 && parts[i + 1] ? parts[i + 1] : undefined;\n}\n\n/**\n * Derive { collection, projectId } from the read target's path so an UNtagged read\n * cascades to `col:<collection>` instead of \"uncategorized\". PURE CPU; never reads,\n * never throws. Falls back to firebase-admin's internal `_queryOptions` for filtered\n * queries (which don't expose `.path`).\n */\nfunction hintFrom(target: any): CostHint | undefined {\n try {\n const p = typeof target?.path === \"string\" ? target.path : \"\";\n if (p) {\n const parts = p.split(\"/\").filter(Boolean);\n const collection = parts.length % 2 === 0 ? parts[parts.length - 2] : parts[parts.length - 1];\n return { collection, projectId: projectFromPath(p) };\n }\n const qo = target?._queryOptions;\n if (qo) {\n const collection = typeof qo.collectionId === \"string\" ? qo.collectionId : undefined;\n const parent =\n typeof qo.parentPath?.relativeName === \"string\"\n ? qo.parentPath.relativeName\n : typeof qo.parentPath?.toString === \"function\"\n ? String(qo.parentPath.toString())\n : \"\";\n return { collection, projectId: parent ? projectFromPath(parent) : undefined };\n }\n } catch {\n /* the meter must never throw */\n }\n return undefined;\n}\n\nfunction meterSnap(snap: unknown, hint?: CostHint): void {\n try {\n const size = (snap as { size?: number } | null)?.size;\n recordReads(typeof size === \"number\" ? size : 1, hint);\n } catch {\n /* best-effort */\n }\n}\nfunction meterCount(n: number, hint?: CostHint): void {\n try {\n recordReads(n, hint);\n } catch {\n /* best-effort */\n }\n}\n\n/**\n * Install the universal read meter on the firebase-admin Firestore classes. Call\n * ONCE at process start, before any reads. Pass the namespace from\n * `firebase-admin/firestore` so the exact prototypes the SDK uses are patched:\n *\n * import * as Firestore from \"firebase-admin/firestore\";\n * installFirestoreMeter(Firestore);\n */\nexport function installFirestoreMeter(classes: FirestoreClasses): void {\n if (installed) return;\n installed = true;\n const { Query, DocumentReference, Transaction, Firestore, AggregateQuery } = classes;\n\n // Query.get — covers Query AND CollectionReference (shared prototype method).\n const qGet = Query?.prototype?.get;\n if (qGet) {\n Query!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await qGet.apply(this, args);\n meterSnap(snap, hintFrom(this));\n return snap;\n };\n }\n\n // DocumentReference.get — a single doc = 1 read.\n const dGet = DocumentReference?.prototype?.get;\n if (dGet) {\n DocumentReference!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await dGet.apply(this, args);\n meterCount(1, hintFrom(this));\n return snap;\n };\n }\n\n // Transaction.get — query or doc; size when present, else 1.\n const tGet = Transaction?.prototype?.get;\n if (tGet) {\n Transaction!.prototype.get = async function (this: unknown, ...args: any[]) {\n const res = await tGet.apply(this, args);\n meterSnap(res, hintFrom(args[0]));\n return res;\n };\n }\n\n // Transaction.getAll(...refs) — one read per ref.\n const tGetAll = Transaction?.prototype?.getAll;\n if (tGetAll) {\n Transaction!.prototype.getAll = async function (this: unknown, ...args: any[]) {\n const res = await tGetAll.apply(this, args);\n meterCount(Array.isArray(res) ? res.length : args.length || 1, hintFrom(args[0]));\n return res;\n };\n }\n\n // Firestore.getAll(...refs) — batched doc reads.\n const fGetAll = Firestore?.prototype?.getAll;\n if (fGetAll) {\n Firestore!.prototype.getAll = async function (this: unknown, ...args: any[]) {\n const res = await fGetAll.apply(this, args);\n meterCount(Array.isArray(res) ? res.length : args.length || 1, hintFrom(args[0]));\n return res;\n };\n }\n\n // AggregateQuery.get — count() / sum() / average(). Firestore bills one read per\n // up to 1000 index entries scanned, and the client never sees that entry count —\n // so this is an HONEST ESTIMATE: for count() we derive it from the result\n // (ceil(count / 1000)), else the billed minimum of 1. Observe-only: it reads the\n // result you already got, adds zero reads. (Closes the aggregation blind spot.)\n const aGet = AggregateQuery?.prototype?.get;\n if (aGet) {\n AggregateQuery!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await aGet.apply(this, args);\n try {\n const data = (snap as { data?: () => { count?: number } } | null)?.data?.();\n const count = typeof data?.count === \"number\" ? data.count : 0;\n meterCount(Math.max(1, Math.ceil(count / 1000)), hintFrom(this));\n } catch {\n /* best-effort */\n }\n return snap;\n };\n }\n\n // Query.onSnapshot / DocumentReference.onSnapshot — server-side realtime\n // listeners. We OBSERVE: wrap onNext and count the docs each fire delivers (a\n // query's changed docs — first fire = all matching; a doc = 1). We attach no\n // listener and issue no read; the meter just sees what the listener is already\n // billed. (Closes the server-listener blind spot.)\n const patchOnSnapshot = (proto: { onSnapshot?: AnyFn } | undefined, perDoc: boolean): void => {\n const real = proto?.onSnapshot;\n if (!real) return;\n proto!.onSnapshot = function (this: unknown, ...args: any[]) {\n const hint = hintFrom(this);\n const i = args.findIndex((a) => typeof a === \"function\");\n if (i >= 0) {\n const onNext = args[i];\n args[i] = function (snap: any) {\n try {\n const n = perDoc\n ? 1\n : typeof snap?.docChanges === \"function\"\n ? snap.docChanges().length\n : typeof snap?.size === \"number\"\n ? snap.size\n : 1;\n if (n > 0) meterCount(n, hint);\n } catch {\n /* best-effort */\n }\n return onNext(snap);\n };\n }\n return real.apply(this, args);\n };\n };\n patchOnSnapshot(Query?.prototype, false);\n patchOnSnapshot(DocumentReference?.prototype, true);\n}\n\n/** Test-only: reset the install guard so a suite can re-patch fresh prototypes. */\nexport function __resetFirestoreMeterForTests(): void {\n installed = false;\n}\n","/**\n * @cross-deck/buckets — know exactly what every database read costs you, and who\n * caused it. A tiny, never-throws collector for Firestore.\n *\n * The whole footprint a consumer sees:\n * 1. init({ apiKey, firestore }) — configure once, install the trap once\n * 2. bucket(name, fn) — name the read paths that matter\n * 3. (the dashboard shows the rest — and names the ones you haven't yet)\n */\nimport { configureMeter, type MeterConfig } from \"./cost-meter\";\nimport { ReportSink, type Sink } from \"./sink\";\nimport { installFirestoreMeter, type FirestoreClasses } from \"./adapters/firestore\";\n\nexport interface InitOptions {\n /** The project's `cd_sk_` SECRET key. Server-to-server only — never a browser key. */\n apiKey: string;\n /**\n * Pass the namespace from `firebase-admin/firestore` to auto-install the read\n * trap (recommended — this is what makes every read count with no per-call work).\n * Omit it if you'd rather call `installFirestoreMeter()` yourself, or you only\n * use the manual `recordReads()` recorders.\n */\n firestore?: FirestoreClasses;\n /** Override the report endpoint (defaults to Crossdeck's ingest). */\n endpoint?: string;\n /** How often to flush coalesced counts (ms). Default 60_000. */\n flushIntervalMs?: number;\n /** Bring your own sink (self-host the rollups). Defaults to reporting up to Crossdeck. */\n sink?: Sink;\n /** Notified when a flush fails, so a dropped window is never silent. Best-effort. */\n onError?: MeterConfig[\"onError\"];\n}\n\n/**\n * Configure Buckets once, at process start. Points the meter at a sink (Crossdeck's\n * ingest by default) and — if you pass `firestore` — installs the universal read\n * trap so every read counts automatically.\n */\nexport function init(options: InitOptions): void {\n const sink = options.sink ?? new ReportSink({ apiKey: options.apiKey, endpoint: options.endpoint });\n configureMeter({ sink, flushIntervalMs: options.flushIntervalMs, onError: options.onError });\n if (options.firestore) installFirestoreMeter(options.firestore);\n}\n\n/** Alias — reads well next to `bucket()` at a call site. */\nexport { init as initBuckets };\n\n// The headline verb + the lower-level tag controls it is sugar over.\nexport {\n bucket,\n runWithCostTag,\n enterCostTag,\n refineCostTag,\n currentCostTag,\n type CostTag,\n} from \"./cost-context\";\n\n// Manual recorders (for non-Firestore ops, or when you don't install the trap).\nexport {\n recordReads,\n recordWrites,\n recordDeletes,\n flush,\n type CostHint,\n type OpType,\n type MeterConfig,\n} from \"./cost-meter\";\n\n// The trap (the only datastore adapter today) + its class shape.\nexport { installFirestoreMeter, type FirestoreClasses } from \"./adapters/firestore\";\n\n// The sink seam — for self-hosting rollups instead of reporting to Crossdeck.\nexport { ReportSink, type Sink, type BucketsReport, type OpCounts, type ReportSinkConfig } from \"./sink\";\n"]}
1
+ {"version":3,"sources":["../src/cost-context.ts","../src/cost-meter.ts","../src/sink.ts","../src/adapters/firestore.ts","../src/index.ts"],"names":["AsyncLocalStorage","sink"],"mappings":";;;;;AAqBA,IAAM,cAAuB,EAAC;AAC9B,IAAM,KAAA,GAAQ,IAAIA,6BAAA,EAA2B;AAGtC,SAAS,cAAA,CAAkB,KAAc,EAAA,EAAgB;AAC9D,EAAA,OAAO,MAAM,GAAA,CAAI,EAAE,GAAG,GAAA,IAAO,EAAE,CAAA;AACjC;AAGO,SAAS,aAAa,GAAA,EAAoB;AAC/C,EAAA,KAAA,CAAM,SAAA,CAAU,EAAE,GAAG,GAAA,EAAK,CAAA;AAC5B;AAGO,SAAS,cAAc,KAAA,EAA+B;AAC3D,EAAA,MAAM,GAAA,GAAM,MAAM,QAAA,EAAS;AAC3B,EAAA,IAAI,GAAA,EAAK,MAAA,CAAO,MAAA,CAAO,GAAA,EAAK,KAAK,CAAA;AACnC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WAAW,IAAI,CAAA;AACpD;;;ACnLO,SAAS,KAAK,OAAA,EAA4B;AAC/C,EAAA,MAAMC,KAAAA,GAAO,OAAA,CAAQ,IAAA,IAAQ,IAAI,UAAA,CAAW,EAAE,MAAA,EAAQ,OAAA,CAAQ,MAAA,EAAQ,QAAA,EAAU,OAAA,CAAQ,QAAA,EAAU,CAAA;AAClG,EAAA,cAAA,CAAe,EAAE,MAAAA,KAAAA,EAAM,eAAA,EAAiB,QAAQ,eAAA,EAAiB,OAAA,EAAS,OAAA,CAAQ,OAAA,EAAS,CAAA;AAC3F,EAAA,IAAI,OAAA,CAAQ,SAAA,EAAW,qBAAA,CAAsB,OAAA,CAAQ,SAAS,CAAA;AAChE","file":"index.js","sourcesContent":["/**\n * cost-context — the request-scoped tag every counted operation attributes\n * itself to. Set it ONCE at a boundary (or wrap a path with `bucket()`); it\n * rides Node's AsyncLocalStorage down through every async fan-out, so one\n * handler that triggers 15 reads attributes all 15 to the same bucket — with\n * zero per-call-site work.\n *\n * Generic by design: unlike a hardcoded product taxonomy, the only meaningful\n * field a consumer sets is the free-form `label` (the bucket name). `feature`\n * is an optional coarse grouping if you want one; nothing here is\n * Crossdeck-specific.\n */\nimport { AsyncLocalStorage } from \"node:async_hooks\";\n\nexport interface CostTag {\n /** Optional coarse grouping (a caller-defined surface name). */\n feature?: string;\n /** The bucket name — what `bucket()` sets. Drives the report's `byLabel`. */\n label?: string;\n}\n\nconst DEFAULT_TAG: CostTag = {};\nconst store = new AsyncLocalStorage<CostTag>();\n\n/** Run `fn` with `tag` bound for its entire async subtree. */\nexport function runWithCostTag<T>(tag: CostTag, fn: () => T): T {\n return store.run({ ...tag }, fn);\n}\n\n/** Bind a tag for the remainder of the current async context (no closure to wrap). */\nexport function enterCostTag(tag: CostTag): void {\n store.enterWith({ ...tag });\n}\n\n/** Refine the live tag in place (e.g. stamp a feature after the boundary). */\nexport function refineCostTag(patch: Partial<CostTag>): void {\n const cur = store.getStore();\n if (cur) Object.assign(cur, patch);\n}\n\n/** The current tag, or a safe empty default outside any bound context. */\nexport function currentCostTag(): CostTag {\n return store.getStore() ?? DEFAULT_TAG;\n}\n\n/**\n * `bucket(name, fn)` — the headline verb, the `track()` of cost. Run `fn` with\n * every operation inside it attributed to the bucket `name`; the attribution\n * rides the async subtree automatically. The one verb most developers ever touch:\n *\n * await bucket(\"nightly-export\", async () => {\n * const rows = await db.collection(\"events\").where(...).get(); // → \"nightly-export\"\n * });\n */\nexport function bucket<T>(name: string, fn: () => T): T {\n return runWithCostTag({ ...currentCostTag(), label: name }, fn);\n}\n","/**\n * cost-meter — counts operations against the ambient tag and flushes them to the\n * configured Sink cheaply.\n *\n * LOW-OVERHEAD CONTRACT (the thing that warns you about reads must not run them\n * up): counts accumulate in an in-memory buffer and flush periodically — NEVER one\n * network call per counted operation. A flush coalesces the whole window into one\n * report per UTC day and hands it to the Sink. At steady state that is ~1 small\n * request a minute, regardless of how many ops you served.\n *\n * BEST-EFFORT CONTRACT: metering must never throw into your code. Every recorder\n * swallows its own errors; a failed flush drops that window's counts (surfaced via\n * `onError` if you pass one) rather than disturbing the app.\n */\nimport { currentCostTag } from \"./cost-context\";\nimport type { Sink, BucketsReport, ResourceCounts } from \"./sink\";\n\n/**\n * A resource unit — what an adapter counts. Firestore emits `read`/`write`/\n * `delete`; other adapters emit their own (`clickhouse.query_ms`, `openai.tokens`).\n * It is a free identifier on purpose, BUT each one is kept entirely distinct: the\n * meter only ever sums quantities WITHIN a single resource, never across two.\n */\nexport type ResourceUnit = string;\n/** @deprecated — Firestore-era name; use {@link ResourceUnit}. */\nexport type OpType = ResourceUnit;\n\n/** Optional read-site hint — the collection touched, derived at the trap from the\n * path. Lets an UNtagged read cascade to `col:<collection>` instead of vanishing. */\nexport interface CostHint {\n collection?: string;\n projectId?: string;\n}\n\n// NUL separator — a bucket/collection name can contain almost anything except\n// this, so the key never collides with a name that has a \"|\" or \":\" in it.\nconst SEP = \"\\u001f\"; // ASCII Unit Separator\n\n/** key = date <NUL> op <NUL> label → count */\nconst labelBuffer = new Map<string, number>();\n/** key = date <NUL> op <NUL> hour → count */\nconst hourBuffer = new Map<string, number>();\n\nlet sink: Sink | null = null;\nlet flushIntervalMs = 60_000;\nlet onError: ((e: unknown) => void) | null = null;\nlet timer: ReturnType<typeof setInterval> | null = null;\nlet flushing = false;\n/** Safety valve — flush early if a burst fills the buffer between intervals. */\nconst MAX_BUFFER_KEYS = 5_000;\n\nexport interface MeterConfig {\n sink: Sink;\n flushIntervalMs?: number;\n onError?: (e: unknown) => void;\n}\n\n/** Point the meter at a sink. Called by `init()`; pass your own sink to self-host. */\nexport function configureMeter(config: MeterConfig): void {\n sink = config.sink;\n if (config.flushIntervalMs && config.flushIntervalMs > 0) flushIntervalMs = config.flushIntervalMs;\n onError = config.onError ?? null;\n}\n\nconst utcDate = (): string => new Date().toISOString().slice(0, 10);\nconst utcHour = (): string => new Date().toISOString().slice(11, 13);\n\nfunction ensureFlushLoop(): void {\n if (timer) return;\n timer = setInterval(() => void flush(), flushIntervalMs);\n // Don't keep the event loop alive just for metering.\n (timer as { unref?: () => void }).unref?.();\n // Flush the last window on shutdown.\n process.once?.(\"SIGTERM\", () => void flush());\n process.once?.(\"beforeExit\", () => void flush());\n}\n\n/**\n * Count `quantity` of a `resource` against the live tag. THE adapter primitive —\n * a Firestore adapter records \"read\"; a ClickHouse adapter records\n * \"clickhouse.query_ms\"; an OpenAI adapter records \"openai.tokens\". Each resource\n * is bucketed entirely on its own; nothing is ever added across resources. Never\n * throws.\n */\nexport function record(resource: ResourceUnit, quantity: number, hint?: CostHint): void {\n try {\n if (!Number.isFinite(quantity) || quantity <= 0) return;\n const t = currentCostTag();\n const date = utcDate();\n // CASCADE — every unit gets a label, by design (no blind spots): the bucket\n // name wins; else the collection it actually touched (`col:posts`); else\n // \"uncategorized\" as a loud last resort. A unit is never invisible.\n const label = t.label || (hint?.collection ? `col:${hint.collection}` : \"uncategorized\");\n // Key includes the resource, so each resource accumulates in its OWN slot.\n const lk = date + SEP + resource + SEP + label;\n labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + quantity);\n const hk = date + SEP + resource + SEP + utcHour();\n hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + quantity);\n ensureFlushLoop();\n if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS) void flush();\n } catch {\n /* metering is best-effort — never disturb the caller */\n }\n}\n\n/** @deprecated — use {@link record}. Firestore-era alias. */\nexport const recordFirestore = record;\n\n/** Firestore bills a minimum of one read even for an empty result, so 0 counts as 1. */\nexport function recordReads(n: number, hint?: CostHint): void {\n record(\"read\", Math.max(n, 1), hint);\n}\nexport function recordWrites(n = 1): void {\n record(\"write\", n);\n}\nexport function recordDeletes(n = 1): void {\n record(\"delete\", n);\n}\n\nfunction add(target: Record<string, ResourceCounts>, key: string, resource: ResourceUnit, n: number): void {\n const bag = (target[key] ??= {});\n // Accumulate WITHIN this resource only — never merge resources.\n bag[resource] = (bag[resource] ?? 0) + n;\n}\n\n/**\n * Coalesce the buffer into one report per UTC day and hand each to the Sink.\n * Snapshots + clears up front so concurrent records land in the next window.\n * Never throws; a sink failure drops that window (surfaced via `onError`).\n */\nexport async function flush(): Promise<void> {\n if (flushing) return;\n // Not configured (init not called) — drop, don't grow unbounded.\n if (!sink) {\n labelBuffer.clear();\n hourBuffer.clear();\n return;\n }\n if (labelBuffer.size === 0 && hourBuffer.size === 0) return;\n flushing = true;\n\n const labels = new Map(labelBuffer);\n const hours = new Map(hourBuffer);\n labelBuffer.clear();\n hourBuffer.clear();\n\n try {\n const byDate = new Map<string, BucketsReport>();\n const reportFor = (date: string): BucketsReport => {\n let r = byDate.get(date);\n if (!r) {\n r = { date, byLabel: {}, byHour: {} };\n byDate.set(date, r);\n }\n return r;\n };\n for (const [k, n] of labels) {\n const [date, op, label] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byLabel, label, op, n);\n }\n for (const [k, n] of hours) {\n const [date, op, hour] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byHour!, hour, op, n);\n }\n for (const report of byDate.values()) {\n await sink.flush(report);\n }\n } catch (e) {\n // Drop this window rather than risk a partial/double report on retry.\n onError?.(e);\n } finally {\n flushing = false;\n }\n}\n","/**\n * sink — where the meter sends a coalesced rollup, and the wire shape it sends.\n *\n * Abstracting the sink is what makes Buckets storage-agnostic: the meter never\n * knows where counts go. The DEFAULT sink (`ReportSink`) reports up to Crossdeck's\n * ingest endpoint so the numbers surface on your Crossdeck dashboard. A team that\n * wants to self-host can implement `Sink` against anything (Postgres, a file, your\n * own API) without touching the meter.\n */\n\n/**\n * Counts for ONE bucket, keyed by RESOURCE UNIT — the raw quantity of each unit,\n * nothing more (no money; you verify cost on your provider's bill).\n *\n * Firestore, the first adapter, emits `read` / `write` / `delete`. Other adapters\n * emit their own units (`clickhouse.query_ms`, `openai.tokens`, …). The rule that\n * keeps this honest: **each resource is its own line. Counts are only ever summed\n * WITHIN a resource, NEVER across one.** A read is not a query-millisecond; the two\n * never land in the same number. (There is deliberately no \"total units\" field.)\n */\nexport interface ResourceCounts {\n read?: number;\n write?: number;\n delete?: number;\n /** Any other resource unit an adapter emits — kept distinct, never merged. */\n [resource: string]: number | undefined;\n}\n\n/** @deprecated name — kept for back-compat; use {@link ResourceCounts}. */\nexport type OpCounts = ResourceCounts;\n\n/**\n * One coalesced report — the wire contract (see docs/ROLLUP_SCHEMA.md). The meter\n * produces one of these per UTC day in a flush window (usually exactly one).\n */\nexport interface BucketsReport {\n /** UTC day \"YYYY-MM-DD\". */\n date: string;\n /** bucket name → counts. The heart of the report. */\n byLabel: Record<string, OpCounts>;\n /** UTC hour \"HH\" → counts, for the hourly \"did my fix land this hour?\" view. */\n byHour?: Record<string, OpCounts>;\n}\n\n/**\n * A destination for coalesced rollups. `flush` MAY throw on failure — the meter\n * catches it, drops that one window, and never lets it reach your app.\n */\nexport interface Sink {\n flush(report: BucketsReport): Promise<void>;\n}\n\nexport interface ReportSinkConfig {\n /** The project's `cd_sk_` secret key. Server-to-server only. */\n apiKey: string;\n /** Defaults to https://api.cross-deck.com/v1/buckets/report */\n endpoint?: string;\n /** Request timeout (ms); a slow Crossdeck must never stall your flush. */\n timeoutMs?: number;\n}\n\nconst DEFAULT_ENDPOINT = \"https://api.cross-deck.com/v1/buckets/report\";\n\n/**\n * The default sink: POST one coalesced rollup to Crossdeck's ingest endpoint.\n * The ingest folds it into the day's maintained doc with `increment`, so many\n * reports a minute coalesce safely. This path does ZERO database reads — it sends\n * a summary, it does not read. Throws on a non-202 so the meter can log/drop the\n * window; the meter guarantees it never reaches your app.\n */\nexport class ReportSink implements Sink {\n private readonly endpoint: string;\n private readonly apiKey: string;\n private readonly timeoutMs: number;\n\n constructor(config: ReportSinkConfig) {\n this.endpoint = config.endpoint ?? DEFAULT_ENDPOINT;\n this.apiKey = config.apiKey;\n this.timeoutMs = config.timeoutMs ?? 5000;\n }\n\n async flush(report: BucketsReport): Promise<void> {\n const res = await fetch(this.endpoint, {\n method: \"POST\",\n signal: AbortSignal.timeout(this.timeoutMs),\n headers: {\n \"content-type\": \"application/json\",\n authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify(report),\n });\n if (res.status !== 202) {\n throw new Error(`Buckets report rejected: HTTP ${res.status}`);\n }\n }\n}\n","/**\n * adapters/firestore — the universal Firestore read meter (the trap).\n *\n * THE LESSON (learned on a real product): per-call-site `recordReads()`\n * instrumentation MISSES paths. You meter the read sites you're looking at and\n * leave the cron / trigger / ingest path uncounted — often the majority of reads,\n * invisible. Humans tag what they see and miss the path that matters.\n *\n * THE FIX: patch the admin SDK's read methods ONCE. From install onward, EVERY\n * read — anywhere, on any code path — is counted under the ambient tag, with zero\n * per-call-site work and no blind spots.\n *\n * SAFETY CONTRACT — this sits on your production read path, so it is defensive by\n * construction. Each wrapper:\n * 1. calls the REAL method first and captures the result,\n * 2. counts in a try/catch that can never throw into the caller,\n * 3. ALWAYS returns the real result, untouched.\n * It cannot break a read, change a result, or add latency beyond one in-memory\n * counter increment. A wrong count is a measurement error, never a correctness or\n * availability one. Idempotent — calling it twice patches once.\n *\n * COUNTING MODEL — a query returning N docs = N reads (an empty result still bills\n * 1, which the meter enforces). A document get = 1. getAll(...) = the ref count.\n * CollectionReference.get IS Query.get (shared prototype method), so patching Query\n * covers collections with no double-count.\n */\nimport { recordReads, type CostHint } from \"../cost-meter\";\n\nlet installed = false;\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\ntype AnyFn = (...args: any[]) => any;\n\n/**\n * The firebase-admin Firestore classes to patch. Pass the module namespace from\n * `firebase-admin/firestore` — only the prototypes present are patched.\n */\nexport interface FirestoreClasses {\n Query?: { prototype: { get?: AnyFn; onSnapshot?: AnyFn } };\n DocumentReference?: { prototype: { get?: AnyFn; onSnapshot?: AnyFn } };\n Transaction?: { prototype: { get?: AnyFn; getAll?: AnyFn } };\n Firestore?: { prototype: { getAll?: AnyFn } };\n /** count() / sum() / average() — aggregation queries bill reads too. */\n AggregateQuery?: { prototype: { get?: AnyFn } };\n}\n\n/** `projects/{id}/…` → the project id, else undefined. Pure string op. */\nfunction projectFromPath(path: string): string | undefined {\n const parts = path.split(\"/\");\n const i = parts.indexOf(\"projects\");\n return i >= 0 && parts[i + 1] ? parts[i + 1] : undefined;\n}\n\n/**\n * Derive { collection, projectId } from the read target's path so an UNtagged read\n * cascades to `col:<collection>` instead of \"uncategorized\". PURE CPU; never reads,\n * never throws. Falls back to firebase-admin's internal `_queryOptions` for filtered\n * queries (which don't expose `.path`).\n */\nfunction hintFrom(target: any): CostHint | undefined {\n try {\n const p = typeof target?.path === \"string\" ? target.path : \"\";\n if (p) {\n const parts = p.split(\"/\").filter(Boolean);\n const collection = parts.length % 2 === 0 ? parts[parts.length - 2] : parts[parts.length - 1];\n return { collection, projectId: projectFromPath(p) };\n }\n const qo = target?._queryOptions;\n if (qo) {\n const collection = typeof qo.collectionId === \"string\" ? qo.collectionId : undefined;\n const parent =\n typeof qo.parentPath?.relativeName === \"string\"\n ? qo.parentPath.relativeName\n : typeof qo.parentPath?.toString === \"function\"\n ? String(qo.parentPath.toString())\n : \"\";\n return { collection, projectId: parent ? projectFromPath(parent) : undefined };\n }\n } catch {\n /* the meter must never throw */\n }\n return undefined;\n}\n\nfunction meterSnap(snap: unknown, hint?: CostHint): void {\n try {\n const size = (snap as { size?: number } | null)?.size;\n recordReads(typeof size === \"number\" ? size : 1, hint);\n } catch {\n /* best-effort */\n }\n}\nfunction meterCount(n: number, hint?: CostHint): void {\n try {\n recordReads(n, hint);\n } catch {\n /* best-effort */\n }\n}\n\n/**\n * Install the universal read meter on the firebase-admin Firestore classes. Call\n * ONCE at process start, before any reads. Pass the namespace from\n * `firebase-admin/firestore` so the exact prototypes the SDK uses are patched:\n *\n * import * as Firestore from \"firebase-admin/firestore\";\n * installFirestoreMeter(Firestore);\n */\nexport function installFirestoreMeter(classes: FirestoreClasses): void {\n if (installed) return;\n installed = true;\n const { Query, DocumentReference, Transaction, Firestore, AggregateQuery } = classes;\n\n // Query.get — covers Query AND CollectionReference (shared prototype method).\n const qGet = Query?.prototype?.get;\n if (qGet) {\n Query!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await qGet.apply(this, args);\n meterSnap(snap, hintFrom(this));\n return snap;\n };\n }\n\n // DocumentReference.get — a single doc = 1 read.\n const dGet = DocumentReference?.prototype?.get;\n if (dGet) {\n DocumentReference!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await dGet.apply(this, args);\n meterCount(1, hintFrom(this));\n return snap;\n };\n }\n\n // Transaction.get — query or doc; size when present, else 1.\n const tGet = Transaction?.prototype?.get;\n if (tGet) {\n Transaction!.prototype.get = async function (this: unknown, ...args: any[]) {\n const res = await tGet.apply(this, args);\n meterSnap(res, hintFrom(args[0]));\n return res;\n };\n }\n\n // Transaction.getAll(...refs) — one read per ref.\n const tGetAll = Transaction?.prototype?.getAll;\n if (tGetAll) {\n Transaction!.prototype.getAll = async function (this: unknown, ...args: any[]) {\n const res = await tGetAll.apply(this, args);\n meterCount(Array.isArray(res) ? res.length : args.length || 1, hintFrom(args[0]));\n return res;\n };\n }\n\n // Firestore.getAll(...refs) — batched doc reads.\n const fGetAll = Firestore?.prototype?.getAll;\n if (fGetAll) {\n Firestore!.prototype.getAll = async function (this: unknown, ...args: any[]) {\n const res = await fGetAll.apply(this, args);\n meterCount(Array.isArray(res) ? res.length : args.length || 1, hintFrom(args[0]));\n return res;\n };\n }\n\n // AggregateQuery.get — count() / sum() / average(). Firestore bills one read per\n // up to 1000 index entries scanned, and the client never sees that entry count —\n // so this is an HONEST ESTIMATE: for count() we derive it from the result\n // (ceil(count / 1000)), else the billed minimum of 1. Observe-only: it reads the\n // result you already got, adds zero reads. (Closes the aggregation blind spot.)\n const aGet = AggregateQuery?.prototype?.get;\n if (aGet) {\n AggregateQuery!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await aGet.apply(this, args);\n try {\n const data = (snap as { data?: () => { count?: number } } | null)?.data?.();\n const count = typeof data?.count === \"number\" ? data.count : 0;\n meterCount(Math.max(1, Math.ceil(count / 1000)), hintFrom(this));\n } catch {\n /* best-effort */\n }\n return snap;\n };\n }\n\n // Query.onSnapshot / DocumentReference.onSnapshot — server-side realtime\n // listeners. We OBSERVE: wrap onNext and count the docs each fire delivers (a\n // query's changed docs — first fire = all matching; a doc = 1). We attach no\n // listener and issue no read; the meter just sees what the listener is already\n // billed. (Closes the server-listener blind spot.)\n const patchOnSnapshot = (proto: { onSnapshot?: AnyFn } | undefined, perDoc: boolean): void => {\n const real = proto?.onSnapshot;\n if (!real) return;\n proto!.onSnapshot = function (this: unknown, ...args: any[]) {\n const hint = hintFrom(this);\n const i = args.findIndex((a) => typeof a === \"function\");\n if (i >= 0) {\n const onNext = args[i];\n args[i] = function (snap: any) {\n try {\n const n = perDoc\n ? 1\n : typeof snap?.docChanges === \"function\"\n ? snap.docChanges().length\n : typeof snap?.size === \"number\"\n ? snap.size\n : 1;\n if (n > 0) meterCount(n, hint);\n } catch {\n /* best-effort */\n }\n return onNext(snap);\n };\n }\n return real.apply(this, args);\n };\n };\n patchOnSnapshot(Query?.prototype, false);\n patchOnSnapshot(DocumentReference?.prototype, true);\n}\n\n/** Test-only: reset the install guard so a suite can re-patch fresh prototypes. */\nexport function __resetFirestoreMeterForTests(): void {\n installed = false;\n}\n","/**\n * @cross-deck/buckets — know exactly what every database read costs you, and who\n * caused it. A tiny, never-throws collector for Firestore.\n *\n * The whole footprint a consumer sees:\n * 1. init({ apiKey, firestore }) — configure once, install the trap once\n * 2. bucket(name, fn) — name the read paths that matter\n * 3. (the dashboard shows the rest — and names the ones you haven't yet)\n */\nimport { configureMeter, type MeterConfig } from \"./cost-meter\";\nimport { ReportSink, type Sink } from \"./sink\";\nimport { installFirestoreMeter, type FirestoreClasses } from \"./adapters/firestore\";\n\nexport interface InitOptions {\n /** The project's `cd_sk_` SECRET key. Server-to-server only — never a browser key. */\n apiKey: string;\n /**\n * Pass the namespace from `firebase-admin/firestore` to auto-install the read\n * trap (recommended — this is what makes every read count with no per-call work).\n * Omit it if you'd rather call `installFirestoreMeter()` yourself, or you only\n * use the manual `recordReads()` recorders.\n */\n firestore?: FirestoreClasses;\n /** Override the report endpoint (defaults to Crossdeck's ingest). */\n endpoint?: string;\n /** How often to flush coalesced counts (ms). Default 60_000. */\n flushIntervalMs?: number;\n /** Bring your own sink (self-host the rollups). Defaults to reporting up to Crossdeck. */\n sink?: Sink;\n /** Notified when a flush fails, so a dropped window is never silent. Best-effort. */\n onError?: MeterConfig[\"onError\"];\n}\n\n/**\n * Configure Buckets once, at process start. Points the meter at a sink (Crossdeck's\n * ingest by default) and — if you pass `firestore` — installs the universal read\n * trap so every read counts automatically.\n */\nexport function init(options: InitOptions): void {\n const sink = options.sink ?? new ReportSink({ apiKey: options.apiKey, endpoint: options.endpoint });\n configureMeter({ sink, flushIntervalMs: options.flushIntervalMs, onError: options.onError });\n if (options.firestore) installFirestoreMeter(options.firestore);\n}\n\n/** Alias — reads well next to `bucket()` at a call site. */\nexport { init as initBuckets };\n\n// The headline verb + the lower-level tag controls it is sugar over.\nexport {\n bucket,\n runWithCostTag,\n enterCostTag,\n refineCostTag,\n currentCostTag,\n type CostTag,\n} from \"./cost-context\";\n\n// Recorders. `record(resource, quantity)` is the generic adapter primitive — count\n// any resource unit (a future adapter records \"clickhouse.query_ms\"); recordReads/\n// Writes/Deletes are the Firestore conveniences over it.\nexport {\n record,\n recordReads,\n recordWrites,\n recordDeletes,\n flush,\n type CostHint,\n type ResourceUnit,\n type OpType,\n type MeterConfig,\n} from \"./cost-meter\";\n\n// The trap (the only datastore adapter today) + its class shape.\nexport { installFirestoreMeter, type FirestoreClasses } from \"./adapters/firestore\";\n\n// The sink seam — for self-hosting rollups instead of reporting to Crossdeck.\nexport {\n ReportSink,\n type Sink,\n type BucketsReport,\n type ResourceCounts,\n type OpCounts,\n type ReportSinkConfig,\n} from \"./sink\";\n"]}
package/dist/index.mjs CHANGED
@@ -44,33 +44,33 @@ function ensureFlushLoop() {
44
44
  process.once?.("SIGTERM", () => void flush());
45
45
  process.once?.("beforeExit", () => void flush());
46
46
  }
47
- function recordFirestore(op, n, hint) {
47
+ function record(resource, quantity, hint) {
48
48
  try {
49
- if (!Number.isFinite(n) || n <= 0) return;
49
+ if (!Number.isFinite(quantity) || quantity <= 0) return;
50
50
  const t = currentCostTag();
51
51
  const date = utcDate();
52
52
  const label = t.label || (hint?.collection ? `col:${hint.collection}` : "uncategorized");
53
- const lk = date + SEP + op + SEP + label;
54
- labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + n);
55
- const hk = date + SEP + op + SEP + utcHour();
56
- hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + n);
53
+ const lk = date + SEP + resource + SEP + label;
54
+ labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + quantity);
55
+ const hk = date + SEP + resource + SEP + utcHour();
56
+ hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + quantity);
57
57
  ensureFlushLoop();
58
58
  if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS) void flush();
59
59
  } catch {
60
60
  }
61
61
  }
62
62
  function recordReads(n, hint) {
63
- recordFirestore("read", Math.max(n, 1), hint);
63
+ record("read", Math.max(n, 1), hint);
64
64
  }
65
65
  function recordWrites(n = 1) {
66
- recordFirestore("write", n);
66
+ record("write", n);
67
67
  }
68
68
  function recordDeletes(n = 1) {
69
- recordFirestore("delete", n);
69
+ record("delete", n);
70
70
  }
71
- function add(target, key, op, n) {
71
+ function add(target, key, resource, n) {
72
72
  const bag = target[key] ??= {};
73
- bag[op] = (bag[op] ?? 0) + n;
73
+ bag[resource] = (bag[resource] ?? 0) + n;
74
74
  }
75
75
  async function flush() {
76
76
  if (flushing) return;
@@ -266,6 +266,6 @@ function init(options) {
266
266
  if (options.firestore) installFirestoreMeter(options.firestore);
267
267
  }
268
268
 
269
- export { ReportSink, bucket, currentCostTag, enterCostTag, flush, init, init as initBuckets, installFirestoreMeter, recordDeletes, recordReads, recordWrites, refineCostTag, runWithCostTag };
269
+ export { ReportSink, bucket, currentCostTag, enterCostTag, flush, init, init as initBuckets, installFirestoreMeter, record, recordDeletes, recordReads, recordWrites, refineCostTag, runWithCostTag };
270
270
  //# sourceMappingURL=index.mjs.map
271
271
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/cost-context.ts","../src/cost-meter.ts","../src/sink.ts","../src/adapters/firestore.ts","../src/index.ts"],"names":["sink"],"mappings":";;;AAqBA,IAAM,cAAuB,EAAC;AAC9B,IAAM,KAAA,GAAQ,IAAI,iBAAA,EAA2B;AAGtC,SAAS,cAAA,CAAkB,KAAc,EAAA,EAAgB;AAC9D,EAAA,OAAO,MAAM,GAAA,CAAI,EAAE,GAAG,GAAA,IAAO,EAAE,CAAA;AACjC;AAGO,SAAS,aAAa,GAAA,EAAoB;AAC/C,EAAA,KAAA,CAAM,SAAA,CAAU,EAAE,GAAG,GAAA,EAAK,CAAA;AAC5B;AAGO,SAAS,c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M,IAAA,EAAM,IAAI,CAAA;AAAA,IAC9B,CAAA;AAAA,EACF,CAAA;AACA,EAAA,eAAA,CAAgB,KAAA,EAAO,WAAW,KAAK,CAAA;AACvC,EAAA,eAAA,CAAgB,iBAAA,EAAmB,WAAW,IAAI,CAAA;AACpD;;;ACnLO,SAAS,KAAK,OAAA,EAA4B;AAC/C,EAAA,MAAMA,KAAAA,GAAO,OAAA,CAAQ,IAAA,IAAQ,IAAI,UAAA,CAAW,EAAE,MAAA,EAAQ,OAAA,CAAQ,MAAA,EAAQ,QAAA,EAAU,OAAA,CAAQ,QAAA,EAAU,CAAA;AAClG,EAAA,cAAA,CAAe,EAAE,MAAAA,KAAAA,EAAM,eAAA,EAAiB,QAAQ,eAAA,EAAiB,OAAA,EAAS,OAAA,CAAQ,OAAA,EAAS,CAAA;AAC3F,EAAA,IAAI,OAAA,CAAQ,SAAA,EAAW,qBAAA,CAAsB,OAAA,CAAQ,SAAS,CAAA;AAChE","file":"index.mjs","sourcesContent":["/**\n * cost-context — the request-scoped tag every counted operation attributes\n * itself to. Set it ONCE at a boundary (or wrap a path with `bucket()`); it\n * rides Node's AsyncLocalStorage down through every async fan-out, so one\n * handler that triggers 15 reads attributes all 15 to the same bucket — with\n * zero per-call-site work.\n *\n * Generic by design: unlike a hardcoded product taxonomy, the only meaningful\n * field a consumer sets is the free-form `label` (the bucket name). `feature`\n * is an optional coarse grouping if you want one; nothing here is\n * Crossdeck-specific.\n */\nimport { AsyncLocalStorage } from \"node:async_hooks\";\n\nexport interface CostTag {\n /** Optional coarse grouping (a caller-defined surface name). */\n feature?: string;\n /** The bucket name — what `bucket()` sets. Drives the report's `byLabel`. */\n label?: string;\n}\n\nconst DEFAULT_TAG: CostTag = {};\nconst store = new AsyncLocalStorage<CostTag>();\n\n/** Run `fn` with `tag` bound for its entire async subtree. */\nexport function runWithCostTag<T>(tag: CostTag, fn: () => T): T {\n return store.run({ ...tag }, fn);\n}\n\n/** Bind a tag for the remainder of the current async context (no closure to wrap). */\nexport function enterCostTag(tag: CostTag): void {\n store.enterWith({ ...tag });\n}\n\n/** Refine the live tag in place (e.g. stamp a feature after the boundary). */\nexport function refineCostTag(patch: Partial<CostTag>): void {\n const cur = store.getStore();\n if (cur) Object.assign(cur, patch);\n}\n\n/** The current tag, or a safe empty default outside any bound context. */\nexport function currentCostTag(): CostTag {\n return store.getStore() ?? DEFAULT_TAG;\n}\n\n/**\n * `bucket(name, fn)` — the headline verb, the `track()` of cost. Run `fn` with\n * every operation inside it attributed to the bucket `name`; the attribution\n * rides the async subtree automatically. The one verb most developers ever touch:\n *\n * await bucket(\"nightly-export\", async () => {\n * const rows = await db.collection(\"events\").where(...).get(); // → \"nightly-export\"\n * });\n */\nexport function bucket<T>(name: string, fn: () => T): T {\n return runWithCostTag({ ...currentCostTag(), label: name }, fn);\n}\n","/**\n * cost-meter — counts operations against the ambient tag and flushes them to the\n * configured Sink cheaply.\n *\n * LOW-OVERHEAD CONTRACT (the thing that warns you about reads must not run them\n * up): counts accumulate in an in-memory buffer and flush periodically — NEVER one\n * network call per counted operation. A flush coalesces the whole window into one\n * report per UTC day and hands it to the Sink. At steady state that is ~1 small\n * request a minute, regardless of how many ops you served.\n *\n * BEST-EFFORT CONTRACT: metering must never throw into your code. Every recorder\n * swallows its own errors; a failed flush drops that window's counts (surfaced via\n * `onError` if you pass one) rather than disturbing the app.\n */\nimport { currentCostTag } from \"./cost-context\";\nimport type { Sink, BucketsReport, OpCounts } from \"./sink\";\n\nexport type OpType = \"read\" | \"write\" | \"delete\";\n\n/** Optional read-site hint — the collection touched, derived at the trap from the\n * path. Lets an UNtagged read cascade to `col:<collection>` instead of vanishing. */\nexport interface CostHint {\n collection?: string;\n projectId?: string;\n}\n\n// NUL separator — a bucket/collection name can contain almost anything except\n// this, so the key never collides with a name that has a \"|\" or \":\" in it.\nconst SEP = \"\\u001f\"; // ASCII Unit Separator\n\n/** key = date <NUL> op <NUL> label → count */\nconst labelBuffer = new Map<string, number>();\n/** key = date <NUL> op <NUL> hour → count */\nconst hourBuffer = new Map<string, number>();\n\nlet sink: Sink | null = null;\nlet flushIntervalMs = 60_000;\nlet onError: ((e: unknown) => void) | null = null;\nlet timer: ReturnType<typeof setInterval> | null = null;\nlet flushing = false;\n/** Safety valve — flush early if a burst fills the buffer between intervals. */\nconst MAX_BUFFER_KEYS = 5_000;\n\nexport interface MeterConfig {\n sink: Sink;\n flushIntervalMs?: number;\n onError?: (e: unknown) => void;\n}\n\n/** Point the meter at a sink. Called by `init()`; pass your own sink to self-host. */\nexport function configureMeter(config: MeterConfig): void {\n sink = config.sink;\n if (config.flushIntervalMs && config.flushIntervalMs > 0) flushIntervalMs = config.flushIntervalMs;\n onError = config.onError ?? null;\n}\n\nconst utcDate = (): string => new Date().toISOString().slice(0, 10);\nconst utcHour = (): string => new Date().toISOString().slice(11, 13);\n\nfunction ensureFlushLoop(): void {\n if (timer) return;\n timer = setInterval(() => void flush(), flushIntervalMs);\n // Don't keep the event loop alive just for metering.\n (timer as { unref?: () => void }).unref?.();\n // Flush the last window on shutdown.\n process.once?.(\"SIGTERM\", () => void flush());\n process.once?.(\"beforeExit\", () => void flush());\n}\n\n/** Count `n` ops of `op` against the live tag. Never throws. */\nexport function recordFirestore(op: OpType, n: number, hint?: CostHint): void {\n try {\n if (!Number.isFinite(n) || n <= 0) return;\n const t = currentCostTag();\n const date = utcDate();\n // CASCADE — every op gets a label, by design (no blind spots): the bucket name\n // wins; else the collection it actually touched (`col:posts`); else\n // \"uncategorized\" as a loud last resort. A read is never invisible.\n const label = t.label || (hint?.collection ? `col:${hint.collection}` : \"uncategorized\");\n const lk = date + SEP + op + SEP + label;\n labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + n);\n const hk = date + SEP + op + SEP + utcHour();\n hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + n);\n ensureFlushLoop();\n if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS) void flush();\n } catch {\n /* metering is best-effort — never disturb the caller */\n }\n}\n\n/** Firestore bills a minimum of one read even for an empty result, so 0 counts as 1. */\nexport function recordReads(n: number, hint?: CostHint): void {\n recordFirestore(\"read\", Math.max(n, 1), hint);\n}\nexport function recordWrites(n = 1): void {\n recordFirestore(\"write\", n);\n}\nexport function recordDeletes(n = 1): void {\n recordFirestore(\"delete\", n);\n}\n\nfunction add(target: Record<string, OpCounts>, key: string, op: OpType, n: number): void {\n const bag = (target[key] ??= {});\n bag[op] = (bag[op] ?? 0) + n;\n}\n\n/**\n * Coalesce the buffer into one report per UTC day and hand each to the Sink.\n * Snapshots + clears up front so concurrent records land in the next window.\n * Never throws; a sink failure drops that window (surfaced via `onError`).\n */\nexport async function flush(): Promise<void> {\n if (flushing) return;\n // Not configured (init not called) — drop, don't grow unbounded.\n if (!sink) {\n labelBuffer.clear();\n hourBuffer.clear();\n return;\n }\n if (labelBuffer.size === 0 && hourBuffer.size === 0) return;\n flushing = true;\n\n const labels = new Map(labelBuffer);\n const hours = new Map(hourBuffer);\n labelBuffer.clear();\n hourBuffer.clear();\n\n try {\n const byDate = new Map<string, BucketsReport>();\n const reportFor = (date: string): BucketsReport => {\n let r = byDate.get(date);\n if (!r) {\n r = { date, byLabel: {}, byHour: {} };\n byDate.set(date, r);\n }\n return r;\n };\n for (const [k, n] of labels) {\n const [date, op, label] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byLabel, label, op, n);\n }\n for (const [k, n] of hours) {\n const [date, op, hour] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byHour!, hour, op, n);\n }\n for (const report of byDate.values()) {\n await sink.flush(report);\n }\n } catch (e) {\n // Drop this window rather than risk a partial/double report on retry.\n onError?.(e);\n } finally {\n flushing = false;\n }\n}\n","/**\n * sink — where the meter sends a coalesced rollup, and the wire shape it sends.\n *\n * Abstracting the sink is what makes Buckets storage-agnostic: the meter never\n * knows where counts go. The DEFAULT sink (`ReportSink`) reports up to Crossdeck's\n * ingest endpoint so the numbers surface on your Crossdeck dashboard. A team that\n * wants to self-host can implement `Sink` against anything (Postgres, a file, your\n * own API) without touching the meter.\n */\n\nexport interface OpCounts {\n read?: number;\n write?: number;\n delete?: number;\n}\n\n/**\n * One coalesced report — the wire contract (see docs/ROLLUP_SCHEMA.md). The meter\n * produces one of these per UTC day in a flush window (usually exactly one).\n */\nexport interface BucketsReport {\n /** UTC day \"YYYY-MM-DD\". */\n date: string;\n /** bucket name → counts. The heart of the report. */\n byLabel: Record<string, OpCounts>;\n /** UTC hour \"HH\" → counts, for the hourly \"did my fix land this hour?\" view. */\n byHour?: Record<string, OpCounts>;\n}\n\n/**\n * A destination for coalesced rollups. `flush` MAY throw on failure — the meter\n * catches it, drops that one window, and never lets it reach your app.\n */\nexport interface Sink {\n flush(report: BucketsReport): Promise<void>;\n}\n\nexport interface ReportSinkConfig {\n /** The project's `cd_sk_` secret key. Server-to-server only. */\n apiKey: string;\n /** Defaults to https://api.cross-deck.com/v1/buckets/report */\n endpoint?: string;\n /** Request timeout (ms); a slow Crossdeck must never stall your flush. */\n timeoutMs?: number;\n}\n\nconst DEFAULT_ENDPOINT = \"https://api.cross-deck.com/v1/buckets/report\";\n\n/**\n * The default sink: POST one coalesced rollup to Crossdeck's ingest endpoint.\n * The ingest folds it into the day's maintained doc with `increment`, so many\n * reports a minute coalesce safely. This path does ZERO database reads — it sends\n * a summary, it does not read. Throws on a non-202 so the meter can log/drop the\n * window; the meter guarantees it never reaches your app.\n */\nexport class ReportSink implements Sink {\n private readonly endpoint: string;\n private readonly apiKey: string;\n private readonly timeoutMs: number;\n\n constructor(config: ReportSinkConfig) {\n this.endpoint = config.endpoint ?? DEFAULT_ENDPOINT;\n this.apiKey = config.apiKey;\n this.timeoutMs = config.timeoutMs ?? 5000;\n }\n\n async flush(report: BucketsReport): Promise<void> {\n const res = await fetch(this.endpoint, {\n method: \"POST\",\n signal: AbortSignal.timeout(this.timeoutMs),\n headers: {\n \"content-type\": \"application/json\",\n authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify(report),\n });\n if (res.status !== 202) {\n throw new Error(`Buckets report rejected: HTTP ${res.status}`);\n }\n }\n}\n","/**\n * adapters/firestore — the universal Firestore read meter (the trap).\n *\n * THE LESSON (learned on a real product): per-call-site `recordReads()`\n * instrumentation MISSES paths. You meter the read sites you're looking at and\n * leave the cron / trigger / ingest path uncounted — often the majority of reads,\n * invisible. Humans tag what they see and miss the path that matters.\n *\n * THE FIX: patch the admin SDK's read methods ONCE. From install onward, EVERY\n * read — anywhere, on any code path — is counted under the ambient tag, with zero\n * per-call-site work and no blind spots.\n *\n * SAFETY CONTRACT — this sits on your production read path, so it is defensive by\n * construction. Each wrapper:\n * 1. calls the REAL method first and captures the result,\n * 2. counts in a try/catch that can never throw into the caller,\n * 3. ALWAYS returns the real result, untouched.\n * It cannot break a read, change a result, or add latency beyond one in-memory\n * counter increment. A wrong count is a measurement error, never a correctness or\n * availability one. Idempotent — calling it twice patches once.\n *\n * COUNTING MODEL — a query returning N docs = N reads (an empty result still bills\n * 1, which the meter enforces). A document get = 1. getAll(...) = the ref count.\n * CollectionReference.get IS Query.get (shared prototype method), so patching Query\n * covers collections with no double-count.\n */\nimport { recordReads, type CostHint } from \"../cost-meter\";\n\nlet installed = false;\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\ntype AnyFn = (...args: any[]) => any;\n\n/**\n * The firebase-admin Firestore classes to patch. Pass the module namespace from\n * `firebase-admin/firestore` — only the prototypes present are patched.\n */\nexport interface FirestoreClasses {\n Query?: { prototype: { get?: AnyFn; onSnapshot?: AnyFn } };\n DocumentReference?: { prototype: { get?: AnyFn; onSnapshot?: AnyFn } };\n Transaction?: { prototype: { get?: AnyFn; getAll?: AnyFn } };\n Firestore?: { prototype: { getAll?: AnyFn } };\n /** count() / sum() / average() — aggregation queries bill reads too. */\n AggregateQuery?: { prototype: { get?: AnyFn } };\n}\n\n/** `projects/{id}/…` → the project id, else undefined. Pure string op. */\nfunction projectFromPath(path: string): string | undefined {\n const parts = path.split(\"/\");\n const i = parts.indexOf(\"projects\");\n return i >= 0 && parts[i + 1] ? parts[i + 1] : undefined;\n}\n\n/**\n * Derive { collection, projectId } from the read target's path so an UNtagged read\n * cascades to `col:<collection>` instead of \"uncategorized\". PURE CPU; never reads,\n * never throws. Falls back to firebase-admin's internal `_queryOptions` for filtered\n * queries (which don't expose `.path`).\n */\nfunction hintFrom(target: any): CostHint | undefined {\n try {\n const p = typeof target?.path === \"string\" ? target.path : \"\";\n if (p) {\n const parts = p.split(\"/\").filter(Boolean);\n const collection = parts.length % 2 === 0 ? parts[parts.length - 2] : parts[parts.length - 1];\n return { collection, projectId: projectFromPath(p) };\n }\n const qo = target?._queryOptions;\n if (qo) {\n const collection = typeof qo.collectionId === \"string\" ? qo.collectionId : undefined;\n const parent =\n typeof qo.parentPath?.relativeName === \"string\"\n ? qo.parentPath.relativeName\n : typeof qo.parentPath?.toString === \"function\"\n ? String(qo.parentPath.toString())\n : \"\";\n return { collection, projectId: parent ? projectFromPath(parent) : undefined };\n }\n } catch {\n /* the meter must never throw */\n }\n return undefined;\n}\n\nfunction meterSnap(snap: unknown, hint?: CostHint): void {\n try {\n const size = (snap as { size?: number } | null)?.size;\n recordReads(typeof size === \"number\" ? size : 1, hint);\n } catch {\n /* best-effort */\n }\n}\nfunction meterCount(n: number, hint?: CostHint): void {\n try {\n recordReads(n, hint);\n } catch {\n /* best-effort */\n }\n}\n\n/**\n * Install the universal read meter on the firebase-admin Firestore classes. Call\n * ONCE at process start, before any reads. Pass the namespace from\n * `firebase-admin/firestore` so the exact prototypes the SDK uses are patched:\n *\n * import * as Firestore from \"firebase-admin/firestore\";\n * installFirestoreMeter(Firestore);\n */\nexport function installFirestoreMeter(classes: FirestoreClasses): void {\n if (installed) return;\n installed = true;\n const { Query, DocumentReference, Transaction, Firestore, AggregateQuery } = classes;\n\n // Query.get — covers Query AND CollectionReference (shared prototype method).\n const qGet = Query?.prototype?.get;\n if (qGet) {\n Query!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await qGet.apply(this, args);\n meterSnap(snap, hintFrom(this));\n return snap;\n };\n }\n\n // DocumentReference.get — a single doc = 1 read.\n const dGet = DocumentReference?.prototype?.get;\n if (dGet) {\n DocumentReference!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await dGet.apply(this, args);\n meterCount(1, hintFrom(this));\n return snap;\n };\n }\n\n // Transaction.get — query or doc; size when present, else 1.\n const tGet = Transaction?.prototype?.get;\n if (tGet) {\n Transaction!.prototype.get = async function (this: unknown, ...args: any[]) {\n const res = await tGet.apply(this, args);\n meterSnap(res, hintFrom(args[0]));\n return res;\n };\n }\n\n // Transaction.getAll(...refs) — one read per ref.\n const tGetAll = Transaction?.prototype?.getAll;\n if (tGetAll) {\n Transaction!.prototype.getAll = async function (this: unknown, ...args: any[]) {\n const res = await tGetAll.apply(this, args);\n meterCount(Array.isArray(res) ? res.length : args.length || 1, hintFrom(args[0]));\n return res;\n };\n }\n\n // Firestore.getAll(...refs) — batched doc reads.\n const fGetAll = Firestore?.prototype?.getAll;\n if (fGetAll) {\n Firestore!.prototype.getAll = async function (this: unknown, ...args: any[]) {\n const res = await fGetAll.apply(this, args);\n meterCount(Array.isArray(res) ? res.length : args.length || 1, hintFrom(args[0]));\n return res;\n };\n }\n\n // AggregateQuery.get — count() / sum() / average(). Firestore bills one read per\n // up to 1000 index entries scanned, and the client never sees that entry count —\n // so this is an HONEST ESTIMATE: for count() we derive it from the result\n // (ceil(count / 1000)), else the billed minimum of 1. Observe-only: it reads the\n // result you already got, adds zero reads. (Closes the aggregation blind spot.)\n const aGet = AggregateQuery?.prototype?.get;\n if (aGet) {\n AggregateQuery!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await aGet.apply(this, args);\n try {\n const data = (snap as { data?: () => { count?: number } } | null)?.data?.();\n const count = typeof data?.count === \"number\" ? data.count : 0;\n meterCount(Math.max(1, Math.ceil(count / 1000)), hintFrom(this));\n } catch {\n /* best-effort */\n }\n return snap;\n };\n }\n\n // Query.onSnapshot / DocumentReference.onSnapshot — server-side realtime\n // listeners. We OBSERVE: wrap onNext and count the docs each fire delivers (a\n // query's changed docs — first fire = all matching; a doc = 1). We attach no\n // listener and issue no read; the meter just sees what the listener is already\n // billed. (Closes the server-listener blind spot.)\n const patchOnSnapshot = (proto: { onSnapshot?: AnyFn } | undefined, perDoc: boolean): void => {\n const real = proto?.onSnapshot;\n if (!real) return;\n proto!.onSnapshot = function (this: unknown, ...args: any[]) {\n const hint = hintFrom(this);\n const i = args.findIndex((a) => typeof a === \"function\");\n if (i >= 0) {\n const onNext = args[i];\n args[i] = function (snap: any) {\n try {\n const n = perDoc\n ? 1\n : typeof snap?.docChanges === \"function\"\n ? snap.docChanges().length\n : typeof snap?.size === \"number\"\n ? snap.size\n : 1;\n if (n > 0) meterCount(n, hint);\n } catch {\n /* best-effort */\n }\n return onNext(snap);\n };\n }\n return real.apply(this, args);\n };\n };\n patchOnSnapshot(Query?.prototype, false);\n patchOnSnapshot(DocumentReference?.prototype, true);\n}\n\n/** Test-only: reset the install guard so a suite can re-patch fresh prototypes. */\nexport function __resetFirestoreMeterForTests(): void {\n installed = false;\n}\n","/**\n * @cross-deck/buckets — know exactly what every database read costs you, and who\n * caused it. A tiny, never-throws collector for Firestore.\n *\n * The whole footprint a consumer sees:\n * 1. init({ apiKey, firestore }) — configure once, install the trap once\n * 2. bucket(name, fn) — name the read paths that matter\n * 3. (the dashboard shows the rest — and names the ones you haven't yet)\n */\nimport { configureMeter, type MeterConfig } from \"./cost-meter\";\nimport { ReportSink, type Sink } from \"./sink\";\nimport { installFirestoreMeter, type FirestoreClasses } from \"./adapters/firestore\";\n\nexport interface InitOptions {\n /** The project's `cd_sk_` SECRET key. Server-to-server only — never a browser key. */\n apiKey: string;\n /**\n * Pass the namespace from `firebase-admin/firestore` to auto-install the read\n * trap (recommended — this is what makes every read count with no per-call work).\n * Omit it if you'd rather call `installFirestoreMeter()` yourself, or you only\n * use the manual `recordReads()` recorders.\n */\n firestore?: FirestoreClasses;\n /** Override the report endpoint (defaults to Crossdeck's ingest). */\n endpoint?: string;\n /** How often to flush coalesced counts (ms). Default 60_000. */\n flushIntervalMs?: number;\n /** Bring your own sink (self-host the rollups). Defaults to reporting up to Crossdeck. */\n sink?: Sink;\n /** Notified when a flush fails, so a dropped window is never silent. Best-effort. */\n onError?: MeterConfig[\"onError\"];\n}\n\n/**\n * Configure Buckets once, at process start. Points the meter at a sink (Crossdeck's\n * ingest by default) and — if you pass `firestore` — installs the universal read\n * trap so every read counts automatically.\n */\nexport function init(options: InitOptions): void {\n const sink = options.sink ?? new ReportSink({ apiKey: options.apiKey, endpoint: options.endpoint });\n configureMeter({ sink, flushIntervalMs: options.flushIntervalMs, onError: options.onError });\n if (options.firestore) installFirestoreMeter(options.firestore);\n}\n\n/** Alias — reads well next to `bucket()` at a call site. */\nexport { init as initBuckets };\n\n// The headline verb + the lower-level tag controls it is sugar over.\nexport {\n bucket,\n runWithCostTag,\n enterCostTag,\n refineCostTag,\n currentCostTag,\n type CostTag,\n} from \"./cost-context\";\n\n// Manual recorders (for non-Firestore ops, or when you don't install the trap).\nexport {\n recordReads,\n recordWrites,\n recordDeletes,\n flush,\n type CostHint,\n type OpType,\n type MeterConfig,\n} from \"./cost-meter\";\n\n// The trap (the only datastore adapter today) + its class shape.\nexport { installFirestoreMeter, type FirestoreClasses } from \"./adapters/firestore\";\n\n// The sink seam — for self-hosting rollups instead of reporting to Crossdeck.\nexport { ReportSink, type Sink, type BucketsReport, type OpCounts, type ReportSinkConfig } from \"./sink\";\n"]}
1
+ {"version":3,"sources":["../src/cost-context.ts","../src/cost-meter.ts","../src/sink.ts","../src/adapters/firestore.ts","../src/index.ts"],"names":["sink"],"mappings":";;;AAqBA,IAAM,cAAuB,EAAC;AAC9B,IAAM,KAAA,GAAQ,IAAI,iBAAA,EAA2B;AAGtC,SAAS,cAAA,CAAkB,KAAc,EAAA,EAAgB;AAC9D,EAAA,OAAO,MAAM,GAAA,CAAI,EAAE,GAAG,GAAA,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AC/B,CAAA,CAAA,MAAQ;AAAA,UAER;AACA,UAAA,OAAO,OAAO,IAAI,CAAA;AAAA,QACpB,CAAA;AAAA,MACF;AACA,MAAA,OAAO,IAAA,CAAK,KAAA,CAAM,IAAA,EAAM,IAAI,CAAA;AAAA,IAC9B,CAAA;AAAA,EACF,CAAA;AACA,EAAA,eAAA,CAAgB,KAAA,EAAO,WAAW,KAAK,CAAA;AACvC,EAAA,eAAA,CAAgB,iBAAA,EAAmB,WAAW,IAAI,CAAA;AACpD;;;ACnLO,SAAS,KAAK,OAAA,EAA4B;AAC/C,EAAA,MAAMA,KAAAA,GAAO,OAAA,CAAQ,IAAA,IAAQ,IAAI,UAAA,CAAW,EAAE,MAAA,EAAQ,OAAA,CAAQ,MAAA,EAAQ,QAAA,EAAU,OAAA,CAAQ,QAAA,EAAU,CAAA;AAClG,EAAA,cAAA,CAAe,EAAE,MAAAA,KAAAA,EAAM,eAAA,EAAiB,QAAQ,eAAA,EAAiB,OAAA,EAAS,OAAA,CAAQ,OAAA,EAAS,CAAA;AAC3F,EAAA,IAAI,OAAA,CAAQ,SAAA,EAAW,qBAAA,CAAsB,OAAA,CAAQ,SAAS,CAAA;AAChE","file":"index.mjs","sourcesContent":["/**\n * cost-context — the request-scoped tag every counted operation attributes\n * itself to. Set it ONCE at a boundary (or wrap a path with `bucket()`); it\n * rides Node's AsyncLocalStorage down through every async fan-out, so one\n * handler that triggers 15 reads attributes all 15 to the same bucket — with\n * zero per-call-site work.\n *\n * Generic by design: unlike a hardcoded product taxonomy, the only meaningful\n * field a consumer sets is the free-form `label` (the bucket name). `feature`\n * is an optional coarse grouping if you want one; nothing here is\n * Crossdeck-specific.\n */\nimport { AsyncLocalStorage } from \"node:async_hooks\";\n\nexport interface CostTag {\n /** Optional coarse grouping (a caller-defined surface name). */\n feature?: string;\n /** The bucket name — what `bucket()` sets. Drives the report's `byLabel`. */\n label?: string;\n}\n\nconst DEFAULT_TAG: CostTag = {};\nconst store = new AsyncLocalStorage<CostTag>();\n\n/** Run `fn` with `tag` bound for its entire async subtree. */\nexport function runWithCostTag<T>(tag: CostTag, fn: () => T): T {\n return store.run({ ...tag }, fn);\n}\n\n/** Bind a tag for the remainder of the current async context (no closure to wrap). */\nexport function enterCostTag(tag: CostTag): void {\n store.enterWith({ ...tag });\n}\n\n/** Refine the live tag in place (e.g. stamp a feature after the boundary). */\nexport function refineCostTag(patch: Partial<CostTag>): void {\n const cur = store.getStore();\n if (cur) Object.assign(cur, patch);\n}\n\n/** The current tag, or a safe empty default outside any bound context. */\nexport function currentCostTag(): CostTag {\n return store.getStore() ?? DEFAULT_TAG;\n}\n\n/**\n * `bucket(name, fn)` — the headline verb, the `track()` of cost. Run `fn` with\n * every operation inside it attributed to the bucket `name`; the attribution\n * rides the async subtree automatically. The one verb most developers ever touch:\n *\n * await bucket(\"nightly-export\", async () => {\n * const rows = await db.collection(\"events\").where(...).get(); // → \"nightly-export\"\n * });\n */\nexport function bucket<T>(name: string, fn: () => T): T {\n return runWithCostTag({ ...currentCostTag(), label: name }, fn);\n}\n","/**\n * cost-meter — counts operations against the ambient tag and flushes them to the\n * configured Sink cheaply.\n *\n * LOW-OVERHEAD CONTRACT (the thing that warns you about reads must not run them\n * up): counts accumulate in an in-memory buffer and flush periodically — NEVER one\n * network call per counted operation. A flush coalesces the whole window into one\n * report per UTC day and hands it to the Sink. At steady state that is ~1 small\n * request a minute, regardless of how many ops you served.\n *\n * BEST-EFFORT CONTRACT: metering must never throw into your code. Every recorder\n * swallows its own errors; a failed flush drops that window's counts (surfaced via\n * `onError` if you pass one) rather than disturbing the app.\n */\nimport { currentCostTag } from \"./cost-context\";\nimport type { Sink, BucketsReport, ResourceCounts } from \"./sink\";\n\n/**\n * A resource unit — what an adapter counts. Firestore emits `read`/`write`/\n * `delete`; other adapters emit their own (`clickhouse.query_ms`, `openai.tokens`).\n * It is a free identifier on purpose, BUT each one is kept entirely distinct: the\n * meter only ever sums quantities WITHIN a single resource, never across two.\n */\nexport type ResourceUnit = string;\n/** @deprecated — Firestore-era name; use {@link ResourceUnit}. */\nexport type OpType = ResourceUnit;\n\n/** Optional read-site hint — the collection touched, derived at the trap from the\n * path. Lets an UNtagged read cascade to `col:<collection>` instead of vanishing. */\nexport interface CostHint {\n collection?: string;\n projectId?: string;\n}\n\n// NUL separator — a bucket/collection name can contain almost anything except\n// this, so the key never collides with a name that has a \"|\" or \":\" in it.\nconst SEP = \"\\u001f\"; // ASCII Unit Separator\n\n/** key = date <NUL> op <NUL> label → count */\nconst labelBuffer = new Map<string, number>();\n/** key = date <NUL> op <NUL> hour → count */\nconst hourBuffer = new Map<string, number>();\n\nlet sink: Sink | null = null;\nlet flushIntervalMs = 60_000;\nlet onError: ((e: unknown) => void) | null = null;\nlet timer: ReturnType<typeof setInterval> | null = null;\nlet flushing = false;\n/** Safety valve — flush early if a burst fills the buffer between intervals. */\nconst MAX_BUFFER_KEYS = 5_000;\n\nexport interface MeterConfig {\n sink: Sink;\n flushIntervalMs?: number;\n onError?: (e: unknown) => void;\n}\n\n/** Point the meter at a sink. Called by `init()`; pass your own sink to self-host. */\nexport function configureMeter(config: MeterConfig): void {\n sink = config.sink;\n if (config.flushIntervalMs && config.flushIntervalMs > 0) flushIntervalMs = config.flushIntervalMs;\n onError = config.onError ?? null;\n}\n\nconst utcDate = (): string => new Date().toISOString().slice(0, 10);\nconst utcHour = (): string => new Date().toISOString().slice(11, 13);\n\nfunction ensureFlushLoop(): void {\n if (timer) return;\n timer = setInterval(() => void flush(), flushIntervalMs);\n // Don't keep the event loop alive just for metering.\n (timer as { unref?: () => void }).unref?.();\n // Flush the last window on shutdown.\n process.once?.(\"SIGTERM\", () => void flush());\n process.once?.(\"beforeExit\", () => void flush());\n}\n\n/**\n * Count `quantity` of a `resource` against the live tag. THE adapter primitive —\n * a Firestore adapter records \"read\"; a ClickHouse adapter records\n * \"clickhouse.query_ms\"; an OpenAI adapter records \"openai.tokens\". Each resource\n * is bucketed entirely on its own; nothing is ever added across resources. Never\n * throws.\n */\nexport function record(resource: ResourceUnit, quantity: number, hint?: CostHint): void {\n try {\n if (!Number.isFinite(quantity) || quantity <= 0) return;\n const t = currentCostTag();\n const date = utcDate();\n // CASCADE — every unit gets a label, by design (no blind spots): the bucket\n // name wins; else the collection it actually touched (`col:posts`); else\n // \"uncategorized\" as a loud last resort. A unit is never invisible.\n const label = t.label || (hint?.collection ? `col:${hint.collection}` : \"uncategorized\");\n // Key includes the resource, so each resource accumulates in its OWN slot.\n const lk = date + SEP + resource + SEP + label;\n labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + quantity);\n const hk = date + SEP + resource + SEP + utcHour();\n hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + quantity);\n ensureFlushLoop();\n if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS) void flush();\n } catch {\n /* metering is best-effort — never disturb the caller */\n }\n}\n\n/** @deprecated — use {@link record}. Firestore-era alias. */\nexport const recordFirestore = record;\n\n/** Firestore bills a minimum of one read even for an empty result, so 0 counts as 1. */\nexport function recordReads(n: number, hint?: CostHint): void {\n record(\"read\", Math.max(n, 1), hint);\n}\nexport function recordWrites(n = 1): void {\n record(\"write\", n);\n}\nexport function recordDeletes(n = 1): void {\n record(\"delete\", n);\n}\n\nfunction add(target: Record<string, ResourceCounts>, key: string, resource: ResourceUnit, n: number): void {\n const bag = (target[key] ??= {});\n // Accumulate WITHIN this resource only — never merge resources.\n bag[resource] = (bag[resource] ?? 0) + n;\n}\n\n/**\n * Coalesce the buffer into one report per UTC day and hand each to the Sink.\n * Snapshots + clears up front so concurrent records land in the next window.\n * Never throws; a sink failure drops that window (surfaced via `onError`).\n */\nexport async function flush(): Promise<void> {\n if (flushing) return;\n // Not configured (init not called) — drop, don't grow unbounded.\n if (!sink) {\n labelBuffer.clear();\n hourBuffer.clear();\n return;\n }\n if (labelBuffer.size === 0 && hourBuffer.size === 0) return;\n flushing = true;\n\n const labels = new Map(labelBuffer);\n const hours = new Map(hourBuffer);\n labelBuffer.clear();\n hourBuffer.clear();\n\n try {\n const byDate = new Map<string, BucketsReport>();\n const reportFor = (date: string): BucketsReport => {\n let r = byDate.get(date);\n if (!r) {\n r = { date, byLabel: {}, byHour: {} };\n byDate.set(date, r);\n }\n return r;\n };\n for (const [k, n] of labels) {\n const [date, op, label] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byLabel, label, op, n);\n }\n for (const [k, n] of hours) {\n const [date, op, hour] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byHour!, hour, op, n);\n }\n for (const report of byDate.values()) {\n await sink.flush(report);\n }\n } catch (e) {\n // Drop this window rather than risk a partial/double report on retry.\n onError?.(e);\n } finally {\n flushing = false;\n }\n}\n","/**\n * sink — where the meter sends a coalesced rollup, and the wire shape it sends.\n *\n * Abstracting the sink is what makes Buckets storage-agnostic: the meter never\n * knows where counts go. The DEFAULT sink (`ReportSink`) reports up to Crossdeck's\n * ingest endpoint so the numbers surface on your Crossdeck dashboard. A team that\n * wants to self-host can implement `Sink` against anything (Postgres, a file, your\n * own API) without touching the meter.\n */\n\n/**\n * Counts for ONE bucket, keyed by RESOURCE UNIT — the raw quantity of each unit,\n * nothing more (no money; you verify cost on your provider's bill).\n *\n * Firestore, the first adapter, emits `read` / `write` / `delete`. Other adapters\n * emit their own units (`clickhouse.query_ms`, `openai.tokens`, …). The rule that\n * keeps this honest: **each resource is its own line. Counts are only ever summed\n * WITHIN a resource, NEVER across one.** A read is not a query-millisecond; the two\n * never land in the same number. (There is deliberately no \"total units\" field.)\n */\nexport interface ResourceCounts {\n read?: number;\n write?: number;\n delete?: number;\n /** Any other resource unit an adapter emits — kept distinct, never merged. */\n [resource: string]: number | undefined;\n}\n\n/** @deprecated name — kept for back-compat; use {@link ResourceCounts}. */\nexport type OpCounts = ResourceCounts;\n\n/**\n * One coalesced report — the wire contract (see docs/ROLLUP_SCHEMA.md). The meter\n * produces one of these per UTC day in a flush window (usually exactly one).\n */\nexport interface BucketsReport {\n /** UTC day \"YYYY-MM-DD\". */\n date: string;\n /** bucket name → counts. The heart of the report. */\n byLabel: Record<string, OpCounts>;\n /** UTC hour \"HH\" → counts, for the hourly \"did my fix land this hour?\" view. */\n byHour?: Record<string, OpCounts>;\n}\n\n/**\n * A destination for coalesced rollups. `flush` MAY throw on failure — the meter\n * catches it, drops that one window, and never lets it reach your app.\n */\nexport interface Sink {\n flush(report: BucketsReport): Promise<void>;\n}\n\nexport interface ReportSinkConfig {\n /** The project's `cd_sk_` secret key. Server-to-server only. */\n apiKey: string;\n /** Defaults to https://api.cross-deck.com/v1/buckets/report */\n endpoint?: string;\n /** Request timeout (ms); a slow Crossdeck must never stall your flush. */\n timeoutMs?: number;\n}\n\nconst DEFAULT_ENDPOINT = \"https://api.cross-deck.com/v1/buckets/report\";\n\n/**\n * The default sink: POST one coalesced rollup to Crossdeck's ingest endpoint.\n * The ingest folds it into the day's maintained doc with `increment`, so many\n * reports a minute coalesce safely. This path does ZERO database reads — it sends\n * a summary, it does not read. Throws on a non-202 so the meter can log/drop the\n * window; the meter guarantees it never reaches your app.\n */\nexport class ReportSink implements Sink {\n private readonly endpoint: string;\n private readonly apiKey: string;\n private readonly timeoutMs: number;\n\n constructor(config: ReportSinkConfig) {\n this.endpoint = config.endpoint ?? DEFAULT_ENDPOINT;\n this.apiKey = config.apiKey;\n this.timeoutMs = config.timeoutMs ?? 5000;\n }\n\n async flush(report: BucketsReport): Promise<void> {\n const res = await fetch(this.endpoint, {\n method: \"POST\",\n signal: AbortSignal.timeout(this.timeoutMs),\n headers: {\n \"content-type\": \"application/json\",\n authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify(report),\n });\n if (res.status !== 202) {\n throw new Error(`Buckets report rejected: HTTP ${res.status}`);\n }\n }\n}\n","/**\n * adapters/firestore — the universal Firestore read meter (the trap).\n *\n * THE LESSON (learned on a real product): per-call-site `recordReads()`\n * instrumentation MISSES paths. You meter the read sites you're looking at and\n * leave the cron / trigger / ingest path uncounted — often the majority of reads,\n * invisible. Humans tag what they see and miss the path that matters.\n *\n * THE FIX: patch the admin SDK's read methods ONCE. From install onward, EVERY\n * read — anywhere, on any code path — is counted under the ambient tag, with zero\n * per-call-site work and no blind spots.\n *\n * SAFETY CONTRACT — this sits on your production read path, so it is defensive by\n * construction. Each wrapper:\n * 1. calls the REAL method first and captures the result,\n * 2. counts in a try/catch that can never throw into the caller,\n * 3. ALWAYS returns the real result, untouched.\n * It cannot break a read, change a result, or add latency beyond one in-memory\n * counter increment. A wrong count is a measurement error, never a correctness or\n * availability one. Idempotent — calling it twice patches once.\n *\n * COUNTING MODEL — a query returning N docs = N reads (an empty result still bills\n * 1, which the meter enforces). A document get = 1. getAll(...) = the ref count.\n * CollectionReference.get IS Query.get (shared prototype method), so patching Query\n * covers collections with no double-count.\n */\nimport { recordReads, type CostHint } from \"../cost-meter\";\n\nlet installed = false;\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\ntype AnyFn = (...args: any[]) => any;\n\n/**\n * The firebase-admin Firestore classes to patch. Pass the module namespace from\n * `firebase-admin/firestore` — only the prototypes present are patched.\n */\nexport interface FirestoreClasses {\n Query?: { prototype: { get?: AnyFn; onSnapshot?: AnyFn } };\n DocumentReference?: { prototype: { get?: AnyFn; onSnapshot?: AnyFn } };\n Transaction?: { prototype: { get?: AnyFn; getAll?: AnyFn } };\n Firestore?: { prototype: { getAll?: AnyFn } };\n /** count() / sum() / average() — aggregation queries bill reads too. */\n AggregateQuery?: { prototype: { get?: AnyFn } };\n}\n\n/** `projects/{id}/…` → the project id, else undefined. Pure string op. */\nfunction projectFromPath(path: string): string | undefined {\n const parts = path.split(\"/\");\n const i = parts.indexOf(\"projects\");\n return i >= 0 && parts[i + 1] ? parts[i + 1] : undefined;\n}\n\n/**\n * Derive { collection, projectId } from the read target's path so an UNtagged read\n * cascades to `col:<collection>` instead of \"uncategorized\". PURE CPU; never reads,\n * never throws. Falls back to firebase-admin's internal `_queryOptions` for filtered\n * queries (which don't expose `.path`).\n */\nfunction hintFrom(target: any): CostHint | undefined {\n try {\n const p = typeof target?.path === \"string\" ? target.path : \"\";\n if (p) {\n const parts = p.split(\"/\").filter(Boolean);\n const collection = parts.length % 2 === 0 ? parts[parts.length - 2] : parts[parts.length - 1];\n return { collection, projectId: projectFromPath(p) };\n }\n const qo = target?._queryOptions;\n if (qo) {\n const collection = typeof qo.collectionId === \"string\" ? qo.collectionId : undefined;\n const parent =\n typeof qo.parentPath?.relativeName === \"string\"\n ? qo.parentPath.relativeName\n : typeof qo.parentPath?.toString === \"function\"\n ? String(qo.parentPath.toString())\n : \"\";\n return { collection, projectId: parent ? projectFromPath(parent) : undefined };\n }\n } catch {\n /* the meter must never throw */\n }\n return undefined;\n}\n\nfunction meterSnap(snap: unknown, hint?: CostHint): void {\n try {\n const size = (snap as { size?: number } | null)?.size;\n recordReads(typeof size === \"number\" ? size : 1, hint);\n } catch {\n /* best-effort */\n }\n}\nfunction meterCount(n: number, hint?: CostHint): void {\n try {\n recordReads(n, hint);\n } catch {\n /* best-effort */\n }\n}\n\n/**\n * Install the universal read meter on the firebase-admin Firestore classes. Call\n * ONCE at process start, before any reads. Pass the namespace from\n * `firebase-admin/firestore` so the exact prototypes the SDK uses are patched:\n *\n * import * as Firestore from \"firebase-admin/firestore\";\n * installFirestoreMeter(Firestore);\n */\nexport function installFirestoreMeter(classes: FirestoreClasses): void {\n if (installed) return;\n installed = true;\n const { Query, DocumentReference, Transaction, Firestore, AggregateQuery } = classes;\n\n // Query.get — covers Query AND CollectionReference (shared prototype method).\n const qGet = Query?.prototype?.get;\n if (qGet) {\n Query!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await qGet.apply(this, args);\n meterSnap(snap, hintFrom(this));\n return snap;\n };\n }\n\n // DocumentReference.get — a single doc = 1 read.\n const dGet = DocumentReference?.prototype?.get;\n if (dGet) {\n DocumentReference!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await dGet.apply(this, args);\n meterCount(1, hintFrom(this));\n return snap;\n };\n }\n\n // Transaction.get — query or doc; size when present, else 1.\n const tGet = Transaction?.prototype?.get;\n if (tGet) {\n Transaction!.prototype.get = async function (this: unknown, ...args: any[]) {\n const res = await tGet.apply(this, args);\n meterSnap(res, hintFrom(args[0]));\n return res;\n };\n }\n\n // Transaction.getAll(...refs) — one read per ref.\n const tGetAll = Transaction?.prototype?.getAll;\n if (tGetAll) {\n Transaction!.prototype.getAll = async function (this: unknown, ...args: any[]) {\n const res = await tGetAll.apply(this, args);\n meterCount(Array.isArray(res) ? res.length : args.length || 1, hintFrom(args[0]));\n return res;\n };\n }\n\n // Firestore.getAll(...refs) — batched doc reads.\n const fGetAll = Firestore?.prototype?.getAll;\n if (fGetAll) {\n Firestore!.prototype.getAll = async function (this: unknown, ...args: any[]) {\n const res = await fGetAll.apply(this, args);\n meterCount(Array.isArray(res) ? res.length : args.length || 1, hintFrom(args[0]));\n return res;\n };\n }\n\n // AggregateQuery.get — count() / sum() / average(). Firestore bills one read per\n // up to 1000 index entries scanned, and the client never sees that entry count —\n // so this is an HONEST ESTIMATE: for count() we derive it from the result\n // (ceil(count / 1000)), else the billed minimum of 1. Observe-only: it reads the\n // result you already got, adds zero reads. (Closes the aggregation blind spot.)\n const aGet = AggregateQuery?.prototype?.get;\n if (aGet) {\n AggregateQuery!.prototype.get = async function (this: unknown, ...args: any[]) {\n const snap = await aGet.apply(this, args);\n try {\n const data = (snap as { data?: () => { count?: number } } | null)?.data?.();\n const count = typeof data?.count === \"number\" ? data.count : 0;\n meterCount(Math.max(1, Math.ceil(count / 1000)), hintFrom(this));\n } catch {\n /* best-effort */\n }\n return snap;\n };\n }\n\n // Query.onSnapshot / DocumentReference.onSnapshot — server-side realtime\n // listeners. We OBSERVE: wrap onNext and count the docs each fire delivers (a\n // query's changed docs — first fire = all matching; a doc = 1). We attach no\n // listener and issue no read; the meter just sees what the listener is already\n // billed. (Closes the server-listener blind spot.)\n const patchOnSnapshot = (proto: { onSnapshot?: AnyFn } | undefined, perDoc: boolean): void => {\n const real = proto?.onSnapshot;\n if (!real) return;\n proto!.onSnapshot = function (this: unknown, ...args: any[]) {\n const hint = hintFrom(this);\n const i = args.findIndex((a) => typeof a === \"function\");\n if (i >= 0) {\n const onNext = args[i];\n args[i] = function (snap: any) {\n try {\n const n = perDoc\n ? 1\n : typeof snap?.docChanges === \"function\"\n ? snap.docChanges().length\n : typeof snap?.size === \"number\"\n ? snap.size\n : 1;\n if (n > 0) meterCount(n, hint);\n } catch {\n /* best-effort */\n }\n return onNext(snap);\n };\n }\n return real.apply(this, args);\n };\n };\n patchOnSnapshot(Query?.prototype, false);\n patchOnSnapshot(DocumentReference?.prototype, true);\n}\n\n/** Test-only: reset the install guard so a suite can re-patch fresh prototypes. */\nexport function __resetFirestoreMeterForTests(): void {\n installed = false;\n}\n","/**\n * @cross-deck/buckets — know exactly what every database read costs you, and who\n * caused it. A tiny, never-throws collector for Firestore.\n *\n * The whole footprint a consumer sees:\n * 1. init({ apiKey, firestore }) — configure once, install the trap once\n * 2. bucket(name, fn) — name the read paths that matter\n * 3. (the dashboard shows the rest — and names the ones you haven't yet)\n */\nimport { configureMeter, type MeterConfig } from \"./cost-meter\";\nimport { ReportSink, type Sink } from \"./sink\";\nimport { installFirestoreMeter, type FirestoreClasses } from \"./adapters/firestore\";\n\nexport interface InitOptions {\n /** The project's `cd_sk_` SECRET key. Server-to-server only — never a browser key. */\n apiKey: string;\n /**\n * Pass the namespace from `firebase-admin/firestore` to auto-install the read\n * trap (recommended — this is what makes every read count with no per-call work).\n * Omit it if you'd rather call `installFirestoreMeter()` yourself, or you only\n * use the manual `recordReads()` recorders.\n */\n firestore?: FirestoreClasses;\n /** Override the report endpoint (defaults to Crossdeck's ingest). */\n endpoint?: string;\n /** How often to flush coalesced counts (ms). Default 60_000. */\n flushIntervalMs?: number;\n /** Bring your own sink (self-host the rollups). Defaults to reporting up to Crossdeck. */\n sink?: Sink;\n /** Notified when a flush fails, so a dropped window is never silent. Best-effort. */\n onError?: MeterConfig[\"onError\"];\n}\n\n/**\n * Configure Buckets once, at process start. Points the meter at a sink (Crossdeck's\n * ingest by default) and — if you pass `firestore` — installs the universal read\n * trap so every read counts automatically.\n */\nexport function init(options: InitOptions): void {\n const sink = options.sink ?? new ReportSink({ apiKey: options.apiKey, endpoint: options.endpoint });\n configureMeter({ sink, flushIntervalMs: options.flushIntervalMs, onError: options.onError });\n if (options.firestore) installFirestoreMeter(options.firestore);\n}\n\n/** Alias — reads well next to `bucket()` at a call site. */\nexport { init as initBuckets };\n\n// The headline verb + the lower-level tag controls it is sugar over.\nexport {\n bucket,\n runWithCostTag,\n enterCostTag,\n refineCostTag,\n currentCostTag,\n type CostTag,\n} from \"./cost-context\";\n\n// Recorders. `record(resource, quantity)` is the generic adapter primitive — count\n// any resource unit (a future adapter records \"clickhouse.query_ms\"); recordReads/\n// Writes/Deletes are the Firestore conveniences over it.\nexport {\n record,\n recordReads,\n recordWrites,\n recordDeletes,\n flush,\n type CostHint,\n type ResourceUnit,\n type OpType,\n type MeterConfig,\n} from \"./cost-meter\";\n\n// The trap (the only datastore adapter today) + its class shape.\nexport { installFirestoreMeter, type FirestoreClasses } from \"./adapters/firestore\";\n\n// The sink seam — for self-hosting rollups instead of reporting to Crossdeck.\nexport {\n ReportSink,\n type Sink,\n type BucketsReport,\n type ResourceCounts,\n type OpCounts,\n type ReportSinkConfig,\n} from \"./sink\";\n"]}
@@ -7,11 +7,25 @@
7
7
  * wants to self-host can implement `Sink` against anything (Postgres, a file, your
8
8
  * own API) without touching the meter.
9
9
  */
10
- interface OpCounts {
10
+ /**
11
+ * Counts for ONE bucket, keyed by RESOURCE UNIT — the raw quantity of each unit,
12
+ * nothing more (no money; you verify cost on your provider's bill).
13
+ *
14
+ * Firestore, the first adapter, emits `read` / `write` / `delete`. Other adapters
15
+ * emit their own units (`clickhouse.query_ms`, `openai.tokens`, …). The rule that
16
+ * keeps this honest: **each resource is its own line. Counts are only ever summed
17
+ * WITHIN a resource, NEVER across one.** A read is not a query-millisecond; the two
18
+ * never land in the same number. (There is deliberately no "total units" field.)
19
+ */
20
+ interface ResourceCounts {
11
21
  read?: number;
12
22
  write?: number;
13
23
  delete?: number;
24
+ /** Any other resource unit an adapter emits — kept distinct, never merged. */
25
+ [resource: string]: number | undefined;
14
26
  }
27
+ /** @deprecated name — kept for back-compat; use {@link ResourceCounts}. */
28
+ type OpCounts = ResourceCounts;
15
29
  /**
16
30
  * One coalesced report — the wire contract (see docs/ROLLUP_SCHEMA.md). The meter
17
31
  * produces one of these per UTC day in a flush window (usually exactly one).
@@ -54,4 +68,4 @@ declare class ReportSink implements Sink {
54
68
  flush(report: BucketsReport): Promise<void>;
55
69
  }
56
70
 
57
- export { type BucketsReport as B, type OpCounts as O, ReportSink as R, type Sink as S, type ReportSinkConfig as a };
71
+ export { type BucketsReport as B, type OpCounts as O, ReportSink as R, type Sink as S, type ReportSinkConfig as a, type ResourceCounts as b };
@@ -7,11 +7,25 @@
7
7
  * wants to self-host can implement `Sink` against anything (Postgres, a file, your
8
8
  * own API) without touching the meter.
9
9
  */
10
- interface OpCounts {
10
+ /**
11
+ * Counts for ONE bucket, keyed by RESOURCE UNIT — the raw quantity of each unit,
12
+ * nothing more (no money; you verify cost on your provider's bill).
13
+ *
14
+ * Firestore, the first adapter, emits `read` / `write` / `delete`. Other adapters
15
+ * emit their own units (`clickhouse.query_ms`, `openai.tokens`, …). The rule that
16
+ * keeps this honest: **each resource is its own line. Counts are only ever summed
17
+ * WITHIN a resource, NEVER across one.** A read is not a query-millisecond; the two
18
+ * never land in the same number. (There is deliberately no "total units" field.)
19
+ */
20
+ interface ResourceCounts {
11
21
  read?: number;
12
22
  write?: number;
13
23
  delete?: number;
24
+ /** Any other resource unit an adapter emits — kept distinct, never merged. */
25
+ [resource: string]: number | undefined;
14
26
  }
27
+ /** @deprecated name — kept for back-compat; use {@link ResourceCounts}. */
28
+ type OpCounts = ResourceCounts;
15
29
  /**
16
30
  * One coalesced report — the wire contract (see docs/ROLLUP_SCHEMA.md). The meter
17
31
  * produces one of these per UTC day in a flush window (usually exactly one).
@@ -54,4 +68,4 @@ declare class ReportSink implements Sink {
54
68
  flush(report: BucketsReport): Promise<void>;
55
69
  }
56
70
 
57
- export { type BucketsReport as B, type OpCounts as O, ReportSink as R, type Sink as S, type ReportSinkConfig as a };
71
+ export { type BucketsReport as B, type OpCounts as O, ReportSink as R, type Sink as S, type ReportSinkConfig as a, type ResourceCounts as b };
package/dist/web.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { S as Sink, B as BucketsReport } from './sink-DjFci4Uz.mjs';
2
- export { O as OpCounts } from './sink-DjFci4Uz.mjs';
1
+ import { S as Sink, B as BucketsReport } from './sink-Cojdm73l.mjs';
2
+ export { O as OpCounts } from './sink-Cojdm73l.mjs';
3
3
 
4
4
  /**
5
5
  * web/meter — the browser read meter. Same contract as the Node meter (count in
package/dist/web.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { S as Sink, B as BucketsReport } from './sink-DjFci4Uz.js';
2
- export { O as OpCounts } from './sink-DjFci4Uz.js';
1
+ import { S as Sink, B as BucketsReport } from './sink-Cojdm73l.js';
2
+ export { O as OpCounts } from './sink-Cojdm73l.js';
3
3
 
4
4
  /**
5
5
  * web/meter — the browser read meter. Same contract as the Node meter (count in
package/dist/web.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/web/meter.ts","../src/web/sink.ts","../src/web/context.ts","../src/web/firestore.ts","../src/web/index.ts"],"names":["FS","segs","getDoc","getDocs","onSnapshot","getDocFromServer","getDocsFromServer","getDocFromCache","getDocsFromCache","getCountFromServer","getAggregateFromServer","sink"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAiBA,IAAM,GAAA,GAAM,GAAA;AAGZ,IAAM,WAAA,uBAAkB,GAAA,EAAoB;AAE5C,IAAM,UAAA,uBAAiB,GAAA,EAAoB;AAE3C,IAAI,IAAA,GAAoB,IAAA;AACxB,IAAI,eAAA,GAAkB,GAAA;AACtB,IAAI,OAAA,G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KAAA,EAAO,QAAA,CAAS,IAAI,CAAC,CAAA;AAC3B,IAAA,OAAO,IAAA;AAAA,EACT,CAAC,CAAA;AACH;AACO,SAASC,uBAAAA,CAAuB,KAAA,EAAY,IAAA,EAAA,GAAc,IAAA,EAA2B;AAC1F,EAAA,MAAM,KAAA,GAAQ,YAAA,EAAa,IAAK,SAAA,CAAU,KAAK,CAAA;AAC/C,EAAA,OAAO,yBAAA,CAA2B,OAAO,IAAA,EAAM,GAAG,IAAI,CAAA,CAAE,IAAA,CAAK,CAAC,IAAA,KAAc;AAC1E,IAAA,KAAA,CAAM,KAAA,EAAO,QAAA,CAAS,IAAI,CAAC,CAAA;AAC3B,IAAA,OAAO,IAAA;AAAA,EACT,CAAC,CAAA;AACH;;;ACxIO,SAAS,eAAe,OAAA,EAA+B;AAC5D,EAAA,MAAMC,KAAAA,GAAO,IAAI,aAAA,CAAc,EAAE,MAAA,EAAQ,QAAQ,MAAA,EAAQ,QAAA,EAAU,OAAA,CAAQ,QAAA,EAAU,CAAA;AACrF,EAAA,iBAAA,CAAkB,EAAE,MAAAA,KAAAA,EAAM,eAAA,EAAiB,QAAQ,eAAA,EAAiB,OAAA,EAAS,OAAA,CAAQ,OAAA,EAAS,CAAA;AAChG","file":"web.js","sourcesContent":["/**\n * web/meter — the browser read meter. Same contract as the Node meter (count in\n * memory, flush ~1/min, never throw into the app), adapted to the browser:\n *\n * - no AsyncLocalStorage — labels come from web/context (synchronous),\n * - \"shutdown\" is the tab going hidden/closed, so we also flush on\n * visibilitychange→hidden and pagehide (a `fetch(..., {keepalive:true})`\n * survives the unload),\n * - it talks to a Sink exactly like the Node meter, so the wire shape is\n * identical and the same ingest receives both.\n */\nimport type { Sink, BucketsReport, OpCounts } from \"../sink\";\n\nexport type OpType = \"read\" | \"write\" | \"delete\";\n\n// ASCII Unit Separator — a bucket/collection name never contains it, so the\n// composite key splits back cleanly.\nconst SEP = \"\\u001f\"; // ASCII Unit Separator\n\n/** key = date <US> op <US> label → count */\nconst labelBuffer = new Map<string, number>();\n/** key = date <US> op <US> hour → count */\nconst hourBuffer = new Map<string, number>();\n\nlet sink: Sink | null = null;\nlet flushIntervalMs = 60_000;\nlet onError: ((e: unknown) => void) | null = null;\nlet timer: ReturnType<typeof setInterval> | null = null;\nlet flushing = false;\nlet lifecycleBound = false;\nconst MAX_BUFFER_KEYS = 5_000;\n\nexport interface WebMeterConfig {\n sink: Sink;\n flushIntervalMs?: number;\n onError?: (e: unknown) => void;\n}\n\nexport function configureWebMeter(config: WebMeterConfig): void {\n sink = config.sink;\n if (config.flushIntervalMs && config.flushIntervalMs > 0) flushIntervalMs = config.flushIntervalMs;\n onError = config.onError ?? null;\n}\n\nconst utcDate = (): string => new Date().toISOString().slice(0, 10);\nconst utcHour = (): string => new Date().toISOString().slice(11, 13);\n\nfunction ensureLoop(): void {\n if (timer) return;\n timer = setInterval(() => void flushWeb(), flushIntervalMs);\n if (!lifecycleBound && typeof addEventListener === \"function\") {\n lifecycleBound = true;\n // The tab being hidden or torn down is the browser's \"shutdown\" — flush the\n // last window. keepalive on the sink's fetch lets it complete during unload.\n addEventListener(\"visibilitychange\", () => {\n if (typeof document !== \"undefined\" && document.visibilityState === \"hidden\") void flushWeb();\n });\n addEventListener(\"pagehide\", () => void flushWeb());\n }\n}\n\n/** Count `n` ops of `op` against `label`. Never throws. */\nexport function recordWeb(op: OpType, n: number, label: string): void {\n try {\n if (!Number.isFinite(n) || n <= 0) return;\n const date = utcDate();\n const lk = date + SEP + op + SEP + label;\n labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + n);\n const hk = date + SEP + op + SEP + utcHour();\n hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + n);\n ensureLoop();\n if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS) void flushWeb();\n } catch {\n /* metering is best-effort — never disturb the page */\n }\n}\n\nfunction add(target: Record<string, OpCounts>, key: string, op: OpType, n: number): void {\n const bag = (target[key] ??= {});\n bag[op] = (bag[op] ?? 0) + n;\n}\n\n/** Coalesce the buffer into one report per UTC day and hand each to the Sink. */\nexport async function flushWeb(): Promise<void> {\n if (flushing) return;\n if (!sink) {\n labelBuffer.clear();\n hourBuffer.clear();\n return;\n }\n if (labelBuffer.size === 0 && hourBuffer.size === 0) return;\n flushing = true;\n\n const labels = new Map(labelBuffer);\n const hours = new Map(hourBuffer);\n labelBuffer.clear();\n hourBuffer.clear();\n\n try {\n const byDate = new Map<string, BucketsReport>();\n const reportFor = (date: string): BucketsReport => {\n let r = byDate.get(date);\n if (!r) {\n r = { date, byLabel: {}, byHour: {} };\n byDate.set(date, r);\n }\n return r;\n };\n for (const [k, n] of labels) {\n const [date, op, label] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byLabel, label, op, n);\n }\n for (const [k, n] of hours) {\n const [date, op, hour] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byHour!, hour, op, n);\n }\n for (const report of byDate.values()) {\n await sink.flush(report);\n }\n } catch (e) {\n onError?.(e);\n } finally {\n flushing = false;\n }\n}\n","/**\n * web/sink — reports the browser's coalesced rollup up to Crossdeck's ingest.\n *\n * Two differences from the Node sink, both forced by the browser:\n * - it authenticates with a PUBLISHABLE key (`cd_pub_live_`), never a secret — a\n * secret key cannot live in client code. (The ingest accepts publishable keys\n * for Buckets reports the same way the analytics SDK accepts them for events.)\n * - it uses `fetch(..., { keepalive: true })` so a report fired as the tab is\n * closing still completes.\n *\n * It performs ZERO database operations — it sends a summary, it does not read.\n */\nimport type { BucketsReport, Sink } from \"../sink\";\n\nconst DEFAULT_ENDPOINT = \"https://api.cross-deck.com/v1/buckets/report\";\n\nexport interface WebReportSinkConfig {\n /** The project's `cd_pub_live_` PUBLISHABLE key. */\n apiKey: string;\n endpoint?: string;\n}\n\nexport class WebReportSink implements Sink {\n private readonly endpoint: string;\n private readonly apiKey: string;\n\n constructor(config: WebReportSinkConfig) {\n this.endpoint = config.endpoint ?? DEFAULT_ENDPOINT;\n this.apiKey = config.apiKey;\n }\n\n async flush(report: BucketsReport): Promise<void> {\n const res = await fetch(this.endpoint, {\n method: \"POST\",\n keepalive: true,\n headers: {\n \"content-type\": \"application/json\",\n authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify(report),\n });\n if (res.status !== 202) {\n throw new Error(`Buckets web report rejected: HTTP ${res.status}`);\n }\n }\n}\n","/**\n * web/context — the browser's tagging primitive.\n *\n * The Node collector rides AsyncLocalStorage to attribute reads to a bucket\n * across async fan-outs. The browser has no AsyncLocalStorage — but it doesn't\n * need one: a read (a `getDocs` call, an `onSnapshot` registration) is set up\n * SYNCHRONOUSLY, so a plain module-level \"current label\" captured at call time is\n * exact. `bucket(name, fn)` sets it for the synchronous body of `fn` and restores\n * it after — so the read inside picks up the name, and an `onSnapshot` listener\n * keeps that name for every future fire.\n */\n\nlet current: string | undefined;\n\n/** The bucket name in effect right now, or undefined (→ cascade to collection). */\nexport function currentLabel(): string | undefined {\n return current;\n}\n\n/**\n * Attribute every read SET UP inside `fn` to the bucket `name`:\n *\n * bucket(\"pulse-map\", () => onSnapshot(liveQuery, render));\n * // → that listener's reads all show as \"pulse-map\", forever\n */\nexport function bucket<T>(name: string, fn: () => T): T {\n const prev = current;\n current = name;\n try {\n return fn();\n } finally {\n current = prev;\n }\n}\n","/**\n * web/firestore — drop-in wrappers for the three Firestore client read calls.\n *\n * Swap your import source and nothing else:\n * - import { getDoc, getDocs, onSnapshot } from \"firebase/firestore\"\n * + import { getDoc, getDocs, onSnapshot } from \"@cross-deck/buckets/web\"\n *\n * Each wrapper calls the REAL Firestore function, counts the documents it\n * delivers (exactly what Firestore bills), labels it (your `bucket()` name, else\n * the collection), and returns the real result untouched. It can never change a\n * result or throw from the metering — same safety contract as the server trap.\n *\n * COUNTING:\n * - getDoc → 1 read\n * - getDocs → snapshot.size reads\n * - onSnapshot → on EVERY fire, the number of doc changes delivered\n * (first fire = all matching docs; each update = just the changed ones —\n * which is precisely what a listener is billed).\n */\nimport * as FS from \"firebase/firestore\";\nimport { recordWeb } from \"./meter\";\nimport { currentLabel } from \"./context\";\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\ntype AnyAsync = (...args: any[]) => Promise<any>;\n\n// Call the real reads through the namespace + loose aliases: a function missing on\n// an older `firebase` (e.g. getAggregateFromServer pre-v10) is simply `undefined`\n// rather than a hard import error, and the wrappers pass arguments through verbatim.\nconst rawGetDoc = FS.getDoc as AnyAsync;\nconst rawGetDocs = FS.getDocs as AnyAsync;\nconst rawOnSnapshot = FS.onSnapshot as (...args: any[]) => any;\nconst rawGetDocFromServer = (FS as any).getDocFromServer as AnyAsync | undefined;\nconst rawGetDocsFromServer = (FS as any).getDocsFromServer as AnyAsync | undefined;\nconst rawGetDocFromCache = (FS as any).getDocFromCache as AnyAsync | undefined;\nconst rawGetDocsFromCache = (FS as any).getDocsFromCache as AnyAsync | undefined;\nconst rawGetCountFromServer = (FS as any).getCountFromServer as AnyAsync | undefined;\nconst rawGetAggregateFromServer = (FS as any).getAggregateFromServer as AnyAsync | undefined;\n\n/** Best-effort collection label from a ref/query. PURE; never throws. */\nfunction collLabel(ref: any): string {\n try {\n const path: string =\n (typeof ref?.path === \"string\" && ref.path) ||\n (ref?._query?.path?.segments?.join?.(\"/\") ?? \"\") ||\n \"\";\n if (path) {\n const segs = path.split(\"/\").filter(Boolean);\n // even segment count → document path (…/coll/id); odd → collection.\n const coll = segs.length % 2 === 0 ? segs[segs.length - 2] : segs[segs.length - 1];\n return coll ? `col:${coll}` : \"uncategorized\";\n }\n const segs = ref?._query?.path?.segments;\n if (Array.isArray(segs) && segs.length) return `col:${segs[segs.length - 1]}`;\n } catch {\n /* never throw from labelling */\n }\n return \"uncategorized\";\n}\n\nfunction meter(label: string, n: number): void {\n try {\n recordWeb(\"read\", n, label);\n } catch {\n /* best-effort */\n }\n}\n\n/** Count the docs a snapshot delivers: a query's changed docs, or 1 for a doc. */\nfunction countSnap(snap: any): number {\n try {\n if (typeof snap?.docChanges === \"function\") return snap.docChanges().length;\n } catch {\n /* fall through */\n }\n return 1;\n}\n\nexport function getDoc(ref: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(ref);\n return rawGetDoc(ref, ...rest).then((snap: any) => {\n meter(label, 1);\n return snap;\n });\n}\n\nexport function getDocs(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetDocs(query, ...rest).then((snap: any) => {\n meter(label, typeof snap?.size === \"number\" ? Math.max(snap.size, 1) : 1);\n return snap;\n });\n}\n\n/**\n * onSnapshot has several overloads — (ref, observer), (ref, onNext, onError,\n * onComplete), and either of those with a leading SnapshotListenOptions. We find\n * the next-handler wherever it is (a function or `observer.next`) and wrap it to\n * count on each fire, leaving every other argument exactly as passed.\n */\nexport function onSnapshot(ref: any, ...args: any[]): any {\n const label = currentLabel() ?? collLabel(ref);\n const wrapNext = (fn: any) =>\n typeof fn === \"function\"\n ? (snap: any) => {\n meter(label, countSnap(snap));\n return fn(snap);\n }\n : fn;\n\n const out = args.slice();\n // Leading options object (not a function, not an observer): skip it.\n let i = 0;\n if (out[0] && typeof out[0] !== \"function\" && !(\"next\" in out[0])) i = 1;\n\n if (out[i] && typeof out[i] === \"object\" && \"next\" in out[i]) {\n // observer form: clone with wrapped next\n out[i] = { ...out[i], next: wrapNext(out[i].next) };\n } else if (typeof out[i] === \"function\") {\n // callback form: wrap onNext (the first function)\n out[i] = wrapNext(out[i]);\n }\n\n return rawOnSnapshot(ref, ...out);\n}\n\n// ── Force-from-server reads — bill exactly like getDoc / getDocs ──────────────\nexport function getDocFromServer(ref: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(ref);\n return rawGetDocFromServer!(ref, ...rest).then((snap: any) => {\n meter(label, 1);\n return snap;\n });\n}\nexport function getDocsFromServer(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetDocsFromServer!(query, ...rest).then((snap: any) => {\n meter(label, typeof snap?.size === \"number\" ? Math.max(snap.size, 1) : 1);\n return snap;\n });\n}\n\n// ── Cache-only reads — NOT billed, so pass through and count NOTHING ──────────\n// (Counting these would over-report; a cache hit costs you nothing.)\nexport function getDocFromCache(ref: any, ...rest: any[]): Promise<any> {\n return rawGetDocFromCache!(ref, ...rest);\n}\nexport function getDocsFromCache(query: any, ...rest: any[]): Promise<any> {\n return rawGetDocsFromCache!(query, ...rest);\n}\n\n// ── Aggregation — count() / sum() / average() ────────────────────────────────\n// Honest estimate: ceil(count / 1000), min 1 — Firestore never exposes the exact\n// index-entry count. Same model as the server trap, so the numbers reconcile.\nfunction aggReads(snap: any): number {\n const count = typeof snap?.data?.()?.count === \"number\" ? snap.data().count : 0;\n return Math.max(1, Math.ceil(count / 1000));\n}\nexport function getCountFromServer(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetCountFromServer!(query, ...rest).then((snap: any) => {\n meter(label, aggReads(snap));\n return snap;\n });\n}\nexport function getAggregateFromServer(query: any, spec: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetAggregateFromServer!(query, spec, ...rest).then((snap: any) => {\n meter(label, aggReads(snap));\n return snap;\n });\n}\n","/**\n * @cross-deck/buckets/web — the BROWSER collector.\n *\n * Most Firebase apps read straight from the browser (live `onSnapshot`\n * listeners, `getDocs`, `getDoc`) — reads billed to your project that a\n * server-side collector can never see. This adapter closes that hole.\n *\n * Setup (two lines + one import swap):\n *\n * import { initBucketsWeb, bucket } from \"@cross-deck/buckets/web\";\n * import { getDoc, getDocs, onSnapshot } from \"@cross-deck/buckets/web\"; // was \"firebase/firestore\"\n *\n * initBucketsWeb({ apiKey: \"cd_pub_live_…\" }); // your PUBLISHABLE key\n *\n * bucket(\"pulse-map\", () => onSnapshot(liveQuery, render));\n *\n * Every read those wrappers see is counted, labelled, and reported up the same\n * ingest pipe as the server collector — so the dashboard shows server AND browser\n * reads side by side.\n */\nimport { configureWebMeter, flushWeb, type WebMeterConfig } from \"./meter\";\nimport { WebReportSink } from \"./sink\";\n\nexport interface InitWebOptions {\n /** The project's `cd_pub_live_` PUBLISHABLE key (safe in client code). */\n apiKey: string;\n /** Override the report endpoint (defaults to Crossdeck's ingest). */\n endpoint?: string;\n /** How often to flush coalesced counts (ms). Default 60_000. */\n flushIntervalMs?: number;\n /** Notified when a flush fails, so a dropped window is never silent. */\n onError?: WebMeterConfig[\"onError\"];\n}\n\n/** Configure the browser collector once, at app start. */\nexport function initBucketsWeb(options: InitWebOptions): void {\n const sink = new WebReportSink({ apiKey: options.apiKey, endpoint: options.endpoint });\n configureWebMeter({ sink, flushIntervalMs: options.flushIntervalMs, onError: options.onError });\n}\n\n// The tagging verb + the metered read wrappers (swap your firebase/firestore\n// import for these). Cache-only reads pass through uncounted (they aren't billed).\nexport { bucket } from \"./context\";\nexport {\n getDoc,\n getDocs,\n onSnapshot,\n getDocFromServer,\n getDocsFromServer,\n getDocFromCache,\n getDocsFromCache,\n getCountFromServer,\n getAggregateFromServer,\n} from \"./firestore\";\nexport { flushWeb as flush } from \"./meter\";\n\n// The sink seam — for self-hosting the browser rollups instead of reporting to Crossdeck.\nexport { WebReportSink, type WebReportSinkConfig } from \"./sink\";\nexport type { BucketsReport, OpCounts, Sink } from \"../sink\";\n"]}
1
+ {"version":3,"sources":["../src/web/meter.ts","../src/web/sink.ts","../src/web/context.ts","../src/web/firestore.ts","../src/web/index.ts"],"names":["FS","segs","getDoc","getDocs","onSnapshot","getDocFromServer","getDocsFromServer","getDocFromCache","getDocsFromCache","getCountFromServer","getAggregateFromServer","sink"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAmBA,IAAM,GAAA,GAAM,GAAA;AAGZ,IAAM,WAAA,uBAAkB,GAAA,EAAoB;AAE5C,IAAM,UAAA,uBAAiB,GAAA,EAAoB;AAE3C,IAAI,IAAA,GAAoB,IAAA;AACxB,IAAI,eAAA,GAAkB,GAAA;AACtB,IAAI,OAAA,GAAyC,IAAA;AAC7C,IAAI,KAAA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AAC,CAAA;AAC3B,IAAA,OAAO,IAAA;AAAA,EACT,CAAC,CAAA;AACH;AACO,SAASC,uBAAAA,CAAuB,KAAA,EAAY,IAAA,EAAA,GAAc,IAAA,EAA2B;AAC1F,EAAA,MAAM,KAAA,GAAQ,YAAA,EAAa,IAAK,SAAA,CAAU,KAAK,CAAA;AAC/C,EAAA,OAAO,yBAAA,CAA2B,OAAO,IAAA,EAAM,GAAG,IAAI,CAAA,CAAE,IAAA,CAAK,CAAC,IAAA,KAAc;AAC1E,IAAA,KAAA,CAAM,KAAA,EAAO,QAAA,CAAS,IAAI,CAAC,CAAA;AAC3B,IAAA,OAAO,IAAA;AAAA,EACT,CAAC,CAAA;AACH;;;ACxIO,SAAS,eAAe,OAAA,EAA+B;AAC5D,EAAA,MAAMC,KAAAA,GAAO,IAAI,aAAA,CAAc,EAAE,MAAA,EAAQ,QAAQ,MAAA,EAAQ,QAAA,EAAU,OAAA,CAAQ,QAAA,EAAU,CAAA;AACrF,EAAA,iBAAA,CAAkB,EAAE,MAAAA,KAAAA,EAAM,eAAA,EAAiB,QAAQ,eAAA,EAAiB,OAAA,EAAS,OAAA,CAAQ,OAAA,EAAS,CAAA;AAChG","file":"web.js","sourcesContent":["/**\n * web/meter — the browser read meter. Same contract as the Node meter (count in\n * memory, flush ~1/min, never throw into the app), adapted to the browser:\n *\n * - no AsyncLocalStorage — labels come from web/context (synchronous),\n * - \"shutdown\" is the tab going hidden/closed, so we also flush on\n * visibilitychange→hidden and pagehide (a `fetch(..., {keepalive:true})`\n * survives the unload),\n * - it talks to a Sink exactly like the Node meter, so the wire shape is\n * identical and the same ingest receives both.\n */\nimport type { Sink, BucketsReport, OpCounts } from \"../sink\";\n\n// A resource unit. The browser Firestore adapter records \"read\"; kept generic so\n// each resource stays distinct and is never merged with another.\nexport type OpType = string;\n\n// ASCII Unit Separator — a bucket/collection name never contains it, so the\n// composite key splits back cleanly.\nconst SEP = \"\\u001f\"; // ASCII Unit Separator\n\n/** key = date <US> op <US> label → count */\nconst labelBuffer = new Map<string, number>();\n/** key = date <US> op <US> hour → count */\nconst hourBuffer = new Map<string, number>();\n\nlet sink: Sink | null = null;\nlet flushIntervalMs = 60_000;\nlet onError: ((e: unknown) => void) | null = null;\nlet timer: ReturnType<typeof setInterval> | null = null;\nlet flushing = false;\nlet lifecycleBound = false;\nconst MAX_BUFFER_KEYS = 5_000;\n\nexport interface WebMeterConfig {\n sink: Sink;\n flushIntervalMs?: number;\n onError?: (e: unknown) => void;\n}\n\nexport function configureWebMeter(config: WebMeterConfig): void {\n sink = config.sink;\n if (config.flushIntervalMs && config.flushIntervalMs > 0) flushIntervalMs = config.flushIntervalMs;\n onError = config.onError ?? null;\n}\n\nconst utcDate = (): string => new Date().toISOString().slice(0, 10);\nconst utcHour = (): string => new Date().toISOString().slice(11, 13);\n\nfunction ensureLoop(): void {\n if (timer) return;\n timer = setInterval(() => void flushWeb(), flushIntervalMs);\n if (!lifecycleBound && typeof addEventListener === \"function\") {\n lifecycleBound = true;\n // The tab being hidden or torn down is the browser's \"shutdown\" — flush the\n // last window. keepalive on the sink's fetch lets it complete during unload.\n addEventListener(\"visibilitychange\", () => {\n if (typeof document !== \"undefined\" && document.visibilityState === \"hidden\") void flushWeb();\n });\n addEventListener(\"pagehide\", () => void flushWeb());\n }\n}\n\n/** Count `n` ops of `op` against `label`. Never throws. */\nexport function recordWeb(op: OpType, n: number, label: string): void {\n try {\n if (!Number.isFinite(n) || n <= 0) return;\n const date = utcDate();\n const lk = date + SEP + op + SEP + label;\n labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + n);\n const hk = date + SEP + op + SEP + utcHour();\n hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + n);\n ensureLoop();\n if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS) void flushWeb();\n } catch {\n /* metering is best-effort — never disturb the page */\n }\n}\n\nfunction add(target: Record<string, OpCounts>, key: string, op: OpType, n: number): void {\n const bag = (target[key] ??= {});\n bag[op] = (bag[op] ?? 0) + n;\n}\n\n/** Coalesce the buffer into one report per UTC day and hand each to the Sink. */\nexport async function flushWeb(): Promise<void> {\n if (flushing) return;\n if (!sink) {\n labelBuffer.clear();\n hourBuffer.clear();\n return;\n }\n if (labelBuffer.size === 0 && hourBuffer.size === 0) return;\n flushing = true;\n\n const labels = new Map(labelBuffer);\n const hours = new Map(hourBuffer);\n labelBuffer.clear();\n hourBuffer.clear();\n\n try {\n const byDate = new Map<string, BucketsReport>();\n const reportFor = (date: string): BucketsReport => {\n let r = byDate.get(date);\n if (!r) {\n r = { date, byLabel: {}, byHour: {} };\n byDate.set(date, r);\n }\n return r;\n };\n for (const [k, n] of labels) {\n const [date, op, label] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byLabel, label, op, n);\n }\n for (const [k, n] of hours) {\n const [date, op, hour] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byHour!, hour, op, n);\n }\n for (const report of byDate.values()) {\n await sink.flush(report);\n }\n } catch (e) {\n onError?.(e);\n } finally {\n flushing = false;\n }\n}\n","/**\n * web/sink — reports the browser's coalesced rollup up to Crossdeck's ingest.\n *\n * Two differences from the Node sink, both forced by the browser:\n * - it authenticates with a PUBLISHABLE key (`cd_pub_live_`), never a secret — a\n * secret key cannot live in client code. (The ingest accepts publishable keys\n * for Buckets reports the same way the analytics SDK accepts them for events.)\n * - it uses `fetch(..., { keepalive: true })` so a report fired as the tab is\n * closing still completes.\n *\n * It performs ZERO database operations — it sends a summary, it does not read.\n */\nimport type { BucketsReport, Sink } from \"../sink\";\n\nconst DEFAULT_ENDPOINT = \"https://api.cross-deck.com/v1/buckets/report\";\n\nexport interface WebReportSinkConfig {\n /** The project's `cd_pub_live_` PUBLISHABLE key. */\n apiKey: string;\n endpoint?: string;\n}\n\nexport class WebReportSink implements Sink {\n private readonly endpoint: string;\n private readonly apiKey: string;\n\n constructor(config: WebReportSinkConfig) {\n this.endpoint = config.endpoint ?? DEFAULT_ENDPOINT;\n this.apiKey = config.apiKey;\n }\n\n async flush(report: BucketsReport): Promise<void> {\n const res = await fetch(this.endpoint, {\n method: \"POST\",\n keepalive: true,\n headers: {\n \"content-type\": \"application/json\",\n authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify(report),\n });\n if (res.status !== 202) {\n throw new Error(`Buckets web report rejected: HTTP ${res.status}`);\n }\n }\n}\n","/**\n * web/context — the browser's tagging primitive.\n *\n * The Node collector rides AsyncLocalStorage to attribute reads to a bucket\n * across async fan-outs. The browser has no AsyncLocalStorage — but it doesn't\n * need one: a read (a `getDocs` call, an `onSnapshot` registration) is set up\n * SYNCHRONOUSLY, so a plain module-level \"current label\" captured at call time is\n * exact. `bucket(name, fn)` sets it for the synchronous body of `fn` and restores\n * it after — so the read inside picks up the name, and an `onSnapshot` listener\n * keeps that name for every future fire.\n */\n\nlet current: string | undefined;\n\n/** The bucket name in effect right now, or undefined (→ cascade to collection). */\nexport function currentLabel(): string | undefined {\n return current;\n}\n\n/**\n * Attribute every read SET UP inside `fn` to the bucket `name`:\n *\n * bucket(\"pulse-map\", () => onSnapshot(liveQuery, render));\n * // → that listener's reads all show as \"pulse-map\", forever\n */\nexport function bucket<T>(name: string, fn: () => T): T {\n const prev = current;\n current = name;\n try {\n return fn();\n } finally {\n current = prev;\n }\n}\n","/**\n * web/firestore — drop-in wrappers for the three Firestore client read calls.\n *\n * Swap your import source and nothing else:\n * - import { getDoc, getDocs, onSnapshot } from \"firebase/firestore\"\n * + import { getDoc, getDocs, onSnapshot } from \"@cross-deck/buckets/web\"\n *\n * Each wrapper calls the REAL Firestore function, counts the documents it\n * delivers (exactly what Firestore bills), labels it (your `bucket()` name, else\n * the collection), and returns the real result untouched. It can never change a\n * result or throw from the metering — same safety contract as the server trap.\n *\n * COUNTING:\n * - getDoc → 1 read\n * - getDocs → snapshot.size reads\n * - onSnapshot → on EVERY fire, the number of doc changes delivered\n * (first fire = all matching docs; each update = just the changed ones —\n * which is precisely what a listener is billed).\n */\nimport * as FS from \"firebase/firestore\";\nimport { recordWeb } from \"./meter\";\nimport { currentLabel } from \"./context\";\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\ntype AnyAsync = (...args: any[]) => Promise<any>;\n\n// Call the real reads through the namespace + loose aliases: a function missing on\n// an older `firebase` (e.g. getAggregateFromServer pre-v10) is simply `undefined`\n// rather than a hard import error, and the wrappers pass arguments through verbatim.\nconst rawGetDoc = FS.getDoc as AnyAsync;\nconst rawGetDocs = FS.getDocs as AnyAsync;\nconst rawOnSnapshot = FS.onSnapshot as (...args: any[]) => any;\nconst rawGetDocFromServer = (FS as any).getDocFromServer as AnyAsync | undefined;\nconst rawGetDocsFromServer = (FS as any).getDocsFromServer as AnyAsync | undefined;\nconst rawGetDocFromCache = (FS as any).getDocFromCache as AnyAsync | undefined;\nconst rawGetDocsFromCache = (FS as any).getDocsFromCache as AnyAsync | undefined;\nconst rawGetCountFromServer = (FS as any).getCountFromServer as AnyAsync | undefined;\nconst rawGetAggregateFromServer = (FS as any).getAggregateFromServer as AnyAsync | undefined;\n\n/** Best-effort collection label from a ref/query. PURE; never throws. */\nfunction collLabel(ref: any): string {\n try {\n const path: string =\n (typeof ref?.path === \"string\" && ref.path) ||\n (ref?._query?.path?.segments?.join?.(\"/\") ?? \"\") ||\n \"\";\n if (path) {\n const segs = path.split(\"/\").filter(Boolean);\n // even segment count → document path (…/coll/id); odd → collection.\n const coll = segs.length % 2 === 0 ? segs[segs.length - 2] : segs[segs.length - 1];\n return coll ? `col:${coll}` : \"uncategorized\";\n }\n const segs = ref?._query?.path?.segments;\n if (Array.isArray(segs) && segs.length) return `col:${segs[segs.length - 1]}`;\n } catch {\n /* never throw from labelling */\n }\n return \"uncategorized\";\n}\n\nfunction meter(label: string, n: number): void {\n try {\n recordWeb(\"read\", n, label);\n } catch {\n /* best-effort */\n }\n}\n\n/** Count the docs a snapshot delivers: a query's changed docs, or 1 for a doc. */\nfunction countSnap(snap: any): number {\n try {\n if (typeof snap?.docChanges === \"function\") return snap.docChanges().length;\n } catch {\n /* fall through */\n }\n return 1;\n}\n\nexport function getDoc(ref: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(ref);\n return rawGetDoc(ref, ...rest).then((snap: any) => {\n meter(label, 1);\n return snap;\n });\n}\n\nexport function getDocs(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetDocs(query, ...rest).then((snap: any) => {\n meter(label, typeof snap?.size === \"number\" ? Math.max(snap.size, 1) : 1);\n return snap;\n });\n}\n\n/**\n * onSnapshot has several overloads — (ref, observer), (ref, onNext, onError,\n * onComplete), and either of those with a leading SnapshotListenOptions. We find\n * the next-handler wherever it is (a function or `observer.next`) and wrap it to\n * count on each fire, leaving every other argument exactly as passed.\n */\nexport function onSnapshot(ref: any, ...args: any[]): any {\n const label = currentLabel() ?? collLabel(ref);\n const wrapNext = (fn: any) =>\n typeof fn === \"function\"\n ? (snap: any) => {\n meter(label, countSnap(snap));\n return fn(snap);\n }\n : fn;\n\n const out = args.slice();\n // Leading options object (not a function, not an observer): skip it.\n let i = 0;\n if (out[0] && typeof out[0] !== \"function\" && !(\"next\" in out[0])) i = 1;\n\n if (out[i] && typeof out[i] === \"object\" && \"next\" in out[i]) {\n // observer form: clone with wrapped next\n out[i] = { ...out[i], next: wrapNext(out[i].next) };\n } else if (typeof out[i] === \"function\") {\n // callback form: wrap onNext (the first function)\n out[i] = wrapNext(out[i]);\n }\n\n return rawOnSnapshot(ref, ...out);\n}\n\n// ── Force-from-server reads — bill exactly like getDoc / getDocs ──────────────\nexport function getDocFromServer(ref: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(ref);\n return rawGetDocFromServer!(ref, ...rest).then((snap: any) => {\n meter(label, 1);\n return snap;\n });\n}\nexport function getDocsFromServer(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetDocsFromServer!(query, ...rest).then((snap: any) => {\n meter(label, typeof snap?.size === \"number\" ? Math.max(snap.size, 1) : 1);\n return snap;\n });\n}\n\n// ── Cache-only reads — NOT billed, so pass through and count NOTHING ──────────\n// (Counting these would over-report; a cache hit costs you nothing.)\nexport function getDocFromCache(ref: any, ...rest: any[]): Promise<any> {\n return rawGetDocFromCache!(ref, ...rest);\n}\nexport function getDocsFromCache(query: any, ...rest: any[]): Promise<any> {\n return rawGetDocsFromCache!(query, ...rest);\n}\n\n// ── Aggregation — count() / sum() / average() ────────────────────────────────\n// Honest estimate: ceil(count / 1000), min 1 — Firestore never exposes the exact\n// index-entry count. Same model as the server trap, so the numbers reconcile.\nfunction aggReads(snap: any): number {\n const count = typeof snap?.data?.()?.count === \"number\" ? snap.data().count : 0;\n return Math.max(1, Math.ceil(count / 1000));\n}\nexport function getCountFromServer(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetCountFromServer!(query, ...rest).then((snap: any) => {\n meter(label, aggReads(snap));\n return snap;\n });\n}\nexport function getAggregateFromServer(query: any, spec: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetAggregateFromServer!(query, spec, ...rest).then((snap: any) => {\n meter(label, aggReads(snap));\n return snap;\n });\n}\n","/**\n * @cross-deck/buckets/web — the BROWSER collector.\n *\n * Most Firebase apps read straight from the browser (live `onSnapshot`\n * listeners, `getDocs`, `getDoc`) — reads billed to your project that a\n * server-side collector can never see. This adapter closes that hole.\n *\n * Setup (two lines + one import swap):\n *\n * import { initBucketsWeb, bucket } from \"@cross-deck/buckets/web\";\n * import { getDoc, getDocs, onSnapshot } from \"@cross-deck/buckets/web\"; // was \"firebase/firestore\"\n *\n * initBucketsWeb({ apiKey: \"cd_pub_live_…\" }); // your PUBLISHABLE key\n *\n * bucket(\"pulse-map\", () => onSnapshot(liveQuery, render));\n *\n * Every read those wrappers see is counted, labelled, and reported up the same\n * ingest pipe as the server collector — so the dashboard shows server AND browser\n * reads side by side.\n */\nimport { configureWebMeter, flushWeb, type WebMeterConfig } from \"./meter\";\nimport { WebReportSink } from \"./sink\";\n\nexport interface InitWebOptions {\n /** The project's `cd_pub_live_` PUBLISHABLE key (safe in client code). */\n apiKey: string;\n /** Override the report endpoint (defaults to Crossdeck's ingest). */\n endpoint?: string;\n /** How often to flush coalesced counts (ms). Default 60_000. */\n flushIntervalMs?: number;\n /** Notified when a flush fails, so a dropped window is never silent. */\n onError?: WebMeterConfig[\"onError\"];\n}\n\n/** Configure the browser collector once, at app start. */\nexport function initBucketsWeb(options: InitWebOptions): void {\n const sink = new WebReportSink({ apiKey: options.apiKey, endpoint: options.endpoint });\n configureWebMeter({ sink, flushIntervalMs: options.flushIntervalMs, onError: options.onError });\n}\n\n// The tagging verb + the metered read wrappers (swap your firebase/firestore\n// import for these). Cache-only reads pass through uncounted (they aren't billed).\nexport { bucket } from \"./context\";\nexport {\n getDoc,\n getDocs,\n onSnapshot,\n getDocFromServer,\n getDocsFromServer,\n getDocFromCache,\n getDocsFromCache,\n getCountFromServer,\n getAggregateFromServer,\n} from \"./firestore\";\nexport { flushWeb as flush } from \"./meter\";\n\n// The sink seam — for self-hosting the browser rollups instead of reporting to Crossdeck.\nexport { WebReportSink, type WebReportSinkConfig } from \"./sink\";\nexport type { BucketsReport, OpCounts, Sink } from \"../sink\";\n"]}
package/dist/web.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/web/meter.ts","../src/web/sink.ts","../src/web/context.ts","../src/web/firestore.ts","../src/web/index.ts"],"names":["segs","getDoc","getDocs","onSnapshot","getDocFromServer","getDocsFromServer","getDocFromCache","getDocsFromCache","getCountFromServer","getAggregateFromServer","sink"],"mappings":";;;AAiBA,IAAM,GAAA,GAAM,GAAA;AAGZ,IAAM,WAAA,uBAAkB,GAAA,EAAoB;AAE5C,IAAM,UAAA,uBAAiB,GAAA,EAAoB;AAE3C,IAAI,IAAA,GAAoB,IAAA;AACxB,IAAI,eAAA,GAAkB,GAAA;AACtB,IAAI,OAAA,GAAyC,IAAA;AAC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AI,CAAC,CAAA;AAC3B,IAAA,OAAO,IAAA;AAAA,EACT,CAAC,CAAA;AACH;AACO,SAASC,uBAAAA,CAAuB,KAAA,EAAY,IAAA,EAAA,GAAc,IAAA,EAA2B;AAC1F,EAAA,MAAM,KAAA,GAAQ,YAAA,EAAa,IAAK,SAAA,CAAU,KAAK,CAAA;AAC/C,EAAA,OAAO,yBAAA,CAA2B,OAAO,IAAA,EAAM,GAAG,IAAI,CAAA,CAAE,IAAA,CAAK,CAAC,IAAA,KAAc;AAC1E,IAAA,KAAA,CAAM,KAAA,EAAO,QAAA,CAAS,IAAI,CAAC,CAAA;AAC3B,IAAA,OAAO,IAAA;AAAA,EACT,CAAC,CAAA;AACH;;;ACxIO,SAAS,eAAe,OAAA,EAA+B;AAC5D,EAAA,MAAMC,KAAAA,GAAO,IAAI,aAAA,CAAc,EAAE,MAAA,EAAQ,QAAQ,MAAA,EAAQ,QAAA,EAAU,OAAA,CAAQ,QAAA,EAAU,CAAA;AACrF,EAAA,iBAAA,CAAkB,EAAE,MAAAA,KAAAA,EAAM,eAAA,EAAiB,QAAQ,eAAA,EAAiB,OAAA,EAAS,OAAA,CAAQ,OAAA,EAAS,CAAA;AAChG","file":"web.mjs","sourcesContent":["/**\n * web/meter — the browser read meter. Same contract as the Node meter (count in\n * memory, flush ~1/min, never throw into the app), adapted to the browser:\n *\n * - no AsyncLocalStorage — labels come from web/context (synchronous),\n * - \"shutdown\" is the tab going hidden/closed, so we also flush on\n * visibilitychange→hidden and pagehide (a `fetch(..., {keepalive:true})`\n * survives the unload),\n * - it talks to a Sink exactly like the Node meter, so the wire shape is\n * identical and the same ingest receives both.\n */\nimport type { Sink, BucketsReport, OpCounts } from \"../sink\";\n\nexport type OpType = \"read\" | \"write\" | \"delete\";\n\n// ASCII Unit Separator — a bucket/collection name never contains it, so the\n// composite key splits back cleanly.\nconst SEP = \"\\u001f\"; // ASCII Unit Separator\n\n/** key = date <US> op <US> label → count */\nconst labelBuffer = new Map<string, number>();\n/** key = date <US> op <US> hour → count */\nconst hourBuffer = new Map<string, number>();\n\nlet sink: Sink | null = null;\nlet flushIntervalMs = 60_000;\nlet onError: ((e: unknown) => void) | null = null;\nlet timer: ReturnType<typeof setInterval> | null = null;\nlet flushing = false;\nlet lifecycleBound = false;\nconst MAX_BUFFER_KEYS = 5_000;\n\nexport interface WebMeterConfig {\n sink: Sink;\n flushIntervalMs?: number;\n onError?: (e: unknown) => void;\n}\n\nexport function configureWebMeter(config: WebMeterConfig): void {\n sink = config.sink;\n if (config.flushIntervalMs && config.flushIntervalMs > 0) flushIntervalMs = config.flushIntervalMs;\n onError = config.onError ?? null;\n}\n\nconst utcDate = (): string => new Date().toISOString().slice(0, 10);\nconst utcHour = (): string => new Date().toISOString().slice(11, 13);\n\nfunction ensureLoop(): void {\n if (timer) return;\n timer = setInterval(() => void flushWeb(), flushIntervalMs);\n if (!lifecycleBound && typeof addEventListener === \"function\") {\n lifecycleBound = true;\n // The tab being hidden or torn down is the browser's \"shutdown\" — flush the\n // last window. keepalive on the sink's fetch lets it complete during unload.\n addEventListener(\"visibilitychange\", () => {\n if (typeof document !== \"undefined\" && document.visibilityState === \"hidden\") void flushWeb();\n });\n addEventListener(\"pagehide\", () => void flushWeb());\n }\n}\n\n/** Count `n` ops of `op` against `label`. Never throws. */\nexport function recordWeb(op: OpType, n: number, label: string): void {\n try {\n if (!Number.isFinite(n) || n <= 0) return;\n const date = utcDate();\n const lk = date + SEP + op + SEP + label;\n labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + n);\n const hk = date + SEP + op + SEP + utcHour();\n hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + n);\n ensureLoop();\n if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS) void flushWeb();\n } catch {\n /* metering is best-effort — never disturb the page */\n }\n}\n\nfunction add(target: Record<string, OpCounts>, key: string, op: OpType, n: number): void {\n const bag = (target[key] ??= {});\n bag[op] = (bag[op] ?? 0) + n;\n}\n\n/** Coalesce the buffer into one report per UTC day and hand each to the Sink. */\nexport async function flushWeb(): Promise<void> {\n if (flushing) return;\n if (!sink) {\n labelBuffer.clear();\n hourBuffer.clear();\n return;\n }\n if (labelBuffer.size === 0 && hourBuffer.size === 0) return;\n flushing = true;\n\n const labels = new Map(labelBuffer);\n const hours = new Map(hourBuffer);\n labelBuffer.clear();\n hourBuffer.clear();\n\n try {\n const byDate = new Map<string, BucketsReport>();\n const reportFor = (date: string): BucketsReport => {\n let r = byDate.get(date);\n if (!r) {\n r = { date, byLabel: {}, byHour: {} };\n byDate.set(date, r);\n }\n return r;\n };\n for (const [k, n] of labels) {\n const [date, op, label] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byLabel, label, op, n);\n }\n for (const [k, n] of hours) {\n const [date, op, hour] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byHour!, hour, op, n);\n }\n for (const report of byDate.values()) {\n await sink.flush(report);\n }\n } catch (e) {\n onError?.(e);\n } finally {\n flushing = false;\n }\n}\n","/**\n * web/sink — reports the browser's coalesced rollup up to Crossdeck's ingest.\n *\n * Two differences from the Node sink, both forced by the browser:\n * - it authenticates with a PUBLISHABLE key (`cd_pub_live_`), never a secret — a\n * secret key cannot live in client code. (The ingest accepts publishable keys\n * for Buckets reports the same way the analytics SDK accepts them for events.)\n * - it uses `fetch(..., { keepalive: true })` so a report fired as the tab is\n * closing still completes.\n *\n * It performs ZERO database operations — it sends a summary, it does not read.\n */\nimport type { BucketsReport, Sink } from \"../sink\";\n\nconst DEFAULT_ENDPOINT = \"https://api.cross-deck.com/v1/buckets/report\";\n\nexport interface WebReportSinkConfig {\n /** The project's `cd_pub_live_` PUBLISHABLE key. */\n apiKey: string;\n endpoint?: string;\n}\n\nexport class WebReportSink implements Sink {\n private readonly endpoint: string;\n private readonly apiKey: string;\n\n constructor(config: WebReportSinkConfig) {\n this.endpoint = config.endpoint ?? DEFAULT_ENDPOINT;\n this.apiKey = config.apiKey;\n }\n\n async flush(report: BucketsReport): Promise<void> {\n const res = await fetch(this.endpoint, {\n method: \"POST\",\n keepalive: true,\n headers: {\n \"content-type\": \"application/json\",\n authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify(report),\n });\n if (res.status !== 202) {\n throw new Error(`Buckets web report rejected: HTTP ${res.status}`);\n }\n }\n}\n","/**\n * web/context — the browser's tagging primitive.\n *\n * The Node collector rides AsyncLocalStorage to attribute reads to a bucket\n * across async fan-outs. The browser has no AsyncLocalStorage — but it doesn't\n * need one: a read (a `getDocs` call, an `onSnapshot` registration) is set up\n * SYNCHRONOUSLY, so a plain module-level \"current label\" captured at call time is\n * exact. `bucket(name, fn)` sets it for the synchronous body of `fn` and restores\n * it after — so the read inside picks up the name, and an `onSnapshot` listener\n * keeps that name for every future fire.\n */\n\nlet current: string | undefined;\n\n/** The bucket name in effect right now, or undefined (→ cascade to collection). */\nexport function currentLabel(): string | undefined {\n return current;\n}\n\n/**\n * Attribute every read SET UP inside `fn` to the bucket `name`:\n *\n * bucket(\"pulse-map\", () => onSnapshot(liveQuery, render));\n * // → that listener's reads all show as \"pulse-map\", forever\n */\nexport function bucket<T>(name: string, fn: () => T): T {\n const prev = current;\n current = name;\n try {\n return fn();\n } finally {\n current = prev;\n }\n}\n","/**\n * web/firestore — drop-in wrappers for the three Firestore client read calls.\n *\n * Swap your import source and nothing else:\n * - import { getDoc, getDocs, onSnapshot } from \"firebase/firestore\"\n * + import { getDoc, getDocs, onSnapshot } from \"@cross-deck/buckets/web\"\n *\n * Each wrapper calls the REAL Firestore function, counts the documents it\n * delivers (exactly what Firestore bills), labels it (your `bucket()` name, else\n * the collection), and returns the real result untouched. It can never change a\n * result or throw from the metering — same safety contract as the server trap.\n *\n * COUNTING:\n * - getDoc → 1 read\n * - getDocs → snapshot.size reads\n * - onSnapshot → on EVERY fire, the number of doc changes delivered\n * (first fire = all matching docs; each update = just the changed ones —\n * which is precisely what a listener is billed).\n */\nimport * as FS from \"firebase/firestore\";\nimport { recordWeb } from \"./meter\";\nimport { currentLabel } from \"./context\";\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\ntype AnyAsync = (...args: any[]) => Promise<any>;\n\n// Call the real reads through the namespace + loose aliases: a function missing on\n// an older `firebase` (e.g. getAggregateFromServer pre-v10) is simply `undefined`\n// rather than a hard import error, and the wrappers pass arguments through verbatim.\nconst rawGetDoc = FS.getDoc as AnyAsync;\nconst rawGetDocs = FS.getDocs as AnyAsync;\nconst rawOnSnapshot = FS.onSnapshot as (...args: any[]) => any;\nconst rawGetDocFromServer = (FS as any).getDocFromServer as AnyAsync | undefined;\nconst rawGetDocsFromServer = (FS as any).getDocsFromServer as AnyAsync | undefined;\nconst rawGetDocFromCache = (FS as any).getDocFromCache as AnyAsync | undefined;\nconst rawGetDocsFromCache = (FS as any).getDocsFromCache as AnyAsync | undefined;\nconst rawGetCountFromServer = (FS as any).getCountFromServer as AnyAsync | undefined;\nconst rawGetAggregateFromServer = (FS as any).getAggregateFromServer as AnyAsync | undefined;\n\n/** Best-effort collection label from a ref/query. PURE; never throws. */\nfunction collLabel(ref: any): string {\n try {\n const path: string =\n (typeof ref?.path === \"string\" && ref.path) ||\n (ref?._query?.path?.segments?.join?.(\"/\") ?? \"\") ||\n \"\";\n if (path) {\n const segs = path.split(\"/\").filter(Boolean);\n // even segment count → document path (…/coll/id); odd → collection.\n const coll = segs.length % 2 === 0 ? segs[segs.length - 2] : segs[segs.length - 1];\n return coll ? `col:${coll}` : \"uncategorized\";\n }\n const segs = ref?._query?.path?.segments;\n if (Array.isArray(segs) && segs.length) return `col:${segs[segs.length - 1]}`;\n } catch {\n /* never throw from labelling */\n }\n return \"uncategorized\";\n}\n\nfunction meter(label: string, n: number): void {\n try {\n recordWeb(\"read\", n, label);\n } catch {\n /* best-effort */\n }\n}\n\n/** Count the docs a snapshot delivers: a query's changed docs, or 1 for a doc. */\nfunction countSnap(snap: any): number {\n try {\n if (typeof snap?.docChanges === \"function\") return snap.docChanges().length;\n } catch {\n /* fall through */\n }\n return 1;\n}\n\nexport function getDoc(ref: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(ref);\n return rawGetDoc(ref, ...rest).then((snap: any) => {\n meter(label, 1);\n return snap;\n });\n}\n\nexport function getDocs(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetDocs(query, ...rest).then((snap: any) => {\n meter(label, typeof snap?.size === \"number\" ? Math.max(snap.size, 1) : 1);\n return snap;\n });\n}\n\n/**\n * onSnapshot has several overloads — (ref, observer), (ref, onNext, onError,\n * onComplete), and either of those with a leading SnapshotListenOptions. We find\n * the next-handler wherever it is (a function or `observer.next`) and wrap it to\n * count on each fire, leaving every other argument exactly as passed.\n */\nexport function onSnapshot(ref: any, ...args: any[]): any {\n const label = currentLabel() ?? collLabel(ref);\n const wrapNext = (fn: any) =>\n typeof fn === \"function\"\n ? (snap: any) => {\n meter(label, countSnap(snap));\n return fn(snap);\n }\n : fn;\n\n const out = args.slice();\n // Leading options object (not a function, not an observer): skip it.\n let i = 0;\n if (out[0] && typeof out[0] !== \"function\" && !(\"next\" in out[0])) i = 1;\n\n if (out[i] && typeof out[i] === \"object\" && \"next\" in out[i]) {\n // observer form: clone with wrapped next\n out[i] = { ...out[i], next: wrapNext(out[i].next) };\n } else if (typeof out[i] === \"function\") {\n // callback form: wrap onNext (the first function)\n out[i] = wrapNext(out[i]);\n }\n\n return rawOnSnapshot(ref, ...out);\n}\n\n// ── Force-from-server reads — bill exactly like getDoc / getDocs ──────────────\nexport function getDocFromServer(ref: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(ref);\n return rawGetDocFromServer!(ref, ...rest).then((snap: any) => {\n meter(label, 1);\n return snap;\n });\n}\nexport function getDocsFromServer(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetDocsFromServer!(query, ...rest).then((snap: any) => {\n meter(label, typeof snap?.size === \"number\" ? Math.max(snap.size, 1) : 1);\n return snap;\n });\n}\n\n// ── Cache-only reads — NOT billed, so pass through and count NOTHING ──────────\n// (Counting these would over-report; a cache hit costs you nothing.)\nexport function getDocFromCache(ref: any, ...rest: any[]): Promise<any> {\n return rawGetDocFromCache!(ref, ...rest);\n}\nexport function getDocsFromCache(query: any, ...rest: any[]): Promise<any> {\n return rawGetDocsFromCache!(query, ...rest);\n}\n\n// ── Aggregation — count() / sum() / average() ────────────────────────────────\n// Honest estimate: ceil(count / 1000), min 1 — Firestore never exposes the exact\n// index-entry count. Same model as the server trap, so the numbers reconcile.\nfunction aggReads(snap: any): number {\n const count = typeof snap?.data?.()?.count === \"number\" ? snap.data().count : 0;\n return Math.max(1, Math.ceil(count / 1000));\n}\nexport function getCountFromServer(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetCountFromServer!(query, ...rest).then((snap: any) => {\n meter(label, aggReads(snap));\n return snap;\n });\n}\nexport function getAggregateFromServer(query: any, spec: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetAggregateFromServer!(query, spec, ...rest).then((snap: any) => {\n meter(label, aggReads(snap));\n return snap;\n });\n}\n","/**\n * @cross-deck/buckets/web — the BROWSER collector.\n *\n * Most Firebase apps read straight from the browser (live `onSnapshot`\n * listeners, `getDocs`, `getDoc`) — reads billed to your project that a\n * server-side collector can never see. This adapter closes that hole.\n *\n * Setup (two lines + one import swap):\n *\n * import { initBucketsWeb, bucket } from \"@cross-deck/buckets/web\";\n * import { getDoc, getDocs, onSnapshot } from \"@cross-deck/buckets/web\"; // was \"firebase/firestore\"\n *\n * initBucketsWeb({ apiKey: \"cd_pub_live_…\" }); // your PUBLISHABLE key\n *\n * bucket(\"pulse-map\", () => onSnapshot(liveQuery, render));\n *\n * Every read those wrappers see is counted, labelled, and reported up the same\n * ingest pipe as the server collector — so the dashboard shows server AND browser\n * reads side by side.\n */\nimport { configureWebMeter, flushWeb, type WebMeterConfig } from \"./meter\";\nimport { WebReportSink } from \"./sink\";\n\nexport interface InitWebOptions {\n /** The project's `cd_pub_live_` PUBLISHABLE key (safe in client code). */\n apiKey: string;\n /** Override the report endpoint (defaults to Crossdeck's ingest). */\n endpoint?: string;\n /** How often to flush coalesced counts (ms). Default 60_000. */\n flushIntervalMs?: number;\n /** Notified when a flush fails, so a dropped window is never silent. */\n onError?: WebMeterConfig[\"onError\"];\n}\n\n/** Configure the browser collector once, at app start. */\nexport function initBucketsWeb(options: InitWebOptions): void {\n const sink = new WebReportSink({ apiKey: options.apiKey, endpoint: options.endpoint });\n configureWebMeter({ sink, flushIntervalMs: options.flushIntervalMs, onError: options.onError });\n}\n\n// The tagging verb + the metered read wrappers (swap your firebase/firestore\n// import for these). Cache-only reads pass through uncounted (they aren't billed).\nexport { bucket } from \"./context\";\nexport {\n getDoc,\n getDocs,\n onSnapshot,\n getDocFromServer,\n getDocsFromServer,\n getDocFromCache,\n getDocsFromCache,\n getCountFromServer,\n getAggregateFromServer,\n} from \"./firestore\";\nexport { flushWeb as flush } from \"./meter\";\n\n// The sink seam — for self-hosting the browser rollups instead of reporting to Crossdeck.\nexport { WebReportSink, type WebReportSinkConfig } from \"./sink\";\nexport type { BucketsReport, OpCounts, Sink } from \"../sink\";\n"]}
1
+ {"version":3,"sources":["../src/web/meter.ts","../src/web/sink.ts","../src/web/context.ts","../src/web/firestore.ts","../src/web/index.ts"],"names":["segs","getDoc","getDocs","onSnapshot","getDocFromServer","getDocsFromServer","getDocFromCache","getDocsFromCache","getCountFromServer","getAggregateFromServer","sink"],"mappings":";;;AAmBA,IAAM,GAAA,GAAM,GAAA;AAGZ,IAAM,WAAA,uBAAkB,GAAA,EAAoB;AAE5C,IAAM,UAAA,uBAAiB,GAAA,EAAoB;AAE3C,IAAI,IAAA,GAAoB,IAAA;AACxB,IAAI,eAAA,GAAkB,GAAA;AACtB,IAAI,OAAA,GAAyC,IAAA;AAC7C,IAAI,KAAA,GAA+C,IAAA;AACnD,IAAI,QAAA,GAAW,KAAA;AACf,IAAI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ACO,SAASC,uBAAAA,CAAuB,KAAA,EAAY,IAAA,EAAA,GAAc,IAAA,EAA2B;AAC1F,EAAA,MAAM,KAAA,GAAQ,YAAA,EAAa,IAAK,SAAA,CAAU,KAAK,CAAA;AAC/C,EAAA,OAAO,yBAAA,CAA2B,OAAO,IAAA,EAAM,GAAG,IAAI,CAAA,CAAE,IAAA,CAAK,CAAC,IAAA,KAAc;AAC1E,IAAA,KAAA,CAAM,KAAA,EAAO,QAAA,CAAS,IAAI,CAAC,CAAA;AAC3B,IAAA,OAAO,IAAA;AAAA,EACT,CAAC,CAAA;AACH;;;ACxIO,SAAS,eAAe,OAAA,EAA+B;AAC5D,EAAA,MAAMC,KAAAA,GAAO,IAAI,aAAA,CAAc,EAAE,MAAA,EAAQ,QAAQ,MAAA,EAAQ,QAAA,EAAU,OAAA,CAAQ,QAAA,EAAU,CAAA;AACrF,EAAA,iBAAA,CAAkB,EAAE,MAAAA,KAAAA,EAAM,eAAA,EAAiB,QAAQ,eAAA,EAAiB,OAAA,EAAS,OAAA,CAAQ,OAAA,EAAS,CAAA;AAChG","file":"web.mjs","sourcesContent":["/**\n * web/meter — the browser read meter. Same contract as the Node meter (count in\n * memory, flush ~1/min, never throw into the app), adapted to the browser:\n *\n * - no AsyncLocalStorage — labels come from web/context (synchronous),\n * - \"shutdown\" is the tab going hidden/closed, so we also flush on\n * visibilitychange→hidden and pagehide (a `fetch(..., {keepalive:true})`\n * survives the unload),\n * - it talks to a Sink exactly like the Node meter, so the wire shape is\n * identical and the same ingest receives both.\n */\nimport type { Sink, BucketsReport, OpCounts } from \"../sink\";\n\n// A resource unit. The browser Firestore adapter records \"read\"; kept generic so\n// each resource stays distinct and is never merged with another.\nexport type OpType = string;\n\n// ASCII Unit Separator — a bucket/collection name never contains it, so the\n// composite key splits back cleanly.\nconst SEP = \"\\u001f\"; // ASCII Unit Separator\n\n/** key = date <US> op <US> label → count */\nconst labelBuffer = new Map<string, number>();\n/** key = date <US> op <US> hour → count */\nconst hourBuffer = new Map<string, number>();\n\nlet sink: Sink | null = null;\nlet flushIntervalMs = 60_000;\nlet onError: ((e: unknown) => void) | null = null;\nlet timer: ReturnType<typeof setInterval> | null = null;\nlet flushing = false;\nlet lifecycleBound = false;\nconst MAX_BUFFER_KEYS = 5_000;\n\nexport interface WebMeterConfig {\n sink: Sink;\n flushIntervalMs?: number;\n onError?: (e: unknown) => void;\n}\n\nexport function configureWebMeter(config: WebMeterConfig): void {\n sink = config.sink;\n if (config.flushIntervalMs && config.flushIntervalMs > 0) flushIntervalMs = config.flushIntervalMs;\n onError = config.onError ?? null;\n}\n\nconst utcDate = (): string => new Date().toISOString().slice(0, 10);\nconst utcHour = (): string => new Date().toISOString().slice(11, 13);\n\nfunction ensureLoop(): void {\n if (timer) return;\n timer = setInterval(() => void flushWeb(), flushIntervalMs);\n if (!lifecycleBound && typeof addEventListener === \"function\") {\n lifecycleBound = true;\n // The tab being hidden or torn down is the browser's \"shutdown\" — flush the\n // last window. keepalive on the sink's fetch lets it complete during unload.\n addEventListener(\"visibilitychange\", () => {\n if (typeof document !== \"undefined\" && document.visibilityState === \"hidden\") void flushWeb();\n });\n addEventListener(\"pagehide\", () => void flushWeb());\n }\n}\n\n/** Count `n` ops of `op` against `label`. Never throws. */\nexport function recordWeb(op: OpType, n: number, label: string): void {\n try {\n if (!Number.isFinite(n) || n <= 0) return;\n const date = utcDate();\n const lk = date + SEP + op + SEP + label;\n labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + n);\n const hk = date + SEP + op + SEP + utcHour();\n hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + n);\n ensureLoop();\n if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS) void flushWeb();\n } catch {\n /* metering is best-effort — never disturb the page */\n }\n}\n\nfunction add(target: Record<string, OpCounts>, key: string, op: OpType, n: number): void {\n const bag = (target[key] ??= {});\n bag[op] = (bag[op] ?? 0) + n;\n}\n\n/** Coalesce the buffer into one report per UTC day and hand each to the Sink. */\nexport async function flushWeb(): Promise<void> {\n if (flushing) return;\n if (!sink) {\n labelBuffer.clear();\n hourBuffer.clear();\n return;\n }\n if (labelBuffer.size === 0 && hourBuffer.size === 0) return;\n flushing = true;\n\n const labels = new Map(labelBuffer);\n const hours = new Map(hourBuffer);\n labelBuffer.clear();\n hourBuffer.clear();\n\n try {\n const byDate = new Map<string, BucketsReport>();\n const reportFor = (date: string): BucketsReport => {\n let r = byDate.get(date);\n if (!r) {\n r = { date, byLabel: {}, byHour: {} };\n byDate.set(date, r);\n }\n return r;\n };\n for (const [k, n] of labels) {\n const [date, op, label] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byLabel, label, op, n);\n }\n for (const [k, n] of hours) {\n const [date, op, hour] = k.split(SEP) as [string, OpType, string];\n add(reportFor(date).byHour!, hour, op, n);\n }\n for (const report of byDate.values()) {\n await sink.flush(report);\n }\n } catch (e) {\n onError?.(e);\n } finally {\n flushing = false;\n }\n}\n","/**\n * web/sink — reports the browser's coalesced rollup up to Crossdeck's ingest.\n *\n * Two differences from the Node sink, both forced by the browser:\n * - it authenticates with a PUBLISHABLE key (`cd_pub_live_`), never a secret — a\n * secret key cannot live in client code. (The ingest accepts publishable keys\n * for Buckets reports the same way the analytics SDK accepts them for events.)\n * - it uses `fetch(..., { keepalive: true })` so a report fired as the tab is\n * closing still completes.\n *\n * It performs ZERO database operations — it sends a summary, it does not read.\n */\nimport type { BucketsReport, Sink } from \"../sink\";\n\nconst DEFAULT_ENDPOINT = \"https://api.cross-deck.com/v1/buckets/report\";\n\nexport interface WebReportSinkConfig {\n /** The project's `cd_pub_live_` PUBLISHABLE key. */\n apiKey: string;\n endpoint?: string;\n}\n\nexport class WebReportSink implements Sink {\n private readonly endpoint: string;\n private readonly apiKey: string;\n\n constructor(config: WebReportSinkConfig) {\n this.endpoint = config.endpoint ?? DEFAULT_ENDPOINT;\n this.apiKey = config.apiKey;\n }\n\n async flush(report: BucketsReport): Promise<void> {\n const res = await fetch(this.endpoint, {\n method: \"POST\",\n keepalive: true,\n headers: {\n \"content-type\": \"application/json\",\n authorization: `Bearer ${this.apiKey}`,\n },\n body: JSON.stringify(report),\n });\n if (res.status !== 202) {\n throw new Error(`Buckets web report rejected: HTTP ${res.status}`);\n }\n }\n}\n","/**\n * web/context — the browser's tagging primitive.\n *\n * The Node collector rides AsyncLocalStorage to attribute reads to a bucket\n * across async fan-outs. The browser has no AsyncLocalStorage — but it doesn't\n * need one: a read (a `getDocs` call, an `onSnapshot` registration) is set up\n * SYNCHRONOUSLY, so a plain module-level \"current label\" captured at call time is\n * exact. `bucket(name, fn)` sets it for the synchronous body of `fn` and restores\n * it after — so the read inside picks up the name, and an `onSnapshot` listener\n * keeps that name for every future fire.\n */\n\nlet current: string | undefined;\n\n/** The bucket name in effect right now, or undefined (→ cascade to collection). */\nexport function currentLabel(): string | undefined {\n return current;\n}\n\n/**\n * Attribute every read SET UP inside `fn` to the bucket `name`:\n *\n * bucket(\"pulse-map\", () => onSnapshot(liveQuery, render));\n * // → that listener's reads all show as \"pulse-map\", forever\n */\nexport function bucket<T>(name: string, fn: () => T): T {\n const prev = current;\n current = name;\n try {\n return fn();\n } finally {\n current = prev;\n }\n}\n","/**\n * web/firestore — drop-in wrappers for the three Firestore client read calls.\n *\n * Swap your import source and nothing else:\n * - import { getDoc, getDocs, onSnapshot } from \"firebase/firestore\"\n * + import { getDoc, getDocs, onSnapshot } from \"@cross-deck/buckets/web\"\n *\n * Each wrapper calls the REAL Firestore function, counts the documents it\n * delivers (exactly what Firestore bills), labels it (your `bucket()` name, else\n * the collection), and returns the real result untouched. It can never change a\n * result or throw from the metering — same safety contract as the server trap.\n *\n * COUNTING:\n * - getDoc → 1 read\n * - getDocs → snapshot.size reads\n * - onSnapshot → on EVERY fire, the number of doc changes delivered\n * (first fire = all matching docs; each update = just the changed ones —\n * which is precisely what a listener is billed).\n */\nimport * as FS from \"firebase/firestore\";\nimport { recordWeb } from \"./meter\";\nimport { currentLabel } from \"./context\";\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\ntype AnyAsync = (...args: any[]) => Promise<any>;\n\n// Call the real reads through the namespace + loose aliases: a function missing on\n// an older `firebase` (e.g. getAggregateFromServer pre-v10) is simply `undefined`\n// rather than a hard import error, and the wrappers pass arguments through verbatim.\nconst rawGetDoc = FS.getDoc as AnyAsync;\nconst rawGetDocs = FS.getDocs as AnyAsync;\nconst rawOnSnapshot = FS.onSnapshot as (...args: any[]) => any;\nconst rawGetDocFromServer = (FS as any).getDocFromServer as AnyAsync | undefined;\nconst rawGetDocsFromServer = (FS as any).getDocsFromServer as AnyAsync | undefined;\nconst rawGetDocFromCache = (FS as any).getDocFromCache as AnyAsync | undefined;\nconst rawGetDocsFromCache = (FS as any).getDocsFromCache as AnyAsync | undefined;\nconst rawGetCountFromServer = (FS as any).getCountFromServer as AnyAsync | undefined;\nconst rawGetAggregateFromServer = (FS as any).getAggregateFromServer as AnyAsync | undefined;\n\n/** Best-effort collection label from a ref/query. PURE; never throws. */\nfunction collLabel(ref: any): string {\n try {\n const path: string =\n (typeof ref?.path === \"string\" && ref.path) ||\n (ref?._query?.path?.segments?.join?.(\"/\") ?? \"\") ||\n \"\";\n if (path) {\n const segs = path.split(\"/\").filter(Boolean);\n // even segment count → document path (…/coll/id); odd → collection.\n const coll = segs.length % 2 === 0 ? segs[segs.length - 2] : segs[segs.length - 1];\n return coll ? `col:${coll}` : \"uncategorized\";\n }\n const segs = ref?._query?.path?.segments;\n if (Array.isArray(segs) && segs.length) return `col:${segs[segs.length - 1]}`;\n } catch {\n /* never throw from labelling */\n }\n return \"uncategorized\";\n}\n\nfunction meter(label: string, n: number): void {\n try {\n recordWeb(\"read\", n, label);\n } catch {\n /* best-effort */\n }\n}\n\n/** Count the docs a snapshot delivers: a query's changed docs, or 1 for a doc. */\nfunction countSnap(snap: any): number {\n try {\n if (typeof snap?.docChanges === \"function\") return snap.docChanges().length;\n } catch {\n /* fall through */\n }\n return 1;\n}\n\nexport function getDoc(ref: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(ref);\n return rawGetDoc(ref, ...rest).then((snap: any) => {\n meter(label, 1);\n return snap;\n });\n}\n\nexport function getDocs(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetDocs(query, ...rest).then((snap: any) => {\n meter(label, typeof snap?.size === \"number\" ? Math.max(snap.size, 1) : 1);\n return snap;\n });\n}\n\n/**\n * onSnapshot has several overloads — (ref, observer), (ref, onNext, onError,\n * onComplete), and either of those with a leading SnapshotListenOptions. We find\n * the next-handler wherever it is (a function or `observer.next`) and wrap it to\n * count on each fire, leaving every other argument exactly as passed.\n */\nexport function onSnapshot(ref: any, ...args: any[]): any {\n const label = currentLabel() ?? collLabel(ref);\n const wrapNext = (fn: any) =>\n typeof fn === \"function\"\n ? (snap: any) => {\n meter(label, countSnap(snap));\n return fn(snap);\n }\n : fn;\n\n const out = args.slice();\n // Leading options object (not a function, not an observer): skip it.\n let i = 0;\n if (out[0] && typeof out[0] !== \"function\" && !(\"next\" in out[0])) i = 1;\n\n if (out[i] && typeof out[i] === \"object\" && \"next\" in out[i]) {\n // observer form: clone with wrapped next\n out[i] = { ...out[i], next: wrapNext(out[i].next) };\n } else if (typeof out[i] === \"function\") {\n // callback form: wrap onNext (the first function)\n out[i] = wrapNext(out[i]);\n }\n\n return rawOnSnapshot(ref, ...out);\n}\n\n// ── Force-from-server reads — bill exactly like getDoc / getDocs ──────────────\nexport function getDocFromServer(ref: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(ref);\n return rawGetDocFromServer!(ref, ...rest).then((snap: any) => {\n meter(label, 1);\n return snap;\n });\n}\nexport function getDocsFromServer(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetDocsFromServer!(query, ...rest).then((snap: any) => {\n meter(label, typeof snap?.size === \"number\" ? Math.max(snap.size, 1) : 1);\n return snap;\n });\n}\n\n// ── Cache-only reads — NOT billed, so pass through and count NOTHING ──────────\n// (Counting these would over-report; a cache hit costs you nothing.)\nexport function getDocFromCache(ref: any, ...rest: any[]): Promise<any> {\n return rawGetDocFromCache!(ref, ...rest);\n}\nexport function getDocsFromCache(query: any, ...rest: any[]): Promise<any> {\n return rawGetDocsFromCache!(query, ...rest);\n}\n\n// ── Aggregation — count() / sum() / average() ────────────────────────────────\n// Honest estimate: ceil(count / 1000), min 1 — Firestore never exposes the exact\n// index-entry count. Same model as the server trap, so the numbers reconcile.\nfunction aggReads(snap: any): number {\n const count = typeof snap?.data?.()?.count === \"number\" ? snap.data().count : 0;\n return Math.max(1, Math.ceil(count / 1000));\n}\nexport function getCountFromServer(query: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetCountFromServer!(query, ...rest).then((snap: any) => {\n meter(label, aggReads(snap));\n return snap;\n });\n}\nexport function getAggregateFromServer(query: any, spec: any, ...rest: any[]): Promise<any> {\n const label = currentLabel() ?? collLabel(query);\n return rawGetAggregateFromServer!(query, spec, ...rest).then((snap: any) => {\n meter(label, aggReads(snap));\n return snap;\n });\n}\n","/**\n * @cross-deck/buckets/web — the BROWSER collector.\n *\n * Most Firebase apps read straight from the browser (live `onSnapshot`\n * listeners, `getDocs`, `getDoc`) — reads billed to your project that a\n * server-side collector can never see. This adapter closes that hole.\n *\n * Setup (two lines + one import swap):\n *\n * import { initBucketsWeb, bucket } from \"@cross-deck/buckets/web\";\n * import { getDoc, getDocs, onSnapshot } from \"@cross-deck/buckets/web\"; // was \"firebase/firestore\"\n *\n * initBucketsWeb({ apiKey: \"cd_pub_live_…\" }); // your PUBLISHABLE key\n *\n * bucket(\"pulse-map\", () => onSnapshot(liveQuery, render));\n *\n * Every read those wrappers see is counted, labelled, and reported up the same\n * ingest pipe as the server collector — so the dashboard shows server AND browser\n * reads side by side.\n */\nimport { configureWebMeter, flushWeb, type WebMeterConfig } from \"./meter\";\nimport { WebReportSink } from \"./sink\";\n\nexport interface InitWebOptions {\n /** The project's `cd_pub_live_` PUBLISHABLE key (safe in client code). */\n apiKey: string;\n /** Override the report endpoint (defaults to Crossdeck's ingest). */\n endpoint?: string;\n /** How often to flush coalesced counts (ms). Default 60_000. */\n flushIntervalMs?: number;\n /** Notified when a flush fails, so a dropped window is never silent. */\n onError?: WebMeterConfig[\"onError\"];\n}\n\n/** Configure the browser collector once, at app start. */\nexport function initBucketsWeb(options: InitWebOptions): void {\n const sink = new WebReportSink({ apiKey: options.apiKey, endpoint: options.endpoint });\n configureWebMeter({ sink, flushIntervalMs: options.flushIntervalMs, onError: options.onError });\n}\n\n// The tagging verb + the metered read wrappers (swap your firebase/firestore\n// import for these). Cache-only reads pass through uncounted (they aren't billed).\nexport { bucket } from \"./context\";\nexport {\n getDoc,\n getDocs,\n onSnapshot,\n getDocFromServer,\n getDocsFromServer,\n getDocFromCache,\n getDocsFromCache,\n getCountFromServer,\n getAggregateFromServer,\n} from \"./firestore\";\nexport { flushWeb as flush } from \"./meter\";\n\n// The sink seam — for self-hosting the browser rollups instead of reporting to Crossdeck.\nexport { WebReportSink, type WebReportSinkConfig } from \"./sink\";\nexport type { BucketsReport, OpCounts, Sink } from \"../sink\";\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cross-deck/buckets",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "Know exactly what every database read costs you — and who caused it. A tiny, never-throws read-cost collector for Firestore, server AND browser.",
5
5
  "license": "MIT",
6
6
  "author": "Crossdeck",