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 Metadata = require("./metadata");
Channel.clearOwnedChannel = function (Env, channelId, unsafeKey, cb) {
Channel.clearOwnedChannel = function (Env, safeKey, channelId, cb) {
if (typeof(channelId) !== 'string' || channelId.length !== 32) {
return cb('INVALID_ARGUMENTS');
}
var unsafeKey = Util.unescapeKeyCharacters(safeKey);
Metadata.getMetadata(Env, channelId, function (err, metadata) {
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)) {
return cb('INVALID_ARGUMENTS');
}
var unsafeKey = Util.unescapeKeyCharacters(safeKey);
if (Env.blobStore.isFileId(channelId)) {
var safeKey = Util.escapeKeyCharacters(unsafeKey);
//var safeKey = Util.escapeKeyCharacters(unsafeKey);
var blobId = channelId;
return void nThen(function (w) {
@@ -65,7 +67,7 @@ Channel.removeOwnedChannel = function (Env, channelId, unsafeKey, cb) {
if (err) {
return void cb("E_PROOF_REMOVAL");
}
cb();
cb(void 0, 'OK');
});
});
}
@@ -83,12 +85,15 @@ Channel.removeOwnedChannel = function (Env, channelId, unsafeKey, cb) {
channelId: channelId,
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) {
Metadata.getMetadata(Env, channelId, w(function (err, metadata) {
if (err) { return void cb(err); }
@@ -107,6 +112,7 @@ Channel.removeOwnedChannelHistory = function (Env, channelId, unsafeKey, hash, c
if (err) { return void cb(err); }
// clear historyKeeper's cache for this channel
Env.historyKeeper.channelClose(channelId);
cb(void 0, 'OK');
});
});
};

View File

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

View File

@@ -5,6 +5,7 @@ const Meta = require("../metadata");
const BatchRead = require("../batch-read");
const WriteQueue = require("../write-queue");
const Core = require("./core");
const Util = require("../common-util");
const batchMetadata = BatchRead("GET_METADATA");
Data.getMetadata = function (Env, channel, cb) {
@@ -34,7 +35,9 @@ Data.getMetadata = function (Env, channel, cb) {
}
*/
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 command = data.command;
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 Pinning = require("./pin-rpc");
const nThen = require("nthen");
const Core = require("./core");
// upload_status
Upload.upload_status = function (Env, safeKey, filesize, _cb) { // FIXME FILES
Upload.status = function (Env, safeKey, filesize, _cb) { // FIXME FILES
var cb = Util.once(Util.mkAsync(_cb));
// 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) {
if (e) { return void cb(e); }
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);
});
});
};
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);
};