codebase-snapshot 0.20.5 → 0.20.7

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.
@@ -1,4 +1,4 @@
1
- import { buildTools, createServer, GENERATOR_VERSION } from './chunk-C3WVC4FV.js';
1
+ import { buildTools, createServer, GENERATOR_VERSION } from './chunk-RSWAYB6U.js';
2
2
  import * as http from 'http';
3
3
  import * as crypto from 'crypto';
4
4
 
@@ -8,6 +8,7 @@ var RATE_LIMIT = 60;
8
8
  var RATE_WINDOW_MS = 1e4;
9
9
  var MAX_IN_FLIGHT = 4;
10
10
  var MAX_SESSION_ENTRIES = 64;
11
+ var MAX_SSE_SESSIONS = 64;
11
12
  var MAX_RATE_WINDOWS = 1024;
12
13
  function generateToken() {
13
14
  return crypto.randomBytes(24).toString("base64url");
@@ -45,10 +46,7 @@ async function startHttpTransport(options) {
45
46
  const mcpSessions = /* @__PURE__ */ new Map();
46
47
  const socketServers = /* @__PURE__ */ new WeakMap();
47
48
  const rememberSession = (id, server) => {
48
- if (!mcpSessions.has(id) && mcpSessions.size >= MAX_SESSION_ENTRIES) {
49
- const oldest = mcpSessions.keys().next().value;
50
- if (oldest !== void 0) mcpSessions.delete(oldest);
51
- }
49
+ if (!mcpSessions.has(id) && mcpSessions.size >= MAX_SESSION_ENTRIES) return;
52
50
  mcpSessions.set(id, server);
53
51
  };
54
52
  const anonymousServers = /* @__PURE__ */ new Map();
@@ -195,6 +193,10 @@ async function startHttpTransport(options) {
195
193
  return;
196
194
  }
197
195
  if (request.method === "GET" && request.url === "/metrics") {
196
+ if (token !== null && host !== "127.0.0.1" && host !== "localhost" && host !== "::1" && !tokenAccepted(request, new URL(request.url, "http://localhost"))) {
197
+ send(401, { error: "unauthorized" }, { "www-authenticate": 'Bearer realm="codebase-snapshot"' });
198
+ return;
199
+ }
198
200
  const lines = [
199
201
  "# HELP snapshot_uptime_seconds Seconds since the process started.",
200
202
  "# TYPE snapshot_uptime_seconds gauge",
@@ -232,6 +234,10 @@ async function startHttpTransport(options) {
232
234
  send(429, { error: "too many requests" }, { ...cors, "retry-after": "10" });
233
235
  return;
234
236
  }
237
+ if (sseSessions.size >= MAX_SSE_SESSIONS) {
238
+ send(503, { error: "too many open SSE sessions" }, { ...cors, "retry-after": "10" });
239
+ return;
240
+ }
235
241
  const id = crypto.randomBytes(12).toString("hex");
236
242
  response.writeHead(200, {
237
243
  ...cors,
@@ -241,9 +247,21 @@ async function startHttpTransport(options) {
241
247
  "x-accel-buffering": "no"
242
248
  });
243
249
  const write = (payload) => {
244
- response.write(payload);
250
+ try {
251
+ response.write(payload);
252
+ } catch {
253
+ }
245
254
  };
246
- sseSessions.set(id, { write, end: () => response.end(), server: createMcp() });
255
+ sseSessions.set(id, {
256
+ write,
257
+ end: () => {
258
+ try {
259
+ response.end();
260
+ } catch {
261
+ }
262
+ },
263
+ server: createMcp()
264
+ });
247
265
  const heartbeat = setInterval(() => write(": ping\n\n"), 15e3);
248
266
  const cleanup = () => {
249
267
  clearInterval(heartbeat);
@@ -308,7 +326,13 @@ async function startHttpTransport(options) {
308
326
  if (gated) releaseToolSlot();
309
327
  }
310
328
  send(202, "", cors);
311
- })();
329
+ })().catch((error) => {
330
+ log("mcp/http: " + error.message);
331
+ try {
332
+ send(500, { jsonrpc: "2.0", id: null, error: { code: -32603, message: "Internal error" } }, cors);
333
+ } catch {
334
+ }
335
+ });
312
336
  });
313
337
  return;
314
338
  }