@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.
Files changed (220) hide show
  1. package/bin/deco.js +13 -0
  2. package/package.json +32 -82
  3. package/src/index.ts +35 -8
  4. package/src/protocol/assets.ts +124 -0
  5. package/src/protocol/canonical.ts +7 -0
  6. package/src/protocol/ciphertext.ts +6 -0
  7. package/src/protocol/client.ts +170 -0
  8. package/src/protocol/conformance/cases/assets.ts +81 -0
  9. package/src/protocol/conformance/cases/guards.ts +314 -0
  10. package/src/protocol/conformance/cases/reads.ts +168 -0
  11. package/src/protocol/conformance/cases/types.ts +9 -0
  12. package/src/protocol/conformance/cases/wire.ts +138 -0
  13. package/src/protocol/conformance/cases/writes.ts +345 -0
  14. package/src/protocol/conformance/contentHashFixtures.ts +79 -0
  15. package/src/protocol/conformance/context.ts +222 -0
  16. package/src/protocol/conformance/index.ts +140 -0
  17. package/src/protocol/errors.ts +142 -0
  18. package/src/protocol/index.ts +39 -0
  19. package/src/protocol/keys.ts +279 -0
  20. package/src/protocol/params.ts +67 -0
  21. package/src/protocol/secrets.ts +231 -0
  22. package/src/protocol/server/assets.ts +119 -0
  23. package/src/protocol/server/auth.ts +49 -0
  24. package/src/protocol/server/bodyCache.ts +84 -0
  25. package/src/protocol/server/content.ts +175 -0
  26. package/src/protocol/server/core.ts +112 -0
  27. package/src/protocol/server/handler.ts +102 -0
  28. package/src/protocol/server/http.ts +139 -0
  29. package/src/protocol/server/index.ts +11 -0
  30. package/src/protocol/server/methods/apply.ts +341 -0
  31. package/src/protocol/server/methods/read.ts +156 -0
  32. package/src/protocol/server/rpc.ts +136 -0
  33. package/src/protocol/storage/fs/hash.ts +21 -0
  34. package/src/protocol/storage/fs/index.ts +338 -0
  35. package/src/protocol/storage/fs/lock.ts +68 -0
  36. package/src/protocol/storage/fs/transaction.ts +183 -0
  37. package/src/protocol/storage/fs/unsupported.ts +15 -0
  38. package/src/protocol/storage/memory.ts +238 -0
  39. package/src/protocol/storage.ts +182 -0
  40. package/src/protocol/types.ts +172 -0
  41. package/src/v8/analytics.ts +50 -0
  42. package/src/v8/background.ts +107 -0
  43. package/src/v8/builtins/data.ts +23 -0
  44. package/src/v8/builtins/index.ts +26 -0
  45. package/src/v8/builtins/lazy.ts +23 -0
  46. package/src/v8/builtins/legacy.ts +65 -0
  47. package/src/v8/builtins/matchers.ts +31 -0
  48. package/src/v8/builtins/multivariate.ts +25 -0
  49. package/src/v8/builtins/secret.ts +82 -0
  50. package/src/v8/canonical.ts +117 -0
  51. package/src/v8/ciphertext.ts +158 -0
  52. package/src/v8/cli/builtins.ts +20 -0
  53. package/src/v8/cli/check/index.ts +774 -0
  54. package/src/v8/cli/check/messages.ts +168 -0
  55. package/src/v8/cli/content.ts +287 -0
  56. package/src/v8/cli/index.ts +17 -0
  57. package/src/v8/cli/log.ts +18 -0
  58. package/src/v8/cli/main.ts +12 -0
  59. package/src/v8/cli/root.ts +66 -0
  60. package/src/v8/cli/run.ts +236 -0
  61. package/src/v8/cli/schema/builtinSchemas.ts +236 -0
  62. package/src/v8/cli/schema/generate.ts +507 -0
  63. package/src/v8/cli/schema/index.ts +63 -0
  64. package/src/v8/cli/schema/tsProgram.ts +497 -0
  65. package/src/v8/cli/schema/typeToSchema.ts +689 -0
  66. package/src/v8/cli/serve/server.ts +303 -0
  67. package/src/v8/cli/watch.ts +86 -0
  68. package/src/v8/client.ts +147 -0
  69. package/src/v8/cms.ts +237 -0
  70. package/src/v8/content.ts +195 -0
  71. package/src/v8/draft.ts +136 -0
  72. package/src/v8/errors.ts +59 -0
  73. package/src/v8/fetch.ts +133 -0
  74. package/src/v8/identity.ts +65 -0
  75. package/src/v8/index.ts +39 -0
  76. package/src/v8/json.ts +74 -0
  77. package/src/v8/matchRoute.ts +204 -0
  78. package/src/v8/remoteLoader.ts +282 -0
  79. package/src/v8/resolver.ts +278 -0
  80. package/src/v8/secrets.ts +17 -0
  81. package/src/v8/telemetry.ts +533 -0
  82. package/src/v8/types.ts +240 -0
  83. package/src/cms/applySectionConventions.ts +0 -124
  84. package/src/cms/blockSource.test.ts +0 -76
  85. package/src/cms/blockSource.ts +0 -191
  86. package/src/cms/client.browserBundle.test.ts +0 -44
  87. package/src/cms/client.ts +0 -80
  88. package/src/cms/draftSource.test.ts +0 -343
  89. package/src/cms/draftSource.ts +0 -362
  90. package/src/cms/index.ts +0 -151
  91. package/src/cms/layoutCacheRace.test.ts +0 -181
  92. package/src/cms/loadDecofileDirectory.test.ts +0 -109
  93. package/src/cms/loadDecofileDirectory.ts +0 -89
  94. package/src/cms/loader.test.ts +0 -200
  95. package/src/cms/loader.ts +0 -304
  96. package/src/cms/registry.test.ts +0 -110
  97. package/src/cms/registry.ts +0 -242
  98. package/src/cms/resolve.test.ts +0 -980
  99. package/src/cms/resolve.ts +0 -2282
  100. package/src/cms/schema.test.ts +0 -356
  101. package/src/cms/schema.ts +0 -1215
  102. package/src/cms/sectionLoaderContext.test.ts +0 -111
  103. package/src/cms/sectionLoaderContext.ts +0 -113
  104. package/src/cms/sectionLoaders.test.ts +0 -533
  105. package/src/cms/sectionLoaders.ts +0 -636
  106. package/src/cms/sectionMixins.test.ts +0 -167
  107. package/src/cms/sectionMixins.ts +0 -153
  108. package/src/cms/stickyFlags.test.ts +0 -117
  109. package/src/flags/audience.ts +0 -56
  110. package/src/flags/everyone.ts +0 -29
  111. package/src/flags/flag.ts +0 -15
  112. package/src/flags/flags.test.ts +0 -148
  113. package/src/flags/multivariate/image.ts +0 -11
  114. package/src/flags/multivariate/message.ts +0 -11
  115. package/src/flags/multivariate/page.ts +0 -16
  116. package/src/flags/multivariate/section.ts +0 -16
  117. package/src/flags/multivariate.ts +0 -1
  118. package/src/flags/types.ts +0 -59
  119. package/src/flags/utils/multivariate.ts +0 -20
  120. package/src/hooks/Image.test.ts +0 -100
  121. package/src/hooks/Image.tsx +0 -250
  122. package/src/hooks/JsonLd.tsx +0 -353
  123. package/src/hooks/LazySection.tsx +0 -121
  124. package/src/hooks/LiveControls.tsx +0 -122
  125. package/src/hooks/Picture.tsx +0 -127
  126. package/src/hooks/RenderSection.test.tsx +0 -81
  127. package/src/hooks/RenderSection.tsx +0 -91
  128. package/src/hooks/SectionErrorFallback.tsx +0 -97
  129. package/src/hooks/index.ts +0 -30
  130. package/src/matchers/builtins.test.ts +0 -318
  131. package/src/matchers/builtins.ts +0 -459
  132. package/src/matchers/countryNames.ts +0 -104
  133. package/src/matchers/override.test.ts +0 -205
  134. package/src/matchers/override.ts +0 -136
  135. package/src/matchers/posthog.ts +0 -154
  136. package/src/middleware/decoState.ts +0 -55
  137. package/src/middleware/healthMetrics.ts +0 -133
  138. package/src/middleware/hydrationContext.test.ts +0 -61
  139. package/src/middleware/hydrationContext.ts +0 -79
  140. package/src/middleware/index.ts +0 -88
  141. package/src/middleware/liveness.ts +0 -21
  142. package/src/middleware/observability.test.ts +0 -242
  143. package/src/middleware/observability.ts +0 -643
  144. package/src/middleware/validateSection.test.ts +0 -147
  145. package/src/middleware/validateSection.ts +0 -100
  146. package/src/sdk/abTesting.test.ts +0 -326
  147. package/src/sdk/abTesting.ts +0 -499
  148. package/src/sdk/analytics.ts +0 -77
  149. package/src/sdk/cacheHeaders.test.ts +0 -115
  150. package/src/sdk/cacheHeaders.ts +0 -433
  151. package/src/sdk/cachedLoader.test.ts +0 -122
  152. package/src/sdk/cachedLoader.ts +0 -503
  153. package/src/sdk/cachedLoaderModule.test.ts +0 -209
  154. package/src/sdk/clx.ts +0 -5
  155. package/src/sdk/cn.test.ts +0 -34
  156. package/src/sdk/cn.ts +0 -28
  157. package/src/sdk/composite.test.ts +0 -121
  158. package/src/sdk/composite.ts +0 -114
  159. package/src/sdk/cookie.test.ts +0 -108
  160. package/src/sdk/cookie.ts +0 -129
  161. package/src/sdk/crypto.ts +0 -185
  162. package/src/sdk/csp.ts +0 -59
  163. package/src/sdk/detectDevice.ts +0 -45
  164. package/src/sdk/djb2.ts +0 -20
  165. package/src/sdk/encoding.test.ts +0 -71
  166. package/src/sdk/encoding.ts +0 -47
  167. package/src/sdk/env.test.ts +0 -33
  168. package/src/sdk/env.ts +0 -54
  169. package/src/sdk/flags.test.ts +0 -91
  170. package/src/sdk/flags.ts +0 -108
  171. package/src/sdk/http.test.ts +0 -71
  172. package/src/sdk/http.ts +0 -124
  173. package/src/sdk/index.ts +0 -90
  174. package/src/sdk/inflightTimeout.test.ts +0 -35
  175. package/src/sdk/inflightTimeout.ts +0 -53
  176. package/src/sdk/instrumentedFetch.test.ts +0 -559
  177. package/src/sdk/instrumentedFetch.ts +0 -339
  178. package/src/sdk/invoke.test.ts +0 -115
  179. package/src/sdk/invoke.ts +0 -298
  180. package/src/sdk/logger.test.ts +0 -432
  181. package/src/sdk/logger.ts +0 -304
  182. package/src/sdk/mergeCacheControl.ts +0 -150
  183. package/src/sdk/normalizeUrls.ts +0 -91
  184. package/src/sdk/observability.ts +0 -109
  185. package/src/sdk/otel.test.ts +0 -526
  186. package/src/sdk/otel.ts +0 -1012
  187. package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
  188. package/src/sdk/otelAdapters.test.ts +0 -89
  189. package/src/sdk/otelAdapters.ts +0 -144
  190. package/src/sdk/otelHttpLog.test.ts +0 -457
  191. package/src/sdk/otelHttpLog.ts +0 -419
  192. package/src/sdk/otelHttpMeter.test.ts +0 -292
  193. package/src/sdk/otelHttpMeter.ts +0 -506
  194. package/src/sdk/otelHttpTracer.test.ts +0 -474
  195. package/src/sdk/otelHttpTracer.ts +0 -543
  196. package/src/sdk/redirects.ts +0 -228
  197. package/src/sdk/requestContext.ts +0 -281
  198. package/src/sdk/requestContextStorage.browser.test.ts +0 -29
  199. package/src/sdk/requestContextStorage.browser.ts +0 -43
  200. package/src/sdk/requestContextStorage.ts +0 -35
  201. package/src/sdk/retry.ts +0 -45
  202. package/src/sdk/serverTimings.ts +0 -68
  203. package/src/sdk/signal.ts +0 -42
  204. package/src/sdk/sitemap.ts +0 -160
  205. package/src/sdk/urlRedaction.test.ts +0 -73
  206. package/src/sdk/urlRedaction.ts +0 -82
  207. package/src/sdk/urlUtils.ts +0 -134
  208. package/src/sdk/useDevice.test.ts +0 -130
  209. package/src/sdk/useDevice.ts +0 -89
  210. package/src/sdk/useDeviceContext.tsx +0 -108
  211. package/src/sdk/useId.ts +0 -7
  212. package/src/sdk/useScript.test.ts +0 -128
  213. package/src/sdk/useScript.ts +0 -210
  214. package/src/sdk/useSuggestions.test.ts +0 -230
  215. package/src/sdk/useSuggestions.ts +0 -188
  216. package/src/sdk/wrapCaughtErrors.ts +0 -107
  217. package/src/setup.ts +0 -120
  218. package/src/types/index.ts +0 -53
  219. package/src/types/widgets.ts +0 -14
  220. package/tsconfig.json +0 -7
package/src/cms/client.ts DELETED
@@ -1,80 +0,0 @@
1
- /**
2
- * Client-safe subset of `@decocms/blocks/cms`.
3
- *
4
- * `@decocms/blocks/cms` (the full barrel in `./index.ts`) re-exports
5
- * `loader.ts` and `resolve.ts`, which transitively import `node:async_hooks`
6
- * and `node:fs/promises`. Bundling ANY export from that barrel for a browser
7
- * target — even one that itself has zero Node dependencies, like
8
- * `getResolvedComponent` — drags the whole module graph in, since ES module
9
- * imports are evaluated at the file level, not per-export. Turbopack rejects
10
- * this outright in production builds ("the chunking context does not
11
- * support external modules"); webpack has historically let it through
12
- * uncaught, which just defers the failure to whoever notices the bloated
13
- * client bundle or a runtime error.
14
- *
15
- * This entry point exists so Client Components can import section-registry
16
- * lookups (e.g. to resolve and render a section by name, given data already
17
- * fetched server-side) without pulling in the resolver/loader machinery.
18
- *
19
- * Verified client-safe by import-graph inspection, transitively:
20
- * - `registry.ts` imports only React types.
21
- * - `sectionMixins.ts` imports `useDevice.ts`, which goes through
22
- * `requestContext.ts` — that module's only `node:async_hooks` dependency
23
- * (`requestContextStorage.ts`) is itself swapped for a no-op browser stub
24
- * via this package's `"browser"` export condition (see
25
- * `sdk/requestContextStorage.browser.ts`), so it's already safe for a
26
- * browser bundle.
27
- * - `schema.ts` has no imports at all.
28
- *
29
- * Deliberately NOT re-exported here: `loader.ts`, `resolve.ts`,
30
- * `sectionLoaders.ts`, `loadDecofileDirectory.ts`, `blockSource.ts`, and
31
- * `applySectionConventions.ts` (which itself pulls in `resolve.ts` and
32
- * `sectionLoaders.ts` for section-registration setup). Those are resolver/
33
- * storage concerns that only make sense server-side — import them from
34
- * `@decocms/blocks/cms` instead.
35
- */
36
- export type { OnBeforeResolveProps, SectionModule, SectionOptions } from "./registry";
37
- export {
38
- getResolvedComponent,
39
- getSection,
40
- getSectionOptions,
41
- getSectionRegistry,
42
- getSyncComponent,
43
- listRegisteredSections,
44
- preloadSectionComponents,
45
- preloadSectionModule,
46
- registerOnBeforeResolveProps,
47
- registerSection,
48
- registerSections,
49
- registerSectionsSync,
50
- setResolvedComponent,
51
- } from "./registry";
52
- export type {
53
- ActionConfig,
54
- AppSchemas,
55
- BlockPropsSchema,
56
- LoaderConfig,
57
- MatcherConfig,
58
- MetaResponse,
59
- } from "./schema";
60
- export {
61
- composeMeta,
62
- getRegisteredLoaders,
63
- getRegisteredMatchers,
64
- inferLoaderTags,
65
- registerActionSchema,
66
- registerActionSchemas,
67
- registerAppSchemas,
68
- registerLoaderSchema,
69
- registerLoaderSchemas,
70
- registerMatcherSchema,
71
- registerMatcherSchemas,
72
- } from "./schema";
73
- export type { SectionLoaderFn } from "./sectionLoaders";
74
- export {
75
- compose,
76
- withDevice,
77
- withMobile,
78
- withSearchParam,
79
- withSectionLoader,
80
- } from "./sectionMixins";
@@ -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
- });