@decocms/tanstack 8.0.0 → 8.4.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.
Files changed (150) hide show
  1. package/README.md +79 -0
  2. package/dist/draft.d.ts +33 -0
  3. package/dist/draft.d.ts.map +1 -0
  4. package/dist/draft.js +41 -0
  5. package/dist/draft.js.map +1 -0
  6. package/dist/index.d.ts +9 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +9 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/react/DecoDocument.d.ts +61 -0
  11. package/dist/react/DecoDocument.d.ts.map +1 -0
  12. package/dist/react/DecoDocument.js +96 -0
  13. package/dist/react/DecoDocument.js.map +1 -0
  14. package/dist/react/Image.d.ts +96 -0
  15. package/dist/react/Image.d.ts.map +1 -0
  16. package/dist/react/Image.js +79 -0
  17. package/dist/react/Image.js.map +1 -0
  18. package/dist/react/index.d.ts +7 -0
  19. package/dist/react/index.d.ts.map +1 -0
  20. package/dist/react/index.js +7 -0
  21. package/dist/react/index.js.map +1 -0
  22. package/dist/react/pageValues.d.ts +10 -0
  23. package/dist/react/pageValues.d.ts.map +1 -0
  24. package/dist/react/pageValues.js +29 -0
  25. package/dist/react/pageValues.js.map +1 -0
  26. package/dist/react/scripts.d.ts +14 -0
  27. package/dist/react/scripts.d.ts.map +1 -0
  28. package/dist/react/scripts.js +96 -0
  29. package/dist/react/scripts.js.map +1 -0
  30. package/dist/router/index.d.ts +38 -0
  31. package/dist/router/index.d.ts.map +1 -0
  32. package/dist/router/index.js +60 -0
  33. package/dist/router/index.js.map +1 -0
  34. package/dist/scope/browser.d.ts +11 -0
  35. package/dist/scope/browser.d.ts.map +1 -0
  36. package/dist/scope/browser.js +8 -0
  37. package/dist/scope/browser.js.map +1 -0
  38. package/dist/scope/server.d.ts +13 -0
  39. package/dist/scope/server.d.ts.map +1 -0
  40. package/dist/scope/server.js +26 -0
  41. package/dist/scope/server.js.map +1 -0
  42. package/dist/server/index.d.ts +27 -0
  43. package/dist/server/index.d.ts.map +1 -0
  44. package/dist/server/index.js +31 -0
  45. package/dist/server/index.js.map +1 -0
  46. package/dist/shared.d.ts +26 -0
  47. package/dist/shared.d.ts.map +1 -0
  48. package/dist/shared.js +26 -0
  49. package/dist/shared.js.map +1 -0
  50. package/dist/start/index.d.ts +9 -0
  51. package/dist/start/index.d.ts.map +1 -0
  52. package/dist/start/index.js +33 -0
  53. package/dist/start/index.js.map +1 -0
  54. package/dist/studio.d.ts +28 -0
  55. package/dist/studio.d.ts.map +1 -0
  56. package/dist/studio.js +53 -0
  57. package/dist/studio.js.map +1 -0
  58. package/dist/vite/index.d.ts +15 -0
  59. package/dist/vite/index.d.ts.map +1 -0
  60. package/dist/vite/index.js +95 -0
  61. package/dist/vite/index.js.map +1 -0
  62. package/dist/worker/cacheProfiles.d.ts +43 -0
  63. package/dist/worker/cacheProfiles.d.ts.map +1 -0
  64. package/dist/worker/cacheProfiles.js +162 -0
  65. package/dist/worker/cacheProfiles.js.map +1 -0
  66. package/dist/worker/cookies.d.ts +20 -0
  67. package/dist/worker/cookies.d.ts.map +1 -0
  68. package/dist/worker/cookies.js +69 -0
  69. package/dist/worker/cookies.js.map +1 -0
  70. package/dist/worker/drafts.d.ts +59 -0
  71. package/dist/worker/drafts.d.ts.map +1 -0
  72. package/dist/worker/drafts.js +140 -0
  73. package/dist/worker/drafts.js.map +1 -0
  74. package/dist/worker/index.d.ts +107 -0
  75. package/dist/worker/index.d.ts.map +1 -0
  76. package/dist/worker/index.js +511 -0
  77. package/dist/worker/index.js.map +1 -0
  78. package/dist/worker/security.d.ts +14 -0
  79. package/dist/worker/security.d.ts.map +1 -0
  80. package/dist/worker/security.js +48 -0
  81. package/dist/worker/security.js.map +1 -0
  82. package/dist/worker/segment.d.ts +21 -0
  83. package/dist/worker/segment.d.ts.map +1 -0
  84. package/dist/worker/segment.js +43 -0
  85. package/dist/worker/segment.js.map +1 -0
  86. package/package.json +86 -31
  87. package/src/draft.ts +43 -0
  88. package/src/index.ts +8 -44
  89. package/src/react/DecoDocument.tsx +234 -0
  90. package/src/react/Image.tsx +213 -0
  91. package/src/react/index.ts +24 -0
  92. package/src/react/pageValues.ts +35 -0
  93. package/src/react/scripts.ts +98 -0
  94. package/src/router/index.ts +80 -0
  95. package/src/scope/browser.ts +18 -0
  96. package/src/scope/server.ts +37 -0
  97. package/src/server/index.ts +43 -0
  98. package/src/shared.ts +29 -0
  99. package/src/start/index.ts +31 -0
  100. package/src/studio.ts +52 -0
  101. package/src/vite/index.ts +105 -0
  102. package/src/worker/cacheProfiles.ts +180 -0
  103. package/src/worker/cookies.ts +65 -0
  104. package/src/worker/drafts.ts +157 -0
  105. package/src/worker/index.ts +648 -0
  106. package/src/worker/security.ts +63 -0
  107. package/src/worker/segment.ts +49 -0
  108. package/src/cms/kvBlockSource.test.ts +0 -82
  109. package/src/cms/kvBlockSource.ts +0 -68
  110. package/src/daemon/auth.ts +0 -204
  111. package/src/daemon/fs.ts +0 -238
  112. package/src/daemon/index.ts +0 -8
  113. package/src/daemon/middleware.ts +0 -156
  114. package/src/daemon/tunnel.ts +0 -129
  115. package/src/daemon/volumes.ts +0 -366
  116. package/src/daemon/watch.ts +0 -272
  117. package/src/hooks/DecoPageRenderer.tsx +0 -688
  118. package/src/hooks/DecoRootLayout.tsx +0 -111
  119. package/src/hooks/NavigationProgress.tsx +0 -21
  120. package/src/hooks/PreviewProviders.tsx +0 -26
  121. package/src/hooks/StableOutlet.tsx +0 -30
  122. package/src/hooks/index.ts +0 -9
  123. package/src/routes/adminRoutes.test.ts +0 -99
  124. package/src/routes/adminRoutes.ts +0 -153
  125. package/src/routes/cmsRoute.test.ts +0 -100
  126. package/src/routes/cmsRoute.ts +0 -944
  127. package/src/routes/components.tsx +0 -57
  128. package/src/routes/index.ts +0 -24
  129. package/src/routes/pageUrl.test.ts +0 -70
  130. package/src/routes/pageUrl.ts +0 -54
  131. package/src/routes/withSiteGlobals.test.ts +0 -206
  132. package/src/routes/withSiteGlobals.ts +0 -222
  133. package/src/sdk/cookiePassthrough.ts +0 -58
  134. package/src/sdk/createInvoke.ts +0 -77
  135. package/src/sdk/deferredSectionLoader.test.ts +0 -94
  136. package/src/sdk/deferredSectionLoader.ts +0 -46
  137. package/src/sdk/kvHydration.test.ts +0 -215
  138. package/src/sdk/kvHydration.ts +0 -193
  139. package/src/sdk/outboundHeaders.test.ts +0 -113
  140. package/src/sdk/outboundHeaders.ts +0 -81
  141. package/src/sdk/router.ts +0 -92
  142. package/src/sdk/useHydrated.ts +0 -19
  143. package/src/sdk/workerEntry.test.ts +0 -495
  144. package/src/sdk/workerEntry.ts +0 -2307
  145. package/src/setupFastDeploy.ts +0 -13
  146. package/src/vite/plugin-scaffold-gen.test.js +0 -106
  147. package/src/vite/plugin.js +0 -850
  148. package/src/vite/plugin.test.js +0 -113
  149. package/src/vite/plugin.test.ts +0 -52
  150. package/tsconfig.json +0 -7
@@ -1,495 +0,0 @@
1
- import { setBlocks } from "@decocms/blocks/cms";
2
- import { afterEach, describe, expect, it } from "vitest";
3
- import { __resetKvHydrationStateForTests } from "./kvHydration";
4
- import {
5
- buildGeoCacheParam,
6
- createDecoWorkerEntry,
7
- DECO_ADMIN_FRAME_ANCESTORS,
8
- DEFAULT_FRAME_ANCESTORS_CSP,
9
- DEFAULT_SECURITY_HEADERS,
10
- detectLocationMatcher,
11
- injectGeoCookies,
12
- } from "./workerEntry";
13
-
14
- const EMPTY_ENV = {};
15
- const MOCK_CTX = {
16
- waitUntil: (_p: Promise<unknown>) => {},
17
- passThroughOnException: () => {},
18
- };
19
- const MOCK_SERVER_ENTRY = {
20
- fetch: async (_req: Request) => new Response("page content", { status: 200 }),
21
- };
22
-
23
- function parseCookies(header: string): Record<string, string> {
24
- return Object.fromEntries(
25
- header.split("; ").map((c) => {
26
- const [k, ...v] = c.split("=");
27
- return [k, v.join("=")];
28
- }),
29
- );
30
- }
31
-
32
- function makeRequest(
33
- cf: Record<string, string> | undefined,
34
- headers: Record<string, string> = {},
35
- ): Request {
36
- const req = new Request("https://example.com/", { headers });
37
- if (cf) {
38
- Object.defineProperty(req, "cf", { value: cf, configurable: true });
39
- }
40
- return req;
41
- }
42
-
43
- describe("injectGeoCookies", () => {
44
- it("strips cf-region from the outgoing Request headers while preserving the value in __cf_geo_region cookie", () => {
45
- const req = makeRequest(
46
- { region: "São Paulo", country: "BR" },
47
- { "cf-region": "São Paulo", "cf-ipcountry": "BR" },
48
- );
49
-
50
- const out = injectGeoCookies(req);
51
-
52
- expect(out.headers.get("cf-region")).toBeNull();
53
- // ASCII CF headers (cf-ipcountry) are still forwarded
54
- expect(out.headers.get("cf-ipcountry")).toBe("BR");
55
- // Geo data is preserved as cookies for matchers
56
- const cookies = parseCookies(out.headers.get("cookie") ?? "");
57
- expect(cookies.__cf_geo_region).toBe(encodeURIComponent("São Paulo"));
58
- expect(cookies.__cf_geo_country).toBe("BR");
59
- });
60
-
61
- it("strips cf-ipcity from the outgoing Request headers while preserving the value in __cf_geo_city cookie", () => {
62
- const req = makeRequest({ city: "Brasília", country: "BR" }, { "cf-ipcity": "Brasília" });
63
-
64
- const out = injectGeoCookies(req);
65
-
66
- expect(out.headers.get("cf-ipcity")).toBeNull();
67
- const cookies = parseCookies(out.headers.get("cookie") ?? "");
68
- expect(cookies.__cf_geo_city).toBe(encodeURIComponent("Brasília"));
69
- });
70
-
71
- it("returns the original request unchanged when there is no cf object", () => {
72
- const req = makeRequest(undefined, { "cf-region": "São Paulo" });
73
-
74
- const out = injectGeoCookies(req);
75
-
76
- // Without cf, we don't build cookies, and we return the original request
77
- // untouched (so the cf-region header is still present — but that's the
78
- // caller's pre-existing state, not something we re-introduced).
79
- expect(out).toBe(req);
80
- });
81
-
82
- it("returns the original request unchanged when cf has no relevant geo fields", () => {
83
- const req = makeRequest({ asn: "12345" }, { "cf-region": "São Paulo" });
84
-
85
- const out = injectGeoCookies(req);
86
-
87
- expect(out).toBe(req);
88
- });
89
-
90
- it("preserves a pre-existing cookie header", () => {
91
- const req = makeRequest({ region: "São Paulo" }, { cookie: "vtex_segment=abc; another=xyz" });
92
-
93
- const out = injectGeoCookies(req);
94
-
95
- const raw = out.headers.get("cookie") ?? "";
96
- expect(raw).toContain("vtex_segment=abc");
97
- expect(raw).toContain("another=xyz");
98
- expect(raw).toContain("__cf_geo_region=");
99
- });
100
-
101
- it("forwards non-geo headers untouched", () => {
102
- const req = makeRequest(
103
- { region: "Paraná" },
104
- {
105
- "user-agent": "test-agent",
106
- accept: "*/*",
107
- "x-custom": "value",
108
- "cf-ray": "9ff5b26cf9bc067a",
109
- },
110
- );
111
-
112
- const out = injectGeoCookies(req);
113
-
114
- expect(out.headers.get("user-agent")).toBe("test-agent");
115
- expect(out.headers.get("accept")).toBe("*/*");
116
- expect(out.headers.get("x-custom")).toBe("value");
117
- expect(out.headers.get("cf-ray")).toBe("9ff5b26cf9bc067a");
118
- });
119
- });
120
-
121
- describe("buildGeoCacheParam", () => {
122
- const cf = { country: "BR", region: "São Paulo", city: "SP" };
123
-
124
- it("returns undefined when granularity is off", () => {
125
- expect(buildGeoCacheParam(cf, "off")).toBeUndefined();
126
- });
127
-
128
- it("returns undefined when cf is undefined", () => {
129
- expect(buildGeoCacheParam(undefined, "city")).toBeUndefined();
130
- });
131
-
132
- it("returns country only when granularity is country", () => {
133
- expect(buildGeoCacheParam(cf, "country")).toBe("BR");
134
- });
135
-
136
- it("returns country|region when granularity is region", () => {
137
- expect(buildGeoCacheParam(cf, "region")).toBe("BR|São Paulo");
138
- });
139
-
140
- it("returns country|region|city when granularity is city", () => {
141
- expect(buildGeoCacheParam(cf, "city")).toBe("BR|São Paulo|SP");
142
- });
143
-
144
- it("omits missing fields gracefully", () => {
145
- expect(buildGeoCacheParam({ country: "BR" }, "city")).toBe("BR");
146
- expect(buildGeoCacheParam({ country: "BR", region: "MG" }, "city")).toBe("BR|MG");
147
- });
148
-
149
- it("returns undefined when cf has none of country/region/city", () => {
150
- expect(buildGeoCacheParam({ asn: "12345" }, "city")).toBeUndefined();
151
- });
152
- });
153
-
154
- describe("detectLocationMatcher", () => {
155
- it("returns true when decofile has a website/matchers/location.ts __resolveType", () => {
156
- const blocks = {
157
- "audiences/geo-audience.json": {
158
- __resolveType: "website/flags/audience.ts",
159
- matcher: {
160
- __resolveType: "website/matchers/location.ts",
161
- includeLocations: [{ country: "BR" }],
162
- },
163
- },
164
- };
165
- expect(detectLocationMatcher(blocks)).toBe(true);
166
- });
167
-
168
- it("returns false when decofile has no location matcher", () => {
169
- const blocks = {
170
- "audiences/device-audience.json": {
171
- __resolveType: "website/flags/audience.ts",
172
- matcher: { __resolveType: "website/matchers/device.ts" },
173
- },
174
- };
175
- expect(detectLocationMatcher(blocks)).toBe(false);
176
- });
177
-
178
- it("returns false for an empty decofile", () => {
179
- expect(detectLocationMatcher({})).toBe(false);
180
- });
181
-
182
- it("returns true when the matcher is nested deeply", () => {
183
- const blocks = {
184
- "pages/home.json": {
185
- variant: {
186
- matcher: { __resolveType: "website/matchers/location.ts" },
187
- },
188
- },
189
- };
190
- expect(detectLocationMatcher(blocks)).toBe(true);
191
- });
192
-
193
- it("returns false when location.ts appears only in a non-resolveType string value (no false positive)", () => {
194
- const blocks = {
195
- "content/help.json": {
196
- __resolveType: "website/sections/RichText.tsx",
197
- body: "This page is controlled by website/matchers/location.ts for geo targeting.",
198
- },
199
- };
200
- expect(detectLocationMatcher(blocks)).toBe(false);
201
- });
202
- });
203
-
204
- describe("CMS redirects", () => {
205
- afterEach(() => {
206
- setBlocks({});
207
- __resetKvHydrationStateForTests();
208
- });
209
-
210
- it("returns a 301 redirect for a permanent redirect block", async () => {
211
- setBlocks({
212
- "redirect-1": {
213
- __resolveType: "website/loaders/redirect.ts",
214
- redirects: [{ from: "/old", to: "/new", type: "permanent" }],
215
- },
216
- });
217
- const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false });
218
- const res = await worker.fetch(new Request("https://example.com/old"), EMPTY_ENV, MOCK_CTX);
219
- expect(res.status).toBe(301);
220
- expect(res.headers.get("Location")).toBe("/new");
221
- });
222
-
223
- it("redirects ?asJson requests too (does not fall through to page resolver)", async () => {
224
- setBlocks({
225
- "redirect-1": {
226
- __resolveType: "website/loaders/redirect.ts",
227
- redirects: [{ from: "/old", to: "/new", type: "permanent" }],
228
- },
229
- });
230
- const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false });
231
- const res = await worker.fetch(
232
- new Request("https://example.com/old?asJson"),
233
- EMPTY_ENV,
234
- MOCK_CTX,
235
- );
236
- expect(res.status).toBe(301);
237
- expect(res.headers.get("Location")).toBe("/new");
238
- });
239
-
240
- it("URI-encodes the Location header for non-ASCII destinations", async () => {
241
- setBlocks({
242
- "redirect-1": {
243
- __resolveType: "website/loaders/redirect.ts",
244
- redirects: [{ from: "/promo", to: "/promoção", type: "temporary" }],
245
- },
246
- });
247
- const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false });
248
- const res = await worker.fetch(new Request("https://example.com/promo"), EMPTY_ENV, MOCK_CTX);
249
- expect(res.status).toBe(302);
250
- expect(res.headers.get("Location")).toBe("/promo%C3%A7%C3%A3o");
251
- });
252
-
253
- it("returns a 302 redirect for a temporary redirect block", async () => {
254
- setBlocks({
255
- "redirect-1": {
256
- __resolveType: "website/loaders/redirect.ts",
257
- redirects: [{ from: "/promo", to: "/sale", type: "temporary" }],
258
- },
259
- });
260
- const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false });
261
- const res = await worker.fetch(new Request("https://example.com/promo"), EMPTY_ENV, MOCK_CTX);
262
- expect(res.status).toBe(302);
263
- expect(res.headers.get("Location")).toBe("/sale");
264
- });
265
-
266
- it("falls through to serverEntry for paths with no redirect", async () => {
267
- setBlocks({
268
- "redirect-1": {
269
- __resolveType: "website/loaders/redirect.ts",
270
- redirects: [{ from: "/old", to: "/new", type: "permanent" }],
271
- },
272
- });
273
- const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false });
274
- const res = await worker.fetch(new Request("https://example.com/other"), EMPTY_ENV, MOCK_CTX);
275
- expect(res.status).toBe(200);
276
- });
277
-
278
- it("picks up new redirects after setBlocks hot-reload", async () => {
279
- setBlocks({});
280
- const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false });
281
-
282
- // First request with no redirects — falls through
283
- const res1 = await worker.fetch(new Request("https://example.com/v1"), EMPTY_ENV, MOCK_CTX);
284
- expect(res1.status).toBe(200);
285
-
286
- // Hot-reload: add a redirect
287
- setBlocks({
288
- "redirect-1": {
289
- __resolveType: "website/loaders/redirect.ts",
290
- redirects: [{ from: "/v1", to: "/v2", type: "permanent" }],
291
- },
292
- });
293
-
294
- // Same path should now redirect
295
- const res2 = await worker.fetch(new Request("https://example.com/v1"), EMPTY_ENV, MOCK_CTX);
296
- expect(res2.status).toBe(301);
297
- expect(res2.headers.get("Location")).toBe("/v2");
298
- });
299
- });
300
-
301
- describe("degraded origin (anti-cache-poisoning)", () => {
302
- afterEach(() => {
303
- setBlocks({});
304
- __resetKvHydrationStateForTests();
305
- });
306
-
307
- it("never caches a degraded 200 and marks it no-store", async () => {
308
- setBlocks({});
309
- const degradedEntry = {
310
- fetch: async () =>
311
- new Response("empty shelf", {
312
- status: 200,
313
- headers: { "X-Deco-Degraded": "true" },
314
- }),
315
- };
316
- const worker = createDecoWorkerEntry(degradedEntry, { observability: false });
317
- const res = await worker.fetch(
318
- new Request("https://example.com/some-category"),
319
- EMPTY_ENV,
320
- MOCK_CTX,
321
- );
322
- expect(res.status).toBe(200);
323
- expect(res.headers.get("X-Cache")).toBe("BYPASS");
324
- expect(res.headers.get("X-Cache-Reason")).toBe("degraded");
325
- expect(res.headers.get("Cache-Control")).toContain("no-store");
326
- });
327
-
328
- it("serves a healthy 200 origin without the degraded bypass", async () => {
329
- setBlocks({});
330
- const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false });
331
- const res = await worker.fetch(
332
- new Request("https://example.com/some-category"),
333
- EMPTY_ENV,
334
- MOCK_CTX,
335
- );
336
- expect(res.status).toBe(200);
337
- expect(res.headers.get("X-Cache-Reason")).not.toBe("degraded");
338
- });
339
- });
340
-
341
- describe("security headers — frame-ancestors default", () => {
342
- afterEach(() => {
343
- __resetKvHydrationStateForTests();
344
- setBlocks({});
345
- });
346
-
347
- const HTML_SERVER_ENTRY = {
348
- fetch: async (_req: Request) =>
349
- new Response("<html>hi</html>", {
350
- status: 200,
351
- headers: { "content-type": "text/html; charset=utf-8" },
352
- }),
353
- };
354
-
355
- it("does NOT ship X-Frame-Options (frame-ancestors supersedes it)", () => {
356
- expect(DEFAULT_SECURITY_HEADERS["X-Frame-Options"]).toBeUndefined();
357
- });
358
-
359
- it("allows the deco studio to embed the storefront by default", () => {
360
- expect(DEFAULT_FRAME_ANCESTORS_CSP).toContain("frame-ancestors");
361
- expect(DECO_ADMIN_FRAME_ANCESTORS).toContain("https://studio.decocms.com");
362
- expect(DECO_ADMIN_FRAME_ANCESTORS).toContain("https://*.deco.studio");
363
- expect(DECO_ADMIN_FRAME_ANCESTORS).toContain("'self'");
364
- });
365
-
366
- it("applies the default frame-ancestors CSP on HTML responses and drops X-Frame-Options", async () => {
367
- const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY);
368
- const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
369
- expect(res.headers.get("Content-Security-Policy")).toBe(DEFAULT_FRAME_ANCESTORS_CSP);
370
- expect(res.headers.get("X-Frame-Options")).toBeNull();
371
- // Other defaults are still present.
372
- expect(res.headers.get("X-Content-Type-Options")).toBe("nosniff");
373
- });
374
-
375
- it("lets a site override the frame-ancestors CSP via securityHeaders", async () => {
376
- const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
377
- securityHeaders: { "Content-Security-Policy": "frame-ancestors 'none'" },
378
- });
379
- const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
380
- expect(res.headers.get("Content-Security-Policy")).toBe("frame-ancestors 'none'");
381
- });
382
-
383
- it("emits the full site CSP report-only alongside the enforced frame-ancestors", async () => {
384
- const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
385
- csp: ["default-src 'self'", "img-src 'self' https:"],
386
- });
387
- const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
388
- expect(res.headers.get("Content-Security-Policy")).toBe(DEFAULT_FRAME_ANCESTORS_CSP);
389
- expect(res.headers.get("Content-Security-Policy-Report-Only")).toBe(
390
- "default-src 'self'; img-src 'self' https:",
391
- );
392
- });
393
-
394
- it("omits all security headers when securityHeaders is false", async () => {
395
- const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, { securityHeaders: false });
396
- const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
397
- expect(res.headers.get("Content-Security-Policy")).toBeNull();
398
- expect(res.headers.get("X-Content-Type-Options")).toBeNull();
399
- });
400
- });
401
-
402
- describe("POST /_cache/purge-loaders", () => {
403
- afterEach(() => {
404
- __resetKvHydrationStateForTests();
405
- setBlocks({});
406
- });
407
-
408
- const purge = (env: Record<string, unknown>, headers: Record<string, string> = {}) => {
409
- const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false });
410
- return worker.fetch(
411
- new Request("https://example.com/_cache/purge-loaders", { method: "POST", headers }),
412
- env,
413
- MOCK_CTX,
414
- );
415
- };
416
-
417
- it("401s without a valid Bearer token", async () => {
418
- const res = await purge({ PURGE_TOKEN: "secret" });
419
- expect(res.status).toBe(401);
420
- });
421
-
422
- it("401s with the wrong token", async () => {
423
- const res = await purge({ PURGE_TOKEN: "secret" }, { Authorization: "Bearer nope" });
424
- expect(res.status).toBe(401);
425
- });
426
-
427
- it("clears the loader cache with a valid token", async () => {
428
- const res = await purge({ PURGE_TOKEN: "secret" }, { Authorization: "Bearer secret" });
429
- expect(res.status).toBe(200);
430
- expect(await res.json()).toEqual({ cleared: true, scope: "isolate" });
431
- });
432
-
433
- it("404s when purge is disabled (purgeTokenEnv: false)", async () => {
434
- const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
435
- observability: false,
436
- purgeTokenEnv: false,
437
- });
438
- const res = await worker.fetch(
439
- new Request("https://example.com/_cache/purge-loaders", { method: "POST" }),
440
- EMPTY_ENV,
441
- MOCK_CTX,
442
- );
443
- expect(res.status).toBe(404);
444
- });
445
- });
446
-
447
- describe("X-Cache-Segment — custom SegmentKey fields (#284)", () => {
448
- afterEach(() => {
449
- __resetKvHydrationStateForTests();
450
- setBlocks({});
451
- });
452
-
453
- const HTML_SERVER_ENTRY = {
454
- fetch: async (_req: Request) =>
455
- new Response("<html>hi</html>", {
456
- status: 200,
457
- headers: { "content-type": "text/html; charset=utf-8" },
458
- }),
459
- };
460
-
461
- it("includes a custom segment field in X-Cache-Segment instead of dropping it", async () => {
462
- const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
463
- observability: false,
464
- buildSegment: () => ({ device: "desktop", regionId: "RJ", delivery: "store-42" }),
465
- });
466
- const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
467
- expect(res.headers.get("X-Cache-Segment")).toBe("desktop|r=RJ|delivery=store-42");
468
- });
469
-
470
- it("produces different X-Cache-Segment hashes for requests that only differ by a custom field", async () => {
471
- let delivery = "store-1";
472
- const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
473
- observability: false,
474
- buildSegment: () => ({ device: "desktop", delivery }),
475
- });
476
-
477
- const first = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
478
- const firstSegment = first.headers.get("X-Cache-Segment");
479
-
480
- delivery = "store-2";
481
- const second = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
482
- const secondSegment = second.headers.get("X-Cache-Segment");
483
-
484
- expect(firstSegment).not.toBe(secondSegment);
485
- });
486
-
487
- it("keeps the existing hash format for segments using only known fields (back-compat)", async () => {
488
- const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
489
- observability: false,
490
- buildSegment: () => ({ device: "desktop", salesChannel: "1", flags: ["b", "a"] }),
491
- });
492
- const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
493
- expect(res.headers.get("X-Cache-Segment")).toBe("desktop|sc=1|f=a,b");
494
- });
495
- });