hadars 0.1.17 → 0.1.19

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.
@@ -1,140 +1,760 @@
1
1
  // src/ssr-render-worker.ts
2
2
  import { workerData, parentPort } from "node:worker_threads";
3
- import { createRequire } from "node:module";
4
- import pathMod from "node:path";
5
3
  import { pathToFileURL } from "node:url";
4
+
5
+ // src/utils/segmentCache.ts
6
+ function getStore() {
7
+ const g = globalThis;
8
+ if (!g.__hadarsSegmentStore) {
9
+ g.__hadarsSegmentStore = /* @__PURE__ */ new Map();
10
+ }
11
+ return g.__hadarsSegmentStore;
12
+ }
13
+ function setSegment(key, html, ttl) {
14
+ getStore().set(key, {
15
+ html,
16
+ expiresAt: ttl != null ? Date.now() + ttl : null
17
+ });
18
+ }
19
+ function processSegmentCache(html) {
20
+ let prev;
21
+ do {
22
+ prev = html;
23
+ html = html.replace(
24
+ /<hadars-c([^>]*)>([\s\S]*?)<\/hadars-c>/g,
25
+ (match, attrs, content) => {
26
+ const cacheM = /data-cache="([^"]+)"/.exec(attrs);
27
+ const keyM = /data-key="([^"]+)"/.exec(attrs);
28
+ const ttlM = /data-ttl="(\d+)"/.exec(attrs);
29
+ if (!cacheM || !keyM)
30
+ return match;
31
+ if (cacheM[1] === "miss") {
32
+ setSegment(keyM[1], content, ttlM ? Number(ttlM[1]) : void 0);
33
+ return content;
34
+ }
35
+ if (cacheM[1] === "hit")
36
+ return content;
37
+ return match;
38
+ }
39
+ );
40
+ } while (html !== prev);
41
+ return html;
42
+ }
43
+
44
+ // src/slim-react/types.ts
45
+ var SLIM_ELEMENT = Symbol.for("react.element");
46
+ var REACT19_ELEMENT = Symbol.for("react.transitional.element");
47
+ var FRAGMENT_TYPE = Symbol.for("react.fragment");
48
+ var SUSPENSE_TYPE = Symbol.for("react.suspense");
49
+
50
+ // src/slim-react/jsx.ts
51
+ var Fragment = FRAGMENT_TYPE;
52
+ function createElement(type, props, ...children) {
53
+ const normalizedProps = { ...props || {} };
54
+ if (children.length === 1) {
55
+ normalizedProps.children = children[0];
56
+ } else if (children.length > 1) {
57
+ normalizedProps.children = children;
58
+ }
59
+ const key = normalizedProps.key ?? null;
60
+ delete normalizedProps.key;
61
+ return {
62
+ $$typeof: SLIM_ELEMENT,
63
+ type,
64
+ props: normalizedProps,
65
+ key
66
+ };
67
+ }
68
+
69
+ // src/slim-react/renderContext.ts
70
+ var GLOBAL_KEY = "__slimReactRenderState";
71
+ var EMPTY = { id: 0, overflow: "", bits: 0 };
72
+ function s() {
73
+ const g = globalThis;
74
+ if (!g[GLOBAL_KEY]) {
75
+ g[GLOBAL_KEY] = { currentTreeContext: { ...EMPTY }, localIdCounter: 0, idPrefix: "" };
76
+ }
77
+ return g[GLOBAL_KEY];
78
+ }
79
+ function resetRenderState() {
80
+ const st = s();
81
+ st.currentTreeContext = { ...EMPTY };
82
+ st.localIdCounter = 0;
83
+ }
84
+ function pushTreeContext(totalChildren, index) {
85
+ const st = s();
86
+ const saved = { ...st.currentTreeContext };
87
+ const pendingBits = 32 - Math.clz32(totalChildren);
88
+ const slot = index + 1;
89
+ const totalBits = st.currentTreeContext.bits + pendingBits;
90
+ if (totalBits <= 30) {
91
+ st.currentTreeContext = {
92
+ id: st.currentTreeContext.id << pendingBits | slot,
93
+ overflow: st.currentTreeContext.overflow,
94
+ bits: totalBits
95
+ };
96
+ } else {
97
+ let newOverflow = st.currentTreeContext.overflow;
98
+ if (st.currentTreeContext.bits > 0)
99
+ newOverflow += st.currentTreeContext.id.toString(32);
100
+ st.currentTreeContext = { id: 1 << pendingBits | slot, overflow: newOverflow, bits: pendingBits };
101
+ }
102
+ return saved;
103
+ }
104
+ function popTreeContext(saved) {
105
+ s().currentTreeContext = saved;
106
+ }
107
+ function pushComponentScope() {
108
+ const st = s();
109
+ const saved = st.localIdCounter;
110
+ st.localIdCounter = 0;
111
+ return saved;
112
+ }
113
+ function popComponentScope(saved) {
114
+ s().localIdCounter = saved;
115
+ }
116
+ function snapshotContext() {
117
+ const st = s();
118
+ return { tree: { ...st.currentTreeContext }, localId: st.localIdCounter };
119
+ }
120
+ function restoreContext(snap) {
121
+ const st = s();
122
+ st.currentTreeContext = { ...snap.tree };
123
+ st.localIdCounter = snap.localId;
124
+ }
125
+
126
+ // src/slim-react/render.ts
127
+ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
128
+ "area",
129
+ "base",
130
+ "br",
131
+ "col",
132
+ "embed",
133
+ "hr",
134
+ "img",
135
+ "input",
136
+ "link",
137
+ "meta",
138
+ "param",
139
+ "source",
140
+ "track",
141
+ "wbr"
142
+ ]);
143
+ function escapeHtml(str) {
144
+ return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/'/g, "&#x27;");
145
+ }
146
+ function escapeAttr(str) {
147
+ return str.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
148
+ }
149
+ function styleObjectToString(style) {
150
+ return Object.entries(style).map(([key, value]) => {
151
+ const cssKey = key.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase());
152
+ return `${cssKey}:${value}`;
153
+ }).join(";");
154
+ }
155
+ var SVG_ATTR_MAP = {
156
+ // Presentation / geometry
157
+ accentHeight: "accent-height",
158
+ alignmentBaseline: "alignment-baseline",
159
+ arabicForm: "arabic-form",
160
+ baselineShift: "baseline-shift",
161
+ capHeight: "cap-height",
162
+ clipPath: "clip-path",
163
+ clipRule: "clip-rule",
164
+ colorInterpolation: "color-interpolation",
165
+ colorInterpolationFilters: "color-interpolation-filters",
166
+ colorProfile: "color-profile",
167
+ dominantBaseline: "dominant-baseline",
168
+ enableBackground: "enable-background",
169
+ fillOpacity: "fill-opacity",
170
+ fillRule: "fill-rule",
171
+ floodColor: "flood-color",
172
+ floodOpacity: "flood-opacity",
173
+ fontFamily: "font-family",
174
+ fontSize: "font-size",
175
+ fontSizeAdjust: "font-size-adjust",
176
+ fontStretch: "font-stretch",
177
+ fontStyle: "font-style",
178
+ fontVariant: "font-variant",
179
+ fontWeight: "font-weight",
180
+ glyphName: "glyph-name",
181
+ glyphOrientationHorizontal: "glyph-orientation-horizontal",
182
+ glyphOrientationVertical: "glyph-orientation-vertical",
183
+ horizAdvX: "horiz-adv-x",
184
+ horizOriginX: "horiz-origin-x",
185
+ imageRendering: "image-rendering",
186
+ letterSpacing: "letter-spacing",
187
+ lightingColor: "lighting-color",
188
+ markerEnd: "marker-end",
189
+ markerMid: "marker-mid",
190
+ markerStart: "marker-start",
191
+ overlinePosition: "overline-position",
192
+ overlineThickness: "overline-thickness",
193
+ paintOrder: "paint-order",
194
+ panose1: "panose-1",
195
+ pointerEvents: "pointer-events",
196
+ renderingIntent: "rendering-intent",
197
+ shapeRendering: "shape-rendering",
198
+ stopColor: "stop-color",
199
+ stopOpacity: "stop-opacity",
200
+ strikethroughPosition: "strikethrough-position",
201
+ strikethroughThickness: "strikethrough-thickness",
202
+ strokeDasharray: "stroke-dasharray",
203
+ strokeDashoffset: "stroke-dashoffset",
204
+ strokeLinecap: "stroke-linecap",
205
+ strokeLinejoin: "stroke-linejoin",
206
+ strokeMiterlimit: "stroke-miterlimit",
207
+ strokeOpacity: "stroke-opacity",
208
+ strokeWidth: "stroke-width",
209
+ textAnchor: "text-anchor",
210
+ textDecoration: "text-decoration",
211
+ textRendering: "text-rendering",
212
+ underlinePosition: "underline-position",
213
+ underlineThickness: "underline-thickness",
214
+ unicodeBidi: "unicode-bidi",
215
+ unicodeRange: "unicode-range",
216
+ unitsPerEm: "units-per-em",
217
+ vAlphabetic: "v-alphabetic",
218
+ vHanging: "v-hanging",
219
+ vIdeographic: "v-ideographic",
220
+ vMathematical: "v-mathematical",
221
+ vertAdvY: "vert-adv-y",
222
+ vertOriginX: "vert-origin-x",
223
+ vertOriginY: "vert-origin-y",
224
+ wordSpacing: "word-spacing",
225
+ writingMode: "writing-mode",
226
+ xHeight: "x-height",
227
+ // Namespace-prefixed
228
+ xlinkActuate: "xlink:actuate",
229
+ xlinkArcrole: "xlink:arcrole",
230
+ xlinkHref: "xlink:href",
231
+ xlinkRole: "xlink:role",
232
+ xlinkShow: "xlink:show",
233
+ xlinkTitle: "xlink:title",
234
+ xlinkType: "xlink:type",
235
+ xmlBase: "xml:base",
236
+ xmlLang: "xml:lang",
237
+ xmlSpace: "xml:space",
238
+ xmlns: "xmlns",
239
+ xmlnsXlink: "xmlns:xlink",
240
+ // Filter / lighting
241
+ baseFrequency: "baseFrequency",
242
+ colorInterpolation_filters: "color-interpolation-filters",
243
+ diffuseConstant: "diffuseConstant",
244
+ edgeMode: "edgeMode",
245
+ filterUnits: "filterUnits",
246
+ gradientTransform: "gradientTransform",
247
+ gradientUnits: "gradientUnits",
248
+ kernelMatrix: "kernelMatrix",
249
+ kernelUnitLength: "kernelUnitLength",
250
+ lengthAdjust: "lengthAdjust",
251
+ limitingConeAngle: "limitingConeAngle",
252
+ markerHeight: "markerHeight",
253
+ markerWidth: "markerWidth",
254
+ maskContentUnits: "maskContentUnits",
255
+ maskUnits: "maskUnits",
256
+ numOctaves: "numOctaves",
257
+ pathLength: "pathLength",
258
+ patternContentUnits: "patternContentUnits",
259
+ patternTransform: "patternTransform",
260
+ patternUnits: "patternUnits",
261
+ pointsAtX: "pointsAtX",
262
+ pointsAtY: "pointsAtY",
263
+ pointsAtZ: "pointsAtZ",
264
+ preserveAspectRatio: "preserveAspectRatio",
265
+ primitiveUnits: "primitiveUnits",
266
+ refX: "refX",
267
+ refY: "refY",
268
+ repeatCount: "repeatCount",
269
+ repeatDur: "repeatDur",
270
+ specularConstant: "specularConstant",
271
+ specularExponent: "specularExponent",
272
+ spreadMethod: "spreadMethod",
273
+ startOffset: "startOffset",
274
+ stdDeviation: "stdDeviation",
275
+ stitchTiles: "stitchTiles",
276
+ surfaceScale: "surfaceScale",
277
+ systemLanguage: "systemLanguage",
278
+ tableValues: "tableValues",
279
+ targetX: "targetX",
280
+ targetY: "targetY",
281
+ textLength: "textLength",
282
+ viewBox: "viewBox",
283
+ xChannelSelector: "xChannelSelector",
284
+ yChannelSelector: "yChannelSelector"
285
+ };
286
+ function renderAttributes(props, isSvg) {
287
+ let attrs = "";
288
+ for (const [key, value] of Object.entries(props)) {
289
+ if (key === "children" || key === "key" || key === "ref" || key === "dangerouslySetInnerHTML" || key === "suppressHydrationWarning" || key === "suppressContentEditableWarning")
290
+ continue;
291
+ if (key.startsWith("on") && key.length > 2 && key[2] === key[2].toUpperCase())
292
+ continue;
293
+ let attrName;
294
+ if (isSvg && key in SVG_ATTR_MAP) {
295
+ attrName = SVG_ATTR_MAP[key];
296
+ } else {
297
+ attrName = key === "className" ? "class" : key === "htmlFor" ? "for" : key === "tabIndex" ? "tabindex" : key === "defaultValue" ? "value" : key === "defaultChecked" ? "checked" : key;
298
+ }
299
+ if (value === false || value == null) {
300
+ if (value === false && (attrName.startsWith("aria-") || attrName.startsWith("data-"))) {
301
+ attrs += ` ${attrName}="false"`;
302
+ }
303
+ continue;
304
+ }
305
+ if (value === true) {
306
+ attrs += ` ${attrName}=""`;
307
+ continue;
308
+ }
309
+ if (key === "style" && typeof value === "object") {
310
+ attrs += ` style="${escapeAttr(styleObjectToString(value))}"`;
311
+ continue;
312
+ }
313
+ attrs += ` ${attrName}="${escapeAttr(String(value))}"`;
314
+ }
315
+ return attrs;
316
+ }
317
+ var BufferWriter = class {
318
+ chunks = [];
319
+ lastWasText = false;
320
+ write(chunk) {
321
+ this.chunks.push(chunk);
322
+ this.lastWasText = false;
323
+ }
324
+ text(s2) {
325
+ this.chunks.push(s2);
326
+ this.lastWasText = true;
327
+ }
328
+ flush(target) {
329
+ for (const c of this.chunks)
330
+ target.write(c);
331
+ target.lastWasText = this.lastWasText;
332
+ }
333
+ };
334
+ function renderNode(node, writer, isSvg = false) {
335
+ if (node == null || typeof node === "boolean")
336
+ return;
337
+ if (typeof node === "string") {
338
+ writer.text(escapeHtml(node));
339
+ return;
340
+ }
341
+ if (typeof node === "number") {
342
+ writer.text(String(node));
343
+ return;
344
+ }
345
+ if (Array.isArray(node)) {
346
+ return renderChildArray(node, writer, isSvg);
347
+ }
348
+ if (typeof node === "object" && node !== null && Symbol.iterator in node && !("$$typeof" in node)) {
349
+ return renderChildArray(
350
+ Array.from(node),
351
+ writer,
352
+ isSvg
353
+ );
354
+ }
355
+ if (typeof node === "object" && node !== null && "$$typeof" in node) {
356
+ const elType = node["$$typeof"];
357
+ if (elType !== SLIM_ELEMENT && elType !== REACT19_ELEMENT)
358
+ return;
359
+ const element = node;
360
+ const { type, props } = element;
361
+ if (type === FRAGMENT_TYPE) {
362
+ return renderChildren(props.children, writer, isSvg);
363
+ }
364
+ if (type === SUSPENSE_TYPE) {
365
+ return renderSuspense(props, writer, isSvg);
366
+ }
367
+ if (typeof type === "function") {
368
+ return renderComponent(type, props, writer, isSvg);
369
+ }
370
+ if (typeof type === "object" && type !== null) {
371
+ return renderComponent(type, props, writer, isSvg);
372
+ }
373
+ if (typeof type === "string") {
374
+ return renderHostElement(type, props, writer, isSvg);
375
+ }
376
+ }
377
+ }
378
+ function markSelectedOptionsMulti(children, selectedValues) {
379
+ if (children == null || typeof children === "boolean")
380
+ return children;
381
+ if (typeof children === "string" || typeof children === "number")
382
+ return children;
383
+ if (Array.isArray(children)) {
384
+ return children.map((c) => markSelectedOptionsMulti(c, selectedValues));
385
+ }
386
+ if (typeof children === "object" && "$$typeof" in children) {
387
+ const elType = children["$$typeof"];
388
+ if (elType !== SLIM_ELEMENT && elType !== REACT19_ELEMENT)
389
+ return children;
390
+ const el = children;
391
+ if (el.type === "option") {
392
+ const optValue = el.props.value !== void 0 ? el.props.value : el.props.children;
393
+ const isSelected = selectedValues.has(String(optValue));
394
+ return { ...el, props: { ...el.props, selected: isSelected || void 0 } };
395
+ }
396
+ if (el.type === "optgroup" || el.type === FRAGMENT_TYPE) {
397
+ const newChildren = markSelectedOptionsMulti(el.props.children, selectedValues);
398
+ return { ...el, props: { ...el.props, children: newChildren } };
399
+ }
400
+ }
401
+ return children;
402
+ }
403
+ function renderHostElement(tag, props, writer, isSvg) {
404
+ const enteringSvg = tag === "svg";
405
+ const childSvg = isSvg || enteringSvg;
406
+ if (tag === "textarea") {
407
+ const textContent = props.value ?? props.defaultValue ?? props.children ?? "";
408
+ const filteredProps = {};
409
+ for (const k of Object.keys(props)) {
410
+ if (k !== "value" && k !== "defaultValue" && k !== "children")
411
+ filteredProps[k] = props[k];
412
+ }
413
+ writer.write(`<textarea${renderAttributes(filteredProps, false)}>`);
414
+ writer.text(escapeHtml(String(textContent)));
415
+ writer.write("</textarea>");
416
+ return;
417
+ }
418
+ if (tag === "select") {
419
+ const selectedValue = props.value ?? props.defaultValue;
420
+ const filteredProps = {};
421
+ for (const k of Object.keys(props)) {
422
+ if (k !== "value" && k !== "defaultValue")
423
+ filteredProps[k] = props[k];
424
+ }
425
+ writer.write(`<select${renderAttributes(filteredProps, false)}>`);
426
+ const selectedSet = selectedValue == null ? null : Array.isArray(selectedValue) ? new Set(selectedValue.map(String)) : /* @__PURE__ */ new Set([String(selectedValue)]);
427
+ const patchedChildren = selectedSet != null ? markSelectedOptionsMulti(props.children, selectedSet) : props.children;
428
+ const inner2 = renderChildren(patchedChildren, writer, false);
429
+ if (inner2 && typeof inner2.then === "function") {
430
+ return inner2.then(() => {
431
+ writer.write("</select>");
432
+ });
433
+ }
434
+ writer.write("</select>");
435
+ return;
436
+ }
437
+ writer.write(`<${tag}${renderAttributes(props, childSvg)}`);
438
+ if (VOID_ELEMENTS.has(tag)) {
439
+ writer.write("/>");
440
+ return;
441
+ }
442
+ writer.write(">");
443
+ const childContext = tag === "foreignObject" ? false : childSvg;
444
+ let inner = void 0;
445
+ if (props.dangerouslySetInnerHTML) {
446
+ writer.write(props.dangerouslySetInnerHTML.__html);
447
+ } else {
448
+ inner = renderChildren(props.children, writer, childContext);
449
+ }
450
+ if (inner && typeof inner.then === "function") {
451
+ return inner.then(() => {
452
+ writer.write(`</${tag}>`);
453
+ });
454
+ }
455
+ writer.write(`</${tag}>`);
456
+ }
457
+ var REACT_MEMO = Symbol.for("react.memo");
458
+ var REACT_FORWARD_REF = Symbol.for("react.forward_ref");
459
+ var REACT_PROVIDER = Symbol.for("react.provider");
460
+ var REACT_CONTEXT = Symbol.for("react.context");
461
+ var REACT_CONSUMER = Symbol.for("react.consumer");
462
+ var REACT_LAZY = Symbol.for("react.lazy");
463
+ function renderComponent(type, props, writer, isSvg) {
464
+ const typeOf = type?.$$typeof;
465
+ if (typeOf === REACT_MEMO) {
466
+ return renderNode(
467
+ { $$typeof: SLIM_ELEMENT, type: type.type, props, key: null },
468
+ writer,
469
+ isSvg
470
+ );
471
+ }
472
+ if (typeOf === REACT_FORWARD_REF) {
473
+ return renderComponent(type.render, props, writer, isSvg);
474
+ }
475
+ if (typeOf === REACT_LAZY) {
476
+ const resolved = type._init(type._payload);
477
+ const LazyComp = resolved?.default ?? resolved;
478
+ return renderComponent(LazyComp, props, writer, isSvg);
479
+ }
480
+ if (typeOf === REACT_CONSUMER) {
481
+ const ctx2 = type._context;
482
+ const value = ctx2?._currentValue;
483
+ const result2 = typeof props.children === "function" ? props.children(value) : null;
484
+ const savedScope2 = pushComponentScope();
485
+ const finish2 = () => popComponentScope(savedScope2);
486
+ const r2 = renderNode(result2, writer, isSvg);
487
+ if (r2 && typeof r2.then === "function") {
488
+ return r2.then(finish2);
489
+ }
490
+ finish2();
491
+ return;
492
+ }
493
+ const isProvider = "_context" in type || typeOf === REACT_PROVIDER || typeOf === REACT_CONTEXT && "value" in props;
494
+ let prevCtxValue;
495
+ let ctx;
496
+ if (isProvider) {
497
+ ctx = type._context ?? type;
498
+ prevCtxValue = ctx._currentValue;
499
+ ctx._currentValue = props.value;
500
+ }
501
+ const savedScope = pushComponentScope();
502
+ if (isProvider && typeof type !== "function") {
503
+ const finish2 = () => {
504
+ popComponentScope(savedScope);
505
+ ctx._currentValue = prevCtxValue;
506
+ };
507
+ const r2 = renderChildren(props.children, writer, isSvg);
508
+ if (r2 && typeof r2.then === "function") {
509
+ return r2.then(finish2);
510
+ }
511
+ finish2();
512
+ return;
513
+ }
514
+ let result;
515
+ try {
516
+ if (type.prototype && typeof type.prototype.render === "function") {
517
+ const instance = new type(props);
518
+ if (typeof type.getDerivedStateFromProps === "function") {
519
+ const derived = type.getDerivedStateFromProps(props, instance.state ?? {});
520
+ if (derived != null)
521
+ instance.state = { ...instance.state ?? {}, ...derived };
522
+ }
523
+ result = instance.render();
524
+ } else {
525
+ result = type(props);
526
+ }
527
+ } catch (e) {
528
+ popComponentScope(savedScope);
529
+ if (isProvider)
530
+ ctx._currentValue = prevCtxValue;
531
+ throw e;
532
+ }
533
+ const finish = () => {
534
+ popComponentScope(savedScope);
535
+ if (isProvider)
536
+ ctx._currentValue = prevCtxValue;
537
+ };
538
+ if (result instanceof Promise) {
539
+ return result.then((resolved) => {
540
+ const r2 = renderNode(resolved, writer, isSvg);
541
+ if (r2 && typeof r2.then === "function") {
542
+ return r2.then(finish);
543
+ }
544
+ finish();
545
+ });
546
+ }
547
+ const r = renderNode(result, writer, isSvg);
548
+ if (r && typeof r.then === "function") {
549
+ return r.then(finish);
550
+ }
551
+ finish();
552
+ }
553
+ function isTextLike(node) {
554
+ return typeof node === "string" || typeof node === "number";
555
+ }
556
+ function renderChildArray(children, writer, isSvg) {
557
+ const totalChildren = children.length;
558
+ for (let i = 0; i < totalChildren; i++) {
559
+ if (isTextLike(children[i]) && writer.lastWasText) {
560
+ writer.write("<!-- -->");
561
+ }
562
+ const savedTree = pushTreeContext(totalChildren, i);
563
+ const r = renderNode(children[i], writer, isSvg);
564
+ if (r && typeof r.then === "function") {
565
+ return r.then(() => {
566
+ popTreeContext(savedTree);
567
+ return renderChildArrayFrom(children, i + 1, writer, isSvg);
568
+ });
569
+ }
570
+ popTreeContext(savedTree);
571
+ }
572
+ }
573
+ function renderChildArrayFrom(children, startIndex, writer, isSvg) {
574
+ const totalChildren = children.length;
575
+ for (let i = startIndex; i < totalChildren; i++) {
576
+ if (isTextLike(children[i]) && writer.lastWasText) {
577
+ writer.write("<!-- -->");
578
+ }
579
+ const savedTree = pushTreeContext(totalChildren, i);
580
+ const r = renderNode(children[i], writer, isSvg);
581
+ if (r && typeof r.then === "function") {
582
+ return r.then(() => {
583
+ popTreeContext(savedTree);
584
+ return renderChildArrayFrom(children, i + 1, writer, isSvg);
585
+ });
586
+ }
587
+ popTreeContext(savedTree);
588
+ }
589
+ }
590
+ function renderChildren(children, writer, isSvg = false) {
591
+ if (children == null)
592
+ return;
593
+ if (Array.isArray(children)) {
594
+ return renderChildArray(children, writer, isSvg);
595
+ }
596
+ return renderNode(children, writer, isSvg);
597
+ }
598
+ var MAX_SUSPENSE_RETRIES = 25;
599
+ async function renderSuspense(props, writer, isSvg = false) {
600
+ const { children, fallback } = props;
601
+ let attempts = 0;
602
+ const snap = snapshotContext();
603
+ while (attempts < MAX_SUSPENSE_RETRIES) {
604
+ restoreContext(snap);
605
+ try {
606
+ const buffer = new BufferWriter();
607
+ const r = renderNode(children, buffer, isSvg);
608
+ if (r && typeof r.then === "function") {
609
+ await r;
610
+ }
611
+ writer.write("<!--$-->");
612
+ buffer.flush(writer);
613
+ writer.write("<!--/$-->");
614
+ return;
615
+ } catch (error) {
616
+ if (error && typeof error.then === "function") {
617
+ await error;
618
+ attempts++;
619
+ } else {
620
+ throw error;
621
+ }
622
+ }
623
+ }
624
+ restoreContext(snap);
625
+ writer.write("<!--$?-->");
626
+ if (fallback) {
627
+ const r = renderNode(fallback, writer, isSvg);
628
+ if (r && typeof r.then === "function")
629
+ await r;
630
+ }
631
+ writer.write("<!--/$-->");
632
+ }
633
+ async function renderToString(element) {
634
+ for (let attempt = 0; attempt < MAX_SUSPENSE_RETRIES; attempt++) {
635
+ resetRenderState();
636
+ const chunks = [];
637
+ const writer = {
638
+ lastWasText: false,
639
+ write(c) {
640
+ chunks.push(c);
641
+ this.lastWasText = false;
642
+ },
643
+ text(s2) {
644
+ chunks.push(s2);
645
+ this.lastWasText = true;
646
+ }
647
+ };
648
+ try {
649
+ const r = renderNode(element, writer);
650
+ if (r && typeof r.then === "function")
651
+ await r;
652
+ return chunks.join("");
653
+ } catch (error) {
654
+ if (error && typeof error.then === "function") {
655
+ await error;
656
+ continue;
657
+ }
658
+ throw error;
659
+ }
660
+ }
661
+ throw new Error("[slim-react] renderToString exceeded maximum retries");
662
+ }
663
+
664
+ // src/ssr-render-worker.ts
6
665
  var { ssrBundlePath } = workerData;
7
- var _React = null;
8
- var _renderToStaticMarkup = null;
9
- var _renderToString = null;
10
666
  var _ssrMod = null;
11
667
  async function init() {
12
- if (_React && _ssrMod)
668
+ if (_ssrMod)
13
669
  return;
14
- const req = createRequire(pathMod.resolve(process.cwd(), "__hadars_fake__.js"));
15
- if (!_React) {
16
- const reactPath = pathToFileURL(req.resolve("react")).href;
17
- const reactMod = await import(reactPath);
18
- _React = reactMod.default ?? reactMod;
19
- }
20
- if (!_renderToString || !_renderToStaticMarkup) {
21
- const serverPath = pathToFileURL(req.resolve("react-dom/server")).href;
22
- const serverMod = await import(serverPath);
23
- _renderToString = serverMod.renderToString;
24
- _renderToStaticMarkup = serverMod.renderToStaticMarkup;
25
- }
26
- if (!_ssrMod) {
27
- _ssrMod = await import(pathToFileURL(ssrBundlePath).href);
28
- }
29
- }
30
- function deserializeRequest(s) {
31
- const init2 = { method: s.method, headers: new Headers(s.headers) };
32
- if (s.body)
33
- init2.body = s.body.buffer;
34
- const req = new Request(s.url, init2);
35
- Object.assign(req, {
36
- pathname: s.pathname,
37
- search: s.search,
38
- location: s.location,
39
- cookies: s.cookies
40
- });
670
+ _ssrMod = await import(pathToFileURL(ssrBundlePath).href);
671
+ }
672
+ function deserializeRequest(s2) {
673
+ const init2 = { method: s2.method, headers: new Headers(s2.headers) };
674
+ if (s2.body)
675
+ init2.body = s2.body.buffer;
676
+ const req = new Request(s2.url, init2);
677
+ Object.assign(req, { pathname: s2.pathname, search: s2.search, location: s2.location, cookies: s2.cookies });
41
678
  return req;
42
679
  }
680
+ var ESC = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" };
681
+ var escAttr = (s2) => s2.replace(/[&<>"]/g, (c) => ESC[c] ?? c);
682
+ var escText = (s2) => s2.replace(/[&<>]/g, (c) => ESC[c] ?? c);
683
+ var HEAD_ATTR = {
684
+ className: "class",
685
+ htmlFor: "for",
686
+ httpEquiv: "http-equiv",
687
+ charSet: "charset",
688
+ crossOrigin: "crossorigin"
689
+ };
690
+ function renderHeadTag(tag, id, opts, selfClose = false) {
691
+ let a = ` id="${escAttr(id)}"`;
692
+ let inner = "";
693
+ for (const [k, v] of Object.entries(opts)) {
694
+ if (k === "key" || k === "children")
695
+ continue;
696
+ if (k === "dangerouslySetInnerHTML") {
697
+ inner = v.__html ?? "";
698
+ continue;
699
+ }
700
+ const attr = HEAD_ATTR[k] ?? k;
701
+ if (v === true)
702
+ a += ` ${attr}`;
703
+ else if (v !== false && v != null)
704
+ a += ` ${attr}="${escAttr(String(v))}"`;
705
+ }
706
+ return selfClose ? `<${tag}${a}>` : `<${tag}${a}>${inner}</${tag}>`;
707
+ }
43
708
  function buildHeadHtml(head) {
44
- const R = _React;
45
- const metaEntries = Object.entries(head.meta ?? {});
46
- const linkEntries = Object.entries(head.link ?? {});
47
- const styleEntries = Object.entries(head.style ?? {});
48
- const scriptEntries = Object.entries(head.script ?? {});
49
- return _renderToStaticMarkup(
50
- R.createElement(
51
- R.Fragment,
52
- null,
53
- R.createElement("title", null, head.title),
54
- ...metaEntries.map(([id, opts]) => R.createElement("meta", { key: id, id, ...opts })),
55
- ...linkEntries.map(([id, opts]) => R.createElement("link", { key: id, id, ...opts })),
56
- ...styleEntries.map(([id, opts]) => R.createElement("style", { key: id, id, ...opts })),
57
- ...scriptEntries.map(([id, opts]) => R.createElement("script", { key: id, id, ...opts }))
58
- )
59
- );
60
- }
61
- function buildReactPage(appProps, clientProps) {
62
- const R = _React;
63
- const Component = _ssrMod.default;
64
- return R.createElement(
65
- R.Fragment,
66
- null,
67
- R.createElement(
68
- "div",
69
- { id: "app" },
70
- R.createElement(Component, appProps)
71
- ),
72
- R.createElement("script", {
73
- id: "hadars",
74
- type: "application/json",
75
- dangerouslySetInnerHTML: {
76
- __html: JSON.stringify({ hadars: { props: clientProps } }).replace(/</g, "\\u003c")
77
- }
78
- })
79
- );
709
+ let html = `<title>${escText(head.title ?? "")}</title>`;
710
+ for (const [id, opts] of Object.entries(head.meta ?? {}))
711
+ html += renderHeadTag("meta", id, opts, true);
712
+ for (const [id, opts] of Object.entries(head.link ?? {}))
713
+ html += renderHeadTag("link", id, opts, true);
714
+ for (const [id, opts] of Object.entries(head.style ?? {}))
715
+ html += renderHeadTag("style", id, opts);
716
+ for (const [id, opts] of Object.entries(head.script ?? {}))
717
+ html += renderHeadTag("script", id, opts);
718
+ return html;
80
719
  }
81
720
  async function runFullLifecycle(serialReq) {
82
- const R = _React;
83
721
  const Component = _ssrMod.default;
84
722
  const { getInitProps, getAfterRenderProps, getFinalProps } = _ssrMod;
85
723
  const parsedReq = deserializeRequest(serialReq);
86
- const unsuspend = { cache: /* @__PURE__ */ new Map(), hasPending: false };
87
724
  const context = {
88
- head: { title: "Hadars App", meta: {}, link: {}, style: {}, script: {}, status: 200 },
89
- _unsuspend: unsuspend
725
+ head: { title: "Hadars App", meta: {}, link: {}, style: {}, script: {}, status: 200 }
90
726
  };
91
727
  let props = {
92
728
  ...getInitProps ? await getInitProps(parsedReq) : {},
93
729
  location: serialReq.location,
94
730
  context
95
731
  };
96
- let html = "";
97
- let iters = 0;
98
- do {
99
- unsuspend.hasPending = false;
100
- try {
101
- globalThis.__hadarsUnsuspend = unsuspend;
102
- html = _renderToStaticMarkup(R.createElement(Component, props));
103
- } finally {
104
- globalThis.__hadarsUnsuspend = null;
105
- }
106
- if (unsuspend.hasPending) {
107
- const pending = [...unsuspend.cache.values()].filter((e) => e.status === "pending").map((e) => e.promise);
108
- await Promise.all(pending);
109
- }
110
- } while (unsuspend.hasPending && ++iters < 25);
111
- if (unsuspend.hasPending) {
112
- console.warn("[hadars] SSR render loop hit the 25-iteration cap \u2014 some useServerData values may not be resolved. Check for data dependencies that are never fulfilled.");
113
- }
114
- props = getAfterRenderProps ? await getAfterRenderProps(props, html) : props;
732
+ const unsuspend = { cache: /* @__PURE__ */ new Map() };
733
+ globalThis.__hadarsUnsuspend = unsuspend;
115
734
  try {
116
- globalThis.__hadarsUnsuspend = unsuspend;
117
- _renderToStaticMarkup(R.createElement(Component, { ...props, location: serialReq.location, context }));
735
+ let html = await renderToString(createElement(Component, props));
736
+ if (getAfterRenderProps) {
737
+ props = await getAfterRenderProps(props, html);
738
+ await renderToString(
739
+ createElement(Component, { ...props, location: serialReq.location, context })
740
+ );
741
+ }
118
742
  } finally {
119
743
  globalThis.__hadarsUnsuspend = null;
120
744
  }
745
+ const { context: _ctx, ...restProps } = getFinalProps ? await getFinalProps(props) : props;
121
746
  const serverData = {};
122
- for (const [k, v] of unsuspend.cache) {
123
- if (v.status === "fulfilled")
124
- serverData[k] = v.value;
125
- if (v.status === "suspense-cached")
126
- serverData[k] = v.value;
747
+ for (const [key, entry] of unsuspend.cache) {
748
+ if (entry.status === "fulfilled")
749
+ serverData[key] = entry.value;
127
750
  }
128
- const { context: _ctx, ...restProps } = getFinalProps ? await getFinalProps(props) : props;
129
751
  const clientProps = {
130
752
  ...restProps,
131
753
  location: serialReq.location,
132
754
  ...Object.keys(serverData).length > 0 ? { __serverData: serverData } : {}
133
755
  };
134
- const headHtml = buildHeadHtml(context.head);
135
- const status = context.head.status ?? 200;
136
756
  const finalAppProps = { ...props, location: serialReq.location, context };
137
- return { finalAppProps, clientProps, headHtml, status, unsuspend };
757
+ return { finalAppProps, clientProps, unsuspend, headHtml: buildHeadHtml(context.head), status: context.head.status ?? 200 };
138
758
  }
139
759
  parentPort.on("message", async (msg) => {
140
760
  const { id, type, request } = msg;
@@ -142,18 +762,30 @@ parentPort.on("message", async (msg) => {
142
762
  await init();
143
763
  if (type !== "renderFull")
144
764
  return;
145
- const { finalAppProps, clientProps, headHtml, status, unsuspend } = await runFullLifecycle(request);
146
- const ReactPage = buildReactPage(finalAppProps, clientProps);
765
+ const { finalAppProps, clientProps, unsuspend, headHtml, status } = await runFullLifecycle(request);
766
+ const Component = _ssrMod.default;
767
+ const page = createElement(
768
+ Fragment,
769
+ null,
770
+ createElement("div", { id: "app" }, createElement(Component, finalAppProps)),
771
+ createElement("script", {
772
+ id: "hadars",
773
+ type: "application/json",
774
+ dangerouslySetInnerHTML: {
775
+ __html: JSON.stringify({ hadars: { props: clientProps } }).replace(/</g, "\\u003c")
776
+ }
777
+ })
778
+ );
779
+ globalThis.__hadarsUnsuspend = unsuspend;
147
780
  let html;
148
781
  try {
149
- globalThis.__hadarsUnsuspend = unsuspend;
150
- html = _renderToString(ReactPage);
782
+ html = await renderToString(page);
151
783
  } finally {
152
784
  globalThis.__hadarsUnsuspend = null;
153
785
  }
786
+ html = processSegmentCache(html);
154
787
  parentPort.postMessage({ id, html, headHtml, status });
155
788
  } catch (err) {
156
- globalThis.__hadarsUnsuspend = null;
157
789
  parentPort.postMessage({ id, error: err?.message ?? String(err) });
158
790
  }
159
791
  });