forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.ts
247 lines (214 loc) 路 7.28 KB
/
container.ts
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { DynamicModule, Provider } from '@nestjs/common';
import { GLOBAL_MODULE_METADATA } from '@nestjs/common/constants';
import { Injectable } from '@nestjs/common/interfaces/injectable.interface';
import { Type } from '@nestjs/common/interfaces/type.interface';
import { ApplicationConfig } from '../application-config';
import { CircularDependencyException } from '../errors/exceptions/circular-dependency.exception';
import { UndefinedForwardRefException } from '../errors/exceptions/undefined-forwardref.exception';
import { UnknownModuleException } from '../errors/exceptions/unknown-module.exception';
import { REQUEST } from '../router/request/request-constants';
import { ModuleCompiler } from './compiler';
import { ContextId } from './instance-wrapper';
import { InternalCoreModule } from './internal-core-module';
import { InternalProvidersStorage } from './internal-providers-storage';
import { Module } from './module';
import { ModuleTokenFactory } from './module-token-factory';
import { ModulesContainer } from './modules-container';
export class NestContainer {
private readonly globalModules = new Set<Module>();
private readonly moduleTokenFactory = new ModuleTokenFactory();
private readonly moduleCompiler = new ModuleCompiler(this.moduleTokenFactory);
private readonly modules = new ModulesContainer();
private readonly dynamicModulesMetadata = new Map<
string,
Partial<DynamicModule>
>();
private readonly internalProvidersStorage = new InternalProvidersStorage();
private internalCoreModule: Module;
constructor(
private readonly _applicationConfig: ApplicationConfig = undefined,
) {}
get applicationConfig(): ApplicationConfig | undefined {
return this._applicationConfig;
}
public setHttpAdapter(httpAdapter: any) {
this.internalProvidersStorage.httpAdapter = httpAdapter;
if (!this.internalProvidersStorage.httpAdapterHost) {
return;
}
const host = this.internalProvidersStorage.httpAdapterHost;
host.httpAdapter = httpAdapter;
}
public getHttpAdapterRef() {
return this.internalProvidersStorage.httpAdapter;
}
public getHttpAdapterHostRef() {
return this.internalProvidersStorage.httpAdapterHost;
}
public async addModule(
metatype: Type<any> | DynamicModule | Promise<DynamicModule>,
scope: Type<any>[],
): Promise<Module | undefined> {
// In DependenciesScanner#scanForModules we already check for undefined or invalid modules
// We still need to catch the edge-case of `forwardRef(() => undefined)`
if (!metatype) {
throw new UndefinedForwardRefException(scope);
}
const { type, dynamicMetadata, token } = await this.moduleCompiler.compile(
metatype,
);
if (this.modules.has(token)) {
return this.modules.get(token);
}
const moduleRef = new Module(type, this);
moduleRef.token = token;
this.modules.set(token, moduleRef);
await this.addDynamicMetadata(
token,
dynamicMetadata,
[].concat(scope, type),
);
if (this.isGlobalModule(type, dynamicMetadata)) {
this.addGlobalModule(moduleRef);
}
return moduleRef;
}
public async addDynamicMetadata(
token: string,
dynamicModuleMetadata: Partial<DynamicModule>,
scope: Type<any>[],
) {
if (!dynamicModuleMetadata) {
return;
}
this.dynamicModulesMetadata.set(token, dynamicModuleMetadata);
const { imports } = dynamicModuleMetadata;
await this.addDynamicModules(imports, scope);
}
public async addDynamicModules(modules: any[], scope: Type<any>[]) {
if (!modules) {
return;
}
await Promise.all(modules.map(module => this.addModule(module, scope)));
}
public isGlobalModule(
metatype: Type<any>,
dynamicMetadata?: Partial<DynamicModule>,
): boolean {
if (dynamicMetadata && dynamicMetadata.global) {
return true;
}
return !!Reflect.getMetadata(GLOBAL_MODULE_METADATA, metatype);
}
public addGlobalModule(module: Module) {
this.globalModules.add(module);
}
public getModules(): ModulesContainer {
return this.modules;
}
public getModuleCompiler(): ModuleCompiler {
return this.moduleCompiler;
}
public getModuleByKey(moduleKey: string): Module {
return this.modules.get(moduleKey);
}
public getInternalCoreModuleRef(): Module | undefined {
return this.internalCoreModule;
}
public async addImport(
relatedModule: Type<any> | DynamicModule,
token: string,
) {
if (!this.modules.has(token)) {
return;
}
const moduleRef = this.modules.get(token);
const { token: relatedModuleToken } = await this.moduleCompiler.compile(
relatedModule,
);
const related = this.modules.get(relatedModuleToken);
moduleRef.addRelatedModule(related);
}
public addProvider(
provider: Provider,
token: string,
): string | symbol | Function {
const moduleRef = this.modules.get(token);
if (!provider) {
throw new CircularDependencyException(moduleRef?.metatype.name);
}
if (!moduleRef) {
throw new UnknownModuleException();
}
return moduleRef.addProvider(provider);
}
public addInjectable(
injectable: Provider,
token: string,
host?: Type<Injectable>,
) {
if (!this.modules.has(token)) {
throw new UnknownModuleException();
}
const moduleRef = this.modules.get(token);
moduleRef.addInjectable(injectable, host);
}
public addExportedProvider(provider: Type<any>, token: string) {
if (!this.modules.has(token)) {
throw new UnknownModuleException();
}
const moduleRef = this.modules.get(token);
moduleRef.addExportedProvider(provider);
}
public addController(controller: Type<any>, token: string) {
if (!this.modules.has(token)) {
throw new UnknownModuleException();
}
const moduleRef = this.modules.get(token);
moduleRef.addController(controller);
}
public clear() {
this.modules.clear();
}
public replace(toReplace: any, options: any & { scope: any[] | null }) {
this.modules.forEach(moduleRef => moduleRef.replace(toReplace, options));
}
public bindGlobalScope() {
this.modules.forEach(moduleRef => this.bindGlobalsToImports(moduleRef));
}
public bindGlobalsToImports(moduleRef: Module) {
this.globalModules.forEach(globalModule =>
this.bindGlobalModuleToModule(moduleRef, globalModule),
);
}
public bindGlobalModuleToModule(target: Module, globalModule: Module) {
if (target === globalModule || target === this.internalCoreModule) {
return;
}
target.addRelatedModule(globalModule);
}
public getDynamicMetadataByToken(
token: string,
metadataKey: keyof DynamicModule,
) {
const metadata = this.dynamicModulesMetadata.get(token);
if (metadata && metadata[metadataKey]) {
return metadata[metadataKey] as any[];
}
return [];
}
public registerCoreModuleRef(moduleRef: Module) {
this.internalCoreModule = moduleRef;
this.modules[InternalCoreModule.name] = moduleRef;
}
public getModuleTokenFactory(): ModuleTokenFactory {
return this.moduleTokenFactory;
}
public registerRequestProvider<T = any>(request: T, contextId: ContextId) {
const wrapper = this.internalCoreModule.getProviderByKey(REQUEST);
wrapper.setInstanceByContextId(contextId, {
instance: request,
isResolved: true,
});
}
}