vibecode-anything 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +239 -67
- package/dist/cli.js +2140 -73
- package/dist/core/adapters.d.ts +26 -3
- package/dist/core/adapters.js +411 -25
- package/dist/core/agent-adapter.d.ts +67 -0
- package/dist/core/agent-adapter.js +312 -0
- package/dist/core/anti-cheat.d.ts +6 -0
- package/dist/core/anti-cheat.js +372 -0
- package/dist/core/badge.d.ts +18 -0
- package/dist/core/badge.js +360 -0
- package/dist/core/cache.d.ts +47 -0
- package/dist/core/cache.js +403 -0
- package/dist/core/ci-export.d.ts +35 -0
- package/dist/core/ci-export.js +796 -0
- package/dist/core/ci-formats.d.ts +21 -0
- package/dist/core/ci-formats.js +388 -0
- package/dist/core/client-codegen.d.ts +36 -0
- package/dist/core/client-codegen.js +2538 -0
- package/dist/core/compose.d.ts +71 -0
- package/dist/core/compose.js +1088 -0
- package/dist/core/docs-export.d.ts +37 -0
- package/dist/core/docs-export.js +2706 -0
- package/dist/core/doctor.d.ts +21 -1
- package/dist/core/doctor.js +234 -21
- package/dist/core/exec.d.ts +26 -2
- package/dist/core/exec.js +235 -8
- package/dist/core/fixtures.d.ts +153 -0
- package/dist/core/fixtures.js +2245 -0
- package/dist/core/format.d.ts +77 -0
- package/dist/core/format.js +632 -0
- package/dist/core/fs-snapshot.d.ts +37 -0
- package/dist/core/fs-snapshot.js +216 -0
- package/dist/core/graphql-export.d.ts +34 -0
- package/dist/core/graphql-export.js +2482 -0
- package/dist/core/har-export.d.ts +29 -0
- package/dist/core/har-export.js +988 -0
- package/dist/core/html-report.d.ts +27 -0
- package/dist/core/html-report.js +1535 -0
- package/dist/core/http-export.d.ts +31 -0
- package/dist/core/http-export.js +726 -0
- package/dist/core/impact.d.ts +80 -0
- package/dist/core/impact.js +1412 -0
- package/dist/core/lint.d.ts +38 -0
- package/dist/core/lint.js +1111 -0
- package/dist/core/load-export.d.ts +37 -0
- package/dist/core/load-export.js +1813 -0
- package/dist/core/mcp-export.d.ts +35 -0
- package/dist/core/mcp-export.js +1736 -0
- package/dist/core/mock-server.d.ts +47 -0
- package/dist/core/mock-server.js +1722 -0
- package/dist/core/openapi-export.d.ts +44 -0
- package/dist/core/openapi-export.js +1074 -0
- package/dist/core/parity.d.ts +6 -1
- package/dist/core/parity.js +36 -1
- package/dist/core/playwright-exporter.d.ts +23 -0
- package/dist/core/playwright-exporter.js +1343 -0
- package/dist/core/png.js +45 -9
- package/dist/core/regression.d.ts +54 -0
- package/dist/core/regression.js +529 -0
- package/dist/core/responsive.d.ts +86 -0
- package/dist/core/responsive.js +606 -0
- package/dist/core/run.d.ts +74 -0
- package/dist/core/run.js +696 -0
- package/dist/core/scheduler.d.ts +24 -0
- package/dist/core/scheduler.js +162 -0
- package/dist/core/schema-export.d.ts +29 -0
- package/dist/core/schema-export.js +2215 -0
- package/dist/core/schema.d.ts +3 -2
- package/dist/core/schema.js +460 -17
- package/dist/core/security.d.ts +233 -0
- package/dist/core/security.js +1839 -0
- package/dist/core/server-export.d.ts +38 -0
- package/dist/core/server-export.js +3198 -0
- package/dist/core/spec-writer.d.ts +9 -0
- package/dist/core/spec-writer.js +1202 -0
- package/dist/core/store.d.ts +11 -2
- package/dist/core/store.js +128 -8
- package/dist/core/target-map.d.ts +34 -0
- package/dist/core/target-map.js +262 -0
- package/dist/core/test-exporter.d.ts +25 -0
- package/dist/core/test-exporter.js +1918 -0
- package/dist/core/types.d.ts +182 -14
- package/dist/core/visual-semantic.d.ts +67 -0
- package/dist/core/visual-semantic.js +673 -0
- package/dist/core/visual.js +65 -16
- package/dist/core/watch.d.ts +28 -3
- package/dist/core/watch.js +222 -12
- package/dist/core/yaml.d.ts +5 -0
- package/dist/core/yaml.js +1009 -0
- package/dist/diagnostics/diagnose.d.ts +53 -0
- package/dist/diagnostics/diagnose.js +1083 -0
- package/dist/diagnostics/flake.d.ts +71 -0
- package/dist/diagnostics/flake.js +608 -0
- package/dist/diagnostics/mutation-audit.d.ts +69 -0
- package/dist/diagnostics/mutation-audit.js +769 -0
- package/dist/diagnostics/spatial.d.ts +42 -0
- package/dist/diagnostics/spatial.js +346 -0
- package/dist/diagnostics/trends.d.ts +122 -0
- package/dist/diagnostics/trends.js +1190 -0
- package/dist/discovery/discover.d.ts +111 -0
- package/dist/discovery/discover.js +2017 -0
- package/dist/discovery/lock.d.ts +116 -0
- package/dist/discovery/lock.js +1505 -0
- package/dist/discovery/routes.d.ts +36 -0
- package/dist/discovery/routes.js +1494 -0
- package/dist/discovery/test-importer.d.ts +92 -0
- package/dist/discovery/test-importer.js +4952 -0
- package/dist/index.d.ts +123 -0
- package/dist/index.js +99 -0
- package/dist/ingestors/api-collections.d.ts +28 -0
- package/dist/ingestors/api-collections.js +2335 -0
- package/dist/ingestors/api-repo.d.ts +2 -1
- package/dist/ingestors/api-repo.js +483 -85
- package/dist/ingestors/asyncapi.d.ts +26 -0
- package/dist/ingestors/asyncapi.js +2105 -0
- package/dist/ingestors/bridges.d.ts +2 -0
- package/dist/ingestors/bridges.js +702 -0
- package/dist/ingestors/cli.d.ts +2 -1
- package/dist/ingestors/cli.js +234 -46
- package/dist/ingestors/curl.d.ts +14 -0
- package/dist/ingestors/curl.js +1024 -0
- package/dist/ingestors/graphql.d.ts +56 -0
- package/dist/ingestors/graphql.js +1852 -0
- package/dist/ingestors/http-file.d.ts +20 -0
- package/dist/ingestors/http-file.js +923 -0
- package/dist/ingestors/index.js +486 -6
- package/dist/ingestors/openrpc.d.ts +29 -0
- package/dist/ingestors/openrpc.js +1807 -0
- package/dist/ingestors/proto.d.ts +47 -0
- package/dist/ingestors/proto.js +2690 -0
- package/dist/ingestors/trace.d.ts +10 -0
- package/dist/ingestors/trace.js +599 -0
- package/dist/ingestors/video.d.ts +4 -1
- package/dist/ingestors/video.js +86 -36
- package/dist/ingestors/web.d.ts +16 -1
- package/dist/ingestors/web.js +474 -32
- package/dist/mcp/server.js +3532 -67
- package/dist/oracles/api-repo.js +102 -30
- package/dist/oracles/cli.js +65 -17
- package/dist/oracles/http-workflow.d.ts +68 -0
- package/dist/oracles/http-workflow.js +828 -0
- package/dist/oracles/image.js +178 -28
- package/dist/oracles/index.d.ts +3 -1
- package/dist/oracles/index.js +243 -14
- package/dist/oracles/perf-budget.d.ts +90 -0
- package/dist/oracles/perf-budget.js +1329 -0
- package/dist/oracles/stream.d.ts +94 -0
- package/dist/oracles/stream.js +1268 -0
- package/dist/oracles/ui-states.d.ts +49 -0
- package/dist/oracles/ui-states.js +894 -0
- package/dist/oracles/video.d.ts +1 -1
- package/dist/oracles/video.js +232 -16
- package/dist/oracles/web.js +464 -73
- package/dist/plugins/builtins.d.ts +12 -0
- package/dist/plugins/builtins.js +1052 -0
- package/dist/plugins/registry.d.ts +86 -0
- package/dist/plugins/registry.js +554 -0
- package/dist/setup/index.js +145 -33
- package/package.json +56 -47
- package/skills/vibecode-anything/SKILL.md +75 -52
|
@@ -0,0 +1,1494 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-dependency, linear-time (ReDoS-safe) static route & endpoint extractor.
|
|
3
|
+
*
|
|
4
|
+
* Supports:
|
|
5
|
+
* - JS/TS HTTP routers: Express, Fastify, Hono, Koa (including multiline chains,
|
|
6
|
+
* TypeScript generic route calls, `.route(path).get().post()`, and Fastify `.route({ method, url })`)
|
|
7
|
+
* - Next.js App Router (`app/**\/route.{ts,js,mts,mjs}`, `app/**\/page.{tsx,jsx,ts,js}`)
|
|
8
|
+
* and Pages Router (`pages/api/**`, `pages/**` excluding `_app`, `_document`, `_error`)
|
|
9
|
+
* - Python FastAPI & Flask decorators (`@app.get`, `@router.post`, `@bp.delete`, `@app.route(..., methods=[...])`)
|
|
10
|
+
*/
|
|
11
|
+
import fs from "node:fs";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
/** Maximum file size (256 KiB) scanned for route definitions to keep discovery fast and bounded. */
|
|
14
|
+
export const MAX_ROUTE_FILE_BYTES = 256 * 1024;
|
|
15
|
+
const VALID_HTTP_METHODS = new Set([
|
|
16
|
+
"GET",
|
|
17
|
+
"POST",
|
|
18
|
+
"PUT",
|
|
19
|
+
"PATCH",
|
|
20
|
+
"DELETE",
|
|
21
|
+
"HEAD",
|
|
22
|
+
"OPTIONS",
|
|
23
|
+
"ALL"
|
|
24
|
+
]);
|
|
25
|
+
const JS_METHOD_MAP = new Map([
|
|
26
|
+
["get", "GET"],
|
|
27
|
+
["post", "POST"],
|
|
28
|
+
["put", "PUT"],
|
|
29
|
+
["patch", "PATCH"],
|
|
30
|
+
["delete", "DELETE"],
|
|
31
|
+
["del", "DELETE"],
|
|
32
|
+
["head", "HEAD"],
|
|
33
|
+
["options", "OPTIONS"],
|
|
34
|
+
["all", "ALL"]
|
|
35
|
+
]);
|
|
36
|
+
const PY_DIRECT_METHOD_MAP = new Map([
|
|
37
|
+
["get", "GET"],
|
|
38
|
+
["post", "POST"],
|
|
39
|
+
["put", "PUT"],
|
|
40
|
+
["patch", "PATCH"],
|
|
41
|
+
["delete", "DELETE"],
|
|
42
|
+
["head", "HEAD"],
|
|
43
|
+
["options", "OPTIONS"]
|
|
44
|
+
]);
|
|
45
|
+
const PY_MULTI_ROUTE_METHODS = new Set(["route", "api_route", "add_api_route"]);
|
|
46
|
+
const JS_EXTS = new Set([".js", ".mjs", ".cjs", ".ts", ".mts", ".cts", ".jsx", ".tsx"]);
|
|
47
|
+
const PY_EXTS = new Set([".py"]);
|
|
48
|
+
const NON_ROUTER_RECEIVERS = new Set([
|
|
49
|
+
"headers",
|
|
50
|
+
"searchparams",
|
|
51
|
+
"urlsearchparams",
|
|
52
|
+
"params",
|
|
53
|
+
"cookies",
|
|
54
|
+
"localstorage",
|
|
55
|
+
"sessionstorage",
|
|
56
|
+
"storage",
|
|
57
|
+
"map",
|
|
58
|
+
"weakmap",
|
|
59
|
+
"cache",
|
|
60
|
+
"set",
|
|
61
|
+
"weakset",
|
|
62
|
+
"formdata",
|
|
63
|
+
"reflect",
|
|
64
|
+
"object",
|
|
65
|
+
"array",
|
|
66
|
+
"process",
|
|
67
|
+
"fs",
|
|
68
|
+
"path",
|
|
69
|
+
"url",
|
|
70
|
+
"document",
|
|
71
|
+
"window",
|
|
72
|
+
"console",
|
|
73
|
+
"math",
|
|
74
|
+
"json",
|
|
75
|
+
"intl",
|
|
76
|
+
"promise",
|
|
77
|
+
"symbol",
|
|
78
|
+
"env",
|
|
79
|
+
"store",
|
|
80
|
+
"state",
|
|
81
|
+
"config",
|
|
82
|
+
"flags",
|
|
83
|
+
"options",
|
|
84
|
+
"opts",
|
|
85
|
+
"args",
|
|
86
|
+
"argv",
|
|
87
|
+
"query",
|
|
88
|
+
"body",
|
|
89
|
+
"req",
|
|
90
|
+
"request",
|
|
91
|
+
"res",
|
|
92
|
+
"response",
|
|
93
|
+
"ctx",
|
|
94
|
+
"context",
|
|
95
|
+
"reply",
|
|
96
|
+
"meta",
|
|
97
|
+
"registry",
|
|
98
|
+
"redis",
|
|
99
|
+
"db",
|
|
100
|
+
"knex",
|
|
101
|
+
"prisma",
|
|
102
|
+
"mongoose",
|
|
103
|
+
"axios",
|
|
104
|
+
"got",
|
|
105
|
+
"ky",
|
|
106
|
+
"superagent",
|
|
107
|
+
"fetch",
|
|
108
|
+
"http",
|
|
109
|
+
"https",
|
|
110
|
+
"client",
|
|
111
|
+
"httpclient",
|
|
112
|
+
"apiclient"
|
|
113
|
+
]);
|
|
114
|
+
const REGEX_PREV_KEYWORDS = new Set([
|
|
115
|
+
"return",
|
|
116
|
+
"case",
|
|
117
|
+
"throw",
|
|
118
|
+
"typeof",
|
|
119
|
+
"void",
|
|
120
|
+
"delete",
|
|
121
|
+
"in",
|
|
122
|
+
"instanceof",
|
|
123
|
+
"new",
|
|
124
|
+
"of",
|
|
125
|
+
"await",
|
|
126
|
+
"yield",
|
|
127
|
+
"else",
|
|
128
|
+
"do"
|
|
129
|
+
]);
|
|
130
|
+
const REGEX_PREV_PUNCT = new Set([
|
|
131
|
+
"(",
|
|
132
|
+
"[",
|
|
133
|
+
"{",
|
|
134
|
+
",",
|
|
135
|
+
";",
|
|
136
|
+
":",
|
|
137
|
+
"=",
|
|
138
|
+
"!",
|
|
139
|
+
"?",
|
|
140
|
+
"&",
|
|
141
|
+
"|",
|
|
142
|
+
"^",
|
|
143
|
+
"~",
|
|
144
|
+
"+",
|
|
145
|
+
"-",
|
|
146
|
+
"*",
|
|
147
|
+
"%",
|
|
148
|
+
"<",
|
|
149
|
+
">"
|
|
150
|
+
]);
|
|
151
|
+
const NEXT_EXCLUDED_PAGES = new Set(["_app", "_document", "_error", "_middleware"]);
|
|
152
|
+
const NEXT_APP_ROUTE_RE = /^(?:src\/)?app\/(?:(.+)\/)?route\.(?:ts|js|mts|mjs)$/;
|
|
153
|
+
const NEXT_APP_PAGE_RE = /^(?:src\/)?app\/(?:(.+)\/)?page\.(?:tsx|jsx|ts|js|mts|mjs)$/;
|
|
154
|
+
const NEXT_PAGES_RE = /^(?:src\/)?pages\/(.+)\.(?:tsx|jsx|ts|js|mts|mjs)$/;
|
|
155
|
+
const IGNORED_TEST_FILE_RE = /\.(?:test|spec|stories|d)\.(?:ts|tsx|js|jsx|mts|mjs|cts|cjs)$/i;
|
|
156
|
+
const MAX_ROUTE_PATH_LENGTH = 512;
|
|
157
|
+
const MAX_GENERIC_TOKENS = 64;
|
|
158
|
+
const MAX_METHOD_LIST_TOKENS = 32;
|
|
159
|
+
const MAX_UNCLOSED_EXPORT_TOKENS = 64;
|
|
160
|
+
const ROUTE_QUERY_SECRET_RE = /([?&](?:access[_-]?token|id[_-]?token|refresh[_-]?token|auth[_-]?token|token|api[_-]?key|apikey|key|x[-_]api[-_]key|x[-_]auth[-_]token|secret|client[_-]?secret|password|passwd|auth|jwt|session[_-]?id|sig|signature|private[_-]?key)=)([^&#\s"'`<>]+)/gi;
|
|
161
|
+
const ROUTE_URL_USERINFO_RE = /(\b[a-z][a-z0-9+.-]{0,24}:\/\/)([^@\s"'`<>/?#]{1,512})(@)/gi;
|
|
162
|
+
const CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/;
|
|
163
|
+
const WINDOWS_DRIVE_PREFIX_RE = /^[A-Za-z]:/;
|
|
164
|
+
function normalizePosixPath(relPath) {
|
|
165
|
+
const normalized = relPath.replace(/\\/g, "/").replace(/^\.\//, "").replace(/\/+/g, "/");
|
|
166
|
+
return normalized.startsWith("/") ? normalized.slice(1) : normalized;
|
|
167
|
+
}
|
|
168
|
+
function isIdentStart(code) {
|
|
169
|
+
return ((code >= 65 && code <= 90) || // A-Z
|
|
170
|
+
(code >= 97 && code <= 122) || // a-z
|
|
171
|
+
code === 95 || // _
|
|
172
|
+
code === 36 // $
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
function isIdentPart(code) {
|
|
176
|
+
return isIdentStart(code) || (code >= 48 && code <= 57); // 0-9
|
|
177
|
+
}
|
|
178
|
+
function isRoutePathLiteral(value) {
|
|
179
|
+
if (value.length === 0 || value.length > MAX_ROUTE_PATH_LENGTH)
|
|
180
|
+
return false;
|
|
181
|
+
if (value !== "*" && !value.startsWith("/"))
|
|
182
|
+
return false;
|
|
183
|
+
for (let i = 0; i < value.length; i++) {
|
|
184
|
+
const code = value.charCodeAt(i);
|
|
185
|
+
if (code <= 32 || code === 127)
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
function joinRoutePrefix(prefix, routePath) {
|
|
191
|
+
if (!prefix)
|
|
192
|
+
return routePath;
|
|
193
|
+
const cleanPrefix = prefix.endsWith("/") && prefix.length > 1 ? prefix.slice(0, -1) : prefix;
|
|
194
|
+
if (routePath === "/")
|
|
195
|
+
return cleanPrefix || "/";
|
|
196
|
+
if (routePath.startsWith("/")) {
|
|
197
|
+
return `${cleanPrefix === "/" ? "" : cleanPrefix}${routePath}`;
|
|
198
|
+
}
|
|
199
|
+
return `${cleanPrefix}/${routePath}`;
|
|
200
|
+
}
|
|
201
|
+
function stripLeadingBom(text) {
|
|
202
|
+
return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
|
|
203
|
+
}
|
|
204
|
+
function sanitizeRoutePath(routePath) {
|
|
205
|
+
let out = routePath;
|
|
206
|
+
if (out.includes("://")) {
|
|
207
|
+
out = out.replace(ROUTE_URL_USERINFO_RE, "$1[REDACTED]$3");
|
|
208
|
+
}
|
|
209
|
+
if (out.includes("?") || out.includes("&")) {
|
|
210
|
+
out = out.replace(ROUTE_QUERY_SECRET_RE, "$1[REDACTED]");
|
|
211
|
+
}
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
function containsNullByte(text) {
|
|
215
|
+
return text.indexOf("\0") !== -1;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Compute O(N) matching closer indices for balanced punctuation (`{`/`}`, `(`/`)`, `[`/`]`).
|
|
219
|
+
* Returns an Int32Array where `match[openIdx] = closeIdx`, or `-1` when unclosed.
|
|
220
|
+
*/
|
|
221
|
+
function computeMatchingClosers(tokens, openChar, closeChar) {
|
|
222
|
+
const match = new Int32Array(tokens.length);
|
|
223
|
+
match.fill(-1);
|
|
224
|
+
const stack = [];
|
|
225
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
226
|
+
const tok = tokens[i];
|
|
227
|
+
if (tok.kind !== "punct")
|
|
228
|
+
continue;
|
|
229
|
+
if (tok.value === openChar) {
|
|
230
|
+
stack.push(i);
|
|
231
|
+
}
|
|
232
|
+
else if (tok.value === closeChar && stack.length > 0) {
|
|
233
|
+
const openIdx = stack.pop();
|
|
234
|
+
match[openIdx] = i;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return match;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Safely read a UTF-8 text file up to `maxBytes` (default 256 KiB).
|
|
241
|
+
* Returns `null` for missing, non-regular, oversized, unreadable, or binary files.
|
|
242
|
+
*/
|
|
243
|
+
export function safeReadTextFile(filePath, maxBytes = MAX_ROUTE_FILE_BYTES) {
|
|
244
|
+
if (typeof filePath !== "string" || !filePath || filePath.includes("\0"))
|
|
245
|
+
return null;
|
|
246
|
+
const cap = Number.isFinite(maxBytes) && maxBytes > 0 ? Math.floor(maxBytes) : MAX_ROUTE_FILE_BYTES;
|
|
247
|
+
let fd = null;
|
|
248
|
+
try {
|
|
249
|
+
fd = fs.openSync(filePath, "r");
|
|
250
|
+
const stat = fs.fstatSync(fd);
|
|
251
|
+
if (!stat.isFile() || stat.size > cap)
|
|
252
|
+
return null;
|
|
253
|
+
const buf = Buffer.allocUnsafe(cap + 1);
|
|
254
|
+
const bytesRead = fs.readSync(fd, buf, 0, cap + 1, 0);
|
|
255
|
+
if (bytesRead > cap)
|
|
256
|
+
return null;
|
|
257
|
+
const slice = buf.subarray(0, bytesRead);
|
|
258
|
+
if (slice.includes(0))
|
|
259
|
+
return null;
|
|
260
|
+
return stripLeadingBom(slice.toString("utf8"));
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
finally {
|
|
266
|
+
if (fd !== null) {
|
|
267
|
+
try {
|
|
268
|
+
fs.closeSync(fd);
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
// ignore close errors
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
function detectJsFrameworkHint(content) {
|
|
277
|
+
// Linear indexOf checks over bounded source string.
|
|
278
|
+
if (content.includes("fastify"))
|
|
279
|
+
return "fastify";
|
|
280
|
+
if (content.includes("hono"))
|
|
281
|
+
return "hono";
|
|
282
|
+
if (content.includes("@koa/router") || content.includes("koa-router") || content.includes("from 'koa'") || content.includes('from "koa"') || content.includes("require('koa')") || content.includes('require("koa")')) {
|
|
283
|
+
return "koa";
|
|
284
|
+
}
|
|
285
|
+
if (content.includes("express"))
|
|
286
|
+
return "express";
|
|
287
|
+
return "http-router";
|
|
288
|
+
}
|
|
289
|
+
function detectPythonFrameworkHint(content, decoratorName) {
|
|
290
|
+
if (content.includes("fastapi") || content.includes("FastAPI") || content.includes("APIRouter")) {
|
|
291
|
+
return "fastapi";
|
|
292
|
+
}
|
|
293
|
+
if (content.includes("flask") || content.includes("Flask") || content.includes("Blueprint")) {
|
|
294
|
+
return "flask";
|
|
295
|
+
}
|
|
296
|
+
return decoratorName === "route" ? "flask" : "fastapi";
|
|
297
|
+
}
|
|
298
|
+
function canStartJsRegex(prev) {
|
|
299
|
+
if (!prev)
|
|
300
|
+
return true;
|
|
301
|
+
if (prev.kind === "punct" || prev.kind === "other") {
|
|
302
|
+
return REGEX_PREV_PUNCT.has(prev.value);
|
|
303
|
+
}
|
|
304
|
+
if (prev.kind === "ident") {
|
|
305
|
+
return REGEX_PREV_KEYWORDS.has(prev.value);
|
|
306
|
+
}
|
|
307
|
+
return false;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Single-pass O(N) JS/TS tokenizer that skips comments, handles regex literals and
|
|
311
|
+
* template literals without backtracking, and tracks 1-based line numbers.
|
|
312
|
+
*/
|
|
313
|
+
function tokenizeJsTs(source) {
|
|
314
|
+
const tokens = [];
|
|
315
|
+
const len = source.length;
|
|
316
|
+
let i = 0;
|
|
317
|
+
let line = 1;
|
|
318
|
+
while (i < len) {
|
|
319
|
+
const ch = source.charCodeAt(i);
|
|
320
|
+
// Whitespace
|
|
321
|
+
if (ch === 32 || ch === 9 || ch === 12 || ch === 11) {
|
|
322
|
+
i++;
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
if (ch === 13) {
|
|
326
|
+
line++;
|
|
327
|
+
i += i + 1 < len && source.charCodeAt(i + 1) === 10 ? 2 : 1;
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (ch === 10) {
|
|
331
|
+
line++;
|
|
332
|
+
i++;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
// Comments or regex literal or division
|
|
336
|
+
if (ch === 47) {
|
|
337
|
+
const next = i + 1 < len ? source.charCodeAt(i + 1) : 0;
|
|
338
|
+
if (next === 47) {
|
|
339
|
+
// Line comment //
|
|
340
|
+
i += 2;
|
|
341
|
+
while (i < len) {
|
|
342
|
+
const c = source.charCodeAt(i);
|
|
343
|
+
if (c === 10 || c === 13)
|
|
344
|
+
break;
|
|
345
|
+
i++;
|
|
346
|
+
}
|
|
347
|
+
continue;
|
|
348
|
+
}
|
|
349
|
+
if (next === 42) {
|
|
350
|
+
// Block comment /* ... */
|
|
351
|
+
i += 2;
|
|
352
|
+
while (i < len) {
|
|
353
|
+
const c = source.charCodeAt(i);
|
|
354
|
+
if (c === 13) {
|
|
355
|
+
line++;
|
|
356
|
+
i += i + 1 < len && source.charCodeAt(i + 1) === 10 ? 2 : 1;
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
if (c === 10) {
|
|
360
|
+
line++;
|
|
361
|
+
i++;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (c === 42 && i + 1 < len && source.charCodeAt(i + 1) === 47) {
|
|
365
|
+
i += 2;
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
i++;
|
|
369
|
+
}
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
if (canStartJsRegex(tokens[tokens.length - 1])) {
|
|
373
|
+
// Regex literal /.../flags (cannot span lines)
|
|
374
|
+
const startLine = line;
|
|
375
|
+
i++;
|
|
376
|
+
let inClass = false;
|
|
377
|
+
let closed = false;
|
|
378
|
+
while (i < len) {
|
|
379
|
+
const c = source.charCodeAt(i);
|
|
380
|
+
if (c === 10 || c === 13)
|
|
381
|
+
break;
|
|
382
|
+
if (c === 92) {
|
|
383
|
+
// Backslash escape (cannot escape a line terminator)
|
|
384
|
+
if (i + 1 >= len) {
|
|
385
|
+
i++;
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
const nextChar = source.charCodeAt(i + 1);
|
|
389
|
+
if (nextChar === 10 || nextChar === 13) {
|
|
390
|
+
i++;
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
i += 2;
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
if (c === 91) {
|
|
397
|
+
inClass = true;
|
|
398
|
+
}
|
|
399
|
+
else if (c === 93) {
|
|
400
|
+
inClass = false;
|
|
401
|
+
}
|
|
402
|
+
else if (c === 47 && !inClass) {
|
|
403
|
+
i++;
|
|
404
|
+
closed = true;
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
i++;
|
|
408
|
+
}
|
|
409
|
+
if (closed) {
|
|
410
|
+
while (i < len && isIdentStart(source.charCodeAt(i)))
|
|
411
|
+
i++;
|
|
412
|
+
}
|
|
413
|
+
tokens.push({ kind: "other", value: "/", line: startLine });
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
tokens.push({ kind: "other", value: "/", line });
|
|
417
|
+
i++;
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
// Single or double quoted string
|
|
421
|
+
if (ch === 34 || ch === 39) {
|
|
422
|
+
const quote = ch;
|
|
423
|
+
const startLine = line;
|
|
424
|
+
i++;
|
|
425
|
+
let out = "";
|
|
426
|
+
while (i < len) {
|
|
427
|
+
const c = source.charCodeAt(i);
|
|
428
|
+
if (c === 92 && i + 1 < len) {
|
|
429
|
+
const escaped = source.charCodeAt(i + 1);
|
|
430
|
+
if (escaped === 13) {
|
|
431
|
+
line++;
|
|
432
|
+
i += i + 2 < len && source.charCodeAt(i + 2) === 10 ? 3 : 2;
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
if (escaped === 10) {
|
|
436
|
+
line++;
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
out += source[i + 1];
|
|
440
|
+
}
|
|
441
|
+
i += 2;
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
if (c === quote) {
|
|
445
|
+
i++;
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
if (c === 13) {
|
|
449
|
+
line++;
|
|
450
|
+
i += i + 1 < len && source.charCodeAt(i + 1) === 10 ? 2 : 1;
|
|
451
|
+
break;
|
|
452
|
+
}
|
|
453
|
+
if (c === 10) {
|
|
454
|
+
line++;
|
|
455
|
+
i++;
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
out += source[i];
|
|
459
|
+
i++;
|
|
460
|
+
}
|
|
461
|
+
tokens.push({ kind: "string", value: out, line: startLine });
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
// Template literal `...`
|
|
465
|
+
if (ch === 96) {
|
|
466
|
+
const startLine = line;
|
|
467
|
+
i++;
|
|
468
|
+
let out = "";
|
|
469
|
+
let hasExpr = false;
|
|
470
|
+
while (i < len) {
|
|
471
|
+
const c = source.charCodeAt(i);
|
|
472
|
+
if (c === 92 && i + 1 < len) {
|
|
473
|
+
const escaped = source.charCodeAt(i + 1);
|
|
474
|
+
if (escaped === 13) {
|
|
475
|
+
line++;
|
|
476
|
+
i += i + 2 < len && source.charCodeAt(i + 2) === 10 ? 3 : 2;
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
if (escaped === 10)
|
|
480
|
+
line++;
|
|
481
|
+
else if (!hasExpr)
|
|
482
|
+
out += source[i + 1];
|
|
483
|
+
i += 2;
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
if (c === 36 && i + 1 < len && source.charCodeAt(i + 1) === 123) {
|
|
487
|
+
// Interpolation ${ ... } — skip balanced braces linearly
|
|
488
|
+
hasExpr = true;
|
|
489
|
+
i += 2;
|
|
490
|
+
let braceDepth = 1;
|
|
491
|
+
while (i < len && braceDepth > 0) {
|
|
492
|
+
const ic = source.charCodeAt(i);
|
|
493
|
+
if (ic === 13) {
|
|
494
|
+
line++;
|
|
495
|
+
i += i + 1 < len && source.charCodeAt(i + 1) === 10 ? 2 : 1;
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
if (ic === 10) {
|
|
499
|
+
line++;
|
|
500
|
+
i++;
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
if (ic === 92) {
|
|
504
|
+
i += 2;
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
507
|
+
if (ic === 34 || ic === 39) {
|
|
508
|
+
const q = ic;
|
|
509
|
+
i++;
|
|
510
|
+
while (i < len) {
|
|
511
|
+
const sc = source.charCodeAt(i);
|
|
512
|
+
if (sc === 92) {
|
|
513
|
+
i += 2;
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
if (sc === 13) {
|
|
517
|
+
line++;
|
|
518
|
+
i += i + 1 < len && source.charCodeAt(i + 1) === 10 ? 2 : 1;
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
if (sc === 10) {
|
|
522
|
+
line++;
|
|
523
|
+
i++;
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
if (sc === q) {
|
|
527
|
+
i++;
|
|
528
|
+
break;
|
|
529
|
+
}
|
|
530
|
+
i++;
|
|
531
|
+
}
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
if (ic === 123) {
|
|
535
|
+
braceDepth++;
|
|
536
|
+
}
|
|
537
|
+
else if (ic === 125) {
|
|
538
|
+
braceDepth--;
|
|
539
|
+
}
|
|
540
|
+
i++;
|
|
541
|
+
}
|
|
542
|
+
continue;
|
|
543
|
+
}
|
|
544
|
+
if (c === 96) {
|
|
545
|
+
i++;
|
|
546
|
+
break;
|
|
547
|
+
}
|
|
548
|
+
if (c === 13) {
|
|
549
|
+
line++;
|
|
550
|
+
i += i + 1 < len && source.charCodeAt(i + 1) === 10 ? 2 : 1;
|
|
551
|
+
if (!hasExpr)
|
|
552
|
+
out += "\n";
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
if (c === 10)
|
|
556
|
+
line++;
|
|
557
|
+
if (!hasExpr)
|
|
558
|
+
out += source[i];
|
|
559
|
+
i++;
|
|
560
|
+
}
|
|
561
|
+
tokens.push({
|
|
562
|
+
kind: hasExpr ? "template" : "string",
|
|
563
|
+
value: hasExpr ? "" : out,
|
|
564
|
+
line: startLine
|
|
565
|
+
});
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
// Identifier
|
|
569
|
+
if (isIdentStart(ch)) {
|
|
570
|
+
const start = i;
|
|
571
|
+
i++;
|
|
572
|
+
while (i < len && isIdentPart(source.charCodeAt(i)))
|
|
573
|
+
i++;
|
|
574
|
+
tokens.push({ kind: "ident", value: source.slice(start, i), line });
|
|
575
|
+
continue;
|
|
576
|
+
}
|
|
577
|
+
// Numbers (consume so `.` in `1.5` is not treated as property access)
|
|
578
|
+
if (ch >= 48 && ch <= 57) {
|
|
579
|
+
const start = i;
|
|
580
|
+
i++;
|
|
581
|
+
while (i < len) {
|
|
582
|
+
const nc = source.charCodeAt(i);
|
|
583
|
+
if ((nc >= 48 && nc <= 57) || nc === 46 || nc === 95 || (nc >= 65 && nc <= 90) || (nc >= 97 && nc <= 122)) {
|
|
584
|
+
i++;
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
break;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
tokens.push({ kind: "other", value: source.slice(start, i), line });
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
// Punctuation relevant to call/object/generic syntax
|
|
594
|
+
const s = source[i];
|
|
595
|
+
if (s === "." ||
|
|
596
|
+
s === "(" ||
|
|
597
|
+
s === ")" ||
|
|
598
|
+
s === "{" ||
|
|
599
|
+
s === "}" ||
|
|
600
|
+
s === "[" ||
|
|
601
|
+
s === "]" ||
|
|
602
|
+
s === "," ||
|
|
603
|
+
s === ";" ||
|
|
604
|
+
s === ":" ||
|
|
605
|
+
s === "<" ||
|
|
606
|
+
s === ">") {
|
|
607
|
+
tokens.push({ kind: "punct", value: s, line });
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
tokens.push({ kind: "other", value: s, line });
|
|
611
|
+
}
|
|
612
|
+
i++;
|
|
613
|
+
}
|
|
614
|
+
return tokens;
|
|
615
|
+
}
|
|
616
|
+
/**
|
|
617
|
+
* Optionally skip balanced TypeScript type arguments `<...>` between a method name and `(`.
|
|
618
|
+
* Bounded to `MAX_GENERIC_TOKENS` so malformed `<` comparisons never scan far.
|
|
619
|
+
*/
|
|
620
|
+
function skipOptionalTypeArgs(tokens, startIndex) {
|
|
621
|
+
if (startIndex >= tokens.length || tokens[startIndex].kind !== "punct" || tokens[startIndex].value !== "<") {
|
|
622
|
+
return startIndex;
|
|
623
|
+
}
|
|
624
|
+
let depth = 0;
|
|
625
|
+
const limit = Math.min(tokens.length, startIndex + MAX_GENERIC_TOKENS);
|
|
626
|
+
for (let idx = startIndex; idx < limit; idx++) {
|
|
627
|
+
const tok = tokens[idx];
|
|
628
|
+
if (tok.kind === "punct") {
|
|
629
|
+
if (tok.value === ";")
|
|
630
|
+
return startIndex;
|
|
631
|
+
if (tok.value === "<")
|
|
632
|
+
depth++;
|
|
633
|
+
else if (tok.value === ">") {
|
|
634
|
+
depth--;
|
|
635
|
+
if (depth === 0)
|
|
636
|
+
return idx + 1;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
return startIndex;
|
|
641
|
+
}
|
|
642
|
+
function parseFastifyRouteObject(tokens, openBraceIndex, matchingBrace, matchingBracket) {
|
|
643
|
+
const closeBraceIndex = matchingBrace[openBraceIndex];
|
|
644
|
+
if (closeBraceIndex <= openBraceIndex) {
|
|
645
|
+
return { methods: [], path: null };
|
|
646
|
+
}
|
|
647
|
+
let routePath = null;
|
|
648
|
+
const methods = [];
|
|
649
|
+
for (let i = openBraceIndex + 1; i < closeBraceIndex; i++) {
|
|
650
|
+
const tok = tokens[i];
|
|
651
|
+
if (tok.kind === "punct" && tok.value === "{") {
|
|
652
|
+
const nestedClose = matchingBrace[i];
|
|
653
|
+
if (nestedClose > i && nestedClose < closeBraceIndex) {
|
|
654
|
+
i = nestedClose;
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
break;
|
|
658
|
+
}
|
|
659
|
+
if ((tok.kind === "ident" || tok.kind === "string") && i + 2 < closeBraceIndex) {
|
|
660
|
+
const colon = tokens[i + 1];
|
|
661
|
+
if (colon.kind !== "punct" || colon.value !== ":")
|
|
662
|
+
continue;
|
|
663
|
+
const key = tok.value.toLowerCase();
|
|
664
|
+
const val = tokens[i + 2];
|
|
665
|
+
if ((key === "url" || key === "path") && val.kind === "string" && isRoutePathLiteral(val.value)) {
|
|
666
|
+
routePath = val.value;
|
|
667
|
+
}
|
|
668
|
+
else if (key === "method") {
|
|
669
|
+
if (val.kind === "string") {
|
|
670
|
+
const upper = val.value.trim().toUpperCase();
|
|
671
|
+
if (VALID_HTTP_METHODS.has(upper))
|
|
672
|
+
methods.push(upper);
|
|
673
|
+
}
|
|
674
|
+
else if (val.kind === "punct" && val.value === "[") {
|
|
675
|
+
const closeBracket = matchingBracket[i + 2];
|
|
676
|
+
const listEnd = closeBracket > i + 2
|
|
677
|
+
? Math.min(closeBracket, i + 3 + MAX_METHOD_LIST_TOKENS)
|
|
678
|
+
: Math.min(closeBraceIndex, i + 3 + MAX_METHOD_LIST_TOKENS);
|
|
679
|
+
for (let j = i + 3; j < listEnd; j++) {
|
|
680
|
+
const item = tokens[j];
|
|
681
|
+
if (item.kind === "punct" && item.value === "]")
|
|
682
|
+
break;
|
|
683
|
+
if (item.kind === "string") {
|
|
684
|
+
const upper = item.value.trim().toUpperCase();
|
|
685
|
+
if (VALID_HTTP_METHODS.has(upper))
|
|
686
|
+
methods.push(upper);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
if (closeBracket > i + 2 && closeBracket < closeBraceIndex) {
|
|
690
|
+
i = closeBracket;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return { methods, path: routePath };
|
|
697
|
+
}
|
|
698
|
+
function isAllowedJsReceiver(prev) {
|
|
699
|
+
if (!prev)
|
|
700
|
+
return false;
|
|
701
|
+
if (prev.kind === "punct" && (prev.value === ")" || prev.value === "]"))
|
|
702
|
+
return true;
|
|
703
|
+
if (prev.kind === "ident") {
|
|
704
|
+
return !NON_ROUTER_RECEIVERS.has(prev.value.toLowerCase());
|
|
705
|
+
}
|
|
706
|
+
return false;
|
|
707
|
+
}
|
|
708
|
+
function extractJsTsRoutes(content, relPath) {
|
|
709
|
+
// Every JS/TS route path requires a string or template literal; skip tokenizing quote-free files.
|
|
710
|
+
if (!content.includes('"') && !content.includes("'") && !content.includes("`")) {
|
|
711
|
+
return [];
|
|
712
|
+
}
|
|
713
|
+
const tokens = tokenizeJsTs(content);
|
|
714
|
+
const matchingBrace = computeMatchingClosers(tokens, "{", "}");
|
|
715
|
+
const matchingBracket = computeMatchingClosers(tokens, "[", "]");
|
|
716
|
+
const framework = detectJsFrameworkHint(content);
|
|
717
|
+
const routes = [];
|
|
718
|
+
let parenDepth = 0;
|
|
719
|
+
let routeChain = null;
|
|
720
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
721
|
+
const tok = tokens[i];
|
|
722
|
+
// Track paren depth for `.route("/path").get(...).post(...)` chains
|
|
723
|
+
if (tok.kind === "punct" && tok.value === "(") {
|
|
724
|
+
if (routeChain && routeChain.state === "before_open" && parenDepth === routeChain.baseDepth) {
|
|
725
|
+
routeChain.state = "in_call";
|
|
726
|
+
}
|
|
727
|
+
parenDepth++;
|
|
728
|
+
continue;
|
|
729
|
+
}
|
|
730
|
+
if (tok.kind === "punct" && tok.value === ")") {
|
|
731
|
+
parenDepth = Math.max(0, parenDepth - 1);
|
|
732
|
+
if (routeChain && routeChain.state === "in_call" && parenDepth === routeChain.baseDepth) {
|
|
733
|
+
routeChain.state = "after_call";
|
|
734
|
+
}
|
|
735
|
+
continue;
|
|
736
|
+
}
|
|
737
|
+
if (routeChain && routeChain.state === "after_call" && parenDepth === routeChain.baseDepth) {
|
|
738
|
+
if (tok.kind === "punct" && tok.value === ".") {
|
|
739
|
+
routeChain.state = "await_method";
|
|
740
|
+
}
|
|
741
|
+
else {
|
|
742
|
+
routeChain = null;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
// Look for `.method(...)` or `.route(...)`
|
|
746
|
+
if (tok.kind !== "punct" || tok.value !== ".")
|
|
747
|
+
continue;
|
|
748
|
+
const methodTok = tokens[i + 1];
|
|
749
|
+
if (!methodTok || methodTok.kind !== "ident") {
|
|
750
|
+
if (routeChain && routeChain.state === "await_method")
|
|
751
|
+
routeChain = null;
|
|
752
|
+
continue;
|
|
753
|
+
}
|
|
754
|
+
const receiverTok = i > 0 ? tokens[i - 1] : undefined;
|
|
755
|
+
const methodLower = methodTok.value.toLowerCase();
|
|
756
|
+
const afterGeneric = skipOptionalTypeArgs(tokens, i + 2);
|
|
757
|
+
const openParen = tokens[afterGeneric];
|
|
758
|
+
if (!openParen || openParen.kind !== "punct" || openParen.value !== "(") {
|
|
759
|
+
if (routeChain && routeChain.state === "await_method")
|
|
760
|
+
routeChain = null;
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
const firstArg = tokens[afterGeneric + 1];
|
|
764
|
+
// Handle `.route("/path")` (Express/Hono chain) or `.route({ method, url })` (Fastify)
|
|
765
|
+
if (methodLower === "route" && isAllowedJsReceiver(receiverTok)) {
|
|
766
|
+
if (firstArg && firstArg.kind === "string" && isRoutePathLiteral(firstArg.value)) {
|
|
767
|
+
routeChain = {
|
|
768
|
+
path: sanitizeRoutePath(firstArg.value),
|
|
769
|
+
baseDepth: parenDepth,
|
|
770
|
+
state: "before_open"
|
|
771
|
+
};
|
|
772
|
+
}
|
|
773
|
+
else if (firstArg && firstArg.kind === "punct" && firstArg.value === "{") {
|
|
774
|
+
const parsed = parseFastifyRouteObject(tokens, afterGeneric + 1, matchingBrace, matchingBracket);
|
|
775
|
+
if (parsed.path && parsed.methods.length > 0) {
|
|
776
|
+
const safePath = sanitizeRoutePath(parsed.path);
|
|
777
|
+
for (const method of parsed.methods) {
|
|
778
|
+
routes.push({
|
|
779
|
+
method,
|
|
780
|
+
path: safePath,
|
|
781
|
+
source: relPath,
|
|
782
|
+
line: methodTok.line,
|
|
783
|
+
kind: "api",
|
|
784
|
+
framework: framework === "http-router" ? "fastify" : framework
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
routeChain = null;
|
|
789
|
+
}
|
|
790
|
+
else {
|
|
791
|
+
routeChain = null;
|
|
792
|
+
}
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
// Handle HTTP verb method calls: `.get(...)`, `.post(...)`, etc.
|
|
796
|
+
const httpMethod = JS_METHOD_MAP.get(methodLower);
|
|
797
|
+
if (!httpMethod) {
|
|
798
|
+
if (routeChain && routeChain.state === "await_method")
|
|
799
|
+
routeChain = null;
|
|
800
|
+
continue;
|
|
801
|
+
}
|
|
802
|
+
if (routeChain && routeChain.state === "await_method" && parenDepth === routeChain.baseDepth) {
|
|
803
|
+
const explicitPath = firstArg && firstArg.kind === "string" && isRoutePathLiteral(firstArg.value)
|
|
804
|
+
? sanitizeRoutePath(firstArg.value)
|
|
805
|
+
: null;
|
|
806
|
+
routes.push({
|
|
807
|
+
method: httpMethod,
|
|
808
|
+
path: explicitPath ?? routeChain.path,
|
|
809
|
+
source: relPath,
|
|
810
|
+
line: methodTok.line,
|
|
811
|
+
kind: "api",
|
|
812
|
+
framework
|
|
813
|
+
});
|
|
814
|
+
routeChain.state = "before_open";
|
|
815
|
+
continue;
|
|
816
|
+
}
|
|
817
|
+
if (!isAllowedJsReceiver(receiverTok))
|
|
818
|
+
continue;
|
|
819
|
+
if (firstArg && firstArg.kind === "string" && isRoutePathLiteral(firstArg.value)) {
|
|
820
|
+
routes.push({
|
|
821
|
+
method: httpMethod,
|
|
822
|
+
path: sanitizeRoutePath(firstArg.value),
|
|
823
|
+
source: relPath,
|
|
824
|
+
line: methodTok.line,
|
|
825
|
+
kind: "api",
|
|
826
|
+
framework
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
return routes;
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Single-pass O(N) Python tokenizer that skips `#` comments and consumes
|
|
834
|
+
* triple-quoted and single/double-quoted strings cleanly.
|
|
835
|
+
*/
|
|
836
|
+
function tokenizePython(source) {
|
|
837
|
+
const tokens = [];
|
|
838
|
+
const len = source.length;
|
|
839
|
+
let i = 0;
|
|
840
|
+
let line = 1;
|
|
841
|
+
while (i < len) {
|
|
842
|
+
const ch = source.charCodeAt(i);
|
|
843
|
+
if (ch === 32 || ch === 9 || ch === 12) {
|
|
844
|
+
i++;
|
|
845
|
+
continue;
|
|
846
|
+
}
|
|
847
|
+
if (ch === 13) {
|
|
848
|
+
line++;
|
|
849
|
+
i += i + 1 < len && source.charCodeAt(i + 1) === 10 ? 2 : 1;
|
|
850
|
+
continue;
|
|
851
|
+
}
|
|
852
|
+
if (ch === 10) {
|
|
853
|
+
line++;
|
|
854
|
+
i++;
|
|
855
|
+
continue;
|
|
856
|
+
}
|
|
857
|
+
// Line comment #
|
|
858
|
+
if (ch === 35) {
|
|
859
|
+
while (i < len) {
|
|
860
|
+
const c = source.charCodeAt(i);
|
|
861
|
+
if (c === 10 || c === 13)
|
|
862
|
+
break;
|
|
863
|
+
i++;
|
|
864
|
+
}
|
|
865
|
+
continue;
|
|
866
|
+
}
|
|
867
|
+
// Optional string prefix (r, u, b, f, rb, br, rf, fr) followed by quote
|
|
868
|
+
let prefixLen = 0;
|
|
869
|
+
if (isIdentStart(ch)) {
|
|
870
|
+
let j = i;
|
|
871
|
+
while (j < len && j - i < 2) {
|
|
872
|
+
const lower = source[j].toLowerCase();
|
|
873
|
+
if (lower === "r" || lower === "u" || lower === "b" || lower === "f") {
|
|
874
|
+
j++;
|
|
875
|
+
}
|
|
876
|
+
else {
|
|
877
|
+
break;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
if (j > i && j < len && (source.charCodeAt(j) === 34 || source.charCodeAt(j) === 39)) {
|
|
881
|
+
prefixLen = j - i;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
const quotePos = i + prefixLen;
|
|
885
|
+
const quoteChar = quotePos < len ? source.charCodeAt(quotePos) : 0;
|
|
886
|
+
if (quoteChar === 34 || quoteChar === 39) {
|
|
887
|
+
const startLine = line;
|
|
888
|
+
const isTriple = quotePos + 2 < len &&
|
|
889
|
+
source.charCodeAt(quotePos + 1) === quoteChar &&
|
|
890
|
+
source.charCodeAt(quotePos + 2) === quoteChar;
|
|
891
|
+
i = quotePos + (isTriple ? 3 : 1);
|
|
892
|
+
let out = "";
|
|
893
|
+
if (isTriple) {
|
|
894
|
+
while (i < len) {
|
|
895
|
+
const c = source.charCodeAt(i);
|
|
896
|
+
if (c === 92 && i + 1 < len) {
|
|
897
|
+
const esc = source.charCodeAt(i + 1);
|
|
898
|
+
if (esc === 13) {
|
|
899
|
+
line++;
|
|
900
|
+
i += i + 2 < len && source.charCodeAt(i + 2) === 10 ? 3 : 2;
|
|
901
|
+
continue;
|
|
902
|
+
}
|
|
903
|
+
if (esc === 10)
|
|
904
|
+
line++;
|
|
905
|
+
i += 2;
|
|
906
|
+
continue;
|
|
907
|
+
}
|
|
908
|
+
if (c === quoteChar &&
|
|
909
|
+
i + 2 < len &&
|
|
910
|
+
source.charCodeAt(i + 1) === quoteChar &&
|
|
911
|
+
source.charCodeAt(i + 2) === quoteChar) {
|
|
912
|
+
i += 3;
|
|
913
|
+
break;
|
|
914
|
+
}
|
|
915
|
+
if (c === 13) {
|
|
916
|
+
line++;
|
|
917
|
+
i += i + 1 < len && source.charCodeAt(i + 1) === 10 ? 2 : 1;
|
|
918
|
+
continue;
|
|
919
|
+
}
|
|
920
|
+
if (c === 10)
|
|
921
|
+
line++;
|
|
922
|
+
i++;
|
|
923
|
+
}
|
|
924
|
+
// Triple-quoted strings are usually docstrings, not route paths.
|
|
925
|
+
tokens.push({ kind: "other", value: '"""', line: startLine });
|
|
926
|
+
}
|
|
927
|
+
else {
|
|
928
|
+
const isRaw = prefixLen > 0 && source.slice(i - prefixLen - 1, i - 1).toLowerCase().includes("r");
|
|
929
|
+
while (i < len) {
|
|
930
|
+
const c = source.charCodeAt(i);
|
|
931
|
+
if (c === 92 && i + 1 < len && !isRaw) {
|
|
932
|
+
const esc = source.charCodeAt(i + 1);
|
|
933
|
+
if (esc === 13) {
|
|
934
|
+
line++;
|
|
935
|
+
i += i + 2 < len && source.charCodeAt(i + 2) === 10 ? 3 : 2;
|
|
936
|
+
continue;
|
|
937
|
+
}
|
|
938
|
+
if (esc === 10)
|
|
939
|
+
line++;
|
|
940
|
+
else
|
|
941
|
+
out += source[i + 1];
|
|
942
|
+
i += 2;
|
|
943
|
+
continue;
|
|
944
|
+
}
|
|
945
|
+
if (c === 92 && i + 1 < len && isRaw) {
|
|
946
|
+
const esc = source.charCodeAt(i + 1);
|
|
947
|
+
if (esc === 13) {
|
|
948
|
+
line++;
|
|
949
|
+
i += i + 2 < len && source.charCodeAt(i + 2) === 10 ? 3 : 2;
|
|
950
|
+
continue;
|
|
951
|
+
}
|
|
952
|
+
if (esc === 10) {
|
|
953
|
+
line++;
|
|
954
|
+
i += 2;
|
|
955
|
+
continue;
|
|
956
|
+
}
|
|
957
|
+
out += source[i] + source[i + 1];
|
|
958
|
+
i += 2;
|
|
959
|
+
continue;
|
|
960
|
+
}
|
|
961
|
+
if (c === quoteChar) {
|
|
962
|
+
i++;
|
|
963
|
+
break;
|
|
964
|
+
}
|
|
965
|
+
if (c === 13) {
|
|
966
|
+
line++;
|
|
967
|
+
i += i + 1 < len && source.charCodeAt(i + 1) === 10 ? 2 : 1;
|
|
968
|
+
break;
|
|
969
|
+
}
|
|
970
|
+
if (c === 10) {
|
|
971
|
+
line++;
|
|
972
|
+
i++;
|
|
973
|
+
break;
|
|
974
|
+
}
|
|
975
|
+
out += source[i];
|
|
976
|
+
i++;
|
|
977
|
+
}
|
|
978
|
+
tokens.push({ kind: "string", value: out, line: startLine });
|
|
979
|
+
}
|
|
980
|
+
continue;
|
|
981
|
+
}
|
|
982
|
+
// Identifier
|
|
983
|
+
if (isIdentStart(ch)) {
|
|
984
|
+
const start = i;
|
|
985
|
+
i++;
|
|
986
|
+
while (i < len && isIdentPart(source.charCodeAt(i)))
|
|
987
|
+
i++;
|
|
988
|
+
tokens.push({ kind: "ident", value: source.slice(start, i), line });
|
|
989
|
+
continue;
|
|
990
|
+
}
|
|
991
|
+
const s = source[i];
|
|
992
|
+
if (s === "@" ||
|
|
993
|
+
s === "." ||
|
|
994
|
+
s === "(" ||
|
|
995
|
+
s === ")" ||
|
|
996
|
+
s === "[" ||
|
|
997
|
+
s === "]" ||
|
|
998
|
+
s === "," ||
|
|
999
|
+
s === "=") {
|
|
1000
|
+
tokens.push({ kind: "punct", value: s, line });
|
|
1001
|
+
}
|
|
1002
|
+
else {
|
|
1003
|
+
tokens.push({ kind: "other", value: s, line });
|
|
1004
|
+
}
|
|
1005
|
+
i++;
|
|
1006
|
+
}
|
|
1007
|
+
return tokens;
|
|
1008
|
+
}
|
|
1009
|
+
function collectPythonRouterPrefixes(tokens, matchingParen) {
|
|
1010
|
+
const prefixes = new Map();
|
|
1011
|
+
for (let i = 0; i + 4 < tokens.length; i++) {
|
|
1012
|
+
const varTok = tokens[i];
|
|
1013
|
+
const eqTok = tokens[i + 1];
|
|
1014
|
+
if (varTok.kind !== "ident" || eqTok.kind !== "punct" || eqTok.value !== "=")
|
|
1015
|
+
continue;
|
|
1016
|
+
// Match `router = APIRouter(prefix="/v1")` or `bp = Blueprint("users", __name__, url_prefix="/users")`
|
|
1017
|
+
let ctorIdx = i + 2;
|
|
1018
|
+
while (ctorIdx + 1 < tokens.length &&
|
|
1019
|
+
tokens[ctorIdx].kind === "ident" &&
|
|
1020
|
+
tokens[ctorIdx + 1].kind === "punct" &&
|
|
1021
|
+
tokens[ctorIdx + 1].value === ".") {
|
|
1022
|
+
ctorIdx += 2;
|
|
1023
|
+
}
|
|
1024
|
+
const ctorTok = tokens[ctorIdx];
|
|
1025
|
+
const openTok = tokens[ctorIdx + 1];
|
|
1026
|
+
if (!ctorTok || ctorTok.kind !== "ident" || !openTok || openTok.kind !== "punct" || openTok.value !== "(") {
|
|
1027
|
+
continue;
|
|
1028
|
+
}
|
|
1029
|
+
if (ctorTok.value !== "APIRouter" && ctorTok.value !== "Blueprint")
|
|
1030
|
+
continue;
|
|
1031
|
+
const closeIdx = matchingParen[ctorIdx + 1];
|
|
1032
|
+
if (closeIdx <= ctorIdx + 1)
|
|
1033
|
+
continue;
|
|
1034
|
+
for (let j = ctorIdx + 2; j < closeIdx; j++) {
|
|
1035
|
+
const t = tokens[j];
|
|
1036
|
+
if (t.kind === "punct" && t.value === "(") {
|
|
1037
|
+
const nestedClose = matchingParen[j];
|
|
1038
|
+
if (nestedClose > j && nestedClose < closeIdx) {
|
|
1039
|
+
j = nestedClose;
|
|
1040
|
+
continue;
|
|
1041
|
+
}
|
|
1042
|
+
break;
|
|
1043
|
+
}
|
|
1044
|
+
if (t.kind === "ident" && (t.value === "prefix" || t.value === "url_prefix") && j + 2 < closeIdx) {
|
|
1045
|
+
const eq = tokens[j + 1];
|
|
1046
|
+
const val = tokens[j + 2];
|
|
1047
|
+
if (eq && eq.kind === "punct" && eq.value === "=" && val && val.kind === "string" && isRoutePathLiteral(val.value)) {
|
|
1048
|
+
prefixes.set(varTok.value, sanitizeRoutePath(val.value));
|
|
1049
|
+
break;
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
return prefixes;
|
|
1055
|
+
}
|
|
1056
|
+
function extractPythonRoutes(content, relPath) {
|
|
1057
|
+
const tokens = tokenizePython(content);
|
|
1058
|
+
const matchingParen = computeMatchingClosers(tokens, "(", ")");
|
|
1059
|
+
const matchingBracket = computeMatchingClosers(tokens, "[", "]");
|
|
1060
|
+
const prefixes = collectPythonRouterPrefixes(tokens, matchingParen);
|
|
1061
|
+
const routes = [];
|
|
1062
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
1063
|
+
const tok = tokens[i];
|
|
1064
|
+
if (tok.kind !== "punct" || tok.value !== "@")
|
|
1065
|
+
continue;
|
|
1066
|
+
// Parse dotted decorator target: `@app.get`, `@router.post`, `@api.bp.route`
|
|
1067
|
+
const chain = [];
|
|
1068
|
+
let cursor = i + 1;
|
|
1069
|
+
if (cursor < tokens.length && tokens[cursor].kind === "ident") {
|
|
1070
|
+
chain.push(tokens[cursor].value);
|
|
1071
|
+
cursor++;
|
|
1072
|
+
while (cursor + 1 < tokens.length &&
|
|
1073
|
+
tokens[cursor].kind === "punct" &&
|
|
1074
|
+
tokens[cursor].value === "." &&
|
|
1075
|
+
tokens[cursor + 1].kind === "ident") {
|
|
1076
|
+
chain.push(tokens[cursor + 1].value);
|
|
1077
|
+
cursor += 2;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
if (chain.length < 2)
|
|
1081
|
+
continue;
|
|
1082
|
+
const openParen = tokens[cursor];
|
|
1083
|
+
if (!openParen || openParen.kind !== "punct" || openParen.value !== "(")
|
|
1084
|
+
continue;
|
|
1085
|
+
const methodName = chain[chain.length - 1].toLowerCase();
|
|
1086
|
+
const directMethod = PY_DIRECT_METHOD_MAP.get(methodName);
|
|
1087
|
+
const isMultiRoute = PY_MULTI_ROUTE_METHODS.has(methodName);
|
|
1088
|
+
if (!directMethod && !isMultiRoute)
|
|
1089
|
+
continue;
|
|
1090
|
+
const closeIdx = matchingParen[cursor];
|
|
1091
|
+
if (closeIdx <= cursor)
|
|
1092
|
+
continue;
|
|
1093
|
+
// Scan decorator arguments at paren depth 1 in strict O(N) by jumping nested `(...)` in O(1)
|
|
1094
|
+
let argIndex = 0;
|
|
1095
|
+
let routePath = null;
|
|
1096
|
+
const explicitMethods = [];
|
|
1097
|
+
for (let j = cursor + 1; j < closeIdx; j++) {
|
|
1098
|
+
const argTok = tokens[j];
|
|
1099
|
+
if (argTok.kind === "punct" && argTok.value === "(") {
|
|
1100
|
+
const nestedClose = matchingParen[j];
|
|
1101
|
+
if (nestedClose > j && nestedClose < closeIdx) {
|
|
1102
|
+
j = nestedClose;
|
|
1103
|
+
continue;
|
|
1104
|
+
}
|
|
1105
|
+
break;
|
|
1106
|
+
}
|
|
1107
|
+
if (argTok.kind === "punct" && argTok.value === ",") {
|
|
1108
|
+
argIndex++;
|
|
1109
|
+
continue;
|
|
1110
|
+
}
|
|
1111
|
+
// First positional string argument is the route path
|
|
1112
|
+
if (argIndex === 0 && routePath === null && argTok.kind === "string" && isRoutePathLiteral(argTok.value)) {
|
|
1113
|
+
routePath = argTok.value;
|
|
1114
|
+
continue;
|
|
1115
|
+
}
|
|
1116
|
+
// Named `path="/..."` or `rule="/..."`
|
|
1117
|
+
if (argTok.kind === "ident" &&
|
|
1118
|
+
(argTok.value === "path" || argTok.value === "rule") &&
|
|
1119
|
+
j + 2 < closeIdx &&
|
|
1120
|
+
tokens[j + 1].kind === "punct" &&
|
|
1121
|
+
tokens[j + 1].value === "=" &&
|
|
1122
|
+
tokens[j + 2].kind === "string" &&
|
|
1123
|
+
isRoutePathLiteral(tokens[j + 2].value)) {
|
|
1124
|
+
routePath = tokens[j + 2].value;
|
|
1125
|
+
j += 2;
|
|
1126
|
+
continue;
|
|
1127
|
+
}
|
|
1128
|
+
// Named `methods=["GET", "POST"]` or `methods=("POST",)` or `methods="POST"`
|
|
1129
|
+
if (isMultiRoute &&
|
|
1130
|
+
argTok.kind === "ident" &&
|
|
1131
|
+
argTok.value === "methods" &&
|
|
1132
|
+
j + 2 < closeIdx &&
|
|
1133
|
+
tokens[j + 1].kind === "punct" &&
|
|
1134
|
+
tokens[j + 1].value === "=") {
|
|
1135
|
+
const rhs = tokens[j + 2];
|
|
1136
|
+
if (rhs.kind === "string") {
|
|
1137
|
+
const upper = rhs.value.trim().toUpperCase();
|
|
1138
|
+
if (VALID_HTTP_METHODS.has(upper))
|
|
1139
|
+
explicitMethods.push(upper);
|
|
1140
|
+
j += 2;
|
|
1141
|
+
}
|
|
1142
|
+
else if (rhs.kind === "punct" && (rhs.value === "[" || rhs.value === "(")) {
|
|
1143
|
+
const closeBracket = rhs.value === "[" ? "]" : ")";
|
|
1144
|
+
const matched = rhs.value === "[" ? matchingBracket[j + 2] : matchingParen[j + 2];
|
|
1145
|
+
const listEnd = matched > j + 2
|
|
1146
|
+
? Math.min(matched, j + 3 + MAX_METHOD_LIST_TOKENS)
|
|
1147
|
+
: Math.min(closeIdx, j + 3 + MAX_METHOD_LIST_TOKENS);
|
|
1148
|
+
for (let k = j + 3; k < listEnd; k++) {
|
|
1149
|
+
const item = tokens[k];
|
|
1150
|
+
if (item.kind === "punct" && item.value === closeBracket)
|
|
1151
|
+
break;
|
|
1152
|
+
if (item.kind === "string") {
|
|
1153
|
+
const upper = item.value.trim().toUpperCase();
|
|
1154
|
+
if (VALID_HTTP_METHODS.has(upper))
|
|
1155
|
+
explicitMethods.push(upper);
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
j = matched > j + 2 && matched < closeIdx ? matched : listEnd - 1;
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
if (routePath) {
|
|
1163
|
+
const receiverName = chain[chain.length - 2];
|
|
1164
|
+
const prefix = prefixes.get(receiverName) ?? "";
|
|
1165
|
+
const fullPath = sanitizeRoutePath(joinRoutePrefix(prefix, routePath));
|
|
1166
|
+
const framework = detectPythonFrameworkHint(content, methodName);
|
|
1167
|
+
const methods = directMethod ? [directMethod] : explicitMethods.length > 0 ? explicitMethods : ["GET"];
|
|
1168
|
+
for (const method of methods) {
|
|
1169
|
+
routes.push({
|
|
1170
|
+
method,
|
|
1171
|
+
path: fullPath,
|
|
1172
|
+
source: relPath,
|
|
1173
|
+
line: tok.line,
|
|
1174
|
+
kind: "api",
|
|
1175
|
+
framework
|
|
1176
|
+
});
|
|
1177
|
+
}
|
|
1178
|
+
i = closeIdx;
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
return routes;
|
|
1182
|
+
}
|
|
1183
|
+
function normalizeNextSegment(segment) {
|
|
1184
|
+
// Route groups `(marketing)` and parallel slots `@modal` do not add URL path segments.
|
|
1185
|
+
if (segment.startsWith("(") && segment.endsWith(")"))
|
|
1186
|
+
return null;
|
|
1187
|
+
if (segment.startsWith("@"))
|
|
1188
|
+
return null;
|
|
1189
|
+
// Optional catch-all `[[...slug]]` or catch-all `[...slug]`
|
|
1190
|
+
if (segment.startsWith("[[...") && segment.endsWith("]]")) {
|
|
1191
|
+
const param = segment.slice(5, -2).trim();
|
|
1192
|
+
return param ? `*${param}` : "*";
|
|
1193
|
+
}
|
|
1194
|
+
if (segment.startsWith("[...") && segment.endsWith("]")) {
|
|
1195
|
+
const param = segment.slice(4, -1).trim();
|
|
1196
|
+
return param ? `*${param}` : "*";
|
|
1197
|
+
}
|
|
1198
|
+
// Dynamic segment `[id]`
|
|
1199
|
+
if (segment.startsWith("[") && segment.endsWith("]")) {
|
|
1200
|
+
const param = segment.slice(1, -1).trim();
|
|
1201
|
+
return param ? `:${param}` : ":param";
|
|
1202
|
+
}
|
|
1203
|
+
return segment;
|
|
1204
|
+
}
|
|
1205
|
+
function buildNextAppUrlPath(rawSegmentPath) {
|
|
1206
|
+
if (!rawSegmentPath)
|
|
1207
|
+
return "/";
|
|
1208
|
+
const parts = rawSegmentPath.split("/").filter(Boolean);
|
|
1209
|
+
const urlParts = [];
|
|
1210
|
+
for (const part of parts) {
|
|
1211
|
+
if (part === "." || part === "..")
|
|
1212
|
+
return null;
|
|
1213
|
+
// Next.js App Router ignores private folders prefixed with `_`.
|
|
1214
|
+
if (part.startsWith("_"))
|
|
1215
|
+
return null;
|
|
1216
|
+
const mapped = normalizeNextSegment(part);
|
|
1217
|
+
if (mapped)
|
|
1218
|
+
urlParts.push(mapped);
|
|
1219
|
+
}
|
|
1220
|
+
return urlParts.length === 0 ? "/" : `/${urlParts.join("/")}`;
|
|
1221
|
+
}
|
|
1222
|
+
function buildNextPagesUrlPath(segments) {
|
|
1223
|
+
const mapped = [];
|
|
1224
|
+
for (let i = 0; i < segments.length; i++) {
|
|
1225
|
+
const seg = segments[i];
|
|
1226
|
+
if (seg === "." || seg === "..")
|
|
1227
|
+
return null;
|
|
1228
|
+
if (i === segments.length - 1 && seg === "index")
|
|
1229
|
+
continue;
|
|
1230
|
+
const normalized = normalizeNextSegment(seg);
|
|
1231
|
+
if (normalized)
|
|
1232
|
+
mapped.push(normalized);
|
|
1233
|
+
}
|
|
1234
|
+
return mapped.length === 0 ? "/" : `/${mapped.join("/")}`;
|
|
1235
|
+
}
|
|
1236
|
+
/**
|
|
1237
|
+
* Extract exported Next.js App Router HTTP handler verbs (`GET`, `POST`, etc.)
|
|
1238
|
+
* using the linear-time JS/TS tokenizer (ignores comments and string literals).
|
|
1239
|
+
*/
|
|
1240
|
+
function extractNextAppExportedMethods(content) {
|
|
1241
|
+
if (Buffer.byteLength(content, "utf8") > MAX_ROUTE_FILE_BYTES || containsNullByte(content)) {
|
|
1242
|
+
return [];
|
|
1243
|
+
}
|
|
1244
|
+
const tokens = tokenizeJsTs(stripLeadingBom(content));
|
|
1245
|
+
const matchingBrace = computeMatchingClosers(tokens, "{", "}");
|
|
1246
|
+
const found = [];
|
|
1247
|
+
const seen = new Set();
|
|
1248
|
+
const record = (name, line) => {
|
|
1249
|
+
if (name !== "ALL" && VALID_HTTP_METHODS.has(name) && !seen.has(name)) {
|
|
1250
|
+
seen.add(name);
|
|
1251
|
+
found.push({ method: name, line });
|
|
1252
|
+
}
|
|
1253
|
+
};
|
|
1254
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
1255
|
+
const tok = tokens[i];
|
|
1256
|
+
if (tok.kind === "ident" && tok.value === "export") {
|
|
1257
|
+
const next = tokens[i + 1];
|
|
1258
|
+
if (!next)
|
|
1259
|
+
continue;
|
|
1260
|
+
// `export async function GET(...)`
|
|
1261
|
+
if (next.kind === "ident" &&
|
|
1262
|
+
next.value === "async" &&
|
|
1263
|
+
tokens[i + 2]?.kind === "ident" &&
|
|
1264
|
+
tokens[i + 2].value === "function" &&
|
|
1265
|
+
tokens[i + 3]?.kind === "ident") {
|
|
1266
|
+
record(tokens[i + 3].value, tokens[i + 3].line);
|
|
1267
|
+
continue;
|
|
1268
|
+
}
|
|
1269
|
+
// `export function GET(...)` / `export const GET = ...` / `export let GET = ...` / `export var GET = ...`
|
|
1270
|
+
if (next.kind === "ident" &&
|
|
1271
|
+
(next.value === "function" || next.value === "const" || next.value === "let" || next.value === "var") &&
|
|
1272
|
+
tokens[i + 2]?.kind === "ident") {
|
|
1273
|
+
record(tokens[i + 2].value, tokens[i + 2].line);
|
|
1274
|
+
continue;
|
|
1275
|
+
}
|
|
1276
|
+
// `export { GET, postHandler as POST }`
|
|
1277
|
+
if (next.kind === "punct" && next.value === "{") {
|
|
1278
|
+
const closeBrace = matchingBrace[i + 1];
|
|
1279
|
+
const end = closeBrace > i + 1
|
|
1280
|
+
? closeBrace
|
|
1281
|
+
: Math.min(tokens.length, i + 2 + MAX_UNCLOSED_EXPORT_TOKENS);
|
|
1282
|
+
for (let j = i + 2; j < end; j++) {
|
|
1283
|
+
const inner = tokens[j];
|
|
1284
|
+
if (inner.kind === "punct" && inner.value === "}")
|
|
1285
|
+
break;
|
|
1286
|
+
if (inner.kind !== "ident")
|
|
1287
|
+
continue;
|
|
1288
|
+
if (j + 2 < end &&
|
|
1289
|
+
tokens[j + 1]?.kind === "ident" &&
|
|
1290
|
+
tokens[j + 1].value === "as" &&
|
|
1291
|
+
tokens[j + 2]?.kind === "ident") {
|
|
1292
|
+
record(tokens[j + 2].value, tokens[j + 2].line);
|
|
1293
|
+
j += 2;
|
|
1294
|
+
}
|
|
1295
|
+
else {
|
|
1296
|
+
record(inner.value, inner.line);
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
if (closeBrace > i + 1)
|
|
1300
|
+
i = closeBrace;
|
|
1301
|
+
continue;
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
// CommonJS fallback: `exports.GET = ...`
|
|
1305
|
+
if (tok.kind === "ident" &&
|
|
1306
|
+
tok.value === "exports" &&
|
|
1307
|
+
tokens[i + 1]?.kind === "punct" &&
|
|
1308
|
+
tokens[i + 1].value === "." &&
|
|
1309
|
+
tokens[i + 2]?.kind === "ident") {
|
|
1310
|
+
record(tokens[i + 2].value, tokens[i + 2].line);
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
return found;
|
|
1314
|
+
}
|
|
1315
|
+
/**
|
|
1316
|
+
* Inspect a Next.js Pages Router `pages/api/**` file for explicit `req.method` checks.
|
|
1317
|
+
* Falls back to `["ALL"]` when handlers accept any method without a static check.
|
|
1318
|
+
*/
|
|
1319
|
+
function extractNextPagesApiMethods(content) {
|
|
1320
|
+
if (!content || Buffer.byteLength(content, "utf8") > MAX_ROUTE_FILE_BYTES || containsNullByte(content)) {
|
|
1321
|
+
return ["ALL"];
|
|
1322
|
+
}
|
|
1323
|
+
const tokens = tokenizeJsTs(stripLeadingBom(content));
|
|
1324
|
+
const methods = [];
|
|
1325
|
+
const seen = new Set();
|
|
1326
|
+
const addMethod = (raw) => {
|
|
1327
|
+
const upper = raw.trim().toUpperCase();
|
|
1328
|
+
if (upper !== "ALL" && VALID_HTTP_METHODS.has(upper) && !seen.has(upper)) {
|
|
1329
|
+
seen.add(upper);
|
|
1330
|
+
methods.push(upper);
|
|
1331
|
+
}
|
|
1332
|
+
};
|
|
1333
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
1334
|
+
const tok = tokens[i];
|
|
1335
|
+
// Pattern: `.method === "POST"` / `.method == "GET"` / `.method !== "POST"`
|
|
1336
|
+
if (tok.kind === "punct" && tok.value === "." && tokens[i + 1]?.kind === "ident" && tokens[i + 1].value === "method") {
|
|
1337
|
+
// Look ahead up to 4 tokens for a string HTTP method literal (handles `===`, `!==`, `==`, `!=`)
|
|
1338
|
+
for (let j = i + 2; j < Math.min(tokens.length, i + 6); j++) {
|
|
1339
|
+
const candidate = tokens[j];
|
|
1340
|
+
if (candidate.kind === "punct" && (candidate.value === ";" || candidate.value === ")"))
|
|
1341
|
+
break;
|
|
1342
|
+
if (candidate.kind === "string") {
|
|
1343
|
+
addMethod(candidate.value);
|
|
1344
|
+
break;
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
// Pattern: `case "DELETE":` inside `switch (req.method)`
|
|
1349
|
+
if (tok.kind === "ident" &&
|
|
1350
|
+
tok.value === "case" &&
|
|
1351
|
+
tokens[i + 1]?.kind === "string" &&
|
|
1352
|
+
tokens[i + 2]?.kind === "punct" &&
|
|
1353
|
+
tokens[i + 2].value === ":") {
|
|
1354
|
+
addMethod(tokens[i + 1].value);
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
return methods.length > 0 ? methods : ["ALL"];
|
|
1358
|
+
}
|
|
1359
|
+
function dedupeAndSortRoutes(routes) {
|
|
1360
|
+
const seen = new Set();
|
|
1361
|
+
const out = [];
|
|
1362
|
+
for (const route of routes) {
|
|
1363
|
+
const key = `${route.kind}|${route.method}|${route.path}|${route.source}`;
|
|
1364
|
+
if (seen.has(key))
|
|
1365
|
+
continue;
|
|
1366
|
+
seen.add(key);
|
|
1367
|
+
out.push(route);
|
|
1368
|
+
}
|
|
1369
|
+
out.sort((a, b) => {
|
|
1370
|
+
if (a.kind !== b.kind)
|
|
1371
|
+
return a.kind.localeCompare(b.kind);
|
|
1372
|
+
if (a.path !== b.path)
|
|
1373
|
+
return a.path.localeCompare(b.path);
|
|
1374
|
+
if (a.method !== b.method)
|
|
1375
|
+
return a.method.localeCompare(b.method);
|
|
1376
|
+
return a.source.localeCompare(b.source);
|
|
1377
|
+
});
|
|
1378
|
+
return out;
|
|
1379
|
+
}
|
|
1380
|
+
/**
|
|
1381
|
+
* Extract HTTP routes statically from JS/TS (Express, Fastify, Hono, Koa) or
|
|
1382
|
+
* Python (FastAPI, Flask) source text in linear O(N) time.
|
|
1383
|
+
*/
|
|
1384
|
+
export function extractRoutesFromSource(content, relPath) {
|
|
1385
|
+
if (!content || typeof content !== "string")
|
|
1386
|
+
return [];
|
|
1387
|
+
if (Buffer.byteLength(content, "utf8") > MAX_ROUTE_FILE_BYTES)
|
|
1388
|
+
return [];
|
|
1389
|
+
if (containsNullByte(content))
|
|
1390
|
+
return [];
|
|
1391
|
+
const cleanContent = stripLeadingBom(content);
|
|
1392
|
+
const normalizedRel = normalizePosixPath(relPath);
|
|
1393
|
+
const ext = path.posix.extname(normalizedRel).toLowerCase();
|
|
1394
|
+
let raw = [];
|
|
1395
|
+
if (JS_EXTS.has(ext)) {
|
|
1396
|
+
raw = extractJsTsRoutes(cleanContent, normalizedRel);
|
|
1397
|
+
}
|
|
1398
|
+
else if (PY_EXTS.has(ext)) {
|
|
1399
|
+
raw = extractPythonRoutes(cleanContent, normalizedRel);
|
|
1400
|
+
}
|
|
1401
|
+
return dedupeAndSortRoutes(raw);
|
|
1402
|
+
}
|
|
1403
|
+
/**
|
|
1404
|
+
* Extract filesystem-based routes for Next.js App Router (`app/**\/route.*`, `app/**\/page.*`)
|
|
1405
|
+
* and Pages Router (`pages/api/**`, `pages/**` excluding `_app`, `_document`, `_error`).
|
|
1406
|
+
*/
|
|
1407
|
+
export function extractFileSystemRoutes(relPaths, readText) {
|
|
1408
|
+
const routes = [];
|
|
1409
|
+
for (const rawPath of relPaths) {
|
|
1410
|
+
if (typeof rawPath !== "string" || !rawPath.trim())
|
|
1411
|
+
continue;
|
|
1412
|
+
if (CONTROL_CHAR_RE.test(rawPath) || WINDOWS_DRIVE_PREFIX_RE.test(rawPath.trim()))
|
|
1413
|
+
continue;
|
|
1414
|
+
const norm = normalizePosixPath(rawPath);
|
|
1415
|
+
if (!norm || norm.split("/").includes(".."))
|
|
1416
|
+
continue;
|
|
1417
|
+
if (IGNORED_TEST_FILE_RE.test(norm))
|
|
1418
|
+
continue;
|
|
1419
|
+
// 1. Next.js App Router API route handler (`app/**/route.{ts,js,mts,mjs}`)
|
|
1420
|
+
const appRouteMatch = NEXT_APP_ROUTE_RE.exec(norm);
|
|
1421
|
+
if (appRouteMatch) {
|
|
1422
|
+
const urlPath = buildNextAppUrlPath(appRouteMatch[1]);
|
|
1423
|
+
if (!urlPath)
|
|
1424
|
+
continue;
|
|
1425
|
+
const text = readText(rawPath) ?? (norm !== rawPath ? readText(norm) : null);
|
|
1426
|
+
if (!text)
|
|
1427
|
+
continue;
|
|
1428
|
+
const exported = extractNextAppExportedMethods(text);
|
|
1429
|
+
for (const entry of exported) {
|
|
1430
|
+
routes.push({
|
|
1431
|
+
method: entry.method,
|
|
1432
|
+
path: urlPath,
|
|
1433
|
+
source: norm,
|
|
1434
|
+
line: entry.line,
|
|
1435
|
+
kind: "api",
|
|
1436
|
+
framework: "nextjs-app"
|
|
1437
|
+
});
|
|
1438
|
+
}
|
|
1439
|
+
continue;
|
|
1440
|
+
}
|
|
1441
|
+
// 2. Next.js App Router page (`app/**/page.{tsx,jsx,ts,js}`)
|
|
1442
|
+
const appPageMatch = NEXT_APP_PAGE_RE.exec(norm);
|
|
1443
|
+
if (appPageMatch) {
|
|
1444
|
+
const urlPath = buildNextAppUrlPath(appPageMatch[1]);
|
|
1445
|
+
if (!urlPath)
|
|
1446
|
+
continue;
|
|
1447
|
+
routes.push({
|
|
1448
|
+
method: "GET",
|
|
1449
|
+
path: urlPath,
|
|
1450
|
+
source: norm,
|
|
1451
|
+
kind: "web",
|
|
1452
|
+
framework: "nextjs-app"
|
|
1453
|
+
});
|
|
1454
|
+
continue;
|
|
1455
|
+
}
|
|
1456
|
+
// 3. Next.js Pages Router (`pages/api/**` and `pages/**`)
|
|
1457
|
+
const pagesMatch = NEXT_PAGES_RE.exec(norm);
|
|
1458
|
+
if (pagesMatch) {
|
|
1459
|
+
const segments = pagesMatch[1].split("/").filter(Boolean);
|
|
1460
|
+
if (segments.length === 0)
|
|
1461
|
+
continue;
|
|
1462
|
+
if (NEXT_EXCLUDED_PAGES.has(segments[0]))
|
|
1463
|
+
continue;
|
|
1464
|
+
if (segments.some((seg) => seg.startsWith("_") || seg === "." || seg === ".."))
|
|
1465
|
+
continue;
|
|
1466
|
+
const urlPath = buildNextPagesUrlPath(segments);
|
|
1467
|
+
if (!urlPath)
|
|
1468
|
+
continue;
|
|
1469
|
+
if (segments[0] === "api") {
|
|
1470
|
+
const text = readText(rawPath) ?? (norm !== rawPath ? readText(norm) : null);
|
|
1471
|
+
const methods = extractNextPagesApiMethods(text);
|
|
1472
|
+
for (const method of methods) {
|
|
1473
|
+
routes.push({
|
|
1474
|
+
method,
|
|
1475
|
+
path: urlPath,
|
|
1476
|
+
source: norm,
|
|
1477
|
+
kind: "api",
|
|
1478
|
+
framework: "nextjs-pages"
|
|
1479
|
+
});
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
else {
|
|
1483
|
+
routes.push({
|
|
1484
|
+
method: "GET",
|
|
1485
|
+
path: urlPath,
|
|
1486
|
+
source: norm,
|
|
1487
|
+
kind: "web",
|
|
1488
|
+
framework: "nextjs-pages"
|
|
1489
|
+
});
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
return dedupeAndSortRoutes(routes);
|
|
1494
|
+
}
|