* 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,
vscodeBasePath,
disableTelemetry,
} = {},
)
| 452 | * Returns true if the response was written, false if the file was not found. |
| 453 | */ |
| 454 | async function serveInjectedIndexHtml( |
| 455 | req, |
| 456 | res, |
| 457 | indexPath, |
| 458 | { |
| 459 | sessionApiKey, |
| 460 | authRequired, |
| 461 | runtimeServicesInfo, |
| 462 | lockToCloud, |
| 463 | basePath, |
| 464 | vscodeBasePath, |
| 465 | disableTelemetry, |
| 466 | } = {}, |
| 467 | ) { |
| 468 | let content; |
| 469 | try { |
| 470 | content = await readFile(indexPath, "utf8"); |
| 471 | } catch { |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | const script = makeConfigInjectionScript( |
| 476 | sessionApiKey, |
| 477 | authRequired, |
| 478 | runtimeServicesInfo, |
| 479 | lockToCloud, |
| 480 | basePath, |
| 481 | vscodeBasePath, |
| 482 | disableTelemetry, |
| 483 | ); |
| 484 | // Inject right before </head> so the key is available before any app code runs. |
| 485 | // replace() targets the first (and only) </head> in well-formed HTML. |
| 486 | const injected = content.includes("</head>") |
| 487 | ? content.replace("</head>", `${script}\n</head>`) |
| 488 | : content.includes("</body>") |
| 489 | ? content.replace("</body>", `${script}\n</body>`) |
| 490 | : script + content; |
| 491 | |
| 492 | const buf = Buffer.from(injected, "utf8"); |
| 493 | res.writeHead(200, { |
| 494 | "Content-Type": "text/html; charset=utf-8", |
| 495 | "Content-Length": buf.length, |
| 496 | "Cache-Control": sessionApiKey ? "no-store" : "no-cache", |
| 497 | }); |
| 498 | if (req.method === "HEAD") { |
| 499 | res.end(); |
| 500 | } else { |
| 501 | res.end(buf); |
| 502 | } |
| 503 | return true; |
| 504 | } |
| 505 | |
| 506 | // ───────────────────────────────────────────────────────────────────────────── |
| 507 | // Static file serving |
no test coverage detected