@asenajs/ioc 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +219 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/CircularDependencyDetector.d.ts +49 -0
- package/dist/lib/CircularDependencyDetector.js +69 -0
- package/dist/lib/CircularDependencyDetector.js.map +1 -0
- package/dist/lib/Container.d.ts +247 -0
- package/dist/lib/Container.js +563 -0
- package/dist/lib/Container.js.map +1 -0
- package/dist/lib/IocApplication.d.ts +88 -0
- package/dist/lib/IocApplication.js +110 -0
- package/dist/lib/IocApplication.js.map +1 -0
- package/dist/lib/IocEngine.d.ts +173 -0
- package/dist/lib/IocEngine.js +725 -0
- package/dist/lib/IocEngine.js.map +1 -0
- package/dist/lib/component/Scope.d.ts +4 -0
- package/dist/lib/component/Scope.js +6 -0
- package/dist/lib/component/Scope.js.map +1 -0
- package/dist/lib/component/componentRegistry.d.ts +34 -0
- package/dist/lib/component/componentRegistry.js +134 -0
- package/dist/lib/component/componentRegistry.js.map +1 -0
- package/dist/lib/component/componentUtils.d.ts +16 -0
- package/dist/lib/component/componentUtils.js +44 -0
- package/dist/lib/component/componentUtils.js.map +1 -0
- package/dist/lib/component/decorators/Component.d.ts +8 -0
- package/dist/lib/component/decorators/Component.js +12 -0
- package/dist/lib/component/decorators/Component.js.map +1 -0
- package/dist/lib/component/decorators/Implements.d.ts +7 -0
- package/dist/lib/component/decorators/Implements.js +14 -0
- package/dist/lib/component/decorators/Implements.js.map +1 -0
- package/dist/lib/component/decorators/Inject.d.ts +29 -0
- package/dist/lib/component/decorators/Inject.js +82 -0
- package/dist/lib/component/decorators/Inject.js.map +1 -0
- package/dist/lib/component/decorators/OnStart.d.ts +20 -0
- package/dist/lib/component/decorators/OnStart.js +33 -0
- package/dist/lib/component/decorators/OnStart.js.map +1 -0
- package/dist/lib/component/decorators/OnStop.d.ts +15 -0
- package/dist/lib/component/decorators/OnStop.js +28 -0
- package/dist/lib/component/decorators/OnStop.js.map +1 -0
- package/dist/lib/component/decorators/PostConstruct.d.ts +14 -0
- package/dist/lib/component/decorators/PostConstruct.js +16 -0
- package/dist/lib/component/decorators/PostConstruct.js.map +1 -0
- package/dist/lib/component/decorators/PostProcessor.d.ts +25 -0
- package/dist/lib/component/decorators/PostProcessor.js +29 -0
- package/dist/lib/component/decorators/PostProcessor.js.map +1 -0
- package/dist/lib/component/decorators/Service.d.ts +8 -0
- package/dist/lib/component/decorators/Service.js +12 -0
- package/dist/lib/component/decorators/Service.js.map +1 -0
- package/dist/lib/component/decorators/Strategy.d.ts +9 -0
- package/dist/lib/component/decorators/Strategy.js +29 -0
- package/dist/lib/component/decorators/Strategy.js.map +1 -0
- package/dist/lib/component/decorators/Value.d.ts +29 -0
- package/dist/lib/component/decorators/Value.js +41 -0
- package/dist/lib/component/decorators/Value.js.map +1 -0
- package/dist/lib/component/decorators/index.d.ts +11 -0
- package/dist/lib/component/decorators/index.js +12 -0
- package/dist/lib/component/decorators/index.js.map +1 -0
- package/dist/lib/component/decorators/legacyDecorator.d.ts +15 -0
- package/dist/lib/component/decorators/legacyDecorator.js +24 -0
- package/dist/lib/component/decorators/legacyDecorator.js.map +1 -0
- package/dist/lib/component/index.d.ts +3 -0
- package/dist/lib/component/index.js +4 -0
- package/dist/lib/component/index.js.map +1 -0
- package/dist/lib/constants/ComponentConstants.d.ts +21 -0
- package/dist/lib/constants/ComponentConstants.js +25 -0
- package/dist/lib/constants/ComponentConstants.js.map +1 -0
- package/dist/lib/constants/index.d.ts +1 -0
- package/dist/lib/constants/index.js +2 -0
- package/dist/lib/constants/index.js.map +1 -0
- package/dist/lib/decorators/CoreService.d.ts +11 -0
- package/dist/lib/decorators/CoreService.js +30 -0
- package/dist/lib/decorators/CoreService.js.map +1 -0
- package/dist/lib/decorators/index.d.ts +1 -0
- package/dist/lib/decorators/index.js +2 -0
- package/dist/lib/decorators/index.js.map +1 -0
- package/dist/lib/helper/fileHelper.d.ts +1 -0
- package/dist/lib/helper/fileHelper.js +29 -0
- package/dist/lib/helper/fileHelper.js.map +1 -0
- package/dist/lib/helper/iocHelper.d.ts +2 -0
- package/dist/lib/helper/iocHelper.js +12 -0
- package/dist/lib/helper/iocHelper.js.map +1 -0
- package/dist/lib/helper/valueResolver.d.ts +24 -0
- package/dist/lib/helper/valueResolver.js +37 -0
- package/dist/lib/helper/valueResolver.js.map +1 -0
- package/dist/lib/index.d.ts +12 -0
- package/dist/lib/index.js +13 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/lifecycle/LifecycleService.d.ts +78 -0
- package/dist/lib/lifecycle/LifecycleService.js +181 -0
- package/dist/lib/lifecycle/LifecycleService.js.map +1 -0
- package/dist/lib/lifecycle/LifecycleState.d.ts +20 -0
- package/dist/lib/lifecycle/LifecycleState.js +22 -0
- package/dist/lib/lifecycle/LifecycleState.js.map +1 -0
- package/dist/lib/lifecycle/index.d.ts +2 -0
- package/dist/lib/lifecycle/index.js +3 -0
- package/dist/lib/lifecycle/index.js.map +1 -0
- package/dist/lib/logger/IocLogger.d.ts +35 -0
- package/dist/lib/logger/IocLogger.js +2 -0
- package/dist/lib/logger/IocLogger.js.map +1 -0
- package/dist/lib/logger/index.d.ts +1 -0
- package/dist/lib/logger/index.js +2 -0
- package/dist/lib/logger/index.js.map +1 -0
- package/dist/lib/types/Class.d.ts +1 -0
- package/dist/lib/types/Class.js +2 -0
- package/dist/lib/types/Class.js.map +1 -0
- package/dist/lib/types/ComponentPostProcessor.d.ts +30 -0
- package/dist/lib/types/ComponentPostProcessor.js +2 -0
- package/dist/lib/types/ComponentPostProcessor.js.map +1 -0
- package/dist/lib/types/ContainerService.d.ts +6 -0
- package/dist/lib/types/ContainerService.js +2 -0
- package/dist/lib/types/ContainerService.js.map +1 -0
- package/dist/lib/types/ICoreService.d.ts +9 -0
- package/dist/lib/types/ICoreService.js +2 -0
- package/dist/lib/types/ICoreService.js.map +1 -0
- package/dist/lib/types/ICoreServiceNames.d.ts +10 -0
- package/dist/lib/types/ICoreServiceNames.js +12 -0
- package/dist/lib/types/ICoreServiceNames.js.map +1 -0
- package/dist/lib/types/InjectableComponent.d.ts +42 -0
- package/dist/lib/types/InjectableComponent.js +2 -0
- package/dist/lib/types/InjectableComponent.js.map +1 -0
- package/dist/lib/types/IocConfig.d.ts +11 -0
- package/dist/lib/types/IocConfig.js +2 -0
- package/dist/lib/types/IocConfig.js.map +1 -0
- package/dist/lib/types/LifecycleComponent.d.ts +29 -0
- package/dist/lib/types/LifecycleComponent.js +2 -0
- package/dist/lib/types/LifecycleComponent.js.map +1 -0
- package/dist/lib/types/decorators/ComponentParams.d.ts +5 -0
- package/dist/lib/types/decorators/ComponentParams.js +2 -0
- package/dist/lib/types/decorators/ComponentParams.js.map +1 -0
- package/dist/lib/types/decorators/ComponentType.d.ts +14 -0
- package/dist/lib/types/decorators/ComponentType.js +10 -0
- package/dist/lib/types/decorators/ComponentType.js.map +1 -0
- package/dist/lib/types/decorators/ServiceParams.d.ts +2 -0
- package/dist/lib/types/decorators/ServiceParams.js +2 -0
- package/dist/lib/types/decorators/ServiceParams.js.map +1 -0
- package/dist/lib/types/index.d.ts +11 -0
- package/dist/lib/types/index.js +12 -0
- package/dist/lib/types/index.js.map +1 -0
- package/dist/lib/utils/errorChain.d.ts +4 -0
- package/dist/lib/utils/errorChain.js +29 -0
- package/dist/lib/utils/errorChain.js.map +1 -0
- package/dist/lib/utils/typedMetadata.d.ts +54 -0
- package/dist/lib/utils/typedMetadata.js +97 -0
- package/dist/lib/utils/typedMetadata.js.map +1 -0
- package/package.json +73 -4
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { Container } from './Container';
|
|
2
|
+
import { IocEngine } from './IocEngine';
|
|
3
|
+
import { ComponentConstants } from './constants';
|
|
4
|
+
import { LifecycleService } from './lifecycle';
|
|
5
|
+
import { ICoreServiceNames } from './types';
|
|
6
|
+
import { getTypedMetadata } from './utils/typedMetadata';
|
|
7
|
+
const consoleLogger = {
|
|
8
|
+
info: (message, meta) => (meta === undefined ? console.info(message) : console.info(message, meta)),
|
|
9
|
+
warn: (message, meta) => (meta === undefined ? console.warn(message) : console.warn(message, meta)),
|
|
10
|
+
error: (message, meta) => (meta === undefined ? console.error(message) : console.error(message, meta)),
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The IoC container wired up on its own: container, engine and lifecycle.
|
|
14
|
+
*
|
|
15
|
+
* `create()` registers and constructs every component. Start hooks are deferred until
|
|
16
|
+
* {@link IocApplication.start}, so a hook always sees the finished graph; `stop()` runs the
|
|
17
|
+
* stop hooks in reverse order.
|
|
18
|
+
*/
|
|
19
|
+
export class IocApplication {
|
|
20
|
+
_container;
|
|
21
|
+
engine;
|
|
22
|
+
lifecycle;
|
|
23
|
+
constructor(_container, engine, lifecycle) {
|
|
24
|
+
this._container = _container;
|
|
25
|
+
this.engine = engine;
|
|
26
|
+
this.lifecycle = lifecycle;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Build the container and register every component.
|
|
30
|
+
*
|
|
31
|
+
* @param {IocApplicationOptions} options - Component sources, logger, instances and overrides
|
|
32
|
+
* @returns {Promise<IocApplication>} The application, constructed but not started
|
|
33
|
+
*/
|
|
34
|
+
static async create(options = {}) {
|
|
35
|
+
const { components, imports, sourceFolder, rootFile, importTimeout, instances, overrides } = options;
|
|
36
|
+
const container = new Container();
|
|
37
|
+
await container.registerInstance(ICoreServiceNames.CONTAINER, container);
|
|
38
|
+
await container.registerInstance(ICoreServiceNames.LOGGER, options.logger ?? consoleLogger);
|
|
39
|
+
await container.register(ICoreServiceNames.IOC_ENGINE, IocEngine, true);
|
|
40
|
+
await container.register(ICoreServiceNames.LIFECYCLE_SERVICE, LifecycleService, true);
|
|
41
|
+
const engine = (await container.resolve(ICoreServiceNames.IOC_ENGINE));
|
|
42
|
+
const lifecycle = (await container.resolve(ICoreServiceNames.LIFECYCLE_SERVICE));
|
|
43
|
+
for (const [name, instance] of Object.entries(overrides ?? {})) {
|
|
44
|
+
if (container.has(name)) {
|
|
45
|
+
throw new Error(`Cannot override '${name}' - it is one of the container's own services and has already ` + 'been wired.');
|
|
46
|
+
}
|
|
47
|
+
container.overrideInstance(name, instance);
|
|
48
|
+
}
|
|
49
|
+
for (const [name, instance] of Object.entries(instances ?? {})) {
|
|
50
|
+
if (container.isOverridden(name)) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (container.has(name)) {
|
|
54
|
+
throw new Error(`Cannot register instance '${name}' - the name belongs to one of the container's own services.`);
|
|
55
|
+
}
|
|
56
|
+
await container.registerInstance(name, instance);
|
|
57
|
+
}
|
|
58
|
+
if (sourceFolder) {
|
|
59
|
+
engine.setConfig({ sourceFolder, rootFile, importTimeout });
|
|
60
|
+
}
|
|
61
|
+
const importClasses = imports?.flatMap((entry) => (Array.isArray(entry) ? entry : [entry]));
|
|
62
|
+
if (components?.length) {
|
|
63
|
+
const injectables = components.map((component) => ({
|
|
64
|
+
Class: component,
|
|
65
|
+
interface: getTypedMetadata(ComponentConstants.InterfaceKey, component),
|
|
66
|
+
}));
|
|
67
|
+
await engine.searchAndRegister(injectables, importClasses);
|
|
68
|
+
}
|
|
69
|
+
else if (sourceFolder || importClasses?.length) {
|
|
70
|
+
await engine.searchAndRegister(undefined, importClasses);
|
|
71
|
+
}
|
|
72
|
+
return new IocApplication(container, engine, lifecycle);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Resolve a component by name or by class.
|
|
76
|
+
*
|
|
77
|
+
* @param {string | Class<T>} key - Component name, or the decorated class
|
|
78
|
+
* @returns {Promise<T>} The component
|
|
79
|
+
*/
|
|
80
|
+
async resolve(key) {
|
|
81
|
+
const name = typeof key === 'string' ? key : getTypedMetadata(ComponentConstants.NameKey, key) || key.name;
|
|
82
|
+
return (await this._container.resolve(name));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Run every pending start hook, dependencies first.
|
|
86
|
+
*
|
|
87
|
+
* @returns {Promise<void>}
|
|
88
|
+
* @throws When a hook fails, after rolling back the components that already started
|
|
89
|
+
*/
|
|
90
|
+
async start() {
|
|
91
|
+
this.engine.warnAboutLateEntryComponents();
|
|
92
|
+
await this.lifecycle.start();
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Run stop hooks for everything that started, in reverse order.
|
|
96
|
+
*
|
|
97
|
+
* @param {number} hookTimeout - Per-hook ceiling in milliseconds
|
|
98
|
+
* @returns {Promise<number>} How many hooks threw, rejected or ran out of time
|
|
99
|
+
*/
|
|
100
|
+
async stop(hookTimeout) {
|
|
101
|
+
return this.lifecycle.stop(hookTimeout);
|
|
102
|
+
}
|
|
103
|
+
get container() {
|
|
104
|
+
return this._container;
|
|
105
|
+
}
|
|
106
|
+
get state() {
|
|
107
|
+
return this.lifecycle.state;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=IocApplication.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IocApplication.js","sourceRoot":"","sources":["../../lib/IocApplication.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG/C,OAAO,EAAc,iBAAiB,EAA4B,MAAM,SAAS,CAAC;AAClF,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAmDzD,MAAM,aAAa,GAAc;IAC/B,IAAI,EAAE,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,C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WAAoB;QACpC,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAC1C,CAAC;IAED,IAAW,SAAS;QAClB,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;IAC9B,CAAC;CACF"}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { type Class, type ICoreService, type InjectableComponent, type IocConfig } from './types';
|
|
2
|
+
import type { Container } from './Container';
|
|
3
|
+
export declare class IocEngine implements ICoreService {
|
|
4
|
+
serviceName: string;
|
|
5
|
+
private _container;
|
|
6
|
+
private logger;
|
|
7
|
+
private injectables;
|
|
8
|
+
private config?;
|
|
9
|
+
/** Entry files kept out of the scan, empty unless the scan actually ran */
|
|
10
|
+
private entryFiles;
|
|
11
|
+
/** Entry-declared components that existed by the time the scan ran */
|
|
12
|
+
private entryComponents;
|
|
13
|
+
/**
|
|
14
|
+
* @description Set IoC configuration
|
|
15
|
+
* @param {IocConfig} config - IoC configuration object
|
|
16
|
+
* @returns {void}
|
|
17
|
+
*/
|
|
18
|
+
setConfig(config?: IocConfig): void;
|
|
19
|
+
searchAndRegister(components?: InjectableComponent[], imports?: Class[]): Promise<void>;
|
|
20
|
+
private loadComponents;
|
|
21
|
+
private describeClass;
|
|
22
|
+
/**
|
|
23
|
+
* @description Refuse a component whose name is already held by something registered ahead of
|
|
24
|
+
* the components: one of the container's own services, or a pre-built instance.
|
|
25
|
+
*
|
|
26
|
+
* `Container.register` promotes a key that is registered twice to an array, which is how
|
|
27
|
+
* several implementations share an interface key. Under a component's own name it means
|
|
28
|
+
* every dependent is handed `[instance, component]` instead of either one, so the collision
|
|
29
|
+
* is reported here, before anything is constructed. An overridden name is exempt: there the
|
|
30
|
+
* double replaces the component on purpose.
|
|
31
|
+
* @returns {void}
|
|
32
|
+
* @throws When a component name is already registered
|
|
33
|
+
*/
|
|
34
|
+
private rejectTakenNames;
|
|
35
|
+
/**
|
|
36
|
+
* @description Report components that were declared in the entry file *after* the
|
|
37
|
+
* bootstrap call. Their decorators had not run yet when the scan read the registry,
|
|
38
|
+
* so they never made it into the container - and without this they would just be
|
|
39
|
+
* quietly missing at runtime.
|
|
40
|
+
* @param {string} bootstrapCall - How the warning names the call that built the container
|
|
41
|
+
* @returns {void}
|
|
42
|
+
*/
|
|
43
|
+
warnAboutLateEntryComponents(bootstrapCall?: string): void;
|
|
44
|
+
/**
|
|
45
|
+
* @description Absolute paths of the files that must be kept out of the scan:
|
|
46
|
+
* the entry declared in the config, and the module actually being executed.
|
|
47
|
+
* They are usually the same file, but not always - a project may declare one
|
|
48
|
+
* `rootFile` and start another, or run without a config at all.
|
|
49
|
+
* @returns {Set<string>} Resolved absolute entry paths
|
|
50
|
+
*/
|
|
51
|
+
private resolveEntryFiles;
|
|
52
|
+
/**
|
|
53
|
+
* @description Resolve a scanned path against the working directory. `getAllFiles`
|
|
54
|
+
* mirrors the form of the folder it was given, so a `sourceFolder` configured as an
|
|
55
|
+
* absolute path yields absolute entries that must not be joined onto cwd again.
|
|
56
|
+
* @param {string} file - Path as returned by the scan
|
|
57
|
+
* @returns {string} Absolute path
|
|
58
|
+
*/
|
|
59
|
+
private toAbsolutePath;
|
|
60
|
+
/**
|
|
61
|
+
* @description Collapse components that arrived from more than one source - a class
|
|
62
|
+
* re-exported through a barrel file, or one found by both the scan and the registry.
|
|
63
|
+
*
|
|
64
|
+
* Deduplication is by class identity, not by name: `Container.register` promotes a
|
|
65
|
+
* key to an array when the same name is registered twice, which would construct the
|
|
66
|
+
* component a second time (running @PostConstruct again) and hand every dependent an
|
|
67
|
+
* array instead of an instance.
|
|
68
|
+
* @param {InjectableComponent[]} components - Components from every source
|
|
69
|
+
* @returns {InjectableComponent[]} Components with duplicates removed
|
|
70
|
+
*/
|
|
71
|
+
private dedupeInjectables;
|
|
72
|
+
private validateAndRegisterComponents;
|
|
73
|
+
private register;
|
|
74
|
+
/**
|
|
75
|
+
* @description Warn when a transient declares lifecycle hooks.
|
|
76
|
+
*
|
|
77
|
+
* A transient is constructed per resolve and the container keeps no handle on it, so there is
|
|
78
|
+
* nothing for the lifecycle to start or stop. Its @OnStart still runs at construction, but its
|
|
79
|
+
* @OnStop can never run - and a component that acquires something it never releases is worth
|
|
80
|
+
* saying out loud rather than leaving to be discovered as a leak.
|
|
81
|
+
*
|
|
82
|
+
* @param {string} name - Registered component name
|
|
83
|
+
* @param {Class} injectable - The component class
|
|
84
|
+
* @returns {void}
|
|
85
|
+
*/
|
|
86
|
+
private warnAboutTransientLifecycleHooks;
|
|
87
|
+
private getInjectables;
|
|
88
|
+
private importFiles;
|
|
89
|
+
/**
|
|
90
|
+
* @description Import a single component file, pointing at it if it takes suspiciously
|
|
91
|
+
* long. A module whose top-level await never settles would otherwise hang the boot with
|
|
92
|
+
* no output at all; the import itself is left alone, only reported.
|
|
93
|
+
* @param {string} filePath - Absolute path to import
|
|
94
|
+
* @param {string} file - Path as scanned, for the message
|
|
95
|
+
* @param {number} timeout - Milliseconds before warning, 0 disables
|
|
96
|
+
* @returns {Promise<any>} The imported module namespace
|
|
97
|
+
*/
|
|
98
|
+
private importFile;
|
|
99
|
+
private processComponents;
|
|
100
|
+
/**
|
|
101
|
+
* Validates and converts the `imports` list. Unlike scanned or explicitly listed
|
|
102
|
+
* components, an import that carries no decorator of its own is a hard error, not a
|
|
103
|
+
* warning: `warnAboutUndecoratedSubclass` stays silent for a class with no marker
|
|
104
|
+
* anywhere on its chain, and a silently dropped import is exactly the failure the
|
|
105
|
+
* option exists to prevent.
|
|
106
|
+
*/
|
|
107
|
+
private processImports;
|
|
108
|
+
/**
|
|
109
|
+
* The error for an `imports` entry that failed the identity check. A class that extends a
|
|
110
|
+
* component but carries no decorator of its own is told so explicitly: whoever can see
|
|
111
|
+
* `@Service` on the base class reads the generic message as a contradiction.
|
|
112
|
+
* @param {any} entry - The rejected entry
|
|
113
|
+
* @returns {string} The message to throw
|
|
114
|
+
*/
|
|
115
|
+
private describeInvalidImport;
|
|
116
|
+
/**
|
|
117
|
+
* @description The nearest class up the prototype chain that carries its own component
|
|
118
|
+
* decorator, or undefined when none does.
|
|
119
|
+
* @param {any} component - A class, or anything else (yields undefined)
|
|
120
|
+
* @returns {Class | undefined} The decorated ancestor
|
|
121
|
+
*/
|
|
122
|
+
private findDecoratedAncestor;
|
|
123
|
+
/**
|
|
124
|
+
* Reports a class that extends a component but carries no decorator of its own.
|
|
125
|
+
*
|
|
126
|
+
* Component identity is read own-only, so such a class is simply skipped. Registering it
|
|
127
|
+
* under its *base's* name blew up on first inject - ugly, but at least detectable. Skipping
|
|
128
|
+
* it silently is worse: whatever the class was meant to do never happens and nothing
|
|
129
|
+
* anywhere says so.
|
|
130
|
+
*
|
|
131
|
+
* Only fires when an ancestor carries the marker, so an ordinary undecorated helper class in
|
|
132
|
+
* the scan folder stays quiet. A decorator that returns a wrapper subclass and applies
|
|
133
|
+
* `@Service` to the wrapper gives the wrapper its own marker, so it never reaches here.
|
|
134
|
+
*/
|
|
135
|
+
private warnAboutUndecoratedSubclass;
|
|
136
|
+
private isValidComponent;
|
|
137
|
+
private createComponentObject;
|
|
138
|
+
private topologicalSort;
|
|
139
|
+
private initializeGraph;
|
|
140
|
+
/**
|
|
141
|
+
* `@Inject('IMailer')` names an `@Implements` interface key, not a component, so it has no node
|
|
142
|
+
* in the graph. Its implementations are the components that must be registered first.
|
|
143
|
+
*/
|
|
144
|
+
private expandInterfaceDependency;
|
|
145
|
+
private findCycle;
|
|
146
|
+
/**
|
|
147
|
+
* @description Separates PostProcessor classes and their transitive dependencies from other components.
|
|
148
|
+
* PostProcessors and their deps are registered in Phase A (before post-processing is active).
|
|
149
|
+
* Non-PostProcessor dependencies pulled into Phase A will NOT be post-processed (by design).
|
|
150
|
+
*/
|
|
151
|
+
private separatePostProcessors;
|
|
152
|
+
private getDependencies;
|
|
153
|
+
/**
|
|
154
|
+
* Every strategy key a component consumes, keyed by the field that declares it. Walks the
|
|
155
|
+
* prototype chain like getStrategyDependencies does - a @Strategy field on a base class is
|
|
156
|
+
* injected into the subclass too, so it has to be reported against the subclass. Own class
|
|
157
|
+
* first, so a field redeclared in a subclass wins the same way injection resolves it.
|
|
158
|
+
*/
|
|
159
|
+
private collectStrategyFields;
|
|
160
|
+
/**
|
|
161
|
+
* Reports strategy keys nothing implements, once per injection site, at boot.
|
|
162
|
+
*
|
|
163
|
+
* An empty key is a legitimate plugin point and is injected as `[]`, which means a *typo'd*
|
|
164
|
+
* interface name no longer fails at boot either - it surfaces much later as a collection
|
|
165
|
+
* that is silently always empty. This line is what keeps the two apart.
|
|
166
|
+
*
|
|
167
|
+
* `debug` with no fallback to `info`, deliberately: a deliberate plugin point should stay
|
|
168
|
+
* quiet unless someone is looking.
|
|
169
|
+
*/
|
|
170
|
+
private reportEmptyStrategyKeys;
|
|
171
|
+
private getStrategyDependencies;
|
|
172
|
+
get container(): Container;
|
|
173
|
+
}
|