@decocms/tanstack 8.1.0-next.0 → 8.5.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/README.md +79 -0
- package/dist/draft.d.ts +33 -0
- package/dist/draft.d.ts.map +1 -0
- package/dist/draft.js +41 -0
- package/dist/draft.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/react/DecoDocument.d.ts +61 -0
- package/dist/react/DecoDocument.d.ts.map +1 -0
- package/dist/react/DecoDocument.js +96 -0
- package/dist/react/DecoDocument.js.map +1 -0
- package/dist/react/Image.d.ts +96 -0
- package/dist/react/Image.d.ts.map +1 -0
- package/dist/react/Image.js +79 -0
- package/dist/react/Image.js.map +1 -0
- package/dist/react/index.d.ts +7 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +7 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/pageValues.d.ts +10 -0
- package/dist/react/pageValues.d.ts.map +1 -0
- package/dist/react/pageValues.js +29 -0
- package/dist/react/pageValues.js.map +1 -0
- package/dist/react/scripts.d.ts +14 -0
- package/dist/react/scripts.d.ts.map +1 -0
- package/dist/react/scripts.js +96 -0
- package/dist/react/scripts.js.map +1 -0
- package/dist/router/index.d.ts +38 -0
- package/dist/router/index.d.ts.map +1 -0
- package/dist/router/index.js +60 -0
- package/dist/router/index.js.map +1 -0
- package/dist/scope/browser.d.ts +11 -0
- package/dist/scope/browser.d.ts.map +1 -0
- package/dist/scope/browser.js +8 -0
- package/dist/scope/browser.js.map +1 -0
- package/dist/scope/server.d.ts +13 -0
- package/dist/scope/server.d.ts.map +1 -0
- package/dist/scope/server.js +26 -0
- package/dist/scope/server.js.map +1 -0
- package/dist/server/index.d.ts +27 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +31 -0
- package/dist/server/index.js.map +1 -0
- package/dist/shared.d.ts +26 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +26 -0
- package/dist/shared.js.map +1 -0
- package/dist/start/index.d.ts +9 -0
- package/dist/start/index.d.ts.map +1 -0
- package/dist/start/index.js +33 -0
- package/dist/start/index.js.map +1 -0
- package/dist/studio.d.ts +28 -0
- package/dist/studio.d.ts.map +1 -0
- package/dist/studio.js +53 -0
- package/dist/studio.js.map +1 -0
- package/dist/vite/index.d.ts +15 -0
- package/dist/vite/index.d.ts.map +1 -0
- package/dist/vite/index.js +95 -0
- package/dist/vite/index.js.map +1 -0
- package/dist/worker/cacheProfiles.d.ts +43 -0
- package/dist/worker/cacheProfiles.d.ts.map +1 -0
- package/dist/worker/cacheProfiles.js +162 -0
- package/dist/worker/cacheProfiles.js.map +1 -0
- package/dist/worker/cookies.d.ts +20 -0
- package/dist/worker/cookies.d.ts.map +1 -0
- package/dist/worker/cookies.js +69 -0
- package/dist/worker/cookies.js.map +1 -0
- package/dist/worker/drafts.d.ts +59 -0
- package/dist/worker/drafts.d.ts.map +1 -0
- package/dist/worker/drafts.js +140 -0
- package/dist/worker/drafts.js.map +1 -0
- package/dist/worker/index.d.ts +111 -0
- package/dist/worker/index.d.ts.map +1 -0
- package/dist/worker/index.js +618 -0
- package/dist/worker/index.js.map +1 -0
- package/dist/worker/security.d.ts +14 -0
- package/dist/worker/security.d.ts.map +1 -0
- package/dist/worker/security.js +48 -0
- package/dist/worker/security.js.map +1 -0
- package/dist/worker/segment.d.ts +21 -0
- package/dist/worker/segment.d.ts.map +1 -0
- package/dist/worker/segment.js +43 -0
- package/dist/worker/segment.js.map +1 -0
- package/package.json +85 -37
- package/src/draft.ts +43 -0
- package/src/index.ts +8 -61
- package/src/react/DecoDocument.tsx +234 -0
- package/src/react/Image.tsx +213 -0
- package/src/react/index.ts +24 -0
- package/src/react/pageValues.ts +35 -0
- package/src/react/scripts.ts +98 -0
- package/src/router/index.ts +80 -0
- package/src/scope/browser.ts +18 -0
- package/src/scope/server.ts +37 -0
- package/src/server/index.ts +43 -0
- package/src/shared.ts +29 -0
- package/src/start/index.ts +31 -0
- package/src/studio.ts +52 -0
- package/src/vite/index.ts +105 -0
- package/src/worker/cacheProfiles.ts +180 -0
- package/src/worker/cookies.ts +65 -0
- package/src/worker/drafts.ts +157 -0
- package/src/worker/index.ts +784 -0
- package/src/worker/security.ts +63 -0
- package/src/worker/segment.ts +49 -0
- package/src/__conformance__/observability.test.ts +0 -221
- package/src/cms/kvBlockSource.test.ts +0 -82
- package/src/cms/kvBlockSource.ts +0 -68
- package/src/daemon/auth.ts +0 -204
- package/src/daemon/fs.ts +0 -238
- package/src/daemon/index.ts +0 -8
- package/src/daemon/middleware.ts +0 -156
- package/src/daemon/tunnel.ts +0 -129
- package/src/daemon/volumes.ts +0 -366
- package/src/daemon/watch.ts +0 -272
- package/src/hooks/CdnSegmentMarker.tsx +0 -38
- package/src/hooks/DecoPageRenderer.tsx +0 -750
- package/src/hooks/DecoRootLayout.tsx +0 -192
- package/src/hooks/DraftPreviewIndicator.test.tsx +0 -60
- package/src/hooks/DraftPreviewIndicator.tsx +0 -64
- package/src/hooks/NavigationProgress.test.tsx +0 -58
- package/src/hooks/NavigationProgress.tsx +0 -63
- package/src/hooks/PreviewProviders.test.tsx +0 -42
- package/src/hooks/PreviewProviders.tsx +0 -45
- package/src/hooks/StableOutlet.tsx +0 -30
- package/src/hooks/deferredObserverTiming.test.tsx +0 -91
- package/src/hooks/index.ts +0 -10
- package/src/routes/adminRoutes.test.ts +0 -99
- package/src/routes/adminRoutes.ts +0 -153
- package/src/routes/cmsRoute.test.ts +0 -267
- package/src/routes/cmsRoute.ts +0 -1053
- package/src/routes/components.tsx +0 -57
- package/src/routes/index.ts +0 -24
- package/src/routes/pageUrl.test.ts +0 -70
- package/src/routes/pageUrl.ts +0 -55
- package/src/routes/withSiteGlobals.test.ts +0 -243
- package/src/routes/withSiteGlobals.ts +0 -243
- package/src/sdk/backgroundTasks.test.ts +0 -68
- package/src/sdk/backgroundTasks.ts +0 -38
- package/src/sdk/cdnSegment.test.ts +0 -67
- package/src/sdk/cdnSegment.ts +0 -71
- package/src/sdk/cookiePassthrough.ts +0 -58
- package/src/sdk/createInvoke.ts +0 -77
- package/src/sdk/deferredSectionLoader.test.ts +0 -94
- package/src/sdk/deferredSectionLoader.ts +0 -46
- package/src/sdk/draft.test.ts +0 -341
- package/src/sdk/draft.ts +0 -277
- package/src/sdk/draftShared.ts +0 -16
- package/src/sdk/kvHydration.test.ts +0 -371
- package/src/sdk/kvHydration.ts +0 -263
- package/src/sdk/kvRedirects.test.ts +0 -132
- package/src/sdk/kvRedirects.ts +0 -125
- package/src/sdk/outboundHeaders.test.ts +0 -113
- package/src/sdk/outboundHeaders.ts +0 -81
- package/src/sdk/router.ts +0 -101
- package/src/sdk/serverFnFetch.ts +0 -59
- package/src/sdk/speculationRules.test.ts +0 -82
- package/src/sdk/speculationRules.ts +0 -136
- package/src/sdk/startEntry.ts +0 -31
- package/src/sdk/useHydrated.ts +0 -19
- package/src/sdk/workerEntry.cacheStorage.test.ts +0 -147
- package/src/sdk/workerEntry.test.ts +0 -1033
- package/src/sdk/workerEntry.trackingCache.test.ts +0 -328
- package/src/sdk/workerEntry.ts +0 -3154
- package/src/setupFastDeploy.ts +0 -13
- package/src/v8/__conformance__/delivery.test.ts +0 -100
- package/src/v8/binding.ts +0 -70
- package/src/v8/kvLoader.bundle.test.ts +0 -42
- package/src/v8/kvLoader.test.ts +0 -74
- package/src/v8/kvLoader.ts +0 -43
- package/src/vite/plugin-scaffold-gen.test.js +0 -106
- package/src/vite/plugin.js +0 -1022
- package/src/vite/plugin.test.js +0 -238
- package/src/vite/plugin.test.ts +0 -176
- package/tsconfig.json +0 -7
|
@@ -1,1033 +0,0 @@
|
|
|
1
|
-
import { computeRevision, revisionKey, setBlocks, snapshotKey } from "@decocms/blocks/cms";
|
|
2
|
-
import {
|
|
3
|
-
__resetAutoconfigStateForTests,
|
|
4
|
-
autoconfigApps,
|
|
5
|
-
} from "@decocms/blocks-admin/apps/autoconfig";
|
|
6
|
-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
7
|
-
import { segmentToken } from "./cdnSegment";
|
|
8
|
-
import { __resetKvHydrationStateForTests } from "./kvHydration";
|
|
9
|
-
import {
|
|
10
|
-
buildEnforcedCsp,
|
|
11
|
-
buildGeoCacheParam,
|
|
12
|
-
createDecoWorkerEntry,
|
|
13
|
-
DECO_ADMIN_FRAME_ANCESTORS,
|
|
14
|
-
DEFAULT_FRAME_ANCESTORS_CSP,
|
|
15
|
-
DEFAULT_SECURITY_HEADERS,
|
|
16
|
-
detectLocationMatcher,
|
|
17
|
-
injectGeoCookies,
|
|
18
|
-
} from "./workerEntry";
|
|
19
|
-
|
|
20
|
-
const EMPTY_ENV = {};
|
|
21
|
-
const MOCK_CTX = {
|
|
22
|
-
waitUntil: (_p: Promise<unknown>) => {},
|
|
23
|
-
passThroughOnException: () => {},
|
|
24
|
-
};
|
|
25
|
-
const MOCK_SERVER_ENTRY = {
|
|
26
|
-
fetch: async (_req: Request) => new Response("page content", { status: 200 }),
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
function parseCookies(header: string): Record<string, string> {
|
|
30
|
-
return Object.fromEntries(
|
|
31
|
-
header.split("; ").map((c) => {
|
|
32
|
-
const [k, ...v] = c.split("=");
|
|
33
|
-
return [k, v.join("=")];
|
|
34
|
-
}),
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function makeRequest(
|
|
39
|
-
cf: Record<string, string> | undefined,
|
|
40
|
-
headers: Record<string, string> = {},
|
|
41
|
-
): Request {
|
|
42
|
-
const req = new Request("https://example.com/", { headers });
|
|
43
|
-
if (cf) {
|
|
44
|
-
Object.defineProperty(req, "cf", { value: cf, configurable: true });
|
|
45
|
-
}
|
|
46
|
-
return req;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
describe("injectGeoCookies", () => {
|
|
50
|
-
it("strips cf-region from the outgoing Request headers while preserving the value in __cf_geo_region cookie", () => {
|
|
51
|
-
const req = makeRequest(
|
|
52
|
-
{ region: "São Paulo", country: "BR" },
|
|
53
|
-
{ "cf-region": "São Paulo", "cf-ipcountry": "BR" },
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
const out = injectGeoCookies(req);
|
|
57
|
-
|
|
58
|
-
expect(out.headers.get("cf-region")).toBeNull();
|
|
59
|
-
// ASCII CF headers (cf-ipcountry) are still forwarded
|
|
60
|
-
expect(out.headers.get("cf-ipcountry")).toBe("BR");
|
|
61
|
-
// Geo data is preserved as cookies for matchers
|
|
62
|
-
const cookies = parseCookies(out.headers.get("cookie") ?? "");
|
|
63
|
-
expect(cookies.__cf_geo_region).toBe(encodeURIComponent("São Paulo"));
|
|
64
|
-
expect(cookies.__cf_geo_country).toBe("BR");
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("strips cf-ipcity from the outgoing Request headers while preserving the value in __cf_geo_city cookie", () => {
|
|
68
|
-
const req = makeRequest({ city: "Brasília", country: "BR" }, { "cf-ipcity": "Brasília" });
|
|
69
|
-
|
|
70
|
-
const out = injectGeoCookies(req);
|
|
71
|
-
|
|
72
|
-
expect(out.headers.get("cf-ipcity")).toBeNull();
|
|
73
|
-
const cookies = parseCookies(out.headers.get("cookie") ?? "");
|
|
74
|
-
expect(cookies.__cf_geo_city).toBe(encodeURIComponent("Brasília"));
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it("returns the original request unchanged when there is no cf object", () => {
|
|
78
|
-
const req = makeRequest(undefined, { "cf-region": "São Paulo" });
|
|
79
|
-
|
|
80
|
-
const out = injectGeoCookies(req);
|
|
81
|
-
|
|
82
|
-
// Without cf, we don't build cookies, and we return the original request
|
|
83
|
-
// untouched (so the cf-region header is still present — but that's the
|
|
84
|
-
// caller's pre-existing state, not something we re-introduced).
|
|
85
|
-
expect(out).toBe(req);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("returns the original request unchanged when cf has no relevant geo fields", () => {
|
|
89
|
-
const req = makeRequest({ asn: "12345" }, { "cf-region": "São Paulo" });
|
|
90
|
-
|
|
91
|
-
const out = injectGeoCookies(req);
|
|
92
|
-
|
|
93
|
-
expect(out).toBe(req);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it("preserves a pre-existing cookie header", () => {
|
|
97
|
-
const req = makeRequest({ region: "São Paulo" }, { cookie: "vtex_segment=abc; another=xyz" });
|
|
98
|
-
|
|
99
|
-
const out = injectGeoCookies(req);
|
|
100
|
-
|
|
101
|
-
const raw = out.headers.get("cookie") ?? "";
|
|
102
|
-
expect(raw).toContain("vtex_segment=abc");
|
|
103
|
-
expect(raw).toContain("another=xyz");
|
|
104
|
-
expect(raw).toContain("__cf_geo_region=");
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it("forwards non-geo headers untouched", () => {
|
|
108
|
-
const req = makeRequest(
|
|
109
|
-
{ region: "Paraná" },
|
|
110
|
-
{
|
|
111
|
-
"user-agent": "test-agent",
|
|
112
|
-
accept: "*/*",
|
|
113
|
-
"x-custom": "value",
|
|
114
|
-
"cf-ray": "9ff5b26cf9bc067a",
|
|
115
|
-
},
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
const out = injectGeoCookies(req);
|
|
119
|
-
|
|
120
|
-
expect(out.headers.get("user-agent")).toBe("test-agent");
|
|
121
|
-
expect(out.headers.get("accept")).toBe("*/*");
|
|
122
|
-
expect(out.headers.get("x-custom")).toBe("value");
|
|
123
|
-
expect(out.headers.get("cf-ray")).toBe("9ff5b26cf9bc067a");
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
describe("buildGeoCacheParam", () => {
|
|
128
|
-
const cf = { country: "BR", region: "São Paulo", city: "SP" };
|
|
129
|
-
|
|
130
|
-
it("returns undefined when granularity is off", () => {
|
|
131
|
-
expect(buildGeoCacheParam(cf, "off")).toBeUndefined();
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it("returns undefined when cf is undefined", () => {
|
|
135
|
-
expect(buildGeoCacheParam(undefined, "city")).toBeUndefined();
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it("returns country only when granularity is country", () => {
|
|
139
|
-
expect(buildGeoCacheParam(cf, "country")).toBe("BR");
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it("returns country|region when granularity is region", () => {
|
|
143
|
-
expect(buildGeoCacheParam(cf, "region")).toBe("BR|São Paulo");
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it("returns country|region|city when granularity is city", () => {
|
|
147
|
-
expect(buildGeoCacheParam(cf, "city")).toBe("BR|São Paulo|SP");
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it("omits missing fields gracefully", () => {
|
|
151
|
-
expect(buildGeoCacheParam({ country: "BR" }, "city")).toBe("BR");
|
|
152
|
-
expect(buildGeoCacheParam({ country: "BR", region: "MG" }, "city")).toBe("BR|MG");
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it("returns undefined when cf has none of country/region/city", () => {
|
|
156
|
-
expect(buildGeoCacheParam({ asn: "12345" }, "city")).toBeUndefined();
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
describe("detectLocationMatcher", () => {
|
|
161
|
-
it("returns true when decofile has a website/matchers/location.ts __resolveType", () => {
|
|
162
|
-
const blocks = {
|
|
163
|
-
"audiences/geo-audience.json": {
|
|
164
|
-
__resolveType: "website/flags/audience.ts",
|
|
165
|
-
matcher: {
|
|
166
|
-
__resolveType: "website/matchers/location.ts",
|
|
167
|
-
includeLocations: [{ country: "BR" }],
|
|
168
|
-
},
|
|
169
|
-
},
|
|
170
|
-
};
|
|
171
|
-
expect(detectLocationMatcher(blocks)).toBe(true);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it("returns false when decofile has no location matcher", () => {
|
|
175
|
-
const blocks = {
|
|
176
|
-
"audiences/device-audience.json": {
|
|
177
|
-
__resolveType: "website/flags/audience.ts",
|
|
178
|
-
matcher: { __resolveType: "website/matchers/device.ts" },
|
|
179
|
-
},
|
|
180
|
-
};
|
|
181
|
-
expect(detectLocationMatcher(blocks)).toBe(false);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it("returns false for an empty decofile", () => {
|
|
185
|
-
expect(detectLocationMatcher({})).toBe(false);
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
it("returns true when the matcher is nested deeply", () => {
|
|
189
|
-
const blocks = {
|
|
190
|
-
"pages/home.json": {
|
|
191
|
-
variant: {
|
|
192
|
-
matcher: { __resolveType: "website/matchers/location.ts" },
|
|
193
|
-
},
|
|
194
|
-
},
|
|
195
|
-
};
|
|
196
|
-
expect(detectLocationMatcher(blocks)).toBe(true);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it("returns false when location.ts appears only in a non-resolveType string value (no false positive)", () => {
|
|
200
|
-
const blocks = {
|
|
201
|
-
"content/help.json": {
|
|
202
|
-
__resolveType: "website/sections/RichText.tsx",
|
|
203
|
-
body: "This page is controlled by website/matchers/location.ts for geo targeting.",
|
|
204
|
-
},
|
|
205
|
-
};
|
|
206
|
-
expect(detectLocationMatcher(blocks)).toBe(false);
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
describe("CMS redirects", () => {
|
|
211
|
-
afterEach(() => {
|
|
212
|
-
setBlocks({});
|
|
213
|
-
__resetKvHydrationStateForTests();
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it("returns a 301 redirect for a permanent redirect block", async () => {
|
|
217
|
-
setBlocks({
|
|
218
|
-
"redirect-1": {
|
|
219
|
-
__resolveType: "website/loaders/redirect.ts",
|
|
220
|
-
redirects: [{ from: "/old", to: "/new", type: "permanent" }],
|
|
221
|
-
},
|
|
222
|
-
});
|
|
223
|
-
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
224
|
-
observability: false,
|
|
225
|
-
});
|
|
226
|
-
const res = await worker.fetch(new Request("https://example.com/old"), EMPTY_ENV, MOCK_CTX);
|
|
227
|
-
expect(res.status).toBe(301);
|
|
228
|
-
expect(res.headers.get("Location")).toBe("/new");
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
it("redirects ?asJson requests too (does not fall through to page resolver)", async () => {
|
|
232
|
-
setBlocks({
|
|
233
|
-
"redirect-1": {
|
|
234
|
-
__resolveType: "website/loaders/redirect.ts",
|
|
235
|
-
redirects: [{ from: "/old", to: "/new", type: "permanent" }],
|
|
236
|
-
},
|
|
237
|
-
});
|
|
238
|
-
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
239
|
-
observability: false,
|
|
240
|
-
});
|
|
241
|
-
const res = await worker.fetch(
|
|
242
|
-
new Request("https://example.com/old?asJson"),
|
|
243
|
-
EMPTY_ENV,
|
|
244
|
-
MOCK_CTX,
|
|
245
|
-
);
|
|
246
|
-
expect(res.status).toBe(301);
|
|
247
|
-
expect(res.headers.get("Location")).toBe("/new");
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
it("URI-encodes the Location header for non-ASCII destinations", async () => {
|
|
251
|
-
setBlocks({
|
|
252
|
-
"redirect-1": {
|
|
253
|
-
__resolveType: "website/loaders/redirect.ts",
|
|
254
|
-
redirects: [{ from: "/promo", to: "/promoção", type: "temporary" }],
|
|
255
|
-
},
|
|
256
|
-
});
|
|
257
|
-
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
258
|
-
observability: false,
|
|
259
|
-
});
|
|
260
|
-
const res = await worker.fetch(new Request("https://example.com/promo"), EMPTY_ENV, MOCK_CTX);
|
|
261
|
-
expect(res.status).toBe(302);
|
|
262
|
-
expect(res.headers.get("Location")).toBe("/promo%C3%A7%C3%A3o");
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
it("returns a 302 redirect for a temporary redirect block", async () => {
|
|
266
|
-
setBlocks({
|
|
267
|
-
"redirect-1": {
|
|
268
|
-
__resolveType: "website/loaders/redirect.ts",
|
|
269
|
-
redirects: [{ from: "/promo", to: "/sale", type: "temporary" }],
|
|
270
|
-
},
|
|
271
|
-
});
|
|
272
|
-
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
273
|
-
observability: false,
|
|
274
|
-
});
|
|
275
|
-
const res = await worker.fetch(new Request("https://example.com/promo"), EMPTY_ENV, MOCK_CTX);
|
|
276
|
-
expect(res.status).toBe(302);
|
|
277
|
-
expect(res.headers.get("Location")).toBe("/sale");
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
it("falls through to serverEntry for paths with no redirect", async () => {
|
|
281
|
-
setBlocks({
|
|
282
|
-
"redirect-1": {
|
|
283
|
-
__resolveType: "website/loaders/redirect.ts",
|
|
284
|
-
redirects: [{ from: "/old", to: "/new", type: "permanent" }],
|
|
285
|
-
},
|
|
286
|
-
});
|
|
287
|
-
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
288
|
-
observability: false,
|
|
289
|
-
});
|
|
290
|
-
const res = await worker.fetch(new Request("https://example.com/other"), EMPTY_ENV, MOCK_CTX);
|
|
291
|
-
expect(res.status).toBe(200);
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
it("picks up new redirects after setBlocks hot-reload", async () => {
|
|
295
|
-
setBlocks({});
|
|
296
|
-
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
297
|
-
observability: false,
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
// First request with no redirects — falls through
|
|
301
|
-
const res1 = await worker.fetch(new Request("https://example.com/v1"), EMPTY_ENV, MOCK_CTX);
|
|
302
|
-
expect(res1.status).toBe(200);
|
|
303
|
-
|
|
304
|
-
// Hot-reload: add a redirect
|
|
305
|
-
setBlocks({
|
|
306
|
-
"redirect-1": {
|
|
307
|
-
__resolveType: "website/loaders/redirect.ts",
|
|
308
|
-
redirects: [{ from: "/v1", to: "/v2", type: "permanent" }],
|
|
309
|
-
},
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
// Same path should now redirect
|
|
313
|
-
const res2 = await worker.fetch(new Request("https://example.com/v1"), EMPTY_ENV, MOCK_CTX);
|
|
314
|
-
expect(res2.status).toBe(301);
|
|
315
|
-
expect(res2.headers.get("Location")).toBe("/v2");
|
|
316
|
-
});
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
describe("degraded origin (anti-cache-poisoning)", () => {
|
|
320
|
-
afterEach(() => {
|
|
321
|
-
setBlocks({});
|
|
322
|
-
__resetKvHydrationStateForTests();
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
it("never caches a degraded 200 and marks it no-store", async () => {
|
|
326
|
-
setBlocks({});
|
|
327
|
-
const degradedEntry = {
|
|
328
|
-
fetch: async () =>
|
|
329
|
-
new Response("empty shelf", {
|
|
330
|
-
status: 200,
|
|
331
|
-
headers: { "X-Deco-Degraded": "true" },
|
|
332
|
-
}),
|
|
333
|
-
};
|
|
334
|
-
const worker = createDecoWorkerEntry(degradedEntry, {
|
|
335
|
-
observability: false,
|
|
336
|
-
});
|
|
337
|
-
const res = await worker.fetch(
|
|
338
|
-
new Request("https://example.com/some-category"),
|
|
339
|
-
EMPTY_ENV,
|
|
340
|
-
MOCK_CTX,
|
|
341
|
-
);
|
|
342
|
-
expect(res.status).toBe(200);
|
|
343
|
-
expect(res.headers.get("X-Cache")).toBe("BYPASS");
|
|
344
|
-
expect(res.headers.get("X-Cache-Reason")).toBe("degraded");
|
|
345
|
-
expect(res.headers.get("Cache-Control")).toContain("no-store");
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
it("serves a healthy 200 origin without the degraded bypass", async () => {
|
|
349
|
-
setBlocks({});
|
|
350
|
-
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
351
|
-
observability: false,
|
|
352
|
-
});
|
|
353
|
-
const res = await worker.fetch(
|
|
354
|
-
new Request("https://example.com/some-category"),
|
|
355
|
-
EMPTY_ENV,
|
|
356
|
-
MOCK_CTX,
|
|
357
|
-
);
|
|
358
|
-
expect(res.status).toBe(200);
|
|
359
|
-
expect(res.headers.get("X-Cache-Reason")).not.toBe("degraded");
|
|
360
|
-
});
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
describe("security headers — frame-ancestors default", () => {
|
|
364
|
-
afterEach(() => {
|
|
365
|
-
__resetKvHydrationStateForTests();
|
|
366
|
-
setBlocks({});
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
const HTML_SERVER_ENTRY = {
|
|
370
|
-
fetch: async (_req: Request) =>
|
|
371
|
-
new Response("<html>hi</html>", {
|
|
372
|
-
status: 200,
|
|
373
|
-
headers: { "content-type": "text/html; charset=utf-8" },
|
|
374
|
-
}),
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
it("does NOT ship X-Frame-Options (frame-ancestors supersedes it)", () => {
|
|
378
|
-
expect(DEFAULT_SECURITY_HEADERS["X-Frame-Options"]).toBeUndefined();
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
it("allows the deco studio to embed the storefront by default", () => {
|
|
382
|
-
expect(DEFAULT_FRAME_ANCESTORS_CSP).toContain("frame-ancestors");
|
|
383
|
-
expect(DECO_ADMIN_FRAME_ANCESTORS).toContain("https://studio.decocms.com");
|
|
384
|
-
expect(DECO_ADMIN_FRAME_ANCESTORS).toContain("https://*.deco.studio");
|
|
385
|
-
expect(DECO_ADMIN_FRAME_ANCESTORS).toContain("'self'");
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
it("applies the default frame-ancestors CSP on HTML responses and drops X-Frame-Options", async () => {
|
|
389
|
-
const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY);
|
|
390
|
-
const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
391
|
-
expect(res.headers.get("Content-Security-Policy")).toBe(DEFAULT_FRAME_ANCESTORS_CSP);
|
|
392
|
-
expect(res.headers.get("X-Frame-Options")).toBeNull();
|
|
393
|
-
// Other defaults are still present.
|
|
394
|
-
expect(res.headers.get("X-Content-Type-Options")).toBe("nosniff");
|
|
395
|
-
});
|
|
396
|
-
|
|
397
|
-
it("lets a site override the frame-ancestors CSP via securityHeaders", async () => {
|
|
398
|
-
const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
|
|
399
|
-
securityHeaders: { "Content-Security-Policy": "frame-ancestors 'none'" },
|
|
400
|
-
});
|
|
401
|
-
const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
402
|
-
expect(res.headers.get("Content-Security-Policy")).toBe("frame-ancestors 'none'");
|
|
403
|
-
});
|
|
404
|
-
|
|
405
|
-
it("emits the full site CSP report-only alongside the enforced frame-ancestors", async () => {
|
|
406
|
-
const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
|
|
407
|
-
csp: ["default-src 'self'", "img-src 'self' https:"],
|
|
408
|
-
});
|
|
409
|
-
const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
410
|
-
expect(res.headers.get("Content-Security-Policy")).toBe(DEFAULT_FRAME_ANCESTORS_CSP);
|
|
411
|
-
expect(res.headers.get("Content-Security-Policy-Report-Only")).toBe(
|
|
412
|
-
"default-src 'self'; img-src 'self' https:",
|
|
413
|
-
);
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
it("omits all security headers when securityHeaders is false", async () => {
|
|
417
|
-
const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
|
|
418
|
-
securityHeaders: false,
|
|
419
|
-
});
|
|
420
|
-
const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
421
|
-
expect(res.headers.get("Content-Security-Policy")).toBeNull();
|
|
422
|
-
expect(res.headers.get("X-Content-Type-Options")).toBeNull();
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
it("keeps report-only by default even when a site passes csp (cspMode unset)", async () => {
|
|
426
|
-
const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
|
|
427
|
-
csp: ["default-src 'self'"],
|
|
428
|
-
});
|
|
429
|
-
const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
430
|
-
expect(res.headers.get("Content-Security-Policy")).toBe(DEFAULT_FRAME_ANCESTORS_CSP);
|
|
431
|
-
expect(res.headers.get("Content-Security-Policy-Report-Only")).toBe("default-src 'self'");
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
it("enforce mode emits a nonce CSP (no report-only, no unsafe-inline) on a no-store HTML response", async () => {
|
|
435
|
-
const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
|
|
436
|
-
csp: ["default-src 'self'", "script-src 'self' 'unsafe-inline' https://gtm.example"],
|
|
437
|
-
cspMode: "enforce",
|
|
438
|
-
});
|
|
439
|
-
const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
440
|
-
const csp = res.headers.get("Content-Security-Policy") ?? "";
|
|
441
|
-
// Promoted to enforced — report-only is gone.
|
|
442
|
-
expect(res.headers.get("Content-Security-Policy-Report-Only")).toBeNull();
|
|
443
|
-
expect(csp).toContain("default-src 'self'");
|
|
444
|
-
// script-src hardened: unsafe-inline dropped, per-request nonce +
|
|
445
|
-
// inline-speculation-rules added, the host allowlist kept.
|
|
446
|
-
expect(csp).toMatch(/script-src[^;]*'nonce-[A-Za-z0-9+/=]+'/);
|
|
447
|
-
expect(csp).toContain("'inline-speculation-rules'");
|
|
448
|
-
expect(csp).toContain("https://gtm.example");
|
|
449
|
-
expect(csp).not.toContain("'unsafe-inline'");
|
|
450
|
-
// Framing protection preserved.
|
|
451
|
-
expect(csp).toContain("frame-ancestors");
|
|
452
|
-
});
|
|
453
|
-
|
|
454
|
-
it("enforce mode leaves 'unsafe-eval' in place (opt-out is explicit)", async () => {
|
|
455
|
-
const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
|
|
456
|
-
csp: ["script-src 'self' 'unsafe-inline' 'unsafe-eval'"],
|
|
457
|
-
cspMode: "enforce",
|
|
458
|
-
});
|
|
459
|
-
const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
460
|
-
const csp = res.headers.get("Content-Security-Policy") ?? "";
|
|
461
|
-
expect(csp).toContain("'unsafe-eval'");
|
|
462
|
-
expect(csp).not.toContain("'unsafe-inline'");
|
|
463
|
-
});
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
describe("buildEnforcedCsp", () => {
|
|
467
|
-
const FA = "frame-ancestors 'self' https://studio.decocms.com";
|
|
468
|
-
|
|
469
|
-
it("hardens script-src: drops unsafe-inline, adds nonce + inline-speculation-rules, keeps hosts + unsafe-eval", () => {
|
|
470
|
-
const csp = buildEnforcedCsp(
|
|
471
|
-
["script-src 'self' 'unsafe-inline' 'unsafe-eval' https://gtm.example"],
|
|
472
|
-
FA,
|
|
473
|
-
"abc123",
|
|
474
|
-
);
|
|
475
|
-
expect(csp).toContain("script-src");
|
|
476
|
-
expect(csp).toContain("'self'");
|
|
477
|
-
expect(csp).toContain("https://gtm.example");
|
|
478
|
-
expect(csp).toContain("'unsafe-eval'");
|
|
479
|
-
expect(csp).toContain("'nonce-abc123'");
|
|
480
|
-
expect(csp).toContain("'inline-speculation-rules'");
|
|
481
|
-
expect(csp).not.toContain("'unsafe-inline'");
|
|
482
|
-
expect(csp).toContain(FA);
|
|
483
|
-
});
|
|
484
|
-
|
|
485
|
-
it("adds a script-src when the site declared none", () => {
|
|
486
|
-
const csp = buildEnforcedCsp(["default-src 'self'"], FA, "n0nce");
|
|
487
|
-
expect(csp).toContain("default-src 'self'");
|
|
488
|
-
expect(csp).toContain("script-src 'self' 'nonce-n0nce' 'inline-speculation-rules'");
|
|
489
|
-
expect(csp).toContain(FA);
|
|
490
|
-
});
|
|
491
|
-
|
|
492
|
-
it("drops any frame-ancestors from the site csp in favour of the canonical one", () => {
|
|
493
|
-
const csp = buildEnforcedCsp(["frame-ancestors 'none'", "default-src 'self'"], FA, "x");
|
|
494
|
-
expect(csp).not.toContain("frame-ancestors 'none'");
|
|
495
|
-
expect(csp).toContain(FA);
|
|
496
|
-
});
|
|
497
|
-
});
|
|
498
|
-
|
|
499
|
-
describe("POST /_cache/purge-loaders", () => {
|
|
500
|
-
afterEach(() => {
|
|
501
|
-
__resetKvHydrationStateForTests();
|
|
502
|
-
setBlocks({});
|
|
503
|
-
});
|
|
504
|
-
|
|
505
|
-
const purge = (env: Record<string, unknown>, headers: Record<string, string> = {}) => {
|
|
506
|
-
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
507
|
-
observability: false,
|
|
508
|
-
});
|
|
509
|
-
return worker.fetch(
|
|
510
|
-
new Request("https://example.com/_cache/purge-loaders", {
|
|
511
|
-
method: "POST",
|
|
512
|
-
headers,
|
|
513
|
-
}),
|
|
514
|
-
env,
|
|
515
|
-
MOCK_CTX,
|
|
516
|
-
);
|
|
517
|
-
};
|
|
518
|
-
|
|
519
|
-
it("401s without a valid Bearer token", async () => {
|
|
520
|
-
const res = await purge({ PURGE_TOKEN: "secret" });
|
|
521
|
-
expect(res.status).toBe(401);
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
it("401s with the wrong token", async () => {
|
|
525
|
-
const res = await purge({ PURGE_TOKEN: "secret" }, { Authorization: "Bearer nope" });
|
|
526
|
-
expect(res.status).toBe(401);
|
|
527
|
-
});
|
|
528
|
-
|
|
529
|
-
it("clears the loader cache with a valid token", async () => {
|
|
530
|
-
const res = await purge({ PURGE_TOKEN: "secret" }, { Authorization: "Bearer secret" });
|
|
531
|
-
expect(res.status).toBe(200);
|
|
532
|
-
expect(await res.json()).toEqual({ cleared: true, scope: "isolate" });
|
|
533
|
-
});
|
|
534
|
-
|
|
535
|
-
it("404s when purge is disabled (purgeTokenEnv: false)", async () => {
|
|
536
|
-
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
537
|
-
observability: false,
|
|
538
|
-
purgeTokenEnv: false,
|
|
539
|
-
});
|
|
540
|
-
const res = await worker.fetch(
|
|
541
|
-
new Request("https://example.com/_cache/purge-loaders", {
|
|
542
|
-
method: "POST",
|
|
543
|
-
}),
|
|
544
|
-
EMPTY_ENV,
|
|
545
|
-
MOCK_CTX,
|
|
546
|
-
);
|
|
547
|
-
expect(res.status).toBe(404);
|
|
548
|
-
});
|
|
549
|
-
});
|
|
550
|
-
|
|
551
|
-
describe("X-Cache-Segment — custom SegmentKey fields (#284)", () => {
|
|
552
|
-
afterEach(() => {
|
|
553
|
-
__resetKvHydrationStateForTests();
|
|
554
|
-
setBlocks({});
|
|
555
|
-
});
|
|
556
|
-
|
|
557
|
-
const HTML_SERVER_ENTRY = {
|
|
558
|
-
fetch: async (_req: Request) =>
|
|
559
|
-
new Response("<html>hi</html>", {
|
|
560
|
-
status: 200,
|
|
561
|
-
headers: { "content-type": "text/html; charset=utf-8" },
|
|
562
|
-
}),
|
|
563
|
-
};
|
|
564
|
-
|
|
565
|
-
it("includes a custom segment field in X-Cache-Segment instead of dropping it", async () => {
|
|
566
|
-
const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
|
|
567
|
-
observability: false,
|
|
568
|
-
buildSegment: () => ({
|
|
569
|
-
device: "desktop",
|
|
570
|
-
regionId: "RJ",
|
|
571
|
-
delivery: "store-42",
|
|
572
|
-
}),
|
|
573
|
-
});
|
|
574
|
-
const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
575
|
-
expect(res.headers.get("X-Cache-Segment")).toBe("desktop|r=RJ|delivery=store-42");
|
|
576
|
-
});
|
|
577
|
-
|
|
578
|
-
it("produces different X-Cache-Segment hashes for requests that only differ by a custom field", async () => {
|
|
579
|
-
let delivery = "store-1";
|
|
580
|
-
const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
|
|
581
|
-
observability: false,
|
|
582
|
-
buildSegment: () => ({ device: "desktop", delivery }),
|
|
583
|
-
});
|
|
584
|
-
|
|
585
|
-
const first = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
586
|
-
const firstSegment = first.headers.get("X-Cache-Segment");
|
|
587
|
-
|
|
588
|
-
delivery = "store-2";
|
|
589
|
-
const second = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
590
|
-
const secondSegment = second.headers.get("X-Cache-Segment");
|
|
591
|
-
|
|
592
|
-
expect(firstSegment).not.toBe(secondSegment);
|
|
593
|
-
});
|
|
594
|
-
|
|
595
|
-
it("keeps the existing hash format for segments using only known fields (back-compat)", async () => {
|
|
596
|
-
const worker = createDecoWorkerEntry(HTML_SERVER_ENTRY, {
|
|
597
|
-
observability: false,
|
|
598
|
-
buildSegment: () => ({
|
|
599
|
-
device: "desktop",
|
|
600
|
-
salesChannel: "1",
|
|
601
|
-
flags: ["b", "a"],
|
|
602
|
-
}),
|
|
603
|
-
});
|
|
604
|
-
const res = await worker.fetch(new Request("https://example.com/"), EMPTY_ENV, MOCK_CTX);
|
|
605
|
-
expect(res.headers.get("X-Cache-Segment")).toBe("desktop|sc=1|f=a,b");
|
|
606
|
-
});
|
|
607
|
-
});
|
|
608
|
-
|
|
609
|
-
describe("draft preview (pull-based)", () => {
|
|
610
|
-
const POINTER = "abc.localhost:1234/api/o/decofile/m/main?token=t@v1";
|
|
611
|
-
const SANDBOX_URL = "http://abc.localhost:1234/api/o/decofile/m/main?token=t";
|
|
612
|
-
const DRAFT_BLOCKS = {
|
|
613
|
-
"pages-home": { name: "home", path: "/", title: "DRAFTED" },
|
|
614
|
-
};
|
|
615
|
-
|
|
616
|
-
// A serverEntry that reflects the currently-loaded home title, so a test can
|
|
617
|
-
// prove the draft override actually reached the render (not just the headers).
|
|
618
|
-
const REFLECT_ENTRY = {
|
|
619
|
-
fetch: async () => {
|
|
620
|
-
const { loadBlocks } = await import("@decocms/blocks/cms");
|
|
621
|
-
const home = (loadBlocks()["pages-home"] as { title?: string } | undefined) ?? {};
|
|
622
|
-
return new Response(home.title ?? "PUBLISHED", { status: 200 });
|
|
623
|
-
},
|
|
624
|
-
};
|
|
625
|
-
|
|
626
|
-
let realFetch: typeof fetch;
|
|
627
|
-
afterEach(async () => {
|
|
628
|
-
globalThis.fetch = realFetch;
|
|
629
|
-
const { setDraftPreviewHosts, clearDraftCache } = await import("@decocms/blocks/cms");
|
|
630
|
-
setDraftPreviewHosts([]);
|
|
631
|
-
clearDraftCache();
|
|
632
|
-
__resetKvHydrationStateForTests();
|
|
633
|
-
setBlocks({});
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
async function enable() {
|
|
637
|
-
const { setDraftPreviewHosts, clearDraftCache } = await import("@decocms/blocks/cms");
|
|
638
|
-
setBlocks({
|
|
639
|
-
"pages-home": { name: "home", path: "/", title: "PUBLISHED" },
|
|
640
|
-
});
|
|
641
|
-
setDraftPreviewHosts(["example.com"]);
|
|
642
|
-
clearDraftCache();
|
|
643
|
-
realFetch = globalThis.fetch;
|
|
644
|
-
globalThis.fetch = (async (input: RequestInfo | URL) => {
|
|
645
|
-
if (String(input) === SANDBOX_URL) {
|
|
646
|
-
return new Response(JSON.stringify(DRAFT_BLOCKS), {
|
|
647
|
-
status: 200,
|
|
648
|
-
headers: { "content-type": "application/json" },
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
throw new Error(`unexpected fetch: ${String(input)}`);
|
|
652
|
-
}) as unknown as typeof fetch;
|
|
653
|
-
}
|
|
654
|
-
|
|
655
|
-
it("binds the draft, bypasses the edge cache, and stamps the anti-leak headers", async () => {
|
|
656
|
-
await enable();
|
|
657
|
-
const worker = createDecoWorkerEntry(REFLECT_ENTRY, {
|
|
658
|
-
observability: false,
|
|
659
|
-
});
|
|
660
|
-
const res = await worker.fetch(
|
|
661
|
-
new Request(`https://example.com/?__draft=${POINTER}`, {
|
|
662
|
-
headers: { host: "example.com" },
|
|
663
|
-
}),
|
|
664
|
-
EMPTY_ENV,
|
|
665
|
-
MOCK_CTX,
|
|
666
|
-
);
|
|
667
|
-
|
|
668
|
-
// The render saw the merged draft decofile.
|
|
669
|
-
expect(await res.text()).toBe("DRAFTED");
|
|
670
|
-
// Never cached under the shared URL.
|
|
671
|
-
expect(res.headers.get("X-Cache")).toBe("BYPASS");
|
|
672
|
-
expect(res.headers.get("Cache-Control")).toContain("no-store");
|
|
673
|
-
expect(res.headers.get("X-Robots-Tag")).toBe("noindex, nofollow");
|
|
674
|
-
// Entry persists the navigation cookie.
|
|
675
|
-
const cookie = res.headers.get("set-cookie") ?? "";
|
|
676
|
-
expect(cookie).toContain("__deco_draft=");
|
|
677
|
-
expect(cookie).toContain("SameSite=None");
|
|
678
|
-
expect(cookie).toContain("Partitioned");
|
|
679
|
-
});
|
|
680
|
-
|
|
681
|
-
it("carries the draft across navigation via the cookie alone", async () => {
|
|
682
|
-
await enable();
|
|
683
|
-
const worker = createDecoWorkerEntry(REFLECT_ENTRY, {
|
|
684
|
-
observability: false,
|
|
685
|
-
});
|
|
686
|
-
const res = await worker.fetch(
|
|
687
|
-
new Request("https://example.com/other", {
|
|
688
|
-
headers: { host: "example.com", cookie: `__deco_draft=${POINTER}` },
|
|
689
|
-
}),
|
|
690
|
-
EMPTY_ENV,
|
|
691
|
-
MOCK_CTX,
|
|
692
|
-
);
|
|
693
|
-
expect(await res.text()).toBe("DRAFTED");
|
|
694
|
-
expect(res.headers.get("X-Cache")).toBe("BYPASS");
|
|
695
|
-
});
|
|
696
|
-
|
|
697
|
-
it("is structurally inert on a non-preview host", async () => {
|
|
698
|
-
await enable(); // allowlist = example.com only
|
|
699
|
-
const worker = createDecoWorkerEntry(REFLECT_ENTRY, {
|
|
700
|
-
observability: false,
|
|
701
|
-
});
|
|
702
|
-
const res = await worker.fetch(
|
|
703
|
-
new Request(`https://prod.example/?__draft=${POINTER}`, {
|
|
704
|
-
headers: { host: "prod.example" },
|
|
705
|
-
}),
|
|
706
|
-
EMPTY_ENV,
|
|
707
|
-
MOCK_CTX,
|
|
708
|
-
);
|
|
709
|
-
expect(await res.text()).toBe("PUBLISHED");
|
|
710
|
-
expect(res.headers.get("X-Robots-Tag")).toBeNull();
|
|
711
|
-
expect(res.headers.get("set-cookie")).toBeNull();
|
|
712
|
-
});
|
|
713
|
-
|
|
714
|
-
it("stays inert on any host when no allowlist is configured", async () => {
|
|
715
|
-
setBlocks({
|
|
716
|
-
"pages-home": { name: "home", path: "/", title: "PUBLISHED" },
|
|
717
|
-
});
|
|
718
|
-
const worker = createDecoWorkerEntry(REFLECT_ENTRY, {
|
|
719
|
-
observability: false,
|
|
720
|
-
});
|
|
721
|
-
const res = await worker.fetch(
|
|
722
|
-
new Request(`https://example.com/?__draft=${POINTER}`, {
|
|
723
|
-
headers: { host: "example.com" },
|
|
724
|
-
}),
|
|
725
|
-
EMPTY_ENV,
|
|
726
|
-
MOCK_CTX,
|
|
727
|
-
);
|
|
728
|
-
expect(await res.text()).toBe("PUBLISHED");
|
|
729
|
-
expect(res.headers.get("X-Robots-Tag")).toBeNull();
|
|
730
|
-
});
|
|
731
|
-
});
|
|
732
|
-
|
|
733
|
-
describe('cdnCacheControl: "serverfn-segment"', () => {
|
|
734
|
-
const BUILD = "abc123";
|
|
735
|
-
const ENV = { BUILD_HASH: BUILD };
|
|
736
|
-
// The token is a hash the server issues, so tests derive it the same way the
|
|
737
|
-
// worker does instead of hardcoding a shape.
|
|
738
|
-
const tok = (seg: string) => segmentToken(seg, false, BUILD) as string;
|
|
739
|
-
|
|
740
|
-
function worker(overrides: Record<string, unknown> = {}) {
|
|
741
|
-
return createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
742
|
-
observability: false,
|
|
743
|
-
cdnCacheControl: "serverfn-segment",
|
|
744
|
-
buildSegment: (req: Request) => ({
|
|
745
|
-
device: req.headers.get("user-agent")?.includes("iPhone")
|
|
746
|
-
? ("mobile" as const)
|
|
747
|
-
: ("desktop" as const),
|
|
748
|
-
...(req.headers.get("cookie")?.includes("auth=1") ? { loggedIn: true } : {}),
|
|
749
|
-
}),
|
|
750
|
-
...overrides,
|
|
751
|
-
});
|
|
752
|
-
}
|
|
753
|
-
|
|
754
|
-
const sfnUrl = (marker?: string) =>
|
|
755
|
-
`https://example.com/_serverFn/loadCmsPage${marker ? `?__cseg=${marker}` : ""}`;
|
|
756
|
-
|
|
757
|
-
async function cdnHeader(url: string, headers: Record<string, string> = {}) {
|
|
758
|
-
const res = await worker().fetch(new Request(url, { headers }), ENV, MOCK_CTX);
|
|
759
|
-
return res.headers.get("CDN-Cache-Control");
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
it("releases the CDN when the marker matches the recomputed segment", async () => {
|
|
763
|
-
expect(await cdnHeader(sfnUrl(tok("desktop")))).toMatch(/^public, max-age=\d+$/);
|
|
764
|
-
});
|
|
765
|
-
|
|
766
|
-
it("keeps no-store without a marker (bot, curl, old client)", async () => {
|
|
767
|
-
expect(await cdnHeader(sfnUrl())).toBe("no-store");
|
|
768
|
-
});
|
|
769
|
-
|
|
770
|
-
it("keeps no-store when the marker is for another device", async () => {
|
|
771
|
-
// The forged/diverging case: a desktop request claiming a mobile entry
|
|
772
|
-
// would let the CDN serve mobile HTML to desktop.
|
|
773
|
-
expect(await cdnHeader(sfnUrl(tok("mobile")))).toBe("no-store");
|
|
774
|
-
});
|
|
775
|
-
|
|
776
|
-
it("keeps no-store when the marker is from an older build", async () => {
|
|
777
|
-
// Deploying does not purge the CDN, so a stale build token must not match.
|
|
778
|
-
expect(await cdnHeader(sfnUrl(segmentToken("desktop", false, "oldbuild") as string))).toBe(
|
|
779
|
-
"no-store",
|
|
780
|
-
);
|
|
781
|
-
});
|
|
782
|
-
|
|
783
|
-
it("keeps no-store when there is no build hash", async () => {
|
|
784
|
-
const res = await worker().fetch(new Request(sfnUrl(tok("desktop"))), {}, MOCK_CTX);
|
|
785
|
-
expect(res.headers.get("CDN-Cache-Control")).toBe("no-store");
|
|
786
|
-
});
|
|
787
|
-
|
|
788
|
-
it("keeps no-store for a bot UA even with a valid marker", async () => {
|
|
789
|
-
// Bots render every section eagerly (~10x payload). Sharing one CDN entry
|
|
790
|
-
// would serve that to humans, or the deferred one to crawlers.
|
|
791
|
-
expect(
|
|
792
|
-
await cdnHeader(sfnUrl(tok("desktop")), {
|
|
793
|
-
"user-agent": "Mozilla/5.0 (compatible; Googlebot/2.1)",
|
|
794
|
-
}),
|
|
795
|
-
).toBe("no-store");
|
|
796
|
-
});
|
|
797
|
-
|
|
798
|
-
it("keeps no-store for an A/B cohort cookie even with a valid marker", async () => {
|
|
799
|
-
expect(
|
|
800
|
-
await cdnHeader(sfnUrl(tok("desktop")), {
|
|
801
|
-
cookie: "deco_segment=eyJhY3RpdmUiOlsiYSJdfQ==",
|
|
802
|
-
}),
|
|
803
|
-
).toBe("no-store");
|
|
804
|
-
});
|
|
805
|
-
|
|
806
|
-
it("keeps no-store for a logged-in request", async () => {
|
|
807
|
-
expect(await cdnHeader(sfnUrl(tok("desktop")), { cookie: "auth=1" })).toBe("no-store");
|
|
808
|
-
});
|
|
809
|
-
|
|
810
|
-
it("keeps no-store on HTML documents — they carry no marker", async () => {
|
|
811
|
-
expect(await cdnHeader("https://example.com/some-category")).toBe("no-store");
|
|
812
|
-
});
|
|
813
|
-
|
|
814
|
-
// NOTE: the end-to-end geo case is not exercised here. `injectGeoCookies`
|
|
815
|
-
// rebuilds the Request, and `new Request(req, init)` only carries `cf`
|
|
816
|
-
// through on workerd — under Node it is dropped, so a test would assert the
|
|
817
|
-
// absence of geo rather than its handling. The unit case below covers the
|
|
818
|
-
// property that matters, and the real path is verified against a built site.
|
|
819
|
-
it("never lets two regions share a token", () => {
|
|
820
|
-
// The leak this guards: RJ and SP resolving to the same colo must not
|
|
821
|
-
// share an entry, since their regionalized pricing and stock differ.
|
|
822
|
-
expect(tok("desktop|geo=BR|RJ")).not.toBe(tok("desktop|geo=BR|SP"));
|
|
823
|
-
});
|
|
824
|
-
|
|
825
|
-
it("keeps no-store without buildSegment, since the logged-in bypass is inert", async () => {
|
|
826
|
-
const w = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
827
|
-
observability: false,
|
|
828
|
-
cdnCacheControl: "serverfn-segment",
|
|
829
|
-
});
|
|
830
|
-
const res = await w.fetch(new Request(sfnUrl(tok("desktop"))), ENV, MOCK_CTX);
|
|
831
|
-
expect(res.headers.get("CDN-Cache-Control")).toBe("no-store");
|
|
832
|
-
});
|
|
833
|
-
});
|
|
834
|
-
|
|
835
|
-
describe("cdnCacheControl default", () => {
|
|
836
|
-
const BUILD_D = "abc123";
|
|
837
|
-
const tokD = (seg: string) => segmentToken(seg, false, BUILD_D) as string;
|
|
838
|
-
|
|
839
|
-
// The default flipped from "no-store" to "serverfn-segment". That is only
|
|
840
|
-
// safe because it is inert until a verified marker arrives — these two tests
|
|
841
|
-
// are what make that claim checkable rather than asserted in a comment.
|
|
842
|
-
it("is inert for a client that never sends a marker", async () => {
|
|
843
|
-
const w = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
844
|
-
observability: false,
|
|
845
|
-
buildSegment: () => ({ device: "desktop" as const }),
|
|
846
|
-
});
|
|
847
|
-
const html = await w.fetch(
|
|
848
|
-
new Request("https://example.com/"),
|
|
849
|
-
{ BUILD_HASH: BUILD_D },
|
|
850
|
-
MOCK_CTX,
|
|
851
|
-
);
|
|
852
|
-
expect(html.headers.get("CDN-Cache-Control")).toBe("no-store");
|
|
853
|
-
|
|
854
|
-
const sfn = await w.fetch(
|
|
855
|
-
new Request("https://example.com/_serverFn/loadCmsPage"),
|
|
856
|
-
{ BUILD_HASH: BUILD_D },
|
|
857
|
-
MOCK_CTX,
|
|
858
|
-
);
|
|
859
|
-
expect(sfn.headers.get("CDN-Cache-Control")).toBe("no-store");
|
|
860
|
-
});
|
|
861
|
-
|
|
862
|
-
it("engages once the client sends a valid marker, with no site config", async () => {
|
|
863
|
-
const w = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
864
|
-
observability: false,
|
|
865
|
-
buildSegment: () => ({ device: "desktop" as const }),
|
|
866
|
-
});
|
|
867
|
-
const res = await w.fetch(
|
|
868
|
-
new Request(`https://example.com/_serverFn/loadCmsPage?__cseg=${tokD("desktop")}`),
|
|
869
|
-
{ BUILD_HASH: BUILD_D },
|
|
870
|
-
MOCK_CTX,
|
|
871
|
-
);
|
|
872
|
-
expect(res.headers.get("CDN-Cache-Control")).toMatch(/^public, max-age=\d+$/);
|
|
873
|
-
});
|
|
874
|
-
|
|
875
|
-
it('still honours an explicit opt-out with "no-store"', async () => {
|
|
876
|
-
const w = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
877
|
-
observability: false,
|
|
878
|
-
cdnCacheControl: "no-store",
|
|
879
|
-
buildSegment: () => ({ device: "desktop" as const }),
|
|
880
|
-
});
|
|
881
|
-
const res = await w.fetch(
|
|
882
|
-
new Request(`https://example.com/_serverFn/loadCmsPage?__cseg=${tokD("desktop")}`),
|
|
883
|
-
{ BUILD_HASH: BUILD_D },
|
|
884
|
-
MOCK_CTX,
|
|
885
|
-
);
|
|
886
|
-
expect(res.headers.get("CDN-Cache-Control")).toBe("no-store");
|
|
887
|
-
});
|
|
888
|
-
});
|
|
889
|
-
|
|
890
|
-
describe("CDN-Cache-Control at the single response exit", () => {
|
|
891
|
-
it("defaults to no-store on early returns that never reach dressResponse", async () => {
|
|
892
|
-
// `?asJson` returns the fully resolved page — loaders run with the caller's
|
|
893
|
-
// cookies — and returns before dressResponse, so it expresses no opinion on
|
|
894
|
-
// CDN caching. Without a default here it would inherit whatever the CDN
|
|
895
|
-
// decides for a 200 with no cache directives.
|
|
896
|
-
const w = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false });
|
|
897
|
-
const res = await w.fetch(
|
|
898
|
-
new Request("https://example.com/some-category?asJson"),
|
|
899
|
-
EMPTY_ENV,
|
|
900
|
-
MOCK_CTX,
|
|
901
|
-
);
|
|
902
|
-
expect(res.headers.get("CDN-Cache-Control")).toBe("no-store");
|
|
903
|
-
});
|
|
904
|
-
|
|
905
|
-
it("leaves a value dressResponse already decided", async () => {
|
|
906
|
-
// The cacheable path is the one branch that reasoned about whether the CDN
|
|
907
|
-
// key matches the worker key — the default must not stomp it.
|
|
908
|
-
const w = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
909
|
-
observability: false,
|
|
910
|
-
cdnCacheControl: "match-profile",
|
|
911
|
-
deviceSpecificKeys: false,
|
|
912
|
-
geoCacheKey: "off",
|
|
913
|
-
});
|
|
914
|
-
const res = await w.fetch(
|
|
915
|
-
new Request("https://example.com/some-category"),
|
|
916
|
-
EMPTY_ENV,
|
|
917
|
-
MOCK_CTX,
|
|
918
|
-
);
|
|
919
|
-
expect(res.headers.get("CDN-Cache-Control")).toMatch(/^public, max-age=\d+$/);
|
|
920
|
-
});
|
|
921
|
-
});
|
|
922
|
-
|
|
923
|
-
describe('cdnCacheControl: "match-profile" guard', () => {
|
|
924
|
-
it("is ignored while the cache key is segmented (deviceSpecificKeys defaults to true)", async () => {
|
|
925
|
-
const w = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
926
|
-
observability: false,
|
|
927
|
-
cdnCacheControl: "match-profile",
|
|
928
|
-
});
|
|
929
|
-
const res = await w.fetch(new Request("https://example.com/some-category"), {}, MOCK_CTX);
|
|
930
|
-
expect(res.headers.get("CDN-Cache-Control")).toBe("no-store");
|
|
931
|
-
});
|
|
932
|
-
|
|
933
|
-
it("is honored only when the key really is the raw URL", async () => {
|
|
934
|
-
const w = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
935
|
-
observability: false,
|
|
936
|
-
cdnCacheControl: "match-profile",
|
|
937
|
-
deviceSpecificKeys: false,
|
|
938
|
-
geoCacheKey: "off",
|
|
939
|
-
});
|
|
940
|
-
const res = await w.fetch(new Request("https://example.com/some-category"), {}, MOCK_CTX);
|
|
941
|
-
expect(res.headers.get("CDN-Cache-Control")).toMatch(/^public, max-age=\d+$/);
|
|
942
|
-
});
|
|
943
|
-
});
|
|
944
|
-
|
|
945
|
-
/**
|
|
946
|
-
* Fast-deploy hydration must run BEFORE `reconfigureAppsOnce()`.
|
|
947
|
-
*
|
|
948
|
-
* `reconfigureAppsOnce()` configures apps from the blocks it sees and then
|
|
949
|
-
* latches its promise forever. If it ran first, a `fastDeploy` build — whose
|
|
950
|
-
* bundled decofile is `{}` — would configure ZERO apps, and the only recovery
|
|
951
|
-
* is `setBlocks()`'s onChange listener, whose promise `setBlocks` drops
|
|
952
|
-
* unawaited. The request would proceed with no app loaders registered.
|
|
953
|
-
*/
|
|
954
|
-
describe("fast-deploy hydration ordering", () => {
|
|
955
|
-
// Reset BEFORE as well: both flags are module-level and latching, so an
|
|
956
|
-
// earlier test in this file that fired a request leaves `kvHydrated` true
|
|
957
|
-
// and hydration would be skipped entirely here.
|
|
958
|
-
beforeEach(() => {
|
|
959
|
-
__resetKvHydrationStateForTests();
|
|
960
|
-
__resetAutoconfigStateForTests();
|
|
961
|
-
});
|
|
962
|
-
|
|
963
|
-
afterEach(() => {
|
|
964
|
-
__resetKvHydrationStateForTests();
|
|
965
|
-
__resetAutoconfigStateForTests();
|
|
966
|
-
setBlocks({});
|
|
967
|
-
});
|
|
968
|
-
|
|
969
|
-
it("configures apps from the KV snapshot, not the stubbed bundle", async () => {
|
|
970
|
-
const KV_BLOCKS = { "deco-test": { "@type": "test-app", token: "from-kv" } };
|
|
971
|
-
const ID = "sha-ordering";
|
|
972
|
-
|
|
973
|
-
/** Blocks each configure() call observed, in order. */
|
|
974
|
-
const seen: unknown[] = [];
|
|
975
|
-
const registry = [
|
|
976
|
-
{
|
|
977
|
-
blockKey: "deco-test",
|
|
978
|
-
module: () =>
|
|
979
|
-
Promise.resolve({
|
|
980
|
-
configure: (block: unknown) => {
|
|
981
|
-
seen.push(block);
|
|
982
|
-
return Promise.resolve({
|
|
983
|
-
name: "test-app",
|
|
984
|
-
manifest: { name: "test-app", loaders: {}, actions: {} },
|
|
985
|
-
state: {},
|
|
986
|
-
});
|
|
987
|
-
},
|
|
988
|
-
}),
|
|
989
|
-
},
|
|
990
|
-
];
|
|
991
|
-
|
|
992
|
-
// autoconfigApps and its onChange listener are server-only guarded on
|
|
993
|
-
// `typeof document`. This suite runs under jsdom, so drop `document` for
|
|
994
|
-
// the duration to exercise the worker path.
|
|
995
|
-
const realDocument = globalThis.document;
|
|
996
|
-
delete (globalThis as { document?: unknown }).document;
|
|
997
|
-
try {
|
|
998
|
-
// Simulates the stubbed server bundle: autoconfigApps runs at module init
|
|
999
|
-
// with an empty decofile, exactly as it would in a fastDeploy build.
|
|
1000
|
-
setBlocks({});
|
|
1001
|
-
await autoconfigApps({}, registry as never);
|
|
1002
|
-
expect(seen).toEqual([]); // nothing to configure yet — this is the trap
|
|
1003
|
-
|
|
1004
|
-
const store = new Map<string, string>([
|
|
1005
|
-
[snapshotKey(ID), JSON.stringify(KV_BLOCKS)],
|
|
1006
|
-
[revisionKey(ID), computeRevision(KV_BLOCKS)],
|
|
1007
|
-
]);
|
|
1008
|
-
const env = {
|
|
1009
|
-
DECO_KV: {
|
|
1010
|
-
get: (k: string) => Promise.resolve(store.get(k) ?? null),
|
|
1011
|
-
put: () => Promise.resolve(),
|
|
1012
|
-
delete: () => Promise.resolve(),
|
|
1013
|
-
},
|
|
1014
|
-
DECO_FAST_DEPLOY: "1",
|
|
1015
|
-
DECO_DEPLOYMENT_ID: ID,
|
|
1016
|
-
};
|
|
1017
|
-
|
|
1018
|
-
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false });
|
|
1019
|
-
await worker.fetch(new Request("https://example.com/"), env, MOCK_CTX);
|
|
1020
|
-
|
|
1021
|
-
// The app must have been configured with the block that came from KV.
|
|
1022
|
-
// Not asserting a call COUNT: both the onChange listener and the
|
|
1023
|
-
// one-shot reinit legitimately configure, and how many passes happen is
|
|
1024
|
-
// not the contract. The contract is that no pass saw the empty bundle.
|
|
1025
|
-
// Deep equality, not identity: the snapshot arrives via JSON.parse, so
|
|
1026
|
-
// the block is a structurally-equal copy.
|
|
1027
|
-
expect(seen.length).toBeGreaterThan(0);
|
|
1028
|
-
for (const block of seen) expect(block).toEqual(KV_BLOCKS["deco-test"]);
|
|
1029
|
-
} finally {
|
|
1030
|
-
globalThis.document = realDocument;
|
|
1031
|
-
}
|
|
1032
|
-
});
|
|
1033
|
-
});
|