@navios/di 0.5.0 → 0.6.0
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/CHANGELOG.md +146 -0
- package/README.md +196 -219
- package/docs/README.md +69 -11
- package/docs/api-reference.md +281 -117
- package/docs/container.md +220 -56
- package/docs/examples/request-scope-example.mts +2 -2
- package/docs/factory.md +3 -8
- package/docs/getting-started.md +37 -8
- package/docs/migration.md +318 -37
- package/docs/request-contexts.md +263 -175
- package/docs/scopes.md +79 -42
- package/lib/browser/index.d.mts +1577 -0
- package/lib/browser/index.d.mts.map +1 -0
- package/lib/browser/index.mjs +3012 -0
- package/lib/browser/index.mjs.map +1 -0
- package/lib/index-S_qX2VLI.d.mts +1211 -0
- package/lib/index-S_qX2VLI.d.mts.map +1 -0
- package/lib/index-fKPuT65j.d.cts +1206 -0
- package/lib/index-fKPuT65j.d.cts.map +1 -0
- package/lib/index.cjs +389 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.cts +376 -0
- package/lib/index.d.cts.map +1 -0
- package/lib/index.d.mts +371 -78
- package/lib/index.d.mts.map +1 -0
- package/lib/index.mjs +325 -63
- package/lib/index.mjs.map +1 -1
- package/lib/testing/index.cjs +9 -0
- package/lib/testing/index.d.cts +2 -0
- package/lib/testing/index.d.mts +2 -2
- package/lib/testing/index.mjs +2 -72
- package/lib/testing-BMGmmxH7.cjs +2895 -0
- package/lib/testing-BMGmmxH7.cjs.map +1 -0
- package/lib/testing-DCXz8AJD.mjs +2655 -0
- package/lib/testing-DCXz8AJD.mjs.map +1 -0
- package/package.json +26 -4
- package/project.json +2 -2
- package/src/__tests__/async-local-storage.browser.spec.mts +240 -0
- package/src/__tests__/async-local-storage.spec.mts +333 -0
- package/src/__tests__/container.spec.mts +30 -25
- package/src/__tests__/e2e.browser.spec.mts +790 -0
- package/src/__tests__/e2e.spec.mts +1222 -0
- package/src/__tests__/errors.spec.mts +6 -6
- package/src/__tests__/factory.spec.mts +1 -1
- package/src/__tests__/get-injectors.spec.mts +1 -1
- package/src/__tests__/injectable.spec.mts +1 -1
- package/src/__tests__/injection-token.spec.mts +1 -1
- package/src/__tests__/library-findings.spec.mts +563 -0
- package/src/__tests__/registry.spec.mts +2 -2
- package/src/__tests__/request-scope.spec.mts +266 -274
- package/src/__tests__/service-instantiator.spec.mts +19 -17
- package/src/__tests__/service-locator-event-bus.spec.mts +9 -9
- package/src/__tests__/service-locator-manager.spec.mts +15 -15
- package/src/__tests__/service-locator.spec.mts +167 -244
- package/src/__tests__/unified-api.spec.mts +27 -27
- package/src/__type-tests__/factory.spec-d.mts +2 -2
- package/src/__type-tests__/inject.spec-d.mts +2 -2
- package/src/__type-tests__/injectable.spec-d.mts +1 -1
- package/src/browser.mts +16 -0
- package/src/container/container.mts +319 -0
- package/src/container/index.mts +2 -0
- package/src/container/scoped-container.mts +350 -0
- package/src/decorators/factory.decorator.mts +4 -4
- package/src/decorators/injectable.decorator.mts +5 -5
- package/src/errors/di-error.mts +13 -7
- package/src/errors/index.mts +0 -8
- package/src/index.mts +156 -15
- package/src/interfaces/container.interface.mts +82 -0
- package/src/interfaces/factory.interface.mts +2 -2
- package/src/interfaces/index.mts +1 -0
- package/src/internal/context/async-local-storage.mts +120 -0
- package/src/internal/context/factory-context.mts +18 -0
- package/src/internal/context/index.mts +3 -0
- package/src/{request-context-holder.mts → internal/context/request-context.mts} +40 -27
- package/src/internal/context/resolution-context.mts +63 -0
- package/src/internal/context/sync-local-storage.mts +51 -0
- package/src/internal/core/index.mts +5 -0
- package/src/internal/core/instance-resolver.mts +641 -0
- package/src/{service-instantiator.mts → internal/core/instantiator.mts} +31 -27
- package/src/internal/core/invalidator.mts +437 -0
- package/src/internal/core/service-locator.mts +202 -0
- package/src/{token-processor.mts → internal/core/token-processor.mts} +79 -60
- package/src/{base-instance-holder-manager.mts → internal/holder/base-holder-manager.mts} +91 -21
- package/src/internal/holder/holder-manager.mts +85 -0
- package/src/internal/holder/holder-storage.interface.mts +116 -0
- package/src/internal/holder/index.mts +6 -0
- package/src/internal/holder/instance-holder.mts +109 -0
- package/src/internal/holder/request-storage.mts +134 -0
- package/src/internal/holder/singleton-storage.mts +105 -0
- package/src/internal/index.mts +4 -0
- package/src/internal/lifecycle/circular-detector.mts +77 -0
- package/src/internal/lifecycle/index.mts +2 -0
- package/src/{service-locator-event-bus.mts → internal/lifecycle/lifecycle-event-bus.mts} +12 -5
- package/src/testing/__tests__/test-container.spec.mts +2 -2
- package/src/testing/test-container.mts +4 -4
- package/src/token/index.mts +2 -0
- package/src/{injection-token.mts → token/injection-token.mts} +1 -1
- package/src/{registry.mts → token/registry.mts} +1 -1
- package/src/utils/get-injectable-token.mts +1 -1
- package/src/utils/get-injectors.mts +32 -15
- package/src/utils/types.mts +1 -1
- package/tsdown.config.mts +67 -0
- package/lib/_tsup-dts-rollup.d.mts +0 -1283
- package/lib/_tsup-dts-rollup.d.ts +0 -1283
- package/lib/chunk-44F3LXW5.mjs +0 -2043
- package/lib/chunk-44F3LXW5.mjs.map +0 -1
- package/lib/index.d.ts +0 -78
- package/lib/index.js +0 -2127
- package/lib/index.js.map +0 -1
- package/lib/testing/index.d.ts +0 -2
- package/lib/testing/index.js +0 -2060
- package/lib/testing/index.js.map +0 -1
- package/lib/testing/index.mjs.map +0 -1
- package/src/container.mts +0 -227
- package/src/factory-context.mts +0 -8
- package/src/instance-resolver.mts +0 -559
- package/src/request-context-manager.mts +0 -149
- package/src/service-invalidator.mts +0 -429
- package/src/service-locator-instance-holder.mts +0 -70
- package/src/service-locator-manager.mts +0 -85
- package/src/service-locator.mts +0 -246
- package/tsup.config.mts +0 -12
- /package/src/{injector.mts → injectors.mts} +0 -0
package/src/service-locator.mts
DELETED
|
@@ -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
|