@codefast/di 0.3.14-canary.0 → 0.3.14-canary.2

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 (66) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +54 -29
  3. package/dist/binding-scope.d.mts +11 -0
  4. package/dist/binding-scope.mjs +19 -0
  5. package/dist/binding-select.d.mts +8 -26
  6. package/dist/binding-select.mjs +56 -49
  7. package/dist/binding.d.mts +107 -327
  8. package/dist/binding.mjs +19 -324
  9. package/dist/constraints.d.mts +11 -29
  10. package/dist/constraints.mjs +32 -36
  11. package/dist/constructor-type.d.mts +17 -0
  12. package/dist/constructor-type.mjs +1 -0
  13. package/dist/container.d.mts +44 -128
  14. package/dist/container.mjs +664 -433
  15. package/dist/decorators/inject.d.mts +19 -50
  16. package/dist/decorators/inject.mjs +131 -93
  17. package/dist/decorators/injectable.d.mts +16 -37
  18. package/dist/decorators/injectable.mjs +47 -66
  19. package/dist/decorators/lifecycle-decorators.d.mts +2 -22
  20. package/dist/decorators/lifecycle-decorators.mjs +81 -37
  21. package/dist/dependency-graph.d.mts +24 -54
  22. package/dist/dependency-graph.mjs +51 -153
  23. package/dist/environment.d.mts +38 -12
  24. package/dist/environment.mjs +82 -16
  25. package/dist/errors.d.mts +69 -188
  26. package/dist/errors.mjs +92 -219
  27. package/dist/graph-adapters/cytoscape.d.mts +24 -0
  28. package/dist/graph-adapters/cytoscape.mjs +23 -0
  29. package/dist/graph-adapters/dot.d.mts +6 -0
  30. package/dist/graph-adapters/dot.mjs +17 -0
  31. package/dist/graph-adapters/reactflow.d.mts +29 -0
  32. package/dist/graph-adapters/reactflow.mjs +29 -0
  33. package/dist/graph-adapters/types.d.mts +2 -0
  34. package/dist/graph-adapters/types.mjs +1 -0
  35. package/dist/index.d.mts +16 -9
  36. package/dist/index.mjs +8 -5
  37. package/dist/inspector.d.mts +34 -95
  38. package/dist/inspector.mjs +57 -256
  39. package/dist/lifecycle.d.mts +20 -53
  40. package/dist/lifecycle.mjs +129 -99
  41. package/dist/metadata/metadata-keys.d.mts +9 -26
  42. package/dist/metadata/metadata-keys.mjs +7 -28
  43. package/dist/metadata/metadata-reader-token.d.mts +7 -0
  44. package/dist/metadata/metadata-reader-token.mjs +5 -0
  45. package/dist/metadata/metadata-types.d.mts +22 -71
  46. package/dist/metadata/symbol-metadata-reader.d.mts +10 -26
  47. package/dist/metadata/symbol-metadata-reader.mjs +32 -45
  48. package/dist/module.d.mts +30 -84
  49. package/dist/module.mjs +26 -72
  50. package/dist/registry.d.mts +32 -63
  51. package/dist/registry.mjs +131 -82
  52. package/dist/resolve-options.d.mts +18 -0
  53. package/dist/resolve-options.mjs +22 -0
  54. package/dist/resolver.d.mts +67 -190
  55. package/dist/resolver.mjs +715 -424
  56. package/dist/scope.d.mts +19 -102
  57. package/dist/scope.mjs +37 -192
  58. package/dist/token.d.mts +8 -22
  59. package/dist/token.mjs +9 -11
  60. package/dist/types.d.mts +48 -0
  61. package/dist/types.mjs +1 -0
  62. package/package.json +52 -14
  63. package/dist/metadata/param-registry.d.mts +0 -16
  64. package/dist/metadata/param-registry.mjs +0 -31
  65. package/dist/scope-validation.d.mts +0 -21
  66. package/dist/scope-validation.mjs +0 -35
@@ -1,197 +1,74 @@
1
+ import { Constructor } from "./constructor-type.mjs";
1
2
  import { Token } from "./token.mjs";
2
- import { RegistryKey } from "./registry.mjs";
3
- import { Binding, Constructor, ResolveHint } from "./binding.mjs";
4
- import { MetadataReader } from "./metadata/metadata-types.mjs";
3
+ import { MaterializationFrame, ResolveOptions } from "./types.mjs";
4
+ import { BindingRegistry } from "./registry.mjs";
5
5
  import { ScopeManager } from "./scope.mjs";
6
+ import { MetadataReader } from "./metadata/metadata-types.mjs";
7
+ import { Container } from "./container.mjs";
8
+ import { LifecycleManager } from "./lifecycle.mjs";
6
9
 
7
10
  //#region src/resolver.d.ts
8
- /**
9
- * Dependencies injected into {@link DependencyResolver} at construction time.
10
- */
11
- type ResolverDependencies = {
12
- /**
13
- * Looks up all bindings registered for a given registry key (own + parent containers).
14
- */
15
- readonly lookup: (key: RegistryKey) => readonly Binding<unknown>[] | undefined;
16
- /**
17
- * Manages singleton/scoped instance caches and deactivation.
18
- */
19
- readonly scopeManager: ScopeManager;
20
- /**
21
- * Reads `@injectable()` and lifecycle metadata from constructors. Omit to disable decorator support.
22
- */
23
- readonly metadataReader?: MetadataReader;
24
- };
25
- /**
26
- * Stateless graph walker: selects a binding, checks for cycles and scope violations,
27
- * delegates instance caching to `ScopeManager`, and runs lifecycle hooks.
28
- *
29
- * Resolution algorithm (per token):
30
- * 1. Lookup all bindings for the registry key.
31
- * 2. Apply name/tag hint and `when()` constraint filtering.
32
- * 3. Circular-dependency check via a mutable `visiting` set (per call tree).
33
- * 4. Captive-dependency check via the `materializationStack`.
34
- * 5. Scope-cache hit → return cached instance.
35
- * 6. Scope-cache miss → `materialize` → `@postConstruct` → `onActivation` → cache.
36
- *
37
- * Used exclusively by `DefaultContainer`; not part of the public API.
38
- */
39
11
  declare class DependencyResolver {
40
- private readonly deps;
41
- constructor(deps: ResolverDependencies);
42
- /**
43
- * Entry point for synchronous single-binding resolution.
44
- *
45
- * @throws {@link TokenNotBoundError} — no binding registered for `key`, or a nested dependency is unbound.
46
- * @throws {@link NoMatchingBindingError} — a name/tag `hint` was given for `key` but no binding matched it.
47
- * @throws {@link InternalError} — multiple bindings matched for `key` after applying the hint (ambiguous).
48
- * @throws {@link CircularDependencyError} — `key` or a nested token appears twice on the resolution stack.
49
- * @throws {@link AsyncResolutionError} — an `async-dynamic` binding or async lifecycle/activation on the sync path.
50
- * @throws {@link ScopeViolationError} — captive dependency (singleton → scoped/transient).
51
- * @throws {@link MissingMetadataError} — `class` binding lacks injectable metadata when the reader requires it.
52
- */
53
- resolveRoot<Value>(key: Token<Value> | Constructor<Value>, hint?: ResolveHint): Value;
54
- /**
55
- * Entry point for async single-binding resolution. Awaits `async-dynamic` factories
56
- * and async lifecycle hooks that would cause {@link AsyncResolutionError} on the sync path.
57
- *
58
- * @throws {@link TokenNotBoundError} — no binding registered for `key`, or a nested dependency is unbound.
59
- * @throws {@link NoMatchingBindingError} — a name/tag `hint` was given for `key` but no binding matched it.
60
- * @throws {@link InternalError} — multiple bindings matched for `key` after applying the hint (ambiguous).
61
- * @throws {@link CircularDependencyError} — `key` or a nested token appears twice on the resolution stack.
62
- * @throws {@link ScopeViolationError} — captive dependency (singleton → scoped/transient).
63
- * @throws {@link MissingMetadataError} — `class` binding lacks injectable metadata when the reader requires it.
64
- */
65
- resolveAsyncRoot<Value>(key: Token<Value> | Constructor<Value>, hint?: ResolveHint): Promise<Value>;
66
- /**
67
- * Optional resolution for the **requested key only**: returns `undefined` when that key has no
68
- * bindings or every candidate is filtered out **without** a name/tag hint — without throwing
69
- * {@link TokenNotBoundError} for those cases. Instantiating the selected binding still runs the
70
- * normal sync resolution path for nested dependencies; an unregistered transitive dependency
71
- * throws {@link TokenNotBoundError} as usual.
72
- *
73
- * Behavioral rules:
74
- * 1. If the registry key is completely unbound → returns `undefined`.
75
- * 2. If no candidate survives constraint filtering (without a hint) → returns `undefined`.
76
- * 3. If a name/tag hint was provided but no binding matches it → throws {@link NoMatchingBindingError}.
77
- * 4. Still throws on: circular dependencies, async operations on the sync path,
78
- * scope violations, ambiguous multi-binding matches, and {@link TokenNotBoundError} when a
79
- * required nested dependency is unbound.
80
- */
81
- resolveOptionalRoot<Value>(key: Token<Value> | Constructor<Value>, hint?: ResolveHint): Value | undefined;
82
- /**
83
- * Resolves every matching binding for `key` synchronously into an array.
84
- * Returns an empty array when no bindings exist (does not throw).
85
- *
86
- * @throws {@link AsyncResolutionError} — any candidate is `async-dynamic`.
87
- * @throws {@link NoMatchingBindingError} — hint was specified but no binding matched it.
88
- * @throws {@link TokenNotBoundError} — nested dependency unbound (same as {@link resolveRoot}).
89
- * @throws {@link CircularDependencyError} — cycle while materializing a candidate.
90
- * @throws {@link ScopeViolationError} — captive dependency during materialization.
91
- * @throws {@link MissingMetadataError} — class binding lacks injectable metadata when required.
92
- */
93
- resolveAllRoot<Value>(key: Token<Value> | Constructor<Value>, hint?: ResolveHint): Value[];
94
- /**
95
- * Async counterpart of {@link resolveAllRoot}: resolves every matching binding for `key`,
96
- * awaiting `async-dynamic` factories. Returns an empty array when no bindings exist.
97
- *
98
- * @throws {@link NoMatchingBindingError} — hint was specified but no binding matched it.
99
- * @throws {@link TokenNotBoundError} — nested dependency unbound (same as {@link resolveAsyncRoot}).
100
- * @throws {@link CircularDependencyError} — cycle while materializing a candidate.
101
- * @throws {@link ScopeViolationError} — captive dependency during materialization.
102
- * @throws {@link MissingMetadataError} — class binding lacks injectable metadata when required.
103
- */
104
- resolveAllAsyncRoot<Value>(key: Token<Value> | Constructor<Value>, hint?: ResolveHint): Promise<Value[]>;
105
- /**
106
- * Context-aware sync `resolveAll`: extends the current resolution path and preserves
107
- * visiting/materialization stacks so nested multi-resolution participates in cycle and
108
- * captive-dependency checks exactly like single-value {@link resolve}.
109
- */
110
- private resolveAll;
111
- /**
112
- * Async counterpart of {@link resolveAll}: keeps the current path and materialization stack
113
- * so `when()` constraints and scope validation behave consistently for nested multi-resolution.
114
- */
115
- private resolveAllAsync;
116
- /**
117
- * Assembles the read-only {@link ConstraintContext} snapshot passed to `when()` predicates.
118
- * Extracts the top-of-stack frame as `parent` and the rest as `ancestors`.
119
- */
120
- private buildConstraintContext;
121
- /**
122
- * Throws {@link ScopeViolationError} when the immediate consumer on the stack is a singleton
123
- * and the dependency has `"scoped"` or `"transient"` lifetime (captive dependency).
124
- * Constants are exempt — they have no scope cache.
125
- */
126
- private assertDependencyScopeAllowed;
127
- /**
128
- * Builds a {@link ResolutionContext} for use inside factories and lifecycle hooks.
129
- * The `resolve` / `resolveAsync` / `resolveOptional` closures carry the current path and
130
- * materialization stack forward so nested calls inherit captive-dependency checks.
131
- */
132
- private createContext;
133
- /**
134
- * Core synchronous resolution: lookup → filter → cycle check → scope check → instantiate.
135
- * Called recursively when a binding's dependencies need resolution.
136
- *
137
- * @param key - Token or constructor being resolved.
138
- * @param hint - Optional name/tag filter for multi-binding selection.
139
- * @param pathLabels - Mutable label path accumulated during graph walk; extended in place.
140
- * @param visiting - Registry keys currently on the call stack; used for circular-dependency detection.
141
- * @param materializationStack - Bindings along the current construction chain; used to block singleton→scoped/transient.
142
- */
143
- private resolve;
144
- /**
145
- * Core async resolution: same pipeline as {@link resolve} but awaits `async-dynamic`
146
- * factories and async lifecycle hooks instead of throwing {@link AsyncResolutionError}.
147
- *
148
- * @param key - Token or constructor being resolved.
149
- * @param hint - Optional name/tag filter for multi-binding selection.
150
- * @param pathLabels - Mutable label path accumulated during graph walk; extended in place.
151
- * @param visiting - Registry keys currently on the call stack; used for circular-dependency detection.
152
- * @param materializationStack - Same captive-dependency chain as {@link resolve}.
153
- */
154
- private resolveAsync;
155
- /**
156
- * Handles scope-cache lookup / storage and lifecycle hooks (`@postConstruct`, `onActivation`)
157
- * around a synchronous call to {@link materialize}.
158
- */
159
- private instantiateBinding;
160
- /**
161
- * Async counterpart of {@link instantiateBinding}: delegates to {@link materializeAsync}
162
- * and awaits `@postConstruct` and `onActivation` hooks.
163
- */
164
- private instantiateBindingAsync;
165
- /**
166
- * Duplicate captive-dependency guard applied at instantiation time (after scope-cache miss),
167
- * checking the top of the materialization stack rather than the raw `ConstraintContext`.
168
- */
169
- private assertCaptiveDependencyFromMaterializationStack;
170
- /**
171
- * Synchronously produces the raw instance for a binding **without** touching the scope cache
172
- * or running lifecycle hooks. The caller ({@link instantiateBinding}) wraps this with
173
- * cache logic and post-construction hooks.
174
- *
175
- * Dispatches on `binding.kind`; throws {@link AsyncResolutionError} if the binding is
176
- * `async-dynamic` or if a `dynamic` / `resolved` factory returns a Promise.
177
- */
178
- private materialize;
179
- /**
180
- * Async counterpart of {@link materialize}; awaits `async-dynamic` factories and
181
- * recursively resolves `resolved`-binding dependencies with {@link resolveAsync}.
182
- */
183
- private materializeAsync;
184
- /**
185
- * Reads `@injectable()` constructor metadata and synchronously resolves each parameter,
186
- * then calls `new ImplementationClass(...deps)`. Throws {@link MissingMetadataError} when
187
- * the class has constructor parameters but no metadata.
188
- */
189
- private instantiateClassBinding;
190
- /**
191
- * Async counterpart of {@link instantiateClassBinding}: resolves constructor dependencies
192
- * with {@link resolveAsync} so `async-dynamic` parameters are awaited in order.
193
- */
194
- private instantiateClassBindingAsync;
12
+ private readonly _registry;
13
+ private readonly _scope;
14
+ private readonly _lifecycle;
15
+ private readonly _metadataReader;
16
+ private readonly _container;
17
+ private readonly _parent;
18
+ private readonly _frameByBindingId;
19
+ private readonly _syncResolutionContextPool;
20
+ private readonly _deepCycleMarks;
21
+ private _deepCycleGen;
22
+ private _deepActiveLevels;
23
+ private _deepSyncCtx;
24
+ private _deepSyncCtxPath;
25
+ private _deepAsyncCtx;
26
+ private _deepAsyncCtxPath;
27
+ private _deepAsyncActiveLevels;
28
+ private readonly _classHasPostConstruct;
29
+ private readonly _classNeedsActiveContainer;
30
+ private readonly _classConstructorMetadata;
31
+ private readonly _activationNeedByBindingId;
32
+ private _activationCacheVersion;
33
+ constructor(_registry: BindingRegistry, _scope: ScopeManager, _lifecycle: LifecycleManager, _metadataReader: MetadataReader, _container: Container, _parent: DependencyResolver | undefined);
34
+ private _findBinding;
35
+ resolveFromContext<const Value>(token: Token<Value> | Constructor<Value>, resolutionPath: string[], materializationStack: MaterializationFrame[]): Value;
36
+ resolve<const Value>(token: Token<Value> | Constructor<Value>, hint: ResolveOptions | undefined, resolutionPath: string[], materializationStack: MaterializationFrame[]): Value;
37
+ private _resolveBinding;
38
+ private _instantiateSync;
39
+ private _resolveClassDeps;
40
+ private _resolveDescriptorDeps;
41
+ resolveOptional<const Value>(token: Token<Value> | Constructor<Value>, hint: ResolveOptions | undefined, resolutionPath: string[], materializationStack: MaterializationFrame[]): Value | undefined;
42
+ resolveAll<const Value>(token: Token<Value> | Constructor<Value>, hint: ResolveOptions | undefined, resolutionPath: string[], materializationStack: MaterializationFrame[]): Value[];
43
+ resolveAsyncFromContext<const Value>(token: Token<Value> | Constructor<Value>, resolutionPath: string[], materializationStack: MaterializationFrame[]): Promise<Value>;
44
+ resolveAsync<const Value>(token: Token<Value> | Constructor<Value>, hint: ResolveOptions | undefined, resolutionPath: string[], materializationStack: MaterializationFrame[]): Promise<Value>;
45
+ private _resolveBindingAsync;
46
+ private _instantiateAsync;
47
+ private _resolveClassDepsAsync;
48
+ private _resolveDescriptorDepsAsync;
49
+ resolveOptionalAsync<const Value>(token: Token<Value> | Constructor<Value>, hint: ResolveOptions | undefined, resolutionPath: string[], materializationStack: MaterializationFrame[]): Promise<Value | undefined>;
50
+ resolveAllAsync<const Value>(token: Token<Value> | Constructor<Value>, hint: ResolveOptions | undefined, resolutionPath: string[], materializationStack: MaterializationFrame[]): Promise<Value[]>;
51
+ private _getAllBindingsFromChain;
52
+ private _getSimpleNamedBindingsFromChain;
53
+ private _getAvailableSlots;
54
+ private _makeConstraintContext;
55
+ private _matchesBindingFast;
56
+ private _matchesSlotFast;
57
+ private _getTokenName;
58
+ private _getConstructorMetadata;
59
+ private _instantiateClass;
60
+ private _matchesHintTag;
61
+ private _resolveTransientDynamicSyncFromContext;
62
+ private _resolveTransientDynamicSyncSlow;
63
+ private _resolveTransientDynamicAsyncFromContext;
64
+ private _resolveTransientDynamicAsyncSlow;
65
+ private _resolveCandidateSync;
66
+ private _resolveCandidateAsync;
67
+ private _getMaterializationFrame;
68
+ private _needsActivation;
69
+ private _refreshClassPostConstructCache;
70
+ private _requiresResolutionContext;
71
+ private _acquireSyncResolutionContext;
195
72
  }
196
73
  //#endregion
197
- export { DependencyResolver, ResolverDependencies };
74
+ export { DependencyResolver };