@noego/ioc 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/framework/decorators/Component.cjs.map +1 -1
  2. package/dist/framework/decorators/Component.js.map +1 -1
  3. package/dist/framework/decorators/Inject.cjs.map +1 -1
  4. package/dist/framework/decorators/Inject.js.map +1 -1
  5. package/dist/framework/decorators/index.cjs.map +1 -1
  6. package/dist/framework/decorators/index.js.map +1 -1
  7. package/dist/framework/implementation/Container.cjs +70 -8
  8. package/dist/framework/implementation/Container.cjs.map +1 -1
  9. package/dist/framework/implementation/Container.d.cts +12 -1
  10. package/dist/framework/implementation/Container.d.ts +12 -1
  11. package/dist/framework/implementation/Container.js +61 -9
  12. package/dist/framework/implementation/Container.js.map +1 -1
  13. package/dist/framework/implementation/DecoratorSupport.cjs.map +1 -1
  14. package/dist/framework/implementation/DecoratorSupport.js.map +1 -1
  15. package/dist/framework/implementation/ProxyFactory.cjs +200 -0
  16. package/dist/framework/implementation/ProxyFactory.cjs.map +1 -0
  17. package/dist/framework/implementation/ProxyFactory.d.cts +37 -0
  18. package/dist/framework/implementation/ProxyFactory.d.ts +37 -0
  19. package/dist/framework/implementation/ProxyFactory.js +169 -0
  20. package/dist/framework/implementation/ProxyFactory.js.map +1 -0
  21. package/dist/framework/implementation/TraceCleanup.cjs +83 -0
  22. package/dist/framework/implementation/TraceCleanup.cjs.map +1 -0
  23. package/dist/framework/implementation/TraceCleanup.d.cts +24 -0
  24. package/dist/framework/implementation/TraceCleanup.d.ts +24 -0
  25. package/dist/framework/implementation/TraceCleanup.js +49 -0
  26. package/dist/framework/implementation/TraceCleanup.js.map +1 -0
  27. package/dist/framework/implementation/TraceLogger.cjs +274 -0
  28. package/dist/framework/implementation/TraceLogger.cjs.map +1 -0
  29. package/dist/framework/implementation/TraceLogger.d.cts +82 -0
  30. package/dist/framework/implementation/TraceLogger.d.ts +82 -0
  31. package/dist/framework/implementation/TraceLogger.js +228 -0
  32. package/dist/framework/implementation/TraceLogger.js.map +1 -0
  33. package/dist/framework/interface/Container.cjs.map +1 -1
  34. package/dist/framework/interface/Container.d.cts +9 -1
  35. package/dist/framework/interface/Container.d.ts +9 -1
  36. package/dist/index.cjs.map +1 -1
  37. package/dist/index.js.map +1 -1
  38. package/package.json +9 -3
  39. package/readme.md +130 -3
package/readme.md CHANGED
@@ -4,9 +4,8 @@ A lightweight, flexible Inversion of Control (IoC) container for Node.js and Typ
4
4
 
5
5
  ## Features
6
6
 
7
- **Dual Module Support**: Compatible with CommonJS and ES Modules
8
- **TypeScript & Typings**: Built in TypeScript with bundled declaration files
9
-
7
+ - **Dual Module Support**: Compatible with CommonJS and ES Modules
8
+ - **TypeScript & Typings**: Built in TypeScript with bundled declaration files
10
9
  - **Multiple Lifetime Scopes**: Support for Singleton, Transient, and Scoped dependencies
11
10
  - **Class & Function Registration**: Register both classes and functions as dependencies
12
11
  - **Parameter Injection**: Inject parameter values at resolution time
@@ -14,6 +13,8 @@ A lightweight, flexible Inversion of Control (IoC) container for Node.js and Typ
14
13
  - **Decorator Support**: Use @Component and @Inject decorators for clean, declarative DI
15
14
  - **TypeScript Support**: Built with full TypeScript support for type safety
16
15
  - **Async Resolution**: Support for asynchronous dependency resolution
16
+ - **Method Call Tracing**: Automatic tracing of method calls with performance metrics and dependency hierarchies
17
+ - **Trace Analytics**: Export and analyze traces, track statistics, and monitor dependency interactions
17
18
  - **Lightweight**: Small footprint with minimal external dependencies
18
19
 
19
20
  ## Installation
@@ -413,6 +414,124 @@ childContainer.registerClass(Database, { /* different configuration */ });
413
414
  // Child container uses the new registration
414
415
  ```
415
416
 
417
+ ### Method Call Tracing and Monitoring
418
+
419
+ The container supports automatic tracing of method calls on resolved instances. This is useful for debugging, monitoring, and understanding dependency interactions in your application.
420
+
421
+ #### Enabling Tracing
422
+
423
+ ```typescript
424
+ const container = createContainer();
425
+
426
+ // Enable tracing
427
+ container.setTracingEnabled(true);
428
+
429
+ // Optional: Set trace retention (default is 5 minutes)
430
+ container.setTraceRetentionMinutes(10);
431
+
432
+ // Register your classes
433
+ container.registerClass(DatabaseService);
434
+ container.registerClass(UserService);
435
+
436
+ // When instances are resolved, method calls are automatically traced
437
+ const service = await container.get(UserService);
438
+ service.getUsers(); // This call will be traced
439
+ ```
440
+
441
+ #### Retrieving Traces
442
+
443
+ ```typescript
444
+ // Get recent traces within retention window
445
+ const traces = await container.getTraces();
446
+ console.log(traces);
447
+
448
+ // Get all traces ever recorded
449
+ const allTraces = await container.getAllTraces();
450
+
451
+ // Get trace statistics
452
+ const stats = await container.getTraceStatistics();
453
+ console.log(`Total method calls traced: ${stats.totalTraces}`);
454
+ console.log(`Total proxies created: ${stats.totalProxies}`);
455
+ ```
456
+
457
+ #### How Tracing Works
458
+
459
+ When tracing is enabled:
460
+
461
+ 1. **Automatic Wrapping**: Each resolved instance is wrapped in a JavaScript Proxy that intercepts method calls
462
+ 2. **Call Recording**: Every method call is recorded with:
463
+ - Method name and parameters
464
+ - Return value or error (if thrown)
465
+ - Execution duration in milliseconds
466
+ - Parent-child dependency relationships
467
+ 3. **Zero Overhead When Disabled**: When tracing is disabled, instances are not wrapped and there's no performance impact
468
+ 4. **Database Storage**: Traces are stored in-memory using sql.js (pure JavaScript SQLite)
469
+ 5. **Automatic Cleanup**: Old traces are automatically cleaned up based on retention settings
470
+
471
+ #### Trace Statistics
472
+
473
+ The trace statistics provide insights into your application's dependency interactions:
474
+
475
+ ```typescript
476
+ const stats = await container.getTraceStatistics();
477
+
478
+ // Example output:
479
+ // {
480
+ // totalTraces: 42, // Total method calls recorded
481
+ // totalProxies: 5, // Total unique instances traced
482
+ // proxiesByClass: {
483
+ // UserService: 1,
484
+ // DatabaseService: 1,
485
+ // UserRepository: 1
486
+ // },
487
+ // methodCallsByProxy: {
488
+ // 1: 12, // Proxy 1 had 12 method calls
489
+ // 2: 8, // Proxy 2 had 8 method calls
490
+ // // ...
491
+ // }
492
+ // }
493
+ ```
494
+
495
+ #### Exporting and Analyzing Traces
496
+
497
+ ```typescript
498
+ // Export traces to JSON for analysis
499
+ const exported = await TraceLoggerModule.exportTracesToJson();
500
+ // or use container method
501
+ await container.exportTraces('./traces.json');
502
+
503
+ // Clear traces
504
+ await container.clearTraces();
505
+ ```
506
+
507
+ #### Tracing with Dependency Hierarchies
508
+
509
+ When an instance depends on other instances, the tracing system records the parent-child relationships:
510
+
511
+ ```typescript
512
+ @Component()
513
+ class Database {
514
+ query() { return 'data'; }
515
+ }
516
+
517
+ @Component()
518
+ class UserService {
519
+ constructor(private db: Database) {}
520
+ getUsers() { return this.db.query(); }
521
+ }
522
+
523
+ const container = createContainer();
524
+ container.setTracingEnabled(true);
525
+ container.registerClass(Database);
526
+ container.registerClass(UserService);
527
+
528
+ const service = await container.get(UserService);
529
+ await service.getUsers();
530
+
531
+ // Traces will show the call hierarchy:
532
+ // UserService.getUsers() -> Database.query()
533
+ ```
534
+
416
535
  ## API Reference
417
536
 
418
537
  ### Container
@@ -423,6 +542,14 @@ childContainer.registerClass(Database, { /* different configuration */ });
423
542
  - `instance<T>(classDefinition, params?)`: Resolve a class instance
424
543
  - `get<T>(label, params?)`: Resolve a dependency by key
425
544
  - `extend()`: Create a child container
545
+ - `setTracingEnabled(enabled: boolean)`: Enable/disable method call tracing
546
+ - `isTracingEnabled(): boolean`: Check if tracing is enabled
547
+ - `setTraceRetentionMinutes(minutes: number)`: Set trace retention window
548
+ - `getTraces(retentionMinutes?: number): Promise<TraceRecord[]>`: Get recent traces
549
+ - `getAllTraces(): Promise<TraceRecord[]>`: Get all recorded traces
550
+ - `clearTraces(): Promise<void>`: Clear all traces
551
+ - `exportTraces(filepath: string): Promise<void>`: Export traces to JSON file
552
+ - `getTraceStatistics(): Promise<TraceStatistics>`: Get trace statistics
426
553
 
427
554
  ### Decorators
428
555