@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.
1,133 lines • 57.5 kB
JavaScript
import { Logger, Scope, } from '@nestjs/common';
import { OPTIONAL_DEPS_METADATA, OPTIONAL_PROPERTY_DEPS_METADATA, PARAMTYPES_METADATA, PROPERTY_DEPS_METADATA, SELF_DECLARED_DEPS_METADATA, clc, isFunction, isNil, isObject, isString, isSymbol, isUndefined, } from '@nestjs/common/internal';
import { iterate } from 'iterare';
import { performance } from 'perf_hooks';
import { CircularDependencyException } from '../errors/exceptions/index.js';
import { RuntimeException } from '../errors/exceptions/runtime.exception.js';
import { UndefinedDependencyException } from '../errors/exceptions/undefined-dependency.exception.js';
import { UnknownDependenciesException } from '../errors/exceptions/unknown-dependencies.exception.js';
import { MISSING_DECORATOR_METADATA_MESSAGE } from '../errors/messages.js';
import { Barrier } from '../helpers/barrier.js';
import { makeSafeInstanceDecorator } from '../helpers/safe-instance-decorator.js';
import { REQUEST } from '../router/request/request-constants.js';
import { STATIC_CONTEXT } from './constants.js';
import { isDebugMode } from './helpers/is-debug-mode.util.js';
import { INQUIRER } from './inquirer/index.js';
import { SettlementSignal } from './settlement-signal.js';
/**
* An edge to a dependency, which follows the signal of its load for one
* context. The signal is read lazily, as the load creates it after the edge.
*/
class HostSettlementRef {
id;
host;
constructor(id, host) {
this.id = id;
this.host = host;
}
get settlementSignal() {
return this.host.settlementSignal;
}
}
/**
* Durable instances that may reference the placeholder of a provider whose
* deferred load failed, with the error (see `trackDurableDeferredLoad`).
* These instances live in instance wrappers that every injector of an
* application shares (the one of the application, the one of each
* `ModuleRef`, and those of microservices and gateways), so this is shared
* by all injectors too: an instance one of them knows to be built with a
* failed placeholder must not be handed out by another.
*/
const failedInstances = new WeakMap();
export class Injector {
options;
logger = new Logger('InjectorLogger');
classesWithoutMetadata = new WeakSet();
instanceDecorator = (target) => target;
/**
* Per-context loads that the provider which started them could not wait
* for (see `resolveComponentHost`), awaited by `waitForDeferredLoads`.
*/
deferredLoads = new WeakMap();
/**
* Deferred loads that are still running, per durable dependent of the
* provider being loaded and per context of that dependent (see
* `trackDurableDeferredLoad`). Like `deferredLoads`, these are kept per
* injector: a durable provider that resolves another one through its own
* `ModuleRef` while such a load runs must not wait for it, as the load may
* be waiting for that provider.
*/
durableDeferredLoads = new WeakMap();
failedInstances = failedInstances;
constructor(options) {
this.options = options;
if (options?.instanceDecorator) {
this.instanceDecorator = makeSafeInstanceDecorator(options.instanceDecorator);
}
}
loadPrototype({ token }, collection, contextId = STATIC_CONTEXT) {
if (!collection) {
return;
}
const target = collection.get(token);
const instance = target.createPrototype(contextId);
if (instance) {
// Update the wrapper in place instead of replacing it with a copy, as
// hosts (e.g., the classes its enhancers are bound to) keep a reference
// to it and must be notified of the dependencies it gets later.
target.setInstanceByContextId(contextId, { instance });
}
}
async loadInstance(wrapper, collection, moduleRef, resolutionContext = { contextId: STATIC_CONTEXT }) {
const inquirerId = this.getContextInquirerId(resolutionContext);
const hostContextId = this.getContextId(resolutionContext.contextId, wrapper);
const instanceHost = this.getInstanceHost(wrapper, hostContextId, inquirerId);
if (instanceHost.isPending) {
const settlementSignal = instanceHost.settlementSignal;
if (resolutionContext.inquirer &&
settlementSignal?.isCycle(resolutionContext.inquirer.id)) {
throw new CircularDependencyException(`"${wrapper.name}"`);
}
return instanceHost.donePromise.then((err) => {
if (err) {
throw err;
}
if (this.failedInstances.has(instanceHost)) {
throw this.failedInstances.get(instanceHost);
}
this.linkDurableDeferredLoads(wrapper, resolutionContext.contextId, hostContextId);
});
}
const settlementSignal = this.applySettlementSignal(instanceHost, wrapper);
const token = wrapper.token || wrapper.name;
const { inject } = wrapper;
const targetWrapper = collection.get(token);
if (isUndefined(targetWrapper)) {
throw new RuntimeException();
}
if (instanceHost.isResolved) {
return settlementSignal.complete();
}
try {
const t0 = this.getNowTimestamp();
const localResolutionContext = this.createResolutionContext(resolutionContext.contextId, wrapper, inquirerId);
const callback = async (instances, depth = 0) => {
const properties = await this.resolveProperties(wrapper, moduleRef, inject, localResolutionContext, resolutionContext.inquirer);
const instance = await this.instantiateClass(instances, wrapper, targetWrapper, wrapper.isTransient ? localResolutionContext : resolutionContext);
this.applyProperties(instance, properties);
// Count property dependencies too, so their hooks run first
const propertiesDepth = Math.max(0, ...(wrapper.getPropertiesMetadata() ?? []).map(property => property.wrapper.hierarchyLevel));
wrapper.hierarchyLevel = Math.max(depth, propertiesDepth) + 1;
wrapper.initTime = this.getNowTimestamp() - t0;
settlementSignal.complete();
};
await this.resolveConstructorParams(wrapper, moduleRef, inject, callback, localResolutionContext, resolutionContext.inquirer);
if (!instanceHost.isResolved) {
settlementSignal.complete();
}
}
catch (err) {
wrapper.removeInstanceByContextId(this.getContextId(resolutionContext.contextId, wrapper), inquirerId);
settlementSignal.error(err);
throw err;
}
}
async loadMiddleware(wrapper, collection, moduleRef, contextId = STATIC_CONTEXT, inquirer) {
const { metatype, token } = wrapper;
const targetWrapper = collection.get(token);
if (!isUndefined(targetWrapper.instance)) {
return;
}
targetWrapper.instance = Object.create(metatype.prototype);
await this.loadInstance(wrapper, collection, moduleRef, this.createResolutionContext(contextId, inquirer || wrapper));
}
async loadController(wrapper, moduleRef, contextId = STATIC_CONTEXT) {
const controllers = moduleRef.controllers;
await this.loadInstance(wrapper, controllers, moduleRef, this.createResolutionContext(contextId, wrapper));
if (wrapper.isDependencyTreeStatic()) {
// A static controller never gets a per-request load, so its transient
// enhancers must exist for the controller before the first request.
return this.loadEnhancers(contextId, wrapper, wrapper.getEnhancersMetadata() || []);
}
await this.loadEnhancersPerContext(wrapper, contextId, wrapper);
}
async loadInjectable(wrapper, moduleRef, contextId = STATIC_CONTEXT, inquirer) {
const injectables = moduleRef.injectables;
await this.loadInstance(wrapper, injectables, moduleRef, this.createResolutionContext(contextId, inquirer));
}
async loadProvider(wrapper, moduleRef, resolutionContext = { contextId: STATIC_CONTEXT }) {
if (this.shouldSkipProviderLoading(wrapper, resolutionContext)) {
if (!resolutionContext.inquirer) {
await this.lookupSkippedProviderDependencies(wrapper, moduleRef);
}
return;
}
const providers = moduleRef.providers;
await this.loadInstance(wrapper, providers, moduleRef, resolutionContext);
if (resolutionContext.contextId === STATIC_CONTEXT &&
!resolutionContext.inquirer &&
wrapper.isDependencyTreeStatic()) {
// A static provider that hosts handlers (e.g., an entry provider or a
// gateway) never gets a per-request load either, so its transient
// enhancers must exist for the provider before the first call.
// Only its bootstrap load (no inquirer) waits for them, as they may depend
// on a provider that is itself waiting for this one. The non-transient
// ones are shared, and get loaded with the injectables of the module.
const enhancers = wrapper.getEnhancersMetadata() || [];
return this.loadEnhancers(STATIC_CONTEXT, wrapper, enhancers.filter(item => item.isTransient));
}
await this.loadEnhancersPerContext(wrapper, resolutionContext.contextId, wrapper);
}
applySettlementSignal(instancePerContext, host) {
const settlementSignal = new SettlementSignal();
instancePerContext.donePromise = settlementSignal.asPromise();
instancePerContext.isPending = true;
instancePerContext.settlementSignal = settlementSignal;
host.settlementSignal = settlementSignal;
return settlementSignal;
}
async resolveConstructorParams(wrapper, moduleRef, inject, callback, resolutionContext = { contextId: STATIC_CONTEXT }, parentInquirer) {
const metadata = wrapper.getCtorMetadata();
if (resolutionContext.contextId !== STATIC_CONTEXT &&
this.hasDenseCtorMetadata(wrapper, inject, metadata)) {
const deps = await this.loadCtorMetadata(metadata, resolutionContext.contextId, resolutionContext.inquirer, parentInquirer);
return callback(deps);
}
const isFactoryProvider = !isNil(inject);
const [dependencies, optionalDependenciesIds] = isFactoryProvider
? this.getFactoryProviderDependencies(wrapper)
: this.getClassDependencies(wrapper);
const paramBarrier = new Barrier(dependencies.length);
let isResolved = true;
let depth = 0;
const resolveParam = async (param, index) => {
let isFound = false;
try {
if (this.isInquirer(param, parentInquirer)) {
/*
* Signal the barrier to make sure other dependencies do not get stuck waiting forever.
*/
paramBarrier.signal();
return parentInquirer && parentInquirer.instance;
}
if (resolutionContext.inquirer?.isTransient && parentInquirer) {
// When `inquirer` is transient too, inherit the parent inquirer
// This is required to ensure that transient providers are only resolved
// when requested
resolutionContext.rootInquirer = parentInquirer;
}
const nestedResolutionContext = this.getStaticTransientResolutionContext(resolutionContext, parentInquirer);
const paramWrapper = await this.resolveSingleParam(wrapper, param, { index, dependencies }, moduleRef, nestedResolutionContext, index);
isFound = true;
/*
* Ensure that all instance wrappers are resolved at this point before we continue.
* Otherwise the staticity of `wrapper`'s dependency tree may be evaluated incorrectly
* and result in undefined / null injection.
*/
await paramBarrier.signalAndWait();
const effectiveResolutionContext = this.getEffectiveResolutionContext(paramWrapper, resolutionContext, parentInquirer);
const paramWrapperWithInstance = await this.resolveComponentHost(moduleRef, paramWrapper, effectiveResolutionContext);
if (paramWrapperWithInstance.hierarchyLevel > depth) {
depth = paramWrapperWithInstance.hierarchyLevel;
}
const instanceHost = paramWrapperWithInstance.getInstanceByContextId(this.getContextId(effectiveResolutionContext.contextId, paramWrapperWithInstance), effectiveResolutionContext.effectiveInquirerId);
if (!instanceHost.isResolved && !paramWrapperWithInstance.forwardRef) {
isResolved = false;
}
if (this.shouldInjectDurablePayload(paramWrapperWithInstance, wrapper, parentInquirer, resolutionContext.contextId)) {
return resolutionContext.contextId.payload;
}
return instanceHost?.instance;
}
catch (err) {
/*
* Signal the barrier to make sure other dependencies do not get stuck waiting forever. We
* do not care if this occurs after `Barrier.signalAndWait()` is called in the `try` block
* because the barrier will always have been resolved by then.
*/
paramBarrier.signal();
const isOptional = optionalDependenciesIds.includes(index);
if (!isOptional ||
this.isLoadOfFoundDependency(isFound, resolutionContext)) {
throw err;
}
return undefined;
}
};
const loads = dependencies.map(resolveParam);
let instances;
try {
instances = await Promise.all(loads);
}
catch (err) {
this.releaseDetachedWrappersOnSettle(loads, wrapper, parentInquirer);
throw err;
}
this.releaseDetachedWrappers(wrapper, parentInquirer);
isResolved && (await callback(instances, depth));
}
getClassDependencies(wrapper) {
const ctorRef = wrapper.metatype;
const dependencies = this.reflectConstructorParams(ctorRef);
const optionalDependenciesIds = this.reflectOptionalParams(ctorRef);
this.warnOnMissingDecoratorMetadata(ctorRef, dependencies, optionalDependenciesIds);
return [dependencies, optionalDependenciesIds];
}
/**
* A decorated class that takes constructor parameters has parameter types
* only if its compiler emits decorator metadata, and tsx and esbuild don't.
* Without them, a dependency nothing names is `undefined` until the first
* request that uses it, so this says so while the application starts. It
* doesn't throw, as a class may tolerate that. An undecorated class has no
* metadata either way, so it isn't checked. Only a class's own parameter
* types describe the constructor it declares: those it inherits, say from
* a library built with tsc, leave the parameters it adds undefined.
*/
warnOnMissingDecoratorMetadata(type, dependencies, optionalDependenciesIds) {
if (type.length === 0 ||
this.classesWithoutMetadata.has(type) ||
Reflect.hasOwnMetadata(PARAMTYPES_METADATA, type) ||
Reflect.getOwnMetadataKeys(type).length === 0) {
return;
}
for (let index = 0; index < type.length; index++) {
if (dependencies[index] === undefined &&
!optionalDependenciesIds.includes(index)) {
this.classesWithoutMetadata.add(type);
this.logger.warn(MISSING_DECORATOR_METADATA_MESSAGE(type.name, index));
return;
}
}
}
getFactoryProviderDependencies(wrapper) {
const optionalDependenciesIds = [];
/**
* Same as the internal utility function `isOptionalFactoryDependency` from `@nestjs/common`.
* We are duplicating it here because that one is not supposed to be exported.
*/
function isOptionalFactoryDependency(value) {
const token = value?.token;
if (isNil(token)) {
return false;
}
return (!isUndefined(value.optional) &&
!value.prototype);
}
const mapFactoryProviderInjectArray = (item, index) => {
const isWrappedDependency = !isNil(item) && typeof item === 'object' && 'token' in item;
const token = isWrappedDependency
? item.token
: item;
if (isNil(token)) {
throw new UndefinedDependencyException(wrapper.name, {
index,
dependencies: (wrapper.inject ?? undefined),
});
}
if (typeof item !== 'object') {
return item;
}
if (isOptionalFactoryDependency(item)) {
if (item.optional) {
optionalDependenciesIds.push(index);
}
return item?.token;
}
return item;
};
const inject = wrapper.inject ?? [];
const dependencies = Array.from(inject, mapFactoryProviderInjectArray);
return [dependencies, optionalDependenciesIds];
}
reflectConstructorParams(type) {
const paramtypes = [
...(Reflect.getMetadata(PARAMTYPES_METADATA, type) || []),
];
const selfParams = this.reflectSelfParams(type);
selfParams.forEach(({ index, param }) => (paramtypes[index] = param));
return Array.from(paramtypes);
}
reflectOptionalParams(type) {
const ctorOwner = this.getConstructorOwner(type);
return Reflect.getOwnMetadata(OPTIONAL_DEPS_METADATA, ctorOwner) || [];
}
/**
* Returns the class in the prototype chain that declares the constructor
* `type` is instantiated with. A subclass without its own constructor
* inherits both the parent's parameter types and its `@Optional()` flags,
* while a subclass that redeclares the constructor must not inherit the
* parent's flags, as its parameters are unrelated to the parent's.
*/
getConstructorOwner(type) {
let current = type;
while (isFunction(current) && current !== Function.prototype) {
if (Reflect.hasOwnMetadata(PARAMTYPES_METADATA, current) ||
Reflect.hasOwnMetadata(OPTIONAL_DEPS_METADATA, current) ||
Reflect.hasOwnMetadata(SELF_DECLARED_DEPS_METADATA, current)) {
return current;
}
current = Object.getPrototypeOf(current);
}
return type;
}
reflectSelfParams(type) {
return Reflect.getMetadata(SELF_DECLARED_DEPS_METADATA, type) || [];
}
async resolveSingleParam(wrapper, param, dependencyContext, moduleRef, resolutionContext = { contextId: STATIC_CONTEXT }, keyOrIndex) {
if (isUndefined(param)) {
this.logger.log('Nest encountered an undefined dependency. This may be due to a circular import or a missing dependency declaration.');
throw new UndefinedDependencyException(wrapper.name, dependencyContext, moduleRef);
}
const token = this.resolveParamToken(wrapper, param);
return this.resolveComponentWrapper(moduleRef, token, dependencyContext, wrapper, resolutionContext, keyOrIndex);
}
resolveParamToken(wrapper, param) {
if (typeof param === 'object' && 'forwardRef' in param) {
wrapper.forwardRef = true;
return param.forwardRef();
}
return param;
}
async resolveComponentWrapper(moduleRef, token, dependencyContext, wrapper, resolutionContext = { contextId: STATIC_CONTEXT }, keyOrIndex) {
this.printResolvingDependenciesLog(token, resolutionContext.inquirer);
this.printLookingForProviderLog(token, moduleRef);
const providers = moduleRef.providers;
return this.lookupComponent(providers, moduleRef, { ...dependencyContext, name: token }, wrapper, resolutionContext, keyOrIndex);
}
async resolveComponentHost(moduleRef, instanceWrapper, resolutionContext = { contextId: STATIC_CONTEXT }) {
const inquirerId = this.getContextInquirerId(resolutionContext);
const instanceHost = this.getInstanceHost(instanceWrapper, this.getContextId(resolutionContext.contextId, instanceWrapper), inquirerId);
if (!instanceHost.isResolved && !instanceWrapper.forwardRef) {
this.insertSettlementRef(resolutionContext, instanceWrapper, instanceHost);
await this.loadProvider(instanceWrapper, instanceWrapper.host ?? moduleRef, resolutionContext);
}
else if (!instanceHost.isResolved &&
instanceWrapper.forwardRef &&
(resolutionContext.contextId !== STATIC_CONTEXT || !!inquirerId)) {
/**
* When circular dependency has been detected between
* either request/transient providers, we have to asynchronously
* resolve instance host for a specific contextId or inquirer, to ensure
* that eventual lazily created instance will be merged with the prototype
* instantiated beforehand.
*/
if (instanceHost.donePromise) {
const deferredLoad = instanceHost.donePromise.then((err) => {
// The placeholder handed out belongs to the load that failed...
if (err) {
throw err;
}
// ...or to an instance built with a provider whose deferred load
// failed (see `trackDurableDeferredLoad`). Loading the provider
// would build it again (see `getInstanceHost`), which may defer
// this load again and fail the same way, without end
if (this.failedInstances.has(instanceHost)) {
throw this.failedInstances.get(instanceHost);
}
return this.loadProvider(instanceWrapper, instanceWrapper.host ?? moduleRef, resolutionContext);
});
void deferredLoad.catch(err => {
instanceHost.settlementSignal?.error(err);
});
this.addDeferredLoad(resolutionContext.contextId, deferredLoad);
this.trackDurableDeferredLoad(instanceWrapper, resolutionContext.contextId, deferredLoad);
}
else {
/**
* No load has ever been scheduled for this context (e.g., request-scoped
* providers are no longer instantiated during static bootstrap, so a fresh
* durable/request sub-tree host has no inherited `donePromise`).
* Record the edge before loading it, so that if this load reaches the
* inquirer while it is still pending, `isCycle()` sees the cycle and throws
* instead of awaiting a `donePromise` that depends on itself.
*/
this.insertSettlementRef(resolutionContext, instanceWrapper, instanceHost);
await this.loadProvider(instanceWrapper, instanceWrapper.host ?? moduleRef, resolutionContext);
}
}
if (instanceWrapper.async) {
const host = instanceWrapper.getInstanceByContextId(this.getContextId(resolutionContext.contextId, instanceWrapper), inquirerId);
host.instance = await host.instance;
instanceWrapper.setInstanceByContextId(resolutionContext.contextId, host, inquirerId);
}
this.linkDurableDeferredLoads(instanceWrapper, resolutionContext.contextId, this.getContextId(resolutionContext.contextId, instanceWrapper));
return instanceWrapper;
}
async lookupComponent(providers, moduleRef, dependencyContext, wrapper, resolutionContext = { contextId: STATIC_CONTEXT }, keyOrIndex) {
const token = wrapper.token || wrapper.name;
const { name } = dependencyContext;
if (wrapper && token === name) {
throw new UnknownDependenciesException(wrapper.name, dependencyContext, moduleRef, { id: wrapper.id });
}
if (name && providers.has(name)) {
const instanceWrapper = this.getDependencyWrapper(providers.get(name), wrapper);
this.printFoundInModuleLog(name, moduleRef);
this.addDependencyMetadata(keyOrIndex, wrapper, instanceWrapper);
return instanceWrapper;
}
return this.lookupComponentInParentModules(dependencyContext, moduleRef, wrapper, resolutionContext, keyOrIndex);
}
async lookupComponentInParentModules(dependencyContext, moduleRef, wrapper, resolutionContext = { contextId: STATIC_CONTEXT }, keyOrIndex) {
const instanceWrapper = await this.lookupComponentInImports(moduleRef, dependencyContext.name, wrapper, new Set(), resolutionContext, keyOrIndex);
if (isNil(instanceWrapper)) {
throw new UnknownDependenciesException(wrapper.name, dependencyContext, moduleRef, { id: wrapper.id });
}
return instanceWrapper;
}
async lookupComponentInImports(moduleRef, name, wrapper, moduleRegistry = new Set(), resolutionContext = { contextId: STATIC_CONTEXT }, keyOrIndex, isTraversing) {
return this.lookupComponentInImportsSync(moduleRef, name, wrapper, moduleRegistry, resolutionContext, keyOrIndex, isTraversing);
}
/**
* Recursive part of `lookupComponentInImports`. Kept synchronous so a lookup
* walks the import graph in one go instead of yielding to other in-flight
* resolutions at every import level.
*/
lookupComponentInImportsSync(moduleRef, name, wrapper, moduleRegistry, resolutionContext, keyOrIndex, isTraversing) {
let instanceWrapperRef = null;
const imports = moduleRef.imports || new Set();
for (const relatedModule of imports) {
if (!relatedModule) {
continue;
}
if (isTraversing && !moduleRef.exports.has(relatedModule.metatype)) {
continue;
}
if (moduleRegistry.has(relatedModule.id)) {
continue;
}
this.printLookingForProviderLog(name, relatedModule);
moduleRegistry.add(relatedModule.id);
const { providers, exports } = relatedModule;
if (!exports.has(name) || !providers.has(name)) {
const instanceRef = this.lookupComponentInImportsSync(relatedModule, name, wrapper, moduleRegistry, resolutionContext, keyOrIndex, true);
if (instanceRef) {
this.addDependencyMetadata(keyOrIndex, wrapper, instanceRef);
return instanceRef;
}
continue;
}
this.printFoundInModuleLog(name, relatedModule);
instanceWrapperRef = this.getDependencyWrapper(providers.get(name), wrapper);
this.addDependencyMetadata(keyOrIndex, wrapper, instanceWrapperRef);
/*
* Stop at the first direct export match. Continuing when the provider is
* already resolved would let a later import (e.g. a global forRoot module)
* override an explicit forFeature import for the same token.
*/
break;
}
return instanceWrapperRef;
}
/**
* Returns the provider that an alias (`useExisting`) stands for, following
* a chain of aliases, each looked up the way the alias looks it up when it
* is loaded. An alias is a default-scoped provider of its own, so injecting
* it would hand the one instance it got itself to every consumer, which
* breaks a transient provider behind it. Returns the alias as it is when
* the provider it stands for is not found, or when the aliases point to
* each other, so that loading the alias reports it.
*
* Synchronous, so that a lookup settles after as many microtask hops
* whether it finds an alias or not.
*/
getAliasedWrapper(alias) {
const aliases = new Set();
let instanceWrapper = alias;
while (instanceWrapper.isAlias && instanceWrapper.host) {
if (aliases.has(instanceWrapper)) {
return alias;
}
aliases.add(instanceWrapper);
// `{ inject: [useExisting] }` (see `Module#addCustomUseExisting`)
const dependency = instanceWrapper.inject?.[0];
const token = isObject(dependency) && 'forwardRef' in dependency
? dependency.forwardRef()
: dependency;
if (isNil(token)) {
return alias;
}
const { host } = instanceWrapper;
const aliasedWrapper = host.providers.get(token) ??
this.lookupComponentInImportsSync(host, token, instanceWrapper, new Set(), { contextId: STATIC_CONTEXT }, 0);
if (!aliasedWrapper) {
return alias;
}
instanceWrapper = aliasedWrapper;
}
return instanceWrapper;
}
/**
* The wrapper to inject for the one found for a dependency of the given
* wrapper: the provider an alias stands for (see `getAliasedWrapper`),
* except to another alias, which loads it itself.
*/
getDependencyWrapper(instanceWrapper, wrapper) {
return instanceWrapper.isAlias && !wrapper.isAlias
? this.getAliasedWrapper(instanceWrapper)
: instanceWrapper;
}
async resolveProperties(wrapper, moduleRef, inject, resolutionContext = { contextId: STATIC_CONTEXT }, parentInquirer) {
if (!isNil(inject)) {
return [];
}
const metadata = wrapper.getPropertiesMetadata();
if (resolutionContext.contextId !== STATIC_CONTEXT &&
this.hasCompletePropertiesMetadata(wrapper, metadata)) {
return this.loadPropertiesMetadata(metadata, resolutionContext.contextId, resolutionContext.inquirer, parentInquirer);
}
const properties = this.reflectProperties(wrapper.metatype);
const propertyBarrier = new Barrier(properties.length);
const loads = properties.map(async (item) => {
let isFound = false;
try {
const dependencyContext = {
key: item.key,
name: item.name,
};
if (this.isInquirer(item.name, parentInquirer)) {
/*
* Signal the barrier to make sure other dependencies do not get stuck waiting forever.
*/
propertyBarrier.signal();
return parentInquirer && parentInquirer.instance;
}
const nestedResolutionContext = this.getStaticTransientResolutionContext(resolutionContext, parentInquirer);
const paramWrapper = await this.resolveSingleParam(wrapper, item.name, dependencyContext, moduleRef, nestedResolutionContext, item.key);
isFound = true;
/*
* Ensure that all instance wrappers are resolved at this point before we continue.
* Otherwise the staticity of `wrapper`'s dependency tree may be evaluated incorrectly
* and result in undefined / null injection.
*/
await propertyBarrier.signalAndWait();
const effectivePropertyResolutionContext = this.getEffectiveResolutionContext(paramWrapper, resolutionContext, parentInquirer);
const paramWrapperWithInstance = await this.resolveComponentHost(moduleRef, paramWrapper, effectivePropertyResolutionContext);
if (!paramWrapperWithInstance) {
return undefined;
}
const instanceHost = paramWrapperWithInstance.getInstanceByContextId(this.getContextId(effectivePropertyResolutionContext.contextId, paramWrapperWithInstance), effectivePropertyResolutionContext.effectiveInquirerId);
if (this.shouldInjectDurablePayload(paramWrapperWithInstance, wrapper, parentInquirer, resolutionContext.contextId)) {
return resolutionContext.contextId.payload;
}
return instanceHost.instance;
}
catch (err) {
/*
* Signal the barrier to make sure other dependencies do not get stuck waiting forever. We
* do not care if this occurs after `Barrier.signalAndWait()` is called in the `try` block
* because the barrier will always have been resolved by then.
*/
propertyBarrier.signal();
if (!item.isOptional ||
this.isLoadOfFoundDependency(isFound, resolutionContext)) {
throw err;
}
return undefined;
}
});
let instances;
try {
instances = await Promise.all(loads);
}
catch (err) {
this.releaseDetachedWrappersOnSettle(loads, wrapper, parentInquirer);
throw err;
}
this.releaseDetachedWrappers(wrapper, parentInquirer);
return properties.map((item, index) => ({
...item,
instance: instances[index],
}));
}
reflectProperties(type) {
const properties = Reflect.getMetadata(PROPERTY_DEPS_METADATA, type) || [];
const optionalKeys = Reflect.getMetadata(OPTIONAL_PROPERTY_DEPS_METADATA, type) || [];
return properties.map((item) => ({
...item,
name: item.type,
isOptional: optionalKeys.includes(item.key),
}));
}
applyProperties(instance, properties) {
if (!isObject(instance)) {
return;
}
iterate(properties)
.filter(item => !isNil(item.instance))
.forEach(item => (instance[item.key] = item.instance));
}
async instantiateClass(instances, wrapper, targetMetatype, resolutionContext = { contextId: STATIC_CONTEXT }) {
const { metatype, inject } = wrapper;
const inquirerId = this.getContextInquirerId(resolutionContext);
const instanceHost = targetMetatype.getInstanceByContextId(this.getContextId(resolutionContext.contextId, targetMetatype), inquirerId);
const isInContext = this.isInContext(wrapper, resolutionContext);
if (this.options?.preview && !wrapper.host?.initOnPreview) {
instanceHost.isResolved = true;
return instanceHost.instance;
}
if (isNil(inject) && isInContext) {
instanceHost.instance = wrapper.forwardRef
? Object.assign(instanceHost.instance, new metatype(...instances))
: new metatype(...instances);
instanceHost.instance = this.instanceDecorator(instanceHost.instance);
instanceHost.isConstructorCalled = true;
}
else if (isInContext) {
const factoryReturnValue = targetMetatype.metatype(...instances);
instanceHost.instance = await factoryReturnValue;
instanceHost.instance = this.instanceDecorator(instanceHost.instance);
instanceHost.isConstructorCalled = true;
}
instanceHost.isResolved = true;
return instanceHost.instance;
}
async loadPerContext(instance, moduleRef, collection, ctx, wrapper) {
if (!wrapper) {
const injectionToken = instance.constructor;
wrapper =
collection.get(injectionToken) ??
// A class provider registered under a token other than its class
iterate(collection.values()).find(item => !item.isAlias && item.instance === instance);
}
else {
wrapper = collection.get(wrapper.token) ?? wrapper;
}
await this.loadInstance(wrapper, collection, moduleRef, this.createResolutionContext(ctx, wrapper));
await this.loadEnhancersPerContext(wrapper, ctx, wrapper);
await this.waitForDeferredLoads(ctx);
const host = wrapper.getInstanceByContextId(this.getContextId(ctx, wrapper), wrapper.id);
return host && host.instance;
}
/**
* Waits for the loads deferred while resolving the given context (see
* `resolveComponentHost`), including those that the durable instances it
* uses were built with (see `trackDurableDeferredLoad`), and throws if one
* of them failed. A failed load is kept, as instances of the context already
* reference the provider that it failed to instantiate.
*/
async waitForDeferredLoads(contextId) {
const deferredLoads = this.deferredLoads.get(contextId);
if (!deferredLoads) {
return;
}
for (const deferredLoad of deferredLoads) {
await deferredLoad;
deferredLoads.delete(deferredLoad);
}
}
async loadEnhancersPerContext(wrapper, ctx, inquirer, enhancers = wrapper.getEnhancersMetadata() || []) {
if (ctx === STATIC_CONTEXT) {
return;
}
await this.loadEnhancers(ctx, inquirer, enhancers);
}
async loadEnhancers(ctx, inquirer, enhancers) {
const loadEnhancer = (item) => {
const hostModule = item.host;
return this.loadInstance(item, hostModule.injectables, hostModule, this.createResolutionContext(ctx, inquirer));
};
await Promise.all(enhancers.map(loadEnhancer));
}
async loadCtorMetadata(metadata, contextId, inquirer, parentInquirer) {
const loads = metadata.map(async (item) => this.resolveScopedComponentHost(item, contextId, inquirer, parentInquirer));
let hosts;
try {
hosts = await Promise.all(loads);
}
catch (err) {
this.releaseDetachedWrappersOnSettle(loads, inquirer, parentInquirer);
throw err;
}
this.releaseDetachedWrappers(inquirer, parentInquirer);
return hosts.map((item, index) => {
const dependency = metadata[index];
const effectiveInquirerId = this.getEffectiveInquirerId(dependency, this.createResolutionContext(contextId, inquirer), parentInquirer);
if (this.shouldInjectDurablePayload(item, inquirer, parentInquirer, contextId)) {
return contextId.payload;
}
return item?.getInstanceByContextId(this.getContextId(contextId, item), effectiveInquirerId).instance;
});
}
async loadPropertiesMetadata(metadata, contextId, inquirer, parentInquirer) {
const loads = metadata.map(async ({ wrapper: item, key }) => ({
key,
host: await this.resolveScopedComponentHost(item, contextId, inquirer, parentInquirer),
}));
let dependenciesHosts;
try {
dependenciesHosts = await Promise.all(loads);
}
catch (err) {
this.releaseDetachedWrappersOnSettle(loads, inquirer, parentInquirer);
throw err;
}
this.releaseDetachedWrappers(inquirer, parentInquirer);
return dependenciesHosts.map(({ key, host }, index) => {
const effectiveInquirerId = this.getEffectiveInquirerId(metadata[index].wrapper, this.createResolutionContext(contextId, inquirer), parentInquirer);
return {
key,
name: key,
instance: this.shouldInjectDurablePayload(host, inquirer, parentInquirer, contextId)
? contextId.payload
: host?.getInstanceByContextId(this.getContextId(contextId, host), effectiveInquirerId).instance,
};
});
}
addDeferredLoad(contextId, deferredLoad) {
if (contextId === STATIC_CONTEXT) {
return;
}
let deferredLoads = this.deferredLoads.get(contextId);
if (!deferredLoads) {
deferredLoads = new Set();
this.deferredLoads.set(contextId, deferredLoads);
}
deferredLoads.add(deferredLoad);
}
/**
* In a durable context (see `ContextIdStrategy`), the dependents built with
* the placeholder of a provider whose load is deferred are shared with the
* next contexts mapped to it, which do not wait for that load. Until it
* settles, the contexts that use them wait for it as well (see
* `linkDurableDeferredLoads`); if it fails, the contexts still waiting for
* one of them fail, and the next one that uses them builds them again (see
* `getInstanceHost`).
*/
trackDurableDeferredLoad(wrapper, contextId, deferredLoad) {
if (this.getContextId(contextId, wrapper) === contextId) {
return;
}
const untrackers = [];
this.forEachDurableDependent(wrapper, contextId, (dependent, hostId) => {
const loadsPerContext = this.durableDeferredLoads.get(dependent) ?? new WeakMap();
const loads = loadsPerContext.get(hostId) ?? new Set();
loads.add(deferredLoad);
loadsPerContext.set(hostId, loads);
this.durableDeferredLoads.set(dependent, loadsPerContext);
untrackers.push(() => {
loads.delete(deferredLoad);
if (loads.size === 0) {
loadsPerContext.delete(hostId);
}
});
});
const untrack = () => untrackers.forEach(untrackLoad => untrackLoad());
deferredLoad.then(untrack, (err) => {
// Looked up again, as a context may have built another dependent with
// the placeholder in the meantime
this.forEachDurableDependent(wrapper, contextId, (dependent, hostId) => dependent
.getExistingInstancesByContextId(hostId)
.filter(host => host.isResolved || host.isPending)
.forEach(host => this.failedInstances.set(host, err)));
untrack();
});
}
/**
* Adds the running loads that the instance of the wrapper in a durable
* context was built with (see `trackDurableDeferredLoad`) to the loads of
* the given context.
*/
linkDurableDeferredLoads(wrapper, contextId, hostContextId) {
if (hostContextId === contextId) {
return;
}
this.durableDeferredLoads
.get(wrapper)
?.get(hostContextId)
?.forEach(deferredLoad => this.addDeferredLoad(contextId, deferredLoad));
}
forEachDurableDependent(wrapper, contextId, callback) {
for (const dependent of wrapper.getDependents()) {
const hostContextId = this.getContextId(contextId, dependent);
if (hostContextId !== contextId) {
callback(dependent, hostContextId);
}
}
}
/**
* Returns the instance of the wrapper for the given context, or a new one
* when it was built with a provider whose deferred load failed (see
* `trackDurableDeferredLoad`), so that it is built again.
*/
getInstanceHost(wrapper, contextId, inquirerId) {
const instanceHost = wrapper.getInstanceByContextId(contextId, inquirerId);
if (!instanceHost.isResolved || !this.failedInstances.has(instanceHost)) {
return instanceHost;
}
wrapper.removeInstanceByContextId(contextId, inquirerId);
return wrapper.getInstanceByContextId(contextId, inquirerId);
}
/**
* Whether an error of an optional dependency comes from loading one that
* exists (e.g., its constructor threw), rather than from looking it up.
* `@Optional()` only covers a missing provider, so such an error fails the
* consumer, as it does when the dependencies come from recorded metadata
* (see `loadCtorMetadata`). This is limited to the instances built for a
* context, so that an application that starts with a failing optional
* transient provider still starts.
*/
isLoadOfFoundDependency(isFound, resolutionContext) {
return isFound && resolutionContext.contextId !== STATIC_CONTEXT;
}
/**
* A wrapper may be detached while a dependency of it is still loading,
* e.g., when `ModuleRef#create()` rejects on the first error of its
* dependencies, or when a lazy module fails to load and is removed before
* its other providers settle (see `NestContainer#removeModules`). Releases
* what the dependencies kept for it since.
*/
releaseDetachedWrappers(wrapper, inquirer) {
if (wrapper?.isDetached) {
wrapper.detachFromDependencies();
}
if (inquirer?.isDetached) {
inquirer.detachFromDependencies();
}
}
/**
* As `releaseDetachedWrappers`, once the given loads settle: when one of
* the dependencies of a wrapper fails, the others keep loading after its
* consumers have failed, and may be detached by then.
*/
releaseDetachedWrappersOnSettle(loads, wrapper, inquirer) {
void Promise.allSettled(loads).then(() => this.releaseDetachedWrappers(wrapper, inquirer));
}
/**
* Records that the load of the inquirer waits on the given host.
*/
insertSettlementRef({ contextId, inquirer }, wrapper, instanceHost) {
const inquirerSignal = inquirer?.isTransient
? inquirer.settlementSignal
: inquirer?.getInstanceByContextId(this.getContextId(contextId, inquirer))
.settlementSignal;
inquirerSignal?.insertRef(new HostSettlementRef(wrapper.id, instanceHost));
}
getInquirerId(inquirer) {
return inquirer ? inquirer.id : undefined;
}
createResolutionContext(contextId, inquirer, effectiveInquirerId, rootInquirer) {
return {
contextId,
inquirer,
effectiveInquirerId,
rootInquirer,
};
}
getContextInquirerId({ inquirer, effectiveInquirerId, }) {
return effectiveInquirerId ?? this.getInquirerId(inquirer);
}
isInContext(wrapper, resolutionContext) {
return (wrapper.isStatic(resolutionContext.contextId, resolutionContext.inquirer, resolutionContext.rootInquirer) ||
wrapper.isInRequestScope(resolutionContext.contextId, resolutionContext.inquirer) ||
wrapper.isLazyTransient(resolutionContext.contextId, resolutionContext.inquirer) ||
wrapper.isExplicitlyRequested(resolutionContext.contextId, resolutionContext.inquirer));
}
/**
* Request-scoped and transient providers are not created at bootstrap, so
* their dependencies are only looked up here, without creating anything.
* A missing dependency then fails at bootstrap rather than on first use.
* Every lookup is awaited before the first failure is thrown.
*/
async lookupSkippedProviderDependencies(wrapper, moduleRef) {
const isFactoryProvider = !isNil(wrapper.inject);
if (!isFactoryProvider && !isFunction(wrapper.metatype)) {
return;
}
const [dependencies, optionalDependenciesIds] = isFactoryProvider
? this.getFactoryProviderDependencies(wrapper)
: this.getClassDependencies(wrapper);
const properties = isFactoryProvider
? []
: this.reflectProperties(wrapper.metatype);
const lookup = async (param, dependencyContext, keyOrIndex, isOptional) => {
try {
await this.resolveSingleParam(wrapper, param, dependencyContext, moduleRef, { contextId: STATIC_CONTEXT }, keyOrIndex);
}
catch (err) {
if (!isOptional) {
throw err;
}
}
};
const results = await Promise.allSettled([
...dependencies.map((param, index) => lookup(param, { index, dependencies }, index, optionalDependenciesIds.includes(index))),
...properties.map(({ key, name, isOptional }) => lookup(name, { key, name: name }, key, !!isOptional)),
]);
const failure = results.find((result) => result.status === 'rejected');
if (failure) {
throw failure.reason;
}
}
shouldSkipProviderLoading(wrapper, resolutionContext) {
const isStaticContext = resolutionContext.contextId === STATIC_CONTEXT;
const hasNoInquirer = !resolutionContext.inquirer;
const isTopLevelStaticTransientOrRequestProvider = hasNoInquirer && (wrapper.isTransient || wrapper.scope === Scope.REQUEST);
const isStaticInquirerOutsideResolutionContext = !!resolutionContext.inquirer &&
!this.isInContext(resolutionContext.inquirer, this.createResolutionContext(resolutionContext.contextId, resolutionContext.inquirer, undefined, resolutionContext.rootInquirer));
const shouldSkipForStaticBootstrap = isStaticContext &&
(isTopLevelStaticTransientOrRequestProvider ||
isStaticInquirerOutsideResolutionContext);
return shouldSkipForStaticBootstrap;
}
/**
* For nested TRANSIENT dependencies (TRANSIENT -> TRANSIENT) in non-static contexts,
* returns parentInquirer to ensure each parent TRANSIENT gets its own instance.
* This is necessary because in REQUEST/DURABLE scopes, the same TRANSIENT wrapper
* can be used by multiple parents, causing nested TRANSIENTs to be shared incorrectly.
* For non-TRANSIENT -> TRANSIENT, returns inquirer (current wrapper being created).
*/
getEffectiveInquirer(dependency, resolutionContext, parentInquirer) {
const { inquirer, contextId, rootInquirer } = resolutionContext;
if (dependency?.isTransient && inquirer?.isTransient && parentInquirer) {
if (contextId === STATIC_CONTEXT) {
return rootInquirer ?? parentInquirer;
}
return parentInquirer;
}
return inquirer;
}
getEffectiveInquirerId(dependency, resolutionContext, parentInquirer) {
const { contextId, inquirer, effectiveInquirerId } = resolutionContext;
if (contextId === STATIC_CONTEXT &&
dependency?.isTransient &&
inquirer?.isTransient &&
parentInquirer) {
const baseInquirerId = effectiveInquirerId ?? this.getInquirerId(parentInquirer);
return `${baseInquirerId}:${inquirer.id}`;
}
const effectiveInquirer = this.getEffectiveInquirer(dependency, resolutionContext, parentInquirer);
return this.getInquirerId(effectiveInquirer);
}
getStaticTransientResolutionContext(resolutionContext, parentInquirer) {
const { contextId, inquirer, effectiveInquirerId } = resolutionContext;
if (contextId === STATIC_CONTEXT &&
inquirer?.isTransient &&
parentInquirer) {
const baseInquirerId = effectiveInquirerId ?? this.getInquirerId(parentInquirer);
return this.createResolutionContext(contextId, inquirer, `${baseInquirerId}:${inquirer.id}`);
}
return resolutionContext;
}
getEffectiveResolutionContext(dependency, resolutionContext, parentInquirer) {
const effectiveInquirer = this.getEffectiveInquirer(dependency, resolutionContext, parentInquirer);
return this.createResolutionContext(resolutionContext.contextId, effectiveInquirer, this.getEffectiveInquirerId(dependency, resolutionContext, parentInquirer),
// The root inquirer is the one of `inquirer`
effectiveInquirer === resolutionContext.inquirer
? resolutionContext.rootInquirer
: undefined);
}
hasDenseCtorMetadata(wrapper, inject, metadata) {
if (!metadata) {
return false;
}
// The fast path requires a fully populated metadata array.
// While another request is still registering dependency metadata,
// sparse entries here would feed request-scoped factories `undefined`.
const expectedDepsLength = !isNil(inject)
? inject.length
: wrapper.metatype
? this.reflectConstructorParams(wrapper.metatype).length
: 0;
if (metadata.length !== expectedDepsLength) {
return false;
}
for (let index = 0; index < expectedDepsLength; index++) {
if (metadata[index] === undefined) {
return false;
}
}
return true;
}
hasCompletePropertiesMetadata(wrapper, metadata) {
if (!metadata) {
return false;
}
// The fast path requires every injected property to be registered.
// While another request is still registering dependency metadata,
// the missing properties would be left `undefined`.
const properties = wrapper.metatype
? this.reflectProperties(wrapper.metatype)
: [];
return properties.every(({ key }) => metadata.some(item => item.key === key));
}
resolveScopedComponentHost(item, contextId, inquirer, parentInquirer) {
return this.isInquirerRequest(item, parentInquirer)
? parentInquirer
: this.resolveComponentHost(item.host, item, this.getEffectiveResolutionContext(item, this.createResolutionContext(contextId, inquirer), parentInquirer));
}
isInquirerRequest(item, parentInquirer) {
return item.isTransient && item.name === INQUIRER && parentInquirer;
}
isInquirer(param, parentInquirer) {
return param === INQUIRER && parentInquirer;
}
addDependencyMetadata(keyOrIndex, hostWrapper, instanceWrapper) {
if (isSymbol(keyOrIndex) || isString(keyOrIndex)) {
hostWrapper.addPropertiesMetadata(keyOrIndex, instanceWrapper);
}
else {
hostWrapper.addCtorMetadata(keyOrIndex, instanceWrapper);
}
}
getTokenName(token) {
return isFunction(token) ? token.name : token.toString();
}
printResolvingDependenciesLog(token, inquirer) {
if (!isDebugMode()) {
return;
}
const tokenName = this.getTokenName(token);
const dependentName = (inquirer?.name && inquirer.name.toString?.()) ?? 'unknown';
const isAlias = dependentName === tokenName;
const messageToPrint = `Resolving dependency ${clc.cyanBright(tokenName)}${clc.green(' in the ')}${clc.yellow(dependentName)}${clc.green(` provider ${isAlias ? '(alias)' : ''}`)}`;
this.logger.log(messageToPrint);
}
printLookingForProviderLog(token, moduleRef) {
if (!isDebugMode()) {
return;
}
const tokenName = this.getTokenName(token);
const moduleRefName = moduleRef?.metatype?.name ?? 'unknown';
this.logger.log(`Looking for ${clc.cyanBright(tokenName)}${clc.green(' in ')}${clc.magentaBright(moduleRefName)}`);
}
printFoundInModuleLog(token, moduleRef) {
if (!isDebugMode()) {
return;
}
const tokenName = this.getTokenName(token);
const moduleRefName = moduleRef?.metatype?.name ?? 'unknown';
this.logger.log(`Found ${clc.cyanBright(tokenName)}${clc.green(' in ')}${clc.magentaBright(moduleRefName)}`);
}
getContextId(contextId, instanceWrapper) {
return contextId.getParent
? contextId.getParent({
token: instanceWrapper.token,
// A transient instance belongs to the consumer it was created for,
// which is not necessarily durable, so it is never shared through
// a durable sub-tree
isTreeDurable: !instanceWrapper.isTransient &&
instanceWrapper.isDependencyTreeDurable(),
})
: contextId;
}
/**
* An instance kept in another context than the one being resolved (a
* durable one, see `ContextIdStrategy`) outlives the request it is built
* for, and is handed to the next requests mapped to that context. When the
* strategy supplies a payload, that is its `REQUEST`, never the request
* that the entry point (a controller that is not durable, a middleware, a
* message handler, ...) registered as the `REQUEST` of the context.
*/
shouldInjectDurablePayload(dependency, dependent, parentInquirer, contextId) {
if (dependency?.token !== REQUEST || isUndefined(contextId.payload)) {
return false;
}
// An instance of a transient provider belongs to its consumer
const host = dependent?.isTransient ? parentInquirer : dependent;
return !!host && this.getContextId(contextId, host) !== contextId;
}
getNowTimestamp() {
return performance.now();
}
}