@nestjs/core 10.4.9 → 10.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/injector/opaque-key-factory/by-reference-module-opaque-key-factory.d.ts +15 -0
- package/injector/opaque-key-factory/by-reference-module-opaque-key-factory.js +40 -0
- package/injector/opaque-key-factory/deep-hashed-module-opaque-key-factory.d.ts +15 -0
- package/injector/opaque-key-factory/deep-hashed-module-opaque-key-factory.js +83 -0
- package/injector/opaque-key-factory/interfaces/module-opaque-key-factory.interface.d.ts +18 -0
- package/injector/opaque-key-factory/interfaces/module-opaque-key-factory.interface.js +2 -0
- package/package.json +2 -2
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common/interfaces/modules/dynamic-module.interface';
|
|
2
|
+
import { ForwardReference } from '@nestjs/common/interfaces/modules/forward-reference.interface';
|
|
3
|
+
import { Type } from '@nestjs/common/interfaces/type.interface';
|
|
4
|
+
import { ModuleOpaqueKeyFactory } from './interfaces/module-opaque-key-factory.interface';
|
|
5
|
+
export declare class ByReferenceModuleOpaqueKeyFactory implements ModuleOpaqueKeyFactory {
|
|
6
|
+
private readonly keyGenerationStrategy;
|
|
7
|
+
constructor(options?: {
|
|
8
|
+
keyGenerationStrategy: 'random' | 'shallow';
|
|
9
|
+
});
|
|
10
|
+
createForStatic(moduleCls: Type, originalRef?: Type | ForwardReference): string;
|
|
11
|
+
createForDynamic(moduleCls: Type<unknown>, dynamicMetadata: Omit<DynamicModule, 'module'>, originalRef: DynamicModule | ForwardReference): string;
|
|
12
|
+
private getOrCreateModuleId;
|
|
13
|
+
private hashString;
|
|
14
|
+
private generateRandomString;
|
|
15
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ByReferenceModuleOpaqueKeyFactory = void 0;
|
|
4
|
+
const random_string_generator_util_1 = require("@nestjs/common/utils/random-string-generator.util");
|
|
5
|
+
const crypto_1 = require("crypto");
|
|
6
|
+
const K_MODULE_ID = Symbol('K_MODULE_ID');
|
|
7
|
+
class ByReferenceModuleOpaqueKeyFactory {
|
|
8
|
+
constructor(options) {
|
|
9
|
+
this.keyGenerationStrategy = options?.keyGenerationStrategy ?? 'random';
|
|
10
|
+
}
|
|
11
|
+
createForStatic(moduleCls, originalRef = moduleCls) {
|
|
12
|
+
return this.getOrCreateModuleId(moduleCls, undefined, originalRef);
|
|
13
|
+
}
|
|
14
|
+
createForDynamic(moduleCls, dynamicMetadata, originalRef) {
|
|
15
|
+
return this.getOrCreateModuleId(moduleCls, dynamicMetadata, originalRef);
|
|
16
|
+
}
|
|
17
|
+
getOrCreateModuleId(moduleCls, dynamicMetadata, originalRef) {
|
|
18
|
+
if (originalRef[K_MODULE_ID]) {
|
|
19
|
+
return originalRef[K_MODULE_ID];
|
|
20
|
+
}
|
|
21
|
+
let moduleId;
|
|
22
|
+
if (this.keyGenerationStrategy === 'random') {
|
|
23
|
+
moduleId = this.generateRandomString();
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
moduleId = dynamicMetadata
|
|
27
|
+
? `${this.generateRandomString()}:${this.hashString(moduleCls.name + JSON.stringify(dynamicMetadata))}`
|
|
28
|
+
: `${this.generateRandomString()}:${this.hashString(moduleCls.toString())}`;
|
|
29
|
+
}
|
|
30
|
+
originalRef[K_MODULE_ID] = moduleId;
|
|
31
|
+
return moduleId;
|
|
32
|
+
}
|
|
33
|
+
hashString(value) {
|
|
34
|
+
return (0, crypto_1.createHash)('sha256').update(value).digest('hex');
|
|
35
|
+
}
|
|
36
|
+
generateRandomString() {
|
|
37
|
+
return (0, random_string_generator_util_1.randomStringGenerator)();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.ByReferenceModuleOpaqueKeyFactory = ByReferenceModuleOpaqueKeyFactory;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common/interfaces/modules/dynamic-module.interface';
|
|
2
|
+
import { Type } from '@nestjs/common/interfaces/type.interface';
|
|
3
|
+
import { ModuleOpaqueKeyFactory } from './interfaces/module-opaque-key-factory.interface';
|
|
4
|
+
export declare class DeepHashedModuleOpaqueKeyFactory implements ModuleOpaqueKeyFactory {
|
|
5
|
+
private readonly moduleIdsCache;
|
|
6
|
+
private readonly moduleTokenCache;
|
|
7
|
+
private readonly logger;
|
|
8
|
+
createForStatic(moduleCls: Type): string;
|
|
9
|
+
createForDynamic(moduleCls: Type<unknown>, dynamicMetadata: Omit<DynamicModule, 'module'>): string;
|
|
10
|
+
getStringifiedOpaqueToken(opaqueToken: object | undefined): string;
|
|
11
|
+
getModuleId(metatype: Type<unknown>): string;
|
|
12
|
+
getModuleName(metatype: Type<any>): string;
|
|
13
|
+
private hashString;
|
|
14
|
+
private replacer;
|
|
15
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DeepHashedModuleOpaqueKeyFactory = void 0;
|
|
4
|
+
const logger_service_1 = require("@nestjs/common/services/logger.service");
|
|
5
|
+
const random_string_generator_util_1 = require("@nestjs/common/utils/random-string-generator.util");
|
|
6
|
+
const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
|
7
|
+
const crypto_1 = require("crypto");
|
|
8
|
+
const fast_safe_stringify_1 = require("fast-safe-stringify");
|
|
9
|
+
const CLASS_STR = 'class ';
|
|
10
|
+
const CLASS_STR_LEN = CLASS_STR.length;
|
|
11
|
+
class DeepHashedModuleOpaqueKeyFactory {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.moduleIdsCache = new WeakMap();
|
|
14
|
+
this.moduleTokenCache = new Map();
|
|
15
|
+
this.logger = new logger_service_1.Logger(DeepHashedModuleOpaqueKeyFactory.name, {
|
|
16
|
+
timestamp: true,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
createForStatic(moduleCls) {
|
|
20
|
+
const moduleId = this.getModuleId(moduleCls);
|
|
21
|
+
const moduleName = this.getModuleName(moduleCls);
|
|
22
|
+
const key = `${moduleId}_${moduleName}`;
|
|
23
|
+
if (this.moduleTokenCache.has(key)) {
|
|
24
|
+
return this.moduleTokenCache.get(key);
|
|
25
|
+
}
|
|
26
|
+
const hash = this.hashString(key);
|
|
27
|
+
this.moduleTokenCache.set(key, hash);
|
|
28
|
+
return hash;
|
|
29
|
+
}
|
|
30
|
+
createForDynamic(moduleCls, dynamicMetadata) {
|
|
31
|
+
const moduleId = this.getModuleId(moduleCls);
|
|
32
|
+
const moduleName = this.getModuleName(moduleCls);
|
|
33
|
+
const opaqueToken = {
|
|
34
|
+
id: moduleId,
|
|
35
|
+
module: moduleName,
|
|
36
|
+
dynamic: dynamicMetadata,
|
|
37
|
+
};
|
|
38
|
+
const start = performance.now();
|
|
39
|
+
const opaqueTokenString = this.getStringifiedOpaqueToken(opaqueToken);
|
|
40
|
+
const timeSpentInMs = performance.now() - start;
|
|
41
|
+
if (timeSpentInMs > 10) {
|
|
42
|
+
const formattedTimeSpent = timeSpentInMs.toFixed(2);
|
|
43
|
+
this.logger.warn(`The module "${opaqueToken.module}" is taking ${formattedTimeSpent}ms to serialize, this may be caused by larger objects statically assigned to the module. Consider changing the "moduleIdGeneratorAlgorithm" option to "reference" to improve the performance.`);
|
|
44
|
+
}
|
|
45
|
+
return this.hashString(opaqueTokenString);
|
|
46
|
+
}
|
|
47
|
+
getStringifiedOpaqueToken(opaqueToken) {
|
|
48
|
+
// Uses safeStringify instead of JSON.stringify to support circular dynamic modules
|
|
49
|
+
// The replacer function is also required in order to obtain real class names
|
|
50
|
+
// instead of the unified "Function" key
|
|
51
|
+
return opaqueToken ? (0, fast_safe_stringify_1.default)(opaqueToken, this.replacer) : '';
|
|
52
|
+
}
|
|
53
|
+
getModuleId(metatype) {
|
|
54
|
+
let moduleId = this.moduleIdsCache.get(metatype);
|
|
55
|
+
if (moduleId) {
|
|
56
|
+
return moduleId;
|
|
57
|
+
}
|
|
58
|
+
moduleId = (0, random_string_generator_util_1.randomStringGenerator)();
|
|
59
|
+
this.moduleIdsCache.set(metatype, moduleId);
|
|
60
|
+
return moduleId;
|
|
61
|
+
}
|
|
62
|
+
getModuleName(metatype) {
|
|
63
|
+
return metatype.name;
|
|
64
|
+
}
|
|
65
|
+
hashString(value) {
|
|
66
|
+
return (0, crypto_1.createHash)('sha256').update(value).digest('hex');
|
|
67
|
+
}
|
|
68
|
+
replacer(key, value) {
|
|
69
|
+
if ((0, shared_utils_1.isFunction)(value)) {
|
|
70
|
+
const funcAsString = value.toString();
|
|
71
|
+
const isClass = funcAsString.slice(0, CLASS_STR_LEN) === CLASS_STR;
|
|
72
|
+
if (isClass) {
|
|
73
|
+
return value.name;
|
|
74
|
+
}
|
|
75
|
+
return funcAsString;
|
|
76
|
+
}
|
|
77
|
+
if ((0, shared_utils_1.isSymbol)(value)) {
|
|
78
|
+
return value.toString();
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.DeepHashedModuleOpaqueKeyFactory = DeepHashedModuleOpaqueKeyFactory;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common/interfaces/modules/dynamic-module.interface';
|
|
2
|
+
import { ForwardReference } from '@nestjs/common/interfaces/modules/forward-reference.interface';
|
|
3
|
+
import { Type } from '@nestjs/common/interfaces/type.interface';
|
|
4
|
+
export interface ModuleOpaqueKeyFactory {
|
|
5
|
+
/**
|
|
6
|
+
* Creates a unique opaque key for the given static module.
|
|
7
|
+
* @param moduleCls A static module class.
|
|
8
|
+
* @param originalRef Original object reference. In most cases, it's the same as `moduleCls`.
|
|
9
|
+
*/
|
|
10
|
+
createForStatic(moduleCls: Type, originalRef: Type | ForwardReference): string;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a unique opaque key for the given dynamic module.
|
|
13
|
+
* @param moduleCls A dynamic module class reference.
|
|
14
|
+
* @param dynamicMetadata Dynamic module metadata.
|
|
15
|
+
* @param originalRef Original object reference.
|
|
16
|
+
*/
|
|
17
|
+
createForDynamic(moduleCls: Type<unknown>, dynamicMetadata: Omit<DynamicModule, 'module'>, originalRef: DynamicModule | ForwardReference): string;
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs/core",
|
|
3
|
-
"version": "10.4.
|
|
3
|
+
"version": "10.4.10",
|
|
4
4
|
"description": "Nest - modern, fast, powerful node.js web framework (@core)",
|
|
5
5
|
"author": "Kamil Mysliwiec",
|
|
6
6
|
"license": "MIT",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"uid": "2.0.2"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@nestjs/common": "10.4.
|
|
39
|
+
"@nestjs/common": "10.4.10"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@nestjs/common": "^10.0.0",
|