@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.
Files changed (114) hide show
  1. package/package.json +6 -92
  2. package/src/index.ts +2 -12
  3. package/src/protocol/storage/memory.ts +1 -1
  4. package/src/v8/background.ts +3 -3
  5. package/src/v8/builtins/legacy.ts +1 -1
  6. package/src/v8/cli/index.ts +1 -1
  7. package/src/v8/fetch.ts +8 -27
  8. package/src/v8/remoteLoader.ts +1 -1
  9. package/src/v8/telemetry.ts +1 -1
  10. package/src/cms/applySectionConventions.ts +0 -148
  11. package/src/cms/blockSource.ts +0 -228
  12. package/src/cms/client.ts +0 -85
  13. package/src/cms/deferredTrigger.ts +0 -58
  14. package/src/cms/draftSource.ts +0 -476
  15. package/src/cms/index.ts +0 -180
  16. package/src/cms/loadDecofileDirectory.ts +0 -89
  17. package/src/cms/loader.ts +0 -563
  18. package/src/cms/registry.ts +0 -264
  19. package/src/cms/renderJson.ts +0 -179
  20. package/src/cms/resolve.ts +0 -2583
  21. package/src/cms/schema.ts +0 -1219
  22. package/src/cms/sectionLoaderContext.ts +0 -113
  23. package/src/cms/sectionLoaders.ts +0 -686
  24. package/src/cms/sectionMixins.ts +0 -153
  25. package/src/flags/audience.ts +0 -56
  26. package/src/flags/everyone.ts +0 -29
  27. package/src/flags/flag.ts +0 -15
  28. package/src/flags/multivariate/image.ts +0 -11
  29. package/src/flags/multivariate/message.ts +0 -11
  30. package/src/flags/multivariate/page.ts +0 -16
  31. package/src/flags/multivariate/section.ts +0 -16
  32. package/src/flags/multivariate.ts +0 -1
  33. package/src/flags/types.ts +0 -59
  34. package/src/flags/utils/multivariate.ts +0 -20
  35. package/src/hooks/Image.tsx +0 -314
  36. package/src/hooks/JsonLd.tsx +0 -353
  37. package/src/hooks/LazySection.tsx +0 -121
  38. package/src/hooks/LiveControls.tsx +0 -144
  39. package/src/hooks/Picture.tsx +0 -127
  40. package/src/hooks/RenderSection.tsx +0 -91
  41. package/src/hooks/SectionErrorFallback.tsx +0 -97
  42. package/src/hooks/Stats.tsx +0 -134
  43. package/src/hooks/index.ts +0 -35
  44. package/src/hooks/useLoadMore.ts +0 -100
  45. package/src/matchers/builtins.ts +0 -459
  46. package/src/matchers/countryNames.ts +0 -104
  47. package/src/matchers/override.ts +0 -136
  48. package/src/matchers/posthog.ts +0 -154
  49. package/src/middleware/decoState.ts +0 -55
  50. package/src/middleware/healthMetrics.ts +0 -133
  51. package/src/middleware/hydrationContext.ts +0 -79
  52. package/src/middleware/index.ts +0 -88
  53. package/src/middleware/liveness.ts +0 -21
  54. package/src/middleware/observability.ts +0 -800
  55. package/src/middleware/validateSection.ts +0 -100
  56. package/src/preview/DraftPreviewBadge.tsx +0 -306
  57. package/src/preview/decoMark.ts +0 -11
  58. package/src/preview/index.ts +0 -8
  59. package/src/sdk/abTesting.ts +0 -556
  60. package/src/sdk/analytics.ts +0 -147
  61. package/src/sdk/cacheHeaders.ts +0 -633
  62. package/src/sdk/cacheStorage.ts +0 -287
  63. package/src/sdk/cachedLoader.ts +0 -556
  64. package/src/sdk/clx.ts +0 -5
  65. package/src/sdk/cn.ts +0 -28
  66. package/src/sdk/composite.ts +0 -114
  67. package/src/sdk/cookie.ts +0 -129
  68. package/src/sdk/crypto.ts +0 -199
  69. package/src/sdk/csp.ts +0 -59
  70. package/src/sdk/deepOmit.ts +0 -52
  71. package/src/sdk/detectDevice.ts +0 -45
  72. package/src/sdk/djb2.ts +0 -20
  73. package/src/sdk/encoding.ts +0 -44
  74. package/src/sdk/env.ts +0 -54
  75. package/src/sdk/experiments.ts +0 -314
  76. package/src/sdk/fetchCache.ts +0 -269
  77. package/src/sdk/fetchTimeout.ts +0 -58
  78. package/src/sdk/flags.ts +0 -138
  79. package/src/sdk/http.ts +0 -124
  80. package/src/sdk/index.ts +0 -97
  81. package/src/sdk/inflightTimeout.ts +0 -53
  82. package/src/sdk/instrumentedFetch.ts +0 -362
  83. package/src/sdk/invoke.ts +0 -298
  84. package/src/sdk/logger.ts +0 -304
  85. package/src/sdk/mergeCacheControl.ts +0 -150
  86. package/src/sdk/nonce.ts +0 -46
  87. package/src/sdk/normalizeUrls.ts +0 -91
  88. package/src/sdk/observability.ts +0 -116
  89. package/src/sdk/otel.ts +0 -1118
  90. package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
  91. package/src/sdk/otelAdapters.ts +0 -144
  92. package/src/sdk/otelHttpLog.ts +0 -419
  93. package/src/sdk/otelHttpMeter.ts +0 -532
  94. package/src/sdk/otelHttpTracer.ts +0 -543
  95. package/src/sdk/redirects.ts +0 -336
  96. package/src/sdk/requestContext.ts +0 -281
  97. package/src/sdk/requestContextStorage.browser.ts +0 -43
  98. package/src/sdk/requestContextStorage.ts +0 -35
  99. package/src/sdk/responseCache.ts +0 -82
  100. package/src/sdk/retry.ts +0 -45
  101. package/src/sdk/serverTimings.ts +0 -68
  102. package/src/sdk/signal.ts +0 -42
  103. package/src/sdk/sitemap.ts +0 -160
  104. package/src/sdk/urlRedaction.ts +0 -82
  105. package/src/sdk/urlUtils.ts +0 -183
  106. package/src/sdk/useDevice.ts +0 -89
  107. package/src/sdk/useDeviceContext.tsx +0 -108
  108. package/src/sdk/useId.ts +0 -7
  109. package/src/sdk/useScript.ts +0 -210
  110. package/src/sdk/useSuggestions.ts +0 -188
  111. package/src/sdk/wrapCaughtErrors.ts +0 -107
  112. package/src/setup.ts +0 -120
  113. package/src/types/index.ts +0 -53
  114. package/src/types/widgets.ts +0 -14
@@ -1,336 +0,0 @@
1
- /**
2
- * CMS-managed redirect system.
3
- *
4
- * Loads redirect definitions from .deco/blocks/ and provides
5
- * fast path matching for use in TanStack Start middleware.
6
- *
7
- * Supports:
8
- * - Exact path matches (/old-page -> /new-page)
9
- * - Glob patterns (/old/* -> /new/*)
10
- * - Permanent (301) and temporary (302) redirects
11
- * - CSV import for bulk redirects
12
- *
13
- * @example
14
- * ```ts
15
- * // In TanStack Start middleware
16
- * import { loadRedirects, matchRedirect } from "@decocms/start/sdk/redirects";
17
- * import { loadBlocks } from "@decocms/start/cms";
18
- *
19
- * const redirects = loadRedirects(loadBlocks());
20
- *
21
- * const middleware = createMiddleware().server(async ({ next, request }) => {
22
- * const url = new URL(request.url);
23
- * const redirect = matchRedirect(url.pathname, redirects);
24
- * if (redirect) {
25
- * return new Response(null, {
26
- * status: redirect.status,
27
- * headers: { Location: redirect.to },
28
- * });
29
- * }
30
- * return next();
31
- * });
32
- * ```
33
- */
34
-
35
- // -------------------------------------------------------------------------
36
- // Types
37
- // -------------------------------------------------------------------------
38
-
39
- export interface Redirect {
40
- from: string;
41
- to: string;
42
- status: 301 | 302;
43
- }
44
-
45
- export interface RedirectMap {
46
- /** Exact match redirects for O(1) lookup. */
47
- exact: Map<string, Redirect>;
48
- /** Glob/prefix redirects checked sequentially (few in practice). */
49
- patterns: Array<{ prefix: string; redirect: Redirect }>;
50
- }
51
-
52
- // -------------------------------------------------------------------------
53
- // Loading from CMS blocks
54
- // -------------------------------------------------------------------------
55
-
56
- interface BlockRedirectEntry {
57
- from: string;
58
- to: string;
59
- type?: "permanent" | "temporary";
60
- }
61
-
62
- const REDIRECT_RESOLVE_TYPES = new Set([
63
- "website/loaders/redirect.ts",
64
- "website/loaders/redirects.ts",
65
- "website/loaders/redirectsFromCsv.ts",
66
- "deco-sites/std/loaders/x/redirects.ts",
67
- ]);
68
-
69
- /**
70
- * Register additional __resolveType strings that should be treated as redirect blocks.
71
- * Useful for custom redirect loaders.
72
- */
73
- export function registerRedirectResolveType(resolveType: string): void {
74
- REDIRECT_RESOLVE_TYPES.add(resolveType);
75
- }
76
-
77
- /**
78
- * Load all redirect definitions from CMS blocks.
79
- *
80
- * Scans the blocks for known redirect resolve types and builds
81
- * a fast-lookup redirect map.
82
- */
83
- export function loadRedirects(blocks: Record<string, unknown>): RedirectMap {
84
- const exact = new Map<string, Redirect>();
85
- const patterns: Array<{ prefix: string; redirect: Redirect }> = [];
86
-
87
- for (const [_key, block] of Object.entries(blocks)) {
88
- if (!block || typeof block !== "object") continue;
89
- const obj = block as Record<string, unknown>;
90
- const resolveType = obj.__resolveType as string | undefined;
91
-
92
- if (!resolveType || !REDIRECT_RESOLVE_TYPES.has(resolveType)) continue;
93
-
94
- const entries = (obj.redirects ?? obj.redirect) as
95
- | BlockRedirectEntry[]
96
- | BlockRedirectEntry
97
- | undefined;
98
-
99
- if (!entries) continue;
100
-
101
- const list = Array.isArray(entries) ? entries : [entries];
102
-
103
- for (const entry of list) {
104
- if (!entry.from || !entry.to) continue;
105
-
106
- const redirect: Redirect = {
107
- from: normalizePath(entry.from),
108
- to: entry.to,
109
- status: entry.type === "permanent" ? 301 : 302,
110
- };
111
-
112
- if (redirect.from.includes("*")) {
113
- const prefix = redirect.from.replace(/\*+$/, "");
114
- patterns.push({ prefix, redirect });
115
- } else {
116
- exact.set(redirect.from, redirect);
117
- }
118
- }
119
- }
120
-
121
- return { exact, patterns };
122
- }
123
-
124
- // -------------------------------------------------------------------------
125
- // CSV import
126
- // -------------------------------------------------------------------------
127
-
128
- /**
129
- * Parse a CSV string into redirect entries.
130
- *
131
- * Expected format: `from,to[,type]` (one per line).
132
- * Lines starting with # are comments. Empty lines are skipped.
133
- * Type is "permanent" (301) or "temporary" (302, default).
134
- */
135
- export function parseRedirectsCsv(csv: string): Redirect[] {
136
- const redirects: Redirect[] = [];
137
- const lines = csv.split("\n");
138
-
139
- for (const raw of lines) {
140
- const line = raw.trim();
141
- if (!line || line.startsWith("#")) continue;
142
-
143
- const parts = line.split(",").map((p) => p.trim());
144
- if (parts.length < 2) continue;
145
-
146
- const [from, to, type] = parts;
147
- if (!from || !to) continue;
148
- // Skip a header row (`from,to[,type]`). Robust for CSVs with or without a
149
- // header, and for a header repeated when multiple files are concatenated.
150
- if (from.toLowerCase() === "from" && to.toLowerCase() === "to") continue;
151
-
152
- redirects.push({
153
- from: normalizePath(from),
154
- to,
155
- status: type === "permanent" || type === "301" ? 301 : 302,
156
- });
157
- }
158
-
159
- return redirects;
160
- }
161
-
162
- /**
163
- * Add parsed redirects to an existing redirect map.
164
- */
165
- export function addRedirects(map: RedirectMap, redirects: Redirect[]): void {
166
- for (const redirect of redirects) {
167
- if (redirect.from.includes("*")) {
168
- const prefix = redirect.from.replace(/\*+$/, "");
169
- map.patterns.push({ prefix, redirect });
170
- } else {
171
- map.exact.set(redirect.from, redirect);
172
- }
173
- }
174
- }
175
-
176
- // -------------------------------------------------------------------------
177
- // Matching
178
- // -------------------------------------------------------------------------
179
-
180
- /**
181
- * Find a redirect matching the given path.
182
- *
183
- * Checks exact matches first (O(1)), then glob patterns (O(n), but
184
- * typically few patterns exist).
185
- */
186
- export function matchRedirect(pathname: string, map: RedirectMap): Redirect | null {
187
- return matchExactRedirect(pathname, map) ?? matchPatternRedirect(pathname, map);
188
- }
189
-
190
- /**
191
- * Exact half of `matchRedirect`. Split out because the KV-keyed path has to
192
- * interleave a third source between the two halves: in-memory exact, then the
193
- * `redirect:<id>:<path>` KV lookup, then patterns. Collapsing that to
194
- * "matchRedirect, then KV" would let a glob win over an exact rule, inverting
195
- * the precedence every other path has.
196
- */
197
- export function matchExactRedirect(pathname: string, map: RedirectMap): Redirect | null {
198
- return map.exact.get(normalizePath(pathname)) ?? null;
199
- }
200
-
201
- /** Pattern half of `matchRedirect` — ordered prefix scan, `*` suffix carried over. */
202
- export function matchPatternRedirect(pathname: string, map: RedirectMap): Redirect | null {
203
- const normalized = normalizePath(pathname);
204
- for (const { prefix, redirect } of map.patterns) {
205
- if (normalized.startsWith(prefix)) {
206
- const suffix = normalized.slice(prefix.length);
207
- const to = redirect.to.includes("*") ? redirect.to.replace("*", suffix) : redirect.to;
208
- return { ...redirect, to };
209
- }
210
- }
211
- return null;
212
- }
213
-
214
- // -------------------------------------------------------------------------
215
- // Splitting exact rules out of the decofile (KV-keyed redirects)
216
- // -------------------------------------------------------------------------
217
-
218
- /** One exact rule, ready to be written to its own KV key. */
219
- export interface ExactRedirect {
220
- /** Normalized path — the KV key suffix. */
221
- path: string;
222
- to: string;
223
- status: 301 | 302;
224
- }
225
-
226
- export interface SplitRedirectsResult {
227
- /** The blocks map with every EXACT rule removed. Glob rules stay (they can't
228
- * be addressed by key), and a redirect block left with no entries at all is
229
- * dropped entirely rather than left as an empty husk. */
230
- blocks: Record<string, unknown>;
231
- /** The extracted exact rules, deduped by path (last wins, matching
232
- * `loadRedirects`, which is last-write-wins over insertion order). */
233
- exact: ExactRedirect[];
234
- }
235
-
236
- /**
237
- * Split a decofile into "blocks without exact redirects" + "the exact rules".
238
- *
239
- * A bulk-migration site can carry tens of thousands of rules. Left inside the
240
- * decofile they sit in every isolate twice — the parsed snapshot graph and the
241
- * `RedirectMap` built from it — for data that is consulted at most once per
242
- * request and usually matches nothing. Moved to one KV key each, the list is
243
- * never loaded.
244
- *
245
- * Read-only over `blocks`: the returned map shares every untouched value and
246
- * only clones the redirect blocks it had to rewrite.
247
- */
248
- const MAX_KV_PATH_BYTES = 400;
249
- const utf8 = new TextEncoder();
250
-
251
- export function splitExactRedirects(blocks: Record<string, unknown>): SplitRedirectsResult {
252
- const exact = new Map<string, ExactRedirect>();
253
- const out: Record<string, unknown> = {};
254
-
255
- for (const [key, block] of Object.entries(blocks)) {
256
- const obj = block && typeof block === "object" ? (block as Record<string, unknown>) : null;
257
- const resolveType = obj?.__resolveType as string | undefined;
258
- if (!obj || !resolveType || !REDIRECT_RESOLVE_TYPES.has(resolveType)) {
259
- out[key] = block;
260
- continue;
261
- }
262
-
263
- const raw = (obj.redirects ?? obj.redirect) as
264
- | BlockRedirectEntry[]
265
- | BlockRedirectEntry
266
- | undefined;
267
- if (!raw) {
268
- out[key] = block;
269
- continue;
270
- }
271
-
272
- const kept: BlockRedirectEntry[] = [];
273
- for (const entry of Array.isArray(raw) ? raw : [raw]) {
274
- if (!entry?.from || !entry.to) continue;
275
- const from = normalizePath(entry.from);
276
- // Globs must be scanned in order against the request path, so they can
277
- // never be a key lookup — they stay in the decofile.
278
- // Paths too long for a KV key (512 bytes, minus `redirect:<id>:`) stay
279
- // in memory too — loadRedirects still serves them from the exact map.
280
- if (from.includes("*") || utf8.encode(from).length > MAX_KV_PATH_BYTES) {
281
- kept.push(entry);
282
- continue;
283
- }
284
- exact.set(from, {
285
- path: from,
286
- to: entry.to,
287
- status: entry.type === "permanent" ? 301 : 302,
288
- });
289
- }
290
-
291
- // Nothing left to scan ⇒ drop the block rather than ship an empty husk.
292
- if (kept.length === 0) continue;
293
- // Rebuild without `redirect` — a block may have carried the singular form,
294
- // and leaving it would reintroduce the rule we just extracted.
295
- const { redirect: _dropped, ...rest } = obj;
296
- out[key] = { ...rest, redirects: kept };
297
- }
298
-
299
- return { blocks: out, exact: [...exact.values()] };
300
- }
301
-
302
- // -------------------------------------------------------------------------
303
- // Helpers
304
- // -------------------------------------------------------------------------
305
-
306
- /**
307
- * Canonical redirect-path form: origin stripped, leading slash forced, trailing
308
- * slash dropped, lower-cased.
309
- *
310
- * Exported because it is the KV key contract for `redirect:<id>:<path>` — the
311
- * sync script that WRITES the keys and the worker that READS them must agree
312
- * byte for byte, or a rule is stored under a key nothing ever asks for. Never
313
- * inline a different normalization on either side.
314
- */
315
- export function normalizePath(path: string): string {
316
- let p = path.trim();
317
-
318
- // If the "from" is a full URL, extract just the pathname
319
- if (p.startsWith("http://") || p.startsWith("https://")) {
320
- try {
321
- p = new URL(p).pathname;
322
- } catch {
323
- // malformed URL, keep as-is and try the prefix fallback
324
- const slashIdx = p.indexOf("/", p.indexOf("//") + 2);
325
- p = slashIdx >= 0 ? p.slice(slashIdx) : p;
326
- }
327
- }
328
-
329
- if (!p.startsWith("/")) {
330
- p = "/" + p;
331
- }
332
- if (p.length > 1 && p.endsWith("/")) {
333
- p = p.slice(0, -1);
334
- }
335
- return p.toLowerCase();
336
- }
@@ -1,281 +0,0 @@
1
- /**
2
- * Per-request context via AsyncLocalStorage.
3
- *
4
- * Binds request-scoped state (request, abort signal, device info, flags)
5
- * that any code in the call stack can access without prop drilling.
6
- *
7
- * Requires `nodejs_compat` in wrangler.jsonc (already enabled).
8
- *
9
- * **Design decisions:**
10
- * - We do NOT monkey-patch global `fetch`. Instead, `RequestContext.fetch`
11
- * provides a fetch that auto-injects the request's AbortSignal.
12
- * - The context is optional -- code that doesn't need it just doesn't call it.
13
- * Commerce loaders receive it explicitly via the updated `CommerceLoader` sig.
14
- *
15
- * @example
16
- * ```ts
17
- * // In TanStack Start middleware:
18
- * import { RequestContext } from "@decocms/start/sdk/requestContext";
19
- *
20
- * const middleware = createMiddleware().server(async ({ next, request }) => {
21
- * return RequestContext.run(request, () => next());
22
- * });
23
- *
24
- * // Anywhere in the call stack:
25
- * const req = RequestContext.request; // the current request
26
- * const signal = RequestContext.signal; // AbortSignal
27
- * const resp = await RequestContext.fetch(url); // auto-aborts on disconnect
28
- * ```
29
- */
30
-
31
- // The `node:async_hooks`-backed storage lives in a sibling module so that
32
- // bundlers targeting the browser can swap it for a no-op stub via the
33
- // `"browser"` export condition (see `./requestContextStorage` and the
34
- // `./sdk/requestContextStorage` entry in `package.json`). We import it through
35
- // the package's own name — NOT a relative path — because export conditions are
36
- // only consulted at package-specifier boundaries; a relative `./requestContextStorage`
37
- // would always resolve straight to the Node file and drag `node:async_hooks`
38
- // into the client graph, which is exactly the `UnhandledSchemeError` we're
39
- // avoiding. Node/Vitest resolve the `default`/`node` condition → real storage;
40
- // Next.js's client webpack compiler resolves `browser` → the stub.
41
- import { storage } from "@decocms/blocks/sdk/requestContextStorage";
42
-
43
- // -------------------------------------------------------------------------
44
- // Types
45
- // -------------------------------------------------------------------------
46
-
47
- export interface RequestContextData {
48
- request: Request;
49
- signal: AbortSignal;
50
- startedAt: number;
51
- /**
52
- * Stable per-request identifier. Generated by `workerEntry.ts` from
53
- * `cf-ray` when present, falling back to `crypto.randomUUID()`. Echoed
54
- * back to the client as the `X-Request-Id` response header so:
55
- *
56
- * - The producer Worker can stamp it on every log / span / metric
57
- * attribute (`request.id`) for join queries.
58
- * - The CF tail worker reads it from the response header and stamps
59
- * the same `request.id` on tail rows — restores the join key on a
60
- * channel that has no other shared identity with direct-POST.
61
- * - Clients can include it in support requests so we can pull the
62
- * full trace + logs from one ID.
63
- *
64
- * See `MIGRATION_TOOLING_PLAN.md` D-9 for the propagation policy.
65
- */
66
- requestId: string;
67
- /** Lazily computed device type. */
68
- _device?: "mobile" | "desktop";
69
- /** Lazily computed bot detection result. */
70
- _isBot?: boolean;
71
- /** Arbitrary bag for middleware to attach custom data. */
72
- bag: Map<string, unknown>;
73
- /**
74
- * Outgoing response headers that handlers can write to.
75
- * Invoke handlers (actions/loaders) use this to forward Set-Cookie
76
- * and other headers from upstream APIs (e.g., VTEX checkout).
77
- * The invoke HTTP handler copies these into the final Response.
78
- *
79
- * This mirrors deco-cx/deco's `ctx.response.headers` pattern where
80
- * `proxySetCookie(apiResponse.headers, ctx.response.headers)` forwards
81
- * cookies transparently.
82
- */
83
- responseHeaders: Headers;
84
- }
85
-
86
- // -------------------------------------------------------------------------
87
- // Storage
88
- // -------------------------------------------------------------------------
89
-
90
- import { isMobileUA } from "./detectDevice";
91
-
92
- const BOT_RE =
93
- /bot|crawl|spider|slurp|bingpreview|facebookexternalhit|linkedinbot|twitterbot|whatsapp|telegram|googlebot|yandex|baidu|duckduck/i;
94
-
95
- // -------------------------------------------------------------------------
96
- // Public API
97
- // -------------------------------------------------------------------------
98
-
99
- export const RequestContext = {
100
- /**
101
- * Run a function within a request context.
102
- *
103
- * Call this at the outermost middleware level. Everything inside
104
- * the callback (loaders, resolvers, utilities) can access the
105
- * context via the static getters.
106
- */
107
- run<T>(request: Request, fn: () => T): T {
108
- const controller = new AbortController();
109
-
110
- if (request.signal) {
111
- if (request.signal.aborted) {
112
- controller.abort(request.signal.reason);
113
- } else {
114
- request.signal.addEventListener("abort", () => controller.abort(request.signal.reason), {
115
- once: true,
116
- });
117
- }
118
- }
119
-
120
- // Resolve request.id with explicit precedence so the choice is
121
- // auditable from a single read:
122
- // (1) inbound `x-request-id` — caller-supplied (e.g. a load balancer
123
- // already tagged the request, or a synthetic test passed one in)
124
- // (2) `cf-ray` — Cloudflare's own per-request identifier, present
125
- // on every real Workers request and globally unique
126
- // (3) `crypto.randomUUID()` — last resort for tests and locally-run
127
- // dev without `cf-ray`
128
- const incomingReqId = request.headers.get("x-request-id");
129
- const cfRay = request.headers.get("cf-ray");
130
- const requestId =
131
- incomingReqId && incomingReqId.length > 0
132
- ? incomingReqId
133
- : cfRay && cfRay.length > 0
134
- ? cfRay
135
- : (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function"
136
- ? crypto.randomUUID()
137
- : `${Date.now()}-${Math.random().toString(16).slice(2)}`);
138
-
139
- const ctx: RequestContextData = {
140
- request,
141
- signal: controller.signal,
142
- startedAt: Date.now(),
143
- requestId,
144
- bag: new Map(),
145
- responseHeaders: new Headers(),
146
- };
147
-
148
- return storage.run(ctx, fn);
149
- },
150
-
151
- /**
152
- * Get the current request context, or null if not in a request scope.
153
- */
154
- get current(): RequestContextData | null {
155
- return storage.getStore() ?? null;
156
- },
157
-
158
- /**
159
- * Get the current Request object.
160
- * @throws if called outside a request context
161
- */
162
- get request(): Request {
163
- const ctx = storage.getStore();
164
- if (!ctx) throw new Error("RequestContext.request accessed outside a request scope");
165
- return ctx.request;
166
- },
167
-
168
- /**
169
- * Get the current AbortSignal.
170
- * Use this to cancel in-flight operations when the client disconnects.
171
- */
172
- get signal(): AbortSignal {
173
- const ctx = storage.getStore();
174
- if (!ctx) throw new Error("RequestContext.signal accessed outside a request scope");
175
- return ctx.signal;
176
- },
177
-
178
- /**
179
- * The stable per-request identifier — see `RequestContextData.requestId`
180
- * for the full propagation policy. Returns `null` when called outside a
181
- * request scope (vs throwing, because observability helpers in the
182
- * shutdown path of a Worker may pull it lazily).
183
- */
184
- get requestId(): string | null {
185
- return storage.getStore()?.requestId ?? null;
186
- },
187
-
188
- /**
189
- * Detected device type based on User-Agent.
190
- */
191
- get device(): "mobile" | "desktop" {
192
- const ctx = storage.getStore();
193
- if (!ctx) return "desktop";
194
- if (ctx._device) return ctx._device;
195
- const ua = ctx.request.headers.get("user-agent") ?? "";
196
- ctx._device = isMobileUA(ua) ? "mobile" : "desktop";
197
- return ctx._device;
198
- },
199
-
200
- /**
201
- * Whether the request appears to be from a bot/crawler.
202
- */
203
- get isBot(): boolean {
204
- const ctx = storage.getStore();
205
- if (!ctx) return false;
206
- if (ctx._isBot !== undefined) return ctx._isBot;
207
- const ua = ctx.request.headers.get("user-agent") ?? "";
208
- ctx._isBot = BOT_RE.test(ua);
209
- return ctx._isBot;
210
- },
211
-
212
- /**
213
- * Elapsed time since the request started (in milliseconds).
214
- */
215
- get elapsed(): number {
216
- const ctx = storage.getStore();
217
- if (!ctx) return 0;
218
- return Date.now() - ctx.startedAt;
219
- },
220
-
221
- /**
222
- * Fetch with automatic AbortSignal injection.
223
- *
224
- * When the client disconnects, this fetch aborts automatically.
225
- * This is NOT a global monkey-patch -- only code that explicitly
226
- * calls `RequestContext.fetch()` gets this behavior.
227
- */
228
- fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
229
- const ctx = storage.getStore();
230
- if (!ctx) return globalThis.fetch(input, init);
231
-
232
- return globalThis.fetch(input, {
233
- ...init,
234
- signal: init?.signal ?? ctx.signal,
235
- });
236
- },
237
-
238
- /**
239
- * Outgoing response headers. Handlers write here; the invoke endpoint
240
- * copies them into the HTTP Response (mirroring ctx.response.headers
241
- * from deco-cx/deco).
242
- */
243
- get responseHeaders(): Headers {
244
- const ctx = storage.getStore();
245
- if (!ctx) throw new Error("RequestContext.responseHeaders accessed outside a request scope");
246
- return ctx.responseHeaders;
247
- },
248
-
249
- /**
250
- * Get/set arbitrary values in the request bag.
251
- * Useful for middleware to pass data to loaders.
252
- */
253
- getBag<T>(key: string): T | undefined {
254
- const ctx = storage.getStore();
255
- return ctx?.bag.get(key) as T | undefined;
256
- },
257
-
258
- setBag(key: string, value: unknown): void {
259
- const ctx = storage.getStore();
260
- ctx?.bag.set(key, value);
261
- },
262
-
263
- /**
264
- * Get an app's state from the request bag.
265
- * Apps register their state via `setupApps()` which injects it
266
- * into the bag as `app:{name}:state` before each request.
267
- *
268
- * @example
269
- * ```ts
270
- * import { RequestContext } from "@decocms/start/sdk/requestContext";
271
- * import type { VtexState } from "@decocms/apps/vtex/mod";
272
- *
273
- * const vtex = RequestContext.getAppState<VtexState>("vtex");
274
- * if (vtex) console.log(vtex.config.account);
275
- * ```
276
- */
277
- getAppState<T>(appName: string): T | undefined {
278
- const ctx = storage.getStore();
279
- return ctx?.bag.get(`app:${appName}:state`) as T | undefined;
280
- },
281
- };
@@ -1,43 +0,0 @@
1
- /**
2
- * Browser-bundle stand-in for the `node:async_hooks`-backed request storage in
3
- * `./requestContextStorage`. Selected automatically by bundlers that set the
4
- * `"browser"` export condition (Next.js / webpack client compiler, Vite client
5
- * build) via the conditional `exports` entry in `packages/blocks/package.json`.
6
- *
7
- * There is no per-request async context on the client, so this backend simply
8
- * reports "no active store". Every `RequestContext` accessor built on top of it
9
- * therefore behaves exactly as the real implementation does when called outside
10
- * a request scope:
11
- * - `RequestContext.current` / `requestId` → `null`
12
- * - `RequestContext.request` / `signal` / `responseHeaders` → throw
13
- * - `RequestContext.device` → `"desktop"`, `isBot` → `false`, `elapsed` → `0`
14
- * - `RequestContext.fetch` → falls back to `globalThis.fetch`
15
- *
16
- * That is exactly the fall-through the sole client-reachable consumer
17
- * (`resolveDeviceFromRuntime` in `./useDevice`) relies on: `current === null`
18
- * → use the `navigator.userAgent` client path. Nothing on the client ever
19
- * establishes a request scope, so `run()` never needs real async propagation —
20
- * it just invokes the callback inline.
21
- *
22
- * `RequestContextStorageBackend` is imported `type`-only from the real
23
- * `./requestContextStorage` module — this is erased entirely at compile time
24
- * (by tsc and by the SWC pass Next.js runs before webpack ever sees the
25
- * module graph), so it does NOT drag `node:async_hooks` back into this file's
26
- * runtime output. Annotating `storage` with it means this stub and the real
27
- * `AsyncLocalStorage`-backed implementation are compiler-checked against the
28
- * exact same narrow surface — if one drifts (e.g. `requestContext.ts` starts
29
- * calling a method neither backend exposes), `tsc` fails here, not silently
30
- * at runtime on the client.
31
- */
32
-
33
- import type { RequestContextData } from "./requestContext";
34
- import type { RequestContextStorageBackend } from "./requestContextStorage";
35
-
36
- export const storage: RequestContextStorageBackend = {
37
- getStore(): RequestContextData | undefined {
38
- return undefined;
39
- },
40
- run<R>(_store: RequestContextData, fn: () => R): R {
41
- return fn();
42
- },
43
- };
@@ -1,35 +0,0 @@
1
- /**
2
- * Node/Workers implementation of the per-request storage backend for
3
- * `RequestContext` (see `./requestContext`).
4
- *
5
- * This is the ONE module in the `requestContext` graph that touches
6
- * `node:async_hooks`. It is split out from `requestContext.ts` so that
7
- * bundlers building for a browser target can swap it for the no-op stub in
8
- * `./requestContextStorage.browser` via the `"browser"` export condition in
9
- * `packages/blocks/package.json` — without that condition, Next.js's client
10
- * webpack compiler tries to resolve the `node:async_hooks` specifier for a
11
- * non-Node target and fails the build with `UnhandledSchemeError`, even though
12
- * `AsyncLocalStorage` is only ever meaningfully used server-side.
13
- *
14
- * Requires `nodejs_compat` in wrangler.jsonc (already enabled) on Workers.
15
- */
16
-
17
- import { AsyncLocalStorage } from "node:async_hooks";
18
- import type { RequestContextData } from "./requestContext";
19
-
20
- /**
21
- * The narrow surface `requestContext.ts` actually calls (`storage.getStore()`
22
- * / `storage.run(ctx, fn)`). Exporting `storage` typed as this interface —
23
- * rather than letting it infer the full `AsyncLocalStorage<RequestContextData>`
24
- * type — means `requestContext.ts` only ever sees these two members,
25
- * regardless of which backend (this file or `./requestContextStorage.browser`)
26
- * a given bundler's export condition resolves to. If `requestContext.ts` is
27
- * ever edited to call another `AsyncLocalStorage` method (e.g. `enterWith`,
28
- * `exit`, `disable`), that becomes an immediate compile error at the call
29
- * site — forcing whoever adds it to widen this type here AND add the method
30
- * to the browser stub, instead of silently working against the real backend
31
- * while quietly breaking on the client.
32
- */
33
- export type RequestContextStorageBackend = Pick<AsyncLocalStorage<RequestContextData>, "run" | "getStore">;
34
-
35
- export const storage: RequestContextStorageBackend = new AsyncLocalStorage<RequestContextData>();