forked from cory/tildefriends
Run prettier.
This commit is contained in:
96
core/app.js
96
core/app.js
@ -28,23 +28,23 @@ function App() {
|
||||
* TODOC
|
||||
* @param {*} callback
|
||||
*/
|
||||
App.prototype.readOutput = function(callback) {
|
||||
App.prototype.readOutput = function (callback) {
|
||||
this._on_output = callback;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* TODOC
|
||||
* @param {*} api
|
||||
* @returns
|
||||
*/
|
||||
App.prototype.makeFunction = function(api) {
|
||||
App.prototype.makeFunction = function (api) {
|
||||
let self = this;
|
||||
let result = function() {
|
||||
let result = function () {
|
||||
let id = g_next_id++;
|
||||
while (!id || g_calls[id]) {
|
||||
id = g_next_id++;
|
||||
}
|
||||
let promise = new Promise(function(resolve, reject) {
|
||||
let promise = new Promise(function (resolve, reject) {
|
||||
g_calls[id] = {resolve: resolve, reject: reject};
|
||||
});
|
||||
let message = {
|
||||
@ -58,16 +58,16 @@ App.prototype.makeFunction = function(api) {
|
||||
};
|
||||
Object.defineProperty(result, 'name', {value: api[0], writable: false});
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* TODOC
|
||||
* @param {*} message
|
||||
*/
|
||||
App.prototype.send = function(message) {
|
||||
App.prototype.send = function (message) {
|
||||
if (this._send_queue) {
|
||||
if (this._on_output) {
|
||||
this._send_queue.forEach(x => this._on_output(x));
|
||||
this._send_queue.forEach((x) => this._on_output(x));
|
||||
this._send_queue = null;
|
||||
} else if (message) {
|
||||
this._send_queue.push(message);
|
||||
@ -90,43 +90,48 @@ function socket(request, response, client) {
|
||||
let credentials = auth.query(request.headers);
|
||||
let refresh = auth.makeRefresh(credentials);
|
||||
|
||||
response.onClose = async function() {
|
||||
response.onClose = async function () {
|
||||
if (process && process.task) {
|
||||
process.task.kill();
|
||||
}
|
||||
if (process) {
|
||||
process.timeout = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
response.onMessage = async function(event) {
|
||||
response.onMessage = async function (event) {
|
||||
if (event.opCode == 0x1 || event.opCode == 0x2) {
|
||||
let message;
|
||||
try {
|
||||
message = JSON.parse(event.data);
|
||||
} catch (error) {
|
||||
print("ERROR", error, event.data, event.data.length, event.opCode);
|
||||
print('ERROR', error, event.data, event.data.length, event.opCode);
|
||||
return;
|
||||
}
|
||||
if (message.action == "hello") {
|
||||
if (message.action == 'hello') {
|
||||
let packageOwner;
|
||||
let packageName;
|
||||
let blobId;
|
||||
let match;
|
||||
let parentApp;
|
||||
if (match = /^\/([&%][^\.]{44}(?:\.\w+)?)(\/?.*)/.exec(message.path)) {
|
||||
if (
|
||||
(match = /^\/([&%][^\.]{44}(?:\.\w+)?)(\/?.*)/.exec(message.path))
|
||||
) {
|
||||
blobId = match[1];
|
||||
} else if (match = /^\/\~([^\/]+)\/([^\/]+)\/$/.exec(message.path)) {
|
||||
} else if ((match = /^\/\~([^\/]+)\/([^\/]+)\/$/.exec(message.path))) {
|
||||
packageOwner = match[1];
|
||||
packageName = match[2];
|
||||
blobId = await new Database(packageOwner).get('path:' + packageName);
|
||||
if (!blobId) {
|
||||
response.send(JSON.stringify({
|
||||
message: 'tfrpc',
|
||||
method: "error",
|
||||
params: [message.path + ' not found'],
|
||||
id: -1,
|
||||
}), 0x1);
|
||||
response.send(
|
||||
JSON.stringify({
|
||||
message: 'tfrpc',
|
||||
method: 'error',
|
||||
params: [message.path + ' not found'],
|
||||
id: -1,
|
||||
}),
|
||||
0x1
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (packageOwner != 'core') {
|
||||
@ -137,12 +142,15 @@ function socket(request, response, client) {
|
||||
};
|
||||
}
|
||||
}
|
||||
response.send(JSON.stringify({
|
||||
action: "session",
|
||||
credentials: credentials,
|
||||
parentApp: parentApp,
|
||||
id: blobId,
|
||||
}), 0x1);
|
||||
response.send(
|
||||
JSON.stringify({
|
||||
action: 'session',
|
||||
credentials: credentials,
|
||||
parentApp: parentApp,
|
||||
id: blobId,
|
||||
}),
|
||||
0x1
|
||||
);
|
||||
|
||||
options.api = message.api || [];
|
||||
options.credentials = credentials;
|
||||
@ -152,19 +160,26 @@ function socket(request, response, client) {
|
||||
let sessionId = makeSessionId();
|
||||
if (blobId) {
|
||||
if (message.edit_only) {
|
||||
response.send(JSON.stringify({action: 'ready', edit_only: true}), 0x1);
|
||||
response.send(
|
||||
JSON.stringify({action: 'ready', edit_only: true}),
|
||||
0x1
|
||||
);
|
||||
} else {
|
||||
process = await core.getSessionProcessBlob(blobId, sessionId, options);
|
||||
process = await core.getSessionProcessBlob(
|
||||
blobId,
|
||||
sessionId,
|
||||
options
|
||||
);
|
||||
}
|
||||
}
|
||||
if (process) {
|
||||
process.app.readOutput(function(message) {
|
||||
process.app.readOutput(function (message) {
|
||||
response.send(JSON.stringify(message), 0x1);
|
||||
});
|
||||
process.app.send();
|
||||
}
|
||||
|
||||
let ping = function() {
|
||||
let ping = function () {
|
||||
let now = Date.now();
|
||||
let again = true;
|
||||
if (now - process.lastActive < process.timeout) {
|
||||
@ -177,14 +192,14 @@ function socket(request, response, client) {
|
||||
again = false;
|
||||
} else {
|
||||
// Idle. Ping them.
|
||||
response.send("", 0x9);
|
||||
response.send('', 0x9);
|
||||
process.lastPing = now;
|
||||
}
|
||||
|
||||
if (again && process.timeout) {
|
||||
setTimeout(ping, process.timeout);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (process && process.timeout > 0) {
|
||||
setTimeout(ping, process.timeout);
|
||||
@ -224,11 +239,16 @@ function socket(request, response, client) {
|
||||
if (process) {
|
||||
process.lastActive = Date.now();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
response.upgrade(100, refresh ? {
|
||||
'Set-Cookie': `session=${refresh.token}; path=/; Max-Age=${refresh.interval}; Secure; SameSite=Strict`,
|
||||
} : {});
|
||||
response.upgrade(
|
||||
100,
|
||||
refresh
|
||||
? {
|
||||
'Set-Cookie': `session=${refresh.token}; path=/; Max-Age=${refresh.interval}; Secure; SameSite=Strict`,
|
||||
}
|
||||
: {}
|
||||
);
|
||||
}
|
||||
|
||||
export { socket, App };
|
||||
export {socket, App};
|
||||
|
Reference in New Issue
Block a user