@decocms/blocks 8.0.0 → 8.1.0-next.1
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/bin/deco.js +13 -0
- package/package.json +32 -82
- package/src/index.ts +35 -8
- package/src/protocol/assets.ts +124 -0
- package/src/protocol/canonical.ts +7 -0
- package/src/protocol/ciphertext.ts +6 -0
- package/src/protocol/client.ts +170 -0
- package/src/protocol/conformance/cases/assets.ts +81 -0
- package/src/protocol/conformance/cases/guards.ts +314 -0
- package/src/protocol/conformance/cases/reads.ts +168 -0
- package/src/protocol/conformance/cases/types.ts +9 -0
- package/src/protocol/conformance/cases/wire.ts +138 -0
- package/src/protocol/conformance/cases/writes.ts +345 -0
- package/src/protocol/conformance/contentHashFixtures.ts +79 -0
- package/src/protocol/conformance/context.ts +222 -0
- package/src/protocol/conformance/index.ts +140 -0
- package/src/protocol/errors.ts +142 -0
- package/src/protocol/index.ts +39 -0
- package/src/protocol/keys.ts +279 -0
- package/src/protocol/params.ts +67 -0
- package/src/protocol/secrets.ts +231 -0
- package/src/protocol/server/assets.ts +119 -0
- package/src/protocol/server/auth.ts +49 -0
- package/src/protocol/server/bodyCache.ts +84 -0
- package/src/protocol/server/content.ts +175 -0
- package/src/protocol/server/core.ts +112 -0
- package/src/protocol/server/handler.ts +102 -0
- package/src/protocol/server/http.ts +139 -0
- package/src/protocol/server/index.ts +11 -0
- package/src/protocol/server/methods/apply.ts +341 -0
- package/src/protocol/server/methods/read.ts +156 -0
- package/src/protocol/server/rpc.ts +136 -0
- package/src/protocol/storage/fs/hash.ts +21 -0
- package/src/protocol/storage/fs/index.ts +338 -0
- package/src/protocol/storage/fs/lock.ts +68 -0
- package/src/protocol/storage/fs/transaction.ts +183 -0
- package/src/protocol/storage/fs/unsupported.ts +15 -0
- package/src/protocol/storage/memory.ts +238 -0
- package/src/protocol/storage.ts +182 -0
- package/src/protocol/types.ts +172 -0
- package/src/v8/analytics.ts +50 -0
- package/src/v8/background.ts +107 -0
- package/src/v8/builtins/data.ts +23 -0
- package/src/v8/builtins/index.ts +26 -0
- package/src/v8/builtins/lazy.ts +23 -0
- package/src/v8/builtins/legacy.ts +65 -0
- package/src/v8/builtins/matchers.ts +31 -0
- package/src/v8/builtins/multivariate.ts +25 -0
- package/src/v8/builtins/secret.ts +82 -0
- package/src/v8/canonical.ts +117 -0
- package/src/v8/ciphertext.ts +158 -0
- package/src/v8/cli/builtins.ts +20 -0
- package/src/v8/cli/check/index.ts +774 -0
- package/src/v8/cli/check/messages.ts +168 -0
- package/src/v8/cli/content.ts +287 -0
- package/src/v8/cli/index.ts +17 -0
- package/src/v8/cli/log.ts +18 -0
- package/src/v8/cli/main.ts +12 -0
- package/src/v8/cli/root.ts +66 -0
- package/src/v8/cli/run.ts +236 -0
- package/src/v8/cli/schema/builtinSchemas.ts +236 -0
- package/src/v8/cli/schema/generate.ts +507 -0
- package/src/v8/cli/schema/index.ts +63 -0
- package/src/v8/cli/schema/tsProgram.ts +497 -0
- package/src/v8/cli/schema/typeToSchema.ts +689 -0
- package/src/v8/cli/serve/server.ts +303 -0
- package/src/v8/cli/watch.ts +86 -0
- package/src/v8/client.ts +147 -0
- package/src/v8/cms.ts +237 -0
- package/src/v8/content.ts +195 -0
- package/src/v8/draft.ts +136 -0
- package/src/v8/errors.ts +59 -0
- package/src/v8/fetch.ts +133 -0
- package/src/v8/identity.ts +65 -0
- package/src/v8/index.ts +39 -0
- package/src/v8/json.ts +74 -0
- package/src/v8/matchRoute.ts +204 -0
- package/src/v8/remoteLoader.ts +282 -0
- package/src/v8/resolver.ts +278 -0
- package/src/v8/secrets.ts +17 -0
- package/src/v8/telemetry.ts +533 -0
- package/src/v8/types.ts +240 -0
- package/src/cms/applySectionConventions.ts +0 -124
- package/src/cms/blockSource.test.ts +0 -76
- package/src/cms/blockSource.ts +0 -191
- package/src/cms/client.browserBundle.test.ts +0 -44
- package/src/cms/client.ts +0 -80
- package/src/cms/draftSource.test.ts +0 -343
- package/src/cms/draftSource.ts +0 -362
- package/src/cms/index.ts +0 -151
- package/src/cms/layoutCacheRace.test.ts +0 -181
- package/src/cms/loadDecofileDirectory.test.ts +0 -109
- package/src/cms/loadDecofileDirectory.ts +0 -89
- package/src/cms/loader.test.ts +0 -200
- package/src/cms/loader.ts +0 -304
- package/src/cms/registry.test.ts +0 -110
- package/src/cms/registry.ts +0 -242
- package/src/cms/resolve.test.ts +0 -980
- package/src/cms/resolve.ts +0 -2282
- package/src/cms/schema.test.ts +0 -356
- package/src/cms/schema.ts +0 -1215
- package/src/cms/sectionLoaderContext.test.ts +0 -111
- package/src/cms/sectionLoaderContext.ts +0 -113
- package/src/cms/sectionLoaders.test.ts +0 -533
- package/src/cms/sectionLoaders.ts +0 -636
- package/src/cms/sectionMixins.test.ts +0 -167
- package/src/cms/sectionMixins.ts +0 -153
- package/src/cms/stickyFlags.test.ts +0 -117
- package/src/flags/audience.ts +0 -56
- package/src/flags/everyone.ts +0 -29
- package/src/flags/flag.ts +0 -15
- package/src/flags/flags.test.ts +0 -148
- package/src/flags/multivariate/image.ts +0 -11
- package/src/flags/multivariate/message.ts +0 -11
- package/src/flags/multivariate/page.ts +0 -16
- package/src/flags/multivariate/section.ts +0 -16
- package/src/flags/multivariate.ts +0 -1
- package/src/flags/types.ts +0 -59
- package/src/flags/utils/multivariate.ts +0 -20
- package/src/hooks/Image.test.ts +0 -100
- package/src/hooks/Image.tsx +0 -250
- package/src/hooks/JsonLd.tsx +0 -353
- package/src/hooks/LazySection.tsx +0 -121
- package/src/hooks/LiveControls.tsx +0 -122
- package/src/hooks/Picture.tsx +0 -127
- package/src/hooks/RenderSection.test.tsx +0 -81
- package/src/hooks/RenderSection.tsx +0 -91
- package/src/hooks/SectionErrorFallback.tsx +0 -97
- package/src/hooks/index.ts +0 -30
- package/src/matchers/builtins.test.ts +0 -318
- package/src/matchers/builtins.ts +0 -459
- package/src/matchers/countryNames.ts +0 -104
- package/src/matchers/override.test.ts +0 -205
- package/src/matchers/override.ts +0 -136
- package/src/matchers/posthog.ts +0 -154
- package/src/middleware/decoState.ts +0 -55
- package/src/middleware/healthMetrics.ts +0 -133
- package/src/middleware/hydrationContext.test.ts +0 -61
- package/src/middleware/hydrationContext.ts +0 -79
- package/src/middleware/index.ts +0 -88
- package/src/middleware/liveness.ts +0 -21
- package/src/middleware/observability.test.ts +0 -242
- package/src/middleware/observability.ts +0 -643
- package/src/middleware/validateSection.test.ts +0 -147
- package/src/middleware/validateSection.ts +0 -100
- package/src/sdk/abTesting.test.ts +0 -326
- package/src/sdk/abTesting.ts +0 -499
- package/src/sdk/analytics.ts +0 -77
- package/src/sdk/cacheHeaders.test.ts +0 -115
- package/src/sdk/cacheHeaders.ts +0 -433
- package/src/sdk/cachedLoader.test.ts +0 -122
- package/src/sdk/cachedLoader.ts +0 -503
- package/src/sdk/cachedLoaderModule.test.ts +0 -209
- package/src/sdk/clx.ts +0 -5
- package/src/sdk/cn.test.ts +0 -34
- package/src/sdk/cn.ts +0 -28
- package/src/sdk/composite.test.ts +0 -121
- package/src/sdk/composite.ts +0 -114
- package/src/sdk/cookie.test.ts +0 -108
- package/src/sdk/cookie.ts +0 -129
- package/src/sdk/crypto.ts +0 -185
- package/src/sdk/csp.ts +0 -59
- package/src/sdk/detectDevice.ts +0 -45
- package/src/sdk/djb2.ts +0 -20
- package/src/sdk/encoding.test.ts +0 -71
- package/src/sdk/encoding.ts +0 -47
- package/src/sdk/env.test.ts +0 -33
- package/src/sdk/env.ts +0 -54
- package/src/sdk/flags.test.ts +0 -91
- package/src/sdk/flags.ts +0 -108
- package/src/sdk/http.test.ts +0 -71
- package/src/sdk/http.ts +0 -124
- package/src/sdk/index.ts +0 -90
- package/src/sdk/inflightTimeout.test.ts +0 -35
- package/src/sdk/inflightTimeout.ts +0 -53
- package/src/sdk/instrumentedFetch.test.ts +0 -559
- package/src/sdk/instrumentedFetch.ts +0 -339
- package/src/sdk/invoke.test.ts +0 -115
- package/src/sdk/invoke.ts +0 -298
- package/src/sdk/logger.test.ts +0 -432
- package/src/sdk/logger.ts +0 -304
- package/src/sdk/mergeCacheControl.ts +0 -150
- package/src/sdk/normalizeUrls.ts +0 -91
- package/src/sdk/observability.ts +0 -109
- package/src/sdk/otel.test.ts +0 -526
- package/src/sdk/otel.ts +0 -1012
- package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
- package/src/sdk/otelAdapters.test.ts +0 -89
- package/src/sdk/otelAdapters.ts +0 -144
- package/src/sdk/otelHttpLog.test.ts +0 -457
- package/src/sdk/otelHttpLog.ts +0 -419
- package/src/sdk/otelHttpMeter.test.ts +0 -292
- package/src/sdk/otelHttpMeter.ts +0 -506
- package/src/sdk/otelHttpTracer.test.ts +0 -474
- package/src/sdk/otelHttpTracer.ts +0 -543
- package/src/sdk/redirects.ts +0 -228
- package/src/sdk/requestContext.ts +0 -281
- package/src/sdk/requestContextStorage.browser.test.ts +0 -29
- package/src/sdk/requestContextStorage.browser.ts +0 -43
- package/src/sdk/requestContextStorage.ts +0 -35
- package/src/sdk/retry.ts +0 -45
- package/src/sdk/serverTimings.ts +0 -68
- package/src/sdk/signal.ts +0 -42
- package/src/sdk/sitemap.ts +0 -160
- package/src/sdk/urlRedaction.test.ts +0 -73
- package/src/sdk/urlRedaction.ts +0 -82
- package/src/sdk/urlUtils.ts +0 -134
- package/src/sdk/useDevice.test.ts +0 -130
- package/src/sdk/useDevice.ts +0 -89
- package/src/sdk/useDeviceContext.tsx +0 -108
- package/src/sdk/useId.ts +0 -7
- package/src/sdk/useScript.test.ts +0 -128
- package/src/sdk/useScript.ts +0 -210
- package/src/sdk/useSuggestions.test.ts +0 -230
- package/src/sdk/useSuggestions.ts +0 -188
- package/src/sdk/wrapCaughtErrors.ts +0 -107
- package/src/setup.ts +0 -120
- package/src/types/index.ts +0 -53
- package/src/types/widgets.ts +0 -14
- package/tsconfig.json +0 -7
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import { createOtlpHttpMeterAdapter } from "./otelHttpMeter";
|
|
3
|
-
|
|
4
|
-
interface OtlpPayload {
|
|
5
|
-
resourceMetrics: Array<{
|
|
6
|
-
resource: {
|
|
7
|
-
attributes: Array<{ key: string; value: { stringValue: string } }>;
|
|
8
|
-
};
|
|
9
|
-
scopeMetrics: Array<{
|
|
10
|
-
scope: { name: string; version: string };
|
|
11
|
-
metrics: Array<{
|
|
12
|
-
name: string;
|
|
13
|
-
sum?: {
|
|
14
|
-
aggregationTemporality: number;
|
|
15
|
-
isMonotonic: boolean;
|
|
16
|
-
dataPoints: Array<{
|
|
17
|
-
attributes: Array<{ key: string; value: Record<string, unknown> }>;
|
|
18
|
-
startTimeUnixNano: string;
|
|
19
|
-
timeUnixNano: string;
|
|
20
|
-
asDouble: number;
|
|
21
|
-
}>;
|
|
22
|
-
};
|
|
23
|
-
gauge?: {
|
|
24
|
-
dataPoints: Array<{
|
|
25
|
-
attributes: Array<{ key: string; value: Record<string, unknown> }>;
|
|
26
|
-
timeUnixNano: string;
|
|
27
|
-
asDouble: number;
|
|
28
|
-
}>;
|
|
29
|
-
};
|
|
30
|
-
histogram?: {
|
|
31
|
-
aggregationTemporality: number;
|
|
32
|
-
dataPoints: Array<{
|
|
33
|
-
attributes: Array<{ key: string; value: Record<string, unknown> }>;
|
|
34
|
-
startTimeUnixNano: string;
|
|
35
|
-
timeUnixNano: string;
|
|
36
|
-
count: string;
|
|
37
|
-
sum: number;
|
|
38
|
-
min: number;
|
|
39
|
-
max: number;
|
|
40
|
-
bucketCounts: string[];
|
|
41
|
-
explicitBounds: number[];
|
|
42
|
-
}>;
|
|
43
|
-
};
|
|
44
|
-
}>;
|
|
45
|
-
}>;
|
|
46
|
-
}>;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function captureFetch() {
|
|
50
|
-
const calls: Array<{ url: string; init?: RequestInit }> = [];
|
|
51
|
-
const impl = vi.fn(async (url: string | URL | Request, init?: RequestInit) => {
|
|
52
|
-
calls.push({ url: String(url), init });
|
|
53
|
-
return new Response("{}", { status: 200 });
|
|
54
|
-
});
|
|
55
|
-
return { impl: impl as unknown as typeof fetch, calls };
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function buildAdapter(
|
|
59
|
-
overrides: {
|
|
60
|
-
fetchImpl?: typeof fetch;
|
|
61
|
-
minFlushIntervalMs?: number;
|
|
62
|
-
maxBufferDatapoints?: number;
|
|
63
|
-
nowMs?: () => number;
|
|
64
|
-
onError?: (kind: "flush" | "overflow" | "kind-mismatch", err: unknown) => void;
|
|
65
|
-
histogramBounds?: number[];
|
|
66
|
-
} = {},
|
|
67
|
-
) {
|
|
68
|
-
return createOtlpHttpMeterAdapter({
|
|
69
|
-
endpoint: "https://ingest.test/v1/metrics",
|
|
70
|
-
resourceAttributes: {
|
|
71
|
-
"service.name": "smoke-site",
|
|
72
|
-
"service.version": "abc123",
|
|
73
|
-
},
|
|
74
|
-
scopeVersion: "5.0.0-test",
|
|
75
|
-
fetchImpl: overrides.fetchImpl,
|
|
76
|
-
minFlushIntervalMs: overrides.minFlushIntervalMs ?? 0,
|
|
77
|
-
maxBufferDatapoints: overrides.maxBufferDatapoints,
|
|
78
|
-
nowMs: overrides.nowMs,
|
|
79
|
-
onError: overrides.onError,
|
|
80
|
-
histogramBounds: overrides.histogramBounds,
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
describe("createOtlpHttpMeterAdapter — buffer + flush", () => {
|
|
85
|
-
beforeEach(() => {
|
|
86
|
-
vi.useFakeTimers();
|
|
87
|
-
vi.setSystemTime(new Date("2026-05-18T16:00:00.000Z"));
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
afterEach(() => {
|
|
91
|
-
vi.useRealTimers();
|
|
92
|
-
vi.restoreAllMocks();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("counterInc accumulates per attr-key and exports cumulative sum", async () => {
|
|
96
|
-
const { impl, calls } = captureFetch();
|
|
97
|
-
const meter = buildAdapter({ fetchImpl: impl });
|
|
98
|
-
|
|
99
|
-
meter.counterInc("deco.http.requests", 1, { method: "GET", status: "2xx" });
|
|
100
|
-
meter.counterInc("deco.http.requests", 1, { method: "GET", status: "2xx" });
|
|
101
|
-
meter.counterInc("deco.http.requests", 1, { method: "POST", status: "5xx" });
|
|
102
|
-
|
|
103
|
-
await meter.flush();
|
|
104
|
-
|
|
105
|
-
expect(calls).toHaveLength(1);
|
|
106
|
-
expect(calls[0].url).toBe("https://ingest.test/v1/metrics");
|
|
107
|
-
const payload = JSON.parse(calls[0].init!.body as string) as OtlpPayload;
|
|
108
|
-
const m = payload.resourceMetrics[0].scopeMetrics[0].metrics[0];
|
|
109
|
-
expect(m.name).toBe("deco.http.requests");
|
|
110
|
-
expect(m.sum?.isMonotonic).toBe(true);
|
|
111
|
-
expect(m.sum?.aggregationTemporality).toBe(2);
|
|
112
|
-
expect(m.sum?.dataPoints).toHaveLength(2);
|
|
113
|
-
|
|
114
|
-
// Sort for stable assertion order.
|
|
115
|
-
const points = [...(m.sum?.dataPoints ?? [])].sort((a, b) =>
|
|
116
|
-
JSON.stringify(a.attributes).localeCompare(JSON.stringify(b.attributes)),
|
|
117
|
-
);
|
|
118
|
-
expect(points[0].asDouble).toBe(2); // GET+2xx
|
|
119
|
-
expect(points[1].asDouble).toBe(1); // POST+5xx
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it("histogramRecord assigns buckets correctly and reports count/sum/min/max", async () => {
|
|
123
|
-
const { impl, calls } = captureFetch();
|
|
124
|
-
const meter = buildAdapter({
|
|
125
|
-
fetchImpl: impl,
|
|
126
|
-
histogramBounds: [5, 10, 25, 50, 75, 100, 250, 500, 1000],
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
// 12 samples across the [5,10,25,50,75,100,250,500,1000] bounds, with
|
|
130
|
-
// the `value <= bound[i]` lower-bucket convention used by the exporter.
|
|
131
|
-
// Distribution:
|
|
132
|
-
// bucket 0 (≤5): []
|
|
133
|
-
// bucket 1 (5..10]: [8.4]
|
|
134
|
-
// bucket 2 (10..25]: [12,14,20]
|
|
135
|
-
// bucket 3 (25..50]: [30,35,38,48]
|
|
136
|
-
// bucket 4 (50..75]: [60,70]
|
|
137
|
-
// bucket 5 (75..100]:[80,87.2]
|
|
138
|
-
// bucket 6+: []
|
|
139
|
-
const samples = [8.4, 12, 14, 20, 30, 35, 38, 48, 60, 70, 80, 87.2];
|
|
140
|
-
expect(samples).toHaveLength(12);
|
|
141
|
-
const sum = samples.reduce((a, b) => a + b, 0);
|
|
142
|
-
for (const v of samples) {
|
|
143
|
-
meter.histogramRecord("outbound_request_duration_ms", v, { provider: "vtex" });
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
await meter.flush();
|
|
147
|
-
|
|
148
|
-
const payload = JSON.parse(calls[0].init!.body as string) as OtlpPayload;
|
|
149
|
-
const m = payload.resourceMetrics[0].scopeMetrics[0].metrics[0];
|
|
150
|
-
expect(m.histogram?.aggregationTemporality).toBe(2);
|
|
151
|
-
expect(m.histogram?.dataPoints).toHaveLength(1);
|
|
152
|
-
const dp = m.histogram!.dataPoints[0];
|
|
153
|
-
expect(dp.count).toBe("12");
|
|
154
|
-
expect(dp.sum).toBeCloseTo(sum, 5);
|
|
155
|
-
expect(dp.min).toBe(8.4);
|
|
156
|
-
expect(dp.max).toBe(87.2);
|
|
157
|
-
expect(dp.bucketCounts).toEqual(["0", "1", "3", "4", "2", "2", "0", "0", "0", "0"]);
|
|
158
|
-
expect(dp.explicitBounds).toEqual([5, 10, 25, 50, 75, 100, 250, 500, 1000]);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it("gaugeSet keeps last write per attr-key", async () => {
|
|
162
|
-
const { impl, calls } = captureFetch();
|
|
163
|
-
const meter = buildAdapter({ fetchImpl: impl });
|
|
164
|
-
|
|
165
|
-
meter.gaugeSet("deco.metrics.buffer_size", 3);
|
|
166
|
-
meter.gaugeSet("deco.metrics.buffer_size", 7);
|
|
167
|
-
|
|
168
|
-
await meter.flush();
|
|
169
|
-
|
|
170
|
-
const payload = JSON.parse(calls[0].init!.body as string) as OtlpPayload;
|
|
171
|
-
const m = payload.resourceMetrics[0].scopeMetrics[0].metrics[0];
|
|
172
|
-
expect(m.gauge?.dataPoints).toHaveLength(1);
|
|
173
|
-
expect(m.gauge?.dataPoints[0].asDouble).toBe(7);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
it("resource attributes are stamped on every payload", async () => {
|
|
177
|
-
const { impl, calls } = captureFetch();
|
|
178
|
-
const meter = buildAdapter({ fetchImpl: impl });
|
|
179
|
-
meter.counterInc("x", 1);
|
|
180
|
-
await meter.flush();
|
|
181
|
-
|
|
182
|
-
const payload = JSON.parse(calls[0].init!.body as string) as OtlpPayload;
|
|
183
|
-
const attrs = payload.resourceMetrics[0].resource.attributes;
|
|
184
|
-
expect(attrs).toContainEqual({
|
|
185
|
-
key: "service.name",
|
|
186
|
-
value: { stringValue: "smoke-site" },
|
|
187
|
-
});
|
|
188
|
-
expect(attrs).toContainEqual({
|
|
189
|
-
key: "service.version",
|
|
190
|
-
value: { stringValue: "abc123" },
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it("never throws when fetch rejects — surfaces via onError", async () => {
|
|
195
|
-
const onError = vi.fn();
|
|
196
|
-
const failing: typeof fetch = vi.fn(() =>
|
|
197
|
-
Promise.reject(new Error("network unreachable")),
|
|
198
|
-
) as unknown as typeof fetch;
|
|
199
|
-
const meter = buildAdapter({ fetchImpl: failing, onError });
|
|
200
|
-
|
|
201
|
-
meter.counterInc("x", 1);
|
|
202
|
-
await expect(meter.flush()).resolves.toBeUndefined();
|
|
203
|
-
expect(onError).toHaveBeenCalledWith("flush", expect.any(Error));
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it("non-200 HTTP response surfaces via onError but does not throw", async () => {
|
|
207
|
-
const onError = vi.fn();
|
|
208
|
-
const non200: typeof fetch = vi.fn(async () =>
|
|
209
|
-
new Response("oops", { status: 500 }),
|
|
210
|
-
) as unknown as typeof fetch;
|
|
211
|
-
const meter = buildAdapter({ fetchImpl: non200, onError });
|
|
212
|
-
meter.counterInc("x", 1);
|
|
213
|
-
await meter.flush();
|
|
214
|
-
expect(onError).toHaveBeenCalledWith("flush", expect.any(Error));
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it("registering a metric name as two different kinds drops the second + onError", () => {
|
|
218
|
-
const onError = vi.fn();
|
|
219
|
-
const meter = buildAdapter({ onError });
|
|
220
|
-
|
|
221
|
-
meter.counterInc("conflict", 1);
|
|
222
|
-
meter.gaugeSet?.("conflict", 7);
|
|
223
|
-
|
|
224
|
-
expect(onError).toHaveBeenCalledWith("kind-mismatch", expect.any(Error));
|
|
225
|
-
expect(meter.pendingDatapointCount()).toBe(1);
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
it("cooldown gates flushes; cooldown is bypassed once buffer reaches the cap", async () => {
|
|
229
|
-
let mockNow = 1_000_000;
|
|
230
|
-
const { impl, calls } = captureFetch();
|
|
231
|
-
const meter = buildAdapter({
|
|
232
|
-
fetchImpl: impl,
|
|
233
|
-
minFlushIntervalMs: 5000,
|
|
234
|
-
maxBufferDatapoints: 3,
|
|
235
|
-
nowMs: () => mockNow,
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
// 1st flush at t=0 — buffer has 1 entry, cooldown bypass via lastFlush=0
|
|
239
|
-
meter.counterInc("a", 1, { k: "1" });
|
|
240
|
-
await meter.flush();
|
|
241
|
-
expect(calls).toHaveLength(1);
|
|
242
|
-
|
|
243
|
-
// 2nd flush at t=2s — cooldown not elapsed AND buffer below cap → no-op
|
|
244
|
-
mockNow += 2000;
|
|
245
|
-
meter.counterInc("a", 1, { k: "2" });
|
|
246
|
-
await meter.flush();
|
|
247
|
-
expect(calls).toHaveLength(1);
|
|
248
|
-
|
|
249
|
-
// 3rd flush at t=2.5s — still under cooldown but buffer at cap (3) → flush
|
|
250
|
-
mockNow += 500;
|
|
251
|
-
meter.counterInc("a", 1, { k: "3" });
|
|
252
|
-
expect(meter.pendingDatapointCount()).toBe(3);
|
|
253
|
-
await meter.flush();
|
|
254
|
-
expect(calls).toHaveLength(2);
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
it("overflow drops new attribute-keys when buffer is at cap (existing keys still update)", () => {
|
|
258
|
-
const onError = vi.fn();
|
|
259
|
-
const meter = buildAdapter({ maxBufferDatapoints: 2, onError });
|
|
260
|
-
|
|
261
|
-
meter.counterInc("a", 1, { k: "1" });
|
|
262
|
-
meter.counterInc("a", 1, { k: "2" });
|
|
263
|
-
expect(meter.pendingDatapointCount()).toBe(2);
|
|
264
|
-
|
|
265
|
-
meter.counterInc("a", 1, { k: "3" });
|
|
266
|
-
expect(onError).toHaveBeenCalledWith("overflow", expect.any(Error));
|
|
267
|
-
expect(meter.pendingDatapointCount()).toBe(2);
|
|
268
|
-
|
|
269
|
-
// Existing key still updates (no new datapoint, just bumps the existing value).
|
|
270
|
-
meter.counterInc("a", 5, { k: "1" });
|
|
271
|
-
expect(meter.pendingDatapointCount()).toBe(2);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
it("concurrent flushes share a single in-flight POST", async () => {
|
|
275
|
-
let releaseFetch: ((res: Response) => void) | undefined;
|
|
276
|
-
const slow: typeof fetch = vi.fn(
|
|
277
|
-
() =>
|
|
278
|
-
new Promise<Response>((resolve) => {
|
|
279
|
-
releaseFetch = resolve;
|
|
280
|
-
}),
|
|
281
|
-
) as unknown as typeof fetch;
|
|
282
|
-
const meter = buildAdapter({ fetchImpl: slow });
|
|
283
|
-
|
|
284
|
-
meter.counterInc("x", 1);
|
|
285
|
-
const a = meter.flush();
|
|
286
|
-
const b = meter.flush();
|
|
287
|
-
expect(slow).toHaveBeenCalledTimes(1);
|
|
288
|
-
releaseFetch?.(new Response("{}", { status: 200 }));
|
|
289
|
-
await Promise.all([a, b]);
|
|
290
|
-
expect(slow).toHaveBeenCalledTimes(1);
|
|
291
|
-
});
|
|
292
|
-
});
|