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.

131 lines (130 loc) • 5.87 kB
export class DiscoverableMetaHostCollection { /** * A map of class references to the metadata keys of the discoverable * decorators applied to them. */ static metaHostLinks = new Map(); /** * A map of metadata keys to instance wrappers (providers) with the corresponding metadata key. * The map is weakly referenced by the modules container (unique per application). */ static providersByMetaKey = new WeakMap(); /** * A map of metadata keys to instance wrappers (controllers) with the corresponding metadata key. * The map is weakly referenced by the modules container (unique per application). */ static controllersByMetaKey = new WeakMap(); /** * Adds a link between a class reference and a metadata key. * @param target The class reference. * @param metadataKey The metadata key. */ static addClassMetaHostLink(target, metadataKey) { const metadataKeys = this.metaHostLinks.get(target) ?? new Set(); metadataKeys.add(metadataKey); this.metaHostLinks.set(target, metadataKeys); } /** * Inspects a provider instance wrapper and adds it to the collection of providers * if it has a metadata key. * @param hostContainerRef A reference to the modules container. * @param instanceWrapper A provider instance wrapper. * @returns void */ static inspectProvider(hostContainerRef, instanceWrapper) { return this.inspectInstanceWrapper(hostContainerRef, instanceWrapper, this.providersByMetaKey); } /** * Inspects a controller instance wrapper and adds it to the collection of controllers * if it has a metadata key. * @param hostContainerRef A reference to the modules container. * @param instanceWrapper A controller's instance wrapper. * @returns void */ static inspectController(hostContainerRef, instanceWrapper) { return this.inspectInstanceWrapper(hostContainerRef, instanceWrapper, this.controllersByMetaKey); } static insertByMetaKey(metaKey, instanceWrapper, collection) { if (collection.has(metaKey)) { const wrappers = collection.get(metaKey); // provider wrappers are re-created between registration and instantiation // (the prototype phase replaces the map entry with a copy), so entries // are keyed by the wrapper instance id to keep one entry per provider for (const existing of wrappers) { if (existing.id === instanceWrapper.id) { wrappers.delete(existing); break; } } wrappers.add(instanceWrapper); } else { const wrappers = new Set(); wrappers.add(instanceWrapper); collection.set(metaKey, wrappers); } } static removeInstanceWrappers(hostContainerRef, instanceWrappers) { const ids = new Set(instanceWrappers.filter(Boolean).map(wrapper => wrapper.id)); for (const wrapperByMetaKeyMap of [ this.providersByMetaKey, this.controllersByMetaKey, ]) { wrapperByMetaKeyMap.get(hostContainerRef)?.forEach(wrappers => { for (const wrapper of wrappers) { if (ids.has(wrapper.id)) { wrappers.delete(wrapper); } } }); } } static getProvidersByMetaKey(hostContainerRef, metaKey) { const wrappersByMetaKey = this.providersByMetaKey.get(hostContainerRef); return wrappersByMetaKey?.get(metaKey) ?? new Set(); } static getControllersByMetaKey(hostContainerRef, metaKey) { const wrappersByMetaKey = this.controllersByMetaKey.get(hostContainerRef); return wrappersByMetaKey?.get(metaKey) ?? new Set(); } static inspectInstanceWrapper(hostContainerRef, instanceWrapper, wrapperByMetaKeyMap) { const metaKeys = DiscoverableMetaHostCollection.getMetaKeysByInstanceWrapper(instanceWrapper); if (!metaKeys) { return; } let collection; if (wrapperByMetaKeyMap.has(hostContainerRef)) { collection = wrapperByMetaKeyMap.get(hostContainerRef); } else { collection = new Map(); wrapperByMetaKeyMap.set(hostContainerRef, collection); } for (const metaKey of metaKeys) { this.insertByMetaKey(metaKey, instanceWrapper, collection); } } static getMetaKeysByInstanceWrapper(instanceWrapper) { let target = // NOTE: Regarding the ternary statement below, // - The condition `!wrapper.metatype` is needed because when we use `useValue` // the value of `wrapper.metatype` will be `null`. // - The condition `wrapper.inject` is needed here because when we use // `useFactory`, the value of `wrapper.metatype` will be the supplied // factory function. // For both cases, we should use `wrapper.instance.constructor` instead // of `wrapper.metatype` to resolve processor's class properly. // But since calling `wrapper.instance` could degrade overall performance // we must defer it as much we can. !instanceWrapper.metatype || instanceWrapper.inject ? (instanceWrapper.instance?.constructor ?? instanceWrapper.metatype) : instanceWrapper.metatype; // A subclass inherits the metadata of its parent classes' decorators const metaKeys = new Set(); while (target && target !== Function.prototype) { this.metaHostLinks.get(target)?.forEach(key => metaKeys.add(key)); target = Object.getPrototypeOf(target); } return metaKeys.size > 0 ? metaKeys : undefined; } }