@decocms/blocks 7.85.3 → 7.86.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks",
3
- "version": "7.85.3",
3
+ "version": "7.86.0",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
@@ -54,6 +54,7 @@
54
54
  "./sdk/csp": "./src/sdk/csp.ts",
55
55
  "./sdk/detectDevice": "./src/sdk/detectDevice.ts",
56
56
  "./sdk/djb2": "./src/sdk/djb2.ts",
57
+ "./sdk/editorOrigin": "./src/sdk/editorOrigin.ts",
57
58
  "./sdk/encoding": "./src/sdk/encoding.ts",
58
59
  "./sdk/env": "./src/sdk/env.ts",
59
60
  "./sdk/fetchCache": "./src/sdk/fetchCache.ts",
@@ -1,4 +1,5 @@
1
1
  import React, { useEffect } from "react";
2
+ import { EDITOR_ORIGIN_CHECK_JS } from "../sdk/editorOrigin";
2
3
  import { getRequestNonce } from "../sdk/nonce";
3
4
 
4
5
  interface LiveControlsProps {
@@ -58,19 +59,13 @@ function LiveControlsScript() {
58
59
  if (window.__DECO_LIVE_CONTROLS__) return;
59
60
  window.__DECO_LIVE_CONTROLS__ = true;
60
61
 
61
- var TRUSTED_ORIGINS = ["https://deco.cx", "https://admin.deco.cx", "https://play.deco.cx", "https://decocms.com", "https://studio.decocms.com"];
62
- function isTrustedOrigin(origin) {
63
- return TRUSTED_ORIGINS.indexOf(origin) !== -1 ||
64
- (origin.startsWith("https://") && origin.endsWith(".deco.cx")) ||
65
- (origin.startsWith("https://") && origin.endsWith(".decocms.com")) ||
66
- origin === window.location.origin;
67
- }
62
+ ${EDITOR_ORIGIN_CHECK_JS}
68
63
 
69
64
  var LIVE = JSON.parse(document.getElementById("__DECO_STATE")?.textContent || "{}");
70
65
  window.LIVE = { ...window.LIVE, ...LIVE };
71
66
 
72
67
  window.addEventListener("message", function(event) {
73
- if (!isTrustedOrigin(event.origin)) return;
68
+ if (!isTrustedEditorOrigin(event.origin)) return;
74
69
 
75
70
  var data = event.data;
76
71
  if (!data || typeof data !== "object") return;
@@ -240,6 +240,28 @@ it("refuses to serialize private HTTP responses or cookies", async () => {
240
240
  expect(await cache.match(req)).toBeUndefined();
241
241
  });
242
242
 
243
+ it("reports what each response put did, without changing what it stores", async () => {
244
+ const req = new Request("https://site.test/");
245
+ const publicHeaders = { "cache-control": "public, max-age=60" };
246
+ const cache = createResponseCache(createMemoryCacheStorage(), "site");
247
+ expect(await cache.put(req, new Response("ok", { headers: publicHeaders }))).toBe("stored");
248
+ expect(
249
+ await cache.put(
250
+ req,
251
+ new Response("p", { headers: { "cache-control": "private, max-age=60" } }),
252
+ ),
253
+ ).toBe("skipped:uncacheable");
254
+ const huge = new Uint8Array(8 * 1024 * 1024 + 1);
255
+ expect(await cache.put(req, new Response(huge, { headers: publicHeaders }))).toBe(
256
+ "skipped:too-large",
257
+ );
258
+ const failure = vi.fn(async () => {
259
+ throw new Error("storage down");
260
+ });
261
+ const broken = createResponseCache({ get: failure, set: failure, delete: failure }, "site");
262
+ expect(await broken.put(req, new Response("ok", { headers: publicHeaders }))).toBe("error");
263
+ });
264
+
243
265
  it("bounds SWR separately from stale-if-error and stops serving at the final TTL", async () => {
244
266
  vi.useFakeTimers();
245
267
  const storage = createKVCacheStorage(fakeKV());
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Inline-script source defining `isTrustedEditorOrigin(origin)`: whether a
3
+ * postMessage sender may drive the storefront (e.g. `editor::inject`, which
4
+ * evals the script it carries). An exact list of deco admin and Studio
5
+ * origins, never a wildcard: user-written pages live on deco subdomains too
6
+ * (sandbox previews). Also the page itself, and, on a local dev page, other
7
+ * localhost ports (Studio or the admin run locally).
8
+ *
9
+ * A source string, not a function, because every consumer is a bootstrap
10
+ * `<script>` that runs before any bundle loads.
11
+ */
12
+ export const EDITOR_ORIGIN_CHECK_JS = `
13
+ var TRUSTED_EDITOR_ORIGINS = [
14
+ "https://deco.cx", "https://admin.deco.cx", "https://v0-admin.deco.cx", "https://play.deco.cx",
15
+ "https://decocms.com", "https://admin.decocms.com", "https://studio.decocms.com", "https://studio-stg.decocms.com"
16
+ ];
17
+ function isLoopbackOrigin(origin) {
18
+ try { return ["localhost", "127.0.0.1", "[::1]"].indexOf(new URL(origin).hostname) !== -1; }
19
+ catch (e) { return false; }
20
+ }
21
+ function isTrustedEditorOrigin(origin) {
22
+ return TRUSTED_EDITOR_ORIGINS.indexOf(origin) !== -1 ||
23
+ origin === window.location.origin ||
24
+ (isLoopbackOrigin(window.location.origin) && isLoopbackOrigin(origin));
25
+ }
26
+ `;
@@ -8,6 +8,25 @@ interface StoredResponse {
8
8
  body: string;
9
9
  }
10
10
 
11
+ /**
12
+ * What `put` did. Reported for observability only (the edge-cache telemetry in
13
+ * `@decocms/tanstack`); every outcome but `stored` used to be silent. The
14
+ * write semantics are the same as before: nothing here throws.
15
+ *
16
+ * - `skipped:uncacheable` — no `max-age`, a `private`/`no-store`/`no-cache`
17
+ * `Cache-Control`, or a `Set-Cookie` on the copy to store.
18
+ * - `skipped:too-large` — the body passed the 8 MiB buffering bound.
19
+ * - `error` — the storage adapter threw.
20
+ *
21
+ * `stored` means the adapter accepted the write; an adapter may still drop it
22
+ * on its own limits (an already-expired entry, the KV value size).
23
+ */
24
+ export type ResponseCachePutResult =
25
+ | "stored"
26
+ | "skipped:uncacheable"
27
+ | "skipped:too-large"
28
+ | "error";
29
+
11
30
  /** HTTP serialization belongs to the cache consumer, independent of its storage adapter. */
12
31
  export function createResponseCache(storage: CacheStorage, scope: string) {
13
32
  const keyFor = (request: Request) => JSON.stringify([scope, "responses", request.url]);
@@ -29,11 +48,11 @@ export function createResponseCache(storage: CacheStorage, scope: string) {
29
48
  return undefined;
30
49
  }
31
50
  },
32
- async put(request: Request, response: Response): Promise<void> {
51
+ async put(request: Request, response: Response): Promise<ResponseCachePutResult> {
33
52
  const cc = response.headers.get("Cache-Control") ?? "";
34
53
  const ttl = Number(/(?:^|[,\s])max-age=(\d+)/i.exec(cc)?.[1] ?? 0);
35
54
  if (!ttl || /(?:private|no-store|no-cache)/i.test(cc) || response.headers.has("set-cookie"))
36
- return;
55
+ return "skipped:uncacheable";
37
56
  const expiresAt = Date.now() + ttl * 1000;
38
57
  // Bound buffering before serialization; an oversized page can still stream to its caller.
39
58
  const chunks: Uint8Array[] = [];
@@ -46,7 +65,7 @@ export function createResponseCache(storage: CacheStorage, scope: string) {
46
65
  size += value.byteLength;
47
66
  if (size > 8 * 1024 * 1024) {
48
67
  void reader.cancel().catch(() => {});
49
- return;
68
+ return "skipped:too-large";
50
69
  }
51
70
  chunks.push(value);
52
71
  }
@@ -69,8 +88,10 @@ export function createResponseCache(storage: CacheStorage, scope: string) {
69
88
  };
70
89
  try {
71
90
  await storage.set(keyFor(request), JSON.stringify(value), expiresAt);
91
+ return "stored";
72
92
  } catch {
73
93
  /* Cache writes must not fail the response or its waitUntil task. */
94
+ return "error";
74
95
  }
75
96
  },
76
97
  async delete(request: Request): Promise<boolean> {