weavatrix 0.2.12 → 0.2.14
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 +90 -21
- package/SECURITY.md +2 -2
- package/docs/releases/v0.2.14.md +93 -0
- package/package.json +2 -2
- package/skill/SKILL.md +108 -43
- package/src/analysis/allowed-test-runner.js +20 -9
- package/src/analysis/architecture/contract-graph.js +119 -0
- package/src/analysis/architecture/contract-schema.js +110 -0
- package/src/analysis/architecture/contract-storage.js +35 -0
- package/src/analysis/architecture/contract-verification.js +168 -0
- package/src/analysis/architecture-contract.js +16 -343
- package/src/analysis/change-classification/diff-parser.js +103 -0
- package/src/analysis/change-classification/options.js +40 -0
- package/src/analysis/change-classification/symbol-classifier.js +177 -0
- package/src/analysis/change-classification.js +120 -519
- package/src/analysis/dead-code-review/policy.js +23 -0
- package/src/analysis/dead-code-review.js +9 -19
- package/src/analysis/dep-check.js +10 -57
- package/src/analysis/dep-rules.js +10 -303
- package/src/analysis/dependency/scoped-dependencies.js +40 -0
- package/src/analysis/endpoints/common.js +62 -0
- package/src/analysis/endpoints/extract.js +107 -0
- package/src/analysis/endpoints/inventory.js +84 -0
- package/src/analysis/endpoints/mounts.js +129 -0
- package/src/analysis/endpoints-java.js +80 -3
- package/src/analysis/endpoints.js +3 -448
- package/src/analysis/git-history/analytics.js +177 -0
- package/src/analysis/git-history/collector.js +109 -0
- package/src/analysis/git-history/options.js +68 -0
- package/src/analysis/git-history.js +128 -577
- package/src/analysis/health-capabilities.js +82 -0
- package/src/analysis/http-contracts/analysis.js +169 -0
- package/src/analysis/http-contracts/client-call-detection.js +81 -0
- package/src/analysis/http-contracts/client-call-parser.js +255 -0
- package/src/analysis/http-contracts/graph-context.js +143 -0
- package/src/analysis/http-contracts/matching.js +61 -0
- package/src/analysis/http-contracts/shared.js +86 -0
- package/src/analysis/http-contracts.js +6 -822
- package/src/analysis/internal-audit/dependency-health.js +111 -0
- package/src/analysis/internal-audit/python-manifests.js +65 -0
- package/src/analysis/internal-audit/repo-files.js +55 -0
- package/src/analysis/internal-audit/supply-chain.js +132 -0
- package/src/analysis/internal-audit.collect.js +5 -106
- package/src/analysis/internal-audit.run.js +113 -200
- package/src/analysis/jvm-dependency-evidence.js +69 -0
- package/src/analysis/source-correctness/retry-patterns.js +93 -0
- package/src/analysis/source-correctness.js +225 -0
- package/src/analysis/structure/dependency-graph.js +156 -0
- package/src/analysis/structure/findings.js +142 -0
- package/src/graph/builder/go-receiver-resolution.js +112 -0
- package/src/graph/builder/js/queries.js +48 -0
- package/src/graph/builder/lang-go.js +89 -4
- package/src/graph/builder/lang-js.js +4 -44
- package/src/graph/builder/pass2-resolution.js +68 -0
- package/src/graph/freshness-probe.js +2 -1
- package/src/graph/incremental-refresh.js +3 -2
- package/src/graph/internal-builder.build.js +22 -183
- package/src/graph/internal-builder.pass2.js +161 -0
- package/src/graph/internal-builder.resolvers.js +2 -105
- package/src/graph/resolvers/rust.js +117 -0
- package/src/mcp/actions/advisories.mjs +18 -0
- package/src/mcp/actions/graph-lifecycle.mjs +148 -0
- package/src/mcp/actions/graph-sync.mjs +195 -0
- package/src/mcp/actions/hosted-architecture.mjs +57 -0
- package/src/mcp/architecture-bootstrap.mjs +168 -0
- package/src/mcp/architecture-starter.mjs +234 -0
- package/src/mcp/catalog.mjs +22 -6
- package/src/mcp/evidence/bun-lock-graph.mjs +209 -0
- package/src/mcp/evidence/package-graph-common.mjs +115 -0
- package/src/mcp/evidence/package-lock-graph.mjs +92 -0
- package/src/mcp/evidence-snapshot.package-graph.mjs +5 -474
- package/src/mcp/graph/context-core.mjs +111 -0
- package/src/mcp/graph/context-seeds.mjs +208 -0
- package/src/mcp/graph/context-state.mjs +86 -0
- package/src/mcp/graph/tools-core.mjs +143 -0
- package/src/mcp/graph/tools-query.mjs +223 -0
- package/src/mcp/graph-context.mjs +4 -496
- package/src/mcp/health/audit-format.mjs +172 -0
- package/src/mcp/health/audit.mjs +171 -0
- package/src/mcp/health/dead-code.mjs +110 -0
- package/src/mcp/health/duplicates.mjs +83 -0
- package/src/mcp/health/endpoints.mjs +42 -0
- package/src/mcp/health/structure.mjs +219 -0
- package/src/mcp/runtime-version.mjs +32 -0
- package/src/mcp/server/auto-refresh.mjs +66 -0
- package/src/mcp/server/runtime-config.mjs +43 -0
- package/src/mcp/server/shutdown.mjs +40 -0
- package/src/mcp/sync/evidence-architecture.mjs +54 -0
- package/src/mcp/sync/evidence-common.mjs +88 -0
- package/src/mcp/sync/evidence-duplicates.mjs +100 -0
- package/src/mcp/sync/evidence-health.mjs +56 -0
- package/src/mcp/sync/evidence-packages.mjs +95 -0
- package/src/mcp/sync/payload-common.mjs +97 -0
- package/src/mcp/sync/payload-v2.mjs +53 -0
- package/src/mcp/sync/payload-v3.mjs +79 -0
- package/src/mcp/sync-evidence.mjs +7 -402
- package/src/mcp/sync-payload.mjs +10 -244
- package/src/mcp/tools-actions.mjs +18 -414
- package/src/mcp/tools-architecture.mjs +18 -70
- package/src/mcp/tools-context.mjs +56 -15
- package/src/mcp/tools-endpoints.mjs +4 -1
- package/src/mcp/tools-graph.mjs +17 -330
- package/src/mcp/tools-health.mjs +24 -705
- package/src/mcp/tools-verified-change.mjs +12 -4
- package/src/mcp-server.mjs +49 -146
- package/src/precision/lsp-client/constants.js +12 -0
- package/src/precision/lsp-client/environment.js +20 -0
- package/src/precision/lsp-client/errors.js +15 -0
- package/src/precision/lsp-client/lifecycle.js +127 -0
- package/src/precision/lsp-client/message-parser.js +81 -0
- package/src/precision/lsp-client/protocol.js +212 -0
- package/src/precision/lsp-client/registry.js +58 -0
- package/src/precision/lsp-client/repo-uri.js +60 -0
- package/src/precision/lsp-client/stdio-client.js +151 -0
- package/src/precision/lsp-client.js +10 -682
- package/src/precision/lsp-overlay/build.js +238 -0
- package/src/precision/lsp-overlay/contract.js +61 -0
- package/src/precision/lsp-overlay/reference-results.js +108 -0
- package/src/precision/lsp-overlay/semantic-inputs.js +62 -0
- package/src/precision/lsp-overlay/source-session.js +134 -0
- package/src/precision/lsp-overlay/store.js +150 -0
- package/src/precision/lsp-overlay/target-index.js +147 -0
- package/src/precision/lsp-overlay/target-query.js +55 -0
- package/src/precision/lsp-overlay.js +11 -868
- package/src/precision/typescript-lsp-provider.js +12 -682
- package/src/precision/typescript-provider/client.js +69 -0
- package/src/precision/typescript-provider/discovery.js +124 -0
- package/src/precision/typescript-provider/project-host.js +249 -0
- package/src/precision/typescript-provider/project-safety.js +161 -0
- package/src/precision/typescript-provider/reference-usage.js +53 -0
- package/src/version.js +7 -0
- package/docs/releases/v0.2.12.md +0 -45
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { extractRustEndpoints } from "../endpoints-rust.js";
|
|
2
|
+
import { extractSpringEndpoints } from "../endpoints-java.js";
|
|
3
|
+
import { HTTP_METHODS, cleanPath, handlerName, handlerReference, lineAt, looksLikePath, maskComments } from "./common.js";
|
|
4
|
+
|
|
5
|
+
const HTTP_CLIENT_CALLER = /^(axios|https?|fetch|ky|got|superagent|needle|undici|xhr|\$http|http[Cc]lient|api[Cc]lient|rest[Cc]lient)$/;
|
|
6
|
+
const OPENAPI_BLOCK = /\boperationId\b|\bresponses\s*:|\brequestBody\b|\bschemaRef\b|\boperation\s*\(|\bsummary\s*:/;
|
|
7
|
+
|
|
8
|
+
export function nextRoutePath(file) {
|
|
9
|
+
const parts = String(file || "").replace(/\\/g, "/").split("/").filter(Boolean);
|
|
10
|
+
if (!/^route\.[cm]?[jt]s$/i.test(parts.at(-1) || "")) return "";
|
|
11
|
+
const appAt = parts.lastIndexOf("app");
|
|
12
|
+
if (appAt < 0) return "";
|
|
13
|
+
const route = [];
|
|
14
|
+
for (let segment of parts.slice(appAt + 1, -1)) {
|
|
15
|
+
if (!segment || /^\([^)]*\)$/.test(segment) || segment.startsWith("@")) continue;
|
|
16
|
+
segment = segment.replace(/^\((?:\.{1,3})\)/, "");
|
|
17
|
+
let match;
|
|
18
|
+
if ((match = /^\[\[\.\.\.([^\]]+)\]\]$/.exec(segment))) segment = `*${match[1]}?`;
|
|
19
|
+
else if ((match = /^\[\.\.\.([^\]]+)\]$/.exec(segment))) segment = `*${match[1]}`;
|
|
20
|
+
else if ((match = /^\[([^\]]+)\]$/.exec(segment))) segment = `:${match[1]}`;
|
|
21
|
+
if (segment) route.push(segment);
|
|
22
|
+
}
|
|
23
|
+
return `/${route.join("/")}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function extractEndpointsFromText(text, file) {
|
|
27
|
+
const out = [];
|
|
28
|
+
const py = /\.py$/i.test(file), rust = /\.rs$/i.test(file), java = /\.java$/i.test(file);
|
|
29
|
+
const scanText = maskComments(text, { hashComments: py });
|
|
30
|
+
const add = (method, path, expr, index) => {
|
|
31
|
+
const normalizedPath = cleanPath(path);
|
|
32
|
+
const normalizedMethod = String(method || "ANY").toUpperCase();
|
|
33
|
+
if (!looksLikePath(normalizedPath) || !HTTP_METHODS.has(normalizedMethod)) return;
|
|
34
|
+
const handler = handlerName(expr);
|
|
35
|
+
const handlerRef = handlerReference(expr);
|
|
36
|
+
out.push({ method: normalizedMethod, path: normalizedPath, handler, ...(handlerRef ? { handlerRef } : {}), file, line: lineAt(text, index) });
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const nextPath = nextRoutePath(file);
|
|
40
|
+
if (nextPath) {
|
|
41
|
+
const seen = new Set();
|
|
42
|
+
const direct = /\bexport\s+(?:(?:async|declare)\s+)*(?:function\s+|(?:const|let|var)\s+)(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\b/g;
|
|
43
|
+
let match;
|
|
44
|
+
while ((match = direct.exec(scanText))) {
|
|
45
|
+
const method = match[1].toUpperCase();
|
|
46
|
+
if (!seen.has(method)) { seen.add(method); add(method, nextPath, method, match.index); }
|
|
47
|
+
}
|
|
48
|
+
const lists = /\bexport\s*\{([^}]+)\}/g;
|
|
49
|
+
while ((match = lists.exec(scanText))) {
|
|
50
|
+
for (const item of match[1].split(",")) {
|
|
51
|
+
const methodMatch = /^\s*([A-Za-z_$][\w$]*)(?:\s+as\s+(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS))?\s*$/.exec(item);
|
|
52
|
+
if (!methodMatch || (!methodMatch[2] && !HTTP_METHODS.has(methodMatch[1]))) continue;
|
|
53
|
+
const method = String(methodMatch[2] || methodMatch[1]).toUpperCase();
|
|
54
|
+
if (HTTP_METHODS.has(method) && !seen.has(method)) { seen.add(method); add(method, nextPath, methodMatch[1], match.index); }
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (rust) extractRustEndpoints(scanText, add);
|
|
60
|
+
if (java) {
|
|
61
|
+
out.push(...extractSpringEndpoints(scanText, file));
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const objKey = /(["'`])(\/[^"'`]*)\1\s*:\s*(\{)?/g;
|
|
66
|
+
let match;
|
|
67
|
+
while ((match = objKey.exec(scanText))) {
|
|
68
|
+
const path = match[2], keyIndex = match.index;
|
|
69
|
+
if (match[3]) {
|
|
70
|
+
let cursor = objKey.lastIndex, depth = 1;
|
|
71
|
+
const start = cursor;
|
|
72
|
+
while (cursor < scanText.length && depth > 0) {
|
|
73
|
+
if (scanText[cursor] === "{") depth++;
|
|
74
|
+
else if (scanText[cursor] === "}") depth--;
|
|
75
|
+
cursor++;
|
|
76
|
+
}
|
|
77
|
+
const body = scanText.slice(start, cursor - 1);
|
|
78
|
+
objKey.lastIndex = cursor;
|
|
79
|
+
if (OPENAPI_BLOCK.test(body)) continue;
|
|
80
|
+
const methodEntry = /\b(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\b\s*:\s*([^,\n}]+)/gi;
|
|
81
|
+
let entry;
|
|
82
|
+
while ((entry = methodEntry.exec(body))) add(entry[1], path, entry[2], keyIndex);
|
|
83
|
+
} else {
|
|
84
|
+
const expression = /^([^,\n}]+)/.exec(scanText.slice(objKey.lastIndex, objKey.lastIndex + 200));
|
|
85
|
+
if (expression && !/^\s*(?:\{|["'`\[]|[-+]?\d|true\b|false\b|null\b|undefined\b)/i.test(expression[1])) add("ANY", path, expression[1], keyIndex);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const call = /(?<!@)\b([\w$]+)\s*\.\s*(get|post|put|patch|delete|head|options|all)\s*\(\s*(["'`])(\/[^"'`]*)\3\s*(?:,\s*([\s\S]{0,160}?))?\)/gi;
|
|
90
|
+
while ((match = call.exec(scanText))) {
|
|
91
|
+
const handler = String(match[5] || "").trim();
|
|
92
|
+
if (!HTTP_CLIENT_CALLER.test(match[1]) && handler && handler[0] !== "{") add(match[2], match[4], match[5], match.index);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const go = /\.\s*(?:HandleFunc|Handle)\s*\(\s*(["'`])(\/[^"'`]*)\1\s*,\s*([\s\S]{0,120}?)\)/g;
|
|
96
|
+
while ((match = go.exec(scanText))) add("ANY", match[2], match[3], match.index);
|
|
97
|
+
|
|
98
|
+
if (py || /\.(?:[cm]?[jt]sx?)$/i.test(file)) {
|
|
99
|
+
const decorator = /@[\w$]*\.?\s*(get|post|put|patch|delete|head|options)\s*\(\s*(["'`])(\/[^"'`]*)\2/gi;
|
|
100
|
+
while ((match = decorator.exec(scanText))) {
|
|
101
|
+
const after = scanText.slice(decorator.lastIndex, decorator.lastIndex + 200);
|
|
102
|
+
const fn = /\b(?:def|async\s+def|function|const|export\s+function)\s+([A-Za-z_$][\w$]*)/.exec(after);
|
|
103
|
+
add(match[1], match[3], fn ? fn[1] : "", match.index);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { safeRead } from "../../util.js";
|
|
2
|
+
import { createRepoBoundary } from "../../repo-path.js";
|
|
3
|
+
import { MAX_ENDPOINT_FILES, MAX_ENDPOINTS, normalizedFile } from "./common.js";
|
|
4
|
+
import { extractEndpointsFromText, nextRoutePath } from "./extract.js";
|
|
5
|
+
import { joinEndpointPath, mountedBasePaths } from "./mounts.js";
|
|
6
|
+
|
|
7
|
+
const normParamKey = (path) => String(path).replace(/\{([^/}]+)\}/g, ":$1");
|
|
8
|
+
const sortEndpoints = (left, right) => left.path.localeCompare(right.path) || left.method.localeCompare(right.method);
|
|
9
|
+
|
|
10
|
+
function preferEndpoint(candidate, current) {
|
|
11
|
+
if (Boolean(candidate.handler) !== Boolean(current.handler)) return Boolean(candidate.handler);
|
|
12
|
+
return Number(candidate.path.includes("{")) < Number(current.path.includes("{"));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function analyzeEndpointInventory(repoPath, codeFiles) {
|
|
16
|
+
const files = (codeFiles || []).slice(0, MAX_ENDPOINT_FILES);
|
|
17
|
+
const byKey = new Map();
|
|
18
|
+
const declarations = new Map();
|
|
19
|
+
const boundary = createRepoBoundary(repoPath);
|
|
20
|
+
const sources = new Map();
|
|
21
|
+
const eligibleFiles = [];
|
|
22
|
+
for (const file of files) {
|
|
23
|
+
const relative = normalizedFile(file.path || file);
|
|
24
|
+
if (!/\.(?:[cm]?[jt]sx?|py|go|rs|java)$/i.test(relative)) continue;
|
|
25
|
+
const resolved = boundary.resolve(relative);
|
|
26
|
+
if (!resolved.ok) continue;
|
|
27
|
+
sources.set(relative, safeRead(resolved.path) || "");
|
|
28
|
+
eligibleFiles.push(relative);
|
|
29
|
+
}
|
|
30
|
+
const mountAnalysis = mountedBasePaths(eligibleFiles, sources);
|
|
31
|
+
let truncated = false;
|
|
32
|
+
for (const relative of eligibleFiles) {
|
|
33
|
+
const text = sources.get(relative);
|
|
34
|
+
const routeSignal = /["'`]\/|\.(?:get|post|put|patch|delete)\s*\(|HandleFunc|@\w*\.?(?:get|post|put|patch|delete)|@(?:[\w$]+\.)*(?:Request|Get|Post|Put|Patch|Delete)Mapping\b/i;
|
|
35
|
+
if (!text || (!nextRoutePath(relative) && !routeSignal.test(text))) continue;
|
|
36
|
+
for (const declaration of extractEndpointsFromText(text, relative)) {
|
|
37
|
+
const declarationKey = `${declaration.file}\0${declaration.line}\0${declaration.method}\0${normParamKey(declaration.path)}`;
|
|
38
|
+
if (!declarations.has(declarationKey)) declarations.set(declarationKey, declaration);
|
|
39
|
+
const bases = mountAnalysis.paths.get(relative) || [{ path: "", chain: [] }];
|
|
40
|
+
for (const base of bases) {
|
|
41
|
+
const path = base.path ? joinEndpointPath(base.path, declaration.path) : declaration.path;
|
|
42
|
+
const endpoint = {
|
|
43
|
+
...declaration,
|
|
44
|
+
declaredPath: declaration.path,
|
|
45
|
+
path,
|
|
46
|
+
mountState: base.chain.length ? "COMPOSED_STATIC" : "DECLARED_LOCAL",
|
|
47
|
+
confidence: base.chain.length ? "high" : "medium",
|
|
48
|
+
mountChain: base.chain,
|
|
49
|
+
...(base.path ? { localPath: declaration.path } : {}),
|
|
50
|
+
};
|
|
51
|
+
const key = `${endpoint.method} ${normParamKey(endpoint.path)}`;
|
|
52
|
+
const current = byKey.get(key);
|
|
53
|
+
if (!current || preferEndpoint(endpoint, current)) byKey.set(key, endpoint);
|
|
54
|
+
if (byKey.size >= MAX_ENDPOINTS) { truncated = true; break; }
|
|
55
|
+
}
|
|
56
|
+
if (truncated) break;
|
|
57
|
+
}
|
|
58
|
+
if (truncated) break;
|
|
59
|
+
}
|
|
60
|
+
const endpoints = [...byKey.values()].sort(sortEndpoints);
|
|
61
|
+
const composed = endpoints.filter((endpoint) => endpoint.mountChain.length).length;
|
|
62
|
+
return {
|
|
63
|
+
endpoints,
|
|
64
|
+
declarations: [...declarations.values()].sort(sortEndpoints),
|
|
65
|
+
mounts: mountAnalysis.mounts,
|
|
66
|
+
stats: {
|
|
67
|
+
scannedFiles: eligibleFiles.length,
|
|
68
|
+
declaredRoutes: declarations.size,
|
|
69
|
+
emittedRoutes: endpoints.length,
|
|
70
|
+
reachableRoutes: composed,
|
|
71
|
+
reachableStaticRoutes: composed,
|
|
72
|
+
composedRoutes: composed,
|
|
73
|
+
localRoutes: endpoints.length - composed,
|
|
74
|
+
localDeclarations: endpoints.length - composed,
|
|
75
|
+
staticMounts: mountAnalysis.mounts.length,
|
|
76
|
+
truncated,
|
|
77
|
+
maxEndpoints: MAX_ENDPOINTS,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function detectEndpoints(repoPath, codeFiles) {
|
|
83
|
+
return analyzeEndpointInventory(repoPath, codeFiles).endpoints;
|
|
84
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
import { cleanPath, lineAt, maskComments } from "./common.js";
|
|
3
|
+
|
|
4
|
+
const JS_ROUTE_EXTENSIONS = [".js", ".ts", ".tsx", ".jsx", ".cjs", ".mjs"];
|
|
5
|
+
|
|
6
|
+
function importBindings(text) {
|
|
7
|
+
const bindings = new Map();
|
|
8
|
+
const add = (name, specifier) => {
|
|
9
|
+
if (/^[A-Za-z_$][\w$]*$/.test(name || "") && typeof specifier === "string") bindings.set(name, specifier);
|
|
10
|
+
};
|
|
11
|
+
let match;
|
|
12
|
+
const direct = /\bimport\s+([A-Za-z_$][\w$]*)\s*(?:,\s*\{[^}]*\})?\s+from\s*(["'`])([^"'`]+)\2/g;
|
|
13
|
+
while ((match = direct.exec(text))) add(match[1], match[3]);
|
|
14
|
+
const namespace = /\bimport\s+\*\s+as\s+([A-Za-z_$][\w$]*)\s+from\s*(["'`])([^"'`]+)\2/g;
|
|
15
|
+
while ((match = namespace.exec(text))) add(match[1], match[3]);
|
|
16
|
+
const named = /\bimport\s*\{([^}]+)\}\s*from\s*(["'`])([^"'`]+)\2/g;
|
|
17
|
+
while ((match = named.exec(text))) {
|
|
18
|
+
for (const item of match[1].split(",")) {
|
|
19
|
+
const binding = /^\s*[A-Za-z_$][\w$]*(?:\s+as\s+([A-Za-z_$][\w$]*))?\s*$/.exec(item);
|
|
20
|
+
if (binding) add(binding[1] || item.trim(), match[3]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const commonJs = /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*require\s*\(\s*(["'`])([^"'`]+)\2\s*\)/g;
|
|
24
|
+
while ((match = commonJs.exec(text))) add(match[1], match[3]);
|
|
25
|
+
return bindings;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function resolveImportedFile(importer, specifier, availableFiles) {
|
|
29
|
+
if (!specifier?.startsWith(".")) return null;
|
|
30
|
+
const base = posix.normalize(posix.join(posix.dirname(importer), specifier));
|
|
31
|
+
const candidates = [base];
|
|
32
|
+
if (!JS_ROUTE_EXTENSIONS.some((extension) => base.endsWith(extension))) {
|
|
33
|
+
for (const extension of JS_ROUTE_EXTENSIONS) candidates.push(`${base}${extension}`);
|
|
34
|
+
for (const extension of JS_ROUTE_EXTENSIONS) candidates.push(`${base}/index${extension}`);
|
|
35
|
+
}
|
|
36
|
+
return candidates.find((candidate) => availableFiles.has(candidate)) || null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function callArguments(text, openParen) {
|
|
40
|
+
let quote = "", escaped = false, depth = 0, start = openParen + 1;
|
|
41
|
+
const args = [];
|
|
42
|
+
for (let index = openParen; index < text.length; index++) {
|
|
43
|
+
const char = text[index];
|
|
44
|
+
if (quote) {
|
|
45
|
+
if (escaped) escaped = false;
|
|
46
|
+
else if (char === "\\") escaped = true;
|
|
47
|
+
else if (char === quote) quote = "";
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (char === '"' || char === "'" || char === "`") { quote = char; continue; }
|
|
51
|
+
if (char === "(") { depth++; continue; }
|
|
52
|
+
if (char === ")") {
|
|
53
|
+
depth--;
|
|
54
|
+
if (depth === 0) {
|
|
55
|
+
args.push(text.slice(start, index).trim());
|
|
56
|
+
return { args, end: index + 1 };
|
|
57
|
+
}
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (char === "," && depth === 1) {
|
|
61
|
+
args.push(text.slice(start, index).trim());
|
|
62
|
+
start = index + 1;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function routerMounts(text, file, availableFiles) {
|
|
69
|
+
const scanText = maskComments(text);
|
|
70
|
+
const bindings = importBindings(scanText);
|
|
71
|
+
const mounts = [];
|
|
72
|
+
const useCall = /\b[A-Za-z_$][\w$]*\s*\.\s*use\s*\(/g;
|
|
73
|
+
let match;
|
|
74
|
+
while ((match = useCall.exec(scanText))) {
|
|
75
|
+
const parsed = callArguments(scanText, scanText.indexOf("(", match.index));
|
|
76
|
+
if (!parsed) continue;
|
|
77
|
+
useCall.lastIndex = parsed.end;
|
|
78
|
+
const args = parsed.args.filter(Boolean);
|
|
79
|
+
if (!args.length) continue;
|
|
80
|
+
const literal = /^(["'`])(\/[^"'`]*)\1$/.exec(args[0]);
|
|
81
|
+
const mountPath = literal ? cleanPath(literal[2]) : "/";
|
|
82
|
+
const identifier = /^([A-Za-z_$][\w$]*)(?:\.[A-Za-z_$][\w$]*)?$/.exec(args.at(-1) || "")?.[1];
|
|
83
|
+
const child = resolveImportedFile(file, identifier ? bindings.get(identifier) : null, availableFiles);
|
|
84
|
+
if (child && child !== file) mounts.push({ parent: file, child, path: mountPath, line: lineAt(text, match.index) });
|
|
85
|
+
}
|
|
86
|
+
return mounts;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function joinEndpointPath(base, route) {
|
|
90
|
+
const left = cleanPath(base || "/");
|
|
91
|
+
const right = cleanPath(route || "/");
|
|
92
|
+
if (left === "/") return right;
|
|
93
|
+
if (right === "/") return left;
|
|
94
|
+
return cleanPath(`${left}/${right.replace(/^\/+/, "")}`.replace(/\/{2,}/g, "/"));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function mountedBasePaths(files, sources) {
|
|
98
|
+
const incoming = new Map();
|
|
99
|
+
const mounts = [];
|
|
100
|
+
const available = new Set(files);
|
|
101
|
+
for (const file of files) {
|
|
102
|
+
for (const mount of routerMounts(sources.get(file) || "", file, available)) {
|
|
103
|
+
mounts.push(mount);
|
|
104
|
+
if (!incoming.has(mount.child)) incoming.set(mount.child, []);
|
|
105
|
+
incoming.get(mount.child).push(mount);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const cache = new Map();
|
|
109
|
+
const resolve = (file, stack = new Set()) => {
|
|
110
|
+
if (cache.has(file)) return cache.get(file);
|
|
111
|
+
if (stack.has(file)) return [];
|
|
112
|
+
const parents = incoming.get(file) || [];
|
|
113
|
+
if (!parents.length) return [{ path: "", chain: [] }];
|
|
114
|
+
const paths = [];
|
|
115
|
+
for (const mount of parents) {
|
|
116
|
+
for (const base of resolve(mount.parent, new Set(stack).add(file))) {
|
|
117
|
+
const path = joinEndpointPath(base.path, mount.path);
|
|
118
|
+
const key = `${path}\0${base.chain.map((item) => `${item.file}:${item.line}:${item.path}`).join("|")}\0${mount.parent}:${mount.line}:${mount.path}`;
|
|
119
|
+
if (!paths.some((item) => item.key === key)) paths.push({ key, path, chain: [...base.chain, { file: mount.parent, line: mount.line, path: mount.path, child: mount.child }] });
|
|
120
|
+
if (paths.length >= 32) break;
|
|
121
|
+
}
|
|
122
|
+
if (paths.length >= 32) break;
|
|
123
|
+
}
|
|
124
|
+
const result = paths.length ? paths.map(({ key: _key, ...item }) => item) : [{ path: "", chain: [] }];
|
|
125
|
+
cache.set(file, result);
|
|
126
|
+
return result;
|
|
127
|
+
};
|
|
128
|
+
return { paths: new Map(files.map((file) => [file, resolve(file)])), mounts };
|
|
129
|
+
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { maskJavaNonCode } from "./java-source.js";
|
|
5
5
|
|
|
6
6
|
const SPRING_MAPPING = /@(?:org\.springframework\.web\.bind\.annotation\.)?(RequestMapping|GetMapping|PostMapping|PutMapping|PatchMapping|DeleteMapping)\b/g;
|
|
7
|
+
const SPRING_CONDITION = /@(?:org\.springframework\.boot\.autoconfigure\.condition\.)?(ConditionalOnExpression|ConditionalOnProperty)\b/g;
|
|
7
8
|
const REQUEST_METHOD = /\bRequestMethod\s*\.\s*(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS|TRACE)\b/g;
|
|
8
9
|
|
|
9
10
|
const lineAt = (text, index) => {
|
|
@@ -119,6 +120,58 @@ function mappingMethods(name, args) {
|
|
|
119
120
|
return methods.length ? methods : ["ANY"];
|
|
120
121
|
}
|
|
121
122
|
|
|
123
|
+
function namedInitializer(args, name) {
|
|
124
|
+
const source = String(args || "");
|
|
125
|
+
const match = new RegExp(`\\b${name}\\s*=`).exec(source);
|
|
126
|
+
return match ? initializer(source, source.indexOf("=", match.index)) : "";
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function namedStrings(args, name) {
|
|
130
|
+
return stringLiterals(namedInitializer(args, name));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function namedBoolean(args, name, fallback) {
|
|
134
|
+
const value = namedInitializer(args, name).trim();
|
|
135
|
+
if (/^true\b/i.test(value)) return true;
|
|
136
|
+
if (/^false\b/i.test(value)) return false;
|
|
137
|
+
return fallback;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function positionalStrings(args) {
|
|
141
|
+
const source = String(args || "");
|
|
142
|
+
const start = source.trimStart()[0];
|
|
143
|
+
return start === '"' || start === "{" ? stringLiterals(initializer(source, -1)) : [];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function expressionDefaultActive(expression) {
|
|
147
|
+
const value = String(expression || "").trim();
|
|
148
|
+
if (/^(?:true|false)$/i.test(value)) return value.toLowerCase() === "true";
|
|
149
|
+
const match = /^\$\{[^{}:]+:(true|false)\}$/i.exec(value);
|
|
150
|
+
return match ? match[1].toLowerCase() === "true" : null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function conditionMetadata(annotation, args) {
|
|
154
|
+
if (annotation === "ConditionalOnExpression") {
|
|
155
|
+
const expression = namedStrings(args, "value")[0] || stringLiterals(args)[0] || "";
|
|
156
|
+
return {
|
|
157
|
+
type: annotation,
|
|
158
|
+
expression,
|
|
159
|
+
defaultActive: expressionDefaultActive(expression),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const namedProperties = [...namedStrings(args, "name"), ...namedStrings(args, "value")];
|
|
163
|
+
const properties = [...new Set(namedProperties.length ? namedProperties : positionalStrings(args))];
|
|
164
|
+
const matchIfMissing = namedBoolean(args, "matchIfMissing", false);
|
|
165
|
+
return {
|
|
166
|
+
type: annotation,
|
|
167
|
+
prefix: namedStrings(args, "prefix")[0] || "",
|
|
168
|
+
properties,
|
|
169
|
+
havingValue: namedStrings(args, "havingValue")[0] || "",
|
|
170
|
+
matchIfMissing,
|
|
171
|
+
defaultActive: matchIfMissing,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
122
175
|
function joinPaths(prefix, path) {
|
|
123
176
|
const left = String(prefix || "").trim().replace(/^\/+|\/+$/g, "");
|
|
124
177
|
const right = String(path || "").trim().replace(/^\/+|\/+$/g, "");
|
|
@@ -135,10 +188,10 @@ function declarationAfter(text, start) {
|
|
|
135
188
|
if (classMatch) {
|
|
136
189
|
const open = bounded.indexOf("{");
|
|
137
190
|
const bodyOpen = open < 0 ? -1 : declarationStart + open;
|
|
138
|
-
return { kind: "class", name: classMatch[2], bodyOpen, bodyClose: bodyOpen < 0 ? text.length : balancedEnd(text, bodyOpen, "{", "}") };
|
|
191
|
+
return { kind: "class", name: classMatch[2], start: declarationStart, bodyOpen, bodyClose: bodyOpen < 0 ? text.length : balancedEnd(text, bodyOpen, "{", "}") };
|
|
139
192
|
}
|
|
140
193
|
const methodMatch = /\b([A-Za-z_$][\w$]*)\s*\(/.exec(head);
|
|
141
|
-
return methodMatch ? { kind: "method", name: methodMatch[1] } : { kind: "unknown", name: "" };
|
|
194
|
+
return methodMatch ? { kind: "method", name: methodMatch[1], start: declarationStart } : { kind: "unknown", name: "", start: declarationStart };
|
|
142
195
|
}
|
|
143
196
|
|
|
144
197
|
export function extractSpringEndpoints(text, file) {
|
|
@@ -160,6 +213,19 @@ export function extractSpringEndpoints(text, file) {
|
|
|
160
213
|
});
|
|
161
214
|
SPRING_MAPPING.lastIndex = Math.max(SPRING_MAPPING.lastIndex, invocation.end);
|
|
162
215
|
}
|
|
216
|
+
const conditions = [];
|
|
217
|
+
SPRING_CONDITION.lastIndex = 0;
|
|
218
|
+
while ((match = SPRING_CONDITION.exec(code))) {
|
|
219
|
+
const invocation = annotationInvocation(text, SPRING_CONDITION.lastIndex);
|
|
220
|
+
const declaration = declarationAfter(code, invocation.end);
|
|
221
|
+
conditions.push({
|
|
222
|
+
index: match.index,
|
|
223
|
+
line: lineAt(text, match.index),
|
|
224
|
+
declaration,
|
|
225
|
+
...conditionMetadata(match[1], invocation.args),
|
|
226
|
+
});
|
|
227
|
+
SPRING_CONDITION.lastIndex = Math.max(SPRING_CONDITION.lastIndex, invocation.end);
|
|
228
|
+
}
|
|
163
229
|
|
|
164
230
|
const classMappings = mappings
|
|
165
231
|
.filter((item) => item.declaration.kind === "class")
|
|
@@ -176,9 +242,20 @@ export function extractSpringEndpoints(text, file) {
|
|
|
176
242
|
const prefixes = owner?.paths?.length ? owner.paths : [""];
|
|
177
243
|
const paths = mappingPaths(item.args);
|
|
178
244
|
if (!paths.length) continue; // literal path was requested but could not be resolved
|
|
245
|
+
const activationConditions = conditions.filter((condition) => condition.declaration.start === item.declaration.start
|
|
246
|
+
|| (owner && condition.declaration.start === owner.declaration.start));
|
|
247
|
+
const activation = activationConditions.length
|
|
248
|
+
? {
|
|
249
|
+
conditional: true,
|
|
250
|
+
defaultActive: activationConditions.some((condition) => condition.defaultActive === false)
|
|
251
|
+
? false
|
|
252
|
+
: activationConditions.every((condition) => condition.defaultActive === true) ? true : null,
|
|
253
|
+
conditions: activationConditions.map(({ declaration: _declaration, index: _index, ...condition }) => condition),
|
|
254
|
+
}
|
|
255
|
+
: {};
|
|
179
256
|
for (const method of mappingMethods(item.annotation, item.args)) {
|
|
180
257
|
for (const prefix of prefixes) for (const path of paths) {
|
|
181
|
-
out.push({ method, path: joinPaths(prefix, path), handler: item.declaration.name, file, line: item.line });
|
|
258
|
+
out.push({ method, path: joinPaths(prefix, path), handler: item.declaration.name, file, line: item.line, ...activation });
|
|
182
259
|
}
|
|
183
260
|
}
|
|
184
261
|
}
|