@morojs/moro 1.5.17 → 1.6.1
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/README.md +48 -65
- package/dist/core/auth/morojs-adapter.js +12 -16
- package/dist/core/auth/morojs-adapter.js.map +1 -1
- package/dist/core/config/file-loader.d.ts +5 -0
- package/dist/core/config/file-loader.js +171 -0
- package/dist/core/config/file-loader.js.map +1 -1
- package/dist/core/config/index.d.ts +10 -39
- package/dist/core/config/index.js +29 -66
- package/dist/core/config/index.js.map +1 -1
- package/dist/core/config/loader.d.ts +7 -0
- package/dist/core/config/loader.js +269 -0
- package/dist/core/config/loader.js.map +1 -0
- package/dist/core/config/schema.js +31 -41
- package/dist/core/config/schema.js.map +1 -1
- package/dist/core/config/utils.d.ts +2 -9
- package/dist/core/config/utils.js +32 -19
- package/dist/core/config/utils.js.map +1 -1
- package/dist/core/config/validation.d.ts +17 -0
- package/dist/core/config/validation.js +131 -0
- package/dist/core/config/validation.js.map +1 -0
- package/dist/core/database/adapters/mongodb.d.ts +0 -10
- package/dist/core/database/adapters/mongodb.js +2 -23
- package/dist/core/database/adapters/mongodb.js.map +1 -1
- package/dist/core/database/adapters/mysql.d.ts +0 -11
- package/dist/core/database/adapters/mysql.js +0 -1
- package/dist/core/database/adapters/mysql.js.map +1 -1
- package/dist/core/database/adapters/postgresql.d.ts +1 -9
- package/dist/core/database/adapters/postgresql.js +1 -1
- package/dist/core/database/adapters/postgresql.js.map +1 -1
- package/dist/core/database/adapters/redis.d.ts +0 -9
- package/dist/core/database/adapters/redis.js +4 -14
- package/dist/core/database/adapters/redis.js.map +1 -1
- package/dist/core/framework.d.ts +7 -6
- package/dist/core/framework.js +16 -131
- package/dist/core/framework.js.map +1 -1
- package/dist/core/http/http-server.d.ts +0 -12
- package/dist/core/http/http-server.js +23 -151
- package/dist/core/http/http-server.js.map +1 -1
- package/dist/core/http/router.d.ts +0 -12
- package/dist/core/http/router.js +36 -114
- package/dist/core/http/router.js.map +1 -1
- package/dist/core/logger/filters.js +4 -12
- package/dist/core/logger/filters.js.map +1 -1
- package/dist/core/logger/index.d.ts +1 -1
- package/dist/core/logger/index.js +1 -2
- package/dist/core/logger/index.js.map +1 -1
- package/dist/core/logger/logger.d.ts +13 -29
- package/dist/core/logger/logger.js +203 -380
- package/dist/core/logger/logger.js.map +1 -1
- package/dist/core/logger/outputs.js +2 -0
- package/dist/core/logger/outputs.js.map +1 -1
- package/dist/core/middleware/built-in/auth.js +17 -88
- package/dist/core/middleware/built-in/auth.js.map +1 -1
- package/dist/core/middleware/built-in/cache.js +1 -3
- package/dist/core/middleware/built-in/cache.js.map +1 -1
- package/dist/core/middleware/built-in/index.d.ts +0 -1
- package/dist/core/middleware/built-in/index.js +1 -6
- package/dist/core/middleware/built-in/index.js.map +1 -1
- package/dist/core/middleware/built-in/request-logger.js +2 -3
- package/dist/core/middleware/built-in/request-logger.js.map +1 -1
- package/dist/core/middleware/built-in/sse.js +7 -9
- package/dist/core/middleware/built-in/sse.js.map +1 -1
- package/dist/core/modules/auto-discovery.d.ts +0 -17
- package/dist/core/modules/auto-discovery.js +12 -367
- package/dist/core/modules/auto-discovery.js.map +1 -1
- package/dist/core/modules/modules.js +2 -12
- package/dist/core/modules/modules.js.map +1 -1
- package/dist/core/networking/adapters/ws-adapter.d.ts +1 -1
- package/dist/core/networking/adapters/ws-adapter.js +2 -2
- package/dist/core/networking/adapters/ws-adapter.js.map +1 -1
- package/dist/core/networking/service-discovery.js +7 -7
- package/dist/core/networking/service-discovery.js.map +1 -1
- package/dist/core/routing/index.d.ts +0 -20
- package/dist/core/routing/index.js +13 -178
- package/dist/core/routing/index.js.map +1 -1
- package/dist/core/runtime/node-adapter.js +6 -12
- package/dist/core/runtime/node-adapter.js.map +1 -1
- package/dist/moro.d.ts +0 -48
- package/dist/moro.js +148 -456
- package/dist/moro.js.map +1 -1
- package/dist/types/config.d.ts +2 -58
- package/dist/types/core.d.ts +40 -34
- package/dist/types/http.d.ts +1 -16
- package/dist/types/logger.d.ts +0 -7
- package/dist/types/module.d.ts +0 -11
- package/package.json +2 -2
- package/src/core/auth/morojs-adapter.ts +13 -18
- package/src/core/config/file-loader.ts +233 -0
- package/src/core/config/index.ts +32 -77
- package/src/core/config/loader.ts +633 -0
- package/src/core/config/schema.ts +31 -41
- package/src/core/config/utils.ts +29 -22
- package/src/core/config/validation.ts +140 -0
- package/src/core/database/README.md +16 -26
- package/src/core/database/adapters/mongodb.ts +2 -30
- package/src/core/database/adapters/mysql.ts +0 -14
- package/src/core/database/adapters/postgresql.ts +2 -12
- package/src/core/database/adapters/redis.ts +4 -27
- package/src/core/framework.ts +23 -163
- package/src/core/http/http-server.ts +36 -176
- package/src/core/http/router.ts +38 -127
- package/src/core/logger/filters.ts +4 -12
- package/src/core/logger/index.ts +0 -1
- package/src/core/logger/logger.ts +216 -427
- package/src/core/logger/outputs.ts +2 -0
- package/src/core/middleware/built-in/auth.ts +17 -98
- package/src/core/middleware/built-in/cache.ts +1 -3
- package/src/core/middleware/built-in/index.ts +0 -8
- package/src/core/middleware/built-in/request-logger.ts +1 -3
- package/src/core/middleware/built-in/sse.ts +7 -9
- package/src/core/modules/auto-discovery.ts +13 -476
- package/src/core/modules/modules.ts +9 -20
- package/src/core/networking/adapters/ws-adapter.ts +5 -2
- package/src/core/networking/service-discovery.ts +7 -6
- package/src/core/routing/index.ts +14 -198
- package/src/core/runtime/node-adapter.ts +6 -12
- package/src/moro.ts +166 -554
- package/src/types/config.ts +2 -59
- package/src/types/core.ts +45 -47
- package/src/types/http.ts +1 -23
- package/src/types/logger.ts +0 -9
- package/src/types/module.ts +0 -12
- package/dist/core/config/config-manager.d.ts +0 -44
- package/dist/core/config/config-manager.js +0 -114
- package/dist/core/config/config-manager.js.map +0 -1
- package/dist/core/config/config-sources.d.ts +0 -21
- package/dist/core/config/config-sources.js +0 -502
- package/dist/core/config/config-sources.js.map +0 -1
- package/dist/core/config/config-validator.d.ts +0 -21
- package/dist/core/config/config-validator.js +0 -765
- package/dist/core/config/config-validator.js.map +0 -1
- package/dist/core/middleware/built-in/jwt-helpers.d.ts +0 -118
- package/dist/core/middleware/built-in/jwt-helpers.js +0 -221
- package/dist/core/middleware/built-in/jwt-helpers.js.map +0 -1
- package/src/core/config/config-manager.ts +0 -133
- package/src/core/config/config-sources.ts +0 -596
- package/src/core/config/config-validator.ts +0 -1078
- package/src/core/middleware/built-in/jwt-helpers.ts +0 -240
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
// Auto-discovery system for Moro modules
|
|
2
2
|
import { readdirSync, statSync } from 'fs';
|
|
3
|
-
import { join, extname
|
|
3
|
+
import { join, extname } from 'path';
|
|
4
4
|
import { ModuleConfig } from '../../types/module';
|
|
5
5
|
import { DiscoveryOptions } from '../../types/discovery';
|
|
6
|
-
import {
|
|
7
|
-
import { createFrameworkLogger } from '../logger';
|
|
6
|
+
import { logger, createFrameworkLogger } from '../logger';
|
|
8
7
|
|
|
9
8
|
export class ModuleDiscovery {
|
|
10
9
|
private baseDir: string;
|
|
11
10
|
private options: DiscoveryOptions;
|
|
12
11
|
private discoveryLogger = createFrameworkLogger('MODULE_DISCOVERY');
|
|
13
|
-
private watchers: any[] = []; // Store file watchers for cleanup
|
|
14
12
|
|
|
15
13
|
constructor(baseDir: string = process.cwd(), options: DiscoveryOptions = {}) {
|
|
16
14
|
this.baseDir = baseDir;
|
|
@@ -37,7 +35,7 @@ export class ModuleDiscovery {
|
|
|
37
35
|
);
|
|
38
36
|
}
|
|
39
37
|
} catch (error) {
|
|
40
|
-
this.discoveryLogger.warn(`Failed to load module from ${modulePath}
|
|
38
|
+
this.discoveryLogger.warn(`Failed to load module from ${modulePath}:`, 'ModuleDiscovery', {
|
|
41
39
|
error: error instanceof Error ? error.message : String(error),
|
|
42
40
|
});
|
|
43
41
|
}
|
|
@@ -179,483 +177,22 @@ export class ModuleDiscovery {
|
|
|
179
177
|
);
|
|
180
178
|
}
|
|
181
179
|
|
|
182
|
-
// Enhanced auto-discovery with advanced configuration
|
|
183
|
-
async discoverModulesAdvanced(
|
|
184
|
-
config: ModuleDefaultsConfig['autoDiscovery']
|
|
185
|
-
): Promise<ModuleConfig[]> {
|
|
186
|
-
if (!config.enabled) {
|
|
187
|
-
return [];
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const allModules: ModuleConfig[] = [];
|
|
191
|
-
|
|
192
|
-
// Discover from all configured paths
|
|
193
|
-
for (const searchPath of config.paths) {
|
|
194
|
-
const modules = await this.discoverFromPath(searchPath, config);
|
|
195
|
-
allModules.push(...modules);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// Remove duplicates based on name@version
|
|
199
|
-
const uniqueModules = this.deduplicateModules(allModules);
|
|
200
|
-
|
|
201
|
-
// Sort modules based on load order strategy
|
|
202
|
-
const sortedModules = this.sortModules(uniqueModules, config.loadOrder);
|
|
203
|
-
|
|
204
|
-
// Validate dependencies if using dependency order
|
|
205
|
-
if (config.loadOrder === 'dependency') {
|
|
206
|
-
return this.resolveDependencyOrder(sortedModules);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return sortedModules;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Discover modules from a specific path with advanced filtering
|
|
213
|
-
private async discoverFromPath(
|
|
214
|
-
searchPath: string,
|
|
215
|
-
config: ModuleDefaultsConfig['autoDiscovery']
|
|
216
|
-
): Promise<ModuleConfig[]> {
|
|
217
|
-
const modules: ModuleConfig[] = [];
|
|
218
|
-
const fullPath = join(this.baseDir, searchPath);
|
|
219
|
-
|
|
220
|
-
try {
|
|
221
|
-
const stat = statSync(fullPath);
|
|
222
|
-
|
|
223
|
-
if (!stat.isDirectory()) {
|
|
224
|
-
return modules;
|
|
225
|
-
}
|
|
226
|
-
} catch (error) {
|
|
227
|
-
return modules;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
try {
|
|
231
|
-
const files = await this.findMatchingFilesWithGlob(
|
|
232
|
-
fullPath,
|
|
233
|
-
config.patterns,
|
|
234
|
-
config.ignorePatterns,
|
|
235
|
-
config.maxDepth
|
|
236
|
-
);
|
|
237
|
-
|
|
238
|
-
for (const filePath of files) {
|
|
239
|
-
try {
|
|
240
|
-
// Convert relative path to absolute path for import
|
|
241
|
-
const absolutePath = join(this.baseDir, filePath);
|
|
242
|
-
const module = await this.loadModule(absolutePath);
|
|
243
|
-
if (module && this.validateAdvancedModule(module, config)) {
|
|
244
|
-
modules.push(module);
|
|
245
|
-
this.discoveryLogger.info(
|
|
246
|
-
`Auto-discovered module: ${module.name}@${module.version} from ${filePath}`
|
|
247
|
-
);
|
|
248
|
-
}
|
|
249
|
-
} catch (error) {
|
|
250
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
251
|
-
|
|
252
|
-
if (config.failOnError) {
|
|
253
|
-
throw new Error(`Failed to load module from ${filePath}: ${errorMsg}`);
|
|
254
|
-
} else {
|
|
255
|
-
this.discoveryLogger.warn(
|
|
256
|
-
`Failed to load module from ${filePath}`,
|
|
257
|
-
'MODULE_DISCOVERY',
|
|
258
|
-
{
|
|
259
|
-
error: errorMsg,
|
|
260
|
-
}
|
|
261
|
-
);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
} catch (error) {
|
|
266
|
-
if (config.failOnError) {
|
|
267
|
-
throw error;
|
|
268
|
-
}
|
|
269
|
-
// Directory doesn't exist or other error, continue silently
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
return modules;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// Find files matching patterns with ignore support
|
|
276
|
-
private findMatchingFiles(
|
|
277
|
-
basePath: string,
|
|
278
|
-
config: ModuleDefaultsConfig['autoDiscovery'],
|
|
279
|
-
currentDepth: number = 0
|
|
280
|
-
): string[] {
|
|
281
|
-
const files: string[] = [];
|
|
282
|
-
|
|
283
|
-
if (currentDepth >= config.maxDepth) {
|
|
284
|
-
return files;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
try {
|
|
288
|
-
const items = readdirSync(basePath);
|
|
289
|
-
|
|
290
|
-
for (const item of items) {
|
|
291
|
-
const fullPath = join(basePath, item);
|
|
292
|
-
const relativePath = relative(this.baseDir, fullPath);
|
|
293
|
-
|
|
294
|
-
// Check ignore patterns
|
|
295
|
-
if (this.shouldIgnore(relativePath, config.ignorePatterns)) {
|
|
296
|
-
continue;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
const stat = statSync(fullPath);
|
|
300
|
-
|
|
301
|
-
if (stat.isDirectory() && config.recursive) {
|
|
302
|
-
files.push(...this.findMatchingFiles(fullPath, config, currentDepth + 1));
|
|
303
|
-
} else if (stat.isFile()) {
|
|
304
|
-
// Check if file matches any pattern
|
|
305
|
-
if (this.matchesPatterns(relativePath, config.patterns)) {
|
|
306
|
-
files.push(fullPath);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
} catch {
|
|
311
|
-
// Directory not accessible, skip
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
return files;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// Use native Node.js glob to find matching files
|
|
318
|
-
private async findMatchingFilesWithGlob(
|
|
319
|
-
searchPath: string,
|
|
320
|
-
patterns: string[],
|
|
321
|
-
ignorePatterns: string[],
|
|
322
|
-
maxDepth: number = 5
|
|
323
|
-
): Promise<string[]> {
|
|
324
|
-
// Force fallback in CI environments or if Node.js version is uncertain
|
|
325
|
-
const isCI = process.env.CI === 'true' || process.env.GITHUB_ACTIONS === 'true';
|
|
326
|
-
|
|
327
|
-
if (isCI) {
|
|
328
|
-
return this.findMatchingFilesFallback(searchPath, patterns, ignorePatterns, maxDepth);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
const allFiles: string[] = [];
|
|
332
|
-
|
|
333
|
-
try {
|
|
334
|
-
// Try to use native fs.glob if available (Node.js 20+)
|
|
335
|
-
const { glob } = await import('fs/promises');
|
|
336
|
-
|
|
337
|
-
// Check if glob is actually a function and test it
|
|
338
|
-
if (typeof glob !== 'function') {
|
|
339
|
-
return this.findMatchingFilesFallback(searchPath, patterns, ignorePatterns, maxDepth);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// Test glob with a simple pattern first
|
|
343
|
-
try {
|
|
344
|
-
const testIterator = glob(join(searchPath, '*'));
|
|
345
|
-
let testCount = 0;
|
|
346
|
-
for await (const _ of testIterator) {
|
|
347
|
-
testCount++;
|
|
348
|
-
if (testCount > 0) break; // Just test that it works
|
|
349
|
-
}
|
|
350
|
-
} catch (testError) {
|
|
351
|
-
return this.findMatchingFilesFallback(searchPath, patterns, ignorePatterns, maxDepth);
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
for (const pattern of patterns) {
|
|
355
|
-
const fullPattern = join(searchPath, pattern);
|
|
356
|
-
try {
|
|
357
|
-
// fs.glob returns an AsyncIterator, need to collect results
|
|
358
|
-
const globIterator = glob(fullPattern);
|
|
359
|
-
const files: string[] = [];
|
|
360
|
-
|
|
361
|
-
for await (const file of globIterator) {
|
|
362
|
-
const filePath = typeof file === 'string' ? file : (file as any).name || String(file);
|
|
363
|
-
const relativePath = relative(this.baseDir, filePath);
|
|
364
|
-
|
|
365
|
-
// Check if file should be ignored and within max depth
|
|
366
|
-
if (
|
|
367
|
-
!this.shouldIgnore(relativePath, ignorePatterns) &&
|
|
368
|
-
this.isWithinMaxDepth(relativePath, searchPath, maxDepth)
|
|
369
|
-
) {
|
|
370
|
-
files.push(relativePath);
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
allFiles.push(...files);
|
|
375
|
-
} catch (error) {
|
|
376
|
-
// If any glob call fails, fall back to manual discovery
|
|
377
|
-
this.discoveryLogger.warn(`Glob pattern failed: ${pattern}`, String(error));
|
|
378
|
-
return this.findMatchingFilesFallback(searchPath, patterns, ignorePatterns, maxDepth);
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
} catch (error) {
|
|
382
|
-
// fs.glob not available, fall back to manual file discovery
|
|
383
|
-
this.discoveryLogger.debug('Native fs.glob not available, using fallback');
|
|
384
|
-
return this.findMatchingFilesFallback(searchPath, patterns, ignorePatterns, maxDepth);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
return [...new Set(allFiles)]; // Remove duplicates
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
// Fallback for Node.js versions without fs.glob
|
|
391
|
-
private async findMatchingFilesFallback(
|
|
392
|
-
searchPath: string,
|
|
393
|
-
patterns: string[],
|
|
394
|
-
ignorePatterns: string[],
|
|
395
|
-
maxDepth: number = 5
|
|
396
|
-
): Promise<string[]> {
|
|
397
|
-
const config = {
|
|
398
|
-
patterns,
|
|
399
|
-
ignorePatterns,
|
|
400
|
-
maxDepth,
|
|
401
|
-
recursive: true,
|
|
402
|
-
} as ModuleDefaultsConfig['autoDiscovery'];
|
|
403
|
-
|
|
404
|
-
// Handle both absolute and relative paths
|
|
405
|
-
const fullSearchPath = isAbsolute(searchPath) ? searchPath : join(this.baseDir, searchPath);
|
|
406
|
-
|
|
407
|
-
// Check if search path exists
|
|
408
|
-
try {
|
|
409
|
-
const { access } = await import('fs/promises');
|
|
410
|
-
await access(fullSearchPath);
|
|
411
|
-
} catch (e) {
|
|
412
|
-
return [];
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
// Get files and convert to relative paths
|
|
416
|
-
const files = this.findMatchingFiles(fullSearchPath, config);
|
|
417
|
-
const relativeFiles = files.map(file => relative(this.baseDir, file));
|
|
418
|
-
|
|
419
|
-
return relativeFiles;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
// Simple pattern matching for fallback (basic glob support)
|
|
423
|
-
private matchesSimplePattern(path: string, pattern: string): boolean {
|
|
424
|
-
try {
|
|
425
|
-
// Normalize path separators
|
|
426
|
-
const normalizedPath = path.replace(/\\/g, '/');
|
|
427
|
-
const normalizedPattern = pattern.replace(/\\/g, '/');
|
|
428
|
-
|
|
429
|
-
// Convert simple glob patterns to regex
|
|
430
|
-
const regexPattern = normalizedPattern
|
|
431
|
-
.replace(/\*\*/g, '___DOUBLESTAR___') // Temporarily replace ** BEFORE escaping
|
|
432
|
-
.replace(/[.+^${}()|[\]\\]/g, '\\$&') // Escape regex chars
|
|
433
|
-
.replace(/\\\*/g, '[^/]*') // * matches anything except /
|
|
434
|
-
.replace(/___DOUBLESTAR___/g, '.*') // ** matches anything including /
|
|
435
|
-
.replace(/\\\?/g, '[^/]') // ? matches single character except /
|
|
436
|
-
.replace(/\\\{([^}]+)\\\}/g, '($1)') // {ts,js} -> (ts|js)
|
|
437
|
-
.replace(/,/g, '|'); // Convert comma to OR
|
|
438
|
-
|
|
439
|
-
const regex = new RegExp(`^${regexPattern}$`, 'i');
|
|
440
|
-
const result = regex.test(normalizedPath);
|
|
441
|
-
|
|
442
|
-
return result;
|
|
443
|
-
} catch (error) {
|
|
444
|
-
this.discoveryLogger.warn(`Pattern matching error for "${pattern}": ${String(error)}`);
|
|
445
|
-
return false;
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
// Check if path should be ignored
|
|
450
|
-
private shouldIgnore(path: string, ignorePatterns: string[]): boolean {
|
|
451
|
-
return ignorePatterns.some(pattern => this.matchesSimplePattern(path, pattern));
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
// Check if path matches any of the patterns
|
|
455
|
-
private matchesPatterns(path: string, patterns: string[]): boolean {
|
|
456
|
-
return patterns.some(pattern => this.matchesSimplePattern(path, pattern));
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
// Check if file is within max depth (for glob results)
|
|
460
|
-
private isWithinMaxDepth(relativePath: string, searchPath: string, maxDepth: number): boolean {
|
|
461
|
-
// Count directory separators to determine depth
|
|
462
|
-
const pathFromSearch = relative(searchPath, join(this.baseDir, relativePath));
|
|
463
|
-
const depth = pathFromSearch.split('/').length - 1; // -1 because file itself doesn't count as depth
|
|
464
|
-
return depth <= maxDepth;
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
// Remove duplicate modules
|
|
468
|
-
private deduplicateModules(modules: ModuleConfig[]): ModuleConfig[] {
|
|
469
|
-
const seen = new Set<string>();
|
|
470
|
-
return modules.filter(module => {
|
|
471
|
-
const key = `${module.name}@${module.version}`;
|
|
472
|
-
if (seen.has(key)) {
|
|
473
|
-
this.discoveryLogger.warn(`Duplicate module found: ${key}`, 'MODULE_DISCOVERY');
|
|
474
|
-
return false;
|
|
475
|
-
}
|
|
476
|
-
seen.add(key);
|
|
477
|
-
return true;
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
// Sort modules based on strategy
|
|
482
|
-
private sortModules(
|
|
483
|
-
modules: ModuleConfig[],
|
|
484
|
-
strategy: ModuleDefaultsConfig['autoDiscovery']['loadOrder']
|
|
485
|
-
): ModuleConfig[] {
|
|
486
|
-
switch (strategy) {
|
|
487
|
-
case 'alphabetical':
|
|
488
|
-
return modules.sort((a, b) => a.name.localeCompare(b.name));
|
|
489
|
-
|
|
490
|
-
case 'dependency':
|
|
491
|
-
// Will be handled by resolveDependencyOrder
|
|
492
|
-
return modules;
|
|
493
|
-
|
|
494
|
-
case 'custom':
|
|
495
|
-
// Allow custom sorting via module priority (if defined)
|
|
496
|
-
return modules.sort((a, b) => {
|
|
497
|
-
const aPriority = (a.config as any)?.priority || 0;
|
|
498
|
-
const bPriority = (b.config as any)?.priority || 0;
|
|
499
|
-
return bPriority - aPriority; // Higher priority first
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
default:
|
|
503
|
-
return modules;
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
// Resolve dependency order using topological sort
|
|
508
|
-
private resolveDependencyOrder(modules: ModuleConfig[]): ModuleConfig[] {
|
|
509
|
-
const moduleMap = new Map<string, ModuleConfig>();
|
|
510
|
-
const dependencyGraph = new Map<string, string[]>();
|
|
511
|
-
|
|
512
|
-
// Build module map and dependency graph
|
|
513
|
-
modules.forEach(module => {
|
|
514
|
-
const key = `${module.name}@${module.version}`;
|
|
515
|
-
moduleMap.set(key, module);
|
|
516
|
-
dependencyGraph.set(key, module.dependencies || []);
|
|
517
|
-
});
|
|
518
|
-
|
|
519
|
-
// Topological sort
|
|
520
|
-
const visited = new Set<string>();
|
|
521
|
-
const visiting = new Set<string>();
|
|
522
|
-
const sorted: ModuleConfig[] = [];
|
|
523
|
-
|
|
524
|
-
const visit = (moduleKey: string): void => {
|
|
525
|
-
if (visiting.has(moduleKey)) {
|
|
526
|
-
throw new Error(`Circular dependency detected involving ${moduleKey}`);
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
if (visited.has(moduleKey)) {
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
visiting.add(moduleKey);
|
|
534
|
-
|
|
535
|
-
const dependencies = dependencyGraph.get(moduleKey) || [];
|
|
536
|
-
dependencies.forEach(dep => {
|
|
537
|
-
// Find the dependency in our modules
|
|
538
|
-
const depModule = Array.from(moduleMap.keys()).find(key =>
|
|
539
|
-
key.startsWith(`${dep.split('@')[0]}@`)
|
|
540
|
-
);
|
|
541
|
-
if (depModule) {
|
|
542
|
-
visit(depModule);
|
|
543
|
-
}
|
|
544
|
-
});
|
|
545
|
-
|
|
546
|
-
visiting.delete(moduleKey);
|
|
547
|
-
visited.add(moduleKey);
|
|
548
|
-
|
|
549
|
-
const module = moduleMap.get(moduleKey);
|
|
550
|
-
if (module) {
|
|
551
|
-
sorted.push(module);
|
|
552
|
-
}
|
|
553
|
-
};
|
|
554
|
-
|
|
555
|
-
// Visit all modules
|
|
556
|
-
Array.from(moduleMap.keys()).forEach(key => {
|
|
557
|
-
if (!visited.has(key)) {
|
|
558
|
-
visit(key);
|
|
559
|
-
}
|
|
560
|
-
});
|
|
561
|
-
|
|
562
|
-
return sorted;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
// Enhanced module validation
|
|
566
|
-
private validateAdvancedModule(
|
|
567
|
-
module: ModuleConfig,
|
|
568
|
-
_config: ModuleDefaultsConfig['autoDiscovery']
|
|
569
|
-
): boolean {
|
|
570
|
-
// Basic validation
|
|
571
|
-
if (!this.isValidModule(module)) {
|
|
572
|
-
return false;
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
// Additional validation can be added here
|
|
576
|
-
// For example, checking module compatibility, version constraints, etc.
|
|
577
|
-
|
|
578
|
-
return true;
|
|
579
|
-
}
|
|
580
|
-
|
|
581
180
|
// Watch for module changes (for development)
|
|
582
181
|
watchModules(callback: (modules: ModuleConfig[]) => void): void {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
.then(fs => {
|
|
586
|
-
const modulePaths = this.findModuleFiles();
|
|
587
|
-
|
|
588
|
-
modulePaths.forEach(path => {
|
|
589
|
-
try {
|
|
590
|
-
fs.watchFile(path, async () => {
|
|
591
|
-
this.discoveryLogger.info(`Module file changed: ${path}`);
|
|
592
|
-
const modules = await this.discoverModules();
|
|
593
|
-
callback(modules);
|
|
594
|
-
});
|
|
595
|
-
} catch {
|
|
596
|
-
// File watching not supported or failed
|
|
597
|
-
}
|
|
598
|
-
});
|
|
599
|
-
})
|
|
600
|
-
.catch(() => {
|
|
601
|
-
// fs module not available
|
|
602
|
-
});
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
// Watch modules with advanced configuration
|
|
606
|
-
watchModulesAdvanced(
|
|
607
|
-
config: ModuleDefaultsConfig['autoDiscovery'],
|
|
608
|
-
callback: (modules: ModuleConfig[]) => void
|
|
609
|
-
): void {
|
|
610
|
-
if (!config.watchForChanges) {
|
|
611
|
-
return;
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
import('fs')
|
|
615
|
-
.then(fs => {
|
|
616
|
-
const watchedPaths = new Set<string>();
|
|
617
|
-
|
|
618
|
-
// Watch all configured paths
|
|
619
|
-
config.paths.forEach(searchPath => {
|
|
620
|
-
const fullPath = join(this.baseDir, searchPath);
|
|
621
|
-
|
|
622
|
-
try {
|
|
623
|
-
if (statSync(fullPath).isDirectory() && !watchedPaths.has(fullPath)) {
|
|
624
|
-
watchedPaths.add(fullPath);
|
|
625
|
-
|
|
626
|
-
const watcher = fs.watch(
|
|
627
|
-
fullPath,
|
|
628
|
-
{ recursive: config.recursive },
|
|
629
|
-
async (eventType: string, filename: string | null) => {
|
|
630
|
-
if (filename && this.matchesPatterns(filename, config.patterns)) {
|
|
631
|
-
this.discoveryLogger.info(`Module file changed: ${filename}`);
|
|
632
|
-
const modules = await this.discoverModulesAdvanced(config);
|
|
633
|
-
callback(modules);
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
);
|
|
182
|
+
const fs = require('fs');
|
|
183
|
+
const modulePaths = this.findModuleFiles();
|
|
637
184
|
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
185
|
+
modulePaths.forEach(path => {
|
|
186
|
+
try {
|
|
187
|
+
fs.watchFile(path, async () => {
|
|
188
|
+
this.discoveryLogger.info(`Module file changed: ${path}`);
|
|
189
|
+
const modules = await this.discoverModules();
|
|
190
|
+
callback(modules);
|
|
644
191
|
});
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
// fs module not available
|
|
648
|
-
});
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
// Clean up file watchers
|
|
652
|
-
cleanup(): void {
|
|
653
|
-
this.watchers.forEach(watcher => {
|
|
654
|
-
if (watcher && typeof watcher.close === 'function') {
|
|
655
|
-
watcher.close();
|
|
192
|
+
} catch {
|
|
193
|
+
// File watching not supported or failed
|
|
656
194
|
}
|
|
657
195
|
});
|
|
658
|
-
this.watchers = [];
|
|
659
196
|
}
|
|
660
197
|
}
|
|
661
198
|
|
|
@@ -4,7 +4,7 @@ import path from 'path';
|
|
|
4
4
|
import { Container } from '../utilities';
|
|
5
5
|
import { ModuleConfig } from '../../types/module';
|
|
6
6
|
import { ModuleDefinition, ModuleRoute, ModuleSocket } from '../../types/module';
|
|
7
|
-
import { createFrameworkLogger } from '../logger';
|
|
7
|
+
import { logger, createFrameworkLogger } from '../logger';
|
|
8
8
|
|
|
9
9
|
// Module Definition Function
|
|
10
10
|
export function defineModule(definition: ModuleDefinition): ModuleConfig {
|
|
@@ -24,21 +24,6 @@ export function defineModule(definition: ModuleDefinition): ModuleConfig {
|
|
|
24
24
|
cache: route.cache,
|
|
25
25
|
rateLimit: route.rateLimit,
|
|
26
26
|
middleware: route.middleware,
|
|
27
|
-
// Copy all additional properties for extensibility
|
|
28
|
-
...Object.fromEntries(
|
|
29
|
-
Object.entries(route).filter(
|
|
30
|
-
([key]) =>
|
|
31
|
-
![
|
|
32
|
-
'method',
|
|
33
|
-
'path',
|
|
34
|
-
'handler',
|
|
35
|
-
'validation',
|
|
36
|
-
'cache',
|
|
37
|
-
'rateLimit',
|
|
38
|
-
'middleware',
|
|
39
|
-
].includes(key)
|
|
40
|
-
)
|
|
41
|
-
),
|
|
42
27
|
}));
|
|
43
28
|
|
|
44
29
|
// Store the actual route handler functions
|
|
@@ -109,14 +94,18 @@ export class ModuleLoader {
|
|
|
109
94
|
}
|
|
110
95
|
}
|
|
111
96
|
} catch (error) {
|
|
112
|
-
this.moduleLogger.warn(
|
|
113
|
-
|
|
114
|
-
|
|
97
|
+
this.moduleLogger.warn(
|
|
98
|
+
`⚠️ Could not load module from ${modulePath}:`,
|
|
99
|
+
'ModuleLoader',
|
|
100
|
+
{
|
|
101
|
+
error: error instanceof Error ? error.message : String(error),
|
|
102
|
+
}
|
|
103
|
+
);
|
|
115
104
|
}
|
|
116
105
|
}
|
|
117
106
|
}
|
|
118
107
|
} catch (error) {
|
|
119
|
-
this.moduleLogger.error('Failed to discover modules', '
|
|
108
|
+
this.moduleLogger.error('Failed to discover modules:', 'ModuleLoader', {
|
|
120
109
|
error: error instanceof Error ? error.message : String(error),
|
|
121
110
|
});
|
|
122
111
|
}
|
|
@@ -19,9 +19,9 @@ export class WSAdapter implements WebSocketAdapter {
|
|
|
19
19
|
private wss: any; // WebSocket server instance
|
|
20
20
|
private namespaces = new Map<string, WSNamespaceWrapper>();
|
|
21
21
|
private connections = new Map<string, WSConnectionWrapper>();
|
|
22
|
-
private wsLogger = createFrameworkLogger('WEBSOCKET_ADAPTER');
|
|
23
22
|
private customIdGenerator?: () => string;
|
|
24
23
|
private connectionCounter = 0;
|
|
24
|
+
private wsLogger = createFrameworkLogger('WEBSOCKET_ADAPTER');
|
|
25
25
|
|
|
26
26
|
async initialize(httpServer: any, options: WebSocketAdapterOptions = {}): Promise<void> {
|
|
27
27
|
try {
|
|
@@ -100,7 +100,10 @@ export class WSAdapter implements WebSocketAdapter {
|
|
|
100
100
|
// ws library handles compression at the browser level
|
|
101
101
|
// This is a no-op but kept for interface compatibility
|
|
102
102
|
if (enabled) {
|
|
103
|
-
this.wsLogger.warn(
|
|
103
|
+
this.wsLogger.warn(
|
|
104
|
+
'Compression is handled automatically by the ws library and browsers',
|
|
105
|
+
'WSAdapter'
|
|
106
|
+
);
|
|
104
107
|
}
|
|
105
108
|
}
|
|
106
109
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Service Discovery Client for Microservices
|
|
2
2
|
// Supports Consul, Kubernetes, and in-memory registry
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
import { logger, createFrameworkLogger } from '../logger';
|
|
4
5
|
|
|
5
6
|
export interface ServiceInfo {
|
|
6
7
|
name: string;
|
|
@@ -123,7 +124,7 @@ export class ServiceRegistry {
|
|
|
123
124
|
throw new Error(`Consul registration failed: ${response.statusText}`);
|
|
124
125
|
}
|
|
125
126
|
} catch (error) {
|
|
126
|
-
this.serviceLogger.error('Failed to register with Consul', '
|
|
127
|
+
this.serviceLogger.error('Failed to register with Consul:', 'ServiceRegistry', {
|
|
127
128
|
error: error instanceof Error ? error.message : String(error),
|
|
128
129
|
});
|
|
129
130
|
// Fallback to in-memory
|
|
@@ -154,7 +155,7 @@ export class ServiceRegistry {
|
|
|
154
155
|
metadata: entry.Service.Meta,
|
|
155
156
|
}));
|
|
156
157
|
} catch (error) {
|
|
157
|
-
this.serviceLogger.error('Failed to discover from Consul', '
|
|
158
|
+
this.serviceLogger.error('Failed to discover from Consul:', 'ServiceRegistry', {
|
|
158
159
|
error: error instanceof Error ? error.message : String(error),
|
|
159
160
|
});
|
|
160
161
|
return this.discoverFromMemory(serviceName);
|
|
@@ -169,7 +170,7 @@ export class ServiceRegistry {
|
|
|
169
170
|
method: 'PUT',
|
|
170
171
|
});
|
|
171
172
|
} catch (error) {
|
|
172
|
-
this.serviceLogger.error('Failed to deregister from Consul', '
|
|
173
|
+
this.serviceLogger.error('Failed to deregister from Consul:', 'ServiceRegistry', {
|
|
173
174
|
error: error instanceof Error ? error.message : String(error),
|
|
174
175
|
});
|
|
175
176
|
}
|
|
@@ -204,7 +205,7 @@ export class ServiceRegistry {
|
|
|
204
205
|
},
|
|
205
206
|
];
|
|
206
207
|
} catch (error) {
|
|
207
|
-
this.serviceLogger.error('Failed to discover from Kubernetes', '
|
|
208
|
+
this.serviceLogger.error('Failed to discover from Kubernetes:', 'ServiceRegistry', {
|
|
208
209
|
error: error instanceof Error ? error.message : String(error),
|
|
209
210
|
});
|
|
210
211
|
return this.discoverFromMemory(serviceName);
|
|
@@ -241,7 +242,7 @@ export class ServiceRegistry {
|
|
|
241
242
|
this.removeUnhealthyService(serviceName, service);
|
|
242
243
|
}
|
|
243
244
|
} catch (error) {
|
|
244
|
-
this.serviceLogger.warn(`Health check failed for ${serviceName}
|
|
245
|
+
this.serviceLogger.warn(`Health check failed for ${serviceName}:`, 'ServiceRegistry', {
|
|
245
246
|
error: error instanceof Error ? error.message : String(error),
|
|
246
247
|
});
|
|
247
248
|
this.removeUnhealthyService(serviceName, service);
|