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:
38
lib/rpc.js
38
lib/rpc.js
@@ -24,7 +24,6 @@ const UNAUTHENTICATED_CALLS = [
|
||||
'GET_MULTIPLE_FILE_SIZE',
|
||||
'IS_CHANNEL_PINNED',
|
||||
'IS_NEW_CHANNEL',
|
||||
'GET_HISTORY_OFFSET',
|
||||
'GET_DELETED_PADS',
|
||||
'WRITE_PRIVATE_MESSAGE',
|
||||
];
|
||||
@@ -66,25 +65,9 @@ var isUnauthenticateMessage = function (msg) {
|
||||
return msg && msg.length === 2 && isUnauthenticatedCall(msg[0]);
|
||||
};
|
||||
|
||||
var handleUnauthenticatedMessage = function (Env, msg, respond, nfwssCtx) {
|
||||
var handleUnauthenticatedMessage = function (Env, msg, respond, Server) {
|
||||
Env.Log.silly('LOG_RPC', msg[0]);
|
||||
switch (msg[0]) {
|
||||
case 'GET_HISTORY_OFFSET': { // XXX not actually used anywhere?
|
||||
if (typeof(msg[1]) !== 'object' || typeof(msg[1].channelName) !== 'string') {
|
||||
return respond('INVALID_ARG_FORMAT', msg);
|
||||
}
|
||||
const msgHash = typeof(msg[1].msgHash) === 'string' ? msg[1].msgHash : undefined;
|
||||
nfwssCtx.getHistoryOffset(nfwssCtx, msg[1].channelName, msgHash, (e, ret) => {
|
||||
if (e) {
|
||||
if (e.code !== 'ENOENT') {
|
||||
Env.WARN(e.stack, msg);
|
||||
}
|
||||
return respond(e.message);
|
||||
}
|
||||
respond(e, [null, ret, null]);
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'GET_FILE_SIZE':
|
||||
return void Pinning.getFileSize(Env, msg[1], function (e, size) {
|
||||
Env.WARN(e, msg[1]);
|
||||
@@ -120,7 +103,7 @@ var handleUnauthenticatedMessage = function (Env, msg, respond, nfwssCtx) {
|
||||
respond(e, [null, isNew, null]);
|
||||
});
|
||||
case 'WRITE_PRIVATE_MESSAGE':
|
||||
return void Channel.writePrivateMessage(Env, msg[1], nfwssCtx, function (e, output) {
|
||||
return void Channel.writePrivateMessage(Env, msg[1], Server, function (e, output) {
|
||||
respond(e, output);
|
||||
});
|
||||
default:
|
||||
@@ -134,7 +117,7 @@ var handleAuthenticatedMessage = function (Env, map) {
|
||||
var safeKey = map.safeKey;
|
||||
var publicKey = map.publicKey;
|
||||
var Respond = map.Respond;
|
||||
var ctx = map.ctx;
|
||||
var Server = map.Server;
|
||||
|
||||
Env.Log.silly('LOG_RPC', msg[0]);
|
||||
switch (msg[0]) {
|
||||
@@ -265,7 +248,7 @@ var handleAuthenticatedMessage = function (Env, map) {
|
||||
Respond(e);
|
||||
});
|
||||
case 'ADMIN':
|
||||
return void Admin.command(Env, ctx, safeKey, msg[1], function (e, result) { // XXX SPECIAL
|
||||
return void Admin.command(Env, Server, safeKey, msg[1], function (e, result) { // XXX SPECIAL
|
||||
if (e) {
|
||||
Env.WARN(e, result);
|
||||
return void Respond(e);
|
||||
@@ -285,7 +268,7 @@ var handleAuthenticatedMessage = function (Env, map) {
|
||||
}
|
||||
};
|
||||
|
||||
var rpc = function (Env, ctx, data, respond) {
|
||||
var rpc = function (Env, Server, data, respond) {
|
||||
if (!Array.isArray(data)) {
|
||||
Env.Log.debug('INVALID_ARG_FORMET', data);
|
||||
return void respond('INVALID_ARG_FORMAT');
|
||||
@@ -304,7 +287,7 @@ var rpc = function (Env, ctx, data, respond) {
|
||||
}
|
||||
|
||||
if (isUnauthenticateMessage(msg)) {
|
||||
return handleUnauthenticatedMessage(Env, msg, respond, ctx);
|
||||
return handleUnauthenticatedMessage(Env, msg, respond);
|
||||
}
|
||||
|
||||
var signature = msg.shift();
|
||||
@@ -369,7 +352,7 @@ var rpc = function (Env, ctx, data, respond) {
|
||||
safeKey: safeKey,
|
||||
publicKey: publicKey,
|
||||
Respond: Respond,
|
||||
ctx: ctx,
|
||||
Server: Server,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -394,6 +377,7 @@ RPC.create = function (config, cb) {
|
||||
};
|
||||
|
||||
var Env = {
|
||||
historyKeeper: config.historyKeeper,
|
||||
defaultStorageLimit: config.defaultStorageLimit,
|
||||
maxUploadSize: config.maxUploadSize || (20 * 1024 * 1024),
|
||||
Sessions: {},
|
||||
@@ -465,11 +449,9 @@ RPC.create = function (config, cb) {
|
||||
Env.blobStore = blob;
|
||||
}));
|
||||
}).nThen(function () {
|
||||
// XXX it's ugly that we pass ctx and Env separately
|
||||
// when they're effectively the same thing...
|
||||
cb(void 0, function (ctx, data, respond) {
|
||||
cb(void 0, function (Server, data, respond) {
|
||||
try {
|
||||
return rpc(Env, ctx, data, respond);
|
||||
return rpc(Env, Server, data, respond);
|
||||
} catch (e) {
|
||||
console.log("Error from RPC with data " + JSON.stringify(data));
|
||||
console.log(e.stack);
|
||||
|
||||
Reference in New Issue
Block a user