@codefast/di 0.5.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +712 -0
- package/README.md +119 -30
- package/dist/ambient/active-container.d.ts +37 -0
- package/dist/ambient/active-container.d.ts.map +1 -0
- package/dist/ambient/active-container.js +41 -0
- package/dist/ambient/active-container.js.map +1 -0
- package/dist/container/binding-builders.d.ts +12 -8
- package/dist/container/binding-builders.d.ts.map +1 -1
- package/dist/container/binding-builders.js +62 -31
- package/dist/container/binding-builders.js.map +1 -1
- package/dist/container/container.d.ts +33 -19
- package/dist/container/container.d.ts.map +1 -1
- package/dist/container/container.js +281 -118
- package/dist/container/container.js.map +1 -1
- package/dist/{resolution → core}/binding-scope.d.ts +2 -2
- package/dist/core/binding-scope.d.ts.map +1 -0
- package/dist/core/binding-scope.js.map +1 -0
- package/dist/{binding.d.ts → core/binding.d.ts} +70 -15
- package/dist/core/binding.d.ts.map +1 -0
- package/dist/{binding.js → core/binding.js} +27 -12
- package/dist/core/binding.js.map +1 -0
- package/dist/core/constraint-requirement.d.ts +53 -0
- package/dist/core/constraint-requirement.d.ts.map +1 -0
- package/dist/core/constraint-requirement.js +62 -0
- package/dist/core/constraint-requirement.js.map +1 -0
- package/dist/{constructor-type.d.ts → core/constructor-type.d.ts} +1 -1
- package/dist/core/constructor-type.d.ts.map +1 -0
- package/dist/core/constructor-type.js.map +1 -0
- package/dist/{module.d.ts → core/module.d.ts} +5 -5
- package/dist/core/module.d.ts.map +1 -0
- package/dist/{module.js → core/module.js} +4 -4
- package/dist/core/module.js.map +1 -0
- package/dist/{registry.d.ts → core/registry.d.ts} +27 -7
- package/dist/core/registry.d.ts.map +1 -0
- package/dist/{registry.js → core/registry.js} +125 -52
- package/dist/core/registry.js.map +1 -0
- package/dist/core/tag.d.ts +80 -0
- package/dist/core/tag.d.ts.map +1 -0
- package/dist/core/tag.js +96 -0
- package/dist/core/tag.js.map +1 -0
- package/dist/{token.d.ts → core/token.d.ts} +2 -6
- package/dist/core/token.d.ts.map +1 -0
- package/dist/core/token.js +13 -0
- package/dist/core/token.js.map +1 -0
- package/dist/{types.d.ts → core/types.d.ts} +27 -21
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js.map +1 -0
- package/dist/decorators/inject.d.ts +5 -49
- package/dist/decorators/inject.d.ts.map +1 -1
- package/dist/decorators/inject.js +34 -104
- package/dist/decorators/inject.js.map +1 -1
- package/dist/decorators/injectable.d.ts +25 -5
- package/dist/decorators/injectable.d.ts.map +1 -1
- package/dist/decorators/injectable.js +2 -3
- package/dist/decorators/injectable.js.map +1 -1
- package/dist/decorators/lifecycle-decorators.d.ts +4 -2
- package/dist/decorators/lifecycle-decorators.d.ts.map +1 -1
- package/dist/decorators/lifecycle-decorators.js +19 -25
- package/dist/decorators/lifecycle-decorators.js.map +1 -1
- package/dist/{resolution → errors}/diagnostics.d.ts +2 -0
- package/dist/errors/diagnostics.d.ts.map +1 -0
- package/dist/errors/diagnostics.js.map +1 -0
- package/dist/{errors.d.ts → errors/errors.d.ts} +89 -4
- package/dist/errors/errors.d.ts.map +1 -0
- package/dist/{errors.js → errors/errors.js} +129 -7
- package/dist/errors/errors.js.map +1 -0
- package/dist/index.d.ts +21 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -7
- package/dist/index.js.map +1 -1
- package/dist/injection/descriptor.d.ts +79 -0
- package/dist/injection/descriptor.d.ts.map +1 -0
- package/dist/injection/descriptor.js +118 -0
- package/dist/injection/descriptor.js.map +1 -0
- package/dist/{resolution → injection}/resolve-options.d.ts +21 -11
- package/dist/injection/resolve-options.d.ts.map +1 -0
- package/dist/injection/resolve-options.js +101 -0
- package/dist/injection/resolve-options.js.map +1 -0
- package/dist/introspection/dependency-graph.d.ts +16 -5
- package/dist/introspection/dependency-graph.d.ts.map +1 -1
- package/dist/introspection/dependency-graph.js +138 -57
- package/dist/introspection/dependency-graph.js.map +1 -1
- package/dist/introspection/graph-adapters/cytoscape.d.ts +6 -3
- package/dist/introspection/graph-adapters/cytoscape.d.ts.map +1 -1
- package/dist/introspection/graph-adapters/cytoscape.js +3 -0
- package/dist/introspection/graph-adapters/cytoscape.js.map +1 -1
- package/dist/introspection/graph-adapters/dot.d.ts.map +1 -1
- package/dist/introspection/graph-adapters/dot.js +12 -5
- package/dist/introspection/graph-adapters/dot.js.map +1 -1
- package/dist/introspection/graph-adapters/mermaid.d.ts +9 -0
- package/dist/introspection/graph-adapters/mermaid.d.ts.map +1 -0
- package/dist/introspection/graph-adapters/mermaid.js +47 -0
- package/dist/introspection/graph-adapters/mermaid.js.map +1 -0
- package/dist/introspection/graph-adapters/reactflow.d.ts +6 -3
- package/dist/introspection/graph-adapters/reactflow.d.ts.map +1 -1
- package/dist/introspection/graph-adapters/reactflow.js +5 -1
- package/dist/introspection/graph-adapters/reactflow.js.map +1 -1
- package/dist/introspection/inspector.d.ts +4 -4
- package/dist/introspection/inspector.d.ts.map +1 -1
- package/dist/introspection/inspector.js +10 -6
- package/dist/introspection/inspector.js.map +1 -1
- package/dist/lifecycle/lifecycle-manager.d.ts +27 -0
- package/dist/lifecycle/lifecycle-manager.d.ts.map +1 -0
- package/dist/{resolution/lifecycle.js → lifecycle/lifecycle-manager.js} +39 -9
- package/dist/lifecycle/lifecycle-manager.js.map +1 -0
- package/dist/lifecycle/scope-manager.d.ts +48 -0
- package/dist/lifecycle/scope-manager.d.ts.map +1 -0
- package/dist/lifecycle/scope-manager.js +135 -0
- package/dist/lifecycle/scope-manager.js.map +1 -0
- package/dist/metadata/metadata-reader-token.d.ts +1 -1
- package/dist/metadata/metadata-reader-token.d.ts.map +1 -1
- package/dist/metadata/metadata-reader-token.js +1 -1
- package/dist/metadata/metadata-reader-token.js.map +1 -1
- package/dist/metadata/metadata-types.d.ts +9 -9
- package/dist/metadata/metadata-types.d.ts.map +1 -1
- package/dist/metadata/symbol-metadata-reader.d.ts +19 -6
- package/dist/metadata/symbol-metadata-reader.d.ts.map +1 -1
- package/dist/metadata/symbol-metadata-reader.js +89 -9
- package/dist/metadata/symbol-metadata-reader.js.map +1 -1
- package/dist/metadata/verifying-metadata-reader.d.ts +16 -0
- package/dist/metadata/verifying-metadata-reader.d.ts.map +1 -0
- package/dist/metadata/verifying-metadata-reader.js +32 -0
- package/dist/metadata/verifying-metadata-reader.js.map +1 -0
- package/dist/resolution/{activation-need.d.ts → cache/activation-need.d.ts} +5 -5
- package/dist/resolution/cache/activation-need.d.ts.map +1 -0
- package/dist/resolution/cache/activation-need.js.map +1 -0
- package/dist/resolution/{binding-lookup-cache.d.ts → cache/binding-lookup-cache.d.ts} +9 -5
- package/dist/resolution/cache/binding-lookup-cache.d.ts.map +1 -0
- package/dist/resolution/{binding-lookup-cache.js → cache/binding-lookup-cache.js} +55 -0
- package/dist/resolution/cache/binding-lookup-cache.js.map +1 -0
- package/dist/resolution/cache/class-introspector.d.ts +66 -0
- package/dist/resolution/cache/class-introspector.d.ts.map +1 -0
- package/dist/resolution/cache/class-introspector.js +194 -0
- package/dist/resolution/cache/class-introspector.js.map +1 -0
- package/dist/resolution/context.d.ts +84 -0
- package/dist/resolution/context.d.ts.map +1 -0
- package/dist/resolution/{environment.js → context.js} +54 -80
- package/dist/resolution/context.js.map +1 -0
- package/dist/resolution/path/resolution-path.d.ts +94 -0
- package/dist/resolution/path/resolution-path.d.ts.map +1 -0
- package/dist/resolution/path/resolution-path.js +117 -0
- package/dist/resolution/path/resolution-path.js.map +1 -0
- package/dist/resolution/{instantiation-plan.d.ts → plan/instantiation-plan.d.ts} +37 -7
- package/dist/resolution/plan/instantiation-plan.d.ts.map +1 -0
- package/dist/resolution/plan/instantiation-plan.js +419 -0
- package/dist/resolution/plan/instantiation-plan.js.map +1 -0
- package/dist/resolution/resolver.d.ts +28 -28
- package/dist/resolution/resolver.d.ts.map +1 -1
- package/dist/resolution/resolver.js +581 -239
- package/dist/resolution/resolver.js.map +1 -1
- package/dist/resolution/select/binding-select.d.ts +34 -0
- package/dist/resolution/select/binding-select.d.ts.map +1 -0
- package/dist/resolution/select/binding-select.js +175 -0
- package/dist/resolution/select/binding-select.js.map +1 -0
- package/dist/resolution/{constraints.d.ts → select/constraints.d.ts} +13 -12
- package/dist/resolution/select/constraints.d.ts.map +1 -0
- package/dist/resolution/{constraints.js → select/constraints.js} +33 -22
- package/dist/resolution/select/constraints.js.map +1 -0
- package/package.json +160 -20
- package/src/ambient/active-container.ts +65 -0
- package/src/container/binding-builders.ts +79 -44
- package/src/container/container.ts +357 -170
- package/src/{resolution → core}/binding-scope.ts +2 -2
- package/src/{binding.ts → core/binding.ts} +101 -39
- package/src/core/constraint-requirement.ts +94 -0
- package/src/{constructor-type.ts → core/constructor-type.ts} +1 -1
- package/src/{module.ts → core/module.ts} +10 -10
- package/src/core/registry.ts +365 -0
- package/src/core/tag.ts +158 -0
- package/src/{token.ts → core/token.ts} +2 -14
- package/src/{types.ts → core/types.ts} +37 -34
- package/src/decorators/inject.ts +39 -175
- package/src/decorators/injectable.ts +40 -10
- package/src/decorators/lifecycle-decorators.ts +22 -34
- package/src/{resolution → errors}/diagnostics.ts +2 -0
- package/src/{errors.ts → errors/errors.ts} +152 -8
- package/src/index.ts +43 -15
- package/src/injection/descriptor.ts +206 -0
- package/src/injection/resolve-options.ts +144 -0
- package/src/introspection/dependency-graph.ts +223 -63
- package/src/introspection/graph-adapters/cytoscape.ts +9 -3
- package/src/introspection/graph-adapters/dot.ts +13 -5
- package/src/introspection/graph-adapters/mermaid.ts +61 -0
- package/src/introspection/graph-adapters/reactflow.ts +11 -4
- package/src/introspection/inspector.ts +16 -12
- package/src/{resolution/lifecycle.ts → lifecycle/lifecycle-manager.ts} +68 -32
- package/src/lifecycle/scope-manager.ts +156 -0
- package/src/metadata/metadata-reader-token.ts +2 -2
- package/src/metadata/metadata-types.ts +9 -9
- package/src/metadata/symbol-metadata-reader.ts +95 -18
- package/src/metadata/verifying-metadata-reader.ts +41 -0
- package/src/resolution/{activation-need.ts → cache/activation-need.ts} +8 -8
- package/src/resolution/{binding-lookup-cache.ts → cache/binding-lookup-cache.ts} +63 -5
- package/src/resolution/cache/class-introspector.ts +251 -0
- package/src/resolution/{environment.ts → context.ts} +84 -145
- package/src/resolution/path/resolution-path.ts +167 -0
- package/src/resolution/plan/instantiation-plan.ts +615 -0
- package/src/resolution/resolver.ts +663 -313
- package/src/resolution/select/binding-select.ts +210 -0
- package/src/resolution/select/constraints.ts +130 -0
- package/dist/binding.d.ts.map +0 -1
- package/dist/binding.js.map +0 -1
- package/dist/constructor-type.d.ts.map +0 -1
- package/dist/constructor-type.js.map +0 -1
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js.map +0 -1
- package/dist/module.d.ts.map +0 -1
- package/dist/module.js.map +0 -1
- package/dist/registry.d.ts.map +0 -1
- package/dist/registry.js.map +0 -1
- package/dist/resolution/activation-need.d.ts.map +0 -1
- package/dist/resolution/activation-need.js.map +0 -1
- package/dist/resolution/binding-lookup-cache.d.ts.map +0 -1
- package/dist/resolution/binding-lookup-cache.js.map +0 -1
- package/dist/resolution/binding-scope.d.ts.map +0 -1
- package/dist/resolution/binding-scope.js.map +0 -1
- package/dist/resolution/binding-select.d.ts +0 -23
- package/dist/resolution/binding-select.d.ts.map +0 -1
- package/dist/resolution/binding-select.js +0 -121
- package/dist/resolution/binding-select.js.map +0 -1
- package/dist/resolution/class-introspector.d.ts +0 -27
- package/dist/resolution/class-introspector.d.ts.map +0 -1
- package/dist/resolution/class-introspector.js +0 -60
- package/dist/resolution/class-introspector.js.map +0 -1
- package/dist/resolution/constraints.d.ts.map +0 -1
- package/dist/resolution/constraints.js.map +0 -1
- package/dist/resolution/diagnostics.d.ts.map +0 -1
- package/dist/resolution/diagnostics.js.map +0 -1
- package/dist/resolution/environment.d.ts +0 -94
- package/dist/resolution/environment.d.ts.map +0 -1
- package/dist/resolution/environment.js.map +0 -1
- package/dist/resolution/instantiation-plan.d.ts.map +0 -1
- package/dist/resolution/instantiation-plan.js +0 -183
- package/dist/resolution/instantiation-plan.js.map +0 -1
- package/dist/resolution/lifecycle.d.ts +0 -23
- package/dist/resolution/lifecycle.d.ts.map +0 -1
- package/dist/resolution/lifecycle.js.map +0 -1
- package/dist/resolution/resolution-path.d.ts +0 -98
- package/dist/resolution/resolution-path.d.ts.map +0 -1
- package/dist/resolution/resolution-path.js +0 -98
- package/dist/resolution/resolution-path.js.map +0 -1
- package/dist/resolution/resolve-options.d.ts.map +0 -1
- package/dist/resolution/resolve-options.js +0 -56
- package/dist/resolution/resolve-options.js.map +0 -1
- package/dist/resolution/scope.d.ts +0 -28
- package/dist/resolution/scope.d.ts.map +0 -1
- package/dist/resolution/scope.js +0 -88
- package/dist/resolution/scope.js.map +0 -1
- package/dist/token.d.ts.map +0 -1
- package/dist/token.js +0 -22
- package/dist/token.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- package/src/registry.ts +0 -282
- package/src/resolution/binding-select.ts +0 -154
- package/src/resolution/class-introspector.ts +0 -74
- package/src/resolution/constraints.ts +0 -121
- package/src/resolution/instantiation-plan.ts +0 -292
- package/src/resolution/resolution-path.ts +0 -156
- package/src/resolution/resolve-options.ts +0 -89
- package/src/resolution/scope.ts +0 -105
- /package/dist/{resolution → core}/binding-scope.js +0 -0
- /package/dist/{constructor-type.js → core/constructor-type.js} +0 -0
- /package/dist/{types.js → core/types.js} +0 -0
- /package/dist/{resolution → errors}/diagnostics.js +0 -0
- /package/dist/resolution/{activation-need.js → cache/activation-need.js} +0 -0
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Options-less token → terminal binding, memoized per container chain.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
4
|
+
* @remarks Each cache links to its parent's, so a child answers from its own chain without walking
|
|
5
|
+
* the container hierarchy again.
|
|
5
6
|
*/
|
|
6
|
-
import type { Binding } from "#/binding";
|
|
7
|
-
import type { BindingRegistry } from "#/registry";
|
|
8
|
-
import type {
|
|
9
|
-
import type {
|
|
7
|
+
import type { Binding } from "#/core/binding";
|
|
8
|
+
import type { BindingRegistry } from "#/core/registry";
|
|
9
|
+
import type { BindingTag } from "#/core/tag";
|
|
10
|
+
import type { Token } from "#/core/token";
|
|
11
|
+
import type { Constructor } from "#/core/types";
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
14
|
* A token's terminal binding with alias hops already folded, plus the container that owns it.
|
|
@@ -33,6 +35,8 @@ export const ALIAS_HOP_LIMIT = 32;
|
|
|
33
35
|
*/
|
|
34
36
|
const newNameToEntryMap = <Owner>(): Map<string, DefaultLookupEntry<Owner> | null> => new Map();
|
|
35
37
|
|
|
38
|
+
const newTagToEntryMap = <Owner>(): Map<BindingTag, DefaultLookupEntry<Owner> | null> => new Map();
|
|
39
|
+
|
|
36
40
|
/**
|
|
37
41
|
* @since 0.5.0-canary.9
|
|
38
42
|
*/
|
|
@@ -46,6 +50,14 @@ export class BindingLookupCache<Owner> {
|
|
|
46
50
|
#lastEntry: DefaultLookupEntry<Owner> | null = null;
|
|
47
51
|
readonly #byTokenAndName = new Map<Token<unknown> | Constructor, Map<string, DefaultLookupEntry<Owner> | null>>();
|
|
48
52
|
#namedVersion = -1;
|
|
53
|
+
readonly #byTokenAndTag = new Map<Token<unknown> | Constructor, Map<BindingTag, DefaultLookupEntry<Owner> | null>>();
|
|
54
|
+
#taggedVersion = -1;
|
|
55
|
+
// One entry in front of the tag map, and the map is not written until a second distinct request
|
|
56
|
+
// shape appears: a per-request child usually asks one (token, tag) once, and the inner-map
|
|
57
|
+
// allocation was that shape's whole regression when this memo landed.
|
|
58
|
+
#lastTagToken: Token<unknown> | Constructor | undefined;
|
|
59
|
+
#lastTag: BindingTag | undefined;
|
|
60
|
+
#lastTaggedEntry: DefaultLookupEntry<Owner> | null = null;
|
|
49
61
|
|
|
50
62
|
readonly #registry: BindingRegistry;
|
|
51
63
|
readonly #owner: Owner;
|
|
@@ -104,6 +116,37 @@ export class BindingLookupCache<Owner> {
|
|
|
104
116
|
return entry;
|
|
105
117
|
}
|
|
106
118
|
|
|
119
|
+
/** `null` when the tag's shape needs the full selection path. */
|
|
120
|
+
taggedEntry(token: Token<unknown> | Constructor, tag: BindingTag): DefaultLookupEntry<Owner> | null {
|
|
121
|
+
const version = this.chainVersion();
|
|
122
|
+
if (version !== this.#taggedVersion) {
|
|
123
|
+
this.#byTokenAndTag.clear();
|
|
124
|
+
this.#taggedVersion = version;
|
|
125
|
+
this.#lastTagToken = undefined;
|
|
126
|
+
this.#lastTag = undefined;
|
|
127
|
+
} else if (token === this.#lastTagToken && tag === this.#lastTag) {
|
|
128
|
+
return this.#lastTaggedEntry;
|
|
129
|
+
}
|
|
130
|
+
let entry: DefaultLookupEntry<Owner> | null | undefined;
|
|
131
|
+
if (this.#lastTagToken === undefined) {
|
|
132
|
+
// First shape this cache generation sees: answer from the walk and defer the map entirely.
|
|
133
|
+
entry = this.#findTaggedInChain(token, tag);
|
|
134
|
+
} else {
|
|
135
|
+
// Keyed by the criterion object itself: criteria are interned, so identity is the slot
|
|
136
|
+
// contract's own `Object.is` — the same exactness the registry's tagged index relies on.
|
|
137
|
+
const byTag = this.#byTokenAndTag.getOrInsertComputed(token, newTagToEntryMap);
|
|
138
|
+
entry = byTag.get(tag);
|
|
139
|
+
if (entry === undefined) {
|
|
140
|
+
entry = this.#findTaggedInChain(token, tag);
|
|
141
|
+
byTag.set(tag, entry);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
this.#lastTagToken = token;
|
|
145
|
+
this.#lastTag = tag;
|
|
146
|
+
this.#lastTaggedEntry = entry;
|
|
147
|
+
return entry;
|
|
148
|
+
}
|
|
149
|
+
|
|
107
150
|
#foldAliases(token: Token<unknown> | Constructor): DefaultLookupEntry<Owner> | null {
|
|
108
151
|
let current = token;
|
|
109
152
|
for (let hop = 0; hop < ALIAS_HOP_LIMIT; hop += 1) {
|
|
@@ -145,4 +188,19 @@ export class BindingLookupCache<Owner> {
|
|
|
145
188
|
}
|
|
146
189
|
return this.#parent === undefined ? null : this.#parent.#findNamedInChain(token, name);
|
|
147
190
|
}
|
|
191
|
+
|
|
192
|
+
#findTaggedInChain(token: Token<unknown> | Constructor, tag: BindingTag): DefaultLookupEntry<Owner> | null {
|
|
193
|
+
const tagged = this.#registry.getSimpleTagged(token, tag);
|
|
194
|
+
if (tagged !== undefined) {
|
|
195
|
+
// Predicates need a live context; aliases carry options through the full path.
|
|
196
|
+
if (tagged.predicate !== undefined || tagged.kind === "alias") {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
return { binding: tagged, owner: this.#owner };
|
|
200
|
+
}
|
|
201
|
+
if (this.#registry.has(token)) {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
return this.#parent === undefined ? null : this.#parent.#findTaggedInChain(token, tag);
|
|
205
|
+
}
|
|
148
206
|
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-class decorator metadata, cached by constructor.
|
|
3
|
+
*
|
|
4
|
+
* @remarks Metadata cannot change once a class is defined, so nothing here needs version stamping.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { runWithAmbientResolution } from "#/ambient/active-container";
|
|
8
|
+
import type { AmbientResolution } from "#/ambient/active-container";
|
|
9
|
+
import type { Container } from "#/container/container";
|
|
10
|
+
import type { ConstructorInvocation } from "#/core/constructor-type";
|
|
11
|
+
import type { Constructor } from "#/core/types";
|
|
12
|
+
import { InvalidMetadataError } from "#/errors/errors";
|
|
13
|
+
import type { InjectionDescriptor } from "#/injection/descriptor";
|
|
14
|
+
import type { ConstructorMetadata, LifecycleMetadata, MetadataReader } from "#/metadata/metadata-types";
|
|
15
|
+
|
|
16
|
+
// Verified pairs, not verified classes: two readers may disagree about the same class, and a reader
|
|
17
|
+
// that goes out of scope takes its record with it.
|
|
18
|
+
const verifiedTargets = new WeakMap<MetadataReader, WeakSet<Constructor>>();
|
|
19
|
+
const verifiedLifecycleTargets = new WeakMap<MetadataReader, WeakSet<Constructor>>();
|
|
20
|
+
const verifiedAccessorTargets = new WeakMap<MetadataReader, WeakSet<Constructor>>();
|
|
21
|
+
|
|
22
|
+
function isVerified(
|
|
23
|
+
cache: WeakMap<MetadataReader, WeakSet<Constructor>>,
|
|
24
|
+
reader: MetadataReader,
|
|
25
|
+
target: Constructor,
|
|
26
|
+
): boolean {
|
|
27
|
+
return cache.get(reader)?.has(target) ?? false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function markVerified(
|
|
31
|
+
cache: WeakMap<MetadataReader, WeakSet<Constructor>>,
|
|
32
|
+
reader: MetadataReader,
|
|
33
|
+
target: Constructor,
|
|
34
|
+
): void {
|
|
35
|
+
let verified = cache.get(reader);
|
|
36
|
+
if (verified === undefined) {
|
|
37
|
+
verified = new WeakSet();
|
|
38
|
+
cache.set(reader, verified);
|
|
39
|
+
}
|
|
40
|
+
verified.add(target);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A reader's constructor metadata for a class, verified the first time this process asks.
|
|
45
|
+
*
|
|
46
|
+
* @remarks Metadata cannot change once a class is defined, so re-checking per container would charge
|
|
47
|
+
* every fresh container for a fact already established — a per-request child or a cold boot pays
|
|
48
|
+
* that repeatedly.
|
|
49
|
+
*
|
|
50
|
+
* @since 0.6.0
|
|
51
|
+
*/
|
|
52
|
+
export function verifyConstructorMetadata(
|
|
53
|
+
reader: MetadataReader,
|
|
54
|
+
target: Constructor,
|
|
55
|
+
): ConstructorMetadata | undefined {
|
|
56
|
+
const metadata = reader.getConstructorMetadata(target);
|
|
57
|
+
if (metadata === undefined) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
if (isVerified(verifiedTargets, reader, target)) {
|
|
61
|
+
return metadata;
|
|
62
|
+
}
|
|
63
|
+
assertConstructorMetadata(metadata, target);
|
|
64
|
+
markVerified(verifiedTargets, reader, target);
|
|
65
|
+
|
|
66
|
+
return metadata;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Verifies what a reader claims about a class, since a `MetadataReader` is a public seam.
|
|
71
|
+
*
|
|
72
|
+
* @remarks Every path that reads constructor metadata comes through here, so a reader that answers
|
|
73
|
+
* wrongly is a named error at the class it described rather than a `TypeError` raised later inside a
|
|
74
|
+
* resolve. Only what a consumer dereferences is checked: `params` and each entry's `token`.
|
|
75
|
+
* `optional`/`multi` degrade to falsy without crashing, and `index` is decorative — dependencies are
|
|
76
|
+
* consumed positionally.
|
|
77
|
+
*
|
|
78
|
+
* @since 0.6.0
|
|
79
|
+
*/
|
|
80
|
+
export function assertConstructorMetadata(metadata: unknown, target: Constructor): ConstructorMetadata | undefined {
|
|
81
|
+
if (metadata === undefined) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
if (typeof metadata !== "object" || metadata === null) {
|
|
85
|
+
throw new InvalidMetadataError(
|
|
86
|
+
target.name,
|
|
87
|
+
`constructor metadata: expected an object, received ${typeof metadata}`,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
const params: unknown = Reflect.get(metadata, "params");
|
|
91
|
+
if (!Array.isArray(params)) {
|
|
92
|
+
throw new InvalidMetadataError(target.name, "constructor metadata: params is not an array");
|
|
93
|
+
}
|
|
94
|
+
for (const [position, param] of params.entries()) {
|
|
95
|
+
if (typeof param !== "object" || param === null) {
|
|
96
|
+
throw new InvalidMetadataError(target.name, `constructor metadata: params[${String(position)}] is not an object`);
|
|
97
|
+
}
|
|
98
|
+
const dependency: unknown = Reflect.get(param, "token");
|
|
99
|
+
if (typeof dependency !== "object" && typeof dependency !== "function") {
|
|
100
|
+
throw new InvalidMetadataError(
|
|
101
|
+
target.name,
|
|
102
|
+
`constructor metadata: params[${String(position)}].token is not a token or a class`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return metadata as ConstructorMetadata;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A reader's lifecycle metadata for a class, verified the first time this process asks.
|
|
112
|
+
*
|
|
113
|
+
* @since 0.6.0
|
|
114
|
+
*/
|
|
115
|
+
export function verifyLifecycleMetadata(reader: MetadataReader, target: Constructor): LifecycleMetadata | undefined {
|
|
116
|
+
const metadata = reader.getLifecycleMetadata(target);
|
|
117
|
+
if (metadata === undefined || isVerified(verifiedLifecycleTargets, reader, target)) {
|
|
118
|
+
return metadata;
|
|
119
|
+
}
|
|
120
|
+
if (typeof metadata !== "object" || metadata === null) {
|
|
121
|
+
throw new InvalidMetadataError(target.name, `lifecycle metadata: expected an object, received ${typeof metadata}`);
|
|
122
|
+
}
|
|
123
|
+
for (const phase of ["postConstruct", "preDestroy"] as const) {
|
|
124
|
+
const methods: unknown = Reflect.get(metadata, phase);
|
|
125
|
+
if (methods === undefined) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (!Array.isArray(methods)) {
|
|
129
|
+
throw new InvalidMetadataError(target.name, `lifecycle metadata: ${phase} is not an array`);
|
|
130
|
+
}
|
|
131
|
+
for (const [position, name] of methods.entries()) {
|
|
132
|
+
if (typeof name !== "string") {
|
|
133
|
+
throw new InvalidMetadataError(
|
|
134
|
+
target.name,
|
|
135
|
+
`lifecycle metadata: ${phase}[${String(position)}] is not a string`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
markVerified(verifiedLifecycleTargets, reader, target);
|
|
141
|
+
return metadata;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* A reader's accessor metadata for a class, verified the first time this process asks.
|
|
146
|
+
*
|
|
147
|
+
* @since 0.6.0
|
|
148
|
+
*/
|
|
149
|
+
export function verifyAccessorMetadata(
|
|
150
|
+
reader: MetadataReader,
|
|
151
|
+
target: Constructor,
|
|
152
|
+
): ReadonlyArray<{ readonly key: string | symbol; readonly descriptor: InjectionDescriptor }> | undefined {
|
|
153
|
+
const metadata = reader.getAccessorMetadata?.(target);
|
|
154
|
+
if (metadata === undefined || isVerified(verifiedAccessorTargets, reader, target)) {
|
|
155
|
+
return metadata;
|
|
156
|
+
}
|
|
157
|
+
if (!Array.isArray(metadata)) {
|
|
158
|
+
throw new InvalidMetadataError(target.name, "accessor metadata: expected an array");
|
|
159
|
+
}
|
|
160
|
+
for (const [position, entry] of metadata.entries()) {
|
|
161
|
+
if (typeof entry !== "object" || entry === null) {
|
|
162
|
+
throw new InvalidMetadataError(target.name, `accessor metadata: [${String(position)}] is not an object`);
|
|
163
|
+
}
|
|
164
|
+
const key: unknown = Reflect.get(entry, "key");
|
|
165
|
+
if (typeof key !== "string" && typeof key !== "symbol") {
|
|
166
|
+
throw new InvalidMetadataError(
|
|
167
|
+
target.name,
|
|
168
|
+
`accessor metadata: [${String(position)}].key is not a string or symbol`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
const descriptor: unknown = Reflect.get(entry, "descriptor");
|
|
172
|
+
if (typeof descriptor !== "object" || descriptor === null) {
|
|
173
|
+
throw new InvalidMetadataError(
|
|
174
|
+
target.name,
|
|
175
|
+
`accessor metadata: [${String(position)}].descriptor is not an object`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
const dependency: unknown = Reflect.get(descriptor, "token");
|
|
179
|
+
if (typeof dependency !== "object" && typeof dependency !== "function") {
|
|
180
|
+
throw new InvalidMetadataError(
|
|
181
|
+
target.name,
|
|
182
|
+
`accessor metadata: [${String(position)}].descriptor.token is not a token or a class`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
markVerified(verifiedAccessorTargets, reader, target);
|
|
187
|
+
return metadata;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* @since 0.5.0-canary.8
|
|
192
|
+
*/
|
|
193
|
+
export class ClassIntrospector {
|
|
194
|
+
// Unallocated until the container resolves its first class binding — a container bound entirely
|
|
195
|
+
// to constants, factories or aliases never introspects one.
|
|
196
|
+
#constructorMetadata: WeakMap<Constructor, ConstructorMetadata | null> | undefined;
|
|
197
|
+
#hasPostConstruct: WeakMap<Constructor, boolean> | undefined;
|
|
198
|
+
#needsActiveContainer: WeakMap<Constructor, boolean> | undefined;
|
|
199
|
+
readonly #reader: MetadataReader;
|
|
200
|
+
readonly #container: Container;
|
|
201
|
+
|
|
202
|
+
constructor(reader: MetadataReader, container: Container) {
|
|
203
|
+
this.#reader = reader;
|
|
204
|
+
this.#container = container;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
constructorMetadata(target: Constructor): ConstructorMetadata | undefined {
|
|
208
|
+
const cached = this.#constructorMetadata?.get(target);
|
|
209
|
+
if (cached !== undefined) {
|
|
210
|
+
return cached === null ? undefined : cached;
|
|
211
|
+
}
|
|
212
|
+
const metadata = this.#reader.getConstructorMetadata(target);
|
|
213
|
+
(this.#constructorMetadata ??= new WeakMap()).set(target, metadata ?? null);
|
|
214
|
+
return metadata;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Whether the class has a `@postConstruct` hook, or `undefined` until {@link discoverPostConstruct}.
|
|
219
|
+
*
|
|
220
|
+
* @remarks Callers treat unknown as "assume it does", so the first activation settles it.
|
|
221
|
+
*/
|
|
222
|
+
knownPostConstruct(target: Constructor): boolean | undefined {
|
|
223
|
+
return this.#hasPostConstruct?.get(target);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
discoverPostConstruct(target: Constructor): void {
|
|
227
|
+
const lifecycle = this.#reader.getLifecycleMetadata(target);
|
|
228
|
+
(this.#hasPostConstruct ??= new WeakMap()).set(
|
|
229
|
+
target,
|
|
230
|
+
lifecycle !== undefined && lifecycle.postConstruct !== undefined && lifecycle.postConstruct.length > 0,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** True when the class has accessor injection, which reads the container during construction. */
|
|
235
|
+
needsActiveContainer(target: Constructor): boolean {
|
|
236
|
+
let needsActiveContainer = this.#needsActiveContainer?.get(target);
|
|
237
|
+
if (needsActiveContainer === undefined) {
|
|
238
|
+
needsActiveContainer = (this.#reader.getAccessorMetadata?.(target)?.length ?? 0) > 0;
|
|
239
|
+
(this.#needsActiveContainer ??= new WeakMap()).set(target, needsActiveContainer);
|
|
240
|
+
}
|
|
241
|
+
return needsActiveContainer;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
instantiate(target: Constructor, deps: Array<unknown>, resolution?: AmbientResolution): unknown {
|
|
245
|
+
const invokable = target as ConstructorInvocation;
|
|
246
|
+
if (!this.needsActiveContainer(target)) {
|
|
247
|
+
return new invokable(...deps);
|
|
248
|
+
}
|
|
249
|
+
return runWithAmbientResolution(this.#container, resolution, () => new invokable(...deps));
|
|
250
|
+
}
|
|
251
|
+
}
|