@nestjs/core 11.0.0-next.3 → 11.0.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  (The MIT License)
2
2
 
3
- Copyright (c) 2017-2024 Kamil Mysliwiec <https://kamilmysliwiec.com>
3
+ Copyright (c) 2017-2025 Kamil Mysliwiec <https://kamilmysliwiec.com>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
@@ -1,14 +1,13 @@
1
1
  import { HttpServer, RequestMethod, VersioningOptions } from '@nestjs/common';
2
2
  import { RequestHandler, VersionValue } from '@nestjs/common/interfaces';
3
- import { CorsOptions, CorsOptionsDelegate } from '@nestjs/common/interfaces/external/cors-options.interface';
4
3
  import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-application-options.interface';
5
4
  /**
6
5
  * @publicApi
7
6
  */
8
7
  export declare abstract class AbstractHttpAdapter<TServer = any, TRequest = any, TResponse = any> implements HttpServer<TRequest, TResponse> {
9
- protected instance?: any;
8
+ protected instance?: any | undefined;
10
9
  protected httpServer: TServer;
11
- constructor(instance?: any);
10
+ constructor(instance?: any | undefined);
12
11
  init(): Promise<void>;
13
12
  use(...args: any[]): any;
14
13
  get(handler: RequestHandler): any;
@@ -67,7 +66,7 @@ export declare abstract class AbstractHttpAdapter<TServer = any, TRequest = any,
67
66
  abstract isHeadersSent(response: any): any;
68
67
  abstract setHeader(response: any, name: string, value: string): any;
69
68
  abstract registerParserMiddleware(prefix?: string, rawBody?: boolean): any;
70
- abstract enableCors(options: CorsOptions | CorsOptionsDelegate<TRequest>, prefix?: string): any;
69
+ abstract enableCors(options?: any, prefix?: string): any;
71
70
  abstract createMiddlewareFactory(requestMethod: RequestMethod): ((path: string, callback: Function) => any) | Promise<(path: string, callback: Function) => any>;
72
71
  abstract getType(): string;
73
72
  abstract applyVersionFilter(handler: Function, version: VersionValue, versioningOptions: VersioningOptions): (req: TRequest, res: TResponse, next: () => void) => Function;
@@ -7,7 +7,7 @@ export declare const UNDEFINED_FORWARDREF_MESSAGE: (scope: Type<any>[]) => strin
7
7
  export declare const INVALID_MODULE_MESSAGE: (parentModule: any, index: number, scope: any[]) => string;
8
8
  export declare const USING_INVALID_CLASS_AS_A_MODULE_MESSAGE: (metatypeUsedAsAModule: Type | ForwardReference, scope: any[]) => string;
9
9
  export declare const UNDEFINED_MODULE_MESSAGE: (parentModule: any, index: number, scope: any[]) => string;
10
- export declare const UNKNOWN_EXPORT_MESSAGE: (token: (string | symbol) | undefined, module: string) => string;
10
+ export declare const UNKNOWN_EXPORT_MESSAGE: (token: string | symbol | undefined, module: string) => string;
11
11
  export declare const INVALID_CLASS_MESSAGE: (text: TemplateStringsArray, value: any) => string;
12
12
  export declare const INVALID_CLASS_SCOPE_MESSAGE: (text: TemplateStringsArray, name: string | undefined) => string;
13
13
  export declare const UNKNOWN_REQUEST_MAPPING: (metatype: Type) => string;
@@ -1,5 +1,23 @@
1
1
  import { HttpServer } from '@nestjs/common';
2
2
  import { RequestMethod } from '@nestjs/common/enums/request-method.enum';
3
+ export declare const REQUEST_METHOD_MAP: {
4
+ readonly 0: "get";
5
+ readonly 1: "post";
6
+ readonly 2: "put";
7
+ readonly 3: "delete";
8
+ readonly 4: "patch";
9
+ readonly 5: "all";
10
+ readonly 6: "options";
11
+ readonly 7: "head";
12
+ readonly 8: "search";
13
+ readonly 9: "propfind";
14
+ readonly 10: "proppatch";
15
+ readonly 11: "mkcol";
16
+ readonly 12: "copy";
17
+ readonly 13: "move";
18
+ readonly 14: "lock";
19
+ readonly 15: "unlock";
20
+ };
3
21
  export declare class RouterMethodFactory {
4
22
  get(target: HttpServer, requestMethod: RequestMethod): Function;
5
23
  }
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RouterMethodFactory = void 0;
3
+ exports.RouterMethodFactory = exports.REQUEST_METHOD_MAP = void 0;
4
4
  const request_method_enum_1 = require("@nestjs/common/enums/request-method.enum");
5
- const REQUEST_METHOD_MAP = {
5
+ exports.REQUEST_METHOD_MAP = {
6
6
  [request_method_enum_1.RequestMethod.GET]: 'get',
7
7
  [request_method_enum_1.RequestMethod.POST]: 'post',
8
8
  [request_method_enum_1.RequestMethod.PUT]: 'put',
@@ -22,7 +22,7 @@ const REQUEST_METHOD_MAP = {
22
22
  };
23
23
  class RouterMethodFactory {
24
24
  get(target, requestMethod) {
25
- const methodName = REQUEST_METHOD_MAP[requestMethod];
25
+ const methodName = exports.REQUEST_METHOD_MAP[requestMethod];
26
26
  const method = target[methodName];
27
27
  if (!method) {
28
28
  return target.use;
package/index.js CHANGED
@@ -4,7 +4,7 @@ exports.NestFactory = exports.APP_PIPE = exports.APP_INTERCEPTOR = exports.APP_G
4
4
  const tslib_1 = require("tslib");
5
5
  /*
6
6
  * Nest @core
7
- * Copyright(c) 2017 - 2023 Kamil Mysliwiec
7
+ * Copyright(c) 2017 - 2025 Kamil Mysliwiec
8
8
  * https://nestjs.com
9
9
  * MIT Licensed
10
10
  */
@@ -47,7 +47,7 @@ export declare class NestContainer {
47
47
  addImport(relatedModule: Type<any> | DynamicModule, token: string): Promise<void>;
48
48
  addProvider(provider: Provider, token: string, enhancerSubtype?: EnhancerSubtype): string | symbol | Function;
49
49
  addInjectable(injectable: Provider, token: string, enhancerSubtype: EnhancerSubtype, host?: Type<Injectable>): string | symbol | Function | import("./instance-wrapper").InstanceWrapper<unknown>;
50
- addExportedProvider(provider: Type<any>, token: string): void;
50
+ addExportedProviderOrModule(toExport: Type<any> | DynamicModule, token: string): void;
51
51
  addController(controller: Type<any>, token: string): void;
52
52
  clear(): void;
53
53
  replace(toReplace: any, options: {
@@ -100,9 +100,9 @@ class NestContainer {
100
100
  await this.addDynamicMetadata(token, dynamicMetadata, updatedScope);
101
101
  if (this.isGlobalModule(type, dynamicMetadata)) {
102
102
  moduleRef.isGlobal = true;
103
- // Set global module distance to -1 to ensure their lifecycle hooks
103
+ // Set global module distance to MAX_VALUE to ensure their lifecycle hooks
104
104
  // are always executed first (when initializing the application)
105
- moduleRef.distance = -1;
105
+ moduleRef.distance = Number.MAX_VALUE;
106
106
  this.addGlobalModule(moduleRef);
107
107
  }
108
108
  return moduleRef;
@@ -171,12 +171,12 @@ class NestContainer {
171
171
  const moduleRef = this.modules.get(token);
172
172
  return moduleRef.addInjectable(injectable, enhancerSubtype, host);
173
173
  }
174
- addExportedProvider(provider, token) {
174
+ addExportedProviderOrModule(toExport, token) {
175
175
  if (!this.modules.has(token)) {
176
176
  throw new exceptions_1.UnknownModuleException();
177
177
  }
178
178
  const moduleRef = this.modules.get(token);
179
- moduleRef.addExportedProvider(provider);
179
+ moduleRef.addExportedProviderOrModule(toExport);
180
180
  }
181
181
  addController(controller, token) {
182
182
  if (!this.modules.has(token)) {
@@ -0,0 +1,14 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import { Type } from '@nestjs/common/interfaces/type.interface';
3
+ export declare class ModuleTokenFactory {
4
+ private readonly moduleTokenCache;
5
+ private readonly moduleIdsCache;
6
+ private readonly logger;
7
+ create(metatype: Type<unknown>, dynamicModuleMetadata?: Partial<DynamicModule> | undefined): string;
8
+ getStaticModuleToken(moduleId: string, moduleName: string): string;
9
+ getStringifiedOpaqueToken(opaqueToken: object | undefined): string;
10
+ getModuleId(metatype: Type<unknown>): string;
11
+ getModuleName(metatype: Type<any>): string;
12
+ private hashString;
13
+ private replacer;
14
+ }
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModuleTokenFactory = void 0;
4
+ const common_1 = require("@nestjs/common");
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 perf_hooks_1 = require("perf_hooks");
10
+ const CLASS_STR = 'class ';
11
+ const CLASS_STR_LEN = CLASS_STR.length;
12
+ class ModuleTokenFactory {
13
+ constructor() {
14
+ this.moduleTokenCache = new Map();
15
+ this.moduleIdsCache = new WeakMap();
16
+ this.logger = new common_1.Logger(ModuleTokenFactory.name, {
17
+ timestamp: true,
18
+ });
19
+ }
20
+ create(metatype, dynamicModuleMetadata) {
21
+ const moduleId = this.getModuleId(metatype);
22
+ if (!dynamicModuleMetadata) {
23
+ return this.getStaticModuleToken(moduleId, this.getModuleName(metatype));
24
+ }
25
+ const opaqueToken = {
26
+ id: moduleId,
27
+ module: this.getModuleName(metatype),
28
+ dynamic: dynamicModuleMetadata,
29
+ };
30
+ const start = perf_hooks_1.performance.now();
31
+ const opaqueTokenString = this.getStringifiedOpaqueToken(opaqueToken);
32
+ const timeSpentInMs = perf_hooks_1.performance.now() - start;
33
+ if (timeSpentInMs > 10) {
34
+ const formattedTimeSpent = timeSpentInMs.toFixed(2);
35
+ 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. More details: https://github.com/nestjs/nest/issues/12738`);
36
+ }
37
+ return this.hashString(opaqueTokenString);
38
+ }
39
+ getStaticModuleToken(moduleId, moduleName) {
40
+ const key = `${moduleId}_${moduleName}`;
41
+ if (this.moduleTokenCache.has(key)) {
42
+ return this.moduleTokenCache.get(key);
43
+ }
44
+ const hash = this.hashString(key);
45
+ this.moduleTokenCache.set(key, hash);
46
+ return hash;
47
+ }
48
+ getStringifiedOpaqueToken(opaqueToken) {
49
+ // Uses safeStringify instead of JSON.stringify to support circular dynamic modules
50
+ // The replacer function is also required in order to obtain real class names
51
+ // instead of the unified "Function" key
52
+ return opaqueToken ? (0, fast_safe_stringify_1.default)(opaqueToken, this.replacer) : '';
53
+ }
54
+ getModuleId(metatype) {
55
+ let moduleId = this.moduleIdsCache.get(metatype);
56
+ if (moduleId) {
57
+ return moduleId;
58
+ }
59
+ moduleId = (0, random_string_generator_util_1.randomStringGenerator)();
60
+ this.moduleIdsCache.set(metatype, moduleId);
61
+ return moduleId;
62
+ }
63
+ getModuleName(metatype) {
64
+ return metatype.name;
65
+ }
66
+ hashString(value) {
67
+ return (0, crypto_1.createHash)('sha256').update(value).digest('hex');
68
+ }
69
+ replacer(key, value) {
70
+ if ((0, shared_utils_1.isFunction)(value)) {
71
+ const funcAsString = value.toString();
72
+ const isClass = funcAsString.slice(0, CLASS_STR_LEN) === CLASS_STR;
73
+ if (isClass) {
74
+ return value.name;
75
+ }
76
+ return funcAsString;
77
+ }
78
+ if ((0, shared_utils_1.isSymbol)(value)) {
79
+ return value.toString();
80
+ }
81
+ return value;
82
+ }
83
+ }
84
+ exports.ModuleTokenFactory = ModuleTokenFactory;
@@ -56,16 +56,12 @@ export declare class Module {
56
56
  addCustomValue(provider: ValueProvider, collection: Map<Function | string | symbol, InstanceWrapper>, enhancerSubtype?: EnhancerSubtype): void;
57
57
  addCustomFactory(provider: FactoryProvider, collection: Map<Function | string | symbol, InstanceWrapper>, enhancerSubtype?: EnhancerSubtype): void;
58
58
  addCustomUseExisting(provider: ExistingProvider, collection: Map<Function | string | symbol, InstanceWrapper>, enhancerSubtype?: EnhancerSubtype): void;
59
- addExportedProvider(provider: Provider | string | symbol | DynamicModule): Set<InjectionToken> | undefined;
59
+ addExportedProviderOrModule(toExport: Provider | string | symbol | DynamicModule): Set<InjectionToken> | undefined;
60
60
  addCustomExportedProvider(provider: FactoryProvider | ValueProvider | ClassProvider | ExistingProvider): Set<InjectionToken> | undefined;
61
61
  validateExportedProvider(token: InjectionToken): InjectionToken;
62
62
  addController(controller: Type<Controller>): void;
63
63
  assignControllerUniqueId(controller: Type<Controller>): void;
64
64
  addImport(moduleRef: Module): void;
65
- /**
66
- * @deprecated
67
- */
68
- addRelatedModule(module: Module): void;
69
65
  replace(toReplace: InjectionToken, options: any): void;
70
66
  hasProvider(token: InjectionToken): boolean;
71
67
  hasInjectable(token: InjectionToken): boolean;
@@ -277,19 +277,19 @@ class Module {
277
277
  subtype: enhancerSubtype,
278
278
  }));
279
279
  }
280
- addExportedProvider(provider) {
280
+ addExportedProviderOrModule(toExport) {
281
281
  const addExportedUnit = (token) => this._exports.add(this.validateExportedProvider(token));
282
- if (this.isCustomProvider(provider)) {
283
- return this.addCustomExportedProvider(provider);
282
+ if (this.isCustomProvider(toExport)) {
283
+ return this.addCustomExportedProvider(toExport);
284
284
  }
285
- else if ((0, shared_utils_1.isString)(provider) || (0, shared_utils_1.isSymbol)(provider)) {
286
- return addExportedUnit(provider);
285
+ else if ((0, shared_utils_1.isString)(toExport) || (0, shared_utils_1.isSymbol)(toExport)) {
286
+ return addExportedUnit(toExport);
287
287
  }
288
- else if (this.isDynamicModule(provider)) {
289
- const { module: moduleClassRef } = provider;
288
+ else if (this.isDynamicModule(toExport)) {
289
+ const { module: moduleClassRef } = toExport;
290
290
  return addExportedUnit(moduleClassRef);
291
291
  }
292
- addExportedUnit(provider);
292
+ addExportedUnit(toExport);
293
293
  }
294
294
  addCustomExportedProvider(provider) {
295
295
  const provide = provider.provide;
@@ -338,12 +338,6 @@ class Module {
338
338
  addImport(moduleRef) {
339
339
  this._imports.add(moduleRef);
340
340
  }
341
- /**
342
- * @deprecated
343
- */
344
- addRelatedModule(module) {
345
- this._imports.add(module);
346
- }
347
341
  replace(toReplace, options) {
348
342
  if (options.isProvider && this.hasProvider(toReplace)) {
349
343
  const originalProvider = this._providers.get(toReplace);
@@ -1,6 +1,5 @@
1
1
  import { CanActivate, ExceptionFilter, HttpServer, INestApplication, INestMicroservice, NestHybridApplicationOptions, NestInterceptor, PipeTransform, VersioningOptions, WebSocketAdapter } from '@nestjs/common';
2
2
  import { GlobalPrefixOptions, NestApplicationOptions } from '@nestjs/common/interfaces';
3
- import { CorsOptions, CorsOptionsDelegate } from '@nestjs/common/interfaces/external/cors-options.interface';
4
3
  import { Logger } from '@nestjs/common/services/logger.service';
5
4
  import { AbstractHttpAdapter } from './adapters';
6
5
  import { ApplicationConfig } from './application-config';
@@ -42,7 +41,7 @@ export declare class NestApplication extends NestApplicationContext<NestApplicat
42
41
  startAllMicroservices(): Promise<this>;
43
42
  use(...args: [any, any?]): this;
44
43
  useBodyParser(...args: [any, any?]): this;
45
- enableCors(options?: CorsOptions | CorsOptionsDelegate<any>): void;
44
+ enableCors(options?: any): void;
46
45
  enableVersioning(options?: VersioningOptions): this;
47
46
  listen(port: number | string): Promise<any>;
48
47
  listen(port: number | string, hostname: string): Promise<any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/core",
3
- "version": "11.0.0-next.3",
3
+ "version": "11.0.0",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@core)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "license": "MIT",
@@ -9,6 +9,9 @@
9
9
  "type": "opencollective",
10
10
  "url": "https://opencollective.com/nest"
11
11
  },
12
+ "engines": {
13
+ "node": ">= 20"
14
+ },
12
15
  "repository": {
13
16
  "type": "git",
14
17
  "url": "https://github.com/nestjs/nest.git",
@@ -28,21 +31,21 @@
28
31
  }
29
32
  },
30
33
  "dependencies": {
31
- "@nuxtjs/opencollective": "0.3.2",
34
+ "@nuxt/opencollective": "0.4.1",
32
35
  "fast-safe-stringify": "2.1.1",
33
36
  "iterare": "1.2.1",
34
37
  "path-to-regexp": "8.2.0",
35
- "tslib": "2.7.0",
38
+ "tslib": "2.8.1",
36
39
  "uid": "2.0.2"
37
40
  },
38
41
  "devDependencies": {
39
- "@nestjs/common": "^11.0.0-next.3"
42
+ "@nestjs/common": "^11.0.0"
40
43
  },
41
44
  "peerDependencies": {
42
- "@nestjs/common": "^10.0.0",
43
- "@nestjs/microservices": "^10.0.0",
44
- "@nestjs/platform-express": "^10.0.0",
45
- "@nestjs/websockets": "^10.0.0",
45
+ "@nestjs/common": "^11.0.0",
46
+ "@nestjs/microservices": "^11.0.0",
47
+ "@nestjs/platform-express": "^11.0.0",
48
+ "@nestjs/websockets": "^11.0.0",
46
49
  "reflect-metadata": "^0.1.12 || ^0.2.0",
47
50
  "rxjs": "^7.1.0"
48
51
  },
package/scanner.d.ts CHANGED
@@ -48,7 +48,7 @@ export declare class DependenciesScanner {
48
48
  isCustomProvider(provider: Provider): provider is ClassProvider | ValueProvider | FactoryProvider | ExistingProvider;
49
49
  insertProvider(provider: Provider, token: string): string | symbol | Function | InstanceWrapper<unknown> | undefined;
50
50
  insertInjectable(injectable: Type<Injectable> | object, token: string, host: Type<Injectable>, subtype: EnhancerSubtype, methodKey?: string): InstanceWrapper<any> | undefined;
51
- insertExportedProvider(exportedProvider: Type<Injectable> | ForwardReference, token: string): void;
51
+ insertExportedProviderOrModule(toExport: ForwardReference | DynamicModule | Type<unknown>, token: string): void;
52
52
  insertController(controller: Type<Controller>, token: string): void;
53
53
  private insertOrOverrideModule;
54
54
  private getOverrideModuleByModule;
package/scanner.js CHANGED
@@ -32,7 +32,7 @@ class DependenciesScanner {
32
32
  this.addScopedEnhancersMetadata();
33
33
  // Modules distance calculation should be done after all modules are scanned
34
34
  // but before global modules are registered (linked to all modules).
35
- // Global modules have their distance set to -1 anyway.
35
+ // Global modules have their distance set to MAX anyway.
36
36
  this.calculateModulesDistance();
37
37
  this.container.bindGlobalScope();
38
38
  }
@@ -148,7 +148,7 @@ class DependenciesScanner {
148
148
  ...this.reflectMetadata(constants_1.MODULE_METADATA.EXPORTS, module),
149
149
  ...this.container.getDynamicMetadataByToken(token, constants_1.MODULE_METADATA.EXPORTS),
150
150
  ];
151
- exports.forEach(exportedProvider => this.insertExportedProvider(exportedProvider, token));
151
+ exports.forEach(exportedProvider => this.insertExportedProviderOrModule(exportedProvider, token));
152
152
  }
153
153
  reflectInjectables(component, token, metadataKey) {
154
154
  const controllerInjectables = this.reflectMetadata(metadataKey, component);
@@ -210,7 +210,8 @@ class DependenciesScanner {
210
210
  const moduleImports = moduleRef.imports;
211
211
  moduleImports.forEach(importedModuleRef => {
212
212
  if (importedModuleRef) {
213
- if (distance > importedModuleRef.distance) {
213
+ if (distance > importedModuleRef.distance &&
214
+ !importedModuleRef.isGlobal) {
214
215
  importedModuleRef.distance = distance;
215
216
  }
216
217
  calculateDistance(importedModuleRef, distance + 1, localModulesStack);
@@ -290,13 +291,11 @@ class DependenciesScanner {
290
291
  });
291
292
  }
292
293
  }
293
- insertExportedProvider(
294
- // TODO: improve the type definition below because it doesn't reflects the real usage of this method
295
- exportedProvider, token) {
296
- const fulfilledProvider = this.isForwardReference(exportedProvider)
297
- ? exportedProvider.forwardRef()
298
- : exportedProvider;
299
- this.container.addExportedProvider(fulfilledProvider, token);
294
+ insertExportedProviderOrModule(toExport, token) {
295
+ const fulfilledProvider = this.isForwardReference(toExport)
296
+ ? toExport.forwardRef()
297
+ : toExport;
298
+ this.container.addExportedProviderOrModule(fulfilledProvider, token);
300
299
  }
301
300
  insertController(controller, token) {
302
301
  this.container.addController(controller, token);