-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
module-token-factory.ts
105 lines (88 loc) 路 3.22 KB
/
module-token-factory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { DynamicModule } from '@nestjs/common';
import { Type } from '@nestjs/common/interfaces/type.interface';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import { isFunction, isSymbol } from '@nestjs/common/utils/shared.utils';
import { createHash } from 'crypto';
import stringify from 'fast-safe-stringify';
import * as hash from 'object-hash';
export class ModuleToken implements DynamicModule {
constructor(props: DynamicModule) {
this.moduleId = randomStringGenerator();
this.module = props.module;
this.global = props.global;
this.imports = props.imports;
this.controllers = props.controllers;
this.providers = props.providers;
this.exports = props.exports;
}
moduleId: string;
module: DynamicModule['module'];
global?: DynamicModule['global'];
imports?: DynamicModule['imports'];
controllers?: DynamicModule['controllers'];
providers?: DynamicModule['providers'];
exports?: DynamicModule['exports'];
}
export class ModuleTokenFactory {
private readonly moduleIdsCache = new WeakMap<Type<unknown>, string>();
private readonly moduleTokensCache = new Map<string, string>();
public create(
metatype: Type<unknown>,
dynamicModuleMetadata?: Partial<DynamicModule> | undefined,
): string {
if (dynamicModuleMetadata instanceof ModuleToken)
return dynamicModuleMetadata.moduleId;
const moduleId = this.getModuleId(metatype);
if (!dynamicModuleMetadata)
return this.getFastModuleToken(moduleId, this.getModuleName(metatype));
const opaqueToken = {
id: moduleId,
module: this.getModuleName(metatype),
dynamic: this.getDynamicMetadataToken(dynamicModuleMetadata),
};
return hash(opaqueToken, { ignoreUnknown: true });
}
public getFastModuleToken(moduleId: string, moduleName: string): string {
const key = `${moduleId}_${moduleName}`;
if (this.moduleTokensCache.has(key)) return this.moduleTokensCache.get(key);
const hash = createHash('sha1').update(key).digest('hex');
this.moduleTokensCache.set(key, hash);
return hash;
}
public getDynamicMetadataToken(
dynamicModuleMetadata: Partial<DynamicModule> | undefined,
): string {
// 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
? stringify(dynamicModuleMetadata, this.replacer)
: '';
}
public getModuleId(metatype: Type<unknown>): string {
let moduleId = this.moduleIdsCache.get(metatype);
if (moduleId) {
return moduleId;
}
moduleId = randomStringGenerator();
this.moduleIdsCache.set(metatype, moduleId);
return moduleId;
}
public getModuleName(metatype: Type<any>): string {
return metatype.name;
}
private replacer(key: string, value: any) {
if (isFunction(value)) {
const funcAsString = value.toString();
const isClass = /^class\s/.test(funcAsString);
if (isClass) {
return value.name;
}
return hash(funcAsString, { ignoreUnknown: true });
}
if (isSymbol(value)) {
return value.toString();
}
return value;
}
}