(config)
| 541 | // ───────────────────────────────────────────────────────────────────────────── |
| 542 | |
| 543 | export function startStaticServer(config) { |
| 544 | const route = createRouter(config.routes); |
| 545 | const proxy = createProxyHandlers({ label: `static:${config.port}` }); |
| 546 | const dirAbs = resolve(config.dir); |
| 547 | const injectionOpts = { |
| 548 | sessionApiKey: config.sessionApiKey || null, |
| 549 | authRequired: config.authRequired || false, |
| 550 | runtimeServicesInfo: config.runtimeServicesInfo || null, |
| 551 | lockToCloud: config.lockToCloud || null, |
| 552 | basePath: normalizeBasePath(config.basePath), |
| 553 | }; |
| 554 | const basePath = injectionOpts.basePath; |
| 555 | const rejectPrefixes = config.rejectPrefixes ?? []; |
| 556 | const staticMiddleware = createStaticMiddleware(dirAbs); |
| 557 | |
| 558 | const uninstallDiagnostics = proxy.installDiagnostics(); |
| 559 | |
| 560 | const server = createServer((req, res) => { |
| 561 | const backend = route(req.url ?? "/"); |
| 562 | if (backend) { |
| 563 | if ( |
| 564 | config.runtimeServicesInfo && |
| 565 | isServerInfoRequest(req) && |
| 566 | (req.method === "GET" || req.method === "HEAD") |
| 567 | ) { |
| 568 | proxyServerInfoRequest(req, res, backend, config.runtimeServicesInfo); |
| 569 | return; |
| 570 | } |
| 571 | proxy.proxyHttp(req, res, backend); |
| 572 | return; |
| 573 | } |
| 574 | handleStatic( |
| 575 | req, |
| 576 | res, |
| 577 | dirAbs, |
| 578 | staticMiddleware, |
| 579 | injectionOpts, |
| 580 | rejectPrefixes, |
| 581 | basePath, |
| 582 | ).catch((err) => { |
| 583 | console.error(`Static handler error for ${req.url}:`, err); |
| 584 | if (!res.headersSent) { |
| 585 | res.writeHead(500); |
| 586 | res.end("Internal Server Error"); |
| 587 | } |
| 588 | }); |
| 589 | }); |
| 590 | |
| 591 | server.on("upgrade", (req, socket, head) => { |
| 592 | const backend = route(req.url ?? "/"); |
| 593 | if (backend) { |
| 594 | proxy.proxyWebSocket(req, socket, head, backend); |
| 595 | return; |
| 596 | } |
| 597 | socket.destroy(); |
| 598 | }); |
| 599 | server.on("close", uninstallDiagnostics); |
| 600 |
no test coverage detected