@decocms/blocks 8.0.0 → 8.1.0-next.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/bin/deco.js +13 -0
- package/package.json +39 -3
- package/src/cms/applySectionConventions.ts +26 -2
- package/src/cms/blockSource.ts +37 -0
- package/src/cms/client.ts +5 -0
- package/src/cms/deferredTrigger.ts +58 -0
- package/src/cms/draftSource.ts +171 -57
- package/src/cms/index.ts +32 -3
- package/src/cms/loader.ts +318 -59
- package/src/cms/registry.ts +22 -0
- package/src/cms/renderJson.ts +179 -0
- package/src/cms/resolve.ts +396 -95
- package/src/cms/schema.ts +9 -5
- package/src/cms/sectionLoaders.ts +106 -56
- package/src/hooks/Image.tsx +70 -6
- package/src/hooks/LiveControls.tsx +30 -8
- package/src/hooks/Stats.tsx +134 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/useLoadMore.ts +100 -0
- package/src/index.ts +37 -0
- package/src/middleware/observability.ts +173 -16
- package/src/preview/DraftPreviewBadge.tsx +306 -0
- package/src/preview/decoMark.ts +11 -0
- package/src/preview/index.ts +8 -0
- 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/sdk/abTesting.ts +60 -3
- package/src/sdk/analytics.ts +72 -2
- package/src/sdk/cacheHeaders.ts +210 -10
- package/src/sdk/cacheStorage.ts +287 -0
- package/src/sdk/cachedLoader.ts +143 -90
- package/src/sdk/crypto.ts +16 -2
- package/src/sdk/deepOmit.ts +52 -0
- package/src/sdk/encoding.ts +2 -5
- package/src/sdk/experiments.ts +314 -0
- package/src/sdk/fetchCache.ts +269 -0
- package/src/sdk/fetchTimeout.ts +58 -0
- package/src/sdk/flags.ts +38 -8
- package/src/sdk/index.ts +7 -0
- package/src/sdk/instrumentedFetch.ts +25 -2
- package/src/sdk/nonce.ts +46 -0
- package/src/sdk/normalizeUrls.ts +2 -2
- package/src/sdk/observability.ts +8 -1
- package/src/sdk/otel.ts +131 -25
- package/src/sdk/otelHttpLog.ts +4 -4
- package/src/sdk/otelHttpMeter.ts +39 -13
- package/src/sdk/otelHttpTracer.ts +4 -4
- package/src/sdk/redirects.ts +113 -5
- package/src/sdk/responseCache.ts +82 -0
- package/src/sdk/urlUtils.ts +49 -0
- package/src/sdk/useSuggestions.ts +1 -1
- 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 +152 -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/blockSource.test.ts +0 -76
- package/src/cms/client.browserBundle.test.ts +0 -44
- package/src/cms/draftSource.test.ts +0 -343
- package/src/cms/layoutCacheRace.test.ts +0 -181
- package/src/cms/loadDecofileDirectory.test.ts +0 -109
- package/src/cms/loader.test.ts +0 -200
- package/src/cms/registry.test.ts +0 -110
- package/src/cms/resolve.test.ts +0 -980
- package/src/cms/schema.test.ts +0 -356
- package/src/cms/sectionLoaderContext.test.ts +0 -111
- package/src/cms/sectionLoaders.test.ts +0 -533
- package/src/cms/sectionMixins.test.ts +0 -167
- package/src/cms/stickyFlags.test.ts +0 -117
- package/src/flags/flags.test.ts +0 -148
- package/src/hooks/Image.test.ts +0 -100
- package/src/hooks/RenderSection.test.tsx +0 -81
- package/src/matchers/builtins.test.ts +0 -318
- package/src/matchers/override.test.ts +0 -205
- package/src/middleware/hydrationContext.test.ts +0 -61
- package/src/middleware/observability.test.ts +0 -242
- package/src/middleware/validateSection.test.ts +0 -147
- package/src/sdk/abTesting.test.ts +0 -326
- package/src/sdk/cacheHeaders.test.ts +0 -115
- package/src/sdk/cachedLoader.test.ts +0 -122
- package/src/sdk/cachedLoaderModule.test.ts +0 -209
- package/src/sdk/cn.test.ts +0 -34
- package/src/sdk/composite.test.ts +0 -121
- package/src/sdk/cookie.test.ts +0 -108
- package/src/sdk/encoding.test.ts +0 -71
- package/src/sdk/env.test.ts +0 -33
- package/src/sdk/flags.test.ts +0 -91
- package/src/sdk/http.test.ts +0 -71
- package/src/sdk/inflightTimeout.test.ts +0 -35
- package/src/sdk/instrumentedFetch.test.ts +0 -559
- package/src/sdk/invoke.test.ts +0 -115
- package/src/sdk/logger.test.ts +0 -432
- package/src/sdk/otel.test.ts +0 -526
- package/src/sdk/otelAdapters.test.ts +0 -89
- package/src/sdk/otelHttpLog.test.ts +0 -457
- package/src/sdk/otelHttpMeter.test.ts +0 -292
- package/src/sdk/otelHttpTracer.test.ts +0 -474
- package/src/sdk/requestContextStorage.browser.test.ts +0 -29
- package/src/sdk/urlRedaction.test.ts +0 -73
- package/src/sdk/useDevice.test.ts +0 -130
- package/src/sdk/useScript.test.ts +0 -128
- package/src/sdk/useSuggestions.test.ts +0 -230
- package/tsconfig.json +0 -7
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it } from "vitest";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
clearDraftCache,
|
|
5
|
-
DEFAULT_PREVIEW_API_DOMAINS,
|
|
6
|
-
draftPointerFromRequest,
|
|
7
|
-
isDraftHostAllowed,
|
|
8
|
-
isDraftPreviewEnabled,
|
|
9
|
-
parseDraftPointer,
|
|
10
|
-
previewApiOriginForHost,
|
|
11
|
-
resolveDraftDecofile,
|
|
12
|
-
resolveDraftForRequest,
|
|
13
|
-
setDraftPreviewHosts,
|
|
14
|
-
} from "./draftSource";
|
|
15
|
-
|
|
16
|
-
const ENV_ON = { DECO_ALLOWED_PREVIEW_HOSTS: "preview.example" };
|
|
17
|
-
|
|
18
|
-
function jsonResponse(body: unknown, init?: ResponseInit): Response {
|
|
19
|
-
return new Response(JSON.stringify(body), {
|
|
20
|
-
status: 200,
|
|
21
|
-
headers: { "content-type": "application/json" },
|
|
22
|
-
...init,
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
beforeEach(() => {
|
|
27
|
-
clearDraftCache();
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe("parseDraftPointer", () => {
|
|
31
|
-
it("parses authority@version, lowercasing the authority", () => {
|
|
32
|
-
expect(parseDraftPointer("ABC.Preview-Studio.decocms.com@FF00")).toEqual({
|
|
33
|
-
host: "abc.preview-studio.decocms.com",
|
|
34
|
-
version: "FF00",
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("keeps an explicit port on the authority", () => {
|
|
39
|
-
expect(parseDraftPointer("abc.localhost:60534@v1")).toEqual({
|
|
40
|
-
host: "abc.localhost:60534",
|
|
41
|
-
version: "v1",
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("rejects more than one @", () => {
|
|
46
|
-
// A naive split("@") accepts this and silently uses the first two
|
|
47
|
-
// segments — the exact hole found while spiking the fetch path.
|
|
48
|
-
expect(parseDraftPointer("a.example@b@c")).toBeNull();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it("rejects anything that could escape the authority", () => {
|
|
52
|
-
// No scheme, no path, no userinfo — the token carries an authority only,
|
|
53
|
-
// so `javascript:`/`file:`/full URLs fail structurally at parse time.
|
|
54
|
-
expect(parseDraftPointer("https://evil.example@v1")).toBeNull();
|
|
55
|
-
expect(parseDraftPointer("evil.example/x@v1")).toBeNull();
|
|
56
|
-
expect(parseDraftPointer("a.example:80:80@v1")).toBeNull();
|
|
57
|
-
expect(parseDraftPointer("a.example:abc@v1")).toBeNull();
|
|
58
|
-
expect(parseDraftPointer(".leading.dot@v1")).toBeNull();
|
|
59
|
-
expect(parseDraftPointer("bare-label@v1")).toBeNull();
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it("validates the version charset — it becomes a cache key", () => {
|
|
63
|
-
expect(parseDraftPointer("a.example@")).toBeNull();
|
|
64
|
-
expect(parseDraftPointer(`a.example@${"x".repeat(65)}`)).toBeNull();
|
|
65
|
-
expect(parseDraftPointer("a.example@v 1")).toBeNull();
|
|
66
|
-
expect(parseDraftPointer(null)).toBeNull();
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
describe("previewApiOriginForHost", () => {
|
|
71
|
-
it("admits authorities under the default deco domains", () => {
|
|
72
|
-
expect(previewApiOriginForHost("abc.preview-studio.decocms.com", {})).toBe(
|
|
73
|
-
"https://abc.preview-studio.decocms.com",
|
|
74
|
-
);
|
|
75
|
-
expect(previewApiOriginForHost("abc.local.studio.decocms.com", {})).toBe(
|
|
76
|
-
"https://abc.local.studio.decocms.com",
|
|
77
|
-
);
|
|
78
|
-
expect(previewApiOriginForHost("abc.localhost:60534", {})).toBe("http://abc.localhost:60534");
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it("rejects hosts outside the domains — the token proposes, config disposes", () => {
|
|
82
|
-
expect(previewApiOriginForHost("evil.example", {})).toBeNull();
|
|
83
|
-
// Dot-prefixed suffixes guarantee a label boundary: a lookalike domain
|
|
84
|
-
// that merely ends with the same characters cannot pass.
|
|
85
|
-
expect(previewApiOriginForHost("evil-preview-studio.decocms.com", {})).toBeNull();
|
|
86
|
-
// The domain itself (no label in front) is not a draft host.
|
|
87
|
-
expect(previewApiOriginForHost("preview-studio.decocms.com", {})).toBeNull();
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("allows an explicit port only under localhost-ish domains", () => {
|
|
91
|
-
// A public-domain token must not steer the fetch at odd ports.
|
|
92
|
-
expect(previewApiOriginForHost("abc.preview-studio.decocms.com:8500", {})).toBeNull();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("honours a configured override instead of the defaults", () => {
|
|
96
|
-
const env = { DECO_PREVIEW_API_DOMAINS: ".staging.example" };
|
|
97
|
-
expect(previewApiOriginForHost("abc.staging.example", env)).toBe("https://abc.staging.example");
|
|
98
|
-
expect(previewApiOriginForHost("abc.preview-studio.decocms.com", env)).toBeNull();
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
describe("gating", () => {
|
|
103
|
-
it("is on iff an allowed host is configured — API domains have defaults", () => {
|
|
104
|
-
expect(isDraftPreviewEnabled(ENV_ON)).toBe(true);
|
|
105
|
-
expect(isDraftPreviewEnabled({})).toBe(false);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it("matches request hosts verbatim, port included, case-insensitively", () => {
|
|
109
|
-
const env = { DECO_ALLOWED_PREVIEW_HOSTS: "fila.vtex.app, localhost:3100" };
|
|
110
|
-
expect(isDraftHostAllowed("FILA.VTEX.APP", env)).toBe(true);
|
|
111
|
-
expect(isDraftHostAllowed("localhost:3100", env)).toBe(true);
|
|
112
|
-
expect(isDraftHostAllowed("fila.com.br", env)).toBe(false);
|
|
113
|
-
expect(isDraftHostAllowed("localhost", env)).toBe(false);
|
|
114
|
-
expect(isDraftHostAllowed(null, env)).toBe(false);
|
|
115
|
-
expect(isDraftHostAllowed("fila.vtex.app", {})).toBe(false);
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
describe("resolveDraftDecofile", () => {
|
|
120
|
-
it("fetches exactly the token's validated origin", async () => {
|
|
121
|
-
const calls: string[] = [];
|
|
122
|
-
const blocks = await resolveDraftDecofile({
|
|
123
|
-
pointer: "abc.preview-studio.decocms.com@v1",
|
|
124
|
-
env: ENV_ON,
|
|
125
|
-
fetchImpl: (async (url: string) => {
|
|
126
|
-
calls.push(String(url));
|
|
127
|
-
return jsonResponse({ "pages-home": { title: "draft" } });
|
|
128
|
-
}) as unknown as typeof fetch,
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
expect(blocks).toEqual({ "pages-home": { title: "draft" } });
|
|
132
|
-
expect(calls).toEqual(["https://abc.preview-studio.decocms.com/_sandbox/decofile"]);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
it("is inert without a host allowlist — no fetch at all", async () => {
|
|
136
|
-
let called = false;
|
|
137
|
-
const blocks = await resolveDraftDecofile({
|
|
138
|
-
pointer: "abc.preview-studio.decocms.com@v1",
|
|
139
|
-
env: {},
|
|
140
|
-
fetchImpl: (async () => {
|
|
141
|
-
called = true;
|
|
142
|
-
return jsonResponse({});
|
|
143
|
-
}) as unknown as typeof fetch,
|
|
144
|
-
});
|
|
145
|
-
expect(blocks).toBeNull();
|
|
146
|
-
expect(called).toBe(false);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("refuses a parseable token whose origin no domain admits — no fetch", async () => {
|
|
150
|
-
let called = false;
|
|
151
|
-
const blocks = await resolveDraftDecofile({
|
|
152
|
-
pointer: "abc.evil.example@v1",
|
|
153
|
-
env: ENV_ON,
|
|
154
|
-
fetchImpl: (async () => {
|
|
155
|
-
called = true;
|
|
156
|
-
return jsonResponse({});
|
|
157
|
-
}) as unknown as typeof fetch,
|
|
158
|
-
});
|
|
159
|
-
expect(blocks).toBeNull();
|
|
160
|
-
expect(called).toBe(false);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it("caches by version — one fetch per version, not per request", async () => {
|
|
164
|
-
let fetches = 0;
|
|
165
|
-
const fetchImpl = (async () => {
|
|
166
|
-
fetches++;
|
|
167
|
-
return jsonResponse({ n: fetches });
|
|
168
|
-
}) as unknown as typeof fetch;
|
|
169
|
-
const P = "abc.preview-studio.decocms.com";
|
|
170
|
-
|
|
171
|
-
const a = await resolveDraftDecofile({ pointer: `${P}@v1`, env: ENV_ON, fetchImpl });
|
|
172
|
-
const b = await resolveDraftDecofile({ pointer: `${P}@v1`, env: ENV_ON, fetchImpl });
|
|
173
|
-
expect(fetches).toBe(1);
|
|
174
|
-
expect(b).toBe(a);
|
|
175
|
-
|
|
176
|
-
await resolveDraftDecofile({ pointer: `${P}@v2`, env: ENV_ON, fetchImpl });
|
|
177
|
-
expect(fetches).toBe(2);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it("bounds the cache so multi-MB decofiles can't accumulate", async () => {
|
|
181
|
-
let fetches = 0;
|
|
182
|
-
const fetchImpl = (async () => {
|
|
183
|
-
fetches++;
|
|
184
|
-
return jsonResponse({ n: fetches });
|
|
185
|
-
}) as unknown as typeof fetch;
|
|
186
|
-
const P = "abc.preview-studio.decocms.com";
|
|
187
|
-
|
|
188
|
-
for (const v of ["v1", "v2", "v3", "v4"]) {
|
|
189
|
-
await resolveDraftDecofile({ pointer: `${P}@${v}`, env: ENV_ON, fetchImpl });
|
|
190
|
-
}
|
|
191
|
-
expect(fetches).toBe(4);
|
|
192
|
-
await resolveDraftDecofile({ pointer: `${P}@v1`, env: ENV_ON, fetchImpl });
|
|
193
|
-
expect(fetches).toBe(5); // v1 evicted (cap 3) — re-fetch, never stale
|
|
194
|
-
await resolveDraftDecofile({ pointer: `${P}@v4`, env: ENV_ON, fetchImpl });
|
|
195
|
-
expect(fetches).toBe(5); // v4 resident
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
it("degrades to published on unreachable / non-2xx / unparseable", async () => {
|
|
199
|
-
const P = "abc.preview-studio.decocms.com";
|
|
200
|
-
for (const fetchImpl of [
|
|
201
|
-
async () => {
|
|
202
|
-
throw new Error("ECONNREFUSED");
|
|
203
|
-
},
|
|
204
|
-
async () => new Response("nope", { status: 404 }),
|
|
205
|
-
async () =>
|
|
206
|
-
new Response("<html>not json</html>", {
|
|
207
|
-
status: 200,
|
|
208
|
-
headers: { "content-type": "text/html" },
|
|
209
|
-
}),
|
|
210
|
-
]) {
|
|
211
|
-
clearDraftCache();
|
|
212
|
-
expect(
|
|
213
|
-
await resolveDraftDecofile({
|
|
214
|
-
pointer: `${P}@v1`,
|
|
215
|
-
env: ENV_ON,
|
|
216
|
-
fetchImpl: fetchImpl as unknown as typeof fetch,
|
|
217
|
-
}),
|
|
218
|
-
).toBeNull();
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
// DEFAULT_PREVIEW_API_DOMAINS is part of the public contract — pin it.
|
|
224
|
-
describe("DEFAULT_PREVIEW_API_DOMAINS", () => {
|
|
225
|
-
it("ships the deco-operated origins, dot-prefixed", () => {
|
|
226
|
-
expect(DEFAULT_PREVIEW_API_DOMAINS).toEqual([
|
|
227
|
-
".preview-studio.decocms.com",
|
|
228
|
-
".local.studio.decocms.com",
|
|
229
|
-
".localhost",
|
|
230
|
-
]);
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
describe("site-block preview hosts", () => {
|
|
235
|
-
it("enables the feature from the site block alone — no env needed", () => {
|
|
236
|
-
setDraftPreviewHosts(["fila.vtex.app", "LOCALHOST:3100", 42, " "]);
|
|
237
|
-
try {
|
|
238
|
-
expect(isDraftPreviewEnabled({})).toBe(true);
|
|
239
|
-
expect(isDraftHostAllowed("fila.vtex.app", {})).toBe(true);
|
|
240
|
-
// Sanitized: lowercased, non-strings and blanks dropped.
|
|
241
|
-
expect(isDraftHostAllowed("localhost:3100", {})).toBe(true);
|
|
242
|
-
expect(isDraftHostAllowed("evil.example", {})).toBe(false);
|
|
243
|
-
} finally {
|
|
244
|
-
setDraftPreviewHosts([]);
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
it("env REPLACES the block hosts when set — the operational escape hatch", () => {
|
|
249
|
-
setDraftPreviewHosts(["fila.vtex.app"]);
|
|
250
|
-
try {
|
|
251
|
-
const env = { DECO_ALLOWED_PREVIEW_HOSTS: "other.example" };
|
|
252
|
-
expect(isDraftHostAllowed("other.example", env)).toBe(true);
|
|
253
|
-
// Not merged: env is a kill switch / override, so the block value must
|
|
254
|
-
// not survive alongside it.
|
|
255
|
-
expect(isDraftHostAllowed("fila.vtex.app", env)).toBe(false);
|
|
256
|
-
} finally {
|
|
257
|
-
setDraftPreviewHosts([]);
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
describe("draftPointerFromRequest", () => {
|
|
263
|
-
it("reads the pointer from the __deco_draft cookie (in-preview navigation)", () => {
|
|
264
|
-
const req = new Request("https://preview.example/deco/invoke/site/loaders/x.ts", {
|
|
265
|
-
headers: { cookie: "a=1; __deco_draft=abc.preview-studio.decocms.com@v1; b=2" },
|
|
266
|
-
});
|
|
267
|
-
expect(draftPointerFromRequest(req)).toBe("abc.preview-studio.decocms.com@v1");
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
it("lets ?__draft= win over the cookie", () => {
|
|
271
|
-
const req = new Request("https://preview.example/p?__draft=h@v2", {
|
|
272
|
-
headers: { cookie: "__deco_draft=h@v1" },
|
|
273
|
-
});
|
|
274
|
-
expect(draftPointerFromRequest(req)).toBe("h@v2");
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
it("returns null on ?__draft=off even with a cookie set", () => {
|
|
278
|
-
const req = new Request("https://preview.example/p?__draft=off", {
|
|
279
|
-
headers: { cookie: "__deco_draft=h@v1" },
|
|
280
|
-
});
|
|
281
|
-
expect(draftPointerFromRequest(req)).toBeNull();
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
it("returns null with neither param nor cookie", () => {
|
|
285
|
-
expect(draftPointerFromRequest(new Request("https://preview.example/p"))).toBeNull();
|
|
286
|
-
});
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
describe("resolveDraftForRequest", () => {
|
|
290
|
-
const ENV = { DECO_ALLOWED_PREVIEW_HOSTS: "preview.example" };
|
|
291
|
-
function invokeReq(host = "preview.example"): Request {
|
|
292
|
-
return new Request("https://preview.example/deco/invoke/site/loaders/x.ts", {
|
|
293
|
-
method: "POST",
|
|
294
|
-
headers: {
|
|
295
|
-
"x-forwarded-host": host,
|
|
296
|
-
cookie: "__deco_draft=abc.preview-studio.decocms.com@v1",
|
|
297
|
-
},
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
it("binds the draft when host is allowed and the pointer resolves", async () => {
|
|
302
|
-
const fetchImpl = (async () =>
|
|
303
|
-
jsonResponse({ "site/x": { value: "draft" } })) as unknown as typeof fetch;
|
|
304
|
-
expect(await resolveDraftForRequest(invokeReq(), { env: ENV, fetchImpl })).toEqual({
|
|
305
|
-
"site/x": { value: "draft" },
|
|
306
|
-
});
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
it("is inert with no allowlist — never touches the network", async () => {
|
|
310
|
-
let called = false;
|
|
311
|
-
const fetchImpl = (async () => {
|
|
312
|
-
called = true;
|
|
313
|
-
return jsonResponse({});
|
|
314
|
-
}) as unknown as typeof fetch;
|
|
315
|
-
expect(await resolveDraftForRequest(invokeReq(), { env: {}, fetchImpl })).toBeNull();
|
|
316
|
-
expect(called).toBe(false);
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
it("returns null when the request host is not on the allowlist (prod stays published)", async () => {
|
|
320
|
-
let called = false;
|
|
321
|
-
const fetchImpl = (async () => {
|
|
322
|
-
called = true;
|
|
323
|
-
return jsonResponse({});
|
|
324
|
-
}) as unknown as typeof fetch;
|
|
325
|
-
expect(
|
|
326
|
-
await resolveDraftForRequest(invokeReq("prod.example"), { env: ENV, fetchImpl }),
|
|
327
|
-
).toBeNull();
|
|
328
|
-
expect(called).toBe(false);
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
it("returns null when the request carries no draft pointer", async () => {
|
|
332
|
-
const req = new Request("https://preview.example/deco/invoke/site/loaders/x.ts", {
|
|
333
|
-
headers: { "x-forwarded-host": "preview.example" },
|
|
334
|
-
});
|
|
335
|
-
let called = false;
|
|
336
|
-
const fetchImpl = (async () => {
|
|
337
|
-
called = true;
|
|
338
|
-
return jsonResponse({});
|
|
339
|
-
}) as unknown as typeof fetch;
|
|
340
|
-
expect(await resolveDraftForRequest(req, { env: ENV, fetchImpl })).toBeNull();
|
|
341
|
-
expect(called).toBe(false);
|
|
342
|
-
});
|
|
343
|
-
});
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
registerLayoutSections,
|
|
4
|
-
registerSectionLoader,
|
|
5
|
-
registerSections,
|
|
6
|
-
resolveDecoPage,
|
|
7
|
-
runSectionLoaders,
|
|
8
|
-
setBlocks,
|
|
9
|
-
unregisterLayoutSections,
|
|
10
|
-
} from "./index";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Regression test for the layout-cache index-corruption bug that shipped in
|
|
14
|
-
* @decocms/start@6.12.1 and was fixed in 6.12.2 (blocks commit 73d4f19,
|
|
15
|
-
* "fix(cms): don't mutate shared cached section objects when stamping
|
|
16
|
-
* index"). Two live production sites (casaevideo-tanstack, bagaggio-tanstack)
|
|
17
|
-
* hit this in the wild as an intermittent "footer renders above other
|
|
18
|
-
* sections" bug under concurrent traffic.
|
|
19
|
-
*
|
|
20
|
-
* Mechanism: resolveDecoPage caches a layout section's (Header/Footer)
|
|
21
|
-
* resolved output in a module-level Map, shared across every request that
|
|
22
|
-
* references the same layout block — and dedupes concurrent in-flight
|
|
23
|
-
* resolutions to the same shared Promise. Each request then stamps its own
|
|
24
|
-
* page's flat position onto `.index` so mergeSections can sort eager +
|
|
25
|
-
* deferred sections back into CMS order. If two concurrent requests need the
|
|
26
|
-
* SAME cached layout section at DIFFERENT flat positions (e.g. page A has
|
|
27
|
-
* Footer at index 0, page B has Footer at index 2), and stamping mutates the
|
|
28
|
-
* shared cached object in place, whichever request's stamp lands last wins —
|
|
29
|
-
* and both requests then see that same (possibly wrong-for-them) index.
|
|
30
|
-
*
|
|
31
|
-
* The fix clones the wrapper object before stamping, so each caller gets its
|
|
32
|
-
* own `{ ...section, index }` and the shared cache/in-flight objects are
|
|
33
|
-
* never touched.
|
|
34
|
-
*/
|
|
35
|
-
describe("resolveDecoPage — layout section cache does not leak `.index` across concurrent requests", () => {
|
|
36
|
-
it("gives each page its own correct Footer index when resolved concurrently", async () => {
|
|
37
|
-
const FOOTER_TYPE = "site/sections/RaceFooter.tsx";
|
|
38
|
-
const FILLER_TYPE = "site/sections/RaceFiller.tsx";
|
|
39
|
-
|
|
40
|
-
// A controllable delay on Footer's resolver forces both concurrent
|
|
41
|
-
// resolveDecoPage() calls to land on the SAME in-flight promise
|
|
42
|
-
// (resolvedLayoutInflight), reproducing the exact shared-object window
|
|
43
|
-
// the original bug required — without this, the two calls might resolve
|
|
44
|
-
// sequentially and never actually share state.
|
|
45
|
-
let releaseFooter: () => void = () => {};
|
|
46
|
-
const footerGate = new Promise<void>((resolve) => {
|
|
47
|
-
releaseFooter = resolve;
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
registerSections({
|
|
51
|
-
[FOOTER_TYPE]: async () => {
|
|
52
|
-
await footerGate;
|
|
53
|
-
return { default: () => null };
|
|
54
|
-
},
|
|
55
|
-
[FILLER_TYPE]: async () => ({ default: () => null }),
|
|
56
|
-
});
|
|
57
|
-
registerLayoutSections([FOOTER_TYPE]);
|
|
58
|
-
|
|
59
|
-
setBlocks({
|
|
60
|
-
"pages-race-a": {
|
|
61
|
-
path: "/race-a",
|
|
62
|
-
// Footer at flat index 0 for this page.
|
|
63
|
-
sections: [{ __resolveType: FOOTER_TYPE }, { __resolveType: FILLER_TYPE }],
|
|
64
|
-
},
|
|
65
|
-
"pages-race-b": {
|
|
66
|
-
path: "/race-b",
|
|
67
|
-
// Same Footer, but at flat index 2 for this page.
|
|
68
|
-
sections: [
|
|
69
|
-
{ __resolveType: FILLER_TYPE },
|
|
70
|
-
{ __resolveType: FILLER_TYPE },
|
|
71
|
-
{ __resolveType: FOOTER_TYPE },
|
|
72
|
-
],
|
|
73
|
-
},
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
const pending = Promise.all([resolveDecoPage("/race-a"), resolveDecoPage("/race-b")]);
|
|
78
|
-
|
|
79
|
-
// Let both resolveDecoPage() calls start and reach the shared
|
|
80
|
-
// in-flight Footer promise before releasing it — this is what forces
|
|
81
|
-
// them onto the SAME cached/in-flight object rather than resolving
|
|
82
|
-
// one after the other.
|
|
83
|
-
await new Promise((r) => setTimeout(r, 10));
|
|
84
|
-
releaseFooter();
|
|
85
|
-
|
|
86
|
-
const [pageA, pageB] = await pending;
|
|
87
|
-
|
|
88
|
-
const footerA = pageA?.resolvedSections.find((s) => s.component === FOOTER_TYPE);
|
|
89
|
-
const footerB = pageB?.resolvedSections.find((s) => s.component === FOOTER_TYPE);
|
|
90
|
-
|
|
91
|
-
expect(footerA).toBeDefined();
|
|
92
|
-
expect(footerB).toBeDefined();
|
|
93
|
-
|
|
94
|
-
// The bug: mutating a shared cached object in place means both pages
|
|
95
|
-
// would observe whichever index was stamped last (either both 0 or
|
|
96
|
-
// both 2), not their own correct position.
|
|
97
|
-
expect(footerA?.index).toBe(0);
|
|
98
|
-
expect(footerB?.index).toBe(2);
|
|
99
|
-
|
|
100
|
-
// The fix clones the wrapper before stamping — each page's Footer
|
|
101
|
-
// section must be its own object, not the same shared reference.
|
|
102
|
-
expect(footerA).not.toBe(footerB);
|
|
103
|
-
} finally {
|
|
104
|
-
unregisterLayoutSections([FOOTER_TYPE]);
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it("does not leak index across sequential requests when the result is already in the TTL cache", async () => {
|
|
109
|
-
// This covers the scenario missing from the concurrent test above:
|
|
110
|
-
// Page A is fully served first (result lands in layoutCache), then Page B
|
|
111
|
-
// arrives and hits the cache — it must get its own index, not Page A's.
|
|
112
|
-
const FOOTER_KEY = "site/sections/SeqCacheFooter.tsx";
|
|
113
|
-
|
|
114
|
-
registerLayoutSections([FOOTER_KEY]);
|
|
115
|
-
registerSectionLoader(FOOTER_KEY, async (props) => props);
|
|
116
|
-
|
|
117
|
-
const req = new Request("https://example.com/");
|
|
118
|
-
const makeSection = (index: number) => ({
|
|
119
|
-
component: FOOTER_KEY,
|
|
120
|
-
props: {},
|
|
121
|
-
key: `${FOOTER_KEY}-${index}`,
|
|
122
|
-
index,
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
try {
|
|
126
|
-
// First request: populate the TTL cache with index 2.
|
|
127
|
-
const [sectionA] = await runSectionLoaders([makeSection(2)], req);
|
|
128
|
-
|
|
129
|
-
// Second request: hits the TTL cache — must get index 7, not 2.
|
|
130
|
-
const [sectionB] = await runSectionLoaders([makeSection(7)], req);
|
|
131
|
-
|
|
132
|
-
expect(sectionA?.index).toBe(2);
|
|
133
|
-
expect(sectionB?.index).toBe(7);
|
|
134
|
-
// Must be separate objects — the cached entry must never carry an index.
|
|
135
|
-
expect(sectionA).not.toBe(sectionB);
|
|
136
|
-
} finally {
|
|
137
|
-
unregisterLayoutSections([FOOTER_KEY]);
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it("mechanism check: mutating a shared object in place under concurrent stamping loses one side's index (isolated, framework-independent)", async () => {
|
|
142
|
-
// This isolates the exact bug mechanism from the diff that shipped in
|
|
143
|
-
// 6.12.1 and was reverted after the rollback, independent of the CMS
|
|
144
|
-
// registry/cache machinery — proof that the failure mode is a plain JS
|
|
145
|
-
// shared-mutable-object race, not something specific to this framework's
|
|
146
|
-
// plumbing.
|
|
147
|
-
type Wrapper = { component: string; index?: number };
|
|
148
|
-
|
|
149
|
-
const sharedCached: Wrapper[] = [{ component: "Footer" }];
|
|
150
|
-
|
|
151
|
-
// Old (buggy) behavior: mutate the shared array's objects in place.
|
|
152
|
-
function stampBuggy(indexForThisCaller: number): Promise<Wrapper[]> {
|
|
153
|
-
return Promise.resolve(sharedCached).then((sections) => {
|
|
154
|
-
for (const s of sections) s.index = indexForThisCaller;
|
|
155
|
-
return sections;
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// New (fixed) behavior: clone before stamping.
|
|
160
|
-
function stampFixed(indexForThisCaller: number): Promise<Wrapper[]> {
|
|
161
|
-
return Promise.resolve(sharedCached).then((sections) =>
|
|
162
|
-
sections.map((s) => ({ ...s, index: indexForThisCaller })),
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// Two "requests" concurrently want different indices for the same
|
|
167
|
-
// shared cached object. With the buggy stamper, whichever `.then()`
|
|
168
|
-
// callback runs last (caller B, since microtasks run in scheduling
|
|
169
|
-
// order) overwrites the object caller A already received a *reference*
|
|
170
|
-
// to — so caller A's array ends up reporting caller B's index too.
|
|
171
|
-
const [buggyA, buggyB] = await Promise.all([stampBuggy(0), stampBuggy(2)]);
|
|
172
|
-
expect(buggyA[0].index).toBe(2); // <- corrupted: A wanted 0, sees 2
|
|
173
|
-
expect(buggyB[0].index).toBe(2);
|
|
174
|
-
expect(buggyA[0]).toBe(buggyB[0]); // same object reference — the bug
|
|
175
|
-
|
|
176
|
-
const [fixedA, fixedB] = await Promise.all([stampFixed(0), stampFixed(2)]);
|
|
177
|
-
expect(fixedA[0].index).toBe(0); // <- correct
|
|
178
|
-
expect(fixedB[0].index).toBe(2); // <- correct
|
|
179
|
-
expect(fixedA[0]).not.toBe(fixedB[0]); // independent objects — the fix
|
|
180
|
-
});
|
|
181
|
-
});
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { tmpdir } from "node:os";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
-
import { loadDecofileDirectory } from "./loadDecofileDirectory";
|
|
6
|
-
|
|
7
|
-
describe("loadDecofileDirectory", () => {
|
|
8
|
-
let dir: string;
|
|
9
|
-
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
dir = mkdtempSync(join(tmpdir(), "decofile-test-"));
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
afterEach(() => {
|
|
15
|
-
rmSync(dir, { recursive: true, force: true });
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("loads every .json file in the directory into a keyed map", async () => {
|
|
19
|
-
writeFileSync(
|
|
20
|
-
join(dir, "pages-home-123.json"),
|
|
21
|
-
JSON.stringify({ __resolveType: "website/pages/Page.tsx", path: "/" }),
|
|
22
|
-
);
|
|
23
|
-
writeFileSync(
|
|
24
|
-
join(dir, "Banner Grid - 01.json"),
|
|
25
|
-
JSON.stringify({ __resolveType: "site/sections/BannerGrid.tsx" }),
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
const blocks = await loadDecofileDirectory(dir);
|
|
29
|
-
|
|
30
|
-
expect(Object.keys(blocks)).toHaveLength(2);
|
|
31
|
-
expect(blocks["pages-home-123"]).toEqual({
|
|
32
|
-
__resolveType: "website/pages/Page.tsx",
|
|
33
|
-
path: "/",
|
|
34
|
-
});
|
|
35
|
-
expect(blocks["Banner Grid - 01"]).toEqual({
|
|
36
|
-
__resolveType: "site/sections/BannerGrid.tsx",
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it("ignores non-.json files", async () => {
|
|
41
|
-
writeFileSync(join(dir, "readme.md"), "not a block");
|
|
42
|
-
writeFileSync(join(dir, "a.json"), JSON.stringify({ x: 1 }));
|
|
43
|
-
|
|
44
|
-
const blocks = await loadDecofileDirectory(dir);
|
|
45
|
-
expect(Object.keys(blocks)).toEqual(["a"]);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it("throws with the filename on invalid JSON", async () => {
|
|
49
|
-
writeFileSync(join(dir, "broken.json"), "{not valid json");
|
|
50
|
-
await expect(loadDecofileDirectory(dir)).rejects.toThrow(/broken\.json/);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("URL-decodes the filename stem once to produce the block key", async () => {
|
|
54
|
-
// Classic deco stores blocks as encodeURIComponent(<block id>).json and
|
|
55
|
-
// derives the id back with one decodeURIComponent (parseBlockId). Content
|
|
56
|
-
// references saved blocks by the DECODED id ("Cores dos preços"), so the
|
|
57
|
-
// map must be keyed by it or every such reference dangles.
|
|
58
|
-
writeFileSync(
|
|
59
|
-
join(dir, "Cores%20dos%20pre%C3%A7os.json"),
|
|
60
|
-
JSON.stringify({ __resolveType: "site/loaders/priceColors.ts" }),
|
|
61
|
-
);
|
|
62
|
-
// Double-encoded page snapshot (deco.cx corpus shape): decodes ONCE.
|
|
63
|
-
writeFileSync(
|
|
64
|
-
join(dir, "pages-Acess%25C3%25B3rios-421596.json"),
|
|
65
|
-
JSON.stringify({ __resolveType: "website/pages/Page.tsx" }),
|
|
66
|
-
);
|
|
67
|
-
// Encoded slash becomes a real slash in the id — ids are plain map keys.
|
|
68
|
-
writeFileSync(join(dir, "collections%2Fblog%2Fauthors.json"), JSON.stringify({ authors: [] }));
|
|
69
|
-
|
|
70
|
-
const blocks = await loadDecofileDirectory(dir);
|
|
71
|
-
|
|
72
|
-
expect(blocks["Cores dos preços"]).toEqual({
|
|
73
|
-
__resolveType: "site/loaders/priceColors.ts",
|
|
74
|
-
});
|
|
75
|
-
expect(blocks["pages-Acess%C3%B3rios-421596"]).toEqual({
|
|
76
|
-
__resolveType: "website/pages/Page.tsx",
|
|
77
|
-
});
|
|
78
|
-
expect(blocks["collections/blog/authors"]).toEqual({ authors: [] });
|
|
79
|
-
// The raw stems must NOT appear as keys.
|
|
80
|
-
expect(blocks["Cores%20dos%20pre%C3%A7os"]).toBeUndefined();
|
|
81
|
-
expect(blocks["pages-Acess%25C3%25B3rios-421596"]).toBeUndefined();
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("keeps a stem verbatim when it is not valid percent-encoding", async () => {
|
|
85
|
-
// A lone % (e.g. a page literally named "50% off") throws in
|
|
86
|
-
// decodeURIComponent — the stem is already the id, keep it.
|
|
87
|
-
writeFileSync(join(dir, "pages-50% off-7.json"), JSON.stringify({ x: 1 }));
|
|
88
|
-
|
|
89
|
-
const blocks = await loadDecofileDirectory(dir);
|
|
90
|
-
expect(blocks["pages-50% off-7"]).toEqual({ x: 1 });
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
it("resolves decoded-key collisions deterministically (last sorted filename wins) and warns", async () => {
|
|
94
|
-
writeFileSync(join(dir, "A B.json"), JSON.stringify({ raw: true }));
|
|
95
|
-
writeFileSync(join(dir, "A%20B.json"), JSON.stringify({ encoded: true }));
|
|
96
|
-
|
|
97
|
-
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
98
|
-
try {
|
|
99
|
-
const blocks = await loadDecofileDirectory(dir);
|
|
100
|
-
// Code-unit sort: " " (0x20) < "%" (0x25), so "A B.json" comes first
|
|
101
|
-
// and "A%20B.json" is processed last — the encoded file wins.
|
|
102
|
-
expect(blocks["A B"]).toEqual({ encoded: true });
|
|
103
|
-
expect(Object.keys(blocks)).toHaveLength(1);
|
|
104
|
-
expect(warn).toHaveBeenCalledWith(expect.stringContaining("A%20B.json"));
|
|
105
|
-
} finally {
|
|
106
|
-
warn.mockRestore();
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
});
|