codegraph-engine 2.3.0__py3-none-any.whl → 3.0.1__py3-none-any.whl
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.
- codegraph/__init__.py +1 -1
- codegraph/__main__.py +7 -0
- codegraph/agent_brain.py +253 -2
- codegraph/agent_capabilities.py +1348 -67
- codegraph/agent_rules.py +2 -1
- codegraph/api_drift.py +455 -0
- codegraph/architecture_rules.py +123 -0
- codegraph/async_queue/__init__.py +50 -0
- codegraph/async_queue/detectors/__init__.py +48 -0
- codegraph/async_queue/detectors/celery.py +160 -0
- codegraph/async_queue/detectors/kafka.py +168 -0
- codegraph/async_queue/detectors/rabbitmq.py +116 -0
- codegraph/async_queue/detectors/redis.py +130 -0
- codegraph/async_queue/detectors/sqs.py +135 -0
- codegraph/async_queue/models.py +161 -0
- codegraph/async_queue/otel_models.py +74 -0
- codegraph/async_queue/otel_parser.py +337 -0
- codegraph/async_queue/reconciler.py +427 -0
- codegraph/async_queue/scanner.py +291 -0
- codegraph/async_queue/traversal.py +447 -0
- codegraph/change_impact.py +320 -110
- codegraph/changelog.py +170 -0
- codegraph/cli.py +1605 -62
- codegraph/cli_output.py +17 -2
- codegraph/context.py +135 -24
- codegraph/context_freshness.py +2 -0
- codegraph/database/__init__.py +14 -0
- codegraph/database/extractor.py +712 -17
- codegraph/database/interrogation.py +388 -2
- codegraph/database/schema_drift.py +862 -0
- codegraph/dataflow_propagation.py +118 -0
- codegraph/dead_code.py +387 -0
- codegraph/epistemic.py +15 -0
- codegraph/evidence_contract.py +34 -3
- codegraph/federation.py +245 -0
- codegraph/frameworks.py +234 -12
- codegraph/frameworks_nestjs.py +502 -0
- codegraph/frameworks_nextjs.py +337 -0
- codegraph/frameworks_react.py +457 -0
- codegraph/freshness.py +2 -0
- codegraph/frontend_html.py +109 -0
- codegraph/git.py +2 -0
- codegraph/git_state.py +2 -0
- codegraph/graph/traversal.py +150 -7
- codegraph/graph_csr.py +218 -0
- codegraph/incremental.py +150 -0
- codegraph/indexing/__init__.py +7 -1
- codegraph/indexing/classifier.py +5 -0
- codegraph/indexing/indexer.py +292 -38
- codegraph/indexing/languages/__init__.py +1 -0
- codegraph/indexing/languages/cpp.py +457 -0
- codegraph/indexing/languages/go.py +459 -0
- codegraph/indexing/languages/jvm.py +583 -0
- codegraph/indexing/languages/rust.py +507 -0
- codegraph/indexing/models.py +15 -1
- codegraph/indexing/parser.py +385 -9
- codegraph/indexing/pool.py +136 -0
- codegraph/indexing/protocols.py +220 -0
- codegraph/indexing/scanner.py +23 -0
- codegraph/indexing/templates.py +418 -0
- codegraph/interrogation.py +207 -39
- codegraph/lsp.py +318 -0
- codegraph/lsp_bridge/__init__.py +16 -0
- codegraph/lsp_bridge/bridge.py +102 -0
- codegraph/lsp_bridge/client.py +207 -0
- codegraph/lsp_bridge/models.py +71 -0
- codegraph/mcp/server.py +502 -29
- codegraph/memory/trajectory.py +282 -0
- codegraph/models.py +7 -0
- codegraph/monorepo.py +3 -0
- codegraph/multi_agent/__init__.py +13 -0
- codegraph/multi_agent/coordinator.py +229 -0
- codegraph/multi_agent/models.py +76 -0
- codegraph/optimizer.py +42 -5
- codegraph/polymorphism.py +95 -0
- codegraph/pr_review.py +296 -0
- codegraph/process_lifecycle.py +50 -12
- codegraph/reachability.py +198 -0
- codegraph/reachability_index.py +74 -0
- codegraph/refactor/__init__.py +48 -0
- codegraph/refactor/models.py +114 -0
- codegraph/refactor/patcher.py +348 -0
- codegraph/refactor/renamer.py +844 -0
- codegraph/refactor/transactions.py +414 -0
- codegraph/repair.py +233 -0
- codegraph/resolver.py +1357 -161
- codegraph/retrieval_policy.py +22 -1
- codegraph/roblox.py +2174 -0
- codegraph/route_composer.py +7 -0
- codegraph/route_topology.py +172 -0
- codegraph/runtime/__init__.py +25 -0
- codegraph/runtime/collector.py +155 -0
- codegraph/runtime/db_watcher.py +191 -0
- codegraph/runtime/ingestor.py +30 -10
- codegraph/runtime/otel_receiver.py +208 -0
- codegraph/runtime/port_inspector.py +304 -0
- codegraph/runtime/runner.py +155 -6
- codegraph/runtime/trace_linker.py +175 -0
- codegraph/sandbox.py +215 -0
- codegraph/search/hybrid.py +26 -0
- codegraph/security/supply_chain.py +188 -0
- codegraph/security/taint.py +421 -0
- codegraph/semantic_decorators.py +34 -3
- codegraph/structural_diff.py +2 -0
- codegraph/styles.py +144 -0
- codegraph/symbol_history.py +2 -0
- codegraph/test_gen.py +437 -0
- codegraph/tsconfig.py +236 -0
- codegraph/ui/__init__.py +6 -0
- codegraph/ui/server.py +876 -0
- codegraph/watcher.py +323 -0
- {codegraph_engine-2.3.0.dist-info → codegraph_engine-3.0.1.dist-info}/METADATA +365 -67
- codegraph_engine-3.0.1.dist-info/RECORD +164 -0
- {codegraph_engine-2.3.0.dist-info → codegraph_engine-3.0.1.dist-info}/WHEEL +1 -1
- codegraph_engine-2.3.0.dist-info/RECORD +0 -94
- {codegraph_engine-2.3.0.dist-info → codegraph_engine-3.0.1.dist-info}/entry_points.txt +0 -0
- {codegraph_engine-2.3.0.dist-info → codegraph_engine-3.0.1.dist-info}/licenses/LICENSE +0 -0
- {codegraph_engine-2.3.0.dist-info → codegraph_engine-3.0.1.dist-info}/top_level.txt +0 -0
codegraph/federation.py
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"""Cross-Repository & Microservice Graph Federation Engine for CodeGraph.
|
|
2
|
+
|
|
3
|
+
Discovers multi-repo workspaces (via codegraph.workspace.json or directory discovery),
|
|
4
|
+
interrogates individual repository SQLite indices, and deterministically resolves cross-service
|
|
5
|
+
HTTP/REST routes, gRPC endpoints, and service-to-service call topologies across repositories.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import re
|
|
11
|
+
import sqlite3
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from codegraph.indexing.indexer import Indexer
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class FederatedRepoInfo:
|
|
21
|
+
name: str
|
|
22
|
+
path: str
|
|
23
|
+
db_path: str
|
|
24
|
+
symbol_count: int
|
|
25
|
+
route_count: int
|
|
26
|
+
|
|
27
|
+
def as_dict(self) -> dict[str, Any]:
|
|
28
|
+
return {
|
|
29
|
+
"name": self.name,
|
|
30
|
+
"path": self.path,
|
|
31
|
+
"db_path": self.db_path,
|
|
32
|
+
"symbol_count": self.symbol_count,
|
|
33
|
+
"route_count": self.route_count,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass
|
|
38
|
+
class FederatedRouteLink:
|
|
39
|
+
source_repo: str
|
|
40
|
+
source_file: str
|
|
41
|
+
source_symbol: str
|
|
42
|
+
http_method: str
|
|
43
|
+
route_path: str
|
|
44
|
+
target_repo: str
|
|
45
|
+
target_handler: str
|
|
46
|
+
target_file: str
|
|
47
|
+
confidence: str = "STATIC_VERIFIED"
|
|
48
|
+
|
|
49
|
+
def as_dict(self) -> dict[str, Any]:
|
|
50
|
+
return {
|
|
51
|
+
"source_repo": self.source_repo,
|
|
52
|
+
"source_file": self.source_file,
|
|
53
|
+
"source_symbol": self.source_symbol,
|
|
54
|
+
"http_method": self.http_method,
|
|
55
|
+
"route_path": self.route_path,
|
|
56
|
+
"target_repo": self.target_repo,
|
|
57
|
+
"target_handler": self.target_handler,
|
|
58
|
+
"target_file": self.target_file,
|
|
59
|
+
"confidence": self.confidence,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass
|
|
64
|
+
class FederationReport:
|
|
65
|
+
workspace_root: str
|
|
66
|
+
repos: list[FederatedRepoInfo] = field(default_factory=list)
|
|
67
|
+
cross_repo_links: list[FederatedRouteLink] = field(default_factory=list)
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def total_repos(self) -> int:
|
|
71
|
+
return len(self.repos)
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def total_links(self) -> int:
|
|
75
|
+
return len(self.cross_repo_links)
|
|
76
|
+
|
|
77
|
+
def as_dict(self) -> dict[str, Any]:
|
|
78
|
+
return {
|
|
79
|
+
"workspace_root": self.workspace_root,
|
|
80
|
+
"repos": [r.as_dict() for r in self.repos],
|
|
81
|
+
"cross_repo_links": [link.as_dict() for link in self.cross_repo_links],
|
|
82
|
+
"total_repos": len(self.repos),
|
|
83
|
+
"total_links": len(self.cross_repo_links),
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def discover_federated_repos(workspace_root: Path) -> list[Path]:
|
|
88
|
+
"""Discover repositories in workspace via codegraph.workspace.json or directory search."""
|
|
89
|
+
root = workspace_root.resolve()
|
|
90
|
+
manifest = root / "codegraph.workspace.json"
|
|
91
|
+
|
|
92
|
+
if manifest.is_file():
|
|
93
|
+
try:
|
|
94
|
+
data = json.loads(manifest.read_text(encoding="utf-8"))
|
|
95
|
+
repo_paths = data.get("repositories", [])
|
|
96
|
+
valid_paths: list[Path] = []
|
|
97
|
+
for rp in repo_paths:
|
|
98
|
+
p = (root / rp).resolve()
|
|
99
|
+
if p.is_dir():
|
|
100
|
+
valid_paths.append(p)
|
|
101
|
+
if valid_paths:
|
|
102
|
+
return valid_paths
|
|
103
|
+
except Exception:
|
|
104
|
+
pass
|
|
105
|
+
|
|
106
|
+
# Discover child directories containing .git or .codegraph.sqlite3
|
|
107
|
+
repos: list[Path] = []
|
|
108
|
+
for item in sorted(root.iterdir()):
|
|
109
|
+
if item.is_dir() and not item.name.startswith("."):
|
|
110
|
+
if (item / ".git").is_dir() or (item / ".codegraph.sqlite3").is_file():
|
|
111
|
+
repos.append(item)
|
|
112
|
+
|
|
113
|
+
# If workspace_root itself is a repo, include it
|
|
114
|
+
if (root / ".git").is_dir() or (root / ".codegraph.sqlite3").is_file():
|
|
115
|
+
if root not in repos:
|
|
116
|
+
repos.insert(0, root)
|
|
117
|
+
|
|
118
|
+
return repos
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def federate_workspace(workspace_root: Path) -> FederationReport:
|
|
122
|
+
"""Analyze a multi-repository workspace, compiling cross-repository routes and links."""
|
|
123
|
+
root = workspace_root.resolve()
|
|
124
|
+
repo_dirs = discover_federated_repos(root)
|
|
125
|
+
|
|
126
|
+
repo_infos: list[FederatedRepoInfo] = []
|
|
127
|
+
# repo_name -> list of routes
|
|
128
|
+
repo_routes: dict[str, list[dict[str, Any]]] = {}
|
|
129
|
+
|
|
130
|
+
for r_dir in repo_dirs:
|
|
131
|
+
indexer = Indexer(r_dir)
|
|
132
|
+
db_path = indexer.db_path
|
|
133
|
+
if not db_path.is_file():
|
|
134
|
+
continue
|
|
135
|
+
|
|
136
|
+
try:
|
|
137
|
+
with sqlite3.connect(f"file:{db_path}?mode=ro", uri=True) as con:
|
|
138
|
+
con.row_factory = sqlite3.Row
|
|
139
|
+
sym_cnt = con.execute("SELECT COUNT(*) FROM symbols").fetchone()[0]
|
|
140
|
+
routes_cur = con.execute(
|
|
141
|
+
"SELECT http_method, route_path, handler_name, file_path, line FROM framework_routes"
|
|
142
|
+
).fetchall()
|
|
143
|
+
route_cnt = len(routes_cur)
|
|
144
|
+
|
|
145
|
+
r_name = r_dir.name
|
|
146
|
+
repo_infos.append(
|
|
147
|
+
FederatedRepoInfo(
|
|
148
|
+
name=r_name,
|
|
149
|
+
path=str(r_dir),
|
|
150
|
+
db_path=str(db_path),
|
|
151
|
+
symbol_count=sym_cnt,
|
|
152
|
+
route_count=route_cnt,
|
|
153
|
+
)
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
repo_routes[r_name] = [
|
|
157
|
+
{
|
|
158
|
+
"http_method": row["http_method"],
|
|
159
|
+
"route_path": row["route_path"],
|
|
160
|
+
"handler_name": row["handler_name"],
|
|
161
|
+
"file_path": row["file_path"],
|
|
162
|
+
"line": row["line"],
|
|
163
|
+
}
|
|
164
|
+
for row in routes_cur
|
|
165
|
+
]
|
|
166
|
+
except Exception:
|
|
167
|
+
continue
|
|
168
|
+
|
|
169
|
+
# Detect Cross-Repository Route Calls
|
|
170
|
+
# Search calls or code references matching HTTP endpoint paths across repositories
|
|
171
|
+
links: list[FederatedRouteLink] = []
|
|
172
|
+
|
|
173
|
+
# Map of normalized endpoint path -> list of (repo_name, route_dict)
|
|
174
|
+
endpoint_index: dict[str, list[tuple[str, dict[str, Any]]]] = {}
|
|
175
|
+
for r_name, r_list in repo_routes.items():
|
|
176
|
+
for r in r_list:
|
|
177
|
+
norm_path = _normalize_route_path(r["route_path"])
|
|
178
|
+
if norm_path and norm_path != "/":
|
|
179
|
+
endpoint_index.setdefault(norm_path, []).append((r_name, r))
|
|
180
|
+
|
|
181
|
+
# Inspect other repos for calls/fetches to these endpoint paths
|
|
182
|
+
for r_dir in repo_dirs:
|
|
183
|
+
indexer = Indexer(r_dir)
|
|
184
|
+
if not indexer.db_path.is_file():
|
|
185
|
+
continue
|
|
186
|
+
source_repo_name = r_dir.name
|
|
187
|
+
|
|
188
|
+
try:
|
|
189
|
+
with sqlite3.connect(f"file:{indexer.db_path}?mode=ro", uri=True) as con:
|
|
190
|
+
con.row_factory = sqlite3.Row
|
|
191
|
+
# Check calls table and text chunks for URLs matching known paths
|
|
192
|
+
for norm_path, target_list in endpoint_index.items():
|
|
193
|
+
# Look for callers containing norm_path
|
|
194
|
+
matching_chunks = con.execute(
|
|
195
|
+
"SELECT path, symbol, content FROM chunks WHERE content LIKE ? LIMIT 5",
|
|
196
|
+
(f"%{norm_path}%",),
|
|
197
|
+
).fetchall()
|
|
198
|
+
|
|
199
|
+
for chunk in matching_chunks:
|
|
200
|
+
for target_repo_name, target_route in target_list:
|
|
201
|
+
if target_repo_name == source_repo_name:
|
|
202
|
+
continue # Internal call within same repo
|
|
203
|
+
|
|
204
|
+
links.append(
|
|
205
|
+
FederatedRouteLink(
|
|
206
|
+
source_repo=source_repo_name,
|
|
207
|
+
source_file=chunk["path"],
|
|
208
|
+
source_symbol=chunk["symbol"] or "anonymous",
|
|
209
|
+
http_method=target_route["http_method"],
|
|
210
|
+
route_path=target_route["route_path"],
|
|
211
|
+
target_repo=target_repo_name,
|
|
212
|
+
target_handler=target_route["handler_name"],
|
|
213
|
+
target_file=f"{target_route['file_path']}:{target_route['line']}",
|
|
214
|
+
confidence="STATIC_VERIFIED",
|
|
215
|
+
)
|
|
216
|
+
)
|
|
217
|
+
except Exception:
|
|
218
|
+
continue
|
|
219
|
+
|
|
220
|
+
# Deduplicate links
|
|
221
|
+
seen: set[tuple[str, str, str, str, str]] = set()
|
|
222
|
+
deduped_links: list[FederatedRouteLink] = []
|
|
223
|
+
for item in links:
|
|
224
|
+
key = (item.source_repo, item.source_file, item.target_repo, item.http_method, item.route_path)
|
|
225
|
+
if key not in seen:
|
|
226
|
+
seen.add(key)
|
|
227
|
+
deduped_links.append(item)
|
|
228
|
+
|
|
229
|
+
return FederationReport(
|
|
230
|
+
workspace_root=str(root),
|
|
231
|
+
repos=repo_infos,
|
|
232
|
+
cross_repo_links=deduped_links,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _normalize_route_path(path: str) -> str:
|
|
237
|
+
"""Normalize route path for cross-repository matching, stripping hostnames/parameters."""
|
|
238
|
+
p = path.strip()
|
|
239
|
+
p = re.sub(r"^https?://[^/]+", "", p)
|
|
240
|
+
# Remove query string
|
|
241
|
+
p = p.split("?")[0]
|
|
242
|
+
# Replace path parameters {id} or :id with wildcard
|
|
243
|
+
p = re.sub(r"\{[^}]+\}", "*", p)
|
|
244
|
+
p = re.sub(r":\w+", "*", p)
|
|
245
|
+
return p.rstrip("/") or "/"
|
codegraph/frameworks.py
CHANGED
|
@@ -55,12 +55,12 @@ def normalize_route_path(route_path: str, framework: str) -> str:
|
|
|
55
55
|
if framework == "flask":
|
|
56
56
|
# Match <[type:]param>
|
|
57
57
|
return re.sub(r"<(?:\w+:)?([A-Za-z_][\w]*)>", r"{\1}", raw)
|
|
58
|
-
elif framework
|
|
58
|
+
elif framework in ("express", "nestjs"):
|
|
59
59
|
# Match :param
|
|
60
60
|
return re.sub(r":([A-Za-z_][\w]*)", r"{\1}", raw)
|
|
61
61
|
elif framework == "nextjs":
|
|
62
62
|
# Match [[...param]] or [...param] or [param]
|
|
63
|
-
return re.sub(r"\[(?:\.\.\.)?([A-Za-z_][\w]*)\]", r"{\1}", raw)
|
|
63
|
+
return re.sub(r"\[{1,2}(?:\.\.\.)?([A-Za-z_][\w]*)\]{1,2}", r"{\1}", raw)
|
|
64
64
|
return raw
|
|
65
65
|
|
|
66
66
|
|
|
@@ -74,17 +74,31 @@ def join_route_prefixes(*parts: str) -> str:
|
|
|
74
74
|
join_route_prefixes("/api", "") -> "/api"
|
|
75
75
|
join_route_prefixes("/", "/") -> "/"
|
|
76
76
|
"""
|
|
77
|
-
|
|
77
|
+
tokens: list[str] = []
|
|
78
78
|
for part in parts:
|
|
79
79
|
p = part.strip()
|
|
80
80
|
if not p or p == "/":
|
|
81
81
|
continue
|
|
82
|
-
|
|
83
|
-
if
|
|
84
|
-
|
|
85
|
-
|
|
82
|
+
sub_tokens = [tok for tok in p.strip("/").split("/") if tok]
|
|
83
|
+
if not sub_tokens:
|
|
84
|
+
continue
|
|
85
|
+
if not tokens:
|
|
86
|
+
tokens.extend(sub_tokens)
|
|
87
|
+
continue
|
|
88
|
+
|
|
89
|
+
# Find maximum overlap between suffix of tokens and prefix of sub_tokens
|
|
90
|
+
max_overlap = 0
|
|
91
|
+
max_k = min(len(tokens), len(sub_tokens))
|
|
92
|
+
for k in range(max_k, 0, -1):
|
|
93
|
+
if tokens[-k:] == sub_tokens[:k]:
|
|
94
|
+
max_overlap = k
|
|
95
|
+
break
|
|
96
|
+
|
|
97
|
+
tokens.extend(sub_tokens[max_overlap:])
|
|
98
|
+
|
|
99
|
+
if not tokens:
|
|
86
100
|
return "/"
|
|
87
|
-
return "/" + "/".join(
|
|
101
|
+
return "/" + "/".join(tokens)
|
|
88
102
|
|
|
89
103
|
|
|
90
104
|
@dataclass(frozen=True)
|
|
@@ -651,12 +665,13 @@ def get_python_analyzers(imports_modules: set[str]) -> list[FrameworkAnalyzer]:
|
|
|
651
665
|
return analyzers
|
|
652
666
|
|
|
653
667
|
|
|
668
|
+
# ---------------------------------------------------------------------------
|
|
654
669
|
# ---------------------------------------------------------------------------
|
|
655
670
|
# JavaScript / TypeScript Framework Analyzers
|
|
656
671
|
# ---------------------------------------------------------------------------
|
|
657
672
|
|
|
658
673
|
_EXPRESS_CALL_RE = re.compile(
|
|
659
|
-
r"\b([A-Za-z_$][\w$]*)\.(get|post|put|delete|patch|options|head|all)\s*\(\s*['\"]([^'\"]+)['\"]\s*,\s*(
|
|
674
|
+
r"\b([A-Za-z_$][\w$]*)\.(get|post|put|delete|patch|options|head|all)\s*\(\s*['\"]([^'\"]+)['\"]\s*,\s*(.+)",
|
|
660
675
|
re.IGNORECASE,
|
|
661
676
|
)
|
|
662
677
|
|
|
@@ -676,6 +691,169 @@ _EXPRESS_IGNORE_USE = {
|
|
|
676
691
|
"bodyparser",
|
|
677
692
|
}
|
|
678
693
|
|
|
694
|
+
_NON_HANDLER_CALLS = {
|
|
695
|
+
"async",
|
|
696
|
+
"await",
|
|
697
|
+
"function",
|
|
698
|
+
"if",
|
|
699
|
+
"return",
|
|
700
|
+
"try",
|
|
701
|
+
"catch",
|
|
702
|
+
"switch",
|
|
703
|
+
"while",
|
|
704
|
+
"for",
|
|
705
|
+
"throw",
|
|
706
|
+
"res.send",
|
|
707
|
+
"res.json",
|
|
708
|
+
"res.status",
|
|
709
|
+
"res.sendstatus",
|
|
710
|
+
"res.redirect",
|
|
711
|
+
"res.setheader",
|
|
712
|
+
"res.cookie",
|
|
713
|
+
"res.end",
|
|
714
|
+
"next",
|
|
715
|
+
"console.log",
|
|
716
|
+
"console.error",
|
|
717
|
+
"console.warn",
|
|
718
|
+
"console.info",
|
|
719
|
+
"object.assign",
|
|
720
|
+
"promise.all",
|
|
721
|
+
"promise.resolve",
|
|
722
|
+
"promise.reject",
|
|
723
|
+
"require",
|
|
724
|
+
"typeof",
|
|
725
|
+
"then",
|
|
726
|
+
"settimeout",
|
|
727
|
+
"cleartimeout",
|
|
728
|
+
"setinterval",
|
|
729
|
+
"clearinterval",
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
|
|
733
|
+
def split_js_arguments(args_str: str) -> list[str]:
|
|
734
|
+
"""Split comma-separated JavaScript arguments respecting nested parens, brackets, braces, and quotes."""
|
|
735
|
+
args: list[str] = []
|
|
736
|
+
current: list[str] = []
|
|
737
|
+
depth_paren = 0
|
|
738
|
+
depth_bracket = 0
|
|
739
|
+
depth_brace = 0
|
|
740
|
+
in_quote = False
|
|
741
|
+
quote_char = ""
|
|
742
|
+
escape = False
|
|
743
|
+
|
|
744
|
+
for ch in args_str:
|
|
745
|
+
if escape:
|
|
746
|
+
current.append(ch)
|
|
747
|
+
escape = False
|
|
748
|
+
continue
|
|
749
|
+
if ch == "\\":
|
|
750
|
+
current.append(ch)
|
|
751
|
+
escape = True
|
|
752
|
+
continue
|
|
753
|
+
if in_quote:
|
|
754
|
+
current.append(ch)
|
|
755
|
+
if ch == quote_char:
|
|
756
|
+
in_quote = False
|
|
757
|
+
continue
|
|
758
|
+
if ch in ('"', "'", "`"):
|
|
759
|
+
in_quote = True
|
|
760
|
+
quote_char = ch
|
|
761
|
+
current.append(ch)
|
|
762
|
+
continue
|
|
763
|
+
|
|
764
|
+
if ch == "(":
|
|
765
|
+
depth_paren += 1
|
|
766
|
+
elif ch == ")":
|
|
767
|
+
if depth_paren == 0 and depth_bracket == 0 and depth_brace == 0:
|
|
768
|
+
# Trailing closing paren of router call reached
|
|
769
|
+
break
|
|
770
|
+
depth_paren = max(0, depth_paren - 1)
|
|
771
|
+
elif ch == "[":
|
|
772
|
+
depth_bracket += 1
|
|
773
|
+
elif ch == "]":
|
|
774
|
+
depth_bracket = max(0, depth_bracket - 1)
|
|
775
|
+
elif ch == "{":
|
|
776
|
+
depth_brace += 1
|
|
777
|
+
elif ch == "}":
|
|
778
|
+
depth_brace = max(0, depth_brace - 1)
|
|
779
|
+
|
|
780
|
+
if ch == "," and depth_paren == 0 and depth_bracket == 0 and depth_brace == 0:
|
|
781
|
+
arg = "".join(current).strip()
|
|
782
|
+
if arg:
|
|
783
|
+
args.append(arg)
|
|
784
|
+
current = []
|
|
785
|
+
else:
|
|
786
|
+
current.append(ch)
|
|
787
|
+
|
|
788
|
+
arg = "".join(current).strip()
|
|
789
|
+
if arg:
|
|
790
|
+
args.append(arg)
|
|
791
|
+
return args
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
def _clean_middleware_token(raw_tok: str) -> str:
|
|
795
|
+
"""Extract clean identifier from middleware expression, e.g. requireRoles('admin') -> requireRoles."""
|
|
796
|
+
tok = raw_tok.strip()
|
|
797
|
+
if tok.startswith("[") and tok.endswith("]"):
|
|
798
|
+
tok = tok[1:-1].strip()
|
|
799
|
+
m = re.match(r"^([A-Za-z_$][\w$]*)\s*(?:\(|$)", tok)
|
|
800
|
+
if m:
|
|
801
|
+
return m.group(1)
|
|
802
|
+
return tok.split("(")[0].strip()
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
def _fallback_handler_name(route_path: str) -> str:
|
|
806
|
+
"""Generate a readable fallback handler name from the route path."""
|
|
807
|
+
clean_parts = [p for p in route_path.strip("/").split("/") if p and not p.startswith(":") and not p.startswith("{")]
|
|
808
|
+
if clean_parts:
|
|
809
|
+
return f"{clean_parts[-1]}_handler"
|
|
810
|
+
return "root_handler"
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
def _resolve_express_handler_and_middleware(
|
|
814
|
+
raw_args: str,
|
|
815
|
+
route_path: str,
|
|
816
|
+
) -> tuple[str, str, list[str]]:
|
|
817
|
+
"""Look past middleware to resolve the terminal route handler and list of middlewares."""
|
|
818
|
+
args = split_js_arguments(raw_args)
|
|
819
|
+
if not args:
|
|
820
|
+
name = _fallback_handler_name(route_path)
|
|
821
|
+
return name, name, []
|
|
822
|
+
|
|
823
|
+
# Intermediate arguments are middleware
|
|
824
|
+
middlewares: list[str] = []
|
|
825
|
+
for mw in args[:-1]:
|
|
826
|
+
clean_mw = _clean_middleware_token(mw)
|
|
827
|
+
if clean_mw:
|
|
828
|
+
middlewares.append(clean_mw)
|
|
829
|
+
|
|
830
|
+
last_arg = args[-1].strip()
|
|
831
|
+
|
|
832
|
+
# Case 1: Simple named identifier or member access, e.g. uploadInvoiceFile or controller.uploadInvoiceFile
|
|
833
|
+
id_match = re.match(r"^([A-Za-z_$][\w$.]*)$", last_arg)
|
|
834
|
+
if id_match:
|
|
835
|
+
expr = id_match.group(1)
|
|
836
|
+
name = expr.split(".")[-1]
|
|
837
|
+
return name, expr, middlewares
|
|
838
|
+
|
|
839
|
+
# Case 2: Arrow function or function expression, e.g. async (req, res) => uploadInvoiceFile(req, res)
|
|
840
|
+
# Search for business function calls inside the function body
|
|
841
|
+
calls = re.findall(r"\b([A-Za-z_$][\w$.]*)\s*\(", last_arg)
|
|
842
|
+
mw_set = set(middlewares)
|
|
843
|
+
for call in calls:
|
|
844
|
+
clean_call = call.split(".")[-1]
|
|
845
|
+
if (
|
|
846
|
+
call.lower() not in _NON_HANDLER_CALLS
|
|
847
|
+
and clean_call.lower() not in _NON_HANDLER_CALLS
|
|
848
|
+
and clean_call not in mw_set
|
|
849
|
+
):
|
|
850
|
+
return clean_call, call, middlewares
|
|
851
|
+
|
|
852
|
+
# Case 3: Anonymous inline handler without distinct call
|
|
853
|
+
name = _fallback_handler_name(route_path)
|
|
854
|
+
return name, name, middlewares
|
|
855
|
+
|
|
856
|
+
|
|
679
857
|
_NEXT_ROUTE_EXPORT_RE = re.compile(
|
|
680
858
|
r"export\s+(?:async\s+)?function\s+(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD)\s*\("
|
|
681
859
|
)
|
|
@@ -687,6 +865,8 @@ def analyze_js_ts_line(
|
|
|
687
865
|
file_path: str,
|
|
688
866
|
module: str,
|
|
689
867
|
imports_modules: set[str],
|
|
868
|
+
all_lines: list[str] | None = None,
|
|
869
|
+
line_idx: int = 0,
|
|
690
870
|
) -> list[RouteDetection]:
|
|
691
871
|
"""Analyze a single JS/TS line during single-pass scan for Express and Next.js routes."""
|
|
692
872
|
routes: list[RouteDetection] = []
|
|
@@ -702,10 +882,11 @@ def analyze_js_ts_line(
|
|
|
702
882
|
if method == "ALL":
|
|
703
883
|
method = "ANY"
|
|
704
884
|
route_path = m.group(3)
|
|
705
|
-
|
|
706
|
-
handler_name =
|
|
885
|
+
raw_args = m.group(4)
|
|
886
|
+
handler_name, handler_expr, middlewares = _resolve_express_handler_and_middleware(raw_args, route_path)
|
|
707
887
|
handler_canon = f"{module}.{handler_expr}"
|
|
708
888
|
norm = normalize_route_path(route_path, "express")
|
|
889
|
+
mw_str = f" (middleware: {', '.join(middlewares)})" if middlewares else ""
|
|
709
890
|
|
|
710
891
|
routes.append(
|
|
711
892
|
RouteDetection(
|
|
@@ -720,12 +901,53 @@ def analyze_js_ts_line(
|
|
|
720
901
|
column=m.start(),
|
|
721
902
|
confidence="HIGH",
|
|
722
903
|
resolution_status="RESOLVED",
|
|
723
|
-
evidence=f"Express route registration {
|
|
904
|
+
evidence=f"Express route registration {router_obj}.{method.lower()}('{route_path}', ...){mw_str}",
|
|
724
905
|
module=module,
|
|
725
906
|
router_name=router_obj,
|
|
726
907
|
)
|
|
727
908
|
)
|
|
728
909
|
|
|
910
|
+
# 1b. Multi-line Express route registration (e.g. router.patch(\n "/:id", ...))
|
|
911
|
+
if not routes and all_lines:
|
|
912
|
+
m_start = re.search(r"\b([A-Za-z_$][\w$]*)\.(get|post|put|delete|patch|options|head|all)\s*\(", line, re.IGNORECASE)
|
|
913
|
+
if m_start:
|
|
914
|
+
router_obj = m_start.group(1)
|
|
915
|
+
if router_obj.lower() in ("app", "router") or router_obj.lower().endswith("router") or router_obj.lower().endswith("routes"):
|
|
916
|
+
combined_expr = line[m_start.start():]
|
|
917
|
+
for next_l in all_lines[line_idx + 1:min(len(all_lines), line_idx + 15)]:
|
|
918
|
+
combined_expr += " " + next_l.strip()
|
|
919
|
+
if ")" in next_l:
|
|
920
|
+
break
|
|
921
|
+
for m in _EXPRESS_CALL_RE.finditer(combined_expr):
|
|
922
|
+
method = m.group(2).upper()
|
|
923
|
+
if method == "ALL":
|
|
924
|
+
method = "ANY"
|
|
925
|
+
route_path = m.group(3)
|
|
926
|
+
raw_args = m.group(4)
|
|
927
|
+
handler_name, handler_expr, middlewares = _resolve_express_handler_and_middleware(raw_args, route_path)
|
|
928
|
+
handler_canon = f"{module}.{handler_expr}"
|
|
929
|
+
norm = normalize_route_path(route_path, "express")
|
|
930
|
+
mw_str = f" (middleware: {', '.join(middlewares)})" if middlewares else ""
|
|
931
|
+
|
|
932
|
+
routes.append(
|
|
933
|
+
RouteDetection(
|
|
934
|
+
framework="express",
|
|
935
|
+
http_method=method,
|
|
936
|
+
route_path=route_path,
|
|
937
|
+
normalized_route=norm,
|
|
938
|
+
handler_name=handler_name,
|
|
939
|
+
handler_canonical_id=handler_canon,
|
|
940
|
+
file_path=file_path,
|
|
941
|
+
line=lineno,
|
|
942
|
+
column=m_start.start(),
|
|
943
|
+
confidence="HIGH",
|
|
944
|
+
resolution_status="RESOLVED",
|
|
945
|
+
evidence=f"Express route registration {router_obj}.{method.lower()}('{route_path}', ...){mw_str}",
|
|
946
|
+
module=module,
|
|
947
|
+
router_name=router_obj,
|
|
948
|
+
)
|
|
949
|
+
)
|
|
950
|
+
|
|
729
951
|
# 2. Next.js App Router route handlers (e.g. app/api/login/route.ts)
|
|
730
952
|
norm_path = file_path.replace("\\", "/")
|
|
731
953
|
is_next_app_route = "/route." in norm_path or norm_path.startswith("route.")
|