@nestjs/core 9.3.5 → 9.3.6

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/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from './discovery';
6
6
  export * from './exceptions';
7
7
  export * from './helpers';
8
8
  export * from './injector';
9
+ export * from './inspector';
9
10
  export * from './metadata-scanner';
10
11
  export * from './middleware';
11
12
  export * from './nest-application';
package/index.js CHANGED
@@ -20,6 +20,7 @@ tslib_1.__exportStar(require("./discovery"), exports);
20
20
  tslib_1.__exportStar(require("./exceptions"), exports);
21
21
  tslib_1.__exportStar(require("./helpers"), exports);
22
22
  tslib_1.__exportStar(require("./injector"), exports);
23
+ tslib_1.__exportStar(require("./inspector"), exports);
23
24
  tslib_1.__exportStar(require("./metadata-scanner"), exports);
24
25
  tslib_1.__exportStar(require("./middleware"), exports);
25
26
  tslib_1.__exportStar(require("./nest-application"), exports);
@@ -50,4 +50,5 @@ export declare class NestContainer {
50
50
  registerCoreModuleRef(moduleRef: Module): void;
51
51
  getModuleTokenFactory(): ModuleTokenFactory;
52
52
  registerRequestProvider<T = any>(request: T, contextId: ContextId): void;
53
+ private shouldInitOnPreview;
53
54
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NestContainer = void 0;
4
4
  const constants_1 = require("@nestjs/common/constants");
5
5
  const exceptions_1 = require("../errors/exceptions");
6
+ const initialize_on_preview_allowlist_1 = require("../inspector/initialize-on-preview.allowlist");
6
7
  const serialized_graph_1 = require("../inspector/serialized-graph");
7
8
  const request_constants_1 = require("../router/request/request-constants");
8
9
  const compiler_1 = require("./compiler");
@@ -54,6 +55,7 @@ class NestContainer {
54
55
  }
55
56
  const moduleRef = new module_1.Module(type, this);
56
57
  moduleRef.token = token;
58
+ moduleRef.initOnPreview = this.shouldInitOnPreview(type);
57
59
  this.modules.set(token, moduleRef);
58
60
  const updatedScope = [].concat(scope, type);
59
61
  await this.addDynamicMetadata(token, dynamicMetadata, updatedScope);
@@ -175,5 +177,8 @@ class NestContainer {
175
177
  isResolved: true,
176
178
  });
177
179
  }
180
+ shouldInitOnPreview(type) {
181
+ return initialize_on_preview_allowlist_1.InitializeOnPreviewAllowlist.has(type);
182
+ }
178
183
  }
179
184
  exports.NestContainer = NestContainer;
@@ -330,7 +330,7 @@ class Injector {
330
330
  .forEach(item => (instance[item.key] = item.instance));
331
331
  }
332
332
  async instantiateClass(instances, wrapper, targetMetatype, contextId = constants_2.STATIC_CONTEXT, inquirer) {
333
- var _a;
333
+ var _a, _b;
334
334
  const { metatype, inject } = wrapper;
335
335
  const inquirerId = this.getInquirerId(inquirer);
336
336
  const instanceHost = targetMetatype.getInstanceByContextId(this.getContextId(contextId, targetMetatype), inquirerId);
@@ -338,7 +338,7 @@ class Injector {
338
338
  wrapper.isInRequestScope(contextId, inquirer) ||
339
339
  wrapper.isLazyTransient(contextId, inquirer) ||
340
340
  wrapper.isExplicitlyRequested(contextId, inquirer);
341
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.preview) {
341
+ if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.preview) && !((_b = wrapper.host) === null || _b === void 0 ? void 0 : _b.initOnPreview)) {
342
342
  instanceHost.isResolved = true;
343
343
  return instanceHost.instance;
344
344
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ModuleTokenFactory = void 0;
4
4
  const random_string_generator_util_1 = require("@nestjs/common/utils/random-string-generator.util");
5
5
  const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
6
- const xxhash_1 = require("@node-rs/xxhash");
6
+ const crypto_1 = require("crypto");
7
7
  const fast_safe_stringify_1 = require("fast-safe-stringify");
8
8
  const CLASS_STR = 'class ';
9
9
  const CLASS_STR_LEN = CLASS_STR.length;
@@ -53,7 +53,7 @@ class ModuleTokenFactory {
53
53
  return metatype.name;
54
54
  }
55
55
  hashString(value) {
56
- return (0, xxhash_1.xxh32)(value).toString();
56
+ return (0, crypto_1.createHash)('sha256').update(value).digest('hex');
57
57
  }
58
58
  replacer(key, value) {
59
59
  if ((0, shared_utils_1.isFunction)(value)) {
@@ -19,6 +19,7 @@ export declare class Module {
19
19
  private readonly _controllers;
20
20
  private readonly _exports;
21
21
  private _distance;
22
+ private _initOnPreview;
22
23
  private _isGlobal;
23
24
  private _token;
24
25
  constructor(_metatype: Type<any>, container: NestContainer);
@@ -28,6 +29,8 @@ export declare class Module {
28
29
  get name(): string;
29
30
  get isGlobal(): boolean;
30
31
  set isGlobal(global: boolean);
32
+ get initOnPreview(): boolean;
33
+ set initOnPreview(initOnPreview: boolean);
31
34
  get providers(): Map<InstanceToken, InstanceWrapper<Injectable>>;
32
35
  get middlewares(): Map<InstanceToken, InstanceWrapper<Injectable>>;
33
36
  get imports(): Set<Module>;
@@ -24,6 +24,7 @@ class Module {
24
24
  this._controllers = new Map();
25
25
  this._exports = new Set();
26
26
  this._distance = 0;
27
+ this._initOnPreview = false;
27
28
  this._isGlobal = false;
28
29
  this.addCoreProviders();
29
30
  this._id = this.generateUuid();
@@ -46,6 +47,12 @@ class Module {
46
47
  set isGlobal(global) {
47
48
  this._isGlobal = global;
48
49
  }
50
+ get initOnPreview() {
51
+ return this._initOnPreview;
52
+ }
53
+ set initOnPreview(initOnPreview) {
54
+ this._initOnPreview = initOnPreview;
55
+ }
49
56
  get providers() {
50
57
  return this._providers;
51
58
  }
@@ -399,9 +406,9 @@ class Module {
399
406
  }
400
407
  generateUuid() {
401
408
  var _a, _b, _c;
402
- const UUID_NAMESPACE = 'fb848993-0c82-4b9e-ae95-3c3c1dbe3d6b';
409
+ const prefix = 'M_';
403
410
  const key = (_b = (_a = this.name) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : (_c = this.token) === null || _c === void 0 ? void 0 : _c.toString();
404
- return key ? uuid_factory_1.UuidFactory.get(key, UUID_NAMESPACE) : (0, random_string_generator_util_1.randomStringGenerator)();
411
+ return key ? uuid_factory_1.UuidFactory.get(`${prefix}_${key}`) : (0, random_string_generator_util_1.randomStringGenerator)();
405
412
  }
406
413
  }
407
414
  exports.Module = Module;
@@ -1,5 +1,6 @@
1
1
  export declare class DeterministicUuidRegistry {
2
2
  private static readonly registry;
3
- static get(str: string, namespace?: string, inc?: number): any;
3
+ static get(str: string, inc?: number): any;
4
4
  static clear(): void;
5
+ private static hashCode;
5
6
  }
@@ -1,23 +1,25 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DeterministicUuidRegistry = void 0;
4
- const xxhash_1 = require("@node-rs/xxhash");
5
- const DEFAULT_UUID_NAMESPACE = 'efa0df42-88af-474f-9cad-4206a2319f07';
6
4
  class DeterministicUuidRegistry {
7
- static get(str, namespace = DEFAULT_UUID_NAMESPACE, inc = 0) {
8
- const id = inc
9
- ? (0, xxhash_1.xxh32)(`${namespace}_${str}_${inc}`)
10
- : (0, xxhash_1.xxh32)(`${namespace}_${str}`);
11
- const idAsString = `${id}`;
12
- if (this.registry.has(idAsString)) {
13
- return this.get(str, namespace, inc + 1);
5
+ static get(str, inc = 0) {
6
+ const key = inc ? `${str}_${inc}` : `${str}`;
7
+ if (this.registry.has(key)) {
8
+ return this.get(str, inc + 1);
14
9
  }
15
- this.registry.add(idAsString);
16
- return idAsString;
10
+ const id = this.hashCode(key);
11
+ this.registry.set(id, true);
12
+ return id;
17
13
  }
18
14
  static clear() {
19
15
  this.registry.clear();
20
16
  }
17
+ static hashCode(s) {
18
+ let h = 0;
19
+ for (let i = 0; i < s.length; i++)
20
+ h = (Math.imul(31, h) + s.charCodeAt(i)) | 0;
21
+ return h.toString();
22
+ }
21
23
  }
22
24
  exports.DeterministicUuidRegistry = DeterministicUuidRegistry;
23
- DeterministicUuidRegistry.registry = new Set();
25
+ DeterministicUuidRegistry.registry = new Map();
@@ -0,0 +1,2 @@
1
+ export * from './graph-inspector';
2
+ export * from './initialize-on-preview.allowlist';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./graph-inspector"), exports);
5
+ tslib_1.__exportStar(require("./initialize-on-preview.allowlist"), exports);
@@ -0,0 +1,6 @@
1
+ import { Type } from '@nestjs/common';
2
+ export declare class InitializeOnPreviewAllowlist {
3
+ private static readonly allowlist;
4
+ static add(type: Type): void;
5
+ static has(type: Type): boolean;
6
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InitializeOnPreviewAllowlist = void 0;
4
+ class InitializeOnPreviewAllowlist {
5
+ static add(type) {
6
+ this.allowlist.set(type, true);
7
+ }
8
+ static has(type) {
9
+ return this.allowlist.has(type);
10
+ }
11
+ }
12
+ exports.InitializeOnPreviewAllowlist = InitializeOnPreviewAllowlist;
13
+ InitializeOnPreviewAllowlist.allowlist = new WeakMap();
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
@@ -83,8 +83,7 @@ class SerializedGraph {
83
83
  return JSON.stringify(this.toJSON(), replacer, 2);
84
84
  }
85
85
  generateUuidByEdgeDefinition(edgeDefinition) {
86
- const UUID_NAMESPACE = '7a6d19ab-ec88-419f-a833-062532c468a7';
87
- return deterministic_uuid_registry_1.DeterministicUuidRegistry.get(JSON.stringify(edgeDefinition), UUID_NAMESPACE);
86
+ return deterministic_uuid_registry_1.DeterministicUuidRegistry.get(JSON.stringify(edgeDefinition));
88
87
  }
89
88
  }
90
89
  exports.SerializedGraph = SerializedGraph;
@@ -5,5 +5,5 @@ export declare enum UuidFactoryMode {
5
5
  export declare class UuidFactory {
6
6
  private static _mode;
7
7
  static set mode(value: UuidFactoryMode);
8
- static get(key?: string, namespace?: string): any;
8
+ static get(key?: string): any;
9
9
  }
@@ -12,9 +12,9 @@ class UuidFactory {
12
12
  static set mode(value) {
13
13
  this._mode = value;
14
14
  }
15
- static get(key = '', namespace) {
15
+ static get(key = '') {
16
16
  return this._mode === UuidFactoryMode.Deterministic
17
- ? deterministic_uuid_registry_1.DeterministicUuidRegistry.get(key, namespace)
17
+ ? deterministic_uuid_registry_1.DeterministicUuidRegistry.get(key)
18
18
  : (0, random_string_generator_util_1.randomStringGenerator)();
19
19
  }
20
20
  }
@@ -23,7 +23,7 @@ export declare class NestApplicationContext<TOptions extends NestApplicationCont
23
23
  private readonly moduleCompiler;
24
24
  private shutdownCleanupRef?;
25
25
  private _instanceLinksHost;
26
- private _moduleRefsByDistance?;
26
+ private _moduleRefsForHooksByDistance?;
27
27
  protected get instanceLinksHost(): InstanceLinksHost;
28
28
  constructor(container: NestContainer, appOptions?: TOptions, contextModule?: Module, scope?: Type<any>[]);
29
29
  selectContextModule(): void;
@@ -165,6 +165,6 @@ export declare class NestApplicationContext<TOptions extends NestApplicationCont
165
165
  */
166
166
  protected callBeforeShutdownHook(signal?: string): Promise<void>;
167
167
  protected assertNotInPreviewMode(methodName: string): void;
168
- private getModulesSortedByDistance;
168
+ private getModulesToTriggerHooksOn;
169
169
  private printInPreviewModeWarning;
170
170
  }
@@ -219,10 +219,7 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
219
219
  * modules and its children.
220
220
  */
221
221
  async callInitHook() {
222
- if (this.appOptions.preview) {
223
- return;
224
- }
225
- const modulesSortedByDistance = this.getModulesSortedByDistance();
222
+ const modulesSortedByDistance = this.getModulesToTriggerHooksOn();
226
223
  for (const module of modulesSortedByDistance) {
227
224
  await (0, hooks_1.callModuleInitHook)(module);
228
225
  }
@@ -232,10 +229,7 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
232
229
  * modules and its children.
233
230
  */
234
231
  async callDestroyHook() {
235
- if (this.appOptions.preview) {
236
- return;
237
- }
238
- const modulesSortedByDistance = this.getModulesSortedByDistance();
232
+ const modulesSortedByDistance = this.getModulesToTriggerHooksOn();
239
233
  for (const module of modulesSortedByDistance) {
240
234
  await (0, hooks_1.callModuleDestroyHook)(module);
241
235
  }
@@ -245,10 +239,7 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
245
239
  * modules and its children.
246
240
  */
247
241
  async callBootstrapHook() {
248
- if (this.appOptions.preview) {
249
- return;
250
- }
251
- const modulesSortedByDistance = this.getModulesSortedByDistance();
242
+ const modulesSortedByDistance = this.getModulesToTriggerHooksOn();
252
243
  for (const module of modulesSortedByDistance) {
253
244
  await (0, hooks_1.callModuleBootstrapHook)(module);
254
245
  }
@@ -258,10 +249,7 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
258
249
  * modules and children.
259
250
  */
260
251
  async callShutdownHook(signal) {
261
- if (this.appOptions.preview) {
262
- return;
263
- }
264
- const modulesSortedByDistance = this.getModulesSortedByDistance();
252
+ const modulesSortedByDistance = this.getModulesToTriggerHooksOn();
265
253
  for (const module of modulesSortedByDistance) {
266
254
  await (0, hooks_1.callAppShutdownHook)(module, signal);
267
255
  }
@@ -271,10 +259,7 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
271
259
  * modules and children.
272
260
  */
273
261
  async callBeforeShutdownHook(signal) {
274
- if (this.appOptions.preview) {
275
- return;
276
- }
277
- const modulesSortedByDistance = this.getModulesSortedByDistance();
262
+ const modulesSortedByDistance = this.getModulesToTriggerHooksOn();
278
263
  for (const module of modulesSortedByDistance) {
279
264
  await (0, hooks_1.callBeforeAppShutdownHook)(module, signal);
280
265
  }
@@ -286,14 +271,18 @@ class NestApplicationContext extends abstract_instance_resolver_1.AbstractInstan
286
271
  throw new Error(error);
287
272
  }
288
273
  }
289
- getModulesSortedByDistance() {
290
- if (this._moduleRefsByDistance) {
291
- return this._moduleRefsByDistance;
274
+ getModulesToTriggerHooksOn() {
275
+ var _a;
276
+ if (this._moduleRefsForHooksByDistance) {
277
+ return this._moduleRefsForHooksByDistance;
292
278
  }
293
279
  const modulesContainer = this.container.getModules();
294
280
  const compareFn = (a, b) => b.distance - a.distance;
295
- this._moduleRefsByDistance = Array.from(modulesContainer.values()).sort(compareFn);
296
- return this._moduleRefsByDistance;
281
+ const modulesSortedByDistance = Array.from(modulesContainer.values()).sort(compareFn);
282
+ this._moduleRefsForHooksByDistance = ((_a = this.appOptions) === null || _a === void 0 ? void 0 : _a.preview)
283
+ ? modulesSortedByDistance.filter(moduleRef => moduleRef.initOnPreview)
284
+ : modulesSortedByDistance;
285
+ return this._moduleRefsForHooksByDistance;
297
286
  }
298
287
  printInPreviewModeWarning() {
299
288
  this.logger.warn('------------------------------------------------');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/core",
3
- "version": "9.3.5",
3
+ "version": "9.3.6",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@core)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "license": "MIT",
@@ -27,7 +27,6 @@
27
27
  }
28
28
  },
29
29
  "dependencies": {
30
- "@node-rs/xxhash": "^1.3.0",
31
30
  "@nuxtjs/opencollective": "0.3.2",
32
31
  "fast-safe-stringify": "2.1.1",
33
32
  "iterare": "1.2.1",
@@ -36,7 +35,7 @@
36
35
  "uid": "2.0.1"
37
36
  },
38
37
  "devDependencies": {
39
- "@nestjs/common": "9.3.5"
38
+ "@nestjs/common": "9.3.6"
40
39
  },
41
40
  "peerDependencies": {
42
41
  "@nestjs/common": "^9.0.0",