2022-06-18 13:39:08 -04:00
|
|
|
import * as core from './core.js';
|
2022-03-17 21:24:29 -04:00
|
|
|
|
2022-08-13 14:58:06 -04:00
|
|
|
let g_next_id = 1;
|
|
|
|
let g_calls = {};
|
|
|
|
|
|
|
|
let gSessionIndex = 0;
|
2022-03-17 21:24:29 -04:00
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2022-03-17 21:24:29 -04:00
|
|
|
function makeSessionId() {
|
2024-10-16 19:50:31 -04:00
|
|
|
return 'session_' + (gSessionIndex++).toString();
|
2022-03-17 21:24:29 -04:00
|
|
|
}
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2021-01-02 13:10:00 -05:00
|
|
|
function App() {
|
|
|
|
this._on_output = null;
|
|
|
|
this._send_queue = [];
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @param {*} callback
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2024-02-24 11:09:34 -05:00
|
|
|
App.prototype.readOutput = function (callback) {
|
2021-01-02 13:10:00 -05:00
|
|
|
this._on_output = callback;
|
2024-02-24 11:09:34 -05:00
|
|
|
};
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @param {*} api
|
|
|
|
* @returns
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2024-02-24 11:09:34 -05:00
|
|
|
App.prototype.makeFunction = function (api) {
|
2021-01-02 13:10:00 -05:00
|
|
|
let self = this;
|
2024-02-24 11:09:34 -05:00
|
|
|
let result = function () {
|
2023-01-20 19:16:18 -05:00
|
|
|
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) {
|
2023-01-20 19:16:18 -05:00
|
|
|
g_calls[id] = {resolve: resolve, reject: reject};
|
|
|
|
});
|
2022-08-13 14:58:06 -04:00
|
|
|
let message = {
|
|
|
|
message: 'tfrpc',
|
|
|
|
method: api[0],
|
|
|
|
params: [...arguments],
|
|
|
|
id: id,
|
|
|
|
};
|
2021-01-02 13:10:00 -05:00
|
|
|
self.send(message);
|
2022-08-13 14:58:06 -04:00
|
|
|
return promise;
|
2021-01-02 13:10:00 -05:00
|
|
|
};
|
|
|
|
Object.defineProperty(result, 'name', {value: api[0], writable: false});
|
|
|
|
return result;
|
2024-02-24 11:09:34 -05:00
|
|
|
};
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @param {*} message
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2024-02-24 11:09:34 -05:00
|
|
|
App.prototype.send = function (message) {
|
2022-08-13 14:58:06 -04:00
|
|
|
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));
|
2022-08-13 14:58:06 -04:00
|
|
|
this._send_queue = null;
|
|
|
|
} else if (message) {
|
|
|
|
this._send_queue.push(message);
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-08-13 14:58:06 -04:00
|
|
|
if (message && this._on_output) {
|
|
|
|
this._on_output(message);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-02-19 13:12:42 -05:00
|
|
|
/**
|
|
|
|
* TODOC
|
2024-02-22 15:23:39 -05:00
|
|
|
* @param {*} request
|
|
|
|
* @param {*} response
|
|
|
|
* @param {*} client
|
2024-02-19 13:12:42 -05:00
|
|
|
*/
|
2024-06-10 20:22:28 -04:00
|
|
|
async function socket(request, response, client) {
|
2022-08-13 14:58:06 -04:00
|
|
|
let process;
|
|
|
|
let options = {};
|
2024-06-10 20:22:28 -04:00
|
|
|
let credentials = await httpd.auth_query(request.headers);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
response.onClose = async function () {
|
2022-01-20 19:49:03 -05:00
|
|
|
if (process && process.task) {
|
|
|
|
process.task.kill();
|
|
|
|
}
|
2024-01-27 10:45:51 -05:00
|
|
|
if (process) {
|
|
|
|
process.timeout = 0;
|
2022-01-20 19:49:03 -05:00
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
};
|
2022-01-20 19:49:03 -05:00
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
response.onMessage = async function (event) {
|
2021-01-02 13:10:00 -05:00
|
|
|
if (event.opCode == 0x1 || event.opCode == 0x2) {
|
2022-08-13 14:58:06 -04:00
|
|
|
let message;
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
2021-01-02 13:10:00 -05:00
|
|
|
return;
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
if (message.action == 'hello') {
|
2022-08-13 14:58:06 -04:00
|
|
|
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))
|
|
|
|
) {
|
2021-01-02 13:10:00 -05:00
|
|
|
blobId = match[1];
|
2024-02-24 11:09:34 -05:00
|
|
|
} else if ((match = /^\/\~([^\/]+)\/([^\/]+)\/$/.exec(message.path))) {
|
2022-03-15 20:23:14 -04:00
|
|
|
packageOwner = match[1];
|
|
|
|
packageName = match[2];
|
|
|
|
blobId = await new Database(packageOwner).get('path:' + packageName);
|
2021-01-02 13:10:00 -05:00
|
|
|
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
|
|
|
|
);
|
2022-01-17 21:50:46 -05:00
|
|
|
return;
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
2022-03-15 20:23:14 -04:00
|
|
|
if (packageOwner != 'core') {
|
2022-08-13 14:58:06 -04:00
|
|
|
let coreId = await new Database('core').get('path:' + packageName);
|
2022-01-30 09:51:09 -05:00
|
|
|
parentApp = {
|
2022-03-15 20:23:14 -04:00
|
|
|
path: '/~core/' + packageName + '/',
|
2022-01-30 09:51:09 -05:00
|
|
|
id: coreId,
|
|
|
|
};
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
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
|
|
|
|
);
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
options.api = message.api || [];
|
|
|
|
options.credentials = credentials;
|
2022-03-15 20:23:14 -04:00
|
|
|
options.packageOwner = packageOwner;
|
|
|
|
options.packageName = packageName;
|
2023-07-30 20:26:09 -04:00
|
|
|
options.url = message.url;
|
2022-08-13 14:58:06 -04:00
|
|
|
let sessionId = makeSessionId();
|
2021-01-02 13:10:00 -05:00
|
|
|
if (blobId) {
|
2023-08-16 20:49:02 -04:00
|
|
|
if (message.edit_only) {
|
2024-02-24 11:09:34 -05:00
|
|
|
response.send(
|
|
|
|
JSON.stringify({action: 'ready', edit_only: true}),
|
|
|
|
0x1
|
|
|
|
);
|
2023-08-16 20:49:02 -04:00
|
|
|
} else {
|
2024-10-23 15:38:49 -04:00
|
|
|
process = await core.getProcessBlob(blobId, sessionId, options);
|
2023-08-16 20:49:02 -04:00
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
if (process) {
|
2024-02-24 11:09:34 -05:00
|
|
|
process.app.readOutput(function (message) {
|
2021-01-02 13:10:00 -05:00
|
|
|
response.send(JSON.stringify(message), 0x1);
|
|
|
|
});
|
|
|
|
process.app.send();
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
let ping = function () {
|
2022-08-13 14:58:06 -04:00
|
|
|
let now = Date.now();
|
|
|
|
let again = true;
|
2021-01-02 13:10:00 -05:00
|
|
|
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);
|
2021-01-02 13:10:00 -05:00
|
|
|
process.lastPing = now;
|
|
|
|
}
|
|
|
|
|
2024-01-27 10:45:51 -05:00
|
|
|
if (again && process.timeout) {
|
2021-01-02 13:10:00 -05:00
|
|
|
setTimeout(ping, process.timeout);
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
};
|
2021-01-02 13:10:00 -05:00
|
|
|
|
|
|
|
if (process && process.timeout > 0) {
|
|
|
|
setTimeout(ping, process.timeout);
|
|
|
|
}
|
2022-08-14 14:24:41 -04:00
|
|
|
} else if (message.action == 'resetPermission') {
|
|
|
|
if (process) {
|
|
|
|
process.resetPermission(message.permission);
|
|
|
|
}
|
2024-04-13 13:22:59 -04:00
|
|
|
} else if (message.action == 'setActiveIdentity') {
|
|
|
|
process.setActiveIdentity(message.identity);
|
|
|
|
} else if (message.action == 'createIdentity') {
|
2024-06-12 20:47:48 -04:00
|
|
|
await process.createIdentity();
|
2022-08-13 14:58:06 -04:00
|
|
|
} 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];
|
|
|
|
}
|
2021-01-02 13:10:00 -05:00
|
|
|
} else {
|
|
|
|
if (process && process.eventHandlers['message']) {
|
2022-03-17 21:24:29 -04:00
|
|
|
await core.invoke(process.eventHandlers['message'], [message]);
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (event.opCode == 0x8) {
|
|
|
|
// Close.
|
2022-01-20 19:49:03 -05:00
|
|
|
if (process && process.task) {
|
2021-01-02 13:10:00 -05:00
|
|
|
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
|
|
|
};
|
2022-10-04 21:20:47 -04:00
|
|
|
|
2024-04-02 12:42:31 -04:00
|
|
|
response.upgrade(100, {});
|
2021-01-02 13:10:00 -05:00
|
|
|
}
|
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
export {socket, App};
|