elit 3.1.7 → 3.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/database.mjs CHANGED
@@ -1,68 +1,19 @@
1
1
  import {createRequire as __createRequire} from 'module';const require = __createRequire(import.meta.url);
2
- var __getOwnPropNames = Object.getOwnPropertyNames;
3
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
4
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
5
- }) : x)(function(x) {
6
- if (typeof require !== "undefined") return require.apply(this, arguments);
7
- throw Error('Dynamic require of "' + x + '" is not supported');
8
- });
9
- var __esm = (fn, res) => function __init() {
10
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
11
- };
12
-
13
- // src/runtime.ts
14
- var runtime, isNode, isBun, isDeno;
15
- var init_runtime = __esm({
16
- "src/runtime.ts"() {
17
- "use strict";
18
- runtime = (() => {
19
- if (typeof Deno !== "undefined") return "deno";
20
- if (typeof Bun !== "undefined") return "bun";
21
- return "node";
22
- })();
23
- isNode = runtime === "node";
24
- isBun = runtime === "bun";
25
- isDeno = runtime === "deno";
26
- }
27
- });
28
-
29
- // src/http.ts
30
- import { EventEmitter } from "events";
31
- var http, https;
32
- var init_http = __esm({
33
- "src/http.ts"() {
34
- "use strict";
35
- init_runtime();
36
- if (isNode && typeof process !== "undefined") {
37
- try {
38
- http = __require("http");
39
- https = __require("https");
40
- } catch (e) {
41
- http = __require("http");
42
- https = __require("https");
43
- }
44
- }
45
- }
46
- });
47
-
48
- // src/fs.ts
49
- var fs, fsPromises;
50
- var init_fs = __esm({
51
- "src/fs.ts"() {
52
- "use strict";
53
- init_runtime();
54
- if (isNode) {
55
- fs = __require("fs");
56
- fsPromises = __require("fs/promises");
57
- }
58
- }
59
- });
60
2
 
61
3
  // src/database.ts
62
4
  import vm from "vm";
63
5
 
6
+ // src/runtime.ts
7
+ var runtime = (() => {
8
+ if (typeof Deno !== "undefined") return "deno";
9
+ if (typeof Bun !== "undefined") return "bun";
10
+ return "node";
11
+ })();
12
+ var isNode = runtime === "node";
13
+ var isBun = runtime === "bun";
14
+ var isDeno = runtime === "deno";
15
+
64
16
  // src/path.ts
65
- init_runtime();
66
17
  function getSeparator(isWin) {
67
18
  return isWin ? "\\" : "/";
68
19
  }
@@ -280,766 +231,7 @@ function resolve(...paths) {
280
231
 
281
232
  // src/database.ts
282
233
  import path from "path";
283
- import fs2 from "fs";
284
-
285
- // src/server.ts
286
- init_http();
287
-
288
- // src/https.ts
289
- init_runtime();
290
- import { EventEmitter as EventEmitter2 } from "events";
291
-
292
- // src/ws.ts
293
- init_runtime();
294
- import { EventEmitter as EventEmitter3 } from "events";
295
-
296
- // src/chokidar.ts
297
- init_runtime();
298
- import { EventEmitter as EventEmitter4 } from "events";
299
-
300
- // src/server.ts
301
- init_fs();
302
-
303
- // src/mime-types.ts
304
- init_runtime();
305
- var MIME_TYPES = {
306
- // Text
307
- "txt": "text/plain",
308
- "html": "text/html",
309
- "htm": "text/html",
310
- "css": "text/css",
311
- "js": "text/javascript",
312
- "mjs": "text/javascript",
313
- "json": "application/json",
314
- "xml": "application/xml",
315
- "csv": "text/csv",
316
- "md": "text/markdown",
317
- "markdown": "text/x-markdown",
318
- // Images
319
- "png": "image/png",
320
- "jpg": "image/jpeg",
321
- "jpeg": "image/jpeg",
322
- "gif": "image/gif",
323
- "svg": "image/svg+xml",
324
- "webp": "image/webp",
325
- "ico": "image/x-icon",
326
- "bmp": "image/bmp",
327
- "tiff": "image/tiff",
328
- "tif": "image/tiff",
329
- // Audio
330
- "mp3": "audio/mpeg",
331
- "wav": "audio/wav",
332
- "ogg": "audio/ogg",
333
- "aac": "audio/aac",
334
- "m4a": "audio/mp4",
335
- "flac": "audio/flac",
336
- // Video
337
- "mp4": "video/mp4",
338
- "webm": "video/webm",
339
- "avi": "video/x-msvideo",
340
- "mov": "video/quicktime",
341
- "mkv": "video/x-matroska",
342
- "flv": "video/x-flv",
343
- // Application
344
- "pdf": "application/pdf",
345
- "zip": "application/zip",
346
- "gz": "application/gzip",
347
- "tar": "application/x-tar",
348
- "rar": "application/x-rar-compressed",
349
- "7z": "application/x-7z-compressed",
350
- // Documents
351
- "doc": "application/msword",
352
- "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
353
- "xls": "application/vnd.ms-excel",
354
- "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
355
- "ppt": "application/vnd.ms-powerpoint",
356
- "pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
357
- // Fonts
358
- "woff": "font/woff",
359
- "woff2": "font/woff2",
360
- "ttf": "font/ttf",
361
- "otf": "font/otf",
362
- "eot": "application/vnd.ms-fontobject",
363
- // Web
364
- "wasm": "application/wasm",
365
- "manifest": "application/manifest+json",
366
- // Binary
367
- "bin": "application/octet-stream",
368
- "exe": "application/x-msdownload",
369
- "dll": "application/x-msdownload",
370
- // TypeScript/Modern JS
371
- "ts": "text/typescript",
372
- "tsx": "text/tsx",
373
- "jsx": "text/jsx"
374
- };
375
- var TYPE_TO_EXTENSIONS = {};
376
- for (const ext in MIME_TYPES) {
377
- const type = MIME_TYPES[ext];
378
- if (!TYPE_TO_EXTENSIONS[type]) {
379
- TYPE_TO_EXTENSIONS[type] = [];
380
- }
381
- TYPE_TO_EXTENSIONS[type].push(ext);
382
- }
383
-
384
- // src/server.ts
385
- init_runtime();
386
-
387
- // src/dom.ts
388
- function resolveElement(rootElement) {
389
- return typeof rootElement === "string" ? document.getElementById(rootElement.replace("#", "")) : rootElement;
390
- }
391
- function ensureElement(el, rootElement) {
392
- if (!el) {
393
- throw new Error(`Element not found: ${rootElement}`);
394
- }
395
- return el;
396
- }
397
- function shouldSkipChild(child) {
398
- return child == null || child === false;
399
- }
400
- function isPrimitiveJson(json) {
401
- return json == null || typeof json === "boolean" || typeof json === "string" || typeof json === "number";
402
- }
403
- var DomNode = class {
404
- constructor() {
405
- this.elementCache = /* @__PURE__ */ new WeakMap();
406
- this.reactiveNodes = /* @__PURE__ */ new Map();
407
- }
408
- createElement(tagName, props = {}, children = []) {
409
- return { tagName, props, children };
410
- }
411
- renderToDOM(vNode, parent) {
412
- if (vNode == null || vNode === false) return;
413
- if (typeof vNode !== "object") {
414
- parent.appendChild(document.createTextNode(String(vNode)));
415
- return;
416
- }
417
- const { tagName, props, children } = vNode;
418
- const isSVG = tagName === "svg" || tagName[0] === "s" && tagName[1] === "v" && tagName[2] === "g" || parent.namespaceURI === "http://www.w3.org/2000/svg";
419
- const el = isSVG ? document.createElementNS("http://www.w3.org/2000/svg", tagName.replace("svg", "").toLowerCase() || tagName) : document.createElement(tagName);
420
- for (const key in props) {
421
- const value = props[key];
422
- if (value == null || value === false) continue;
423
- const c = key.charCodeAt(0);
424
- if (c === 99 && (key.length < 6 || key[5] === "N")) {
425
- const classValue = Array.isArray(value) ? value.join(" ") : value;
426
- isSVG ? el.setAttribute("class", classValue) : el.className = classValue;
427
- } else if (c === 115 && key.length === 5) {
428
- if (typeof value === "string") {
429
- el.style.cssText = value;
430
- } else {
431
- const s = el.style;
432
- for (const k in value) s[k] = value[k];
433
- }
434
- } else if (c === 111 && key.charCodeAt(1) === 110) {
435
- el[key.toLowerCase()] = value;
436
- } else if (c === 100 && key.length > 20) {
437
- el.innerHTML = value.__html;
438
- } else if (c === 114 && key.length === 3) {
439
- setTimeout(() => {
440
- typeof value === "function" ? value(el) : value.current = el;
441
- }, 0);
442
- } else {
443
- el.setAttribute(key, value === true ? "" : String(value));
444
- }
445
- }
446
- const len = children.length;
447
- if (!len) {
448
- parent.appendChild(el);
449
- return;
450
- }
451
- const renderChildren = (target) => {
452
- for (let i = 0; i < len; i++) {
453
- const child = children[i];
454
- if (shouldSkipChild(child)) continue;
455
- if (Array.isArray(child)) {
456
- for (let j = 0, cLen = child.length; j < cLen; j++) {
457
- const c = child[j];
458
- !shouldSkipChild(c) && this.renderToDOM(c, target);
459
- }
460
- } else {
461
- this.renderToDOM(child, target);
462
- }
463
- }
464
- };
465
- if (len > 30) {
466
- const fragment = document.createDocumentFragment();
467
- renderChildren(fragment);
468
- el.appendChild(fragment);
469
- } else {
470
- renderChildren(el);
471
- }
472
- parent.appendChild(el);
473
- }
474
- render(rootElement, vNode) {
475
- const el = ensureElement(resolveElement(rootElement), rootElement);
476
- el.innerHTML = "";
477
- if (vNode.children && vNode.children.length > 500) {
478
- const fragment = document.createDocumentFragment();
479
- this.renderToDOM(vNode, fragment);
480
- el.appendChild(fragment);
481
- } else {
482
- this.renderToDOM(vNode, el);
483
- }
484
- return el;
485
- }
486
- batchRender(rootElement, vNodes) {
487
- const el = ensureElement(resolveElement(rootElement), rootElement);
488
- const len = vNodes.length;
489
- if (len > 3e3) {
490
- const fragment = document.createDocumentFragment();
491
- let processed = 0;
492
- const chunkSize = 1500;
493
- const processChunk = () => {
494
- const end = Math.min(processed + chunkSize, len);
495
- for (let i = processed; i < end; i++) {
496
- this.renderToDOM(vNodes[i], fragment);
497
- }
498
- processed = end;
499
- if (processed >= len) {
500
- el.appendChild(fragment);
501
- } else {
502
- requestAnimationFrame(processChunk);
503
- }
504
- };
505
- processChunk();
506
- } else {
507
- const fragment = document.createDocumentFragment();
508
- for (let i = 0; i < len; i++) {
509
- this.renderToDOM(vNodes[i], fragment);
510
- }
511
- el.appendChild(fragment);
512
- }
513
- return el;
514
- }
515
- renderChunked(rootElement, vNodes, chunkSize = 5e3, onProgress) {
516
- const el = ensureElement(resolveElement(rootElement), rootElement);
517
- const len = vNodes.length;
518
- let index = 0;
519
- const renderChunk = () => {
520
- const end = Math.min(index + chunkSize, len);
521
- const fragment = document.createDocumentFragment();
522
- for (let i = index; i < end; i++) {
523
- this.renderToDOM(vNodes[i], fragment);
524
- }
525
- el.appendChild(fragment);
526
- index = end;
527
- if (onProgress) onProgress(index, len);
528
- if (index < len) {
529
- requestAnimationFrame(renderChunk);
530
- }
531
- };
532
- requestAnimationFrame(renderChunk);
533
- return el;
534
- }
535
- renderToHead(...vNodes) {
536
- const head = document.head;
537
- if (head) {
538
- for (const vNode of vNodes.flat()) {
539
- vNode && this.renderToDOM(vNode, head);
540
- }
541
- }
542
- return head;
543
- }
544
- addStyle(cssText) {
545
- const el = document.createElement("style");
546
- el.textContent = cssText;
547
- return document.head.appendChild(el);
548
- }
549
- addMeta(attrs) {
550
- const el = document.createElement("meta");
551
- for (const k in attrs) el.setAttribute(k, attrs[k]);
552
- return document.head.appendChild(el);
553
- }
554
- addLink(attrs) {
555
- const el = document.createElement("link");
556
- for (const k in attrs) el.setAttribute(k, attrs[k]);
557
- return document.head.appendChild(el);
558
- }
559
- setTitle(text) {
560
- return document.title = text;
561
- }
562
- // Reactive State Management
563
- createState(initialValue, options = {}) {
564
- let value = initialValue;
565
- const listeners = /* @__PURE__ */ new Set();
566
- let updateTimer = null;
567
- const { throttle = 0, deep = false } = options;
568
- const notify = () => listeners.forEach((fn) => fn(value));
569
- const scheduleUpdate = () => {
570
- if (throttle > 0) {
571
- if (!updateTimer) {
572
- updateTimer = setTimeout(() => {
573
- updateTimer = null;
574
- notify();
575
- }, throttle);
576
- }
577
- } else {
578
- notify();
579
- }
580
- };
581
- return {
582
- get value() {
583
- return value;
584
- },
585
- set value(newValue) {
586
- const changed = deep ? JSON.stringify(value) !== JSON.stringify(newValue) : value !== newValue;
587
- if (changed) {
588
- value = newValue;
589
- scheduleUpdate();
590
- }
591
- },
592
- subscribe(fn) {
593
- listeners.add(fn);
594
- return () => listeners.delete(fn);
595
- },
596
- destroy() {
597
- listeners.clear();
598
- updateTimer && clearTimeout(updateTimer);
599
- }
600
- };
601
- }
602
- computed(states, computeFn) {
603
- const values = states.map((s) => s.value);
604
- const result = this.createState(computeFn(...values));
605
- states.forEach((state, index) => {
606
- state.subscribe((newValue) => {
607
- values[index] = newValue;
608
- result.value = computeFn(...values);
609
- });
610
- });
611
- return result;
612
- }
613
- effect(stateFn) {
614
- stateFn();
615
- }
616
- // Virtual scrolling helper for large lists
617
- createVirtualList(container, items, renderItem, itemHeight = 50, bufferSize = 5) {
618
- const viewportHeight = container.clientHeight;
619
- const totalHeight = items.length * itemHeight;
620
- let scrollTop = 0;
621
- const getVisibleRange = () => {
622
- const start = Math.max(0, Math.floor(scrollTop / itemHeight) - bufferSize);
623
- const end = Math.min(items.length, Math.ceil((scrollTop + viewportHeight) / itemHeight) + bufferSize);
624
- return { start, end };
625
- };
626
- const render2 = () => {
627
- const { start, end } = getVisibleRange();
628
- const wrapper = document.createElement("div");
629
- wrapper.style.cssText = `height:${totalHeight}px;position:relative`;
630
- for (let i = start; i < end; i++) {
631
- const itemEl = document.createElement("div");
632
- itemEl.style.cssText = `position:absolute;top:${i * itemHeight}px;height:${itemHeight}px;width:100%`;
633
- this.renderToDOM(renderItem(items[i], i), itemEl);
634
- wrapper.appendChild(itemEl);
635
- }
636
- container.innerHTML = "";
637
- container.appendChild(wrapper);
638
- };
639
- const scrollHandler = () => {
640
- scrollTop = container.scrollTop;
641
- requestAnimationFrame(render2);
642
- };
643
- container.addEventListener("scroll", scrollHandler);
644
- render2();
645
- return {
646
- render: render2,
647
- destroy: () => {
648
- container.removeEventListener("scroll", scrollHandler);
649
- container.innerHTML = "";
650
- }
651
- };
652
- }
653
- // Lazy load components
654
- lazy(loadFn) {
655
- let component = null;
656
- let loading = false;
657
- return async (...args) => {
658
- if (!component && !loading) {
659
- loading = true;
660
- component = await loadFn();
661
- loading = false;
662
- }
663
- return component ? component(...args) : { tagName: "div", props: { class: "loading" }, children: ["Loading..."] };
664
- };
665
- }
666
- // Memory management - cleanup unused elements
667
- cleanupUnusedElements(root) {
668
- const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
669
- const toRemove = [];
670
- while (walker.nextNode()) {
671
- const node = walker.currentNode;
672
- if (node.id && node.id.startsWith("r") && !this.elementCache.has(node)) {
673
- toRemove.push(node);
674
- }
675
- }
676
- toRemove.forEach((el) => el.remove());
677
- return toRemove.length;
678
- }
679
- // Server-Side Rendering - convert VNode to HTML string
680
- renderToString(vNode, options = {}) {
681
- const { pretty = false, indent = 0 } = options;
682
- const indentStr = pretty ? " ".repeat(indent) : "";
683
- const newLine = pretty ? "\n" : "";
684
- let resolvedVNode = this.resolveStateValue(vNode);
685
- resolvedVNode = this.unwrapReactive(resolvedVNode);
686
- if (Array.isArray(resolvedVNode)) {
687
- return resolvedVNode.map((child) => this.renderToString(child, options)).join("");
688
- }
689
- if (typeof resolvedVNode !== "object" || resolvedVNode === null) {
690
- if (resolvedVNode === null || resolvedVNode === void 0 || resolvedVNode === false) {
691
- return "";
692
- }
693
- return this.escapeHtml(String(resolvedVNode));
694
- }
695
- const { tagName, props, children } = resolvedVNode;
696
- const isSelfClosing = this.isSelfClosingTag(tagName);
697
- let html = `${indentStr}<${tagName}`;
698
- const attrs = this.propsToAttributes(props);
699
- if (attrs) {
700
- html += ` ${attrs}`;
701
- }
702
- if (isSelfClosing) {
703
- html += ` />${newLine}`;
704
- return html;
705
- }
706
- html += ">";
707
- if (props.dangerouslySetInnerHTML) {
708
- html += props.dangerouslySetInnerHTML.__html;
709
- html += `</${tagName}>${newLine}`;
710
- return html;
711
- }
712
- if (children && children.length > 0) {
713
- const resolvedChildren = children.map((c) => {
714
- const resolved = this.resolveStateValue(c);
715
- return this.unwrapReactive(resolved);
716
- });
717
- const hasComplexChildren = resolvedChildren.some(
718
- (c) => typeof c === "object" && c !== null && !Array.isArray(c) && "tagName" in c
719
- );
720
- if (pretty && hasComplexChildren) {
721
- html += newLine;
722
- for (const child of resolvedChildren) {
723
- if (shouldSkipChild(child)) continue;
724
- if (Array.isArray(child)) {
725
- for (const c of child) {
726
- if (!shouldSkipChild(c)) {
727
- html += this.renderToString(c, { pretty, indent: indent + 1 });
728
- }
729
- }
730
- } else {
731
- html += this.renderToString(child, { pretty, indent: indent + 1 });
732
- }
733
- }
734
- html += indentStr;
735
- } else {
736
- for (const child of resolvedChildren) {
737
- if (shouldSkipChild(child)) continue;
738
- if (Array.isArray(child)) {
739
- for (const c of child) {
740
- if (!shouldSkipChild(c)) {
741
- html += this.renderToString(c, { pretty: false, indent: 0 });
742
- }
743
- }
744
- } else {
745
- html += this.renderToString(child, { pretty: false, indent: 0 });
746
- }
747
- }
748
- }
749
- }
750
- html += `</${tagName}>${newLine}`;
751
- return html;
752
- }
753
- resolveStateValue(value) {
754
- if (value && typeof value === "object" && "value" in value && "subscribe" in value) {
755
- return value.value;
756
- }
757
- return value;
758
- }
759
- isReactiveWrapper(vNode) {
760
- if (!vNode || typeof vNode !== "object" || !vNode.tagName) {
761
- return false;
762
- }
763
- return vNode.tagName === "span" && vNode.props?.id && typeof vNode.props.id === "string" && vNode.props.id.match(/^r[a-z0-9]{9}$/);
764
- }
765
- unwrapReactive(vNode) {
766
- if (!this.isReactiveWrapper(vNode)) {
767
- return vNode;
768
- }
769
- const children = vNode.children;
770
- if (!children || children.length === 0) {
771
- return "";
772
- }
773
- if (children.length === 1) {
774
- const child = children[0];
775
- if (child && typeof child === "object" && child.tagName === "span") {
776
- const props = child.props;
777
- const hasNoProps = !props || Object.keys(props).length === 0;
778
- const hasSingleStringChild = child.children && child.children.length === 1 && typeof child.children[0] === "string";
779
- if (hasNoProps && hasSingleStringChild) {
780
- return child.children[0];
781
- }
782
- }
783
- return this.unwrapReactive(child);
784
- }
785
- return children.map((c) => this.unwrapReactive(c));
786
- }
787
- escapeHtml(text) {
788
- const htmlEscapes = {
789
- "&": "&amp;",
790
- "<": "&lt;",
791
- ">": "&gt;",
792
- '"': "&quot;",
793
- "'": "&#x27;"
794
- };
795
- return text.replace(/[&<>"']/g, (char) => htmlEscapes[char]);
796
- }
797
- isSelfClosingTag(tagName) {
798
- const selfClosingTags = /* @__PURE__ */ new Set([
799
- "area",
800
- "base",
801
- "br",
802
- "col",
803
- "embed",
804
- "hr",
805
- "img",
806
- "input",
807
- "link",
808
- "meta",
809
- "param",
810
- "source",
811
- "track",
812
- "wbr"
813
- ]);
814
- return selfClosingTags.has(tagName.toLowerCase());
815
- }
816
- propsToAttributes(props) {
817
- const attrs = [];
818
- for (const key in props) {
819
- if (key === "children" || key === "dangerouslySetInnerHTML" || key === "ref") {
820
- continue;
821
- }
822
- let value = props[key];
823
- value = this.resolveStateValue(value);
824
- if (value == null || value === false) continue;
825
- if (key.startsWith("on") && typeof value === "function") {
826
- continue;
827
- }
828
- if (key === "className" || key === "class") {
829
- const className = Array.isArray(value) ? value.join(" ") : value;
830
- if (className) {
831
- attrs.push(`class="${this.escapeHtml(String(className))}"`);
832
- }
833
- continue;
834
- }
835
- if (key === "style") {
836
- const styleStr = this.styleToString(value);
837
- if (styleStr) {
838
- attrs.push(`style="${this.escapeHtml(styleStr)}"`);
839
- }
840
- continue;
841
- }
842
- if (value === true) {
843
- attrs.push(key);
844
- continue;
845
- }
846
- attrs.push(`${key}="${this.escapeHtml(String(value))}"`);
847
- }
848
- return attrs.join(" ");
849
- }
850
- styleToString(style) {
851
- if (typeof style === "string") {
852
- return style;
853
- }
854
- if (typeof style === "object" && style !== null) {
855
- const styles = [];
856
- for (const key in style) {
857
- const cssKey = key.replace(/([A-Z])/g, "-$1").toLowerCase();
858
- styles.push(`${cssKey}:${style[key]}`);
859
- }
860
- return styles.join(";");
861
- }
862
- return "";
863
- }
864
- isState(value) {
865
- return value && typeof value === "object" && "value" in value && "subscribe" in value && typeof value.subscribe === "function";
866
- }
867
- createReactiveChild(state, renderFn) {
868
- const currentValue = renderFn(state.value);
869
- if (typeof window !== "undefined" && typeof document !== "undefined") {
870
- const entry = { node: null, renderFn };
871
- this.reactiveNodes.set(state, entry);
872
- state.subscribe(() => {
873
- if (entry.node && entry.node.parentNode) {
874
- const newValue = renderFn(state.value);
875
- entry.node.textContent = String(newValue ?? "");
876
- }
877
- });
878
- }
879
- return currentValue;
880
- }
881
- jsonToVNode(json) {
882
- if (this.isState(json)) {
883
- return this.createReactiveChild(json, (v) => v);
884
- }
885
- if (isPrimitiveJson(json)) {
886
- return json;
887
- }
888
- const { tag, attributes = {}, children } = json;
889
- const props = {};
890
- for (const key in attributes) {
891
- const value = attributes[key];
892
- if (key === "class") {
893
- props.className = this.isState(value) ? value.value : value;
894
- } else {
895
- props[key] = this.isState(value) ? value.value : value;
896
- }
897
- }
898
- const childrenArray = [];
899
- if (children != null) {
900
- if (Array.isArray(children)) {
901
- for (const child of children) {
902
- if (this.isState(child)) {
903
- childrenArray.push(this.createReactiveChild(child, (v) => v));
904
- } else {
905
- const converted = this.jsonToVNode(child);
906
- if (converted != null && converted !== false) {
907
- childrenArray.push(converted);
908
- }
909
- }
910
- }
911
- } else if (this.isState(children)) {
912
- childrenArray.push(this.createReactiveChild(children, (v) => v));
913
- } else if (typeof children === "object" && "tag" in children) {
914
- const converted = this.jsonToVNode(children);
915
- if (converted != null && converted !== false) {
916
- childrenArray.push(converted);
917
- }
918
- } else {
919
- childrenArray.push(children);
920
- }
921
- }
922
- return { tagName: tag, props, children: childrenArray };
923
- }
924
- vNodeJsonToVNode(json) {
925
- if (this.isState(json)) {
926
- return this.createReactiveChild(json, (v) => v);
927
- }
928
- if (isPrimitiveJson(json)) {
929
- return json;
930
- }
931
- const { tagName, props = {}, children = [] } = json;
932
- const resolvedProps = {};
933
- for (const key in props) {
934
- const value = props[key];
935
- resolvedProps[key] = this.isState(value) ? value.value : value;
936
- }
937
- const childrenArray = [];
938
- for (const child of children) {
939
- if (this.isState(child)) {
940
- childrenArray.push(this.createReactiveChild(child, (v) => v));
941
- } else {
942
- const converted = this.vNodeJsonToVNode(child);
943
- if (converted != null && converted !== false) {
944
- childrenArray.push(converted);
945
- }
946
- }
947
- }
948
- return { tagName, props: resolvedProps, children: childrenArray };
949
- }
950
- renderJson(rootElement, json) {
951
- const vNode = this.jsonToVNode(json);
952
- if (!vNode || typeof vNode !== "object" || !("tagName" in vNode)) {
953
- throw new Error("Invalid JSON structure");
954
- }
955
- return this.render(rootElement, vNode);
956
- }
957
- renderVNode(rootElement, json) {
958
- const vNode = this.vNodeJsonToVNode(json);
959
- if (!vNode || typeof vNode !== "object" || !("tagName" in vNode)) {
960
- throw new Error("Invalid VNode JSON structure");
961
- }
962
- return this.render(rootElement, vNode);
963
- }
964
- renderJsonToString(json, options = {}) {
965
- const vNode = this.jsonToVNode(json);
966
- return this.renderToString(vNode, options);
967
- }
968
- renderVNodeToString(json, options = {}) {
969
- const vNode = this.vNodeJsonToVNode(json);
970
- return this.renderToString(vNode, options);
971
- }
972
- // Generate complete HTML document as string (for SSR)
973
- renderToHTMLDocument(vNode, options = {}) {
974
- const { title = "", meta = [], links = [], scripts = [], styles = [], lang = "en", head = "", bodyAttrs = {}, pretty = false } = options;
975
- const nl = pretty ? "\n" : "";
976
- const indent = pretty ? " " : "";
977
- const indent2 = pretty ? " " : "";
978
- let html = `<!DOCTYPE html>${nl}<html lang="${lang}">${nl}${indent}<head>${nl}${indent2}<meta charset="UTF-8">${nl}${indent2}<meta name="viewport" content="width=device-width, initial-scale=1.0">${nl}`;
979
- if (title) html += `${indent2}<title>${this.escapeHtml(title)}</title>${nl}`;
980
- for (const m of meta) {
981
- html += `${indent2}<meta`;
982
- for (const k in m) html += ` ${k}="${this.escapeHtml(m[k])}"`;
983
- html += `>${nl}`;
984
- }
985
- for (const l of links) {
986
- html += `${indent2}<link`;
987
- for (const k in l) html += ` ${k}="${this.escapeHtml(l[k])}"`;
988
- html += `>${nl}`;
989
- }
990
- for (const s of styles) {
991
- if (s.href) {
992
- html += `${indent2}<link rel="stylesheet" href="${this.escapeHtml(s.href)}">${nl}`;
993
- } else if (s.content) {
994
- html += `${indent2}<style>${s.content}</style>${nl}`;
995
- }
996
- }
997
- if (head) html += head + nl;
998
- html += `${indent}</head>${nl}${indent}<body`;
999
- for (const k in bodyAttrs) html += ` ${k}="${this.escapeHtml(bodyAttrs[k])}"`;
1000
- html += `>${nl}`;
1001
- html += this.renderToString(vNode, { pretty, indent: 2 });
1002
- for (const script of scripts) {
1003
- html += `${indent2}<script`;
1004
- if (script.type) html += ` type="${this.escapeHtml(script.type)}"`;
1005
- if (script.async) html += ` async`;
1006
- if (script.defer) html += ` defer`;
1007
- if (script.src) {
1008
- html += ` src="${this.escapeHtml(script.src)}"></script>${nl}`;
1009
- } else if (script.content) {
1010
- html += `>${script.content}</script>${nl}`;
1011
- } else {
1012
- html += `></script>${nl}`;
1013
- }
1014
- }
1015
- html += `${indent}</body>${nl}</html>`;
1016
- return html;
1017
- }
1018
- // Expose elementCache for reactive updates
1019
- getElementCache() {
1020
- return this.elementCache;
1021
- }
1022
- };
1023
- var dom = new DomNode();
1024
- var render = dom.render.bind(dom);
1025
- var renderToString = dom.renderToString.bind(dom);
1026
-
1027
- // src/server.ts
1028
- var ServerDatabase = class {
1029
- constructor() {
1030
- this._db = null;
1031
- }
1032
- async initialize(config) {
1033
- this._db = new Database(config);
1034
- }
1035
- database() {
1036
- return this._db;
1037
- }
1038
- };
1039
- var serverDatabase = new ServerDatabase();
1040
- var database = serverDatabase.database;
1041
-
1042
- // src/database.ts
234
+ import fs from "fs";
1043
235
  import * as esbuild from "esbuild";
1044
236
  var Database = class {
1045
237
  constructor(config) {
@@ -1077,7 +269,7 @@ var Database = class {
1077
269
  });
1078
270
  }
1079
271
  async moduleLinker(specifier, referencingModule) {
1080
- const dbFiles = fs2.readdirSync(this._config.dir || resolve(process.cwd(), "databases")).filter((f) => f.endsWith(".ts")).map((f) => path.join(this._config.dir || resolve(process.cwd(), "databases"), f));
272
+ const dbFiles = fs.readdirSync(this._config.dir || resolve(process.cwd(), "databases")).filter((f) => f.endsWith(".ts")).map((f) => path.join(this._config.dir || resolve(process.cwd(), "databases"), f));
1081
273
  const dbResult = this.resolvePath(dbFiles, specifier);
1082
274
  if (dbResult) {
1083
275
  try {
@@ -1111,26 +303,66 @@ var Database = class {
1111
303
  let stringCode;
1112
304
  if (typeof code === "function") {
1113
305
  const funcStr = code.toString();
1114
- const arrowMatch = funcStr.match(/^[\s]*\(?\s*\)?\s*=>\s*{?/);
1115
- const functionMatch = funcStr.match(/^[\s]*function\s*\(?[\w\s]*\)?\s*{/);
1116
- const match = arrowMatch || functionMatch;
1117
- const start = match ? match[0].length : 0;
1118
- const end = funcStr.lastIndexOf("}");
1119
- stringCode = funcStr.substring(start, end);
1120
- stringCode = stringCode.replace(/^[\s\r\n]+/, "").replace(/[\s\r\n]+$/, "");
1121
- stringCode = stringCode.replace(
1122
- /import\s*\(\s*([^)]+?)\s*\)\s*\.from\s*\(\s*(['"])([^'"]+)\2\s*\)/g,
1123
- (_, importArg, quote, modulePath) => {
1124
- const trimmed = importArg.trim();
1125
- if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
1126
- const inner = trimmed.slice(1, -1).trim();
1127
- return `import { ${inner} } from ${quote}${modulePath}${quote}`;
1128
- } else {
1129
- return `import ${trimmed} from ${quote}${modulePath}${quote}`;
1130
- }
306
+ if (funcStr.includes("=>")) {
307
+ const arrowIndex = funcStr.indexOf("=>");
308
+ let start = arrowIndex + 2;
309
+ while (start < funcStr.length && funcStr[start] === " ") start++;
310
+ if (funcStr[start] === "{") start++;
311
+ let end = funcStr.lastIndexOf("}");
312
+ if (start < end) {
313
+ stringCode = funcStr.substring(start, end);
314
+ } else {
315
+ stringCode = funcStr.substring(start);
316
+ }
317
+ } else if (funcStr.includes("function")) {
318
+ const funcIndex = funcStr.indexOf("function");
319
+ let start = funcIndex + 8;
320
+ while (start < funcStr.length && funcStr[start] === " ") start++;
321
+ if (funcStr[start] === "(") start++;
322
+ if (start < funcStr.length && funcStr[start] !== "(") {
323
+ while (start < funcStr.length && funcStr[start] !== " " && funcStr[start] !== "(") start++;
324
+ }
325
+ if (funcStr[start] === "(") start++;
326
+ while (start < funcStr.length && funcStr[start] === " ") start++;
327
+ if (funcStr[start] === "{") start++;
328
+ const end = funcStr.lastIndexOf("}");
329
+ if (start < end) {
330
+ stringCode = funcStr.substring(start, end);
331
+ } else {
332
+ stringCode = funcStr.substring(start);
1131
333
  }
1132
- );
1133
- stringCode = stringCode.split("\n").map((line) => line.trim()).join("\n").trim();
334
+ } else {
335
+ stringCode = funcStr;
336
+ }
337
+ stringCode = stringCode.trim();
338
+ let importPos = 0;
339
+ while ((importPos = stringCode.indexOf("import(", importPos)) !== -1) {
340
+ const fromPos = stringCode.indexOf(".from(", importPos);
341
+ if (fromPos === -1) break;
342
+ const quoteStart = stringCode.indexOf("(", fromPos + 7) + 1;
343
+ if (quoteStart === -1) break;
344
+ const quoteChar = stringCode[quoteStart];
345
+ if (quoteChar !== '"' && quoteChar !== "'") break;
346
+ const quoteEnd = stringCode.indexOf(quoteChar, quoteStart + 1);
347
+ if (quoteEnd === -1) break;
348
+ const modulePath = stringCode.substring(quoteStart + 1, quoteEnd);
349
+ const importArgEnd = fromPos - 1;
350
+ const importArgStart = importPos + 7;
351
+ const trimmed = stringCode.substring(importArgStart, importArgEnd).trim();
352
+ let replacement;
353
+ if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
354
+ const inner = trimmed.slice(1, -1).trim();
355
+ replacement = `import { ${inner} } from "${modulePath}"`;
356
+ } else {
357
+ replacement = `import ${trimmed} from "${modulePath}"`;
358
+ }
359
+ const before = stringCode.substring(0, importPos);
360
+ const after = stringCode.substring(quoteEnd + 2);
361
+ stringCode = before + replacement + after;
362
+ }
363
+ const lines = stringCode.split("\n");
364
+ const trimmedLines = lines.map((line) => line.trim());
365
+ stringCode = trimmedLines.join("\n").trim();
1134
366
  } else {
1135
367
  stringCode = code;
1136
368
  }
@@ -1161,10 +393,14 @@ var Database = class {
1161
393
  return await this.vmRun(code, options);
1162
394
  }
1163
395
  };
1164
- var database2 = serverDatabase.database;
1165
- var database_default = database2;
396
+ function database() {
397
+ return new Database({
398
+ dir: resolve(process.cwd(), "databases")
399
+ });
400
+ }
401
+ var database_default = database;
1166
402
  export {
1167
403
  Database,
1168
- database2 as database,
404
+ database,
1169
405
  database_default as default
1170
406
  };