nest-graph-inspector 0.7.1 → 0.8.0-test.1

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.
@@ -1,12 +1,12 @@
1
- import { OnModuleInit, Type } from '@nestjs/common';
2
- import { ModulesContainer } from '@nestjs/core';
3
- import type { NestGraphInspectorModuleOptions } from './nest-graph-inspector.type';
4
- import { ModuleMap } from './types/module-map.type';
5
- import { HttpOutputAdapter } from './adapters/http-output.adapter';
6
- import { FileOutputAdapter } from './adapters/file-output.adapter';
7
- import { JsonOutputAdapter } from './adapters/json-output.adapter';
8
- import { ViewerOutputAdapter } from './adapters/viewer-output.adapter';
9
- import { RuntimeTraceRecorder } from './runtime-trace.recorder';
1
+ import { OnModuleInit, Type } from "@nestjs/common";
2
+ import { ModulesContainer } from "@nestjs/core";
3
+ import type { NestGraphInspectorModuleOptions } from "./nest-graph-inspector.type";
4
+ import { ModuleMap } from "./types/module-map.type";
5
+ import { HttpOutputAdapter } from "./adapters/http-output.adapter";
6
+ import { FileOutputAdapter } from "./adapters/file-output.adapter";
7
+ import { JsonOutputAdapter } from "./adapters/json-output.adapter";
8
+ import { ViewerOutputAdapter } from "./adapters/viewer-output.adapter";
9
+ import { RuntimeTraceRecorder } from "./runtime-trace.recorder";
10
10
  export declare class NestGraphInspectorSetup implements OnModuleInit {
11
11
  private readonly options;
12
12
  private readonly modulesContainer;
@@ -84,6 +84,11 @@ export declare class NestGraphInspectorSetup implements OnModuleInit {
84
84
  private extractMethodParameterTypesFromProject;
85
85
  private extractMethodParameterNamesFromFunctionSource;
86
86
  private typeToTypeScriptCode;
87
+ private renderTypeSequence;
88
+ private renderObjectType;
89
+ private typeReferenceCode;
90
+ private typeTextWithinLimit;
91
+ private typeFallback;
87
92
  private wrapArrayItemType;
88
93
  private propertyNameToTypeScriptCode;
89
94
  private typeAllowsUndefined;
@@ -26,6 +26,11 @@ const json_output_adapter_1 = require("./adapters/json-output.adapter");
26
26
  const viewer_output_adapter_1 = require("./adapters/viewer-output.adapter");
27
27
  const ts_morph_1 = require("ts-morph");
28
28
  const runtime_trace_recorder_1 = require("./runtime-trace.recorder");
29
+ const MAX_TYPE_RENDER_DEPTH = 12;
30
+ const MAX_TYPE_RENDER_MEMBERS = 50;
31
+ const MAX_TYPE_RENDER_LENGTH = 8_000;
32
+ const MAX_TYPE_PROPERTY_NAME_LENGTH = 160;
33
+ const MIN_TYPE_RENDER_LENGTH = "unknown".length;
29
34
  let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspectorSetup {
30
35
  options;
31
36
  modulesContainer;
@@ -65,7 +70,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
65
70
  this.nestCoreModuleName =
66
71
  this.options.nestCoreModuleName ??
67
72
  nest_graph_inspector_module_1.defaultOptions.nestCoreModuleName ??
68
- 'NestJSCoreModule';
73
+ "NestJSCoreModule";
69
74
  this.nestCoreProviders = [
70
75
  ...(this.options.nestCoreProviders ??
71
76
  nest_graph_inspector_module_1.defaultOptions.nestCoreProviders ??
@@ -139,11 +144,11 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
139
144
  }
140
145
  }
141
146
  withDefaultOutputOptions(output) {
142
- if (output.type !== 'viewer') {
147
+ if (output.type !== "viewer") {
143
148
  return output;
144
149
  }
145
- const defaultViewerOutput = nest_graph_inspector_module_1.defaultOptions.outputs?.find((defaultOutput) => defaultOutput.type === 'viewer');
146
- if (!defaultViewerOutput || defaultViewerOutput.type !== 'viewer') {
150
+ const defaultViewerOutput = nest_graph_inspector_module_1.defaultOptions.outputs?.find((defaultOutput) => defaultOutput.type === "viewer");
151
+ if (!defaultViewerOutput || defaultViewerOutput.type !== "viewer") {
147
152
  return output;
148
153
  }
149
154
  return {
@@ -167,9 +172,9 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
167
172
  };
168
173
  }
169
174
  getDirectRunHistoryDirPath() {
170
- const jsonOutput = this.options.outputs?.find((output) => output.type === 'json');
171
- return jsonOutput?.type === 'json'
172
- ? (0, node_path_1.join)(process.cwd(), (0, node_path_1.dirname)(jsonOutput.path), 'direct-run', 'history')
175
+ const jsonOutput = this.options.outputs?.find((output) => output.type === "json");
176
+ return jsonOutput?.type === "json"
177
+ ? (0, node_path_1.join)(process.cwd(), (0, node_path_1.dirname)(jsonOutput.path), "direct-run", "history")
173
178
  : undefined;
174
179
  }
175
180
  buildModuleMapFromAutoDetect() {
@@ -185,7 +190,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
185
190
  createModuleMapFromModuleTree(moduleTree) {
186
191
  const modules = this.flattenModuleTree(moduleTree);
187
192
  return {
188
- version: '3',
193
+ version: "3",
189
194
  root: moduleTree.name,
190
195
  modules,
191
196
  };
@@ -194,7 +199,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
194
199
  for (const moduleRef of this.modulesContainer.values()) {
195
200
  const moduleName = this.moduleName(moduleRef);
196
201
  if (this.ignoreImport.includes(moduleName) ||
197
- moduleName === 'InternalCoreModule') {
202
+ moduleName === "InternalCoreModule") {
198
203
  continue;
199
204
  }
200
205
  for (const importedModule of moduleRef.imports.values()) {
@@ -203,7 +208,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
203
208
  }
204
209
  }
205
210
  }
206
- throw new Error('Could not auto-detect root module. No module imports NestGraphInspectorModule.');
211
+ throw new Error("Could not auto-detect root module. No module imports NestGraphInspectorModule.");
207
212
  }
208
213
  resolveModuleTree(moduleRef, visited = new Set()) {
209
214
  if (visited.has(moduleRef)) {
@@ -253,13 +258,13 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
253
258
  }
254
259
  this.instrumentRuntimeTraceWrappers({
255
260
  moduleName: node.name,
256
- type: 'provider',
261
+ type: "provider",
257
262
  wrappers: node.moduleRef.providers.values(),
258
263
  shouldIgnore: (name) => name === node.name || this.ignoreProvider.includes(name),
259
264
  });
260
265
  this.instrumentRuntimeTraceWrappers({
261
266
  moduleName: node.name,
262
- type: 'controller',
267
+ type: "controller",
263
268
  wrappers: node.moduleRef.controllers.values(),
264
269
  });
265
270
  });
@@ -267,7 +272,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
267
272
  instrumentRuntimeTraceWrappers(param) {
268
273
  for (const wrapper of param.wrappers) {
269
274
  const instance = wrapper.instance;
270
- if (!instance || typeof instance !== 'object') {
275
+ if (!instance || typeof instance !== "object") {
271
276
  continue;
272
277
  }
273
278
  const className = this.wrapperClassName(wrapper);
@@ -292,12 +297,12 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
292
297
  return;
293
298
  }
294
299
  for (const methodName of Object.getOwnPropertyNames(prototype)) {
295
- if (methodName === 'constructor') {
300
+ if (methodName === "constructor") {
296
301
  continue;
297
302
  }
298
303
  const descriptor = Object.getOwnPropertyDescriptor(prototype, methodName);
299
304
  const method = descriptor?.value;
300
- if (typeof method !== 'function') {
305
+ if (typeof method !== "function") {
301
306
  continue;
302
307
  }
303
308
  Object.defineProperty(param.instance, methodName, {
@@ -449,7 +454,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
449
454
  };
450
455
  }
451
456
  createTsMorphProject() {
452
- const tsConfigFilePath = (0, node_path_1.join)(process.cwd(), 'tsconfig.json');
457
+ const tsConfigFilePath = (0, node_path_1.join)(process.cwd(), "tsconfig.json");
453
458
  if (!(0, node_fs_1.existsSync)(tsConfigFilePath)) {
454
459
  this.logger.warn(`Could not find tsconfig.json at ${tsConfigFilePath}; JSDoc metadata will be skipped.`);
455
460
  return new ts_morph_1.Project();
@@ -477,7 +482,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
477
482
  ?.getJsDocs()
478
483
  .map((doc) => doc.getCommentText())
479
484
  .filter((comment) => !!comment)
480
- .join('\n');
485
+ .join("\n");
481
486
  }
482
487
  wrapperClassName(wrapper) {
483
488
  if (wrapper.metatype?.name) {
@@ -485,7 +490,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
485
490
  }
486
491
  const instance = wrapper.instance;
487
492
  if (instance &&
488
- (typeof instance === 'object' || typeof instance === 'function')) {
493
+ (typeof instance === "object" || typeof instance === "function")) {
489
494
  return instance.constructor?.name ?? null;
490
495
  }
491
496
  return null;
@@ -504,7 +509,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
504
509
  for (const depWrapper of ctorDeps) {
505
510
  if (depWrapper) {
506
511
  const dependencyName = this.resolveDependencyName(depWrapper.token, moduleRef);
507
- if (dependencyName && dependencyName !== 'Object') {
512
+ if (dependencyName && dependencyName !== "Object") {
508
513
  dependencies.add(dependencyName);
509
514
  }
510
515
  }
@@ -514,7 +519,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
514
519
  const depWrapper = propertyDep?.wrapper;
515
520
  if (depWrapper) {
516
521
  const dependencyName = this.resolveDependencyName(depWrapper.token, moduleRef);
517
- if (dependencyName && dependencyName !== 'Object') {
522
+ if (dependencyName && dependencyName !== "Object") {
518
523
  dependencies.add(dependencyName);
519
524
  }
520
525
  }
@@ -522,7 +527,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
522
527
  return [...dependencies];
523
528
  }
524
529
  resolveInjectionToken(token) {
525
- if (typeof token === 'object' && token !== null && 'token' in token) {
530
+ if (typeof token === "object" && token !== null && "token" in token) {
526
531
  return token.token;
527
532
  }
528
533
  return token;
@@ -557,8 +562,8 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
557
562
  continue;
558
563
  }
559
564
  const providerInstance = wrapper.instance &&
560
- (typeof wrapper.instance === 'object' ||
561
- typeof wrapper.instance === 'function')
565
+ (typeof wrapper.instance === "object" ||
566
+ typeof wrapper.instance === "function")
562
567
  ? wrapper.instance
563
568
  : null;
564
569
  const providerName = wrapper.metatype?.name ||
@@ -582,7 +587,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
582
587
  return true;
583
588
  }
584
589
  if (wrapper.instance &&
585
- typeof wrapper.instance === 'object' &&
590
+ typeof wrapper.instance === "object" &&
586
591
  wrapper.instance.constructor === token) {
587
592
  return true;
588
593
  }
@@ -624,16 +629,16 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
624
629
  moduleName(moduleRef) {
625
630
  return (moduleRef.metatype?.name ||
626
631
  this.tokenName(moduleRef.token) ||
627
- 'AnonymousModule');
632
+ "AnonymousModule");
628
633
  }
629
634
  tokenName(token) {
630
635
  if (!token)
631
636
  return null;
632
- if (typeof token === 'string')
637
+ if (typeof token === "string")
633
638
  return token;
634
- if (typeof token === 'symbol')
639
+ if (typeof token === "symbol")
635
640
  return token.toString();
636
- if (typeof token === 'function')
641
+ if (typeof token === "function")
637
642
  return token.name;
638
643
  return null;
639
644
  }
@@ -682,8 +687,8 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
682
687
  }
683
688
  const providerWrapper = [...moduleRef.providers.values()].find((wrapper) => {
684
689
  const instance = wrapper.instance &&
685
- (typeof wrapper.instance === 'object' ||
686
- typeof wrapper.instance === 'function')
690
+ (typeof wrapper.instance === "object" ||
691
+ typeof wrapper.instance === "function")
687
692
  ? wrapper.instance
688
693
  : null;
689
694
  const resolvedName = wrapper.metatype?.name ||
@@ -695,7 +700,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
695
700
  }
696
701
  getDirectRunMethods(instance) {
697
702
  if (!instance ||
698
- (typeof instance !== 'object' && typeof instance !== 'function')) {
703
+ (typeof instance !== "object" && typeof instance !== "function")) {
699
704
  return [];
700
705
  }
701
706
  const prototype = Object.getPrototypeOf(instance);
@@ -703,16 +708,17 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
703
708
  return [];
704
709
  }
705
710
  const methods = Object.getOwnPropertyNames(prototype)
706
- .filter((name) => name !== 'constructor')
711
+ .filter((name) => name !== "constructor")
707
712
  .map((name) => {
708
- const candidate = prototype[name];
709
- if (typeof candidate !== 'function') {
713
+ const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
714
+ const method = descriptor?.value;
715
+ if (typeof method !== "function") {
710
716
  return null;
711
717
  }
712
- const method = candidate;
718
+ const callable = method;
713
719
  const parameterTypes = this.getDirectRunMethodParameterTypes({
714
720
  instance,
715
- method,
721
+ method: callable,
716
722
  methodName: name,
717
723
  });
718
724
  return {
@@ -728,7 +734,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
728
734
  }
729
735
  getDirectRunMethodParameterTypes(param) {
730
736
  const { instance, method, methodName } = param;
731
- const className = typeof instance === 'function'
737
+ const className = typeof instance === "function"
732
738
  ? instance.name
733
739
  : instance.constructor?.name;
734
740
  const sourceTypes = className
@@ -739,9 +745,9 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
739
745
  }
740
746
  const runtimeNames = this.extractMethodParameterNamesFromFunctionSource(methodName, method);
741
747
  if (!runtimeNames?.length) {
742
- return '[]';
748
+ return "[]";
743
749
  }
744
- return `[${runtimeNames.map((name) => `${name}: unknown`).join(', ')}]`;
750
+ return `[${runtimeNames.map((name) => `${name}: unknown`).join(", ")}]`;
745
751
  }
746
752
  extractMethodParameterTypesFromProject(className, methodName) {
747
753
  const targetClass = this.tsMorphProject
@@ -755,15 +761,15 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
755
761
  const parameters = method.getParameters();
756
762
  return `[${parameters
757
763
  .map((parameter) => `${parameter.getName()}: ${this.typeToTypeScriptCode(parameter.getType(), parameter)}`)
758
- .join(', ')}]`;
764
+ .join(", ")}]`;
759
765
  }
760
766
  extractMethodParameterNamesFromFunctionSource(methodName, method) {
761
767
  const methodSource = method.toString().trim();
762
768
  const project = new ts_morph_1.Project({ useInMemoryFileSystem: true });
763
769
  try {
764
- const sourceFile = project.createSourceFile('direct-run-method.ts', `class DirectRunMethodSource { ${methodSource} }`);
770
+ const sourceFile = project.createSourceFile("direct-run-method.ts", `class DirectRunMethodSource { ${methodSource} }`);
765
771
  const names = sourceFile
766
- .getClassOrThrow('DirectRunMethodSource')
772
+ .getClassOrThrow("DirectRunMethodSource")
767
773
  .getInstanceMethod(methodName)
768
774
  ?.getParameters()
769
775
  .map((parameter) => parameter.getName());
@@ -774,9 +780,9 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
774
780
  catch {
775
781
  }
776
782
  try {
777
- const sourceFile = project.createSourceFile('direct-run-function.ts', `const directRunMethod = ${methodSource};`);
783
+ const sourceFile = project.createSourceFile("direct-run-function.ts", `const directRunMethod = ${methodSource};`);
778
784
  const initializer = sourceFile
779
- .getVariableDeclarationOrThrow('directRunMethod')
785
+ .getVariableDeclarationOrThrow("directRunMethod")
780
786
  .getInitializer();
781
787
  const callable = initializer?.asKind(ts_morph_1.SyntaxKind.FunctionExpression) ??
782
788
  initializer?.asKind(ts_morph_1.SyntaxKind.ArrowFunction);
@@ -786,79 +792,209 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
786
792
  return undefined;
787
793
  }
788
794
  }
789
- typeToTypeScriptCode(type, enclosingNode, seenTypeTexts = new Set()) {
795
+ typeToTypeScriptCode(type, enclosingNode, state = {
796
+ activeTypes: new Set(),
797
+ depth: 0,
798
+ maxLength: MAX_TYPE_RENDER_LENGTH,
799
+ }) {
790
800
  if (type.isAny() || type.isUnknown()) {
791
- return 'unknown';
801
+ return "unknown";
792
802
  }
793
803
  if (type.isNever()) {
794
- return 'never';
804
+ return "never";
795
805
  }
796
806
  if (type.isUndefined()) {
797
- return 'undefined';
807
+ return "undefined";
798
808
  }
799
809
  if (type.isVoid()) {
800
- return 'void';
810
+ return "void";
801
811
  }
802
812
  if (type.isString()) {
803
- return 'string';
813
+ return "string";
804
814
  }
805
815
  if (type.isNumber()) {
806
- return 'number';
816
+ return "number";
807
817
  }
808
818
  if (type.isBoolean()) {
809
- return 'boolean';
819
+ return "boolean";
810
820
  }
811
821
  if (type.isNull()) {
812
- return 'null';
822
+ return "null";
813
823
  }
814
824
  if (type.isStringLiteral()) {
815
- return JSON.stringify(type.getLiteralValue());
825
+ const literalValue = type.getLiteralValue();
826
+ if (typeof literalValue !== "string" ||
827
+ literalValue.length > Math.floor(state.maxLength / 6)) {
828
+ return "string";
829
+ }
830
+ const literal = JSON.stringify(literalValue);
831
+ return literal.length <= state.maxLength ? literal : "string";
816
832
  }
817
833
  if (type.isNumberLiteral()) {
818
- return String(type.getLiteralValue());
834
+ const literal = String(type.getLiteralValue());
835
+ return literal.length <= state.maxLength ? literal : "number";
819
836
  }
820
837
  if (type.isBooleanLiteral()) {
821
- return type.getText(enclosingNode);
822
- }
823
- if (type.isUnion()) {
824
- return type
825
- .getUnionTypes()
826
- .map((unionType) => this.typeToTypeScriptCode(unionType, enclosingNode, new Set(seenTypeTexts)))
827
- .join(' | ');
828
- }
829
- if (type.isTuple()) {
830
- return `[${type
831
- .getTupleElements()
832
- .map((tupleType) => this.typeToTypeScriptCode(tupleType, enclosingNode, new Set(seenTypeTexts)))
833
- .join(', ')}]`;
834
- }
835
- if (type.isArray() || type.isReadonlyArray()) {
836
- const itemType = this.typeToTypeScriptCode(type.getArrayElementType() ?? type, enclosingNode, new Set(seenTypeTexts));
837
- return `${this.wrapArrayItemType(itemType)}[]`;
838
- }
839
- if (!type.isObject() || type.getCallSignatures().length > 0) {
840
- return type.getText(enclosingNode);
841
- }
842
- const typeText = type.getText(enclosingNode);
843
- if (seenTypeTexts.has(typeText)) {
844
- return typeText;
845
- }
846
- seenTypeTexts.add(typeText);
847
- const properties = type.getProperties();
848
- if (properties.length === 0) {
849
- return typeText;
850
- }
851
- return `{ ${properties
852
- .map((property) => {
838
+ return this.typeTextWithinLimit(type, enclosingNode, state.maxLength);
839
+ }
840
+ const isArray = type.isArray() || type.isReadonlyArray();
841
+ const isComposite = type.isUnion() || type.isTuple() || isArray || type.isObject();
842
+ if (!isComposite) {
843
+ return (this.typeReferenceCode(type) ??
844
+ this.typeTextWithinLimit(type, enclosingNode, state.maxLength));
845
+ }
846
+ if (state.activeTypes.has(type) || state.depth >= MAX_TYPE_RENDER_DEPTH) {
847
+ return this.typeReferenceCode(type) ?? "unknown";
848
+ }
849
+ state.activeTypes.add(type);
850
+ try {
851
+ if (type.isUnion()) {
852
+ const unionTypes = type.getUnionTypes();
853
+ if (unionTypes.length > MAX_TYPE_RENDER_MEMBERS) {
854
+ return "unknown";
855
+ }
856
+ return this.renderTypeSequence({
857
+ types: unionTypes,
858
+ enclosingNode,
859
+ state,
860
+ prefix: "",
861
+ delimiter: " | ",
862
+ suffix: "",
863
+ fallback: "unknown",
864
+ });
865
+ }
866
+ if (type.isTuple()) {
867
+ const tupleTypes = type.getTupleElements();
868
+ if (tupleTypes.length > MAX_TYPE_RENDER_MEMBERS) {
869
+ return this.typeFallback(state.maxLength, "unknown[]");
870
+ }
871
+ return this.renderTypeSequence({
872
+ types: tupleTypes,
873
+ enclosingNode,
874
+ state,
875
+ prefix: "[",
876
+ delimiter: ", ",
877
+ suffix: "]",
878
+ fallback: "unknown[]",
879
+ });
880
+ }
881
+ if (isArray) {
882
+ if (state.maxLength < MIN_TYPE_RENDER_LENGTH + 4) {
883
+ return this.typeFallback(state.maxLength, "unknown[]");
884
+ }
885
+ const itemType = this.typeToTypeScriptCode(type.getArrayElementType() ?? type, enclosingNode, {
886
+ ...state,
887
+ depth: state.depth + 1,
888
+ maxLength: state.maxLength - 4,
889
+ });
890
+ return `${this.wrapArrayItemType(itemType)}[]`;
891
+ }
892
+ if (type.getCallSignatures().length > 0) {
893
+ return (this.typeReferenceCode(type) ??
894
+ this.typeTextWithinLimit(type, enclosingNode, state.maxLength));
895
+ }
896
+ const properties = type.getProperties();
897
+ if (properties.length === 0) {
898
+ return (this.typeReferenceCode(type) ??
899
+ this.typeTextWithinLimit(type, enclosingNode, state.maxLength));
900
+ }
901
+ return this.renderObjectType(properties, enclosingNode, state);
902
+ }
903
+ finally {
904
+ state.activeTypes.delete(type);
905
+ }
906
+ }
907
+ renderTypeSequence(param) {
908
+ const { types, enclosingNode, state, prefix, delimiter, suffix, fallback } = param;
909
+ let output = prefix;
910
+ for (let index = 0; index < types.length; index += 1) {
911
+ const remainingTypes = types.length - index - 1;
912
+ const separator = index === 0 ? "" : delimiter;
913
+ const childMaxLength = state.maxLength -
914
+ output.length -
915
+ separator.length -
916
+ suffix.length -
917
+ remainingTypes * (delimiter.length + MIN_TYPE_RENDER_LENGTH);
918
+ if (childMaxLength < MIN_TYPE_RENDER_LENGTH) {
919
+ return this.typeFallback(state.maxLength, fallback);
920
+ }
921
+ output += `${separator}${this.typeToTypeScriptCode(types[index], enclosingNode, {
922
+ ...state,
923
+ depth: state.depth + 1,
924
+ maxLength: childMaxLength,
925
+ })}`;
926
+ }
927
+ return `${output}${suffix}`;
928
+ }
929
+ renderObjectType(properties, enclosingNode, state) {
930
+ const maxProperties = Math.min(properties.length, MAX_TYPE_RENDER_MEMBERS);
931
+ const truncation = "[key: string]: unknown";
932
+ const propertyCodes = [];
933
+ let propertyCodesLength = 0;
934
+ let truncated = properties.length > maxProperties;
935
+ for (let index = 0; index < maxProperties; index += 1) {
936
+ const property = properties[index];
937
+ const propertyName = property.getName();
938
+ if (propertyName.length > MAX_TYPE_PROPERTY_NAME_LENGTH) {
939
+ truncated = true;
940
+ break;
941
+ }
853
942
  const propertyNode = property.getValueDeclaration() ?? property.getDeclarations()[0];
854
943
  const propertyType = property.getTypeAtLocation(propertyNode ?? enclosingNode);
855
944
  const optional = property.isOptional() || this.typeAllowsUndefined(propertyType);
856
- return `${this.propertyNameToTypeScriptCode(property.getName())}${optional ? '?' : ''}: ${this.typeToTypeScriptCode(propertyType, propertyNode ?? enclosingNode, new Set(seenTypeTexts))}`;
857
- })
858
- .join('; ')} }`;
945
+ const propertyPrefix = `${this.propertyNameToTypeScriptCode(propertyName)}${optional ? "?" : ""}: `;
946
+ const separator = propertyCodes.length === 0 ? "" : "; ";
947
+ const reservedTruncationLength = 2 + (propertyCodes.length === 0 ? 0 : 2) + truncation.length;
948
+ const childMaxLength = state.maxLength -
949
+ 2 -
950
+ propertyCodesLength -
951
+ separator.length -
952
+ propertyPrefix.length -
953
+ reservedTruncationLength;
954
+ if (childMaxLength < MIN_TYPE_RENDER_LENGTH) {
955
+ truncated = true;
956
+ break;
957
+ }
958
+ const propertyCode = `${propertyPrefix}${this.typeToTypeScriptCode(propertyType, propertyNode ?? enclosingNode, {
959
+ ...state,
960
+ depth: state.depth + 1,
961
+ maxLength: childMaxLength,
962
+ })}`;
963
+ propertyCodes.push(propertyCode);
964
+ propertyCodesLength += separator.length + propertyCode.length;
965
+ }
966
+ if (truncated) {
967
+ const separator = propertyCodes.length === 0 ? "" : "; ";
968
+ const output = `{ ${propertyCodes.join("; ")}${separator}${truncation} }`;
969
+ return output.length <= state.maxLength
970
+ ? output
971
+ : this.typeFallback(state.maxLength, "unknown");
972
+ }
973
+ const output = `{ ${propertyCodes.join("; ")} }`;
974
+ return output.length <= state.maxLength
975
+ ? output
976
+ : this.typeFallback(state.maxLength, "unknown");
977
+ }
978
+ typeReferenceCode(type) {
979
+ const name = type.getAliasSymbol()?.getName() ?? type.getSymbol()?.getName();
980
+ return name && !name.startsWith("__") && /^[A-Za-z_$][\w$]*$/.test(name)
981
+ ? name
982
+ : undefined;
983
+ }
984
+ typeTextWithinLimit(type, enclosingNode, maxLength) {
985
+ try {
986
+ const typeText = type.getText(enclosingNode);
987
+ return typeText.length <= maxLength ? typeText : "unknown";
988
+ }
989
+ catch {
990
+ return "unknown";
991
+ }
992
+ }
993
+ typeFallback(maxLength, preferred) {
994
+ return preferred.length <= maxLength ? preferred : "unknown";
859
995
  }
860
996
  wrapArrayItemType(typeText) {
861
- return typeText.includes(' | ') || typeText.includes('&')
997
+ return typeText.includes(" | ") || typeText.includes("&")
862
998
  ? `(${typeText})`
863
999
  : typeText;
864
1000
  }
@@ -896,7 +1032,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
896
1032
  const key = this.dependencyNodeKey(moduleName, provider.name);
897
1033
  nodes.set(key, {
898
1034
  key,
899
- kind: 'provider',
1035
+ kind: "provider",
900
1036
  moduleName,
901
1037
  name: provider.name,
902
1038
  });
@@ -905,7 +1041,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
905
1041
  const key = this.dependencyNodeKey(moduleName, controller.name);
906
1042
  nodes.set(key, {
907
1043
  key,
908
- kind: 'controller',
1044
+ kind: "controller",
909
1045
  moduleName,
910
1046
  name: controller.name,
911
1047
  });
@@ -923,9 +1059,9 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
923
1059
  const cycles = this.findCycles(graph, nextCycleId);
924
1060
  return {
925
1061
  providers: cycles
926
- .filter((cycle) => nodes.get(cycle.from)?.kind === 'provider')
1062
+ .filter((cycle) => nodes.get(cycle.from)?.kind === "provider")
927
1063
  .map((cycle) => this.toProviderCycle(cycle, nodes)),
928
- controllers: cycles.filter((cycle) => nodes.get(cycle.from)?.kind === 'controller'),
1064
+ controllers: cycles.filter((cycle) => nodes.get(cycle.from)?.kind === "controller"),
929
1065
  };
930
1066
  }
931
1067
  addDependencyEdges(graph, sourceKey, dependencies, nodes) {
@@ -981,14 +1117,14 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
981
1117
  getCanonicalCycleKey(path) {
982
1118
  const cyclePath = path.slice(0, -1);
983
1119
  if (cyclePath.length <= 1) {
984
- return cyclePath.join('->');
1120
+ return cyclePath.join("->");
985
1121
  }
986
1122
  const rotations = cyclePath.map((_, index) => [
987
1123
  ...cyclePath.slice(index),
988
1124
  ...cyclePath.slice(0, index),
989
1125
  ]);
990
1126
  return rotations
991
- .map((rotation) => rotation.join('->'))
1127
+ .map((rotation) => rotation.join("->"))
992
1128
  .sort((a, b) => a.localeCompare(b))[0];
993
1129
  }
994
1130
  toProviderCycle(cycle, nodes) {
@@ -1005,10 +1141,10 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
1005
1141
  provider: { name: node.name },
1006
1142
  };
1007
1143
  }
1008
- const separatorIndex = key.indexOf(':');
1144
+ const separatorIndex = key.indexOf(":");
1009
1145
  if (separatorIndex === -1) {
1010
1146
  return {
1011
- module: { name: '' },
1147
+ module: { name: "" },
1012
1148
  provider: { name: key },
1013
1149
  };
1014
1150
  }
@@ -1019,9 +1155,9 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
1019
1155
  }
1020
1156
  getCycleType(source, target, graph) {
1021
1157
  if (source === target || graph.get(target)?.has(source)) {
1022
- return 'direct';
1158
+ return "direct";
1023
1159
  }
1024
- return 'indirect';
1160
+ return "indirect";
1025
1161
  }
1026
1162
  findPath(source, target, graph) {
1027
1163
  const visited = new Set();
@@ -1061,18 +1197,18 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
1061
1197
  return `${moduleName}:${dependencyName}`;
1062
1198
  }
1063
1199
  enrichDependency(dependency, currentModule) {
1064
- const colonIndex = dependency.indexOf(':');
1200
+ const colonIndex = dependency.indexOf(":");
1065
1201
  if (colonIndex !== -1) {
1066
1202
  return {
1067
1203
  providedBy: {
1068
- type: 'module',
1204
+ type: "module",
1069
1205
  name: dependency.substring(0, colonIndex),
1070
1206
  },
1071
1207
  token: dependency.substring(colonIndex + 1),
1072
1208
  };
1073
1209
  }
1074
1210
  return {
1075
- providedBy: { type: 'module', name: currentModule },
1211
+ providedBy: { type: "module", name: currentModule },
1076
1212
  token: dependency,
1077
1213
  };
1078
1214
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nest-graph-inspector",
3
- "version": "0.7.1",
3
+ "version": "0.8.0-test.1",
4
4
  "description": "NestJS module graph inspector for discovery and dependency analysis",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",