@nestjs/core 10.4.8 → 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/injector.js +10 -3
- package/injector/module.d.ts +1 -0
- package/injector/module.js +8 -0
- 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/middleware/middleware-module.js +3 -1
- package/package.json +2 -2
- package/router/router-explorer.js +3 -1
package/injector/injector.js
CHANGED
|
@@ -153,13 +153,20 @@ class Injector {
|
|
|
153
153
|
}
|
|
154
154
|
getFactoryProviderDependencies(wrapper) {
|
|
155
155
|
const optionalDependenciesIds = [];
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
/**
|
|
157
|
+
* Same as the internal utility function `isOptionalFactoryDependency` from `@nestjs/common`.
|
|
158
|
+
* We are duplicating it here because that one is not supposed to be exported.
|
|
159
|
+
*/
|
|
160
|
+
function isOptionalFactoryDependency(value) {
|
|
161
|
+
return (!(0, shared_utils_1.isUndefined)(value.token) &&
|
|
162
|
+
!(0, shared_utils_1.isUndefined)(value.optional) &&
|
|
163
|
+
!value.prototype);
|
|
164
|
+
}
|
|
158
165
|
const mapFactoryProviderInjectArray = (item, index) => {
|
|
159
166
|
if (typeof item !== 'object') {
|
|
160
167
|
return item;
|
|
161
168
|
}
|
|
162
|
-
if (
|
|
169
|
+
if (isOptionalFactoryDependency(item)) {
|
|
163
170
|
if (item.optional) {
|
|
164
171
|
optionalDependenciesIds.push(index);
|
|
165
172
|
}
|
package/injector/module.d.ts
CHANGED
package/injector/module.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Module = void 0;
|
|
4
4
|
const constants_1 = require("@nestjs/common/constants");
|
|
5
|
+
const interfaces_1 = require("@nestjs/common/interfaces");
|
|
5
6
|
const random_string_generator_util_1 = require("@nestjs/common/utils/random-string-generator.util");
|
|
6
7
|
const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
|
7
8
|
const iterare_1 = require("iterare");
|
|
@@ -159,6 +160,10 @@ class Module {
|
|
|
159
160
|
}
|
|
160
161
|
return this.addCustomProvider(provider, this._providers, enhancerSubtype);
|
|
161
162
|
}
|
|
163
|
+
const isAlreadyDeclared = this._providers.has(provider);
|
|
164
|
+
if (this.isTransientProvider(provider) && isAlreadyDeclared) {
|
|
165
|
+
return provider;
|
|
166
|
+
}
|
|
162
167
|
this._providers.set(provider, new instance_wrapper_1.InstanceWrapper({
|
|
163
168
|
token: provider,
|
|
164
169
|
name: provider.name,
|
|
@@ -416,5 +421,8 @@ class Module {
|
|
|
416
421
|
const key = this.name?.toString() ?? this.token?.toString();
|
|
417
422
|
return key ? uuid_factory_1.UuidFactory.get(`${prefix}_${key}`) : (0, random_string_generator_util_1.randomStringGenerator)();
|
|
418
423
|
}
|
|
424
|
+
isTransientProvider(provider) {
|
|
425
|
+
return (0, get_class_scope_1.getClassScope)(provider) === interfaces_1.Scope.TRANSIENT;
|
|
426
|
+
}
|
|
419
427
|
}
|
|
420
428
|
exports.Module = Module;
|
|
@@ -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
|
+
}
|
|
@@ -182,7 +182,9 @@ class MiddlewareModule {
|
|
|
182
182
|
writable: false,
|
|
183
183
|
configurable: false,
|
|
184
184
|
});
|
|
185
|
-
const requestProviderValue = isTreeDurable
|
|
185
|
+
const requestProviderValue = isTreeDurable
|
|
186
|
+
? contextId.payload
|
|
187
|
+
: Object.assign(request, contextId.payload);
|
|
186
188
|
this.container.registerRequestProvider(requestProviderValue, contextId);
|
|
187
189
|
}
|
|
188
190
|
return contextId;
|
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",
|
|
@@ -196,7 +196,9 @@ class RouterExplorer {
|
|
|
196
196
|
writable: false,
|
|
197
197
|
configurable: false,
|
|
198
198
|
});
|
|
199
|
-
const requestProviderValue = isTreeDurable
|
|
199
|
+
const requestProviderValue = isTreeDurable
|
|
200
|
+
? contextId.payload
|
|
201
|
+
: Object.assign(request, contextId.payload);
|
|
200
202
|
this.container.registerRequestProvider(requestProviderValue, contextId);
|
|
201
203
|
}
|
|
202
204
|
return contextId;
|