UNPKG

7.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.NestContainer = void 0;
4const constants_1 = require("@nestjs/common/constants");
5const circular_dependency_exception_1 = require("../errors/exceptions/circular-dependency.exception");
6const undefined_forwardref_exception_1 = require("../errors/exceptions/undefined-forwardref.exception");
7const unknown_module_exception_1 = require("../errors/exceptions/unknown-module.exception");
8const request_constants_1 = require("../router/request/request-constants");
9const compiler_1 = require("./compiler");
10const internal_core_module_1 = require("./internal-core-module");
11const internal_providers_storage_1 = require("./internal-providers-storage");
12const module_1 = require("./module");
13const module_token_factory_1 = require("./module-token-factory");
14const modules_container_1 = require("./modules-container");
15class NestContainer {
16 constructor(_applicationConfig = undefined) {
17 this._applicationConfig = _applicationConfig;
18 this.globalModules = new Set();
19 this.moduleTokenFactory = new module_token_factory_1.ModuleTokenFactory();
20 this.moduleCompiler = new compiler_1.ModuleCompiler(this.moduleTokenFactory);
21 this.modules = new modules_container_1.ModulesContainer();
22 this.dynamicModulesMetadata = new Map();
23 this.internalProvidersStorage = new internal_providers_storage_1.InternalProvidersStorage();
24 }
25 get applicationConfig() {
26 return this._applicationConfig;
27 }
28 setHttpAdapter(httpAdapter) {
29 this.internalProvidersStorage.httpAdapter = httpAdapter;
30 if (!this.internalProvidersStorage.httpAdapterHost) {
31 return;
32 }
33 const host = this.internalProvidersStorage.httpAdapterHost;
34 host.httpAdapter = httpAdapter;
35 }
36 getHttpAdapterRef() {
37 return this.internalProvidersStorage.httpAdapter;
38 }
39 getHttpAdapterHostRef() {
40 return this.internalProvidersStorage.httpAdapterHost;
41 }
42 async addModule(metatype, scope) {
43 // In DependenciesScanner#scanForModules we already check for undefined or invalid modules
44 // We still need to catch the edge-case of `forwardRef(() => undefined)`
45 if (!metatype) {
46 throw new undefined_forwardref_exception_1.UndefinedForwardRefException(scope);
47 }
48 const { type, dynamicMetadata, token } = await this.moduleCompiler.compile(metatype);
49 if (this.modules.has(token)) {
50 return this.modules.get(token);
51 }
52 const moduleRef = new module_1.Module(type, this);
53 moduleRef.token = token;
54 this.modules.set(token, moduleRef);
55 await this.addDynamicMetadata(token, dynamicMetadata, [].concat(scope, type));
56 if (this.isGlobalModule(type, dynamicMetadata)) {
57 this.addGlobalModule(moduleRef);
58 }
59 return moduleRef;
60 }
61 async addDynamicMetadata(token, dynamicModuleMetadata, scope) {
62 if (!dynamicModuleMetadata) {
63 return;
64 }
65 this.dynamicModulesMetadata.set(token, dynamicModuleMetadata);
66 const { imports } = dynamicModuleMetadata;
67 await this.addDynamicModules(imports, scope);
68 }
69 async addDynamicModules(modules, scope) {
70 if (!modules) {
71 return;
72 }
73 await Promise.all(modules.map(module => this.addModule(module, scope)));
74 }
75 isGlobalModule(metatype, dynamicMetadata) {
76 if (dynamicMetadata && dynamicMetadata.global) {
77 return true;
78 }
79 return !!Reflect.getMetadata(constants_1.GLOBAL_MODULE_METADATA, metatype);
80 }
81 addGlobalModule(module) {
82 this.globalModules.add(module);
83 }
84 getModules() {
85 return this.modules;
86 }
87 getModuleCompiler() {
88 return this.moduleCompiler;
89 }
90 getModuleByKey(moduleKey) {
91 return this.modules.get(moduleKey);
92 }
93 getInternalCoreModuleRef() {
94 return this.internalCoreModule;
95 }
96 async addImport(relatedModule, token) {
97 if (!this.modules.has(token)) {
98 return;
99 }
100 const moduleRef = this.modules.get(token);
101 const { token: relatedModuleToken } = await this.moduleCompiler.compile(relatedModule);
102 const related = this.modules.get(relatedModuleToken);
103 moduleRef.addRelatedModule(related);
104 }
105 addProvider(provider, token) {
106 const moduleRef = this.modules.get(token);
107 if (!provider) {
108 throw new circular_dependency_exception_1.CircularDependencyException(moduleRef === null || moduleRef === void 0 ? void 0 : moduleRef.metatype.name);
109 }
110 if (!moduleRef) {
111 throw new unknown_module_exception_1.UnknownModuleException();
112 }
113 return moduleRef.addProvider(provider);
114 }
115 addInjectable(injectable, token, host) {
116 if (!this.modules.has(token)) {
117 throw new unknown_module_exception_1.UnknownModuleException();
118 }
119 const moduleRef = this.modules.get(token);
120 moduleRef.addInjectable(injectable, host);
121 }
122 addExportedProvider(provider, token) {
123 if (!this.modules.has(token)) {
124 throw new unknown_module_exception_1.UnknownModuleException();
125 }
126 const moduleRef = this.modules.get(token);
127 moduleRef.addExportedProvider(provider);
128 }
129 addController(controller, token) {
130 if (!this.modules.has(token)) {
131 throw new unknown_module_exception_1.UnknownModuleException();
132 }
133 const moduleRef = this.modules.get(token);
134 moduleRef.addController(controller);
135 }
136 clear() {
137 this.modules.clear();
138 }
139 replace(toReplace, options) {
140 this.modules.forEach(moduleRef => moduleRef.replace(toReplace, options));
141 }
142 bindGlobalScope() {
143 this.modules.forEach(moduleRef => this.bindGlobalsToImports(moduleRef));
144 }
145 bindGlobalsToImports(moduleRef) {
146 this.globalModules.forEach(globalModule => this.bindGlobalModuleToModule(moduleRef, globalModule));
147 }
148 bindGlobalModuleToModule(target, globalModule) {
149 if (target === globalModule || target === this.internalCoreModule) {
150 return;
151 }
152 target.addRelatedModule(globalModule);
153 }
154 getDynamicMetadataByToken(token, metadataKey) {
155 const metadata = this.dynamicModulesMetadata.get(token);
156 if (metadata && metadata[metadataKey]) {
157 return metadata[metadataKey];
158 }
159 return [];
160 }
161 registerCoreModuleRef(moduleRef) {
162 this.internalCoreModule = moduleRef;
163 this.modules[internal_core_module_1.InternalCoreModule.name] = moduleRef;
164 }
165 getModuleTokenFactory() {
166 return this.moduleTokenFactory;
167 }
168 registerRequestProvider(request, contextId) {
169 const wrapper = this.internalCoreModule.getProviderByKey(request_constants_1.REQUEST);
170 wrapper.setInstanceByContextId(contextId, {
171 instance: request,
172 isResolved: true,
173 });
174 }
175}
176exports.NestContainer = NestContainer;