@navios/di 0.5.1 → 0.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.
Files changed (122) hide show
  1. package/CHANGELOG.md +145 -0
  2. package/README.md +196 -219
  3. package/docs/README.md +69 -11
  4. package/docs/api-reference.md +281 -117
  5. package/docs/container.md +220 -56
  6. package/docs/examples/request-scope-example.mts +2 -2
  7. package/docs/factory.md +3 -8
  8. package/docs/getting-started.md +37 -8
  9. package/docs/migration.md +318 -37
  10. package/docs/request-contexts.md +263 -175
  11. package/docs/scopes.md +79 -42
  12. package/lib/browser/index.d.mts +1577 -0
  13. package/lib/browser/index.d.mts.map +1 -0
  14. package/lib/browser/index.mjs +3013 -0
  15. package/lib/browser/index.mjs.map +1 -0
  16. package/lib/index-7jfWsiG4.d.mts +1211 -0
  17. package/lib/index-7jfWsiG4.d.mts.map +1 -0
  18. package/lib/index-DW3K5sOX.d.cts +1206 -0
  19. package/lib/index-DW3K5sOX.d.cts.map +1 -0
  20. package/lib/index.cjs +389 -0
  21. package/lib/index.cjs.map +1 -0
  22. package/lib/index.d.cts +376 -0
  23. package/lib/index.d.cts.map +1 -0
  24. package/lib/index.d.mts +371 -78
  25. package/lib/index.d.mts.map +1 -0
  26. package/lib/index.mjs +325 -63
  27. package/lib/index.mjs.map +1 -1
  28. package/lib/testing/index.cjs +9 -0
  29. package/lib/testing/index.d.cts +2 -0
  30. package/lib/testing/index.d.mts +2 -2
  31. package/lib/testing/index.mjs +2 -72
  32. package/lib/testing-BG_fa9TJ.mjs +2656 -0
  33. package/lib/testing-BG_fa9TJ.mjs.map +1 -0
  34. package/lib/testing-DIaIRiJz.cjs +2896 -0
  35. package/lib/testing-DIaIRiJz.cjs.map +1 -0
  36. package/package.json +29 -7
  37. package/project.json +2 -2
  38. package/src/__tests__/async-local-storage.browser.spec.mts +240 -0
  39. package/src/__tests__/async-local-storage.spec.mts +333 -0
  40. package/src/__tests__/container.spec.mts +30 -25
  41. package/src/__tests__/e2e.browser.spec.mts +790 -0
  42. package/src/__tests__/e2e.spec.mts +1222 -0
  43. package/src/__tests__/factory.spec.mts +1 -1
  44. package/src/__tests__/get-injectors.spec.mts +1 -1
  45. package/src/__tests__/injectable.spec.mts +1 -1
  46. package/src/__tests__/injection-token.spec.mts +1 -1
  47. package/src/__tests__/library-findings.spec.mts +563 -0
  48. package/src/__tests__/registry.spec.mts +2 -2
  49. package/src/__tests__/request-scope.spec.mts +266 -274
  50. package/src/__tests__/service-instantiator.spec.mts +18 -17
  51. package/src/__tests__/service-locator-event-bus.spec.mts +9 -9
  52. package/src/__tests__/service-locator-manager.spec.mts +15 -15
  53. package/src/__tests__/service-locator.spec.mts +167 -244
  54. package/src/__tests__/unified-api.spec.mts +27 -27
  55. package/src/__type-tests__/factory.spec-d.mts +2 -2
  56. package/src/__type-tests__/inject.spec-d.mts +2 -2
  57. package/src/__type-tests__/injectable.spec-d.mts +1 -1
  58. package/src/browser.mts +16 -0
  59. package/src/container/container.mts +319 -0
  60. package/src/container/index.mts +2 -0
  61. package/src/container/scoped-container.mts +350 -0
  62. package/src/decorators/factory.decorator.mts +4 -4
  63. package/src/decorators/injectable.decorator.mts +5 -5
  64. package/src/errors/di-error.mts +12 -5
  65. package/src/errors/index.mts +0 -8
  66. package/src/index.mts +156 -15
  67. package/src/interfaces/container.interface.mts +82 -0
  68. package/src/interfaces/factory.interface.mts +2 -2
  69. package/src/interfaces/index.mts +1 -0
  70. package/src/internal/context/async-local-storage.mts +120 -0
  71. package/src/internal/context/factory-context.mts +18 -0
  72. package/src/internal/context/index.mts +3 -0
  73. package/src/{request-context-holder.mts → internal/context/request-context.mts} +40 -27
  74. package/src/internal/context/resolution-context.mts +63 -0
  75. package/src/internal/context/sync-local-storage.mts +51 -0
  76. package/src/internal/core/index.mts +5 -0
  77. package/src/internal/core/instance-resolver.mts +641 -0
  78. package/src/{service-instantiator.mts → internal/core/instantiator.mts} +31 -27
  79. package/src/internal/core/invalidator.mts +437 -0
  80. package/src/internal/core/service-locator.mts +202 -0
  81. package/src/{token-processor.mts → internal/core/token-processor.mts} +79 -60
  82. package/src/{base-instance-holder-manager.mts → internal/holder/base-holder-manager.mts} +91 -21
  83. package/src/internal/holder/holder-manager.mts +85 -0
  84. package/src/internal/holder/holder-storage.interface.mts +116 -0
  85. package/src/internal/holder/index.mts +6 -0
  86. package/src/internal/holder/instance-holder.mts +109 -0
  87. package/src/internal/holder/request-storage.mts +134 -0
  88. package/src/internal/holder/singleton-storage.mts +105 -0
  89. package/src/internal/index.mts +4 -0
  90. package/src/internal/lifecycle/circular-detector.mts +77 -0
  91. package/src/internal/lifecycle/index.mts +2 -0
  92. package/src/{service-locator-event-bus.mts → internal/lifecycle/lifecycle-event-bus.mts} +11 -4
  93. package/src/testing/__tests__/test-container.spec.mts +2 -2
  94. package/src/testing/test-container.mts +4 -4
  95. package/src/token/index.mts +2 -0
  96. package/src/{injection-token.mts → token/injection-token.mts} +1 -1
  97. package/src/{registry.mts → token/registry.mts} +1 -1
  98. package/src/utils/get-injectable-token.mts +1 -1
  99. package/src/utils/get-injectors.mts +32 -15
  100. package/src/utils/types.mts +1 -1
  101. package/tsdown.config.mts +67 -0
  102. package/lib/_tsup-dts-rollup.d.mts +0 -1283
  103. package/lib/_tsup-dts-rollup.d.ts +0 -1283
  104. package/lib/chunk-2M576LCC.mjs +0 -2043
  105. package/lib/chunk-2M576LCC.mjs.map +0 -1
  106. package/lib/index.d.ts +0 -78
  107. package/lib/index.js +0 -2127
  108. package/lib/index.js.map +0 -1
  109. package/lib/testing/index.d.ts +0 -2
  110. package/lib/testing/index.js +0 -2060
  111. package/lib/testing/index.js.map +0 -1
  112. package/lib/testing/index.mjs.map +0 -1
  113. package/src/container.mts +0 -227
  114. package/src/factory-context.mts +0 -8
  115. package/src/instance-resolver.mts +0 -559
  116. package/src/request-context-manager.mts +0 -149
  117. package/src/service-invalidator.mts +0 -429
  118. package/src/service-locator-instance-holder.mts +0 -70
  119. package/src/service-locator-manager.mts +0 -85
  120. package/src/service-locator.mts +0 -246
  121. package/tsup.config.mts +0 -12
  122. /package/src/{injector.mts → injectors.mts} +0 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,145 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.6.1] - 2025-12-20
9
+
10
+ ### Fixed
11
+
12
+ - `DIError` now properly extends `Error` class for correct error handling and stack traces
13
+ - Fixed package.json exports to use correct CommonJS file extensions (`.cjs` and `.d.cts`)
14
+
15
+ ## [0.6.0] - 2025-12-18
16
+
17
+ ### Added
18
+
19
+ - **Circular Dependency Detection**: Automatic detection of circular dependencies with clear error messages
20
+ - Detects cycles when using `inject()` and throws descriptive errors
21
+ - Shows the full cycle path: `ServiceA -> ServiceB -> ServiceA`
22
+ - Use `asyncInject()` to break circular dependencies
23
+ - **Browser Support**: Full browser environment support
24
+ - Dedicated browser entry point via `package.json` exports
25
+ - Automatically uses `SyncLocalStorage` instead of `AsyncLocalStorage` in browsers
26
+ - Works seamlessly with bundlers (webpack, vite, etc.)
27
+ - **Cross-Storage Invalidation**: Proper invalidation of singletons that depend on request-scoped services
28
+ - Singletons are automatically invalidated when their request-scoped dependencies are cleaned up
29
+ - Prevents memory leaks and stale references
30
+ - **Resolution Context**: Uses `AsyncLocalStorage` to track resolution across async boundaries
31
+ - Enables proper circular dependency detection in async scenarios
32
+ - Maintains context across async operations
33
+
34
+ ### Changed
35
+
36
+ - **Type Renames**: Many internal types have been renamed for cleaner naming
37
+ - Old names are still available as deprecated aliases for backward compatibility
38
+ - See migration guide for complete list of renamed types
39
+ - New names: `InstanceHolder`, `InstanceStatus`, `HolderManager`, `LifecycleEventBus`, etc.
40
+
41
+ ### Fixed
42
+
43
+ - Circular dependencies now throw clear errors instead of hanging indefinitely
44
+ - Request-scoped services properly invalidate dependent singletons
45
+ - Browser compatibility issues resolved
46
+
47
+ ## [0.4.0] - 2025
48
+
49
+ ### Added
50
+
51
+ - **ScopedContainer**: New request context management API
52
+ - Eliminates race conditions in concurrent request handling
53
+ - Each request gets its own isolated container
54
+ - Proper cleanup with `endRequest()` method
55
+ - **IContainer Interface**: Common interface for `Container` and `ScopedContainer`
56
+ - Enables polymorphic usage of containers
57
+ - Consistent API across container types
58
+ - **Active Request Tracking**: Methods to track active requests
59
+ - `hasActiveRequest(requestId: string): boolean`
60
+ - `getActiveRequestIds(): ReadonlySet<string>`
61
+ - **Request-Scoped Error Protection**: Clear error when attempting to resolve request-scoped services from `Container`
62
+ - Helpful error message guides users to use `ScopedContainer`
63
+
64
+ ### Changed
65
+
66
+ - **BREAKING**: `Container.beginRequest()` now returns `ScopedContainer` instead of `RequestContextHolder`
67
+ - **BREAKING**: Removed `Container.setCurrentRequestContext(requestId)`
68
+ - **BREAKING**: Removed `Container.getCurrentRequestContext()`
69
+ - **BREAKING**: Removed `Container.endRequest(requestId)` - use `ScopedContainer.endRequest()` instead
70
+
71
+ ### Fixed
72
+
73
+ - Race condition in concurrent request handling
74
+ - Old API could cause request contexts to be mixed when multiple requests processed concurrently
75
+ - New `ScopedContainer` API eliminates this issue completely
76
+
77
+ ### Migration
78
+
79
+ - See [Migration Guide](./docs/migration.md) for detailed migration steps
80
+ - Old request context API is no longer available
81
+ - Update middleware code to use `ScopedContainer` pattern
82
+
83
+ ## [0.3.0] - 2024-XX-XX
84
+
85
+ ### Added
86
+
87
+ - **Request Context Management**: Initial request-scoped service support
88
+ - `Container.beginRequest(requestId, metadata?, priority?)` - returns `RequestContextHolder`
89
+ - `Container.endRequest(requestId)` - cleanup request context
90
+ - `Container.setCurrentRequestContext(requestId)` - set active context
91
+ - `Container.getCurrentRequestContext()` - get current context
92
+ - **RequestContextHolder Interface**: Interface for managing request contexts
93
+ - Enhanced error messages for injection failures
94
+ - Improved TypeScript type definitions
95
+
96
+ ### Changed
97
+
98
+ - Enhanced container API with request context methods
99
+ - Better error messages throughout the library
100
+
101
+ ## [0.2.0] - 2024-XX-XX
102
+
103
+ ### Added
104
+
105
+ - **Basic Dependency Injection**: Core DI functionality
106
+ - `@Injectable()` decorator for service registration
107
+ - `@Factory()` decorator for factory pattern
108
+ - `Container` class for dependency management
109
+ - **Injection Tokens**: Token-based dependency resolution
110
+ - `InjectionToken.create()` for creating tokens
111
+ - `InjectionToken.bound()` for pre-bound values
112
+ - `InjectionToken.factory()` for factory-based resolution
113
+ - Zod schema validation support
114
+ - **Service Lifecycle Hooks**:
115
+ - `OnServiceInit` interface for initialization
116
+ - `OnServiceDestroy` interface for cleanup
117
+ - **Service Scopes**:
118
+ - `InjectableScope.Singleton` - single instance per container
119
+ - `InjectableScope.Transient` - new instance each time
120
+ - `InjectableScope.Request` - instance per request context
121
+ - **Injection Methods**:
122
+ - `inject()` - synchronous injection
123
+ - `asyncInject()` - asynchronous injection
124
+ - `optional()` - optional injection
125
+ - **Registry**: Service registration system
126
+ - `Registry` class for managing service registrations
127
+ - `globalRegistry` for default registrations
128
+
129
+ ## [0.1.0] - 2024-XX-XX
130
+
131
+ ### Added
132
+
133
+ - Initial release
134
+ - Basic dependency injection functionality
135
+
136
+ ---
137
+
138
+ ## Types of Changes
139
+
140
+ - **Added** for new features
141
+ - **Changed** for changes in existing functionality
142
+ - **Deprecated** for soon-to-be removed features
143
+ - **Removed** for now removed features
144
+ - **Fixed** for any bug fixes
145
+ - **Security** for vulnerability fixes