@dimassetoid/septum-mcp 0.2.1 → 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 +46 -45
- package/package.json +8 -6
- package/src/cli/commands/check.ts +41 -26
- package/src/cli/commands/hook.ts +77 -105
- package/src/cli/commands/init.ts +35 -9
- package/src/cli/commands/stats.ts +33 -0
- package/src/cli/commands/sync-progress.ts +71 -0
- package/src/cli/commands/sync.ts +35 -9
- package/src/cli/hook-payload.ts +43 -0
- package/src/cli/index.ts +15 -51
- package/src/cli/scaffold/harness-config.ts +48 -8
- package/src/cli/templates/hooks.ts +51 -27
- package/src/cli/templates/rules.ts +34 -49
- package/src/cli/templates/skill.ts +46 -35
- package/src/core/boundary/audit.ts +383 -0
- package/src/core/boundary/evaluator.ts +299 -178
- package/src/core/boundary/staged-auditor.ts +188 -34
- package/src/core/config/domain-ownership.ts +47 -0
- package/src/core/config/domain-registration.ts +141 -0
- package/src/core/config/loader.ts +82 -54
- package/src/core/config/schema.ts +24 -46
- package/src/core/database/client.ts +2 -0
- package/src/core/database/repositories/dependency.repository.ts +326 -543
- package/src/core/database/repositories/domain.repository.ts +48 -4
- package/src/core/database/repositories/file.repository.ts +10 -0
- package/src/core/database/repositories/symbol.repository.ts +97 -259
- package/src/core/database/repository.ts +138 -24
- package/src/core/database/schema.ts +28 -33
- package/src/core/discovery/declarative-routes.ts +69 -0
- package/src/core/discovery/framework-detector.ts +149 -0
- package/src/core/discovery/navigation.ts +269 -0
- package/src/core/discovery/topology-detector.ts +153 -170
- package/src/core/errors.ts +19 -0
- package/src/core/ingestion/pipeline.ts +378 -212
- package/src/core/parser/behavioral-analyzer.ts +26 -22
- package/src/core/parser/boundary-tracker.ts +15 -0
- package/src/core/parser/extractors/base.ts +16 -4
- package/src/core/parser/extractors/csharp.ts +9 -5
- package/src/core/parser/extractors/django-routing.ts +163 -0
- package/src/core/parser/extractors/django-semantic.ts +400 -516
- package/src/core/parser/extractors/elysia-semantic.ts +172 -496
- package/src/core/parser/extractors/express-semantic.ts +195 -398
- package/src/core/parser/extractors/fastapi-semantic.ts +182 -399
- package/src/core/parser/extractors/fastify-semantic.ts +216 -732
- package/src/core/parser/extractors/flask-semantic.ts +92 -388
- package/src/core/parser/extractors/go-routing.ts +344 -0
- package/src/core/parser/extractors/go-semantic.ts +210 -508
- 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 -530
- package/src/core/parser/extractors/java.ts +7 -5
- package/src/core/parser/extractors/laravel-routing.ts +263 -0
- package/src/core/parser/extractors/laravel-semantic.ts +169 -333
- 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 +84 -336
- package/src/core/parser/extractors/nestjs-semantic.ts +238 -510
- 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 +17 -1
- package/src/core/parser/extractors/python-route-ast.ts +249 -0
- package/src/core/parser/extractors/python.ts +9 -8
- package/src/core/parser/extractors/registry.ts +28 -5
- package/src/core/parser/extractors/route-prefixes.ts +43 -0
- package/src/core/parser/extractors/ruby.ts +10 -7
- package/src/core/parser/extractors/rust.ts +35 -17
- package/src/core/parser/extractors/semantic-extractor.interface.ts +3 -1
- package/src/core/parser/extractors/symfony-routing.ts +114 -0
- package/src/core/parser/extractors/symfony-semantic.ts +75 -262
- 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 +36 -216
- package/src/core/parser/implementation-fingerprint.ts +97 -0
- package/src/core/parser/language-capabilities.ts +23 -7
- package/src/core/parser/schema/providers/laravel-migration-provider.ts +77 -51
- package/src/core/parser/schema/schema-provider-registry.ts +1 -3
- package/src/core/parser/schema/schema-provider.interface.ts +2 -1
- package/src/core/parser/semantic-revision.ts +3 -0
- package/src/core/parser/static-string.ts +51 -0
- 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 +375 -311
- 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 +162 -181
- package/src/core/resolver/interpretation-inputs.ts +65 -0
- package/src/core/resolver/languages/go.ts +103 -0
- package/src/core/resolver/languages/managed.ts +200 -0
- package/src/core/resolver/languages/python.ts +133 -0
- package/src/core/resolver/languages/ruby.ts +40 -0
- package/src/core/resolver/languages/rust.ts +127 -0
- package/src/core/resolver/languages/shared.ts +74 -0
- package/src/core/resolver/module-resolver.ts +18 -7
- 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 +94 -0
- package/src/core/resolver/slice-format.ts +22 -0
- package/src/core/resolver/slice-selection.ts +237 -0
- package/src/core/resolver/symbol-impact.ts +477 -0
- package/src/core/resolver/symbol-locator.ts +82 -15
- package/src/core/resolver/vertical-slice-tracer.ts +57 -278
- package/src/core/resources/ignore.ts +21 -1
- package/src/core/resources/limits.ts +2 -1
- package/src/core/resources/source-files.ts +143 -20
- package/src/core/resources/source-inventory.ts +55 -0
- package/src/core/resources/work-queue.ts +167 -4
- package/src/core/retrieval/context-page.ts +116 -0
- package/src/core/retrieval/context.ts +457 -0
- package/src/core/retrieval/resolution-warnings.ts +21 -0
- package/src/core/retrieval/source-context.ts +448 -0
- package/src/core/retrieval/symbol-details.ts +453 -0
- package/src/core/retrieval/symbol-evidence.ts +97 -0
- package/src/core/retrieval/typescript-references.ts +293 -0
- package/src/core/retrieval/universal-context.ts +182 -0
- package/src/core/telemetry/summary.ts +114 -0
- package/src/core/telemetry/telemetry.ts +225 -37
- package/src/index.ts +0 -3
- 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 -1105
- package/src/mcp/server.ts +263 -597
- package/src/mcp/tool-catalog.ts +331 -0
- package/src/mcp/tools/boundary.ts +69 -4
- package/src/mcp/tools/map.ts +126 -0
- package/src/mcp/tools/slice.ts +101 -3
- package/src/mcp/tools/symbol.ts +480 -26
- 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 +102 -18
- package/src/mcp/workspace-watcher.ts +38 -19
- package/src/types/index.ts +47 -53
- package/src/cli/commands/topology.ts +0 -161
- package/src/core/database/repositories/environment.repository.ts +0 -207
- package/src/core/parser/schema/providers/prisma-schema-provider.ts +0 -90
- package/src/core/session/session-manager.ts +0 -107
- package/src/core/topology/environment-detector.ts +0 -973
- package/src/core/topology/types.ts +0 -83
- package/src/mcp/tools/check-boundary.ts +0 -387
- package/src/mcp/tools/domain.ts +0 -40
- package/src/mcp/tools/feature.ts +0 -67
- package/src/mcp/tools/get-domain-catalog.ts +0 -226
- package/src/mcp/tools/get-environment-topology.ts +0 -138
- package/src/mcp/tools/get-feature-context.ts +0 -224
- package/src/mcp/tools/get-symbol-hotspots.ts +0 -54
- package/src/mcp/tools/get-symbol-impact.ts +0 -64
- package/src/mcp/tools/get-symbol.ts +0 -175
- package/src/mcp/tools/locate-symbol.ts +0 -196
- package/src/mcp/tools/register-domain.ts +0 -75
- package/src/mcp/tools/register-feature.ts +0 -110
- package/src/mcp/tools/trace-vertical-slice.ts +0 -80
|
@@ -1,70 +1,30 @@
|
|
|
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
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";
|
|
7
7
|
import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
httpMethods: string[];
|
|
25
|
-
rawPath: string;
|
|
26
|
-
hooks: Array<{ symbol: string; line: number }>;
|
|
27
|
-
handler: { symbol: string; line: number };
|
|
28
|
-
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[];
|
|
29
24
|
schemaRef?: string;
|
|
30
|
-
|
|
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
|
-
}
|
|
25
|
+
bodyType?: string;
|
|
26
|
+
rootSlash?: string;
|
|
27
|
+
};
|
|
68
28
|
|
|
69
29
|
export class FastifySemanticExtractor implements SemanticSliceExtractor {
|
|
70
30
|
public readonly id = "fastify";
|
|
@@ -72,703 +32,227 @@ export class FastifySemanticExtractor implements SemanticSliceExtractor {
|
|
|
72
32
|
|
|
73
33
|
public canHandle(projectRoot: string, framework?: string): boolean {
|
|
74
34
|
if (framework === "fastify") return true;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (deps["fastify"]) return true;
|
|
82
|
-
} catch {}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
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; }
|
|
86
41
|
}
|
|
87
42
|
|
|
88
43
|
public async extractSlices(
|
|
89
44
|
projectRoot: string,
|
|
90
45
|
domainId?: number,
|
|
91
|
-
signal?: AbortSignal
|
|
46
|
+
signal?: AbortSignal,
|
|
47
|
+
inventory?: SourceInventory,
|
|
92
48
|
): Promise<VerticalSliceCandidate[]> {
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const
|
|
106
|
-
|
|
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
|
-
}
|
|
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;
|
|
330
63
|
};
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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
|
-
}
|
|
64
|
+
const option = (object: ts.ObjectLiteralExpression | undefined, key: string) => {
|
|
65
|
+
const value = object && objectMember(object, key);
|
|
66
|
+
return value && !ts.isMethodDeclaration(value) ? value : undefined;
|
|
466
67
|
};
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
if (
|
|
480
|
-
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;
|
|
481
82
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
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
|
-
}
|
|
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;
|
|
507
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);
|
|
508
92
|
}
|
|
509
|
-
|
|
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;
|
|
93
|
+
return undefined;
|
|
94
|
+
};
|
|
569
95
|
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
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;
|
|
580
112
|
}
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
return hooks;
|
|
584
|
-
}
|
|
585
113
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
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
|
-
}
|
|
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];
|
|
610
127
|
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
const
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
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);
|
|
624
141
|
}
|
|
142
|
+
return;
|
|
625
143
|
}
|
|
626
|
-
}
|
|
627
144
|
|
|
628
|
-
|
|
629
|
-
|
|
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
|
+
};
|
|
630
176
|
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
if (
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
}
|
|
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));
|
|
643
188
|
}
|
|
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
189
|
|
|
660
|
-
const
|
|
661
|
-
|
|
662
|
-
if (
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
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;
|
|
667
197
|
};
|
|
668
198
|
|
|
669
|
-
|
|
670
|
-
const
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
targetScopeId = Array.from(targets)[0];
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
} else {
|
|
688
|
-
// Local symbol in same file
|
|
689
|
-
targetScopeId = `${reg.relPath}#${reg.targetPluginSymbol}`;
|
|
690
|
-
}
|
|
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
|
+
});
|
|
691
214
|
}
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
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
|
+
});
|
|
695
224
|
}
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
for (const sc of scopes) {
|
|
701
|
-
if (!childScopes.has(sc) || sc.endsWith("#root")) {
|
|
702
|
-
getPrefixes(sc).add("");
|
|
703
|
-
}
|
|
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
|
+
});
|
|
704
229
|
}
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
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
|
-
}
|
|
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
|
+
});
|
|
748
236
|
}
|
|
749
|
-
}
|
|
750
237
|
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
} else if (entry.isFile() && /\.(?:[cm]?[jt]sx?)$/.test(entry.name) && !entry.name.endsWith(".d.ts")) {
|
|
767
|
-
results.push(full);
|
|
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
|
+
});
|
|
768
253
|
}
|
|
769
254
|
}
|
|
770
|
-
}
|
|
771
|
-
return
|
|
255
|
+
}
|
|
256
|
+
return slices;
|
|
772
257
|
}
|
|
773
258
|
}
|
|
774
|
-
|