@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
@@ -1,246 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- /* eslint-disable @typescript-eslint/no-empty-object-type */
3
- import type { z, ZodObject, ZodOptional } from 'zod/v4'
4
-
5
- import type {
6
- AnyInjectableType,
7
- InjectionTokenSchemaType,
8
- } from './injection-token.mjs'
9
- import type { Registry } from './registry.mjs'
10
- import type { RequestContextHolder } from './request-context-holder.mjs'
11
- import type { ClearAllOptions } from './service-invalidator.mjs'
12
- import type { Injectors } from './utils/index.mjs'
13
-
14
- import { defaultInjectors } from './injector.mjs'
15
- import { InstanceResolver } from './instance-resolver.mjs'
16
- import { globalRegistry } from './registry.mjs'
17
- import { RequestContextManager } from './request-context-manager.mjs'
18
- import { ServiceInstantiator } from './service-instantiator.mjs'
19
- import { ServiceInvalidator } from './service-invalidator.mjs'
20
- import { ServiceLocatorEventBus } from './service-locator-event-bus.mjs'
21
- import { ServiceLocatorManager } from './service-locator-manager.mjs'
22
- import { TokenProcessor } from './token-processor.mjs'
23
-
24
- export class ServiceLocator {
25
- private readonly eventBus: ServiceLocatorEventBus
26
- private readonly manager: ServiceLocatorManager
27
- private readonly serviceInstantiator: ServiceInstantiator
28
- private readonly tokenProcessor: TokenProcessor
29
- private readonly requestContextManager: RequestContextManager
30
- private readonly serviceInvalidator: ServiceInvalidator
31
- private readonly instanceResolver: InstanceResolver
32
-
33
- constructor(
34
- private readonly registry: Registry = globalRegistry,
35
- private readonly logger: Console | null = null,
36
- private readonly injectors: Injectors = defaultInjectors,
37
- ) {
38
- this.eventBus = new ServiceLocatorEventBus(logger)
39
- this.manager = new ServiceLocatorManager(logger)
40
- this.serviceInstantiator = new ServiceInstantiator(injectors)
41
- this.tokenProcessor = new TokenProcessor(logger)
42
- this.requestContextManager = new RequestContextManager(logger)
43
- this.serviceInvalidator = new ServiceInvalidator(
44
- this.manager,
45
- this.requestContextManager,
46
- this.eventBus,
47
- logger,
48
- )
49
- this.instanceResolver = new InstanceResolver(
50
- this.registry,
51
- this.manager,
52
- this.serviceInstantiator,
53
- this.tokenProcessor,
54
- logger,
55
- this,
56
- )
57
- }
58
-
59
- // ============================================================================
60
- // PUBLIC METHODS
61
- // ============================================================================
62
-
63
- getEventBus() {
64
- return this.eventBus
65
- }
66
-
67
- getManager() {
68
- return this.manager
69
- }
70
-
71
- getRequestContexts() {
72
- return this.requestContextManager.getRequestContexts()
73
- }
74
-
75
- getRequestContextManager() {
76
- return this.requestContextManager
77
- }
78
-
79
- getServiceInvalidator() {
80
- return this.serviceInvalidator
81
- }
82
-
83
- public getInstanceIdentifier(token: AnyInjectableType, args?: any): string {
84
- const [err, { actualToken, validatedArgs }] =
85
- this.tokenProcessor.validateAndResolveTokenArgs(token, args)
86
- if (err) {
87
- throw err
88
- }
89
- return this.tokenProcessor.generateInstanceName(actualToken, validatedArgs)
90
- }
91
-
92
- public async getInstance(
93
- token: AnyInjectableType,
94
- args?: any,
95
- onPrepare?: (data: {
96
- instanceName: string
97
- actualToken: any
98
- validatedArgs?: any
99
- }) => void,
100
- ) {
101
- const [err, data] = await this.instanceResolver.resolveInstance(
102
- token,
103
- args,
104
- this.requestContextManager.getCurrentRequestContext() || undefined,
105
- )
106
- if (err) {
107
- return [err]
108
- }
109
-
110
- // Call onPrepare callback if provided
111
- if (onPrepare) {
112
- const instanceName = this.getInstanceIdentifier(token, args)
113
- const [tokenErr, { actualToken, validatedArgs }] =
114
- this.tokenProcessor.validateAndResolveTokenArgs(token, args)
115
- if (!tokenErr) {
116
- onPrepare({ instanceName, actualToken, validatedArgs })
117
- }
118
- }
119
-
120
- return [undefined, data]
121
- }
122
-
123
- public async getOrThrowInstance<Instance>(
124
- token: AnyInjectableType,
125
- args: any,
126
- ): Promise<Instance> {
127
- const [error, instance] = await this.getInstance(token, args)
128
- if (error) {
129
- throw error
130
- }
131
- return instance
132
- }
133
-
134
- public getSyncInstance<
135
- Instance,
136
- Schema extends InjectionTokenSchemaType | undefined,
137
- >(
138
- token: AnyInjectableType,
139
- args: Schema extends ZodObject
140
- ? z.input<Schema>
141
- : Schema extends ZodOptional<ZodObject>
142
- ? z.input<Schema> | undefined
143
- : undefined,
144
- ): Instance | null {
145
- return this.instanceResolver.getSyncInstance(
146
- token,
147
- args as any,
148
- this.requestContextManager.getCurrentRequestContext(),
149
- )
150
- }
151
-
152
- invalidate(service: string, round = 1): Promise<any> {
153
- return this.serviceInvalidator.invalidate(service, round)
154
- }
155
-
156
- /**
157
- * Gracefully clears all services in the ServiceLocator using invalidation logic.
158
- * This method respects service dependencies and ensures proper cleanup order.
159
- * Services that depend on others will be invalidated first, then their dependencies.
160
- *
161
- * @param options Optional configuration for the clearing process
162
- * @returns Promise that resolves when all services have been cleared
163
- */
164
- async clearAll(options: ClearAllOptions = {}): Promise<void> {
165
- return this.serviceInvalidator.clearAll(options)
166
- }
167
-
168
- // ============================================================================
169
- // REQUEST CONTEXT MANAGEMENT
170
- // ============================================================================
171
-
172
- /**
173
- * Begins a new request context with the given parameters.
174
- * @param requestId Unique identifier for this request
175
- * @param metadata Optional metadata for the request
176
- * @param priority Priority for resolution (higher = more priority)
177
- * @returns The created request context holder
178
- */
179
- beginRequest(
180
- requestId: string,
181
- metadata?: Record<string, any>,
182
- priority: number = 100,
183
- ): RequestContextHolder {
184
- return this.requestContextManager.beginRequest(
185
- requestId,
186
- metadata,
187
- priority,
188
- )
189
- }
190
-
191
- /**
192
- * Ends a request context and cleans up all associated instances.
193
- * @param requestId The request ID to end
194
- */
195
- async endRequest(requestId: string): Promise<void> {
196
- return this.requestContextManager.endRequest(requestId)
197
- }
198
-
199
- /**
200
- * Gets the current request context.
201
- * @returns The current request context holder or null
202
- */
203
- getCurrentRequestContext(): RequestContextHolder | null {
204
- return this.requestContextManager.getCurrentRequestContext()
205
- }
206
-
207
- /**
208
- * Sets the current request context.
209
- * @param requestId The request ID to set as current
210
- */
211
- setCurrentRequestContext(requestId: string): void {
212
- return this.requestContextManager.setCurrentRequestContext(requestId)
213
- }
214
-
215
- /**
216
- * Waits for all services to settle (either created, destroyed, or error state).
217
- */
218
- async ready(): Promise<void> {
219
- return this.serviceInvalidator.ready()
220
- }
221
-
222
- /**
223
- * Helper method for TokenProcessor to access pre-prepared instances.
224
- * This is needed for the factory context creation.
225
- */
226
- tryGetPrePreparedInstance(
227
- instanceName: string,
228
- contextHolder: RequestContextHolder | undefined,
229
- deps: Set<string>,
230
- ): any {
231
- return this.tokenProcessor.tryGetPrePreparedInstance(
232
- instanceName,
233
- contextHolder,
234
- deps,
235
- this.requestContextManager.getCurrentRequestContext(),
236
- )
237
- }
238
-
239
- /**
240
- * Helper method for InstanceResolver to generate instance names.
241
- * This is needed for the factory context creation.
242
- */
243
- generateInstanceName(token: any, args: any): string {
244
- return this.tokenProcessor.generateInstanceName(token, args)
245
- }
246
- }
package/tsup.config.mts DELETED
@@ -1,12 +0,0 @@
1
- import { defineConfig } from 'tsup'
2
-
3
- export default defineConfig({
4
- entry: ['src/index.mts', 'src/testing/index.mts'],
5
- outDir: 'lib',
6
- format: ['esm', 'cjs'],
7
- clean: true,
8
- treeshake: 'smallest',
9
- sourcemap: true,
10
- platform: 'node',
11
- experimentalDts: true,
12
- })
File without changes