2016-03-12 13:50:43 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var auth = require("auth");
|
2021-01-02 13:10:00 -05:00
|
|
|
var app = require("app");
|
2016-03-12 13:50:43 -05:00
|
|
|
|
|
|
|
var gProcessIndex = 0;
|
|
|
|
var gProcesses = {};
|
2022-02-26 16:17:15 -05:00
|
|
|
var gStatsTimer = false;
|
2016-03-12 13:50:43 -05:00
|
|
|
|
|
|
|
var gGlobalSettings = {
|
2022-01-17 17:00:42 -05:00
|
|
|
index: "/~core/index",
|
2016-03-12 13:50:43 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var kGlobalSettingsFile = "data/global/settings.json";
|
|
|
|
|
|
|
|
var kPingInterval = 60 * 1000;
|
|
|
|
|
|
|
|
function printError(out, error) {
|
|
|
|
if (error.stackTrace) {
|
|
|
|
out.print(error.fileName + ":" + error.lineNumber + ": " + error.message);
|
|
|
|
out.print(error.stackTrace);
|
|
|
|
} else {
|
|
|
|
for (var i in error) {
|
|
|
|
out.print(i);
|
|
|
|
}
|
|
|
|
out.print(error.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
function invoke(handlers, argv) {
|
|
|
|
var promises = [];
|
|
|
|
if (handlers) {
|
|
|
|
for (var i = 0; i < handlers.length; ++i) {
|
|
|
|
try {
|
|
|
|
promises.push(handlers[i](...argv));
|
|
|
|
} catch (error) {
|
|
|
|
handlers.splice(i, 1);
|
|
|
|
i--;
|
|
|
|
promises.push(new Promise(function(resolve, reject) { reject(error); }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
2016-03-12 13:50:43 -05:00
|
|
|
function broadcastEvent(eventName, argv) {
|
|
|
|
var promises = [];
|
|
|
|
for (var i in gProcesses) {
|
|
|
|
var process = gProcesses[i];
|
2021-01-02 13:10:00 -05:00
|
|
|
if (process.eventHandlers[eventName]) {
|
|
|
|
promises.push(invoke(process.eventHandlers[eventName], argv));
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
return Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
|
|
|
function broadcast(message) {
|
|
|
|
var sender = this;
|
|
|
|
var promises = [];
|
|
|
|
for (var i in gProcesses) {
|
|
|
|
var process = gProcesses[i];
|
|
|
|
if (process != sender
|
|
|
|
&& process.packageOwner == sender.packageOwner
|
|
|
|
&& process.packageName == sender.packageName) {
|
|
|
|
var from = getUser(process, sender);
|
|
|
|
promises.push(postMessageInternal(from, process, message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUser(caller, process) {
|
|
|
|
return {
|
|
|
|
name: process.userName,
|
2016-03-12 13:55:55 -05:00
|
|
|
key: process.key,
|
2016-03-12 13:50:43 -05:00
|
|
|
index: process.index,
|
|
|
|
packageOwner: process.packageOwner,
|
|
|
|
packageName: process.packageName,
|
|
|
|
credentials: process.credentials,
|
|
|
|
postMessage: postMessageInternal.bind(caller, caller, process),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-13 17:03:12 -05:00
|
|
|
function getApps(user, process) {
|
|
|
|
if (process.credentials &&
|
|
|
|
process.credentials.session &&
|
|
|
|
process.credentials.session.name) {
|
|
|
|
if (user && user !== process.credentials.session.name && user !== 'core') {
|
|
|
|
return {};
|
|
|
|
} else if (!user) {
|
|
|
|
user = process.credentials.session.name;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
2022-02-13 17:03:12 -05:00
|
|
|
if (user) {
|
|
|
|
var db = new Database(user);
|
|
|
|
try {
|
|
|
|
var names = JSON.parse(db.get('apps'));
|
|
|
|
return Object.fromEntries(names.map(name => [name, db.get('path:' + name)]));
|
|
|
|
} catch {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function postMessageInternal(from, to, message) {
|
2021-01-02 13:10:00 -05:00
|
|
|
if (to.eventHandlers['message']) {
|
|
|
|
return invoke(to.eventHandlers['message'], [getUser(from, from), message]);
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
async function getSessionProcessBlob(blobId, session, options) {
|
|
|
|
var actualOptions = {timeout: kPingInterval};
|
2016-03-12 13:50:43 -05:00
|
|
|
if (options) {
|
|
|
|
for (var i in options) {
|
|
|
|
actualOptions[i] = options[i];
|
|
|
|
}
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
return getProcessBlob(blobId, 'session_' + session, actualOptions);
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2017-01-12 20:04:00 -05:00
|
|
|
let gManifestCache = {};
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
async function getProcessBlob(blobId, key, options) {
|
2016-03-12 13:50:43 -05:00
|
|
|
var process = gProcesses[key];
|
|
|
|
if (!process
|
2021-01-02 13:10:00 -05:00
|
|
|
&& !(options && "create" in options && !options.create)) {
|
2016-03-12 13:50:43 -05:00
|
|
|
try {
|
2021-01-02 13:10:00 -05:00
|
|
|
print("Creating task for " + blobId + " " + key);
|
2016-03-12 13:50:43 -05:00
|
|
|
process = {};
|
|
|
|
process.key = key;
|
|
|
|
process.index = gProcessIndex++;
|
2021-01-02 13:10:00 -05:00
|
|
|
process.userName = 'user' + process.index;
|
2016-03-12 13:50:43 -05:00
|
|
|
process.credentials = options.credentials || {};
|
|
|
|
process.task = new Task();
|
|
|
|
process.eventHandlers = {};
|
2022-03-17 21:24:29 -04:00
|
|
|
process.app = new app.App();
|
2016-03-12 13:50:43 -05:00
|
|
|
process.lastActive = Date.now();
|
|
|
|
process.lastPing = null;
|
|
|
|
process.timeout = options.timeout;
|
2022-02-26 16:17:15 -05:00
|
|
|
process.stats = false;
|
2016-03-12 13:50:43 -05:00
|
|
|
var resolveReady;
|
|
|
|
var rejectReady;
|
|
|
|
process.ready = new Promise(function(resolve, reject) {
|
|
|
|
resolveReady = resolve;
|
|
|
|
rejectReady = reject;
|
|
|
|
});
|
|
|
|
gProcesses[key] = process;
|
|
|
|
process.task.onExit = function(exitCode, terminationSignal) {
|
|
|
|
broadcastEvent('onSessionEnd', [getUser(process, process)]);
|
2021-01-02 13:10:00 -05:00
|
|
|
process.task = null;
|
2016-03-12 13:50:43 -05:00
|
|
|
delete gProcesses[key];
|
|
|
|
};
|
|
|
|
var imports = {
|
|
|
|
'core': {
|
|
|
|
'broadcast': broadcast.bind(process),
|
|
|
|
'register': function(eventName, handler) {
|
|
|
|
if (!process.eventHandlers[eventName]) {
|
|
|
|
process.eventHandlers[eventName] = [];
|
|
|
|
}
|
|
|
|
process.eventHandlers[eventName].push(handler);
|
|
|
|
},
|
2022-03-27 15:53:02 -04:00
|
|
|
'unregister': function(eventName, handler) {
|
2021-01-02 13:10:00 -05:00
|
|
|
if (process.eventHandlers[eventName]) {
|
2016-04-03 15:31:03 -04:00
|
|
|
let index = process.eventHandlers[eventName].indexOf(handler);
|
|
|
|
if (index != -1) {
|
|
|
|
process.eventHandlers[eventName].splice(index, 1);
|
|
|
|
}
|
|
|
|
if (process.eventHandlers[eventName].length == 0) {
|
|
|
|
delete process.eventHandlers[eventName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-03-12 13:50:43 -05:00
|
|
|
'user': getUser(process, process),
|
2022-02-13 17:03:12 -05:00
|
|
|
'apps': user => getApps(user, process),
|
2016-04-30 06:50:43 -04:00
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
};
|
|
|
|
if (options.api) {
|
|
|
|
imports.app = {};
|
|
|
|
for (let i in options.api) {
|
|
|
|
let api = options.api[i];
|
|
|
|
imports.app[api[0]] = process.app.makeFunction(api);
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
2022-02-13 17:39:22 -05:00
|
|
|
process.task.onPrint = function(args) {
|
|
|
|
process.app.send({action: 'print', args: args});
|
|
|
|
};
|
2021-01-02 13:10:00 -05:00
|
|
|
process.task.onError = function(error) {
|
|
|
|
try {
|
|
|
|
process.app.send({action: 'error', error: error});
|
|
|
|
} catch(e) {
|
|
|
|
print(e);
|
2016-04-16 17:30:52 -04:00
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
};
|
|
|
|
imports.ssb = Object.fromEntries(Object.keys(ssb).map(key => [key, ssb[key].bind(ssb)]));
|
|
|
|
if (process.credentials &&
|
|
|
|
process.credentials.session &&
|
|
|
|
process.credentials.session.name) {
|
|
|
|
imports.database = function(key) {
|
|
|
|
var db = new Database(process.credentials.session.name + ':' + key);
|
|
|
|
return Object.fromEntries(Object.keys(db).map(x => [x, db[x].bind(db)]));
|
|
|
|
};
|
2016-04-09 11:24:48 -04:00
|
|
|
}
|
2022-03-15 20:23:14 -04:00
|
|
|
if (options.packageOwner && options.packageName) {
|
|
|
|
imports.shared_database = function(key) {
|
|
|
|
var db = new Database(':shared:' + options.packageOwner + ':' + options.packageName + ':' + key);
|
|
|
|
return Object.fromEntries(Object.keys(db).map(x => [x, db[x].bind(db)]));
|
|
|
|
}
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
process.task.setImports(imports);
|
|
|
|
process.task.activate();
|
2021-01-09 18:06:33 -05:00
|
|
|
let source = await getBlobOrContent(blobId);
|
2022-01-30 16:09:32 -05:00
|
|
|
var appSourceName = blobId;
|
2021-01-02 13:10:00 -05:00
|
|
|
var appSource = utf8Decode(source);
|
|
|
|
try {
|
2022-03-17 21:24:29 -04:00
|
|
|
var appObject = JSON.parse(appSource);
|
|
|
|
if (appObject.type == "tildefriends-app") {
|
2022-01-30 16:09:32 -05:00
|
|
|
appSourceName = 'app.js';
|
2022-03-17 21:24:29 -04:00
|
|
|
var id = appObject.files[appSourceName];
|
2021-01-09 18:06:33 -05:00
|
|
|
var blob = await getBlobOrContent(id);
|
2021-01-02 13:10:00 -05:00
|
|
|
appSource = utf8Decode(blob);
|
2022-03-17 21:24:29 -04:00
|
|
|
await Promise.all(Object.keys(appObject.files).map(async function(f) {
|
|
|
|
await process.task.loadFile([f, await getBlobOrContent(appObject.files[f])]);
|
2021-01-02 13:10:00 -05:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
printError({print: print}, e);
|
|
|
|
}
|
2017-05-23 13:47:48 -04:00
|
|
|
broadcastEvent('onSessionBegin', [getUser(process, process)]);
|
|
|
|
resolveReady(process);
|
2021-01-02 13:10:00 -05:00
|
|
|
if (process.app) {
|
|
|
|
process.app.send({action: "ready"});
|
2017-05-23 13:47:48 -04:00
|
|
|
}
|
2022-01-30 16:09:32 -05:00
|
|
|
await process.task.execute({name: appSourceName, source: appSource});
|
2016-03-12 13:50:43 -05:00
|
|
|
} catch (error) {
|
2022-01-02 21:25:11 -05:00
|
|
|
if (process.app) {
|
|
|
|
process.app.send({action: 'error', error: error});
|
|
|
|
} else {
|
|
|
|
printError({print: print}, error);
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
rejectReady();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return process;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setGlobalSettings(settings) {
|
2021-10-27 19:27:21 -04:00
|
|
|
gGlobalSettings = settings;
|
2022-01-17 21:50:46 -05:00
|
|
|
try {
|
2022-02-13 17:03:12 -05:00
|
|
|
return new Database('core').set('settings', JSON.stringify(settings));
|
2022-01-17 21:50:46 -05:00
|
|
|
} catch (error) {
|
|
|
|
print('Error storing settings:', error);
|
|
|
|
}
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
var kStaticFiles = [
|
|
|
|
{uri: '/', path: 'index.html', type: 'text/html; charset=UTF-8'},
|
2021-12-22 09:27:52 -05:00
|
|
|
{uri: '/style.css', type: 'text/css; charset=UTF-8'},
|
|
|
|
{uri: '/favicon.png', type: 'image/png'},
|
|
|
|
{uri: '/client.js', type: 'text/javascript; charset=UTF-8'},
|
|
|
|
{uri: '/robots.txt', type: 'text/plain; charset=UTF-8'},
|
|
|
|
|
2021-12-22 10:05:08 -05:00
|
|
|
{uri: '/split.min.js'},
|
|
|
|
{uri: '/split.min.js.map'},
|
2022-01-20 21:53:15 -05:00
|
|
|
{uri: '/smoothie.js'},
|
2021-12-22 09:27:52 -05:00
|
|
|
{uri: '/codemirror/codemirror.min.js'},
|
|
|
|
{uri: '/codemirror/base16-dark.min.css'},
|
|
|
|
{uri: '/codemirror/matchesonscrollbar.min.css'},
|
|
|
|
{uri: '/codemirror/dialog.min.css'},
|
|
|
|
{uri: '/codemirror/codemirror.min.css'},
|
|
|
|
{uri: '/codemirror/trailingspace.min.js'},
|
|
|
|
{uri: '/codemirror/dialog.min.js'},
|
|
|
|
{uri: '/codemirror/search.min.js'},
|
|
|
|
{uri: '/codemirror/searchcursor.min.js'},
|
|
|
|
{uri: '/codemirror/jump-to-line.min.js'},
|
|
|
|
{uri: '/codemirror/matchesonscrollbar.min.js'},
|
|
|
|
{uri: '/codemirror/annotatescrollbar.min.js'},
|
|
|
|
{uri: '/codemirror/javascript.min.js'},
|
|
|
|
{uri: '/codemirror/css.min.js'},
|
|
|
|
{uri: '/codemirror/xml.min.js'},
|
|
|
|
{uri: '/codemirror/htmlmixed.min.js'},
|
2021-01-02 13:10:00 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
function startsWithBytes(data, bytes) {
|
|
|
|
if (data.byteLength >= bytes.length) {
|
|
|
|
var dataBytes = new Uint8Array(data.slice(0, bytes.length));
|
|
|
|
for (var i = 0; i < bytes.length; i++) {
|
2022-01-30 15:23:55 -05:00
|
|
|
if (dataBytes[i] != bytes[i] && bytes[i] !== null) {
|
2021-01-02 13:10:00 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function staticFileHandler(request, response, blobId, uri) {
|
|
|
|
for (var i in kStaticFiles) {
|
|
|
|
if (uri === kStaticFiles[i].uri) {
|
2021-12-22 09:27:52 -05:00
|
|
|
var path = kStaticFiles[i].path || uri.substring(1);
|
|
|
|
var type = kStaticFiles[i].type || guessType(path);
|
|
|
|
var data = await File.readFile("core/" + path);
|
|
|
|
response.writeHead(200, {"Content-Type": type, "Content-Length": data.byteLength});
|
2021-01-02 13:10:00 -05:00
|
|
|
response.end(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": "File not found".length});
|
|
|
|
response.end("File not found");
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:10:45 -05:00
|
|
|
const k_mime_types = {
|
|
|
|
'json': 'text/json',
|
|
|
|
'js': 'text/javascript',
|
|
|
|
'html': 'text/html',
|
|
|
|
'css': 'text/css',
|
|
|
|
'map': 'application/json',
|
|
|
|
};
|
|
|
|
|
|
|
|
async function perfettoHandler(request, response, uri) {
|
|
|
|
var filename = uri || 'index.html';
|
|
|
|
if (filename.indexOf('..') != -1) {
|
|
|
|
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": "File not found".length});
|
|
|
|
response.end("File not found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
var data = await File.readFile("deps/perfetto/" + filename);
|
|
|
|
response.writeHead(200, {"Content-Type": k_mime_types[filename.split('.').pop()] || 'text/plain', "Content-Length": data.byteLength});
|
2021-12-21 14:53:30 -05:00
|
|
|
response.end(data);
|
|
|
|
} catch {
|
|
|
|
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": "File not found".length});
|
|
|
|
response.end("File not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 20:58:12 -05:00
|
|
|
async function wellKnownHandler(request, response, path) {
|
|
|
|
var data = await File.readFile("data/global/.well-known/" + path);
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 14:05:59 -05:00
|
|
|
function sendData(response, data, type, headers) {
|
2021-01-02 13:10:00 -05:00
|
|
|
if (data) {
|
|
|
|
if (startsWithBytes(data, [0xff, 0xd8, 0xff, 0xdb]) ||
|
|
|
|
startsWithBytes(data, [0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01]) ||
|
|
|
|
startsWithBytes(data, [0xff, 0xd8, 0xff, 0xee]) ||
|
|
|
|
startsWithBytes(data, [0xff, 0xd8, 0xff, 0xe1, null, null, 0x45, 0x78, 0x69, 0x66, 0x00, 0x00])) {
|
2022-01-15 14:05:59 -05:00
|
|
|
response.writeHead(200, Object.assign({"Content-Type": "image/jpeg", "Content-Length": data.byteLength}, headers || {}));
|
2021-01-02 13:10:00 -05:00
|
|
|
response.end(data);
|
|
|
|
} else if (startsWithBytes(data, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) {
|
2022-01-15 14:05:59 -05:00
|
|
|
response.writeHead(200, Object.assign({"Content-Type": "image/png", "Content-Length": data.byteLength}, headers || {}));
|
2021-01-02 13:10:00 -05:00
|
|
|
response.end(data);
|
|
|
|
} else if (startsWithBytes(data, [0x47, 0x49, 0x46, 0x38, 0x37, 0x61]) ||
|
|
|
|
startsWithBytes(data, [0x47, 0x49, 0x46, 0x38, 0x39, 0x61])) {
|
2022-01-15 14:05:59 -05:00
|
|
|
response.writeHead(200, Object.assign({"Content-Type": "image/gif", "Content-Length": data.byteLength}, headers || {}));
|
2021-01-02 13:10:00 -05:00
|
|
|
response.end(data);
|
|
|
|
} else {
|
2022-01-15 14:05:59 -05:00
|
|
|
response.writeHead(200, Object.assign({"Content-Type": type || "text/javascript; charset=utf-8", "Content-Length": data.byteLength}, headers || {}));
|
2021-01-02 13:10:00 -05:00
|
|
|
response.end(data);
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-15 14:05:59 -05:00
|
|
|
response.writeHead(404, Object.assign({"Content-Type": "text/plain; charset=utf-8", "Content-Length": "File not found".length}, headers || {}));
|
2021-01-02 13:10:00 -05:00
|
|
|
response.end("File not found");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 18:06:33 -05:00
|
|
|
async function getBlobOrContent(id) {
|
|
|
|
if (!id) {
|
|
|
|
return;
|
|
|
|
} else if (id.startsWith('&')) {
|
|
|
|
return ssb.blobGet(id);
|
|
|
|
} else if (id.startsWith('%')) {
|
|
|
|
return ssb.messageContentGet(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 08:51:49 -05:00
|
|
|
function guessType(path) {
|
|
|
|
const k_extension_to_type = {
|
|
|
|
'css': 'text/css',
|
2021-12-22 09:27:52 -05:00
|
|
|
'html': 'text/html',
|
|
|
|
'js': 'text/javascript',
|
2021-12-22 08:51:49 -05:00
|
|
|
};
|
|
|
|
var extension = path.split('.').pop();
|
|
|
|
return k_extension_to_type[extension];
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:10:00 -05:00
|
|
|
async function blobHandler(request, response, blobId, uri) {
|
|
|
|
var found = false;
|
|
|
|
if (!found) {
|
|
|
|
for (var i in kStaticFiles) {
|
2022-02-27 16:15:36 -05:00
|
|
|
if (uri === kStaticFiles[i].uri && kStaticFiles[i].path) {
|
2021-01-02 13:10:00 -05:00
|
|
|
found = true;
|
2021-10-05 21:25:33 -04:00
|
|
|
var data = await File.readFile("core/" + kStaticFiles[i].path);
|
2021-01-02 13:10:00 -05:00
|
|
|
response.writeHead(200, {"Content-Type": kStaticFiles[i].type, "Content-Length": data.byteLength});
|
|
|
|
response.end(data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!uri) {
|
2022-01-08 20:36:29 -05:00
|
|
|
response.writeHead(303, {"Location": (request.client.tls ? 'https://' : 'http://') + request.headers.host + blobId + '/', "Content-Length": "0"});
|
2021-01-02 13:10:00 -05:00
|
|
|
response.end(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
var process;
|
|
|
|
if (uri == "/view") {
|
|
|
|
var data;
|
|
|
|
if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) {
|
|
|
|
var id = await new Database(match[1]).get('path:' + match[2]);
|
|
|
|
if (id) {
|
2022-01-15 14:05:59 -05:00
|
|
|
if (request.headers['if-none-match'] && request.headers['if-none-match'] == '"' + id + '"') {
|
|
|
|
response.writeHead(304, {});
|
|
|
|
response.end();
|
|
|
|
} else {
|
|
|
|
data = await getBlobOrContent(id);
|
|
|
|
if (match[3]) {
|
2022-03-17 21:24:29 -04:00
|
|
|
var appObject = JSON.parse(data);
|
|
|
|
data = appObject.files[match[3]];
|
2022-01-15 14:05:59 -05:00
|
|
|
}
|
|
|
|
sendData(response, data, undefined, {etag: '"' + id + '"'});
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-01-15 14:05:59 -05:00
|
|
|
} else {
|
|
|
|
sendData(response, data);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-09 18:06:33 -05:00
|
|
|
data = await getBlobOrContent(blobId);
|
2022-01-15 14:05:59 -05:00
|
|
|
sendData(response, data);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
} else if (uri == "/save") {
|
|
|
|
let newBlobId = await ssb.blobStore(request.body);
|
|
|
|
|
|
|
|
var match;
|
|
|
|
if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) {
|
|
|
|
var user = match[1];
|
2022-03-17 21:24:29 -04:00
|
|
|
var appName = match[2];
|
2021-01-02 13:10:00 -05:00
|
|
|
var credentials = auth.query(request.headers);
|
2022-01-30 09:51:09 -05:00
|
|
|
if (credentials && credentials.session &&
|
|
|
|
(credentials.session.name == user ||
|
|
|
|
(credentials.permissions.administration && user == 'core'))) {
|
|
|
|
var database = new Database(user);
|
|
|
|
var apps = new Set();
|
|
|
|
try {
|
|
|
|
apps = new Set(JSON.parse(database.get('apps')));
|
|
|
|
} catch {
|
|
|
|
}
|
2022-03-17 21:24:29 -04:00
|
|
|
if (!apps.has(appName)) {
|
|
|
|
apps.add(appName);
|
2022-01-30 09:51:09 -05:00
|
|
|
database.set('apps', JSON.stringify([...apps]));
|
|
|
|
}
|
2022-03-17 21:24:29 -04:00
|
|
|
database.set('path:' + appName, newBlobId);
|
2022-01-30 09:51:09 -05:00
|
|
|
} else {
|
2021-01-02 13:10:00 -05:00
|
|
|
response.writeHead(401, {"Content-Type": "text/plain; charset=utf-8"});
|
|
|
|
response.end("401 Unauthorized");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response.writeHead(200, {"Content-Type": "text/plain; charset=utf-8"});
|
|
|
|
response.end("/" + newBlobId);
|
|
|
|
} else {
|
|
|
|
var data;
|
2021-12-22 08:51:49 -05:00
|
|
|
var type;
|
2022-01-15 14:05:59 -05:00
|
|
|
var headers;
|
2021-01-02 13:10:00 -05:00
|
|
|
if (match = /^\/\~(\w+)\/(\w+)$/.exec(blobId)) {
|
|
|
|
var db = new Database(match[1]);
|
|
|
|
var id = await db.get('path:' + match[2]);
|
|
|
|
if (id) {
|
2022-01-15 14:05:59 -05:00
|
|
|
if (request.headers['if-none-match'] && request.headers['if-none-match'] == '"' + id + '"') {
|
2022-03-19 21:17:20 -04:00
|
|
|
headers = {
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
};
|
|
|
|
response.writeHead(304, headers);
|
2022-01-15 14:05:59 -05:00
|
|
|
response.end();
|
|
|
|
} else {
|
|
|
|
data = utf8Decode(await getBlobOrContent(id));
|
2022-03-17 21:24:29 -04:00
|
|
|
var appObject = JSON.parse(data);
|
|
|
|
data = appObject.files[uri.substring(1)];
|
2022-01-15 14:05:59 -05:00
|
|
|
data = await getBlobOrContent(data);
|
|
|
|
type = guessType(uri);
|
2022-03-19 21:17:20 -04:00
|
|
|
headers = {
|
|
|
|
'ETag': '"' + id + '"',
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
};
|
2022-01-15 14:05:59 -05:00
|
|
|
sendData(response, data, type, headers);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sendData(response, data, type, headers);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2021-01-09 18:06:33 -05:00
|
|
|
} else {
|
|
|
|
data = utf8Decode(await getBlobOrContent(blobId));
|
2022-03-17 21:24:29 -04:00
|
|
|
var appObject = JSON.parse(data);
|
|
|
|
data = appObject.files[uri.substring(1)];
|
2021-01-09 18:06:33 -05:00
|
|
|
data = await getBlobOrContent(data);
|
2022-03-19 21:17:20 -04:00
|
|
|
headers = {
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
};
|
2022-01-15 14:05:59 -05:00
|
|
|
sendData(response, data, type, headers);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
ssb.addEventListener('broadcasts', function() {
|
2021-01-02 13:10:00 -05:00
|
|
|
broadcastEvent('onBroadcastsChanged', []);
|
2021-11-07 17:28:58 -05:00
|
|
|
});
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2021-11-07 17:28:58 -05:00
|
|
|
ssb.addEventListener('connections', function() {
|
2021-01-02 13:10:00 -05:00
|
|
|
broadcastEvent('onConnectionsChanged', []);
|
2021-11-07 17:28:58 -05:00
|
|
|
});
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2021-10-05 21:25:33 -04:00
|
|
|
async function loadSettings() {
|
2022-01-17 21:50:46 -05:00
|
|
|
var data;
|
|
|
|
|
2021-10-05 21:25:33 -04:00
|
|
|
try {
|
2022-01-17 21:50:46 -05:00
|
|
|
var settings = new Database('core').get('settings');
|
|
|
|
if (settings) {
|
|
|
|
data = JSON.parse(settings);
|
2017-01-29 07:30:02 -05:00
|
|
|
}
|
2021-10-05 21:25:33 -04:00
|
|
|
} catch (error) {
|
2022-01-17 21:50:46 -05:00
|
|
|
print("Settings not found in database:", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
try {
|
2022-02-13 17:03:12 -05:00
|
|
|
data = JSON.parse(utf8Decode(await File.readFile(kGlobalSettingsFile)));
|
2022-01-17 21:50:46 -05:00
|
|
|
new Database('core').set('settings', JSON.stringify(data));
|
|
|
|
} catch (error) {
|
|
|
|
print("Unable to load settings from " + kGlobalSettingsFile + ":", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
gGlobalSettings = data;
|
2016-03-12 13:50:43 -05:00
|
|
|
}
|
2021-10-05 21:25:33 -04:00
|
|
|
}
|
|
|
|
|
2022-01-20 21:53:15 -05:00
|
|
|
function sendStats() {
|
2022-02-26 16:17:15 -05:00
|
|
|
var any = false;
|
2022-01-20 21:53:15 -05:00
|
|
|
for (var process of Object.values(gProcesses)) {
|
2022-02-26 16:17:15 -05:00
|
|
|
if (process.app && process.stats) {
|
2022-01-20 21:53:15 -05:00
|
|
|
process.app.send({action: 'stats', stats: getStats()});
|
2022-02-26 16:17:15 -05:00
|
|
|
any = true;
|
2022-01-20 21:53:15 -05:00
|
|
|
}
|
|
|
|
}
|
2022-02-26 16:17:15 -05:00
|
|
|
|
|
|
|
if (any) {
|
|
|
|
setTimeout(sendStats, 1000);
|
|
|
|
} else {
|
|
|
|
gStatsTimer = false;
|
|
|
|
}
|
2022-01-20 21:53:15 -05:00
|
|
|
}
|
|
|
|
|
2021-10-05 21:25:33 -04:00
|
|
|
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 === "") {
|
2022-01-08 20:36:29 -05:00
|
|
|
response.writeHead(303, {"Location": (request.client.tls ? 'https://' : 'http://') + request.headers.host + gGlobalSettings.index, "Content-Length": "0"});
|
2021-10-05 21:25:33 -04:00
|
|
|
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]);
|
2022-01-02 14:10:45 -05:00
|
|
|
} else if (match = /^\/perfetto\/([\.\w-/]*)$/.exec(request.uri)) {
|
|
|
|
return perfettoHandler(request, response, match[1]);
|
2022-01-30 09:51:09 -05:00
|
|
|
} else if (match = /^(.*)(\/save?)$/.exec(request.uri)) {
|
2021-10-05 21:25:33 -04:00
|
|
|
return blobHandler(request, response, match[1], match[2]);
|
|
|
|
} else if (match = /^\/trace$/.exec(request.uri)) {
|
|
|
|
var data = trace();
|
2022-01-02 14:10:45 -05:00
|
|
|
response.writeHead(200, {"Content-Type": "application/json; charset=utf-8", "Content-Length": data.length.toString()});
|
2021-10-05 21:25:33 -04:00
|
|
|
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) {
|
2022-01-04 20:58:12 -05:00
|
|
|
return wellKnownHandler(request, response, match[1]);
|
2021-10-05 21:25:33 -04:00
|
|
|
} 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.');
|
2016-03-12 13:50:43 -05:00
|
|
|
});
|
2022-03-17 21:24:29 -04:00
|
|
|
|
|
|
|
exports.getSessionProcessBlob = getSessionProcessBlob;
|
|
|
|
exports.invoke = invoke;
|
|
|
|
exports.globalSettings = gGlobalSettings;
|
|
|
|
exports.setGlobalSettings = setGlobalSettings;
|