@mostfeatured/dbi 0.2.8 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1,63 @@
1
- import type { walk as WalkType } from "estree-walker";
2
1
  import * as stuffs from "stuffs";
3
2
 
4
3
  // Lazy imports to avoid issues with package managers that don't properly hoist dependencies
5
4
  let _parse: typeof import("svelte/compiler").parse;
6
- let _walk: typeof WalkType;
7
5
 
8
6
  async function ensureImports() {
9
7
  if (!_parse) {
10
8
  const svelteCompiler = await import("svelte/compiler");
11
9
  _parse = svelteCompiler.parse;
12
10
  }
13
- if (!_walk) {
14
- const estreeWalker = await import("estree-walker");
15
- _walk = estreeWalker.walk;
11
+ }
12
+
13
+ /**
14
+ * Simple AST walker for Svelte AST nodes
15
+ */
16
+ function walkSvelteAst(node: any, callback: (node: any) => void) {
17
+ if (!node || typeof node !== 'object') return;
18
+
19
+ callback(node);
20
+
21
+ // Walk children based on node type
22
+ if (node.children && Array.isArray(node.children)) {
23
+ for (const child of node.children) {
24
+ walkSvelteAst(child, callback);
25
+ }
26
+ }
27
+ if (node.fragment && node.fragment.nodes) {
28
+ for (const child of node.fragment.nodes) {
29
+ walkSvelteAst(child, callback);
30
+ }
31
+ }
32
+ if (node.nodes && Array.isArray(node.nodes)) {
33
+ for (const child of node.nodes) {
34
+ walkSvelteAst(child, callback);
35
+ }
36
+ }
37
+ // Handle other potential child properties
38
+ if (node.else) {
39
+ walkSvelteAst(node.else, callback);
40
+ }
41
+ if (node.consequent) {
42
+ walkSvelteAst(node.consequent, callback);
43
+ }
44
+ if (node.alternate) {
45
+ walkSvelteAst(node.alternate, callback);
46
+ }
47
+ if (node.then) {
48
+ walkSvelteAst(node.then, callback);
49
+ }
50
+ if (node.catch) {
51
+ walkSvelteAst(node.catch, callback);
52
+ }
53
+ if (node.body) {
54
+ if (Array.isArray(node.body)) {
55
+ for (const child of node.body) {
56
+ walkSvelteAst(child, callback);
57
+ }
58
+ } else {
59
+ walkSvelteAst(node.body, callback);
60
+ }
16
61
  }
17
62
  }
18
63
 
@@ -50,95 +95,93 @@ export async function parseSvelteComponent(source: string, data?: Record<string,
50
95
  let autoNameCounter = 0;
51
96
 
52
97
  // Walk through HTML nodes to find event handlers
53
- _walk(ast.html as any, {
54
- enter(node: any) {
55
- if (node.type === "Element" || node.type === "InlineComponent") {
56
- const attributes = node.attributes || [];
57
-
58
- // Find name attribute
59
- const nameAttr = attributes.find((attr: any) =>
60
- attr.type === "Attribute" && attr.name === "name"
61
- );
62
-
63
- // Check if element has an onclick/onchange handler and get the handler info
64
- let foundHandler: { eventType: string; handlerName: string } | null = null;
65
-
66
- for (const attr of attributes) {
67
- const isEventHandler = attr.type === "EventHandler";
68
- const isOnAttribute = attr.type === "Attribute" && attr.name && attr.name.startsWith("on");
69
-
70
- if (isEventHandler || isOnAttribute) {
71
- const eventType = attr.name;
72
- let handlerName = "";
73
-
74
- if (attr.type === "Attribute" && Array.isArray(attr.value)) {
75
- const exprValue = attr.value.find((v: any) => v.type === "ExpressionTag" || v.type === "MustacheTag");
76
- if (exprValue && exprValue.expression) {
77
- if (exprValue.expression.type === "Identifier") {
78
- handlerName = exprValue.expression.name;
79
- } else if (exprValue.expression.type === "CallExpression" && exprValue.expression.callee) {
80
- handlerName = exprValue.expression.callee.name;
81
- }
82
- }
83
- } else if (attr.expression) {
84
- if (attr.expression.type === "Identifier") {
85
- handlerName = attr.expression.name;
86
- } else if (attr.expression.type === "CallExpression" && attr.expression.callee) {
87
- handlerName = attr.expression.callee.name;
88
- } else if (attr.expression.type === "MemberExpression") {
89
- handlerName = extractMemberExpressionName(attr.expression);
98
+ walkSvelteAst(ast.html || ast.fragment, (node: any) => {
99
+ if (node.type === "Element" || node.type === "InlineComponent" || node.type === "RegularElement" || node.type === "Component") {
100
+ const attributes = node.attributes || [];
101
+
102
+ // Find name attribute
103
+ const nameAttr = attributes.find((attr: any) =>
104
+ attr.type === "Attribute" && attr.name === "name"
105
+ );
106
+
107
+ // Check if element has an onclick/onchange handler and get the handler info
108
+ let foundHandler: { eventType: string; handlerName: string } | null = null;
109
+
110
+ for (const attr of attributes) {
111
+ const isEventHandler = attr.type === "EventHandler";
112
+ const isOnAttribute = attr.type === "Attribute" && attr.name && attr.name.startsWith("on");
113
+
114
+ if (isEventHandler || isOnAttribute) {
115
+ const eventType = attr.name;
116
+ let handlerName = "";
117
+
118
+ if (attr.type === "Attribute" && Array.isArray(attr.value)) {
119
+ const exprValue = attr.value.find((v: any) => v.type === "ExpressionTag" || v.type === "MustacheTag");
120
+ if (exprValue && exprValue.expression) {
121
+ if (exprValue.expression.type === "Identifier") {
122
+ handlerName = exprValue.expression.name;
123
+ } else if (exprValue.expression.type === "CallExpression" && exprValue.expression.callee) {
124
+ handlerName = exprValue.expression.callee.name;
90
125
  }
91
126
  }
92
-
93
- if (handlerName) {
94
- foundHandler = { eventType, handlerName };
95
- break;
127
+ } else if (attr.expression) {
128
+ if (attr.expression.type === "Identifier") {
129
+ handlerName = attr.expression.name;
130
+ } else if (attr.expression.type === "CallExpression" && attr.expression.callee) {
131
+ handlerName = attr.expression.callee.name;
132
+ } else if (attr.expression.type === "MemberExpression") {
133
+ handlerName = extractMemberExpressionName(attr.expression);
96
134
  }
97
135
  }
136
+
137
+ if (handlerName) {
138
+ foundHandler = { eventType, handlerName };
139
+ break;
140
+ }
98
141
  }
142
+ }
99
143
 
100
- if (!foundHandler) return; // No handler found, skip
144
+ if (!foundHandler) return; // No handler found, skip
101
145
 
102
- let componentName: string;
103
- if (nameAttr) {
104
- componentName = getAttributeValue(nameAttr);
105
- } else {
106
- // No name attribute - generate a deterministic one based on position
107
- // Use the handler name and counter for deterministic naming
108
- const positionKey = `${node.name.toLowerCase()}_${autoNameCounter++}`;
109
-
110
- // If data is provided, use/store in $autoNames for persistence across re-renders
111
- if (data) {
112
- if (!data.$autoNames) {
113
- data.$autoNames = {};
114
- }
115
- if (!data.$autoNames[positionKey]) {
116
- data.$autoNames[positionKey] = `__auto_${positionKey}`;
117
- }
118
- componentName = data.$autoNames[positionKey];
119
- } else {
120
- // No data - use deterministic name based on position
121
- componentName = `__auto_${positionKey}`;
122
- }
146
+ let componentName: string;
147
+ if (nameAttr) {
148
+ componentName = getAttributeValue(nameAttr);
149
+ } else {
150
+ // No name attribute - generate a deterministic one based on position
151
+ // Use the handler name and counter for deterministic naming
152
+ const positionKey = `${node.name.toLowerCase()}_${autoNameCounter++}`;
123
153
 
124
- // Track this element for source injection
125
- elementsNeedingNames.push({
126
- node,
127
- name: componentName,
128
- handlerName: foundHandler.handlerName,
129
- eventType: foundHandler.eventType,
130
- element: node.name.toLowerCase()
131
- });
154
+ // If data is provided, use/store in $autoNames for persistence across re-renders
155
+ if (data) {
156
+ if (!data.$autoNames) {
157
+ data.$autoNames = {};
158
+ }
159
+ if (!data.$autoNames[positionKey]) {
160
+ data.$autoNames[positionKey] = `__auto_${positionKey}`;
161
+ }
162
+ componentName = data.$autoNames[positionKey];
163
+ } else {
164
+ // No data - use deterministic name based on position
165
+ componentName = `__auto_${positionKey}`;
132
166
  }
133
167
 
134
- // Add to handlers map
135
- handlers.set(componentName, {
168
+ // Track this element for source injection
169
+ elementsNeedingNames.push({
170
+ node,
136
171
  name: componentName,
137
172
  handlerName: foundHandler.handlerName,
138
173
  eventType: foundHandler.eventType,
139
- element: node.name.toLowerCase(),
174
+ element: node.name.toLowerCase()
140
175
  });
141
176
  }
177
+
178
+ // Add to handlers map
179
+ handlers.set(componentName, {
180
+ name: componentName,
181
+ handlerName: foundHandler.handlerName,
182
+ eventType: foundHandler.eventType,
183
+ element: node.name.toLowerCase(),
184
+ });
142
185
  }
143
186
  });
144
187
 
@@ -318,7 +361,7 @@ function loadModules(imports: ImportInfo[]): { modules: Record<string, any>; var
318
361
  declarations.push(`var ${varName} = __modules__["${varName}"];`);
319
362
  }
320
363
  } catch (err) {
321
- console.error(`[Svelte] Failed to import module "${importInfo.moduleName}":`, err);
364
+ // Module import failed
322
365
  }
323
366
  }
324
367
 
@@ -441,7 +484,6 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
441
484
  // Check for rate limit (429)
442
485
  if (err.status === 429 || (err.message && err.message.includes('rate limit'))) {
443
486
  var retryAfter = err.retry_after || err.retryAfter || 1;
444
- console.log("[Svelte] Rate limited, waiting " + retryAfter + "s before retry");
445
487
  __isRateLimited__ = true;
446
488
  __rateLimitEndTime__ = Date.now() + (retryAfter * 1000);
447
489
 
@@ -451,13 +493,11 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
451
493
  if (retryCount < maxRetries) {
452
494
  resolve(__safeEdit__(editFn, retryCount + 1));
453
495
  } else {
454
- console.error("[Svelte] Max retries reached after rate limit");
455
496
  resolve();
456
497
  }
457
498
  }, retryAfter * 1000);
458
499
  });
459
500
  }
460
- console.error("[Svelte] Edit error:", err.message);
461
501
  return Promise.resolve();
462
502
  });
463
503
  }
@@ -499,8 +539,8 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
499
539
  }
500
540
 
501
541
  // Actual render execution
502
- function __executeRender__() {
503
- var components = __component__.toJSON({ data: __data__ });
542
+ async function __executeRender__() {
543
+ var components = await __component__.toJSON({ data: __data__ });
504
544
 
505
545
  // Try to use current interaction if available
506
546
  if (__ctx__ && __ctx__.interaction) {
@@ -530,7 +570,7 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
530
570
  });
531
571
  }
532
572
  } catch (err) {
533
- console.error("[Svelte] Error in render with interaction:", err.message);
573
+ // Silently fail
534
574
  }
535
575
  }
536
576
 
@@ -544,7 +584,6 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
544
584
  });
545
585
  }
546
586
 
547
- console.error("[Svelte] Cannot render: no interaction or message context");
548
587
  return Promise.resolve();
549
588
  }
550
589
 
@@ -637,7 +676,7 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
637
676
  __throttledRender__(false);
638
677
  }
639
678
  }).catch(function(err) {
640
- console.error("[Svelte] Error in " + prop + ":", err.message);
679
+ // Silently fail
641
680
  });
642
681
  }
643
682
  return result;
@@ -675,30 +714,30 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
675
714
 
676
715
  // Helper: Force update message using interaction.update (for button clicks without reply)
677
716
  // Helper: Force update message using interaction.update (for button clicks without reply)
678
- function update() {
717
+ async function update() {
679
718
  if (!__ctx__ || !__ctx__.interaction) {
680
- console.error("[Svelte] Cannot update: no interaction context");
681
719
  return Promise.resolve();
682
720
  }
683
721
  __autoRenderEnabled__ = false; // Disable auto-render since manual update called
722
+ var components = await __component__.toJSON({ data: __data__ });
684
723
  return __safeEdit__(function() {
685
724
  return __ctx__.interaction.update({
686
- components: __component__.toJSON({ data: __data__ }),
725
+ components: components,
687
726
  flags: ["IsComponentsV2"],
688
727
  });
689
728
  });
690
729
  }
691
730
 
692
731
  // Helper: Force re-render message using message.edit (after reply/followUp)
693
- function rerender() {
732
+ async function rerender() {
694
733
  if (!__ctx__ || !__ctx__.interaction || !__ctx__.interaction.message) {
695
- console.error("[Svelte] Cannot rerender: no message context");
696
734
  return Promise.resolve();
697
735
  }
698
736
  __autoRenderEnabled__ = false; // Disable auto-render since manual rerender called
737
+ var components = await __component__.toJSON({ data: __data__ });
699
738
  return __safeEdit__(function() {
700
739
  return __ctx__.interaction.message.edit({
701
- components: __component__.toJSON({ data: __data__ }),
740
+ components: components,
702
741
  flags: ["IsComponentsV2"],
703
742
  });
704
743
  });
@@ -757,7 +796,7 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
757
796
  __destroyCallbacks__.push(result);
758
797
  }
759
798
  } catch (err) {
760
- console.error("[Svelte] Error in onMount:", err);
799
+ // Mount callback failed
761
800
  }
762
801
  }
763
802
  }
@@ -768,7 +807,7 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
768
807
  try {
769
808
  __destroyCallbacks__[i]();
770
809
  } catch (err) {
771
- console.error("[Svelte] Error in onDestroy:", err);
810
+ // Destroy callback failed
772
811
  }
773
812
  }
774
813
  // Clear pending timeouts
@@ -799,10 +838,6 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
799
838
  };
800
839
  `;
801
840
 
802
- console.log("[Svelte] Processed script:", processedScript);
803
- console.log("[Svelte] Wrapped script:", wrappedScript);
804
- console.log("[Svelte] Initial data:", initialData);
805
-
806
841
  // Create the factory function
807
842
  const factoryFunc = new Function('console', wrappedScript);
808
843
  const createHandlers = factoryFunc(console);
@@ -820,7 +855,7 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
820
855
  try {
821
856
  effect();
822
857
  } catch (error) {
823
- console.error("Error running effect:", error);
858
+ // Effect failed
824
859
  }
825
860
  }
826
861
  };
@@ -839,8 +874,7 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
839
874
  setInHandler: result.setInHandler || (() => { })
840
875
  };
841
876
  } catch (error) {
842
- console.error("Error creating handler context:", error);
843
- console.error("Processed script:", scriptContent);
877
+ // Silently fail and return fallback
844
878
  }
845
879
 
846
880
  // Function to run all effects (fallback)
@@ -849,7 +883,7 @@ export function createHandlerContext(scriptContent: string, initialData: Record<
849
883
  try {
850
884
  effect();
851
885
  } catch (error) {
852
- console.error("Error running effect:", error);
886
+ // Effect failed
853
887
  }
854
888
  }
855
889
  };