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