1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.NestContainer = void 0;
|
4 | const constants_1 = require("@nestjs/common/constants");
|
5 | const discoverable_meta_host_collection_1 = require("../discovery/discoverable-meta-host-collection");
|
6 | const exceptions_1 = require("../errors/exceptions");
|
7 | const initialize_on_preview_allowlist_1 = require("../inspector/initialize-on-preview.allowlist");
|
8 | const serialized_graph_1 = require("../inspector/serialized-graph");
|
9 | const request_constants_1 = require("../router/request/request-constants");
|
10 | const compiler_1 = require("./compiler");
|
11 | const internal_core_module_1 = require("./internal-core-module/internal-core-module");
|
12 | const internal_providers_storage_1 = require("./internal-providers-storage");
|
13 | const module_1 = require("./module");
|
14 | const module_token_factory_1 = require("./module-token-factory");
|
15 | const modules_container_1 = require("./modules-container");
|
16 | class NestContainer {
|
17 | constructor(_applicationConfig = undefined) {
|
18 | this._applicationConfig = _applicationConfig;
|
19 | this.globalModules = new Set();
|
20 | this.moduleTokenFactory = new module_token_factory_1.ModuleTokenFactory();
|
21 | this.moduleCompiler = new compiler_1.ModuleCompiler(this.moduleTokenFactory);
|
22 | this.modules = new modules_container_1.ModulesContainer();
|
23 | this.dynamicModulesMetadata = new Map();
|
24 | this.internalProvidersStorage = new internal_providers_storage_1.InternalProvidersStorage();
|
25 | this._serializedGraph = new serialized_graph_1.SerializedGraph();
|
26 | }
|
27 | get serializedGraph() {
|
28 | return this._serializedGraph;
|
29 | }
|
30 | get applicationConfig() {
|
31 | return this._applicationConfig;
|
32 | }
|
33 | setHttpAdapter(httpAdapter) {
|
34 | this.internalProvidersStorage.httpAdapter = httpAdapter;
|
35 | if (!this.internalProvidersStorage.httpAdapterHost) {
|
36 | return;
|
37 | }
|
38 | const host = this.internalProvidersStorage.httpAdapterHost;
|
39 | host.httpAdapter = httpAdapter;
|
40 | }
|
41 | getHttpAdapterRef() {
|
42 | return this.internalProvidersStorage.httpAdapter;
|
43 | }
|
44 | getHttpAdapterHostRef() {
|
45 | return this.internalProvidersStorage.httpAdapterHost;
|
46 | }
|
47 | async addModule(metatype, scope) {
|
48 |
|
49 |
|
50 | if (!metatype) {
|
51 | throw new exceptions_1.UndefinedForwardRefException(scope);
|
52 | }
|
53 | const { type, dynamicMetadata, token } = await this.moduleCompiler.compile(metatype);
|
54 | if (this.modules.has(token)) {
|
55 | return {
|
56 | moduleRef: this.modules.get(token),
|
57 | inserted: true,
|
58 | };
|
59 | }
|
60 | return {
|
61 | moduleRef: await this.setModule({
|
62 | token,
|
63 | type,
|
64 | dynamicMetadata,
|
65 | }, scope),
|
66 | inserted: true,
|
67 | };
|
68 | }
|
69 | async replaceModule(metatypeToReplace, newMetatype, scope) {
|
70 |
|
71 |
|
72 | if (!metatypeToReplace || !newMetatype) {
|
73 | throw new exceptions_1.UndefinedForwardRefException(scope);
|
74 | }
|
75 | const { token } = await this.moduleCompiler.compile(metatypeToReplace);
|
76 | const { type, dynamicMetadata } = await this.moduleCompiler.compile(newMetatype);
|
77 | return {
|
78 | moduleRef: await this.setModule({
|
79 | token,
|
80 | type,
|
81 | dynamicMetadata,
|
82 | }, scope),
|
83 | inserted: false,
|
84 | };
|
85 | }
|
86 | async setModule({ token, dynamicMetadata, type }, scope) {
|
87 | const moduleRef = new module_1.Module(type, this);
|
88 | moduleRef.token = token;
|
89 | moduleRef.initOnPreview = this.shouldInitOnPreview(type);
|
90 | this.modules.set(token, moduleRef);
|
91 | const updatedScope = [].concat(scope, type);
|
92 | await this.addDynamicMetadata(token, dynamicMetadata, updatedScope);
|
93 | if (this.isGlobalModule(type, dynamicMetadata)) {
|
94 | moduleRef.isGlobal = true;
|
95 | this.addGlobalModule(moduleRef);
|
96 | }
|
97 | return moduleRef;
|
98 | }
|
99 | async addDynamicMetadata(token, dynamicModuleMetadata, scope) {
|
100 | if (!dynamicModuleMetadata) {
|
101 | return;
|
102 | }
|
103 | this.dynamicModulesMetadata.set(token, dynamicModuleMetadata);
|
104 | const { imports } = dynamicModuleMetadata;
|
105 | await this.addDynamicModules(imports, scope);
|
106 | }
|
107 | async addDynamicModules(modules, scope) {
|
108 | if (!modules) {
|
109 | return;
|
110 | }
|
111 | await Promise.all(modules.map(module => this.addModule(module, scope)));
|
112 | }
|
113 | isGlobalModule(metatype, dynamicMetadata) {
|
114 | if (dynamicMetadata && dynamicMetadata.global) {
|
115 | return true;
|
116 | }
|
117 | return !!Reflect.getMetadata(constants_1.GLOBAL_MODULE_METADATA, metatype);
|
118 | }
|
119 | addGlobalModule(module) {
|
120 | this.globalModules.add(module);
|
121 | }
|
122 | getModules() {
|
123 | return this.modules;
|
124 | }
|
125 | getModuleCompiler() {
|
126 | return this.moduleCompiler;
|
127 | }
|
128 | getModuleByKey(moduleKey) {
|
129 | return this.modules.get(moduleKey);
|
130 | }
|
131 | getInternalCoreModuleRef() {
|
132 | return this.internalCoreModule;
|
133 | }
|
134 | async addImport(relatedModule, token) {
|
135 | if (!this.modules.has(token)) {
|
136 | return;
|
137 | }
|
138 | const moduleRef = this.modules.get(token);
|
139 | const { token: relatedModuleToken } = await this.moduleCompiler.compile(relatedModule);
|
140 | const related = this.modules.get(relatedModuleToken);
|
141 | moduleRef.addImport(related);
|
142 | }
|
143 | addProvider(provider, token, enhancerSubtype) {
|
144 | const moduleRef = this.modules.get(token);
|
145 | if (!provider) {
|
146 | throw new exceptions_1.CircularDependencyException(moduleRef?.metatype.name);
|
147 | }
|
148 | if (!moduleRef) {
|
149 | throw new exceptions_1.UnknownModuleException();
|
150 | }
|
151 | const providerKey = moduleRef.addProvider(provider, enhancerSubtype);
|
152 | const providerRef = moduleRef.getProviderByKey(providerKey);
|
153 | discoverable_meta_host_collection_1.DiscoverableMetaHostCollection.inspectProvider(this.modules, providerRef);
|
154 | return providerKey;
|
155 | }
|
156 | addInjectable(injectable, token, enhancerSubtype, host) {
|
157 | if (!this.modules.has(token)) {
|
158 | throw new exceptions_1.UnknownModuleException();
|
159 | }
|
160 | const moduleRef = this.modules.get(token);
|
161 | return moduleRef.addInjectable(injectable, enhancerSubtype, host);
|
162 | }
|
163 | addExportedProvider(provider, token) {
|
164 | if (!this.modules.has(token)) {
|
165 | throw new exceptions_1.UnknownModuleException();
|
166 | }
|
167 | const moduleRef = this.modules.get(token);
|
168 | moduleRef.addExportedProvider(provider);
|
169 | }
|
170 | addController(controller, token) {
|
171 | if (!this.modules.has(token)) {
|
172 | throw new exceptions_1.UnknownModuleException();
|
173 | }
|
174 | const moduleRef = this.modules.get(token);
|
175 | moduleRef.addController(controller);
|
176 | const controllerRef = moduleRef.controllers.get(controller);
|
177 | discoverable_meta_host_collection_1.DiscoverableMetaHostCollection.inspectController(this.modules, controllerRef);
|
178 | }
|
179 | clear() {
|
180 | this.modules.clear();
|
181 | }
|
182 | replace(toReplace, options) {
|
183 | this.modules.forEach(moduleRef => moduleRef.replace(toReplace, options));
|
184 | }
|
185 | bindGlobalScope() {
|
186 | this.modules.forEach(moduleRef => this.bindGlobalsToImports(moduleRef));
|
187 | }
|
188 | bindGlobalsToImports(moduleRef) {
|
189 | this.globalModules.forEach(globalModule => this.bindGlobalModuleToModule(moduleRef, globalModule));
|
190 | }
|
191 | bindGlobalModuleToModule(target, globalModule) {
|
192 | if (target === globalModule || target === this.internalCoreModule) {
|
193 | return;
|
194 | }
|
195 | target.addImport(globalModule);
|
196 | }
|
197 | getDynamicMetadataByToken(token, metadataKey) {
|
198 | const metadata = this.dynamicModulesMetadata.get(token);
|
199 | return metadataKey ? metadata?.[metadataKey] ?? [] : metadata;
|
200 | }
|
201 | registerCoreModuleRef(moduleRef) {
|
202 | this.internalCoreModule = moduleRef;
|
203 | this.modules[internal_core_module_1.InternalCoreModule.name] = moduleRef;
|
204 | }
|
205 | getModuleTokenFactory() {
|
206 | return this.moduleTokenFactory;
|
207 | }
|
208 | registerRequestProvider(request, contextId) {
|
209 | const wrapper = this.internalCoreModule.getProviderByKey(request_constants_1.REQUEST);
|
210 | wrapper.setInstanceByContextId(contextId, {
|
211 | instance: request,
|
212 | isResolved: true,
|
213 | });
|
214 | }
|
215 | shouldInitOnPreview(type) {
|
216 | return initialize_on_preview_allowlist_1.InitializeOnPreviewAllowlist.has(type);
|
217 | }
|
218 | }
|
219 | exports.NestContainer = NestContainer;
|