@decocms/blocks 8.0.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 (220) hide show
  1. package/bin/deco.js +13 -0
  2. package/package.json +32 -82
  3. package/src/index.ts +35 -8
  4. package/src/protocol/assets.ts +124 -0
  5. package/src/protocol/canonical.ts +7 -0
  6. package/src/protocol/ciphertext.ts +6 -0
  7. package/src/protocol/client.ts +170 -0
  8. package/src/protocol/conformance/cases/assets.ts +81 -0
  9. package/src/protocol/conformance/cases/guards.ts +314 -0
  10. package/src/protocol/conformance/cases/reads.ts +168 -0
  11. package/src/protocol/conformance/cases/types.ts +9 -0
  12. package/src/protocol/conformance/cases/wire.ts +138 -0
  13. package/src/protocol/conformance/cases/writes.ts +345 -0
  14. package/src/protocol/conformance/contentHashFixtures.ts +79 -0
  15. package/src/protocol/conformance/context.ts +222 -0
  16. package/src/protocol/conformance/index.ts +140 -0
  17. package/src/protocol/errors.ts +142 -0
  18. package/src/protocol/index.ts +39 -0
  19. package/src/protocol/keys.ts +279 -0
  20. package/src/protocol/params.ts +67 -0
  21. package/src/protocol/secrets.ts +231 -0
  22. package/src/protocol/server/assets.ts +119 -0
  23. package/src/protocol/server/auth.ts +49 -0
  24. package/src/protocol/server/bodyCache.ts +84 -0
  25. package/src/protocol/server/content.ts +175 -0
  26. package/src/protocol/server/core.ts +112 -0
  27. package/src/protocol/server/handler.ts +102 -0
  28. package/src/protocol/server/http.ts +139 -0
  29. package/src/protocol/server/index.ts +11 -0
  30. package/src/protocol/server/methods/apply.ts +341 -0
  31. package/src/protocol/server/methods/read.ts +156 -0
  32. package/src/protocol/server/rpc.ts +136 -0
  33. package/src/protocol/storage/fs/hash.ts +21 -0
  34. package/src/protocol/storage/fs/index.ts +338 -0
  35. package/src/protocol/storage/fs/lock.ts +68 -0
  36. package/src/protocol/storage/fs/transaction.ts +183 -0
  37. package/src/protocol/storage/fs/unsupported.ts +15 -0
  38. package/src/protocol/storage/memory.ts +238 -0
  39. package/src/protocol/storage.ts +182 -0
  40. package/src/protocol/types.ts +172 -0
  41. package/src/v8/analytics.ts +50 -0
  42. package/src/v8/background.ts +107 -0
  43. package/src/v8/builtins/data.ts +23 -0
  44. package/src/v8/builtins/index.ts +26 -0
  45. package/src/v8/builtins/lazy.ts +23 -0
  46. package/src/v8/builtins/legacy.ts +65 -0
  47. package/src/v8/builtins/matchers.ts +31 -0
  48. package/src/v8/builtins/multivariate.ts +25 -0
  49. package/src/v8/builtins/secret.ts +82 -0
  50. package/src/v8/canonical.ts +117 -0
  51. package/src/v8/ciphertext.ts +158 -0
  52. package/src/v8/cli/builtins.ts +20 -0
  53. package/src/v8/cli/check/index.ts +774 -0
  54. package/src/v8/cli/check/messages.ts +168 -0
  55. package/src/v8/cli/content.ts +287 -0
  56. package/src/v8/cli/index.ts +17 -0
  57. package/src/v8/cli/log.ts +18 -0
  58. package/src/v8/cli/main.ts +12 -0
  59. package/src/v8/cli/root.ts +66 -0
  60. package/src/v8/cli/run.ts +236 -0
  61. package/src/v8/cli/schema/builtinSchemas.ts +236 -0
  62. package/src/v8/cli/schema/generate.ts +507 -0
  63. package/src/v8/cli/schema/index.ts +63 -0
  64. package/src/v8/cli/schema/tsProgram.ts +497 -0
  65. package/src/v8/cli/schema/typeToSchema.ts +689 -0
  66. package/src/v8/cli/serve/server.ts +303 -0
  67. package/src/v8/cli/watch.ts +86 -0
  68. package/src/v8/client.ts +147 -0
  69. package/src/v8/cms.ts +237 -0
  70. package/src/v8/content.ts +195 -0
  71. package/src/v8/draft.ts +136 -0
  72. package/src/v8/errors.ts +59 -0
  73. package/src/v8/fetch.ts +133 -0
  74. package/src/v8/identity.ts +65 -0
  75. package/src/v8/index.ts +39 -0
  76. package/src/v8/json.ts +74 -0
  77. package/src/v8/matchRoute.ts +204 -0
  78. package/src/v8/remoteLoader.ts +282 -0
  79. package/src/v8/resolver.ts +278 -0
  80. package/src/v8/secrets.ts +17 -0
  81. package/src/v8/telemetry.ts +533 -0
  82. package/src/v8/types.ts +240 -0
  83. package/src/cms/applySectionConventions.ts +0 -124
  84. package/src/cms/blockSource.test.ts +0 -76
  85. package/src/cms/blockSource.ts +0 -191
  86. package/src/cms/client.browserBundle.test.ts +0 -44
  87. package/src/cms/client.ts +0 -80
  88. package/src/cms/draftSource.test.ts +0 -343
  89. package/src/cms/draftSource.ts +0 -362
  90. package/src/cms/index.ts +0 -151
  91. package/src/cms/layoutCacheRace.test.ts +0 -181
  92. package/src/cms/loadDecofileDirectory.test.ts +0 -109
  93. package/src/cms/loadDecofileDirectory.ts +0 -89
  94. package/src/cms/loader.test.ts +0 -200
  95. package/src/cms/loader.ts +0 -304
  96. package/src/cms/registry.test.ts +0 -110
  97. package/src/cms/registry.ts +0 -242
  98. package/src/cms/resolve.test.ts +0 -980
  99. package/src/cms/resolve.ts +0 -2282
  100. package/src/cms/schema.test.ts +0 -356
  101. package/src/cms/schema.ts +0 -1215
  102. package/src/cms/sectionLoaderContext.test.ts +0 -111
  103. package/src/cms/sectionLoaderContext.ts +0 -113
  104. package/src/cms/sectionLoaders.test.ts +0 -533
  105. package/src/cms/sectionLoaders.ts +0 -636
  106. package/src/cms/sectionMixins.test.ts +0 -167
  107. package/src/cms/sectionMixins.ts +0 -153
  108. package/src/cms/stickyFlags.test.ts +0 -117
  109. package/src/flags/audience.ts +0 -56
  110. package/src/flags/everyone.ts +0 -29
  111. package/src/flags/flag.ts +0 -15
  112. package/src/flags/flags.test.ts +0 -148
  113. package/src/flags/multivariate/image.ts +0 -11
  114. package/src/flags/multivariate/message.ts +0 -11
  115. package/src/flags/multivariate/page.ts +0 -16
  116. package/src/flags/multivariate/section.ts +0 -16
  117. package/src/flags/multivariate.ts +0 -1
  118. package/src/flags/types.ts +0 -59
  119. package/src/flags/utils/multivariate.ts +0 -20
  120. package/src/hooks/Image.test.ts +0 -100
  121. package/src/hooks/Image.tsx +0 -250
  122. package/src/hooks/JsonLd.tsx +0 -353
  123. package/src/hooks/LazySection.tsx +0 -121
  124. package/src/hooks/LiveControls.tsx +0 -122
  125. package/src/hooks/Picture.tsx +0 -127
  126. package/src/hooks/RenderSection.test.tsx +0 -81
  127. package/src/hooks/RenderSection.tsx +0 -91
  128. package/src/hooks/SectionErrorFallback.tsx +0 -97
  129. package/src/hooks/index.ts +0 -30
  130. package/src/matchers/builtins.test.ts +0 -318
  131. package/src/matchers/builtins.ts +0 -459
  132. package/src/matchers/countryNames.ts +0 -104
  133. package/src/matchers/override.test.ts +0 -205
  134. package/src/matchers/override.ts +0 -136
  135. package/src/matchers/posthog.ts +0 -154
  136. package/src/middleware/decoState.ts +0 -55
  137. package/src/middleware/healthMetrics.ts +0 -133
  138. package/src/middleware/hydrationContext.test.ts +0 -61
  139. package/src/middleware/hydrationContext.ts +0 -79
  140. package/src/middleware/index.ts +0 -88
  141. package/src/middleware/liveness.ts +0 -21
  142. package/src/middleware/observability.test.ts +0 -242
  143. package/src/middleware/observability.ts +0 -643
  144. package/src/middleware/validateSection.test.ts +0 -147
  145. package/src/middleware/validateSection.ts +0 -100
  146. package/src/sdk/abTesting.test.ts +0 -326
  147. package/src/sdk/abTesting.ts +0 -499
  148. package/src/sdk/analytics.ts +0 -77
  149. package/src/sdk/cacheHeaders.test.ts +0 -115
  150. package/src/sdk/cacheHeaders.ts +0 -433
  151. package/src/sdk/cachedLoader.test.ts +0 -122
  152. package/src/sdk/cachedLoader.ts +0 -503
  153. package/src/sdk/cachedLoaderModule.test.ts +0 -209
  154. package/src/sdk/clx.ts +0 -5
  155. package/src/sdk/cn.test.ts +0 -34
  156. package/src/sdk/cn.ts +0 -28
  157. package/src/sdk/composite.test.ts +0 -121
  158. package/src/sdk/composite.ts +0 -114
  159. package/src/sdk/cookie.test.ts +0 -108
  160. package/src/sdk/cookie.ts +0 -129
  161. package/src/sdk/crypto.ts +0 -185
  162. package/src/sdk/csp.ts +0 -59
  163. package/src/sdk/detectDevice.ts +0 -45
  164. package/src/sdk/djb2.ts +0 -20
  165. package/src/sdk/encoding.test.ts +0 -71
  166. package/src/sdk/encoding.ts +0 -47
  167. package/src/sdk/env.test.ts +0 -33
  168. package/src/sdk/env.ts +0 -54
  169. package/src/sdk/flags.test.ts +0 -91
  170. package/src/sdk/flags.ts +0 -108
  171. package/src/sdk/http.test.ts +0 -71
  172. package/src/sdk/http.ts +0 -124
  173. package/src/sdk/index.ts +0 -90
  174. package/src/sdk/inflightTimeout.test.ts +0 -35
  175. package/src/sdk/inflightTimeout.ts +0 -53
  176. package/src/sdk/instrumentedFetch.test.ts +0 -559
  177. package/src/sdk/instrumentedFetch.ts +0 -339
  178. package/src/sdk/invoke.test.ts +0 -115
  179. package/src/sdk/invoke.ts +0 -298
  180. package/src/sdk/logger.test.ts +0 -432
  181. package/src/sdk/logger.ts +0 -304
  182. package/src/sdk/mergeCacheControl.ts +0 -150
  183. package/src/sdk/normalizeUrls.ts +0 -91
  184. package/src/sdk/observability.ts +0 -109
  185. package/src/sdk/otel.test.ts +0 -526
  186. package/src/sdk/otel.ts +0 -1012
  187. package/src/sdk/otelAdapters/clickhouseCollector.ts +0 -65
  188. package/src/sdk/otelAdapters.test.ts +0 -89
  189. package/src/sdk/otelAdapters.ts +0 -144
  190. package/src/sdk/otelHttpLog.test.ts +0 -457
  191. package/src/sdk/otelHttpLog.ts +0 -419
  192. package/src/sdk/otelHttpMeter.test.ts +0 -292
  193. package/src/sdk/otelHttpMeter.ts +0 -506
  194. package/src/sdk/otelHttpTracer.test.ts +0 -474
  195. package/src/sdk/otelHttpTracer.ts +0 -543
  196. package/src/sdk/redirects.ts +0 -228
  197. package/src/sdk/requestContext.ts +0 -281
  198. package/src/sdk/requestContextStorage.browser.test.ts +0 -29
  199. package/src/sdk/requestContextStorage.browser.ts +0 -43
  200. package/src/sdk/requestContextStorage.ts +0 -35
  201. package/src/sdk/retry.ts +0 -45
  202. package/src/sdk/serverTimings.ts +0 -68
  203. package/src/sdk/signal.ts +0 -42
  204. package/src/sdk/sitemap.ts +0 -160
  205. package/src/sdk/urlRedaction.test.ts +0 -73
  206. package/src/sdk/urlRedaction.ts +0 -82
  207. package/src/sdk/urlUtils.ts +0 -134
  208. package/src/sdk/useDevice.test.ts +0 -130
  209. package/src/sdk/useDevice.ts +0 -89
  210. package/src/sdk/useDeviceContext.tsx +0 -108
  211. package/src/sdk/useId.ts +0 -7
  212. package/src/sdk/useScript.test.ts +0 -128
  213. package/src/sdk/useScript.ts +0 -210
  214. package/src/sdk/useSuggestions.test.ts +0 -230
  215. package/src/sdk/useSuggestions.ts +0 -188
  216. package/src/sdk/wrapCaughtErrors.ts +0 -107
  217. package/src/setup.ts +0 -120
  218. package/src/types/index.ts +0 -53
  219. package/src/types/widgets.ts +0 -14
  220. package/tsconfig.json +0 -7
@@ -1,89 +0,0 @@
1
- /**
2
- * Server-side device detection via RequestContext.
3
- *
4
- * @example
5
- * ```tsx
6
- * // In a section loader or server function:
7
- * import { detectDevice } from "@decocms/start/sdk/useDevice";
8
- *
9
- * export function loader(props: Props, req: Request) {
10
- * const device = detectDevice(req.headers.get("user-agent") ?? "");
11
- * return { ...props, device };
12
- * }
13
- *
14
- * // Or via RequestContext (no request argument needed):
15
- * import { useDevice } from "@decocms/start/sdk/useDevice";
16
- *
17
- * export function loader(props: Props) {
18
- * const device = useDevice();
19
- * return { ...props, isMobile: device === "mobile" };
20
- * }
21
- * ```
22
- */
23
-
24
- import { detectDevice } from "./detectDevice";
25
- import { RequestContext } from "./requestContext";
26
-
27
- // The pure UA-parsing half (`Device`, `MOBILE_RE`, `TABLET_RE`,
28
- // `isMobileUA`, `detectDevice`) lives in `./detectDevice` — a leaf with no
29
- // React and no RequestContext — and is re-exported below for backward
30
- // compatibility. Modules that can land in a Next.js route-handler graph
31
- // (`cms/sectionMixins.ts`, `sdk/requestContext.ts`) MUST import that leaf
32
- // directly, not this file: this file also re-exports the `"use client"`
33
- // context half, which crashes route handlers (see ./detectDevice's doc
34
- // comment for the vendored-RSC-React mechanics).
35
- export { type Device, detectDevice, isMobileUA, MOBILE_RE, TABLET_RE } from "./detectDevice";
36
-
37
- // `DeviceContext`, the `useDevice()` hook, and `DeviceProvider` live in
38
- // `./useDeviceContext` (a `"use client"` file) and are re-exported below for
39
- // full backward compatibility with this module's public API — see that
40
- // file's doc comment for why the split exists.
41
- export { DeviceContext, useDevice, DeviceProvider } from "./useDeviceContext";
42
-
43
- /**
44
- * Resolve the current device from the ambient runtime (RequestContext on the
45
- * server, `navigator.userAgent` on the client) — no React hooks involved.
46
- * Used both by the plain `check*()` helpers below and by `useDevice()`/
47
- * `DeviceProvider` in `./useDeviceContext`.
48
- */
49
- export function resolveDeviceFromRuntime(): ReturnType<typeof detectDevice> {
50
- // Server: use RequestContext UA header
51
- if (typeof document === "undefined") {
52
- const ctx = RequestContext.current;
53
- if (!ctx) return "desktop";
54
- const ua = ctx.request.headers.get("user-agent") ?? "";
55
- return detectDevice(ua);
56
- }
57
- // Client: use navigator.userAgent for consistency with server-side UA detection.
58
- // Using viewport width would produce different results between SSR and
59
- // hydration (server sees UA, client sees pixels), causing hydration mismatch.
60
- const ua = typeof navigator !== "undefined" ? navigator.userAgent : "";
61
- return detectDevice(ua);
62
- }
63
-
64
- /**
65
- * Check if the current request is from a mobile device.
66
- */
67
- export function checkMobile(): boolean {
68
- const ctx = RequestContext.current;
69
- if (!ctx) return false;
70
- return detectDevice(ctx.request.headers.get("user-agent") ?? "") === "mobile";
71
- }
72
-
73
- /**
74
- * Check if the current request is from a tablet device.
75
- */
76
- export function checkTablet(): boolean {
77
- const ctx = RequestContext.current;
78
- if (!ctx) return false;
79
- return detectDevice(ctx.request.headers.get("user-agent") ?? "") === "tablet";
80
- }
81
-
82
- /**
83
- * Check if the current request is from a desktop device.
84
- */
85
- export function checkDesktop(): boolean {
86
- const ctx = RequestContext.current;
87
- if (!ctx) return true;
88
- return detectDevice(ctx.request.headers.get("user-agent") ?? "") === "desktop";
89
- }
@@ -1,108 +0,0 @@
1
- "use client";
2
-
3
- /**
4
- * The hook/context half of device detection — split out of `useDevice.ts`
5
- * because it uses `createContext`/`useContext`, which Next.js's App Router
6
- * (unlike the Vite-based `@decocms/tanstack` binding, and unlike this
7
- * package's own Vitest/jsdom unit tests) statically rejects unless the
8
- * *file* that calls them carries a `"use client"` directive — Next's RSC
9
- * webpack loader errors ("This React Hook only works in a Client
10
- * Component") on ANY file reachable from a Server Component's module graph
11
- * that calls a client-only hook, even if the actual call site is never
12
- * invoked along that particular path.
13
- *
14
- * That's exactly what happened here: `packages/blocks/src/cms/sectionMixins.ts`
15
- * imports only the plain, hook-free `detectDevice()` from `useDevice.ts` for
16
- * its `withDevice()`/`withMobile()` section-loader mixins — a 100%
17
- * server-side call path (loaders receive a `Request`, never render). But
18
- * because `sectionMixins.ts` is pulled in by `cms/index.ts`, which
19
- * `createSiteSetup()` (runtime/setup.ts) imports, and a site's `setup.ts` is
20
- * typically imported for its side effects from `app/layout.tsx` (a Server
21
- * Component under `@decocms/nextjs`), the *whole* `useDevice.ts` file used to
22
- * ride along into the server compilation graph — including the
23
- * `createContext`/`useContext` calls now isolated in this file, which had no
24
- * "use client" directive of their own. `@decocms/nextjs`'s next-smoke fixture
25
- * (this plan's Task 9) is the first consumer to hit Next's real RSC
26
- * compiler, so this split was never needed before.
27
- *
28
- * `detectDevice`, `isMobileUA`, `checkMobile/Tablet/Desktop`, and the
29
- * `Device` type stay in `useDevice.ts` (no directive, safe to import from
30
- * server-only code); `useDevice.ts` re-exports `DeviceContext`/`useDevice`/
31
- * `DeviceProvider` from here so the public `@decocms/blocks/sdk/useDevice`
32
- * entry point (and every existing consumer, e.g.
33
- * `packages/tanstack/src/hooks/DecoPageRenderer.tsx`) is unchanged.
34
- */
35
- import { createContext, createElement, type ReactNode, useContext } from "react";
36
- import { type Device, resolveDeviceFromRuntime } from "./useDevice";
37
-
38
- /**
39
- * React context for the resolved device. Populated by `<DeviceProvider>` at
40
- * the top of the framework tree (DecoPageRenderer mounts it for sites that
41
- * use the standard wiring). Once set, `useDevice()` reads from here in
42
- * preference to `AsyncLocalStorage`, which is known to be unreliable across
43
- * streaming SSR Suspense boundaries on Cloudflare Workers.
44
- */
45
- export const DeviceContext = createContext<Device | null>(null);
46
-
47
- /**
48
- * Get the current device type. Works everywhere:
49
- * - Server (loader, middleware, server function): reads User-Agent from RequestContext.
50
- * - Client (component, event handler): uses `window.innerWidth` breakpoints.
51
- *
52
- * @example
53
- * ```tsx
54
- * import { useDevice } from "@decocms/start/sdk/useDevice";
55
- *
56
- * // In a component:
57
- * const device = useDevice(); // "mobile" | "tablet" | "desktop"
58
- * ```
59
- */
60
- export function useDevice(): Device {
61
- // Prefer the value resolved by <DeviceProvider> at the framework root —
62
- // safe across streaming-SSR Suspense boundaries where AsyncLocalStorage
63
- // can lose the request context. The try/catch keeps backward compat with
64
- // callers outside a React render (loaders, server functions, tests
65
- // without the framework root), where useContext throws "Invalid hook
66
- // call" — those callers fall through to the original runtime resolution.
67
- //
68
- // Rules-of-hooks note: `useContext` is called *unconditionally* inside the
69
- // try block — exactly once per `useDevice()` invocation. The catch only
70
- // fires when there is no React dispatcher at all (outside a render),
71
- // never *between* hooks in the same render. Hook order within a component
72
- // therefore remains consistent. Callers that conditionally call
73
- // `useDevice()` itself were already violating rules of hooks; this PR
74
- // doesn't change that.
75
- try {
76
- // eslint-disable-next-line react-hooks/rules-of-hooks
77
- const fromContext = useContext(DeviceContext);
78
- if (fromContext) return fromContext;
79
- } catch {
80
- // Not in a React render — fall through.
81
- }
82
- return resolveDeviceFromRuntime();
83
- }
84
-
85
- /**
86
- * Wraps children in a `DeviceContext` populated by resolving the device once
87
- * here, at a point in the React tree where `AsyncLocalStorage` is reliable.
88
- * Any descendant calling `useDevice()` reads from this context instead of
89
- * re-resolving through ALS — preventing the "wrong device value cached at
90
- * the edge" failure mode that produces React #418 hydration mismatches.
91
- *
92
- * Mount this near the top of the React tree. `DecoPageRenderer` already
93
- * mounts it automatically; sites with custom roots can mount it explicitly:
94
- *
95
- * @example
96
- * ```tsx
97
- * <DeviceProvider>
98
- * <App />
99
- * </DeviceProvider>
100
- * ```
101
- *
102
- * Pass an explicit `value` to override detection (useful for tests or
103
- * admin preview where the runtime UA isn't meaningful).
104
- */
105
- export function DeviceProvider(props: { children: ReactNode; value?: Device }): ReactNode {
106
- const device = props.value ?? resolveDeviceFromRuntime();
107
- return createElement(DeviceContext.Provider, { value: device }, props.children);
108
- }
package/src/sdk/useId.ts DELETED
@@ -1,7 +0,0 @@
1
- import { useId as useReactId } from "react";
2
-
3
- /** Wraps React's useId, stripping colons for safe DOM usage */
4
- export const useId = () => {
5
- const id = useReactId();
6
- return id.replace(/:/g, "");
7
- };
@@ -1,128 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
- import {
3
- HTMX_LEGACY_URL,
4
- inlineScript,
5
- usePartialSection,
6
- useScript,
7
- useSection,
8
- } from "./useScript";
9
-
10
- describe("inlineScript", () => {
11
- it("returns dangerouslySetInnerHTML with the provided string", () => {
12
- const result = inlineScript('alert("hello")');
13
- expect(result).toEqual({
14
- dangerouslySetInnerHTML: { __html: 'alert("hello")' },
15
- });
16
- });
17
-
18
- it("handles empty string", () => {
19
- const result = inlineScript("");
20
- expect(result).toEqual({
21
- dangerouslySetInnerHTML: { __html: "" },
22
- });
23
- });
24
-
25
- it("handles multiline scripts", () => {
26
- const js = `
27
- const el = document.getElementById("btn");
28
- el.addEventListener("click", () => { console.log("clicked"); });
29
- `;
30
- const result = inlineScript(js);
31
- expect(result.dangerouslySetInnerHTML.__html).toBe(js);
32
- });
33
- });
34
-
35
- describe("useScript", () => {
36
- it("serializes a function into an IIFE string", () => {
37
- function greet(name: string) {
38
- console.log(name);
39
- }
40
- const result = useScript(greet, "world");
41
- expect(result).toContain("(");
42
- expect(result).toContain('"world"');
43
- expect(result).toContain("console.log");
44
- });
45
-
46
- it("serializes multiple arguments", () => {
47
- function add(a: number, b: number) {
48
- return a + b;
49
- }
50
- const result = useScript(add, 1, 2);
51
- expect(result).toContain("1,2");
52
- });
53
-
54
- it("handles functions with no arguments", () => {
55
- function noop() {}
56
- const result = useScript(noop);
57
- expect(result).toMatch(/^\(.*\)\(\)$/);
58
- });
59
- });
60
-
61
- // ---------------------------------------------------------------------------
62
- // Legacy HTMX stubs — useSection / usePartialSection
63
- // ---------------------------------------------------------------------------
64
-
65
- describe("useSection / usePartialSection (legacy HTMX stubs)", () => {
66
- let warnSpy: ReturnType<typeof vi.spyOn>;
67
- let prevNodeEnv: string | undefined;
68
-
69
- beforeEach(() => {
70
- // Reset the dedup set so each test sees a fresh warning fire.
71
- delete (globalThis as any).__DECO_LEGACY_HTMX_WARNED;
72
- prevNodeEnv = process.env.NODE_ENV;
73
- process.env.NODE_ENV = "development";
74
- warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
75
- });
76
-
77
- afterEach(() => {
78
- warnSpy.mockRestore();
79
- if (prevNodeEnv === undefined) delete process.env.NODE_ENV;
80
- else process.env.NODE_ENV = prevNodeEnv;
81
- });
82
-
83
- it("useSection returns a stable placeholder URL instead of throwing", () => {
84
- expect(useSection()).toBe(HTMX_LEGACY_URL);
85
- expect(useSection({ props: { x: 1 } })).toBe(HTMX_LEGACY_URL);
86
- });
87
-
88
- it("usePartialSection returns the same placeholder URL", () => {
89
- expect(usePartialSection()).toBe(HTMX_LEGACY_URL);
90
- });
91
-
92
- it("warns on first call (in development)", () => {
93
- useSection();
94
- expect(warnSpy).toHaveBeenCalledTimes(1);
95
- expect(warnSpy.mock.calls[0][0]).toMatch(/useSection/);
96
- expect(warnSpy.mock.calls[0][0]).toMatch(/were removed/);
97
- expect(warnSpy.mock.calls[0][0]).toMatch(/htmx-residue/);
98
- });
99
-
100
- it("dedups warnings: a second call to the same stub is silent", () => {
101
- useSection();
102
- useSection();
103
- useSection();
104
- expect(warnSpy).toHaveBeenCalledTimes(1);
105
- });
106
-
107
- it("tracks warnings per-stub: useSection and usePartialSection warn independently", () => {
108
- useSection();
109
- usePartialSection();
110
- expect(warnSpy).toHaveBeenCalledTimes(2);
111
- const messages = warnSpy.mock.calls.map((c) => c[0] as string);
112
- expect(messages.some((m) => m.includes("useSection"))).toBe(true);
113
- expect(messages.some((m) => m.includes("usePartialSection"))).toBe(true);
114
- });
115
-
116
- it("does NOT warn in production", () => {
117
- process.env.NODE_ENV = "production";
118
- useSection();
119
- usePartialSection();
120
- expect(warnSpy).not.toHaveBeenCalled();
121
- });
122
-
123
- it("returns the placeholder URL even in production (still safe to embed)", () => {
124
- process.env.NODE_ENV = "production";
125
- expect(useSection()).toBe(HTMX_LEGACY_URL);
126
- expect(usePartialSection()).toBe(HTMX_LEGACY_URL);
127
- });
128
- });
@@ -1,210 +0,0 @@
1
- /**
2
- * Serializes functions into inline script strings for SSR output.
3
- *
4
- * Includes lightweight minification (whitespace + comment removal)
5
- * with an LRU cache to avoid re-processing the same function bodies.
6
- */
7
-
8
- // ---------------------------------------------------------------------------
9
- // LRU cache for minified function bodies
10
- // ---------------------------------------------------------------------------
11
-
12
- const MAX_CACHE_SIZE = 200;
13
- const minifyCache = new Map<string, string>();
14
-
15
- function cacheGet(key: string): string | undefined {
16
- const value = minifyCache.get(key);
17
- if (value !== undefined) {
18
- // Move to end (most recently used)
19
- minifyCache.delete(key);
20
- minifyCache.set(key, value);
21
- }
22
- return value;
23
- }
24
-
25
- function cacheSet(key: string, value: string) {
26
- if (minifyCache.size >= MAX_CACHE_SIZE) {
27
- // Delete oldest entry (first key in insertion order)
28
- const oldest = minifyCache.keys().next().value;
29
- if (oldest !== undefined) minifyCache.delete(oldest);
30
- }
31
- minifyCache.set(key, value);
32
- }
33
-
34
- // ---------------------------------------------------------------------------
35
- // Lightweight minification (no external deps)
36
- // ---------------------------------------------------------------------------
37
-
38
- function minifyJs(code: string): string {
39
- return (
40
- code
41
- // Remove block comments only — line-comment stripping and operator-spacing
42
- // collapse are unsafe: they can corrupt string literals containing "//"
43
- // or operators, and regex literals whose delimiters look like operators.
44
- .replace(/\/\*[\s\S]*?\*\//g, "")
45
- // Collapse runs of whitespace (spaces, tabs, newlines) to a single space.
46
- // This is safe because whitespace inside string/template literals is
47
- // preserved as a single space, which is semantically identical.
48
- .replace(/\s+/g, " ")
49
- .trim()
50
- );
51
- }
52
-
53
- // ---------------------------------------------------------------------------
54
- // Public API
55
- // ---------------------------------------------------------------------------
56
-
57
- /**
58
- * Serializes a function and its arguments into a self-executing inline script.
59
- *
60
- * @deprecated `fn.toString()` produces different output in SSR vs client Vite
61
- * builds (React Compiler transforms differ), causing hydration mismatches on
62
- * `dangerouslySetInnerHTML.__html`. Use {@link inlineScript} with a plain
63
- * string constant instead.
64
- *
65
- * @example
66
- * ```tsx
67
- * // BEFORE (causes hydration mismatch):
68
- * <script dangerouslySetInnerHTML={{ __html: useScript(onLoad, elementId) }} />
69
- *
70
- * // AFTER (safe):
71
- * <script {...inlineScript(`(${MY_SCRIPT_STRING})("${elementId}")`)} />
72
- * ```
73
- */
74
- export function useScript<T extends (...args: any[]) => void>(
75
- fn: T,
76
- ...args: Parameters<T>
77
- ): string {
78
- if (typeof (globalThis as any).__DECO_USE_SCRIPT_WARNED === "undefined") {
79
- (globalThis as any).__DECO_USE_SCRIPT_WARNED = new Set<string>();
80
- }
81
- const warnedSet = (globalThis as any).__DECO_USE_SCRIPT_WARNED as Set<string>;
82
- const fnName = fn.name || "anonymous";
83
- if (
84
- typeof process !== "undefined" &&
85
- process.env?.NODE_ENV !== "production" &&
86
- !warnedSet.has(fnName)
87
- ) {
88
- warnedSet.add(fnName);
89
- console.warn(
90
- `[useScript] Using fn.toString() for "${fnName}". ` +
91
- `This may produce different output in SSR vs client builds, causing hydration mismatch. ` +
92
- `Consider using inlineScript() with a plain string constant instead.`,
93
- );
94
- }
95
-
96
- const fnStr = fn.toString();
97
- let minified = cacheGet(fnStr);
98
- if (minified === undefined) {
99
- minified = minifyJs(fnStr);
100
- cacheSet(fnStr, minified);
101
- }
102
-
103
- const serializedArgs = args.map((a) => JSON.stringify(a)).join(",");
104
- return `(${minified})(${serializedArgs})`;
105
- }
106
-
107
- /**
108
- * Like useScript, but returns a data: URI suitable for `<script src="...">`.
109
- *
110
- * @deprecated Same hydration issues as {@link useScript}. Use {@link inlineScript} instead.
111
- */
112
- export function useScriptAsDataURI<T extends (...args: any[]) => void>(
113
- fn: T,
114
- ...args: Parameters<T>
115
- ): string {
116
- const code = useScript(fn, ...args);
117
- return `data:text/javascript;charset=utf-8,${encodeURIComponent(code)}`;
118
- }
119
-
120
- /**
121
- * Returns props for a `<script>` element with safe inline JavaScript.
122
- * Unlike {@link useScript}, this accepts a plain string — no `fn.toString()`
123
- * means no SSR/client divergence and no hydration mismatch.
124
- *
125
- * @example
126
- * ```tsx
127
- * const SCROLL_SCRIPT = `document.getElementById("btn").addEventListener("click", () => { ... })`;
128
- * <script {...inlineScript(SCROLL_SCRIPT)} />
129
- *
130
- * // With arguments:
131
- * const INIT_SCRIPT = (id: string) => `document.getElementById("${id}").dataset.ready = "true"`;
132
- * <script {...inlineScript(INIT_SCRIPT("my-element"))} />
133
- * ```
134
- */
135
- export function inlineScript(js: string) {
136
- return { dangerouslySetInnerHTML: { __html: js } } as const;
137
- }
138
-
139
- /**
140
- * @deprecated Removed in TanStack Start.
141
- *
142
- * The Fresh/Deno HTMX-based partial-section pattern (`useSection` /
143
- * `usePartialSection` + `sections/Component.tsx`) does not apply on
144
- * Cloudflare Workers and React. Replace call-sites with one of:
145
- *
146
- * 1. Local React state (`useState` + event handlers) for client-side toggles.
147
- * 2. `createServerFn` + `useMutation` for server actions.
148
- * 3. Direct `invoke` calls (`~/server/invoke`) for ad-hoc loaders.
149
- *
150
- * See: deco-to-tanstack-migration skill, "useComponent / partial sections"
151
- * section, for the per-pattern recipes.
152
- *
153
- * ## SSR-safe stub behavior (since 2.27)
154
- *
155
- * Earlier versions threw on call. That broke SSR for the *entire page* if a
156
- * single section still imported `useSection` — even sections that the user
157
- * never interacts with (e.g. legacy login form on the homepage). React's
158
- * error boundary would catch the throw and degrade the whole route to
159
- * client rendering.
160
- *
161
- * The current behavior:
162
- * - returns a stable placeholder URL (`HTMX_LEGACY_URL`) so it can be
163
- * embedded in `hx-get` / `hx-post` attributes without crashing the
164
- * SSR pass,
165
- * - logs a deduped `console.warn` (in dev only) per call site so the
166
- * migration signal stays loud,
167
- * - the `htmx-residue` audit rule still catalogues every call site for
168
- * systematic rewrite.
169
- *
170
- * This is a deliberate trade-off: SSR success > strict-throw enforcement.
171
- * Audit + skill docs do the enforcement instead.
172
- */
173
- const DEPRECATION_MESSAGE =
174
- "[@decocms/start] useSection / usePartialSection were removed. " +
175
- "The Fresh/Deno HTMX partial-section pattern does not apply on " +
176
- "TanStack Start / Cloudflare Workers. Replace call-sites with " +
177
- "createServerFn + useMutation, or local React state. See the " +
178
- "deco-to-tanstack-migration skill for per-pattern recipes. " +
179
- "Run `deco-post-cleanup` and look for rule [9] htmx-residue to find " +
180
- "every site call-site that still depends on this.";
181
-
182
- /**
183
- * Stable placeholder URL returned by the legacy `useSection` /
184
- * `usePartialSection` stubs. Hitting this URL surfaces a clear error.
185
- * Exported so frameworks/tests can match against it.
186
- */
187
- export const HTMX_LEGACY_URL = "/__deco_legacy_htmx_section__";
188
-
189
- function warnLegacyHtmx(name: string) {
190
- if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") {
191
- return;
192
- }
193
- if (typeof (globalThis as any).__DECO_LEGACY_HTMX_WARNED === "undefined") {
194
- (globalThis as any).__DECO_LEGACY_HTMX_WARNED = new Set<string>();
195
- }
196
- const set = (globalThis as any).__DECO_LEGACY_HTMX_WARNED as Set<string>;
197
- if (set.has(name)) return;
198
- set.add(name);
199
- console.warn(`[${name}] ${DEPRECATION_MESSAGE}`);
200
- }
201
-
202
- export function usePartialSection(_props?: Record<string, unknown>): string {
203
- warnLegacyHtmx("usePartialSection");
204
- return HTMX_LEGACY_URL;
205
- }
206
-
207
- export function useSection(_props?: Record<string, unknown>): string {
208
- warnLegacyHtmx("useSection");
209
- return HTMX_LEGACY_URL;
210
- }