@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.
105 lines (104 loc) • 4.88 kB
JavaScript
import { Scope } from '@nestjs/common';
import { randomStringGenerator } from '@nestjs/common/internal';
import { RequestScopedCreateException } from '../errors/exceptions/request-scoped-create.exception.js';
import { getClassScope } from '../helpers/get-class-scope.js';
import { isDurable } from '../helpers/is-durable.js';
import { AbstractInstanceResolver } from './abstract-instance-resolver.js';
import { STATIC_CONTEXT } from './constants.js';
import { Injector } from './injector.js';
import { InstanceLinksHost } from './instance-links-host.js';
import { INSTANCE_ID_SYMBOL, InstanceWrapper, } from './instance-wrapper.js';
export class ModuleRef extends AbstractInstanceResolver {
container;
injector;
_instanceLinksHost;
get instanceLinksHost() {
if (!this._instanceLinksHost) {
this._instanceLinksHost = new InstanceLinksHost(this.container);
}
return this._instanceLinksHost;
}
constructor(container) {
super();
this.container = container;
const contextOptions = container.contextOptions;
this.injector = new Injector({
preview: contextOptions?.preview ?? false,
snapshot: contextOptions?.snapshot,
instanceDecorator: contextOptions?.instrument?.instanceDecorator,
});
}
introspect(token) {
const { wrapperRef: wrapper } = this.instanceLinksHost.get(token);
// An alias (`useExisting`) has the scope of the provider it stands for
const wrapperRef = wrapper.isAlias
? this.injector.getAliasedWrapper(wrapper)
: wrapper;
let scope = Scope.DEFAULT;
if (!wrapperRef.isDependencyTreeStatic()) {
scope = Scope.REQUEST;
}
else if (wrapperRef.isTransient) {
scope = Scope.TRANSIENT;
}
return { scope };
}
registerRequestByContextId(request, contextId) {
this.container.registerRequestProvider(request, contextId);
}
async instantiateClass(type, moduleRef, contextId) {
const wrapper = new InstanceWrapper({
name: type && type.name,
metatype: type,
isResolved: false,
scope: getClassScope(type),
durable: isDurable(type),
host: moduleRef,
// Thrown away once the instance is created, and never a node of the
// serialized graph, so it takes no deterministic id (snapshot mode)
// from a registry that would grow with every call
[INSTANCE_ID_SYMBOL]: randomStringGenerator(),
});
if (type?.prototype) {
wrapper.setInstanceByContextId(contextId ?? STATIC_CONTEXT, {
instance: Object.create(type.prototype),
isResolved: false,
isPending: false,
});
}
return new Promise((resolve, reject) => {
let isCreated = false;
const loadInstance = async () => {
const callback = async (instances) => {
const properties = await this.injector.resolveProperties(wrapper, moduleRef, undefined, {
contextId: contextId ?? STATIC_CONTEXT,
inquirer: wrapper,
});
// In the static context, a request-scoped provider is a placeholder
// that is never constructed and is shared by every caller. Unlike a
// constructor parameter, one injected into a property (of the class
// or of one of its dependencies) does not skip this callback, so
// return without creating the instance, and create() rejects below
if ((contextId ?? STATIC_CONTEXT) === STATIC_CONTEXT &&
wrapper.introspectDepsAttribute(dependencies => dependencies.some(item => !item.isDependencyTreeStatic()))) {
return;
}
await this.injector.waitForDeferredLoads(contextId ?? STATIC_CONTEXT);
const instance = new type(...instances);
this.injector.applyProperties(instance, properties);
isCreated = true;
resolve(instance);
};
await this.injector.resolveConstructorParams(wrapper, moduleRef, undefined, callback, {
contextId: contextId ?? STATIC_CONTEXT,
inquirer: wrapper,
});
// The callback is skipped when a dependency is request-scoped
if (!isCreated) {
reject(new RequestScopedCreateException(type.name));
}
};
void loadInstance().catch(reject);
}).finally(() => wrapper.detachFromDependencies());
}
}