@csszyx/runtime 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,258 @@
1
+ # @csszyx/runtime
2
+
3
+ Runtime package for csszyx - provides zero-allocation className helpers, recovery token verification, and hydration guards.
4
+
5
+ ## Features
6
+
7
+ - **Zero-Allocation Helpers**: Efficient className concatenation with `_sz()` and variants
8
+ - **Token Verification**: Runtime validation of recovery tokens
9
+ - **Hydration Guards**: SSR/CSR consistency with abort protocol
10
+ - **Type Safety**: Full TypeScript support with strict types
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ pnpm add @csszyx/runtime
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ### Basic Concatenation
21
+
22
+ ```typescript
23
+ import { _sz } from "@csszyx/runtime";
24
+
25
+ // Basic usage
26
+ const className = _sz("a", "b", "c");
27
+ // Returns: "a b c"
28
+
29
+ // With conditionals
30
+ const className = _sz("base", isActive && "active", hasError && "error");
31
+ // Returns: "base active" (if isActive is true, hasError is false)
32
+
33
+ // Filters out falsy values
34
+ const className = _sz("a", null, "b", undefined, false, "c");
35
+ // Returns: "a b c"
36
+ ```
37
+
38
+ ### Conditional Helpers
39
+
40
+ ```typescript
41
+ import { _szIf, _szSwitch } from "@csszyx/runtime";
42
+
43
+ // Simple conditional
44
+ _szIf(isActive, "active"); // "active" or false
45
+
46
+ // With fallback
47
+ _szIf(isActive, "active", "inactive"); // "active" or "inactive"
48
+
49
+ // Switch-like
50
+ _szSwitch(
51
+ [
52
+ [status === "success", "text-green-500"],
53
+ [status === "error", "text-red-500"],
54
+ [status === "warning", "text-yellow-500"],
55
+ ],
56
+ "text-gray-500",
57
+ );
58
+ ```
59
+
60
+ ### Merging Classes
61
+
62
+ ```typescript
63
+ import { _szMerge } from "@csszyx/runtime";
64
+
65
+ // Merge with duplicate removal
66
+ _szMerge("a b", "b c", "c d");
67
+ // Returns: "a b c d"
68
+ ```
69
+
70
+ ### Runtime Initialization
71
+
72
+ ```typescript
73
+ import { initRuntime } from "@csszyx/runtime";
74
+
75
+ // Initialize at app startup
76
+ initRuntime({
77
+ development: process.env.NODE_ENV === "development",
78
+ allowCSRRecovery: true,
79
+ strictHydration: true,
80
+ debug: false,
81
+ });
82
+ ```
83
+
84
+ ### Recovery Token Verification
85
+
86
+ ```typescript
87
+ import { verifyRecoveryToken, loadManifestFromDOM } from "@csszyx/runtime";
88
+
89
+ // Load manifest from embedded script
90
+ const manifest = loadManifestFromDOM();
91
+
92
+ if (manifest) {
93
+ const element = document.querySelector("[data-sz-recovery-token]");
94
+ const result = verifyRecoveryToken(element, manifest);
95
+
96
+ if (result.valid) {
97
+ console.log("Token verified:", result.tokenData);
98
+ } else {
99
+ console.error("Invalid token:", result.error);
100
+ }
101
+ }
102
+ ```
103
+
104
+ ### Hydration Guard
105
+
106
+ ```typescript
107
+ import {
108
+ guardHydration,
109
+ loadManifestFromDOM,
110
+ enableCSRRecovery,
111
+ } from "@csszyx/runtime";
112
+
113
+ // Enable CSR recovery in development
114
+ if (process.env.NODE_ENV === "development") {
115
+ enableCSRRecovery();
116
+ }
117
+
118
+ // Guard hydration process
119
+ const manifest = loadManifestFromDOM();
120
+ if (manifest && !guardHydration(manifest)) {
121
+ console.error("Hydration guard failed - mangle map mismatch");
122
+ }
123
+ ```
124
+
125
+ ## API Reference
126
+
127
+ ### Concatenation Helpers
128
+
129
+ #### `_sz(...classes): string`
130
+
131
+ Zero-allocation className concatenation. Filters out falsy values.
132
+
133
+ #### `_sz2(a, b): string`
134
+
135
+ Optimized two-argument version.
136
+
137
+ #### `_sz3(a, b, c): string`
138
+
139
+ Optimized three-argument version.
140
+
141
+ #### `_szIf(condition, className, fallback?): string | false`
142
+
143
+ Conditional className application.
144
+
145
+ #### `_szSwitch(conditions, default?): string`
146
+
147
+ Switch-like className selection.
148
+
149
+ #### `_szMerge(...classes): string`
150
+
151
+ Merge className strings with duplicate removal.
152
+
153
+ ### Verification
154
+
155
+ #### `verifyRecoveryToken(element, manifest): VerificationResult`
156
+
157
+ Verifies a recovery token against the manifest.
158
+
159
+ #### `loadManifestFromDOM(): RecoveryManifest | null`
160
+
161
+ Loads the recovery manifest from the DOM.
162
+
163
+ #### `hasRecoveryToken(element): boolean`
164
+
165
+ Checks if an element has a recovery token.
166
+
167
+ #### `getRecoveryMode(element): RecoveryMode | null`
168
+
169
+ Gets the recovery mode from an element.
170
+
171
+ #### `verifyAllTokens(root, manifest): VerificationResult[]`
172
+
173
+ Verifies all tokens in a subtree.
174
+
175
+ ### Hydration
176
+
177
+ #### `guardHydration(manifest): boolean`
178
+
179
+ Guards the hydration process by verifying mangle map integrity.
180
+
181
+ #### `loadMangleMapFromDOM(): MangleMap | null`
182
+
183
+ Loads the mangle map from the DOM.
184
+
185
+ #### `verifyMangleChecksum(expectedChecksum): boolean`
186
+
187
+ Verifies the mangle map checksum.
188
+
189
+ #### `abortHydration(element, error): void`
190
+
191
+ Executes the hydration abort protocol for a subtree.
192
+
193
+ #### `isHydrationAborted(element): boolean`
194
+
195
+ Checks if a subtree has been aborted.
196
+
197
+ #### `attemptCSRRecovery(element): boolean`
198
+
199
+ Attempts client-side recovery for an aborted subtree.
200
+
201
+ #### `enableCSRRecovery(): void`
202
+
203
+ Enables CSR recovery mode (development only).
204
+
205
+ #### `disableCSRRecovery(): void`
206
+
207
+ Disables CSR recovery mode.
208
+
209
+ #### `isCSRRecoveryAllowed(): boolean`
210
+
211
+ Checks if CSR recovery is allowed.
212
+
213
+ #### `getHydrationErrors(): HydrationError[]`
214
+
215
+ Gets all hydration errors.
216
+
217
+ #### `getAbortedSubtreeCount(): number`
218
+
219
+ Gets the count of aborted subtrees.
220
+
221
+ ### Runtime Initialization
222
+
223
+ #### `initRuntime(config?): void`
224
+
225
+ Initializes the csszyx runtime with optional configuration.
226
+
227
+ #### `getRuntimeConfig(): RuntimeConfig`
228
+
229
+ Gets the current runtime configuration.
230
+
231
+ #### `isRuntimeInitialized(): boolean`
232
+
233
+ Checks if the runtime has been initialized.
234
+
235
+ ## Configuration Options
236
+
237
+ ```typescript
238
+ interface RuntimeConfig {
239
+ development?: boolean; // Enable development mode features
240
+ allowCSRRecovery?: boolean; // Allow CSR recovery in development
241
+ strictHydration?: boolean; // Enable strict hydration checks
242
+ debug?: boolean; // Enable debug logging
243
+ }
244
+ ```
245
+
246
+ ## Performance
247
+
248
+ The `_sz()` family of functions are optimized for zero-allocation concatenation:
249
+
250
+ - `_sz()` - Variadic version for any number of arguments
251
+ - `_sz2()` - 10-15% faster for exactly 2 arguments
252
+ - `_sz3()` - 10-15% faster for exactly 3 arguments
253
+
254
+ Use the optimized versions in hot paths when the argument count is known at compile time.
255
+
256
+ ## License
257
+
258
+ MIT