@nebulr-group/bridge-svelte 0.9.4 → 0.9.5
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/flags/bootstrap.js
CHANGED
|
@@ -21,6 +21,7 @@ import { tokenStaleHandlerOf } from '../core/billing-store.js';
|
|
|
21
21
|
import { getBridgeRealtime, onBridgeRealtimeOpen, } from '../core/bridge-runtime.js';
|
|
22
22
|
import { getConfig } from '../client/stores/config.store.js';
|
|
23
23
|
import { takePrefetchedFlagDefinitions } from '../core/flag-definitions.js';
|
|
24
|
+
import { coalesceUsageReports } from './evaluation-count.js';
|
|
24
25
|
/** A storage implementation that uses `localStorage` (persistent) or `sessionStorage` (per-tab). */
|
|
25
26
|
export class BrowserIdentityStorage {
|
|
26
27
|
mode;
|
|
@@ -117,9 +118,14 @@ export function createBridgeFlags(config = {}) {
|
|
|
117
118
|
const bridge = new BridgeFlags({ mode: config.mode });
|
|
118
119
|
// Billing 2.0 US-11 — self-report `bridge.flag_evaluations` to the usage
|
|
119
120
|
// pipeline. Best-effort: in SSR test harnesses without BridgeAuth, skip.
|
|
121
|
+
// Summed per second before it is queued (port of TBP-942): one event per
|
|
122
|
+
// evaluation used to fill the shared usage queue, so the app's own
|
|
123
|
+
// `bridge.usage.report()` reached Bridge seconds late.
|
|
124
|
+
let evaluationCounter;
|
|
120
125
|
try {
|
|
121
126
|
const auth = getBridgeAuth();
|
|
122
|
-
|
|
127
|
+
evaluationCounter = coalesceUsageReports(auth.usage);
|
|
128
|
+
bridge.setUsageReporter(evaluationCounter);
|
|
123
129
|
}
|
|
124
130
|
catch {
|
|
125
131
|
// No BridgeAuth instance yet — flags can still operate without usage reporting.
|
|
@@ -280,6 +286,7 @@ export function createBridgeFlags(config = {}) {
|
|
|
280
286
|
for (const off of unsubscribeBilling)
|
|
281
287
|
off();
|
|
282
288
|
unsubscribeOpen();
|
|
289
|
+
evaluationCounter?.stop();
|
|
283
290
|
await telemetry.stop();
|
|
284
291
|
};
|
|
285
292
|
return {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The part of auth-core's usage reporter the flag client calls. */
|
|
2
|
+
export interface UsageReportTarget {
|
|
3
|
+
report(metric: string, value?: number, idempotencyKey?: string): void;
|
|
4
|
+
}
|
|
5
|
+
/** How long evaluations are added up before one event is queued. */
|
|
6
|
+
export declare const EVALUATION_COUNT_WINDOW_MS = 1000;
|
|
7
|
+
export interface CoalescedUsageReporter extends UsageReportTarget {
|
|
8
|
+
/** Queue whatever has been added up so far, now. */
|
|
9
|
+
flush(): void;
|
|
10
|
+
/** Flush, and stop listening for the page being hidden. Idempotent. */
|
|
11
|
+
stop(): void;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Wrap `target` so repeated reports of the same metric within
|
|
15
|
+
* `windowMs` reach it as one report carrying the sum. Reports that carry their
|
|
16
|
+
* own idempotency key are passed through unchanged (they name one event).
|
|
17
|
+
* What has been added up is also flushed when the page is hidden or closed.
|
|
18
|
+
*/
|
|
19
|
+
export declare function coalesceUsageReports(target: UsageReportTarget, windowMs?: number): CoalescedUsageReporter;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// The SDK's own `bridge.flag_evaluations` count, summed before it is queued.
|
|
2
|
+
// Ported from bridge-react / bridge-nextjs (TBP-942).
|
|
3
|
+
//
|
|
4
|
+
// auth-core's flag client reports `bridge.flag_evaluations` once per
|
|
5
|
+
// evaluation, into the same usage queue the app's `bridge.usage.report()`
|
|
6
|
+
// uses. That queue sends 10 events a second, oldest first. A page that reads a
|
|
7
|
+
// few flags queues hundreds of evaluation events, and the app's own report — the one a `<QuotaGate>` waits
|
|
8
|
+
// for — reached Bridge many seconds later. The gate stayed open and a Free
|
|
9
|
+
// workspace could go past its limit until a reload.
|
|
10
|
+
//
|
|
11
|
+
// So evaluations are added up here and handed to the queue as one event per
|
|
12
|
+
// metric per window, with the summed value. Bridge counts exactly as many
|
|
13
|
+
// evaluations as before; the app's reports are never stuck behind them.
|
|
14
|
+
/** How long evaluations are added up before one event is queued. */
|
|
15
|
+
export const EVALUATION_COUNT_WINDOW_MS = 1000;
|
|
16
|
+
/**
|
|
17
|
+
* Wrap `target` so repeated reports of the same metric within
|
|
18
|
+
* `windowMs` reach it as one report carrying the sum. Reports that carry their
|
|
19
|
+
* own idempotency key are passed through unchanged (they name one event).
|
|
20
|
+
* What has been added up is also flushed when the page is hidden or closed.
|
|
21
|
+
*/
|
|
22
|
+
export function coalesceUsageReports(target, windowMs = EVALUATION_COUNT_WINDOW_MS) {
|
|
23
|
+
const pending = new Map();
|
|
24
|
+
let timer;
|
|
25
|
+
let stopped = false;
|
|
26
|
+
const flush = () => {
|
|
27
|
+
if (timer !== undefined) {
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
timer = undefined;
|
|
30
|
+
}
|
|
31
|
+
if (pending.size === 0)
|
|
32
|
+
return;
|
|
33
|
+
const sums = [...pending];
|
|
34
|
+
pending.clear();
|
|
35
|
+
for (const [metric, value] of sums) {
|
|
36
|
+
try {
|
|
37
|
+
target.report(metric, value);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// A usage count must never break a flag read.
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const onHidden = () => {
|
|
45
|
+
if (typeof document !== 'undefined' && document.visibilityState !== 'hidden')
|
|
46
|
+
return;
|
|
47
|
+
flush();
|
|
48
|
+
};
|
|
49
|
+
const win = typeof window !== 'undefined' && typeof window.addEventListener === 'function' ? window : null;
|
|
50
|
+
const doc = typeof document !== 'undefined' && typeof document.addEventListener === 'function' ? document : null;
|
|
51
|
+
win?.addEventListener('pagehide', flush);
|
|
52
|
+
doc?.addEventListener('visibilitychange', onHidden);
|
|
53
|
+
return {
|
|
54
|
+
report(metric, value = 1, idempotencyKey) {
|
|
55
|
+
if (stopped || idempotencyKey !== undefined) {
|
|
56
|
+
target.report(metric, value, idempotencyKey);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
60
|
+
return;
|
|
61
|
+
pending.set(metric, (pending.get(metric) ?? 0) + value);
|
|
62
|
+
if (timer === undefined) {
|
|
63
|
+
timer = setTimeout(flush, windowMs);
|
|
64
|
+
const t = timer;
|
|
65
|
+
if (typeof t.unref === 'function')
|
|
66
|
+
t.unref();
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
flush,
|
|
70
|
+
stop() {
|
|
71
|
+
if (stopped)
|
|
72
|
+
return;
|
|
73
|
+
stopped = true;
|
|
74
|
+
flush();
|
|
75
|
+
win?.removeEventListener('pagehide', flush);
|
|
76
|
+
doc?.removeEventListener('visibilitychange', onHidden);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nebulr-group/bridge-svelte",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.5",
|
|
4
4
|
"description": "The Bridge for SvelteKit: sign-in, workspaces and roles, feature flags, Stripe subscriptions and plan limits in one .env line and three files.",
|
|
5
5
|
"author": "Iman Pouya",
|
|
6
6
|
"license": "MIT",
|