@venizia/ignis-inversion 0.4.7-4 → 0.4.7-5
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/common/types.d.ts +10 -14
- package/dist/esm/common/types.js.map +1 -1
- package/dist/esm/common/utilities.d.ts +3 -3
- package/dist/esm/common/utilities.js.map +1 -1
- package/dist/esm/modules/binding/binding.d.ts +2 -2
- package/dist/esm/modules/binding/binding.js.map +1 -1
- package/dist/esm/modules/binding/common/types.d.ts +2 -2
- package/dist/esm/modules/binding/common/types.js.map +1 -1
- package/dist/esm/modules/container/abstract.d.ts +1 -1
- package/dist/esm/modules/container/abstract.js.map +1 -1
- package/dist/esm/modules/container/base.d.ts +4 -1
- package/dist/esm/modules/container/base.js +3 -0
- package/dist/esm/modules/container/base.js.map +1 -1
- package/dist/esm/modules/container/common/types.d.ts +1 -1
- package/dist/esm/modules/container/common/types.js.map +1 -1
- package/dist/esm/modules/container/container.d.ts +2 -2
- package/dist/esm/modules/container/container.js +13 -3
- package/dist/esm/modules/container/container.js.map +1 -1
- package/dist/esm/modules/metadata/injectors.d.ts +1 -1
- package/dist/esm/modules/metadata/injectors.js.map +1 -1
- package/dist/esm/modules/registry/registry.d.ts +3 -3
- package/dist/esm/modules/registry/registry.js.map +1 -1
- package/package.json +2 -2
|
@@ -4,32 +4,28 @@ export type TNullable<T> = T | undefined | null;
|
|
|
4
4
|
export type ValueOrPromise<T> = T | Promise<T>;
|
|
5
5
|
/** Union of the property value types of `T`. */
|
|
6
6
|
export type ValueOf<T> = T[keyof T];
|
|
7
|
-
/**
|
|
8
|
-
export type
|
|
9
|
-
/** A concrete class constructor taking any arguments and producing `T`. */
|
|
10
|
-
export type TConstructor<T> = new (...args: any[]) => T;
|
|
7
|
+
/** A concrete class constructor producing `T`. Its arguments are `never[]`, so any constructor fits; build an instance with `Reflect.construct`. */
|
|
8
|
+
export type TConstructor<T> = new (...args: never[]) => T;
|
|
11
9
|
/** Like `TConstructor`, but also matches abstract classes. */
|
|
12
|
-
export type TAbstractConstructor<T> = abstract new (...args:
|
|
13
|
-
/** A `
|
|
14
|
-
export type TClass<T> = TConstructor<T
|
|
15
|
-
[property: string]: any;
|
|
16
|
-
};
|
|
10
|
+
export type TAbstractConstructor<T> = abstract new (...args: never[]) => T;
|
|
11
|
+
/** A class producing `T`. Read its static members through a type that declares them, such as `typeof MyClass`. */
|
|
12
|
+
export type TClass<T> = TConstructor<T>;
|
|
17
13
|
/** A function that returns `T` when called. */
|
|
18
|
-
export type TResolver<T> = (...args:
|
|
14
|
+
export type TResolver<T> = (...args: never[]) => T;
|
|
19
15
|
/** A function that returns `T` or a `Promise` of `T`. */
|
|
20
|
-
export type TAsyncResolver<T> = (...args:
|
|
16
|
+
export type TAsyncResolver<T> = (...args: never[]) => T | Promise<T>;
|
|
21
17
|
/** A `T` itself, or a function that returns it. */
|
|
22
18
|
export type TValueOrResolver<T> = T | TResolver<T>;
|
|
23
19
|
/** A `T` itself, or a function that returns it or a `Promise` of it. */
|
|
24
20
|
export type TValueOrAsyncResolver<T> = T | TAsyncResolver<T>;
|
|
25
21
|
/** What `@inject({ target })` names: the class, or a function returning it. The function form defers the reference to resolve time, which is how a class reached through an import cycle stays reachable. */
|
|
26
|
-
export type TInjectTarget = TValueOrResolver<TClass<
|
|
22
|
+
export type TInjectTarget = TValueOrResolver<TClass<unknown>>;
|
|
27
23
|
/** `& {}` keeps the literal set but stops it widening to `string` / `number` in mutable positions; see `TConstValue` in `@venizia/ignis-helpers`. */
|
|
28
|
-
export type TConstValue<T extends TClass<
|
|
24
|
+
export type TConstValue<T extends TClass<unknown>> = Extract<ValueOf<T>, string | number> & {};
|
|
29
25
|
/** Identifier for a binding: a string or a symbol. */
|
|
30
26
|
export type TBindingKey = string | symbol;
|
|
31
27
|
/** Free-form map of tag names to values. */
|
|
32
28
|
export interface IBindingTag {
|
|
33
|
-
[name: string]:
|
|
29
|
+
[name: string]: unknown;
|
|
34
30
|
}
|
|
35
31
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/common/types.ts"],"names":[],"mappings":"","sourcesContent":["/** A `T` that may also be `undefined` or `null`. */\nexport type TNullable<T> = T | undefined | null;\n\n/** A `T` or a `Promise` of it, for APIs that accept sync and async results. */\nexport type ValueOrPromise<T> = T | Promise<T>;\n/** Union of the property value types of `T`. */\nexport type ValueOf<T> = T[keyof T];\n\n/**
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/common/types.ts"],"names":[],"mappings":"","sourcesContent":["/** A `T` that may also be `undefined` or `null`. */\nexport type TNullable<T> = T | undefined | null;\n\n/** A `T` or a `Promise` of it, for APIs that accept sync and async results. */\nexport type ValueOrPromise<T> = T | Promise<T>;\n/** Union of the property value types of `T`. */\nexport type ValueOf<T> = T[keyof T];\n\n/** A concrete class constructor producing `T`. Its arguments are `never[]`, so any constructor fits; build an instance with `Reflect.construct`. */\nexport type TConstructor<T> = new (...args: never[]) => T;\n/** Like `TConstructor`, but also matches abstract classes. */\nexport type TAbstractConstructor<T> = abstract new (...args: never[]) => T;\n/** A class producing `T`. Read its static members through a type that declares them, such as `typeof MyClass`. */\nexport type TClass<T> = TConstructor<T>;\n\n/** A function that returns `T` when called. */\nexport type TResolver<T> = (...args: never[]) => T;\n/** A function that returns `T` or a `Promise` of `T`. */\nexport type TAsyncResolver<T> = (...args: never[]) => T | Promise<T>;\n/** A `T` itself, or a function that returns it. */\nexport type TValueOrResolver<T> = T | TResolver<T>;\n/** A `T` itself, or a function that returns it or a `Promise` of it. */\nexport type TValueOrAsyncResolver<T> = T | TAsyncResolver<T>;\n\n/** What `@inject({ target })` names: the class, or a function returning it. The function form defers the reference to resolve time, which is how a class reached through an import cycle stays reachable. */\nexport type TInjectTarget = TValueOrResolver<TClass<unknown>>;\n\n/** `& {}` keeps the literal set but stops it widening to `string` / `number` in mutable positions; see `TConstValue` in `@venizia/ignis-helpers`. */\nexport type TConstValue<T extends TClass<unknown>> = Extract<ValueOf<T>, string | number> & {};\n\n/** Identifier for a binding: a string or a symbol. */\nexport type TBindingKey = string | symbol;\n\n/** Free-form map of tag names to values. */\nexport interface IBindingTag {\n [name: string]: unknown;\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { TClass, TInjectTarget, TValueOrAsyncResolver, TValueOrResolver } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Object helpers. Hand-written rather than `lodash/isEmpty` and `lodash/omit`: this package is bundled
|
|
4
4
|
* for browsers, where importing lodash for two functions costs 24 KB.
|
|
@@ -25,7 +25,7 @@ export declare class ObjectUtility {
|
|
|
25
25
|
}): Omit<T, Extract<K, keyof T>>;
|
|
26
26
|
}
|
|
27
27
|
/** Tells a CONSTRUCTOR from a RESOLVER via source text - `prototype !== undefined` is true of every non-arrow function. Sound only on ES2020+ output (classes emit as `class`); ES5 bundling breaks it. */
|
|
28
|
-
export declare const isClass: <T>(target:
|
|
28
|
+
export declare const isClass: <T>(target: unknown) => target is TClass<T>;
|
|
29
29
|
/** Readers for the value-or-resolver vocabulary the container branches on. */
|
|
30
30
|
export declare class ResolverUtility {
|
|
31
31
|
/** A resolver is anything callable that is not a class - a class is a value here, never a factory. */
|
|
@@ -42,6 +42,6 @@ export declare class ResolverUtility {
|
|
|
42
42
|
/** Reads what `@inject({ target })` named. The function form is called HERE, never at decoration: that is what lets a class reached through an import cycle be named at all. Answers `undefined` when the function hands back something that is not a class. The container resolves synchronously, so there is no async form. */
|
|
43
43
|
static resolveInjectTarget(opts: {
|
|
44
44
|
target: TInjectTarget;
|
|
45
|
-
}): TClass<
|
|
45
|
+
}): TClass<unknown> | undefined;
|
|
46
46
|
}
|
|
47
47
|
//# sourceMappingURL=utilities.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utilities.js","sourceRoot":"","sources":["../../../src/common/utilities.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utilities.js","sourceRoot":"","sources":["../../../src/common/utilities.ts"],"names":[],"mappings":"AASA;;;GAGG;AACH,MAAM,OAAO,aAAa;IACxB;;;;;;;OAOG;IACH,MAAM,CAAC,OAAO,CAAC,IAAwB;QACrC,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;QAEvB,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YAC1C,OAAO,IAAI,CAAC;QACd,CAAC;QAED,wFAAwF;QACxF,8DAA8D;QAC9D,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;YACnF,OAAQ,KAA4B,CAAC,MAAM,KAAK,CAAC,CAAC;QACpD,CAAC;QAED,IAAI,KAAK,YAAY,GAAG,IAAI,KAAK,YAAY,GAAG,EAAE,CAAC;YACjD,OAAO,KAAK,CAAC,IAAI,KAAK,CAAC,CAAC;QAC1B,CAAC;QAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,OAAO,IAAI,CAAC;QACd,CAAC;QAED,gGAAgG;QAChG,sBAAsB;QACtB,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC;IACzC,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,IAAI,CAA0C,IAGpD;QACC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,IAAI,CAAC;QAC9B,MAAM,OAAO,GAAG,IAAI,GAAG,CAAc,IAAI,CAAC,CAAC;QAC3C,MAAM,IAAI,GAA4B,EAAE,CAAC;QAEzC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QAEvC,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,O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,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IAClE,CAAC;IAED,iUAAiU;IACjU,MAAM,CAAC,mBAAmB,CAAC,IAA+B;QACxD,MAAM,QAAQ,GAAG,eAAe,CAAC,YAAY,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;QAEtE,OAAO,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;IAClD,CAAC;CACF","sourcesContent":["import type {\n TAsyncResolver,\n TClass,\n TInjectTarget,\n TResolver,\n TValueOrAsyncResolver,\n TValueOrResolver,\n} from './types';\n\n/**\n * Object helpers. Hand-written rather than `lodash/isEmpty` and `lodash/omit`: this package is bundled\n * for browsers, where importing lodash for two functions costs 24 KB.\n */\nexport class ObjectUtility {\n /**\n * True for a value carrying nothing: `null`/`undefined`, an empty string, array, `Map`, `Set`, or an\n * object with no own enumerable keys. A number, boolean or function is empty too - it holds no\n * entries.\n *\n * NOT a plain `!value`: `!{}` is `false` while an empty object IS empty, so the two disagree on\n * exactly the case a swap would silently flip.\n */\n static isEmpty(opts: { value: unknown }): boolean {\n const { value } = opts;\n\n if (value === null || value === undefined) {\n return true;\n }\n\n // The types that carry a MEANINGFUL length. Duck-typing on `.length` instead would call\n // `{ length: 0 }` empty, which it is not - it has an own key.\n if (typeof value === 'string' || Array.isArray(value) || ArrayBuffer.isView(value)) {\n return (value as { length: number }).length === 0;\n }\n\n if (value instanceof Map || value instanceof Set) {\n return value.size === 0;\n }\n\n if (typeof value !== 'object') {\n return true;\n }\n\n // Own keys only: `for...in` would walk the prototype chain and call an object with an inherited\n // property non-empty.\n return Object.keys(value).length === 0;\n }\n\n /**\n * A shallow copy without `keys`. Matches lodash on the shapes we pass it - a flat key list, never a\n * nested path.\n */\n static omit<T extends object, K extends PropertyKey>(opts: {\n source: T;\n keys: readonly K[];\n }): Omit<T, Extract<K, keyof T>> {\n const { source, keys } = opts;\n const dropped = new Set<PropertyKey>(keys);\n const kept: Record<string, unknown> = {};\n\n const entries = Object.entries(source);\n\n for (const [key, value] of entries) {\n if (dropped.has(key)) {\n continue;\n }\n\n kept[key] = value;\n }\n\n return kept as Omit<T, Extract<K, keyof T>>;\n }\n}\n\n/** Tells a CONSTRUCTOR from a RESOLVER via source text - `prototype !== undefined` is true of every non-arrow function. Sound only on ES2020+ output (classes emit as `class`); ES5 bundling breaks it. */\nexport const isClass = <T>(target: unknown): target is TClass<T> => {\n if (typeof target !== 'function' || target.prototype === undefined) {\n return false;\n }\n\n return /^class[\\s{]/.test(Function.prototype.toString.call(target));\n};\n\n/** Readers for the value-or-resolver vocabulary the container branches on. */\nexport class ResolverUtility {\n /** A resolver is anything callable that is not a class - a class is a value here, never a factory. */\n private static isResolver<T>(\n valueOrResolver: TValueOrResolver<T>,\n ): valueOrResolver is TResolver<T> {\n return typeof valueOrResolver === 'function' && !isClass(valueOrResolver);\n }\n\n private static isAsyncResolver<T>(\n valueOrResolver: TValueOrAsyncResolver<T>,\n ): valueOrResolver is TAsyncResolver<T> {\n return typeof valueOrResolver === 'function' && !isClass(valueOrResolver);\n }\n\n /** Reads a value-or-resolver. A class constructor is returned as-is; only a plain function is called. */\n static resolveValue<T>(opts: { value: TValueOrResolver<T> }): T {\n const { value } = opts;\n\n return ResolverUtility.isResolver(value) ? value() : value;\n }\n\n /** `resolveValue` for a resolver that may answer a promise. */\n static async resolveValueAsync<T>(opts: { value: TValueOrAsyncResolver<T> }): Promise<T> {\n const { value } = opts;\n\n return ResolverUtility.isAsyncResolver(value) ? value() : value;\n }\n\n /** Reads what `@inject({ target })` named. The function form is called HERE, never at decoration: that is what lets a class reached through an import cycle be named at all. Answers `undefined` when the function hands back something that is not a class. The container resolves synchronously, so there is no async form. */\n static resolveInjectTarget(opts: { target: TInjectTarget }): TClass<unknown> | undefined {\n const resolved = ResolverUtility.resolveValue({ value: opts.target });\n\n return isClass(resolved) ? resolved : undefined;\n }\n}\n"]}
|
|
@@ -4,7 +4,7 @@ import type { IContainer } from '../container/common/types.js';
|
|
|
4
4
|
import { BindingValueTypes, type TBindingScope } from './common/constants.js';
|
|
5
5
|
import { type IBinding, type TBindingProvider, type TBindingResolverValue } from './common/types.js';
|
|
6
6
|
/** One container entry under `key`, resolved to a class, a value or a provider (set with `toClass`, `toValue`, `toProvider`). Scope is transient unless `setScope` selects singleton; a key with a dot gets its first segment as a tag. */
|
|
7
|
-
export declare class Binding<T =
|
|
7
|
+
export declare class Binding<T = unknown> extends BaseHelper implements IBinding<T> {
|
|
8
8
|
key: string;
|
|
9
9
|
private bindScope;
|
|
10
10
|
private tags;
|
|
@@ -14,7 +14,7 @@ export declare class Binding<T = any> extends BaseHelper implements IBinding<T>
|
|
|
14
14
|
constructor(opts: {
|
|
15
15
|
key: string;
|
|
16
16
|
});
|
|
17
|
-
static bind<T =
|
|
17
|
+
static bind<T = unknown>(opts: {
|
|
18
18
|
key: string;
|
|
19
19
|
}): Binding<T>;
|
|
20
20
|
toClass(value: TClass<T>): this;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"binding.js","sourceRoot":"","sources":["../../../../src/modules/binding/binding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGlD,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAsB,MAAM,oBAAoB,CAAC;AAC1F,OAAO,EAEL,eAAe,GAGhB,MAAM,gBAAgB,CAAC;AAExB,2OAA2O;AAC3O,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"binding.js","sourceRoot":"","sources":["../../../../src/modules/binding/binding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAGlD,OAAO,EAAE,aAAa,EAAE,iBA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ChH,CAAC,CAAC;gBACL,CAAC;gBAED,QAAQ,GAAG,SAAS,CAAC,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;gBACtD,MAAM;YACR,CAAC;YACD,OAAO,CAAC,CAAC,CAAC;gBACR,MAAM,QAAQ,CAAC;oBACb,OAAO,EAAE,8CAA8C,YAAY,EAAE;iBACtE,CAAC,CAAC;YACL,CAAC;QACH,CAAC;QAED,IAAI,IAAI,CAAC,SAAS,KAAK,aAAa,CAAC,SAAS,EAAE,CAAC;YAC/C,IAAI,CAAC,MAAM,GAAG,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC;QACpC,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,UAAU;QACR,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;IAC1B,CAAC;CACF","sourcesContent":["import { getError } from '../error';\nimport { BaseHelper } from '@/common/base-helper';\nimport { type TClass, type TConstValue } from '@/common/types';\nimport type { IContainer } from '../container/common/types';\nimport { BindingScopes, BindingValueTypes, type TBindingScope } from './common/constants';\nimport {\n type IBinding,\n isClassProvider,\n type TBindingProvider,\n type TBindingResolverValue,\n} from './common/types';\n\n/** One container entry under `key`, resolved to a class, a value or a provider (set with `toClass`, `toValue`, `toProvider`). Scope is transient unless `setScope` selects singleton; a key with a dot gets its first segment as a tag. */\nexport class Binding<T = unknown> extends BaseHelper implements IBinding<T> {\n key: string;\n\n private bindScope: TBindingScope = BindingScopes.TRANSIENT;\n private tags: Set<string>;\n /** Boxed: a singleton may legitimately be `null`, `false`, `0`, `''` or `undefined`. */\n private cached?: { value: T };\n\n private resolver:\n | { type: typeof BindingValueTypes.CLASS; value: TClass<T> }\n | { type: typeof BindingValueTypes.VALUE; value: T }\n | { type: typeof BindingValueTypes.PROVIDER; value: TBindingProvider<T> };\n\n constructor(opts: { key: string }) {\n super({ scope: opts.key });\n this.tags = new Set([]);\n\n this.key = opts.key;\n\n const keyParts = this.key.split('.');\n\n if (keyParts.length > 1) {\n const [namespace] = keyParts;\n this.setTags(namespace);\n }\n }\n\n static override bind<T = unknown>(opts: { key: string }): Binding<T> {\n return new Binding<T>(opts);\n }\n\n toClass(value: TClass<T>): this {\n this.resolver = { type: BindingValueTypes.CLASS, value };\n\n return this;\n }\n\n toValue(value: T): this {\n this.resolver = { type: BindingValueTypes.VALUE, value };\n\n return this;\n }\n\n toProvider(value: TBindingProvider<T>): this {\n this.resolver = { type: BindingValueTypes.PROVIDER, value };\n\n return this;\n }\n\n /** The class a `toClass` binding instantiates; `undefined` for a value or a provider. */\n boundClass(): TClass<T> | undefined {\n return this.resolver.type === BindingValueTypes.CLASS ? this.resolver.value : undefined;\n }\n\n getBindingMeta(opts: { type: TConstValue<typeof BindingValueTypes> }): TBindingResolverValue<T> {\n if (this.resolver.type !== opts.type) {\n throw getError({\n message: `[getBindingMeta] Invalid resolver type, only ${this.resolver.type} is allowd | resolverType: ${this.resolver.type} | optType: ${opts.type}`,\n });\n }\n\n return this.resolver.value;\n }\n\n setScope(scope: TBindingScope): this {\n this.bindScope = scope;\n\n return this;\n }\n\n setTags(...tags: string[]): this {\n tags.forEach((t) => this.tags.add(t));\n\n return this;\n }\n\n hasTag(tag: string): boolean {\n return this.tags.has(tag);\n }\n\n getTags(): string[] {\n return Array.from(this.tags);\n }\n\n getScope(): TBindingScope {\n return this.bindScope;\n }\n\n /** True when `getValue` answers without building anything: a cached singleton or a plain value. Nothing it reads can form a cycle. */\n isSettled(): boolean {\n return (\n (this.bindScope === BindingScopes.SINGLETON && this.cached !== undefined) ||\n this.resolver.type === BindingValueTypes.VALUE\n );\n }\n\n getValue(container?: IContainer): T {\n if (this.bindScope === BindingScopes.SINGLETON && this.cached) {\n return this.cached.value;\n }\n\n let instance: T;\n\n const { type: resolverType } = this.resolver;\n\n switch (resolverType) {\n case BindingValueTypes.VALUE: {\n instance = this.resolver.value;\n break;\n }\n case BindingValueTypes.PROVIDER: {\n const provider = this.resolver.value;\n\n if (!container) {\n throw getError({\n message: `[getValue] Invalid context/container to get provider value | type: ${resolverType} | key: ${this.key}`,\n });\n }\n\n if (!isClassProvider(provider)) {\n instance = provider(container);\n break;\n }\n\n const p = container.instantiate(provider);\n instance = p.value(container);\n break;\n }\n case BindingValueTypes.CLASS: {\n if (!container) {\n throw getError({\n message: `[getValue] Invalid context/container to instantiate class | type: ${resolverType} | key: ${this.key}`,\n });\n }\n\n instance = container.instantiate(this.resolver.value);\n break;\n }\n default: {\n throw getError({\n message: `[getValue] Invalid value type | valueType: ${resolverType}`,\n });\n }\n }\n\n if (this.bindScope === BindingScopes.SINGLETON) {\n this.cached = { value: instance };\n }\n\n return instance;\n }\n\n clearCache() {\n this.cached = undefined;\n }\n}\n"]}
|
|
@@ -6,13 +6,13 @@ export interface IProvider<T> {
|
|
|
6
6
|
value(container: IContainer): T;
|
|
7
7
|
}
|
|
8
8
|
/** True for a function whose prototype has a `value` method, so a provider class is told apart from a plain provider function. */
|
|
9
|
-
export declare const isClassProvider: <T>(target:
|
|
9
|
+
export declare const isClassProvider: <T>(target: unknown) => target is TClass<IProvider<T>>;
|
|
10
10
|
/** What `toProvider` accepts: a function taking the container, or a class implementing {@link IProvider}. */
|
|
11
11
|
export type TBindingProvider<T> = (<C extends IContainer>(container: C) => T) | TClass<IProvider<T>>;
|
|
12
12
|
/** What a binding's resolver holds - a class, a plain value, or a provider. */
|
|
13
13
|
export type TBindingResolverValue<T> = TClass<T> | T | TBindingProvider<T>;
|
|
14
14
|
/** A container entry: one key bound to a class, a value or a provider, with a scope and tags. */
|
|
15
|
-
export interface IBinding<T =
|
|
15
|
+
export interface IBinding<T = unknown> {
|
|
16
16
|
/** Normalized at construction - a symbol key arrives here as its string form. */
|
|
17
17
|
key: string;
|
|
18
18
|
toClass(value: TClass<T>): this;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/modules/binding/common/types.ts"],"names":[],"mappings":"AASA,kIAAkI;AAClI,MAAM,CAAC,MAAM,eAAe,GAAG,CAAI,
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/modules/binding/common/types.ts"],"names":[],"mappings":"AASA,kIAAkI;AAClI,MAAM,CAAC,MAAM,eAAe,GAAG,CAAI,MAAe,EAAkC,EAAE;IACpF,OAAO,CACL,OAAO,MAAM,KAAK,UAAU,IAAI,MAAM,CAAC,SAAS,IAAI,OAAO,MAAM,CAAC,SAAS,CAAC,KAAK,KAAK,UAAU,CACjG,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import type { IContainer } from '../../container/common/types';\nimport { type TClass, type TConstValue } from '@/common/types';\nimport type { BindingValueTypes, TBindingScope } from './constants';\n\n/** A value source resolved against a container; `value` runs on each resolution of the binding it backs. */\nexport interface IProvider<T> {\n value(container: IContainer): T;\n}\n\n/** True for a function whose prototype has a `value` method, so a provider class is told apart from a plain provider function. */\nexport const isClassProvider = <T>(target: unknown): target is TClass<IProvider<T>> => {\n return (\n typeof target === 'function' && target.prototype && typeof target.prototype.value === 'function'\n );\n};\n\n/** What `toProvider` accepts: a function taking the container, or a class implementing {@link IProvider}. */\nexport type TBindingProvider<T> =\n | (<C extends IContainer>(container: C) => T)\n | TClass<IProvider<T>>;\n\n/** What a binding's resolver holds - a class, a plain value, or a provider. */\nexport type TBindingResolverValue<T> = TClass<T> | T | TBindingProvider<T>;\n\n/** A container entry: one key bound to a class, a value or a provider, with a scope and tags. */\nexport interface IBinding<T = unknown> {\n /** Normalized at construction - a symbol key arrives here as its string form. */\n key: string;\n\n toClass(value: TClass<T>): this;\n toValue(value: T): this;\n toProvider(value: TBindingProvider<T>): this;\n getBindingMeta(opts: { type: TConstValue<typeof BindingValueTypes> }): TBindingResolverValue<T>;\n\n setScope(scope: TBindingScope): this;\n getScope(): TBindingScope;\n setTags(...tags: string[]): this;\n hasTag(tag: string): boolean;\n getTags(): string[];\n\n getValue(container?: IContainer): T;\n clearCache(): void;\n}\n"]}
|
|
@@ -56,7 +56,7 @@ export declare abstract class AbstractContainer extends BaseHelper implements IC
|
|
|
56
56
|
};
|
|
57
57
|
abstract resolve<T>(cls: TClass<T>): T;
|
|
58
58
|
abstract instantiate<T>(cls: TClass<T>): T;
|
|
59
|
-
abstract findByTag<T =
|
|
59
|
+
abstract findByTag<T = unknown>(opts: {
|
|
60
60
|
tag: string;
|
|
61
61
|
exclude?: Array<string> | Set<string>;
|
|
62
62
|
}): IBinding<T>[];
|
|
@@ -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;CA0CF","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 }): IBinding<T>;\n abstract isBound(opts: { key: TBindingKey }): boolean;\n abstract getBinding<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n }): TNullable<IBinding<T>>;\n abstract unbind(opts: { key: TBindingKey }): boolean;\n abstract set<T>(opts: { binding: IBinding<T> }): void;\n\n abstract get<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: false;\n }): T;\n abstract get<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: boolean;\n }): T | undefined;\n abstract gets<T extends unknown[]>(opts: {\n bindings: {\n [K in keyof T]: {\n key: TBindingKey | { namespace: string; key: string };\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 =
|
|
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;CA0CF","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 }): IBinding<T>;\n abstract isBound(opts: { key: TBindingKey }): boolean;\n abstract getBinding<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n }): TNullable<IBinding<T>>;\n abstract unbind(opts: { key: TBindingKey }): boolean;\n abstract set<T>(opts: { binding: IBinding<T> }): void;\n\n abstract get<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: false;\n }): T;\n abstract get<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: boolean;\n }): T | undefined;\n abstract gets<T extends unknown[]>(opts: {\n bindings: {\n [K in keyof T]: {\n key: TBindingKey | { namespace: string; key: string };\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"]}
|
|
@@ -3,6 +3,9 @@ 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. */
|
|
5
5
|
export declare abstract class BaseContainer extends AbstractContainer {
|
|
6
|
+
/**
|
|
7
|
+
* 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.
|
|
8
|
+
*/
|
|
6
9
|
protected bindings: Map<string, Binding<any>>;
|
|
7
10
|
/** How often each bound key has been handed out since counting started. */
|
|
8
11
|
protected resolutionCounts: Map<string, number>;
|
|
@@ -96,7 +99,7 @@ export declare abstract class BaseContainer extends AbstractContainer {
|
|
|
96
99
|
/** 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. */
|
|
97
100
|
getResolutionCounts(): ReadonlyMap<string, number>;
|
|
98
101
|
resolve<T>(cls: TClass<T>): T;
|
|
99
|
-
findByTag<T =
|
|
102
|
+
findByTag<T = unknown>(opts: {
|
|
100
103
|
tag: string;
|
|
101
104
|
exclude?: Array<string> | Set<string>;
|
|
102
105
|
}): Binding<T>[];
|
|
@@ -9,6 +9,9 @@ const CYCLE_CHECK_DEPTH = 64;
|
|
|
9
9
|
export class BaseContainer extends AbstractContainer {
|
|
10
10
|
constructor(opts) {
|
|
11
11
|
super({ scope: opts?.scope ?? BaseContainer.name });
|
|
12
|
+
/**
|
|
13
|
+
* 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.
|
|
14
|
+
*/
|
|
12
15
|
this.bindings = new Map();
|
|
13
16
|
/** How often each bound key has been handed out since counting started. */
|
|
14
17
|
this.resolutionCounts = new Map();
|
|
@@ -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,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC1D,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;IA+B3D,YAAY,IAAwB;QAClC,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,IAAI,aAAa,CAAC,IAAI,EAAE,CAAC,CAAC;QA/B5C,aAAQ,GAAG,IAAI,GAAG,EAAmB,CAAC;QAEhD,2EAA2E;QACjE,qBAAgB,GAAG,IAAI,GAAG,EAAkB,CAAC;QAEvD;;;;;WAKG;QACO,0BAAqB,GAAG,KAAK,CAAC;QAExC,2JAA2J;QACjJ,iCAA4B,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AAC;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;QACtB,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;QACxB,IAAI,CAAC,uBAAuB,CAAC,KAAK,EAAE,CAAC;IACvC,CAAC;CACF","sourcesContent":["import { type TBindingKey, type TClass, type TNullable } from '@/common/types';\nimport { Binding } from '../binding/binding';\nimport { BindingKeys } from '../binding/common/constants';\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 protected bindings = new Map<string, Binding>();\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 /** The string each BOUND symbol is stored under: `Symbol('x')` twice is two keys, and both print as \"Symbol(x)\". */\n protected symbolKeys = new Map<symbol, string>();\n\n /** How many symbols have been bound per description, so a new one gets its suffix in one step. */\n protected symbolDescriptionCounts = new Map<string, number>();\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 /**\n * The string a key is stored under, or `undefined` for a symbol that was never bound - a lookup\n * never records one, so reading with throwaway symbols leaves nothing behind.\n */\n protected toKeyString(opts: {\n key: TBindingKey | { namespace: string; key: string };\n }): string | undefined {\n const { key } = opts;\n\n switch (typeof key) {\n case 'string': {\n return key;\n }\n case 'symbol': {\n return this.symbolKeys.get(key);\n }\n case 'object': {\n return BindingKeys.build(key);\n }\n default: {\n throw getError({\n message: `[getBinding] Invalid binding key type | opts: ${String(key)} | allowed: [string, symbol, { namespace: string, key: string }]`,\n });\n }\n }\n }\n\n /** A bound symbol's own string: its description, suffixed (`Symbol(x)#2`) when another symbol holds it. */\n protected bindSymbolKey(opts: { key: symbol }): string {\n const { key } = opts;\n\n const known = this.symbolKeys.get(key);\n\n if (known !== undefined) {\n return known;\n }\n\n const description = key.toString();\n const count = (this.symbolDescriptionCounts.get(description) ?? 0) + 1;\n this.symbolDescriptionCounts.set(description, count);\n\n const keyStr = count === 1 ? description : `${description}#${count}`;\n this.symbolKeys.set(key, keyStr);\n\n return keyStr;\n }\n\n override bind<T>(opts: { key: TBindingKey }): Binding<T> {\n const { key } = opts;\n const keyStr = typeof key === 'symbol' ? this.bindSymbolKey({ key }) : key;\n const binding = new Binding<T>({ key: keyStr });\n this.bindings.set(keyStr, binding as Binding);\n\n return binding;\n }\n\n override isBound(opts: { key: TBindingKey }): boolean {\n const keyStr = this.toKeyString({ key: opts.key });\n\n return keyStr !== undefined && this.bindings.has(keyStr);\n }\n\n override getBinding<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n }): TNullable<Binding<T>> {\n const keyStr = this.toKeyString({ key: opts.key });\n\n return keyStr === undefined ? undefined : this.bindings.get(keyStr);\n }\n\n override unbind(opts: { key: TBindingKey }): boolean {\n const { key } = opts;\n const keyStr = this.toKeyString({ key });\n\n if (typeof key === 'symbol') {\n this.symbolKeys.delete(key);\n }\n\n return keyStr !== undefined && this.bindings.delete(keyStr);\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: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: false;\n }): T;\n override get<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: boolean;\n }): T | undefined;\n override get<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: boolean;\n }): T | undefined {\n const { key, isOptional = false } = opts;\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 const printed = typeof key === 'object' ? BindingKeys.build(key) : String(key);\n\n throw getError({\n message: `Binding key: ${printed} is not bounded in context!`,\n extra: { unboundKey: printed },\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 | { namespace: string; key: string };\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 = any>(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 this.symbolKeys.clear();\n this.symbolDescriptionCounts.clear();\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../../../src/modules/container/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AA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CAAc,IAG/B;QACC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;QAE9B,MAAM,EAAE,GAAiB,EAAE,CAAC;QAE5B,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC;QAExC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC/B,IAAI,CAAC,OAAO,CAAC,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;QACtB,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;QACxB,IAAI,CAAC,uBAAuB,CAAC,KAAK,EAAE,CAAC;IACvC,CAAC;CACF","sourcesContent":["import { type TBindingKey, type TClass, type TNullable } from '@/common/types';\nimport { Binding } from '../binding/binding';\nimport { BindingKeys } from '../binding/common/constants';\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 /** The string each BOUND symbol is stored under: `Symbol('x')` twice is two keys, and both print as \"Symbol(x)\". */\n protected symbolKeys = new Map<symbol, string>();\n\n /** How many symbols have been bound per description, so a new one gets its suffix in one step. */\n protected symbolDescriptionCounts = new Map<string, number>();\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 /**\n * The string a key is stored under, or `undefined` for a symbol that was never bound - a lookup\n * never records one, so reading with throwaway symbols leaves nothing behind.\n */\n protected toKeyString(opts: {\n key: TBindingKey | { namespace: string; key: string };\n }): string | undefined {\n const { key } = opts;\n\n switch (typeof key) {\n case 'string': {\n return key;\n }\n case 'symbol': {\n return this.symbolKeys.get(key);\n }\n case 'object': {\n return BindingKeys.build(key);\n }\n default: {\n throw getError({\n message: `[getBinding] Invalid binding key type | opts: ${String(key)} | allowed: [string, symbol, { namespace: string, key: string }]`,\n });\n }\n }\n }\n\n /** A bound symbol's own string: its description, suffixed (`Symbol(x)#2`) when another symbol holds it. */\n protected bindSymbolKey(opts: { key: symbol }): string {\n const { key } = opts;\n\n const known = this.symbolKeys.get(key);\n\n if (known !== undefined) {\n return known;\n }\n\n const description = key.toString();\n const count = (this.symbolDescriptionCounts.get(description) ?? 0) + 1;\n this.symbolDescriptionCounts.set(description, count);\n\n const keyStr = count === 1 ? description : `${description}#${count}`;\n this.symbolKeys.set(key, keyStr);\n\n return keyStr;\n }\n\n override bind<T>(opts: { key: TBindingKey }): Binding<T> {\n const { key } = opts;\n const keyStr = typeof key === 'symbol' ? this.bindSymbolKey({ key }) : key;\n const binding = new Binding<T>({ key: keyStr });\n this.bindings.set(keyStr, binding);\n\n return binding;\n }\n\n override isBound(opts: { key: TBindingKey }): boolean {\n const keyStr = this.toKeyString({ key: opts.key });\n\n return keyStr !== undefined && this.bindings.has(keyStr);\n }\n\n override getBinding<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n }): TNullable<Binding<T>> {\n const keyStr = this.toKeyString({ key: opts.key });\n\n return keyStr === undefined ? undefined : this.bindings.get(keyStr);\n }\n\n override unbind(opts: { key: TBindingKey }): boolean {\n const { key } = opts;\n const keyStr = this.toKeyString({ key });\n\n if (typeof key === 'symbol') {\n this.symbolKeys.delete(key);\n }\n\n return keyStr !== undefined && this.bindings.delete(keyStr);\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: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: false;\n }): T;\n override get<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: boolean;\n }): T | undefined;\n override get<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: boolean;\n }): T | undefined {\n const { key, isOptional = false } = opts;\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 const printed = typeof key === 'object' ? BindingKeys.build(key) : String(key);\n\n throw getError({\n message: `Binding key: ${printed} is not bounded in context!`,\n extra: { unboundKey: printed },\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 | { namespace: string; key: string };\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 this.symbolKeys.clear();\n this.symbolDescriptionCounts.clear();\n }\n}\n"]}
|
|
@@ -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 bind<T>(opts: { key: TBindingKey }): IBinding<T>;\n isBound(opts: { key: TBindingKey }): boolean;\n getBinding<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n }): TNullable<IBinding<T>>;\n unbind(opts: { key: TBindingKey }): boolean;\n set<T>(opts: { binding: IBinding<T> }): void;\n\n get<T>(opts: { key: TBindingKey | { namespace: string; key: string }; isOptional?: false }): T;\n get<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: boolean;\n }): T | undefined;\n gets<T extends unknown[]>(opts: {\n bindings: {\n [K in keyof T]: {\n key: TBindingKey | { namespace: string; key: string };\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 =
|
|
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 bind<T>(opts: { key: TBindingKey }): IBinding<T>;\n isBound(opts: { key: TBindingKey }): boolean;\n getBinding<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n }): TNullable<IBinding<T>>;\n unbind(opts: { key: TBindingKey }): boolean;\n set<T>(opts: { binding: IBinding<T> }): void;\n\n get<T>(opts: { key: TBindingKey | { namespace: string; key: string }; isOptional?: false }): T;\n get<T>(opts: {\n key: TBindingKey | { namespace: string; key: string };\n isOptional?: boolean;\n }): T | undefined;\n gets<T extends unknown[]>(opts: {\n bindings: {\n [K in keyof T]: {\n key: TBindingKey | { namespace: string; key: string };\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,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type TBindingKey, type TClass, type TInjectTarget } 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 {
|
|
@@ -16,7 +16,7 @@ export declare class Container extends BaseContainer {
|
|
|
16
16
|
key?: TBindingKey;
|
|
17
17
|
target?: TInjectTarget;
|
|
18
18
|
isOptional?: boolean;
|
|
19
|
-
cls: TClass<
|
|
19
|
+
cls: TClass<unknown>;
|
|
20
20
|
at: string;
|
|
21
21
|
}): TBindingKey | undefined;
|
|
22
22
|
instantiate<T>(cls: TClass<T>): T;
|
|
@@ -71,7 +71,12 @@ export class Container extends BaseContainer {
|
|
|
71
71
|
});
|
|
72
72
|
args[meta.index] = key === undefined ? undefined : this.get({ key, isOptional });
|
|
73
73
|
}
|
|
74
|
-
|
|
74
|
+
// `TClass` takes `never[]`, so any constructor fits; Reflect.construct is how the arguments reach it.
|
|
75
|
+
const instance = Reflect.construct(cls, args);
|
|
76
|
+
// A constructor always answers an object (or a function); the guard narrows `T` to something `Reflect.set` takes.
|
|
77
|
+
if ((typeof instance !== 'object' && typeof instance !== 'function') || instance === null) {
|
|
78
|
+
return instance;
|
|
79
|
+
}
|
|
75
80
|
const propertyMetadata = registry.getPropertiesMetadata({
|
|
76
81
|
target: instance,
|
|
77
82
|
});
|
|
@@ -88,8 +93,13 @@ export class Container extends BaseContainer {
|
|
|
88
93
|
cls,
|
|
89
94
|
at: `Property '${String(propertyKey)}'`,
|
|
90
95
|
});
|
|
91
|
-
instance
|
|
92
|
-
|
|
96
|
+
const assigned = Reflect.set(instance, propertyKey, key === undefined ? undefined : this.get({ key, isOptional }));
|
|
97
|
+
// Assigning to a read-only property threw; Reflect.set only answers false, so keep the throw.
|
|
98
|
+
if (!assigned) {
|
|
99
|
+
throw getError({
|
|
100
|
+
message: `[${cls.name}] Property '${String(propertyKey)}' cannot be injected | It is read-only`,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
93
103
|
}
|
|
94
104
|
return instance;
|
|
95
105
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"container.js","sourceRoot":"","sources":["../../../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AC,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 TBindingKey, type TClass, type TInjectTarget } from '@/common/types';\nimport { ResolverUtility } from '@/common/utilities';\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 /**\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 cls: TClass<unknown>;\n at: string;\n }): TBindingKey | undefined {\n const { key, target: declared, isOptional = false, cls, at } = opts;\n\n if (declared === undefined && key !== undefined) {\n return key;\n }\n\n if (declared === undefined) {\n throw getError({\n message: `[${cls.name}] ${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: `[${cls.name}] ${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: `[${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`,\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: `[${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`,\n });\n }\n\n return recorded;\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 cls,\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 cls,\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"]}
|
|
@@ -12,5 +12,5 @@ export type TInjectOptions = {
|
|
|
12
12
|
key?: never;
|
|
13
13
|
});
|
|
14
14
|
/** Marks a property or constructor parameter for dependency injection. */
|
|
15
|
-
export declare const inject: (opts: TInjectOptions) => (target:
|
|
15
|
+
export declare const inject: (opts: TInjectOptions) => (target: object, propertyName: string | symbol | undefined, parameterIndex?: number) => void;
|
|
16
16
|
//# sourceMappingURL=injectors.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectors.js","sourceRoot":"","sources":["../../../../src/modules/metadata/injectors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,OAAO,EAAyB,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAQ7E,0EAA0E;AAC1E,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,IAAoB,EAAE,EAAE;IAC7C,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;IAEvC,iGAAiG;IACjG,6EAA6E;IAC7E,IAAI,GAAG,KAAK,SAAS,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;QACxD,MAAM,QAAQ,CAAC;YACb,OAAO,EAAE,2DAA2D,MAAM,CAAC,QAAQ,CAAC,EAAE;SACvF,CAAC,CAAC;IACL,CAAC;IAED,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"injectors.js","sourceRoot":"","sources":["../../../../src/modules/metadata/injectors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,OAAO,EAAyB,gBAAgB,EAAE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K;iBACrC;aACF,CAAC,CAAC;YAEH,OAAO;QACT,CAAC;QAED,MAAM,QAAQ,CAAC;YACb,OAAO,EAAE,kFAAkF;SAC5F,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { getError } from '@/modules/error';\nimport { type TBindingKey, type TInjectTarget } from '@/common/types';\nimport { type MetadataRegistry, metadataRegistry } from '@/modules/registry';\n\n/** Either a key, or the class bound under it - never both. The class may be named directly or through a function, which defers the reference past an import cycle. */\nexport type TInjectOptions = {\n isOptional?: boolean;\n registry?: MetadataRegistry;\n} & ({ key: TBindingKey; target?: never } | { target: TInjectTarget; key?: never });\n\n/** Marks a property or constructor parameter for dependency injection. */\nexport const inject = (opts: TInjectOptions) => {\n const { key, target: injected } = opts;\n\n // A circular import leaves `target` undefined; fail here, not at resolve time. A function is the\n // deferred form - what it returns is checked when the container resolves it.\n if (key === undefined && typeof injected !== 'function') {\n throw getError({\n message: `@inject was given no binding key and no class | target: ${String(injected)}`,\n });\n }\n\n return (target: object, propertyName: string | symbol | undefined, parameterIndex?: number) => {\n const registry = opts.registry ?? metadataRegistry;\n\n if (typeof parameterIndex === 'number') {\n registry.setInjectMetadata({\n target,\n index: parameterIndex,\n metadata: {\n ...(key === undefined ? { target: injected } : { key }),\n index: parameterIndex,\n isOptional: opts.isOptional ?? false,\n },\n });\n\n return;\n }\n\n if (propertyName !== undefined) {\n registry.setPropertyMetadata({\n target,\n propertyName: propertyName,\n metadata: {\n ...(key === undefined ? { target: injected } : { bindingKey: key }),\n isOptional: opts.isOptional ?? false,\n },\n });\n\n return;\n }\n\n throw getError({\n message: '@inject decorator can only be used on class properties or constructor parameters',\n });\n };\n};\n"]}
|
|
@@ -5,12 +5,12 @@ import { type IInjectMetadata, type IPropertyMetadata } from './common/types.js'
|
|
|
5
5
|
/** Central metadata registry for storing and retrieving decorator metadata. */
|
|
6
6
|
export declare class MetadataRegistry extends BaseHelper {
|
|
7
7
|
constructor();
|
|
8
|
-
define<Target extends object = object, Value =
|
|
8
|
+
define<Target extends object = object, Value = unknown>(opts: {
|
|
9
9
|
target: Target;
|
|
10
10
|
key: TBindingKey;
|
|
11
11
|
value: Value;
|
|
12
12
|
}): void;
|
|
13
|
-
get<Target extends object = object, Value =
|
|
13
|
+
get<Target extends object = object, Value = unknown>(opts: {
|
|
14
14
|
target: Target;
|
|
15
15
|
key: TBindingKey;
|
|
16
16
|
}): Value | undefined;
|
|
@@ -25,7 +25,7 @@ export declare class MetadataRegistry extends BaseHelper {
|
|
|
25
25
|
getKeys<Target extends object = object>(opts: {
|
|
26
26
|
target: Target;
|
|
27
27
|
}): TBindingKey[];
|
|
28
|
-
getMethodNames<T =
|
|
28
|
+
getMethodNames<T = unknown>(opts: {
|
|
29
29
|
target: TClass<T>;
|
|
30
30
|
}): string[];
|
|
31
31
|
clearMetadata<T extends object = object>(opts: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../../../src/modules/registry/registry.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAO5D,+EAA+E;AAC/E,MAAM,OAAO,gBAAiB,SAAQ,UAAU;IAC9C;QACE,KAAK,CAAC,EAAE,KAAK,EAAE,gBAAgB,CAAC,IAAI,EAAE,CAAC,CAAC;IAC1C,CAAC;IAED,MAAM,CAA8C,IAInD;QACC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;QAEpC,MAAM,CAAC,KAAK,CACV,0DAA0D,EAC1D,MAAM,CAAC,WAAW,CAAC,IAAI,EACvB,GAAG,CAAC,QAAQ,EAAE,EACd,KAAK,CACN,CAAC;QAEF,OAAO,CAAC,cAAc,CAAC,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AC;IACnF,CAAC;IAED,2GAA2G;IAC3G,aAAa,CAA4B,IAAmB;QAC1D,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QACxB,MAAM,MAAM,GAAkC,OAAO,CAAC,cAAc,CAClE,YAAY,CAAC,WAAW,EACxB,MAAM,CACP,CAAC;QAEF,OAAO,MAAM,EAAE,GAAG,CAAC;IACrB,CAAC;CACF;AAED,8CAA8C;AAC9C,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,gBAAgB,EAAE,CAAC","sourcesContent":["import 'reflect-metadata';\n\nimport { BaseHelper } from '@/common/base-helper';\nimport { Logger } from '@/common/logger';\nimport { type TBindingKey, type TClass } from '@/common/types';\nimport { MetadataKeys } from '../metadata/common/constants';\nimport {\n type IBindingKeyRecord,\n type IInjectMetadata,\n type IPropertyMetadata,\n} from './common/types';\n\n/** Central metadata registry for storing and retrieving decorator metadata. */\nexport class MetadataRegistry extends BaseHelper {\n constructor() {\n super({ scope: MetadataRegistry.name });\n }\n\n define<Target extends object = object, Value = any>(opts: {\n target: Target;\n key: TBindingKey;\n value: Value;\n }): void {\n const { target, key, value } = opts;\n\n Logger.debug(\n '[define] Set metadata | target: %s | key: %s | value: %j',\n target.constructor.name,\n key.toString(),\n value,\n );\n\n Reflect.defineMetadata(key, value, target);\n }\n\n get<Target extends object = object, Value = any>(opts: {\n target: Target;\n key: TBindingKey;\n }): Value | undefined {\n const { target, key } = opts;\n\n return Reflect.getMetadata(key, target);\n }\n\n has<Target extends object = object>(opts: { target: Target; key: TBindingKey }): boolean {\n const { target, key } = opts;\n\n return Reflect.hasMetadata(key, target);\n }\n\n delete<Target extends object = object>(opts: { target: Target; key: TBindingKey }): boolean {\n const { target, key } = opts;\n\n return Reflect.deleteMetadata(key, target);\n }\n\n getKeys<Target extends object = object>(opts: { target: Target }): TBindingKey[] {\n const { target } = opts;\n\n return (\n Reflect.getMetadataKeys(target)?.filter((key) => {\n return typeof key === 'symbol' || typeof key === 'string';\n }) ?? []\n );\n }\n\n getMethodNames<T = any>(opts: { target: TClass<T> }): string[] {\n const { target } = opts;\n const prototype = target.prototype;\n const methods = Object.getOwnPropertyNames(prototype).filter(\n (name) => name !== 'constructor' && typeof prototype[name] === 'function',\n );\n\n return methods;\n }\n\n clearMetadata<T extends object = object>(opts: { target: T }): void {\n const { target } = opts;\n const keys = Reflect.getMetadataKeys(target);\n\n for (const key of keys) {\n Reflect.deleteMetadata(key, target);\n }\n }\n\n setPropertyMetadata<T extends object = object>(opts: {\n target: T;\n propertyName: string | symbol;\n metadata: IPropertyMetadata;\n }): void {\n const { target, propertyName, metadata } = opts;\n\n // Copy-on-write, as for constructor injection: the inherited map belongs to the parent class and\n // every sibling subclass reads it.\n const own: Map<string | symbol, IPropertyMetadata> | undefined = Reflect.getOwnMetadata(\n MetadataKeys.PROPERTIES,\n target.constructor,\n );\n const properties = own ?? new Map(this.getPropertiesMetadata({ target }));\n\n properties.set(propertyName, metadata);\n Reflect.defineMetadata(MetadataKeys.PROPERTIES, properties, target.constructor);\n }\n\n getPropertiesMetadata<T extends object = object>(opts: {\n target: T;\n }): Map<string | symbol, IPropertyMetadata> | undefined {\n const { target } = opts;\n\n return Reflect.getMetadata(MetadataKeys.PROPERTIES, target.constructor);\n }\n\n getPropertyMetadata<T extends object = object>(opts: {\n target: T;\n propertyName: string | symbol;\n }): IPropertyMetadata | undefined {\n const { target, propertyName } = opts;\n const properties = this.getPropertiesMetadata({ target });\n\n return properties?.get(propertyName);\n }\n\n setInjectMetadata<T extends object = object>(opts: {\n target: T;\n index: number;\n metadata: IInjectMetadata;\n }): void {\n const { target, index, metadata } = opts;\n // Copy-on-write: the inherited list belongs to the parent class and every sibling subclass reads it.\n const own: IInjectMetadata[] | undefined = Reflect.getOwnMetadata(MetadataKeys.INJECT, target);\n const injects = own ?? [...(Reflect.getMetadata(MetadataKeys.INJECT, target) ?? [])];\n injects[index] = metadata;\n Reflect.defineMetadata(MetadataKeys.INJECT, injects, target);\n }\n\n getInjectMetadata<T extends object = object>(opts: { target: T }): IInjectMetadata[] | undefined {\n const { target } = opts;\n\n return Reflect.getMetadata(MetadataKeys.INJECT, target);\n }\n\n /** `isProvisional` marks a DERIVED key; a registration replaces it silently, having seen the call-site override. */\n setBindingKey<T extends object = object>(opts: {\n target: T;\n key: TBindingKey;\n isProvisional?: boolean;\n }): void {\n const { target, key, isProvisional = false } = opts;\n const current: IBindingKeyRecord | undefined = Reflect.getOwnMetadata(\n MetadataKeys.BINDING_KEY,\n target,\n );\n\n // Two apps in one process, one class, two keys: the last registration would decide for both.\n if (current !== undefined && current.key !== key && !current.isProvisional && !isProvisional) {\n Logger.warn(\n '[setBindingKey] Rebound under a different key | target: %s | was: %s | now: %s',\n (target as { name?: string }).name,\n current.key.toString(),\n key.toString(),\n );\n }\n\n Reflect.defineMetadata(MetadataKeys.BINDING_KEY, { key, isProvisional }, target);\n }\n\n /** Own metadata only: a subclass is not registered, and the parent key would resolve the wrong binding. */\n getBindingKey<T extends object = object>(opts: { target: T }): TBindingKey | undefined {\n const { target } = opts;\n const record: IBindingKeyRecord | undefined = Reflect.getOwnMetadata(\n MetadataKeys.BINDING_KEY,\n target,\n );\n\n return record?.key;\n }\n}\n\n/** The shared `MetadataRegistry` instance. */\nexport const metadataRegistry = new MetadataRegistry();\n"]}
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../../../src/modules/registry/registry.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AAO5D,+EAA+E;AAC/E,MAAM,OAAO,gBAAiB,SAAQ,UAAU;IAC9C;QACE,KAAK,CAAC,EAAE,KAAK,EAAE,gBAAgB,CAAC,IAAI,EAAE,CAAC,CAAC;IAC1C,CAAC;IAED,MAAM,CA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,CACT,gFAAgF,EAC/E,MAA4B,CAAC,IAAI,EAClC,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,EACtB,GAAG,CAAC,QAAQ,EAAE,CACf,CAAC;QACJ,CAAC;QAED,OAAO,CAAC,cAAc,CAAC,YAAY,CAAC,WAAW,EAAE,EAAE,GAAG,EAAE,aAAa,EAAE,EAAE,MAAM,CAAC,CAAC;IACnF,CAAC;IAED,2GAA2G;IAC3G,aAAa,CAA4B,IAAmB;QAC1D,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;QACxB,MAAM,MAAM,GAAkC,OAAO,CAAC,cAAc,CAClE,YAAY,CAAC,WAAW,EACxB,MAAM,CACP,CAAC;QAEF,OAAO,MAAM,EAAE,GAAG,CAAC;IACrB,CAAC;CACF;AAED,8CAA8C;AAC9C,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,gBAAgB,EAAE,CAAC","sourcesContent":["import 'reflect-metadata';\n\nimport { BaseHelper } from '@/common/base-helper';\nimport { Logger } from '@/common/logger';\nimport { type TBindingKey, type TClass } from '@/common/types';\nimport { MetadataKeys } from '../metadata/common/constants';\nimport {\n type IBindingKeyRecord,\n type IInjectMetadata,\n type IPropertyMetadata,\n} from './common/types';\n\n/** Central metadata registry for storing and retrieving decorator metadata. */\nexport class MetadataRegistry extends BaseHelper {\n constructor() {\n super({ scope: MetadataRegistry.name });\n }\n\n define<Target extends object = object, Value = unknown>(opts: {\n target: Target;\n key: TBindingKey;\n value: Value;\n }): void {\n const { target, key, value } = opts;\n\n Logger.debug(\n '[define] Set metadata | target: %s | key: %s | value: %j',\n target.constructor.name,\n key.toString(),\n value,\n );\n\n Reflect.defineMetadata(key, value, target);\n }\n\n get<Target extends object = object, Value = unknown>(opts: {\n target: Target;\n key: TBindingKey;\n }): Value | undefined {\n const { target, key } = opts;\n\n return Reflect.getMetadata(key, target);\n }\n\n has<Target extends object = object>(opts: { target: Target; key: TBindingKey }): boolean {\n const { target, key } = opts;\n\n return Reflect.hasMetadata(key, target);\n }\n\n delete<Target extends object = object>(opts: { target: Target; key: TBindingKey }): boolean {\n const { target, key } = opts;\n\n return Reflect.deleteMetadata(key, target);\n }\n\n getKeys<Target extends object = object>(opts: { target: Target }): TBindingKey[] {\n const { target } = opts;\n\n return (\n Reflect.getMetadataKeys(target)?.filter((key) => {\n return typeof key === 'symbol' || typeof key === 'string';\n }) ?? []\n );\n }\n\n getMethodNames<T = unknown>(opts: { target: TClass<T> }): string[] {\n const { target } = opts;\n const prototype = target.prototype;\n const methods = Object.getOwnPropertyNames(prototype).filter(\n (name) => name !== 'constructor' && typeof prototype[name] === 'function',\n );\n\n return methods;\n }\n\n clearMetadata<T extends object = object>(opts: { target: T }): void {\n const { target } = opts;\n const keys = Reflect.getMetadataKeys(target);\n\n for (const key of keys) {\n Reflect.deleteMetadata(key, target);\n }\n }\n\n setPropertyMetadata<T extends object = object>(opts: {\n target: T;\n propertyName: string | symbol;\n metadata: IPropertyMetadata;\n }): void {\n const { target, propertyName, metadata } = opts;\n\n // Copy-on-write, as for constructor injection: the inherited map belongs to the parent class and\n // every sibling subclass reads it.\n const own: Map<string | symbol, IPropertyMetadata> | undefined = Reflect.getOwnMetadata(\n MetadataKeys.PROPERTIES,\n target.constructor,\n );\n const properties = own ?? new Map(this.getPropertiesMetadata({ target }));\n\n properties.set(propertyName, metadata);\n Reflect.defineMetadata(MetadataKeys.PROPERTIES, properties, target.constructor);\n }\n\n getPropertiesMetadata<T extends object = object>(opts: {\n target: T;\n }): Map<string | symbol, IPropertyMetadata> | undefined {\n const { target } = opts;\n\n return Reflect.getMetadata(MetadataKeys.PROPERTIES, target.constructor);\n }\n\n getPropertyMetadata<T extends object = object>(opts: {\n target: T;\n propertyName: string | symbol;\n }): IPropertyMetadata | undefined {\n const { target, propertyName } = opts;\n const properties = this.getPropertiesMetadata({ target });\n\n return properties?.get(propertyName);\n }\n\n setInjectMetadata<T extends object = object>(opts: {\n target: T;\n index: number;\n metadata: IInjectMetadata;\n }): void {\n const { target, index, metadata } = opts;\n // Copy-on-write: the inherited list belongs to the parent class and every sibling subclass reads it.\n const own: IInjectMetadata[] | undefined = Reflect.getOwnMetadata(MetadataKeys.INJECT, target);\n const injects = own ?? [...(Reflect.getMetadata(MetadataKeys.INJECT, target) ?? [])];\n injects[index] = metadata;\n Reflect.defineMetadata(MetadataKeys.INJECT, injects, target);\n }\n\n getInjectMetadata<T extends object = object>(opts: { target: T }): IInjectMetadata[] | undefined {\n const { target } = opts;\n\n return Reflect.getMetadata(MetadataKeys.INJECT, target);\n }\n\n /** `isProvisional` marks a DERIVED key; a registration replaces it silently, having seen the call-site override. */\n setBindingKey<T extends object = object>(opts: {\n target: T;\n key: TBindingKey;\n isProvisional?: boolean;\n }): void {\n const { target, key, isProvisional = false } = opts;\n const current: IBindingKeyRecord | undefined = Reflect.getOwnMetadata(\n MetadataKeys.BINDING_KEY,\n target,\n );\n\n // Two apps in one process, one class, two keys: the last registration would decide for both.\n if (current !== undefined && current.key !== key && !current.isProvisional && !isProvisional) {\n Logger.warn(\n '[setBindingKey] Rebound under a different key | target: %s | was: %s | now: %s',\n (target as { name?: string }).name,\n current.key.toString(),\n key.toString(),\n );\n }\n\n Reflect.defineMetadata(MetadataKeys.BINDING_KEY, { key, isProvisional }, target);\n }\n\n /** Own metadata only: a subclass is not registered, and the parent key would resolve the wrong binding. */\n getBindingKey<T extends object = object>(opts: { target: T }): TBindingKey | undefined {\n const { target } = opts;\n const record: IBindingKeyRecord | undefined = Reflect.getOwnMetadata(\n MetadataKeys.BINDING_KEY,\n target,\n );\n\n return record?.key;\n }\n}\n\n/** The shared `MetadataRegistry` instance. */\nexport const metadataRegistry = new MetadataRegistry();\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@venizia/ignis-inversion",
|
|
3
|
-
"version": "0.4.7-
|
|
3
|
+
"version": "0.4.7-5",
|
|
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.3",
|
|
91
|
-
"@venizia/dev-configs": "0.4.8-
|
|
91
|
+
"@venizia/dev-configs": "0.4.8-10",
|
|
92
92
|
"eslint": "^10.13.0",
|
|
93
93
|
"oxlint": "1.89.0",
|
|
94
94
|
"oxlint-tsgolint": "7.0.2004",
|