@navios/di 0.8.0 → 0.9.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.
Files changed (264) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/README.md +117 -17
  3. package/lib/browser/container/abstract-container.d.mts +112 -0
  4. package/lib/browser/container/abstract-container.d.mts.map +1 -0
  5. package/lib/browser/container/abstract-container.mjs +100 -0
  6. package/lib/browser/container/abstract-container.mjs.map +1 -0
  7. package/lib/browser/container/container.d.mts +100 -0
  8. package/lib/browser/container/container.d.mts.map +1 -0
  9. package/lib/browser/container/container.mjs +424 -0
  10. package/lib/browser/container/container.mjs.map +1 -0
  11. package/lib/browser/container/scoped-container.d.mts +93 -0
  12. package/lib/browser/container/scoped-container.d.mts.map +1 -0
  13. package/lib/browser/container/scoped-container.mjs +119 -0
  14. package/lib/browser/container/scoped-container.mjs.map +1 -0
  15. package/lib/browser/decorators/factory.decorator.d.mts +26 -0
  16. package/lib/browser/decorators/factory.decorator.d.mts.map +1 -0
  17. package/lib/browser/decorators/factory.decorator.mjs +20 -0
  18. package/lib/browser/decorators/factory.decorator.mjs.map +1 -0
  19. package/lib/browser/decorators/injectable.decorator.d.mts +38 -0
  20. package/lib/browser/decorators/injectable.decorator.d.mts.map +1 -0
  21. package/lib/browser/decorators/injectable.decorator.mjs +21 -0
  22. package/lib/browser/decorators/injectable.decorator.mjs.map +1 -0
  23. package/lib/browser/enums/injectable-scope.enum.d.mts +18 -0
  24. package/lib/browser/enums/injectable-scope.enum.d.mts.map +1 -0
  25. package/lib/browser/enums/injectable-scope.enum.mjs +20 -0
  26. package/lib/browser/enums/injectable-scope.enum.mjs.map +1 -0
  27. package/lib/browser/enums/injectable-type.enum.d.mts +8 -0
  28. package/lib/browser/enums/injectable-type.enum.d.mts.map +1 -0
  29. package/lib/browser/enums/injectable-type.enum.mjs +10 -0
  30. package/lib/browser/enums/injectable-type.enum.mjs.map +1 -0
  31. package/lib/browser/errors/di-error.d.mts +43 -0
  32. package/lib/browser/errors/di-error.d.mts.map +1 -0
  33. package/lib/browser/errors/di-error.mjs +98 -0
  34. package/lib/browser/errors/di-error.mjs.map +1 -0
  35. package/lib/browser/event-emitter.d.mts +16 -0
  36. package/lib/browser/event-emitter.d.mts.map +1 -0
  37. package/lib/browser/event-emitter.mjs +320 -0
  38. package/lib/browser/event-emitter.mjs.map +1 -0
  39. package/lib/browser/index.d.mts +37 -1558
  40. package/lib/browser/index.mjs +29 -2749
  41. package/lib/browser/interfaces/container.interface.d.mts +59 -0
  42. package/lib/browser/interfaces/container.interface.d.mts.map +1 -0
  43. package/lib/browser/interfaces/factory.interface.d.mts +14 -0
  44. package/lib/browser/interfaces/factory.interface.d.mts.map +1 -0
  45. package/lib/browser/interfaces/on-service-destroy.interface.d.mts +7 -0
  46. package/lib/browser/interfaces/on-service-destroy.interface.d.mts.map +1 -0
  47. package/lib/browser/interfaces/on-service-init.interface.d.mts +7 -0
  48. package/lib/browser/interfaces/on-service-init.interface.d.mts.map +1 -0
  49. package/lib/browser/internal/context/async-local-storage.browser.mjs +20 -0
  50. package/lib/browser/internal/context/async-local-storage.browser.mjs.map +1 -0
  51. package/lib/browser/internal/context/async-local-storage.d.mts +9 -0
  52. package/lib/browser/internal/context/async-local-storage.d.mts.map +1 -0
  53. package/lib/browser/internal/context/async-local-storage.types.d.mts +11 -0
  54. package/lib/browser/internal/context/async-local-storage.types.d.mts.map +1 -0
  55. package/lib/browser/internal/context/factory-context.d.mts +23 -0
  56. package/lib/browser/internal/context/factory-context.d.mts.map +1 -0
  57. package/lib/browser/internal/context/resolution-context.d.mts +43 -0
  58. package/lib/browser/internal/context/resolution-context.d.mts.map +1 -0
  59. package/lib/browser/internal/context/resolution-context.mjs +56 -0
  60. package/lib/browser/internal/context/resolution-context.mjs.map +1 -0
  61. package/lib/browser/internal/context/service-initialization-context.d.mts +48 -0
  62. package/lib/browser/internal/context/service-initialization-context.d.mts.map +1 -0
  63. package/lib/browser/internal/context/sync-local-storage.mjs +53 -0
  64. package/lib/browser/internal/context/sync-local-storage.mjs.map +1 -0
  65. package/lib/browser/internal/core/instance-resolver.d.mts +119 -0
  66. package/lib/browser/internal/core/instance-resolver.d.mts.map +1 -0
  67. package/lib/browser/internal/core/instance-resolver.mjs +306 -0
  68. package/lib/browser/internal/core/instance-resolver.mjs.map +1 -0
  69. package/lib/browser/internal/core/name-resolver.d.mts +52 -0
  70. package/lib/browser/internal/core/name-resolver.d.mts.map +1 -0
  71. package/lib/browser/internal/core/name-resolver.mjs +118 -0
  72. package/lib/browser/internal/core/name-resolver.mjs.map +1 -0
  73. package/lib/browser/internal/core/scope-tracker.d.mts +65 -0
  74. package/lib/browser/internal/core/scope-tracker.d.mts.map +1 -0
  75. package/lib/browser/internal/core/scope-tracker.mjs +120 -0
  76. package/lib/browser/internal/core/scope-tracker.mjs.map +1 -0
  77. package/lib/browser/internal/core/service-initializer.d.mts +44 -0
  78. package/lib/browser/internal/core/service-initializer.d.mts.map +1 -0
  79. package/lib/browser/internal/core/service-initializer.mjs +109 -0
  80. package/lib/browser/internal/core/service-initializer.mjs.map +1 -0
  81. package/lib/browser/internal/core/service-invalidator.d.mts +81 -0
  82. package/lib/browser/internal/core/service-invalidator.d.mts.map +1 -0
  83. package/lib/browser/internal/core/service-invalidator.mjs +142 -0
  84. package/lib/browser/internal/core/service-invalidator.mjs.map +1 -0
  85. package/lib/browser/internal/core/token-resolver.d.mts +54 -0
  86. package/lib/browser/internal/core/token-resolver.d.mts.map +1 -0
  87. package/lib/browser/internal/core/token-resolver.mjs +77 -0
  88. package/lib/browser/internal/core/token-resolver.mjs.map +1 -0
  89. package/lib/browser/internal/holder/holder-storage.interface.d.mts +99 -0
  90. package/lib/browser/internal/holder/holder-storage.interface.d.mts.map +1 -0
  91. package/lib/browser/internal/holder/instance-holder.d.mts +101 -0
  92. package/lib/browser/internal/holder/instance-holder.d.mts.map +1 -0
  93. package/lib/browser/internal/holder/instance-holder.mjs +19 -0
  94. package/lib/browser/internal/holder/instance-holder.mjs.map +1 -0
  95. package/lib/browser/internal/holder/unified-storage.d.mts +53 -0
  96. package/lib/browser/internal/holder/unified-storage.d.mts.map +1 -0
  97. package/lib/browser/internal/holder/unified-storage.mjs +144 -0
  98. package/lib/browser/internal/holder/unified-storage.mjs.map +1 -0
  99. package/lib/browser/internal/lifecycle/circular-detector.d.mts +39 -0
  100. package/lib/browser/internal/lifecycle/circular-detector.d.mts.map +1 -0
  101. package/lib/browser/internal/lifecycle/circular-detector.mjs +55 -0
  102. package/lib/browser/internal/lifecycle/circular-detector.mjs.map +1 -0
  103. package/lib/browser/internal/lifecycle/lifecycle-event-bus.d.mts +18 -0
  104. package/lib/browser/internal/lifecycle/lifecycle-event-bus.d.mts.map +1 -0
  105. package/lib/browser/internal/lifecycle/lifecycle-event-bus.mjs +43 -0
  106. package/lib/browser/internal/lifecycle/lifecycle-event-bus.mjs.map +1 -0
  107. package/lib/browser/internal/stub-factory-class.d.mts +14 -0
  108. package/lib/browser/internal/stub-factory-class.d.mts.map +1 -0
  109. package/lib/browser/internal/stub-factory-class.mjs +18 -0
  110. package/lib/browser/internal/stub-factory-class.mjs.map +1 -0
  111. package/lib/browser/symbols/injectable-token.d.mts +5 -0
  112. package/lib/browser/symbols/injectable-token.d.mts.map +1 -0
  113. package/lib/browser/symbols/injectable-token.mjs +6 -0
  114. package/lib/browser/symbols/injectable-token.mjs.map +1 -0
  115. package/lib/browser/token/injection-token.d.mts +55 -0
  116. package/lib/browser/token/injection-token.d.mts.map +1 -0
  117. package/lib/browser/token/injection-token.mjs +100 -0
  118. package/lib/browser/token/injection-token.mjs.map +1 -0
  119. package/lib/browser/token/registry.d.mts +37 -0
  120. package/lib/browser/token/registry.d.mts.map +1 -0
  121. package/lib/browser/token/registry.mjs +86 -0
  122. package/lib/browser/token/registry.mjs.map +1 -0
  123. package/lib/browser/utils/default-injectors.d.mts +12 -0
  124. package/lib/browser/utils/default-injectors.d.mts.map +1 -0
  125. package/lib/browser/utils/default-injectors.mjs +13 -0
  126. package/lib/browser/utils/default-injectors.mjs.map +1 -0
  127. package/lib/browser/utils/get-injectable-token.d.mts +9 -0
  128. package/lib/browser/utils/get-injectable-token.d.mts.map +1 -0
  129. package/lib/browser/utils/get-injectable-token.mjs +13 -0
  130. package/lib/browser/utils/get-injectable-token.mjs.map +1 -0
  131. package/lib/browser/utils/get-injectors.d.mts +55 -0
  132. package/lib/browser/utils/get-injectors.d.mts.map +1 -0
  133. package/lib/browser/utils/get-injectors.mjs +121 -0
  134. package/lib/browser/utils/get-injectors.mjs.map +1 -0
  135. package/lib/browser/utils/types.d.mts +23 -0
  136. package/lib/browser/utils/types.d.mts.map +1 -0
  137. package/lib/{container-DAKOvAgr.mjs → container-8-z89TyQ.mjs} +1325 -1462
  138. package/lib/container-8-z89TyQ.mjs.map +1 -0
  139. package/lib/{container-Bp1W-pWJ.d.mts → container-CNiqesCL.d.mts} +598 -617
  140. package/lib/container-CNiqesCL.d.mts.map +1 -0
  141. package/lib/{container-DENMeJ87.cjs → container-CaY2fDuk.cjs} +1369 -1512
  142. package/lib/container-CaY2fDuk.cjs.map +1 -0
  143. package/lib/{container-YPwvmlK2.d.cts → container-D-0Ho3qL.d.cts} +598 -612
  144. package/lib/container-D-0Ho3qL.d.cts.map +1 -0
  145. package/lib/index.cjs +13 -15
  146. package/lib/index.cjs.map +1 -1
  147. package/lib/index.d.cts +58 -223
  148. package/lib/index.d.cts.map +1 -1
  149. package/lib/index.d.mts +62 -222
  150. package/lib/index.d.mts.map +1 -1
  151. package/lib/index.mjs +5 -6
  152. package/lib/index.mjs.map +1 -1
  153. package/lib/testing/index.cjs +569 -311
  154. package/lib/testing/index.cjs.map +1 -1
  155. package/lib/testing/index.d.cts +370 -41
  156. package/lib/testing/index.d.cts.map +1 -1
  157. package/lib/testing/index.d.mts +370 -41
  158. package/lib/testing/index.d.mts.map +1 -1
  159. package/lib/testing/index.mjs +568 -305
  160. package/lib/testing/index.mjs.map +1 -1
  161. package/package.json +2 -1
  162. package/src/__tests__/circular-detector.spec.mts +193 -0
  163. package/src/__tests__/concurrent.spec.mts +368 -0
  164. package/src/__tests__/container.spec.mts +32 -30
  165. package/src/__tests__/di-error.spec.mts +351 -0
  166. package/src/__tests__/e2e.browser.spec.mts +0 -4
  167. package/src/__tests__/e2e.spec.mts +10 -19
  168. package/src/__tests__/event-emitter.spec.mts +232 -109
  169. package/src/__tests__/get-injectors.spec.mts +250 -39
  170. package/src/__tests__/injection-token.spec.mts +293 -349
  171. package/src/__tests__/library-findings.spec.mts +8 -8
  172. package/src/__tests__/registry.spec.mts +358 -210
  173. package/src/__tests__/resolution-context.spec.mts +255 -0
  174. package/src/__tests__/scope-tracker.spec.mts +598 -0
  175. package/src/__tests__/scope-upgrade.spec.mts +808 -0
  176. package/src/__tests__/scoped-container.spec.mts +595 -0
  177. package/src/__tests__/test-container.spec.mts +293 -0
  178. package/src/__tests__/token-resolver.spec.mts +207 -0
  179. package/src/__tests__/unified-storage.spec.mts +535 -0
  180. package/src/__tests__/unit-test-container.spec.mts +405 -0
  181. package/src/__type-tests__/container.spec-d.mts +180 -0
  182. package/src/__type-tests__/factory.spec-d.mts +15 -3
  183. package/src/__type-tests__/inject.spec-d.mts +115 -20
  184. package/src/__type-tests__/injectable.spec-d.mts +69 -52
  185. package/src/__type-tests__/injection-token.spec-d.mts +176 -0
  186. package/src/__type-tests__/scoped-container.spec-d.mts +212 -0
  187. package/src/container/abstract-container.mts +327 -0
  188. package/src/container/container.mts +142 -170
  189. package/src/container/scoped-container.mts +126 -208
  190. package/src/decorators/factory.decorator.mts +16 -11
  191. package/src/decorators/injectable.decorator.mts +20 -16
  192. package/src/enums/index.mts +2 -2
  193. package/src/enums/injectable-scope.enum.mts +1 -0
  194. package/src/enums/injectable-type.enum.mts +1 -0
  195. package/src/errors/di-error.mts +96 -0
  196. package/src/event-emitter.mts +3 -27
  197. package/src/index.mts +6 -153
  198. package/src/interfaces/container.interface.mts +13 -0
  199. package/src/interfaces/factory.interface.mts +1 -1
  200. package/src/interfaces/index.mts +1 -1
  201. package/src/internal/context/async-local-storage.mts +3 -2
  202. package/src/internal/context/async-local-storage.types.mts +1 -0
  203. package/src/internal/context/factory-context.mts +1 -0
  204. package/src/internal/context/index.mts +3 -1
  205. package/src/internal/context/resolution-context.mts +1 -0
  206. package/src/internal/context/service-initialization-context.mts +43 -0
  207. package/src/internal/core/index.mts +5 -4
  208. package/src/internal/core/instance-resolver.mts +460 -302
  209. package/src/internal/core/name-resolver.mts +196 -0
  210. package/src/internal/core/scope-tracker.mts +242 -0
  211. package/src/internal/core/{instantiator.mts → service-initializer.mts} +51 -29
  212. package/src/internal/core/service-invalidator.mts +290 -0
  213. package/src/internal/core/token-resolver.mts +122 -0
  214. package/src/internal/holder/holder-storage.interface.mts +11 -5
  215. package/src/internal/holder/index.mts +2 -5
  216. package/src/internal/holder/instance-holder.mts +1 -3
  217. package/src/internal/holder/unified-storage.mts +245 -0
  218. package/src/internal/index.mts +2 -1
  219. package/src/internal/lifecycle/circular-detector.mts +1 -0
  220. package/src/internal/lifecycle/index.mts +1 -1
  221. package/src/internal/lifecycle/lifecycle-event-bus.mts +1 -0
  222. package/src/internal/stub-factory-class.mts +16 -0
  223. package/src/symbols/injectable-token.mts +3 -1
  224. package/src/testing/index.mts +2 -0
  225. package/src/testing/test-container.mts +546 -85
  226. package/src/testing/types.mts +117 -0
  227. package/src/testing/unit-test-container.mts +509 -0
  228. package/src/token/injection-token.mts +41 -4
  229. package/src/token/registry.mts +75 -9
  230. package/src/utils/default-injectors.mts +16 -0
  231. package/src/utils/get-injectable-token.mts +2 -3
  232. package/src/utils/get-injectors.mts +26 -15
  233. package/src/utils/index.mts +3 -1
  234. package/src/utils/types.mts +1 -0
  235. package/tsdown.config.mts +11 -1
  236. package/lib/browser/index.d.mts.map +0 -1
  237. package/lib/browser/index.mjs.map +0 -1
  238. package/lib/container-Bp1W-pWJ.d.mts.map +0 -1
  239. package/lib/container-DAKOvAgr.mjs.map +0 -1
  240. package/lib/container-DENMeJ87.cjs.map +0 -1
  241. package/lib/container-YPwvmlK2.d.cts.map +0 -1
  242. package/src/__tests__/async-local-storage.browser.spec.mts +0 -166
  243. package/src/__tests__/async-local-storage.spec.mts +0 -333
  244. package/src/__tests__/errors.spec.mts +0 -87
  245. package/src/__tests__/factory.spec.mts +0 -137
  246. package/src/__tests__/injectable.spec.mts +0 -246
  247. package/src/__tests__/request-scope.spec.mts +0 -416
  248. package/src/__tests__/service-instantiator.spec.mts +0 -410
  249. package/src/__tests__/service-locator-event-bus.spec.mts +0 -242
  250. package/src/__tests__/service-locator-manager.spec.mts +0 -300
  251. package/src/__tests__/service-locator.spec.mts +0 -966
  252. package/src/__tests__/unified-api.spec.mts +0 -130
  253. package/src/browser.mts +0 -11
  254. package/src/injectors.mts +0 -18
  255. package/src/internal/context/request-context.mts +0 -225
  256. package/src/internal/core/invalidator.mts +0 -437
  257. package/src/internal/core/service-locator.mts +0 -202
  258. package/src/internal/core/token-processor.mts +0 -252
  259. package/src/internal/holder/base-holder-manager.mts +0 -334
  260. package/src/internal/holder/holder-manager.mts +0 -85
  261. package/src/internal/holder/request-storage.mts +0 -127
  262. package/src/internal/holder/singleton-storage.mts +0 -92
  263. package/src/testing/README.md +0 -80
  264. package/src/testing/__tests__/test-container.spec.mts +0 -173
@@ -1,5 +1,55 @@
1
1
  import { ZodObject, ZodOptional, ZodRecord, ZodType, z } from "zod/v4";
2
2
 
3
+ //#region src/token/injection-token.d.mts
4
+ type ClassType = new (...args: any[]) => any;
5
+ type ClassTypeWithoutArguments = new () => any;
6
+ type ClassTypeWithArgument<Arg> = new (arg: Arg) => any;
7
+ type ClassTypeWithOptionalArgument<Arg> = new (arg?: Arg) => any;
8
+ type ClassTypeWithInstance<T> = new (...args: any[]) => T;
9
+ type ClassTypeWithInstanceAndArgument<T, Arg> = new (arg: Arg) => T;
10
+ type ClassTypeWithInstanceAndOptionalArgument<T, Arg> = new (arg?: Arg) => T;
11
+ type BaseInjectionTokenSchemaType = ZodObject | ZodRecord;
12
+ type OptionalInjectionTokenSchemaType = ZodOptional<ZodObject> | ZodOptional<ZodRecord>;
13
+ type InjectionTokenSchemaType = BaseInjectionTokenSchemaType | OptionalInjectionTokenSchemaType;
14
+ declare class InjectionToken<T, S extends InjectionTokenSchemaType | unknown = unknown, Required extends boolean = (S extends ZodOptional<ZodObject> ? false : S extends ZodOptional<ZodRecord> ? false : S extends ZodObject ? true : S extends ZodRecord ? true : false)> {
15
+ readonly name: string | symbol | ClassType;
16
+ readonly schema: ZodObject | undefined;
17
+ readonly id: string;
18
+ private formattedName;
19
+ constructor(name: string | symbol | ClassType, schema: ZodObject | undefined, customId?: string);
20
+ static create<T extends ClassType>(name: T): InjectionToken<InstanceType<T>, undefined>;
21
+ static create<T extends ClassType, Schema extends InjectionTokenSchemaType>(name: T, schema: Schema): Schema['_def']['type'] extends 'ZodOptional' ? InjectionToken<InstanceType<T>, Schema, false> : InjectionToken<InstanceType<T>, Schema, true>;
22
+ static create<T>(name: string | symbol): InjectionToken<T, undefined>;
23
+ static create<T, Schema extends InjectionTokenSchemaType>(name: string | any, schema: Schema): InjectionToken<T, Schema>;
24
+ static bound<T, S extends InjectionTokenSchemaType>(token: InjectionToken<T, S>, value: z.input<S>): BoundInjectionToken<T, S>;
25
+ static factory<T, S extends InjectionTokenSchemaType>(token: InjectionToken<T, S>, factory: (ctx: FactoryContext) => Promise<z.input<S>>): FactoryInjectionToken<T, S>;
26
+ static refineType<T>(token: BoundInjectionToken<any, any>): BoundInjectionToken<T, any>;
27
+ toString(): string;
28
+ }
29
+ declare class BoundInjectionToken<T, S extends InjectionTokenSchemaType> {
30
+ readonly token: InjectionToken<T, S>;
31
+ readonly value: z.input<S>;
32
+ id: string;
33
+ name: string | symbol | ClassType;
34
+ schema: InjectionTokenSchemaType;
35
+ constructor(token: InjectionToken<T, S>, value: z.input<S>);
36
+ toString(): string;
37
+ }
38
+ declare class FactoryInjectionToken<T, S extends InjectionTokenSchemaType> {
39
+ readonly token: InjectionToken<T, S>;
40
+ readonly factory: (ctx: FactoryContext) => Promise<z.input<S>>;
41
+ value?: z.input<S>;
42
+ resolved: boolean;
43
+ id: string;
44
+ name: string | symbol | ClassType;
45
+ schema: InjectionTokenSchemaType;
46
+ constructor(token: InjectionToken<T, S>, factory: (ctx: FactoryContext) => Promise<z.input<S>>);
47
+ resolve(ctx: FactoryContext): Promise<z.input<S>>;
48
+ toString(): string;
49
+ }
50
+ type AnyInjectableType = ClassType | InjectionToken<any, any> | BoundInjectionToken<any, any> | FactoryInjectionToken<any, any>;
51
+ type InjectionTokenType = InjectionToken<any, any> | BoundInjectionToken<any, any> | FactoryInjectionToken<any, any>;
52
+ //#endregion
3
53
  //#region src/utils/types.d.mts
4
54
  type Join<TElements, TSeparator extends string> = TElements extends Readonly<[infer First, ...infer Rest]> ? Rest extends ReadonlyArray<string> ? First extends string ? `${First}${Rest extends [] ? '' : TSeparator}${Join<Rest, TSeparator>}` : never : never : '';
5
55
  type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
@@ -19,14 +69,6 @@ interface InjectState {
19
69
  requests: InjectRequest[];
20
70
  }
21
71
  //#endregion
22
- //#region src/interfaces/factory.interface.d.mts
23
- interface Factorable<T> {
24
- create(ctx?: FactoryContext): Promise<T> | T;
25
- }
26
- interface FactorableWithArgs<T, A extends InjectionTokenSchemaType> {
27
- create(ctx?: FactoryContext, ...args: [z.output<A>]): Promise<T> | T;
28
- }
29
- //#endregion
30
72
  //#region src/interfaces/container.interface.d.mts
31
73
  /**
32
74
  * Interface for dependency injection containers.
@@ -52,6 +94,15 @@ interface IContainer {
52
94
  * Checks if a service is registered in the container.
53
95
  */
54
96
  isRegistered(token: any): boolean;
97
+ /**
98
+ * Adds an instance to the container.
99
+ * Accepts class types, InjectionTokens, and BoundInjectionTokens.
100
+ * Rejects InjectionTokens with required schemas (use BoundInjectionToken instead).
101
+ *
102
+ * @param token The class type, InjectionToken, or BoundInjectionToken to register the instance for
103
+ * @param instance The instance to store
104
+ */
105
+ addInstance<T>(token: ClassType | InjectionToken<T, any> | BoundInjectionToken<T, any>, instance: T): void;
55
106
  /**
56
107
  * Disposes the container and cleans up all resources.
57
108
  */
@@ -69,6 +120,69 @@ interface IContainer {
69
120
  tryGetSync<T>(token: any, args?: any): T | null;
70
121
  }
71
122
  //#endregion
123
+ //#region src/enums/injectable-scope.enum.d.mts
124
+ declare enum InjectableScope {
125
+ /**
126
+ * Singleton scope: The instance is created once and shared across the application.
127
+ */
128
+ Singleton = "Singleton",
129
+ /**
130
+ * Instance scope: A new instance is created for each injection.
131
+ */
132
+ Transient = "Transient",
133
+ /**
134
+ * Request scope: The instance is created once per request and shared within that request context.
135
+ */
136
+ Request = "Request",
137
+ }
138
+ //#endregion
139
+ //#region src/enums/injectable-type.enum.d.mts
140
+ declare enum InjectableType {
141
+ Class = "Class",
142
+ Factory = "Factory",
143
+ }
144
+ //#endregion
145
+ //#region src/internal/context/service-initialization-context.d.mts
146
+ /**
147
+ * Context provided to injectors during service initialization.
148
+ *
149
+ * Extends FactoryContext with additional tracking information about
150
+ * the service being initialized, its dependencies, and scope.
151
+ * Used for scope upgrade tracking and dependency management.
152
+ */
153
+ interface ServiceInitializationContext {
154
+ inject: Injectors['asyncInject'];
155
+ /**
156
+ * The container instance for dependency resolution.
157
+ * This may be either a Container or ScopedContainer.
158
+ */
159
+ container: IContainer;
160
+ addDestroyListener: (listener: () => void) => void;
161
+ getDestroyListeners: () => (() => void)[];
162
+ /**
163
+ * The name of the service being initialized.
164
+ */
165
+ serviceName: string;
166
+ /**
167
+ * Set of dependency names that this service depends on.
168
+ * Automatically populated as dependencies are resolved.
169
+ */
170
+ dependencies: Set<string>;
171
+ /**
172
+ * The scope of the service being initialized.
173
+ */
174
+ scope: InjectableScope;
175
+ /**
176
+ * Track a dependency that was resolved.
177
+ * Used for scope upgrade tracking - if a Singleton service
178
+ * resolves a Request dependency, the scope can be upgraded.
179
+ *
180
+ * @param name The name of the dependency
181
+ * @param scope The scope of the dependency
182
+ */
183
+ trackDependency(name: string, scope: InjectableScope): void;
184
+ }
185
+ //#endregion
72
186
  //#region src/utils/get-injectors.d.mts
73
187
  interface Injectors {
74
188
  asyncInject<T extends ClassTypeWithoutArguments>(token: T): InstanceType<T> extends Factorable<infer R> ? Promise<R> : Promise<InstanceType<T>>;
@@ -112,15 +226,10 @@ interface Injectors {
112
226
  optional<T>(token: BoundInjectionToken<T, any>): T | null;
113
227
  optional<T>(token: FactoryInjectionToken<T, any>): T | null;
114
228
  wrapSyncInit(cb: () => any): (injectState?: InjectState) => [any, Promise<any>[], InjectState];
115
- provideFactoryContext(context: FactoryContext | null): FactoryContext | null;
229
+ provideFactoryContext(context: ServiceInitializationContext | null): ServiceInitializationContext | null;
116
230
  }
117
231
  declare function getInjectors(): Injectors;
118
232
  //#endregion
119
- //#region src/utils/get-injectable-token.d.mts
120
- declare function getInjectableToken<R>(target: ClassType): R extends {
121
- create(...args: any[]): infer V;
122
- } ? InjectionToken<V> : InjectionToken<R>;
123
- //#endregion
124
233
  //#region src/internal/context/factory-context.d.mts
125
234
  /**
126
235
  * Context provided to factory functions during service instantiation.
@@ -138,76 +247,12 @@ interface FactoryContext {
138
247
  addDestroyListener: (listener: () => void) => void;
139
248
  }
140
249
  //#endregion
141
- //#region src/token/injection-token.d.mts
142
- type ClassType = new (...args: any[]) => any;
143
- type ClassTypeWithoutArguments = new () => any;
144
- type ClassTypeWithArgument<Arg> = new (arg: Arg) => any;
145
- type ClassTypeWithOptionalArgument<Arg> = new (arg?: Arg) => any;
146
- type ClassTypeWithInstance<T> = new (...args: any[]) => T;
147
- type ClassTypeWithInstanceAndArgument<T, Arg> = new (arg: Arg) => T;
148
- type ClassTypeWithInstanceAndOptionalArgument<T, Arg> = new (arg?: Arg) => T;
149
- type BaseInjectionTokenSchemaType = ZodObject | ZodRecord;
150
- type OptionalInjectionTokenSchemaType = ZodOptional<ZodObject> | ZodOptional<ZodRecord>;
151
- type InjectionTokenSchemaType = BaseInjectionTokenSchemaType | OptionalInjectionTokenSchemaType;
152
- declare class InjectionToken<T, S extends InjectionTokenSchemaType | unknown = unknown, Required extends boolean = (S extends ZodOptional<ZodObject> ? false : S extends ZodOptional<ZodRecord> ? false : S extends ZodObject ? true : S extends ZodRecord ? true : false)> {
153
- readonly name: string | symbol | ClassType;
154
- readonly schema: ZodObject | undefined;
155
- id: `${string}-${string}-${string}-${string}-${string}`;
156
- private formattedName;
157
- constructor(name: string | symbol | ClassType, schema: ZodObject | undefined);
158
- static create<T extends ClassType>(name: T): InjectionToken<InstanceType<T>, undefined>;
159
- static create<T extends ClassType, Schema extends InjectionTokenSchemaType>(name: T, schema: Schema): Schema['_def']['type'] extends 'ZodOptional' ? InjectionToken<InstanceType<T>, Schema, false> : InjectionToken<InstanceType<T>, Schema, true>;
160
- static create<T>(name: string | symbol): InjectionToken<T, undefined>;
161
- static create<T, Schema extends InjectionTokenSchemaType>(name: string | any, schema: Schema): InjectionToken<T, Schema>;
162
- static bound<T, S extends InjectionTokenSchemaType>(token: InjectionToken<T, S>, value: z.input<S>): BoundInjectionToken<T, S>;
163
- static factory<T, S extends InjectionTokenSchemaType>(token: InjectionToken<T, S>, factory: (ctx: FactoryContext) => Promise<z.input<S>>): FactoryInjectionToken<T, S>;
164
- static refineType<T>(token: BoundInjectionToken<any, any>): BoundInjectionToken<T, any>;
165
- toString(): string;
166
- }
167
- declare class BoundInjectionToken<T, S extends InjectionTokenSchemaType> {
168
- readonly token: InjectionToken<T, S>;
169
- readonly value: z.input<S>;
170
- id: string;
171
- name: string | symbol | ClassType;
172
- schema: InjectionTokenSchemaType;
173
- constructor(token: InjectionToken<T, S>, value: z.input<S>);
174
- toString(): string;
175
- }
176
- declare class FactoryInjectionToken<T, S extends InjectionTokenSchemaType> {
177
- readonly token: InjectionToken<T, S>;
178
- readonly factory: (ctx: FactoryContext) => Promise<z.input<S>>;
179
- value?: z.input<S>;
180
- resolved: boolean;
181
- id: string;
182
- name: string | symbol | ClassType;
183
- schema: InjectionTokenSchemaType;
184
- constructor(token: InjectionToken<T, S>, factory: (ctx: FactoryContext) => Promise<z.input<S>>);
185
- resolve(ctx: FactoryContext): Promise<z.input<S>>;
186
- toString(): string;
187
- }
188
- type AnyInjectableType = ClassType | InjectionToken<any, any> | BoundInjectionToken<any, any> | FactoryInjectionToken<any, any>;
189
- type InjectionTokenType = InjectionToken<any, any> | BoundInjectionToken<any, any> | FactoryInjectionToken<any, any>;
190
- //#endregion
191
- //#region src/enums/injectable-scope.enum.d.mts
192
- declare enum InjectableScope {
193
- /**
194
- * Singleton scope: The instance is created once and shared across the application.
195
- */
196
- Singleton = "Singleton",
197
- /**
198
- * Instance scope: A new instance is created for each injection.
199
- */
200
- Transient = "Transient",
201
- /**
202
- * Request scope: The instance is created once per request and shared within that request context.
203
- */
204
- Request = "Request",
250
+ //#region src/interfaces/factory.interface.d.mts
251
+ interface Factorable<T> {
252
+ create(ctx?: FactoryContext): Promise<T> | T;
205
253
  }
206
- //#endregion
207
- //#region src/enums/injectable-type.enum.d.mts
208
- declare enum InjectableType {
209
- Class = "Class",
210
- Factory = "Factory",
254
+ interface FactorableWithArgs<T, A extends InjectionTokenSchemaType> {
255
+ create(ctx?: FactoryContext, ...args: [z.output<A>]): Promise<T> | T;
211
256
  }
212
257
  //#endregion
213
258
  //#region src/token/registry.d.mts
@@ -216,14 +261,17 @@ type FactoryRecord<Instance = any, Schema = any> = {
216
261
  originalToken: InjectionToken<Instance, Schema>;
217
262
  target: ClassType;
218
263
  type: InjectableType;
264
+ priority: number;
219
265
  };
220
266
  declare class Registry {
221
267
  private readonly parent?;
222
268
  private readonly factories;
269
+ private readonly highestPriority;
223
270
  constructor(parent?: Registry | undefined);
224
271
  has(token: InjectionToken<any, any>): boolean;
225
272
  get<Instance, Schema>(token: InjectionToken<Instance, Schema>): FactoryRecord<Instance, Schema>;
226
- set<Instance, Schema>(token: InjectionToken<Instance, Schema>, scope: InjectableScope, target: ClassType, type: InjectableType): void;
273
+ getAll<Instance, Schema>(token: InjectionToken<Instance, Schema>): FactoryRecord<Instance, Schema>[];
274
+ set<Instance, Schema>(token: InjectionToken<Instance, Schema>, scope: InjectableScope, target: ClassType, type: InjectableType, priority?: number): void;
227
275
  delete(token: InjectionToken<any, any>): void;
228
276
  /**
229
277
  * Updates the scope of an already registered factory.
@@ -238,6 +286,52 @@ declare class Registry {
238
286
  }
239
287
  declare const globalRegistry: Registry;
240
288
  //#endregion
289
+ //#region src/internal/core/name-resolver.d.mts
290
+ /**
291
+ * Handles instance name generation with support for requestId and scope.
292
+ *
293
+ * Generates unique instance identifiers based on token, arguments, and scope.
294
+ * Request-scoped services MUST include requestId in their name for proper isolation.
295
+ */
296
+ declare class NameResolver {
297
+ private readonly logger;
298
+ private readonly instanceNameCache;
299
+ constructor(logger?: Console | null);
300
+ /**
301
+ * Generates a unique instance name based on token, arguments, requestId, and scope.
302
+ *
303
+ * Name formats:
304
+ * - Singleton/Transient without args: `${tokenId}`
305
+ * - Singleton/Transient with args: `${tokenId}:${argsHash}`
306
+ * - Request without args: `${tokenId}:requestId=${requestId}`
307
+ * - Request with args: `${tokenId}:requestId=${requestId}:${argsHash}`
308
+ *
309
+ * @param token The injection token
310
+ * @param args Optional arguments
311
+ * @param requestId Optional request ID (required for request-scoped services)
312
+ * @param scope Optional scope (used to determine if requestId should be included)
313
+ * @returns The generated instance name
314
+ */
315
+ generateInstanceName(token: InjectionTokenType, args?: any, requestId?: string, scope?: InjectableScope): string;
316
+ /**
317
+ * Upgrades an existing instance name to include requestId.
318
+ * Preserves any args hash that might already be in the name.
319
+ *
320
+ * Examples:
321
+ * - `TokenName` → `TokenName:requestId=req-123`
322
+ * - `TokenName:abc123` → `TokenName:requestId=req-123:abc123`
323
+ *
324
+ * @param existingName The existing instance name (without requestId)
325
+ * @param requestId The request ID to add
326
+ * @returns The upgraded instance name with requestId
327
+ */
328
+ upgradeInstanceNameToRequest(existingName: string, requestId: string): string;
329
+ /**
330
+ * Formats a single argument value for instance name generation.
331
+ */
332
+ formatArgValue(value: any): string;
333
+ }
334
+ //#endregion
241
335
  //#region src/errors/di-error.d.mts
242
336
  declare enum DIErrorCode {
243
337
  FactoryNotFound = "FactoryNotFound",
@@ -245,6 +339,14 @@ declare enum DIErrorCode {
245
339
  InstanceNotFound = "InstanceNotFound",
246
340
  InstanceDestroying = "InstanceDestroying",
247
341
  CircularDependency = "CircularDependency",
342
+ TokenValidationError = "TokenValidationError",
343
+ TokenSchemaRequiredError = "TokenSchemaRequiredError",
344
+ ClassNotInjectable = "ClassNotInjectable",
345
+ ScopeMismatchError = "ScopeMismatchError",
346
+ PriorityConflictError = "PriorityConflictError",
347
+ StorageError = "StorageError",
348
+ InitializationError = "InitializationError",
349
+ DependencyResolutionError = "DependencyResolutionError",
248
350
  UnknownError = "UnknownError",
249
351
  }
250
352
  declare class DIError extends Error {
@@ -258,6 +360,14 @@ declare class DIError extends Error {
258
360
  static instanceDestroying(name: string): DIError;
259
361
  static unknown(message: string | Error, context?: Record<string, unknown>): DIError;
260
362
  static circularDependency(cycle: string[]): DIError;
363
+ static tokenValidationError(message: string, schema: InjectionTokenSchemaType | undefined, value: unknown): DIError;
364
+ static tokenSchemaRequiredError(token: string | symbol | unknown): DIError;
365
+ static classNotInjectable(className: string): DIError;
366
+ static scopeMismatchError(token: string | symbol | unknown, expectedScope: string, actualScope: string): DIError;
367
+ static priorityConflictError(token: string | symbol | unknown, records: FactoryRecord[]): DIError;
368
+ static storageError(message: string, operation: string, instanceName?: string): DIError;
369
+ static initializationError(serviceName: string, error: Error | string): DIError;
370
+ static dependencyResolutionError(serviceName: string, dependencyName: string, error: Error | string): DIError;
261
371
  }
262
372
  //#endregion
263
373
  //#region src/internal/holder/instance-holder.d.mts
@@ -274,8 +384,6 @@ declare enum InstanceStatus {
274
384
  /** Instance creation failed with an error */
275
385
  Error = "error",
276
386
  }
277
- /** Callback function for instance effects */
278
- type InstanceEffect = () => void;
279
387
  /** Callback function for instance destruction listeners */
280
388
  type InstanceDestroyListener = () => void | Promise<void>;
281
389
  /**
@@ -370,8 +478,7 @@ type HolderGetResult<T = unknown> = [undefined, InstanceHolder<T>] | [DIError, I
370
478
  * Interface for abstracting holder storage operations.
371
479
  *
372
480
  * Enables unified instance resolution logic regardless of where
373
- * holders are stored (singleton manager, request context, etc.).
374
- * This is the key abstraction for the Storage Strategy pattern.
481
+ * holders are stored. This is the key abstraction for the unified storage pattern.
375
482
  */
376
483
  interface IHolderStorage {
377
484
  /**
@@ -440,271 +547,296 @@ interface IHolderStorage {
440
547
  * @returns Array of instance names that have this instance as a dependency
441
548
  */
442
549
  findDependents(instanceName: string): string[];
550
+ /**
551
+ * Updates dependency references when instance names change.
552
+ * Used during scope upgrades when instance names are regenerated with requestId.
553
+ *
554
+ * @param oldName The old instance name
555
+ * @param newName The new instance name
556
+ */
557
+ updateDependencyReference(oldName: string, newName: string): void;
443
558
  }
444
559
  //#endregion
445
- //#region src/internal/holder/base-holder-manager.d.mts
560
+ //#region src/internal/lifecycle/lifecycle-event-bus.d.mts
446
561
  /**
447
- * Result type for waitForHolderReady.
448
- * Returns either [undefined, holder] on success or [error] on failure.
562
+ * Event bus for service lifecycle events (create, destroy, etc.).
563
+ *
564
+ * Enables loose coupling between services by allowing them to subscribe
565
+ * to lifecycle events of their dependencies without direct references.
566
+ * Used primarily for invalidation cascading.
449
567
  */
450
- type HolderReadyResult<T> = [undefined, InstanceHolder<T>] | [DIError];
568
+ declare class LifecycleEventBus {
569
+ private readonly logger;
570
+ private listeners;
571
+ constructor(logger?: Console | null);
572
+ on<Event extends string | `pre:${string}` | `post:${string}`>(ns: string, event: Event, listener: (event: Event) => void): () => void;
573
+ emit(key: string, event: string): Promise<PromiseSettledResult<any>[] | undefined>;
574
+ }
575
+ //#endregion
576
+ //#region src/internal/core/service-invalidator.d.mts
577
+ interface ClearAllOptions {
578
+ /** Whether to wait for all services to settle before starting (default: true) */
579
+ waitForSettlement?: boolean;
580
+ }
581
+ interface InvalidationOptions {
582
+ /** Whether to emit events after invalidation (default: true) */
583
+ emitEvents?: boolean;
584
+ /** Custom event emitter function */
585
+ onInvalidated?: (instanceName: string) => Promise<void>;
586
+ /** Whether to cascade invalidation to dependents (default: false - events handle it) */
587
+ cascade?: boolean;
588
+ }
451
589
  /**
452
- * Abstract base class providing common functionality for managing InstanceHolder objects.
590
+ * Manages graceful service cleanup with event-based invalidation.
453
591
  *
454
- * Provides shared patterns for holder storage, creation, and lifecycle management
455
- * used by both singleton (HolderManager) and request-scoped (RequestContext) managers.
592
+ * Uses event subscriptions instead of manual dependent finding.
593
+ * When a service is created, it subscribes to destroy events of its dependencies.
594
+ * When a dependency is destroyed, the event automatically invalidates dependents.
456
595
  */
457
- declare abstract class BaseHolderManager {
458
- protected readonly logger: Console | null;
459
- protected readonly _holders: Map<string, InstanceHolder>;
596
+ declare class ServiceInvalidator {
597
+ private readonly eventBus;
598
+ private readonly logger;
599
+ constructor(eventBus: LifecycleEventBus | null, logger?: Console | null);
460
600
  /**
461
- * Reverse dependency index: maps a dependency name to the set of holder names that depend on it.
462
- * This allows O(1) lookup of dependents instead of O(n) iteration.
601
+ * Invalidates a service using a specific storage.
602
+ * Event-based invalidation means dependents are automatically invalidated
603
+ * via destroy event subscriptions - no need to manually find dependents.
604
+ *
605
+ * @param service The instance name to invalidate
606
+ * @param storage The storage to use for this invalidation
607
+ * @param options Additional options for invalidation behavior
463
608
  */
464
- protected readonly _dependents: Map<string, Set<string>>;
465
- constructor(logger?: Console | null);
609
+ invalidateWithStorage(service: string, storage: IHolderStorage, options?: InvalidationOptions): Promise<void>;
466
610
  /**
467
- * Protected getter for accessing the holders map from subclasses.
611
+ * Sets up destroy event subscriptions for a service's dependencies.
612
+ * Called when a service is successfully instantiated.
613
+ *
614
+ * @param serviceName The name of the service
615
+ * @param dependencies The set of dependency names
616
+ * @param storage The storage to use for invalidation
617
+ * @param holder The holder for the service (to add unsubscribe to destroy listeners)
468
618
  */
469
- protected get holders(): Map<string, InstanceHolder>;
619
+ setupDependencySubscriptions(serviceName: string, dependencies: Set<string>, storage: IHolderStorage, holder: InstanceHolder): void;
470
620
  /**
471
- * Abstract method to get a holder by name. Each implementation defines its own return type
472
- * based on their specific error handling and validation needs.
621
+ * Gracefully clears all services in a specific storage.
622
+ * This allows clearing request-scoped services using a RequestStorage.
473
623
  */
474
- abstract get(name: string): any;
624
+ clearAllWithStorage(storage: IHolderStorage, options?: ClearAllOptions): Promise<void>;
475
625
  /**
476
- * Abstract method to set a holder by name. Each implementation may have different validation logic.
626
+ * Waits for all services in a specific storage to settle.
477
627
  */
478
- abstract set(name: string, holder: InstanceHolder): void;
628
+ readyWithStorage(storage: IHolderStorage): Promise<void>;
479
629
  /**
480
- * Abstract method to check if a holder exists. Each implementation may have different validation logic.
630
+ * Invalidates a single holder using a specific storage.
481
631
  */
482
- abstract has(name: string): any;
632
+ private invalidateHolderWithStorage;
483
633
  /**
484
- * Deletes a holder by name and cleans up the reverse dependency index.
485
- * @param name The name of the holder to delete
486
- * @returns true if the holder was deleted, false if it didn't exist
634
+ * Common invalidation logic for holders based on their status.
487
635
  */
488
- delete(name: string): boolean;
636
+ private invalidateHolderByStatus;
489
637
  /**
490
- * Registers a holder's dependencies in the reverse index.
491
- * Call this after creating a holder with dependencies.
492
- * @param holderName The name of the holder that has dependencies
493
- * @param deps The set of dependency names
638
+ * Destroys a holder using a specific storage.
494
639
  */
495
- registerDependencies(holderName: string, deps: Set<string>): void;
640
+ private destroyHolderWithStorage;
496
641
  /**
497
- * Removes a holder from the reverse dependency index.
498
- * @param holderName The name of the holder to remove
499
- * @param deps The set of dependency names to clean up
642
+ * Waits for a holder to settle (either created, destroyed, or error state).
500
643
  */
501
- protected removeFromDependentsIndex(holderName: string, deps: Set<string>): void;
644
+ private waitForHolderToSettle;
502
645
  /**
503
- * Gets all holder names that depend on the given instance name.
504
- * O(1) lookup using the reverse dependency index.
505
- * @param instanceName The instance name to find dependents for
506
- * @returns Array of holder names that depend on this instance
646
+ * Emits events to listeners for instance lifecycle events.
507
647
  */
508
- getDependents(instanceName: string): string[];
509
- /**
510
- * Filters holders based on a predicate function.
511
- * @param predicate Function to test each holder
512
- * @returns A new Map containing only the holders that match the predicate
513
- * @deprecated Use forEachHolder() for iteration to avoid allocations
514
- */
515
- filter(predicate: (value: InstanceHolder<any>, key: string) => boolean): Map<string, InstanceHolder>;
516
- /**
517
- * Iterates over holders with a callback. More efficient than filter() as it
518
- * avoids creating intermediate arrays and Maps.
519
- * @param callback Function called for each holder with (holder, name)
520
- */
521
- forEachHolder(callback: (holder: InstanceHolder<any>, name: string) => void): void;
522
- /**
523
- * Finds the first holder matching a predicate. More efficient than filter()
524
- * when only one result is needed.
525
- * @param predicate Function to test each holder
526
- * @returns The first matching holder or undefined
527
- */
528
- findHolder(predicate: (holder: InstanceHolder<any>, name: string) => boolean): InstanceHolder | undefined;
529
- /**
530
- * Clears all holders from this manager and the reverse dependency index.
531
- */
532
- clear(): void;
533
- /**
534
- * Gets the number of holders currently managed.
535
- */
536
- size(): number;
537
- /**
538
- * Creates a new holder with Creating status and a deferred creation promise.
539
- * This is useful for creating placeholder holders that can be fulfilled later.
540
- * @param name The name of the instance
541
- * @param type The injectable type
542
- * @param scope The injectable scope
543
- * @param deps Optional set of dependencies
544
- * @returns A tuple containing the deferred promise and the holder
545
- */
546
- createCreatingHolder<Instance>(name: string, type: InjectableType, scope: InjectableScope, deps?: Set<string>): [ReturnType<typeof Promise.withResolvers<[undefined, Instance]>>, InstanceHolder<Instance>];
547
- /**
548
- * Creates a new holder with Created status and an actual instance.
549
- * This is useful for creating holders that already have their instance ready.
550
- * @param name The name of the instance
551
- * @param instance The actual instance to store
552
- * @param type The injectable type
553
- * @param scope The injectable scope
554
- * @param deps Optional set of dependencies
555
- * @returns The created holder
556
- */
557
- protected createCreatedHolder<Instance>(name: string, instance: Instance, type: InjectableType, scope: InjectableScope, deps?: Set<string>): InstanceHolder<Instance>;
648
+ private emitInstanceEvent;
649
+ }
650
+ //#endregion
651
+ //#region src/internal/core/token-resolver.d.mts
652
+ /**
653
+ * Handles token validation and resolution.
654
+ *
655
+ * Focuses on token validation, normalization, and argument validation.
656
+ * Name generation is handled by NameResolver.
657
+ */
658
+ declare class TokenResolver {
659
+ private readonly logger;
660
+ constructor(logger?: Console | null);
558
661
  /**
559
- * Gets all holder names currently managed.
662
+ * Normalizes a token to an InjectionToken.
663
+ * Handles class constructors by getting their injectable token.
664
+ *
665
+ * @param token A class constructor, InjectionToken, BoundInjectionToken, or FactoryInjectionToken
666
+ * @returns The normalized InjectionTokenType
560
667
  */
561
- getAllNames(): string[];
668
+ normalizeToken(token: AnyInjectableType): InjectionTokenType;
562
669
  /**
563
- * Gets all holders currently managed.
670
+ * Gets the underlying "real" token from wrapped tokens.
671
+ * For BoundInjectionToken and FactoryInjectionToken, returns the wrapped token.
672
+ * For other tokens, returns the token itself.
673
+ *
674
+ * @param token The token to unwrap
675
+ * @returns The underlying InjectionToken
564
676
  */
565
- getAllHolders(): InstanceHolder[];
677
+ getRealToken<T = unknown>(token: InjectionTokenType): InjectionToken<T>;
566
678
  /**
567
- * Checks if this manager has any holders.
679
+ * Convenience method that normalizes a token and then gets the real token.
680
+ * Useful for checking registry entries where you need the actual registered token.
681
+ *
682
+ * @param token Any injectable type
683
+ * @returns The underlying InjectionToken
568
684
  */
569
- isEmpty(): boolean;
685
+ getRegistryToken<T = unknown>(token: AnyInjectableType): InjectionToken<T>;
570
686
  /**
571
- * Waits for a holder to be ready and returns the appropriate result.
572
- * This is a shared utility used by both singleton and request-scoped resolution.
687
+ * Validates and resolves token arguments, handling factory token resolution and validation.
573
688
  *
574
- * @param holder The holder to wait for
575
- * @param waiterHolder Optional holder that is doing the waiting (for circular dependency detection)
576
- * @param getHolder Optional function to retrieve holders by name (required if waiterHolder is provided)
577
- * @returns A promise that resolves with [undefined, holder] on success or [DIError] on failure
689
+ * @param token The token to validate
690
+ * @param args Optional arguments
691
+ * @returns [error, { actualToken, validatedArgs }]
578
692
  */
579
- static waitForHolderReady<T>(holder: InstanceHolder<T>, waiterHolder?: InstanceHolder, getHolder?: (name: string) => InstanceHolder | undefined): Promise<HolderReadyResult<T>>;
693
+ validateAndResolveTokenArgs(token: AnyInjectableType, args?: any): [DIError | undefined, {
694
+ actualToken: InjectionTokenType;
695
+ validatedArgs?: any;
696
+ }];
580
697
  }
581
698
  //#endregion
582
- //#region src/internal/context/request-context.d.mts
699
+ //#region src/internal/holder/unified-storage.d.mts
583
700
  /**
584
- * Interface for request context that manages pre-prepared instances for a specific request.
701
+ * Unified storage implementation that works the same way regardless of scope.
702
+ * Replaces RequestContext, HolderManager, SingletonStorage, RequestStorage.
585
703
  *
586
- * Provides isolated storage for request-scoped services, enabling efficient
587
- * instantiation and cleanup within the lifecycle of a single request.
704
+ * Scope is just metadata - storage operations are identical for all scopes.
705
+ * Different storage instances are just isolated storage spaces.
588
706
  */
589
- interface RequestContext {
590
- /**
591
- * Unique identifier for this request context.
592
- */
593
- readonly requestId: string;
707
+ declare class UnifiedStorage implements IHolderStorage {
708
+ readonly scope: InjectableScope;
709
+ private readonly holders;
594
710
  /**
595
- * Instance holders for request-scoped services.
711
+ * Reverse dependency index: maps a dependency name to the set of holder names that depend on it.
712
+ * This allows O(1) lookup of dependents instead of O(n) iteration.
596
713
  */
597
- readonly holders: Map<string, InstanceHolder>;
714
+ private readonly dependents;
715
+ constructor(scope?: InjectableScope);
716
+ get<T = unknown>(instanceName: string): HolderGetResult<T>;
717
+ set(instanceName: string, holder: InstanceHolder): void;
718
+ delete(instanceName: string): boolean;
719
+ createHolder<T>(instanceName: string, type: InjectableType, deps: Set<string>): [ReturnType<typeof Promise.withResolvers<[undefined, T]>>, InstanceHolder<T>];
720
+ storeInstance(instanceName: string, instance: unknown): void;
721
+ handles(scope: InjectableScope): boolean;
722
+ getAllNames(): string[];
723
+ forEach(callback: (name: string, holder: InstanceHolder) => void): void;
724
+ findByInstance(instance: unknown): InstanceHolder | null;
725
+ findDependents(instanceName: string): string[];
598
726
  /**
599
- * Priority for resolution in FactoryContext.inject method.
600
- * Higher values take precedence.
727
+ * Updates dependency references when instance names change.
728
+ * Used during scope upgrades when instance names are regenerated with requestId.
729
+ *
730
+ * @param oldName The old instance name
731
+ * @param newName The new instance name
601
732
  */
602
- readonly priority: number;
733
+ updateDependencyReference(oldName: string, newName: string): void;
603
734
  /**
604
- * Request-specific metadata that can be used during instantiation.
735
+ * Registers a holder's dependencies in the reverse index.
605
736
  */
606
- readonly metadata: Map<string, any>;
737
+ private registerDependencies;
607
738
  /**
608
- * Timestamp when this context was created.
739
+ * Removes a holder from the reverse dependency index.
609
740
  */
610
- readonly createdAt: number;
741
+ private removeFromDependentsIndex;
742
+ }
743
+ //#endregion
744
+ //#region src/container/abstract-container.d.mts
745
+ /**
746
+ * Abstract base class for dependency injection containers.
747
+ *
748
+ * Provides shared implementation for common container operations.
749
+ * Both Container and ScopedContainer extend this class.
750
+ */
751
+ declare abstract class AbstractContainer implements IContainer {
611
752
  /**
612
- * Adds a pre-prepared instance to this context.
753
+ * The default scope used when adding instances without explicit registration.
613
754
  */
614
- addInstance(instanceName: string, instance: any, holder: InstanceHolder): void;
755
+ protected abstract readonly defaultScope: InjectableScope;
615
756
  /**
616
- * Adds a pre-prepared instance to this context.
757
+ * The request ID for scoped containers, undefined for root container.
617
758
  */
618
- addInstance(token: InjectionToken<any, undefined>, instance: any): void;
759
+ protected abstract readonly requestId: string | undefined;
619
760
  /**
620
- * Gets an instance holder from this context.
761
+ * Gets the storage for this container.
621
762
  */
622
- get(instanceName: string): InstanceHolder | undefined;
763
+ abstract getStorage(): UnifiedStorage;
623
764
  /**
624
- * Sets an instance holder by name.
765
+ * Gets the registry for this container.
625
766
  */
626
- set(instanceName: string, holder: InstanceHolder): void;
767
+ protected abstract getRegistry(): Registry;
627
768
  /**
628
- * Checks if this context has a pre-prepared instance.
769
+ * Gets the token resolver.
629
770
  */
630
- has(instanceName: string): boolean;
771
+ protected abstract getTokenResolver(): TokenResolver;
631
772
  /**
632
- * Clears all instances and holders from this context.
773
+ * Gets the name resolver.
633
774
  */
634
- clear(): void;
775
+ protected abstract getNameResolver(): NameResolver;
635
776
  /**
636
- * Gets metadata value by key.
777
+ * Gets the service invalidator.
637
778
  */
638
- getMetadata(key: string): any | undefined;
779
+ protected abstract getServiceInvalidator(): ServiceInvalidator;
639
780
  /**
640
- * Sets metadata value by key.
641
- */
642
- setMetadata(key: string, value: any): void;
643
- /**
644
- * Filters holders based on a predicate function.
781
+ * Gets an instance from the container.
645
782
  */
646
- filter(predicate: (value: InstanceHolder<any>, key: string) => boolean): Map<string, InstanceHolder>;
783
+ abstract get<T extends ClassType>(token: T): InstanceType<T> extends Factorable<infer R> ? Promise<R> : Promise<InstanceType<T>>;
784
+ abstract get<T extends ClassTypeWithArgument<R>, R>(token: T, args: R): Promise<InstanceType<T>>;
785
+ abstract get<T, S extends InjectionTokenSchemaType>(token: InjectionToken<T, S>, args: z.input<S>): Promise<T>;
786
+ abstract get<T, S extends InjectionTokenSchemaType, R extends boolean>(token: InjectionToken<T, S, R>): R extends false ? Promise<T> : S extends ZodType<infer Type> ? `Error: Your token requires args: ${Join<UnionToArray<keyof Type>, ', '>}` : 'Error: Your token requires args';
787
+ abstract get<T>(token: InjectionToken<T, undefined>): Promise<T>;
788
+ abstract get<T>(token: BoundInjectionToken<T, any>): Promise<T>;
789
+ abstract get<T>(token: FactoryInjectionToken<T, any>): Promise<T>;
647
790
  /**
648
- * Deletes a holder by name.
791
+ * Invalidates a service and its dependencies.
649
792
  */
650
- delete(name: string): boolean;
793
+ abstract invalidate(service: unknown): Promise<void>;
651
794
  /**
652
- * Gets the number of holders currently managed.
795
+ * Disposes the container and cleans up all resources.
653
796
  */
654
- size(): number;
797
+ abstract dispose(): Promise<void>;
655
798
  /**
656
- * Checks if this manager has any holders.
799
+ * Calculates the instance name for a given token and optional arguments.
800
+ *
801
+ * @internal
802
+ * @param token The class type, InjectionToken, BoundInjectionToken, or FactoryInjectionToken
803
+ * @param args Optional arguments (ignored for BoundInjectionToken which uses its bound value)
804
+ * @returns The calculated instance name string, or null if the token is a FactoryInjectionToken that is not yet resolved
657
805
  */
658
- isEmpty(): boolean;
806
+ calculateInstanceName(token: ClassType | InjectionToken<any, any> | BoundInjectionToken<any, any> | FactoryInjectionToken<any, any>, args?: unknown): string | null;
659
807
  /**
660
- * Registers a holder's dependencies in the reverse index.
808
+ * Checks if a service is registered in the container.
661
809
  */
662
- registerDependencies(holderName: string, deps: Set<string>): void;
810
+ isRegistered(token: any): boolean;
663
811
  /**
664
- * Gets all holder names that depend on the given instance name.
665
- * O(1) lookup using the reverse dependency index.
812
+ * Waits for all pending operations to complete.
666
813
  */
667
- getDependents(instanceName: string): string[];
668
- }
669
- /** @deprecated Use RequestContext instead */
670
- type RequestContextHolder = RequestContext;
671
- /**
672
- * Default implementation of RequestContext.
673
- *
674
- * Extends BaseHolderManager to provide holder management functionality
675
- * with request-specific metadata and lifecycle support.
676
- */
677
- declare class DefaultRequestContext extends BaseHolderManager implements RequestContext {
678
- readonly requestId: string;
679
- readonly priority: number;
680
- readonly metadata: Map<string, any>;
681
- readonly createdAt: number;
682
- constructor(requestId: string, priority?: number, initialMetadata?: Record<string, any>);
814
+ ready(): Promise<void>;
683
815
  /**
684
- * Public getter for holders to maintain interface compatibility.
816
+ * @internal
817
+ * Attempts to get an instance synchronously if it already exists.
685
818
  */
686
- get holders(): Map<string, InstanceHolder>;
819
+ tryGetSync<T>(token: any, args?: any): T | null;
687
820
  /**
688
- * Gets a holder by name. For RequestContext, this is a simple lookup.
821
+ * @internal
822
+ * Internal method for getting instances synchronously with configurable storage.
689
823
  */
690
- get(name: string): InstanceHolder | undefined;
824
+ protected tryGetSyncFromStorage<T>(token: any, args: any, storage: UnifiedStorage, requestId?: string): T | null;
691
825
  /**
692
- * Sets a holder by name.
826
+ * Adds an instance to the container.
827
+ * Accepts class types, InjectionTokens, and BoundInjectionTokens.
828
+ * Rejects InjectionTokens with required schemas (use BoundInjectionToken instead).
829
+ *
830
+ * @param token The class type, InjectionToken, or BoundInjectionToken to register the instance for
831
+ * @param instance The instance to store
693
832
  */
694
- set(name: string, holder: InstanceHolder): void;
833
+ addInstance<T>(token: ClassType | InjectionToken<T, any> | BoundInjectionToken<T, any>, instance: T): void;
695
834
  /**
696
- * Checks if a holder exists by name.
835
+ * @internal
836
+ * Internal method for adding instances with configurable scope and storage.
697
837
  */
698
- has(name: string): boolean;
699
- addInstance(instanceName: string | InjectionToken<any, undefined>, instance: any, holder?: InstanceHolder): void;
700
- clear(): void;
701
- getMetadata(key: string): any | undefined;
702
- setMetadata(key: string, value: any): void;
838
+ protected addInstanceToStorage<T>(token: ClassType | InjectionToken<T, any> | BoundInjectionToken<T, any>, instance: T, storage: UnifiedStorage, scope: InjectableScope, requestId?: string): void;
703
839
  }
704
- /**
705
- * Creates a new request context with the given parameters.
706
- */
707
- declare function createRequestContext(requestId: string, priority?: number, initialMetadata?: Record<string, any>): RequestContext;
708
840
  //#endregion
709
841
  //#region src/container/scoped-container.d.mts
710
842
  /**
@@ -715,23 +847,20 @@ declare function createRequestContext(requestId: string, priority?: number, init
715
847
  * This design eliminates race conditions that can occur with async operations
716
848
  * when multiple requests are processed concurrently.
717
849
  */
718
- declare class ScopedContainer implements IContainer {
850
+ declare class ScopedContainer extends AbstractContainer {
719
851
  private readonly parent;
720
852
  private readonly registry;
721
853
  readonly requestId: string;
722
- private readonly requestContextHolder;
723
- private readonly holderStorage;
854
+ protected readonly defaultScope = InjectableScope.Request;
855
+ private readonly storage;
724
856
  private disposed;
725
- constructor(parent: Container, registry: Registry, requestId: string, metadata?: Record<string, any>, priority?: number);
726
- /**
727
- * Gets the request context holder for this scoped container.
728
- */
729
- getRequestContextHolder(): RequestContext;
730
- /**
731
- * Gets the holder storage for this scoped container.
732
- * Used by InstanceResolver for request-scoped resolution.
733
- */
734
- getHolderStorage(): IHolderStorage;
857
+ private readonly metadata;
858
+ constructor(parent: Container, registry: Registry, requestId: string, metadata?: Record<string, any>);
859
+ getStorage(): UnifiedStorage;
860
+ protected getRegistry(): Registry;
861
+ protected getTokenResolver(): TokenResolver;
862
+ protected getNameResolver(): NameResolver;
863
+ protected getServiceInvalidator(): ServiceInvalidator;
735
864
  /**
736
865
  * Gets the request ID for this scoped container.
737
866
  */
@@ -748,13 +877,9 @@ declare class ScopedContainer implements IContainer {
748
877
  * Sets metadata on the request context.
749
878
  */
750
879
  setMetadata(key: string, value: any): void;
751
- /**
752
- * Adds a pre-prepared instance to the request context.
753
- */
754
- addInstance(token: InjectionToken<any, undefined>, instance: any): void;
755
880
  /**
756
881
  * Gets an instance from the container.
757
- * Request-scoped services are resolved from this container's context.
882
+ * Request-scoped services are resolved from this container's storage.
758
883
  * All other services are delegated to the parent container.
759
884
  */
760
885
  get<T extends ClassType>(token: T): InstanceType<T> extends Factorable<infer R> ? Promise<R> : Promise<InstanceType<T>>;
@@ -766,354 +891,251 @@ declare class ScopedContainer implements IContainer {
766
891
  get<T>(token: FactoryInjectionToken<T, any>): Promise<T>;
767
892
  /**
768
893
  * Invalidates a service and its dependencies.
769
- * For request-scoped services, invalidation is handled within this context.
770
894
  */
771
895
  invalidate(service: unknown): Promise<void>;
772
896
  /**
773
- * Checks if a service is registered.
774
- */
775
- isRegistered(token: any): boolean;
776
- /**
777
- * Disposes this scoped container and cleans up all request-scoped instances.
778
- * This is an alias for endRequest() for IContainer compatibility.
897
+ * Disposes the container and cleans up all resources.
898
+ * Alias for endRequest().
779
899
  */
780
900
  dispose(): Promise<void>;
781
- /**
782
- * Ends the request and cleans up all request-scoped instances.
783
- * Uses the invalidation system to properly cascade to dependent singletons.
784
- */
785
- endRequest(): Promise<void>;
786
- /**
787
- * Waits for all pending operations to complete.
788
- */
789
- ready(): Promise<void>;
790
901
  /**
791
902
  * @internal
792
- * Attempts to get an instance synchronously if it already exists and is ready.
793
- * For request-scoped services, checks this container's context first.
794
- * For other services, delegates to the parent container.
795
- *
796
- * Returns null if the instance doesn't exist or is not yet ready (still creating).
903
+ * Attempts to get an instance synchronously if it already exists.
904
+ * Checks request storage first, then delegates to parent.
797
905
  */
798
906
  tryGetSync<T>(token: any, args?: any): T | null;
799
907
  /**
800
- * Checks if a token is for a request-scoped service.
801
- */
802
- private isRequestScoped;
803
- /**
804
- * Resolves a request-scoped service from this container's context.
805
- * Uses locking to prevent duplicate initialization during concurrent resolution.
908
+ * Adds an instance to the container.
909
+ * Overrides base class to check disposed state.
806
910
  */
807
- private resolveRequestScoped;
911
+ addInstance<T>(token: ClassType | InjectionToken<T, any> | BoundInjectionToken<T, any>, instance: T): void;
808
912
  /**
809
- * Stores an instance in the request context.
810
- * Called by Container during request-scoped service resolution.
913
+ * Ends the request and cleans up all request-scoped services.
811
914
  */
812
- storeRequestInstance(instanceName: string, instance: any, holder: InstanceHolder): void;
813
- /**
814
- * Gets an existing instance from the request context.
815
- * Called by Container during resolution to check for existing instances.
816
- */
817
- getRequestInstance(instanceName: string): InstanceHolder | undefined;
818
- /**
819
- * Generates a prefixed event name for request-scoped services.
820
- * Format: {requestId}:{instanceName}
821
- */
822
- getPrefixedEventName(instanceName: string): string;
823
- }
824
- //#endregion
825
- //#region src/internal/lifecycle/lifecycle-event-bus.d.mts
826
- /**
827
- * Event bus for service lifecycle events (create, destroy, etc.).
828
- *
829
- * Enables loose coupling between services by allowing them to subscribe
830
- * to lifecycle events of their dependencies without direct references.
831
- * Used primarily for invalidation cascading.
832
- */
833
- declare class LifecycleEventBus {
834
- private readonly logger;
835
- private listeners;
836
- constructor(logger?: Console | null);
837
- on<Event extends string | `pre:${string}` | `post:${string}`>(ns: string, event: Event, listener: (event: Event) => void): () => void;
838
- emit(key: string, event: string): Promise<PromiseSettledResult<any>[] | undefined>;
839
- }
840
- //#endregion
841
- //#region src/internal/holder/holder-manager.d.mts
842
- /**
843
- * Manages the storage and retrieval of singleton instance holders.
844
- *
845
- * Provides CRUD operations and filtering for the holder map.
846
- * Handles holder state validation (destroying, error states) on retrieval.
847
- */
848
- declare class HolderManager extends BaseHolderManager {
849
- constructor(logger?: Console | null);
850
- get(name: string): [DIError, InstanceHolder] | [DIError] | [undefined, InstanceHolder];
851
- set(name: string, holder: InstanceHolder): void;
852
- has(name: string): [DIError] | [undefined, boolean];
853
- /**
854
- * Creates a new holder with Created status and stores it.
855
- * This is useful for creating holders that already have their instance ready.
856
- * @param name The name of the instance
857
- * @param instance The actual instance to store
858
- * @param type The injectable type
859
- * @param scope The injectable scope
860
- * @param deps Optional set of dependencies
861
- * @returns The created holder
862
- */
863
- storeCreatedHolder<Instance>(name: string, instance: Instance, type: InjectableType, scope: InjectableScope, deps?: Set<string>): InstanceHolder<Instance>;
915
+ endRequest(): Promise<void>;
864
916
  }
865
917
  //#endregion
866
- //#region src/internal/core/invalidator.d.mts
867
- interface ClearAllOptions {
868
- /** Maximum number of invalidation rounds to prevent infinite loops (default: 10) */
869
- maxRounds?: number;
870
- /** Whether to wait for all services to settle before starting (default: true) */
871
- waitForSettlement?: boolean;
872
- }
873
- interface InvalidationOptions {
874
- /** Whether to emit events after invalidation (default: true for singletons) */
875
- emitEvents?: boolean;
876
- /** Custom event emitter function */
877
- onInvalidated?: (instanceName: string) => Promise<void>;
878
- /** Whether to cascade invalidation to dependents (default: true) */
879
- cascade?: boolean;
880
- /** Internal: tracks services being invalidated in the current call chain to prevent circular loops */
881
- _invalidating?: Set<string>;
882
- }
918
+ //#region src/internal/core/scope-tracker.d.mts
883
919
  /**
884
- * Manages graceful service cleanup with dependency-aware invalidation.
920
+ * Component for tracking and handling scope upgrades.
885
921
  *
886
- * Ensures services are destroyed in the correct order based on their dependencies.
887
- * Works with any IHolderStorage implementation, enabling unified invalidation
888
- * for both singleton and request-scoped services.
922
+ * Detects when a Singleton service needs to be upgraded to Request scope
923
+ * and coordinates the scope upgrade process atomically.
889
924
  */
890
- declare class Invalidator {
891
- private readonly eventBus;
925
+ declare class ScopeTracker {
926
+ private readonly registry;
927
+ private readonly nameResolver;
892
928
  private readonly logger;
893
- private readonly storage;
894
- constructor(manager: HolderManager, eventBus: LifecycleEventBus | null, logger?: Console | null);
895
- /**
896
- * Invalidates a service and all its dependencies.
897
- * Works with the configured storage (singleton by default).
898
- */
899
- invalidate(service: string, round?: number): Promise<any>;
929
+ constructor(registry: Registry, nameResolver: NameResolver, logger?: Console | null);
900
930
  /**
901
- * Invalidates a service using a specific storage.
902
- * This allows request-scoped invalidation using a RequestStorage.
931
+ * Checks if a dependency requires scope upgrade and performs it if needed.
932
+ * Called during service resolution when a dependency is resolved.
903
933
  *
904
- * @param service The instance name to invalidate
905
- * @param storage The storage to use for this invalidation
906
- * @param round Current invalidation round (for recursion limiting)
907
- * @param options Additional options for invalidation behavior
908
- */
909
- invalidateWithStorage(service: string, storage: IHolderStorage, round?: number, options?: InvalidationOptions): Promise<void>;
910
- /**
911
- * Gracefully clears all services using invalidation logic.
912
- * This method respects service dependencies and ensures proper cleanup order.
913
- * Services that depend on others will be invalidated first, then their dependencies.
914
- */
915
- clearAll(options?: ClearAllOptions): Promise<void>;
916
- /**
917
- * Gracefully clears all services in a specific storage.
918
- * This allows clearing request-scoped services using a RequestStorage.
919
- */
920
- clearAllWithStorage(storage: IHolderStorage, options?: ClearAllOptions): Promise<void>;
921
- /**
922
- * Waits for all services to settle (either created, destroyed, or error state).
923
- */
924
- ready(): Promise<void>;
925
- /**
926
- * Waits for all services in a specific storage to settle.
927
- */
928
- readyWithStorage(storage: IHolderStorage): Promise<void>;
929
- /**
930
- * Invalidates a single holder using a specific storage.
931
- */
932
- private invalidateHolderWithStorage;
933
- /**
934
- * Common invalidation logic for holders based on their status.
935
- */
936
- private invalidateHolderByStatus;
937
- /**
938
- * Destroys a holder using a specific storage.
934
+ * @param currentServiceName - Name of the service being created
935
+ * @param currentServiceScope - Current scope of the service being created
936
+ * @param dependencyName - Name of the dependency being resolved
937
+ * @param dependencyScope - Scope of the dependency
938
+ * @param dependencyToken - Token of the dependency
939
+ * @param singletonStorage - Singleton storage instance
940
+ * @param requestStorage - Request storage instance (if in request context)
941
+ * @param requestId - Request ID (if in request context)
942
+ * @returns [needsUpgrade: boolean, newName?: string] - whether upgrade occurred and new name
943
+ */
944
+ checkAndUpgradeScope(currentServiceName: string, currentServiceScope: InjectableScope, dependencyName: string, dependencyScope: InjectableScope, dependencyToken: InjectionToken<any, any>, singletonStorage: IHolderStorage, requestStorage?: IHolderStorage, requestId?: string): [boolean, string?];
945
+ /**
946
+ * Performs the actual scope upgrade from Singleton to Request.
947
+ * This is the core migration logic.
948
+ *
949
+ * @param serviceName - Current service name (without requestId)
950
+ * @param token - Service injection token
951
+ * @param singletonStorage - Source storage
952
+ * @param requestStorage - Target storage
953
+ * @param requestId - Request ID to include in new name
954
+ * @returns [success: boolean, newName?: string, error?: DIError]
939
955
  */
940
- private destroyHolderWithStorage;
956
+ upgradeScopeToRequest(serviceName: string, token: InjectionToken<any, any>, singletonStorage: IHolderStorage, requestStorage: IHolderStorage, requestId: string): Promise<[boolean, string?, DIError?]>;
941
957
  /**
942
- * Waits for a holder to settle (either created, destroyed, or error state).
958
+ * Synchronous part of scope upgrade - handles immediate updates.
959
+ * Async operations (like waiting for holder creation) should be done separately.
943
960
  */
944
- private waitForHolderToSettle;
961
+ private upgradeScopeToRequestSync;
945
962
  /**
946
- * Clears services with dependency awareness for a specific storage.
963
+ * Updates all parent dependencies to reference the new service name.
964
+ *
965
+ * @param oldName - Original service name
966
+ * @param newName - New service name with requestId
967
+ * @param singletonStorage - Singleton storage to check
968
+ * @param requestStorage - Request storage to check
947
969
  */
948
- private clearServicesWithDependencyAwarenessForStorage;
970
+ updateParentDependencies(oldName: string, newName: string, singletonStorage: IHolderStorage, requestStorage?: IHolderStorage): void;
971
+ }
972
+ //#endregion
973
+ //#region src/internal/core/service-initializer.d.mts
974
+ /**
975
+ * Creates service instances from registry records.
976
+ *
977
+ * Handles both class-based (@Injectable) and factory-based (@Factory) services,
978
+ * managing the instantiation lifecycle including lifecycle hook invocation.
979
+ */
980
+ declare class ServiceInitializer {
981
+ private readonly injectors;
982
+ constructor(injectors: Injectors);
949
983
  /**
950
- * Finds services that are ready to be cleared in the current round.
951
- * A service is ready if all its dependencies have already been cleared.
984
+ * Instantiates a service based on its registry record.
985
+ * @param ctx The factory context for dependency injection
986
+ * @param record The factory record from the registry
987
+ * @param args Optional arguments for the service
988
+ * @returns Promise resolving to [undefined, instance] or [error]
952
989
  */
953
- private findServicesReadyForClearingInStorage;
990
+ instantiateService<T>(ctx: ServiceInitializationContext, record: FactoryRecord<T, any>, args?: any): Promise<[undefined, T] | [DIError]>;
954
991
  /**
955
- * Force clears services that couldn't be cleared through normal dependency resolution.
956
- * This handles edge cases like circular dependencies.
992
+ * Instantiates a class-based service (Injectable decorator).
993
+ * @param ctx The factory context for dependency injection
994
+ * @param record The factory record from the registry
995
+ * @param args Optional arguments for the service constructor
996
+ * @returns Promise resolving to [undefined, instance] or [error]
957
997
  */
958
- private forceClearServicesInStorage;
998
+ private instantiateClass;
959
999
  /**
960
- * Emits events to listeners for instance lifecycle events.
1000
+ * Instantiates a factory-based service (Factory decorator).
1001
+ * @param ctx The factory context for dependency injection
1002
+ * @param record The factory record from the registry
1003
+ * @param args Optional arguments for the factory
1004
+ * @returns Promise resolving to [undefined, instance] or [error]
961
1005
  */
962
- private emitInstanceEvent;
1006
+ private instantiateFactory;
963
1007
  }
964
1008
  //#endregion
965
- //#region src/internal/core/token-processor.d.mts
1009
+ //#region src/internal/core/instance-resolver.d.mts
966
1010
  /**
967
- * Handles token validation, normalization, and instance name generation.
1011
+ * Resolves instances from tokens, handling caching, creation, and scope rules.
968
1012
  *
969
- * Provides utilities for resolving tokens to their underlying InjectionToken,
970
- * validating arguments against schemas, and generating unique instance identifiers.
1013
+ * Uses unified storage for both singleton and request-scoped services.
1014
+ * Coordinates with ServiceInitializer for actual service creation.
1015
+ * Integrates ScopeTracker for automatic scope upgrades.
971
1016
  */
972
- declare class TokenProcessor {
1017
+ declare class InstanceResolver {
1018
+ private readonly registry;
1019
+ private readonly storage;
1020
+ private readonly serviceInitializer;
1021
+ private readonly tokenResolver;
1022
+ private readonly nameResolver;
1023
+ private readonly scopeTracker;
1024
+ private readonly serviceInvalidator;
1025
+ private readonly eventBus;
973
1026
  private readonly logger;
974
- private readonly instanceNameCache;
975
- constructor(logger?: Console | null);
1027
+ constructor(registry: Registry, storage: IHolderStorage, serviceInitializer: ServiceInitializer, tokenResolver: TokenResolver, nameResolver: NameResolver, scopeTracker: ScopeTracker, serviceInvalidator: ServiceInvalidator, eventBus: LifecycleEventBus, logger?: Console | null);
976
1028
  /**
977
- * Normalizes a token to an InjectionToken.
978
- * Handles class constructors by getting their injectable token.
1029
+ * Resolves an instance for the given token and arguments.
1030
+ * This method is used for singleton and transient services.
979
1031
  *
980
- * @param token A class constructor, InjectionToken, BoundInjectionToken, or FactoryInjectionToken
981
- * @returns The normalized InjectionTokenType
1032
+ * @param token The injection token
1033
+ * @param args Optional arguments
1034
+ * @param contextContainer The container to use for creating context
1035
+ * @param requestStorage Optional request storage (for scope upgrades)
1036
+ * @param requestId Optional request ID (for scope upgrades)
982
1037
  */
983
- normalizeToken(token: AnyInjectableType): InjectionTokenType;
1038
+ resolveInstance(token: AnyInjectableType, args: any, contextContainer: IContainer, requestStorage?: IHolderStorage, requestId?: string): Promise<[undefined, any] | [DIError]>;
984
1039
  /**
985
- * Gets the underlying "real" token from wrapped tokens.
986
- * For BoundInjectionToken and FactoryInjectionToken, returns the wrapped token.
987
- * For other tokens, returns the token itself.
1040
+ * Resolves a request-scoped instance for a ScopedContainer.
1041
+ * The service will be stored in the ScopedContainer's request storage.
988
1042
  *
989
- * @param token The token to unwrap
990
- * @returns The underlying InjectionToken
1043
+ * @param token The injection token
1044
+ * @param args Optional arguments
1045
+ * @param scopedContainer The ScopedContainer that owns the request context
991
1046
  */
992
- getRealToken<T = unknown>(token: InjectionTokenType): InjectionToken<T>;
1047
+ resolveRequestScopedInstance(token: AnyInjectableType, args: any, scopedContainer: ScopedContainer): Promise<[undefined, any] | [DIError]>;
993
1048
  /**
994
- * Convenience method that normalizes a token and then gets the real token.
995
- * Useful for checking registry entries where you need the actual registered token.
1049
+ * Unified resolution method that works with any IHolderStorage.
1050
+ * This eliminates duplication between singleton and request-scoped resolution.
996
1051
  *
997
- * @param token Any injectable type
998
- * @returns The underlying InjectionToken
1052
+ * IMPORTANT: The check-and-store logic is carefully designed to avoid race conditions.
1053
+ * The storage check and holder creation must happen synchronously (no awaits between).
1054
+ *
1055
+ * @param token The injection token
1056
+ * @param args Optional arguments
1057
+ * @param contextContainer The container for context
1058
+ * @param storage The storage strategy to use
1059
+ * @param scopedContainer Optional scoped container for request-scoped services
1060
+ * @param requestStorage Optional request storage (for scope upgrades)
1061
+ * @param requestId Optional request ID (for scope upgrades)
999
1062
  */
1000
- getRegistryToken<T = unknown>(token: AnyInjectableType): InjectionToken<T>;
1063
+ private resolveWithStorage;
1001
1064
  /**
1002
- * Validates and resolves token arguments, handling factory token resolution and validation.
1065
+ * Internal method to resolve token args and create instance name.
1066
+ * Handles factory token resolution and validation.
1003
1067
  */
1004
- validateAndResolveTokenArgs(token: AnyInjectableType, args?: any): [DIError | undefined, {
1005
- actualToken: InjectionTokenType;
1006
- validatedArgs?: any;
1007
- }];
1068
+ private resolveTokenAndPrepareInstanceName;
1008
1069
  /**
1009
- * Generates a unique instance name based on token and arguments.
1010
- * Results are cached using an LRU cache for performance.
1070
+ * Handles storage error states (destroying, error, etc.).
1071
+ * Returns a result if handled, null if should proceed with creation.
1011
1072
  */
1012
- generateInstanceName(token: InjectionTokenType, args: any): string;
1073
+ private handleStorageError;
1013
1074
  /**
1014
- * Formats a single argument value for instance name generation.
1075
+ * Creates a new instance and stores it using the provided storage strategy.
1076
+ * This unified method replaces instantiateServiceFromRegistry and createRequestScopedInstance.
1077
+ *
1078
+ * For transient services, the instance is created but not stored (no caching).
1015
1079
  */
1016
- formatArgValue(value: any): string;
1080
+ private createAndStoreInstance;
1017
1081
  /**
1018
- * Creates a factory context for dependency injection during service instantiation.
1019
- * @param container The container instance (Container or ScopedContainer) for dependency resolution
1020
- * @param onDependencyResolved Callback when a dependency is resolved, receives the instance name
1082
+ * Creates a transient instance without storage or locking.
1083
+ * Each call creates a new instance.
1021
1084
  */
1022
- createFactoryContext(container: IContainer, onDependencyResolved?: (instanceName: string) => void): FactoryContext & {
1023
- getDestroyListeners: () => (() => void)[];
1024
- deps: Set<string>;
1025
- };
1026
- }
1027
- //#endregion
1028
- //#region src/internal/core/service-locator.d.mts
1029
- /**
1030
- * Core DI engine that coordinates service instantiation, resolution, and lifecycle.
1031
- *
1032
- * Acts as the central orchestrator for dependency injection operations,
1033
- * delegating to specialized components (InstanceResolver, Instantiator, Invalidator)
1034
- * for specific tasks.
1035
- */
1036
- declare class ServiceLocator {
1037
- private readonly registry;
1038
- private readonly logger;
1039
- private readonly injectors;
1040
- private readonly eventBus;
1041
- private readonly manager;
1042
- private readonly instantiator;
1043
- private readonly tokenProcessor;
1044
- private readonly invalidator;
1045
- private readonly instanceResolver;
1046
- constructor(registry?: Registry, logger?: Console | null, injectors?: Injectors);
1047
- getEventBus(): LifecycleEventBus;
1048
- getManager(): HolderManager;
1049
- getInvalidator(): Invalidator;
1050
- getTokenProcessor(): TokenProcessor;
1051
- getInstanceIdentifier(token: AnyInjectableType, args?: any): string;
1085
+ private createTransientInstance;
1052
1086
  /**
1053
- * Gets or creates an instance for the given token.
1054
- * @param token The injection token
1055
- * @param args Optional arguments
1056
- * @param contextContainer The container to use for creating FactoryContext
1087
+ * Handles successful service instantiation.
1057
1088
  */
1058
- getInstance(token: AnyInjectableType, args: any, contextContainer: IContainer): Promise<any[]>;
1089
+ private handleInstantiationSuccess;
1059
1090
  /**
1060
- * Gets or throws an instance for the given token.
1061
- * @param token The injection token
1062
- * @param args Optional arguments
1063
- * @param contextContainer The container to use for creating FactoryContext
1091
+ * Handles service instantiation errors.
1064
1092
  */
1065
- getOrThrowInstance<Instance>(token: AnyInjectableType, args: any, contextContainer: IContainer): Promise<Instance>;
1093
+ private handleInstantiationError;
1066
1094
  /**
1067
- * Resolves a request-scoped service for a ScopedContainer.
1068
- * The service will be stored in the ScopedContainer's request context.
1069
- *
1070
- * @param token The injection token
1071
- * @param args Optional arguments
1072
- * @param scopedContainer The ScopedContainer that owns the request context
1095
+ * Handles instantiation result (success or error).
1073
1096
  */
1074
- resolveRequestScoped(token: AnyInjectableType, args: any, scopedContainer: ScopedContainer): Promise<any>;
1075
- getSyncInstance<Instance, Schema extends InjectionTokenSchemaType | undefined>(token: AnyInjectableType, args: Schema extends ZodObject ? z.input<Schema> : Schema extends ZodOptional<ZodObject> ? z.input<Schema> | undefined : undefined, contextContainer: IContainer): Instance | null;
1076
- invalidate(service: string, round?: number): Promise<any>;
1097
+ private handleInstantiationResult;
1077
1098
  /**
1078
- * Gracefully clears all services in the ServiceLocator using invalidation logic.
1079
- * This method respects service dependencies and ensures proper cleanup order.
1080
- * Services that depend on others will be invalidated first, then their dependencies.
1099
+ * Waits for an instance holder to be ready and returns the appropriate result.
1081
1100
  *
1082
- * @param options Optional configuration for the clearing process
1083
- * @returns Promise that resolves when all services have been cleared
1084
- */
1085
- clearAll(options?: ClearAllOptions): Promise<void>;
1086
- /**
1087
- * Waits for all services to settle (either created, destroyed, or error state).
1101
+ * @param holder The holder to wait for
1102
+ * @param waiterHolder Optional holder that is doing the waiting (for circular dependency detection)
1103
+ * @param getHolder Optional function to retrieve holders by name (required if waiterHolder is provided)
1088
1104
  */
1089
- ready(): Promise<void>;
1105
+ private waitForInstanceReady;
1090
1106
  /**
1091
- * Helper method for InstanceResolver to generate instance names.
1092
- * This is needed for the factory context creation.
1107
+ * Creates a ServiceInitializationContext for service instantiation.
1093
1108
  */
1094
- generateInstanceName(token: any, args: any): string;
1109
+ private createServiceInitializationContext;
1095
1110
  }
1096
1111
  //#endregion
1097
1112
  //#region src/container/container.d.mts
1098
1113
  /**
1099
1114
  * Main dependency injection container.
1100
1115
  *
1101
- * Provides a simplified public API for dependency injection, wrapping
1102
- * a ServiceLocator instance. Handles singleton and transient services directly,
1116
+ * Provides a simplified public API for dependency injection.
1117
+ * Handles singleton and transient services directly,
1103
1118
  * while request-scoped services require using beginRequest() to create a ScopedContainer.
1104
1119
  */
1105
- declare class Container implements IContainer {
1120
+ declare class Container extends AbstractContainer {
1106
1121
  protected readonly registry: Registry;
1107
1122
  protected readonly logger: Console | null;
1108
1123
  protected readonly injectors: Injectors;
1109
- private readonly serviceLocator;
1124
+ protected readonly defaultScope = InjectableScope.Singleton;
1125
+ protected readonly requestId: undefined;
1126
+ private readonly storage;
1127
+ private readonly serviceInitializer;
1128
+ private readonly serviceInvalidator;
1129
+ private readonly tokenResolver;
1130
+ private readonly nameResolver;
1131
+ private readonly scopeTracker;
1132
+ private readonly eventBus;
1133
+ private readonly instanceResolver;
1110
1134
  private readonly activeRequestIds;
1111
1135
  constructor(registry?: Registry, logger?: Console | null, injectors?: Injectors);
1112
1136
  private registerSelf;
1113
1137
  /**
1114
1138
  * Gets an instance from the container.
1115
- * This method has the same type signature as the inject method from get-injectors.mts
1116
- *
1117
1139
  * NOTE: Request-scoped services cannot be resolved directly from Container.
1118
1140
  * Use beginRequest() to create a ScopedContainer for request-scoped services.
1119
1141
  */
@@ -1125,87 +1147,46 @@ declare class Container implements IContainer {
1125
1147
  get<T>(token: BoundInjectionToken<T, any>): Promise<T>;
1126
1148
  get<T>(token: FactoryInjectionToken<T, any>): Promise<T>;
1127
1149
  /**
1128
- * Gets an instance with a specific container context.
1129
- * Used by ScopedContainer to delegate singleton/transient resolution
1130
- * while maintaining the correct container context for nested inject() calls.
1131
- *
1132
- * @internal
1133
- */
1134
- getWithContext(token: ClassType | InjectionToken<any> | BoundInjectionToken<any, any> | FactoryInjectionToken<any, any>, args: unknown, contextContainer: IContainer): Promise<any>;
1135
- /**
1136
- * Resolves a request-scoped service for a ScopedContainer.
1137
- * The service will be stored in the ScopedContainer's request context.
1138
- *
1139
- * @internal
1140
- */
1141
- resolveForRequest(token: ClassType | InjectionToken<any> | BoundInjectionToken<any, any> | FactoryInjectionToken<any, any>, args: unknown, scopedContainer: ScopedContainer): Promise<any>;
1142
- /**
1143
- * Gets the underlying ServiceLocator instance for advanced usage
1144
- */
1145
- getServiceLocator(): ServiceLocator;
1146
- /**
1147
- * Gets the registry
1148
- */
1149
- getRegistry(): Registry;
1150
- /**
1151
- * Invalidates a service and its dependencies
1150
+ * Invalidates a service and its dependencies.
1152
1151
  */
1153
1152
  invalidate(service: unknown): Promise<void>;
1154
1153
  /**
1155
- * Gets a service holder by instance (reverse lookup)
1156
- */
1157
- private getHolderByInstance;
1158
- /**
1159
- * Checks if a service is registered in the container
1160
- */
1161
- isRegistered(token: any): boolean;
1162
- /**
1163
- * Disposes the container and cleans up all resources
1154
+ * Disposes the container and cleans up all resources.
1164
1155
  */
1165
1156
  dispose(): Promise<void>;
1166
- /**
1167
- * Waits for all pending operations to complete
1168
- */
1169
- ready(): Promise<void>;
1170
1157
  /**
1171
1158
  * @internal
1172
1159
  * Attempts to get an instance synchronously if it already exists.
1173
- * Returns null if the instance doesn't exist or is not ready.
1160
+ * Overrides base class to support requestId parameter for ScopedContainer compatibility.
1174
1161
  */
1175
- tryGetSync<T>(token: any, args?: any): T | null;
1162
+ tryGetSync<T>(token: any, args?: any, requestId?: string): T | null;
1176
1163
  /**
1177
1164
  * Begins a new request context and returns a ScopedContainer.
1178
- *
1179
- * The ScopedContainer provides isolated request-scoped service resolution
1180
- * while delegating singleton and transient services to this Container.
1181
- *
1182
- * @param requestId Unique identifier for this request
1183
- * @param metadata Optional metadata for the request
1184
- * @param priority Priority for resolution (higher = more priority)
1185
- * @returns A ScopedContainer for this request
1186
1165
  */
1187
- beginRequest(requestId: string, metadata?: Record<string, any>, priority?: number): ScopedContainer;
1166
+ beginRequest(requestId: string, metadata?: Record<string, any>): ScopedContainer;
1188
1167
  /**
1189
- * Removes a request ID from the active set.
1190
- * Called by ScopedContainer when the request ends.
1191
- *
1192
- * @internal
1193
- */
1194
- removeActiveRequest(requestId: string): void;
1195
- /**
1196
- * Gets the set of active request IDs.
1168
+ * Gets all active request IDs.
1197
1169
  */
1198
1170
  getActiveRequestIds(): ReadonlySet<string>;
1199
1171
  /**
1200
- * Checks if a request ID is currently active.
1172
+ * Checks if a request is active.
1201
1173
  */
1202
1174
  hasActiveRequest(requestId: string): boolean;
1203
1175
  /**
1204
- * Clears all instances and bindings from the container.
1205
- * This is useful for testing or resetting the container state.
1176
+ * Removes a request ID from active requests.
1177
+ * Called by ScopedContainer when request ends.
1206
1178
  */
1207
- clear(): Promise<void>;
1179
+ removeRequestId(requestId: string): void;
1180
+ getStorage(): UnifiedStorage;
1181
+ getServiceInitializer(): ServiceInitializer;
1182
+ getServiceInvalidator(): ServiceInvalidator;
1183
+ getTokenResolver(): TokenResolver;
1184
+ getNameResolver(): NameResolver;
1185
+ getScopeTracker(): ScopeTracker;
1186
+ getEventBus(): LifecycleEventBus;
1187
+ getRegistry(): Registry;
1188
+ getInstanceResolver(): InstanceResolver;
1208
1189
  }
1209
1190
  //#endregion
1210
- export { InjectRequest as $, InjectableScope as A, ClassTypeWithoutArguments as B, InstanceStatus as C, Registry as D, FactoryRecord as E, ClassTypeWithArgument as F, OptionalInjectionTokenSchemaType as G, InjectionToken as H, ClassTypeWithInstance as I, Injectors as J, FactoryContext as K, ClassTypeWithInstanceAndArgument as L, BaseInjectionTokenSchemaType as M, BoundInjectionToken as N, globalRegistry as O, ClassType as P, FactorableWithArgs as Q, ClassTypeWithInstanceAndOptionalArgument as R, InstanceHolderError as S, DIErrorCode as T, InjectionTokenSchemaType as U, FactoryInjectionToken as V, InjectionTokenType as W, IContainer as X, getInjectors as Y, Factorable as Z, InstanceEffect as _, HolderManager as a, UnionToIntersection as at, InstanceHolderCreating as b, DefaultRequestContext as c, createRequestContext as d, InjectState as et, BaseHolderManager as f, InstanceDestroyListener as g, IHolderStorage as h, Invalidator as i, UnionToArray as it, AnyInjectableType as j, InjectableType as k, RequestContext as l, HolderGetResult as m, ServiceLocator as n, Join as nt, LifecycleEventBus as o, UnionToOvlds as ot, HolderReadyResult as p, getInjectableToken as q, TokenProcessor as r, PopUnion as rt, ScopedContainer as s, Container as t, IsUnion as tt, RequestContextHolder as u, InstanceHolder as v, DIError as w, InstanceHolderDestroying as x, InstanceHolderCreated as y, ClassTypeWithOptionalArgument as z };
1211
- //# sourceMappingURL=container-Bp1W-pWJ.d.mts.map
1191
+ export { ClassTypeWithoutArguments as $, Injectors as A, PopUnion as B, NameResolver as C, Factorable as D, globalRegistry as E, IContainer as F, BaseInjectionTokenSchemaType as G, UnionToIntersection as H, InjectRequest as I, ClassTypeWithArgument as J, BoundInjectionToken as K, InjectState as L, ServiceInitializationContext as M, InjectableType as N, FactorableWithArgs as O, InjectableScope as P, ClassTypeWithOptionalArgument as Q, IsUnion as R, DIErrorCode as S, Registry as T, UnionToOvlds as U, UnionToArray as V, AnyInjectableType as W, ClassTypeWithInstanceAndArgument as X, ClassTypeWithInstance as Y, ClassTypeWithInstanceAndOptionalArgument as Z, InstanceHolderCreating as _, ScopedContainer as a, InstanceStatus as b, ClearAllOptions as c, LifecycleEventBus as d, FactoryInjectionToken as et, HolderGetResult as f, InstanceHolderCreated as g, InstanceHolder as h, ScopeTracker as i, OptionalInjectionTokenSchemaType as it, getInjectors as j, FactoryContext as k, InvalidationOptions as l, InstanceDestroyListener as m, InstanceResolver as n, InjectionTokenSchemaType as nt, UnifiedStorage as o, IHolderStorage as p, ClassType as q, ServiceInitializer as r, InjectionTokenType as rt, TokenResolver as s, Container as t, InjectionToken as tt, ServiceInvalidator as u, InstanceHolderDestroying as v, FactoryRecord as w, DIError as x, InstanceHolderError as y, Join as z };
1192
+ //# sourceMappingURL=container-CNiqesCL.d.mts.map