nest-graph-inspector 0.7.2 → 0.8.0-test.2
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/README.md +22 -0
- package/dist/adapters/discovery.d.ts +71 -0
- package/dist/adapters/discovery.js +542 -0
- package/dist/adapters/http-output.adapter.d.ts +3 -1
- package/dist/adapters/http-output.adapter.js +7 -2
- package/dist/adapters/viewer-output.adapter.d.ts +2 -3
- package/dist/nest-graph-inspector.module.js +2 -0
- package/dist/nest-graph-inspector.setup.d.ts +23 -56
- package/dist/nest-graph-inspector.setup.js +287 -595
- package/package.json +1 -1
|
@@ -18,6 +18,7 @@ const node_fs_1 = require("node:fs");
|
|
|
18
18
|
const node_path_1 = require("node:path");
|
|
19
19
|
const common_1 = require("@nestjs/common");
|
|
20
20
|
const core_1 = require("@nestjs/core");
|
|
21
|
+
const discovery_1 = require("./adapters/discovery");
|
|
21
22
|
const nest_graph_inspector_config_1 = require("./nest-graph-inspector.config");
|
|
22
23
|
const nest_graph_inspector_module_1 = require("./nest-graph-inspector.module");
|
|
23
24
|
const http_output_adapter_1 = require("./adapters/http-output.adapter");
|
|
@@ -26,9 +27,15 @@ const json_output_adapter_1 = require("./adapters/json-output.adapter");
|
|
|
26
27
|
const viewer_output_adapter_1 = require("./adapters/viewer-output.adapter");
|
|
27
28
|
const ts_morph_1 = require("ts-morph");
|
|
28
29
|
const runtime_trace_recorder_1 = require("./runtime-trace.recorder");
|
|
30
|
+
const MAX_TYPE_RENDER_DEPTH = 12;
|
|
31
|
+
const MAX_TYPE_RENDER_MEMBERS = 50;
|
|
32
|
+
const MAX_TYPE_RENDER_LENGTH = 8_000;
|
|
33
|
+
const MAX_TYPE_PROPERTY_NAME_LENGTH = 160;
|
|
34
|
+
const MIN_TYPE_RENDER_LENGTH = "unknown".length;
|
|
29
35
|
let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspectorSetup {
|
|
30
36
|
options;
|
|
31
37
|
modulesContainer;
|
|
38
|
+
discovery;
|
|
32
39
|
httpOutputAdapter;
|
|
33
40
|
fileOutputAdapter;
|
|
34
41
|
jsonOutputAdapter;
|
|
@@ -36,15 +43,12 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
36
43
|
runtimeTraceRecorder;
|
|
37
44
|
logger = new common_1.Logger(NestGraphInspectorSetup_1.name);
|
|
38
45
|
outputAdapters;
|
|
39
|
-
ignoreProvider;
|
|
40
|
-
ignoreImport;
|
|
41
|
-
nestCoreModuleName;
|
|
42
|
-
nestCoreProviders;
|
|
43
46
|
tsMorphProject = this.createTsMorphProject();
|
|
44
|
-
|
|
45
|
-
constructor(options, modulesContainer, httpOutputAdapter, fileOutputAdapter, jsonOutputAdapter, viewerOutputAdapter, runtimeTraceRecorder) {
|
|
47
|
+
graphOutput;
|
|
48
|
+
constructor(options, modulesContainer, discovery, httpOutputAdapter, fileOutputAdapter, jsonOutputAdapter, viewerOutputAdapter, runtimeTraceRecorder) {
|
|
46
49
|
this.options = options;
|
|
47
50
|
this.modulesContainer = modulesContainer;
|
|
51
|
+
this.discovery = discovery;
|
|
48
52
|
this.httpOutputAdapter = httpOutputAdapter;
|
|
49
53
|
this.fileOutputAdapter = fileOutputAdapter;
|
|
50
54
|
this.jsonOutputAdapter = jsonOutputAdapter;
|
|
@@ -56,80 +60,50 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
56
60
|
json: this.jsonOutputAdapter,
|
|
57
61
|
viewer: this.viewerOutputAdapter,
|
|
58
62
|
};
|
|
59
|
-
this.ignoreProvider = [
|
|
60
|
-
...(this.options.ignoreProvider ?? nest_graph_inspector_module_1.defaultOptions.ignoreProvider ?? []),
|
|
61
|
-
];
|
|
62
|
-
this.ignoreImport = [
|
|
63
|
-
...(this.options.ignoreImport ?? nest_graph_inspector_module_1.defaultOptions.ignoreImport ?? []),
|
|
64
|
-
];
|
|
65
|
-
this.nestCoreModuleName =
|
|
66
|
-
this.options.nestCoreModuleName ??
|
|
67
|
-
nest_graph_inspector_module_1.defaultOptions.nestCoreModuleName ??
|
|
68
|
-
'NestJSCoreModule';
|
|
69
|
-
this.nestCoreProviders = [
|
|
70
|
-
...(this.options.nestCoreProviders ??
|
|
71
|
-
nest_graph_inspector_module_1.defaultOptions.nestCoreProviders ??
|
|
72
|
-
[]),
|
|
73
|
-
];
|
|
74
63
|
}
|
|
75
64
|
async onModuleInit() {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
async inspectAndPublishGraph() {
|
|
79
|
-
if (!this.hasOutput) {
|
|
65
|
+
const outputs = this.options.outputs ?? [];
|
|
66
|
+
if (!outputs.length) {
|
|
80
67
|
return;
|
|
81
68
|
}
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
await this.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const rootModuleClass = this.options.rootModule;
|
|
91
|
-
return rootModuleClass
|
|
92
|
-
? this.getRootModuleFromClass(rootModuleClass)
|
|
93
|
-
: this.findRootModule();
|
|
94
|
-
}
|
|
95
|
-
getRootModuleFromClass(rootModuleClass) {
|
|
96
|
-
const root = [...this.modulesContainer.values()].find((m) => m.metatype === rootModuleClass);
|
|
97
|
-
if (!root) {
|
|
98
|
-
throw new Error(`Root module not found: ${rootModuleClass.name}`);
|
|
69
|
+
const viewerOutputs = outputs.filter((output) => output.type === "viewer");
|
|
70
|
+
const eagerOutputs = outputs.filter((output) => output.type !== "viewer");
|
|
71
|
+
await Promise.all(viewerOutputs.map((output) => this.installViewerOutput(output)));
|
|
72
|
+
if (eagerOutputs.length) {
|
|
73
|
+
await this.publishOutputs({
|
|
74
|
+
graphOutput: this.getGraphOutput(),
|
|
75
|
+
outputs: eagerOutputs,
|
|
76
|
+
});
|
|
99
77
|
}
|
|
100
|
-
return root;
|
|
101
78
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
this.
|
|
107
|
-
return moduleTree;
|
|
79
|
+
getGraphOutput() {
|
|
80
|
+
if (!this.graphOutput) {
|
|
81
|
+
this.graphOutput = this.createGraphOutput(this.createModuleMapFromTree(this.discovery.scan()));
|
|
82
|
+
}
|
|
83
|
+
return this.graphOutput;
|
|
108
84
|
}
|
|
109
|
-
async
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
85
|
+
async installViewerOutput(output) {
|
|
86
|
+
const configuredOutput = this.withDefaultOutputOptions(output);
|
|
87
|
+
if (configuredOutput.type !== "viewer") {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const { message } = await this.viewerOutputAdapter.execute(() => this.getGraphOutput(), configuredOutput);
|
|
92
|
+
this.logger.debug(message);
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
this.logger.error("Failed to execute output adapter for type viewer", err);
|
|
96
|
+
}
|
|
114
97
|
}
|
|
115
98
|
createGraphOutput(moduleMap) {
|
|
116
99
|
return this.enrichModuleMap(moduleMap);
|
|
117
100
|
}
|
|
118
|
-
prepareOutputs() {
|
|
119
|
-
return this.options.outputs ?? [];
|
|
120
|
-
}
|
|
121
101
|
async publishOutputs(param) {
|
|
122
|
-
await Promise.all(param.outputs.map(
|
|
123
|
-
await this.publishSingleOutput(param.graphOutput, output);
|
|
124
|
-
}));
|
|
102
|
+
await Promise.all(param.outputs.map((output) => this.publishSingleOutput(param.graphOutput, output)));
|
|
125
103
|
}
|
|
126
104
|
async publishSingleOutput(graphOutput, output) {
|
|
127
105
|
output = this.withDefaultOutputOptions(output);
|
|
128
106
|
const adapter = this.outputAdapters[output.type];
|
|
129
|
-
if (!adapter) {
|
|
130
|
-
this.logger.warn(`Unsupported output type: ${output.type}`);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
107
|
try {
|
|
134
108
|
const { message } = await adapter.execute(graphOutput, output);
|
|
135
109
|
this.logger.debug(message);
|
|
@@ -139,11 +113,11 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
139
113
|
}
|
|
140
114
|
}
|
|
141
115
|
withDefaultOutputOptions(output) {
|
|
142
|
-
if (output.type !==
|
|
116
|
+
if (output.type !== "viewer") {
|
|
143
117
|
return output;
|
|
144
118
|
}
|
|
145
|
-
const defaultViewerOutput = nest_graph_inspector_module_1.defaultOptions.outputs?.find((defaultOutput) => defaultOutput.type ===
|
|
146
|
-
if (!defaultViewerOutput || defaultViewerOutput.type !==
|
|
119
|
+
const defaultViewerOutput = nest_graph_inspector_module_1.defaultOptions.outputs?.find((defaultOutput) => defaultOutput.type === "viewer");
|
|
120
|
+
if (!defaultViewerOutput || defaultViewerOutput.type !== "viewer") {
|
|
147
121
|
return output;
|
|
148
122
|
}
|
|
149
123
|
return {
|
|
@@ -167,289 +141,57 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
167
141
|
};
|
|
168
142
|
}
|
|
169
143
|
getDirectRunHistoryDirPath() {
|
|
170
|
-
const jsonOutput = this.options.outputs?.find((output) => output.type ===
|
|
171
|
-
return jsonOutput?.type ===
|
|
172
|
-
? (0, node_path_1.join)(process.cwd(), (0, node_path_1.dirname)(jsonOutput.path),
|
|
144
|
+
const jsonOutput = this.options.outputs?.find((output) => output.type === "json");
|
|
145
|
+
return jsonOutput?.type === "json"
|
|
146
|
+
? (0, node_path_1.join)(process.cwd(), (0, node_path_1.dirname)(jsonOutput.path), "direct-run", "history")
|
|
173
147
|
: undefined;
|
|
174
148
|
}
|
|
175
149
|
buildModuleMapFromAutoDetect() {
|
|
176
|
-
const
|
|
177
|
-
|
|
178
|
-
|
|
150
|
+
const discovery = this.options.rootModule
|
|
151
|
+
? this.createDiscovery({ ...this.options, rootModule: undefined })
|
|
152
|
+
: this.discovery;
|
|
153
|
+
return this.createModuleMapFromTree(discovery.scan());
|
|
179
154
|
}
|
|
180
155
|
buildModuleMap(rootModuleClass) {
|
|
181
|
-
const
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
return
|
|
188
|
-
version: '3',
|
|
189
|
-
root: moduleTree.name,
|
|
190
|
-
modules,
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
findRootModule() {
|
|
194
|
-
for (const moduleRef of this.modulesContainer.values()) {
|
|
195
|
-
const moduleName = this.moduleName(moduleRef);
|
|
196
|
-
if (this.ignoreImport.includes(moduleName) ||
|
|
197
|
-
moduleName === 'InternalCoreModule') {
|
|
198
|
-
continue;
|
|
199
|
-
}
|
|
200
|
-
for (const importedModule of moduleRef.imports.values()) {
|
|
201
|
-
if (importedModule.metatype === nest_graph_inspector_module_1.NestGraphInspectorModule) {
|
|
202
|
-
return moduleRef;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
throw new Error('Could not auto-detect root module. No module imports NestGraphInspectorModule.');
|
|
156
|
+
const discovery = this.options.rootModule === rootModuleClass
|
|
157
|
+
? this.discovery
|
|
158
|
+
: this.createDiscovery({
|
|
159
|
+
...this.options,
|
|
160
|
+
rootModule: rootModuleClass,
|
|
161
|
+
});
|
|
162
|
+
return this.createModuleMapFromTree(discovery.scan());
|
|
207
163
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
return this.createModuleTreeReference(moduleRef);
|
|
211
|
-
}
|
|
212
|
-
visited.add(moduleRef);
|
|
213
|
-
return {
|
|
214
|
-
name: this.moduleName(moduleRef),
|
|
215
|
-
jsdoc: this.extractModuleJsDoc(moduleRef),
|
|
216
|
-
moduleRef,
|
|
217
|
-
imports: [],
|
|
218
|
-
exports: [],
|
|
219
|
-
providers: [],
|
|
220
|
-
controllers: [],
|
|
221
|
-
children: [...moduleRef.imports.values()]
|
|
222
|
-
.filter((childModule) => !this.shouldIgnoreModule(childModule))
|
|
223
|
-
.map((childModule) => this.resolveModuleTree(childModule, visited)),
|
|
224
|
-
};
|
|
164
|
+
createDiscovery(options) {
|
|
165
|
+
return new discovery_1.DiscoveryAdapter(options, this.modulesContainer, this.runtimeTraceRecorder);
|
|
225
166
|
}
|
|
226
|
-
|
|
167
|
+
createModuleMapFromTree(moduleTree) {
|
|
227
168
|
return {
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
imports: [],
|
|
232
|
-
exports: [],
|
|
233
|
-
providers: [],
|
|
234
|
-
controllers: [],
|
|
235
|
-
children: [],
|
|
169
|
+
version: "3",
|
|
170
|
+
root: moduleTree.name,
|
|
171
|
+
modules: this.flattenModuleTree(moduleTree),
|
|
236
172
|
};
|
|
237
173
|
}
|
|
238
|
-
resolveModuleMembers(moduleTree) {
|
|
239
|
-
this.walkModuleTree(moduleTree, (node) => {
|
|
240
|
-
if (!node.moduleRef) {
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
node.imports = node.children.map((child) => child.name);
|
|
244
|
-
node.exports = this.extractExports(node.moduleRef);
|
|
245
|
-
node.providers = this.extractProviders(node.moduleRef, node.name);
|
|
246
|
-
node.controllers = this.extractControllers(node.moduleRef, node.name);
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
instrumentRuntimeTrace(moduleTree) {
|
|
250
|
-
this.walkModuleTree(moduleTree, (node) => {
|
|
251
|
-
if (!node.moduleRef) {
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
this.instrumentRuntimeTraceWrappers({
|
|
255
|
-
moduleName: node.name,
|
|
256
|
-
type: 'provider',
|
|
257
|
-
wrappers: node.moduleRef.providers.values(),
|
|
258
|
-
shouldIgnore: (name) => name === node.name || this.ignoreProvider.includes(name),
|
|
259
|
-
});
|
|
260
|
-
this.instrumentRuntimeTraceWrappers({
|
|
261
|
-
moduleName: node.name,
|
|
262
|
-
type: 'controller',
|
|
263
|
-
wrappers: node.moduleRef.controllers.values(),
|
|
264
|
-
});
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
instrumentRuntimeTraceWrappers(param) {
|
|
268
|
-
for (const wrapper of param.wrappers) {
|
|
269
|
-
const instance = wrapper.instance;
|
|
270
|
-
if (!instance || typeof instance !== 'object') {
|
|
271
|
-
continue;
|
|
272
|
-
}
|
|
273
|
-
const className = this.wrapperClassName(wrapper);
|
|
274
|
-
if (!className || param.shouldIgnore?.(className)) {
|
|
275
|
-
continue;
|
|
276
|
-
}
|
|
277
|
-
this.instrumentRuntimeTraceInstance({
|
|
278
|
-
instance,
|
|
279
|
-
moduleName: param.moduleName,
|
|
280
|
-
className,
|
|
281
|
-
type: param.type,
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
instrumentRuntimeTraceInstance(param) {
|
|
286
|
-
if (this.runtimeTraceInstrumentedInstances.has(param.instance)) {
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
this.runtimeTraceInstrumentedInstances.add(param.instance);
|
|
290
|
-
const prototype = Object.getPrototypeOf(param.instance);
|
|
291
|
-
if (!prototype) {
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
294
|
-
for (const methodName of Object.getOwnPropertyNames(prototype)) {
|
|
295
|
-
if (methodName === 'constructor') {
|
|
296
|
-
continue;
|
|
297
|
-
}
|
|
298
|
-
const descriptor = Object.getOwnPropertyDescriptor(prototype, methodName);
|
|
299
|
-
const method = descriptor?.value;
|
|
300
|
-
if (typeof method !== 'function') {
|
|
301
|
-
continue;
|
|
302
|
-
}
|
|
303
|
-
Object.defineProperty(param.instance, methodName, {
|
|
304
|
-
configurable: true,
|
|
305
|
-
writable: true,
|
|
306
|
-
value: (...args) => this.runtimeTraceRecorder.recordSpan({
|
|
307
|
-
name: `${param.className}.${methodName}`,
|
|
308
|
-
type: param.type,
|
|
309
|
-
moduleName: param.moduleName,
|
|
310
|
-
className: param.className,
|
|
311
|
-
methodName,
|
|
312
|
-
args: this.runtimeTraceRecorder.previewValue(args),
|
|
313
|
-
}, () => method.apply(param.instance, args)),
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
appendNestCoreModule(moduleTree) {
|
|
318
|
-
const usedProviders = this.findUsedNestCoreProvidersFromTree(moduleTree);
|
|
319
|
-
if (!usedProviders.length) {
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
moduleTree.children.push({
|
|
323
|
-
name: this.nestCoreModuleName,
|
|
324
|
-
moduleRef: null,
|
|
325
|
-
imports: [],
|
|
326
|
-
exports: [...usedProviders],
|
|
327
|
-
providers: usedProviders.map((name) => ({
|
|
328
|
-
name,
|
|
329
|
-
dependencies: [],
|
|
330
|
-
})),
|
|
331
|
-
controllers: [],
|
|
332
|
-
children: [],
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
findUsedNestCoreProvidersFromTree(moduleTree) {
|
|
336
|
-
const usedProviders = new Set();
|
|
337
|
-
this.walkModuleTree(moduleTree, (node) => {
|
|
338
|
-
for (const provider of node.providers) {
|
|
339
|
-
for (const dependencyName of provider.dependencies) {
|
|
340
|
-
const nestCoreProviderName = this.extractNestCoreProviderName(dependencyName);
|
|
341
|
-
if (nestCoreProviderName) {
|
|
342
|
-
usedProviders.add(nestCoreProviderName);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
for (const controller of node.controllers) {
|
|
347
|
-
for (const dependencyName of controller.dependencies) {
|
|
348
|
-
const nestCoreProviderName = this.extractNestCoreProviderName(dependencyName);
|
|
349
|
-
if (nestCoreProviderName) {
|
|
350
|
-
usedProviders.add(nestCoreProviderName);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
});
|
|
355
|
-
return this.nestCoreProviders.filter((providerName) => usedProviders.has(providerName));
|
|
356
|
-
}
|
|
357
174
|
flattenModuleTree(moduleTree) {
|
|
358
175
|
const modules = {};
|
|
359
|
-
|
|
360
|
-
if (modules[node.name]
|
|
361
|
-
|
|
176
|
+
const visit = (node) => {
|
|
177
|
+
if (!modules[node.name]) {
|
|
178
|
+
modules[node.name] = {
|
|
179
|
+
...(node.jsdoc ? { jsdoc: node.jsdoc } : {}),
|
|
180
|
+
imports: node.imports,
|
|
181
|
+
exports: node.exports,
|
|
182
|
+
providers: node.providers,
|
|
183
|
+
controllers: node.controllers,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
for (const child of node.children) {
|
|
187
|
+
visit(child);
|
|
362
188
|
}
|
|
363
|
-
modules[node.name] = {
|
|
364
|
-
...(node.jsdoc ? { jsdoc: node.jsdoc } : {}),
|
|
365
|
-
imports: node.imports,
|
|
366
|
-
exports: node.exports,
|
|
367
|
-
providers: node.providers,
|
|
368
|
-
controllers: node.controllers,
|
|
369
|
-
};
|
|
370
|
-
});
|
|
371
|
-
return modules;
|
|
372
|
-
}
|
|
373
|
-
walkModuleTree(moduleTree, visit) {
|
|
374
|
-
visit(moduleTree);
|
|
375
|
-
for (const child of moduleTree.children) {
|
|
376
|
-
this.walkModuleTree(child, visit);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
shouldIgnoreModule(moduleRef) {
|
|
380
|
-
return this.ignoreImport.includes(this.moduleName(moduleRef));
|
|
381
|
-
}
|
|
382
|
-
extractNestCoreProviderName(dependencyName) {
|
|
383
|
-
const prefix = `${this.nestCoreModuleName}:`;
|
|
384
|
-
if (!dependencyName.startsWith(prefix)) {
|
|
385
|
-
return null;
|
|
386
|
-
}
|
|
387
|
-
const providerName = dependencyName.slice(prefix.length);
|
|
388
|
-
if (!providerName) {
|
|
389
|
-
return null;
|
|
390
|
-
}
|
|
391
|
-
if (!this.nestCoreProviders.includes(providerName)) {
|
|
392
|
-
return null;
|
|
393
|
-
}
|
|
394
|
-
return providerName;
|
|
395
|
-
}
|
|
396
|
-
extractImports(moduleRef) {
|
|
397
|
-
return [...moduleRef.imports.values()]
|
|
398
|
-
.map((importedModuleRef) => this.moduleName(importedModuleRef))
|
|
399
|
-
.filter((importName) => !this.ignoreImport.includes(importName));
|
|
400
|
-
}
|
|
401
|
-
extractExports(moduleRef) {
|
|
402
|
-
return [...moduleRef.exports.values()]
|
|
403
|
-
.map((exportedItem) => this.exportName(exportedItem))
|
|
404
|
-
.filter((exportName) => !!exportName && !this.ignoreProvider.includes(exportName));
|
|
405
|
-
}
|
|
406
|
-
extractProviders(moduleRef, moduleName) {
|
|
407
|
-
return this.extractModuleMembers({
|
|
408
|
-
wrappers: moduleRef.providers.values(),
|
|
409
|
-
moduleRef,
|
|
410
|
-
extract: (wrapper) => this.extractModuleMember({
|
|
411
|
-
wrapper,
|
|
412
|
-
moduleRef,
|
|
413
|
-
shouldIgnore: (name) => this.ignoreProvider.includes(name),
|
|
414
|
-
}),
|
|
415
|
-
shouldSkip: (provider) => provider.name === moduleName,
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
extractControllers(moduleRef, moduleName) {
|
|
419
|
-
return this.extractModuleMembers({
|
|
420
|
-
wrappers: moduleRef.controllers.values(),
|
|
421
|
-
moduleRef,
|
|
422
|
-
extract: (wrapper) => this.extractModuleMember({ wrapper, moduleRef }),
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
|
-
extractModuleMembers(param) {
|
|
426
|
-
return [...param.wrappers].reduce((items, wrapper) => {
|
|
427
|
-
const executableWrapper = wrapper;
|
|
428
|
-
const item = param.extract(executableWrapper, param.moduleRef);
|
|
429
|
-
if (item) {
|
|
430
|
-
if (param.shouldSkip?.(item)) {
|
|
431
|
-
return items;
|
|
432
|
-
}
|
|
433
|
-
items.push(item);
|
|
434
|
-
}
|
|
435
|
-
return items;
|
|
436
|
-
}, []);
|
|
437
|
-
}
|
|
438
|
-
extractModuleMember(param) {
|
|
439
|
-
const { wrapper, moduleRef, shouldIgnore = () => false } = param;
|
|
440
|
-
const name = this.wrapperClassName(wrapper) || this.tokenName(wrapper.token);
|
|
441
|
-
if (!name || shouldIgnore(name)) {
|
|
442
|
-
return null;
|
|
443
|
-
}
|
|
444
|
-
const jsdoc = this.extractClassJsDoc(wrapper);
|
|
445
|
-
return {
|
|
446
|
-
name,
|
|
447
|
-
...(jsdoc ? { jsdoc } : {}),
|
|
448
|
-
dependencies: this.extractDependencies(wrapper, moduleRef),
|
|
449
189
|
};
|
|
190
|
+
visit(moduleTree);
|
|
191
|
+
return modules;
|
|
450
192
|
}
|
|
451
193
|
createTsMorphProject() {
|
|
452
|
-
const tsConfigFilePath = (0, node_path_1.join)(process.cwd(),
|
|
194
|
+
const tsConfigFilePath = (0, node_path_1.join)(process.cwd(), "tsconfig.json");
|
|
453
195
|
if (!(0, node_fs_1.existsSync)(tsConfigFilePath)) {
|
|
454
196
|
this.logger.warn(`Could not find tsconfig.json at ${tsConfigFilePath}; JSDoc metadata will be skipped.`);
|
|
455
197
|
return new ts_morph_1.Project();
|
|
@@ -459,184 +201,6 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
459
201
|
skipAddingFilesFromTsConfig: false,
|
|
460
202
|
});
|
|
461
203
|
}
|
|
462
|
-
extractModuleJsDoc(moduleRef) {
|
|
463
|
-
return moduleRef.metatype
|
|
464
|
-
? this.extractClassJsDocByName(moduleRef.metatype.name)
|
|
465
|
-
: undefined;
|
|
466
|
-
}
|
|
467
|
-
extractClassJsDoc(wrapper) {
|
|
468
|
-
const className = this.wrapperClassName(wrapper);
|
|
469
|
-
return className ? this.extractClassJsDocByName(className) : undefined;
|
|
470
|
-
}
|
|
471
|
-
extractClassJsDocByName(className) {
|
|
472
|
-
const targetClass = this.tsMorphProject
|
|
473
|
-
.getSourceFiles()
|
|
474
|
-
.flatMap((sourceFile) => sourceFile.getClasses())
|
|
475
|
-
.find((classDeclaration) => classDeclaration.getName() === className);
|
|
476
|
-
return targetClass
|
|
477
|
-
?.getJsDocs()
|
|
478
|
-
.map((doc) => doc.getCommentText())
|
|
479
|
-
.filter((comment) => !!comment)
|
|
480
|
-
.join('\n');
|
|
481
|
-
}
|
|
482
|
-
wrapperClassName(wrapper) {
|
|
483
|
-
if (wrapper.metatype?.name) {
|
|
484
|
-
return wrapper.metatype.name;
|
|
485
|
-
}
|
|
486
|
-
const instance = wrapper.instance;
|
|
487
|
-
if (instance &&
|
|
488
|
-
(typeof instance === 'object' || typeof instance === 'function')) {
|
|
489
|
-
return instance.constructor?.name ?? null;
|
|
490
|
-
}
|
|
491
|
-
return null;
|
|
492
|
-
}
|
|
493
|
-
extractDependencies(wrapper, moduleRef) {
|
|
494
|
-
const dependencies = new Set();
|
|
495
|
-
if (Array.isArray(wrapper?.inject)) {
|
|
496
|
-
for (const token of wrapper.inject) {
|
|
497
|
-
const dependencyName = this.resolveDependencyName(this.resolveInjectionToken(token), moduleRef);
|
|
498
|
-
if (dependencyName) {
|
|
499
|
-
dependencies.add(dependencyName);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
const ctorDeps = wrapper.getCtorMetadata?.() ?? [];
|
|
504
|
-
for (const depWrapper of ctorDeps) {
|
|
505
|
-
if (depWrapper) {
|
|
506
|
-
const dependencyName = this.resolveDependencyName(depWrapper.token, moduleRef);
|
|
507
|
-
if (dependencyName && dependencyName !== 'Object') {
|
|
508
|
-
dependencies.add(dependencyName);
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
const propertyDeps = wrapper.getPropertiesMetadata?.() ?? [];
|
|
513
|
-
for (const propertyDep of propertyDeps) {
|
|
514
|
-
const depWrapper = propertyDep?.wrapper;
|
|
515
|
-
if (depWrapper) {
|
|
516
|
-
const dependencyName = this.resolveDependencyName(depWrapper.token, moduleRef);
|
|
517
|
-
if (dependencyName && dependencyName !== 'Object') {
|
|
518
|
-
dependencies.add(dependencyName);
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
return [...dependencies];
|
|
523
|
-
}
|
|
524
|
-
resolveInjectionToken(token) {
|
|
525
|
-
if (typeof token === 'object' && token !== null && 'token' in token) {
|
|
526
|
-
return token.token;
|
|
527
|
-
}
|
|
528
|
-
return token;
|
|
529
|
-
}
|
|
530
|
-
resolveDependencyName(token, moduleRef) {
|
|
531
|
-
const tokenName = this.tokenName(token);
|
|
532
|
-
if (!tokenName) {
|
|
533
|
-
return null;
|
|
534
|
-
}
|
|
535
|
-
if (this.nestCoreProviders.includes(tokenName)) {
|
|
536
|
-
return `${this.nestCoreModuleName}:${tokenName}`;
|
|
537
|
-
}
|
|
538
|
-
const ownProviderDependencyName = this.findProviderDependencyNameByToken(moduleRef, token, false);
|
|
539
|
-
if (ownProviderDependencyName) {
|
|
540
|
-
return ownProviderDependencyName;
|
|
541
|
-
}
|
|
542
|
-
for (const importedModuleRef of moduleRef.imports.values()) {
|
|
543
|
-
if (!this.isExportedProviderToken(importedModuleRef, token)) {
|
|
544
|
-
continue;
|
|
545
|
-
}
|
|
546
|
-
const importedProviderDependencyName = this.findProviderDependencyNameByToken(importedModuleRef, token, true);
|
|
547
|
-
if (importedProviderDependencyName) {
|
|
548
|
-
return importedProviderDependencyName;
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
return this.formatDependencyName(tokenName, moduleRef);
|
|
552
|
-
}
|
|
553
|
-
findProviderDependencyNameByToken(moduleRef, token, includeModulePrefix) {
|
|
554
|
-
const moduleName = this.moduleName(moduleRef);
|
|
555
|
-
for (const wrapper of moduleRef.providers.values()) {
|
|
556
|
-
if (!this.isSameProviderToken(wrapper, token)) {
|
|
557
|
-
continue;
|
|
558
|
-
}
|
|
559
|
-
const providerInstance = wrapper.instance &&
|
|
560
|
-
(typeof wrapper.instance === 'object' ||
|
|
561
|
-
typeof wrapper.instance === 'function')
|
|
562
|
-
? wrapper.instance
|
|
563
|
-
: null;
|
|
564
|
-
const providerName = wrapper.metatype?.name ||
|
|
565
|
-
providerInstance?.constructor?.name ||
|
|
566
|
-
this.tokenName(wrapper.token);
|
|
567
|
-
if (!providerName) {
|
|
568
|
-
return null;
|
|
569
|
-
}
|
|
570
|
-
if (includeModulePrefix) {
|
|
571
|
-
return `${moduleName}:${providerName}`;
|
|
572
|
-
}
|
|
573
|
-
return providerName;
|
|
574
|
-
}
|
|
575
|
-
return null;
|
|
576
|
-
}
|
|
577
|
-
isSameProviderToken(wrapper, token) {
|
|
578
|
-
if (wrapper?.token === token) {
|
|
579
|
-
return true;
|
|
580
|
-
}
|
|
581
|
-
if (wrapper?.metatype === token) {
|
|
582
|
-
return true;
|
|
583
|
-
}
|
|
584
|
-
if (wrapper.instance &&
|
|
585
|
-
typeof wrapper.instance === 'object' &&
|
|
586
|
-
wrapper.instance.constructor === token) {
|
|
587
|
-
return true;
|
|
588
|
-
}
|
|
589
|
-
return false;
|
|
590
|
-
}
|
|
591
|
-
exportedNames(moduleRef) {
|
|
592
|
-
return new Set([...moduleRef.exports.values()]
|
|
593
|
-
.map((exportedItem) => this.exportName(exportedItem))
|
|
594
|
-
.filter((exportName) => !!exportName));
|
|
595
|
-
}
|
|
596
|
-
exportName(exportedItem) {
|
|
597
|
-
return this.tokenName(exportedItem);
|
|
598
|
-
}
|
|
599
|
-
isExportedProviderToken(moduleRef, token) {
|
|
600
|
-
const tokenName = this.tokenName(token);
|
|
601
|
-
for (const exportedItem of moduleRef.exports.values()) {
|
|
602
|
-
if (exportedItem === token) {
|
|
603
|
-
return true;
|
|
604
|
-
}
|
|
605
|
-
if (tokenName && this.exportName(exportedItem) === tokenName) {
|
|
606
|
-
return true;
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
return false;
|
|
610
|
-
}
|
|
611
|
-
formatDependencyName(dependencyName, moduleRef) {
|
|
612
|
-
if (this.nestCoreProviders.includes(dependencyName)) {
|
|
613
|
-
return `${this.nestCoreModuleName}:${dependencyName}`;
|
|
614
|
-
}
|
|
615
|
-
for (const importedModuleRef of moduleRef.imports.values()) {
|
|
616
|
-
const importedModuleName = this.moduleName(importedModuleRef);
|
|
617
|
-
const exportedNames = this.exportedNames(importedModuleRef);
|
|
618
|
-
if (exportedNames.has(dependencyName)) {
|
|
619
|
-
return `${importedModuleName}:${dependencyName}`;
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
return dependencyName;
|
|
623
|
-
}
|
|
624
|
-
moduleName(moduleRef) {
|
|
625
|
-
return (moduleRef.metatype?.name ||
|
|
626
|
-
this.tokenName(moduleRef.token) ||
|
|
627
|
-
'AnonymousModule');
|
|
628
|
-
}
|
|
629
|
-
tokenName(token) {
|
|
630
|
-
if (!token)
|
|
631
|
-
return null;
|
|
632
|
-
if (typeof token === 'string')
|
|
633
|
-
return token;
|
|
634
|
-
if (typeof token === 'symbol')
|
|
635
|
-
return token.toString();
|
|
636
|
-
if (typeof token === 'function')
|
|
637
|
-
return token.name;
|
|
638
|
-
return null;
|
|
639
|
-
}
|
|
640
204
|
enrichModuleMap(moduleMap) {
|
|
641
205
|
if (!moduleMap.modules) {
|
|
642
206
|
return moduleMap;
|
|
@@ -676,26 +240,23 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
676
240
|
};
|
|
677
241
|
}
|
|
678
242
|
findDirectRunProviderInstance(moduleName, providerName) {
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
243
|
+
return this.findModuleTree(this.discovery.scan(), moduleName)?.providerInstances.get(providerName);
|
|
244
|
+
}
|
|
245
|
+
findModuleTree(moduleTree, moduleName) {
|
|
246
|
+
if (moduleTree.name === moduleName) {
|
|
247
|
+
return moduleTree;
|
|
682
248
|
}
|
|
683
|
-
const
|
|
684
|
-
const
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
instance?.constructor?.name ||
|
|
691
|
-
this.tokenName(wrapper.token);
|
|
692
|
-
return resolvedName === providerName;
|
|
693
|
-
});
|
|
694
|
-
return providerWrapper?.instance;
|
|
249
|
+
for (const child of moduleTree.children) {
|
|
250
|
+
const match = this.findModuleTree(child, moduleName);
|
|
251
|
+
if (match) {
|
|
252
|
+
return match;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return undefined;
|
|
695
256
|
}
|
|
696
257
|
getDirectRunMethods(instance) {
|
|
697
258
|
if (!instance ||
|
|
698
|
-
(typeof instance !==
|
|
259
|
+
(typeof instance !== "object" && typeof instance !== "function")) {
|
|
699
260
|
return [];
|
|
700
261
|
}
|
|
701
262
|
const prototype = Object.getPrototypeOf(instance);
|
|
@@ -703,11 +264,11 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
703
264
|
return [];
|
|
704
265
|
}
|
|
705
266
|
const methods = Object.getOwnPropertyNames(prototype)
|
|
706
|
-
.filter((name) => name !==
|
|
267
|
+
.filter((name) => name !== "constructor")
|
|
707
268
|
.map((name) => {
|
|
708
269
|
const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
|
|
709
270
|
const method = descriptor?.value;
|
|
710
|
-
if (typeof method !==
|
|
271
|
+
if (typeof method !== "function") {
|
|
711
272
|
return null;
|
|
712
273
|
}
|
|
713
274
|
const callable = method;
|
|
@@ -729,7 +290,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
729
290
|
}
|
|
730
291
|
getDirectRunMethodParameterTypes(param) {
|
|
731
292
|
const { instance, method, methodName } = param;
|
|
732
|
-
const className = typeof instance ===
|
|
293
|
+
const className = typeof instance === "function"
|
|
733
294
|
? instance.name
|
|
734
295
|
: instance.constructor?.name;
|
|
735
296
|
const sourceTypes = className
|
|
@@ -740,9 +301,9 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
740
301
|
}
|
|
741
302
|
const runtimeNames = this.extractMethodParameterNamesFromFunctionSource(methodName, method);
|
|
742
303
|
if (!runtimeNames?.length) {
|
|
743
|
-
return
|
|
304
|
+
return "[]";
|
|
744
305
|
}
|
|
745
|
-
return `[${runtimeNames.map((name) => `${name}: unknown`).join(
|
|
306
|
+
return `[${runtimeNames.map((name) => `${name}: unknown`).join(", ")}]`;
|
|
746
307
|
}
|
|
747
308
|
extractMethodParameterTypesFromProject(className, methodName) {
|
|
748
309
|
const targetClass = this.tsMorphProject
|
|
@@ -756,15 +317,15 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
756
317
|
const parameters = method.getParameters();
|
|
757
318
|
return `[${parameters
|
|
758
319
|
.map((parameter) => `${parameter.getName()}: ${this.typeToTypeScriptCode(parameter.getType(), parameter)}`)
|
|
759
|
-
.join(
|
|
320
|
+
.join(", ")}]`;
|
|
760
321
|
}
|
|
761
322
|
extractMethodParameterNamesFromFunctionSource(methodName, method) {
|
|
762
323
|
const methodSource = method.toString().trim();
|
|
763
324
|
const project = new ts_morph_1.Project({ useInMemoryFileSystem: true });
|
|
764
325
|
try {
|
|
765
|
-
const sourceFile = project.createSourceFile(
|
|
326
|
+
const sourceFile = project.createSourceFile("direct-run-method.ts", `class DirectRunMethodSource { ${methodSource} }`);
|
|
766
327
|
const names = sourceFile
|
|
767
|
-
.getClassOrThrow(
|
|
328
|
+
.getClassOrThrow("DirectRunMethodSource")
|
|
768
329
|
.getInstanceMethod(methodName)
|
|
769
330
|
?.getParameters()
|
|
770
331
|
.map((parameter) => parameter.getName());
|
|
@@ -775,9 +336,9 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
775
336
|
catch {
|
|
776
337
|
}
|
|
777
338
|
try {
|
|
778
|
-
const sourceFile = project.createSourceFile(
|
|
339
|
+
const sourceFile = project.createSourceFile("direct-run-function.ts", `const directRunMethod = ${methodSource};`);
|
|
779
340
|
const initializer = sourceFile
|
|
780
|
-
.getVariableDeclarationOrThrow(
|
|
341
|
+
.getVariableDeclarationOrThrow("directRunMethod")
|
|
781
342
|
.getInitializer();
|
|
782
343
|
const callable = initializer?.asKind(ts_morph_1.SyntaxKind.FunctionExpression) ??
|
|
783
344
|
initializer?.asKind(ts_morph_1.SyntaxKind.ArrowFunction);
|
|
@@ -787,79 +348,209 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
787
348
|
return undefined;
|
|
788
349
|
}
|
|
789
350
|
}
|
|
790
|
-
typeToTypeScriptCode(type, enclosingNode,
|
|
351
|
+
typeToTypeScriptCode(type, enclosingNode, state = {
|
|
352
|
+
activeTypes: new Set(),
|
|
353
|
+
depth: 0,
|
|
354
|
+
maxLength: MAX_TYPE_RENDER_LENGTH,
|
|
355
|
+
}) {
|
|
791
356
|
if (type.isAny() || type.isUnknown()) {
|
|
792
|
-
return
|
|
357
|
+
return "unknown";
|
|
793
358
|
}
|
|
794
359
|
if (type.isNever()) {
|
|
795
|
-
return
|
|
360
|
+
return "never";
|
|
796
361
|
}
|
|
797
362
|
if (type.isUndefined()) {
|
|
798
|
-
return
|
|
363
|
+
return "undefined";
|
|
799
364
|
}
|
|
800
365
|
if (type.isVoid()) {
|
|
801
|
-
return
|
|
366
|
+
return "void";
|
|
802
367
|
}
|
|
803
368
|
if (type.isString()) {
|
|
804
|
-
return
|
|
369
|
+
return "string";
|
|
805
370
|
}
|
|
806
371
|
if (type.isNumber()) {
|
|
807
|
-
return
|
|
372
|
+
return "number";
|
|
808
373
|
}
|
|
809
374
|
if (type.isBoolean()) {
|
|
810
|
-
return
|
|
375
|
+
return "boolean";
|
|
811
376
|
}
|
|
812
377
|
if (type.isNull()) {
|
|
813
|
-
return
|
|
378
|
+
return "null";
|
|
814
379
|
}
|
|
815
380
|
if (type.isStringLiteral()) {
|
|
816
|
-
|
|
381
|
+
const literalValue = type.getLiteralValue();
|
|
382
|
+
if (typeof literalValue !== "string" ||
|
|
383
|
+
literalValue.length > Math.floor(state.maxLength / 6)) {
|
|
384
|
+
return "string";
|
|
385
|
+
}
|
|
386
|
+
const literal = JSON.stringify(literalValue);
|
|
387
|
+
return literal.length <= state.maxLength ? literal : "string";
|
|
817
388
|
}
|
|
818
389
|
if (type.isNumberLiteral()) {
|
|
819
|
-
|
|
390
|
+
const literal = String(type.getLiteralValue());
|
|
391
|
+
return literal.length <= state.maxLength ? literal : "number";
|
|
820
392
|
}
|
|
821
393
|
if (type.isBooleanLiteral()) {
|
|
822
|
-
return
|
|
394
|
+
return this.typeTextWithinLimit(type, enclosingNode, state.maxLength);
|
|
823
395
|
}
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
.
|
|
396
|
+
const isArray = type.isArray() || type.isReadonlyArray();
|
|
397
|
+
const isComposite = type.isUnion() || type.isTuple() || isArray || type.isObject();
|
|
398
|
+
if (!isComposite) {
|
|
399
|
+
return (this.typeReferenceCode(type) ??
|
|
400
|
+
this.typeTextWithinLimit(type, enclosingNode, state.maxLength));
|
|
829
401
|
}
|
|
830
|
-
if (
|
|
831
|
-
return
|
|
832
|
-
.getTupleElements()
|
|
833
|
-
.map((tupleType) => this.typeToTypeScriptCode(tupleType, enclosingNode, new Set(seenTypeTexts)))
|
|
834
|
-
.join(', ')}]`;
|
|
402
|
+
if (state.activeTypes.has(type) || state.depth >= MAX_TYPE_RENDER_DEPTH) {
|
|
403
|
+
return this.typeReferenceCode(type) ?? "unknown";
|
|
835
404
|
}
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
.
|
|
405
|
+
state.activeTypes.add(type);
|
|
406
|
+
try {
|
|
407
|
+
if (type.isUnion()) {
|
|
408
|
+
const unionTypes = type.getUnionTypes();
|
|
409
|
+
if (unionTypes.length > MAX_TYPE_RENDER_MEMBERS) {
|
|
410
|
+
return "unknown";
|
|
411
|
+
}
|
|
412
|
+
return this.renderTypeSequence({
|
|
413
|
+
types: unionTypes,
|
|
414
|
+
enclosingNode,
|
|
415
|
+
state,
|
|
416
|
+
prefix: "",
|
|
417
|
+
delimiter: " | ",
|
|
418
|
+
suffix: "",
|
|
419
|
+
fallback: "unknown",
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
if (type.isTuple()) {
|
|
423
|
+
const tupleTypes = type.getTupleElements();
|
|
424
|
+
if (tupleTypes.length > MAX_TYPE_RENDER_MEMBERS) {
|
|
425
|
+
return this.typeFallback(state.maxLength, "unknown[]");
|
|
426
|
+
}
|
|
427
|
+
return this.renderTypeSequence({
|
|
428
|
+
types: tupleTypes,
|
|
429
|
+
enclosingNode,
|
|
430
|
+
state,
|
|
431
|
+
prefix: "[",
|
|
432
|
+
delimiter: ", ",
|
|
433
|
+
suffix: "]",
|
|
434
|
+
fallback: "unknown[]",
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
if (isArray) {
|
|
438
|
+
if (state.maxLength < MIN_TYPE_RENDER_LENGTH + 4) {
|
|
439
|
+
return this.typeFallback(state.maxLength, "unknown[]");
|
|
440
|
+
}
|
|
441
|
+
const itemType = this.typeToTypeScriptCode(type.getArrayElementType() ?? type, enclosingNode, {
|
|
442
|
+
...state,
|
|
443
|
+
depth: state.depth + 1,
|
|
444
|
+
maxLength: state.maxLength - 4,
|
|
445
|
+
});
|
|
446
|
+
return `${this.wrapArrayItemType(itemType)}[]`;
|
|
447
|
+
}
|
|
448
|
+
if (type.getCallSignatures().length > 0) {
|
|
449
|
+
return (this.typeReferenceCode(type) ??
|
|
450
|
+
this.typeTextWithinLimit(type, enclosingNode, state.maxLength));
|
|
451
|
+
}
|
|
452
|
+
const properties = type.getProperties();
|
|
453
|
+
if (properties.length === 0) {
|
|
454
|
+
return (this.typeReferenceCode(type) ??
|
|
455
|
+
this.typeTextWithinLimit(type, enclosingNode, state.maxLength));
|
|
456
|
+
}
|
|
457
|
+
return this.renderObjectType(properties, enclosingNode, state);
|
|
458
|
+
}
|
|
459
|
+
finally {
|
|
460
|
+
state.activeTypes.delete(type);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
renderTypeSequence(param) {
|
|
464
|
+
const { types, enclosingNode, state, prefix, delimiter, suffix, fallback } = param;
|
|
465
|
+
let output = prefix;
|
|
466
|
+
for (let index = 0; index < types.length; index += 1) {
|
|
467
|
+
const remainingTypes = types.length - index - 1;
|
|
468
|
+
const separator = index === 0 ? "" : delimiter;
|
|
469
|
+
const childMaxLength = state.maxLength -
|
|
470
|
+
output.length -
|
|
471
|
+
separator.length -
|
|
472
|
+
suffix.length -
|
|
473
|
+
remainingTypes * (delimiter.length + MIN_TYPE_RENDER_LENGTH);
|
|
474
|
+
if (childMaxLength < MIN_TYPE_RENDER_LENGTH) {
|
|
475
|
+
return this.typeFallback(state.maxLength, fallback);
|
|
476
|
+
}
|
|
477
|
+
output += `${separator}${this.typeToTypeScriptCode(types[index], enclosingNode, {
|
|
478
|
+
...state,
|
|
479
|
+
depth: state.depth + 1,
|
|
480
|
+
maxLength: childMaxLength,
|
|
481
|
+
})}`;
|
|
482
|
+
}
|
|
483
|
+
return `${output}${suffix}`;
|
|
484
|
+
}
|
|
485
|
+
renderObjectType(properties, enclosingNode, state) {
|
|
486
|
+
const maxProperties = Math.min(properties.length, MAX_TYPE_RENDER_MEMBERS);
|
|
487
|
+
const truncation = "[key: string]: unknown";
|
|
488
|
+
const propertyCodes = [];
|
|
489
|
+
let propertyCodesLength = 0;
|
|
490
|
+
let truncated = properties.length > maxProperties;
|
|
491
|
+
for (let index = 0; index < maxProperties; index += 1) {
|
|
492
|
+
const property = properties[index];
|
|
493
|
+
const propertyName = property.getName();
|
|
494
|
+
if (propertyName.length > MAX_TYPE_PROPERTY_NAME_LENGTH) {
|
|
495
|
+
truncated = true;
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
854
498
|
const propertyNode = property.getValueDeclaration() ?? property.getDeclarations()[0];
|
|
855
499
|
const propertyType = property.getTypeAtLocation(propertyNode ?? enclosingNode);
|
|
856
500
|
const optional = property.isOptional() || this.typeAllowsUndefined(propertyType);
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
.
|
|
501
|
+
const propertyPrefix = `${this.propertyNameToTypeScriptCode(propertyName)}${optional ? "?" : ""}: `;
|
|
502
|
+
const separator = propertyCodes.length === 0 ? "" : "; ";
|
|
503
|
+
const reservedTruncationLength = 2 + (propertyCodes.length === 0 ? 0 : 2) + truncation.length;
|
|
504
|
+
const childMaxLength = state.maxLength -
|
|
505
|
+
2 -
|
|
506
|
+
propertyCodesLength -
|
|
507
|
+
separator.length -
|
|
508
|
+
propertyPrefix.length -
|
|
509
|
+
reservedTruncationLength;
|
|
510
|
+
if (childMaxLength < MIN_TYPE_RENDER_LENGTH) {
|
|
511
|
+
truncated = true;
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
const propertyCode = `${propertyPrefix}${this.typeToTypeScriptCode(propertyType, propertyNode ?? enclosingNode, {
|
|
515
|
+
...state,
|
|
516
|
+
depth: state.depth + 1,
|
|
517
|
+
maxLength: childMaxLength,
|
|
518
|
+
})}`;
|
|
519
|
+
propertyCodes.push(propertyCode);
|
|
520
|
+
propertyCodesLength += separator.length + propertyCode.length;
|
|
521
|
+
}
|
|
522
|
+
if (truncated) {
|
|
523
|
+
const separator = propertyCodes.length === 0 ? "" : "; ";
|
|
524
|
+
const output = `{ ${propertyCodes.join("; ")}${separator}${truncation} }`;
|
|
525
|
+
return output.length <= state.maxLength
|
|
526
|
+
? output
|
|
527
|
+
: this.typeFallback(state.maxLength, "unknown");
|
|
528
|
+
}
|
|
529
|
+
const output = `{ ${propertyCodes.join("; ")} }`;
|
|
530
|
+
return output.length <= state.maxLength
|
|
531
|
+
? output
|
|
532
|
+
: this.typeFallback(state.maxLength, "unknown");
|
|
533
|
+
}
|
|
534
|
+
typeReferenceCode(type) {
|
|
535
|
+
const name = type.getAliasSymbol()?.getName() ?? type.getSymbol()?.getName();
|
|
536
|
+
return name && !name.startsWith("__") && /^[A-Za-z_$][\w$]*$/.test(name)
|
|
537
|
+
? name
|
|
538
|
+
: undefined;
|
|
539
|
+
}
|
|
540
|
+
typeTextWithinLimit(type, enclosingNode, maxLength) {
|
|
541
|
+
try {
|
|
542
|
+
const typeText = type.getText(enclosingNode);
|
|
543
|
+
return typeText.length <= maxLength ? typeText : "unknown";
|
|
544
|
+
}
|
|
545
|
+
catch {
|
|
546
|
+
return "unknown";
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
typeFallback(maxLength, preferred) {
|
|
550
|
+
return preferred.length <= maxLength ? preferred : "unknown";
|
|
860
551
|
}
|
|
861
552
|
wrapArrayItemType(typeText) {
|
|
862
|
-
return typeText.includes(
|
|
553
|
+
return typeText.includes(" | ") || typeText.includes("&")
|
|
863
554
|
? `(${typeText})`
|
|
864
555
|
: typeText;
|
|
865
556
|
}
|
|
@@ -897,7 +588,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
897
588
|
const key = this.dependencyNodeKey(moduleName, provider.name);
|
|
898
589
|
nodes.set(key, {
|
|
899
590
|
key,
|
|
900
|
-
kind:
|
|
591
|
+
kind: "provider",
|
|
901
592
|
moduleName,
|
|
902
593
|
name: provider.name,
|
|
903
594
|
});
|
|
@@ -906,7 +597,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
906
597
|
const key = this.dependencyNodeKey(moduleName, controller.name);
|
|
907
598
|
nodes.set(key, {
|
|
908
599
|
key,
|
|
909
|
-
kind:
|
|
600
|
+
kind: "controller",
|
|
910
601
|
moduleName,
|
|
911
602
|
name: controller.name,
|
|
912
603
|
});
|
|
@@ -924,9 +615,9 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
924
615
|
const cycles = this.findCycles(graph, nextCycleId);
|
|
925
616
|
return {
|
|
926
617
|
providers: cycles
|
|
927
|
-
.filter((cycle) => nodes.get(cycle.from)?.kind ===
|
|
618
|
+
.filter((cycle) => nodes.get(cycle.from)?.kind === "provider")
|
|
928
619
|
.map((cycle) => this.toProviderCycle(cycle, nodes)),
|
|
929
|
-
controllers: cycles.filter((cycle) => nodes.get(cycle.from)?.kind ===
|
|
620
|
+
controllers: cycles.filter((cycle) => nodes.get(cycle.from)?.kind === "controller"),
|
|
930
621
|
};
|
|
931
622
|
}
|
|
932
623
|
addDependencyEdges(graph, sourceKey, dependencies, nodes) {
|
|
@@ -982,14 +673,14 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
982
673
|
getCanonicalCycleKey(path) {
|
|
983
674
|
const cyclePath = path.slice(0, -1);
|
|
984
675
|
if (cyclePath.length <= 1) {
|
|
985
|
-
return cyclePath.join(
|
|
676
|
+
return cyclePath.join("->");
|
|
986
677
|
}
|
|
987
678
|
const rotations = cyclePath.map((_, index) => [
|
|
988
679
|
...cyclePath.slice(index),
|
|
989
680
|
...cyclePath.slice(0, index),
|
|
990
681
|
]);
|
|
991
682
|
return rotations
|
|
992
|
-
.map((rotation) => rotation.join(
|
|
683
|
+
.map((rotation) => rotation.join("->"))
|
|
993
684
|
.sort((a, b) => a.localeCompare(b))[0];
|
|
994
685
|
}
|
|
995
686
|
toProviderCycle(cycle, nodes) {
|
|
@@ -1006,10 +697,10 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
1006
697
|
provider: { name: node.name },
|
|
1007
698
|
};
|
|
1008
699
|
}
|
|
1009
|
-
const separatorIndex = key.indexOf(
|
|
700
|
+
const separatorIndex = key.indexOf(":");
|
|
1010
701
|
if (separatorIndex === -1) {
|
|
1011
702
|
return {
|
|
1012
|
-
module: { name:
|
|
703
|
+
module: { name: "" },
|
|
1013
704
|
provider: { name: key },
|
|
1014
705
|
};
|
|
1015
706
|
}
|
|
@@ -1020,9 +711,9 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
1020
711
|
}
|
|
1021
712
|
getCycleType(source, target, graph) {
|
|
1022
713
|
if (source === target || graph.get(target)?.has(source)) {
|
|
1023
|
-
return
|
|
714
|
+
return "direct";
|
|
1024
715
|
}
|
|
1025
|
-
return
|
|
716
|
+
return "indirect";
|
|
1026
717
|
}
|
|
1027
718
|
findPath(source, target, graph) {
|
|
1028
719
|
const visited = new Set();
|
|
@@ -1062,18 +753,18 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
1062
753
|
return `${moduleName}:${dependencyName}`;
|
|
1063
754
|
}
|
|
1064
755
|
enrichDependency(dependency, currentModule) {
|
|
1065
|
-
const colonIndex = dependency.indexOf(
|
|
756
|
+
const colonIndex = dependency.indexOf(":");
|
|
1066
757
|
if (colonIndex !== -1) {
|
|
1067
758
|
return {
|
|
1068
759
|
providedBy: {
|
|
1069
|
-
type:
|
|
760
|
+
type: "module",
|
|
1070
761
|
name: dependency.substring(0, colonIndex),
|
|
1071
762
|
},
|
|
1072
763
|
token: dependency.substring(colonIndex + 1),
|
|
1073
764
|
};
|
|
1074
765
|
}
|
|
1075
766
|
return {
|
|
1076
|
-
providedBy: { type:
|
|
767
|
+
providedBy: { type: "module", name: currentModule },
|
|
1077
768
|
token: dependency,
|
|
1078
769
|
};
|
|
1079
770
|
}
|
|
@@ -1083,6 +774,7 @@ exports.NestGraphInspectorSetup = NestGraphInspectorSetup = NestGraphInspectorSe
|
|
|
1083
774
|
(0, common_1.Injectable)(),
|
|
1084
775
|
__param(0, (0, common_1.Inject)(nest_graph_inspector_config_1.MODULE_OPTIONS_TOKEN)),
|
|
1085
776
|
__metadata("design:paramtypes", [Object, core_1.ModulesContainer,
|
|
777
|
+
discovery_1.DiscoveryAdapter,
|
|
1086
778
|
http_output_adapter_1.HttpOutputAdapter,
|
|
1087
779
|
file_output_adapter_1.FileOutputAdapter,
|
|
1088
780
|
json_output_adapter_1.JsonOutputAdapter,
|