sinwan 0.0.0 → 1.0.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.
Files changed (134) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +94 -1
  3. package/dist/cjs/index.development.js +2288 -0
  4. package/dist/cjs/index.development.js.map +35 -0
  5. package/dist/cjs/index.production.min.js +3 -0
  6. package/dist/cjs/index.production.min.js.map +35 -0
  7. package/dist/cjs/jsx/jsx-dev-runtime.development.js +124 -0
  8. package/dist/cjs/jsx/jsx-dev-runtime.development.js.map +10 -0
  9. package/dist/cjs/jsx/jsx-dev-runtime.production.min.js +3 -0
  10. package/dist/cjs/jsx/jsx-dev-runtime.production.min.js.map +10 -0
  11. package/dist/cjs/jsx/jsx-runtime.development.js +116 -0
  12. package/dist/cjs/jsx/jsx-runtime.development.js.map +10 -0
  13. package/dist/cjs/jsx/jsx-runtime.production.min.js +3 -0
  14. package/dist/cjs/jsx/jsx-runtime.production.min.js.map +10 -0
  15. package/dist/cjs/package.json +4 -0
  16. package/dist/cjs/renderer/index.development.js +1175 -0
  17. package/dist/cjs/renderer/index.development.js.map +24 -0
  18. package/dist/cjs/renderer/index.production.min.js +3 -0
  19. package/dist/cjs/renderer/index.production.min.js.map +24 -0
  20. package/dist/cjs/server/index.development.js +1265 -0
  21. package/dist/cjs/server/index.development.js.map +23 -0
  22. package/dist/cjs/server/index.production.min.js +3 -0
  23. package/dist/cjs/server/index.production.min.js.map +23 -0
  24. package/dist/component/control-flow.d.ts +18 -0
  25. package/dist/component/control-flow.d.ts.map +1 -0
  26. package/dist/component/create.d.ts +75 -0
  27. package/dist/component/create.d.ts.map +1 -0
  28. package/dist/component/index.d.ts +12 -0
  29. package/dist/component/index.d.ts.map +1 -0
  30. package/dist/component/instance.d.ts +84 -0
  31. package/dist/component/instance.d.ts.map +1 -0
  32. package/dist/component/lifecycle.d.ts +41 -0
  33. package/dist/component/lifecycle.d.ts.map +1 -0
  34. package/dist/component/provide-inject.d.ts +45 -0
  35. package/dist/component/provide-inject.d.ts.map +1 -0
  36. package/dist/escaper.d.ts +26 -0
  37. package/dist/escaper.d.ts.map +1 -0
  38. package/dist/esm/index.development.js +2201 -0
  39. package/dist/esm/index.development.js.map +35 -0
  40. package/dist/esm/index.production.min.js +4 -0
  41. package/dist/esm/index.production.min.js.map +35 -0
  42. package/dist/esm/jsx/jsx-dev-runtime.development.js +73 -0
  43. package/dist/esm/jsx/jsx-dev-runtime.development.js.map +10 -0
  44. package/dist/esm/jsx/jsx-dev-runtime.production.min.js +4 -0
  45. package/dist/esm/jsx/jsx-dev-runtime.production.min.js.map +10 -0
  46. package/dist/esm/jsx/jsx-runtime.development.js +77 -0
  47. package/dist/esm/jsx/jsx-runtime.development.js.map +10 -0
  48. package/dist/esm/jsx/jsx-runtime.production.min.js +4 -0
  49. package/dist/esm/jsx/jsx-runtime.production.min.js.map +10 -0
  50. package/dist/esm/package.json +4 -0
  51. package/dist/esm/renderer/index.development.js +1124 -0
  52. package/dist/esm/renderer/index.development.js.map +24 -0
  53. package/dist/esm/renderer/index.production.min.js +4 -0
  54. package/dist/esm/renderer/index.production.min.js.map +24 -0
  55. package/dist/esm/server/index.development.js +1214 -0
  56. package/dist/esm/server/index.development.js.map +23 -0
  57. package/dist/esm/server/index.production.min.js +4 -0
  58. package/dist/esm/server/index.production.min.js.map +23 -0
  59. package/dist/hydration/hydrate.d.ts +29 -0
  60. package/dist/hydration/hydrate.d.ts.map +1 -0
  61. package/dist/hydration/index.d.ts +7 -0
  62. package/dist/hydration/index.d.ts.map +1 -0
  63. package/dist/hydration/markers.d.ts +48 -0
  64. package/dist/hydration/markers.d.ts.map +1 -0
  65. package/dist/hydration/walk.d.ts +32 -0
  66. package/dist/hydration/walk.d.ts.map +1 -0
  67. package/dist/index.d.ts +14 -0
  68. package/dist/index.d.ts.map +1 -0
  69. package/dist/index.js +7 -0
  70. package/dist/index.mjs +4 -0
  71. package/dist/jsx/jsx-dev-runtime.d.ts +10 -0
  72. package/dist/jsx/jsx-dev-runtime.d.ts.map +1 -0
  73. package/dist/jsx/jsx-runtime.d.ts +67 -0
  74. package/dist/jsx/jsx-runtime.d.ts.map +1 -0
  75. package/dist/jsx/jsx-types.d.ts +702 -0
  76. package/dist/jsx/jsx-types.d.ts.map +1 -0
  77. package/dist/jsx-dev-runtime.d.ts +1 -0
  78. package/dist/jsx-dev-runtime.js +7 -0
  79. package/dist/jsx-dev-runtime.mjs +4 -0
  80. package/dist/jsx-runtime.d.ts +1 -0
  81. package/dist/jsx-runtime.js +7 -0
  82. package/dist/jsx-runtime.mjs +4 -0
  83. package/dist/reactivity/batch.d.ts +27 -0
  84. package/dist/reactivity/batch.d.ts.map +1 -0
  85. package/dist/reactivity/computed.d.ts +41 -0
  86. package/dist/reactivity/computed.d.ts.map +1 -0
  87. package/dist/reactivity/effect.d.ts +79 -0
  88. package/dist/reactivity/effect.d.ts.map +1 -0
  89. package/dist/reactivity/index.d.ts +15 -0
  90. package/dist/reactivity/index.d.ts.map +1 -0
  91. package/dist/reactivity/scheduler.d.ts +35 -0
  92. package/dist/reactivity/scheduler.d.ts.map +1 -0
  93. package/dist/reactivity/signal.d.ts +36 -0
  94. package/dist/reactivity/signal.d.ts.map +1 -0
  95. package/dist/renderer/attributes.d.ts +8 -0
  96. package/dist/renderer/attributes.d.ts.map +1 -0
  97. package/dist/renderer/dom-ops.d.ts +30 -0
  98. package/dist/renderer/dom-ops.d.ts.map +1 -0
  99. package/dist/renderer/events.d.ts +21 -0
  100. package/dist/renderer/events.d.ts.map +1 -0
  101. package/dist/renderer/index.d.ts +12 -0
  102. package/dist/renderer/index.d.ts.map +1 -0
  103. package/dist/renderer/mount.d.ts +27 -0
  104. package/dist/renderer/mount.d.ts.map +1 -0
  105. package/dist/renderer/render-children.d.ts +19 -0
  106. package/dist/renderer/render-children.d.ts.map +1 -0
  107. package/dist/renderer/render-control-flow.d.ts +13 -0
  108. package/dist/renderer/render-control-flow.d.ts.map +1 -0
  109. package/dist/renderer/render-element.d.ts +13 -0
  110. package/dist/renderer/render-element.d.ts.map +1 -0
  111. package/dist/renderer/types.d.ts +65 -0
  112. package/dist/renderer/types.d.ts.map +1 -0
  113. package/dist/renderer/unmount.d.ts +20 -0
  114. package/dist/renderer/unmount.d.ts.map +1 -0
  115. package/dist/renderer.d.ts +1 -0
  116. package/dist/renderer.js +7 -0
  117. package/dist/renderer.mjs +4 -0
  118. package/dist/server/hydration-markers.d.ts +23 -0
  119. package/dist/server/hydration-markers.d.ts.map +1 -0
  120. package/dist/server/index.d.ts +9 -0
  121. package/dist/server/index.d.ts.map +1 -0
  122. package/dist/server/renderer.d.ts +33 -0
  123. package/dist/server/renderer.d.ts.map +1 -0
  124. package/dist/server/stream.d.ts +20 -0
  125. package/dist/server/stream.d.ts.map +1 -0
  126. package/dist/server.d.ts +1 -0
  127. package/dist/server.js +7 -0
  128. package/dist/server.mjs +4 -0
  129. package/dist/types.d.ts +48 -0
  130. package/dist/types.d.ts.map +1 -0
  131. package/package.json +116 -4
  132. package/bun.lock +0 -26
  133. package/src/index.ts +0 -1
  134. package/tsconfig.json +0 -29
@@ -0,0 +1,2288 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ function __accessProp(key) {
6
+ return this[key];
7
+ }
8
+ var __toCommonJS = (from) => {
9
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
10
+ if (entry)
11
+ return entry;
12
+ entry = __defProp({}, "__esModule", { value: true });
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (var key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(entry, key))
16
+ __defProp(entry, key, {
17
+ get: __accessProp.bind(from, key),
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ __moduleCache.set(from, entry);
22
+ return entry;
23
+ };
24
+ var __moduleCache;
25
+ var __returnValue = (v) => v;
26
+ function __exportSetter(name, newValue) {
27
+ this[name] = __returnValue.bind(null, newValue);
28
+ }
29
+ var __export = (target, all) => {
30
+ for (var name in all)
31
+ __defProp(target, name, {
32
+ get: all[name],
33
+ enumerable: true,
34
+ configurable: true,
35
+ set: __exportSetter.bind(all, name)
36
+ });
37
+ };
38
+
39
+ // src/jsx/jsx-runtime.ts
40
+ var exports_jsx_runtime = {};
41
+ __export(exports_jsx_runtime, {
42
+ raw: () => raw,
43
+ jsxs: () => jsxs,
44
+ jsxDEV: () => jsxDEV,
45
+ jsx: () => jsx,
46
+ HtmlEscapedString: () => HtmlEscapedString,
47
+ Fragment: () => Fragment
48
+ });
49
+ module.exports = __toCommonJS(exports_jsx_runtime);
50
+ var Fragment = Symbol("Fragment");
51
+
52
+ class HtmlEscapedString extends String {
53
+ value;
54
+ constructor(value) {
55
+ super(value);
56
+ this.value = value;
57
+ }
58
+ toString() {
59
+ return this.value;
60
+ }
61
+ }
62
+ var raw = (str) => new HtmlEscapedString(str);
63
+ var VOID_ELEMENTS = new Set([
64
+ "area",
65
+ "base",
66
+ "br",
67
+ "col",
68
+ "embed",
69
+ "hr",
70
+ "img",
71
+ "input",
72
+ "link",
73
+ "meta",
74
+ "param",
75
+ "source",
76
+ "track",
77
+ "wbr"
78
+ ]);
79
+ function normalizeChildren(children) {
80
+ if (children == null || typeof children === "boolean")
81
+ return [];
82
+ if (Array.isArray(children))
83
+ return children.flat(Infinity);
84
+ return [children];
85
+ }
86
+ function buildElement(type, props, children) {
87
+ if (type === Fragment) {
88
+ return { tag: "", props: {}, children };
89
+ }
90
+ if (typeof type === "function" || typeof type === "string") {
91
+ const finalProps = props ?? {};
92
+ if (children.length > 0 && finalProps.children === undefined) {
93
+ finalProps.children = children.length === 1 ? children[0] : children;
94
+ }
95
+ return { tag: type, props: finalProps, children };
96
+ }
97
+ return { tag: "", props: {}, children };
98
+ }
99
+ function jsx(type, props, key) {
100
+ return buildElement(type, props, normalizeChildren(props?.children));
101
+ }
102
+ function jsxs(type, props, key) {
103
+ const children = props?.children;
104
+ return buildElement(type, props, Array.isArray(children) ? children.flat(Infinity) : normalizeChildren(children));
105
+ }
106
+ function jsxDEV(type, props, key, isStaticChildren, source, self) {
107
+ const children = isStaticChildren ? Array.isArray(props?.children) ? props.children.flat(Infinity) : normalizeChildren(props?.children) : normalizeChildren(props?.children);
108
+ const element = buildElement(type, props, children);
109
+ if (source) {
110
+ element.__source = source;
111
+ }
112
+ return element;
113
+ }
114
+
115
+ // src/renderer/index.ts
116
+ var exports_renderer = {};
117
+ __export(exports_renderer, {
118
+ unmountNode: () => unmountNode,
119
+ toEventName: () => toEventName,
120
+ setDOMOps: () => setDOMOps,
121
+ resetDOMOps: () => resetDOMOps,
122
+ renderNodeToDOM: () => renderNodeToDOM,
123
+ renderElementToDOM: () => renderElementToDOM,
124
+ renderChildrenToDOM: () => renderChildrenToDOM,
125
+ render: () => render,
126
+ mount: () => mount,
127
+ isEventProp: () => isEventProp,
128
+ domOps: () => domOps,
129
+ bindEvents: () => bindEvents,
130
+ bindEvent: () => bindEvent,
131
+ applyAttributes: () => applyAttributes
132
+ });
133
+ module.exports = __toCommonJS(exports_renderer);
134
+
135
+ // src/renderer/dom-ops.ts
136
+ function createDefaultDOMOps() {
137
+ return {
138
+ createElement(tag) {
139
+ return document.createElement(tag);
140
+ },
141
+ createElementNS(namespace, tag) {
142
+ return document.createElementNS(namespace, tag);
143
+ },
144
+ createTextNode(text) {
145
+ return document.createTextNode(text);
146
+ },
147
+ createComment(text) {
148
+ return document.createComment(text);
149
+ },
150
+ setAttribute(el, key, value) {
151
+ el.setAttribute(key, value);
152
+ },
153
+ removeAttribute(el, key) {
154
+ el.removeAttribute(key);
155
+ },
156
+ setProperty(el, key, value) {
157
+ el[key] = value;
158
+ },
159
+ insertBefore(parent, child, anchor) {
160
+ parent.insertBefore(child, anchor);
161
+ },
162
+ appendChild(parent, child) {
163
+ parent.appendChild(child);
164
+ },
165
+ remove(node) {
166
+ node.parentNode?.removeChild(node);
167
+ },
168
+ setTextContent(node, text) {
169
+ node.data = text;
170
+ },
171
+ addEventListener(el, event, handler) {
172
+ el.addEventListener(event, handler);
173
+ },
174
+ removeEventListener(el, event, handler) {
175
+ el.removeEventListener(event, handler);
176
+ },
177
+ parentNode(node) {
178
+ return node.parentNode;
179
+ },
180
+ nextSibling(node) {
181
+ return node.nextSibling;
182
+ }
183
+ };
184
+ }
185
+ var defaultDOMOps = createDefaultDOMOps();
186
+ var domOps = { ...defaultDOMOps };
187
+ function setDOMOps(overrides) {
188
+ Object.assign(domOps, overrides);
189
+ }
190
+ function resetDOMOps() {
191
+ for (const key of Object.keys(domOps)) {
192
+ delete domOps[key];
193
+ }
194
+ Object.assign(domOps, defaultDOMOps);
195
+ }
196
+
197
+ // src/reactivity/scheduler.ts
198
+ var pendingEffects = new Set;
199
+ var flushScheduled = false;
200
+ var isFlushing = false;
201
+ var pendingCallbacks = [];
202
+ function scheduleEffect(effect) {
203
+ if (!effect.active)
204
+ return;
205
+ pendingEffects.add(effect);
206
+ scheduleFlush();
207
+ }
208
+ function unscheduleEffect(effect) {
209
+ pendingEffects.delete(effect);
210
+ }
211
+ function scheduleFlush() {
212
+ if (!flushScheduled) {
213
+ flushScheduled = true;
214
+ queueMicrotask(flush);
215
+ }
216
+ }
217
+ function flush() {
218
+ isFlushing = true;
219
+ const sorted = [...pendingEffects].sort((a, b) => a.id - b.id);
220
+ pendingEffects.clear();
221
+ for (const effect of sorted) {
222
+ if (effect.active) {
223
+ effect.run();
224
+ }
225
+ }
226
+ let safety = 10;
227
+ while (pendingEffects.size > 0 && safety-- > 0) {
228
+ const next = [...pendingEffects].sort((a, b) => a.id - b.id);
229
+ pendingEffects.clear();
230
+ for (const effect of next) {
231
+ if (effect.active) {
232
+ effect.run();
233
+ }
234
+ }
235
+ }
236
+ flushScheduled = false;
237
+ isFlushing = false;
238
+ const cbs = pendingCallbacks.splice(0);
239
+ for (const cb of cbs) {
240
+ cb();
241
+ }
242
+ }
243
+ function nextTick(fn) {
244
+ return new Promise((resolve) => {
245
+ const callback = () => {
246
+ fn?.();
247
+ resolve();
248
+ };
249
+ if (flushScheduled || isFlushing) {
250
+ pendingCallbacks.push(callback);
251
+ } else {
252
+ queueMicrotask(callback);
253
+ }
254
+ });
255
+ }
256
+ function flushSync() {
257
+ if (flushScheduled) {
258
+ flushScheduled = false;
259
+ flush();
260
+ }
261
+ }
262
+
263
+ // src/reactivity/effect.ts
264
+ var activeEffect = null;
265
+ var effectStack = [];
266
+ var effectIdCounter = 0;
267
+
268
+ class ReactiveEffect {
269
+ id;
270
+ active = true;
271
+ fn;
272
+ cleanup = undefined;
273
+ deps = new Set;
274
+ constructor(fn) {
275
+ this.id = effectIdCounter++;
276
+ this.fn = fn;
277
+ }
278
+ run() {
279
+ if (!this.active)
280
+ return;
281
+ if (effectStack.includes(this))
282
+ return;
283
+ this.cleanupDeps();
284
+ if (this.cleanup) {
285
+ this.cleanup();
286
+ this.cleanup = undefined;
287
+ }
288
+ effectStack.push(this);
289
+ const prevEffect = activeEffect;
290
+ activeEffect = this;
291
+ try {
292
+ const result = this.fn();
293
+ if (typeof result === "function") {
294
+ this.cleanup = result;
295
+ }
296
+ } finally {
297
+ activeEffect = prevEffect;
298
+ effectStack.pop();
299
+ }
300
+ }
301
+ cleanupDeps() {
302
+ for (const dep of this.deps) {
303
+ dep.subscribers.delete(this);
304
+ }
305
+ this.deps.clear();
306
+ }
307
+ notify() {
308
+ scheduleEffect(this);
309
+ }
310
+ dispose() {
311
+ if (!this.active)
312
+ return;
313
+ this.active = false;
314
+ if (this.cleanup) {
315
+ this.cleanup();
316
+ this.cleanup = undefined;
317
+ }
318
+ this.cleanupDeps();
319
+ unscheduleEffect(this);
320
+ }
321
+ }
322
+ function effect(fn) {
323
+ const e = new ReactiveEffect(fn);
324
+ e.run();
325
+ return () => e.dispose();
326
+ }
327
+ function track(dep) {
328
+ if (activeEffect) {
329
+ dep.subscribers.add(activeEffect);
330
+ activeEffect.deps.add(dep);
331
+ }
332
+ }
333
+ function trigger(dep) {
334
+ const effects = [...dep.subscribers];
335
+ for (const effect2 of effects) {
336
+ effect2.notify();
337
+ }
338
+ }
339
+
340
+ // src/reactivity/signal.ts
341
+ var SIGNAL_BRAND = Symbol("Sinwan:signal");
342
+
343
+ class SignalImpl {
344
+ [SIGNAL_BRAND] = true;
345
+ subscribers = new Set;
346
+ _value;
347
+ _manualSubs = new Set;
348
+ constructor(initial) {
349
+ this._value = initial;
350
+ }
351
+ get value() {
352
+ track(this);
353
+ return this._value;
354
+ }
355
+ set value(newValue) {
356
+ if (Object.is(this._value, newValue))
357
+ return;
358
+ this._value = newValue;
359
+ trigger(this);
360
+ for (const fn of this._manualSubs) {
361
+ fn(newValue);
362
+ }
363
+ }
364
+ peek() {
365
+ return this._value;
366
+ }
367
+ subscribe(fn) {
368
+ this._manualSubs.add(fn);
369
+ return () => {
370
+ this._manualSubs.delete(fn);
371
+ };
372
+ }
373
+ toString() {
374
+ return String(this.value);
375
+ }
376
+ valueOf() {
377
+ return this.value;
378
+ }
379
+ }
380
+ function signal(initial) {
381
+ return new SignalImpl(initial);
382
+ }
383
+ function isSignal(value) {
384
+ return value != null && typeof value === "object" && SIGNAL_BRAND in value;
385
+ }
386
+
387
+ // src/reactivity/computed.ts
388
+ var COMPUTED_BRAND = Symbol("Sinwan:computed");
389
+
390
+ class ComputedImpl {
391
+ [COMPUTED_BRAND] = true;
392
+ subscribers = new Set;
393
+ _value;
394
+ _dirty = true;
395
+ _effect;
396
+ constructor(getter) {
397
+ const self = this;
398
+ this._effect = new ReactiveEffect(() => {
399
+ self._value = getter();
400
+ });
401
+ this._effect.notify = function() {
402
+ if (!self._dirty) {
403
+ self._dirty = true;
404
+ trigger(self);
405
+ }
406
+ };
407
+ this._effect.run();
408
+ this._dirty = false;
409
+ }
410
+ get value() {
411
+ track(this);
412
+ if (this._dirty) {
413
+ this._effect.run();
414
+ this._dirty = false;
415
+ }
416
+ return this._value;
417
+ }
418
+ peek() {
419
+ if (this._dirty) {
420
+ this._effect.run();
421
+ this._dirty = false;
422
+ }
423
+ return this._value;
424
+ }
425
+ toString() {
426
+ return String(this.value);
427
+ }
428
+ valueOf() {
429
+ return this.value;
430
+ }
431
+ }
432
+ function computed(getter) {
433
+ return new ComputedImpl(getter);
434
+ }
435
+ function isComputed(value) {
436
+ return value != null && typeof value === "object" && COMPUTED_BRAND in value;
437
+ }
438
+
439
+ // src/renderer/events.ts
440
+ function isEventProp(key) {
441
+ return key.length > 2 && key[0] === "o" && key[1] === "n" && key[2] >= "A" && key[2] <= "Z";
442
+ }
443
+ function toEventName(key) {
444
+ return key.slice(2).toLowerCase();
445
+ }
446
+ function bindEvent(el, eventName, handler) {
447
+ domOps.addEventListener(el, eventName, handler);
448
+ return () => {
449
+ domOps.removeEventListener(el, eventName, handler);
450
+ };
451
+ }
452
+ function bindEvents(el, props) {
453
+ const cleanups = [];
454
+ for (const key of Object.keys(props)) {
455
+ if (isEventProp(key)) {
456
+ const handler = props[key];
457
+ if (typeof handler === "function") {
458
+ const eventName = toEventName(key);
459
+ cleanups.push(bindEvent(el, eventName, handler));
460
+ }
461
+ }
462
+ }
463
+ return cleanups;
464
+ }
465
+
466
+ // src/component/instance.ts
467
+ var uidCounter = 0;
468
+ function createComponentInstance(component, props, parent) {
469
+ return {
470
+ uid: uidCounter++,
471
+ component,
472
+ props,
473
+ element: null,
474
+ parent,
475
+ children: [],
476
+ effects: [],
477
+ _mountedHooks: [],
478
+ _unmountedHooks: [],
479
+ _updatedHooks: [],
480
+ _errorHooks: [],
481
+ provides: parent ? Object.create(parent.provides) : Object.create(null),
482
+ isMounted: false,
483
+ isUnmounted: false
484
+ };
485
+ }
486
+ var currentInstance = null;
487
+ function getCurrentInstance() {
488
+ return currentInstance;
489
+ }
490
+ function setCurrentInstance(instance) {
491
+ const prev = currentInstance;
492
+ currentInstance = instance;
493
+ return prev;
494
+ }
495
+ function withInstance(instance, fn) {
496
+ const prev = setCurrentInstance(instance);
497
+ try {
498
+ return fn();
499
+ } finally {
500
+ setCurrentInstance(prev);
501
+ }
502
+ }
503
+ function fireMountedHooks(instance) {
504
+ if (instance.isUnmounted) {
505
+ return;
506
+ }
507
+ for (const child of instance.children) {
508
+ fireMountedHooks(child);
509
+ }
510
+ if (!instance.isMounted) {
511
+ instance.isMounted = true;
512
+ for (const hook of instance._mountedHooks) {
513
+ hook();
514
+ }
515
+ }
516
+ }
517
+ function fireUnmountedHooks(instance) {
518
+ for (const child of instance.children) {
519
+ fireUnmountedHooks(child);
520
+ }
521
+ if (instance.isMounted && !instance.isUnmounted) {
522
+ instance.isUnmounted = true;
523
+ instance.isMounted = false;
524
+ for (const hook of instance._unmountedHooks) {
525
+ hook();
526
+ }
527
+ for (const dispose of instance.effects) {
528
+ dispose();
529
+ }
530
+ instance.effects.length = 0;
531
+ }
532
+ }
533
+ function fireUpdatedHooks(instance) {
534
+ for (const hook of instance._updatedHooks) {
535
+ hook();
536
+ }
537
+ }
538
+ var queuedUpdatedHooks = new Set;
539
+ function queueUpdatedHooks(instance) {
540
+ if (!instance || !instance.isMounted || instance.isUnmounted || instance._updatedHooks.length === 0 || queuedUpdatedHooks.has(instance)) {
541
+ return;
542
+ }
543
+ queuedUpdatedHooks.add(instance);
544
+ nextTick(() => {
545
+ queuedUpdatedHooks.delete(instance);
546
+ if (instance.isMounted && !instance.isUnmounted) {
547
+ fireUpdatedHooks(instance);
548
+ }
549
+ });
550
+ }
551
+ function handleComponentError(instance, err) {
552
+ let current = instance;
553
+ while (current) {
554
+ if (current._errorHooks.length > 0) {
555
+ for (const hook of current._errorHooks) {
556
+ hook(err);
557
+ }
558
+ return;
559
+ }
560
+ current = current.parent;
561
+ }
562
+ console.error("[Sinwan] Unhandled component error:", err);
563
+ }
564
+
565
+ // src/renderer/attributes.ts
566
+ var SKIP_PROPS = new Set(["children", "key", "ref", "dangerouslySetInnerHTML"]);
567
+ var DOM_PROPERTIES = new Set(["value", "checked", "selected", "disabled", "readOnly", "multiple", "indeterminate"]);
568
+ var PROP_ALIASES = {
569
+ className: "class",
570
+ htmlFor: "for",
571
+ tabIndex: "tabindex",
572
+ crossOrigin: "crossorigin"
573
+ };
574
+ function applyAttributes(el, props) {
575
+ const disposers = [];
576
+ const owner = getCurrentInstance();
577
+ for (const [key, value] of Object.entries(props)) {
578
+ if (SKIP_PROPS.has(key) || isEventProp(key))
579
+ continue;
580
+ if (isSignal(value) || isComputed(value)) {
581
+ let initialized = false;
582
+ const dispose = effect(() => {
583
+ setSingleAttribute(el, key, value.value);
584
+ if (initialized) {
585
+ queueUpdatedHooks(owner);
586
+ }
587
+ initialized = true;
588
+ });
589
+ disposers.push(dispose);
590
+ } else {
591
+ setSingleAttribute(el, key, value);
592
+ }
593
+ }
594
+ return disposers;
595
+ }
596
+ function setSingleAttribute(el, key, value) {
597
+ const attrName = PROP_ALIASES[key] ?? key;
598
+ if (attrName === "style" && typeof value === "object" && value !== null) {
599
+ applyStyle(el, value);
600
+ return;
601
+ }
602
+ if (attrName === "class" && typeof value === "object" && value !== null) {
603
+ applyClass(el, value);
604
+ return;
605
+ }
606
+ if (value == null || value === false) {
607
+ domOps.removeAttribute(el, attrName);
608
+ if (DOM_PROPERTIES.has(attrName)) {
609
+ domOps.setProperty(el, attrName, attrName === "value" ? "" : false);
610
+ }
611
+ return;
612
+ }
613
+ if (value === true) {
614
+ domOps.setAttribute(el, attrName, "");
615
+ if (DOM_PROPERTIES.has(attrName)) {
616
+ domOps.setProperty(el, attrName, true);
617
+ }
618
+ return;
619
+ }
620
+ if (DOM_PROPERTIES.has(attrName)) {
621
+ domOps.setProperty(el, attrName, value);
622
+ return;
623
+ }
624
+ domOps.setAttribute(el, attrName, String(value));
625
+ }
626
+ function applyStyle(el, styles) {
627
+ for (const [prop, val] of Object.entries(styles)) {
628
+ if (prop.includes("-")) {
629
+ el.style.setProperty(prop, val);
630
+ } else {
631
+ el.style[prop] = val;
632
+ }
633
+ }
634
+ }
635
+ function applyClass(el, value) {
636
+ let classStr;
637
+ if (Array.isArray(value)) {
638
+ classStr = value.filter(Boolean).join(" ");
639
+ } else if (typeof value === "object" && value !== null) {
640
+ classStr = Object.entries(value).filter(([, v]) => Boolean(v)).map(([k]) => k).join(" ");
641
+ } else {
642
+ classStr = String(value);
643
+ }
644
+ domOps.setAttribute(el, "class", classStr);
645
+ }
646
+
647
+ // src/component/control-flow.ts
648
+ var SHOW_TYPE = Symbol.for("Sinwan.Show");
649
+ var FOR_TYPE = Symbol.for("Sinwan.For");
650
+ function Show(props) {
651
+ return {
652
+ tag: SHOW_TYPE,
653
+ props,
654
+ children: []
655
+ };
656
+ }
657
+ function For(props) {
658
+ return {
659
+ tag: FOR_TYPE,
660
+ props,
661
+ children: []
662
+ };
663
+ }
664
+ function isShowElement(element) {
665
+ return element.tag === SHOW_TYPE;
666
+ }
667
+ function isForElement(element) {
668
+ return element.tag === FOR_TYPE;
669
+ }
670
+
671
+ // src/renderer/unmount.ts
672
+ function getMountedDomNodes(node) {
673
+ switch (node.type) {
674
+ case "text":
675
+ case "reactive-text":
676
+ return [node.node];
677
+ case "element":
678
+ return [node.node];
679
+ case "fragment":
680
+ return [
681
+ node.anchor,
682
+ ...node.children.flatMap((child) => getMountedDomNodes(child))
683
+ ];
684
+ case "reactive-block":
685
+ return [
686
+ node.startAnchor,
687
+ ...node.children.flatMap((child) => getMountedDomNodes(child)),
688
+ node.endAnchor
689
+ ];
690
+ case "component":
691
+ return node.children.flatMap((child) => getMountedDomNodes(child));
692
+ }
693
+ }
694
+ function unmountNode(node) {
695
+ switch (node.type) {
696
+ case "text":
697
+ break;
698
+ case "reactive-text":
699
+ node.dispose();
700
+ break;
701
+ case "element":
702
+ for (const dispose of node.attrDisposers) {
703
+ dispose();
704
+ }
705
+ for (const cleanup of node.eventCleanups) {
706
+ cleanup();
707
+ }
708
+ node.refCleanup?.();
709
+ for (const child of node.children) {
710
+ unmountNode(child);
711
+ }
712
+ break;
713
+ case "fragment":
714
+ for (const child of node.children) {
715
+ unmountNode(child);
716
+ }
717
+ break;
718
+ case "reactive-block":
719
+ node.dispose();
720
+ for (const child of node.children) {
721
+ unmountNode(child);
722
+ }
723
+ break;
724
+ case "component":
725
+ if (node.instance) {
726
+ fireUnmountedHooks(node.instance);
727
+ } else {
728
+ for (const dispose of node.disposers) {
729
+ dispose();
730
+ }
731
+ }
732
+ for (const child of node.children) {
733
+ unmountNode(child);
734
+ }
735
+ break;
736
+ }
737
+ }
738
+ function removeMountedNode(node) {
739
+ const domNodes = getMountedDomNodes(node);
740
+ unmountNode(node);
741
+ for (const domNode of domNodes) {
742
+ if (domNode.parentNode) {
743
+ domOps.remove(domNode);
744
+ }
745
+ }
746
+ }
747
+
748
+ // src/renderer/render-control-flow.ts
749
+ function renderControlFlowToDOM(element, parent, anchor, namespace) {
750
+ const startAnchor = domOps.createComment("Sinwan-b");
751
+ const endAnchor = domOps.createComment("/Sinwan-b");
752
+ insertNode(parent, startAnchor, anchor);
753
+ insertNode(parent, endAnchor, anchor);
754
+ const owner = getCurrentInstance();
755
+ let disposeEffect = () => {};
756
+ const block = {
757
+ type: "reactive-block",
758
+ dispose: () => disposeEffect(),
759
+ children: [],
760
+ startAnchor,
761
+ endAnchor
762
+ };
763
+ if (isShowElement(element)) {
764
+ disposeEffect = renderShowBlock(element, block, parent, namespace, owner);
765
+ } else if (isForElement(element)) {
766
+ disposeEffect = renderForBlock(element, block, parent, namespace, owner);
767
+ }
768
+ return block;
769
+ }
770
+ function renderShowBlock(element, block, parent, namespace, owner) {
771
+ let initialized = false;
772
+ return effect(() => {
773
+ clearChildren(block);
774
+ const when = readReactive(element.props.when);
775
+ const content = withOptionalInstance(owner, () => when ? resolveShowChildren(element, when) : element.props.fallback);
776
+ block.children = renderBlockContent(content, parent, block.endAnchor, namespace, owner);
777
+ if (initialized) {
778
+ fireMountedAndQueueUpdated(owner);
779
+ }
780
+ initialized = true;
781
+ });
782
+ }
783
+ function renderForBlock(element, block, parent, namespace, owner) {
784
+ let initialized = false;
785
+ let records = [];
786
+ return effect(() => {
787
+ const props = element.props;
788
+ const items = readReactive(props.each);
789
+ const list = Array.isArray(items) ? items : [];
790
+ const renderChild = props.children;
791
+ if (typeof renderChild !== "function") {
792
+ clearChildren(block);
793
+ records = [];
794
+ if (initialized) {
795
+ queueUpdatedHooks(owner);
796
+ }
797
+ initialized = true;
798
+ return;
799
+ }
800
+ const oldByKey = new Map;
801
+ for (const record of records) {
802
+ oldByKey.set(record.key, record);
803
+ }
804
+ const nextRecords = [];
805
+ list.forEach((item, index) => {
806
+ const key = props.key ? props.key(item, index) : item;
807
+ const old = oldByKey.get(key);
808
+ if (old && old.item === item) {
809
+ old.index = index;
810
+ moveBeforeEnd(parent, old.mounted, block.endAnchor);
811
+ nextRecords.push(old);
812
+ oldByKey.delete(key);
813
+ return;
814
+ }
815
+ if (old) {
816
+ removeMountedNode(old.mounted);
817
+ oldByKey.delete(key);
818
+ }
819
+ const record = {
820
+ key,
821
+ item,
822
+ index,
823
+ mounted: { type: "text", node: domOps.createTextNode("") }
824
+ };
825
+ record.mounted = withOptionalInstance(owner, () => renderNodeToDOM(renderChild(item, () => record.index), parent, block.endAnchor, namespace));
826
+ nextRecords.push(record);
827
+ });
828
+ for (const record of oldByKey.values()) {
829
+ removeMountedNode(record.mounted);
830
+ }
831
+ records = nextRecords;
832
+ block.children = nextRecords.map((record) => record.mounted);
833
+ if (initialized) {
834
+ fireMountedAndQueueUpdated(owner);
835
+ }
836
+ initialized = true;
837
+ });
838
+ }
839
+ function resolveShowChildren(element, value) {
840
+ const children = element.props.children ?? element.children;
841
+ if (typeof children === "function") {
842
+ return children(value);
843
+ }
844
+ return children;
845
+ }
846
+ function renderBlockContent(content, parent, anchor, namespace, owner) {
847
+ if (content == null || typeof content === "boolean") {
848
+ return [];
849
+ }
850
+ const nodes = Array.isArray(content) ? content : [content];
851
+ return nodes.map((node) => withOptionalInstance(owner, () => renderNodeToDOM(node, parent, anchor, namespace)));
852
+ }
853
+ function clearChildren(block) {
854
+ for (const child of block.children) {
855
+ removeMountedNode(child);
856
+ }
857
+ block.children = [];
858
+ }
859
+ function moveBeforeEnd(parent, mounted, endAnchor) {
860
+ for (const node of getMountedDomNodes(mounted)) {
861
+ domOps.insertBefore(parent, node, endAnchor);
862
+ }
863
+ }
864
+ function fireMountedAndQueueUpdated(owner) {
865
+ if (owner) {
866
+ fireMountedHooks(owner);
867
+ }
868
+ queueUpdatedHooks(owner);
869
+ }
870
+ function withOptionalInstance(owner, fn) {
871
+ return owner ? withInstance(owner, fn) : fn();
872
+ }
873
+ function readReactive(value) {
874
+ return isSignal(value) || isComputed(value) ? value.value : value;
875
+ }
876
+ function insertNode(parent, child, anchor) {
877
+ if (anchor) {
878
+ domOps.insertBefore(parent, child, anchor);
879
+ } else {
880
+ domOps.appendChild(parent, child);
881
+ }
882
+ }
883
+
884
+ // src/renderer/render-element.ts
885
+ var VOID_ELEMENTS2 = new Set([
886
+ "area",
887
+ "base",
888
+ "br",
889
+ "col",
890
+ "embed",
891
+ "hr",
892
+ "img",
893
+ "input",
894
+ "link",
895
+ "meta",
896
+ "param",
897
+ "source",
898
+ "track",
899
+ "wbr"
900
+ ]);
901
+ var SVG_NS = "http://www.w3.org/2000/svg";
902
+ var MATH_NS = "http://www.w3.org/1998/Math/MathML";
903
+ function renderElementToDOM(element, parent, anchor = null, namespace = null) {
904
+ const { tag, props, children } = element;
905
+ if (tag === "" || tag === Fragment) {
906
+ return renderFragmentToDOM(children, parent, anchor, namespace);
907
+ }
908
+ if (tag === Show || tag === For) {
909
+ return renderElementToDOM(tag(props), parent, anchor, namespace);
910
+ }
911
+ if (isShowElement(element) || isForElement(element)) {
912
+ return renderControlFlowToDOM(element, parent, anchor, namespace);
913
+ }
914
+ if (typeof tag === "function") {
915
+ return renderComponentToDOM(tag, props, parent, anchor, namespace);
916
+ }
917
+ if (typeof tag === "string") {
918
+ return renderIntrinsicToDOM(tag, props, children, parent, anchor, namespace);
919
+ }
920
+ return renderFragmentToDOM(children, parent, anchor, namespace);
921
+ }
922
+ function renderIntrinsicToDOM(tag, props, children, parent, anchor, parentNamespace) {
923
+ const namespace = getElementNamespace(tag, parentNamespace);
924
+ const el = namespace ? domOps.createElementNS(namespace, tag) : domOps.createElement(tag);
925
+ const attrDisposers = applyAttributes(el, props);
926
+ const eventCleanups = bindEvents(el, props);
927
+ let mountedChildren = [];
928
+ if (!VOID_ELEMENTS2.has(tag)) {
929
+ const dangerous = props.dangerouslySetInnerHTML;
930
+ if (dangerous && typeof dangerous.__html === "string") {
931
+ el.innerHTML = dangerous.__html;
932
+ } else {
933
+ mountedChildren = renderChildrenToDOM(children, el, getChildNamespace(tag, namespace));
934
+ }
935
+ }
936
+ if (anchor) {
937
+ domOps.insertBefore(parent, el, anchor);
938
+ } else {
939
+ domOps.appendChild(parent, el);
940
+ }
941
+ const refCleanup = applyRef(el, props.ref);
942
+ return {
943
+ type: "element",
944
+ node: el,
945
+ children: mountedChildren,
946
+ eventCleanups,
947
+ attrDisposers,
948
+ refCleanup
949
+ };
950
+ }
951
+ function renderComponentToDOM(component, props, parent, anchor, namespace) {
952
+ const parentInstance = getCurrentInstance();
953
+ const instance = createComponentInstance(component, props, parentInstance);
954
+ if (parentInstance) {
955
+ parentInstance.children.push(instance);
956
+ }
957
+ const prevInstance = setCurrentInstance(instance);
958
+ let result;
959
+ let child;
960
+ try {
961
+ result = component(props);
962
+ if (result && typeof result === "object" && "tag" in result) {
963
+ child = renderElementToDOM(result, parent, anchor, namespace);
964
+ } else {
965
+ child = renderNodeToDOM(result, parent, anchor, namespace);
966
+ }
967
+ } catch (err) {
968
+ setCurrentInstance(prevInstance);
969
+ handleComponentError(instance, err);
970
+ const text = domOps.createTextNode("");
971
+ if (anchor) {
972
+ domOps.insertBefore(parent, text, anchor);
973
+ } else {
974
+ domOps.appendChild(parent, text);
975
+ }
976
+ return {
977
+ type: "component",
978
+ children: [{ type: "text", node: text }],
979
+ disposers: [],
980
+ instance
981
+ };
982
+ }
983
+ setCurrentInstance(prevInstance);
984
+ instance.element = child;
985
+ return {
986
+ type: "component",
987
+ children: [child],
988
+ disposers: instance.effects,
989
+ instance
990
+ };
991
+ }
992
+ function renderFragmentToDOM(children, parent, anchor, namespace) {
993
+ const anchorComment = domOps.createComment("Sinwan-f");
994
+ if (anchor) {
995
+ domOps.insertBefore(parent, anchorComment, anchor);
996
+ } else {
997
+ domOps.appendChild(parent, anchorComment);
998
+ }
999
+ const mounted = [];
1000
+ for (const child of children) {
1001
+ mounted.push(renderNodeToDOM(child, parent, anchor, namespace));
1002
+ }
1003
+ return { type: "fragment", children: mounted, anchor: anchorComment };
1004
+ }
1005
+ function getElementNamespace(tag, parentNamespace) {
1006
+ if (tag === "svg")
1007
+ return SVG_NS;
1008
+ if (tag === "math")
1009
+ return MATH_NS;
1010
+ return parentNamespace;
1011
+ }
1012
+ function getChildNamespace(tag, namespace) {
1013
+ if (namespace === SVG_NS && tag === "foreignObject") {
1014
+ return null;
1015
+ }
1016
+ return namespace;
1017
+ }
1018
+ function applyRef(el, ref) {
1019
+ const value = ref;
1020
+ if (!value) {
1021
+ return null;
1022
+ }
1023
+ if (typeof value === "function") {
1024
+ value(el);
1025
+ return () => value(null);
1026
+ }
1027
+ if (typeof value === "object" && "current" in value) {
1028
+ value.current = el;
1029
+ return () => {
1030
+ value.current = null;
1031
+ };
1032
+ }
1033
+ return null;
1034
+ }
1035
+
1036
+ // src/renderer/render-children.ts
1037
+ function renderNodeToDOM(node, parent, anchor = null, namespace = null) {
1038
+ if (node == null || typeof node === "boolean") {
1039
+ const text2 = domOps.createTextNode("");
1040
+ insertNode2(parent, text2, anchor);
1041
+ return { type: "text", node: text2 };
1042
+ }
1043
+ if (typeof node === "string") {
1044
+ const text2 = domOps.createTextNode(node);
1045
+ insertNode2(parent, text2, anchor);
1046
+ return { type: "text", node: text2 };
1047
+ }
1048
+ if (typeof node === "number") {
1049
+ const text2 = domOps.createTextNode(String(node));
1050
+ insertNode2(parent, text2, anchor);
1051
+ return { type: "text", node: text2 };
1052
+ }
1053
+ if (node instanceof HtmlEscapedString) {
1054
+ const text2 = domOps.createTextNode(node.value);
1055
+ insertNode2(parent, text2, anchor);
1056
+ return { type: "text", node: text2 };
1057
+ }
1058
+ if (isSignal(node) || isComputed(node)) {
1059
+ const text2 = domOps.createTextNode(String(node.value));
1060
+ insertNode2(parent, text2, anchor);
1061
+ const owner = getCurrentInstance();
1062
+ let initialized = false;
1063
+ const dispose = effect(() => {
1064
+ domOps.setTextContent(text2, String(node.value));
1065
+ if (initialized) {
1066
+ queueUpdatedHooks(owner);
1067
+ }
1068
+ initialized = true;
1069
+ });
1070
+ return { type: "reactive-text", node: text2, dispose };
1071
+ }
1072
+ if (Array.isArray(node)) {
1073
+ return renderArrayToDOM(node, parent, anchor, namespace);
1074
+ }
1075
+ if (node instanceof Promise) {
1076
+ const placeholder = domOps.createTextNode("");
1077
+ insertNode2(parent, placeholder, anchor);
1078
+ node.then((resolved) => {
1079
+ const mounted = renderNodeToDOM(resolved, parent, placeholder, namespace);
1080
+ domOps.remove(placeholder);
1081
+ });
1082
+ return { type: "text", node: placeholder };
1083
+ }
1084
+ if (typeof node === "object" && "tag" in node) {
1085
+ return renderElementToDOM(node, parent, anchor, namespace);
1086
+ }
1087
+ const text = domOps.createTextNode(String(node));
1088
+ insertNode2(parent, text, anchor);
1089
+ return { type: "text", node: text };
1090
+ }
1091
+ function renderArrayToDOM(nodes, parent, anchor, namespace) {
1092
+ const anchorComment = domOps.createComment("Sinwan-f");
1093
+ insertNode2(parent, anchorComment, anchor);
1094
+ const children = [];
1095
+ for (const child of nodes) {
1096
+ children.push(renderNodeToDOM(child, parent, anchor, namespace));
1097
+ }
1098
+ return { type: "fragment", children, anchor: anchorComment };
1099
+ }
1100
+ function renderChildrenToDOM(children, parent, namespace = null) {
1101
+ const mounted = [];
1102
+ for (const child of children) {
1103
+ mounted.push(renderNodeToDOM(child, parent, null, namespace));
1104
+ }
1105
+ return mounted;
1106
+ }
1107
+ function insertNode2(parent, child, anchor) {
1108
+ if (anchor) {
1109
+ domOps.insertBefore(parent, child, anchor);
1110
+ } else {
1111
+ domOps.appendChild(parent, child);
1112
+ }
1113
+ }
1114
+
1115
+ // src/renderer/mount.ts
1116
+ function mount(component, container, props) {
1117
+ container.innerHTML = "";
1118
+ const mergedProps = props ?? {};
1119
+ const instance = createComponentInstance(component, mergedProps, null);
1120
+ let result;
1121
+ let root;
1122
+ setCurrentInstance(instance);
1123
+ try {
1124
+ result = component(mergedProps);
1125
+ if (result instanceof Promise) {
1126
+ const placeholder = document.createTextNode("");
1127
+ container.appendChild(placeholder);
1128
+ root = { type: "text", node: placeholder };
1129
+ result.then((resolved) => {
1130
+ container.innerHTML = "";
1131
+ setCurrentInstance(instance);
1132
+ root = renderElementToDOM(resolved, container);
1133
+ setCurrentInstance(null);
1134
+ instance.element = root;
1135
+ fireMountedHooks(instance);
1136
+ });
1137
+ } else if (result && typeof result === "object" && "tag" in result) {
1138
+ root = renderElementToDOM(result, container);
1139
+ } else {
1140
+ root = renderNodeToDOM(result, container);
1141
+ }
1142
+ } catch (err) {
1143
+ setCurrentInstance(null);
1144
+ handleComponentError(instance, err);
1145
+ return {
1146
+ root: { type: "text", node: document.createTextNode("") },
1147
+ unmount() {}
1148
+ };
1149
+ }
1150
+ setCurrentInstance(null);
1151
+ instance.element = root;
1152
+ fireMountedHooks(instance);
1153
+ return {
1154
+ root,
1155
+ unmount() {
1156
+ fireUnmountedHooks(instance);
1157
+ unmountNode(root);
1158
+ container.innerHTML = "";
1159
+ }
1160
+ };
1161
+ }
1162
+ function render(node, container) {
1163
+ container.innerHTML = "";
1164
+ const root = renderNodeToDOM(node, container);
1165
+ return {
1166
+ root,
1167
+ unmount() {
1168
+ unmountNode(root);
1169
+ container.innerHTML = "";
1170
+ }
1171
+ };
1172
+ }
1173
+ // src/server/index.ts
1174
+ var exports_server = {};
1175
+ __export(exports_server, {
1176
+ streamPage: () => streamPage,
1177
+ streamHydratablePage: () => streamHydratablePage,
1178
+ streamHydratableNode: () => streamHydratableNode,
1179
+ renderToString: () => renderToString,
1180
+ renderToHydratableString: () => renderToHydratableString,
1181
+ renderPage: () => renderPage,
1182
+ renderNodeToHydratableString: () => renderNodeToHydratableString,
1183
+ registerPage: () => registerPage,
1184
+ isSlots: () => isSlots,
1185
+ hasPage: () => hasPage,
1186
+ getPage: () => getPage
1187
+ });
1188
+ module.exports = __toCommonJS(exports_server);
1189
+
1190
+ // src/escaper.ts
1191
+ var _bun = globalThis.Bun;
1192
+ var _nativeEscape = typeof _bun?.escapeHTML === "function" ? _bun.escapeHTML.bind(_bun) : undefined;
1193
+ var HTML_ESCAPE_RE = /[&<>"']/g;
1194
+ var HTML_ESCAPE_MAP = {
1195
+ "&": "&amp;",
1196
+ "<": "&lt;",
1197
+ ">": "&gt;",
1198
+ '"': "&quot;",
1199
+ "'": "&#39;"
1200
+ };
1201
+ function portableEscape(str) {
1202
+ HTML_ESCAPE_RE.lastIndex = 0;
1203
+ if (!HTML_ESCAPE_RE.test(str))
1204
+ return str;
1205
+ return str.replace(HTML_ESCAPE_RE, (c) => HTML_ESCAPE_MAP[c]);
1206
+ }
1207
+ function escapeHtml(value) {
1208
+ if (value == null || typeof value === "boolean")
1209
+ return "";
1210
+ if (typeof value === "number")
1211
+ return String(value);
1212
+ if (value instanceof HtmlEscapedString)
1213
+ return value.value;
1214
+ const s = String(value);
1215
+ return _nativeEscape ? _nativeEscape(s) : portableEscape(s);
1216
+ }
1217
+ function safeHtml(html) {
1218
+ return raw(html);
1219
+ }
1220
+ function isSafeHtml(value) {
1221
+ return value instanceof HtmlEscapedString;
1222
+ }
1223
+
1224
+ // src/server/renderer.ts
1225
+ var componentCache = new WeakMap;
1226
+ var pageRegistry = new Map;
1227
+ function registerPage(name, page) {
1228
+ pageRegistry.set(name, page);
1229
+ }
1230
+ function getPage(name) {
1231
+ return pageRegistry.get(name);
1232
+ }
1233
+ function hasPage(name) {
1234
+ return pageRegistry.has(name);
1235
+ }
1236
+ async function renderPage(name, data) {
1237
+ const page = getPage(name);
1238
+ if (!page) {
1239
+ throw new Error(`Page "${name}" not found in registry`);
1240
+ }
1241
+ const element = await page(data);
1242
+ return renderToString(element);
1243
+ }
1244
+ async function renderToString(node) {
1245
+ if (node == null || typeof node === "boolean") {
1246
+ return "";
1247
+ }
1248
+ if (typeof node === "string") {
1249
+ return escapeHtml(node);
1250
+ }
1251
+ if (typeof node === "number") {
1252
+ return String(node);
1253
+ }
1254
+ if (node instanceof HtmlEscapedString) {
1255
+ return node.value;
1256
+ }
1257
+ if (isSignal(node) || isComputed(node)) {
1258
+ return escapeHtml(String(node.value));
1259
+ }
1260
+ if (Array.isArray(node)) {
1261
+ const results = await Promise.all(node.map((child) => renderToString(child)));
1262
+ return results.join("");
1263
+ }
1264
+ if (node instanceof Promise) {
1265
+ return renderElement(await node);
1266
+ }
1267
+ return renderElement(node);
1268
+ }
1269
+ async function renderElement(element) {
1270
+ const { tag, props, children } = element;
1271
+ if (isShowElement(element)) {
1272
+ const when = readReactive2(props.when);
1273
+ return renderToString(when ? resolveShowChildren2(element, when) : props.fallback);
1274
+ }
1275
+ if (isForElement(element)) {
1276
+ return renderForElement(element);
1277
+ }
1278
+ if (typeof tag === "function") {
1279
+ const result = await tag(props);
1280
+ return renderToString(result);
1281
+ }
1282
+ if (typeof tag === "string") {
1283
+ return renderIntrinsicElement(tag, props, children);
1284
+ }
1285
+ return renderToString(children);
1286
+ }
1287
+ var VOID_ELEMENTS3 = new Set([
1288
+ "area",
1289
+ "base",
1290
+ "br",
1291
+ "col",
1292
+ "embed",
1293
+ "hr",
1294
+ "img",
1295
+ "input",
1296
+ "link",
1297
+ "meta",
1298
+ "param",
1299
+ "source",
1300
+ "track",
1301
+ "wbr"
1302
+ ]);
1303
+ async function renderIntrinsicElement(tag, props, children) {
1304
+ const attrs = renderAttributes(props);
1305
+ if (VOID_ELEMENTS3.has(tag)) {
1306
+ return attrs ? `<${tag}${attrs}>` : `<${tag}>`;
1307
+ }
1308
+ const childrenHtml = await renderChildren(children, props);
1309
+ return attrs ? `<${tag}${attrs}>${childrenHtml}</${tag}>` : `<${tag}>${childrenHtml}</${tag}>`;
1310
+ }
1311
+ function renderAttributes(props) {
1312
+ let attrs = "";
1313
+ for (const [key, value] of Object.entries(props)) {
1314
+ if (key === "children" || key === "key" || key === "ref" || key === "dangerouslySetInnerHTML" || isEventProp(key)) {
1315
+ continue;
1316
+ }
1317
+ const resolvedValue = readReactive2(value);
1318
+ if (resolvedValue == null || resolvedValue === false)
1319
+ continue;
1320
+ const attrName = key === "className" ? "class" : key;
1321
+ const finalName = attrName === "htmlFor" ? "for" : attrName;
1322
+ if (resolvedValue === true) {
1323
+ attrs += ` ${finalName}`;
1324
+ continue;
1325
+ }
1326
+ const attrValue = escapeHtml(String(resolvedValue));
1327
+ attrs += ` ${finalName}="${attrValue}"`;
1328
+ }
1329
+ return attrs;
1330
+ }
1331
+ async function renderChildren(children, props) {
1332
+ const dangerous = props.dangerouslySetInnerHTML;
1333
+ if (dangerous && typeof dangerous.__html === "string") {
1334
+ return dangerous.__html;
1335
+ }
1336
+ return renderToString(children);
1337
+ }
1338
+ function isSlots(children) {
1339
+ return children != null && typeof children === "object" && !Array.isArray(children) && !(children instanceof HtmlEscapedString);
1340
+ }
1341
+ async function renderForElement(element) {
1342
+ const props = element.props;
1343
+ const each = readReactive2(props.each);
1344
+ if (!Array.isArray(each) || typeof props.children !== "function") {
1345
+ return "";
1346
+ }
1347
+ const rendered = await Promise.all(each.map((item, index) => renderToString(props.children(item, () => index))));
1348
+ return rendered.join("");
1349
+ }
1350
+ function resolveShowChildren2(element, value) {
1351
+ const children = element.props.children ?? element.children;
1352
+ if (typeof children === "function") {
1353
+ return children(value);
1354
+ }
1355
+ return children;
1356
+ }
1357
+ function readReactive2(value) {
1358
+ return isSignal(value) || isComputed(value) ? value.value : value;
1359
+ }
1360
+ // src/hydration/markers.ts
1361
+ var COMP_ID_ATTR = "data-sinwan-id";
1362
+ var COMP_ID_PREFIX = "c";
1363
+ var TEXT_MARKER_OPEN = "sinwan-t:";
1364
+ var TEXT_MARKER_CLOSE = "/sinwan-t";
1365
+ var EVENT_ATTR = "data-sinwan-ev";
1366
+ function compId(index) {
1367
+ return `${COMP_ID_PREFIX}${index}`;
1368
+ }
1369
+ function textMarkerOpen(index) {
1370
+ return `<!--${TEXT_MARKER_OPEN}${index}-->`;
1371
+ }
1372
+ function textMarkerCloseStr() {
1373
+ return `<!--${TEXT_MARKER_CLOSE}-->`;
1374
+ }
1375
+ function parseTextOpenMarker(node) {
1376
+ const data = node.data;
1377
+ if (data.startsWith(TEXT_MARKER_OPEN)) {
1378
+ const idx = parseInt(data.slice(TEXT_MARKER_OPEN.length), 10);
1379
+ return Number.isNaN(idx) ? -1 : idx;
1380
+ }
1381
+ return -1;
1382
+ }
1383
+ function isTextCloseMarker(node) {
1384
+ return node.data === TEXT_MARKER_CLOSE;
1385
+ }
1386
+
1387
+ // src/server/stream.ts
1388
+ function createHydratableStreamContext() {
1389
+ return { componentIndex: 0, textIndex: 0, eventIndex: 0 };
1390
+ }
1391
+ var VOID_ELEMENTS4 = new Set([
1392
+ "area",
1393
+ "base",
1394
+ "br",
1395
+ "col",
1396
+ "embed",
1397
+ "hr",
1398
+ "img",
1399
+ "input",
1400
+ "link",
1401
+ "meta",
1402
+ "param",
1403
+ "source",
1404
+ "track",
1405
+ "wbr"
1406
+ ]);
1407
+ function streamPage(page, data) {
1408
+ const encoder = new TextEncoder;
1409
+ return new ReadableStream({
1410
+ async start(controller) {
1411
+ try {
1412
+ const element = await page(data);
1413
+ await streamNode(element, controller, encoder);
1414
+ controller.close();
1415
+ } catch (error) {
1416
+ controller.error(error);
1417
+ }
1418
+ }
1419
+ });
1420
+ }
1421
+ function streamHydratablePage(component, props) {
1422
+ const encoder = new TextEncoder;
1423
+ const ctx = createHydratableStreamContext();
1424
+ return new ReadableStream({
1425
+ async start(controller) {
1426
+ try {
1427
+ await streamHydratableComponent(component, props ?? {}, controller, encoder, ctx, true);
1428
+ controller.close();
1429
+ } catch (error) {
1430
+ controller.error(error);
1431
+ }
1432
+ }
1433
+ });
1434
+ }
1435
+ function streamHydratableNode(node) {
1436
+ const encoder = new TextEncoder;
1437
+ const ctx = createHydratableStreamContext();
1438
+ return new ReadableStream({
1439
+ async start(controller) {
1440
+ try {
1441
+ await streamHydratableNodeToController(node, controller, encoder, ctx);
1442
+ controller.close();
1443
+ } catch (error) {
1444
+ controller.error(error);
1445
+ }
1446
+ }
1447
+ });
1448
+ }
1449
+ async function streamNode(node, controller, encoder) {
1450
+ if (node == null || typeof node === "boolean") {
1451
+ return;
1452
+ }
1453
+ if (typeof node === "string") {
1454
+ controller.enqueue(encoder.encode(escapeHtml(node)));
1455
+ return;
1456
+ }
1457
+ if (typeof node === "number") {
1458
+ controller.enqueue(encoder.encode(String(node)));
1459
+ return;
1460
+ }
1461
+ if (node instanceof HtmlEscapedString) {
1462
+ controller.enqueue(encoder.encode(node.value));
1463
+ return;
1464
+ }
1465
+ if (isSignal(node) || isComputed(node)) {
1466
+ controller.enqueue(encoder.encode(escapeHtml(String(node.value))));
1467
+ return;
1468
+ }
1469
+ if (Array.isArray(node)) {
1470
+ for (const child of node) {
1471
+ await streamNode(child, controller, encoder);
1472
+ }
1473
+ return;
1474
+ }
1475
+ if (node instanceof Promise) {
1476
+ const resolved = await node;
1477
+ await streamElement(resolved, controller, encoder);
1478
+ return;
1479
+ }
1480
+ await streamElement(node, controller, encoder);
1481
+ }
1482
+ async function streamElement(element, controller, encoder) {
1483
+ const { tag, props, children } = element;
1484
+ if (isShowElement(element)) {
1485
+ const when = readReactive3(props.when);
1486
+ await streamNode(when ? resolveShowChildren3(element, when) : props.fallback, controller, encoder);
1487
+ return;
1488
+ }
1489
+ if (isForElement(element)) {
1490
+ await streamForElement(element, controller, encoder);
1491
+ return;
1492
+ }
1493
+ if (typeof tag === "function") {
1494
+ const result = await tag(props);
1495
+ await streamNode(result, controller, encoder);
1496
+ return;
1497
+ }
1498
+ if (typeof tag === "string") {
1499
+ await streamIntrinsicElement(tag, props, children, controller, encoder);
1500
+ return;
1501
+ }
1502
+ await streamNode(children, controller, encoder);
1503
+ }
1504
+ async function streamIntrinsicElement(tag, props, children, controller, encoder) {
1505
+ const attrs = renderAttributes2(props);
1506
+ const dangerous = props.dangerouslySetInnerHTML;
1507
+ if (VOID_ELEMENTS4.has(tag)) {
1508
+ const html = attrs ? `<${tag}${attrs}>` : `<${tag}>`;
1509
+ controller.enqueue(encoder.encode(html));
1510
+ return;
1511
+ }
1512
+ const openTag = attrs ? `<${tag}${attrs}>` : `<${tag}>`;
1513
+ controller.enqueue(encoder.encode(openTag));
1514
+ if (dangerous && typeof dangerous.__html === "string") {
1515
+ controller.enqueue(encoder.encode(dangerous.__html));
1516
+ } else {
1517
+ await streamNode(children, controller, encoder);
1518
+ }
1519
+ controller.enqueue(encoder.encode(`</${tag}>`));
1520
+ }
1521
+ function renderAttributes2(props) {
1522
+ let attrs = "";
1523
+ for (const [key, value] of Object.entries(props)) {
1524
+ if (key === "children" || key === "key" || key === "ref" || key === "dangerouslySetInnerHTML" || isEventProp(key)) {
1525
+ continue;
1526
+ }
1527
+ const resolvedValue = readReactive3(value);
1528
+ if (resolvedValue == null || resolvedValue === false)
1529
+ continue;
1530
+ const attrName = key === "className" ? "class" : key === "htmlFor" ? "for" : key;
1531
+ if (resolvedValue === true) {
1532
+ attrs += ` ${attrName}`;
1533
+ continue;
1534
+ }
1535
+ const attrValue = escapeHtml(String(resolvedValue));
1536
+ attrs += ` ${attrName}="${attrValue}"`;
1537
+ }
1538
+ return attrs;
1539
+ }
1540
+ async function streamHydratableNodeToController(node, controller, encoder, ctx, isComponentRoot = false) {
1541
+ if (node == null || typeof node === "boolean") {
1542
+ return;
1543
+ }
1544
+ if (typeof node === "string") {
1545
+ enqueue(controller, encoder, escapeHtml(node));
1546
+ return;
1547
+ }
1548
+ if (typeof node === "number") {
1549
+ enqueue(controller, encoder, String(node));
1550
+ return;
1551
+ }
1552
+ if (node instanceof HtmlEscapedString) {
1553
+ enqueue(controller, encoder, node.value);
1554
+ return;
1555
+ }
1556
+ if (isSignal(node) || isComputed(node)) {
1557
+ const idx = ctx.textIndex++;
1558
+ enqueue(controller, encoder, `${textMarkerOpen(idx)}${escapeHtml(String(node.value))}${textMarkerCloseStr()}`);
1559
+ return;
1560
+ }
1561
+ if (Array.isArray(node)) {
1562
+ for (const child of node) {
1563
+ await streamHydratableNodeToController(child, controller, encoder, ctx);
1564
+ }
1565
+ return;
1566
+ }
1567
+ if (node instanceof Promise) {
1568
+ await streamHydratableElement(await node, controller, encoder, ctx, isComponentRoot);
1569
+ return;
1570
+ }
1571
+ await streamHydratableElement(node, controller, encoder, ctx, isComponentRoot);
1572
+ }
1573
+ async function streamHydratableElement(element, controller, encoder, ctx, isComponentRoot) {
1574
+ const { tag, props, children } = element;
1575
+ if (tag === "") {
1576
+ for (const child of children) {
1577
+ await streamHydratableNodeToController(child, controller, encoder, ctx);
1578
+ }
1579
+ return;
1580
+ }
1581
+ if (tag === Show || tag === For) {
1582
+ await streamHydratableElement(tag(props), controller, encoder, ctx, isComponentRoot);
1583
+ return;
1584
+ }
1585
+ if (isShowElement(element)) {
1586
+ const when = readReactive3(props.when);
1587
+ await streamHydratableNodeToController(when ? resolveShowChildren3(element, when) : props.fallback, controller, encoder, ctx, isComponentRoot);
1588
+ return;
1589
+ }
1590
+ if (isForElement(element)) {
1591
+ await streamHydratableForElement(element, controller, encoder, ctx);
1592
+ return;
1593
+ }
1594
+ if (typeof tag === "function") {
1595
+ await streamHydratableComponent(tag, props, controller, encoder, ctx, true);
1596
+ return;
1597
+ }
1598
+ if (typeof tag === "string") {
1599
+ await streamHydratableIntrinsic(tag, props, children, controller, encoder, ctx, isComponentRoot);
1600
+ return;
1601
+ }
1602
+ await streamHydratableNodeToController(children, controller, encoder, ctx);
1603
+ }
1604
+ async function streamHydratableComponent(component, props, controller, encoder, ctx, isComponentRoot) {
1605
+ const parentInstance = getCurrentInstance();
1606
+ const instance = createComponentInstance(component, props, parentInstance);
1607
+ if (parentInstance) {
1608
+ parentInstance.children.push(instance);
1609
+ }
1610
+ const prev = setCurrentInstance(instance);
1611
+ try {
1612
+ const result = await component(props);
1613
+ await streamHydratableNodeToController(result, controller, encoder, ctx, isComponentRoot);
1614
+ } finally {
1615
+ setCurrentInstance(prev);
1616
+ }
1617
+ }
1618
+ async function streamHydratableIntrinsic(tag, props, children, controller, encoder, ctx, isComponentRoot) {
1619
+ const attrs = renderHydratableAttributes(props, ctx, isComponentRoot);
1620
+ const dangerous = props.dangerouslySetInnerHTML;
1621
+ enqueue(controller, encoder, attrs ? `<${tag}${attrs}>` : `<${tag}>`);
1622
+ if (VOID_ELEMENTS4.has(tag)) {
1623
+ return;
1624
+ }
1625
+ if (dangerous && typeof dangerous.__html === "string") {
1626
+ enqueue(controller, encoder, dangerous.__html);
1627
+ } else {
1628
+ for (const child of children) {
1629
+ await streamHydratableNodeToController(child, controller, encoder, ctx);
1630
+ }
1631
+ }
1632
+ enqueue(controller, encoder, `</${tag}>`);
1633
+ }
1634
+ async function streamHydratableForElement(element, controller, encoder, ctx) {
1635
+ const props = element.props;
1636
+ const each = readReactive3(props.each);
1637
+ if (!Array.isArray(each) || typeof props.children !== "function") {
1638
+ return;
1639
+ }
1640
+ for (let index = 0;index < each.length; index++) {
1641
+ await streamHydratableNodeToController(props.children(each[index], () => index), controller, encoder, ctx);
1642
+ }
1643
+ }
1644
+ function renderHydratableAttributes(props, ctx, isComponentRoot) {
1645
+ let attrs = "";
1646
+ if (isComponentRoot) {
1647
+ attrs += ` ${COMP_ID_ATTR}="${compId(ctx.componentIndex++)}"`;
1648
+ }
1649
+ const eventParts = [];
1650
+ for (const [key, value] of Object.entries(props)) {
1651
+ if (key === "children" || key === "key" || key === "ref" || key === "dangerouslySetInnerHTML") {
1652
+ continue;
1653
+ }
1654
+ if (isEventProp(key)) {
1655
+ eventParts.push(`${toEventName(key)}:${ctx.eventIndex++}`);
1656
+ continue;
1657
+ }
1658
+ const resolvedValue = readReactive3(value);
1659
+ if (resolvedValue == null || resolvedValue === false)
1660
+ continue;
1661
+ const attrName = key === "className" ? "class" : key === "htmlFor" ? "for" : key;
1662
+ if (resolvedValue === true) {
1663
+ attrs += ` ${attrName}`;
1664
+ continue;
1665
+ }
1666
+ attrs += ` ${attrName}="${escapeHtml(String(resolvedValue))}"`;
1667
+ }
1668
+ if (eventParts.length > 0) {
1669
+ attrs += ` ${EVENT_ATTR}="${eventParts.join(",")}"`;
1670
+ }
1671
+ return attrs;
1672
+ }
1673
+ function enqueue(controller, encoder, html) {
1674
+ controller.enqueue(encoder.encode(html));
1675
+ }
1676
+ async function streamForElement(element, controller, encoder) {
1677
+ const props = element.props;
1678
+ const each = readReactive3(props.each);
1679
+ if (!Array.isArray(each) || typeof props.children !== "function") {
1680
+ return;
1681
+ }
1682
+ for (let index = 0;index < each.length; index++) {
1683
+ await streamNode(props.children(each[index], () => index), controller, encoder);
1684
+ }
1685
+ }
1686
+ function resolveShowChildren3(element, value) {
1687
+ const children = element.props.children ?? element.children;
1688
+ if (typeof children === "function") {
1689
+ return children(value);
1690
+ }
1691
+ return children;
1692
+ }
1693
+ function readReactive3(value) {
1694
+ return isSignal(value) || isComputed(value) ? value.value : value;
1695
+ }
1696
+ // src/server/hydration-markers.ts
1697
+ function createHydrationContext() {
1698
+ return { componentIndex: 0, textIndex: 0, eventIndex: 0 };
1699
+ }
1700
+ async function renderToHydratableString(component, props) {
1701
+ const ctx = createHydrationContext();
1702
+ const mergedProps = props ?? {};
1703
+ const instance = createComponentInstance(component, mergedProps, null);
1704
+ const prev = setCurrentInstance(instance);
1705
+ try {
1706
+ const result = await component(mergedProps);
1707
+ if (result && typeof result === "object" && "tag" in result) {
1708
+ return renderElementH(result, ctx, true);
1709
+ }
1710
+ return renderNodeH(result, ctx);
1711
+ } finally {
1712
+ setCurrentInstance(prev);
1713
+ }
1714
+ }
1715
+ async function renderNodeToHydratableString(node) {
1716
+ const ctx = createHydrationContext();
1717
+ return renderNodeH(node, ctx);
1718
+ }
1719
+ var VOID_ELEMENTS5 = new Set([
1720
+ "area",
1721
+ "base",
1722
+ "br",
1723
+ "col",
1724
+ "embed",
1725
+ "hr",
1726
+ "img",
1727
+ "input",
1728
+ "link",
1729
+ "meta",
1730
+ "param",
1731
+ "source",
1732
+ "track",
1733
+ "wbr"
1734
+ ]);
1735
+ function renderNodeH(node, ctx) {
1736
+ if (node == null || typeof node === "boolean")
1737
+ return "";
1738
+ if (typeof node === "string")
1739
+ return escapeHtml(node);
1740
+ if (typeof node === "number")
1741
+ return String(node);
1742
+ if (node instanceof HtmlEscapedString)
1743
+ return node.value;
1744
+ if (isSignal(node) || isComputed(node)) {
1745
+ const value = node.value;
1746
+ const idx = ctx.textIndex++;
1747
+ return `${textMarkerOpen(idx)}${escapeHtml(String(value))}${textMarkerCloseStr()}`;
1748
+ }
1749
+ if (Array.isArray(node)) {
1750
+ return node.map((child) => renderNodeH(child, ctx)).join("");
1751
+ }
1752
+ if (node instanceof Promise) {
1753
+ return "";
1754
+ }
1755
+ if (typeof node === "object" && "tag" in node) {
1756
+ return renderElementH(node, ctx, false);
1757
+ }
1758
+ return escapeHtml(String(node));
1759
+ }
1760
+ function renderElementH(element, ctx, isComponentRoot) {
1761
+ const { tag, props, children } = element;
1762
+ if (tag === "") {
1763
+ return children.map((child) => renderNodeH(child, ctx)).join("");
1764
+ }
1765
+ if (tag === Show || tag === For) {
1766
+ return renderElementH(tag(props), ctx, isComponentRoot);
1767
+ }
1768
+ if (isShowElement(element)) {
1769
+ const when = readReactive4(props.when);
1770
+ const content = when ? resolveShowChildren4(element, when) : props.fallback;
1771
+ return renderNodeMaybeRoot(content, ctx, isComponentRoot);
1772
+ }
1773
+ if (isForElement(element)) {
1774
+ return renderForElementH(element, ctx);
1775
+ }
1776
+ if (typeof tag === "function") {
1777
+ return renderComponentH(tag, props, ctx);
1778
+ }
1779
+ if (typeof tag === "string") {
1780
+ return renderIntrinsicH(tag, props, children, ctx, isComponentRoot);
1781
+ }
1782
+ return children.map((child) => renderNodeH(child, ctx)).join("");
1783
+ }
1784
+ function renderComponentH(component, props, ctx) {
1785
+ const parentInstance = getCurrentInstance();
1786
+ const instance = createComponentInstance(component, props, parentInstance);
1787
+ if (parentInstance) {
1788
+ parentInstance.children.push(instance);
1789
+ }
1790
+ const prev = setCurrentInstance(instance);
1791
+ try {
1792
+ const result = component(props);
1793
+ if (result && typeof result === "object" && "tag" in result) {
1794
+ return renderElementH(result, ctx, true);
1795
+ }
1796
+ return renderNodeH(result, ctx);
1797
+ } finally {
1798
+ setCurrentInstance(prev);
1799
+ }
1800
+ }
1801
+ function renderIntrinsicH(tag, props, children, ctx, isComponentRoot) {
1802
+ let attrs = "";
1803
+ if (isComponentRoot) {
1804
+ attrs += ` ${COMP_ID_ATTR}="${compId(ctx.componentIndex++)}"`;
1805
+ }
1806
+ const eventParts = [];
1807
+ for (const [key, value] of Object.entries(props)) {
1808
+ if (key === "children" || key === "key" || key === "ref" || key === "dangerouslySetInnerHTML") {
1809
+ continue;
1810
+ }
1811
+ if (isEventProp(key)) {
1812
+ const eventName = toEventName(key);
1813
+ eventParts.push(`${eventName}:${ctx.eventIndex++}`);
1814
+ continue;
1815
+ }
1816
+ if (value == null || value === false)
1817
+ continue;
1818
+ let resolvedValue = value;
1819
+ if (isSignal(value) || isComputed(value)) {
1820
+ resolvedValue = value.value;
1821
+ }
1822
+ if (resolvedValue === true) {
1823
+ const attrName2 = key === "className" ? "class" : key === "htmlFor" ? "for" : key;
1824
+ attrs += ` ${attrName2}`;
1825
+ continue;
1826
+ }
1827
+ const attrName = key === "className" ? "class" : key === "htmlFor" ? "for" : key;
1828
+ attrs += ` ${attrName}="${escapeHtml(String(resolvedValue))}"`;
1829
+ }
1830
+ if (eventParts.length > 0) {
1831
+ attrs += ` ${EVENT_ATTR}="${eventParts.join(",")}"`;
1832
+ }
1833
+ if (VOID_ELEMENTS5.has(tag)) {
1834
+ return `<${tag}${attrs}>`;
1835
+ }
1836
+ const dangerous = props.dangerouslySetInnerHTML;
1837
+ if (dangerous && typeof dangerous.__html === "string") {
1838
+ return `<${tag}${attrs}>${dangerous.__html}</${tag}>`;
1839
+ }
1840
+ const childrenHtml = children.map((child) => renderNodeH(child, ctx)).join("");
1841
+ return `<${tag}${attrs}>${childrenHtml}</${tag}>`;
1842
+ }
1843
+ function renderNodeMaybeRoot(node, ctx, isComponentRoot) {
1844
+ if (isComponentRoot && node && typeof node === "object" && !Array.isArray(node) && "tag" in node) {
1845
+ return renderElementH(node, ctx, true);
1846
+ }
1847
+ return renderNodeH(node, ctx);
1848
+ }
1849
+ function renderForElementH(element, ctx) {
1850
+ const props = element.props;
1851
+ const each = readReactive4(props.each);
1852
+ if (!Array.isArray(each) || typeof props.children !== "function") {
1853
+ return "";
1854
+ }
1855
+ return each.map((item, index) => renderNodeH(props.children(item, () => index), ctx)).join("");
1856
+ }
1857
+ function resolveShowChildren4(element, value) {
1858
+ const children = element.props.children ?? element.children;
1859
+ if (typeof children === "function") {
1860
+ return children(value);
1861
+ }
1862
+ return children;
1863
+ }
1864
+ function readReactive4(value) {
1865
+ return isSignal(value) || isComputed(value) ? value.value : value;
1866
+ }
1867
+ // src/index.ts
1868
+ var exports_src = {};
1869
+ __export(exports_src, {
1870
+ unmountNode: () => unmountNode,
1871
+ streamPage: () => streamPage,
1872
+ streamHydratablePage: () => streamHydratablePage,
1873
+ streamHydratableNode: () => streamHydratableNode,
1874
+ signal: () => signal,
1875
+ setDOMOps: () => setDOMOps,
1876
+ safeHtml: () => safeHtml,
1877
+ resetDOMOps: () => resetDOMOps,
1878
+ renderToString: () => renderToString,
1879
+ renderToHydratableString: () => renderToHydratableString,
1880
+ renderPage: () => renderPage,
1881
+ renderNodeToHydratableString: () => renderNodeToHydratableString,
1882
+ renderNodeToDOM: () => renderNodeToDOM,
1883
+ renderElementToDOM: () => renderElementToDOM,
1884
+ render: () => render,
1885
+ registerPage: () => registerPage,
1886
+ raw: () => raw,
1887
+ provide: () => provide,
1888
+ onUpdated: () => onUpdated,
1889
+ onUnmounted: () => onUnmounted,
1890
+ onMounted: () => onMounted,
1891
+ onError: () => onError,
1892
+ nextTick: () => nextTick,
1893
+ mount: () => mount,
1894
+ jsxs: () => jsxs,
1895
+ jsxDEV: () => jsxDEV,
1896
+ jsx: () => jsx,
1897
+ isSignal: () => isSignal,
1898
+ isSafeHtml: () => isSafeHtml,
1899
+ isComputed: () => isComputed,
1900
+ inject: () => inject,
1901
+ hydrate: () => hydrate,
1902
+ hasPage: () => hasPage,
1903
+ getPage: () => getPage,
1904
+ getCurrentInstance: () => getCurrentInstance,
1905
+ escapeHtml: () => escapeHtml,
1906
+ effect: () => effect,
1907
+ domOps: () => domOps,
1908
+ createPage: () => createPage,
1909
+ createLayout: () => createLayout,
1910
+ createComponent: () => createComponent,
1911
+ computed: () => computed,
1912
+ batch: () => batch,
1913
+ Show: () => Show,
1914
+ HtmlEscapedString: () => HtmlEscapedString,
1915
+ Fragment: () => Fragment,
1916
+ For: () => For
1917
+ });
1918
+ module.exports = __toCommonJS(exports_src);
1919
+ // src/reactivity/batch.ts
1920
+ var batchDepth = 0;
1921
+ function batch(fn) {
1922
+ batchDepth++;
1923
+ try {
1924
+ fn();
1925
+ } finally {
1926
+ batchDepth--;
1927
+ if (batchDepth === 0) {
1928
+ flushSync();
1929
+ }
1930
+ }
1931
+ }
1932
+ // src/component/lifecycle.ts
1933
+ function onMounted(fn) {
1934
+ const instance = getCurrentInstance();
1935
+ if (!instance) {
1936
+ throw new Error("onMounted() called outside of component setup.");
1937
+ }
1938
+ instance._mountedHooks.push(() => withInstance(instance, fn));
1939
+ }
1940
+ function onUnmounted(fn) {
1941
+ const instance = getCurrentInstance();
1942
+ if (!instance) {
1943
+ throw new Error("onUnmounted() called outside of component setup.");
1944
+ }
1945
+ instance._unmountedHooks.push(() => withInstance(instance, fn));
1946
+ }
1947
+ function onUpdated(fn) {
1948
+ const instance = getCurrentInstance();
1949
+ if (!instance) {
1950
+ throw new Error("onUpdated() called outside of component setup.");
1951
+ }
1952
+ instance._updatedHooks.push(() => withInstance(instance, fn));
1953
+ }
1954
+ function onError(fn) {
1955
+ const instance = getCurrentInstance();
1956
+ if (!instance) {
1957
+ throw new Error("onError() called outside of component setup.");
1958
+ }
1959
+ instance._errorHooks.push((err) => withInstance(instance, () => fn(err)));
1960
+ }
1961
+ // src/component/create.ts
1962
+ function createComponent(fn) {
1963
+ const component = (props) => fn(props);
1964
+ component._SinwanComponent = true;
1965
+ component._displayName = fn.name || "AnonymousComponent";
1966
+ return component;
1967
+ }
1968
+ function createPage(fn) {
1969
+ const page = (data) => fn(data);
1970
+ page._SinwanPage = true;
1971
+ page._displayName = fn.name || "AnonymousPage";
1972
+ return page;
1973
+ }
1974
+ function createLayout(fn) {
1975
+ return createComponent(fn);
1976
+ }
1977
+ // src/component/provide-inject.ts
1978
+ function provide(key, value) {
1979
+ const instance = getCurrentInstance();
1980
+ if (!instance) {
1981
+ throw new Error("provide() called outside of component setup.");
1982
+ }
1983
+ instance.provides[key] = value;
1984
+ }
1985
+ function inject(key, defaultValue) {
1986
+ const instance = getCurrentInstance();
1987
+ if (!instance) {
1988
+ throw new Error("inject() called outside of component setup.");
1989
+ }
1990
+ if (key in instance.provides) {
1991
+ return instance.provides[key];
1992
+ }
1993
+ if (arguments.length >= 2) {
1994
+ return defaultValue;
1995
+ }
1996
+ console.warn(`[Sinwan] inject() key "${String(key)}" not found and no default provided.`);
1997
+ return;
1998
+ }
1999
+ // src/hydration/walk.ts
2000
+ function advance(cursor) {
2001
+ const node = cursor.current;
2002
+ if (node) {
2003
+ cursor.current = node.nextSibling;
2004
+ }
2005
+ return node;
2006
+ }
2007
+ function hydrateNode(node, cursor) {
2008
+ if (node == null || typeof node === "boolean") {
2009
+ const textNode2 = advance(cursor);
2010
+ return { type: "text", node: textNode2 ?? document.createTextNode("") };
2011
+ }
2012
+ if (typeof node === "string") {
2013
+ const textNode2 = advance(cursor);
2014
+ return { type: "text", node: textNode2 };
2015
+ }
2016
+ if (typeof node === "number") {
2017
+ const textNode2 = advance(cursor);
2018
+ return { type: "text", node: textNode2 };
2019
+ }
2020
+ if (node instanceof HtmlEscapedString) {
2021
+ const textNode2 = advance(cursor);
2022
+ return { type: "text", node: textNode2 };
2023
+ }
2024
+ if (isSignal(node) || isComputed(node)) {
2025
+ return hydrateReactiveText(node, cursor);
2026
+ }
2027
+ if (Array.isArray(node)) {
2028
+ return hydrateArray(node, cursor);
2029
+ }
2030
+ if (typeof node === "object" && node !== null && "tag" in node) {
2031
+ return hydrateElement(node, cursor);
2032
+ }
2033
+ const textNode = advance(cursor);
2034
+ return { type: "text", node: textNode };
2035
+ }
2036
+ function hydrateReactiveText(reactive, cursor) {
2037
+ const openComment = cursor.current;
2038
+ const owner = getCurrentInstance();
2039
+ if (openComment && openComment.nodeType === 8 && parseTextOpenMarker(openComment) >= 0) {
2040
+ advance(cursor);
2041
+ const textNode2 = advance(cursor);
2042
+ const closeComment = cursor.current;
2043
+ if (closeComment && closeComment.nodeType === 8 && isTextCloseMarker(closeComment)) {
2044
+ advance(cursor);
2045
+ }
2046
+ let initialized2 = false;
2047
+ const dispose2 = effect(() => {
2048
+ textNode2.data = String(reactive.value);
2049
+ if (initialized2) {
2050
+ queueUpdatedHooks(owner);
2051
+ }
2052
+ initialized2 = true;
2053
+ });
2054
+ return { type: "reactive-text", node: textNode2, dispose: dispose2 };
2055
+ }
2056
+ const textNode = advance(cursor);
2057
+ if (textNode) {
2058
+ let initialized2 = false;
2059
+ const dispose2 = effect(() => {
2060
+ textNode.data = String(reactive.value);
2061
+ if (initialized2) {
2062
+ queueUpdatedHooks(owner);
2063
+ }
2064
+ initialized2 = true;
2065
+ });
2066
+ return { type: "reactive-text", node: textNode, dispose: dispose2 };
2067
+ }
2068
+ const newText = document.createTextNode(String(reactive.value));
2069
+ let initialized = false;
2070
+ const dispose = effect(() => {
2071
+ newText.data = String(reactive.value);
2072
+ if (initialized) {
2073
+ queueUpdatedHooks(owner);
2074
+ }
2075
+ initialized = true;
2076
+ });
2077
+ return { type: "reactive-text", node: newText, dispose };
2078
+ }
2079
+ function hydrateElement(element, cursor) {
2080
+ const { tag, props, children } = element;
2081
+ if (tag === "") {
2082
+ return hydrateArray(children, cursor);
2083
+ }
2084
+ if (tag === Show || tag === For) {
2085
+ return hydrateElement(tag(props), cursor);
2086
+ }
2087
+ if (isShowElement(element) || isForElement(element)) {
2088
+ return hydrateControlFlow(element, cursor);
2089
+ }
2090
+ if (typeof tag === "function") {
2091
+ return hydrateComponent(tag, props, cursor);
2092
+ }
2093
+ if (typeof tag === "string") {
2094
+ return hydrateIntrinsic(tag, props, children, cursor);
2095
+ }
2096
+ return hydrateArray(children, cursor);
2097
+ }
2098
+ function hydrateIntrinsic(tag, props, children, cursor) {
2099
+ const el = advance(cursor);
2100
+ if (!el || el.nodeType !== 1) {
2101
+ console.warn(`[Sinwan hydration] expected <${tag}> but found`, el);
2102
+ return { type: "text", node: document.createTextNode("") };
2103
+ }
2104
+ el.removeAttribute(COMP_ID_ATTR);
2105
+ el.removeAttribute("data-sinwan-ev");
2106
+ const attrDisposers = hydrateAttributes(el, props);
2107
+ const eventCleanups = bindEvents(el, props);
2108
+ const refCleanup = applyRef2(el, props.ref);
2109
+ const childCursor = {
2110
+ parent: el,
2111
+ current: el.firstChild
2112
+ };
2113
+ const mountedChildren = [];
2114
+ for (const child of children) {
2115
+ mountedChildren.push(hydrateNode(child, childCursor));
2116
+ }
2117
+ return {
2118
+ type: "element",
2119
+ node: el,
2120
+ children: mountedChildren,
2121
+ eventCleanups,
2122
+ attrDisposers,
2123
+ refCleanup
2124
+ };
2125
+ }
2126
+ function hydrateAttributes(el, props) {
2127
+ const disposers = [];
2128
+ const owner = getCurrentInstance();
2129
+ for (const [key, value] of Object.entries(props)) {
2130
+ if (key === "children" || key === "key" || key === "ref" || isEventProp(key))
2131
+ continue;
2132
+ if (isSignal(value) || isComputed(value)) {
2133
+ let initialized = false;
2134
+ const dispose = effect(() => {
2135
+ const v = value.value;
2136
+ const attrName = key === "className" ? "class" : key === "htmlFor" ? "for" : key;
2137
+ if (v == null || v === false) {
2138
+ el.removeAttribute(attrName);
2139
+ } else if (v === true) {
2140
+ el.setAttribute(attrName, "");
2141
+ } else {
2142
+ el.setAttribute(attrName, String(v));
2143
+ }
2144
+ if (initialized) {
2145
+ queueUpdatedHooks(owner);
2146
+ }
2147
+ initialized = true;
2148
+ });
2149
+ disposers.push(dispose);
2150
+ }
2151
+ }
2152
+ return disposers;
2153
+ }
2154
+ function hydrateControlFlow(element, cursor) {
2155
+ if (isShowElement(element)) {
2156
+ const when = readReactive5(element.props.when);
2157
+ const content = when ? resolveShowChildren5(element, when) : element.props.fallback;
2158
+ return hydrateContent(content, cursor);
2159
+ }
2160
+ if (isForElement(element)) {
2161
+ const props = element.props;
2162
+ const items = readReactive5(props.each);
2163
+ const children = Array.isArray(items) && typeof props.children === "function" ? items.map((item, index) => props.children(item, () => index)) : [];
2164
+ return hydrateArray(children, cursor);
2165
+ }
2166
+ return hydrateArray(element.children, cursor);
2167
+ }
2168
+ function hydrateContent(content, cursor) {
2169
+ if (content == null || typeof content === "boolean") {
2170
+ return hydrateArray([], cursor);
2171
+ }
2172
+ return Array.isArray(content) ? hydrateArray(content, cursor) : hydrateNode(content, cursor);
2173
+ }
2174
+ function resolveShowChildren5(element, value) {
2175
+ const children = element.props.children ?? element.children;
2176
+ if (typeof children === "function") {
2177
+ return children(value);
2178
+ }
2179
+ return children;
2180
+ }
2181
+ function readReactive5(value) {
2182
+ return isSignal(value) || isComputed(value) ? value.value : value;
2183
+ }
2184
+ function applyRef2(el, ref) {
2185
+ const value = ref;
2186
+ if (!value) {
2187
+ return null;
2188
+ }
2189
+ if (typeof value === "function") {
2190
+ value(el);
2191
+ return () => value(null);
2192
+ }
2193
+ if (typeof value === "object" && "current" in value) {
2194
+ value.current = el;
2195
+ return () => {
2196
+ value.current = null;
2197
+ };
2198
+ }
2199
+ return null;
2200
+ }
2201
+ function hydrateComponent(component, props, cursor) {
2202
+ const parentInstance = getCurrentInstance();
2203
+ const instance = createComponentInstance(component, props, parentInstance);
2204
+ if (parentInstance) {
2205
+ parentInstance.children.push(instance);
2206
+ }
2207
+ const prevInstance = setCurrentInstance(instance);
2208
+ let result;
2209
+ let child;
2210
+ try {
2211
+ result = component(props);
2212
+ if (result && typeof result === "object" && "tag" in result) {
2213
+ child = hydrateElement(result, cursor);
2214
+ } else {
2215
+ child = hydrateNode(result, cursor);
2216
+ }
2217
+ } catch (err) {
2218
+ setCurrentInstance(prevInstance);
2219
+ handleComponentError(instance, err);
2220
+ const textNode = advance(cursor);
2221
+ return {
2222
+ type: "component",
2223
+ children: [
2224
+ { type: "text", node: textNode ?? document.createTextNode("") }
2225
+ ],
2226
+ disposers: [],
2227
+ instance
2228
+ };
2229
+ }
2230
+ setCurrentInstance(prevInstance);
2231
+ instance.element = child;
2232
+ return {
2233
+ type: "component",
2234
+ children: [child],
2235
+ disposers: instance.effects,
2236
+ instance
2237
+ };
2238
+ }
2239
+ function hydrateArray(nodes, cursor) {
2240
+ const children = [];
2241
+ for (const child of nodes) {
2242
+ children.push(hydrateNode(child, cursor));
2243
+ }
2244
+ const anchor = document.createComment("Sinwan-f");
2245
+ return { type: "fragment", children, anchor };
2246
+ }
2247
+
2248
+ // src/hydration/hydrate.ts
2249
+ function hydrate(component, container, props) {
2250
+ const mergedProps = props ?? {};
2251
+ const instance = createComponentInstance(component, mergedProps, null);
2252
+ let result;
2253
+ let root;
2254
+ setCurrentInstance(instance);
2255
+ try {
2256
+ result = component(mergedProps);
2257
+ const cursor = {
2258
+ parent: container,
2259
+ current: container.firstChild
2260
+ };
2261
+ if (result && typeof result === "object" && "tag" in result) {
2262
+ root = hydrateElement(result, cursor);
2263
+ } else {
2264
+ root = hydrateNode(result, cursor);
2265
+ }
2266
+ } catch (err) {
2267
+ setCurrentInstance(null);
2268
+ handleComponentError(instance, err);
2269
+ return {
2270
+ root: { type: "text", node: document.createTextNode("") },
2271
+ unmount() {}
2272
+ };
2273
+ }
2274
+ setCurrentInstance(null);
2275
+ instance.element = root;
2276
+ fireMountedHooks(instance);
2277
+ return {
2278
+ root,
2279
+ unmount() {
2280
+ fireUnmountedHooks(instance);
2281
+ unmountNode(root);
2282
+ container.innerHTML = "";
2283
+ }
2284
+ };
2285
+ }
2286
+
2287
+ //# debugId=82414CC0A24D81DC64756E2164756E21
2288
+ //# sourceMappingURL=index.development.js.map