@nodestackapp/server 1.3.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (201) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +14 -13
  3. package/dist/NodeStack.d.ts +14 -0
  4. package/dist/NodeStack.d.ts.map +1 -1
  5. package/dist/NodeStack.js +42 -3
  6. package/dist/NodeStack.js.map +1 -1
  7. package/dist/admin/AdminUIBundler.d.ts.map +1 -1
  8. package/dist/admin/AdminUIBundler.js +10 -5
  9. package/dist/admin/AdminUIBundler.js.map +1 -1
  10. package/dist/admin/ui/css/analytics.css +68 -11
  11. package/dist/admin/ui/css/codemirror.css +98 -0
  12. package/dist/admin/ui/css/main.css +372 -78
  13. package/dist/admin/ui/css/rules.css +292 -264
  14. package/dist/admin/ui/css/webhooks.css +34 -10
  15. package/dist/admin/ui/index.html +9326 -5706
  16. package/dist/admin/ui/js/analytics.js +673 -624
  17. package/dist/admin/ui/js/api.js +60 -14
  18. package/dist/admin/ui/js/app.js +480 -263
  19. package/dist/admin/ui/js/apps.js +50 -17
  20. package/dist/admin/ui/js/codemirror.js +15 -0
  21. package/dist/admin/ui/js/collections.js +41 -13
  22. package/dist/admin/ui/js/command-palette.js +61 -19
  23. package/dist/admin/ui/js/demo.js +373 -271
  24. package/dist/admin/ui/js/functions-tester.js +432 -0
  25. package/dist/admin/ui/js/functions-workbench.js +1038 -0
  26. package/dist/admin/ui/js/functions.js +765 -0
  27. package/dist/admin/ui/js/modals.js +20 -16
  28. package/dist/admin/ui/js/plugin-tester.js +562 -559
  29. package/dist/admin/ui/js/plugins.js +221 -90
  30. package/dist/admin/ui/js/records.js +96 -61
  31. package/dist/admin/ui/js/rules.js +1 -1
  32. package/dist/admin/ui/js/schema.js +23 -9
  33. package/dist/admin/ui/js/theme.js +120 -0
  34. package/dist/admin/ui/js/views.js +680 -714
  35. package/dist/admin/ui/js/webhooks.js +1111 -1086
  36. package/dist/apps/AppScope.d.ts +6 -0
  37. package/dist/apps/AppScope.d.ts.map +1 -1
  38. package/dist/apps/AppScope.js +40 -4
  39. package/dist/apps/AppScope.js.map +1 -1
  40. package/dist/auth/ApiKeyService.d.ts +91 -0
  41. package/dist/auth/ApiKeyService.d.ts.map +1 -0
  42. package/dist/auth/ApiKeyService.js +414 -0
  43. package/dist/auth/ApiKeyService.js.map +1 -0
  44. package/dist/auth/AuthService.d.ts +33 -1
  45. package/dist/auth/AuthService.d.ts.map +1 -1
  46. package/dist/auth/AuthService.js +255 -10
  47. package/dist/auth/AuthService.js.map +1 -1
  48. package/dist/auth/oauth/AppleProvider.d.ts +23 -0
  49. package/dist/auth/oauth/AppleProvider.d.ts.map +1 -0
  50. package/dist/auth/oauth/AppleProvider.js +161 -0
  51. package/dist/auth/oauth/AppleProvider.js.map +1 -0
  52. package/dist/auth/oauth/BaseOAuthProvider.d.ts +18 -0
  53. package/dist/auth/oauth/BaseOAuthProvider.d.ts.map +1 -0
  54. package/dist/auth/oauth/BaseOAuthProvider.js +79 -0
  55. package/dist/auth/oauth/BaseOAuthProvider.js.map +1 -0
  56. package/dist/auth/oauth/DiscordProvider.d.ts +11 -0
  57. package/dist/auth/oauth/DiscordProvider.d.ts.map +1 -0
  58. package/dist/auth/oauth/DiscordProvider.js +47 -0
  59. package/dist/auth/oauth/DiscordProvider.js.map +1 -0
  60. package/dist/auth/oauth/GitHubProvider.d.ts +13 -0
  61. package/dist/auth/oauth/GitHubProvider.d.ts.map +1 -0
  62. package/dist/auth/oauth/GitHubProvider.js +90 -0
  63. package/dist/auth/oauth/GitHubProvider.js.map +1 -0
  64. package/dist/auth/oauth/GoogleProvider.d.ts +11 -0
  65. package/dist/auth/oauth/GoogleProvider.d.ts.map +1 -0
  66. package/dist/auth/oauth/GoogleProvider.js +43 -0
  67. package/dist/auth/oauth/GoogleProvider.js.map +1 -0
  68. package/dist/auth/oauth/MicrosoftProvider.d.ts +11 -0
  69. package/dist/auth/oauth/MicrosoftProvider.d.ts.map +1 -0
  70. package/dist/auth/oauth/MicrosoftProvider.js +83 -0
  71. package/dist/auth/oauth/MicrosoftProvider.js.map +1 -0
  72. package/dist/auth/oauth/OAuthProviderRegistry.d.ts +32 -0
  73. package/dist/auth/oauth/OAuthProviderRegistry.d.ts.map +1 -0
  74. package/dist/auth/oauth/OAuthProviderRegistry.js +232 -0
  75. package/dist/auth/oauth/OAuthProviderRegistry.js.map +1 -0
  76. package/dist/auth/oauth/types.d.ts +63 -0
  77. package/dist/auth/oauth/types.d.ts.map +1 -0
  78. package/dist/auth/oauth/types.js +3 -0
  79. package/dist/auth/oauth/types.js.map +1 -0
  80. package/dist/cli/cli.d.ts.map +1 -1
  81. package/dist/cli/cli.js +74 -10
  82. package/dist/cli/cli.js.map +1 -1
  83. package/dist/core/config/ConfigService.d.ts +1 -0
  84. package/dist/core/config/ConfigService.d.ts.map +1 -1
  85. package/dist/core/config/ConfigService.js +4 -1
  86. package/dist/core/config/ConfigService.js.map +1 -1
  87. package/dist/core/container/Tokens.d.ts +4 -0
  88. package/dist/core/container/Tokens.d.ts.map +1 -1
  89. package/dist/core/container/Tokens.js +4 -0
  90. package/dist/core/container/Tokens.js.map +1 -1
  91. package/dist/core/plugins/PluginManager.d.ts.map +1 -1
  92. package/dist/core/plugins/PluginManager.js +73 -19
  93. package/dist/core/plugins/PluginManager.js.map +1 -1
  94. package/dist/database/migrations/SystemMigrations.d.ts.map +1 -1
  95. package/dist/database/migrations/SystemMigrations.js +41 -1
  96. package/dist/database/migrations/SystemMigrations.js.map +1 -1
  97. package/dist/demo/DemoService.d.ts +15 -103
  98. package/dist/demo/DemoService.d.ts.map +1 -1
  99. package/dist/demo/DemoService.js +14 -419
  100. package/dist/demo/DemoService.js.map +1 -1
  101. package/dist/functions/FunctionService.d.ts +80 -0
  102. package/dist/functions/FunctionService.d.ts.map +1 -0
  103. package/dist/functions/FunctionService.js +610 -0
  104. package/dist/functions/FunctionService.js.map +1 -0
  105. package/dist/functions/runtime/IFunctionRuntime.d.ts +23 -0
  106. package/dist/functions/runtime/IFunctionRuntime.d.ts.map +1 -0
  107. package/dist/functions/runtime/IFunctionRuntime.js +3 -0
  108. package/dist/functions/runtime/IFunctionRuntime.js.map +1 -0
  109. package/dist/functions/runtime/QuickJsWasmRuntime.d.ts +21 -0
  110. package/dist/functions/runtime/QuickJsWasmRuntime.d.ts.map +1 -0
  111. package/dist/functions/runtime/QuickJsWasmRuntime.js +418 -0
  112. package/dist/functions/runtime/QuickJsWasmRuntime.js.map +1 -0
  113. package/dist/functions/runtime/SandboxedRunner.d.ts +31 -0
  114. package/dist/functions/runtime/SandboxedRunner.d.ts.map +1 -0
  115. package/dist/functions/runtime/SandboxedRunner.js +61 -0
  116. package/dist/functions/runtime/SandboxedRunner.js.map +1 -0
  117. package/dist/functions/security/SsrfGuard.d.ts +41 -0
  118. package/dist/functions/security/SsrfGuard.d.ts.map +1 -0
  119. package/dist/functions/security/SsrfGuard.js +470 -0
  120. package/dist/functions/security/SsrfGuard.js.map +1 -0
  121. package/dist/functions/types.d.ts +109 -0
  122. package/dist/functions/types.d.ts.map +1 -0
  123. package/dist/functions/types.js +3 -0
  124. package/dist/functions/types.js.map +1 -0
  125. package/dist/http/HttpServer.d.ts +9 -1
  126. package/dist/http/HttpServer.d.ts.map +1 -1
  127. package/dist/http/HttpServer.js +108 -8
  128. package/dist/http/HttpServer.js.map +1 -1
  129. package/dist/http/controllers/ApiKeyController.d.ts +31 -0
  130. package/dist/http/controllers/ApiKeyController.d.ts.map +1 -0
  131. package/dist/http/controllers/ApiKeyController.js +52 -0
  132. package/dist/http/controllers/ApiKeyController.js.map +1 -0
  133. package/dist/http/controllers/AuthController.d.ts +29 -1
  134. package/dist/http/controllers/AuthController.d.ts.map +1 -1
  135. package/dist/http/controllers/AuthController.js +42 -1
  136. package/dist/http/controllers/AuthController.js.map +1 -1
  137. package/dist/http/controllers/DemoController.d.ts.map +1 -1
  138. package/dist/http/controllers/DemoController.js +1 -0
  139. package/dist/http/controllers/DemoController.js.map +1 -1
  140. package/dist/http/controllers/FunctionController.d.ts +64 -0
  141. package/dist/http/controllers/FunctionController.d.ts.map +1 -0
  142. package/dist/http/controllers/FunctionController.js +202 -0
  143. package/dist/http/controllers/FunctionController.js.map +1 -0
  144. package/dist/http/controllers/HealthController.js +2 -2
  145. package/dist/http/controllers/RecordController.d.ts.map +1 -1
  146. package/dist/http/controllers/RecordController.js +4 -0
  147. package/dist/http/controllers/RecordController.js.map +1 -1
  148. package/dist/http/controllers/SnapshotController.d.ts +58 -0
  149. package/dist/http/controllers/SnapshotController.d.ts.map +1 -0
  150. package/dist/http/controllers/SnapshotController.js +79 -0
  151. package/dist/http/controllers/SnapshotController.js.map +1 -0
  152. package/dist/http/middleware/AuthMiddleware.d.ts +6 -1
  153. package/dist/http/middleware/AuthMiddleware.d.ts.map +1 -1
  154. package/dist/http/middleware/AuthMiddleware.js +79 -8
  155. package/dist/http/middleware/AuthMiddleware.js.map +1 -1
  156. package/dist/index.d.ts +15 -2
  157. package/dist/index.d.ts.map +1 -1
  158. package/dist/index.js +28 -3
  159. package/dist/index.js.map +1 -1
  160. package/dist/plugins/chat-notifications.d.ts +51 -0
  161. package/dist/plugins/chat-notifications.d.ts.map +1 -0
  162. package/dist/plugins/chat-notifications.js +545 -0
  163. package/dist/plugins/chat-notifications.js.map +1 -0
  164. package/dist/plugins/index.d.ts +1 -1
  165. package/dist/plugins/index.d.ts.map +1 -1
  166. package/dist/plugins/index.js +1 -1
  167. package/dist/plugins/index.js.map +1 -1
  168. package/dist/plugins/webhooks.d.ts +5 -34
  169. package/dist/plugins/webhooks.d.ts.map +1 -1
  170. package/dist/plugins/webhooks.js +11 -412
  171. package/dist/plugins/webhooks.js.map +1 -1
  172. package/dist/records/MockDataService.d.ts.map +1 -1
  173. package/dist/records/MockDataService.js +3 -0
  174. package/dist/records/MockDataService.js.map +1 -1
  175. package/dist/records/QueryFilterParser.d.ts.map +1 -1
  176. package/dist/records/QueryFilterParser.js +2 -1
  177. package/dist/records/QueryFilterParser.js.map +1 -1
  178. package/dist/records/RecordService.d.ts.map +1 -1
  179. package/dist/records/RecordService.js +70 -43
  180. package/dist/records/RecordService.js.map +1 -1
  181. package/dist/records/RecordValidator.d.ts +6 -0
  182. package/dist/records/RecordValidator.d.ts.map +1 -0
  183. package/dist/records/RecordValidator.js +100 -0
  184. package/dist/records/RecordValidator.js.map +1 -0
  185. package/dist/rules/RuleEngine.d.ts +14 -0
  186. package/dist/rules/RuleEngine.d.ts.map +1 -1
  187. package/dist/rules/RuleEngine.js +144 -1
  188. package/dist/rules/RuleEngine.js.map +1 -1
  189. package/dist/schema/OpenApiGenerator.js +3 -3
  190. package/dist/snapshots/SnapshotService.d.ts +75 -0
  191. package/dist/snapshots/SnapshotService.d.ts.map +1 -0
  192. package/dist/snapshots/SnapshotService.js +775 -0
  193. package/dist/snapshots/SnapshotService.js.map +1 -0
  194. package/dist/snapshots/types.d.ts +63 -0
  195. package/dist/snapshots/types.d.ts.map +1 -0
  196. package/dist/snapshots/types.js +5 -0
  197. package/dist/snapshots/types.js.map +1 -0
  198. package/dist/templates/definitions/ecommerce.js +13 -13
  199. package/dist/templates/definitions/ecommerce.js.map +1 -1
  200. package/dist/webhooks/WebhookService.js +1 -1
  201. package/package.json +4 -3
@@ -0,0 +1,432 @@
1
+ // Serverless Functions Live Sandbox Test Runner & Workbench Logs Module
2
+ // Companion module for NodeStack Admin UI Functions Workbench
3
+
4
+ // ==========================================
5
+ // 1. Sandbox Test Runner Sub-Renderer
6
+ // ==========================================
7
+
8
+ function renderWorkbenchTestPane(fn, isNew) {
9
+ if (isNew) {
10
+ return `
11
+ <div id="fn-editor-pane-test" class="fn-editor-pane" style="display: ${functionsState.editorTab === 'test' ? 'flex' : 'none'}; width: 100%;">
12
+ <div class="card" style="padding: 3.5rem 1.5rem; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 1rem; width: 100%;">
13
+ <div style="width: 54px; height: 54px; border-radius: 50%; background: rgba(99, 102, 241, 0.12); display: flex; align-items: center; justify-content: center; color: var(--accent-primary);">
14
+ <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"/></svg>
15
+ </div>
16
+ <div>
17
+ <h3 style="margin: 0 0 0.5rem 0; font-size: 1.2rem; font-weight: 600;">Function Not Yet Created</h3>
18
+ <p style="margin: 0; color: var(--text-muted); font-size: 0.875rem; max-width: 480px;">
19
+ Create this function first to unlock live sandboxed HTTP executions and real-time response inspecting.
20
+ </p>
21
+ </div>
22
+ <button class="btn btn-primary btn-sm" onclick="saveFunctionEditor()" style="display: flex; align-items: center; gap: 6px; font-weight: 600; margin-top: 4px;">
23
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/><polyline points="17 21 17 13 7 13 7 21"/><polyline points="7 3 7 8 15 8"/></svg>
24
+ <span>Create Function & Unlock Runner</span>
25
+ </button>
26
+ </div>
27
+ </div>
28
+ `;
29
+ }
30
+
31
+ return `
32
+ <div id="fn-editor-pane-test" class="fn-editor-pane" style="display: ${functionsState.editorTab === 'test' ? 'grid' : 'none'}; grid-template-columns: 1fr 1fr; gap: 1rem; align-items: start;">
33
+ <!-- Left: Test Request Builder -->
34
+ <div class="card" style="padding: 1.25rem; display: flex; flex-direction: column; gap: 1rem;">
35
+ <div style="font-weight: 600; font-size: 13px; color: var(--text-heading); display: flex; align-items: center; justify-content: space-between;">
36
+ <span>Simulate Invocation</span>
37
+ <span style="font-size: 11px; font-weight: normal; color: var(--text-muted);">QuickJS Sandbox Execution</span>
38
+ </div>
39
+
40
+ <div style="display: grid; grid-template-columns: 110px 1fr; gap: 10px;">
41
+ <div>
42
+ <label style="display: block; font-size: 11px; font-weight: 600; margin-bottom: 4px;">Method</label>
43
+ <select id="editor-test-method" class="input" style="width: 100%;">
44
+ <option value="POST">POST</option>
45
+ <option value="GET">GET</option>
46
+ <option value="PUT">PUT</option>
47
+ <option value="PATCH">PATCH</option>
48
+ <option value="DELETE">DELETE</option>
49
+ </select>
50
+ </div>
51
+ <div>
52
+ <label style="display: block; font-size: 11px; font-weight: 600; margin-bottom: 4px;">Simulated Caller Role / Auth</label>
53
+ <select id="editor-test-auth" class="input" style="width: 100%;" onchange="toggleEditorTestAuthCustom(this.value)">
54
+ <option value="none">Public (No Authorization Header)</option>
55
+ <option value="admin">Current Admin Session (Bearer Token)</option>
56
+ <option value="custom">Custom Token...</option>
57
+ </select>
58
+ </div>
59
+ </div>
60
+
61
+ <div id="editor-test-custom-auth-row" style="display: none;">
62
+ <label style="display: block; font-size: 11px; font-weight: 600; margin-bottom: 4px;">Custom Token (JWT / API Key)</label>
63
+ <input type="text" id="editor-test-custom-token" class="input" placeholder="eyJhbGciOi..." style="width: 100%; font-family: monospace; font-size: 11px;">
64
+ </div>
65
+
66
+ <div>
67
+ <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px;">
68
+ <label style="font-size: 11px; font-weight: 600;">Custom Request Headers (JSON)</label>
69
+ <button type="button" class="btn btn-secondary btn-sm" onclick="setEditorTestSampleHeaders()" style="font-size: 10px; padding: 1px 6px;">Sample Headers</button>
70
+ </div>
71
+ <textarea id="editor-test-headers-input" style="width: 100%; height: 60px; font-family: monospace; font-size: 11px; padding: 8px; border-radius: 6px; border: 1px solid var(--border-subtle); background: var(--bg-input); color: var(--text-main);" placeholder='{ "Content-Type": "application/json" }'></textarea>
72
+ </div>
73
+
74
+ <div>
75
+ <div style="display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px;">
76
+ <label style="font-size: 11px; font-weight: 600;">Request Body (JSON)</label>
77
+ <div style="display: flex; align-items: center; gap: 6px;">
78
+ <button type="button" class="btn btn-secondary btn-sm" onclick="setEditorTestSampleBody('empty')" style="font-size: 10px; padding: 1px 6px;">{}</button>
79
+ <button type="button" class="btn btn-secondary btn-sm" onclick="setEditorTestSampleBody('sample')" style="font-size: 10px; padding: 1px 6px;">Sample Data</button>
80
+ </div>
81
+ </div>
82
+ <textarea id="editor-test-body" style="width: 100%; height: 160px; font-family: 'JetBrains Mono', Consolas, Monaco, monospace; font-size: 12px; line-height: 1.4; padding: 10px; border-radius: 6px; border: 1px solid var(--border-subtle); background: #0f172a; color: #f8fafc;" placeholder='{ "items": [{ "id": "p1", "price": 49.99 }] }'>{ "name": "NodeStack Developer" }</textarea>
83
+ </div>
84
+
85
+ <div>
86
+ <button class="btn btn-primary" id="btn-run-editor-test" onclick="executeFunctionEditorTest()" style="width: 100%; justify-content: center; font-weight: 600; padding: 8px;">
87
+ ⚡ Run Sandbox Test
88
+ </button>
89
+ </div>
90
+ </div>
91
+
92
+ <!-- Right: Execution Output -->
93
+ <div class="card" style="padding: 1.25rem; display: flex; flex-direction: column; gap: 1rem; min-height: 480px;">
94
+ <div id="editor-test-empty-state" style="display: flex; flex-direction: column; align-items: center; justify-content: center; flex: 1; padding: 3rem 1rem; text-align: center; color: var(--text-muted); gap: 10px;">
95
+ <div style="width: 48px; height: 48px; border-radius: 50%; background: var(--bg-card-hover); display: flex; align-items: center; justify-content: center; color: var(--accent-primary);">
96
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polygon points="5 3 19 12 5 21 5 3"/></svg>
97
+ </div>
98
+ <div style="font-weight: 600; font-size: 13px; color: var(--text-heading);">Ready to Test</div>
99
+ <p style="margin: 0; font-size: 12px; max-width: 320px;">
100
+ Click <strong>Run Sandbox Test</strong> to execute this function isolated in QuickJS and inspect return values, logs, and headers.
101
+ </p>
102
+ </div>
103
+
104
+ <div id="editor-test-result-pane" style="display: none; flex-direction: column; gap: 12px;">
105
+ <div style="display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border-subtle); padding-bottom: 8px;">
106
+ <div style="display: flex; align-items: center; gap: 8px;">
107
+ <span id="editor-test-badge-status" class="badge" style="font-size: 12px; font-weight: 700; border: 1px solid transparent;">HTTP 200</span>
108
+ <span id="editor-test-badge-duration" class="badge" style="background: var(--bg-card-hover); color: var(--text-muted); font-size: 11px;">0ms</span>
109
+ </div>
110
+ <span style="font-size: 11px; color: var(--text-muted);">Sandbox Invocation</span>
111
+ </div>
112
+
113
+ <div>
114
+ <label style="display: block; font-size: 11px; font-weight: 600; margin-bottom: 4px; color: var(--text-muted);">Captured Logs (ctx.log & console.log):</label>
115
+ <pre id="editor-test-logs" style="background: #0f172a; color: #38bdf8; padding: 10px; border-radius: 6px; font-family: monospace; font-size: 11px; max-height: 120px; overflow-y: auto; margin: 0; white-space: pre-wrap;"></pre>
116
+ </div>
117
+
118
+ <div>
119
+ <label style="display: block; font-size: 11px; font-weight: 600; margin-bottom: 4px; color: var(--text-muted);">Response Body:</label>
120
+ <pre id="editor-test-response" style="background: #0f172a; color: #a7f3d0; padding: 12px; border-radius: 6px; font-family: monospace; font-size: 12px; max-height: 240px; overflow-y: auto; margin: 0; white-space: pre-wrap;"></pre>
121
+ </div>
122
+
123
+ <div>
124
+ <label style="display: block; font-size: 11px; font-weight: 600; margin-bottom: 4px; color: var(--text-muted);">Response Headers:</label>
125
+ <pre id="editor-test-headers" style="background: var(--bg-input); color: var(--text-muted); padding: 8px; border-radius: 6px; font-family: monospace; font-size: 11px; max-height: 80px; overflow-y: auto; margin: 0;"></pre>
126
+ </div>
127
+ </div>
128
+ </div>
129
+ </div>
130
+ `;
131
+ }
132
+
133
+ // ==========================================
134
+ // 2. Test Parameter Helpers
135
+ // ==========================================
136
+
137
+ function setEditorTestSampleHeaders() {
138
+ const el = document.getElementById('editor-test-headers-input');
139
+ if (el) el.value = JSON.stringify({ "x-custom-source": "admin-sandbox", "Content-Type": "application/json" }, null, 2);
140
+ }
141
+
142
+ function setEditorTestSampleBody(type) {
143
+ const el = document.getElementById('editor-test-body');
144
+ if (!el) return;
145
+ if (type === 'empty') el.value = '{}';
146
+ else if (type === 'sample') el.value = JSON.stringify({ name: 'NodeStack', amount: 150, items: ['item_1', 'item_2'] }, null, 2);
147
+ }
148
+
149
+ function toggleEditorTestAuthCustom(val) {
150
+ const row = document.getElementById('editor-test-custom-auth-row');
151
+ if (row) row.style.display = val === 'custom' ? 'block' : 'none';
152
+ }
153
+
154
+ // ==========================================
155
+ // 3. Sandbox Execution Engine
156
+ // ==========================================
157
+
158
+ async function executeFunctionEditorTest() {
159
+ const btn = document.getElementById('btn-run-editor-test');
160
+ if (btn) {
161
+ btn.disabled = true;
162
+ btn.textContent = 'Executing...';
163
+ }
164
+
165
+ const fn = functionsState.activeFunctionForEdit;
166
+ if (!fn) return;
167
+
168
+ const outputEmpty = document.getElementById('editor-test-empty-state');
169
+ const outputResult = document.getElementById('editor-test-result-pane');
170
+ const badgeStatus = document.getElementById('editor-test-badge-status');
171
+ const badgeDuration = document.getElementById('editor-test-badge-duration');
172
+ const logsPre = document.getElementById('editor-test-logs');
173
+ const resPre = document.getElementById('editor-test-response');
174
+ const headersPre = document.getElementById('editor-test-headers');
175
+
176
+ try {
177
+ const method = document.getElementById('editor-test-method')?.value || 'POST';
178
+ const authType = document.getElementById('editor-test-auth')?.value || 'none';
179
+ const customAuthToken = document.getElementById('editor-test-custom-token')?.value || '';
180
+
181
+ let body = {};
182
+ const bodyRaw = document.getElementById('editor-test-body')?.value.trim();
183
+ if (bodyRaw) {
184
+ try {
185
+ body = JSON.parse(bodyRaw);
186
+ } catch {
187
+ throw new Error('Mock request body must be valid JSON');
188
+ }
189
+ }
190
+
191
+ let headers = {};
192
+ const headersRaw = document.getElementById('editor-test-headers-input')?.value.trim();
193
+ if (headersRaw) {
194
+ try {
195
+ headers = JSON.parse(headersRaw);
196
+ } catch {
197
+ throw new Error('Request headers must be valid JSON');
198
+ }
199
+ }
200
+
201
+ if (authType === 'admin' && typeof state !== 'undefined' && state.token) {
202
+ headers['authorization'] = `Bearer ${state.token}`;
203
+ } else if (authType === 'custom' && customAuthToken) {
204
+ headers['authorization'] = `Bearer ${customAuthToken.trim()}`;
205
+ }
206
+
207
+ const slug = typeof getActiveAppSlug === 'function' ? getActiveAppSlug() : 'default';
208
+ const endpoint = slug && slug !== 'default'
209
+ ? `/api/apps/${slug}/functions/${fn.id}/test`
210
+ : `/api/functions/${fn.id}/test`;
211
+
212
+ const res = await api(endpoint, {
213
+ method: 'POST',
214
+ headers: { 'Content-Type': 'application/json' },
215
+ body: JSON.stringify({ method, body, headers }),
216
+ });
217
+
218
+ if (outputEmpty) outputEmpty.style.display = 'none';
219
+ if (outputResult) outputResult.style.display = 'flex';
220
+
221
+ const isOk = res.status === 'success';
222
+ const statusColor = isOk ? 'var(--accent-success)' : 'var(--accent-danger)';
223
+ if (badgeStatus) {
224
+ badgeStatus.textContent = `HTTP ${res.statusCode || 200}`;
225
+ badgeStatus.style.color = statusColor;
226
+ badgeStatus.style.borderColor = statusColor;
227
+ }
228
+ if (badgeDuration) {
229
+ badgeDuration.textContent = `${res.durationMs || 0}ms`;
230
+ }
231
+
232
+ if (logsPre) {
233
+ logsPre.textContent = (res.logs && res.logs.length > 0) ? res.logs.join('\n') : '(No console output logged)';
234
+ }
235
+
236
+ if (resPre) {
237
+ if (res.error) {
238
+ resPre.style.color = 'var(--accent-danger)';
239
+ resPre.textContent = res.error;
240
+ } else {
241
+ resPre.style.color = '#a7f3d0';
242
+ resPre.textContent = typeof res.body === 'object' ? JSON.stringify(res.body, null, 2) : String(res.body);
243
+ }
244
+ }
245
+
246
+ if (headersPre) {
247
+ headersPre.textContent = res.headers ? JSON.stringify(res.headers, null, 2) : '{}';
248
+ }
249
+
250
+ if (typeof loadFunctionStats === 'function') {
251
+ await loadFunctionStats();
252
+ }
253
+ } catch (err) {
254
+ if (outputEmpty) outputEmpty.style.display = 'none';
255
+ if (outputResult) outputResult.style.display = 'flex';
256
+ if (badgeStatus) {
257
+ badgeStatus.textContent = 'ERROR';
258
+ badgeStatus.style.color = 'var(--accent-danger)';
259
+ }
260
+ if (resPre) {
261
+ resPre.style.color = 'var(--accent-danger)';
262
+ resPre.textContent = err.message || String(err);
263
+ }
264
+ } finally {
265
+ if (btn) {
266
+ btn.disabled = false;
267
+ btn.textContent = '⚡ Run Sandbox Test';
268
+ }
269
+ }
270
+ }
271
+
272
+ // ==========================================
273
+ // 4. Workbench Function Logs Sub-Renderer & Loader
274
+ // ==========================================
275
+
276
+ function renderWorkbenchLogsPane(fn, isNew) {
277
+ if (isNew) {
278
+ return `
279
+ <div id="fn-editor-pane-logs" class="fn-editor-pane" style="display: ${functionsState.editorTab === 'logs' ? 'flex' : 'none'}; flex-direction: column; gap: 1rem; width: 100%;">
280
+ <div class="card" style="padding: 3.5rem 1.5rem; text-align: center; display: flex; flex-direction: column; align-items: center; gap: 1rem; width: 100%;">
281
+ <div style="width: 54px; height: 54px; border-radius: 50%; background: var(--bg-card-hover); display: flex; align-items: center; justify-content: center; color: var(--text-muted);">
282
+ <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/></svg>
283
+ </div>
284
+ <div>
285
+ <h3 style="margin: 0 0 0.5rem 0; font-size: 1.2rem; font-weight: 600;">No Logs Available</h3>
286
+ <p style="margin: 0; color: var(--text-muted); font-size: 0.875rem; max-width: 440px;">
287
+ Execution history and stdout/stderr traces will be recorded here once this function is deployed and invoked.
288
+ </p>
289
+ </div>
290
+ </div>
291
+ </div>
292
+ `;
293
+ }
294
+
295
+ return `
296
+ <div id="fn-editor-pane-logs" class="fn-editor-pane" style="display: ${functionsState.editorTab === 'logs' ? 'flex' : 'none'}; flex-direction: column; gap: 1rem; width: 100%;">
297
+ <div class="card" style="padding: 1.25rem; display: flex; flex-direction: column; gap: 1rem;">
298
+ <div style="display: flex; align-items: center; justify-content: space-between;">
299
+ <div style="font-weight: 600; font-size: 13px; color: var(--text-heading);">
300
+ Execution History for <code style="color: var(--accent-primary);">${escapeHtml(fn.name)}</code>
301
+ </div>
302
+ <button class="btn btn-secondary btn-sm" onclick="loadFunctionEditorLogs()">
303
+ Refresh Logs
304
+ </button>
305
+ </div>
306
+
307
+ <div id="fn-editor-logs-container">
308
+ <div style="display: flex; align-items: center; justify-content: center; padding: 3rem; color: var(--text-muted);">
309
+ <span class="spinner" style="margin-right: 8px;"></span> Loading logs...
310
+ </div>
311
+ </div>
312
+ </div>
313
+ </div>
314
+ `;
315
+ }
316
+
317
+ function formatTimestampSafe(iso) {
318
+ if (typeof formatTimestamp === 'function') {
319
+ return formatTimestamp(iso);
320
+ }
321
+ if (!iso) return '-';
322
+ try {
323
+ const d = new Date(iso);
324
+ if (isNaN(d.getTime())) return iso;
325
+ return d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' }) + ' ' + d.toLocaleDateString();
326
+ } catch {
327
+ return iso;
328
+ }
329
+ }
330
+
331
+ async function loadFunctionEditorLogs() {
332
+ const container = document.getElementById('fn-editor-logs-container');
333
+ if (!container) return;
334
+
335
+ const fn = functionsState.activeFunctionForEdit;
336
+ if (!fn) return;
337
+
338
+ container.innerHTML = `
339
+ <div style="display: flex; align-items: center; justify-content: center; padding: 3rem; color: var(--text-muted);">
340
+ <span class="spinner" style="margin-right: 8px;"></span> Loading logs for ${escapeHtml(fn.name)}...
341
+ </div>
342
+ `;
343
+
344
+ try {
345
+ const slug = typeof getActiveAppSlug === 'function' ? getActiveAppSlug() : 'default';
346
+ const baseUrl = slug && slug !== 'default' ? `/api/apps/${slug}/functions/logs` : '/api/functions/logs';
347
+ const params = new URLSearchParams({
348
+ functionId: fn.id,
349
+ page: '1',
350
+ perPage: '50',
351
+ });
352
+
353
+ const res = await api(`${baseUrl}?${params.toString()}`);
354
+ const items = res.items || [];
355
+ functionsState.editorLogs = items;
356
+ renderFunctionEditorLogsTable(items);
357
+ } catch (err) {
358
+ container.innerHTML = `
359
+ <div class="card" style="padding: 2rem; text-align: center; color: var(--accent-danger);">
360
+ Failed to load logs: ${escapeHtml(err.message || String(err))}
361
+ </div>
362
+ `;
363
+ }
364
+ }
365
+
366
+ function renderFunctionEditorLogsTable(logs) {
367
+ const container = document.getElementById('fn-editor-logs-container');
368
+ if (!container) return;
369
+
370
+ if (!logs || logs.length === 0) {
371
+ container.innerHTML = `
372
+ <div style="padding: 2.5rem 1rem; text-align: center; color: var(--text-muted); font-size: 13px;">
373
+ No execution logs recorded for this function yet. Switch to the <strong>Live Test Runner</strong> tab to run a sandbox test.
374
+ </div>
375
+ `;
376
+ return;
377
+ }
378
+
379
+ const rows = logs.map(l => {
380
+ const isOk = l.status === 'success';
381
+ const statusBadge = isOk
382
+ ? `<span class="badge" style="background: rgba(16, 185, 129, 0.15); color: #10b981;">${l.statusCode || 200} OK</span>`
383
+ : `<span class="badge" style="background: rgba(239, 68, 68, 0.15); color: #ef4444;">${escapeHtml(l.status.toUpperCase())}</span>`;
384
+
385
+ return `
386
+ <tr>
387
+ <td style="font-size: 12px; color: var(--text-muted);">${formatTimestampSafe(l.timestamp || l.createdAt)}</td>
388
+ <td>${statusBadge}</td>
389
+ <td style="font-size: 12px; font-weight: 600;">${l.durationMs}ms</td>
390
+ <td><code style="font-size: 11px;">${escapeHtml(l.method || 'POST')}</code></td>
391
+ <td style="font-size: 12px; color: var(--text-muted);">${escapeHtml(l.actorId || 'public')}</td>
392
+ <td style="text-align: right;">
393
+ <button class="btn btn-secondary btn-sm" onclick="viewLogPayload('${l.id}')" title="Inspect Request/Response payload" style="font-size: 11px; padding: 2px 7px;">
394
+ Inspect
395
+ </button>
396
+ </td>
397
+ </tr>
398
+ `;
399
+ }).join('');
400
+
401
+ container.innerHTML = `
402
+ <div style="overflow-x: auto;">
403
+ <table class="table" style="width: 100%; border-collapse: collapse; font-size: 13px;">
404
+ <thead>
405
+ <tr style="border-bottom: 1px solid var(--border-subtle); background: var(--bg-card-hover);">
406
+ <th style="text-align: left; padding: 8px 12px;">Timestamp</th>
407
+ <th style="text-align: left; padding: 8px 12px;">Status</th>
408
+ <th style="text-align: left; padding: 8px 12px;">Latency</th>
409
+ <th style="text-align: left; padding: 8px 12px;">Method</th>
410
+ <th style="text-align: left; padding: 8px 12px;">Caller</th>
411
+ <th style="text-align: right; padding: 8px 12px;">Payload</th>
412
+ </tr>
413
+ </thead>
414
+ <tbody>
415
+ ${rows}
416
+ </tbody>
417
+ </table>
418
+ </div>
419
+ `;
420
+ }
421
+
422
+ // Global Exports
423
+ if (typeof window !== 'undefined') {
424
+ window.renderWorkbenchTestPane = renderWorkbenchTestPane;
425
+ window.setEditorTestSampleHeaders = setEditorTestSampleHeaders;
426
+ window.setEditorTestSampleBody = setEditorTestSampleBody;
427
+ window.toggleEditorTestAuthCustom = toggleEditorTestAuthCustom;
428
+ window.executeFunctionEditorTest = executeFunctionEditorTest;
429
+ window.renderWorkbenchLogsPane = renderWorkbenchLogsPane;
430
+ window.loadFunctionEditorLogs = loadFunctionEditorLogs;
431
+ window.renderFunctionEditorLogsTable = renderFunctionEditorLogsTable;
432
+ }