mirror of
https://github.com/librespeed/speedtest.git
synced 2026-10-22 16:20:56 +00:00
Some checks are pending
* 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>
57 lines
1.4 KiB
JavaScript
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");
|