@takazudo/zfb 0.1.0-next.10
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/CHANGELOG.md +45 -0
- package/LICENSE +21 -0
- package/README.md +207 -0
- package/bin/zfb.mjs +82 -0
- package/dist/config.d.ts +542 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +24 -0
- package/dist/config.js.map +1 -0
- package/dist/content.d.ts +240 -0
- package/dist/content.d.ts.map +1 -0
- package/dist/content.js +460 -0
- package/dist/content.js.map +1 -0
- package/dist/frontmatter.d.ts +23 -0
- package/dist/frontmatter.d.ts.map +1 -0
- package/dist/frontmatter.js +142 -0
- package/dist/frontmatter.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/island.d.ts +121 -0
- package/dist/island.d.ts.map +1 -0
- package/dist/island.js +273 -0
- package/dist/island.js.map +1 -0
- package/dist/jsx-types.d.ts +37 -0
- package/dist/jsx-types.d.ts.map +1 -0
- package/dist/jsx-types.js +12 -0
- package/dist/jsx-types.js.map +1 -0
- package/dist/paginate.d.ts +43 -0
- package/dist/paginate.d.ts.map +1 -0
- package/dist/paginate.js +44 -0
- package/dist/paginate.js.map +1 -0
- package/dist/plugins.d.ts +259 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +42 -0
- package/dist/plugins.js.map +1 -0
- package/dist/runtime.d.ts +101 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +454 -0
- package/dist/runtime.js.map +1 -0
- package/dist/types.d.ts +27 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +33 -0
- package/dist/types.js.map +1 -0
- package/package.json +98 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Public entry point for the "@takazudo/zfb" package.
|
|
2
|
+
//
|
|
3
|
+
// User TSX pages reach this module via `import { Island } from "@takazudo/zfb"`.
|
|
4
|
+
// The hydration runtime (Sub 3) reaches the helper via
|
|
5
|
+
// `import { scheduleHydrate } from "@takazudo/zfb/runtime"` (or by inlining the
|
|
6
|
+
// same logic; coordinated separately).
|
|
7
|
+
export { ANONYMOUS_COMPONENT_NAME, HYDRATE_MARKER_ATTR, Island, SKIP_SSR_MARKER_ATTR, resolveWhen, } from "./island.js";
|
|
8
|
+
export { scheduleHydrate, mountIslands, mountNewIslands, cancelPendingIslands, unmountIslands, } from "./runtime.js";
|
|
9
|
+
export { DEFAULT_WHEN, isWhen, WHEN_VALUES } from "./types.js";
|
|
10
|
+
// `defaultComponents` (htmlOverrides convention) is re-exported from the
|
|
11
|
+
// root entry point so `import { defaultComponents } from "@takazudo/zfb"` is the
|
|
12
|
+
// canonical access path. Each named override is also re-exported so
|
|
13
|
+
// consumers can tree-shake-import a single one (`import { ContentLink } from "@takazudo/zfb"`)
|
|
14
|
+
// without dragging in the whole map.
|
|
15
|
+
// Plugin lifecycle types + `definePlugin` identity helper. Plugin
|
|
16
|
+
// authors typically import these from "@takazudo/zfb/plugins" but the
|
|
17
|
+
// root entry re-exports them so simple plugins can pull everything from
|
|
18
|
+
// one path.
|
|
19
|
+
export { definePlugin, } from "./plugins.js";
|
|
20
|
+
export { ContentBlockquote, ContentCode, ContentH2, ContentH3, ContentH4, ContentLink, ContentOl, ContentParagraph, ContentStrong, ContentTable, ContentUl, defaultComponents, } from "./content.js";
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,sDAAsD;AACtD,EAAE;AACF,iFAAiF;AACjF,uDAAuD;AACvD,gFAAgF;AAChF,uCAAuC;AAEvC,OAAO,EACL,wBAAwB,EACxB,mBAAmB,EACnB,MAAM,EACN,oBAAoB,EACpB,WAAW,GAGZ,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,eAAe,EACf,YAAY,EACZ,eAAe,EACf,oBAAoB,EACpB,cAAc,GACf,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,WAAW,EAAa,MAAM,YAAY,CAAC;AAE1E,yEAAyE;AACzE,iFAAiF;AACjF,oEAAoE;AACpE,+FAA+F;AAC/F,qCAAqC;AACrC,kEAAkE;AAClE,sEAAsE;AACtE,wEAAwE;AACxE,YAAY;AACZ,OAAO,EACL,YAAY,GAQb,MAAM,cAAc,CAAC;AAEtB,OAAO,EACL,iBAAiB,EACjB,WAAW,EACX,SAAS,EACT,SAAS,EACT,SAAS,EACT,WAAW,EACX,SAAS,EACT,gBAAgB,EAChB,aAAa,EACb,YAAY,EACZ,SAAS,EACT,iBAAiB,GAGlB,MAAM,cAAc,CAAC"}
|
package/dist/island.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { VNode } from "./jsx-types.js";
|
|
2
|
+
import { type When } from "./types.js";
|
|
3
|
+
export { resolveWhen } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Marker attribute the SSR wrapper writes when the child component should
|
|
6
|
+
* be hydrated client-side. The hydration runtime queries
|
|
7
|
+
* `[data-${HYDRATE_MARKER_ATTR}]` to find islands.
|
|
8
|
+
*/
|
|
9
|
+
export declare const HYDRATE_MARKER_ATTR = "data-zfb-island";
|
|
10
|
+
/**
|
|
11
|
+
* Marker attribute the SSR wrapper writes when SSR is being skipped (the
|
|
12
|
+
* client:only-equivalent path). The hydration runtime queries
|
|
13
|
+
* `[data-${SKIP_SSR_MARKER_ATTR}]` to find these placeholders and renders
|
|
14
|
+
* the real component into them on hydration — there is no server output
|
|
15
|
+
* to patch up.
|
|
16
|
+
*/
|
|
17
|
+
export declare const SKIP_SSR_MARKER_ATTR = "data-zfb-island-skip-ssr";
|
|
18
|
+
/** Fallback name surfaced when child identity cannot be determined. */
|
|
19
|
+
export declare const ANONYMOUS_COMPONENT_NAME = "Anonymous";
|
|
20
|
+
/**
|
|
21
|
+
* Attribute the SSR wrapper writes to ferry the wrapped component's props
|
|
22
|
+
* across the SSR → hydrate boundary. The hydration runtime parses this
|
|
23
|
+
* with `JSON.parse` and forwards the result to the per-island `mount()`
|
|
24
|
+
* call so the hydrated component sees the same props the SSR pass did.
|
|
25
|
+
*
|
|
26
|
+
* Omitted entirely when the wrapped child has no own data props (other
|
|
27
|
+
* than `children`) — `readProps` already falls back to `{}` when the
|
|
28
|
+
* attribute is missing, and emitting `data-props=""` would just bloat
|
|
29
|
+
* the SSR markup.
|
|
30
|
+
*/
|
|
31
|
+
export declare const PROPS_DATA_ATTR = "data-props";
|
|
32
|
+
/** Props for `<Island>`. */
|
|
33
|
+
export interface IslandProps {
|
|
34
|
+
/** Hydration scheduling strategy. Defaults to `"load"`. */
|
|
35
|
+
when?: When;
|
|
36
|
+
/**
|
|
37
|
+
* If supplied, switches the island into SSR-skip mode (Astro's
|
|
38
|
+
* `client:only` equivalent). The wrapper emits the
|
|
39
|
+
* `data-zfb-island-skip-ssr` marker, the heavy `children` are **not**
|
|
40
|
+
* evaluated server-side, and `ssrFallback` is rendered in their place.
|
|
41
|
+
* On hydration the client runtime swaps in the real component.
|
|
42
|
+
*/
|
|
43
|
+
ssrFallback?: VNode;
|
|
44
|
+
/**
|
|
45
|
+
* Server-rendered children, hydrated client-side once `when` fires.
|
|
46
|
+
*
|
|
47
|
+
* Typed as `VNode` (structural union) rather than `ReactNode` so
|
|
48
|
+
* non-React frameworks can implement `IslandProps` without a React
|
|
49
|
+
* type dependency (BCI-4).
|
|
50
|
+
*/
|
|
51
|
+
children?: VNode;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Public JSX-element shape returned by [`Island`]. Intentionally widened
|
|
55
|
+
* to a structural type so consumers don't infer through the internal
|
|
56
|
+
* `{ type, props, key }` VNode shape of either Preact or React. Both
|
|
57
|
+
* jsx-runtimes accept this object on either side of the boundary.
|
|
58
|
+
*/
|
|
59
|
+
export type IslandElement = {
|
|
60
|
+
readonly type: string;
|
|
61
|
+
readonly props: Readonly<Record<string, unknown>>;
|
|
62
|
+
readonly key: unknown;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* `<Island>` JSX wrapper.
|
|
66
|
+
*
|
|
67
|
+
* Returns a JSX element shape compatible with both Preact and React. The
|
|
68
|
+
* runtime tag is `"div"`. In the default (hydrate) mode the wrapper emits
|
|
69
|
+
* `data-zfb-island="ComponentName"` and `data-when="<resolved-when>"`. In
|
|
70
|
+
* SSR-skip mode (when `ssrFallback` is provided) it emits
|
|
71
|
+
* `data-zfb-island-skip-ssr="ComponentName"` instead and renders the
|
|
72
|
+
* fallback rather than the heavy child.
|
|
73
|
+
*
|
|
74
|
+
* The component-name string is derived from the child JSX element's type
|
|
75
|
+
* identity (`type.displayName ?? type.name`). For string-typed children
|
|
76
|
+
* (host elements) the tag name is used. If no usable identity can be
|
|
77
|
+
* recovered, [`ANONYMOUS_COMPONENT_NAME`] is used so the marker still
|
|
78
|
+
* lines up with the hydration shim's manifest lookup.
|
|
79
|
+
*
|
|
80
|
+
* The return type is the public [`IslandElement`] shape — the internal
|
|
81
|
+
* VNode structure is deliberately not leaked so consumers never type-infer
|
|
82
|
+
* through it.
|
|
83
|
+
*/
|
|
84
|
+
export declare function Island(props: IslandProps): IslandElement;
|
|
85
|
+
/**
|
|
86
|
+
* Pull a component-name string out of a JSX child.
|
|
87
|
+
*
|
|
88
|
+
* Both Preact and React store rendered VNodes as plain objects whose
|
|
89
|
+
* `.type` field is either:
|
|
90
|
+
* - the component function (look at `displayName ?? name`),
|
|
91
|
+
* - or the host element tag name as a string.
|
|
92
|
+
*
|
|
93
|
+
* If `children` is an array (multiple children), the first child with a
|
|
94
|
+
* usable identity wins. This is intentional: the typical island shape is
|
|
95
|
+
* `<Island><Foo /></Island>` (single child); when the caller wraps a
|
|
96
|
+
* fragment-like list we still want a deterministic, debuggable name.
|
|
97
|
+
*
|
|
98
|
+
* Exported for tests; not re-exported from `index.ts`.
|
|
99
|
+
*/
|
|
100
|
+
export declare function captureComponentName(children: unknown): string;
|
|
101
|
+
/**
|
|
102
|
+
* Serialize the wrapped child's data props as a JSON string the runtime
|
|
103
|
+
* can parse out of the `data-props` attribute on the Island marker div.
|
|
104
|
+
*
|
|
105
|
+
* Mirrors [`captureComponentName`]'s array handling: when `children` is
|
|
106
|
+
* an array (multiple JSX siblings), the first child whose own props
|
|
107
|
+
* yield a non-empty serialization wins. This keeps the "first
|
|
108
|
+
* identifiable child" contract consistent across both attributes —
|
|
109
|
+
* whatever the marker name points at is what the data-props payload
|
|
110
|
+
* describes.
|
|
111
|
+
*
|
|
112
|
+
* Returns `undefined` (not `"{}"` and not `""`) when no usable props
|
|
113
|
+
* exist. The runtime's `readProps` already maps a missing attribute to
|
|
114
|
+
* `{}`, so omitting the attribute keeps the SSR markup smaller and
|
|
115
|
+
* preserves the invariant that the attribute, when present, always
|
|
116
|
+
* parses to a non-empty record.
|
|
117
|
+
*
|
|
118
|
+
* Exported for tests; not re-exported from `index.ts`.
|
|
119
|
+
*/
|
|
120
|
+
export declare function captureSerializableProps(children: unknown): string | undefined;
|
|
121
|
+
//# sourceMappingURL=island.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"island.d.ts","sourceRoot":"","sources":["../src/island.ts"],"names":[],"mappings":"AA0CA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAe,KAAK,IAAI,EAAE,MAAM,YAAY,CAAC;AAMpD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,oBAAoB,CAAC;AAErD;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,6BAA6B,CAAC;AAE/D,uEAAuE;AACvE,eAAO,MAAM,wBAAwB,cAAc,CAAC;AAEpD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,eAAe,eAAe,CAAC;AAE5C,4BAA4B;AAC5B,MAAM,WAAW,WAAW;IAC1B,2DAA2D;IAC3D,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,KAAK,CAAC;IACpB;;;;;;OAMG;IACH,QAAQ,CAAC,EAAE,KAAK,CAAC;CAClB;AAED;;;;;GAKG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAClD,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,WAAW,GAAG,aAAa,CAmCxD;AA2BD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,CAS9D;AAgBD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,wBAAwB,CAAC,QAAQ,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAS9E"}
|
package/dist/island.js
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// Build-time `<Island when="...">` JSX wrapper.
|
|
2
|
+
//
|
|
3
|
+
// The wrapper is intentionally JSX-runtime-agnostic: it does not import
|
|
4
|
+
// preact or react and never calls h() / createElement directly. Instead it
|
|
5
|
+
// returns a plain object with a fixed shape that both Preact's and React's
|
|
6
|
+
// jsx-runtime accept when the JSX transform turns the call site into a
|
|
7
|
+
// jsx(Island, props) invocation.
|
|
8
|
+
//
|
|
9
|
+
// At build time, running through the SSR renderer (embedded V8 host):
|
|
10
|
+
//
|
|
11
|
+
// <Island when="visible"><Counter /></Island>
|
|
12
|
+
//
|
|
13
|
+
// renders as:
|
|
14
|
+
//
|
|
15
|
+
// <div data-zfb-island="Counter" data-when="visible">
|
|
16
|
+
// <Counter />
|
|
17
|
+
// </div>
|
|
18
|
+
//
|
|
19
|
+
// The component-name attribute (`data-zfb-island="ComponentName"`) is filled
|
|
20
|
+
// in *here* by reading the child's JSX type identity (`displayName` first,
|
|
21
|
+
// then `name`). Sub 3's hydration shim then `querySelectorAll`s these
|
|
22
|
+
// markers and looks each one up in the islands manifest produced by the
|
|
23
|
+
// scanner (see `crates/zfb-islands/src/manifest.rs` for the contract).
|
|
24
|
+
//
|
|
25
|
+
// SSR-skip mode: when the caller passes `ssrFallback`, the heavy child is
|
|
26
|
+
// **not** evaluated at SSR time. Instead the wrapper emits a different
|
|
27
|
+
// marker:
|
|
28
|
+
//
|
|
29
|
+
// <Island ssrFallback={<div>Loading…</div>}><HeavyClientOnly /></Island>
|
|
30
|
+
// →
|
|
31
|
+
// <div data-zfb-island-skip-ssr="HeavyClientOnly">
|
|
32
|
+
// <div>Loading…</div>
|
|
33
|
+
// </div>
|
|
34
|
+
//
|
|
35
|
+
// On the client the hydration runtime distinguishes hydrate vs. render by
|
|
36
|
+
// which marker attribute is present. This is the equivalent of Astro's
|
|
37
|
+
// `client:only="preact"`.
|
|
38
|
+
//
|
|
39
|
+
// When validation: in development we console.warn for unknown `when`
|
|
40
|
+
// values and fall back to the default. In production we silently fall
|
|
41
|
+
// back to keep the bundle path small.
|
|
42
|
+
import { resolveWhen } from "./types.js";
|
|
43
|
+
// Re-export `resolveWhen` for back-compat: tests and downstream consumers
|
|
44
|
+
// historically imported it from `./island.js`. The implementation lives in
|
|
45
|
+
// `./types.js` so the runtime scheduler can pull it in without dragging
|
|
46
|
+
// the JSX wrapper along for the ride.
|
|
47
|
+
export { resolveWhen } from "./types.js";
|
|
48
|
+
/**
|
|
49
|
+
* Marker attribute the SSR wrapper writes when the child component should
|
|
50
|
+
* be hydrated client-side. The hydration runtime queries
|
|
51
|
+
* `[data-${HYDRATE_MARKER_ATTR}]` to find islands.
|
|
52
|
+
*/
|
|
53
|
+
export const HYDRATE_MARKER_ATTR = "data-zfb-island";
|
|
54
|
+
/**
|
|
55
|
+
* Marker attribute the SSR wrapper writes when SSR is being skipped (the
|
|
56
|
+
* client:only-equivalent path). The hydration runtime queries
|
|
57
|
+
* `[data-${SKIP_SSR_MARKER_ATTR}]` to find these placeholders and renders
|
|
58
|
+
* the real component into them on hydration — there is no server output
|
|
59
|
+
* to patch up.
|
|
60
|
+
*/
|
|
61
|
+
export const SKIP_SSR_MARKER_ATTR = "data-zfb-island-skip-ssr";
|
|
62
|
+
/** Fallback name surfaced when child identity cannot be determined. */
|
|
63
|
+
export const ANONYMOUS_COMPONENT_NAME = "Anonymous";
|
|
64
|
+
/**
|
|
65
|
+
* Attribute the SSR wrapper writes to ferry the wrapped component's props
|
|
66
|
+
* across the SSR → hydrate boundary. The hydration runtime parses this
|
|
67
|
+
* with `JSON.parse` and forwards the result to the per-island `mount()`
|
|
68
|
+
* call so the hydrated component sees the same props the SSR pass did.
|
|
69
|
+
*
|
|
70
|
+
* Omitted entirely when the wrapped child has no own data props (other
|
|
71
|
+
* than `children`) — `readProps` already falls back to `{}` when the
|
|
72
|
+
* attribute is missing, and emitting `data-props=""` would just bloat
|
|
73
|
+
* the SSR markup.
|
|
74
|
+
*/
|
|
75
|
+
export const PROPS_DATA_ATTR = "data-props";
|
|
76
|
+
/**
|
|
77
|
+
* `<Island>` JSX wrapper.
|
|
78
|
+
*
|
|
79
|
+
* Returns a JSX element shape compatible with both Preact and React. The
|
|
80
|
+
* runtime tag is `"div"`. In the default (hydrate) mode the wrapper emits
|
|
81
|
+
* `data-zfb-island="ComponentName"` and `data-when="<resolved-when>"`. In
|
|
82
|
+
* SSR-skip mode (when `ssrFallback` is provided) it emits
|
|
83
|
+
* `data-zfb-island-skip-ssr="ComponentName"` instead and renders the
|
|
84
|
+
* fallback rather than the heavy child.
|
|
85
|
+
*
|
|
86
|
+
* The component-name string is derived from the child JSX element's type
|
|
87
|
+
* identity (`type.displayName ?? type.name`). For string-typed children
|
|
88
|
+
* (host elements) the tag name is used. If no usable identity can be
|
|
89
|
+
* recovered, [`ANONYMOUS_COMPONENT_NAME`] is used so the marker still
|
|
90
|
+
* lines up with the hydration shim's manifest lookup.
|
|
91
|
+
*
|
|
92
|
+
* The return type is the public [`IslandElement`] shape — the internal
|
|
93
|
+
* VNode structure is deliberately not leaked so consumers never type-infer
|
|
94
|
+
* through it.
|
|
95
|
+
*/
|
|
96
|
+
export function Island(props) {
|
|
97
|
+
const when = resolveWhen(props.when);
|
|
98
|
+
const componentName = captureComponentName(props.children);
|
|
99
|
+
const isSkipSsr = props.ssrFallback !== undefined;
|
|
100
|
+
// Always source props from `props.children` (the heavy component VNode),
|
|
101
|
+
// never from `ssrFallback` — the fallback is just SSR placeholder markup;
|
|
102
|
+
// the hydrated component is the child, so its props are what `mount()`
|
|
103
|
+
// needs at hydrate time. (Same rationale as `captureComponentName`.)
|
|
104
|
+
const dataProps = captureSerializableProps(props.children);
|
|
105
|
+
if (isSkipSsr) {
|
|
106
|
+
const skipSsrProps = {
|
|
107
|
+
[SKIP_SSR_MARKER_ATTR]: componentName,
|
|
108
|
+
"data-when": when,
|
|
109
|
+
children: props.ssrFallback ?? null,
|
|
110
|
+
};
|
|
111
|
+
if (dataProps !== undefined)
|
|
112
|
+
skipSsrProps[PROPS_DATA_ATTR] = dataProps;
|
|
113
|
+
return makeVNode({
|
|
114
|
+
type: "div",
|
|
115
|
+
props: skipSsrProps,
|
|
116
|
+
key: null,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
const hydrateProps = {
|
|
120
|
+
[HYDRATE_MARKER_ATTR]: componentName,
|
|
121
|
+
"data-when": when,
|
|
122
|
+
children: props.children,
|
|
123
|
+
};
|
|
124
|
+
if (dataProps !== undefined)
|
|
125
|
+
hydrateProps[PROPS_DATA_ATTR] = dataProps;
|
|
126
|
+
return makeVNode({
|
|
127
|
+
type: "div",
|
|
128
|
+
props: hydrateProps,
|
|
129
|
+
key: null,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Construct the structural JSX-element shape with `constructor` set to
|
|
134
|
+
* `undefined`.
|
|
135
|
+
*
|
|
136
|
+
* Why: `preact-render-to-string` (and Preact's diff path) recognise a
|
|
137
|
+
* VNode by checking `vnode.constructor === undefined`. A plain object
|
|
138
|
+
* literal `{ type, props, key }` inherits `Object` for `.constructor`,
|
|
139
|
+
* so the renderer treats it as foreign data and emits no markup —
|
|
140
|
+
* which would silently strip every `<Island>` from SSG output. Marking
|
|
141
|
+
* `constructor: undefined` is the documented sentinel that both
|
|
142
|
+
* Preact's and React's renderers treat as a structural VNode.
|
|
143
|
+
*
|
|
144
|
+
* The cast back to `IslandElement` keeps the public type tight; the
|
|
145
|
+
* extra runtime field is JS-only and is invisible to type consumers.
|
|
146
|
+
*/
|
|
147
|
+
function makeVNode(shape) {
|
|
148
|
+
const v = shape;
|
|
149
|
+
v.constructor = undefined;
|
|
150
|
+
return shape;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Pull a component-name string out of a JSX child.
|
|
154
|
+
*
|
|
155
|
+
* Both Preact and React store rendered VNodes as plain objects whose
|
|
156
|
+
* `.type` field is either:
|
|
157
|
+
* - the component function (look at `displayName ?? name`),
|
|
158
|
+
* - or the host element tag name as a string.
|
|
159
|
+
*
|
|
160
|
+
* If `children` is an array (multiple children), the first child with a
|
|
161
|
+
* usable identity wins. This is intentional: the typical island shape is
|
|
162
|
+
* `<Island><Foo /></Island>` (single child); when the caller wraps a
|
|
163
|
+
* fragment-like list we still want a deterministic, debuggable name.
|
|
164
|
+
*
|
|
165
|
+
* Exported for tests; not re-exported from `index.ts`.
|
|
166
|
+
*/
|
|
167
|
+
export function captureComponentName(children) {
|
|
168
|
+
if (Array.isArray(children)) {
|
|
169
|
+
for (const child of children) {
|
|
170
|
+
const name = nameFromSingle(child);
|
|
171
|
+
if (name)
|
|
172
|
+
return name;
|
|
173
|
+
}
|
|
174
|
+
return ANONYMOUS_COMPONENT_NAME;
|
|
175
|
+
}
|
|
176
|
+
return nameFromSingle(children) || ANONYMOUS_COMPONENT_NAME;
|
|
177
|
+
}
|
|
178
|
+
function nameFromSingle(child) {
|
|
179
|
+
if (!child || typeof child !== "object")
|
|
180
|
+
return "";
|
|
181
|
+
const c = child;
|
|
182
|
+
const t = c.type;
|
|
183
|
+
if (typeof t === "function") {
|
|
184
|
+
const fn = t;
|
|
185
|
+
if (typeof fn.displayName === "string" && fn.displayName)
|
|
186
|
+
return fn.displayName;
|
|
187
|
+
if (typeof fn.name === "string" && fn.name)
|
|
188
|
+
return fn.name;
|
|
189
|
+
return "";
|
|
190
|
+
}
|
|
191
|
+
if (typeof t === "string" && t)
|
|
192
|
+
return t;
|
|
193
|
+
return "";
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Serialize the wrapped child's data props as a JSON string the runtime
|
|
197
|
+
* can parse out of the `data-props` attribute on the Island marker div.
|
|
198
|
+
*
|
|
199
|
+
* Mirrors [`captureComponentName`]'s array handling: when `children` is
|
|
200
|
+
* an array (multiple JSX siblings), the first child whose own props
|
|
201
|
+
* yield a non-empty serialization wins. This keeps the "first
|
|
202
|
+
* identifiable child" contract consistent across both attributes —
|
|
203
|
+
* whatever the marker name points at is what the data-props payload
|
|
204
|
+
* describes.
|
|
205
|
+
*
|
|
206
|
+
* Returns `undefined` (not `"{}"` and not `""`) when no usable props
|
|
207
|
+
* exist. The runtime's `readProps` already maps a missing attribute to
|
|
208
|
+
* `{}`, so omitting the attribute keeps the SSR markup smaller and
|
|
209
|
+
* preserves the invariant that the attribute, when present, always
|
|
210
|
+
* parses to a non-empty record.
|
|
211
|
+
*
|
|
212
|
+
* Exported for tests; not re-exported from `index.ts`.
|
|
213
|
+
*/
|
|
214
|
+
export function captureSerializableProps(children) {
|
|
215
|
+
if (Array.isArray(children)) {
|
|
216
|
+
for (const child of children) {
|
|
217
|
+
const json = propsFromSingle(child);
|
|
218
|
+
if (json !== undefined)
|
|
219
|
+
return json;
|
|
220
|
+
}
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
return propsFromSingle(children);
|
|
224
|
+
}
|
|
225
|
+
function propsFromSingle(child) {
|
|
226
|
+
if (!child || typeof child !== "object")
|
|
227
|
+
return undefined;
|
|
228
|
+
const c = child;
|
|
229
|
+
const raw = c.props;
|
|
230
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
231
|
+
return undefined;
|
|
232
|
+
// Exclude `children` from the serialized payload: the SSR pass already
|
|
233
|
+
// emitted the rendered children into the DOM (the hydration target),
|
|
234
|
+
// and JSX child nodes are typically VNode trees with non-serializable
|
|
235
|
+
// shapes (functions, circular refs) that would either bloat the
|
|
236
|
+
// payload or throw inside JSON.stringify. The hydration runtime
|
|
237
|
+
// re-renders into the existing DOM, so it does not need the JSX
|
|
238
|
+
// children replayed through props.
|
|
239
|
+
//
|
|
240
|
+
// Note: JSON.stringify already silently drops function / symbol /
|
|
241
|
+
// undefined values from the output, so those don't need explicit
|
|
242
|
+
// pre-filtering here.
|
|
243
|
+
const propsRecord = raw;
|
|
244
|
+
let hasOwn = false;
|
|
245
|
+
const filtered = {};
|
|
246
|
+
for (const key of Object.keys(propsRecord)) {
|
|
247
|
+
if (key === "children")
|
|
248
|
+
continue;
|
|
249
|
+
filtered[key] = propsRecord[key];
|
|
250
|
+
hasOwn = true;
|
|
251
|
+
}
|
|
252
|
+
if (!hasOwn)
|
|
253
|
+
return undefined;
|
|
254
|
+
let json;
|
|
255
|
+
try {
|
|
256
|
+
json = JSON.stringify(filtered);
|
|
257
|
+
}
|
|
258
|
+
catch {
|
|
259
|
+
// Circular references, BigInt, or any other non-serializable input
|
|
260
|
+
// — silently fall through (no `data-props`) rather than ship a
|
|
261
|
+
// partial payload. The runtime already handles the missing-attribute
|
|
262
|
+
// case by returning `{}` from `readProps`.
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
// JSON.stringify can also return `undefined` (when the top-level value
|
|
266
|
+
// serializes to nothing) or `"{}"` (when every key was a function /
|
|
267
|
+
// symbol / undefined and got dropped). Treat both as "nothing useful
|
|
268
|
+
// to ship" so the marker stays clean.
|
|
269
|
+
if (json === undefined || json === "{}")
|
|
270
|
+
return undefined;
|
|
271
|
+
return json;
|
|
272
|
+
}
|
|
273
|
+
//# sourceMappingURL=island.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"island.js","sourceRoot":"","sources":["../src/island.ts"],"names":[],"mappings":"AAAA,gDAAgD;AAChD,EAAE;AACF,wEAAwE;AACxE,2EAA2E;AAC3E,2EAA2E;AAC3E,uEAAuE;AACvE,iCAAiC;AACjC,EAAE;AACF,sEAAsE;AACtE,EAAE;AACF,gDAAgD;AAChD,EAAE;AACF,cAAc;AACd,EAAE;AACF,wDAAwD;AACxD,kBAAkB;AAClB,WAAW;AACX,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,sEAAsE;AACtE,wEAAwE;AACxE,uEAAuE;AACvE,EAAE;AACF,0EAA0E;AAC1E,uEAAuE;AACvE,UAAU;AACV,EAAE;AACF,2EAA2E;AAC3E,MAAM;AACN,qDAAqD;AACrD,0BAA0B;AAC1B,WAAW;AACX,EAAE;AACF,0EAA0E;AAC1E,uEAAuE;AACvE,0BAA0B;AAC1B,EAAE;AACF,qEAAqE;AACrE,sEAAsE;AACtE,sCAAsC;AAGtC,OAAO,EAAE,WAAW,EAAa,MAAM,YAAY,CAAC;AAEpD,0EAA0E;AAC1E,2EAA2E;AAC3E,wEAAwE;AACxE,sCAAsC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC;;;;GAIG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,iBAAiB,CAAC;AAErD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,0BAA0B,CAAC;AAE/D,uEAAuE;AACvE,MAAM,CAAC,MAAM,wBAAwB,GAAG,WAAW,CAAC;A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,CAAC;QACH,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;IAClC,CAAC;IAAC,MAAM,CAAC;QACP,mEAAmE;QACnE,+DAA+D;QAC/D,qEAAqE;QACrE,2CAA2C;QAC3C,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,uEAAuE;IACvE,oEAAoE;IACpE,qEAAqE;IACrE,sCAAsC;IACtC,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IAC1D,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural JSX element (VNode). Framework-agnostic: both Preact and React
|
|
3
|
+
* produce objects that satisfy this shape when a JSX element is rendered.
|
|
4
|
+
*
|
|
5
|
+
* The `props` bag may itself contain `children` (nested VNodes) as well as
|
|
6
|
+
* HTML attribute values (strings, numbers, booleans). All fields are widened
|
|
7
|
+
* to `unknown` so the type stays opaque to callers that don't know which
|
|
8
|
+
* framework produced the value.
|
|
9
|
+
*
|
|
10
|
+
* `type` accepts BOTH function components (call signature) AND class
|
|
11
|
+
* components (construct signature). Preact and React both expose
|
|
12
|
+
* `ComponentType = FunctionComponent | ComponentClass`, and the JSX runtime
|
|
13
|
+
* stores either on `.type`. Without the construct signature, valid Preact
|
|
14
|
+
* `<MyComponent />` expressions fail to assign to `VNode` at the framework
|
|
15
|
+
* boundary (e.g. inside `<Island>` children) — even though class components
|
|
16
|
+
* are rare in modern Preact, the `ComponentType` union includes them.
|
|
17
|
+
*/
|
|
18
|
+
export type VNodeObject = {
|
|
19
|
+
readonly type: string | ((...args: unknown[]) => unknown) | (new (...args: unknown[]) => unknown);
|
|
20
|
+
readonly props: Readonly<Record<string, unknown>>;
|
|
21
|
+
readonly key: unknown;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* A renderable JSX node: a primitive, `null`, `undefined`, an array, or a
|
|
25
|
+
* structured VNode object.
|
|
26
|
+
*
|
|
27
|
+
* This union covers every value both Preact and React treat as valid
|
|
28
|
+
* `children`. Importantly it does **not** reference any framework-specific
|
|
29
|
+
* type, so packages that depend on `zfb` need not install a React / Preact
|
|
30
|
+
* `@types` package to satisfy this type.
|
|
31
|
+
*/
|
|
32
|
+
export type VNode = string | number | boolean | null | undefined | bigint | VNodeArray | VNodeObject;
|
|
33
|
+
export interface VNodeArray extends ReadonlyArray<VNode> {
|
|
34
|
+
}
|
|
35
|
+
/** @deprecated Use `VNode` instead. */
|
|
36
|
+
export type ReactNode = VNode;
|
|
37
|
+
//# sourceMappingURL=jsx-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsx-types.d.ts","sourceRoot":"","sources":["../src/jsx-types.ts"],"names":[],"mappings":"AAWA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,KAAK,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC;IAClG,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IAClD,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,MAAM,KAAK,GACb,MAAM,GACN,MAAM,GACN,OAAO,GACP,IAAI,GACJ,SAAS,GACT,MAAM,GACN,UAAU,GACV,WAAW,CAAC;AAEhB,MAAM,WAAW,UAAW,SAAQ,aAAa,CAAC,KAAK,CAAC;CAAG;AAK3D,uCAAuC;AACvC,MAAM,MAAM,SAAS,GAAG,KAAK,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Lightweight ambient JSX types so the public Island wrapper does not
|
|
2
|
+
// hard-depend on either preact or react as a runtime/types dependency.
|
|
3
|
+
//
|
|
4
|
+
// BCI-4: `IslandProps.children` is now typed as `VNode` (a structural union)
|
|
5
|
+
// rather than the old `ReactNode` alias, so non-React frameworks can
|
|
6
|
+
// implement `IslandProps` without any React type dependency at the type level.
|
|
7
|
+
//
|
|
8
|
+
// Both Preact and React's `children` accept this structural union; the build
|
|
9
|
+
// is type-erased so the actual rendering happens in the user's app under
|
|
10
|
+
// whichever framework adapter they chose.
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=jsx-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsx-types.js","sourceRoot":"","sources":["../src/jsx-types.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,uEAAuE;AACvE,EAAE;AACF,6EAA6E;AAC7E,qEAAqE;AACrE,+EAA+E;AAC/E,EAAE;AACF,6EAA6E;AAC7E,yEAAyE;AACzE,0CAA0C"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Options accepted by [`paginate`]. */
|
|
2
|
+
export type PaginateOptions<K extends string = string> = {
|
|
3
|
+
/** Items per page. Must be a positive integer (>= 1). */
|
|
4
|
+
pageSize: number;
|
|
5
|
+
/** Name of the dynamic param to fill (e.g. `"page"` for `[page].tsx`). */
|
|
6
|
+
param: K;
|
|
7
|
+
};
|
|
8
|
+
/** One page of paginated results. The shape consumed by the page component. */
|
|
9
|
+
export type PaginatedPage<T> = {
|
|
10
|
+
/** Items belonging to this page. */
|
|
11
|
+
data: T[];
|
|
12
|
+
/** 1-based page number. */
|
|
13
|
+
page: number;
|
|
14
|
+
/** Total number of pages (>= 1). */
|
|
15
|
+
lastPage: number;
|
|
16
|
+
/** Items per page (echoed for convenience). */
|
|
17
|
+
pageSize: number;
|
|
18
|
+
/** Total item count across all pages. */
|
|
19
|
+
total: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Route entry returned by [`paginate`], one per page.
|
|
23
|
+
*
|
|
24
|
+
* The `K` generic carries the literal param name through, so consumers
|
|
25
|
+
* accessing e.g. `route.params.page` get `string` rather than
|
|
26
|
+
* `string | undefined` under `noUncheckedIndexedAccess`.
|
|
27
|
+
*/
|
|
28
|
+
export type PaginateRoute<T, K extends string = string> = {
|
|
29
|
+
params: Record<K, string>;
|
|
30
|
+
props: {
|
|
31
|
+
page: PaginatedPage<T>;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Slice `items` into pages of `opts.pageSize` and produce one route entry
|
|
36
|
+
* per page. The `param` option names the dynamic segment to fill — for
|
|
37
|
+
* `pages/blog/page/[page].tsx` pass `param: "page"`.
|
|
38
|
+
*
|
|
39
|
+
* Always emits at least one route. An empty input list yields a single
|
|
40
|
+
* empty page so the index route still renders rather than 404ing.
|
|
41
|
+
*/
|
|
42
|
+
export declare function paginate<T, K extends string = string>(items: readonly T[], opts: PaginateOptions<K>): PaginateRoute<T, K>[];
|
|
43
|
+
//# sourceMappingURL=paginate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paginate.d.ts","sourceRoot":"","sources":["../src/paginate.ts"],"names":[],"mappings":"AAOA,wCAAwC;AACxC,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI;IACvD,yDAAyD;IACzD,QAAQ,EAAE,MAAM,CAAC;IACjB,0EAA0E;IAC1E,KAAK,EAAE,CAAC,CAAC;CACV,CAAC;AAEF,+EAA+E;AAC/E,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;IAC7B,oCAAoC;IACpC,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,2BAA2B;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,oCAAoC;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,+CAA+C;IAC/C,QAAQ,EAAE,MAAM,CAAC;IACjB,yCAAyC;IACzC,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI;IACxD,MAAM,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IAC1B,KAAK,EAAE;QAAE,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC,CAAA;KAAE,CAAC;CACnC,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,EACnD,KAAK,EAAE,SAAS,CAAC,EAAE,EACnB,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC,GACvB,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAgCvB"}
|
package/dist/paginate.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// `zfb/paginate` — paginate an array of items into per-page route entries.
|
|
2
|
+
//
|
|
3
|
+
// Mirrors the shape of Astro's `paginate()` so the bundled basic-blog template can
|
|
4
|
+
// emit one route per page from a `paths()` export. The renderer treats
|
|
5
|
+
// each returned object as `{ params, props }`, the same convention used
|
|
6
|
+
// elsewhere by the routing engine.
|
|
7
|
+
/**
|
|
8
|
+
* Slice `items` into pages of `opts.pageSize` and produce one route entry
|
|
9
|
+
* per page. The `param` option names the dynamic segment to fill — for
|
|
10
|
+
* `pages/blog/page/[page].tsx` pass `param: "page"`.
|
|
11
|
+
*
|
|
12
|
+
* Always emits at least one route. An empty input list yields a single
|
|
13
|
+
* empty page so the index route still renders rather than 404ing.
|
|
14
|
+
*/
|
|
15
|
+
export function paginate(items, opts) {
|
|
16
|
+
if (!Number.isInteger(opts.pageSize) || opts.pageSize < 1) {
|
|
17
|
+
throw new RangeError(`paginate: pageSize must be an integer >= 1, got ${String(opts.pageSize)}`);
|
|
18
|
+
}
|
|
19
|
+
if (typeof opts.param !== "string" || opts.param.length === 0) {
|
|
20
|
+
throw new TypeError(`paginate: param must be a non-empty string, got ${JSON.stringify(opts.param)}`);
|
|
21
|
+
}
|
|
22
|
+
const pageSize = opts.pageSize;
|
|
23
|
+
const total = items.length;
|
|
24
|
+
const lastPage = Math.max(1, Math.ceil(total / pageSize));
|
|
25
|
+
const routes = [];
|
|
26
|
+
for (let page = 1; page <= lastPage; page++) {
|
|
27
|
+
const start = (page - 1) * pageSize;
|
|
28
|
+
const slice = items.slice(start, start + pageSize);
|
|
29
|
+
routes.push({
|
|
30
|
+
params: { [opts.param]: String(page) },
|
|
31
|
+
props: {
|
|
32
|
+
page: {
|
|
33
|
+
data: slice,
|
|
34
|
+
page,
|
|
35
|
+
lastPage,
|
|
36
|
+
pageSize,
|
|
37
|
+
total,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return routes;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=paginate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paginate.js","sourceRoot":"","sources":["../src/paginate.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,EAAE;AACF,mFAAmF;AACnF,uEAAuE;AACvE,wEAAwE;AACxE,mCAAmC;AAoCnC;;;;;;;GAOG;AACH,MAAM,UAAU,QAAQ,CACtB,KAAmB,EACnB,IAAwB;IAExB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,IAAI,CAAC,QAAQ,GAAG,CAAC,EAAE,CAAC;QAC1D,MAAM,IAAI,UAAU,CAClB,mDAAmD,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAC3E,CAAC;IACJ,CAAC;IACD,IAAI,OAAO,IAAI,CAAC,KAAK,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9D,MAAM,IAAI,SAAS,CACjB,mDAAmD,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,EA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