@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,209 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
clearLoaderCache,
|
|
4
|
-
createCachedLoaderFromModule,
|
|
5
|
-
createLoaderEntry,
|
|
6
|
-
type LoaderModule,
|
|
7
|
-
} from "./cachedLoader";
|
|
8
|
-
import { RequestContext } from "./requestContext";
|
|
9
|
-
|
|
10
|
-
/** A loader whose `default` we can count calls on and control resolution of. */
|
|
11
|
-
function makeLoader<T>(result: T) {
|
|
12
|
-
let resolve!: (v: T) => void;
|
|
13
|
-
const gate = new Promise<T>((r) => (resolve = r));
|
|
14
|
-
const fn = vi.fn(async (_props: unknown, _req?: Request) => {
|
|
15
|
-
await gate;
|
|
16
|
-
return result;
|
|
17
|
-
});
|
|
18
|
-
return { fn, release: () => resolve(result) };
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
afterEach(() => {
|
|
22
|
-
clearLoaderCache();
|
|
23
|
-
vi.restoreAllMocks();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
describe("createCachedLoaderFromModule — dedup-only (no SWR)", () => {
|
|
27
|
-
it("runs on every call when the module has no `cache` export (apps no-store default)", async () => {
|
|
28
|
-
const { fn, release } = makeLoader("x");
|
|
29
|
-
release();
|
|
30
|
-
const mod: LoaderModule = { default: fn };
|
|
31
|
-
const loader = createCachedLoaderFromModule("site/loaders/plain", mod);
|
|
32
|
-
|
|
33
|
-
// No wrap at all → returns the original function identity.
|
|
34
|
-
expect(loader).toBe(fn);
|
|
35
|
-
|
|
36
|
-
await loader({ a: 1 });
|
|
37
|
-
await loader({ a: 1 });
|
|
38
|
-
expect(fn).toHaveBeenCalledTimes(2);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("runs on every call for `no-cache` (only stale-while-revalidate opts into dedup)", async () => {
|
|
42
|
-
const { fn, release } = makeLoader("x");
|
|
43
|
-
const mod: LoaderModule = { default: fn, cache: "no-cache" };
|
|
44
|
-
const loader = createCachedLoaderFromModule("site/loaders/nocache", mod);
|
|
45
|
-
|
|
46
|
-
expect(loader).toBe(fn);
|
|
47
|
-
const p1 = loader({ a: 1 });
|
|
48
|
-
const p2 = loader({ a: 1 });
|
|
49
|
-
release();
|
|
50
|
-
await Promise.all([p1, p2]);
|
|
51
|
-
expect(fn).toHaveBeenCalledTimes(2);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it("collapses concurrent identical calls into ONE upstream call when `cache` opts in", async () => {
|
|
55
|
-
const { fn, release } = makeLoader("shelf");
|
|
56
|
-
const mod: LoaderModule = { default: fn, cache: "stale-while-revalidate" };
|
|
57
|
-
const loader = createCachedLoaderFromModule("site/loaders/related", mod);
|
|
58
|
-
|
|
59
|
-
// Three sections on one render invoke the same loader concurrently.
|
|
60
|
-
const p1 = loader({ slug: "abc" });
|
|
61
|
-
const p2 = loader({ slug: "abc" });
|
|
62
|
-
const p3 = loader({ slug: "abc" });
|
|
63
|
-
release();
|
|
64
|
-
const [r1, r2, r3] = await Promise.all([p1, p2, p3]);
|
|
65
|
-
|
|
66
|
-
expect(r1).toBe("shelf");
|
|
67
|
-
expect(r2).toBe("shelf");
|
|
68
|
-
expect(r3).toBe("shelf");
|
|
69
|
-
// The #339 N+1 fix: 3 concurrent references → 1 upstream call.
|
|
70
|
-
expect(fn).toHaveBeenCalledTimes(1);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("does NOT retain results across settlement (no cross-request staleness)", async () => {
|
|
74
|
-
const { fn, release } = makeLoader("v");
|
|
75
|
-
release();
|
|
76
|
-
const mod: LoaderModule = { default: fn, cache: "stale-while-revalidate" };
|
|
77
|
-
const loader = createCachedLoaderFromModule("site/loaders/nostale", mod);
|
|
78
|
-
|
|
79
|
-
await loader({ slug: "abc" });
|
|
80
|
-
// Sequential (non-concurrent) call after the first settled → re-runs.
|
|
81
|
-
await loader({ slug: "abc" });
|
|
82
|
-
expect(fn).toHaveBeenCalledTimes(2);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("dedups by the module's custom cacheKey(props, req), not raw props", async () => {
|
|
86
|
-
const { fn, release } = makeLoader("p");
|
|
87
|
-
const mod: LoaderModule = {
|
|
88
|
-
default: fn,
|
|
89
|
-
cache: "stale-while-revalidate",
|
|
90
|
-
// Key only on pathname — ignores tracking params on the URL.
|
|
91
|
-
cacheKey: (_props, req) => new URL(req!.url).pathname,
|
|
92
|
-
};
|
|
93
|
-
const loader = createCachedLoaderFromModule("site/loaders/pdp", mod);
|
|
94
|
-
|
|
95
|
-
const reqA = new Request("https://x.test/produto?utm_source=a");
|
|
96
|
-
const reqB = new Request("https://x.test/produto?utm_source=b");
|
|
97
|
-
const p1 = loader({ id: 1 }, reqA);
|
|
98
|
-
const p2 = loader({ id: 2 }, reqB); // different props + url, same pathname
|
|
99
|
-
release();
|
|
100
|
-
await Promise.all([p1, p2]);
|
|
101
|
-
|
|
102
|
-
expect(fn).toHaveBeenCalledTimes(1);
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it("runs fresh (no dedup) when cacheKey returns null", async () => {
|
|
106
|
-
const { fn, release } = makeLoader("p");
|
|
107
|
-
const mod: LoaderModule = {
|
|
108
|
-
default: fn,
|
|
109
|
-
cache: "stale-while-revalidate",
|
|
110
|
-
cacheKey: () => null,
|
|
111
|
-
};
|
|
112
|
-
const loader = createCachedLoaderFromModule("site/loaders/personalized", mod);
|
|
113
|
-
|
|
114
|
-
const req = new Request("https://x.test/");
|
|
115
|
-
const p1 = loader({ id: 1 }, req);
|
|
116
|
-
const p2 = loader({ id: 1 }, req);
|
|
117
|
-
release();
|
|
118
|
-
await Promise.all([p1, p2]);
|
|
119
|
-
|
|
120
|
-
expect(fn).toHaveBeenCalledTimes(2);
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it("sources req from RequestContext when the caller passes none (CMS-resolution path)", async () => {
|
|
124
|
-
const { fn, release } = makeLoader("p");
|
|
125
|
-
const seen: string[] = [];
|
|
126
|
-
const mod: LoaderModule = {
|
|
127
|
-
default: fn,
|
|
128
|
-
cache: "stale-while-revalidate",
|
|
129
|
-
cacheKey: (_props, req) => {
|
|
130
|
-
seen.push(new URL(req!.url).pathname);
|
|
131
|
-
return new URL(req!.url).pathname;
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
const loader = createCachedLoaderFromModule("site/loaders/ctx", mod);
|
|
135
|
-
|
|
136
|
-
const req = new Request("https://x.test/categoria");
|
|
137
|
-
await RequestContext.run(req, async () => {
|
|
138
|
-
// Called with ONLY props, mirroring resolve.ts internalResolve.
|
|
139
|
-
const p1 = loader({ a: 1 });
|
|
140
|
-
const p2 = loader({ a: 1 });
|
|
141
|
-
release();
|
|
142
|
-
await Promise.all([p1, p2]);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
expect(seen).toContain("/categoria");
|
|
146
|
-
expect(fn).toHaveBeenCalledTimes(1);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("falls back to props-hash dedup when cacheKey needs req but none is available", async () => {
|
|
150
|
-
const { fn, release } = makeLoader("p");
|
|
151
|
-
const mod: LoaderModule = {
|
|
152
|
-
default: fn,
|
|
153
|
-
cache: "stale-while-revalidate",
|
|
154
|
-
// Would throw on undefined req — the wrapper must not call it without one.
|
|
155
|
-
cacheKey: (_props, req) => new URL(req!.url).pathname,
|
|
156
|
-
};
|
|
157
|
-
const loader = createCachedLoaderFromModule("site/loaders/noreq", mod);
|
|
158
|
-
|
|
159
|
-
// No req arg, no RequestContext scope.
|
|
160
|
-
const p1 = loader({ a: 1 });
|
|
161
|
-
const p2 = loader({ a: 1 });
|
|
162
|
-
release();
|
|
163
|
-
await expect(Promise.all([p1, p2])).resolves.toEqual(["p", "p"]);
|
|
164
|
-
expect(fn).toHaveBeenCalledTimes(1);
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
it("propagates loader errors and clears the in-flight entry", async () => {
|
|
168
|
-
const err = new Error("upstream down");
|
|
169
|
-
const fn = vi.fn(async () => {
|
|
170
|
-
throw err;
|
|
171
|
-
});
|
|
172
|
-
const mod: LoaderModule = { default: fn, cache: "stale-while-revalidate" };
|
|
173
|
-
const loader = createCachedLoaderFromModule("site/loaders/boom", mod);
|
|
174
|
-
|
|
175
|
-
await expect(loader({ a: 1 })).rejects.toThrow("upstream down");
|
|
176
|
-
// A retry after failure is not blocked by a stuck in-flight entry.
|
|
177
|
-
await expect(loader({ a: 1 })).rejects.toThrow("upstream down");
|
|
178
|
-
expect(fn).toHaveBeenCalledTimes(2);
|
|
179
|
-
});
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
describe("createLoaderEntry — lazy import + wrap", () => {
|
|
183
|
-
it("imports the module once and dedups concurrent calls", async () => {
|
|
184
|
-
const { fn, release } = makeLoader("lazy");
|
|
185
|
-
const importFn = vi.fn(async () => ({
|
|
186
|
-
default: fn,
|
|
187
|
-
cache: "stale-while-revalidate" as const,
|
|
188
|
-
}));
|
|
189
|
-
const entry = createLoaderEntry("site/loaders/lazy", importFn);
|
|
190
|
-
|
|
191
|
-
const p1 = entry({ a: 1 });
|
|
192
|
-
const p2 = entry({ a: 1 });
|
|
193
|
-
release();
|
|
194
|
-
await Promise.all([p1, p2]);
|
|
195
|
-
|
|
196
|
-
expect(importFn).toHaveBeenCalledTimes(1);
|
|
197
|
-
expect(fn).toHaveBeenCalledTimes(1);
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
it("passes non-opted-in loaders straight through", async () => {
|
|
201
|
-
const { fn, release } = makeLoader("plain");
|
|
202
|
-
release();
|
|
203
|
-
const entry = createLoaderEntry("site/loaders/plain", async () => ({ default: fn }));
|
|
204
|
-
|
|
205
|
-
await entry({ a: 1 });
|
|
206
|
-
await entry({ a: 1 });
|
|
207
|
-
expect(fn).toHaveBeenCalledTimes(2);
|
|
208
|
-
});
|
|
209
|
-
});
|
package/src/sdk/clx.ts
DELETED
package/src/sdk/cn.test.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import { clx, cn } from "./cn";
|
|
3
|
-
|
|
4
|
-
describe("cn", () => {
|
|
5
|
-
it("joins strings", () => {
|
|
6
|
-
expect(cn("a", "b", "c")).toBe("a b c");
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it("supports conditional objects", () => {
|
|
10
|
-
expect(cn("a", { b: true, c: false }, "d")).toBe("a b d");
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it("filters out falsy values", () => {
|
|
14
|
-
expect(cn("a", null, undefined, false, 0 as any, "b")).toBe("a b");
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it("merges conflicting Tailwind utilities (last one wins)", () => {
|
|
18
|
-
expect(cn("p-2", "p-4")).toBe("p-4");
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it("merges hover variants independently from base utilities", () => {
|
|
22
|
-
expect(cn("p-2", "hover:p-4")).toBe("p-2 hover:p-4");
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
describe("clx (re-exported)", () => {
|
|
27
|
-
it("filters falsy and joins with single spaces", () => {
|
|
28
|
-
expect(clx("a", null, "b", undefined, "c")).toBe("a b c");
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("does NOT merge conflicting utilities (that's cn's job)", () => {
|
|
32
|
-
expect(clx("p-2", "p-4")).toBe("p-2 p-4");
|
|
33
|
-
});
|
|
34
|
-
});
|
package/src/sdk/cn.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `cn(...inputs)` — the canonical Tailwind class-name combinator
|
|
3
|
-
* (clsx + tailwind-merge). Every storefront we audited shipped a
|
|
4
|
-
* site-local copy of this; we promote it to the framework so sites
|
|
5
|
-
* can drop the duplicate.
|
|
6
|
-
*
|
|
7
|
-
* Behaviour:
|
|
8
|
-
* - Accepts the full `clsx` input format (strings, objects, arrays,
|
|
9
|
-
* conditionals, falsy values).
|
|
10
|
-
* - De-duplicates conflicting Tailwind utilities via `tailwind-merge`
|
|
11
|
-
* (e.g. `cn("p-2", "p-4")` → `"p-4"`).
|
|
12
|
-
*
|
|
13
|
-
* The simpler `clx` (no tailwind-merge, just `filter+join`) is still
|
|
14
|
-
* exported from `@decocms/start/sdk/clx` for cases where you want to
|
|
15
|
-
* keep the literal class string. Re-exported here so a single import
|
|
16
|
-
* covers both.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { clsx, type ClassValue } from "clsx";
|
|
20
|
-
import { twMerge } from "tailwind-merge";
|
|
21
|
-
|
|
22
|
-
export { clx } from "./clx";
|
|
23
|
-
|
|
24
|
-
export function cn(...inputs: ClassValue[]): string {
|
|
25
|
-
return twMerge(clsx(inputs));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type { ClassValue };
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import type { MeterAdapter } from "../middleware/observability";
|
|
3
|
-
import { createCompositeLogger, createCompositeMeter } from "./composite";
|
|
4
|
-
import type { LoggerAdapter } from "./logger";
|
|
5
|
-
|
|
6
|
-
describe("createCompositeLogger", () => {
|
|
7
|
-
afterEach(() => vi.restoreAllMocks());
|
|
8
|
-
|
|
9
|
-
it("fans calls out to every adapter", () => {
|
|
10
|
-
const aCalls: any[][] = [];
|
|
11
|
-
const bCalls: any[][] = [];
|
|
12
|
-
const a: LoggerAdapter = { log: (...args) => void aCalls.push(args) };
|
|
13
|
-
const b: LoggerAdapter = { log: (...args) => void bCalls.push(args) };
|
|
14
|
-
const composite = createCompositeLogger([a, b]);
|
|
15
|
-
|
|
16
|
-
composite.log("info", "hello", { foo: 1 });
|
|
17
|
-
|
|
18
|
-
expect(aCalls).toEqual([["info", "hello", { foo: 1 }]]);
|
|
19
|
-
expect(bCalls).toEqual([["info", "hello", { foo: 1 }]]);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("filters falsy entries (null/undefined/false)", () => {
|
|
23
|
-
const calls: any[] = [];
|
|
24
|
-
const a: LoggerAdapter = { log: (...args) => void calls.push(args) };
|
|
25
|
-
const composite = createCompositeLogger([null, a, undefined, false]);
|
|
26
|
-
composite.log("info", "ok");
|
|
27
|
-
// adapter receives the call with no third arg (undefined is omitted)
|
|
28
|
-
expect(calls.length).toBe(1);
|
|
29
|
-
expect(calls[0][0]).toBe("info");
|
|
30
|
-
expect(calls[0][1]).toBe("ok");
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("isolates errors so one bad adapter does not block others", () => {
|
|
34
|
-
const errSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
35
|
-
const okCalls: any[][] = [];
|
|
36
|
-
const broken: LoggerAdapter = {
|
|
37
|
-
log() {
|
|
38
|
-
throw new Error("boom");
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
const ok: LoggerAdapter = { log: (...args) => void okCalls.push(args) };
|
|
42
|
-
const composite = createCompositeLogger([broken, ok]);
|
|
43
|
-
|
|
44
|
-
expect(() => composite.log("error", "still goes through")).not.toThrow();
|
|
45
|
-
expect(okCalls.length).toBe(1);
|
|
46
|
-
expect(okCalls[0][0]).toBe("error");
|
|
47
|
-
expect(okCalls[0][1]).toBe("still goes through");
|
|
48
|
-
expect(errSpy).toHaveBeenCalled(); // composite reports the failure
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it("returns the single adapter directly when only one is provided", () => {
|
|
52
|
-
const a: LoggerAdapter = { log: () => {} };
|
|
53
|
-
expect(createCompositeLogger([a])).toBe(a);
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
describe("createCompositeMeter", () => {
|
|
58
|
-
afterEach(() => vi.restoreAllMocks());
|
|
59
|
-
|
|
60
|
-
function recorder() {
|
|
61
|
-
const counter: any[] = [];
|
|
62
|
-
const gauge: any[] = [];
|
|
63
|
-
const histo: any[] = [];
|
|
64
|
-
const meter: MeterAdapter = {
|
|
65
|
-
counterInc: (n, v, l) => void counter.push([n, v, l]),
|
|
66
|
-
gaugeSet: (n, v, l) => void gauge.push([n, v, l]),
|
|
67
|
-
histogramRecord: (n, v, l) => void histo.push([n, v, l]),
|
|
68
|
-
};
|
|
69
|
-
return { meter, counter, gauge, histo };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
it("fans counter/gauge/histogram across meters", () => {
|
|
73
|
-
const a = recorder();
|
|
74
|
-
const b = recorder();
|
|
75
|
-
const composite = createCompositeMeter([a.meter, b.meter]);
|
|
76
|
-
|
|
77
|
-
composite.counterInc("c", 1, { p: "/" });
|
|
78
|
-
composite.gaugeSet?.("g", 5);
|
|
79
|
-
composite.histogramRecord?.("h", 100, { route: "/p" });
|
|
80
|
-
|
|
81
|
-
expect(a.counter[0].slice(0, 2)).toEqual(["c", 1]);
|
|
82
|
-
expect(a.counter[0][2]).toEqual({ p: "/" });
|
|
83
|
-
expect(b.counter[0].slice(0, 2)).toEqual(["c", 1]);
|
|
84
|
-
expect(a.gauge[0].slice(0, 2)).toEqual(["g", 5]);
|
|
85
|
-
expect(b.gauge[0].slice(0, 2)).toEqual(["g", 5]);
|
|
86
|
-
expect(a.histo[0]).toEqual(["h", 100, { route: "/p" }]);
|
|
87
|
-
expect(b.histo[0]).toEqual(["h", 100, { route: "/p" }]);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("isolates errors per meter and per call type", () => {
|
|
91
|
-
vi.spyOn(console, "error").mockImplementation(() => {});
|
|
92
|
-
const broken: MeterAdapter = {
|
|
93
|
-
counterInc: () => {
|
|
94
|
-
throw new Error("c");
|
|
95
|
-
},
|
|
96
|
-
gaugeSet: () => {
|
|
97
|
-
throw new Error("g");
|
|
98
|
-
},
|
|
99
|
-
histogramRecord: () => {
|
|
100
|
-
throw new Error("h");
|
|
101
|
-
},
|
|
102
|
-
};
|
|
103
|
-
const ok = recorder();
|
|
104
|
-
const composite = createCompositeMeter([broken, ok.meter]);
|
|
105
|
-
|
|
106
|
-
composite.counterInc("c", 1);
|
|
107
|
-
composite.gaugeSet?.("g", 5);
|
|
108
|
-
composite.histogramRecord?.("h", 100);
|
|
109
|
-
|
|
110
|
-
expect(ok.counter[0].slice(0, 2)).toEqual(["c", 1]);
|
|
111
|
-
expect(ok.gauge[0].slice(0, 2)).toEqual(["g", 5]);
|
|
112
|
-
expect(ok.histo[0].slice(0, 2)).toEqual(["h", 100]);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("skips meters that don't implement optional ops", () => {
|
|
116
|
-
const onlyCounter: MeterAdapter = { counterInc: () => {} };
|
|
117
|
-
const composite = createCompositeMeter([onlyCounter]);
|
|
118
|
-
expect(() => composite.gaugeSet?.("g", 1)).not.toThrow();
|
|
119
|
-
expect(() => composite.histogramRecord?.("h", 1)).not.toThrow();
|
|
120
|
-
});
|
|
121
|
-
});
|
package/src/sdk/composite.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Composite adapters — fan out to multiple backends with try/catch isolation.
|
|
3
|
-
*
|
|
4
|
-
* Used by `instrumentWorker()` so the same logger/meter can dual-emit to
|
|
5
|
-
* (e.g.) console-JSON + OTLP, or AE + OTLP, without any one backend's
|
|
6
|
-
* failure taking down the others.
|
|
7
|
-
*
|
|
8
|
-
* The "always include console-JSON logger" guarantee in the observability
|
|
9
|
-
* plan relies on this: even if HyperDX is down, the local console adapter
|
|
10
|
-
* still records the line.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import type { MeterAdapter } from "../middleware/observability";
|
|
14
|
-
import type { LoggerAdapter, LogLevel } from "./logger";
|
|
15
|
-
|
|
16
|
-
type Labels = Record<string, string | number | boolean>;
|
|
17
|
-
|
|
18
|
-
// ---------------------------------------------------------------------------
|
|
19
|
-
// Logger
|
|
20
|
-
// ---------------------------------------------------------------------------
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Returns a single LoggerAdapter that fans every call out to all
|
|
24
|
-
* provided adapters. Falsy entries (e.g. `otelEnabled ? otelAdapter : null`)
|
|
25
|
-
* are filtered out so call sites can write:
|
|
26
|
-
*
|
|
27
|
-
* ```ts
|
|
28
|
-
* createCompositeLogger([defaultLoggerAdapter, otelEnabled ? otelLogger : null]);
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* Each downstream `.log()` call is isolated in try/catch so a thrown
|
|
32
|
-
* error in one backend cannot suppress the others.
|
|
33
|
-
*/
|
|
34
|
-
export function createCompositeLogger(
|
|
35
|
-
adapters: Array<LoggerAdapter | null | undefined | false>,
|
|
36
|
-
): LoggerAdapter {
|
|
37
|
-
const list = adapters.filter((a): a is LoggerAdapter => Boolean(a));
|
|
38
|
-
|
|
39
|
-
if (list.length === 1) return list[0];
|
|
40
|
-
|
|
41
|
-
return {
|
|
42
|
-
log(level: LogLevel, msg: string, attrs?: Record<string, unknown>) {
|
|
43
|
-
for (const adapter of list) {
|
|
44
|
-
try {
|
|
45
|
-
adapter.log(level, msg, attrs);
|
|
46
|
-
} catch (error) {
|
|
47
|
-
// Use console.error directly — calling logger here would recurse.
|
|
48
|
-
try {
|
|
49
|
-
console.error("[composite-logger] adapter failed", String(error));
|
|
50
|
-
} catch {
|
|
51
|
-
/* swallow */
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// ---------------------------------------------------------------------------
|
|
60
|
-
// Meter
|
|
61
|
-
// ---------------------------------------------------------------------------
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Returns a single MeterAdapter that fans counter/gauge/histogram writes
|
|
65
|
-
* out to all provided meters. Same semantics as `createCompositeLogger`:
|
|
66
|
-
* falsy entries are dropped, each downstream call is try/catch isolated.
|
|
67
|
-
*/
|
|
68
|
-
export function createCompositeMeter(
|
|
69
|
-
adapters: Array<MeterAdapter | null | undefined | false>,
|
|
70
|
-
): MeterAdapter {
|
|
71
|
-
const list = adapters.filter((a): a is MeterAdapter => Boolean(a));
|
|
72
|
-
|
|
73
|
-
if (list.length === 1) return list[0];
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
counterInc(name: string, value?: number, labels?: Labels) {
|
|
77
|
-
for (const m of list) {
|
|
78
|
-
try {
|
|
79
|
-
m.counterInc(name, value, labels);
|
|
80
|
-
} catch (error) {
|
|
81
|
-
warn("counterInc", name, error);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
gaugeSet(name: string, value: number, labels?: Labels) {
|
|
86
|
-
for (const m of list) {
|
|
87
|
-
if (!m.gaugeSet) continue;
|
|
88
|
-
try {
|
|
89
|
-
m.gaugeSet(name, value, labels);
|
|
90
|
-
} catch (error) {
|
|
91
|
-
warn("gaugeSet", name, error);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
histogramRecord(name: string, value: number, labels?: Labels) {
|
|
96
|
-
for (const m of list) {
|
|
97
|
-
if (!m.histogramRecord) continue;
|
|
98
|
-
try {
|
|
99
|
-
m.histogramRecord(name, value, labels);
|
|
100
|
-
} catch (error) {
|
|
101
|
-
warn("histogramRecord", name, error);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function warn(op: string, metric: string, error: unknown) {
|
|
109
|
-
try {
|
|
110
|
-
console.error(`[composite-meter] ${op}(${metric}) adapter failed`, String(error));
|
|
111
|
-
} catch {
|
|
112
|
-
/* swallow */
|
|
113
|
-
}
|
|
114
|
-
}
|
package/src/sdk/cookie.test.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
deleteResponseCookie,
|
|
4
|
-
getCookies,
|
|
5
|
-
setResponseCookie,
|
|
6
|
-
} from "./cookie";
|
|
7
|
-
|
|
8
|
-
describe("getCookies", () => {
|
|
9
|
-
it("returns an empty object when no Cookie header is present", () => {
|
|
10
|
-
expect(getCookies(new Headers())).toEqual({});
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it("parses a single cookie", () => {
|
|
14
|
-
const h = new Headers({ cookie: "session=abc" });
|
|
15
|
-
expect(getCookies(h)).toEqual({ session: "abc" });
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("parses multiple cookies separated by '; '", () => {
|
|
19
|
-
const h = new Headers({ cookie: "a=1; b=2; c=3" });
|
|
20
|
-
expect(getCookies(h)).toEqual({ a: "1", b: "2", c: "3" });
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("URL-decodes values", () => {
|
|
24
|
-
const h = new Headers({ cookie: "u=hello%20world" });
|
|
25
|
-
expect(getCookies(h)).toEqual({ u: "hello world" });
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it("falls back to the raw value when decoding fails", () => {
|
|
29
|
-
// Lone '%' is invalid in URL encoding.
|
|
30
|
-
const h = new Headers({ cookie: "x=100%" });
|
|
31
|
-
expect(getCookies(h)).toEqual({ x: "100%" });
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("ignores entries without an '='", () => {
|
|
35
|
-
const h = new Headers({ cookie: "garbage; ok=yes" });
|
|
36
|
-
expect(getCookies(h)).toEqual({ ok: "yes" });
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("trims whitespace around names", () => {
|
|
40
|
-
const h = new Headers({ cookie: " a=1; b=2 " });
|
|
41
|
-
expect(getCookies(h)).toEqual({ a: "1", b: "2" });
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
describe("setResponseCookie", () => {
|
|
46
|
-
it("appends a Set-Cookie header with the cookie name and value", () => {
|
|
47
|
-
const h = new Headers();
|
|
48
|
-
setResponseCookie(h, { name: "session", value: "abc" });
|
|
49
|
-
expect(h.get("set-cookie")).toBe("session=abc");
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it("serializes maxAge, path, secure, httpOnly, sameSite, domain", () => {
|
|
53
|
-
const h = new Headers();
|
|
54
|
-
setResponseCookie(h, {
|
|
55
|
-
name: "session",
|
|
56
|
-
value: "abc",
|
|
57
|
-
maxAge: 3600,
|
|
58
|
-
path: "/",
|
|
59
|
-
domain: "example.com",
|
|
60
|
-
secure: true,
|
|
61
|
-
httpOnly: true,
|
|
62
|
-
sameSite: "Lax",
|
|
63
|
-
});
|
|
64
|
-
const value = h.get("set-cookie")!;
|
|
65
|
-
expect(value).toContain("session=abc");
|
|
66
|
-
expect(value).toContain("Max-Age=3600");
|
|
67
|
-
expect(value).toContain("Domain=example.com");
|
|
68
|
-
expect(value).toContain("Path=/");
|
|
69
|
-
expect(value).toContain("Secure");
|
|
70
|
-
expect(value).toContain("HttpOnly");
|
|
71
|
-
expect(value).toContain("SameSite=Lax");
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("serializes expires as a UTC string", () => {
|
|
75
|
-
const h = new Headers();
|
|
76
|
-
const date = new Date("2030-01-01T00:00:00Z");
|
|
77
|
-
setResponseCookie(h, { name: "x", value: "y", expires: date });
|
|
78
|
-
expect(h.get("set-cookie")).toContain(`Expires=${date.toUTCString()}`);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it("appends multiple cookies (does not overwrite the first)", () => {
|
|
82
|
-
const h = new Headers();
|
|
83
|
-
setResponseCookie(h, { name: "a", value: "1" });
|
|
84
|
-
setResponseCookie(h, { name: "b", value: "2" });
|
|
85
|
-
// Headers.getAll isn't standard; getSetCookie() is the modern API.
|
|
86
|
-
const all = (h as any).getSetCookie?.() as string[] | undefined;
|
|
87
|
-
if (all) {
|
|
88
|
-
expect(all).toEqual(["a=1", "b=2"]);
|
|
89
|
-
} else {
|
|
90
|
-
// Fallback: the combined header value should mention both.
|
|
91
|
-
const v = h.get("set-cookie")!;
|
|
92
|
-
expect(v).toContain("a=1");
|
|
93
|
-
expect(v).toContain("b=2");
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
describe("deleteResponseCookie", () => {
|
|
99
|
-
it("emits a Set-Cookie that expires immediately", () => {
|
|
100
|
-
const h = new Headers();
|
|
101
|
-
deleteResponseCookie(h, "session", { path: "/" });
|
|
102
|
-
const value = h.get("set-cookie")!;
|
|
103
|
-
expect(value).toContain("session=");
|
|
104
|
-
expect(value).toContain("Max-Age=0");
|
|
105
|
-
expect(value).toContain(`Expires=${new Date(0).toUTCString()}`);
|
|
106
|
-
expect(value).toContain("Path=/");
|
|
107
|
-
});
|
|
108
|
-
});
|