@cross-deck/buckets 0.1.1 → 0.2.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/dist/index.d.mts +126 -0
- package/dist/index.d.ts +94 -12
- package/dist/index.js +249 -33
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +236 -0
- package/dist/index.mjs.map +1 -0
- package/dist/{sink.d.ts → sink-DjFci4Uz.d.mts} +7 -6
- package/dist/sink-DjFci4Uz.d.ts +57 -0
- package/dist/web.d.mts +101 -0
- package/dist/web.d.ts +101 -0
- package/dist/web.js +206 -0
- package/dist/web.js.map +1 -0
- package/dist/web.mjs +198 -0
- package/dist/web.mjs.map +1 -0
- package/package.json +18 -10
- package/dist/adapters/firestore.d.ts +0 -41
- package/dist/adapters/firestore.d.ts.map +0 -1
- package/dist/adapters/firestore.js +0 -145
- package/dist/adapters/firestore.js.map +0 -1
- package/dist/cost-context.d.ts +0 -25
- package/dist/cost-context.d.ts.map +0 -1
- package/dist/cost-context.js +0 -46
- package/dist/cost-context.js.map +0 -1
- package/dist/cost-meter.d.ts +0 -28
- package/dist/cost-meter.d.ts.map +0 -1
- package/dist/cost-meter.js +0 -137
- package/dist/cost-meter.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/sink.d.ts.map +0 -1
- package/dist/sink.js +0 -42
- package/dist/sink.js.map +0 -1
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* adapters/firestore — the universal Firestore read meter (the trap).
|
|
3
|
-
*
|
|
4
|
-
* THE LESSON (learned on a real product): per-call-site `recordReads()`
|
|
5
|
-
* instrumentation MISSES paths. You meter the read sites you're looking at and
|
|
6
|
-
* leave the cron / trigger / ingest path uncounted — often the majority of reads,
|
|
7
|
-
* invisible. Humans tag what they see and miss the path that matters.
|
|
8
|
-
*
|
|
9
|
-
* THE FIX: patch the admin SDK's read methods ONCE. From install onward, EVERY
|
|
10
|
-
* read — anywhere, on any code path — is counted under the ambient tag, with zero
|
|
11
|
-
* per-call-site work and no blind spots.
|
|
12
|
-
*
|
|
13
|
-
* SAFETY CONTRACT — this sits on your production read path, so it is defensive by
|
|
14
|
-
* construction. Each wrapper:
|
|
15
|
-
* 1. calls the REAL method first and captures the result,
|
|
16
|
-
* 2. counts in a try/catch that can never throw into the caller,
|
|
17
|
-
* 3. ALWAYS returns the real result, untouched.
|
|
18
|
-
* It cannot break a read, change a result, or add latency beyond one in-memory
|
|
19
|
-
* counter increment. A wrong count is a measurement error, never a correctness or
|
|
20
|
-
* availability one. Idempotent — calling it twice patches once.
|
|
21
|
-
*
|
|
22
|
-
* COUNTING MODEL — a query returning N docs = N reads (an empty result still bills
|
|
23
|
-
* 1, which the meter enforces). A document get = 1. getAll(...) = the ref count.
|
|
24
|
-
* CollectionReference.get IS Query.get (shared prototype method), so patching Query
|
|
25
|
-
* covers collections with no double-count.
|
|
26
|
-
*/
|
|
27
|
-
import { recordReads } from "../cost-meter.js";
|
|
28
|
-
let installed = false;
|
|
29
|
-
/** `projects/{id}/…` → the project id, else undefined. Pure string op. */
|
|
30
|
-
function projectFromPath(path) {
|
|
31
|
-
const parts = path.split("/");
|
|
32
|
-
const i = parts.indexOf("projects");
|
|
33
|
-
return i >= 0 && parts[i + 1] ? parts[i + 1] : undefined;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Derive { collection, projectId } from the read target's path so an UNtagged read
|
|
37
|
-
* cascades to `col:<collection>` instead of "uncategorized". PURE CPU; never reads,
|
|
38
|
-
* never throws. Falls back to firebase-admin's internal `_queryOptions` for filtered
|
|
39
|
-
* queries (which don't expose `.path`).
|
|
40
|
-
*/
|
|
41
|
-
function hintFrom(target) {
|
|
42
|
-
try {
|
|
43
|
-
const p = typeof target?.path === "string" ? target.path : "";
|
|
44
|
-
if (p) {
|
|
45
|
-
const parts = p.split("/").filter(Boolean);
|
|
46
|
-
const collection = parts.length % 2 === 0 ? parts[parts.length - 2] : parts[parts.length - 1];
|
|
47
|
-
return { collection, projectId: projectFromPath(p) };
|
|
48
|
-
}
|
|
49
|
-
const qo = target?._queryOptions;
|
|
50
|
-
if (qo) {
|
|
51
|
-
const collection = typeof qo.collectionId === "string" ? qo.collectionId : undefined;
|
|
52
|
-
const parent = typeof qo.parentPath?.relativeName === "string"
|
|
53
|
-
? qo.parentPath.relativeName
|
|
54
|
-
: typeof qo.parentPath?.toString === "function"
|
|
55
|
-
? String(qo.parentPath.toString())
|
|
56
|
-
: "";
|
|
57
|
-
return { collection, projectId: parent ? projectFromPath(parent) : undefined };
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
catch {
|
|
61
|
-
/* the meter must never throw */
|
|
62
|
-
}
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|
|
65
|
-
function meterSnap(snap, hint) {
|
|
66
|
-
try {
|
|
67
|
-
const size = snap?.size;
|
|
68
|
-
recordReads(typeof size === "number" ? size : 1, hint);
|
|
69
|
-
}
|
|
70
|
-
catch {
|
|
71
|
-
/* best-effort */
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
function meterCount(n, hint) {
|
|
75
|
-
try {
|
|
76
|
-
recordReads(n, hint);
|
|
77
|
-
}
|
|
78
|
-
catch {
|
|
79
|
-
/* best-effort */
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Install the universal read meter on the firebase-admin Firestore classes. Call
|
|
84
|
-
* ONCE at process start, before any reads. Pass the namespace from
|
|
85
|
-
* `firebase-admin/firestore` so the exact prototypes the SDK uses are patched:
|
|
86
|
-
*
|
|
87
|
-
* import * as Firestore from "firebase-admin/firestore";
|
|
88
|
-
* installFirestoreMeter(Firestore);
|
|
89
|
-
*/
|
|
90
|
-
export function installFirestoreMeter(classes) {
|
|
91
|
-
if (installed)
|
|
92
|
-
return;
|
|
93
|
-
installed = true;
|
|
94
|
-
const { Query, DocumentReference, Transaction, Firestore } = classes;
|
|
95
|
-
// Query.get — covers Query AND CollectionReference (shared prototype method).
|
|
96
|
-
const qGet = Query?.prototype?.get;
|
|
97
|
-
if (qGet) {
|
|
98
|
-
Query.prototype.get = async function (...args) {
|
|
99
|
-
const snap = await qGet.apply(this, args);
|
|
100
|
-
meterSnap(snap, hintFrom(this));
|
|
101
|
-
return snap;
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
// DocumentReference.get — a single doc = 1 read.
|
|
105
|
-
const dGet = DocumentReference?.prototype?.get;
|
|
106
|
-
if (dGet) {
|
|
107
|
-
DocumentReference.prototype.get = async function (...args) {
|
|
108
|
-
const snap = await dGet.apply(this, args);
|
|
109
|
-
meterCount(1, hintFrom(this));
|
|
110
|
-
return snap;
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
// Transaction.get — query or doc; size when present, else 1.
|
|
114
|
-
const tGet = Transaction?.prototype?.get;
|
|
115
|
-
if (tGet) {
|
|
116
|
-
Transaction.prototype.get = async function (...args) {
|
|
117
|
-
const res = await tGet.apply(this, args);
|
|
118
|
-
meterSnap(res, hintFrom(args[0]));
|
|
119
|
-
return res;
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
// Transaction.getAll(...refs) — one read per ref.
|
|
123
|
-
const tGetAll = Transaction?.prototype?.getAll;
|
|
124
|
-
if (tGetAll) {
|
|
125
|
-
Transaction.prototype.getAll = async function (...args) {
|
|
126
|
-
const res = await tGetAll.apply(this, args);
|
|
127
|
-
meterCount(Array.isArray(res) ? res.length : args.length || 1, hintFrom(args[0]));
|
|
128
|
-
return res;
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
// Firestore.getAll(...refs) — batched doc reads.
|
|
132
|
-
const fGetAll = Firestore?.prototype?.getAll;
|
|
133
|
-
if (fGetAll) {
|
|
134
|
-
Firestore.prototype.getAll = async function (...args) {
|
|
135
|
-
const res = await fGetAll.apply(this, args);
|
|
136
|
-
meterCount(Array.isArray(res) ? res.length : args.length || 1, hintFrom(args[0]));
|
|
137
|
-
return res;
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
/** Test-only: reset the install guard so a suite can re-patch fresh prototypes. */
|
|
142
|
-
export function __resetFirestoreMeterForTests() {
|
|
143
|
-
installed = false;
|
|
144
|
-
}
|
|
145
|
-
//# sourceMappingURL=firestore.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"firestore.js","sourceRoot":"","sources":["../../src/adapters/firestore.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,EAAE,WAAW,EAAiB,MAAM,kBAAkB,CAAC;AAE9D,IAAI,SAAS,GAAG,KAAK,CAAC;AAgBtB,0EAA0E;AAC1E,SAAS,eAAe,CAAC,IAAY;IACnC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9B,MAAM,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IACpC,OAAO,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAC3D,CAAC;AAED;;;;;GAKG;AACH,SAAS,QAAQ,CAAC,MAAW;IAC3B,IAAI,CAAC;QACH,MAAM,CAAC,GAAG,OAAO,MAAM,EAAE,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;QAC9D,IAAI,CAAC,EAAE,CAAC;YACN,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAC3C,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,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CAAC;QACb,CAAC,CAAC;IACJ,CAAC;AACH,CAAC;AAED,mFAAmF;AACnF,MAAM,UAAU,6BAA6B;IAC3C,SAAS,GAAG,KAAK,CAAC;AACpB,CAAC"}
|
package/dist/cost-context.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export interface CostTag {
|
|
2
|
-
/** Optional coarse grouping (a caller-defined surface name). */
|
|
3
|
-
feature?: string;
|
|
4
|
-
/** The bucket name — what `bucket()` sets. Drives the report's `byLabel`. */
|
|
5
|
-
label?: string;
|
|
6
|
-
}
|
|
7
|
-
/** Run `fn` with `tag` bound for its entire async subtree. */
|
|
8
|
-
export declare function runWithCostTag<T>(tag: CostTag, fn: () => T): T;
|
|
9
|
-
/** Bind a tag for the remainder of the current async context (no closure to wrap). */
|
|
10
|
-
export declare function enterCostTag(tag: CostTag): void;
|
|
11
|
-
/** Refine the live tag in place (e.g. stamp a feature after the boundary). */
|
|
12
|
-
export declare function refineCostTag(patch: Partial<CostTag>): void;
|
|
13
|
-
/** The current tag, or a safe empty default outside any bound context. */
|
|
14
|
-
export declare function currentCostTag(): CostTag;
|
|
15
|
-
/**
|
|
16
|
-
* `bucket(name, fn)` — the headline verb, the `track()` of cost. Run `fn` with
|
|
17
|
-
* every operation inside it attributed to the bucket `name`; the attribution
|
|
18
|
-
* rides the async subtree automatically. The one verb most developers ever touch:
|
|
19
|
-
*
|
|
20
|
-
* await bucket("nightly-export", async () => {
|
|
21
|
-
* const rows = await db.collection("events").where(...).get(); // → "nightly-export"
|
|
22
|
-
* });
|
|
23
|
-
*/
|
|
24
|
-
export declare function bucket<T>(name: string, fn: () => T): T;
|
|
25
|
-
//# sourceMappingURL=cost-context.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cost-context.d.ts","sourceRoot":"","sources":["../src/cost-context.ts"],"names":[],"mappings":"AAcA,MAAM,WAAW,OAAO;IACtB,gEAAgE;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6EAA6E;IAC7E,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAKD,8DAA8D;AAC9D,wBAAgB,cAAc,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAE9D;AAED,sFAAsF;AACtF,wBAAgB,YAAY,CAAC,GAAG,EAAE,OAAO,GAAG,IAAI,CAE/C;AAED,8EAA8E;AAC9E,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CAG3D;AAED,0EAA0E;AAC1E,wBAAgB,cAAc,IAAI,OAAO,CAExC;AAED;;;;;;;;GAQG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAEtD"}
|
package/dist/cost-context.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* cost-context — the request-scoped tag every counted operation attributes
|
|
3
|
-
* itself to. Set it ONCE at a boundary (or wrap a path with `bucket()`); it
|
|
4
|
-
* rides Node's AsyncLocalStorage down through every async fan-out, so one
|
|
5
|
-
* handler that triggers 15 reads attributes all 15 to the same bucket — with
|
|
6
|
-
* zero per-call-site work.
|
|
7
|
-
*
|
|
8
|
-
* Generic by design: unlike a hardcoded product taxonomy, the only meaningful
|
|
9
|
-
* field a consumer sets is the free-form `label` (the bucket name). `feature`
|
|
10
|
-
* is an optional coarse grouping if you want one; nothing here is
|
|
11
|
-
* Crossdeck-specific.
|
|
12
|
-
*/
|
|
13
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
14
|
-
const DEFAULT_TAG = {};
|
|
15
|
-
const store = new AsyncLocalStorage();
|
|
16
|
-
/** Run `fn` with `tag` bound for its entire async subtree. */
|
|
17
|
-
export function runWithCostTag(tag, fn) {
|
|
18
|
-
return store.run({ ...tag }, fn);
|
|
19
|
-
}
|
|
20
|
-
/** Bind a tag for the remainder of the current async context (no closure to wrap). */
|
|
21
|
-
export function enterCostTag(tag) {
|
|
22
|
-
store.enterWith({ ...tag });
|
|
23
|
-
}
|
|
24
|
-
/** Refine the live tag in place (e.g. stamp a feature after the boundary). */
|
|
25
|
-
export function refineCostTag(patch) {
|
|
26
|
-
const cur = store.getStore();
|
|
27
|
-
if (cur)
|
|
28
|
-
Object.assign(cur, patch);
|
|
29
|
-
}
|
|
30
|
-
/** The current tag, or a safe empty default outside any bound context. */
|
|
31
|
-
export function currentCostTag() {
|
|
32
|
-
return store.getStore() ?? DEFAULT_TAG;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* `bucket(name, fn)` — the headline verb, the `track()` of cost. Run `fn` with
|
|
36
|
-
* every operation inside it attributed to the bucket `name`; the attribution
|
|
37
|
-
* rides the async subtree automatically. The one verb most developers ever touch:
|
|
38
|
-
*
|
|
39
|
-
* await bucket("nightly-export", async () => {
|
|
40
|
-
* const rows = await db.collection("events").where(...).get(); // → "nightly-export"
|
|
41
|
-
* });
|
|
42
|
-
*/
|
|
43
|
-
export function bucket(name, fn) {
|
|
44
|
-
return runWithCostTag({ ...currentCostTag(), label: name }, fn);
|
|
45
|
-
}
|
|
46
|
-
//# sourceMappingURL=cost-context.js.map
|
package/dist/cost-context.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cost-context.js","sourceRoot":"","sources":["../src/cost-context.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AASrD,MAAM,WAAW,GAAY,EAAE,CAAC;AAChC,MAAM,KAAK,GAAG,IAAI,iBAAiB,EAAW,CAAC;AAE/C,8DAA8D;AAC9D,MAAM,UAAU,cAAc,CAAI,GAAY,EAAE,EAAW;IACzD,OAAO,KAAK,CAAC,GAAG,CAAC,EAAE,GAAG,GAAG,EAAE,EAAE,EAAE,CAAC,CAAC;AACnC,CAAC;AAED,sFAAsF;AACtF,MAAM,UAAU,YAAY,CAAC,GAAY;IACvC,KAAK,CAAC,SAAS,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,CAAC;AAC9B,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,aAAa,CAAC,KAAuB;IACnD,MAAM,GAAG,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC;IAC7B,IAAI,GAAG;QAAE,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AACrC,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,cAAc;IAC5B,OAAO,KAAK,CAAC,QAAQ,EAAE,IAAI,WAAW,CAAC;AACzC,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CAAI,IAAY,EAAE,EAAW;IACjD,OAAO,cAAc,CAAC,EAAE,GAAG,cAAc,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC;AAClE,CAAC"}
|
package/dist/cost-meter.d.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import type { Sink } from "./sink.js";
|
|
2
|
-
export type OpType = "read" | "write" | "delete";
|
|
3
|
-
/** Optional read-site hint — the collection touched, derived at the trap from the
|
|
4
|
-
* path. Lets an UNtagged read cascade to `col:<collection>` instead of vanishing. */
|
|
5
|
-
export interface CostHint {
|
|
6
|
-
collection?: string;
|
|
7
|
-
projectId?: string;
|
|
8
|
-
}
|
|
9
|
-
export interface MeterConfig {
|
|
10
|
-
sink: Sink;
|
|
11
|
-
flushIntervalMs?: number;
|
|
12
|
-
onError?: (e: unknown) => void;
|
|
13
|
-
}
|
|
14
|
-
/** Point the meter at a sink. Called by `init()`; pass your own sink to self-host. */
|
|
15
|
-
export declare function configureMeter(config: MeterConfig): void;
|
|
16
|
-
/** Count `n` ops of `op` against the live tag. Never throws. */
|
|
17
|
-
export declare function recordFirestore(op: OpType, n: number, hint?: CostHint): void;
|
|
18
|
-
/** Firestore bills a minimum of one read even for an empty result, so 0 counts as 1. */
|
|
19
|
-
export declare function recordReads(n: number, hint?: CostHint): void;
|
|
20
|
-
export declare function recordWrites(n?: number): void;
|
|
21
|
-
export declare function recordDeletes(n?: number): void;
|
|
22
|
-
/**
|
|
23
|
-
* Coalesce the buffer into one report per UTC day and hand each to the Sink.
|
|
24
|
-
* Snapshots + clears up front so concurrent records land in the next window.
|
|
25
|
-
* Never throws; a sink failure drops that window (surfaced via `onError`).
|
|
26
|
-
*/
|
|
27
|
-
export declare function flush(): Promise<void>;
|
|
28
|
-
//# sourceMappingURL=cost-meter.d.ts.map
|
package/dist/cost-meter.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cost-meter.d.ts","sourceRoot":"","sources":["../src/cost-meter.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,IAAI,EAA2B,MAAM,WAAW,CAAC;AAE/D,MAAM,MAAM,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEjD;sFACsF;AACtF,MAAM,WAAW,QAAQ;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAmBD,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,IAAI,CAAC;IACX,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,KAAK,IAAI,CAAC;CAChC;AAED,sFAAsF;AACtF,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI,CAIxD;AAeD,gEAAgE;AAChE,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,QAAQ,GAAG,IAAI,CAkB5E;AAED,wFAAwF;AACxF,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,QAAQ,GAAG,IAAI,CAE5D;AACD,wBAAgB,YAAY,CAAC,CAAC,SAAI,GAAG,IAAI,CAExC;AACD,wBAAgB,aAAa,CAAC,CAAC,SAAI,GAAG,IAAI,CAEzC;AAOD;;;;GAIG;AACH,wBAAsB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CA2C3C"}
|
package/dist/cost-meter.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* cost-meter — counts operations against the ambient tag and flushes them to the
|
|
3
|
-
* configured Sink cheaply.
|
|
4
|
-
*
|
|
5
|
-
* LOW-OVERHEAD CONTRACT (the thing that warns you about reads must not run them
|
|
6
|
-
* up): counts accumulate in an in-memory buffer and flush periodically — NEVER one
|
|
7
|
-
* network call per counted operation. A flush coalesces the whole window into one
|
|
8
|
-
* report per UTC day and hands it to the Sink. At steady state that is ~1 small
|
|
9
|
-
* request a minute, regardless of how many ops you served.
|
|
10
|
-
*
|
|
11
|
-
* BEST-EFFORT CONTRACT: metering must never throw into your code. Every recorder
|
|
12
|
-
* swallows its own errors; a failed flush drops that window's counts (surfaced via
|
|
13
|
-
* `onError` if you pass one) rather than disturbing the app.
|
|
14
|
-
*/
|
|
15
|
-
import { currentCostTag } from "./cost-context.js";
|
|
16
|
-
// NUL separator — a bucket/collection name can contain almost anything except
|
|
17
|
-
// this, so the key never collides with a name that has a "|" or ":" in it.
|
|
18
|
-
const SEP = "\u001f"; // ASCII Unit Separator
|
|
19
|
-
/** key = date <NUL> op <NUL> label → count */
|
|
20
|
-
const labelBuffer = new Map();
|
|
21
|
-
/** key = date <NUL> op <NUL> hour → count */
|
|
22
|
-
const hourBuffer = new Map();
|
|
23
|
-
let sink = null;
|
|
24
|
-
let flushIntervalMs = 60_000;
|
|
25
|
-
let onError = null;
|
|
26
|
-
let timer = null;
|
|
27
|
-
let flushing = false;
|
|
28
|
-
/** Safety valve — flush early if a burst fills the buffer between intervals. */
|
|
29
|
-
const MAX_BUFFER_KEYS = 5_000;
|
|
30
|
-
/** Point the meter at a sink. Called by `init()`; pass your own sink to self-host. */
|
|
31
|
-
export function configureMeter(config) {
|
|
32
|
-
sink = config.sink;
|
|
33
|
-
if (config.flushIntervalMs && config.flushIntervalMs > 0)
|
|
34
|
-
flushIntervalMs = config.flushIntervalMs;
|
|
35
|
-
onError = config.onError ?? null;
|
|
36
|
-
}
|
|
37
|
-
const utcDate = () => new Date().toISOString().slice(0, 10);
|
|
38
|
-
const utcHour = () => new Date().toISOString().slice(11, 13);
|
|
39
|
-
function ensureFlushLoop() {
|
|
40
|
-
if (timer)
|
|
41
|
-
return;
|
|
42
|
-
timer = setInterval(() => void flush(), flushIntervalMs);
|
|
43
|
-
// Don't keep the event loop alive just for metering.
|
|
44
|
-
timer.unref?.();
|
|
45
|
-
// Flush the last window on shutdown.
|
|
46
|
-
process.once?.("SIGTERM", () => void flush());
|
|
47
|
-
process.once?.("beforeExit", () => void flush());
|
|
48
|
-
}
|
|
49
|
-
/** Count `n` ops of `op` against the live tag. Never throws. */
|
|
50
|
-
export function recordFirestore(op, n, hint) {
|
|
51
|
-
try {
|
|
52
|
-
if (!Number.isFinite(n) || n <= 0)
|
|
53
|
-
return;
|
|
54
|
-
const t = currentCostTag();
|
|
55
|
-
const date = utcDate();
|
|
56
|
-
// CASCADE — every op gets a label, by design (no blind spots): the bucket name
|
|
57
|
-
// wins; else the collection it actually touched (`col:posts`); else
|
|
58
|
-
// "uncategorized" as a loud last resort. A read is never invisible.
|
|
59
|
-
const label = t.label || (hint?.collection ? `col:${hint.collection}` : "uncategorized");
|
|
60
|
-
const lk = date + SEP + op + SEP + label;
|
|
61
|
-
labelBuffer.set(lk, (labelBuffer.get(lk) ?? 0) + n);
|
|
62
|
-
const hk = date + SEP + op + SEP + utcHour();
|
|
63
|
-
hourBuffer.set(hk, (hourBuffer.get(hk) ?? 0) + n);
|
|
64
|
-
ensureFlushLoop();
|
|
65
|
-
if (labelBuffer.size + hourBuffer.size > MAX_BUFFER_KEYS)
|
|
66
|
-
void flush();
|
|
67
|
-
}
|
|
68
|
-
catch {
|
|
69
|
-
/* metering is best-effort — never disturb the caller */
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
/** Firestore bills a minimum of one read even for an empty result, so 0 counts as 1. */
|
|
73
|
-
export function recordReads(n, hint) {
|
|
74
|
-
recordFirestore("read", Math.max(n, 1), hint);
|
|
75
|
-
}
|
|
76
|
-
export function recordWrites(n = 1) {
|
|
77
|
-
recordFirestore("write", n);
|
|
78
|
-
}
|
|
79
|
-
export function recordDeletes(n = 1) {
|
|
80
|
-
recordFirestore("delete", n);
|
|
81
|
-
}
|
|
82
|
-
function add(target, key, op, n) {
|
|
83
|
-
const bag = (target[key] ??= {});
|
|
84
|
-
bag[op] = (bag[op] ?? 0) + n;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Coalesce the buffer into one report per UTC day and hand each to the Sink.
|
|
88
|
-
* Snapshots + clears up front so concurrent records land in the next window.
|
|
89
|
-
* Never throws; a sink failure drops that window (surfaced via `onError`).
|
|
90
|
-
*/
|
|
91
|
-
export async function flush() {
|
|
92
|
-
if (flushing)
|
|
93
|
-
return;
|
|
94
|
-
// Not configured (init not called) — drop, don't grow unbounded.
|
|
95
|
-
if (!sink) {
|
|
96
|
-
labelBuffer.clear();
|
|
97
|
-
hourBuffer.clear();
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
if (labelBuffer.size === 0 && hourBuffer.size === 0)
|
|
101
|
-
return;
|
|
102
|
-
flushing = true;
|
|
103
|
-
const labels = new Map(labelBuffer);
|
|
104
|
-
const hours = new Map(hourBuffer);
|
|
105
|
-
labelBuffer.clear();
|
|
106
|
-
hourBuffer.clear();
|
|
107
|
-
try {
|
|
108
|
-
const byDate = new Map();
|
|
109
|
-
const reportFor = (date) => {
|
|
110
|
-
let r = byDate.get(date);
|
|
111
|
-
if (!r) {
|
|
112
|
-
r = { date, byLabel: {}, byHour: {} };
|
|
113
|
-
byDate.set(date, r);
|
|
114
|
-
}
|
|
115
|
-
return r;
|
|
116
|
-
};
|
|
117
|
-
for (const [k, n] of labels) {
|
|
118
|
-
const [date, op, label] = k.split(SEP);
|
|
119
|
-
add(reportFor(date).byLabel, label, op, n);
|
|
120
|
-
}
|
|
121
|
-
for (const [k, n] of hours) {
|
|
122
|
-
const [date, op, hour] = k.split(SEP);
|
|
123
|
-
add(reportFor(date).byHour, hour, op, n);
|
|
124
|
-
}
|
|
125
|
-
for (const report of byDate.values()) {
|
|
126
|
-
await sink.flush(report);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
catch (e) {
|
|
130
|
-
// Drop this window rather than risk a partial/double report on retry.
|
|
131
|
-
onError?.(e);
|
|
132
|
-
}
|
|
133
|
-
finally {
|
|
134
|
-
flushing = false;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
//# sourceMappingURL=cost-meter.js.map
|
package/dist/cost-meter.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cost-meter.js","sourceRoot":"","sources":["../src/cost-meter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAYnD,8EAA8E;AAC9E,2EAA2E;AAC3E,MAAM,GAAG,GAAG,QAAQ,CAAC,CAAC,uBAAuB;AAE7C,8CAA8C;AAC9C,MAAM,WAAW,GAAG,IAAI,GAAG,EAAkB,CAAC;AAC9C,6CAA6C;AAC7C,MAAM,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;AAE7C,IAAI,IAAI,GAAgB,IAAI,CAAC;AAC7B,IAAI,eAAe,GAAG,MAAM,CAAC;AAC7B,IAAI,OAAO,GAAkC,IAAI,CAAC;AAClD,IAAI,KAAK,GAA0C,IAAI,CAAC;AACxD,IAAI,QAAQ,GAAG,KAAK,CAAC;AACrB,gFAAgF;AAChF,MAAM,eAAe,GAAG,KAAK,CAAC;AAQ9B,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AO,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IAClC,gFAAgF;IAChF,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;CACnC;AAED;;;GAGG;AACH,MAAM,WAAW,IAAI;IACnB,KAAK,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7C;AAED,MAAM,WAAW,gBAAgB;IAC/B,gEAAgE;IAChE,MAAM,EAAE,MAAM,CAAC;IACf,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,0EAA0E;IAC1E,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAID;;;;;;GAMG;AACH,qBAAa,UAAW,YAAW,IAAI;IACrC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAS;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;gBAEvB,MAAM,EAAE,gBAAgB;IAM9B,KAAK,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;CAclD"}
|
package/dist/sink.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* sink — where the meter sends a coalesced rollup, and the wire shape it sends.
|
|
3
|
-
*
|
|
4
|
-
* Abstracting the sink is what makes Buckets storage-agnostic: the meter never
|
|
5
|
-
* knows where counts go. The DEFAULT sink (`ReportSink`) reports up to Crossdeck's
|
|
6
|
-
* ingest endpoint so the numbers surface on your Crossdeck dashboard. A team that
|
|
7
|
-
* wants to self-host can implement `Sink` against anything (Postgres, a file, your
|
|
8
|
-
* own API) without touching the meter.
|
|
9
|
-
*/
|
|
10
|
-
const DEFAULT_ENDPOINT = "https://api.cross-deck.com/v1/buckets/report";
|
|
11
|
-
/**
|
|
12
|
-
* The default sink: POST one coalesced rollup to Crossdeck's ingest endpoint.
|
|
13
|
-
* The ingest folds it into the day's maintained doc with `increment`, so many
|
|
14
|
-
* reports a minute coalesce safely. This path does ZERO database reads — it sends
|
|
15
|
-
* a summary, it does not read. Throws on a non-202 so the meter can log/drop the
|
|
16
|
-
* window; the meter guarantees it never reaches your app.
|
|
17
|
-
*/
|
|
18
|
-
export class ReportSink {
|
|
19
|
-
endpoint;
|
|
20
|
-
apiKey;
|
|
21
|
-
timeoutMs;
|
|
22
|
-
constructor(config) {
|
|
23
|
-
this.endpoint = config.endpoint ?? DEFAULT_ENDPOINT;
|
|
24
|
-
this.apiKey = config.apiKey;
|
|
25
|
-
this.timeoutMs = config.timeoutMs ?? 5000;
|
|
26
|
-
}
|
|
27
|
-
async flush(report) {
|
|
28
|
-
const res = await fetch(this.endpoint, {
|
|
29
|
-
method: "POST",
|
|
30
|
-
signal: AbortSignal.timeout(this.timeoutMs),
|
|
31
|
-
headers: {
|
|
32
|
-
"content-type": "application/json",
|
|
33
|
-
authorization: `Bearer ${this.apiKey}`,
|
|
34
|
-
},
|
|
35
|
-
body: JSON.stringify(report),
|
|
36
|
-
});
|
|
37
|
-
if (res.status !== 202) {
|
|
38
|
-
throw new Error(`Buckets report rejected: HTTP ${res.status}`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
//# sourceMappingURL=sink.js.map
|
package/dist/sink.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"sink.js","sourceRoot":"","sources":["../src/sink.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAsCH,MAAM,gBAAgB,GAAG,8CAA8C,CAAC;AAExE;;;;;;GAMG;AACH,MAAM,OAAO,UAAU;IACJ,QAAQ,CAAS;IACjB,MAAM,CAAS;IACf,SAAS,CAAS;IAEnC,YAAY,MAAwB;QAClC,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,gBAAgB,CAAC;QACpD,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,IAAI,CAAC;IAC5C,CAAC;IAED,KAAK,CAAC,KAAK,CAAC,MAAqB;QAC/B,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE;YACrC,MAAM,EAAE,MAAM;YACd,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC;YAC3C,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;gBAClC,aAAa,EAAE,UAAU,IAAI,CAAC,MAAM,EAAE;aACvC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC;SAC7B,CAAC,CAAC;QACH,IAAI,GAAG,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,iCAAiC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;CACF"}
|