weavatrix 0.3.13 → 1.0.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.
Files changed (303) hide show
  1. package/LICENSE +1 -0
  2. package/README.md +231 -199
  3. package/bin/native/darwin-arm64/weavatrix +0 -0
  4. package/bin/native/darwin-x64/weavatrix +0 -0
  5. package/bin/native/linux-arm64/weavatrix +0 -0
  6. package/bin/native/linux-x64/weavatrix +0 -0
  7. package/bin/native/win32-arm64/weavatrix.exe +0 -0
  8. package/bin/native/win32-x64/weavatrix.exe +0 -0
  9. package/bin/resolve-binary.mjs +73 -0
  10. package/bin/run-native.mjs +26 -0
  11. package/bin/weavatrix-mcp.mjs +7 -6
  12. package/bin/weavatrix.mjs +5 -0
  13. package/package.json +8 -65
  14. package/skill/SKILL.md +62 -374
  15. package/skill/agents/openai.yaml +4 -0
  16. package/skill/references/tool-routing.md +45 -0
  17. package/SECURITY.md +0 -101
  18. package/docs/adr/0001-v0.3-offline-online-split.md +0 -60
  19. package/docs/agent-task-benchmark.md +0 -58
  20. package/docs/releases/v0.2.19.md +0 -33
  21. package/docs/releases/v0.3.0.md +0 -43
  22. package/docs/releases/v0.3.1.md +0 -30
  23. package/docs/releases/v0.3.10.md +0 -76
  24. package/docs/releases/v0.3.11.md +0 -55
  25. package/docs/releases/v0.3.12.md +0 -44
  26. package/docs/releases/v0.3.13.md +0 -34
  27. package/docs/releases/v0.3.2.md +0 -15
  28. package/docs/releases/v0.3.3.md +0 -25
  29. package/docs/releases/v0.3.4.md +0 -40
  30. package/docs/releases/v0.3.5.md +0 -50
  31. package/docs/releases/v0.3.6.md +0 -44
  32. package/docs/releases/v0.3.7.md +0 -37
  33. package/docs/releases/v0.3.8.md +0 -21
  34. package/docs/releases/v0.3.9.md +0 -36
  35. package/scripts/run-agent-task-benchmark.mjs +0 -118
  36. package/src/analysis/allowed-test-runner.js +0 -70
  37. package/src/analysis/architecture/contract-graph.js +0 -119
  38. package/src/analysis/architecture/contract-schema.js +0 -110
  39. package/src/analysis/architecture/contract-storage.js +0 -35
  40. package/src/analysis/architecture/contract-verification.js +0 -168
  41. package/src/analysis/architecture-contract.js +0 -16
  42. package/src/analysis/audit-debt.js +0 -94
  43. package/src/analysis/audit-extensions.js +0 -60
  44. package/src/analysis/cargo-dependency-evidence.js +0 -108
  45. package/src/analysis/cargo-manifests.js +0 -81
  46. package/src/analysis/change-classification/diff-parser.js +0 -103
  47. package/src/analysis/change-classification/options.js +0 -40
  48. package/src/analysis/change-classification/symbol-classifier.js +0 -177
  49. package/src/analysis/change-classification.js +0 -188
  50. package/src/analysis/coverage-reports.js +0 -235
  51. package/src/analysis/cycle-route.js +0 -14
  52. package/src/analysis/data-flow-evidence.js +0 -114
  53. package/src/analysis/dead-check.js +0 -300
  54. package/src/analysis/dead-code-review/candidates.js +0 -194
  55. package/src/analysis/dead-code-review/policy.js +0 -23
  56. package/src/analysis/dead-code-review.js +0 -119
  57. package/src/analysis/dep-check-ecosystems.js +0 -205
  58. package/src/analysis/dep-check.js +0 -290
  59. package/src/analysis/dep-rules.js +0 -10
  60. package/src/analysis/dependency/conventions.js +0 -16
  61. package/src/analysis/dependency/scoped-dependencies.js +0 -51
  62. package/src/analysis/dependency/source-references.js +0 -21
  63. package/src/analysis/duplicate-groups.js +0 -104
  64. package/src/analysis/duplicates-worker.js +0 -11
  65. package/src/analysis/duplicates.compute.js +0 -269
  66. package/src/analysis/duplicates.js +0 -15
  67. package/src/analysis/duplicates.run.js +0 -52
  68. package/src/analysis/duplicates.tokenize.js +0 -199
  69. package/src/analysis/endpoints/common.js +0 -62
  70. package/src/analysis/endpoints/extract.js +0 -107
  71. package/src/analysis/endpoints/inventory.js +0 -84
  72. package/src/analysis/endpoints/mounts.js +0 -129
  73. package/src/analysis/endpoints-java.js +0 -260
  74. package/src/analysis/endpoints-rust.js +0 -124
  75. package/src/analysis/endpoints.js +0 -3
  76. package/src/analysis/findings.js +0 -70
  77. package/src/analysis/git-history/analytics.js +0 -177
  78. package/src/analysis/git-history/collector.js +0 -67
  79. package/src/analysis/git-history/options.js +0 -66
  80. package/src/analysis/git-history.js +0 -143
  81. package/src/analysis/git-ref-graph.js +0 -76
  82. package/src/analysis/go-dependency-evidence.js +0 -62
  83. package/src/analysis/graph-analysis.aggregate.js +0 -295
  84. package/src/analysis/graph-analysis.edges.js +0 -24
  85. package/src/analysis/graph-analysis.js +0 -7
  86. package/src/analysis/graph-analysis.refs.js +0 -154
  87. package/src/analysis/graph-analysis.summaries.js +0 -65
  88. package/src/analysis/health-capabilities.js +0 -83
  89. package/src/analysis/hot-path-review.js +0 -266
  90. package/src/analysis/http-contract-wrappers.js +0 -241
  91. package/src/analysis/http-contracts/analysis.js +0 -179
  92. package/src/analysis/http-contracts/client-call-detection.js +0 -82
  93. package/src/analysis/http-contracts/client-call-parser.js +0 -273
  94. package/src/analysis/http-contracts/graph-context.js +0 -143
  95. package/src/analysis/http-contracts/matching.js +0 -61
  96. package/src/analysis/http-contracts/shared.js +0 -85
  97. package/src/analysis/http-contracts.js +0 -6
  98. package/src/analysis/internal-audit/dependency-health.js +0 -113
  99. package/src/analysis/internal-audit/python-manifests.js +0 -78
  100. package/src/analysis/internal-audit/repo-files.js +0 -50
  101. package/src/analysis/internal-audit/supply-chain.js +0 -149
  102. package/src/analysis/internal-audit.collect.js +0 -213
  103. package/src/analysis/internal-audit.js +0 -9
  104. package/src/analysis/internal-audit.reach.js +0 -173
  105. package/src/analysis/internal-audit.run.js +0 -256
  106. package/src/analysis/java-source.js +0 -36
  107. package/src/analysis/jvm-artifact-index.js +0 -87
  108. package/src/analysis/jvm-dependency-evidence.js +0 -138
  109. package/src/analysis/jvm-manifests.js +0 -114
  110. package/src/analysis/manifests.js +0 -123
  111. package/src/analysis/package-reachability.js +0 -39
  112. package/src/analysis/source-complexity.ast.js +0 -97
  113. package/src/analysis/source-complexity.constants.js +0 -55
  114. package/src/analysis/source-complexity.js +0 -14
  115. package/src/analysis/source-complexity.report.js +0 -81
  116. package/src/analysis/source-complexity.walk.js +0 -228
  117. package/src/analysis/source-correctness/retry-patterns.js +0 -93
  118. package/src/analysis/source-correctness.js +0 -222
  119. package/src/analysis/static-test-reachability.js +0 -150
  120. package/src/analysis/structure/dependency-graph.js +0 -151
  121. package/src/analysis/structure/findings.js +0 -162
  122. package/src/analysis/task-retrieval.js +0 -107
  123. package/src/analysis/transport-contracts.js +0 -224
  124. package/src/analysis/transport-runtime-evidence.js +0 -155
  125. package/src/analysis-kit.mjs +0 -17
  126. package/src/bounds.js +0 -4
  127. package/src/build-graph.js +0 -209
  128. package/src/child-env.js +0 -7
  129. package/src/config.js +0 -5
  130. package/src/extension/local-services.mjs +0 -75
  131. package/src/git-exec.js +0 -67
  132. package/src/graph/build-worker.js +0 -49
  133. package/src/graph/builder/go-receiver-resolution.js +0 -112
  134. package/src/graph/builder/js/dynamic-import.js +0 -26
  135. package/src/graph/builder/js/queries.js +0 -48
  136. package/src/graph/builder/lang-csharp.js +0 -40
  137. package/src/graph/builder/lang-css.js +0 -29
  138. package/src/graph/builder/lang-go.js +0 -140
  139. package/src/graph/builder/lang-html.js +0 -29
  140. package/src/graph/builder/lang-java-calls.js +0 -142
  141. package/src/graph/builder/lang-java.js +0 -215
  142. package/src/graph/builder/lang-js.js +0 -291
  143. package/src/graph/builder/lang-python.js +0 -235
  144. package/src/graph/builder/lang-rust-calls.js +0 -63
  145. package/src/graph/builder/lang-rust.js +0 -299
  146. package/src/graph/builder/lang-solidity.js +0 -126
  147. package/src/graph/builder/lang-sql.js +0 -263
  148. package/src/graph/builder/pass2-resolution.js +0 -81
  149. package/src/graph/builder/spec-pkg.js +0 -87
  150. package/src/graph/community.js +0 -27
  151. package/src/graph/edge-provenance.js +0 -31
  152. package/src/graph/file-lock.js +0 -69
  153. package/src/graph/freshness-probe.js +0 -142
  154. package/src/graph/graph-filter.js +0 -63
  155. package/src/graph/incremental-refresh.js +0 -235
  156. package/src/graph/internal-builder.barrels.js +0 -235
  157. package/src/graph/internal-builder.build.js +0 -300
  158. package/src/graph/internal-builder.java.js +0 -33
  159. package/src/graph/internal-builder.js +0 -12
  160. package/src/graph/internal-builder.langs.js +0 -157
  161. package/src/graph/internal-builder.pass2.js +0 -163
  162. package/src/graph/internal-builder.resolvers.js +0 -286
  163. package/src/graph/layout.js +0 -51
  164. package/src/graph/node-id.js +0 -7
  165. package/src/graph/parser-artifact-boundary.js +0 -55
  166. package/src/graph/relations.js +0 -4
  167. package/src/graph/repo-registry.js +0 -124
  168. package/src/graph/resolvers/rust.js +0 -117
  169. package/src/infra/infra-items.js +0 -255
  170. package/src/infra/infra-registry.js +0 -184
  171. package/src/infra/infra.detect.js +0 -119
  172. package/src/infra/infra.js +0 -38
  173. package/src/infra/infra.match.js +0 -102
  174. package/src/infra/infra.scan.js +0 -93
  175. package/src/mcp/actions/graph-lifecycle.mjs +0 -148
  176. package/src/mcp/architecture-bootstrap.mjs +0 -168
  177. package/src/mcp/architecture-starter.mjs +0 -234
  178. package/src/mcp/catalog.mjs +0 -233
  179. package/src/mcp/company-contract-page.mjs +0 -142
  180. package/src/mcp/company-contract-reconcile.mjs +0 -87
  181. package/src/mcp/company-contract-verdict.mjs +0 -42
  182. package/src/mcp/evidence/bun-lock-graph.mjs +0 -209
  183. package/src/mcp/evidence/duplicate-member-order.mjs +0 -8
  184. package/src/mcp/evidence/package-graph-common.mjs +0 -115
  185. package/src/mcp/evidence/package-lock-graph.mjs +0 -92
  186. package/src/mcp/evidence-snapshot.architecture.mjs +0 -80
  187. package/src/mcp/evidence-snapshot.common.mjs +0 -158
  188. package/src/mcp/evidence-snapshot.duplicates.mjs +0 -213
  189. package/src/mcp/evidence-snapshot.health.mjs +0 -102
  190. package/src/mcp/evidence-snapshot.inventory.mjs +0 -148
  191. package/src/mcp/evidence-snapshot.mjs +0 -50
  192. package/src/mcp/evidence-snapshot.package-graph.mjs +0 -34
  193. package/src/mcp/evidence-snapshot.structure.mjs +0 -84
  194. package/src/mcp/extension-api.mjs +0 -77
  195. package/src/mcp/git-output.mjs +0 -7
  196. package/src/mcp/graph/context-core.mjs +0 -111
  197. package/src/mcp/graph/context-seeds.mjs +0 -207
  198. package/src/mcp/graph/context-state.mjs +0 -85
  199. package/src/mcp/graph/reverse-reach.mjs +0 -42
  200. package/src/mcp/graph/tools-core.mjs +0 -143
  201. package/src/mcp/graph/tools-query.mjs +0 -276
  202. package/src/mcp/graph-context.mjs +0 -5
  203. package/src/mcp/graph-diff.mjs +0 -218
  204. package/src/mcp/health/audit-format.mjs +0 -199
  205. package/src/mcp/health/audit.mjs +0 -174
  206. package/src/mcp/health/dead-code.mjs +0 -110
  207. package/src/mcp/health/duplicates.mjs +0 -85
  208. package/src/mcp/health/endpoints.mjs +0 -71
  209. package/src/mcp/health/structure.mjs +0 -219
  210. package/src/mcp/runtime-version.mjs +0 -31
  211. package/src/mcp/server/auto-refresh.mjs +0 -66
  212. package/src/mcp/server/runtime-config.mjs +0 -43
  213. package/src/mcp/server/shutdown.mjs +0 -40
  214. package/src/mcp/staleness-notice.mjs +0 -20
  215. package/src/mcp/sync/evidence-architecture.mjs +0 -54
  216. package/src/mcp/sync/evidence-common.mjs +0 -88
  217. package/src/mcp/sync/evidence-duplicates.mjs +0 -97
  218. package/src/mcp/sync/evidence-health.mjs +0 -56
  219. package/src/mcp/sync/evidence-packages.mjs +0 -95
  220. package/src/mcp/sync/payload-common.mjs +0 -97
  221. package/src/mcp/sync/payload-v2.mjs +0 -54
  222. package/src/mcp/sync/payload-v3.mjs +0 -79
  223. package/src/mcp/sync-evidence.mjs +0 -23
  224. package/src/mcp/sync-payload.mjs +0 -70
  225. package/src/mcp/tool-result.mjs +0 -54
  226. package/src/mcp/tools-actions.mjs +0 -11
  227. package/src/mcp/tools-architecture.mjs +0 -177
  228. package/src/mcp/tools-company.mjs +0 -251
  229. package/src/mcp/tools-context.mjs +0 -222
  230. package/src/mcp/tools-endpoints.mjs +0 -207
  231. package/src/mcp/tools-graph-hubs.mjs +0 -78
  232. package/src/mcp/tools-graph.mjs +0 -17
  233. package/src/mcp/tools-health.mjs +0 -24
  234. package/src/mcp/tools-history.mjs +0 -28
  235. package/src/mcp/tools-impact-change.mjs +0 -220
  236. package/src/mcp/tools-impact-precision.mjs +0 -89
  237. package/src/mcp/tools-impact.mjs +0 -169
  238. package/src/mcp/tools-source.mjs +0 -238
  239. package/src/mcp/tools-verified-change.mjs +0 -200
  240. package/src/mcp-rg.mjs +0 -50
  241. package/src/mcp-runtime.mjs +0 -2
  242. package/src/mcp-server.mjs +0 -298
  243. package/src/mcp-source-tools.mjs +0 -163
  244. package/src/path-classification.js +0 -220
  245. package/src/path-ignore.js +0 -69
  246. package/src/precision/lsp-client/constants.js +0 -12
  247. package/src/precision/lsp-client/environment.js +0 -20
  248. package/src/precision/lsp-client/errors.js +0 -15
  249. package/src/precision/lsp-client/lifecycle.js +0 -114
  250. package/src/precision/lsp-client/message-parser.js +0 -81
  251. package/src/precision/lsp-client/protocol.js +0 -212
  252. package/src/precision/lsp-client/registry.js +0 -59
  253. package/src/precision/lsp-client/repo-uri.js +0 -60
  254. package/src/precision/lsp-client/stdio-client.js +0 -140
  255. package/src/precision/lsp-client.js +0 -10
  256. package/src/precision/lsp-overlay/budget-policy.js +0 -78
  257. package/src/precision/lsp-overlay/build.js +0 -261
  258. package/src/precision/lsp-overlay/contract.js +0 -61
  259. package/src/precision/lsp-overlay/reference-results.js +0 -108
  260. package/src/precision/lsp-overlay/semantic-inputs.js +0 -62
  261. package/src/precision/lsp-overlay/source-session.js +0 -142
  262. package/src/precision/lsp-overlay/store.js +0 -150
  263. package/src/precision/lsp-overlay/target-index.js +0 -152
  264. package/src/precision/lsp-overlay/target-query.js +0 -55
  265. package/src/precision/lsp-overlay.js +0 -11
  266. package/src/precision/symbol-query.js +0 -224
  267. package/src/precision/typescript-lsp-provider.js +0 -12
  268. package/src/precision/typescript-provider/client.js +0 -89
  269. package/src/precision/typescript-provider/discovery.js +0 -124
  270. package/src/precision/typescript-provider/isolated-runtime.js +0 -39
  271. package/src/precision/typescript-provider/project-host.js +0 -254
  272. package/src/precision/typescript-provider/project-safety.js +0 -166
  273. package/src/precision/typescript-provider/reference-usage.js +0 -53
  274. package/src/process.js +0 -95
  275. package/src/repo-path.js +0 -53
  276. package/src/scan/discover.inventory.js +0 -78
  277. package/src/scan/discover.js +0 -5
  278. package/src/scan/discover.list.js +0 -79
  279. package/src/scan/discover.stack.js +0 -227
  280. package/src/scan/search.core.js +0 -24
  281. package/src/scan/search.git.js +0 -102
  282. package/src/scan/search.js +0 -9
  283. package/src/scan/search.node.js +0 -83
  284. package/src/scan/search.preview.js +0 -49
  285. package/src/scan/search.rg.js +0 -145
  286. package/src/security/advisory-store.js +0 -172
  287. package/src/security/installed-jvm-rust.js +0 -46
  288. package/src/security/installed.js +0 -280
  289. package/src/security/malware-file-heuristics.js +0 -121
  290. package/src/security/malware-heuristics.exclusions.js +0 -134
  291. package/src/security/malware-heuristics.js +0 -15
  292. package/src/security/malware-heuristics.roots.js +0 -145
  293. package/src/security/malware-heuristics.scan.js +0 -89
  294. package/src/security/malware-heuristics.sweep.js +0 -122
  295. package/src/security/malware-scoring.js +0 -105
  296. package/src/security/match.js +0 -85
  297. package/src/security/registry-sig.classify.js +0 -137
  298. package/src/security/registry-sig.js +0 -18
  299. package/src/security/registry-sig.rules.js +0 -192
  300. package/src/security/rust-advisory-report.js +0 -60
  301. package/src/security/typosquat.js +0 -81
  302. package/src/util.js +0 -40
  303. package/src/version.js +0 -7
@@ -1,199 +0,0 @@
1
- // duplicates.tokenize.js — lexical layer of the clone detector (split from duplicates.js): comment &
2
- // string stripping, body-end detection, strict/renamed tokenization, and winnowing fingerprints.
3
-
4
- const K = 8; // k-gram length (tokens)
5
- const W = 4; // winnowing window → guaranteed detection of matches ≥ K+W-1 tokens
6
- const STRLIT = String.fromCharCode(0); // opaque-literal sentinel for string bodies: a NUL
7
- // its own class (never an identifier "I" nor a number "N"), so a literal can
8
- // never masquerade as code in either mode
9
- const REGEXLIT = String.fromCharCode(1); // regex bodies are executable behavior, so retain an equality anchor
10
- const literalHash = (value) => {
11
- let hash = 2166136261;
12
- for (const char of String(value || "")) hash = Math.imul(hash ^ char.charCodeAt(0), 16777619);
13
- return (hash >>> 0).toString(16);
14
- };
15
- const regexMarker = (value) => ` ${REGEXLIT} 0x${literalHash(value)} `;
16
-
17
- const KEYWORDS = new Set(("if else for while do switch case break continue return function const let var new class extends async await try catch finally throw import from export default typeof instanceof in of delete void yield static get set this super null undefined true false def elif except lambda pass raise with as is not and or None True False func go defer chan map range struct interface type package nil err string int bool byte float64 public private protected final void long double boolean").split(" "));
18
-
19
- // A leading `/` is a REGEX literal (not division) when the previous significant char is one of these
20
- // or the fragment start; after a word char / ) / ] it is division. Covers .match(/…/), = /…/, (/…/),
21
- // and arrow `x => /…/`. NOTE: `+` and `-` are deliberately EXCLUDED — a regex never legitimately
22
- // follows them, but `i++ / 2` and `a - b / c` do, and including them mis-scanned the division as a
23
- // regex and ate the rest of the line.
24
- const REGEX_PREV = new Set("(,=:[!&|?{};~*%^<>".split(""));
25
-
26
- // Replace comments with nothing and string bodies with an opaque sentinel. Regex bodies retain only
27
- // a one-way equality anchor: their source is executable behavior, and collapsing every pattern to the
28
- // same token creates false clone groups for unrelated replace/validate pipelines.
29
- export function stripNonCode(text, py) {
30
- let out = "", i = 0, prevSig = "";
31
- const s = String(text || "");
32
- const push = (c) => { out += c; const t = c.trim(); if (t) prevSig = t[t.length - 1]; };
33
- while (i < s.length) {
34
- const ch = s[i], two = s.slice(i, i + 2), three = s.slice(i, i + 3);
35
- if (py && ch === "#") { while (i < s.length && s[i] !== "\n") i++; continue; }
36
- if (py && (three === "'''" || three === '"""')) {
37
- i += 3;
38
- while (i < s.length && s.slice(i, i + 3) !== three) i++;
39
- i += 3; push(` ${STRLIT} `); continue;
40
- }
41
- if (!py && two === "//") { while (i < s.length && s[i] !== "\n") i++; continue; }
42
- if (!py && two === "/*") { i += 2; while (i < s.length && s.slice(i, i + 2) !== "*/") i++; i += 2; continue; }
43
- if (!py && ch === "/" && REGEX_PREV.has(prevSig)) {
44
- i++; const regexStart = i; let inClass = false; // '/' inside a [...] char class is literal, not the terminator
45
- while (i < s.length && s[i] !== "\n") {
46
- const c = s[i];
47
- if (c === "\\") { i += 2; continue; }
48
- if (c === "[") inClass = true;
49
- else if (c === "]") inClass = false;
50
- else if (c === "/" && !inClass) { i++; break; }
51
- i++;
52
- }
53
- while (i < s.length && /[a-z]/i.test(s[i])) i++; // regex flags (gimsuy)
54
- push(regexMarker(s.slice(regexStart, i))); continue;
55
- }
56
- if (ch === '"' || ch === "'" || ch === "`") {
57
- const q = ch; i++;
58
- while (i < s.length && s[i] !== q) { if (s[i] === "\\") i++; i++; }
59
- i++; push(` ${STRLIT} `); continue;
60
- }
61
- push(ch); i++;
62
- }
63
- return out;
64
- }
65
-
66
- // Where a symbol's body ends — so the LAST symbol in a file doesn't swallow trailing module-level code
67
- // (module.exports tables, main() calls, handler registrations) up to EOF and dilute/misattribute
68
- // clones. Brace/bracket depth for brace langs (body ends when {}()[] balance after first opening);
69
- // indentation for Python. Returns a 1-based line count within bodyLines (≤ its length; = its length
70
- // when the construct never closes, so the caller's cap stands).
71
- export function bodyEndLineCount(bodyLines, py) {
72
- if (py) {
73
- const base = bodyLines[0].match(/^[ \t]*/)[0].length;
74
- let last = 1;
75
- for (let k = 1; k < bodyLines.length; k++) {
76
- const ln = bodyLines[k];
77
- if (!ln.trim()) continue; // blank lines never end a block
78
- if (ln.match(/^[ \t]*/)[0].length <= base) return last; // dedent to ≤ the def → body ended above
79
- last = k + 1;
80
- }
81
- return bodyLines.length;
82
- }
83
- const text = bodyLines.join("\n");
84
- let depth = 0, opened = false, i = 0, line = 1, prevSig = "";
85
- while (i < text.length) {
86
- const ch = text[i], two = text.slice(i, i + 2);
87
- if (ch === "\n") { line++; i++; continue; }
88
- if (two === "//") { while (i < text.length && text[i] !== "\n") i++; continue; }
89
- if (two === "/*") { i += 2; while (i < text.length && text.slice(i, i + 2) !== "*/") { if (text[i] === "\n") line++; i++; } i += 2; continue; }
90
- if (ch === "/" && REGEX_PREV.has(prevSig)) {
91
- i++; let inClass = false;
92
- while (i < text.length && text[i] !== "\n") { const c = text[i]; if (c === "\\") { i += 2; continue; } if (c === "[") inClass = true; else if (c === "]") inClass = false; else if (c === "/" && !inClass) { i++; break; } i++; }
93
- prevSig = "/"; continue;
94
- }
95
- if (ch === '"' || ch === "'" || ch === "`") { const q = ch; i++; while (i < text.length && text[i] !== q) { if (text[i] === "\\") i++; else if (text[i] === "\n") line++; i++; } i++; prevSig = q; continue; }
96
- // depth tracks all brackets, but only a { or [ opens the BODY — a bare (param list) returning to
97
- // depth 0 must not be read as the construct closing on line 1
98
- if (ch === "{" || ch === "(" || ch === "[") { depth++; if (ch === "{" || ch === "[") opened = true; }
99
- else if (ch === "}" || ch === ")" || ch === "]") { depth--; if (opened && depth <= 0) return line; }
100
- if (ch.trim()) prevSig = ch;
101
- i++;
102
- }
103
- return bodyLines.length;
104
- }
105
-
106
- // Large multi-line string literals — the OPPOSITE selection to stripNonCode: everything the code
107
- // scan throws away. Embedded DSLs (inline C#/SQL/PowerShell templates) are invisible to the normal
108
- // clone pass because their bodies are stripped; this extractor feeds them back in as their own
109
- // fragments when the caller opts in (find_duplicates include_strings). Comment/regex handling
110
- // mirrors stripNonCode so a quote inside a comment never opens a phantom literal.
111
- export function extractLargeStrings(text, { py = false, cs = false, minLines = 6 } = {}) {
112
- const s = String(text || "");
113
- const out = [];
114
- let i = 0, line = 1, prevSig = "";
115
- const capture = (from, to, startLine) => {
116
- const content = s.slice(from, to);
117
- const nl = (content.match(/\n/g) || []).length;
118
- if (nl + 1 >= minLines) out.push({ start: startLine, end: startLine + nl, content });
119
- };
120
- while (i < s.length) {
121
- const ch = s[i], two = s.slice(i, i + 2), three = s.slice(i, i + 3);
122
- if (ch === "\n") { line++; i++; continue; }
123
- if (py && ch === "#") { while (i < s.length && s[i] !== "\n") i++; continue; }
124
- if (py && (three === "'''" || three === '"""')) {
125
- const startLine = line; i += 3; const from = i;
126
- while (i < s.length && s.slice(i, i + 3) !== three) { if (s[i] === "\n") line++; i++; }
127
- capture(from, i, startLine); i += 3; continue;
128
- }
129
- if (!py && two === "//") { while (i < s.length && s[i] !== "\n") i++; continue; }
130
- if (!py && two === "/*") { i += 2; while (i < s.length && s.slice(i, i + 2) !== "*/") { if (s[i] === "\n") line++; i++; } i += 2; continue; }
131
- if (!py && ch === "/" && REGEX_PREV.has(prevSig)) {
132
- i++; let inClass = false;
133
- while (i < s.length && s[i] !== "\n") { const c = s[i]; if (c === "\\") { i += 2; continue; } if (c === "[") inClass = true; else if (c === "]") inClass = false; else if (c === "/" && !inClass) { i++; break; } i++; }
134
- prevSig = "/"; continue;
135
- }
136
- if (cs && two === '@"') { // C# verbatim string; "" is the escaped quote
137
- const startLine = line; i += 2; const from = i;
138
- while (i < s.length) { if (s.slice(i, i + 2) === '""') { i += 2; continue; } if (s[i] === '"') break; if (s[i] === "\n") line++; i++; }
139
- capture(from, i, startLine); i++; continue;
140
- }
141
- if (!py && ch === "`") { // JS/TS template literal — the main multi-line carrier
142
- const startLine = line; i++; const from = i;
143
- while (i < s.length && s[i] !== "`") { if (s[i] === "\\") { i++; if (s[i] === "\n") line++; i++; continue; } if (s[i] === "\n") line++; i++; }
144
- capture(from, i, startLine); i++; continue;
145
- }
146
- if (ch === '"' || ch === "'") { // single-line strings can't span minLines — just skip past
147
- const q = ch; i++;
148
- while (i < s.length && s[i] !== q && s[i] !== "\n") { if (s[i] === "\\") i++; i++; }
149
- if (s[i] === q) i++; continue;
150
- }
151
- if (ch.trim()) prevSig = ch;
152
- i++;
153
- }
154
- return out;
155
- }
156
-
157
- // one raw token stream; the two modes differ only in identifier canonicalization
158
- export function tokenize(text) {
159
- const raw = text.match(/[A-Za-z_$][\w$]*|(?:0[xX][\dA-Fa-f_]+|0[bB][01_]+|0[oO][0-7_]+|\d[\d_]*(?:\.\d[\d_]*)?(?:[eE][+-]?\d[\d_]*)?)n?|[^\s\w]/g) || [];
160
- const strict = [];
161
- const renamed = [];
162
- for (const t of raw) {
163
- if (/^\d/.test(t)) {
164
- // Numeric constants are behavior, not renameable identifiers. Preserve
165
- // their value so same-shape limit/config tables with different numbers do
166
- // not become perfect clones; a small constant edit still leaves the rest
167
- // of a genuine copied body highly similar.
168
- strict.push(`N:${t}`); renamed.push(`N:${t}`); continue;
169
- }
170
- if (/^[A-Za-z_$]/.test(t) && !KEYWORDS.has(t)) { strict.push(t); renamed.push("I"); continue; }
171
- strict.push(t); renamed.push(t);
172
- }
173
- return { strict, renamed };
174
- }
175
-
176
- export function fingerprints(toks) {
177
- if (toks.length < K + W - 1) return new Set();
178
- const hashes = [];
179
- for (let i = 0; i + K <= toks.length; i++) {
180
- let h = 0;
181
- for (let j = i; j < i + K; j++) {
182
- const s = toks[j];
183
- for (let c = 0; c < s.length; c++) h = (h * 31 + s.charCodeAt(c)) | 0;
184
- h = (h * 131) | 0;
185
- }
186
- hashes.push(h >>> 0);
187
- }
188
- const fp = new Set();
189
- for (let i = 0; i + W <= hashes.length + 1; i++) {
190
- let min = Infinity;
191
- for (let j = i; j < Math.min(i + W, hashes.length); j++) if (hashes[j] < min) min = hashes[j];
192
- fp.add(min);
193
- }
194
- // Winnowing can skip every changed literal in a repetitive policy table.
195
- // Exact numeric anchors keep behavior-defining limits in the similarity
196
- // denominator while leaving ordinary copied bodies tolerant of a small edit.
197
- for (const token of toks) if (token.startsWith("N:")) fp.add(`numeric:${token}`);
198
- return fp;
199
- }
@@ -1,62 +0,0 @@
1
- export const MAX_ENDPOINT_FILES = 3_000;
2
- export const MAX_ENDPOINTS = 2_000;
3
- export const HTTP_METHODS = new Set(["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS", "TRACE", "CONNECT", "ALL", "ANY"]);
4
-
5
- export function lineAt(text, index) {
6
- let line = 1;
7
- for (let i = 0; i < index && i < text.length; i++) if (text[i] === "\n") line++;
8
- return line;
9
- }
10
-
11
- // Preserve offsets and literals while hiding comments from regex extractors.
12
- export function maskComments(text, { hashComments = false } = {}) {
13
- const chars = String(text || "").split("");
14
- let quote = "", escaped = false, lineComment = false, blockComment = false;
15
- for (let i = 0; i < chars.length; i++) {
16
- const ch = chars[i], next = chars[i + 1];
17
- if (lineComment) {
18
- if (ch === "\n" || ch === "\r") lineComment = false;
19
- else chars[i] = " ";
20
- continue;
21
- }
22
- if (blockComment) {
23
- if (ch === "*" && next === "/") { chars[i] = chars[i + 1] = " "; i++; blockComment = false; }
24
- else if (ch !== "\n" && ch !== "\r") chars[i] = " ";
25
- continue;
26
- }
27
- if (quote) {
28
- if (escaped) escaped = false;
29
- else if (ch === "\\") escaped = true;
30
- else if (ch === quote) quote = "";
31
- continue;
32
- }
33
- if (ch === '"' || ch === "'" || ch === "`") { quote = ch; continue; }
34
- if (ch === "/" && next === "/") { chars[i] = chars[i + 1] = " "; i++; lineComment = true; continue; }
35
- if (ch === "/" && next === "*") { chars[i] = chars[i + 1] = " "; i++; blockComment = true; continue; }
36
- if (hashComments && ch === "#") { chars[i] = " "; lineComment = true; }
37
- }
38
- return chars.join("");
39
- }
40
-
41
- export function handlerName(expr) {
42
- const source = String(expr || "").trim();
43
- if (!source || /=>/.test(source) || /^\s*(async\s+)?function\b/.test(source) || /^\s*(?:async\s+)?(?:move\s+)?\|[^|]*\|/.test(source)) return "";
44
- const turbofish = /(?:^|::)([A-Za-z_][\w]*)\s*::<[\s\S]*>\s*$/.exec(source);
45
- if (turbofish) return turbofish[1];
46
- const identifiers = source.match(/[A-Za-z_$][\w$]*/g);
47
- if (!identifiers) return "";
48
- const skipped = new Set(["async", "function", "await", "req", "res", "ctx", "request", "response", "next", "return"]);
49
- for (let i = identifiers.length - 1; i >= 0; i--) if (!skipped.has(identifiers[i])) return identifiers[i];
50
- return "";
51
- }
52
-
53
- export function handlerReference(expr) {
54
- const source = String(expr || "").trim();
55
- if (!source || /=>/.test(source) || /^\s*(async\s+)?function\b/.test(source)) return "";
56
- const refs = [...source.matchAll(/([A-Za-z_$][\w$]*(?:\s*\.\s*[A-Za-z_$][\w$]*)+)/g)];
57
- return refs.length ? refs.at(-1)[1].replace(/\s+/g, "") : "";
58
- }
59
-
60
- export const looksLikePath = (path) => typeof path === "string" && /^\/[\w\-./:{}*$?]*$/.test(path) && !path.includes("://");
61
- export const cleanPath = (path) => String(path || "").replace(/\/+$/, "") || "/";
62
- export const normalizedFile = (file) => String(file || "").replace(/\\/g, "/").replace(/^\.\//, "");
@@ -1,107 +0,0 @@
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
- }
@@ -1,84 +0,0 @@
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
- }
@@ -1,129 +0,0 @@
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
- }