UNPKG

@nestjs/core

Version:

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

47 lines (46 loc) 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const random_string_generator_util_1 = require("@nestjs/common/utils/random-string-generator.util"); const fast_safe_stringify_1 = require("fast-safe-stringify"); const hash = require("object-hash"); class ModuleTokenFactory { constructor() { this.moduleIdsCache = new WeakMap(); } create(metatype, dynamicModuleMetadata) { const moduleId = this.getModuleId(metatype); const opaqueToken = { id: moduleId, module: this.getModuleName(metatype), dynamic: this.getDynamicMetadataToken(dynamicModuleMetadata), }; return hash(opaqueToken, { ignoreUnknown: true }); } 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) : ''; } getModuleId(metatype) { let moduleId = this.moduleIdsCache.get(metatype); if (moduleId) { return moduleId; } moduleId = random_string_generator_util_1.randomStringGenerator(); this.moduleIdsCache.set(metatype, moduleId); return moduleId; } getModuleName(metatype) { return metatype.name; } replacer(key, value) { if (typeof value === 'function') { return value.name; } return value; } } exports.ModuleTokenFactory = ModuleTokenFactory;