UNPKG

@nestjs/core

Version:

The core of NestJS, a TypeScript framework for Node.js backends: modules, dependency injection and the request pipeline, on Express, Fastify or node:http.

100 lines (99 loc) • 4.7 kB
import { SilentLogger } from '../helpers/silent-logger.js'; import { ModuleRef } from '../module-ref.js'; export class LazyModuleLoader { dependenciesScanner; instanceLoader; moduleCompiler; modulesContainer; moduleOverrides; modulesInFlight = new WeakMap(); // The instance loader is shared by every load(), so its logger is swapped // for the first silent load() in flight and restored after the last one. silentLoadsInFlight = 0; loggerBeforeSilentLoads; constructor(dependenciesScanner, instanceLoader, moduleCompiler, modulesContainer, moduleOverrides) { this.dependenciesScanner = dependenciesScanner; this.instanceLoader = instanceLoader; this.moduleCompiler = moduleCompiler; this.modulesContainer = modulesContainer; this.moduleOverrides = moduleOverrides; } async load(loaderFn, loadOpts) { const moduleClassOrDynamicDefinition = await loaderFn(); const insertedModules = []; let moduleInstances; try { moduleInstances = await this.dependenciesScanner.scanForModules({ moduleDefinition: moduleClassOrDynamicDefinition, overrides: this.moduleOverrides, lazy: true, insertedModules, }); } catch (err) { // A concurrent load() may be building, or may have built, modules that // this scan registered. Only the ones nobody took over are rolled back. this.dependenciesScanner.removeModules(insertedModules.filter(moduleInstance => !this.modulesInFlight.has(moduleInstance) && !moduleInstance.isInstantiated)); throw err; } if (moduleInstances.length === 0) { // The module has been loaded already. In this case, we must // retrieve a module reference from the existing container. const { token } = await this.moduleCompiler.compile(moduleClassOrDynamicDefinition); const moduleInstance = this.modulesContainer.get(token); await this.modulesInFlight.get(moduleInstance); return moduleInstance && this.getTargetModuleRef(moduleInstance); } // The scan is asynchronous, so a module that was not built yet when the // scan reached it may have been built by a concurrent load() since then const modulesToBuild = moduleInstances.filter(moduleInstance => !this.modulesInFlight.has(moduleInstance) && !moduleInstance.isInstantiated); const loadsInFlight = moduleInstances.map(moduleInstance => this.modulesInFlight.get(moduleInstance)); const build = this.buildModules(modulesToBuild, loadsInFlight, loadOpts); // Registered before any await, so a concurrent load() waits for this build. modulesToBuild.forEach(moduleInstance => this.modulesInFlight.set(moduleInstance, build)); try { await build; } catch (err) { this.dependenciesScanner.removeModules(modulesToBuild); throw err; } finally { modulesToBuild.forEach(moduleInstance => this.modulesInFlight.delete(moduleInstance)); } const [targetModule] = moduleInstances; return this.getTargetModuleRef(targetModule); } async buildModules(moduleInstances, loadsInFlight, loadOpts) { await Promise.all(loadsInFlight); if (moduleInstances.length === 0) { return; } const isSilent = loadOpts?.logger === false; if (isSilent && this.silentLoadsInFlight++ === 0) { this.loggerBeforeSilentLoads = this.instanceLoader.logger; this.instanceLoader.setLogger(new SilentLogger()); } try { const lazyModulesContainer = this.createLazyModulesContainer(moduleInstances); await this.dependenciesScanner.scanModulesForDependencies(lazyModulesContainer); await this.instanceLoader.createInstancesOfDependencies(lazyModulesContainer); } finally { if (isSilent && --this.silentLoadsInFlight === 0) { this.instanceLoader.setLogger(this.loggerBeforeSilentLoads); this.loggerBeforeSilentLoads = undefined; } } } createLazyModulesContainer(moduleInstances) { moduleInstances = Array.from(new Set(moduleInstances)); return new Map(moduleInstances.map(ref => [ref.token, ref])); } getTargetModuleRef(moduleInstance) { const moduleRefInstanceWrapper = moduleInstance.getProviderByKey(ModuleRef); return moduleRefInstanceWrapper.instance; } }