makecode
Version:
MakeCode (PXT) - web-cached build tool
60 lines • 2.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.startSimServer = void 0;
const http = require("http");
const fs = require("fs");
const simloaderfiles_1 = require("makecode-core/built/simloaderfiles");
const mime = {
js: "application/javascript",
css: "text/css",
html: "text/html",
};
function startSimServer(ed, port = 7001, forceLocal = false) {
http.createServer(async (request, response) => {
let path = request.url;
if (path == "/")
path = "/index.html";
path = path.replace(/.*\//, "");
path = path.replace(/\?.*/, "");
let buf = null;
if (path == "binary.js") {
try {
buf = fs.readFileSync("built/binary.js");
}
catch (_a) { }
}
else if (simloaderfiles_1.simloaderFiles.hasOwnProperty(path)) {
if (forceLocal || path != "loader.js")
try {
buf = fs.readFileSync("assets/" + path);
}
catch (_b) {
try {
buf = fs.readFileSync("assets/js/" + path);
}
catch (_c) { }
}
if (!buf)
buf = Buffer.from(simloaderfiles_1.simloaderFiles[path], "utf-8");
}
else if (/^[\w\.\-]+$/.test(path)) {
buf = await ed.cache.getAsync(ed.website + "-" + path);
if (!buf)
buf = await ed.cache.getAsync(path);
}
if (buf) {
const m = mime[path.replace(/.*\./, "")] || "application/octet-stream";
response.writeHead(200, {
"Content-type": m,
"Cache-Control": "no-cache",
});
response.end(buf);
}
else {
response.writeHead(404, { "Content-type": "text/plain" });
response.end("Not found");
}
}).listen(port, "127.0.0.1");
}
exports.startSimServer = startSimServer;
//# sourceMappingURL=simserver.js.map