@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 +258 -0
- package/dist/index.cjs +526 -0
- package/dist/index.d.cts +695 -0
- package/dist/index.d.ts +695 -0
- package/dist/index.js +463 -0
- package/dist/lite.cjs +75 -0
- package/dist/lite.d.cts +64 -0
- package/dist/lite.d.ts +64 -0
- package/dist/lite.js +47 -0
- package/package.json +64 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,695 @@
|
|
|
1
|
+
import { SzObject } from '@csszyx/compiler';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Zero-allocation className concatenation helper.
|
|
5
|
+
*
|
|
6
|
+
* This module provides the runtime helper for composing dynamic className
|
|
7
|
+
* strings with minimal overhead. The _sz() function is designed for
|
|
8
|
+
* performance-critical scenarios where className composition happens
|
|
9
|
+
* frequently.
|
|
10
|
+
*
|
|
11
|
+
* @module @csszyx/runtime/concatenate
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type for sz input - can be a pre-compiled string or SzObject.
|
|
16
|
+
*/
|
|
17
|
+
type SzInput = string | SzObject | null | undefined | false;
|
|
18
|
+
/**
|
|
19
|
+
* Zero-overhead className passthrough/concatenation helper.
|
|
20
|
+
*
|
|
21
|
+
* When the compiler pre-transforms sz objects to strings at build time,
|
|
22
|
+
* this function simply passes through the string (zero overhead).
|
|
23
|
+
* For runtime usage, it can also concatenate multiple class strings
|
|
24
|
+
* or transform SzObjects on-the-fly.
|
|
25
|
+
*
|
|
26
|
+
* @param {...SzInput[]} classes - Class names or SzObjects to concatenate
|
|
27
|
+
* @returns {string} Combined className string
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* // Passthrough (from compiler) - zero overhead
|
|
32
|
+
* _sz('p-4 bg-red-500')
|
|
33
|
+
* // Returns: "p-4 bg-red-500"
|
|
34
|
+
*
|
|
35
|
+
* // With conditionals
|
|
36
|
+
* _sz('base', isActive && 'active', error && 'error')
|
|
37
|
+
* // Returns: "base active" (if isActive is true, error is false)
|
|
38
|
+
*
|
|
39
|
+
* // With SzObject (runtime transform)
|
|
40
|
+
* _sz({ p: 4, bg: 'red-500' })
|
|
41
|
+
* // Returns: "p-4 bg-red-500"
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
declare function _sz(...classes: SzInput[]): string;
|
|
45
|
+
/**
|
|
46
|
+
* Conditionally applies className based on a condition.
|
|
47
|
+
*
|
|
48
|
+
* Supports both pre-compiled strings and SzObjects for dynamic styling.
|
|
49
|
+
* This is the recommended helper for conditional class application.
|
|
50
|
+
*
|
|
51
|
+
* @param {boolean} condition - Whether to apply the truthy value
|
|
52
|
+
* @param {SzInput} truthyValue - ClassName or SzObject when condition is true
|
|
53
|
+
* @param {SzInput} falsyValue - ClassName or SzObject when condition is false
|
|
54
|
+
* @returns {string} The resolved className string
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // With strings (pre-compiled)
|
|
59
|
+
* _szIf(isActive, 'bg-green-500', 'bg-gray-500')
|
|
60
|
+
* // Returns: "bg-green-500" if isActive, "bg-gray-500" otherwise
|
|
61
|
+
*
|
|
62
|
+
* // With SzObjects (runtime transform)
|
|
63
|
+
* _szIf(isActive, { bg: 'green-500' }, { bg: 'gray-500' })
|
|
64
|
+
* // Returns: "bg-green-500" if isActive, "bg-gray-500" otherwise
|
|
65
|
+
*
|
|
66
|
+
* // Without fallback
|
|
67
|
+
* _szIf(isActive, { bg: 'green-500' })
|
|
68
|
+
* // Returns: "bg-green-500" if isActive, "" otherwise
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function _szIf(condition: boolean, truthyValue: SzInput, falsyValue?: SzInput): string;
|
|
72
|
+
/**
|
|
73
|
+
* Applies className based on multiple conditions (switch-like).
|
|
74
|
+
*
|
|
75
|
+
* Returns the className for the first truthy condition, or the default.
|
|
76
|
+
* Supports both strings and SzObjects.
|
|
77
|
+
*
|
|
78
|
+
* @param {Array<[boolean, SzInput]>} conditions - Array of [condition, value] tuples
|
|
79
|
+
* @param {SzInput} defaultValue - Default value if no conditions match
|
|
80
|
+
* @returns {string} The matched className or default
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* _szSwitch([
|
|
85
|
+
* [status === 'success', { text: 'green-500' }],
|
|
86
|
+
* [status === 'error', { text: 'red-500' }],
|
|
87
|
+
* [status === 'warning', { text: 'yellow-500' }]
|
|
88
|
+
* ], { text: 'gray-500' })
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
declare function _szSwitch(conditions: Array<[boolean, SzInput]>, defaultValue?: SzInput): string;
|
|
92
|
+
/**
|
|
93
|
+
* Merges className strings, removing duplicates.
|
|
94
|
+
*
|
|
95
|
+
* Useful when combining multiple className sources that may overlap.
|
|
96
|
+
*
|
|
97
|
+
* @param {...SzInput[]} classes - Class names or SzObjects to merge
|
|
98
|
+
* @returns {string} Merged className string with duplicates removed
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* _szMerge('a b c', 'b c d', 'c d e')
|
|
103
|
+
* // Returns: "a b c d e"
|
|
104
|
+
*
|
|
105
|
+
* _szMerge({ p: 4 }, { p: 2, m: 4 })
|
|
106
|
+
* // Returns: "p-4 p-2 m-4" (duplicates from different calls are kept)
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
declare function _szMerge(...classes: SzInput[]): string;
|
|
110
|
+
/**
|
|
111
|
+
* Performance-optimized variant of _sz() for exactly 2 arguments.
|
|
112
|
+
*
|
|
113
|
+
* Faster than the variadic version when the number of classes is known
|
|
114
|
+
* at compile time. Use for hot paths.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} a - First className
|
|
117
|
+
* @param {string} b - Second className
|
|
118
|
+
* @returns {string} Combined className string
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* _sz2('a', 'b')
|
|
123
|
+
* // Returns: "a b"
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
declare function _sz2(a: string, b: string): string;
|
|
127
|
+
/**
|
|
128
|
+
* Performance-optimized variant for exactly 3 arguments.
|
|
129
|
+
*
|
|
130
|
+
* @param {string} a - First className
|
|
131
|
+
* @param {string} b - Second className
|
|
132
|
+
* @param {string} c - Third className
|
|
133
|
+
* @returns {string} Combined className string
|
|
134
|
+
*/
|
|
135
|
+
declare function _sz3(a: string, b: string, c: string): string;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Recovery Token Verification - Runtime validation of recovery tokens.
|
|
139
|
+
*
|
|
140
|
+
* This module provides runtime verification of recovery tokens against
|
|
141
|
+
* the recovery manifest embedded in the build output. It ensures that
|
|
142
|
+
* szRecover directives are legitimate and haven't been tampered with.
|
|
143
|
+
*/
|
|
144
|
+
/**
|
|
145
|
+
* Recovery mode types.
|
|
146
|
+
*/
|
|
147
|
+
type RecoveryMode = 'csr' | 'dev-only';
|
|
148
|
+
/**
|
|
149
|
+
* Token data from the manifest.
|
|
150
|
+
*/
|
|
151
|
+
interface TokenData {
|
|
152
|
+
mode: RecoveryMode;
|
|
153
|
+
component: string;
|
|
154
|
+
path: string;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Recovery manifest structure.
|
|
158
|
+
*/
|
|
159
|
+
interface RecoveryManifest {
|
|
160
|
+
buildId: string;
|
|
161
|
+
checksum: string;
|
|
162
|
+
tokens: Record<string, TokenData>;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Token verification result.
|
|
166
|
+
*/
|
|
167
|
+
interface VerificationResult {
|
|
168
|
+
/**
|
|
169
|
+
* Whether the token is valid
|
|
170
|
+
*/
|
|
171
|
+
valid: boolean;
|
|
172
|
+
/**
|
|
173
|
+
* Token data if valid
|
|
174
|
+
*/
|
|
175
|
+
tokenData?: TokenData;
|
|
176
|
+
/**
|
|
177
|
+
* Error message if invalid
|
|
178
|
+
*/
|
|
179
|
+
error?: string;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Verifies a recovery token against the manifest.
|
|
183
|
+
*
|
|
184
|
+
* Checks that the token exists in the manifest and returns its metadata.
|
|
185
|
+
* This is used at runtime to validate szRecover directives.
|
|
186
|
+
*
|
|
187
|
+
* @param {HTMLElement} element - The DOM element with the recovery token
|
|
188
|
+
* @param {RecoveryManifest} manifest - The recovery manifest
|
|
189
|
+
* @returns {VerificationResult} Verification result
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```typescript
|
|
193
|
+
* const element = document.querySelector('[data-sz-recovery-token]');
|
|
194
|
+
* const result = verifyRecoveryToken(element, manifest);
|
|
195
|
+
*
|
|
196
|
+
* if (result.valid) {
|
|
197
|
+
* console.log('Token verified:', result.tokenData);
|
|
198
|
+
* } else {
|
|
199
|
+
* console.error('Invalid token:', result.error);
|
|
200
|
+
* }
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
declare function verifyRecoveryToken(element: HTMLElement, manifest: RecoveryManifest): VerificationResult;
|
|
204
|
+
/**
|
|
205
|
+
* Loads the recovery manifest from the DOM.
|
|
206
|
+
*
|
|
207
|
+
* Searches for the embedded manifest script tag and parses it.
|
|
208
|
+
*
|
|
209
|
+
* @returns {RecoveryManifest | null} The manifest or null if not found
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```typescript
|
|
213
|
+
* const manifest = loadManifestFromDOM();
|
|
214
|
+
* if (manifest) {
|
|
215
|
+
* console.log('Manifest loaded:', manifest.buildId);
|
|
216
|
+
* }
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
declare function loadManifestFromDOM(): RecoveryManifest | null;
|
|
220
|
+
/**
|
|
221
|
+
* Validates manifest structure.
|
|
222
|
+
*
|
|
223
|
+
* @param {unknown} manifest - Manifest to validate
|
|
224
|
+
* @returns {boolean} True if valid
|
|
225
|
+
*/
|
|
226
|
+
declare function isValidManifest(manifest: unknown): manifest is RecoveryManifest;
|
|
227
|
+
/**
|
|
228
|
+
* Verifies all elements with recovery tokens in a subtree.
|
|
229
|
+
*
|
|
230
|
+
* Scans the given root element and all its descendants for recovery tokens
|
|
231
|
+
* and verifies them against the manifest.
|
|
232
|
+
*
|
|
233
|
+
* @param {HTMLElement} root - Root element to scan
|
|
234
|
+
* @param {RecoveryManifest} manifest - Recovery manifest
|
|
235
|
+
* @returns {VerificationResult[]} Array of verification results
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```typescript
|
|
239
|
+
* const results = verifyAllTokens(document.body, manifest);
|
|
240
|
+
* const invalid = results.filter(r => !r.valid);
|
|
241
|
+
* if (invalid.length > 0) {
|
|
242
|
+
* console.error('Found invalid tokens:', invalid);
|
|
243
|
+
* }
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
declare function verifyAllTokens(root: HTMLElement, manifest: RecoveryManifest): VerificationResult[];
|
|
247
|
+
/**
|
|
248
|
+
* Checks if an element has a valid recovery token.
|
|
249
|
+
*
|
|
250
|
+
* Quick check to see if an element has a recovery token without
|
|
251
|
+
* full verification.
|
|
252
|
+
*
|
|
253
|
+
* @param {HTMLElement} element - Element to check
|
|
254
|
+
* @returns {boolean} True if element has recovery token
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* if (hasRecoveryToken(element)) {
|
|
259
|
+
* // Element has szRecover directive
|
|
260
|
+
* }
|
|
261
|
+
* ```
|
|
262
|
+
*/
|
|
263
|
+
declare function hasRecoveryToken(element: HTMLElement): boolean;
|
|
264
|
+
/**
|
|
265
|
+
* Gets the recovery mode from an element.
|
|
266
|
+
*
|
|
267
|
+
* @param {HTMLElement} element - Element with recovery token
|
|
268
|
+
* @returns {RecoveryMode | null} The recovery mode or null
|
|
269
|
+
*
|
|
270
|
+
* @example
|
|
271
|
+
* ```typescript
|
|
272
|
+
* const mode = getRecoveryMode(element);
|
|
273
|
+
* if (mode === 'csr') {
|
|
274
|
+
* // Allow client-side recovery
|
|
275
|
+
* }
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
declare function getRecoveryMode(element: HTMLElement): RecoveryMode | null;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Hydration Guard and Abort Protocol.
|
|
282
|
+
*
|
|
283
|
+
* This module implements the hydration safety mechanism that ensures
|
|
284
|
+
* SSR/CSR consistency. It detects mangle map mismatches and executes
|
|
285
|
+
* the abort protocol when necessary to preserve SSR invariants.
|
|
286
|
+
*/
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Window augmentation for csszyx global state.
|
|
290
|
+
*/
|
|
291
|
+
declare global {
|
|
292
|
+
/**
|
|
293
|
+
* Extended Window interface for csszyx runtime state.
|
|
294
|
+
*/
|
|
295
|
+
interface Window {
|
|
296
|
+
/** Flag to allow CSR recovery in development */
|
|
297
|
+
__SZ_ALLOW_CSR_RECOVERY__?: boolean;
|
|
298
|
+
/** Flag indicating if the app is currently hydrating */
|
|
299
|
+
__SZ_HYDRATING__?: boolean;
|
|
300
|
+
/** Next.js data (for framework detection) */
|
|
301
|
+
__NEXT_DATA__?: unknown;
|
|
302
|
+
/** Remix context (for framework detection) */
|
|
303
|
+
__REMIX_CONTEXT__?: unknown;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Mangle map structure loaded from the DOM.
|
|
308
|
+
*/
|
|
309
|
+
interface MangleMap {
|
|
310
|
+
[originalClass: string]: string;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Hydration error types.
|
|
314
|
+
*/
|
|
315
|
+
type HydrationErrorType = 'checksum_mismatch' | 'map_missing' | 'invalid_token';
|
|
316
|
+
/**
|
|
317
|
+
* Hydration error details.
|
|
318
|
+
*/
|
|
319
|
+
interface HydrationError {
|
|
320
|
+
type: HydrationErrorType;
|
|
321
|
+
message: string;
|
|
322
|
+
element?: HTMLElement;
|
|
323
|
+
timestamp: number;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Enables CSR recovery mode (development only).
|
|
327
|
+
*
|
|
328
|
+
* Sets the global flag to allow client-side recovery when
|
|
329
|
+
* hydration mismatches occur. Should only be used in development.
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* if (process.env.NODE_ENV === 'development') {
|
|
334
|
+
* enableCSRRecovery();
|
|
335
|
+
* }
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
declare function enableCSRRecovery(): void;
|
|
339
|
+
/**
|
|
340
|
+
* Disables CSR recovery mode.
|
|
341
|
+
*/
|
|
342
|
+
declare function disableCSRRecovery(): void;
|
|
343
|
+
/**
|
|
344
|
+
* Checks if CSR recovery is allowed.
|
|
345
|
+
*
|
|
346
|
+
* @returns {boolean} True if recovery is allowed
|
|
347
|
+
*/
|
|
348
|
+
declare function isCSRRecoveryAllowed(): boolean;
|
|
349
|
+
/**
|
|
350
|
+
* Loads the mangle map from the DOM.
|
|
351
|
+
*
|
|
352
|
+
* Searches for the embedded mangle map script tag and parses it.
|
|
353
|
+
*
|
|
354
|
+
* @returns {MangleMap | null} The mangle map or null if not found
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```typescript
|
|
358
|
+
* const mangleMap = loadMangleMapFromDOM();
|
|
359
|
+
* if (mangleMap) {
|
|
360
|
+
* console.log('Mangle map loaded');
|
|
361
|
+
* }
|
|
362
|
+
* ```
|
|
363
|
+
*/
|
|
364
|
+
declare function loadMangleMapFromDOM(): MangleMap | null;
|
|
365
|
+
/**
|
|
366
|
+
* Verifies mangle map checksum from HTML tag.
|
|
367
|
+
*
|
|
368
|
+
* Compares the checksum in the data-sz-checksum attribute with the
|
|
369
|
+
* expected checksum to detect mangle map mismatches.
|
|
370
|
+
*
|
|
371
|
+
* @param {string} expectedChecksum - Expected checksum
|
|
372
|
+
* @returns {boolean} True if checksums match
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```typescript
|
|
376
|
+
* if (!verifyMangleChecksum(manifest.checksum)) {
|
|
377
|
+
* console.error('Checksum mismatch detected');
|
|
378
|
+
* }
|
|
379
|
+
* ```
|
|
380
|
+
*/
|
|
381
|
+
declare function verifyMangleChecksum(expectedChecksum: string): boolean;
|
|
382
|
+
/**
|
|
383
|
+
* Verifies mangle map integrity using Rust core verification.
|
|
384
|
+
*
|
|
385
|
+
* Loads the mangle map from DOM and verifies its checksum using the
|
|
386
|
+
* Rust core's `verify_mangle_checksum()` function for cryptographic-grade verification.
|
|
387
|
+
*
|
|
388
|
+
* @returns {boolean} True if mangle map integrity is verified
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* ```typescript
|
|
392
|
+
* if (!verifyMangleMapIntegrity()) {
|
|
393
|
+
* console.error('[csszyx] Mangle map integrity check failed');
|
|
394
|
+
* abortHydration();
|
|
395
|
+
* }
|
|
396
|
+
* ```
|
|
397
|
+
*/
|
|
398
|
+
declare function verifyMangleMapIntegrity(): boolean;
|
|
399
|
+
/**
|
|
400
|
+
* Executes the hydration abort protocol for a subtree.
|
|
401
|
+
*
|
|
402
|
+
* Implements the 5-step abort protocol:
|
|
403
|
+
* 1. Terminate hydration at subtree root
|
|
404
|
+
* 2. Preserve static HTML/CSS from SSR
|
|
405
|
+
* 3. Log audit message
|
|
406
|
+
* 4. Inject abort attribute
|
|
407
|
+
* 5. Block event handlers
|
|
408
|
+
*
|
|
409
|
+
* @param {HTMLElement} element - Root of the subtree to abort
|
|
410
|
+
* @param {HydrationError} error - The hydration error that triggered abort
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```typescript
|
|
414
|
+
* abortHydration(element, {
|
|
415
|
+
* type: 'checksum_mismatch',
|
|
416
|
+
* message: 'Mangle checksum mismatch',
|
|
417
|
+
* timestamp: Date.now()
|
|
418
|
+
* });
|
|
419
|
+
* ```
|
|
420
|
+
*/
|
|
421
|
+
declare function abortHydration(element: HTMLElement, error: HydrationError): void;
|
|
422
|
+
/**
|
|
423
|
+
* Checks if a subtree has been aborted.
|
|
424
|
+
*
|
|
425
|
+
* @param {HTMLElement} element - Element to check
|
|
426
|
+
* @returns {boolean} True if hydration was aborted
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```typescript
|
|
430
|
+
* if (isHydrationAborted(element)) {
|
|
431
|
+
* // Skip hydration for this subtree
|
|
432
|
+
* }
|
|
433
|
+
* ```
|
|
434
|
+
*/
|
|
435
|
+
declare function isHydrationAborted(element: HTMLElement): boolean;
|
|
436
|
+
/**
|
|
437
|
+
* Guards hydration process by verifying mangle map integrity.
|
|
438
|
+
*
|
|
439
|
+
* Main entry point for hydration guarding. Checks the mangle map
|
|
440
|
+
* checksum and executes the abort protocol if there's a mismatch.
|
|
441
|
+
*
|
|
442
|
+
* @param {RecoveryManifest} manifest - Recovery manifest
|
|
443
|
+
* @returns {boolean} True if hydration can proceed safely
|
|
444
|
+
*
|
|
445
|
+
* @example
|
|
446
|
+
* ```typescript
|
|
447
|
+
* const manifest = loadManifestFromDOM();
|
|
448
|
+
* if (manifest && !guardHydration(manifest)) {
|
|
449
|
+
* console.error('Hydration guard failed');
|
|
450
|
+
* }
|
|
451
|
+
* ```
|
|
452
|
+
*/
|
|
453
|
+
declare function guardHydration(manifest: RecoveryManifest): boolean;
|
|
454
|
+
/**
|
|
455
|
+
* Attempts client-side recovery for an aborted subtree.
|
|
456
|
+
*
|
|
457
|
+
* Only allowed in development mode when CSR recovery is enabled.
|
|
458
|
+
* Logs a warning to remind developer to fix the root cause.
|
|
459
|
+
*
|
|
460
|
+
* @param {HTMLElement} element - Element to recover
|
|
461
|
+
* @returns {boolean} True if recovery was attempted
|
|
462
|
+
*
|
|
463
|
+
* @example
|
|
464
|
+
* ```typescript
|
|
465
|
+
* if (isCSRRecoveryAllowed() && isHydrationAborted(element)) {
|
|
466
|
+
* attemptCSRRecovery(element);
|
|
467
|
+
* }
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
470
|
+
declare function attemptCSRRecovery(element: HTMLElement): boolean;
|
|
471
|
+
/**
|
|
472
|
+
* Gets all hydration errors.
|
|
473
|
+
*
|
|
474
|
+
* @returns {HydrationError[]} Array of errors
|
|
475
|
+
*
|
|
476
|
+
* @example
|
|
477
|
+
* ```typescript
|
|
478
|
+
* const errors = getHydrationErrors();
|
|
479
|
+
* console.log(`Found ${errors.length} hydration errors`);
|
|
480
|
+
* ```
|
|
481
|
+
*/
|
|
482
|
+
declare function getHydrationErrors(): HydrationError[];
|
|
483
|
+
/**
|
|
484
|
+
* Clears hydration errors (for testing).
|
|
485
|
+
*/
|
|
486
|
+
declare function clearHydrationErrors(): void;
|
|
487
|
+
/**
|
|
488
|
+
* Gets the count of aborted subtrees.
|
|
489
|
+
*
|
|
490
|
+
* @returns {number} Number of aborted subtrees
|
|
491
|
+
*/
|
|
492
|
+
declare function getAbortedSubtreeCount(): number;
|
|
493
|
+
/**
|
|
494
|
+
* SSR context structure embedded in the HTML.
|
|
495
|
+
*/
|
|
496
|
+
interface SSRContext {
|
|
497
|
+
/**
|
|
498
|
+
* Build ID for cache invalidation.
|
|
499
|
+
*/
|
|
500
|
+
buildId: string;
|
|
501
|
+
/**
|
|
502
|
+
* Checksum of the mangle map.
|
|
503
|
+
*/
|
|
504
|
+
checksum: string;
|
|
505
|
+
/**
|
|
506
|
+
* Timestamp when the page was rendered.
|
|
507
|
+
*/
|
|
508
|
+
timestamp: number;
|
|
509
|
+
/**
|
|
510
|
+
* Whether recovery tokens are present.
|
|
511
|
+
*/
|
|
512
|
+
hasRecoveryTokens: boolean;
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Checks if the current environment is SSR (server-side).
|
|
516
|
+
*
|
|
517
|
+
* @returns {boolean} True if running on server
|
|
518
|
+
*
|
|
519
|
+
* @example
|
|
520
|
+
* ```typescript
|
|
521
|
+
* if (isSSREnvironment()) {
|
|
522
|
+
* // Server-side logic
|
|
523
|
+
* } else {
|
|
524
|
+
* // Client-side logic
|
|
525
|
+
* }
|
|
526
|
+
* ```
|
|
527
|
+
*/
|
|
528
|
+
declare function isSSREnvironment(): boolean;
|
|
529
|
+
/**
|
|
530
|
+
* Checks if the application is currently hydrating.
|
|
531
|
+
*
|
|
532
|
+
* Reads the hydration state from the window object set by the framework.
|
|
533
|
+
*
|
|
534
|
+
* @returns {boolean} True if hydrating
|
|
535
|
+
*
|
|
536
|
+
* @example
|
|
537
|
+
* ```typescript
|
|
538
|
+
* if (isHydrating()) {
|
|
539
|
+
* // Use SSR-generated classes
|
|
540
|
+
* } else {
|
|
541
|
+
* // Safe to use dynamic classes
|
|
542
|
+
* }
|
|
543
|
+
* ```
|
|
544
|
+
*/
|
|
545
|
+
declare function isHydrating(): boolean;
|
|
546
|
+
/**
|
|
547
|
+
* Gets the SSR context from the DOM.
|
|
548
|
+
*
|
|
549
|
+
* Reads the context embedded by the server during SSR to ensure
|
|
550
|
+
* consistent class generation between server and client.
|
|
551
|
+
*
|
|
552
|
+
* @returns {SSRContext | null} The SSR context or null if not found
|
|
553
|
+
*
|
|
554
|
+
* @example
|
|
555
|
+
* ```typescript
|
|
556
|
+
* const context = getSSRContext();
|
|
557
|
+
* if (context) {
|
|
558
|
+
* console.log('Build ID:', context.buildId);
|
|
559
|
+
* console.log('Checksum:', context.checksum);
|
|
560
|
+
* }
|
|
561
|
+
* ```
|
|
562
|
+
*/
|
|
563
|
+
declare function getSSRContext(): SSRContext | null;
|
|
564
|
+
/**
|
|
565
|
+
* Validates that client-side class generation matches SSR.
|
|
566
|
+
*
|
|
567
|
+
* Call this during hydration to ensure consistency between server
|
|
568
|
+
* and client rendered classes.
|
|
569
|
+
*
|
|
570
|
+
* @param {string} className - The className generated on the client
|
|
571
|
+
* @param {string} expectedClassName - The className from SSR
|
|
572
|
+
* @returns {boolean} True if classes match
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* ```typescript
|
|
576
|
+
* const serverClass = element.className;
|
|
577
|
+
* const clientClass = _sz({ p: 4, bg: 'red-500' });
|
|
578
|
+
*
|
|
579
|
+
* if (!validateHydrationClass(clientClass, serverClass)) {
|
|
580
|
+
* console.warn('Hydration mismatch detected');
|
|
581
|
+
* }
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
declare function validateHydrationClass(className: string, expectedClassName: string): boolean;
|
|
585
|
+
/**
|
|
586
|
+
* Marks the start of hydration.
|
|
587
|
+
*
|
|
588
|
+
* Call this when your framework begins hydration to enable
|
|
589
|
+
* hydration-safe mode in the runtime.
|
|
590
|
+
*
|
|
591
|
+
* @example
|
|
592
|
+
* ```typescript
|
|
593
|
+
* // In your app entry point
|
|
594
|
+
* startHydration();
|
|
595
|
+
* hydrateRoot(document.getElementById('root'), <App />);
|
|
596
|
+
* ```
|
|
597
|
+
*/
|
|
598
|
+
declare function startHydration(): void;
|
|
599
|
+
/**
|
|
600
|
+
* Marks the end of hydration.
|
|
601
|
+
*
|
|
602
|
+
* Call this when hydration is complete to allow dynamic class
|
|
603
|
+
* generation without SSR constraints.
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```typescript
|
|
607
|
+
* // After hydration completes
|
|
608
|
+
* endHydration();
|
|
609
|
+
* ```
|
|
610
|
+
*/
|
|
611
|
+
declare function endHydration(): void;
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* @csszyx/runtime - Runtime package for csszyx.
|
|
615
|
+
*
|
|
616
|
+
* This package provides runtime functionality for csszyx, including:
|
|
617
|
+
* - Zero-allocation className concatenation helpers
|
|
618
|
+
* - Recovery token verification
|
|
619
|
+
* - Hydration guard and abort protocol
|
|
620
|
+
*
|
|
621
|
+
* @module @csszyx/runtime
|
|
622
|
+
*/
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Runtime version.
|
|
626
|
+
*/
|
|
627
|
+
declare const VERSION = "0.0.0";
|
|
628
|
+
/**
|
|
629
|
+
* Runtime configuration options.
|
|
630
|
+
*/
|
|
631
|
+
interface RuntimeConfig {
|
|
632
|
+
/**
|
|
633
|
+
* Enable development mode features
|
|
634
|
+
*/
|
|
635
|
+
development?: boolean;
|
|
636
|
+
/**
|
|
637
|
+
* Allow CSR recovery in development
|
|
638
|
+
*/
|
|
639
|
+
allowCSRRecovery?: boolean;
|
|
640
|
+
/**
|
|
641
|
+
* Enable strict hydration checks
|
|
642
|
+
*/
|
|
643
|
+
strictHydration?: boolean;
|
|
644
|
+
/**
|
|
645
|
+
* Enable debug logging
|
|
646
|
+
*/
|
|
647
|
+
debug?: boolean;
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Default runtime configuration.
|
|
651
|
+
*/
|
|
652
|
+
declare const DEFAULT_RUNTIME_CONFIG: Required<RuntimeConfig>;
|
|
653
|
+
/**
|
|
654
|
+
* Initializes the csszyx runtime.
|
|
655
|
+
*
|
|
656
|
+
* Should be called once at application startup to configure the runtime
|
|
657
|
+
* behavior and set up hydration guards.
|
|
658
|
+
*
|
|
659
|
+
* @param {Partial<RuntimeConfig>} config - Runtime configuration
|
|
660
|
+
*
|
|
661
|
+
* @example
|
|
662
|
+
* ```typescript
|
|
663
|
+
* // In your app entry point
|
|
664
|
+
* initRuntime({
|
|
665
|
+
* development: process.env.NODE_ENV === 'development',
|
|
666
|
+
* allowCSRRecovery: true,
|
|
667
|
+
* debug: true
|
|
668
|
+
* });
|
|
669
|
+
* ```
|
|
670
|
+
*/
|
|
671
|
+
declare function initRuntime(config?: Partial<RuntimeConfig>): void;
|
|
672
|
+
/**
|
|
673
|
+
* Gets the current runtime configuration.
|
|
674
|
+
*
|
|
675
|
+
* @returns {Required<RuntimeConfig>} Current runtime config
|
|
676
|
+
*
|
|
677
|
+
* @example
|
|
678
|
+
* ```typescript
|
|
679
|
+
* const config = getRuntimeConfig();
|
|
680
|
+
* console.log('Debug mode:', config.debug);
|
|
681
|
+
* ```
|
|
682
|
+
*/
|
|
683
|
+
declare function getRuntimeConfig(): Required<RuntimeConfig>;
|
|
684
|
+
/**
|
|
685
|
+
* Checks if the runtime has been initialized.
|
|
686
|
+
*
|
|
687
|
+
* @returns {boolean} True if initialized
|
|
688
|
+
*/
|
|
689
|
+
declare function isRuntimeInitialized(): boolean;
|
|
690
|
+
/**
|
|
691
|
+
* Resets the runtime state (for testing).
|
|
692
|
+
*/
|
|
693
|
+
declare function resetRuntime(): void;
|
|
694
|
+
|
|
695
|
+
export { DEFAULT_RUNTIME_CONFIG, type HydrationError, type HydrationErrorType, type MangleMap, type RecoveryManifest, type RecoveryMode, type RuntimeConfig, type SSRContext, type SzInput, type TokenData, VERSION, type VerificationResult, _sz, _sz2, _sz3, _szIf, _szMerge, _szSwitch, abortHydration, attemptCSRRecovery, clearHydrationErrors, disableCSRRecovery, enableCSRRecovery, endHydration, getAbortedSubtreeCount, getHydrationErrors, getRecoveryMode, getRuntimeConfig, getSSRContext, guardHydration, hasRecoveryToken, initRuntime, isCSRRecoveryAllowed, isHydrating, isHydrationAborted, isRuntimeInitialized, isSSREnvironment, isValidManifest, loadMangleMapFromDOM, loadManifestFromDOM, resetRuntime, startHydration, validateHydrationClass, verifyAllTokens, verifyMangleChecksum, verifyMangleMapIntegrity, verifyRecoveryToken };
|