| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 |
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
262x
262x
262x
262x
262x
262x
262x
126x
60x
11x
1x
10x
10x
1x
9x
9x
9x
9x
11x
10x
1x
1x
1x
1x
4x
3x
1x
11x
3x
106x
3x
3x
3x
3x
3x
3x
3x
6x
1x
5x
1x
4x
4x
1x
1x
2x
1x
1x
1x
5x
1x
4x
4x
1x
6x
7x
2x
4x
1x
3x
36x
36x
36x
45x
45x
42x
45x
45x
45x
42x
45x
| import { DynamicModule } from '@nestjs/common';
import { GLOBAL_MODULE_METADATA } from '@nestjs/common/constants';
import { Type } from '@nestjs/common/interfaces/type.interface';
import { ApplicationConfig } from '../application-config';
import { CircularDependencyException } from '../errors/exceptions/circular-dependency.exception';
import { InvalidModuleException } from '../errors/exceptions/invalid-module.exception';
import { UnknownModuleException } from '../errors/exceptions/unknown-module.exception';
import { ApplicationReferenceHost } from '../helpers/application-ref-host';
import { ExternalContextCreator } from '../helpers/external-context-creator';
import { Reflector } from '../services';
import { ModuleCompiler } from './compiler';
import { Module } from './module';
import { ModulesContainer } from './modules-container';
export class NestContainer {
private readonly globalModules = new Set<Module>();
private readonly moduleCompiler = new ModuleCompiler();
private readonly modules = new ModulesContainer();
private readonly dynamicModulesMetadata = new Map<
string,
Partial<DynamicModule>
>();
private readonly reflector = new Reflector();
private readonly applicationRefHost = new ApplicationReferenceHost();
private externalContextCreator: ExternalContextCreator;
private modulesContainer: ModulesContainer;
private applicationRef: any;
constructor(
private readonly _applicationConfig: ApplicationConfig = void 0,
) {}
get applicationConfig(): ApplicationConfig | undefined {
return this._applicationConfig;
}
public setApplicationRef(applicationRef: any) {
this.applicationRef = applicationRef;
if (!this.applicationRefHost) {
return;
}
this.applicationRefHost.applicationRef = applicationRef;
}
public getApplicationRef() {
return this.applicationRef;
}
public async addModule(
metatype: Type<any> | DynamicModule | Promise<DynamicModule>,
scope: Type<any>[],
) {
if (!metatype) {
throw new InvalidModuleException(scope);
}
const { type, dynamicMetadata, token } = await this.moduleCompiler.compile(
metatype,
scope,
);
if (this.modules.has(token)) {
return;
}
const module = new Module(type, scope, this);
this.modules.set(token, module);
this.addDynamicMetadata(token, dynamicMetadata, [].concat(scope, type));
this.isGlobalModule(type) && this.addGlobalModule(module);
}
public addDynamicMetadata(
token: string,
dynamicModuleMetadata: Partial<DynamicModule>,
scope: Type<any>[],
) {
if (!dynamicModuleMetadata) {
return undefined;
}
this.dynamicModulesMetadata.set(token, dynamicModuleMetadata);
const { modules, imports } = dynamicModuleMetadata;
this.addDynamicModules(modules, scope);
this.addDynamicModules(imports, scope);
}
public addDynamicModules(modules: any[], scope: Type<any>[]) {
if (!modules) {
return undefined;
}
modules.forEach(module => this.addModule(module, scope));
}
public isGlobalModule(metatype: Type<any>): boolean {
return !!Reflect.getMetadata(GLOBAL_MODULE_METADATA, metatype);
}
public addGlobalModule(module: Module) {
this.globalModules.add(module);
}
public getModules(): ModulesContainer {
return this.modules;
}
public async addRelatedModule(
relatedModule: Type<any> | DynamicModule,
token: string,
) {
Iif (!this.modules.has(token)) return;
const module = this.modules.get(token);
const parent = module.metatype;
const scope = [].concat(module.scope, parent);
const { token: relatedModuleToken } = await this.moduleCompiler.compile(
relatedModule,
scope,
);
const related = this.modules.get(relatedModuleToken);
module.addRelatedModule(related);
}
public addComponent(component: Type<any>, token: string): string {
if (!component) {
throw new CircularDependencyException();
}
if (!this.modules.has(token)) {
throw new UnknownModuleException();
}
const module = this.modules.get(token);
return module.addComponent(component);
}
public addInjectable(injectable: Type<any>, token: string) {
Eif (!this.modules.has(token)) {
throw new UnknownModuleException();
}
const module = this.modules.get(token);
module.addInjectable(injectable);
}
public addExportedComponent(exportedComponent: Type<any>, token: string) {
if (!this.modules.has(token)) {
throw new UnknownModuleException();
}
const module = this.modules.get(token);
module.addExportedComponent(exportedComponent);
}
public addController(controller: Type<any>, token: string) {
if (!this.modules.has(token)) {
throw new UnknownModuleException();
}
const module = this.modules.get(token);
module.addRoute(controller);
}
public clear() {
this.modules.clear();
}
public replace(toReplace, options: any & { scope: any[] | null }) {
[...this.modules.values()].forEach(module => {
module.replace(toReplace, options);
});
}
public bindGlobalScope() {
this.modules.forEach(module => this.bindGlobalsToRelatedModules(module));
}
public bindGlobalsToRelatedModules(module: Module) {
this.globalModules.forEach(globalModule =>
this.bindGlobalModuleToModule(module, globalModule),
);
}
public bindGlobalModuleToModule(module: Module, globalModule: Module) {
if (module === globalModule) {
return undefined;
}
module.addRelatedModule(globalModule);
}
public getDynamicMetadataByToken(
token: string,
metadataKey: keyof DynamicModule,
): any[] {
const metadata = this.dynamicModulesMetadata.get(token);
Iif (metadata && metadata[metadataKey]) {
return metadata[metadataKey] as any[];
}
return [];
}
public getReflector(): Reflector {
return this.reflector;
}
public getExternalContextCreator(): ExternalContextCreator {
if (!this.externalContextCreator) {
this.externalContextCreator = ExternalContextCreator.fromContainer(this);
}
return this.externalContextCreator;
}
public getApplicationRefHost(): ApplicationReferenceHost {
return this.applicationRefHost;
}
public getModulesContainer(): ModulesContainer {
if (!this.modulesContainer) {
this.modulesContainer = this.getModules();
}
return this.modulesContainer;
}
}
export interface InstanceWrapper<T> {
name: any;
metatype: Type<T>;
instance: T;
isResolved: boolean;
isPending?: boolean;
done$?: Promise<void>;
inject?: Type<any>[];
isNotMetatype?: boolean;
forwardRef?: boolean;
async?: boolean;
}
|