@tamyla/clodo-framework 2.0.19 → 3.0.2

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 (56) hide show
  1. package/CHANGELOG.md +206 -62
  2. package/bin/clodo-service.js +32 -56
  3. package/bin/database/README.md +33 -0
  4. package/bin/database/deployment-db-manager.js +527 -0
  5. package/bin/database/enterprise-db-manager.js +736 -0
  6. package/bin/database/wrangler-d1-manager.js +775 -0
  7. package/bin/shared/cloudflare/domain-discovery.js +636 -0
  8. package/bin/shared/cloudflare/domain-manager.js +952 -0
  9. package/bin/shared/cloudflare/index.js +8 -0
  10. package/bin/shared/cloudflare/ops.js +359 -0
  11. package/bin/shared/config/index.js +1 -1
  12. package/bin/shared/database/connection-manager.js +374 -0
  13. package/bin/shared/database/index.js +7 -0
  14. package/bin/shared/database/orchestrator.js +726 -0
  15. package/bin/shared/deployment/auditor.js +969 -0
  16. package/bin/shared/deployment/index.js +10 -0
  17. package/bin/shared/deployment/rollback-manager.js +570 -0
  18. package/bin/shared/deployment/validator.js +779 -0
  19. package/bin/shared/index.js +32 -0
  20. package/bin/shared/monitoring/health-checker.js +484 -0
  21. package/bin/shared/monitoring/index.js +8 -0
  22. package/bin/shared/monitoring/memory-manager.js +387 -0
  23. package/bin/shared/monitoring/production-monitor.js +391 -0
  24. package/bin/shared/production-tester/api-tester.js +82 -0
  25. package/bin/shared/production-tester/auth-tester.js +132 -0
  26. package/bin/shared/production-tester/core.js +197 -0
  27. package/bin/shared/production-tester/database-tester.js +109 -0
  28. package/bin/shared/production-tester/index.js +77 -0
  29. package/bin/shared/production-tester/load-tester.js +131 -0
  30. package/bin/shared/production-tester/performance-tester.js +103 -0
  31. package/bin/shared/security/api-token-manager.js +312 -0
  32. package/bin/shared/security/index.js +8 -0
  33. package/bin/shared/security/secret-generator.js +937 -0
  34. package/bin/shared/security/secure-token-manager.js +398 -0
  35. package/bin/shared/utils/error-recovery.js +225 -0
  36. package/bin/shared/utils/graceful-shutdown-manager.js +390 -0
  37. package/bin/shared/utils/index.js +9 -0
  38. package/bin/shared/utils/interactive-prompts.js +146 -0
  39. package/bin/shared/utils/interactive-utils.js +530 -0
  40. package/bin/shared/utils/rate-limiter.js +246 -0
  41. package/dist/database/database-orchestrator.js +34 -12
  42. package/dist/deployment/index.js +2 -2
  43. package/dist/orchestration/multi-domain-orchestrator.js +8 -6
  44. package/dist/service-management/GenerationEngine.js +76 -28
  45. package/dist/service-management/ServiceInitializer.js +5 -3
  46. package/dist/shared/cloudflare/domain-manager.js +1 -1
  47. package/dist/shared/cloudflare/ops.js +27 -12
  48. package/dist/shared/config/index.js +1 -1
  49. package/dist/shared/deployment/index.js +2 -2
  50. package/dist/shared/security/secret-generator.js +4 -2
  51. package/dist/shared/utils/error-recovery.js +1 -1
  52. package/dist/shared/utils/graceful-shutdown-manager.js +4 -3
  53. package/dist/utils/deployment/secret-generator.js +19 -6
  54. package/package.json +7 -6
  55. package/bin/shared/config/customer-cli.js +0 -182
  56. package/dist/shared/config/customer-cli.js +0 -175
@@ -0,0 +1,390 @@
1
+ /**
2
+ * Graceful Shutdown Manager
3
+ * Implements SIGTERM/SIGINT handlers and cleanup routines for CLI tools
4
+ */
5
+
6
+ export class GracefulShutdownManager {
7
+ constructor(options = {}) {
8
+ this.options = options;
9
+ this.config = null;
10
+ this.isShuttingDown = false;
11
+ this.shutdownHandlers = [];
12
+ this.shutdownPromise = null;
13
+ this.registered = false;
14
+ }
15
+
16
+ /**
17
+ * Initialize with framework configuration
18
+ */
19
+ async initialize() {
20
+ // Import framework config for consistent timing
21
+ const { frameworkConfig } = await import('../../../dist/utils/framework-config.js');
22
+ const timing = frameworkConfig.getTiming();
23
+
24
+ this.config = {
25
+ shutdownTimeout: this.options.shutdownTimeout || timing.shutdownTimeout,
26
+ forceShutdownTimeout: this.options.forceShutdownTimeout || timing.forceShutdownTimeout,
27
+ enableLogging: this.options.enableLogging !== false,
28
+ ...this.options
29
+ };
30
+ }
31
+
32
+ /**
33
+ * Register the shutdown manager
34
+ * @param {boolean} silent - Suppress registration message (for interactive modes)
35
+ */
36
+ register(silent = false) {
37
+ if (this.registered) return;
38
+
39
+ this.registered = true;
40
+
41
+ // Register signal handlers
42
+ process.on('SIGTERM', () => this.initiateShutdown('SIGTERM'));
43
+ process.on('SIGINT', () => this.initiateShutdown('SIGINT'));
44
+
45
+ // Handle uncaught exceptions and unhandled rejections
46
+ process.on('uncaughtException', (error) => {
47
+ console.error('💥 Uncaught exception:', error);
48
+ this.initiateShutdown('uncaughtException', error);
49
+ });
50
+
51
+ process.on('unhandledRejection', (reason, promise) => {
52
+ console.error('💥 Unhandled rejection at:', promise, 'reason:', reason);
53
+ this.initiateShutdown('unhandledRejection', reason);
54
+ });
55
+
56
+ if (this.config.enableLogging && !silent) {
57
+ console.log('🛑 Graceful shutdown manager registered');
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Unregister the shutdown manager
63
+ */
64
+ unregister() {
65
+ if (!this.registered) return;
66
+
67
+ this.registered = false;
68
+
69
+ process.removeAllListeners('SIGTERM');
70
+ process.removeAllListeners('SIGINT');
71
+ process.removeAllListeners('uncaughtException');
72
+ process.removeAllListeners('unhandledRejection');
73
+
74
+ if (this.config.enableLogging) {
75
+ console.log('🛑 Graceful shutdown manager unregistered');
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Add a shutdown handler
81
+ */
82
+ addShutdownHandler(handler, priority = 0) {
83
+ this.shutdownHandlers.push({
84
+ handler,
85
+ priority,
86
+ id: `handler_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
87
+ });
88
+
89
+ // Sort by priority (higher priority first)
90
+ this.shutdownHandlers.sort((a, b) => b.priority - a.priority);
91
+
92
+ // Return removal function
93
+ return () => {
94
+ const index = this.shutdownHandlers.findIndex(h => h.id === this.id);
95
+ if (index !== -1) {
96
+ this.shutdownHandlers.splice(index, 1);
97
+ }
98
+ };
99
+ }
100
+
101
+ /**
102
+ * Remove a shutdown handler
103
+ */
104
+ removeShutdownHandler(handlerId) {
105
+ const index = this.shutdownHandlers.findIndex(h => h.id === handlerId);
106
+ if (index !== -1) {
107
+ this.shutdownHandlers.splice(index, 1);
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Initiate graceful shutdown
113
+ */
114
+ async initiateShutdown(signal, error = null) {
115
+ if (this.isShuttingDown) return;
116
+ this.isShuttingDown = true;
117
+
118
+ if (this.config.enableLogging) {
119
+ console.log(`🛑 Initiating graceful shutdown: ${signal}`);
120
+ if (error) {
121
+ console.error('Shutdown triggered by error:', error);
122
+ }
123
+ }
124
+
125
+ // Prevent new work from starting
126
+ this.preventNewWork();
127
+
128
+ // Execute shutdown handlers
129
+ await this.executeShutdownHandlers(signal, error);
130
+
131
+ // Force exit if handlers don't complete in time
132
+ this.forceExitIfNeeded(signal);
133
+
134
+ // Exit with appropriate code
135
+ const exitCode = error ? 1 : 0;
136
+ process.exit(exitCode);
137
+ }
138
+
139
+ /**
140
+ * Prevent new work from starting
141
+ */
142
+ preventNewWork() {
143
+ // Set global shutdown flag
144
+ if (typeof global !== 'undefined') {
145
+ global.isShuttingDown = true;
146
+ }
147
+
148
+ // Prevent new database connections
149
+ if (typeof global !== 'undefined' && global.dbManagers) {
150
+ global.dbManagers.forEach(manager => {
151
+ if (typeof manager.preventNewConnections === 'function') {
152
+ manager.preventNewConnections();
153
+ }
154
+ });
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Execute shutdown handlers
160
+ */
161
+ async executeShutdownHandlers(signal, error) {
162
+ const shutdownPromises = [];
163
+
164
+ for (const { handler } of this.shutdownHandlers) {
165
+ try {
166
+ const promise = handler(signal, error);
167
+ if (promise && typeof promise.then === 'function') {
168
+ shutdownPromises.push(promise);
169
+ }
170
+ } catch (handlerError) {
171
+ console.error('Error in shutdown handler:', handlerError);
172
+ }
173
+ }
174
+
175
+ // Wait for all handlers to complete or timeout
176
+ if (shutdownPromises.length > 0) {
177
+ try {
178
+ await Promise.race([
179
+ Promise.all(shutdownPromises),
180
+ new Promise(resolve => setTimeout(resolve, this.config.shutdownTimeout))
181
+ ]);
182
+ } catch (timeoutError) {
183
+ console.error('Shutdown handlers timed out:', timeoutError);
184
+ }
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Force exit if needed
190
+ */
191
+ forceExitIfNeeded(signal) {
192
+ setTimeout(() => {
193
+ console.error(`💥 Force exiting after ${this.config.forceShutdownTimeout}ms`);
194
+ process.exit(1);
195
+ }, this.config.forceShutdownTimeout).unref();
196
+ }
197
+
198
+ /**
199
+ * Create a shutdown-aware interval
200
+ */
201
+ createShutdownAwareInterval(callback, delay) {
202
+ const intervalId = setInterval(() => {
203
+ if (this.isShuttingDown) {
204
+ clearInterval(intervalId);
205
+ return;
206
+ }
207
+
208
+ try {
209
+ callback();
210
+ } catch (error) {
211
+ console.error('Error in shutdown-aware interval:', error);
212
+ clearInterval(intervalId);
213
+ }
214
+ }, delay);
215
+
216
+ intervalId.unref?.();
217
+
218
+ // Add cleanup handler
219
+ this.addShutdownHandler(() => {
220
+ clearInterval(intervalId);
221
+ }, 100); // High priority
222
+
223
+ return intervalId;
224
+ }
225
+
226
+ /**
227
+ * Create a shutdown-aware timeout
228
+ */
229
+ createShutdownAwareTimeout(callback, delay) {
230
+ const timeoutId = setTimeout(() => {
231
+ if (this.isShuttingDown) return;
232
+
233
+ try {
234
+ callback();
235
+ } catch (error) {
236
+ console.error('Error in shutdown-aware timeout:', error);
237
+ }
238
+ }, delay);
239
+
240
+ timeoutId.unref?.();
241
+
242
+ // Add cleanup handler
243
+ this.addShutdownHandler(() => {
244
+ clearTimeout(timeoutId);
245
+ }, 100); // High priority
246
+
247
+ return timeoutId;
248
+ }
249
+
250
+ /**
251
+ * Wait for shutdown signal
252
+ */
253
+ async waitForShutdownSignal() {
254
+ return new Promise((resolve) => {
255
+ const handler = (signal) => {
256
+ resolve(signal);
257
+ };
258
+
259
+ process.once('SIGTERM', () => handler('SIGTERM'));
260
+ process.once('SIGINT', () => handler('SIGINT'));
261
+ });
262
+ }
263
+
264
+ /**
265
+ * Get shutdown status
266
+ */
267
+ getShutdownStatus() {
268
+ return {
269
+ isShuttingDown: this.isShuttingDown,
270
+ handlerCount: this.shutdownHandlers.length,
271
+ registered: this.registered,
272
+ config: this.config
273
+ };
274
+ }
275
+
276
+ /**
277
+ * Create database cleanup handler
278
+ */
279
+ createDatabaseCleanupHandler(dbManager) {
280
+ return async (signal) => {
281
+ if (dbManager && typeof dbManager.closeAllConnections === 'function') {
282
+ console.log('🗃️ Closing database connections...');
283
+ await dbManager.closeAllConnections();
284
+ console.log('✅ Database connections closed');
285
+ }
286
+ };
287
+ }
288
+
289
+ /**
290
+ * Create monitoring cleanup handler
291
+ */
292
+ createMonitoringCleanupHandler(monitor) {
293
+ return async (signal) => {
294
+ if (monitor && typeof monitor.stopMonitoring === 'function') {
295
+ console.log('📊 Stopping monitoring...');
296
+ await monitor.stopMonitoring();
297
+ console.log('✅ Monitoring stopped');
298
+ }
299
+ };
300
+ }
301
+
302
+ /**
303
+ * Create token cleanup handler
304
+ */
305
+ createTokenCleanupHandler(tokenManager) {
306
+ return async (signal) => {
307
+ // Token manager doesn't need special cleanup, but we could log the event
308
+ console.log('🔐 Token manager cleanup completed');
309
+ };
310
+ }
311
+
312
+ /**
313
+ * Setup standard handlers for common services
314
+ */
315
+ setupStandardHandlers(services = {}) {
316
+ const { dbManager, monitor, tokenManager, memoryManager } = services;
317
+
318
+ // Database cleanup (high priority)
319
+ if (dbManager) {
320
+ this.addShutdownHandler(this.createDatabaseCleanupHandler(dbManager), 1000);
321
+ }
322
+
323
+ // Monitoring cleanup (medium priority)
324
+ if (monitor) {
325
+ this.addShutdownHandler(this.createMonitoringCleanupHandler(monitor), 500);
326
+ }
327
+
328
+ // Memory cleanup (medium priority)
329
+ if (memoryManager) {
330
+ this.addShutdownHandler(async (signal) => {
331
+ console.log('🧠 Stopping memory monitoring...');
332
+ memoryManager.stopMonitoring();
333
+ console.log('✅ Memory monitoring stopped');
334
+ }, 500);
335
+ }
336
+
337
+ // Token cleanup (low priority)
338
+ if (tokenManager) {
339
+ this.addShutdownHandler(this.createTokenCleanupHandler(tokenManager), 100);
340
+ }
341
+
342
+ // Final cleanup (lowest priority)
343
+ this.addShutdownHandler(async (signal) => {
344
+ console.log('🧹 Running final cleanup...');
345
+ // Force garbage collection if available
346
+ if (global.gc) {
347
+ global.gc();
348
+ }
349
+ console.log('✅ Final cleanup completed');
350
+ }, 1);
351
+ }
352
+ }
353
+
354
+ // Global shutdown manager instance
355
+ let globalShutdownManager = null;
356
+
357
+ /**
358
+ * Get the global shutdown manager instance
359
+ */
360
+ export function getShutdownManager(options = {}) {
361
+ if (!globalShutdownManager) {
362
+ globalShutdownManager = new GracefulShutdownManager(options);
363
+ }
364
+ return globalShutdownManager;
365
+ }
366
+
367
+ /**
368
+ * Initialize graceful shutdown handling
369
+ */
370
+ export async function initializeGracefulShutdown(services = {}, options = {}) {
371
+ const manager = getShutdownManager(options);
372
+ await manager.initialize();
373
+ manager.setupStandardHandlers(services);
374
+ manager.register();
375
+ return manager;
376
+ }
377
+
378
+ /**
379
+ * Quick shutdown for simple scripts
380
+ */
381
+ export async function withGracefulShutdown(callback, services = {}, options = {}) {
382
+ const manager = initializeGracefulShutdown(services, options);
383
+
384
+ try {
385
+ await callback();
386
+ } catch (error) {
387
+ console.error('Error in main execution:', error);
388
+ await manager.initiateShutdown('error', error);
389
+ }
390
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Utilities Module
3
+ * Exports all general utility functions and managers
4
+ */
5
+
6
+ export { InteractivePrompts } from './interactive-prompts.js';
7
+ export { ErrorRecovery } from './error-recovery.js';
8
+ export { GracefulShutdownManager } from './graceful-shutdown-manager.js';
9
+ export { RateLimiter } from './rate-limiter.js';
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Interactive Prompts Module
3
+ * Standardized user input functions for all scripts
4
+ *
5
+ * Replaces duplicate prompt code across 10+ scripts
6
+ */
7
+
8
+ import readline from 'readline';
9
+
10
+ // Create readline interface (singleton)
11
+ let rl = null;
12
+ function getReadlineInterface() {
13
+ if (!rl) {
14
+ rl = readline.createInterface({
15
+ input: process.stdin,
16
+ output: process.stdout
17
+ });
18
+ }
19
+ return rl;
20
+ }
21
+
22
+ export function askUser(question, defaultValue = null) {
23
+ return new Promise((resolve) => {
24
+ const prompt = defaultValue ? `${question} [${defaultValue}]: ` : `${question}: `;
25
+ getReadlineInterface().question(prompt, (answer) => {
26
+ resolve(answer.trim() || defaultValue);
27
+ });
28
+ });
29
+ }
30
+
31
+ export function askYesNo(question, defaultValue = 'n') {
32
+ return new Promise((resolve) => {
33
+ const prompt = `${question} [y/N]: `;
34
+ getReadlineInterface().question(prompt, (answer) => {
35
+ const response = answer.trim().toLowerCase() || defaultValue;
36
+ resolve(response === 'y' || response === 'yes');
37
+ });
38
+ });
39
+ }
40
+
41
+ export function askChoice(question, choices, defaultIndex = 0) {
42
+ return new Promise((resolve) => {
43
+ console.log(`\n${question}`);
44
+ choices.forEach((choice, index) => {
45
+ const marker = index === defaultIndex ? '>' : ' ';
46
+ console.log(`${marker} ${index + 1}. ${choice}`);
47
+ });
48
+
49
+ getReadlineInterface().question(`\nSelect option [1-${choices.length}]: `, (answer) => {
50
+ const choice = parseInt(answer) - 1;
51
+ if (isNaN(choice) || choice < 0 || choice >= choices.length) {
52
+ resolve(defaultIndex);
53
+ } else {
54
+ resolve(choice);
55
+ }
56
+ });
57
+ });
58
+ }
59
+
60
+ export function askMultiChoice(question, choices, defaultIndices = []) {
61
+ return new Promise((resolve) => {
62
+ console.log(`\n${question}`);
63
+ console.log('(Enter comma-separated numbers, e.g., "1,3,5")');
64
+ choices.forEach((choice, index) => {
65
+ const marker = defaultIndices.includes(index) ? '>' : ' ';
66
+ console.log(`${marker} ${index + 1}. ${choice}`);
67
+ });
68
+
69
+ const defaultStr = defaultIndices.map(i => i + 1).join(',');
70
+ getReadlineInterface().question(`\nSelect options [${defaultStr}]: `, (answer) => {
71
+ if (!answer.trim()) {
72
+ resolve(defaultIndices);
73
+ return;
74
+ }
75
+
76
+ const selected = answer.split(',')
77
+ .map(s => parseInt(s.trim()) - 1)
78
+ .filter(i => !isNaN(i) && i >= 0 && i < choices.length);
79
+
80
+ resolve(selected.length > 0 ? selected : defaultIndices);
81
+ });
82
+ });
83
+ }
84
+
85
+ export function closePrompts() {
86
+ if (rl) {
87
+ rl.close();
88
+ rl = null;
89
+ }
90
+ }
91
+
92
+ // Utility function for progress indicators
93
+ export function showProgress(message, steps = ['⏳', '⚡', '✅']) {
94
+ return new Promise((resolve) => {
95
+ let stepIndex = 0;
96
+ const interval = setInterval(() => {
97
+ process.stdout.write(`\r${steps[stepIndex]} ${message}`);
98
+ stepIndex = (stepIndex + 1) % steps.length;
99
+ }, 500);
100
+
101
+ // Auto-resolve after 2 seconds or when manually resolved
102
+ setTimeout(() => {
103
+ clearInterval(interval);
104
+ process.stdout.write(`\r✅ ${message}\n`);
105
+ resolve();
106
+ }, 2000);
107
+ });
108
+ }
109
+
110
+ /**
111
+ * Ask for sensitive input (like API tokens) with hidden input
112
+ */
113
+ export function askPassword(question) {
114
+ return new Promise((resolve) => {
115
+ const prompt = `${question}: `;
116
+ process.stdout.write(prompt);
117
+
118
+ // Hide input for sensitive data
119
+ process.stdin.setRawMode(true);
120
+ process.stdin.resume();
121
+
122
+ let password = '';
123
+
124
+ const onData = (char) => {
125
+ const charCode = char[0];
126
+
127
+ if (charCode === 13) { // Enter key
128
+ process.stdin.setRawMode(false);
129
+ process.stdin.pause();
130
+ process.stdin.removeListener('data', onData);
131
+ process.stdout.write('\n');
132
+ resolve(password);
133
+ } else if (charCode === 127 || charCode === 8) { // Backspace
134
+ if (password.length > 0) {
135
+ password = password.slice(0, -1);
136
+ process.stdout.write('\b \b');
137
+ }
138
+ } else if (charCode >= 32 && charCode <= 126) { // Printable characters
139
+ password += char.toString();
140
+ process.stdout.write('*');
141
+ }
142
+ };
143
+
144
+ process.stdin.on('data', onData);
145
+ });
146
+ }