@dimassetoid/septum-mcp 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -55
- package/package.json +7 -5
- package/src/cli/commands/check.ts +14 -10
- package/src/cli/commands/hook.ts +41 -6
- package/src/cli/commands/init.ts +10 -1
- package/src/cli/commands/stats.ts +33 -0
- package/src/cli/commands/sync-progress.ts +71 -0
- package/src/cli/commands/sync.ts +32 -4
- package/src/cli/index.ts +7 -0
- package/src/cli/templates/hooks.ts +3 -14
- package/src/cli/templates/skill.ts +17 -10
- package/src/core/boundary/audit.ts +383 -0
- package/src/core/boundary/evaluator.ts +251 -114
- package/src/core/boundary/staged-auditor.ts +147 -55
- package/src/core/config/domain-ownership.ts +6 -4
- package/src/core/config/domain-registration.ts +141 -0
- package/src/core/config/loader.ts +1 -25
- package/src/core/config/schema.ts +24 -36
- package/src/core/database/client.ts +2 -0
- package/src/core/database/repositories/dependency.repository.ts +334 -572
- package/src/core/database/repositories/domain.repository.ts +2 -1
- package/src/core/database/repositories/file.repository.ts +10 -0
- package/src/core/database/repositories/symbol.repository.ts +97 -398
- package/src/core/database/repository.ts +68 -9
- package/src/core/database/schema.ts +26 -0
- package/src/core/discovery/declarative-routes.ts +69 -0
- package/src/core/discovery/framework-detector.ts +149 -0
- package/src/core/discovery/navigation.ts +222 -58
- package/src/core/discovery/topology-detector.ts +93 -168
- package/src/core/errors.ts +19 -0
- package/src/core/ingestion/pipeline.ts +287 -88
- package/src/core/parser/extractors/base.ts +11 -3
- package/src/core/parser/extractors/csharp.ts +2 -2
- package/src/core/parser/extractors/django-routing.ts +163 -0
- package/src/core/parser/extractors/django-semantic.ts +397 -481
- package/src/core/parser/extractors/elysia-semantic.ts +172 -475
- package/src/core/parser/extractors/express-semantic.ts +195 -383
- package/src/core/parser/extractors/fastapi-semantic.ts +178 -361
- package/src/core/parser/extractors/fastify-semantic.ts +216 -717
- package/src/core/parser/extractors/flask-semantic.ts +89 -352
- package/src/core/parser/extractors/go-routing.ts +344 -0
- package/src/core/parser/extractors/go-semantic.ts +205 -562
- package/src/core/parser/extractors/go.ts +6 -3
- package/src/core/parser/extractors/graphql-entrypoint.ts +146 -0
- package/src/core/parser/extractors/hono-semantic.ts +132 -509
- package/src/core/parser/extractors/java.ts +2 -2
- package/src/core/parser/extractors/laravel-routing.ts +263 -0
- package/src/core/parser/extractors/laravel-semantic.ts +143 -335
- package/src/core/parser/extractors/manifest-helpers.ts +64 -0
- package/src/core/parser/extractors/mcp-cli-dispatch.ts +268 -0
- package/src/core/parser/extractors/mcp-cli-semantic.ts +82 -333
- package/src/core/parser/extractors/nestjs-semantic.ts +238 -494
- package/src/core/parser/extractors/nextjs-semantic.ts +164 -0
- package/src/core/parser/extractors/php-ast-helpers.ts +51 -0
- package/src/core/parser/extractors/php.ts +1 -1
- package/src/core/parser/extractors/python-route-ast.ts +249 -0
- package/src/core/parser/extractors/python.ts +5 -9
- package/src/core/parser/extractors/registry.ts +2 -0
- package/src/core/parser/extractors/route-prefixes.ts +43 -0
- package/src/core/parser/extractors/ruby.ts +2 -2
- package/src/core/parser/extractors/rust.ts +4 -4
- package/src/core/parser/extractors/symfony-routing.ts +114 -0
- package/src/core/parser/extractors/symfony-semantic.ts +72 -226
- package/src/core/parser/extractors/typescript-bindings.ts +333 -0
- package/src/core/parser/extractors/typescript-compiler-fallback.ts +380 -0
- package/src/core/parser/extractors/typescript-fallback.ts +207 -0
- package/src/core/parser/extractors/typescript-route-context.ts +58 -0
- package/src/core/parser/extractors/typescript.ts +23 -216
- package/src/core/parser/language-capabilities.ts +1 -1
- package/src/core/parser/schema/providers/laravel-migration-provider.ts +4 -1
- package/src/core/parser/wasm-loader.ts +6 -2
- package/src/core/resolver/ast-method-tracer.ts +230 -0
- package/src/core/resolver/call-graph-helpers.ts +72 -0
- package/src/core/resolver/call-graph-tracer.ts +169 -414
- package/src/core/resolver/dependency-analysis.ts +593 -0
- package/src/core/resolver/import-candidates.ts +120 -0
- package/src/core/resolver/import-evidence.ts +88 -0
- package/src/core/resolver/import-resolver.ts +60 -7
- package/src/core/resolver/interpretation-inputs.ts +60 -2
- package/src/core/resolver/languages/python.ts +31 -13
- package/src/core/resolver/languages/shared.ts +4 -1
- package/src/core/resolver/module-resolver.ts +10 -3
- package/src/core/resolver/path-resolver.ts +6 -3
- package/src/core/resolver/polyglot-slice-tracer.ts +197 -0
- package/src/core/resolver/slice-catalog.ts +21 -6
- package/src/core/resolver/slice-format.ts +13 -0
- package/src/core/resolver/slice-selection.ts +237 -0
- package/src/core/resolver/symbol-impact.ts +104 -8
- package/src/core/resolver/symbol-locator.ts +82 -15
- package/src/core/resolver/vertical-slice-tracer.ts +31 -230
- package/src/core/resources/ignore.ts +0 -1
- package/src/core/resources/limits.ts +2 -1
- package/src/core/resources/work-queue.ts +167 -4
- package/src/core/retrieval/context-page.ts +11 -4
- package/src/core/retrieval/context.ts +363 -226
- package/src/{mcp/tools/get-symbol.ts → core/retrieval/source-context.ts} +145 -96
- package/src/core/retrieval/symbol-details.ts +453 -0
- package/src/core/retrieval/symbol-evidence.ts +3 -3
- package/src/core/retrieval/typescript-references.ts +38 -7
- package/src/core/retrieval/universal-context.ts +182 -0
- package/src/core/telemetry/summary.ts +114 -0
- package/src/core/telemetry/telemetry.ts +192 -49
- package/src/mcp/formatters/compact-text.ts +439 -0
- package/src/mcp/resources-and-prompts.ts +253 -0
- package/src/mcp/schemas/boundary.ts +145 -0
- package/src/mcp/schemas/map.ts +250 -0
- package/src/mcp/schemas/shared.ts +52 -0
- package/src/mcp/schemas/slice.ts +60 -0
- package/src/mcp/schemas/symbol.ts +268 -0
- package/src/mcp/schemas.ts +5 -746
- package/src/mcp/server.ts +269 -429
- package/src/mcp/tool-catalog.ts +331 -0
- package/src/mcp/tools/boundary.ts +68 -3
- package/src/mcp/tools/map.ts +104 -4
- package/src/mcp/tools/slice.ts +101 -3
- package/src/mcp/tools/symbol.ts +469 -17
- package/src/mcp/ui/app-client.ts +899 -0
- package/src/mcp/ui/app.html +478 -0
- package/src/mcp/workflow-playbooks.ts +181 -0
- package/src/mcp/workspace-boundary.ts +50 -0
- package/src/mcp/workspace-manager.ts +88 -15
- package/src/mcp/workspace-watcher.ts +30 -11
- package/src/types/index.ts +12 -0
- package/src/mcp/tools/check-boundary.ts +0 -382
- package/src/mcp/tools/get-domain-catalog.ts +0 -39
- package/src/mcp/tools/get-symbol-impact.ts +0 -104
- package/src/mcp/tools/locate-symbol.ts +0 -236
- package/src/mcp/tools/register-domain.ts +0 -113
- package/src/mcp/tools/trace-vertical-slice.ts +0 -106
|
@@ -1,71 +1,30 @@
|
|
|
1
|
-
import { listSourceFiles, type SourceInventory } from "../../resources/source-inventory.ts";
|
|
2
|
-
import { readOptionalSourceFile } from "../../resources/source-files.ts";
|
|
3
|
-
import { parseWithTreeSitter } from "../wasm-loader.ts";
|
|
4
|
-
import { ImportResolver } from "../../resolver/import-resolver.ts";
|
|
5
|
-
import type Parser from "web-tree-sitter";
|
|
6
|
-
import * as fs from "node:fs";
|
|
7
1
|
import * as path from "node:path";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import type { ExecutionChainNode } from "../../../types/index.ts";
|
|
4
|
+
import { listSourceFiles, type SourceInventory } from "../../resources/source-inventory.ts";
|
|
5
|
+
import { probeSourcePath, readOptionalSourceFile } from "../../resources/source-files.ts";
|
|
6
|
+
import { composeMountPrefixes } from "./route-prefixes.ts";
|
|
8
7
|
import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
httpMethods: string[];
|
|
26
|
-
rawPath: string;
|
|
27
|
-
hooks: Array<{ symbol: string; line: number }>;
|
|
28
|
-
handler: { symbol: string; line: number };
|
|
29
|
-
bodyDto?: string;
|
|
8
|
+
import { lineOf, literal, nameText, objectMember, TypeScriptBindings, visitRouteCalls, type Handler } from "./typescript-bindings.ts";
|
|
9
|
+
|
|
10
|
+
const verbs = new Set(["get", "post", "put", "patch", "delete", "head", "options", "trace", "all"]);
|
|
11
|
+
const instanceMethods = new Set([...verbs, "route", "register", "addSchema", "addHook", "withTypeProvider", "decorate"]);
|
|
12
|
+
const hookNames = ["onRequest", "preParsing", "preValidation", "preHandler", "preSerialization", "onSend", "onResponse"];
|
|
13
|
+
type Scope = ts.CallExpression | ts.FunctionLikeDeclaration;
|
|
14
|
+
type Mount = { parent: Scope; child: ts.FunctionLikeDeclaration; prefix: string | undefined };
|
|
15
|
+
type Schema = { id: string; call: ts.CallExpression };
|
|
16
|
+
type Route = {
|
|
17
|
+
scope: Scope;
|
|
18
|
+
call: ts.CallExpression;
|
|
19
|
+
uri: string;
|
|
20
|
+
methods: string[];
|
|
21
|
+
handler?: Handler;
|
|
22
|
+
target?: ts.Expression | ts.MethodDeclaration;
|
|
23
|
+
hooks: ts.Expression[];
|
|
30
24
|
schemaRef?: string;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
interface RawFastifyRegister {
|
|
35
|
-
file: string;
|
|
36
|
-
relPath: string;
|
|
37
|
-
parentScopeId: string;
|
|
38
|
-
targetPluginSymbol?: string;
|
|
39
|
-
targetScopeId?: string;
|
|
40
|
-
prefix: string;
|
|
41
|
-
line: number;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
interface KnownSchema {
|
|
45
|
-
id: string;
|
|
46
|
-
file: string;
|
|
47
|
-
line: number;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function cleanLiteral(node: Parser.SyntaxNode | null | undefined): string | undefined {
|
|
51
|
-
if (!node) return undefined;
|
|
52
|
-
if (node.type === "string" || node.type === "template_string") {
|
|
53
|
-
const raw = node.text.trim();
|
|
54
|
-
if (raw.includes("${")) return undefined;
|
|
55
|
-
return raw.replace(/^['"`]|['"`]$/g, "");
|
|
56
|
-
}
|
|
57
|
-
return undefined;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function normalizeRouteUri(prefix: string, rawPath: string): string {
|
|
61
|
-
const p = (prefix ?? "").trim().replace(/\/+$/, "");
|
|
62
|
-
let r = (rawPath ?? "").trim();
|
|
63
|
-
if (!r.startsWith("/")) r = "/" + r;
|
|
64
|
-
if (r === "/" && p.length > 0) return p;
|
|
65
|
-
if (r === "/" && p.length === 0) return "/";
|
|
66
|
-
const joined = `${p}${r}`;
|
|
67
|
-
return joined.startsWith("/") ? joined : `/${joined}`;
|
|
68
|
-
}
|
|
25
|
+
bodyType?: string;
|
|
26
|
+
rootSlash?: string;
|
|
27
|
+
};
|
|
69
28
|
|
|
70
29
|
export class FastifySemanticExtractor implements SemanticSliceExtractor {
|
|
71
30
|
public readonly id = "fastify";
|
|
@@ -73,687 +32,227 @@ export class FastifySemanticExtractor implements SemanticSliceExtractor {
|
|
|
73
32
|
|
|
74
33
|
public canHandle(projectRoot: string, framework?: string): boolean {
|
|
75
34
|
if (framework === "fastify") return true;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (deps["fastify"]) return true;
|
|
83
|
-
} catch {}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return false;
|
|
35
|
+
const manifestPath = path.join(projectRoot, "package.json");
|
|
36
|
+
if (probeSourcePath(manifestPath) !== "file") return false;
|
|
37
|
+
try {
|
|
38
|
+
const manifest = JSON.parse(readOptionalSourceFile(manifestPath, "utf-8"));
|
|
39
|
+
return !!(manifest.dependencies?.fastify || manifest.devDependencies?.fastify);
|
|
40
|
+
} catch { return false; }
|
|
87
41
|
}
|
|
88
42
|
|
|
89
43
|
public async extractSlices(
|
|
90
44
|
projectRoot: string,
|
|
91
45
|
domainId?: number,
|
|
92
|
-
signal?: AbortSignal,
|
|
46
|
+
signal?: AbortSignal,
|
|
47
|
+
inventory?: SourceInventory,
|
|
93
48
|
): Promise<VerticalSliceCandidate[]> {
|
|
94
|
-
const
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
if (
|
|
109
|
-
!content ||
|
|
110
|
-
(!content.includes("fastify") &&
|
|
111
|
-
!content.includes("Fastify") &&
|
|
112
|
-
!content.includes(".register") &&
|
|
113
|
-
!content.includes(".route") &&
|
|
114
|
-
!content.includes(".get") &&
|
|
115
|
-
!content.includes(".post") &&
|
|
116
|
-
!content.includes(".addSchema"))
|
|
117
|
-
) {
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const fileData = await parseWithTreeSitter(
|
|
122
|
-
"typescript",
|
|
123
|
-
content,
|
|
124
|
-
(rootNode) => this.extractFileAst(file, relPath, rootNode, resolver)
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
if (fileData) {
|
|
128
|
-
allRoutes.push(...fileData.routes);
|
|
129
|
-
allRegisters.push(...fileData.registers);
|
|
130
|
-
for (const [id, s] of fileData.schemas.entries()) {
|
|
131
|
-
knownSchemas.set(id, s);
|
|
132
|
-
// also normalize by stripping trailing '#'
|
|
133
|
-
if (id.endsWith("#")) {
|
|
134
|
-
knownSchemas.set(id.slice(0, -1), s);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
if (fileData.resolvedImports.size > 0) {
|
|
138
|
-
fileImports.set(relPath, fileData.resolvedImports);
|
|
139
|
-
}
|
|
140
|
-
if (fileData.definedScopes.size > 0) {
|
|
141
|
-
fileScopes.set(relPath, fileData.definedScopes);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Resolve Scope Prefix DAG
|
|
147
|
-
const scopePrefixes = this.resolveScopePrefixes(allRegisters, fileImports, fileScopes);
|
|
148
|
-
|
|
149
|
-
// Build Vertical Slices
|
|
150
|
-
for (const route of allRoutes) {
|
|
151
|
-
const prefixes = scopePrefixes.get(route.scopeId) || [""];
|
|
152
|
-
|
|
153
|
-
for (const prefix of prefixes) {
|
|
154
|
-
for (const httpMethod of route.httpMethods) {
|
|
155
|
-
const fullUri = normalizeRouteUri(prefix, route.rawPath);
|
|
156
|
-
const actionSymbol = route.handler.symbol;
|
|
157
|
-
const rootIdent = actionSymbol.includes(".") ? actionSymbol.split(".")[0] : actionSymbol;
|
|
158
|
-
const resolvedControllerFile = fileImports.get(route.relPath)?.get(rootIdent) || route.relPath;
|
|
159
|
-
const fileBase = path.basename(route.relPath).replace(/\.[^.]+$/, "");
|
|
160
|
-
const controllerClass = actionSymbol.includes(".")
|
|
161
|
-
? actionSymbol.split(".")[0]
|
|
162
|
-
: fileBase.charAt(0).toUpperCase() + fileBase.slice(1);
|
|
163
|
-
const actionName = actionSymbol.includes(".") ? actionSymbol.split(".").pop()! : actionSymbol;
|
|
164
|
-
|
|
165
|
-
const chain: Array<{ stage: string; symbol: string; file?: string; line?: number; description?: string }> = [
|
|
166
|
-
{
|
|
167
|
-
stage: "ingress",
|
|
168
|
-
symbol: `${httpMethod} ${fullUri}`,
|
|
169
|
-
file: route.relPath,
|
|
170
|
-
line: route.routeLine,
|
|
171
|
-
description: `Fastify Route: ${httpMethod} ${fullUri}`,
|
|
172
|
-
},
|
|
173
|
-
];
|
|
174
|
-
|
|
175
|
-
// Schema / DTO stage
|
|
176
|
-
if (route.bodyDto) {
|
|
177
|
-
chain.push({
|
|
178
|
-
stage: "validation",
|
|
179
|
-
symbol: route.bodyDto,
|
|
180
|
-
file: route.relPath,
|
|
181
|
-
line: route.routeLine,
|
|
182
|
-
description: `Fastify Body DTO: ${route.bodyDto}`,
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (route.schemaRef) {
|
|
187
|
-
const rawRef = route.schemaRef;
|
|
188
|
-
const strippedRef = rawRef.endsWith("#") ? rawRef.slice(0, -1) : rawRef;
|
|
189
|
-
const known = knownSchemas.get(rawRef) || knownSchemas.get(strippedRef);
|
|
190
|
-
|
|
191
|
-
if (known) {
|
|
192
|
-
chain.push({
|
|
193
|
-
stage: "validation",
|
|
194
|
-
symbol: known.id,
|
|
195
|
-
file: known.file,
|
|
196
|
-
line: known.line,
|
|
197
|
-
description: `Resolved Fastify Schema: ${known.id}`,
|
|
198
|
-
});
|
|
199
|
-
} else {
|
|
200
|
-
// Unresolved $ref: do not hallucinate models
|
|
201
|
-
chain.push({
|
|
202
|
-
stage: "validation",
|
|
203
|
-
symbol: "unresolved:$ref",
|
|
204
|
-
file: route.relPath,
|
|
205
|
-
line: route.routeLine,
|
|
206
|
-
description: `Unresolved schema reference: ${rawRef}`,
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// Hooks / Middleware
|
|
212
|
-
for (const hook of route.hooks) {
|
|
213
|
-
chain.push({
|
|
214
|
-
stage: "middleware",
|
|
215
|
-
symbol: hook.symbol,
|
|
216
|
-
file: route.relPath,
|
|
217
|
-
line: hook.line,
|
|
218
|
-
description: `Fastify Hook: ${hook.symbol}`,
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Controller Action
|
|
223
|
-
chain.push({
|
|
224
|
-
stage: "controller",
|
|
225
|
-
symbol: actionSymbol,
|
|
226
|
-
file: resolvedControllerFile,
|
|
227
|
-
line: route.handler.line,
|
|
228
|
-
description: `Fastify Action Handler`,
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
slices.push({
|
|
232
|
-
domain_id: domainId ?? null,
|
|
233
|
-
feature_key: `fastify.${controllerClass.toLowerCase()}.${actionName}`,
|
|
234
|
-
http_method: httpMethod,
|
|
235
|
-
route_uri: fullUri,
|
|
236
|
-
route_name: `${controllerClass}.${actionName}`,
|
|
237
|
-
controller_class: controllerClass,
|
|
238
|
-
action_name: actionName,
|
|
239
|
-
controller_file: resolvedControllerFile,
|
|
240
|
-
controller_line: route.handler.line,
|
|
241
|
-
architecture_style: "mvc",
|
|
242
|
-
entry_kind: "http_route",
|
|
243
|
-
execution_chain_json: JSON.stringify(chain),
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
return slices;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
private extractFileAst(
|
|
253
|
-
_filePath: string,
|
|
254
|
-
relPath: string,
|
|
255
|
-
rootNode: Parser.SyntaxNode,
|
|
256
|
-
resolver: ImportResolver
|
|
257
|
-
): {
|
|
258
|
-
routes: RawFastifyRoute[];
|
|
259
|
-
registers: RawFastifyRegister[];
|
|
260
|
-
schemas: Map<string, KnownSchema>;
|
|
261
|
-
resolvedImports: Map<string, string>;
|
|
262
|
-
definedScopes: Set<string>;
|
|
263
|
-
} {
|
|
264
|
-
const routes: RawFastifyRoute[] = [];
|
|
265
|
-
const registers: RawFastifyRegister[] = [];
|
|
266
|
-
const schemas = new Map<string, KnownSchema>();
|
|
267
|
-
const resolvedImports = new Map<string, string>();
|
|
268
|
-
const definedScopes = new Set<string>();
|
|
269
|
-
|
|
270
|
-
definedScopes.add(`${relPath}#root`);
|
|
271
|
-
|
|
272
|
-
// 1. Collect static imports
|
|
273
|
-
for (const child of rootNode.namedChildren) {
|
|
274
|
-
if (child.type === "import_statement") {
|
|
275
|
-
const sourceNode =
|
|
276
|
-
child.childForFieldName("source") ??
|
|
277
|
-
child.namedChildren.find((c) => c.type === "string");
|
|
278
|
-
const specifier = cleanLiteral(sourceNode);
|
|
279
|
-
if (!specifier) continue;
|
|
280
|
-
|
|
281
|
-
const res = resolver.resolve(specifier, relPath);
|
|
282
|
-
const targetFile = res.status === "resolved" && res.targets[0]?.files[0] ? res.targets[0].files[0] : undefined;
|
|
283
|
-
if (!targetFile) continue;
|
|
284
|
-
|
|
285
|
-
const importClause = child.namedChildren.find((c) => c.type === "import_clause");
|
|
286
|
-
if (importClause) {
|
|
287
|
-
for (const ic of importClause.namedChildren) {
|
|
288
|
-
if (ic.type === "identifier") {
|
|
289
|
-
resolvedImports.set(ic.text.trim(), targetFile);
|
|
290
|
-
} else if (ic.type === "namespace_import") {
|
|
291
|
-
const identNode = ic.namedChildren.find((n) => n.type === "identifier");
|
|
292
|
-
if (identNode) resolvedImports.set(identNode.text.trim(), targetFile);
|
|
293
|
-
} else if (ic.type === "named_imports") {
|
|
294
|
-
for (const spec of ic.namedChildren) {
|
|
295
|
-
if (spec.type === "import_specifier") {
|
|
296
|
-
const alias = spec.childForFieldName("alias");
|
|
297
|
-
const name = spec.childForFieldName("name") ?? spec.namedChildren[0];
|
|
298
|
-
const ident = alias ? alias.text.trim() : name ? name.text.trim() : "";
|
|
299
|
-
if (ident) resolvedImports.set(ident, targetFile);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
// 2. Discover named functions / scopes in file
|
|
309
|
-
const registerScope = (node: Parser.SyntaxNode): void => {
|
|
310
|
-
if (node.type === "function_declaration") {
|
|
311
|
-
const nameNode = node.childForFieldName("name");
|
|
312
|
-
if (nameNode) {
|
|
313
|
-
definedScopes.add(`${relPath}#${nameNode.text.trim()}`);
|
|
314
|
-
}
|
|
315
|
-
} else if (node.type === "variable_declarator") {
|
|
316
|
-
const nameNode = node.childForFieldName("name");
|
|
317
|
-
const initNode = node.childForFieldName("value");
|
|
318
|
-
if (nameNode && initNode && (initNode.type === "arrow_function" || initNode.type === "function_expression" || initNode.type === "call_expression")) {
|
|
319
|
-
definedScopes.add(`${relPath}#${nameNode.text.trim()}`);
|
|
320
|
-
}
|
|
321
|
-
} else if (node.type === "export_statement") {
|
|
322
|
-
for (const child of node.namedChildren) {
|
|
323
|
-
if (child.type === "function_declaration" || child.type === "arrow_function" || child.type === "function_expression" || child.type === "function") {
|
|
324
|
-
definedScopes.add(`${relPath}#default`);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
for (const child of node.namedChildren) {
|
|
329
|
-
registerScope(child);
|
|
330
|
-
}
|
|
49
|
+
const root = path.resolve(projectRoot);
|
|
50
|
+
const files = listSourceFiles(root, /\.[cm]?[jt]sx?$/, inventory).filter(file => !/\.d\.[cm]?ts$/.test(file));
|
|
51
|
+
const bindings = new TypeScriptBindings(root, new Set(files), signal);
|
|
52
|
+
const roots = new Map<Scope, string>();
|
|
53
|
+
const mounts: Mount[] = [];
|
|
54
|
+
const parents = new Map<Scope, Set<Scope>>();
|
|
55
|
+
const plugins: ts.FunctionLikeDeclaration[] = [];
|
|
56
|
+
const seenPlugins = new Set<Scope>();
|
|
57
|
+
const schemas = new Map<Scope, Map<string, Schema | null>>();
|
|
58
|
+
const routes: Route[] = [];
|
|
59
|
+
const relative = (node: ts.Node) => path.relative(root, node.getSourceFile().fileName).replace(/\\/g, "/");
|
|
60
|
+
const optionsOf = (expression?: ts.Expression) => {
|
|
61
|
+
const node = bindings.value(expression);
|
|
62
|
+
return node && ts.isObjectLiteralExpression(node) ? node : undefined;
|
|
331
63
|
};
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
const walk = (node: Parser.SyntaxNode): void => {
|
|
336
|
-
if (node.type === "call_expression") {
|
|
337
|
-
const fnNode = node.childForFieldName("function");
|
|
338
|
-
if (fnNode && fnNode.type === "member_expression") {
|
|
339
|
-
const propNode = fnNode.childForFieldName("property");
|
|
340
|
-
const argsNode = node.childForFieldName("arguments");
|
|
341
|
-
|
|
342
|
-
if (propNode && argsNode) {
|
|
343
|
-
const methodName = propNode.text.trim();
|
|
344
|
-
const args = argsNode.namedChildren;
|
|
345
|
-
const currentScopeId = this.findEnclosingScope(node, relPath);
|
|
346
|
-
|
|
347
|
-
// A. app.addSchema({ $id: "userSchema", ... })
|
|
348
|
-
if (methodName === "addSchema" && args.length >= 1) {
|
|
349
|
-
const schemaId = this.extractSchemaId(args[0]);
|
|
350
|
-
if (schemaId) {
|
|
351
|
-
schemas.set(schemaId, {
|
|
352
|
-
id: schemaId,
|
|
353
|
-
file: relPath,
|
|
354
|
-
line: node.startPosition.row + 1,
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
// B. app.register(plugin, { prefix: "/api" })
|
|
360
|
-
else if (methodName === "register" && args.length >= 1) {
|
|
361
|
-
const pluginArg = this.unwrapPluginNode(args[0]);
|
|
362
|
-
let prefix = "";
|
|
363
|
-
if (args.length >= 2 && args[1].type === "object") {
|
|
364
|
-
prefix = this.extractObjectPropertyString(args[1], "prefix") ?? "";
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
if (pluginArg) {
|
|
368
|
-
if (pluginArg.type === "identifier") {
|
|
369
|
-
registers.push({
|
|
370
|
-
file: relPath,
|
|
371
|
-
relPath,
|
|
372
|
-
parentScopeId: currentScopeId,
|
|
373
|
-
targetPluginSymbol: pluginArg.text.trim(),
|
|
374
|
-
prefix,
|
|
375
|
-
line: node.startPosition.row + 1,
|
|
376
|
-
});
|
|
377
|
-
} else if (pluginArg.type === "arrow_function" || pluginArg.type === "function_expression") {
|
|
378
|
-
const inlineScopeId = `${relPath}#inline_${pluginArg.startPosition.row + 1}_${pluginArg.startPosition.column}`;
|
|
379
|
-
definedScopes.add(inlineScopeId);
|
|
380
|
-
registers.push({
|
|
381
|
-
file: relPath,
|
|
382
|
-
relPath,
|
|
383
|
-
parentScopeId: currentScopeId,
|
|
384
|
-
targetScopeId: inlineScopeId,
|
|
385
|
-
prefix,
|
|
386
|
-
line: node.startPosition.row + 1,
|
|
387
|
-
});
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
// C. fastify.route({ method: 'GET', url: '/path', handler: ... })
|
|
393
|
-
else if (methodName === "route" && args.length >= 1 && args[0].type === "object") {
|
|
394
|
-
const routeObj = args[0];
|
|
395
|
-
const rawMethods = this.extractRouteMethods(routeObj);
|
|
396
|
-
const rawPath = this.extractObjectPropertyString(routeObj, "url") ?? this.extractObjectPropertyString(routeObj, "path");
|
|
397
|
-
|
|
398
|
-
if (rawPath !== undefined && rawMethods.length > 0) {
|
|
399
|
-
const handler = this.extractHandlerFromRoute(routeObj, args[1]);
|
|
400
|
-
const hooks = this.extractHooksFromOptions(routeObj);
|
|
401
|
-
const { schemaRef, bodyDto } = this.extractSchemaFromRoute(node, routeObj);
|
|
402
|
-
|
|
403
|
-
routes.push({
|
|
404
|
-
file: relPath,
|
|
405
|
-
relPath,
|
|
406
|
-
scopeId: currentScopeId,
|
|
407
|
-
httpMethods: rawMethods,
|
|
408
|
-
rawPath,
|
|
409
|
-
hooks,
|
|
410
|
-
handler,
|
|
411
|
-
bodyDto,
|
|
412
|
-
schemaRef,
|
|
413
|
-
routeLine: node.startPosition.row + 1,
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// D. Shorthand methods: fastify.get('/path', [opts], handler)
|
|
419
|
-
else if (FASTIFY_VERBS.has(methodName.toLowerCase())) {
|
|
420
|
-
const httpMethod = methodName.toUpperCase();
|
|
421
|
-
if (args.length >= 2) {
|
|
422
|
-
const rawPath = cleanLiteral(args[0]);
|
|
423
|
-
if (rawPath !== undefined) {
|
|
424
|
-
let handlerNode = args[args.length - 1];
|
|
425
|
-
let optsNode: Parser.SyntaxNode | undefined;
|
|
426
|
-
const hooks: Array<{ symbol: string; line: number }> = [];
|
|
427
|
-
|
|
428
|
-
if (args.length === 2) {
|
|
429
|
-
if (args[1].type === "object") {
|
|
430
|
-
optsNode = args[1];
|
|
431
|
-
const innerHandler = this.findObjectPropertyNode(args[1], "handler");
|
|
432
|
-
if (innerHandler) handlerNode = innerHandler;
|
|
433
|
-
}
|
|
434
|
-
} else {
|
|
435
|
-
optsNode = args[1];
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
if (optsNode) {
|
|
439
|
-
hooks.push(...this.extractHooksFromOptions(optsNode));
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
const handler = this.extractSingleHandler(handlerNode);
|
|
443
|
-
const { schemaRef, bodyDto } = this.extractSchemaFromRoute(node, optsNode);
|
|
444
|
-
|
|
445
|
-
routes.push({
|
|
446
|
-
file: relPath,
|
|
447
|
-
relPath,
|
|
448
|
-
scopeId: currentScopeId,
|
|
449
|
-
httpMethods: [httpMethod],
|
|
450
|
-
rawPath,
|
|
451
|
-
hooks,
|
|
452
|
-
handler,
|
|
453
|
-
bodyDto,
|
|
454
|
-
schemaRef,
|
|
455
|
-
routeLine: node.startPosition.row + 1,
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
for (const child of node.namedChildren) {
|
|
465
|
-
walk(child);
|
|
466
|
-
}
|
|
64
|
+
const option = (object: ts.ObjectLiteralExpression | undefined, key: string) => {
|
|
65
|
+
const value = object && objectMember(object, key);
|
|
66
|
+
return value && !ts.isMethodDeclaration(value) ? value : undefined;
|
|
467
67
|
};
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
const
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
if (
|
|
481
|
-
return
|
|
68
|
+
const hasUnknownFields = (opts: ts.ObjectLiteralExpression) => opts.properties.some(property =>
|
|
69
|
+
ts.isSpreadAssignment(property) || property.name && ts.isComputedPropertyName(property.name),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const resolveScope = (
|
|
73
|
+
expression: ts.Expression,
|
|
74
|
+
plugin?: ts.FunctionLikeDeclaration,
|
|
75
|
+
): Scope | undefined => {
|
|
76
|
+
let value = bindings.value(expression);
|
|
77
|
+
const inspected = new Set<ts.Node>();
|
|
78
|
+
while (value && !inspected.has(value)) {
|
|
79
|
+
inspected.add(value);
|
|
80
|
+
if (ts.isIdentifier(value) && plugin?.parameters[0] && bindings.declaration(value) === plugin.parameters[0]) {
|
|
81
|
+
return plugin;
|
|
482
82
|
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
private findEnclosingScope(node: Parser.SyntaxNode, relPath: string): string {
|
|
489
|
-
let curr: Parser.SyntaxNode | null = node.parent;
|
|
490
|
-
while (curr) {
|
|
491
|
-
if (curr.type === "function_declaration") {
|
|
492
|
-
if (curr.parent?.type === "export_statement") return `${relPath}#default`;
|
|
493
|
-
const name = curr.childForFieldName("name");
|
|
494
|
-
if (name) return `${relPath}#${name.text.trim()}`;
|
|
495
|
-
} else if (curr.type === "variable_declarator") {
|
|
496
|
-
const name = curr.childForFieldName("name");
|
|
497
|
-
if (name) return `${relPath}#${name.text.trim()}`;
|
|
498
|
-
} else if (curr.type === "arrow_function" || curr.type === "function_expression" || curr.type === "function") {
|
|
499
|
-
if (curr.parent?.type === "export_statement") return `${relPath}#default`;
|
|
500
|
-
if (curr.parent?.type === "arguments") {
|
|
501
|
-
const call = curr.parent.parent;
|
|
502
|
-
if (call && call.type === "call_expression") {
|
|
503
|
-
const callFn = call.childForFieldName("function");
|
|
504
|
-
if (callFn?.type === "member_expression" && callFn.childForFieldName("property")?.text.trim() === "register") {
|
|
505
|
-
return `${relPath}#inline_${curr.startPosition.row + 1}_${curr.startPosition.column}`;
|
|
506
|
-
}
|
|
507
|
-
}
|
|
83
|
+
if (!ts.isCallExpression(value) || value.questionDotToken) return undefined;
|
|
84
|
+
const imported = bindings.packageBinding(value.expression);
|
|
85
|
+
if (imported?.module === "fastify" && ["default", "fastify", "Fastify", "*"].includes(imported.name)) {
|
|
86
|
+
roots.set(value, "");
|
|
87
|
+
return value;
|
|
508
88
|
}
|
|
89
|
+
if (!ts.isPropertyAccessExpression(value.expression) || value.expression.questionDotToken ||
|
|
90
|
+
!instanceMethods.has(value.expression.name.text)) return undefined;
|
|
91
|
+
value = bindings.value(value.expression.expression);
|
|
509
92
|
}
|
|
510
|
-
|
|
511
|
-
}
|
|
512
|
-
return `${relPath}#root`;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
private extractSchemaId(node: Parser.SyntaxNode): string | undefined {
|
|
516
|
-
if (node.type === "object") {
|
|
517
|
-
return this.extractObjectPropertyString(node, "$id");
|
|
518
|
-
}
|
|
519
|
-
return undefined;
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
private extractRouteMethods(routeObj: Parser.SyntaxNode): string[] {
|
|
523
|
-
const prop = this.findObjectPropertyNode(routeObj, "method");
|
|
524
|
-
if (!prop) return ["GET"];
|
|
525
|
-
|
|
526
|
-
if (prop.type === "string") {
|
|
527
|
-
const val = cleanLiteral(prop);
|
|
528
|
-
return val ? [val.toUpperCase()] : ["GET"];
|
|
529
|
-
}
|
|
93
|
+
return undefined;
|
|
94
|
+
};
|
|
530
95
|
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
96
|
+
const readCall = (call: ts.CallExpression, plugin?: ts.FunctionLikeDeclaration) => {
|
|
97
|
+
signal?.throwIfAborted();
|
|
98
|
+
const callee = call.expression;
|
|
99
|
+
if (call.questionDotToken || !ts.isPropertyAccessExpression(callee) || callee.questionDotToken) return;
|
|
100
|
+
const method = callee.name.text;
|
|
101
|
+
if (!instanceMethods.has(method)) return;
|
|
102
|
+
const scope = resolveScope(callee.expression, plugin);
|
|
103
|
+
if (!scope) return;
|
|
104
|
+
|
|
105
|
+
if (method === "addSchema") {
|
|
106
|
+
const id = literal(option(optionsOf(call.arguments[0]), "$id"))?.replace(/#$/, "");
|
|
107
|
+
if (!id) return;
|
|
108
|
+
const entries = schemas.get(scope) ?? new Map<string, Schema | null>();
|
|
109
|
+
entries.set(id, entries.has(id) ? null : { id, call });
|
|
110
|
+
schemas.set(scope, entries);
|
|
111
|
+
return;
|
|
536
112
|
}
|
|
537
|
-
return methods.length > 0 ? methods : ["GET"];
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
return ["GET"];
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
private extractHandlerFromRoute(
|
|
544
|
-
routeObj: Parser.SyntaxNode,
|
|
545
|
-
secondaryArg?: Parser.SyntaxNode
|
|
546
|
-
): { symbol: string; line: number } {
|
|
547
|
-
const handlerNode = this.findObjectPropertyNode(routeObj, "handler") ?? secondaryArg;
|
|
548
|
-
if (handlerNode) {
|
|
549
|
-
return this.extractSingleHandler(handlerNode);
|
|
550
|
-
}
|
|
551
|
-
return { symbol: "closure", line: routeObj.startPosition.row + 1 };
|
|
552
|
-
}
|
|
553
113
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
private extractHooksFromOptions(optsNode: Parser.SyntaxNode): Array<{ symbol: string; line: number }> {
|
|
568
|
-
const hooks: Array<{ symbol: string; line: number }> = [];
|
|
569
|
-
if (optsNode.type !== "object") return hooks;
|
|
570
|
-
|
|
571
|
-
for (const hookName of ["preHandler", "onRequest", "preValidation", "preSerialization"]) {
|
|
572
|
-
const hookProp = this.findObjectPropertyNode(optsNode, hookName);
|
|
573
|
-
if (hookProp) {
|
|
574
|
-
if (hookProp.type === "array") {
|
|
575
|
-
for (const el of hookProp.namedChildren) {
|
|
576
|
-
hooks.push(this.extractSingleHandler(el));
|
|
577
|
-
}
|
|
578
|
-
} else {
|
|
579
|
-
hooks.push(this.extractSingleHandler(hookProp));
|
|
114
|
+
if (method === "register") {
|
|
115
|
+
let expression = bindings.value(call.arguments[0]);
|
|
116
|
+
let ignorePrefix = false;
|
|
117
|
+
let unresolvedEncapsulation = false;
|
|
118
|
+
if (expression && ts.isCallExpression(expression) &&
|
|
119
|
+
bindings.packageBinding(expression.expression)?.module === "fastify-plugin") {
|
|
120
|
+
const wrapperOptions = optionsOf(expression.arguments[1]);
|
|
121
|
+
const encapsulate = option(wrapperOptions, "encapsulate");
|
|
122
|
+
unresolvedEncapsulation = Boolean(encapsulate && encapsulate.kind !== ts.SyntaxKind.TrueKeyword &&
|
|
123
|
+
encapsulate.kind !== ts.SyntaxKind.FalseKeyword || expression.arguments[1] && !wrapperOptions ||
|
|
124
|
+
wrapperOptions && !encapsulate && hasUnknownFields(wrapperOptions));
|
|
125
|
+
ignorePrefix = encapsulate?.kind !== ts.SyntaxKind.TrueKeyword;
|
|
126
|
+
expression = expression.arguments[0];
|
|
580
127
|
}
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
// 1. Check TypeScript type arguments: fastify.post<{ Body: CreateUserDto }>(...)
|
|
595
|
-
const typeArgs = callNode.childForFieldName("type_arguments");
|
|
596
|
-
if (typeArgs) {
|
|
597
|
-
for (const child of typeArgs.namedChildren) {
|
|
598
|
-
if (child.type === "type_literal" || child.type === "object_type") {
|
|
599
|
-
for (const prop of child.namedChildren) {
|
|
600
|
-
if (prop.type === "property_signature") {
|
|
601
|
-
const nameNode = prop.childForFieldName("name");
|
|
602
|
-
const typeNode = prop.childForFieldName("type");
|
|
603
|
-
if (nameNode && nameNode.text.trim().toLowerCase() === "body" && typeNode) {
|
|
604
|
-
// typeNode is type_annotation -> type_identifier
|
|
605
|
-
const ident = typeNode.namedChildren.find((c) => c.type === "type_identifier") ?? typeNode;
|
|
606
|
-
const dtoName = ident.text.trim().replace(/^:\s*/, "");
|
|
607
|
-
if (dtoName) bodyDto = dtoName;
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
}
|
|
128
|
+
const target = bindings.handler(expression);
|
|
129
|
+
if (!target?.node.body || target.node === scope) return;
|
|
130
|
+
const opts = optionsOf(call.arguments[1]);
|
|
131
|
+
const prefixArg = option(opts, "prefix");
|
|
132
|
+
const prefix = unresolvedEncapsulation ? undefined : ignorePrefix || !call.arguments[1] ? "" :
|
|
133
|
+
prefixArg ? literal(prefixArg)?.replace(/\/+$/, "") : opts && !hasUnknownFields(opts) ? "" : undefined;
|
|
134
|
+
mounts.push({ parent: scope, child: target.node, prefix });
|
|
135
|
+
const sources = parents.get(target.node) ?? new Set<Scope>();
|
|
136
|
+
sources.add(scope);
|
|
137
|
+
parents.set(target.node, sources);
|
|
138
|
+
if (!seenPlugins.has(target.node)) {
|
|
139
|
+
plugins.push(target.node);
|
|
140
|
+
seenPlugins.add(target.node);
|
|
611
141
|
}
|
|
142
|
+
return;
|
|
612
143
|
}
|
|
613
|
-
}
|
|
614
144
|
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
const
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
145
|
+
if (method !== "route" && !verbs.has(method)) return;
|
|
146
|
+
const opts = optionsOf(call.arguments[method === "route" ? 0 : 1]);
|
|
147
|
+
const target = method !== "route" && call.arguments.length >= 3 ? call.arguments[2] :
|
|
148
|
+
(opts && objectMember(opts, "handler")) ?? (method !== "route" && !opts ? call.arguments[1] : undefined);
|
|
149
|
+
const uri = method === "route" ? literal(option(opts, "url") ?? option(opts, "path")) : literal(call.arguments[0]);
|
|
150
|
+
const methodValue = option(opts, "method");
|
|
151
|
+
const strings = method === "route"
|
|
152
|
+
? (methodValue && ts.isArrayLiteralExpression(methodValue) ? methodValue.elements.map(literal) : [literal(methodValue)])
|
|
153
|
+
: [method === "all" ? "ANY" : method.toUpperCase()];
|
|
154
|
+
if (uri === undefined || !target || !strings.length ||
|
|
155
|
+
!strings.every((value): value is string => value !== undefined && /^[a-z]+$/i.test(value))) return;
|
|
156
|
+
const hookExpressions = hookNames.flatMap(name => {
|
|
157
|
+
const hint = option(opts, name);
|
|
158
|
+
return !hint ? [] : ts.isArrayLiteralExpression(hint) ? [...hint.elements] : [hint];
|
|
159
|
+
});
|
|
160
|
+
const handler = ts.isMethodDeclaration(target) && target.body
|
|
161
|
+
? { node: target, name: nameText(target.name) ?? "handler" }
|
|
162
|
+
: ts.isMethodDeclaration(target) ? undefined : bindings.handler(target);
|
|
163
|
+
const schema = optionsOf(option(opts, "schema"));
|
|
164
|
+
const body = optionsOf(option(schema, "body"));
|
|
165
|
+
const schemaRef = literal(option(body ?? schema, "$ref"));
|
|
166
|
+
const type = call.typeArguments?.[0];
|
|
167
|
+
const dto = type && ts.isTypeLiteralNode(type) && type.members.find(member =>
|
|
168
|
+
ts.isPropertySignature(member) && nameText(member.name) === "Body",
|
|
169
|
+
);
|
|
170
|
+
const bodyType = dto && ts.isPropertySignature(dto) ? dto.type?.getText() : undefined;
|
|
171
|
+
routes.push({
|
|
172
|
+
scope, call, uri, methods: [...new Set(strings.map(value => value.toUpperCase()))],
|
|
173
|
+
handler, target, hooks: hookExpressions, schemaRef, bodyType, rootSlash: literal(option(opts, "prefixTrailingSlash")),
|
|
174
|
+
});
|
|
175
|
+
};
|
|
631
176
|
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
if (
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
}
|
|
177
|
+
const routeCallHint = /\.(?:route|register|addHook|addSchema|get|post|put|patch|delete|head|options|trace|connect|all)\s*\(/;
|
|
178
|
+
for (const file of files) {
|
|
179
|
+
signal?.throwIfAborted();
|
|
180
|
+
if (!bindings.matches(file, routeCallHint)) continue;
|
|
181
|
+
const unit = bindings.load(file);
|
|
182
|
+
if (unit) visitRouteCalls(unit.source, readCall);
|
|
183
|
+
}
|
|
184
|
+
for (let index = 0; index < plugins.length; index++) {
|
|
185
|
+
signal?.throwIfAborted();
|
|
186
|
+
const plugin = plugins[index]!;
|
|
187
|
+
if (plugin.body) visitRouteCalls(plugin.body, call => readCall(call, plugin));
|
|
644
188
|
}
|
|
645
|
-
return undefined;
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
private extractObjectPropertyString(objNode: Parser.SyntaxNode, key: string): string | undefined {
|
|
649
|
-
const valNode = this.findObjectPropertyNode(objNode, key);
|
|
650
|
-
return cleanLiteral(valNode);
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
private resolveScopePrefixes(
|
|
654
|
-
registers: RawFastifyRegister[],
|
|
655
|
-
fileImports: Map<string, Map<string, string>>,
|
|
656
|
-
fileScopes: Map<string, Set<string>>
|
|
657
|
-
): Map<string, string[]> {
|
|
658
|
-
// Map: scopeId -> Set of prefix strings
|
|
659
|
-
const prefixes = new Map<string, Set<string>>();
|
|
660
189
|
|
|
661
|
-
const
|
|
662
|
-
|
|
663
|
-
if (
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
return
|
|
190
|
+
const prefixes = composeMountPrefixes(roots, mounts, signal);
|
|
191
|
+
const schemaAt = (scope: Scope, id: string, seen = new Set<Scope>()): Schema | undefined => {
|
|
192
|
+
if (seen.has(scope) || seen.size >= 64) return undefined;
|
|
193
|
+
const local = schemas.get(scope)?.get(id);
|
|
194
|
+
if (local !== undefined) return local ?? undefined;
|
|
195
|
+
const matches = new Set([...parents.get(scope) ?? []].map(parent => schemaAt(parent, id, new Set([...seen, scope]))));
|
|
196
|
+
return matches.size === 1 ? [...matches][0] : undefined;
|
|
668
197
|
};
|
|
669
198
|
|
|
670
|
-
|
|
671
|
-
const
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
targetScopeId = Array.from(targets)[0];
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
} else {
|
|
689
|
-
// Local symbol in same file
|
|
690
|
-
targetScopeId = `${reg.relPath}#${reg.targetPluginSymbol}`;
|
|
691
|
-
}
|
|
199
|
+
const slices: VerticalSliceCandidate[] = [];
|
|
200
|
+
for (const route of routes) {
|
|
201
|
+
signal?.throwIfAborted();
|
|
202
|
+
const routeFile = relative(route.call);
|
|
203
|
+
const routeLine = lineOf(route.call);
|
|
204
|
+
const actionName = route.handler ? (route.handler.name === "callback" ? "closure" : route.handler.name) :
|
|
205
|
+
route.target?.getText() ?? "handler";
|
|
206
|
+
const controllerFile = route.handler ? relative(route.handler.node) : null;
|
|
207
|
+
const controllerLine = route.handler ? lineOf(route.handler.node) : 0;
|
|
208
|
+
const controller = path.basename(controllerFile ?? routeFile).replace(/\.[^.]+$/, "");
|
|
209
|
+
const evidence: ExecutionChainNode[] = [];
|
|
210
|
+
if (route.bodyType) {
|
|
211
|
+
evidence.push({
|
|
212
|
+
stage: "validation", symbol: route.bodyType, file: routeFile, line: routeLine, description: "Fastify Body DTO",
|
|
213
|
+
});
|
|
692
214
|
}
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
215
|
+
if (route.schemaRef) {
|
|
216
|
+
const known = schemaAt(route.scope, route.schemaRef.replace(/#$/, ""));
|
|
217
|
+
evidence.push(known ? {
|
|
218
|
+
stage: "validation", symbol: known.id, file: relative(known.call), line: lineOf(known.call),
|
|
219
|
+
description: "Resolved Fastify Schema: " + known.id,
|
|
220
|
+
} : {
|
|
221
|
+
stage: "validation", symbol: "unresolved:$ref", file: routeFile, line: routeLine,
|
|
222
|
+
description: "Unresolved schema reference: " + route.schemaRef,
|
|
223
|
+
});
|
|
696
224
|
}
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
225
|
+
for (const hook of route.hooks) {
|
|
226
|
+
evidence.push({
|
|
227
|
+
stage: "middleware", symbol: hook.getText(), file: relative(hook), line: lineOf(hook), description: "Fastify Hook Argument",
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
if (controllerFile) {
|
|
231
|
+
const reference = route.target && (ts.isIdentifier(route.target) || ts.isPropertyAccessExpression(route.target))
|
|
232
|
+
? route.target.getText() : actionName;
|
|
233
|
+
evidence.push({
|
|
234
|
+
stage: "controller", symbol: reference, file: controllerFile, line: controllerLine, description: "Fastify Action Handler",
|
|
235
|
+
});
|
|
705
236
|
}
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
// 3. Propagate prefixes along registers
|
|
709
|
-
let changed = true;
|
|
710
|
-
let iterations = 0;
|
|
711
|
-
|
|
712
|
-
while (changed && iterations < 15) {
|
|
713
|
-
changed = false;
|
|
714
|
-
iterations++;
|
|
715
|
-
|
|
716
|
-
for (const reg of registers) {
|
|
717
|
-
let targetScopeId = reg.targetScopeId;
|
|
718
|
-
if (!targetScopeId && reg.targetPluginSymbol) {
|
|
719
|
-
const importedFile = fileImports.get(reg.relPath)?.get(reg.targetPluginSymbol);
|
|
720
|
-
if (importedFile) {
|
|
721
|
-
targetScopeId = `${importedFile}#${reg.targetPluginSymbol}`;
|
|
722
|
-
const targets = fileScopes.get(importedFile);
|
|
723
|
-
if (targets && !targets.has(targetScopeId)) {
|
|
724
|
-
if (targets.has(`${importedFile}#default`)) {
|
|
725
|
-
targetScopeId = `${importedFile}#default`;
|
|
726
|
-
} else if (targets.size > 0) {
|
|
727
|
-
targetScopeId = Array.from(targets)[0];
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
} else {
|
|
731
|
-
targetScopeId = `${reg.relPath}#${reg.targetPluginSymbol}`;
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
if (!targetScopeId) continue;
|
|
736
|
-
|
|
737
|
-
const parentPrefixes = getPrefixes(reg.parentScopeId);
|
|
738
|
-
if (parentPrefixes.size === 0) continue;
|
|
739
|
-
const basePrefixes = Array.from(parentPrefixes);
|
|
740
|
-
const targetSet = getPrefixes(targetScopeId);
|
|
741
237
|
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
238
|
+
for (const prefix of prefixes.get(route.scope) ?? []) {
|
|
239
|
+
const uri = route.uri === "/" && prefix && route.rootSlash !== "slash" ? prefix :
|
|
240
|
+
prefix + "/" + route.uri.replace(/^\/+/, "");
|
|
241
|
+
for (const method of route.methods) {
|
|
242
|
+
const chain: ExecutionChainNode[] = [{
|
|
243
|
+
stage: "ingress", symbol: method + " " + uri, file: routeFile, line: routeLine, description: "Fastify Route Registration",
|
|
244
|
+
}, ...evidence];
|
|
245
|
+
slices.push({
|
|
246
|
+
domain_id: domainId ?? null,
|
|
247
|
+
feature_key: `fastify.${controller.toLowerCase()}.${actionName}`,
|
|
248
|
+
http_method: method, route_uri: uri, route_name: `${controller}.${actionName}`,
|
|
249
|
+
controller_class: controller, action_name: actionName, controller_file: controllerFile,
|
|
250
|
+
controller_line: controllerLine, architecture_style: "mvc", entry_kind: "http_route",
|
|
251
|
+
execution_chain_json: JSON.stringify(chain),
|
|
252
|
+
});
|
|
748
253
|
}
|
|
749
254
|
}
|
|
750
255
|
}
|
|
751
|
-
|
|
752
|
-
const result = new Map<string, string[]>();
|
|
753
|
-
for (const [k, set] of prefixes.entries()) {
|
|
754
|
-
result.set(k, Array.from(set));
|
|
755
|
-
}
|
|
756
|
-
return result;
|
|
256
|
+
return slices;
|
|
757
257
|
}
|
|
758
258
|
}
|
|
759
|
-
|