@venizia/ignis-inversion 0.5.0-1 → 0.5.0-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.
- package/dist/esm/modules/container/abstract.d.ts +8 -0
- package/dist/esm/modules/container/abstract.js.map +1 -1
- package/dist/esm/modules/container/base.d.ts +24 -1
- package/dist/esm/modules/container/base.js +55 -1
- package/dist/esm/modules/container/base.js.map +1 -1
- package/dist/esm/modules/container/common/types.d.ts +8 -0
- package/dist/esm/modules/container/common/types.js.map +1 -1
- package/dist/esm/modules/container/container.d.ts +1 -15
- package/dist/esm/modules/container/container.js +2 -45
- package/dist/esm/modules/container/container.js.map +1 -1
- package/package.json +2 -2
|
@@ -33,10 +33,18 @@ export declare abstract class AbstractContainer extends BaseHelper implements IC
|
|
|
33
33
|
abstract get<T>(opts: {
|
|
34
34
|
key: TBindingKey<T>;
|
|
35
35
|
isOptional?: false;
|
|
36
|
+
} | {
|
|
37
|
+
target: TClass<T>;
|
|
38
|
+
key?: never;
|
|
39
|
+
isOptional?: false;
|
|
36
40
|
}): T;
|
|
37
41
|
abstract get<T>(opts: {
|
|
38
42
|
key: TBindingKey<T>;
|
|
39
43
|
isOptional?: boolean;
|
|
44
|
+
} | {
|
|
45
|
+
target: TClass<T>;
|
|
46
|
+
key?: never;
|
|
47
|
+
isOptional?: boolean;
|
|
40
48
|
}): T | undefined;
|
|
41
49
|
abstract get(opts: {
|
|
42
50
|
key: TBindingKey;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abstract.js","sourceRoot":"","sources":["../../../../src/modules/container/abstract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAMlD,wKAAwK;AACxK,MAAM,OAAgB,iBAAkB,SAAQ,UAAU;IACxD,YAAY,IAAwB;QAClC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,IAAI,iBAAiB,CAAC,IAAI,EAAE,CAAC,CAAC;IAC1D,CAAC;
|
|
1
|
+
{"version":3,"file":"abstract.js","sourceRoot":"","sources":["../../../../src/modules/container/abstract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAMlD,wKAAwK;AACxK,MAAM,OAAgB,iBAAkB,SAAQ,UAAU;IACxD,YAAY,IAAwB;QAClC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,IAAI,iBAAiB,CAAC,IAAI,EAAE,CAAC,CAAC;IAC1D,CAAC;CA6CF","sourcesContent":["import { BaseHelper } from '@/common/base-helper';\nimport type { TBindingKey, TClass, TNullable } from '@/common/types';\nimport type { IBinding } from '../binding/common/types';\nimport type { MetadataRegistry } from '../registry/registry';\nimport type { IContainer } from './common/types';\n\n/** Container CONTRACT as a class, typed against `IBinding` - implementations sharing no storage start here; ones that only vary resolution start at `BaseContainer`. */\nexport abstract class AbstractContainer extends BaseHelper implements IContainer {\n constructor(opts?: { scope: string }) {\n super({ scope: opts?.scope ?? AbstractContainer.name });\n }\n\n abstract getMetadataRegistry(): MetadataRegistry;\n\n abstract bind<T>(opts: { key: TBindingKey<T> }): IBinding<T>;\n abstract bind(opts: { key: TBindingKey }): IBinding;\n abstract isBound(opts: { key: TBindingKey }): boolean;\n abstract getBinding<T>(opts: { key: TBindingKey<T> }): TNullable<IBinding<T>>;\n abstract getBinding(opts: { key: TBindingKey }): TNullable<IBinding>;\n abstract unbind(opts: { key: TBindingKey }): boolean;\n abstract set<T>(opts: { binding: IBinding<T> }): void;\n\n abstract get<T>(\n opts:\n | { key: TBindingKey<T>; isOptional?: false }\n | { target: TClass<T>; key?: never; isOptional?: false },\n ): T;\n abstract get<T>(\n opts:\n | { key: TBindingKey<T>; isOptional?: boolean }\n | { target: TClass<T>; key?: never; isOptional?: boolean },\n ): T | undefined;\n abstract get(opts: { key: TBindingKey; isOptional?: boolean }): unknown;\n abstract gets<T extends unknown[]>(opts: {\n bindings: {\n [K in keyof T]: {\n key: TBindingKey<T[K]>;\n isOptional?: boolean;\n };\n };\n }): { [K in keyof T]: T[K] | undefined };\n\n abstract resolve<T>(cls: TClass<T>): T;\n abstract instantiate<T>(cls: TClass<T>): T;\n\n abstract findByTag<T = unknown>(opts: {\n tag: string;\n exclude?: Array<string> | Set<string>;\n }): IBinding<T>[];\n abstract startResolutionCounting(): void;\n abstract stopResolutionCounting(): void;\n abstract getResolutionCounts(): ReadonlyMap<string, number>;\n\n abstract clear(): void;\n abstract reset(): void;\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type TBindingKey, type TClass, type TNullable } from '../../common/types.js';
|
|
1
|
+
import { type TBindingKey, type TClass, type TInjectTarget, type TNullable } from '../../common/types.js';
|
|
2
2
|
import { Binding } from '../binding/binding.js';
|
|
3
3
|
import { AbstractContainer } from './abstract.js';
|
|
4
4
|
/** Storage half of the container, keyed by normalized binding-key strings - resolution (`instantiate`) stays abstract, it is the part an implementation swaps. */
|
|
@@ -31,6 +31,21 @@ export declare abstract class BaseContainer extends AbstractContainer {
|
|
|
31
31
|
getMetadataRegistry(): import("../index.js").MetadataRegistry;
|
|
32
32
|
/** A JavaScript caller can still pass a removed form (a symbol, `{ namespace, key }`): say what replaced it. */
|
|
33
33
|
private static assertKey;
|
|
34
|
+
/**
|
|
35
|
+
* The class form reads the key the REGISTRATION recorded, so a call-site override and an imperative
|
|
36
|
+
* `application.service(X)` both resolve. `@inject` takes a key or a class; metadata a framework
|
|
37
|
+
* writes may carry both, and then the class wins and the key is the fallback for a class nothing
|
|
38
|
+
* recorded - one bound by hand with `bind({ key }).toClass(X)`. An optional class with neither
|
|
39
|
+
* resolves to `undefined`, as an optional key would.
|
|
40
|
+
*/
|
|
41
|
+
protected resolveBindingKey(opts: {
|
|
42
|
+
key?: TBindingKey;
|
|
43
|
+
target?: TInjectTarget;
|
|
44
|
+
isOptional?: boolean;
|
|
45
|
+
/** Whose declaration this is, as the messages name it: a class, or the container for a `get`. */
|
|
46
|
+
owner: string;
|
|
47
|
+
at: string;
|
|
48
|
+
}): TBindingKey | undefined;
|
|
34
49
|
bind<T>(opts: {
|
|
35
50
|
key: TBindingKey<T>;
|
|
36
51
|
}): Binding<T>;
|
|
@@ -55,10 +70,18 @@ export declare abstract class BaseContainer extends AbstractContainer {
|
|
|
55
70
|
get<T>(opts: {
|
|
56
71
|
key: TBindingKey<T>;
|
|
57
72
|
isOptional?: false;
|
|
73
|
+
} | {
|
|
74
|
+
target: TClass<T>;
|
|
75
|
+
key?: never;
|
|
76
|
+
isOptional?: false;
|
|
58
77
|
}): T;
|
|
59
78
|
get<T>(opts: {
|
|
60
79
|
key: TBindingKey<T>;
|
|
61
80
|
isOptional?: boolean;
|
|
81
|
+
} | {
|
|
82
|
+
target: TClass<T>;
|
|
83
|
+
key?: never;
|
|
84
|
+
isOptional?: boolean;
|
|
62
85
|
}): T | undefined;
|
|
63
86
|
get(opts: {
|
|
64
87
|
key: TBindingKey;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ResolverUtility } from '../../common/utilities.js';
|
|
1
2
|
import { Binding } from '../binding/binding.js';
|
|
2
3
|
import { getError } from '../error/index.js';
|
|
3
4
|
import { metadataRegistry } from '../registry/registry.js';
|
|
@@ -43,6 +44,48 @@ export class BaseContainer extends AbstractContainer {
|
|
|
43
44
|
message: `[${opts.caller}] A binding key is a string made by BindingKeys.build<T>({ namespace?, key }) | got: ${typeof opts.key} | a symbol and the { namespace, key } object are no longer keys`,
|
|
44
45
|
});
|
|
45
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* The class form reads the key the REGISTRATION recorded, so a call-site override and an imperative
|
|
49
|
+
* `application.service(X)` both resolve. `@inject` takes a key or a class; metadata a framework
|
|
50
|
+
* writes may carry both, and then the class wins and the key is the fallback for a class nothing
|
|
51
|
+
* recorded - one bound by hand with `bind({ key }).toClass(X)`. An optional class with neither
|
|
52
|
+
* resolves to `undefined`, as an optional key would.
|
|
53
|
+
*/
|
|
54
|
+
resolveBindingKey(opts) {
|
|
55
|
+
const { key, target: declared, isOptional = false, owner, at } = opts;
|
|
56
|
+
if (declared === undefined && key !== undefined) {
|
|
57
|
+
return key;
|
|
58
|
+
}
|
|
59
|
+
if (declared === undefined) {
|
|
60
|
+
throw getError({
|
|
61
|
+
message: `[${owner}] ${at} has neither an @inject key nor a class`,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
const target = ResolverUtility.resolveInjectTarget({ target: declared });
|
|
65
|
+
if (target === undefined) {
|
|
66
|
+
throw getError({
|
|
67
|
+
message: `[${owner}] ${at} names a function that did not return a class`,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
const recorded = this.getMetadataRegistry().getBindingKey({ target }) ?? key;
|
|
71
|
+
if (recorded === undefined && isOptional) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
if (recorded === undefined) {
|
|
75
|
+
throw getError({
|
|
76
|
+
message: `[${owner}] ${at} names '${target.name}', which is not registered as an artifact | Decorate it (@service, @repository, ...) or register it on the application before it is injected`,
|
|
77
|
+
extra: { unregisteredClass: target.name },
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
// A same-name class records the same derived key (a library's own copy of a class the application registers): resolving it would hand back the OTHER class without a word.
|
|
81
|
+
const bound = this.getBinding({ key: recorded })?.boundClass();
|
|
82
|
+
if (bound && bound !== target && !(bound.prototype instanceof target)) {
|
|
83
|
+
throw getError({
|
|
84
|
+
message: `[${owner}] ${at} names '${target.name}', but '${String(recorded)}' is bound to a different class named '${bound.name}' | Two classes share one name and so one key - rename one, re-export the registered one, or inject by key`,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return recorded;
|
|
88
|
+
}
|
|
46
89
|
bind(opts) {
|
|
47
90
|
const { key } = opts;
|
|
48
91
|
BaseContainer.assertKey({ key, caller: 'bind' });
|
|
@@ -67,7 +110,18 @@ export class BaseContainer extends AbstractContainer {
|
|
|
67
110
|
this.bindings.set(binding.key, binding);
|
|
68
111
|
}
|
|
69
112
|
get(opts) {
|
|
70
|
-
const {
|
|
113
|
+
const { target, isOptional = false } = opts;
|
|
114
|
+
// The class form is the key `@inject({ target })` would read, resolved by the same method with the same errors.
|
|
115
|
+
const key = target === undefined
|
|
116
|
+
? opts.key
|
|
117
|
+
: this.resolveBindingKey({ target, isOptional, owner: 'Container', at: 'get({ target })' });
|
|
118
|
+
if (key === undefined) {
|
|
119
|
+
// An optional class nothing registered answers `undefined`; a call with neither is the JavaScript misuse `assertKey` names.
|
|
120
|
+
if (target === undefined) {
|
|
121
|
+
BaseContainer.assertKey({ key, caller: 'get' });
|
|
122
|
+
}
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
71
125
|
BaseContainer.assertKey({ key, caller: 'get' });
|
|
72
126
|
const binding = this.getBinding({ key });
|
|
73
127
|
if (binding) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../../src/modules/container/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C,wGAAwG;AACxG,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,kKAAkK;AAClK,MAAM,OAAgB,aAAc,SAAQ,iBAAiB;IA4B3D,YAAY,IAAwB;QAClC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,IAAI,aAAa,CAAC,IAAI,EAAE,CAAC,CAAC;QA5BtD;;WAEG;QACO,aAAQ,GAAG,IAAI,GAAG,EAAwB,CAAC;QAErD,2EAA2E;QACjE,qBAAgB,GAAG,IAAI,GAAG,EAAkB,CAAC;QAEvD;;;;;WAKG;QACO,0BAAqB,G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I,QAAQ,EAAE,CAAC;YAC/B,OAAO,CAAC,UAAU,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;IAEQ,KAAK;QACZ,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;CACF","sourcesContent":["import { type TBindingKey, type TClass, type TNullable } from '@/common/types';\nimport { Binding } from '../binding/binding';\nimport { getError } from '../error';\nimport { metadataRegistry } from '../registry/registry';\nimport { AbstractContainer } from './abstract';\n\n/** A dependency graph nests far less than this; deeper is all but certainly a cycle, and is checked. */\nconst CYCLE_CHECK_DEPTH = 64;\n\n/** Storage half of the container, keyed by normalized binding-key strings - resolution (`instantiate`) stays abstract, it is the part an implementation swaps. */\nexport abstract class BaseContainer extends AbstractContainer {\n /**\n * Every binding of the container, whatever its `T`. The one deliberate `any` of the container: `Binding<T>` is invariant in `T` and this store holds every `T`, so no concrete type argument lets a lookup answer the `Binding<T>` its caller names without a cast. Everything around it is typed; a token-keyed container is the way to remove it.\n */\n protected bindings = new Map<string, Binding<any>>();\n\n /** How often each bound key has been handed out since counting started. */\n protected resolutionCounts = new Map<string, number>();\n\n /**\n * Whether resolutions are counted. The counter sits on the hottest path in the container, and an\n * application that never reads the report should not pay for it.\n *\n * @default false\n */\n protected isCountingResolutions = false;\n\n /** Whether `startResolutionCounting` has ever run. A report read without it would name every binding, which is a wrong answer rather than an empty one. */\n protected hasStartedResolutionCounting = false;\n\n /**\n * Nesting depth of the reads in progress. Past {@link CYCLE_CHECK_DEPTH} the keys are tracked, and\n * a key met again is a cycle: a real graph is far shallower, so the common read pays one counter.\n * Only the synchronous part is checked - a cycle through an `await` is not seen.\n */\n protected resolutionDepth = 0;\n protected resolvingKeys = new Set<string>();\n\n constructor(opts?: { scope: string }) {\n super({ scope: opts?.scope ?? BaseContainer.name });\n }\n\n override getMetadataRegistry() {\n return metadataRegistry;\n }\n\n /** A JavaScript caller can still pass a removed form (a symbol, `{ namespace, key }`): say what replaced it. */\n private static assertKey(opts: { key: unknown; caller: string }): void {\n if (typeof opts.key === 'string') {\n return;\n }\n\n throw getError({\n message: `[${opts.caller}] A binding key is a string made by BindingKeys.build<T>({ namespace?, key }) | got: ${typeof opts.key} | a symbol and the { namespace, key } object are no longer keys`,\n });\n }\n\n override bind<T>(opts: { key: TBindingKey<T> }): Binding<T>;\n override bind(opts: { key: TBindingKey }): Binding;\n override bind<T>(opts: { key: TBindingKey<T> }): Binding<T> {\n const { key } = opts;\n BaseContainer.assertKey({ key, caller: 'bind' });\n const binding = new Binding<T>({ key });\n this.bindings.set(key, binding);\n\n return binding;\n }\n\n override isBound(opts: { key: TBindingKey }): boolean {\n BaseContainer.assertKey({ key: opts.key, caller: 'isBound' });\n\n return this.bindings.has(opts.key);\n }\n\n override getBinding<T>(opts: { key: TBindingKey<T> }): TNullable<Binding<T>>;\n override getBinding(opts: { key: TBindingKey }): TNullable<Binding>;\n override getBinding<T>(opts: { key: TBindingKey<T> }): TNullable<Binding<T>> {\n BaseContainer.assertKey({ key: opts.key, caller: 'getBinding' });\n\n return this.bindings.get(opts.key);\n }\n\n override unbind(opts: { key: TBindingKey }): boolean {\n BaseContainer.assertKey({ key: opts.key, caller: 'unbind' });\n\n return this.bindings.delete(opts.key);\n }\n\n override set<T>(opts: { binding: Binding<T> }): void {\n const { binding } = opts;\n this.bindings.set(binding.key, binding);\n }\n\n override get<T>(opts: { key: TBindingKey<T>; isOptional?: false }): T;\n override get<T>(opts: { key: TBindingKey<T>; isOptional?: boolean }): T | undefined;\n override get(opts: { key: TBindingKey; isOptional?: boolean }): unknown;\n override get<T>(opts: { key: TBindingKey<T>; isOptional?: boolean }): T | undefined {\n const { key, isOptional = false } = opts;\n BaseContainer.assertKey({ key, caller: 'get' });\n\n const binding = this.getBinding<T>({ key });\n\n if (binding) {\n // Counted here and nowhere else: constructor injection, property injection, `application.get`\n // and `BindingVerifier.verify` all arrive through this one method. A miss below is not a resolution.\n if (this.isCountingResolutions) {\n this.resolutionCounts.set(binding.key, (this.resolutionCounts.get(binding.key) ?? 0) + 1);\n }\n\n // The hot path: a cached singleton or a value builds nothing, so it cannot be part of a cycle.\n if (binding.isSettled()) {\n return binding.getValue(this);\n }\n\n this.resolutionDepth++;\n const isTracked = this.resolutionDepth > CYCLE_CHECK_DEPTH;\n\n if (isTracked && this.resolvingKeys.has(binding.key)) {\n this.resolutionDepth--;\n throw getError({\n message: `Circular dependency | ${[...this.resolvingKeys, binding.key].join(' -> ')}`,\n });\n }\n\n if (isTracked) {\n this.resolvingKeys.add(binding.key);\n }\n\n try {\n return binding.getValue(this);\n } finally {\n this.resolutionDepth--;\n\n if (isTracked) {\n this.resolvingKeys.delete(binding.key);\n }\n }\n }\n\n if (!isOptional) {\n throw getError({\n message: `Binding key: ${key} is not bounded in context!`,\n extra: { unboundKey: key },\n });\n }\n\n return undefined;\n }\n\n override gets<T extends unknown[]>(opts: {\n bindings: {\n [K in keyof T]: {\n key: TBindingKey<T[K]>;\n isOptional?: boolean;\n };\n };\n }): { [K in keyof T]: T[K] | undefined } {\n return opts.bindings.map((opt) => this.get({ ...opt, isOptional: true })) as {\n [K in keyof T]: T[K] | undefined;\n };\n }\n\n /** Clears the counts and starts counting. Call it AFTER `initialize()`: boot resolves plenty no request ever asks for, and `bootChecks.binding.doVerify` reads every service and repository. */\n override startResolutionCounting(): void {\n this.resolutionCounts.clear();\n this.isCountingResolutions = true;\n this.hasStartedResolutionCounting = true;\n }\n\n /** Stops counting and keeps what was counted, so `get` returns to its uncounted cost. */\n override stopResolutionCounting(): void {\n this.isCountingResolutions = false;\n }\n\n /** How many times each bound key has been read since counting started. A key that was bound and never read is absent. The map is a copy. */\n override getResolutionCounts(): ReadonlyMap<string, number> {\n return new Map(this.resolutionCounts);\n }\n\n override resolve<T>(cls: TClass<T>): T {\n return this.instantiate(cls);\n }\n\n override findByTag<T = unknown>(opts: {\n tag: string;\n exclude?: Array<string> | Set<string>;\n }): Binding<T>[] {\n const { tag, exclude } = opts;\n\n const rs: Binding<T>[] = [];\n\n const bindings = this.bindings.values();\n\n for (const binding of bindings) {\n if (!binding.hasTag(tag)) {\n continue;\n }\n\n if (exclude instanceof Array && exclude.length > 0 && exclude.includes(binding.key)) {\n continue;\n }\n\n if (exclude instanceof Set && exclude.size > 0 && exclude.has(binding.key)) {\n continue;\n }\n\n rs.push(binding);\n }\n\n return rs;\n }\n\n override clear(): void {\n const bindings = this.bindings.values();\n\n for (const binding of bindings) {\n binding.clearCache();\n }\n }\n\n override reset(): void {\n this.bindings.clear();\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../../src/modules/container/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C,wGAAwG;AACxG,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,kKAAkK;AAClK,MAAM,OAAgB,aAAc,SAAQ,iBAAiB;IA4B3D,YAAY,IAAwB;QAClC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,IAAI,aAAa,CAAC,IAAI,EAAE,CAAC,CAAC;QA5BtD;;WAEG;QACO,aAAQ,GAAG,IAAI,GAAG,EAAwB,CAAC;QAErD,2EAA2E;QACjE,qBAAgB,GAAG,IAAI,GAAG,EAAkB,CAAC;QAEvD;;;;;WAKG;QACO,0BAAqB,GAAG,KAAK,CAAC;QAExC,2JAA2J;QACjJ,iCAA4B,GAAG,KAAK,CAAC;QAE/C;;;;WAIG;QACO,oBAAe,GAAG,CAAC,CAAC;QACpB,kBAAa,GAAG,IAAI,GAAG,EAAU,CAAC;IAI5C,CAAC;IAEQ,mBAAmB;QAC1B,OAAO,gB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AAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;gBACzB,SAAS;YACX,CAAC;YAED,IAAI,OAAO,YAAY,KAAK,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;gBACpF,SAAS;YACX,CAAC;YAED,IAAI,OAAO,YAAY,GAAG,IAAI,OAAO,CAAC,IAAI,GAAG,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC3E,SAAS;YACX,CAAC;YAED,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACnB,CAAC;QAED,OAAO,EAAE,CAAC;IACZ,CAAC;IAEQ,KAAK;QACZ,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;QAExC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC/B,OAAO,CAAC,UAAU,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;IAEQ,KAAK;QACZ,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC;IACxB,CAAC;CACF","sourcesContent":["import { type TBindingKey, type TClass, type TInjectTarget, type TNullable } from '@/common/types';\nimport { ResolverUtility } from '@/common/utilities';\nimport { Binding } from '../binding/binding';\nimport { getError } from '../error';\nimport { metadataRegistry } from '../registry/registry';\nimport { AbstractContainer } from './abstract';\n\n/** A dependency graph nests far less than this; deeper is all but certainly a cycle, and is checked. */\nconst CYCLE_CHECK_DEPTH = 64;\n\n/** Storage half of the container, keyed by normalized binding-key strings - resolution (`instantiate`) stays abstract, it is the part an implementation swaps. */\nexport abstract class BaseContainer extends AbstractContainer {\n /**\n * Every binding of the container, whatever its `T`. The one deliberate `any` of the container: `Binding<T>` is invariant in `T` and this store holds every `T`, so no concrete type argument lets a lookup answer the `Binding<T>` its caller names without a cast. Everything around it is typed; a token-keyed container is the way to remove it.\n */\n protected bindings = new Map<string, Binding<any>>();\n\n /** How often each bound key has been handed out since counting started. */\n protected resolutionCounts = new Map<string, number>();\n\n /**\n * Whether resolutions are counted. The counter sits on the hottest path in the container, and an\n * application that never reads the report should not pay for it.\n *\n * @default false\n */\n protected isCountingResolutions = false;\n\n /** Whether `startResolutionCounting` has ever run. A report read without it would name every binding, which is a wrong answer rather than an empty one. */\n protected hasStartedResolutionCounting = false;\n\n /**\n * Nesting depth of the reads in progress. Past {@link CYCLE_CHECK_DEPTH} the keys are tracked, and\n * a key met again is a cycle: a real graph is far shallower, so the common read pays one counter.\n * Only the synchronous part is checked - a cycle through an `await` is not seen.\n */\n protected resolutionDepth = 0;\n protected resolvingKeys = new Set<string>();\n\n constructor(opts?: { scope: string }) {\n super({ scope: opts?.scope ?? BaseContainer.name });\n }\n\n override getMetadataRegistry() {\n return metadataRegistry;\n }\n\n /** A JavaScript caller can still pass a removed form (a symbol, `{ namespace, key }`): say what replaced it. */\n private static assertKey(opts: { key: unknown; caller: string }): void {\n if (typeof opts.key === 'string') {\n return;\n }\n\n throw getError({\n message: `[${opts.caller}] A binding key is a string made by BindingKeys.build<T>({ namespace?, key }) | got: ${typeof opts.key} | a symbol and the { namespace, key } object are no longer keys`,\n });\n }\n\n /**\n * The class form reads the key the REGISTRATION recorded, so a call-site override and an imperative\n * `application.service(X)` both resolve. `@inject` takes a key or a class; metadata a framework\n * writes may carry both, and then the class wins and the key is the fallback for a class nothing\n * recorded - one bound by hand with `bind({ key }).toClass(X)`. An optional class with neither\n * resolves to `undefined`, as an optional key would.\n */\n protected resolveBindingKey(opts: {\n key?: TBindingKey;\n target?: TInjectTarget;\n isOptional?: boolean;\n /** Whose declaration this is, as the messages name it: a class, or the container for a `get`. */\n owner: string;\n at: string;\n }): TBindingKey | undefined {\n const { key, target: declared, isOptional = false, owner, at } = opts;\n\n if (declared === undefined && key !== undefined) {\n return key;\n }\n\n if (declared === undefined) {\n throw getError({\n message: `[${owner}] ${at} has neither an @inject key nor a class`,\n });\n }\n\n const target = ResolverUtility.resolveInjectTarget({ target: declared });\n\n if (target === undefined) {\n throw getError({\n message: `[${owner}] ${at} names a function that did not return a class`,\n });\n }\n\n const recorded = this.getMetadataRegistry().getBindingKey({ target }) ?? key;\n\n if (recorded === undefined && isOptional) {\n return undefined;\n }\n\n if (recorded === undefined) {\n throw getError({\n message: `[${owner}] ${at} names '${target.name}', which is not registered as an artifact | Decorate it (@service, @repository, ...) or register it on the application before it is injected`,\n extra: { unregisteredClass: target.name },\n });\n }\n\n // A same-name class records the same derived key (a library's own copy of a class the application registers): resolving it would hand back the OTHER class without a word.\n const bound = this.getBinding({ key: recorded })?.boundClass();\n\n if (bound && bound !== target && !(bound.prototype instanceof target)) {\n throw getError({\n message: `[${owner}] ${at} names '${target.name}', but '${String(recorded)}' is bound to a different class named '${bound.name}' | Two classes share one name and so one key - rename one, re-export the registered one, or inject by key`,\n });\n }\n\n return recorded;\n }\n\n override bind<T>(opts: { key: TBindingKey<T> }): Binding<T>;\n override bind(opts: { key: TBindingKey }): Binding;\n override bind<T>(opts: { key: TBindingKey<T> }): Binding<T> {\n const { key } = opts;\n BaseContainer.assertKey({ key, caller: 'bind' });\n const binding = new Binding<T>({ key });\n this.bindings.set(key, binding);\n\n return binding;\n }\n\n override isBound(opts: { key: TBindingKey }): boolean {\n BaseContainer.assertKey({ key: opts.key, caller: 'isBound' });\n\n return this.bindings.has(opts.key);\n }\n\n override getBinding<T>(opts: { key: TBindingKey<T> }): TNullable<Binding<T>>;\n override getBinding(opts: { key: TBindingKey }): TNullable<Binding>;\n override getBinding<T>(opts: { key: TBindingKey<T> }): TNullable<Binding<T>> {\n BaseContainer.assertKey({ key: opts.key, caller: 'getBinding' });\n\n return this.bindings.get(opts.key);\n }\n\n override unbind(opts: { key: TBindingKey }): boolean {\n BaseContainer.assertKey({ key: opts.key, caller: 'unbind' });\n\n return this.bindings.delete(opts.key);\n }\n\n override set<T>(opts: { binding: Binding<T> }): void {\n const { binding } = opts;\n this.bindings.set(binding.key, binding);\n }\n\n override get<T>(\n opts:\n | { key: TBindingKey<T>; isOptional?: false }\n | { target: TClass<T>; key?: never; isOptional?: false },\n ): T;\n override get<T>(\n opts:\n | { key: TBindingKey<T>; isOptional?: boolean }\n | { target: TClass<T>; key?: never; isOptional?: boolean },\n ): T | undefined;\n override get(opts: { key: TBindingKey; isOptional?: boolean }): unknown;\n override get<T>(opts: {\n key?: TBindingKey<T>;\n target?: TClass<T>;\n isOptional?: boolean;\n }): T | undefined {\n const { target, isOptional = false } = opts;\n // The class form is the key `@inject({ target })` would read, resolved by the same method with the same errors.\n const key =\n target === undefined\n ? opts.key\n : this.resolveBindingKey({ target, isOptional, owner: 'Container', at: 'get({ target })' });\n\n if (key === undefined) {\n // An optional class nothing registered answers `undefined`; a call with neither is the JavaScript misuse `assertKey` names.\n if (target === undefined) {\n BaseContainer.assertKey({ key, caller: 'get' });\n }\n\n return undefined;\n }\n\n BaseContainer.assertKey({ key, caller: 'get' });\n\n const binding = this.getBinding<T>({ key });\n\n if (binding) {\n // Counted here and nowhere else: constructor injection, property injection, `application.get`\n // and `BindingVerifier.verify` all arrive through this one method. A miss below is not a resolution.\n if (this.isCountingResolutions) {\n this.resolutionCounts.set(binding.key, (this.resolutionCounts.get(binding.key) ?? 0) + 1);\n }\n\n // The hot path: a cached singleton or a value builds nothing, so it cannot be part of a cycle.\n if (binding.isSettled()) {\n return binding.getValue(this);\n }\n\n this.resolutionDepth++;\n const isTracked = this.resolutionDepth > CYCLE_CHECK_DEPTH;\n\n if (isTracked && this.resolvingKeys.has(binding.key)) {\n this.resolutionDepth--;\n throw getError({\n message: `Circular dependency | ${[...this.resolvingKeys, binding.key].join(' -> ')}`,\n });\n }\n\n if (isTracked) {\n this.resolvingKeys.add(binding.key);\n }\n\n try {\n return binding.getValue(this);\n } finally {\n this.resolutionDepth--;\n\n if (isTracked) {\n this.resolvingKeys.delete(binding.key);\n }\n }\n }\n\n if (!isOptional) {\n throw getError({\n message: `Binding key: ${key} is not bounded in context!`,\n extra: { unboundKey: key },\n });\n }\n\n return undefined;\n }\n\n override gets<T extends unknown[]>(opts: {\n bindings: {\n [K in keyof T]: {\n key: TBindingKey<T[K]>;\n isOptional?: boolean;\n };\n };\n }): { [K in keyof T]: T[K] | undefined } {\n return opts.bindings.map((opt) => this.get({ ...opt, isOptional: true })) as {\n [K in keyof T]: T[K] | undefined;\n };\n }\n\n /** Clears the counts and starts counting. Call it AFTER `initialize()`: boot resolves plenty no request ever asks for, and `bootChecks.binding.doVerify` reads every service and repository. */\n override startResolutionCounting(): void {\n this.resolutionCounts.clear();\n this.isCountingResolutions = true;\n this.hasStartedResolutionCounting = true;\n }\n\n /** Stops counting and keeps what was counted, so `get` returns to its uncounted cost. */\n override stopResolutionCounting(): void {\n this.isCountingResolutions = false;\n }\n\n /** How many times each bound key has been read since counting started. A key that was bound and never read is absent. The map is a copy. */\n override getResolutionCounts(): ReadonlyMap<string, number> {\n return new Map(this.resolutionCounts);\n }\n\n override resolve<T>(cls: TClass<T>): T {\n return this.instantiate(cls);\n }\n\n override findByTag<T = unknown>(opts: {\n tag: string;\n exclude?: Array<string> | Set<string>;\n }): Binding<T>[] {\n const { tag, exclude } = opts;\n\n const rs: Binding<T>[] = [];\n\n const bindings = this.bindings.values();\n\n for (const binding of bindings) {\n if (!binding.hasTag(tag)) {\n continue;\n }\n\n if (exclude instanceof Array && exclude.length > 0 && exclude.includes(binding.key)) {\n continue;\n }\n\n if (exclude instanceof Set && exclude.size > 0 && exclude.has(binding.key)) {\n continue;\n }\n\n rs.push(binding);\n }\n\n return rs;\n }\n\n override clear(): void {\n const bindings = this.bindings.values();\n\n for (const binding of bindings) {\n binding.clearCache();\n }\n }\n\n override reset(): void {\n this.bindings.clear();\n }\n}\n"]}
|
|
@@ -28,10 +28,18 @@ export interface IContainer {
|
|
|
28
28
|
get<T>(opts: {
|
|
29
29
|
key: TBindingKey<T>;
|
|
30
30
|
isOptional?: false;
|
|
31
|
+
} | {
|
|
32
|
+
target: TClass<T>;
|
|
33
|
+
key?: never;
|
|
34
|
+
isOptional?: false;
|
|
31
35
|
}): T;
|
|
32
36
|
get<T>(opts: {
|
|
33
37
|
key: TBindingKey<T>;
|
|
34
38
|
isOptional?: boolean;
|
|
39
|
+
} | {
|
|
40
|
+
target: TClass<T>;
|
|
41
|
+
key?: never;
|
|
42
|
+
isOptional?: boolean;
|
|
35
43
|
}): T | undefined;
|
|
36
44
|
get(opts: {
|
|
37
45
|
key: TBindingKey;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/modules/container/common/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { IBinding } from '../../binding/common/types';\nimport type { TBindingKey, TClass, TNullable } from '@/common/types';\nimport type { MetadataRegistry } from '../../registry/registry';\n\n/** Binds keys, resolves them and instantiates classes with their injected dependencies. */\nexport interface IContainer {\n getMetadataRegistry(): MetadataRegistry;\n\n // The overloads without `<T>` take a key that is no single typed key (a union of typed keys, say) and answer `unknown`.\n bind<T>(opts: { key: TBindingKey<T> }): IBinding<T>;\n bind(opts: { key: TBindingKey }): IBinding;\n isBound(opts: { key: TBindingKey }): boolean;\n getBinding<T>(opts: { key: TBindingKey<T> }): TNullable<IBinding<T>>;\n getBinding(opts: { key: TBindingKey }): TNullable<IBinding>;\n unbind(opts: { key: TBindingKey }): boolean;\n set<T>(opts: { binding: IBinding<T> }): void;\n\n get<T>(opts
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/modules/container/common/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { IBinding } from '../../binding/common/types';\nimport type { TBindingKey, TClass, TNullable } from '@/common/types';\nimport type { MetadataRegistry } from '../../registry/registry';\n\n/** Binds keys, resolves them and instantiates classes with their injected dependencies. */\nexport interface IContainer {\n getMetadataRegistry(): MetadataRegistry;\n\n // The overloads without `<T>` take a key that is no single typed key (a union of typed keys, say) and answer `unknown`.\n bind<T>(opts: { key: TBindingKey<T> }): IBinding<T>;\n bind(opts: { key: TBindingKey }): IBinding;\n isBound(opts: { key: TBindingKey }): boolean;\n getBinding<T>(opts: { key: TBindingKey<T> }): TNullable<IBinding<T>>;\n getBinding(opts: { key: TBindingKey }): TNullable<IBinding>;\n unbind(opts: { key: TBindingKey }): boolean;\n set<T>(opts: { binding: IBinding<T> }): void;\n\n get<T>(\n opts:\n | { key: TBindingKey<T>; isOptional?: false }\n | { target: TClass<T>; key?: never; isOptional?: false },\n ): T;\n get<T>(\n opts:\n | { key: TBindingKey<T>; isOptional?: boolean }\n | { target: TClass<T>; key?: never; isOptional?: boolean },\n ): T | undefined;\n get(opts: { key: TBindingKey; isOptional?: boolean }): unknown;\n gets<T extends unknown[]>(opts: {\n bindings: {\n [K in keyof T]: {\n key: TBindingKey<T[K]>;\n isOptional?: boolean;\n };\n };\n }): { [K in keyof T]: T[K] | undefined };\n\n resolve<T>(cls: TClass<T>): T;\n instantiate<T>(cls: TClass<T>): T;\n\n findByTag<T = unknown>(opts: {\n tag: string;\n exclude?: Array<string> | Set<string>;\n }): IBinding<T>[];\n clear(): void;\n reset(): void;\n}\n"]}
|
|
@@ -1,24 +1,10 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type TClass } from '../../common/types.js';
|
|
2
2
|
import { BaseContainer } from './base.js';
|
|
3
3
|
/** Default container: constructor + property injection driven by the decorator metadata registry. */
|
|
4
4
|
export declare class Container extends BaseContainer {
|
|
5
5
|
constructor(opts?: {
|
|
6
6
|
scope: string;
|
|
7
7
|
});
|
|
8
|
-
/**
|
|
9
|
-
* The class form reads the key the REGISTRATION recorded, so a call-site override and an imperative
|
|
10
|
-
* `application.service(X)` both resolve. `@inject` takes a key or a class; metadata a framework
|
|
11
|
-
* writes may carry both, and then the class wins and the key is the fallback for a class nothing
|
|
12
|
-
* recorded - one bound by hand with `bind({ key }).toClass(X)`. An optional class with neither
|
|
13
|
-
* resolves to `undefined`, as an optional key would.
|
|
14
|
-
*/
|
|
15
|
-
protected resolveBindingKey(opts: {
|
|
16
|
-
key?: TBindingKey;
|
|
17
|
-
target?: TInjectTarget;
|
|
18
|
-
isOptional?: boolean;
|
|
19
|
-
cls: TClass<unknown>;
|
|
20
|
-
at: string;
|
|
21
|
-
}): TBindingKey | undefined;
|
|
22
8
|
instantiate<T>(cls: TClass<T>): T;
|
|
23
9
|
}
|
|
24
10
|
//# sourceMappingURL=container.d.ts.map
|
|
@@ -1,53 +1,10 @@
|
|
|
1
1
|
import { getError } from '../error/index.js';
|
|
2
|
-
import { ResolverUtility } from '../../common/utilities.js';
|
|
3
2
|
import { BaseContainer } from './base.js';
|
|
4
3
|
/** Default container: constructor + property injection driven by the decorator metadata registry. */
|
|
5
4
|
export class Container extends BaseContainer {
|
|
6
5
|
constructor(opts) {
|
|
7
6
|
super({ scope: opts?.scope ?? Container.name });
|
|
8
7
|
}
|
|
9
|
-
/**
|
|
10
|
-
* The class form reads the key the REGISTRATION recorded, so a call-site override and an imperative
|
|
11
|
-
* `application.service(X)` both resolve. `@inject` takes a key or a class; metadata a framework
|
|
12
|
-
* writes may carry both, and then the class wins and the key is the fallback for a class nothing
|
|
13
|
-
* recorded - one bound by hand with `bind({ key }).toClass(X)`. An optional class with neither
|
|
14
|
-
* resolves to `undefined`, as an optional key would.
|
|
15
|
-
*/
|
|
16
|
-
resolveBindingKey(opts) {
|
|
17
|
-
const { key, target: declared, isOptional = false, cls, at } = opts;
|
|
18
|
-
if (declared === undefined && key !== undefined) {
|
|
19
|
-
return key;
|
|
20
|
-
}
|
|
21
|
-
if (declared === undefined) {
|
|
22
|
-
throw getError({
|
|
23
|
-
message: `[${cls.name}] ${at} has neither an @inject key nor a class`,
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
const target = ResolverUtility.resolveInjectTarget({ target: declared });
|
|
27
|
-
if (target === undefined) {
|
|
28
|
-
throw getError({
|
|
29
|
-
message: `[${cls.name}] ${at} names a function that did not return a class`,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
const recorded = this.getMetadataRegistry().getBindingKey({ target }) ?? key;
|
|
33
|
-
if (recorded === undefined && isOptional) {
|
|
34
|
-
return undefined;
|
|
35
|
-
}
|
|
36
|
-
if (recorded === undefined) {
|
|
37
|
-
throw getError({
|
|
38
|
-
message: `[${cls.name}] ${at} names '${target.name}', which is not registered as an artifact | Decorate it (@service, @repository, ...) or register it on the application before it is injected`,
|
|
39
|
-
extra: { unregisteredClass: target.name },
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
// A same-name class records the same derived key (a library's own copy of a class the application registers): resolving it would hand back the OTHER class without a word.
|
|
43
|
-
const bound = this.getBinding({ key: recorded })?.boundClass();
|
|
44
|
-
if (bound && bound !== target && !(bound.prototype instanceof target)) {
|
|
45
|
-
throw getError({
|
|
46
|
-
message: `[${cls.name}] ${at} names '${target.name}', but '${String(recorded)}' is bound to a different class named '${bound.name}' | Two classes share one name and so one key - rename one, re-export the registered one, or inject by key`,
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
return recorded;
|
|
50
|
-
}
|
|
51
8
|
instantiate(cls) {
|
|
52
9
|
const registry = this.getMetadataRegistry();
|
|
53
10
|
const injectMetadata = registry.getInjectMetadata({
|
|
@@ -66,7 +23,7 @@ export class Container extends BaseContainer {
|
|
|
66
23
|
key: meta.key,
|
|
67
24
|
target: meta.target,
|
|
68
25
|
isOptional,
|
|
69
|
-
cls,
|
|
26
|
+
owner: cls.name,
|
|
70
27
|
at: `Constructor parameter ${index}`,
|
|
71
28
|
});
|
|
72
29
|
args[meta.index] = key === undefined ? undefined : this.get({ key, isOptional });
|
|
@@ -90,7 +47,7 @@ export class Container extends BaseContainer {
|
|
|
90
47
|
key: metadata.bindingKey,
|
|
91
48
|
target: metadata.target,
|
|
92
49
|
isOptional,
|
|
93
|
-
cls,
|
|
50
|
+
owner: cls.name,
|
|
94
51
|
at: `Property '${String(propertyKey)}'`,
|
|
95
52
|
});
|
|
96
53
|
const assigned = Reflect.set(instance, propertyKey, key === undefined ? undefined : this.get({ key, isOptional }));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"container.js","sourceRoot":"","sources":["../../../../src/modules/container/container.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"container.js","sourceRoot":"","sources":["../../../../src/modules/container/container.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEvC,qGAAqG;AACrG,MAAM,OAAO,SAAU,SAAQ,aAAa;IAC1C,YAAY,IAAwB;QAClC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,IAAI,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;IAClD,CAAC;IAEQ,WAAW,CAAI,GAAc;QACpC,MAAM,QAAQ,GAAG,IAAI,CAAC,mBAAmB,EAAE,CAAC;QAE5C,MAAM,cAAc,GAAG,QAAQ,CAAC,iBAAiB,CAAC;YAChD,MAAM,EAAE,GAAG;SACZ,CAAC,CAAC;QAEH,MAAM,IAAI,GAAc,EAAE,CAAC;QAE3B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,cAAc,EAAE,MAAM,IAAI,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;YACnE,MAAM,IAAI,GAAG,cAAc,EAAE,CAAC,KAAK,CAAC,CAAC;YAErC,IAAI,CAAC,IAAI,EAAE,CAAC;gBACV,MAAM,QAAQ,CAAC;oBACb,OAAO,EAAE,IAAI,GAAG,CAAC,IAAI,2BAA2B,KAAK,wIAAwI;iBAC9L,CAAC,CAAC;YACL,CAAC;YAED,MAAM,UAAU,GAAG,IAAI,CAAC,UAAU,IAAI,KAAK,CAAC;YAC5C,MAAM,GAAG,GAAG,IAAI,C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M,CAAC,WAAW,CAAC,GAAG;aACxC,CAAC,CAAC;YAEH,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAC1B,QAAQ,EACR,WAAW,EACX,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,CAC9D,CAAC;YAEF,8FAA8F;YAC9F,IAAI,CAAC,QAAQ,EAAE,CAAC;gBACd,MAAM,QAAQ,CAAC;oBACb,OAAO,EAAE,IAAI,GAAG,CAAC,IAAI,eAAe,MAAM,CAAC,WAAW,CAAC,wCAAwC;iBAChG,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF","sourcesContent":["import { getError } from '../error';\nimport { type TClass } from '@/common/types';\nimport { BaseContainer } from './base';\n\n/** Default container: constructor + property injection driven by the decorator metadata registry. */\nexport class Container extends BaseContainer {\n constructor(opts?: { scope: string }) {\n super({ scope: opts?.scope ?? Container.name });\n }\n\n override instantiate<T>(cls: TClass<T>): T {\n const registry = this.getMetadataRegistry();\n\n const injectMetadata = registry.getInjectMetadata({\n target: cls,\n });\n\n const args: unknown[] = [];\n\n for (let index = 0; index < (injectMetadata?.length ?? 0); index++) {\n const meta = injectMetadata?.[index];\n\n if (!meta) {\n throw getError({\n message: `[${cls.name}] Constructor parameter ${index} has no @inject | Every parameter of a container-instantiated class must be decorated - the container cannot supply an undecorated one`,\n });\n }\n\n const isOptional = meta.isOptional ?? false;\n const key = this.resolveBindingKey({\n key: meta.key,\n target: meta.target,\n isOptional,\n owner: cls.name,\n at: `Constructor parameter ${index}`,\n });\n\n args[meta.index] = key === undefined ? undefined : this.get({ key, isOptional });\n }\n\n // `TClass` takes `never[]`, so any constructor fits; Reflect.construct is how the arguments reach it.\n const instance: T = Reflect.construct(cls, args);\n\n // A constructor always answers an object (or a function); the guard narrows `T` to something `Reflect.set` takes.\n if ((typeof instance !== 'object' && typeof instance !== 'function') || instance === null) {\n return instance;\n }\n\n const propertyMetadata = registry.getPropertiesMetadata({\n target: instance,\n });\n\n if (!propertyMetadata?.size) {\n return instance;\n }\n\n const properties = propertyMetadata.entries();\n\n for (const [propertyKey, metadata] of properties) {\n const isOptional = metadata.isOptional ?? false;\n const key = this.resolveBindingKey({\n key: metadata.bindingKey,\n target: metadata.target,\n isOptional,\n owner: cls.name,\n at: `Property '${String(propertyKey)}'`,\n });\n\n const assigned = Reflect.set(\n instance,\n propertyKey,\n key === undefined ? undefined : this.get({ key, isOptional }),\n );\n\n // Assigning to a read-only property threw; Reflect.set only answers false, so keep the throw.\n if (!assigned) {\n throw getError({\n message: `[${cls.name}] Property '${String(propertyKey)}' cannot be injected | It is read-only`,\n });\n }\n }\n\n return instance;\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@venizia/ignis-inversion",
|
|
3
|
-
"version": "0.5.0-
|
|
3
|
+
"version": "0.5.0-2",
|
|
4
4
|
"description": "A small, standalone Dependency Injection and Inversion of Control container for TypeScript. Features decorator-based constructor and property injection, fluent Binding API, singleton/transient scoping, namespace auto-tagging, Provider pattern, tag-based discovery, and reflect-metadata integration. Zero framework lock-in — works standalone or with Ignis.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"architecture",
|
|
@@ -88,7 +88,7 @@
|
|
|
88
88
|
},
|
|
89
89
|
"devDependencies": {
|
|
90
90
|
"@types/bun": "^1.4.5",
|
|
91
|
-
"@venizia/dev-configs": "0.5.0-
|
|
91
|
+
"@venizia/dev-configs": "0.5.0-6",
|
|
92
92
|
"eslint": "^10.13.0",
|
|
93
93
|
"oxlint": "1.89.0",
|
|
94
94
|
"oxlint-tsgolint": "7.0.2004",
|