Made File.readFile async.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3667 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2021-10-06 01:25:33 +00:00
parent 24a91219c1
commit 470814f147
5 changed files with 196 additions and 144 deletions

View File

@ -120,43 +120,48 @@ function authHandler(request, response) {
response.writeHead(303, {"Location": formData.return, "Set-Cookie": cookie});
response.end();
} else {
var html = new TextDecoder("UTF-8").decode(File.readFile("core/auth.html"));
var contents = "";
File.readFile("core/auth.html").then(function(data) {
var html = new TextDecoder("UTF-8").decode(data);
var contents = "";
if (entry) {
if (sessionIsNew) {
contents += '<div>Welcome back, ' + entry.name + '.</div>\n';
if (entry) {
if (sessionIsNew) {
contents += '<div>Welcome back, ' + entry.name + '.</div>\n';
} else {
contents += '<div>You are already logged in, ' + entry.name + '.</div>\n';
}
contents += '<div><a href="/login/logout">Logout</a></div>\n';
} else {
contents += '<div>You are already logged in, ' + entry.name + '.</div>\n';
contents += '<form method="POST">\n';
if (loginError) {
contents += "<p>" + loginError + "</p>\n";
}
contents += '<div id="auth_greeting"><b>Halt. Who goes there?</b></div>\n'
contents += '<div id="auth">\n';
contents += '<div id="auth_login">\n'
if (noAdministrator()) {
contents += '<div class="notice">There is currently no administrator. You will be made administrator.</div>\n';
}
contents += '<div><label for="name">Name:</label> <input type="text" id="name" name="name" value=""></div>\n';
contents += '<div><label for="password">Password:</label> <input type="password" id="password" name="password" value=""></div>\n';
contents += '<div id="confirmPassword" style="display: none"><label for="confirm">Confirm:</label> <input type="password" id="confirm" name="confirm" value=""></div>\n';
contents += '<div><input type="checkbox" id="register" name="register" value="1" onchange="showHideConfirm()"> <label for="register">Register a new account</label></div>\n';
contents += '<div><input id="loginButton" type="submit" name="submit" value="Login"></div>\n';
contents += '</div>';
contents += '<div class="auth_or"> - or - </div>';
contents += '<div id="auth_guest">\n';
contents += '<input id="guestButton" type="submit" name="submit" value="Proceeed as Guest">\n';
contents += '</div>\n';
contents += '</div>\n';
contents += '</form>';
}
contents += '<div><a href="/login/logout">Logout</a></div>\n';
} else {
contents += '<form method="POST">\n';
if (loginError) {
contents += "<p>" + loginError + "</p>\n";
}
contents += '<div id="auth_greeting"><b>Halt. Who goes there?</b></div>\n'
contents += '<div id="auth">\n';
contents += '<div id="auth_login">\n'
if (noAdministrator()) {
contents += '<div class="notice">There is currently no administrator. You will be made administrator.</div>\n';
}
contents += '<div><label for="name">Name:</label> <input type="text" id="name" name="name" value=""></div>\n';
contents += '<div><label for="password">Password:</label> <input type="password" id="password" name="password" value=""></div>\n';
contents += '<div id="confirmPassword" style="display: none"><label for="confirm">Confirm:</label> <input type="password" id="confirm" name="confirm" value=""></div>\n';
contents += '<div><input type="checkbox" id="register" name="register" value="1" onchange="showHideConfirm()"> <label for="register">Register a new account</label></div>\n';
contents += '<div><input id="loginButton" type="submit" name="submit" value="Login"></div>\n';
contents += '</div>';
contents += '<div class="auth_or"> - or - </div>';
contents += '<div id="auth_guest">\n';
contents += '<input id="guestButton" type="submit" name="submit" value="Proceeed as Guest">\n';
contents += '</div>\n';
contents += '</div>\n';
contents += '</form>';
}
var text = html.replace("<!--SESSION-->", contents);
response.writeHead(200, {"Content-Type": "text/html; charset=utf-8", "Set-Cookie": cookie, "Content-Length": text.length});
response.end(text);
var text = html.replace("<!--SESSION-->", contents);
response.writeHead(200, {"Content-Type": "text/html; charset=utf-8", "Set-Cookie": cookie, "Content-Length": text.length});
response.end(text);
}).catch(function(error) {
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Connection": "close"});
response.end("404 File not found");
});
}
} else if (request.uri == "/login/logout") {
removeSession(session);

View File

@ -138,48 +138,14 @@ async function getSessionProcessBlob(blobId, session, options) {
return getProcessBlob(blobId, 'session_' + session, actualOptions);
}
function readFileUtf8(fileName) {
let data = File.readFile(fileName);
async function readFileUtf8(fileName) {
let data = await File.readFile(fileName);
data = utf8Decode(data);
return data;
}
let gManifestCache = {};
async function getManifest(fileName) {
let oldEntry = gManifestCache[fileName];
let stat = await File.stat(fileName);
if (oldEntry) {
if (oldEntry.stat.mtime == stat.mtime && oldEntry.stat.size == stat.size) {
return oldEntry.manifest;
}
}
let manifest = [];
let lines = readFileUtf8(fileName).split("\n").map(x => x.trimRight());
for (let i = 0; i < lines.length; i++) {
if (lines[i].substring(0, 4) == "//! ") {
manifest.push(lines[i].substring(4));
}
}
let result;
try {
if (manifest.length) {
result = JSON.parse(manifest.join("\n"));
}
} catch (error) {
print("ERROR: getManifest(" + fileName + "): ", error);
// Oh well. No manifest.
}
gManifestCache[fileName] = {
stat: stat,
manifest: result,
};
return result;
}
async function getProcessBlob(blobId, key, options) {
var process = gProcesses[key];
if (!process
@ -319,15 +285,6 @@ function setGlobalSettings(settings) {
}
}
try {
var data = readFileUtf8(kGlobalSettingsFile);
if (data) {
gGlobalSettings = JSON.parse(data);
}
} catch (error) {
print("Error loading settings from " + kGlobalSettingsFile + ": " + error);
}
var kStaticFiles = [
{uri: '/', path: 'index.html', type: 'text/html; charset=UTF-8'},
{uri: '/style.css', path: 'style.css', type: 'text/css; charset=UTF-8'},
@ -351,7 +308,7 @@ function startsWithBytes(data, bytes) {
async function staticFileHandler(request, response, blobId, uri) {
for (var i in kStaticFiles) {
if (uri === kStaticFiles[i].uri) {
var data = File.readFile("core/" + kStaticFiles[i].path);
var data = await File.readFile("core/" + kStaticFiles[i].path);
response.writeHead(200, {"Content-Type": kStaticFiles[i].type, "Content-Length": data.byteLength});
response.end(data);
return;
@ -403,7 +360,7 @@ async function blobHandler(request, response, blobId, uri) {
for (var i in kStaticFiles) {
if (uri === kStaticFiles[i].uri) {
found = true;
var data = File.readFile("core/" + kStaticFiles[i].path);
var data = await File.readFile("core/" + kStaticFiles[i].path);
response.writeHead(200, {"Content-Type": kStaticFiles[i].type, "Content-Length": data.byteLength});
response.end(data);
break;
@ -483,41 +440,56 @@ ssb.onConnectionsChanged = function() {
broadcastEvent('onConnectionsChanged', []);
}
var auth = require("auth");
var httpd = require("httpd");
httpd.all("/login", auth.handler);
httpd.all("", function(request, response) {
var match;
if (request.uri === "/" || request.uri === "") {
response.writeHead(303, {"Location": 'http://' + request.headers.host + gGlobalSettings.index, "Content-Length": "0"});
return response.end();
} else if (match = /^(\/~[^\/]+\/[^\/]+)(\/?.*)$/.exec(request.uri)) {
return blobHandler(request, response, match[1], match[2]);
} else if (match = /^\/([&\%][^\.]{44}(?:\.\w+)?)(\/?.*)/.exec(request.uri)) {
return blobHandler(request, response, match[1], match[2]);
} else if (match = /^\/static(\/.*)/.exec(request.uri)) {
return staticFileHandler(request, response, null, match[1]);
} else if (match = /^(.*)(\/save)$/.exec(request.uri)) {
return blobHandler(request, response, match[1], match[2]);
} else if (match = /^\/trace$/.exec(request.uri)) {
var data = trace();
response.writeHead(404, {"Content-Type": "application/json; charset=utf-8", "Content-Length": data.length.toString()});
return response.end(data);
} else if (request.uri == "/robots.txt") {
return blobHandler(request, response, null, request.uri);
} else if ((match = /^\/.well-known\/(.*)/.exec(request.uri)) && request.uri.indexOf("..") == -1) {
var data = File.readFile("data/global/.well-known/" + match[1]);
async function loadSettings() {
try {
var data = await readFileUtf8(kGlobalSettingsFile);
if (data) {
response.writeHead(200, {"Content-Type": "text/plain", "Content-Length": data.length});
response.end(data);
} else {
response.writeHead(404, {"Content-Type": "text/plain", "Content-Length": "File not found".length});
response.end("File not found");
gGlobalSettings = JSON.parse(data);
}
} else {
var data = "File not found.";
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": data.length.toString()});
return response.end(data);
} catch (error) {
print("Error loading settings from " + kGlobalSettingsFile + ": " + error);
}
}
loadSettings().then(function() {
var auth = require("auth");
var httpd = require("httpd");
httpd.all("/login", auth.handler);
httpd.all("", function(request, response) {
var match;
if (request.uri === "/" || request.uri === "") {
response.writeHead(303, {"Location": 'http://' + request.headers.host + gGlobalSettings.index, "Content-Length": "0"});
return response.end();
} else if (match = /^(\/~[^\/]+\/[^\/]+)(\/?.*)$/.exec(request.uri)) {
return blobHandler(request, response, match[1], match[2]);
} else if (match = /^\/([&\%][^\.]{44}(?:\.\w+)?)(\/?.*)/.exec(request.uri)) {
return blobHandler(request, response, match[1], match[2]);
} else if (match = /^\/static(\/.*)/.exec(request.uri)) {
return staticFileHandler(request, response, null, match[1]);
} else if (match = /^(.*)(\/save)$/.exec(request.uri)) {
return blobHandler(request, response, match[1], match[2]);
} else if (match = /^\/trace$/.exec(request.uri)) {
var data = trace();
response.writeHead(404, {"Content-Type": "application/json; charset=utf-8", "Content-Length": data.length.toString()});
return response.end(data);
} else if (request.uri == "/robots.txt") {
return blobHandler(request, response, null, request.uri);
} else if ((match = /^\/.well-known\/(.*)/.exec(request.uri)) && request.uri.indexOf("..") == -1) {
var data = File.readFile("data/global/.well-known/" + match[1]);
if (data) {
response.writeHead(200, {"Content-Type": "text/plain", "Content-Length": data.length});
response.end(data);
} else {
response.writeHead(404, {"Content-Type": "text/plain", "Content-Length": "File not found".length});
response.end("File not found");
}
} else {
var data = "File not found.";
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": data.length.toString()});
return response.end(data);
}
});
httpd.registerSocketHandler("/app/socket", app.socket);
}).catch(function(error) {
print('Failed to load settings.');
});
httpd.registerSocketHandler("/app/socket", app.socket);