@decocms/blocks 8.1.0-next.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.
- package/package.json +6 -92
- package/src/index.ts +2 -12
- package/src/protocol/storage/memory.ts +1 -1
- package/src/v8/background.ts +3 -3
- package/src/v8/builtins/legacy.ts +1 -1
- package/src/v8/cli/index.ts +1 -1
- package/src/v8/fetch.ts +8 -27
- package/src/v8/remoteLoader.ts +1 -1
- package/src/v8/telemetry.ts +1 -1
- package/src/cms/applySectionConventions.ts +0 -148
- package/src/cms/blockSource.ts +0 -228
- package/src/cms/client.ts +0 -85
- package/src/cms/deferredTrigger.ts +0 -58
- package/src/cms/draftSource.ts +0 -476
- package/src/cms/index.ts +0 -180
- package/src/cms/loadDecofileDirectory.ts +0 -89
- package/src/cms/loader.ts +0 -563
- package/src/cms/registry.ts +0 -264
- package/src/cms/renderJson.ts +0 -179
- package/src/cms/resolve.ts +0 -2583
- package/src/cms/schema.ts +0 -1219
- package/src/cms/sectionLoaderContext.ts +0 -113
- package/src/cms/sectionLoaders.ts +0 -686
- package/src/cms/sectionMixins.ts +0 -153
- package/src/flags/audience.ts +0 -56
- package/src/flags/everyone.ts +0 -29
- package/src/flags/flag.ts +0 -15
- package/src/flags/multivariate/image.ts +0 -11
- package/src/flags/multivariate/message.ts +0 -11
- package/src/flags/multivariate/page.ts +0 -16
- package/src/flags/multivariate/section.ts +0 -16
- package/src/flags/multivariate.ts +0 -1
- package/src/flags/types.ts +0 -59
- package/src/flags/utils/multivariate.ts +0 -20
- package/src/hooks/Image.tsx +0 -314
- package/src/hooks/JsonLd.tsx +0 -353
- package/src/hooks/LazySection.tsx +0 -121
- package/src/hooks/LiveControls.tsx +0 -144
- package/src/hooks/Picture.tsx +0 -127
- package/src/hooks/RenderSection.tsx +0 -91
- package/src/hooks/SectionErrorFallback.tsx +0 -97
- package/src/hooks/Stats.tsx +0 -134
- package/src/hooks/index.ts +0 -35
- package/src/hooks/useLoadMore.ts +0 -100
- package/src/matchers/builtins.ts +0 -459
- package/src/matchers/countryNames.ts +0 -104
- package/src/matchers/override.ts +0 -136
- package/src/matchers/posthog.ts +0 -154
- package/src/middleware/decoState.ts +0 -55
- package/src/middleware/healthMetrics.ts +0 -133
- package/src/middleware/hydrationContext.ts +0 -79
- package/src/middleware/index.ts +0 -88
- package/src/middleware/liveness.ts +0 -21
- package/src/middleware/observability.ts +0 -800
- package/src/middleware/validateSection.ts +0 -100
- package/src/preview/DraftPreviewBadge.tsx +0 -306
- package/src/preview/decoMark.ts +0 -11
- package/src/preview/index.ts +0 -8
- package/src/sdk/abTesting.ts +0 -556
- package/src/sdk/analytics.ts +0 -147
- package/src/sdk/cacheHeaders.ts +0 -633
- package/src/sdk/cacheStorage.ts +0 -287
- package/src/sdk/cachedLoader.ts +0 -556
- package/src/sdk/clx.ts +0 -5
- package/src/sdk/cn.ts +0 -28
- package/src/sdk/composite.ts +0 -114
- package/src/sdk/cookie.ts +0 -129
- package/src/sdk/crypto.ts +0 -199
- package/src/sdk/csp.ts +0 -59
- package/src/sdk/deepOmit.ts +0 -52
- package/src/sdk/detectDevice.ts +0 -45
- package/src/sdk/djb2.ts +0 -20
- package/src/sdk/encoding.ts +0 -44
- package/src/sdk/env.ts +0 -54
- package/src/sdk/experiments.ts +0 -314
- package/src/sdk/fetchCache.ts +0 -269
- package/src/sdk/fetchTimeout.ts +0 -58
- package/src/sdk/flags.ts +0 -138
- package/src/sdk/http.ts +0 -124
- package/src/sdk/index.ts +0 -97
- package/src/sdk/inflightTimeout.ts +0 -53
- package/src/sdk/instrumentedFetch.ts +0 -362
- package/src/sdk/invoke.ts +0 -298
- package/src/sdk/logger.ts +0 -304
- package/src/sdk/mergeCacheControl.ts +0 -150
- package/src/sdk/nonce.ts +0 -46
- package/src/sdk/normalizeUrls.ts +0 -91
- package/src/sdk/observability.ts +0 -116
- package/src/sdk/otel.ts +0 -1118
- package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
- package/src/sdk/otelAdapters.ts +0 -144
- package/src/sdk/otelHttpLog.ts +0 -419
- package/src/sdk/otelHttpMeter.ts +0 -532
- package/src/sdk/otelHttpTracer.ts +0 -543
- package/src/sdk/redirects.ts +0 -336
- package/src/sdk/requestContext.ts +0 -281
- package/src/sdk/requestContextStorage.browser.ts +0 -43
- package/src/sdk/requestContextStorage.ts +0 -35
- package/src/sdk/responseCache.ts +0 -82
- package/src/sdk/retry.ts +0 -45
- package/src/sdk/serverTimings.ts +0 -68
- package/src/sdk/signal.ts +0 -42
- package/src/sdk/sitemap.ts +0 -160
- package/src/sdk/urlRedaction.ts +0 -82
- package/src/sdk/urlUtils.ts +0 -183
- package/src/sdk/useDevice.ts +0 -89
- package/src/sdk/useDeviceContext.tsx +0 -108
- package/src/sdk/useId.ts +0 -7
- package/src/sdk/useScript.ts +0 -210
- package/src/sdk/useSuggestions.ts +0 -188
- package/src/sdk/wrapCaughtErrors.ts +0 -107
- package/src/setup.ts +0 -120
- package/src/types/index.ts +0 -53
- package/src/types/widgets.ts +0 -14
package/src/sdk/cacheStorage.ts
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
import { RequestContext } from "./requestContext";
|
|
2
|
-
|
|
3
|
-
/** Storage only. Values are serialized by the caller; expiration is absolute epoch ms. */
|
|
4
|
-
export interface CacheStorage {
|
|
5
|
-
get(key: string): Promise<string | null>;
|
|
6
|
-
set(key: string, value: string, expiresAt: number): Promise<void>;
|
|
7
|
-
delete(key: string): Promise<void>;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface CacheStorageContext {
|
|
11
|
-
storage: CacheStorage | null;
|
|
12
|
-
/** Include site, deployment and content revision. Never use a tenant-wide global binding. */
|
|
13
|
-
scope: string;
|
|
14
|
-
/** Bypass both memory and shared storage for previews or private requests. */
|
|
15
|
-
disabled?: boolean;
|
|
16
|
-
waitUntil?: (work: Promise<unknown>) => void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const BAG_KEY = "deco:cache-storage";
|
|
20
|
-
|
|
21
|
-
export function bindCacheStorage(context: CacheStorageContext): void {
|
|
22
|
-
RequestContext.setBag(BAG_KEY, context);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function getCacheStorageContext(): CacheStorageContext | undefined {
|
|
26
|
-
return RequestContext.getBag<CacheStorageContext>(BAG_KEY);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** Keep optional cache work alive on Workers, without letting storage errors fail a request. */
|
|
30
|
-
export function cacheBackground(work: Promise<unknown>): void {
|
|
31
|
-
const safe = work.catch(() => {});
|
|
32
|
-
getCacheStorageContext()?.waitUntil?.(safe);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export async function hashCacheKey(key: string): Promise<string> {
|
|
36
|
-
const bytes = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(key));
|
|
37
|
-
return Array.from(new Uint8Array(bytes), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
interface StoredValue {
|
|
41
|
-
value: string;
|
|
42
|
-
expiresAt: number;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function decode(raw: string | null): string | null {
|
|
46
|
-
if (raw === null) return null;
|
|
47
|
-
try {
|
|
48
|
-
const entry = JSON.parse(raw) as StoredValue;
|
|
49
|
-
return typeof entry.value === "string" &&
|
|
50
|
-
Number.isFinite(entry.expiresAt) &&
|
|
51
|
-
Date.now() < entry.expiresAt
|
|
52
|
-
? entry.value
|
|
53
|
-
: null;
|
|
54
|
-
} catch {
|
|
55
|
-
return null;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** Bounded LRU adapter, useful in Node, development and tests. */
|
|
60
|
-
export function createMemoryCacheStorage(maxBytes = 32 * 1024 * 1024): CacheStorage {
|
|
61
|
-
const entries = new Map<string, string>();
|
|
62
|
-
let bytes = 0;
|
|
63
|
-
function remove(key: string) {
|
|
64
|
-
const old = entries.get(key);
|
|
65
|
-
if (old !== undefined) bytes -= (key.length + old.length) * 2;
|
|
66
|
-
entries.delete(key);
|
|
67
|
-
}
|
|
68
|
-
return {
|
|
69
|
-
async get(key) {
|
|
70
|
-
const raw = entries.get(key);
|
|
71
|
-
const value = decode(raw ?? null);
|
|
72
|
-
if (value === null) remove(key);
|
|
73
|
-
else {
|
|
74
|
-
entries.delete(key);
|
|
75
|
-
entries.set(key, raw!);
|
|
76
|
-
}
|
|
77
|
-
return value;
|
|
78
|
-
},
|
|
79
|
-
async set(key, value, expiresAt) {
|
|
80
|
-
remove(key);
|
|
81
|
-
if (!Number.isFinite(expiresAt) || expiresAt <= Date.now()) return;
|
|
82
|
-
const raw = JSON.stringify({ value, expiresAt });
|
|
83
|
-
const size = (key.length + raw.length) * 2;
|
|
84
|
-
if (size > maxBytes) return;
|
|
85
|
-
entries.set(key, raw);
|
|
86
|
-
bytes += size;
|
|
87
|
-
while (bytes > maxBytes) remove(entries.keys().next().value!);
|
|
88
|
-
},
|
|
89
|
-
async delete(key) {
|
|
90
|
-
remove(key);
|
|
91
|
-
},
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** Structural binding type; the core has no Cloudflare package dependency. */
|
|
96
|
-
export interface CacheKVNamespace {
|
|
97
|
-
get(key: string): Promise<string | null>;
|
|
98
|
-
put(key: string, value: string, options: { expirationTtl: number }): Promise<void>;
|
|
99
|
-
delete(key: string): Promise<void>;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export function createKVCacheStorage(
|
|
103
|
-
kv: CacheKVNamespace,
|
|
104
|
-
prefix = "deco-cache:v1:",
|
|
105
|
-
): CacheStorage {
|
|
106
|
-
const keyFor = async (key: string) => prefix + (await hashCacheKey(key));
|
|
107
|
-
return {
|
|
108
|
-
async get(key) {
|
|
109
|
-
return decode(await kv.get(await keyFor(key)));
|
|
110
|
-
},
|
|
111
|
-
async set(key, value, expiresAt) {
|
|
112
|
-
if (!Number.isFinite(expiresAt) || expiresAt <= Date.now()) return;
|
|
113
|
-
const raw = JSON.stringify({ value, expiresAt });
|
|
114
|
-
// Leave space below KV's 25 MiB value limit. Oversized cache entries are optional.
|
|
115
|
-
if (new TextEncoder().encode(raw).byteLength > 25 * 1024 * 1024) return;
|
|
116
|
-
await kv.put(await keyFor(key), raw, {
|
|
117
|
-
expirationTtl: Math.max(60, Math.ceil((expiresAt - Date.now()) / 1000)),
|
|
118
|
-
});
|
|
119
|
-
},
|
|
120
|
-
async delete(key) {
|
|
121
|
-
await kv.delete(await keyFor(key));
|
|
122
|
-
},
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** The app supplies the native cache and its origin; no global API detection in consumers. */
|
|
127
|
-
export function createWebCacheStorage(
|
|
128
|
-
cache: Pick<Cache, "match" | "put" | "delete">,
|
|
129
|
-
origin: string,
|
|
130
|
-
): CacheStorage {
|
|
131
|
-
const requestFor = async (key: string) =>
|
|
132
|
-
new Request(new URL(`/__deco_cache/${await hashCacheKey(key)}`, origin));
|
|
133
|
-
return {
|
|
134
|
-
async get(key) {
|
|
135
|
-
const response = await cache.match(await requestFor(key));
|
|
136
|
-
return decode(response ? await response.text() : null);
|
|
137
|
-
},
|
|
138
|
-
async set(key, value, expiresAt) {
|
|
139
|
-
if (!Number.isFinite(expiresAt) || expiresAt <= Date.now()) return;
|
|
140
|
-
await cache.put(
|
|
141
|
-
await requestFor(key),
|
|
142
|
-
new Response(JSON.stringify({ value, expiresAt }), {
|
|
143
|
-
headers: {
|
|
144
|
-
"Cache-Control": `public, max-age=${Math.max(1, Math.ceil((expiresAt - Date.now()) / 1000))}`,
|
|
145
|
-
},
|
|
146
|
-
}),
|
|
147
|
-
);
|
|
148
|
-
},
|
|
149
|
-
async delete(key) {
|
|
150
|
-
await cache.delete(await requestFor(key));
|
|
151
|
-
},
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Reject values JSON would silently change, including dates, functions and circular references. */
|
|
156
|
-
function serialize(value: unknown): string | undefined {
|
|
157
|
-
const seen = new Set<object>();
|
|
158
|
-
function check(v: unknown): void {
|
|
159
|
-
if (v === null || typeof v === "string" || typeof v === "boolean") return;
|
|
160
|
-
if (typeof v === "number" && Number.isFinite(v)) return;
|
|
161
|
-
// Optional object fields are common in section props. JSON omits them safely.
|
|
162
|
-
if (v === undefined) return;
|
|
163
|
-
if (typeof v !== "object" || seen.has(v)) throw new Error("Unsupported cached value");
|
|
164
|
-
if (
|
|
165
|
-
!Array.isArray(v) &&
|
|
166
|
-
Object.getPrototypeOf(v) !== Object.prototype &&
|
|
167
|
-
Object.getPrototypeOf(v) !== null
|
|
168
|
-
) {
|
|
169
|
-
throw new Error("Unsupported cached object");
|
|
170
|
-
}
|
|
171
|
-
seen.add(v);
|
|
172
|
-
for (const item of Object.values(v)) check(item);
|
|
173
|
-
seen.delete(v);
|
|
174
|
-
}
|
|
175
|
-
check(value);
|
|
176
|
-
return JSON.stringify(value);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Typed cache with a bounded, decoded memory tier. All remote access uses CacheStorage.
|
|
181
|
-
* Call key() once per operation so a content change cannot redirect an in-flight write.
|
|
182
|
-
* In-flight promises remain in the calling isolate; only completed results are persisted.
|
|
183
|
-
*/
|
|
184
|
-
export function createCacheStore<T>(
|
|
185
|
-
namespace: string,
|
|
186
|
-
maxEntries = 200,
|
|
187
|
-
maxBytes = 4 * 1024 * 1024,
|
|
188
|
-
sizeOf?: (value: T) => number,
|
|
189
|
-
) {
|
|
190
|
-
const local = new Map<string, { value: T; expiresAt: number; bytes: number }>();
|
|
191
|
-
let bytes = 0;
|
|
192
|
-
let generation = 0;
|
|
193
|
-
function remove(key: string) {
|
|
194
|
-
const entry = local.get(key);
|
|
195
|
-
if (entry) bytes -= entry.bytes;
|
|
196
|
-
local.delete(key);
|
|
197
|
-
}
|
|
198
|
-
function remember(key: string, value: T, expiresAt: number) {
|
|
199
|
-
remove(key);
|
|
200
|
-
let size: number;
|
|
201
|
-
try {
|
|
202
|
-
size = sizeOf
|
|
203
|
-
? sizeOf(value)
|
|
204
|
-
: Math.max(512, (key.length + (JSON.stringify(value)?.length ?? 0)) * 2);
|
|
205
|
-
} catch {
|
|
206
|
-
size = 1024;
|
|
207
|
-
}
|
|
208
|
-
if (size > maxBytes) return;
|
|
209
|
-
local.set(key, { value, expiresAt, bytes: size });
|
|
210
|
-
bytes += size;
|
|
211
|
-
while (local.size > 0 && (local.size > maxEntries || bytes > maxBytes))
|
|
212
|
-
remove(local.keys().next().value!);
|
|
213
|
-
}
|
|
214
|
-
function get(key: string): T | undefined {
|
|
215
|
-
if (getCacheStorageContext()?.disabled) return undefined;
|
|
216
|
-
const entry = local.get(key);
|
|
217
|
-
if (!entry) return undefined;
|
|
218
|
-
if (Date.now() >= entry.expiresAt) {
|
|
219
|
-
remove(key);
|
|
220
|
-
return undefined;
|
|
221
|
-
}
|
|
222
|
-
local.delete(key);
|
|
223
|
-
local.set(key, entry);
|
|
224
|
-
return entry.value;
|
|
225
|
-
}
|
|
226
|
-
return {
|
|
227
|
-
key(raw: string): string {
|
|
228
|
-
return JSON.stringify([getCacheStorageContext()?.scope ?? "local", namespace, raw]);
|
|
229
|
-
},
|
|
230
|
-
get,
|
|
231
|
-
async read(key: string): Promise<T | undefined> {
|
|
232
|
-
const hit = get(key);
|
|
233
|
-
if (hit !== undefined) return hit;
|
|
234
|
-
const context = getCacheStorageContext();
|
|
235
|
-
if (!context?.storage || context.disabled) return undefined;
|
|
236
|
-
const gen = generation;
|
|
237
|
-
try {
|
|
238
|
-
const raw = await context.storage.get(key);
|
|
239
|
-
if (gen !== generation) return undefined;
|
|
240
|
-
const concurrent = get(key);
|
|
241
|
-
if (concurrent !== undefined) return concurrent;
|
|
242
|
-
if (raw === null) return undefined;
|
|
243
|
-
const entry = JSON.parse(raw) as { value: T; expiresAt: number };
|
|
244
|
-
if (!Number.isFinite(entry.expiresAt) || Date.now() >= entry.expiresAt) return undefined;
|
|
245
|
-
remember(key, entry.value, entry.expiresAt);
|
|
246
|
-
return entry.value;
|
|
247
|
-
} catch {
|
|
248
|
-
return undefined;
|
|
249
|
-
}
|
|
250
|
-
},
|
|
251
|
-
set(key: string, value: T, expiresAt: number, persist = true): void {
|
|
252
|
-
if (
|
|
253
|
-
getCacheStorageContext()?.disabled ||
|
|
254
|
-
!Number.isFinite(expiresAt) ||
|
|
255
|
-
expiresAt <= Date.now()
|
|
256
|
-
)
|
|
257
|
-
return;
|
|
258
|
-
remember(key, value, expiresAt);
|
|
259
|
-
const storage = getCacheStorageContext()?.storage;
|
|
260
|
-
if (!persist || !storage || !Number.isFinite(expiresAt)) return;
|
|
261
|
-
try {
|
|
262
|
-
const raw = serialize({ value, expiresAt });
|
|
263
|
-
if (raw !== undefined) cacheBackground(storage.set(key, raw, expiresAt));
|
|
264
|
-
} catch {
|
|
265
|
-
/* Non-serializable results remain in the memory tier. */
|
|
266
|
-
}
|
|
267
|
-
},
|
|
268
|
-
delete(key: string): void {
|
|
269
|
-
remove(key);
|
|
270
|
-
},
|
|
271
|
-
/** Local eviction only. Shared invalidation uses a new scope/version or storage.delete(). */
|
|
272
|
-
clear(): void {
|
|
273
|
-
local.clear();
|
|
274
|
-
bytes = 0;
|
|
275
|
-
generation++;
|
|
276
|
-
},
|
|
277
|
-
get size(): number {
|
|
278
|
-
return local.size;
|
|
279
|
-
},
|
|
280
|
-
get estimatedBytes(): number {
|
|
281
|
-
return bytes;
|
|
282
|
-
},
|
|
283
|
-
*entries(): IterableIterator<[string, T]> {
|
|
284
|
-
for (const [key, entry] of local) yield [key, entry.value];
|
|
285
|
-
},
|
|
286
|
-
};
|
|
287
|
-
}
|