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

Function startStaticServer

scripts/static-server.mjs:606–702  ·  view source on GitHub ↗
(config)

Source from the content-addressed store, hash-verified

604// ─────────────────────────────────────────────────────────────────────────────
605
606export function startStaticServer(config) {
607 const route = createRouter(config.routes);
608 const proxy = createProxyHandlers({ label: `static:${config.port}` });
609 const dirAbs = resolve(config.dir);
610 const injectionOpts = {
611 sessionApiKey: config.sessionApiKey || null,
612 authRequired: config.authRequired || false,
613 runtimeServicesInfo: config.runtimeServicesInfo || null,
614 lockToCloud: config.lockToCloud || null,
615 basePath: normalizeBasePath(config.basePath),
616 vscodeBasePath: config.vscodeBasePath || null,
617 };
618 const basePath = injectionOpts.basePath;
619 const rejectPrefixes = config.rejectPrefixes ?? [];
620 const noReferrerPrefixes = config.noReferrerPrefixes ?? [];
621 const staticMiddleware = createStaticMiddleware(dirAbs);
622
623 const uninstallDiagnostics = proxy.installDiagnostics();
624
625 const server = createServer((req, res) => {
626 const url = req.url ?? "/";
627 const backend = route(url);
628 if (backend) {
629 // The editor is advertised as `<origin><prefix>/?tkn=<token>`, and that
630 // token is agent-server's session key. The workbench loads webviews,
631 // previews and extension content from that document, so without this a
632 // Referer carrying the key rides along on those subrequests.
633 if (matchesAnyPrefix(url, noReferrerPrefixes)) {
634 res.setHeader("Referrer-Policy", "no-referrer");
635 }
636 if (
637 config.runtimeServicesInfo &&
638 isServerInfoRequest(req) &&
639 (req.method === "GET" || req.method === "HEAD")
640 ) {
641 proxyServerInfoRequest(req, res, backend, config.runtimeServicesInfo);
642 return;
643 }
644 proxy.proxyHttp(req, res, backend);
645 return;
646 }
647 handleStatic(
648 req,
649 res,
650 dirAbs,
651 staticMiddleware,
652 injectionOpts,
653 rejectPrefixes,
654 basePath,
655 ).catch((err) => {
656 console.error(`Static handler error for ${req.url}:`, err);
657 if (!res.headersSent) {
658 res.writeHead(500);
659 res.end("Internal Server Error");
660 }
661 });
662 });
663

Callers 1

static-server.mjsFile · 0.70

Calls 9

createRouterFunction · 0.90
createProxyHandlersFunction · 0.90
isServerInfoRequestFunction · 0.90
proxyServerInfoRequestFunction · 0.90
createStaticMiddlewareFunction · 0.85
matchesAnyPrefixFunction · 0.85
handleStaticFunction · 0.85
errorMethod · 0.80
normalizeBasePathFunction · 0.70

Tested by

no test coverage detected