tildefriends/core/app.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

259 lines
5.6 KiB
JavaScript
Raw Normal View History

import * as core from './core.js';
let g_next_id = 1;
let g_calls = {};
let gSessionIndex = 0;
/**
* TODOC
2024-02-22 15:23:39 -05:00
* @returns
*/
function makeSessionId() {
return (gSessionIndex++).toString();
}
/**
* TODOC
2024-02-22 15:23:39 -05:00
* @returns
*/
function App() {
this._on_output = null;
this._send_queue = [];
return this;
}
/**
* TODOC
2024-02-22 15:23:39 -05:00
* @param {*} callback
*/
2024-02-24 11:09:34 -05:00
App.prototype.readOutput = function (callback) {
this._on_output = callback;
2024-02-24 11:09:34 -05:00
};
/**
* TODOC
2024-02-22 15:23:39 -05:00
* @param {*} api
* @returns
*/
2024-02-24 11:09:34 -05:00
App.prototype.makeFunction = function (api) {
let self = this;
2024-02-24 11:09:34 -05:00
let result = function () {
let id = g_next_id++;
while (!id || g_calls[id]) {
id = g_next_id++;
}
2024-02-24 11:09:34 -05:00
let promise = new Promise(function (resolve, reject) {
g_calls[id] = {resolve: resolve, reject: reject};
});
let message = {
message: 'tfrpc',
method: api[0],
params: [...arguments],
id: id,
};
self.send(message);
return promise;
};
Object.defineProperty(result, 'name', {value: api[0], writable: false});
return result;
2024-02-24 11:09:34 -05:00
};
/**
* TODOC
2024-02-22 15:23:39 -05:00
* @param {*} message
*/
2024-02-24 11:09:34 -05:00
App.prototype.send = function (message) {
if (this._send_queue) {
if (this._on_output) {
2024-02-24 11:09:34 -05:00
this._send_queue.forEach((x) => this._on_output(x));
this._send_queue = null;
} else if (message) {
this._send_queue.push(message);
}
}
if (message && this._on_output) {
this._on_output(message);
}
};
/**
* TODOC
2024-02-22 15:23:39 -05:00
* @param {*} request
* @param {*} response
* @param {*} client
*/
async function socket(request, response, client) {
let process;
let options = {};
let credentials = await httpd.auth_query(request.headers);
2024-02-24 11:09:34 -05:00
response.onClose = async function () {
if (process && process.task) {
process.task.kill();
}
if (process) {
process.timeout = 0;
}
2024-02-24 11:09:34 -05:00
};
2024-02-24 11:09:34 -05:00
response.onMessage = async function (event) {
if (event.opCode == 0x1 || event.opCode == 0x2) {
let message;
try {
message = JSON.parse(event.data);
} catch (error) {
2024-02-24 11:09:34 -05:00
print('ERROR', error, event.data, event.data.length, event.opCode);
return;
}
2024-02-24 11:09:34 -05:00
if (message.action == 'hello') {
let packageOwner;
let packageName;
let blobId;
let match;
let parentApp;
2024-02-24 11:09:34 -05:00
if (
(match = /^\/([&%][^\.]{44}(?:\.\w+)?)(\/?.*)/.exec(message.path))
) {
blobId = match[1];
2024-02-24 11:09:34 -05:00
} else if ((match = /^\/\~([^\/]+)\/([^\/]+)\/$/.exec(message.path))) {
packageOwner = match[1];
packageName = match[2];
blobId = await new Database(packageOwner).get('path:' + packageName);
if (!blobId) {
2024-02-24 11:09:34 -05:00
response.send(
JSON.stringify({
message: 'tfrpc',
method: 'error',
params: [message.path + ' not found'],
id: -1,
}),
0x1
);
return;
}
if (packageOwner != 'core') {
let coreId = await new Database('core').get('path:' + packageName);
parentApp = {
path: '/~core/' + packageName + '/',
id: coreId,
};
}
}
2024-02-24 11:09:34 -05:00
response.send(
2024-04-17 20:56:33 -04:00
JSON.stringify(
Object.assign(
{
action: 'session',
credentials: credentials,
parentApp: parentApp,
id: blobId,
},
2024-05-05 13:48:22 -04:00
await ssb.getIdentityInfo(
2024-04-17 20:56:33 -04:00
credentials?.session?.name,
packageOwner,
packageName
)
)
),
2024-02-24 11:09:34 -05:00
0x1
);
options.api = message.api || [];
options.credentials = credentials;
options.packageOwner = packageOwner;
options.packageName = packageName;
options.url = message.url;
let sessionId = makeSessionId();
if (blobId) {
if (message.edit_only) {
2024-02-24 11:09:34 -05:00
response.send(
JSON.stringify({action: 'ready', edit_only: true}),
0x1
);
} else {
2024-02-24 11:09:34 -05:00
process = await core.getSessionProcessBlob(
blobId,
sessionId,
options
);
}
}
if (process) {
2024-02-24 11:09:34 -05:00
process.app.readOutput(function (message) {
response.send(JSON.stringify(message), 0x1);
});
process.app.send();
}
2024-02-24 11:09:34 -05:00
let ping = function () {
let now = Date.now();
let again = true;
if (now - process.lastActive < process.timeout) {
// Active.
} else if (process.lastPing > process.lastActive) {
// We lost them.
if (process.task) {
process.task.kill();
}
again = false;
} else {
// Idle. Ping them.
2024-02-24 11:09:34 -05:00
response.send('', 0x9);
process.lastPing = now;
}
if (again && process.timeout) {
setTimeout(ping, process.timeout);
}
2024-02-24 11:09:34 -05:00
};
if (process && process.timeout > 0) {
setTimeout(ping, process.timeout);
}
} else if (message.action == 'enableStats') {
if (process) {
core.enableStats(process, message.enabled);
}
} else if (message.action == 'resetPermission') {
if (process) {
process.resetPermission(message.permission);
}
} else if (message.action == 'setActiveIdentity') {
process.setActiveIdentity(message.identity);
} else if (message.action == 'createIdentity') {
process.createIdentity();
} else if (message.message == 'tfrpc') {
if (message.id && g_calls[message.id]) {
if (message.error !== undefined) {
g_calls[message.id].reject(message.error);
} else {
g_calls[message.id].resolve(message.result);
}
delete g_calls[message.id];
}
} else {
if (process && process.eventHandlers['message']) {
await core.invoke(process.eventHandlers['message'], [message]);
}
}
} else if (event.opCode == 0x8) {
// Close.
if (process && process.task) {
process.task.kill();
}
response.send(event.data, 0x8);
} else if (event.opCode == 0xa) {
// PONG
}
if (process) {
process.lastActive = Date.now();
}
2024-02-24 11:09:34 -05:00
};
2024-04-02 12:42:31 -04:00
response.upgrade(100, {});
}
2024-02-24 11:09:34 -05:00
export {socket, App};