@sparkleideas/shared 3.0.0-alpha.7

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 (96) hide show
  1. package/README.md +323 -0
  2. package/__tests__/hooks/bash-safety.test.ts +289 -0
  3. package/__tests__/hooks/file-organization.test.ts +335 -0
  4. package/__tests__/hooks/git-commit.test.ts +336 -0
  5. package/__tests__/hooks/index.ts +23 -0
  6. package/__tests__/hooks/session-hooks.test.ts +357 -0
  7. package/__tests__/hooks/task-hooks.test.ts +193 -0
  8. package/docs/EVENTS_IMPLEMENTATION_SUMMARY.md +388 -0
  9. package/docs/EVENTS_QUICK_REFERENCE.md +470 -0
  10. package/docs/EVENTS_README.md +352 -0
  11. package/package.json +39 -0
  12. package/src/core/config/defaults.ts +207 -0
  13. package/src/core/config/index.ts +15 -0
  14. package/src/core/config/loader.ts +271 -0
  15. package/src/core/config/schema.ts +188 -0
  16. package/src/core/config/validator.ts +209 -0
  17. package/src/core/event-bus.ts +236 -0
  18. package/src/core/index.ts +22 -0
  19. package/src/core/interfaces/agent.interface.ts +251 -0
  20. package/src/core/interfaces/coordinator.interface.ts +363 -0
  21. package/src/core/interfaces/event.interface.ts +267 -0
  22. package/src/core/interfaces/index.ts +19 -0
  23. package/src/core/interfaces/memory.interface.ts +332 -0
  24. package/src/core/interfaces/task.interface.ts +223 -0
  25. package/src/core/orchestrator/event-coordinator.ts +122 -0
  26. package/src/core/orchestrator/health-monitor.ts +214 -0
  27. package/src/core/orchestrator/index.ts +89 -0
  28. package/src/core/orchestrator/lifecycle-manager.ts +263 -0
  29. package/src/core/orchestrator/session-manager.ts +279 -0
  30. package/src/core/orchestrator/task-manager.ts +317 -0
  31. package/src/events/domain-events.ts +584 -0
  32. package/src/events/event-store.test.ts +387 -0
  33. package/src/events/event-store.ts +588 -0
  34. package/src/events/example-usage.ts +293 -0
  35. package/src/events/index.ts +90 -0
  36. package/src/events/projections.ts +561 -0
  37. package/src/events/state-reconstructor.ts +349 -0
  38. package/src/events.ts +367 -0
  39. package/src/hooks/INTEGRATION.md +658 -0
  40. package/src/hooks/README.md +532 -0
  41. package/src/hooks/example-usage.ts +499 -0
  42. package/src/hooks/executor.ts +379 -0
  43. package/src/hooks/hooks.test.ts +421 -0
  44. package/src/hooks/index.ts +131 -0
  45. package/src/hooks/registry.ts +333 -0
  46. package/src/hooks/safety/bash-safety.ts +604 -0
  47. package/src/hooks/safety/file-organization.ts +473 -0
  48. package/src/hooks/safety/git-commit.ts +623 -0
  49. package/src/hooks/safety/index.ts +46 -0
  50. package/src/hooks/session-hooks.ts +559 -0
  51. package/src/hooks/task-hooks.ts +513 -0
  52. package/src/hooks/types.ts +357 -0
  53. package/src/hooks/verify-exports.test.ts +125 -0
  54. package/src/index.ts +195 -0
  55. package/src/mcp/connection-pool.ts +438 -0
  56. package/src/mcp/index.ts +183 -0
  57. package/src/mcp/server.ts +774 -0
  58. package/src/mcp/session-manager.ts +428 -0
  59. package/src/mcp/tool-registry.ts +566 -0
  60. package/src/mcp/transport/http.ts +557 -0
  61. package/src/mcp/transport/index.ts +294 -0
  62. package/src/mcp/transport/stdio.ts +324 -0
  63. package/src/mcp/transport/websocket.ts +484 -0
  64. package/src/mcp/types.ts +565 -0
  65. package/src/plugin-interface.ts +663 -0
  66. package/src/plugin-loader.ts +638 -0
  67. package/src/plugin-registry.ts +604 -0
  68. package/src/plugins/index.ts +34 -0
  69. package/src/plugins/official/hive-mind-plugin.ts +330 -0
  70. package/src/plugins/official/index.ts +24 -0
  71. package/src/plugins/official/maestro-plugin.ts +508 -0
  72. package/src/plugins/types.ts +108 -0
  73. package/src/resilience/bulkhead.ts +277 -0
  74. package/src/resilience/circuit-breaker.ts +326 -0
  75. package/src/resilience/index.ts +26 -0
  76. package/src/resilience/rate-limiter.ts +420 -0
  77. package/src/resilience/retry.ts +224 -0
  78. package/src/security/index.ts +39 -0
  79. package/src/security/input-validation.ts +265 -0
  80. package/src/security/secure-random.ts +159 -0
  81. package/src/services/index.ts +16 -0
  82. package/src/services/v3-progress.service.ts +505 -0
  83. package/src/types/agent.types.ts +144 -0
  84. package/src/types/index.ts +22 -0
  85. package/src/types/mcp.types.ts +300 -0
  86. package/src/types/memory.types.ts +263 -0
  87. package/src/types/swarm.types.ts +255 -0
  88. package/src/types/task.types.ts +205 -0
  89. package/src/types.ts +367 -0
  90. package/src/utils/secure-logger.d.ts +69 -0
  91. package/src/utils/secure-logger.d.ts.map +1 -0
  92. package/src/utils/secure-logger.js +208 -0
  93. package/src/utils/secure-logger.js.map +1 -0
  94. package/src/utils/secure-logger.ts +257 -0
  95. package/tmp.json +0 -0
  96. package/tsconfig.json +9 -0
@@ -0,0 +1,257 @@
1
+ /**
2
+ * Secure Logger Utility
3
+ *
4
+ * Provides sanitized error logging that strips sensitive information
5
+ * before logging to prevent information disclosure.
6
+ *
7
+ * Security features:
8
+ * - Removes stack traces in production
9
+ * - Sanitizes file paths to prevent internal structure disclosure
10
+ * - Filters sensitive keys from error objects
11
+ * - Truncates long messages to prevent log injection
12
+ *
13
+ * @module @sparkleideas/shared/utils/secure-logger
14
+ */
15
+
16
+ // ============================================================================
17
+ // Configuration
18
+ // ============================================================================
19
+
20
+ interface LoggerConfig {
21
+ /** Environment mode */
22
+ environment: 'development' | 'production' | 'test';
23
+ /** Maximum message length */
24
+ maxMessageLength: number;
25
+ /** Whether to include stack traces */
26
+ includeStackTrace: boolean;
27
+ /** Sensitive keys to filter */
28
+ sensitiveKeys: string[];
29
+ /** Path patterns to sanitize */
30
+ pathPatterns: RegExp[];
31
+ }
32
+
33
+ const DEFAULT_CONFIG: LoggerConfig = {
34
+ environment: (process.env.NODE_ENV as LoggerConfig['environment']) || 'development',
35
+ maxMessageLength: 1000,
36
+ includeStackTrace: process.env.NODE_ENV === 'development',
37
+ sensitiveKeys: [
38
+ 'password', 'passwd', 'secret', 'token', 'apikey', 'api_key',
39
+ 'authorization', 'auth', 'credential', 'private', 'key',
40
+ 'session', 'cookie', 'jwt', 'bearer', 'access_token', 'refresh_token',
41
+ ],
42
+ pathPatterns: [
43
+ /\/home\/[^/]+/g, // Unix home directories
44
+ /\/Users\/[^/]+/g, // macOS home directories
45
+ /C:\\Users\\[^\\]+/gi, // Windows user directories
46
+ /\/var\/[^/]+/g, // Var directories
47
+ /\/tmp\/[^/]+/g, // Temp directories
48
+ ],
49
+ };
50
+
51
+ // ============================================================================
52
+ // Sanitization Functions
53
+ // ============================================================================
54
+
55
+ /**
56
+ * Sanitize a string message
57
+ */
58
+ function sanitizeMessage(message: string, config: LoggerConfig): string {
59
+ let sanitized = message;
60
+
61
+ // Truncate long messages
62
+ if (sanitized.length > config.maxMessageLength) {
63
+ sanitized = sanitized.substring(0, config.maxMessageLength) + '... [truncated]';
64
+ }
65
+
66
+ // Sanitize paths
67
+ for (const pattern of config.pathPatterns) {
68
+ sanitized = sanitized.replace(pattern, '[PATH]');
69
+ }
70
+
71
+ // Remove potential sensitive data patterns
72
+ sanitized = sanitized.replace(/[a-zA-Z0-9+/]{40,}={0,2}/g, '[REDACTED_KEY]');
73
+ sanitized = sanitized.replace(/Bearer\s+[^\s]+/gi, 'Bearer [REDACTED]');
74
+ sanitized = sanitized.replace(/token[=:]\s*[^\s&]+/gi, 'token=[REDACTED]');
75
+
76
+ return sanitized;
77
+ }
78
+
79
+ /**
80
+ * Sanitize an error object
81
+ */
82
+ function sanitizeError(error: unknown, config: LoggerConfig): Record<string, unknown> {
83
+ if (error === null || error === undefined) {
84
+ return { message: 'Unknown error' };
85
+ }
86
+
87
+ if (typeof error === 'string') {
88
+ return { message: sanitizeMessage(error, config) };
89
+ }
90
+
91
+ if (error instanceof Error) {
92
+ const sanitized: Record<string, unknown> = {
93
+ name: error.name,
94
+ message: sanitizeMessage(error.message, config),
95
+ };
96
+
97
+ // Only include stack in development
98
+ if (config.includeStackTrace && error.stack) {
99
+ sanitized.stack = sanitizeMessage(error.stack, config);
100
+ }
101
+
102
+ // Include code if present (common in Node.js errors)
103
+ if ('code' in error) {
104
+ sanitized.code = (error as { code: unknown }).code;
105
+ }
106
+
107
+ return sanitized;
108
+ }
109
+
110
+ if (typeof error === 'object') {
111
+ return sanitizeObject(error as Record<string, unknown>, config);
112
+ }
113
+
114
+ return { message: String(error) };
115
+ }
116
+
117
+ /**
118
+ * Sanitize a plain object
119
+ */
120
+ function sanitizeObject(obj: Record<string, unknown>, config: LoggerConfig): Record<string, unknown> {
121
+ const sanitized: Record<string, unknown> = {};
122
+
123
+ for (const [key, value] of Object.entries(obj)) {
124
+ const lowerKey = key.toLowerCase();
125
+
126
+ // Skip sensitive keys
127
+ if (config.sensitiveKeys.some(sk => lowerKey.includes(sk))) {
128
+ sanitized[key] = '[REDACTED]';
129
+ continue;
130
+ }
131
+
132
+ // Recursively sanitize nested objects
133
+ if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
134
+ sanitized[key] = sanitizeObject(value as Record<string, unknown>, config);
135
+ } else if (typeof value === 'string') {
136
+ sanitized[key] = sanitizeMessage(value, config);
137
+ } else {
138
+ sanitized[key] = value;
139
+ }
140
+ }
141
+
142
+ return sanitized;
143
+ }
144
+
145
+ // ============================================================================
146
+ // Logger Class
147
+ // ============================================================================
148
+
149
+ export class SecureLogger {
150
+ private config: LoggerConfig;
151
+ private prefix: string;
152
+
153
+ constructor(prefix: string = '', config: Partial<LoggerConfig> = {}) {
154
+ this.prefix = prefix;
155
+ this.config = { ...DEFAULT_CONFIG, ...config };
156
+ }
157
+
158
+ /**
159
+ * Log an info message
160
+ */
161
+ info(message: string, data?: Record<string, unknown>): void {
162
+ const sanitizedMessage = sanitizeMessage(message, this.config);
163
+ const sanitizedData = data ? sanitizeObject(data, this.config) : undefined;
164
+
165
+ if (sanitizedData) {
166
+ console.info(`[INFO]${this.prefix ? ` [${this.prefix}]` : ''} ${sanitizedMessage}`, sanitizedData);
167
+ } else {
168
+ console.info(`[INFO]${this.prefix ? ` [${this.prefix}]` : ''} ${sanitizedMessage}`);
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Log a warning message
174
+ */
175
+ warn(message: string, data?: Record<string, unknown>): void {
176
+ const sanitizedMessage = sanitizeMessage(message, this.config);
177
+ const sanitizedData = data ? sanitizeObject(data, this.config) : undefined;
178
+
179
+ if (sanitizedData) {
180
+ console.warn(`[WARN]${this.prefix ? ` [${this.prefix}]` : ''} ${sanitizedMessage}`, sanitizedData);
181
+ } else {
182
+ console.warn(`[WARN]${this.prefix ? ` [${this.prefix}]` : ''} ${sanitizedMessage}`);
183
+ }
184
+ }
185
+
186
+ /**
187
+ * Log an error (sanitized for security)
188
+ */
189
+ error(message: string, error?: unknown): void {
190
+ const sanitizedMessage = sanitizeMessage(message, this.config);
191
+ const sanitizedError = error ? sanitizeError(error, this.config) : undefined;
192
+
193
+ if (sanitizedError) {
194
+ console.error(`[ERROR]${this.prefix ? ` [${this.prefix}]` : ''} ${sanitizedMessage}`, sanitizedError);
195
+ } else {
196
+ console.error(`[ERROR]${this.prefix ? ` [${this.prefix}]` : ''} ${sanitizedMessage}`);
197
+ }
198
+ }
199
+
200
+ /**
201
+ * Log debug message (only in development)
202
+ */
203
+ debug(message: string, data?: Record<string, unknown>): void {
204
+ if (this.config.environment !== 'development') {
205
+ return;
206
+ }
207
+
208
+ const sanitizedMessage = sanitizeMessage(message, this.config);
209
+ const sanitizedData = data ? sanitizeObject(data, this.config) : undefined;
210
+
211
+ if (sanitizedData) {
212
+ console.debug(`[DEBUG]${this.prefix ? ` [${this.prefix}]` : ''} ${sanitizedMessage}`, sanitizedData);
213
+ } else {
214
+ console.debug(`[DEBUG]${this.prefix ? ` [${this.prefix}]` : ''} ${sanitizedMessage}`);
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Create a child logger with a sub-prefix
220
+ */
221
+ child(subPrefix: string): SecureLogger {
222
+ const newPrefix = this.prefix ? `${this.prefix}:${subPrefix}` : subPrefix;
223
+ return new SecureLogger(newPrefix, this.config);
224
+ }
225
+ }
226
+
227
+ // ============================================================================
228
+ // Factory Functions
229
+ // ============================================================================
230
+
231
+ /**
232
+ * Create a secure logger instance
233
+ */
234
+ export function createSecureLogger(prefix?: string, config?: Partial<LoggerConfig>): SecureLogger {
235
+ return new SecureLogger(prefix, config);
236
+ }
237
+
238
+ /**
239
+ * Default logger instance
240
+ */
241
+ export const logger = createSecureLogger('@sparkleideas/claude-flow');
242
+
243
+ /**
244
+ * Sanitize an error for safe logging/display
245
+ */
246
+ export function sanitizeErrorForLogging(error: unknown): Record<string, unknown> {
247
+ return sanitizeError(error, DEFAULT_CONFIG);
248
+ }
249
+
250
+ /**
251
+ * Sanitize a message for safe logging/display
252
+ */
253
+ export function sanitizeMessageForLogging(message: string): string {
254
+ return sanitizeMessage(message, DEFAULT_CONFIG);
255
+ }
256
+
257
+ export default SecureLogger;
package/tmp.json ADDED
File without changes
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "exclude": ["node_modules", "dist"]
9
+ }