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

Function startStaticServer

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

Source from the content-addressed store, hash-verified

667// ─────────────────────────────────────────────────────────────────────────────
668
669export function startStaticServer(config) {
670 const route = createRouter(config.routes);
671 const proxy = createProxyHandlers({ label: `static:${config.port}` });
672 const dirAbs = resolve(config.dir);
673 const policy = applySessionKeyPolicy({
674 host: config.host,
675 sessionApiKey: config.sessionApiKey || null,
676 authRequired: config.authRequired || false,
677 allowLanSessionKey: config.allowLanSessionKey || false,
678 });
679 const injectionOpts = {
680 sessionApiKey: policy.sessionApiKey,
681 authRequired: policy.authRequired,
682 runtimeServicesInfo: config.runtimeServicesInfo || null,
683 lockToCloud: config.lockToCloud || null,
684 basePath: normalizeBasePath(config.basePath),
685 vscodeBasePath: config.vscodeBasePath || null,
686 disableTelemetry: config.disableTelemetry || false,
687 };
688 const basePath = injectionOpts.basePath;
689 const rejectPrefixes = config.rejectPrefixes ?? [];
690 const noReferrerPrefixes = config.noReferrerPrefixes ?? [];
691 const staticMiddleware = createStaticMiddleware(dirAbs);
692
693 const uninstallDiagnostics = proxy.installDiagnostics();
694 // DigitalOcean Managed Agents for the browser: with MARS_WEB=1, host the MARS
695 // bridge and proxies to sessions' tunnel listeners, behind the session key
696 // (see mars-web-bridge.mjs). Deliberately `config.sessionApiKey`, not
697 // `policy.sessionApiKey`: the policy decides whether the key may be baked
698 // into the HTML on a LAN bind (Docker binds `::` and strips it), while the
699 // bridge only ever checks a key the client sends — the one the user types
700 // on the API key screen. A LAN bind with no key at all refuses to mount.
701 const marsWeb = mountMarsWebBridge({
702 host: config.host,
703 sessionApiKey: config.sessionApiKey || null,
704 });
705
706 const server = createServer((req, res) => {
707 const url = req.url ?? "/";
708 if (marsWeb?.handleHttp(req, res)) return;
709 const backend = route(url);
710 if (backend) {
711 // The editor is advertised as `<origin><prefix>/?tkn=<token>`, and that
712 // token is agent-server's session key. The workbench loads webviews,
713 // previews and extension content from that document, so without this a
714 // Referer carrying the key rides along on those subrequests.
715 if (matchesAnyPrefix(url, noReferrerPrefixes)) {
716 res.setHeader("Referrer-Policy", "no-referrer");
717 }
718 if (
719 config.runtimeServicesInfo &&
720 isServerInfoRequest(req) &&
721 (req.method === "GET" || req.method === "HEAD")
722 ) {
723 proxyServerInfoRequest(req, res, backend, config.runtimeServicesInfo);
724 return;
725 }
726 proxy.proxyHttp(req, res, backend);

Callers 1

static-server.mjsFile · 0.70

Calls 12

createRouterFunction · 0.90
createProxyHandlersFunction · 0.90
applySessionKeyPolicyFunction · 0.90
mountMarsWebBridgeFunction · 0.90
isServerInfoRequestFunction · 0.90
proxyServerInfoRequestFunction · 0.90
createStaticMiddlewareFunction · 0.85
matchesAnyPrefixFunction · 0.85
handleStaticFunction · 0.85
errorMethod · 0.80
normalizeBasePathFunction · 0.70
resolveFunction · 0.50

Tested by

no test coverage detected