codebase-snapshot 0.12.15 → 0.13.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/CHANGELOG.md +933 -3
- package/README.md +14 -3
- package/SECURITY.md +2 -2
- package/dist/bin.js +9884 -6034
- package/dist/{chunk-FZB2FTHF.js → chunk-BOJYZQFB.js} +7651 -747
- package/dist/chunk-NESEHOPU.js +266 -0
- package/dist/cli.cjs +9930 -6127
- package/dist/cli.d.cts +115 -2
- package/dist/cli.d.ts +115 -2
- package/dist/cli.js +389 -8
- package/dist/{generate-worker-BK4uJx7Z.d.cts → generate-worker-B79yGkQO.d.cts} +16 -0
- package/dist/{generate-worker-BK4uJx7Z.d.ts → generate-worker-B79yGkQO.d.ts} +16 -0
- package/dist/index.cjs +7389 -680
- package/dist/index.d.cts +145 -6
- package/dist/index.d.ts +145 -6
- package/dist/index.js +1 -1
- package/dist/mcp.cjs +3335 -1075
- package/dist/mcp.d.cts +86 -4
- package/dist/mcp.d.ts +86 -4
- package/dist/mcp.js +2 -2
- package/dist/{types-c0tLF2WC.d.cts → types-CbD8yQRZ.d.cts} +145 -1
- package/dist/{types-c0tLF2WC.d.ts → types-CbD8yQRZ.d.ts} +145 -1
- package/docs/agent.md +1 -1
- package/docs/benchmarks.md +80 -0
- package/docs/budget.md +59 -0
- package/docs/cli.md +41 -4
- package/docs/configuration.md +27 -0
- package/docs/delta.md +50 -0
- package/docs/deploy.md +75 -0
- package/docs/health.md +13 -0
- package/docs/mcp.md +112 -11
- package/docs/migration.md +68 -0
- package/docs/output.md +9 -3
- package/docs/remote.md +60 -0
- package/docs/security-review.md +117 -0
- package/docs/serve.md +66 -0
- package/docs/signing.md +61 -0
- package/docs/slice.md +57 -0
- package/docs/tools.md +161 -0
- package/docs/troubleshooting.md +63 -0
- package/docs/vs-repomix.md +1 -1
- package/docs/workspace.md +93 -0
- package/package.json +1 -1
- package/dist/chunk-EPAXW6QJ.js +0 -3749
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { createServer, buildTools, GENERATOR_VERSION } from './chunk-BOJYZQFB.js';
|
|
2
|
+
import * as http from 'http';
|
|
3
|
+
import * as crypto from 'crypto';
|
|
4
|
+
|
|
5
|
+
var DEFAULT_PORT = 4789;
|
|
6
|
+
var DEFAULT_MAX_BODY = 2 * 1024 * 1024;
|
|
7
|
+
var RATE_LIMIT = 60;
|
|
8
|
+
var RATE_WINDOW_MS = 1e4;
|
|
9
|
+
function generateToken() {
|
|
10
|
+
return crypto.randomBytes(24).toString("base64url");
|
|
11
|
+
}
|
|
12
|
+
function isLoopbackOrigin(origin) {
|
|
13
|
+
try {
|
|
14
|
+
const url = new URL(origin);
|
|
15
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return false;
|
|
16
|
+
const host = url.hostname;
|
|
17
|
+
return host === "localhost" || host === "127.0.0.1" || host === "[::1]" || host === "::1";
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function timingSafeEqual2(left, right) {
|
|
23
|
+
const a = Buffer.from(left, "utf8");
|
|
24
|
+
const b = Buffer.from(right, "utf8");
|
|
25
|
+
if (a.length !== b.length) return false;
|
|
26
|
+
return crypto.timingSafeEqual(a, b);
|
|
27
|
+
}
|
|
28
|
+
async function startHttpTransport(options) {
|
|
29
|
+
const host = options.host ?? "127.0.0.1";
|
|
30
|
+
const port = options.port ?? DEFAULT_PORT;
|
|
31
|
+
const log = options.log ?? ((line) => process.stderr.write(line + "\n"));
|
|
32
|
+
const token = options.token === void 0 ? generateToken() : options.token;
|
|
33
|
+
const maxBody = options.maxBodyBytes ?? DEFAULT_MAX_BODY;
|
|
34
|
+
const extraOrigins = options.allowedOrigins ?? [];
|
|
35
|
+
if (token === null && host !== "127.0.0.1" && host !== "localhost" && host !== "::1") {
|
|
36
|
+
throw new Error(
|
|
37
|
+
"Refusing to serve " + host + " without a token: a repository reachable from the network must require Authorization. Drop --no-auth, or bind the loopback address."
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
const server = createServer({ cwd: options.cwd, log });
|
|
41
|
+
const toolCount = buildTools().length;
|
|
42
|
+
const sseSessions = /* @__PURE__ */ new Map();
|
|
43
|
+
const tokenAccepted = (request, url) => {
|
|
44
|
+
if (token === null) return true;
|
|
45
|
+
const header = request.headers.authorization ?? "";
|
|
46
|
+
const match = /^Bearer\s+(.+)$/i.exec(header);
|
|
47
|
+
if (match !== null && timingSafeEqual2(match[1].trim(), token)) return true;
|
|
48
|
+
const fromQuery = url.searchParams.get("token");
|
|
49
|
+
return fromQuery !== null && timingSafeEqual2(fromQuery, token);
|
|
50
|
+
};
|
|
51
|
+
const sessions = /* @__PURE__ */ new Map();
|
|
52
|
+
const allowedOrigin = (origin) => {
|
|
53
|
+
if (origin === void 0) return null;
|
|
54
|
+
if (isLoopbackOrigin(origin)) return origin;
|
|
55
|
+
return extraOrigins.includes(origin) ? origin : null;
|
|
56
|
+
};
|
|
57
|
+
const httpServer = http.createServer((request, response) => {
|
|
58
|
+
const send = (status, body, extra = {}) => {
|
|
59
|
+
const text = typeof body === "string" ? body : JSON.stringify(body);
|
|
60
|
+
response.writeHead(status, {
|
|
61
|
+
"content-type": typeof body === "string" ? "text/plain; charset=utf-8" : "application/json",
|
|
62
|
+
"content-length": Buffer.byteLength(text, "utf8"),
|
|
63
|
+
"x-content-type-options": "nosniff",
|
|
64
|
+
...extra
|
|
65
|
+
});
|
|
66
|
+
response.end(text);
|
|
67
|
+
};
|
|
68
|
+
const origin = typeof request.headers.origin === "string" ? request.headers.origin : void 0;
|
|
69
|
+
const allowed = allowedOrigin(origin);
|
|
70
|
+
if (origin !== void 0 && allowed === null) {
|
|
71
|
+
send(403, { error: "origin not allowed" });
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const cors = {};
|
|
75
|
+
if (allowed !== null) {
|
|
76
|
+
cors["access-control-allow-origin"] = allowed;
|
|
77
|
+
cors["vary"] = "origin";
|
|
78
|
+
cors["access-control-allow-headers"] = "authorization, content-type, mcp-protocol-version, mcp-session-id";
|
|
79
|
+
cors["access-control-allow-methods"] = "POST, GET, OPTIONS";
|
|
80
|
+
cors["access-control-max-age"] = "600";
|
|
81
|
+
}
|
|
82
|
+
if (request.method === "OPTIONS") {
|
|
83
|
+
send(204, "", cors);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (request.method === "GET" && request.url === "/healthz") {
|
|
87
|
+
send(
|
|
88
|
+
200,
|
|
89
|
+
{
|
|
90
|
+
ok: true,
|
|
91
|
+
name: "codebase-snapshot",
|
|
92
|
+
version: GENERATOR_VERSION,
|
|
93
|
+
tools: toolCount,
|
|
94
|
+
uptimeSeconds: Math.round(process.uptime())
|
|
95
|
+
},
|
|
96
|
+
cors
|
|
97
|
+
);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const url = new URL(request.url ?? "/", "http://localhost");
|
|
101
|
+
if (request.method === "GET" && url.pathname === "/sse") {
|
|
102
|
+
if (!tokenAccepted(request, url)) {
|
|
103
|
+
send(401, { error: "unauthorized" }, { ...cors, "www-authenticate": 'Bearer realm="codebase-snapshot"' });
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const id = crypto.randomBytes(12).toString("hex");
|
|
107
|
+
response.writeHead(200, {
|
|
108
|
+
...cors,
|
|
109
|
+
"content-type": "text/event-stream",
|
|
110
|
+
"cache-control": "no-cache, no-transform",
|
|
111
|
+
connection: "keep-alive",
|
|
112
|
+
"x-accel-buffering": "no"
|
|
113
|
+
});
|
|
114
|
+
const write = (payload) => {
|
|
115
|
+
response.write(payload);
|
|
116
|
+
};
|
|
117
|
+
sseSessions.set(id, { write, end: () => response.end() });
|
|
118
|
+
const heartbeat = setInterval(() => write(": ping\n\n"), 15e3);
|
|
119
|
+
const cleanup = () => {
|
|
120
|
+
clearInterval(heartbeat);
|
|
121
|
+
sseSessions.delete(id);
|
|
122
|
+
};
|
|
123
|
+
request.on("close", cleanup);
|
|
124
|
+
response.on("close", cleanup);
|
|
125
|
+
write("event: endpoint\ndata: /messages?sessionId=" + id + "\n\n");
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (request.method === "POST" && url.pathname === "/messages") {
|
|
129
|
+
if (!tokenAccepted(request, url)) {
|
|
130
|
+
send(401, { error: "unauthorized" }, { ...cors, "www-authenticate": 'Bearer realm="codebase-snapshot"' });
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const id = url.searchParams.get("sessionId") ?? "";
|
|
134
|
+
const stream = sseSessions.get(id);
|
|
135
|
+
if (stream === void 0) {
|
|
136
|
+
send(404, { error: "unknown sessionId; open GET /sse first" });
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const chunks2 = [];
|
|
140
|
+
let received2 = 0;
|
|
141
|
+
request.on("data", (chunk) => {
|
|
142
|
+
received2 += chunk.length;
|
|
143
|
+
if (received2 > maxBody) {
|
|
144
|
+
request.destroy();
|
|
145
|
+
send(413, { error: "request body too large", limit: maxBody });
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
chunks2.push(chunk);
|
|
149
|
+
});
|
|
150
|
+
request.on("end", () => {
|
|
151
|
+
void (async () => {
|
|
152
|
+
let message;
|
|
153
|
+
try {
|
|
154
|
+
message = JSON.parse(Buffer.concat(chunks2).toString("utf8"));
|
|
155
|
+
} catch (error) {
|
|
156
|
+
send(400, { jsonrpc: "2.0", id: null, error: { code: -32700, message: String(error) } });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const answer = await server.handle(message);
|
|
160
|
+
if (answer !== null) stream.write("event: message\ndata: " + JSON.stringify(answer) + "\n\n");
|
|
161
|
+
send(202, "", cors);
|
|
162
|
+
})();
|
|
163
|
+
});
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (request.url === "/") {
|
|
167
|
+
send(
|
|
168
|
+
200,
|
|
169
|
+
"codebase-snapshot MCP server\n\nPOST JSON-RPC 2.0 to /mcp. Health: GET /healthz.\n",
|
|
170
|
+
cors
|
|
171
|
+
);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (request.method !== "POST" || request.url !== "/mcp") {
|
|
175
|
+
send(405, { error: "method not allowed" }, { ...cors, allow: "POST, OPTIONS" });
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (token !== null) {
|
|
179
|
+
const header = request.headers.authorization ?? "";
|
|
180
|
+
const match = /^Bearer\s+(.+)$/i.exec(header);
|
|
181
|
+
if (match === null || !timingSafeEqual2(match[1].trim(), token)) {
|
|
182
|
+
send(401, { error: "unauthorized" }, { ...cors, "www-authenticate": 'Bearer realm="codebase-snapshot"' });
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const remote = request.socket.remoteAddress ?? "unknown";
|
|
187
|
+
const now = Date.now();
|
|
188
|
+
const window = sessions.get(remote);
|
|
189
|
+
if (window === void 0 || window.resetAt <= now) sessions.set(remote, { count: 1, resetAt: now + RATE_WINDOW_MS });
|
|
190
|
+
else if (window.count >= RATE_LIMIT) {
|
|
191
|
+
send(429, { error: "too many requests" }, { ...cors, "retry-after": "10" });
|
|
192
|
+
return;
|
|
193
|
+
} else window.count += 1;
|
|
194
|
+
let received = 0;
|
|
195
|
+
const chunks = [];
|
|
196
|
+
request.on("data", (chunk) => {
|
|
197
|
+
received += chunk.length;
|
|
198
|
+
if (received > maxBody) {
|
|
199
|
+
request.destroy();
|
|
200
|
+
send(413, { error: "request body too large", limit: maxBody }, cors);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
chunks.push(chunk);
|
|
204
|
+
});
|
|
205
|
+
request.on("end", () => {
|
|
206
|
+
void (async () => {
|
|
207
|
+
let message;
|
|
208
|
+
try {
|
|
209
|
+
message = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
210
|
+
} catch (error) {
|
|
211
|
+
send(400, { jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error: " + error.message } }, cors);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const answers = [];
|
|
215
|
+
const batch = Array.isArray(message) ? message : [message];
|
|
216
|
+
for (const item of batch) {
|
|
217
|
+
const answer = await server.handle(item);
|
|
218
|
+
if (answer !== null) answers.push(answer);
|
|
219
|
+
}
|
|
220
|
+
if (answers.length === 0) {
|
|
221
|
+
send(202, "", cors);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
send(200, Array.isArray(message) ? answers : answers[0], cors);
|
|
225
|
+
})().catch((error) => {
|
|
226
|
+
log("mcp/http: " + error.message);
|
|
227
|
+
send(500, { jsonrpc: "2.0", id: null, error: { code: -32603, message: "Internal error" } }, cors);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
request.on("error", () => {
|
|
231
|
+
try {
|
|
232
|
+
response.destroy();
|
|
233
|
+
} catch {
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
await new Promise((resolve, reject) => {
|
|
238
|
+
httpServer.once("error", reject);
|
|
239
|
+
httpServer.listen(port, host, () => {
|
|
240
|
+
httpServer.removeListener("error", reject);
|
|
241
|
+
resolve();
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
const address = httpServer.address();
|
|
245
|
+
const actualPort = typeof address === "object" && address !== null ? address.port : port;
|
|
246
|
+
const displayHost = host === "0.0.0.0" || host === "::" ? "127.0.0.1" : host;
|
|
247
|
+
return {
|
|
248
|
+
url: "http://" + displayHost + ":" + actualPort + "/mcp",
|
|
249
|
+
port: actualPort,
|
|
250
|
+
host,
|
|
251
|
+
token,
|
|
252
|
+
close: () => new Promise((resolve) => {
|
|
253
|
+
for (const [, session] of sseSessions) {
|
|
254
|
+
try {
|
|
255
|
+
session.end();
|
|
256
|
+
} catch {
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
sseSessions.clear();
|
|
260
|
+
httpServer.close(() => resolve());
|
|
261
|
+
httpServer.closeAllConnections();
|
|
262
|
+
})
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export { generateToken, startHttpTransport };
|