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