MCPcopy Create free account
hub / github.com/OpenHands/OpenHands / serveInjectedIndexHtml

Function serveInjectedIndexHtml

scripts/static-server.mjs:334–380  ·  view source on GitHub ↗

* Serve index.html with runtime config injected into . * Returns true if the response was written, false if the file was not found.

(
  req,
  res,
  indexPath,
  {
    sessionApiKey,
    authRequired,
    runtimeServicesInfo,
    lockToCloud,
    basePath,
  } = {},
)

Source from the content-addressed store, hash-verified

332 * Returns true if the response was written, false if the file was not found.
333 */
334async function serveInjectedIndexHtml(
335 req,
336 res,
337 indexPath,
338 {
339 sessionApiKey,
340 authRequired,
341 runtimeServicesInfo,
342 lockToCloud,
343 basePath,
344 } = {},
345) {
346 let content;
347 try {
348 content = await readFile(indexPath, "utf8");
349 } catch {
350 return false;
351 }
352
353 const script = makeConfigInjectionScript(
354 sessionApiKey,
355 authRequired,
356 runtimeServicesInfo,
357 lockToCloud,
358 basePath,
359 );
360 // Inject right before </head> so the key is available before any app code runs.
361 // replace() targets the first (and only) </head> in well-formed HTML.
362 const injected = content.includes("</head>")
363 ? content.replace("</head>", `${script}\n</head>`)
364 : content.includes("</body>")
365 ? content.replace("</body>", `${script}\n</body>`)
366 : script + content;
367
368 const buf = Buffer.from(injected, "utf8");
369 res.writeHead(200, {
370 "Content-Type": "text/html; charset=utf-8",
371 "Content-Length": buf.length,
372 "Cache-Control": "no-cache",
373 });
374 if (req.method === "HEAD") {
375 res.end();
376 } else {
377 res.end(buf);
378 }
379 return true;
380}
381
382// ─────────────────────────────────────────────────────────────────────────────
383// Static file serving

Callers 1

handleStaticFunction · 0.85

Calls 1

Tested by

no test coverage detected