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.

40 lines (39 loc) • 1.64 kB
import { createHash } from 'crypto'; import { randomStringGenerator } from '@nestjs/common/internal'; const K_MODULE_ID = Symbol('K_MODULE_ID'); export class ByReferenceModuleOpaqueKeyFactory { keyGenerationStrategy; constructor(options) { this.keyGenerationStrategy = options?.keyGenerationStrategy ?? 'random'; } createForStatic(moduleCls, originalRef = moduleCls) { return this.getOrCreateModuleId(moduleCls, undefined, originalRef); } createForDynamic(moduleCls, dynamicMetadata, originalRef) { return this.getOrCreateModuleId(moduleCls, dynamicMetadata, originalRef); } getOrCreateModuleId(moduleCls, dynamicMetadata, originalRef) { // Own property only: a subclass would inherit the id of the module it extends. if (Object.prototype.hasOwnProperty.call(originalRef, K_MODULE_ID)) { return originalRef[K_MODULE_ID]; } let moduleId; if (this.keyGenerationStrategy === 'random') { moduleId = this.generateRandomString(); } else { const delimiter = ':'; moduleId = dynamicMetadata ? `${this.generateRandomString()}${delimiter}${this.hashString(moduleCls.name + JSON.stringify(dynamicMetadata))}` : `${this.generateRandomString()}${delimiter}${this.hashString(moduleCls.toString())}`; } originalRef[K_MODULE_ID] = moduleId; return moduleId; } hashString(value) { return createHash('sha256').update(value).digest('hex'); } generateRandomString() { return randomStringGenerator(); } }