standardize some function signatures and factor out a lot of boilerplate

This commit is contained in:
ansuz 2020-02-03 18:32:21 -05:00
parent d1c6e67d17
commit 88be40ede3
5 changed files with 95 additions and 129 deletions

View File

@ -6,10 +6,11 @@ const nThen = require("nthen");
const Core = require("./core"); const Core = require("./core");
const Metadata = require("./metadata"); const Metadata = require("./metadata");
Channel.clearOwnedChannel = function (Env, channelId, unsafeKey, cb) { Channel.clearOwnedChannel = function (Env, safeKey, channelId, cb) {
if (typeof(channelId) !== 'string' || channelId.length !== 32) { if (typeof(channelId) !== 'string' || channelId.length !== 32) {
return cb('INVALID_ARGUMENTS'); return cb('INVALID_ARGUMENTS');
} }
var unsafeKey = Util.unescapeKeyCharacters(safeKey);
Metadata.getMetadata(Env, channelId, function (err, metadata) { Metadata.getMetadata(Env, channelId, function (err, metadata) {
if (err) { return void cb(err); } if (err) { return void cb(err); }
@ -24,13 +25,14 @@ Channel.clearOwnedChannel = function (Env, channelId, unsafeKey, cb) {
}); });
}; };
Channel.removeOwnedChannel = function (Env, channelId, unsafeKey, cb) { Channel.removeOwnedChannel = function (Env, safeKey, channelId, cb) {
if (typeof(channelId) !== 'string' || !Core.isValidId(channelId)) { if (typeof(channelId) !== 'string' || !Core.isValidId(channelId)) {
return cb('INVALID_ARGUMENTS'); return cb('INVALID_ARGUMENTS');
} }
var unsafeKey = Util.unescapeKeyCharacters(safeKey);
if (Env.blobStore.isFileId(channelId)) { if (Env.blobStore.isFileId(channelId)) {
var safeKey = Util.escapeKeyCharacters(unsafeKey); //var safeKey = Util.escapeKeyCharacters(unsafeKey);
var blobId = channelId; var blobId = channelId;
return void nThen(function (w) { return void nThen(function (w) {
@ -65,7 +67,7 @@ Channel.removeOwnedChannel = function (Env, channelId, unsafeKey, cb) {
if (err) { if (err) {
return void cb("E_PROOF_REMOVAL"); return void cb("E_PROOF_REMOVAL");
} }
cb(); cb(void 0, 'OK');
}); });
}); });
} }
@ -83,12 +85,15 @@ Channel.removeOwnedChannel = function (Env, channelId, unsafeKey, cb) {
channelId: channelId, channelId: channelId,
status: e? String(e): 'SUCCESS', status: e? String(e): 'SUCCESS',
}); });
cb(e); if (e) {
return void cb(e);
}
cb(void 0, 'OK');
}); });
}); });
}; };
Channel.removeOwnedChannelHistory = function (Env, channelId, unsafeKey, hash, cb) { Channel.removeOwnedChannelHistory = function (Env, channelId, unsafeKey, hash, cb) { // XXX UNSAFE
nThen(function (w) { nThen(function (w) {
Metadata.getMetadata(Env, channelId, w(function (err, metadata) { Metadata.getMetadata(Env, channelId, w(function (err, metadata) {
if (err) { return void cb(err); } if (err) { return void cb(err); }
@ -107,6 +112,7 @@ Channel.removeOwnedChannelHistory = function (Env, channelId, unsafeKey, hash, c
if (err) { return void cb(err); } if (err) { return void cb(err); }
// clear historyKeeper's cache for this channel // clear historyKeeper's cache for this channel
Env.historyKeeper.channelClose(channelId); Env.historyKeeper.channelClose(channelId);
cb(void 0, 'OK');
}); });
}); });
}; };

View File

@ -59,13 +59,16 @@ Core.getSession = function (Sessions, key) {
return user; return user;
}; };
Core.expireSession = function (Sessions, key) { Core.expireSession = function (Sessions, key, cb) {
var session = Sessions[key]; setTimeout(function () {
if (!session) { return; } var session = Sessions[key];
if (session.blobstage) { if (!session) { return; }
session.blobstage.close(); if (session.blobstage) {
} session.blobstage.close();
delete Sessions[key]; }
delete Sessions[key];
cb(void 0, 'OK');
});
}; };
var isTooOld = function (time, now) { var isTooOld = function (time, now) {

View File

@ -5,6 +5,7 @@ const Meta = require("../metadata");
const BatchRead = require("../batch-read"); const BatchRead = require("../batch-read");
const WriteQueue = require("../write-queue"); const WriteQueue = require("../write-queue");
const Core = require("./core"); const Core = require("./core");
const Util = require("../common-util");
const batchMetadata = BatchRead("GET_METADATA"); const batchMetadata = BatchRead("GET_METADATA");
Data.getMetadata = function (Env, channel, cb) { Data.getMetadata = function (Env, channel, cb) {
@ -34,7 +35,9 @@ Data.getMetadata = function (Env, channel, cb) {
} }
*/ */
var queueMetadata = WriteQueue(); var queueMetadata = WriteQueue();
Data.setMetadata = function (Env, data, unsafeKey, cb) { Data.setMetadata = function (Env, safeKey, data, cb) {
var unsafeKey = Util.unescapeKeyCharacters(safeKey);
var channel = data.channel; var channel = data.channel;
var command = data.command; var command = data.command;
if (!channel || !Core.isValidId(channel)) { return void cb ('INVALID_CHAN'); } if (!channel || !Core.isValidId(channel)) { return void cb ('INVALID_CHAN'); }

View File

@ -3,9 +3,9 @@ const Upload = module.exports;
const Util = require("../common-util"); const Util = require("../common-util");
const Pinning = require("./pin-rpc"); const Pinning = require("./pin-rpc");
const nThen = require("nthen"); const nThen = require("nthen");
const Core = require("./core");
// upload_status Upload.status = function (Env, safeKey, filesize, _cb) { // FIXME FILES
Upload.upload_status = function (Env, safeKey, filesize, _cb) { // FIXME FILES
var cb = Util.once(Util.mkAsync(_cb)); var cb = Util.once(Util.mkAsync(_cb));
// validate that the provided size is actually a positive number // validate that the provided size is actually a positive number
@ -29,9 +29,29 @@ Upload.upload_status = function (Env, safeKey, filesize, _cb) { // FIXME FILES
Pinning.getFreeSpace(Env, safeKey, function (e, free) { Pinning.getFreeSpace(Env, safeKey, function (e, free) {
if (e) { return void cb(e); } if (e) { return void cb(e); }
if (filesize >= free) { return cb('NOT_ENOUGH_SPACE'); } if (filesize >= free) { return cb('NOT_ENOUGH_SPACE'); }
var user = Core.getSession(Env.Sessions, safeKey);
user.pendingUploadSize = filesize;
user.currentUploadSize = 0;
cb(void 0, false); cb(void 0, false);
}); });
}); });
}; };
Upload.upload = function (Env, safeKey, chunk, cb) {
Env.blobStore.upload(safeKey, chunk, cb);
};
Upload.complete = function (Env, safeKey, arg, cb) {
Env.blobStore.complete(safeKey, arg, cb);
};
Upload.cancel = function (Env, safeKey, arg, cb) {
Env.blobStore.cancel(safeKey, arg, cb);
};
Upload.complete_owned = function (Env, safeKey, arg, cb) {
Env.blobStore.completeOwned(safeKey, arg, cb);
};

View File

@ -112,6 +112,30 @@ var handleUnauthenticatedMessage = function (Env, msg, respond, Server) {
} }
}; };
const AUTHENTICATED_USER_TARGETED = {
RESET: Pinning.resetUserPins,
PIN: Pinning.pinChannel,
UNPIN: Pinning.unpinChannel,
CLEAR_OWNED_CHANNEL: Channel.clearOwnedChannel,
REMOVE_OWNED_CHANNEL: Channel.removeOwnedChannel,
UPLOAD_STATUS: Upload.status,
UPLOAD: Upload.upload,
UPLOAD_COMPLETE: Upload.complete,
UPLOAD_CANCEL: Upload.cancel,
OWNED_UPLOAD_COMPLETE: Upload.complete_owned,
};
const AUTHENTICATED_USER_SCOPED = {
GET_HASH: Pinning.getHash,
GET_TOTAL_SIZE: Pinning.getTotalSize,
UPDATE_LIMITS: Quota.updateLimits,
GET_LIMIT: Pinning.getLimit,
EXPIRE_SESSION: Core.expireSession,
REMOVE_PINS: Pinning.removePins,
TRIM_PINS: Pinning.trimPins,
SET_METADATA: Metadata.setMetadata,
};
var handleAuthenticatedMessage = function (Env, map) { var handleAuthenticatedMessage = function (Env, map) {
var msg = map.msg; var msg = map.msg;
var safeKey = map.safeKey; var safeKey = map.safeKey;
@ -119,118 +143,34 @@ var handleAuthenticatedMessage = function (Env, map) {
var Respond = map.Respond; var Respond = map.Respond;
var Server = map.Server; var Server = map.Server;
Env.Log.silly('LOG_RPC', msg[0]); var TYPE = msg[0];
Env.Log.silly('LOG_RPC', TYPE);
if (typeof(AUTHENTICATED_USER_TARGETED[TYPE]) === 'function') {
return void AUTHENTICATED_USER_TARGETED[TYPE](Env, safeKey, msg[1], function (e, value) {
Env.WARN(e, value);
return void Respond(e, value);
});
}
if (typeof(AUTHENTICATED_USER_SCOPED[TYPE]) === 'function') {
return void AUTHENTICATED_USER_SCOPED[TYPE](Env, safeKey, function (e, value) {
if (e) {
Env.WARN(e, safeKey);
return void Respond(e);
}
Respond(e, value);
});
}
switch (msg[0]) { switch (msg[0]) {
case 'COOKIE': return void Respond(void 0); case 'COOKIE': return void Respond(void 0);
case 'RESET':
return Pinning.resetUserPins(Env, safeKey, msg[1], function (e, hash) { // XXX USER_TARGETED
//WARN(e, hash);
return void Respond(e, hash);
});
case 'PIN':
return Pinning.pinChannel(Env, safeKey, msg[1], function (e, hash) { // XXX USER_TARGETED
Env.WARN(e, hash);
Respond(e, hash);
});
case 'UNPIN':
return Pinning.unpinChannel(Env, safeKey, msg[1], function (e, hash) { // XXX USER_TARGETED
Env.WARN(e, hash);
Respond(e, hash);
});
case 'GET_HASH':
return void Pinning.getHash(Env, safeKey, function (e, hash) { // XXX USER_SCOPED
Env.WARN(e, hash);
Respond(e, hash);
});
case 'GET_TOTAL_SIZE': // TODO cache this, since it will get called quite a bit
return Pinning.getTotalSize(Env, safeKey, function (e, size) { // XXX USER_SCOPED
if (e) {
Env.WARN(e, safeKey);
return void Respond(e);
}
Respond(e, size);
});
case 'UPDATE_LIMITS':
return void Quota.updateLimits(Env, safeKey, function (e, limit) { // XXX USER_SCOPED
if (e) {
Env.WARN(e, limit);
return void Respond(e);
}
Respond(void 0, limit);
});
case 'GET_LIMIT':
return void Pinning.getLimit(Env, safeKey, function (e, limit) { // XXX USER_SCOPED
if (e) {
Env.WARN(e, limit);
return void Respond(e);
}
Respond(void 0, limit);
});
case 'EXPIRE_SESSION':
return void setTimeout(function () { // XXX USER_SCOPED
Core.expireSession(Env.Sessions, safeKey);
Respond(void 0, "OK");
});
case 'CLEAR_OWNED_CHANNEL':
return void Channel.clearOwnedChannel(Env, msg[1], publicKey, function (e, response) { // XXX USER_TARGETD_INVERSE
if (e) { return void Respond(e); }
Respond(void 0, response);
});
case 'REMOVE_OWNED_CHANNEL':
return void Channel.removeOwnedChannel(Env, msg[1], publicKey, function (e) { // XXX USER_TARGETED_INVERSE
if (e) { return void Respond(e); }
Respond(void 0, "OK");
});
case 'TRIM_OWNED_CHANNEL_HISTORY': case 'TRIM_OWNED_CHANNEL_HISTORY':
return void Channel.removeOwnedChannelHistory(Env, msg[1], publicKey, msg[2], function (e) { // XXX USER_TARGETED_DOUBLE return void Channel.removeOwnedChannelHistory(Env, msg[1], publicKey, msg[2], function (e) { // XXX USER_TARGETED_DOUBLE
if (e) { return void Respond(e); } if (e) { return void Respond(e); }
Respond(void 0, 'OK'); Respond(void 0, 'OK');
}); });
case 'REMOVE_PINS':
return void Pinning.removePins(Env, safeKey, function (e) { // XXX USER_SCOPED
if (e) { return void Respond(e); }
Respond(void 0, "OK");
});
case 'TRIM_PINS':
return void Pinning.trimPins(Env, safeKey, function (e) { // XXX USER_SCOPED
if (e) { return void Respond(e); }
Respond(void 0, "OK");
});
case 'UPLOAD':
return void Env.blobStore.upload(safeKey, msg[1], function (e, len) { // XXX USER_SCOPED_SPECIAL
Env.WARN(e, len);
Respond(e, len);
});
case 'UPLOAD_STATUS':
var filesize = msg[1];
return void Upload.upload_status(Env, safeKey, filesize, function (e, yes) { // XXX USER_TARGETED
if (!e && !yes) {
// no pending uploads, set the new size
var user = Core.getSession(Env.Sessions, safeKey);
user.pendingUploadSize = filesize;
user.currentUploadSize = 0;
}
Respond(e, yes);
});
case 'UPLOAD_COMPLETE':
return void Env.blobStore.complete(safeKey, msg[1], function (e, hash) { // XXX USER_SCOPED_SPECIAL
Env.WARN(e, hash);
Respond(e, hash);
});
case 'OWNED_UPLOAD_COMPLETE':
return void Env.blobStore.completeOwned(safeKey, msg[1], function (e, blobId) { // XXX USER_SCOPED_SPECIAL
Env.WARN(e, blobId);
Respond(e, blobId);
});
case 'UPLOAD_CANCEL':
// msg[1] is fileSize
// if we pass it here, we can start an upload right away without calling
// UPLOAD_STATUS again
return void Env.blobStore.cancel(safeKey, msg[1], function (e) { // XXX USER_SCOPED_SPECIAL
Env.WARN(e, 'UPLOAD_CANCEL');
Respond(e);
});
case 'WRITE_LOGIN_BLOCK': case 'WRITE_LOGIN_BLOCK':
return void Block.writeLoginBlock(Env, msg[1], function (e) { // XXX SPECIAL return void Block.writeLoginBlock(Env, msg[1], function (e) { // XXX SPECIAL
if (e) { if (e) {
@ -255,15 +195,9 @@ var handleAuthenticatedMessage = function (Env, map) {
} }
Respond(void 0, result); Respond(void 0, result);
}); });
case 'SET_METADATA':
return void Metadata.setMetadata(Env, msg[1], publicKey, function (e, data) { // XXX USER_TARGETED_INVERSE
if (e) {
Env.WARN(e, data);
return void Respond(e);
}
Respond(void 0, data);
});
default: default:
console.log(msg);
throw new Error("OOPS");
return void Respond('UNSUPPORTED_RPC_CALL', msg); return void Respond('UNSUPPORTED_RPC_CALL', msg);
} }
}; };