MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / dispatchPathRoutes

Function dispatchPathRoutes

src/ui-server/api/index.ts:372–412  ·  view source on GitHub ↗

* The two endpoints that carry their argument in the path. * * `ctx.pathname` is already percent-decoded, so a node id or a file path * containing `/` (`file:src/a.ts`) arrives whole — the remainder after the * prefix IS the argument, slashes and all. Node ids are opaque: they go * straight to

(
  route: string,
  res: Parameters<UiApiHandler>[1],
  ctx: UiRequestContext,
  session: GraphSession
)

Source from the content-addressed store, hash-verified

370 * paths go through the read chokepoint before anything is opened.
371 */
372async function dispatchPathRoutes(
373 route: string,
374 res: Parameters<UiApiHandler>[1],
375 ctx: UiRequestContext,
376 session: GraphSession
377): Promise<boolean> {
378 const nodeId = suffixAfter(route, '/api/node/');
379 if (nodeId !== null) {
380 if (nodeId === '') throw badRequest('No symbol id was given. Use /api/node/<id>.');
381 return ok(res, await buildNode(session.acquire(), ctx.projectRoot, nodeId), ctx.method);
382 }
383
384 // Before `/api/file/`: that prefix is not a prefix of this route, but keeping
385 // the more specific one first means adding another `/api/file…` sibling later
386 // cannot silently start matching the shorter one.
387 const codePath = suffixAfter(route, '/api/filecode/');
388 if (codePath !== null) {
389 return ok(res, buildFileCode(session.acquire(), ctx.projectRoot, codePath), ctx.method);
390 }
391
392 const filePath = suffixAfter(route, '/api/file/');
393 if (filePath !== null) {
394 if (filePath === '') throw badRequest('No file path was given. Use /api/file/<path>.');
395 return ok(res, buildFile(session.acquire(), ctx.projectRoot, filePath), ctx.method);
396 }
397
398 // `/api/node` and `/api/file` with no argument at all, so the message can say
399 // what the endpoint wants instead of falling through to a bare 404.
400 if (route === '/api/filecode') {
401 throw badRequest('/api/filecode needs an argument: /api/filecode/<path>.');
402 }
403
404 if (route === '/api/node' || route === '/api/file') {
405 throw badRequest(`${route} needs an argument: ${route}/<${route.endsWith('node') ? 'id' : 'path'}>.`);
406 }
407
408 throw notFound(
409 `No such endpoint: ${route}`,
410 'GET /api lists everything this server answers.'
411 );
412}
413
414/** Drop a single trailing slash, so `/api/stats/` and `/api/stats` are one route. */
415function normalize(pathname: string): string {

Callers 1

handlerFunction · 0.85

Calls 8

badRequestFunction · 0.90
okFunction · 0.90
buildNodeFunction · 0.90
buildFileCodeFunction · 0.90
buildFileFunction · 0.90
notFoundFunction · 0.90
suffixAfterFunction · 0.85
acquireMethod · 0.45

Tested by

no test coverage detected