speedtest/tests/e2e/static-server.js
Copilot bd8501ad13
Some checks are pending
Docker / e2e (push) Waiting to run
Docker / build (./Dockerfile, , ghcr.io/${{ github.repository }}) (push) Blocked by required conditions
Docker / build (./Dockerfile.alpine, -alpine, ghcr.io/${{ github.repository }}) (push) Blocked by required conditions
Allow frontend Docker mode to use SERVER_LIST_URL without a mounted /servers.json (#849)
* Initial plan

* Fix SERVER_LIST_URL frontend startup

Co-authored-by: sstidl <12804296+sstidl@users.noreply.github.com>

* test(e2e): serve remote server-list fixture with CORS

* test(e2e): reject malformed static-server paths

* fix: support SERVER_LIST_URL in classic frontend

* test: preserve existing mode test formatting

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: sstidl <12804296+sstidl@users.noreply.github.com>
Co-authored-by: Stefan Stidl <sti-github@stidl.com>
2026-09-12 00:30:17 +02:00

57 lines
1.4 KiB
JavaScript

const fs = require("node:fs");
const http = require("node:http");
const path = require("node:path");
const root = process.cwd();
const contentTypes = {
".css": "text/css; charset=utf-8",
".html": "text/html; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".svg": "image/svg+xml",
".woff2": "font/woff2"
};
http
.createServer((request, response) => {
response.setHeader("Access-Control-Allow-Origin", "*");
if (request.method === "OPTIONS") {
response.writeHead(204);
response.end();
return;
}
let pathname;
try {
const url = new URL(request.url, "http://127.0.0.1");
pathname = url.pathname === "/" ? "/index.html" : url.pathname;
pathname = decodeURIComponent(pathname);
} catch {
response.writeHead(400);
response.end();
return;
}
const file = path.resolve(root, `.${pathname}`);
if (!file.startsWith(`${root}${path.sep}`)) {
response.writeHead(403);
response.end();
return;
}
fs.readFile(file, (error, content) => {
if (error) {
response.writeHead(error.code === "ENOENT" ? 404 : 500);
response.end();
return;
}
response.writeHead(200, {
"Content-Type": contentTypes[path.extname(file)] || "application/octet-stream"
});
response.end(content);
});
})
.listen(18184, "127.0.0.1");