stop relying on netflux-server internals

* create RPC module from inside historyKeeper
* stop passing around netflux-server context
  * update to use newer netflux-server's formal APIs
* manage your own cache of indexes instead of storing things in the netflux context
This commit is contained in:
ansuz
2020-02-03 14:20:05 -05:00
parent 06c29ef1d1
commit 779e817443
5 changed files with 185 additions and 197 deletions

View File

@@ -6,25 +6,15 @@ var Fs = require("fs");
var Admin = module.exports;
var getActiveSessions = function (Env, ctx, cb) {
var total = ctx.users ? Object.keys(ctx.users).length : '?';
var ips = [];
Object.keys(ctx.users).forEach(function (u) {
var user = ctx.users[u];
var socket = user.socket;
var req = socket.upgradeReq;
var conn = req && req.connection;
var ip = (req && req.headers && req.headers['x-forwarded-for']) || (conn && conn.remoteAddress);
if (ip && ips.indexOf(ip) === -1) {
ips.push(ip);
}
});
cb (void 0, [total, ips.length]);
var getActiveSessions = function (Env, Server, cb) {
var stats = Server.getSessionStats();
cb(void 0, [
stats.total,
stats.unique
]);
};
var shutdown = function (Env, ctx, cb) {
var shutdown = function (Env, Server, cb) {
return void cb('E_NOT_IMPLEMENTED');
//clearInterval(Env.sessionExpirationInterval);
// XXX set a flag to prevent incoming database writes
@@ -91,19 +81,18 @@ var getDiskUsage = function (Env, cb) {
});
};
Admin.command = function (Env, ctx, publicKey, data, cb) {
Admin.command = function (Env, Server, publicKey, data, cb) {
var admins = Env.admins;
if (admins.indexOf(publicKey) === -1) {
return void cb("FORBIDDEN");
}
// Handle commands here
switch (data[0]) {
case 'ACTIVE_SESSIONS':
return getActiveSessions(Env, ctx, cb);
return getActiveSessions(Env, Server, cb);
case 'ACTIVE_PADS':
return cb(void 0, ctx.channels ? Object.keys(ctx.channels).length : '?');
return cb(void 0, Server.getActiveChannelCount());
case 'REGISTERED_USERS':
return getRegisteredUsers(Env, cb);
case 'DISK_USAGE':
@@ -112,7 +101,7 @@ Admin.command = function (Env, ctx, publicKey, data, cb) {
Env.flushCache();
return cb(void 0, true);
case 'SHUTDOWN':
return shutdown(Env, ctx, cb);
return shutdown(Env, Server, cb);
default:
return cb('UNHANDLED_ADMIN_COMMAND');
}

View File

@@ -147,7 +147,7 @@ Channel.isNewChannel = function (Env, channel, cb) {
Otherwise behaves the same as sending to a channel
*/
Channel.writePrivateMessage = function (Env, args, nfwssCtx, cb) {
Channel.writePrivateMessage = function (Env, args, Server, cb) {
var channelId = args[0];
var msg = args[1];
@@ -161,7 +161,7 @@ Channel.writePrivateMessage = function (Env, args, nfwssCtx, cb) {
// We expect a modern netflux-websocket-server instance
// if this API isn't here everything will fall apart anyway
if (!(nfwssCtx && nfwssCtx.historyKeeper && typeof(nfwssCtx.historyKeeper.onChannelMessage) === 'function')) {
if (!(Server && typeof(Server.send) === 'function')) {
return void cb("NOT_IMPLEMENTED");
}
@@ -180,8 +180,9 @@ Channel.writePrivateMessage = function (Env, args, nfwssCtx, cb) {
msg // the actual message content. Generally a string
];
// XXX this API doesn't exist anymore...
// store the message and do everything else that is typically done when going through historyKeeper
nfwssCtx.historyKeeper.onChannelMessage(nfwssCtx, channelStruct, fullMessage);
Env.historyKeeper.onChannelMessage(Server, channelStruct, fullMessage);
// call back with the message and the target channel.
// historyKeeper will take care of broadcasting it if anyone is in the channel