UNPKG

@nestjs/core

Version:

Nest - modern, fast, powerful node.js web framework (@core)

49 lines (48 loc) 1.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("@nestjs/common/constants"); const fast_safe_stringify_1 = require("fast-safe-stringify"); const hash = require("object-hash"); class ModuleTokenFactory { create(metatype, scope, dynamicModuleMetadata) { const moduleScope = this.reflectScope(metatype); const isSingleScoped = moduleScope === true; const opaqueToken = { module: this.getModuleName(metatype), dynamic: this.getDynamicMetadataToken(dynamicModuleMetadata), scope: isSingleScoped ? this.getScopeStack(scope) : moduleScope, }; return hash(opaqueToken); } getDynamicMetadataToken(dynamicModuleMetadata) { // Uses safeStringify instead of JSON.stringify to support circular dynamic modules // The replacer function is also required in order to obtain real class names // instead of the unified "Function" key return dynamicModuleMetadata ? fast_safe_stringify_1.default(dynamicModuleMetadata, this.replacer) : ''; } getModuleName(metatype) { return metatype.name; } getScopeStack(scope) { const reversedScope = scope.reverse(); const firstGlobalIndex = reversedScope.findIndex(s => this.reflectScope(s) === 'global'); scope.reverse(); const stack = firstGlobalIndex >= 0 ? scope.slice(scope.length - firstGlobalIndex - 1) : scope; return stack.map(module => module.name); } reflectScope(metatype) { const scope = Reflect.getMetadata(constants_1.SHARED_MODULE_METADATA, metatype); return scope ? scope : 'global'; } replacer(key, value) { if (typeof value === 'function') { return value.name; } return value; } } exports.ModuleTokenFactory = ModuleTokenFactory;