2019-06-27 17:11:22 +08:00
|
|
|
var Meta = module.exports;
|
2021-04-30 17:18:22 +08:00
|
|
|
var Core = require("./commands/core");
|
2019-06-27 17:11:22 +08:00
|
|
|
|
2020-02-15 05:45:53 +08:00
|
|
|
var deduplicate = require("./common-util").deduplicateString;
|
2019-08-21 18:24:12 +08:00
|
|
|
|
2020-02-19 03:16:07 +08:00
|
|
|
/* Metadata fields and the commands that can modify them
|
|
|
|
|
|
|
|
we assume that these commands can only be performed
|
|
|
|
by owners or in some cases pending owners. Thus
|
|
|
|
the owners field is guaranteed to exist.
|
2019-06-27 17:11:22 +08:00
|
|
|
|
|
|
|
* channel <STRING>
|
|
|
|
* validateKey <STRING>
|
|
|
|
* owners <ARRAY>
|
|
|
|
* ADD_OWNERS
|
|
|
|
* RM_OWNERS
|
2020-02-19 03:16:07 +08:00
|
|
|
* RESET_OWNERS
|
|
|
|
* pending_owners <ARRAY>
|
|
|
|
* ADD_PENDING_OWNERS
|
|
|
|
* RM_PENDING_OWNERS
|
2019-06-27 17:11:22 +08:00
|
|
|
* expire <NUMBER>
|
2020-02-19 03:16:07 +08:00
|
|
|
* UPDATE_EXPIRATION (NOT_IMPLEMENTED)
|
|
|
|
* restricted <BOOLEAN>
|
|
|
|
* RESTRICT_ACCESS
|
|
|
|
* allowed <ARRAY>
|
|
|
|
* ADD_ALLOWED
|
|
|
|
* RM_ALLOWED
|
|
|
|
* RESET_ALLOWED
|
|
|
|
* ADD_OWNERS
|
|
|
|
* RESET_OWNERS
|
|
|
|
* mailbox <STRING|MAP>
|
|
|
|
* ADD_MAILBOX
|
|
|
|
* RM_MAILBOX
|
2019-06-27 17:11:22 +08:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
var commands = {};
|
|
|
|
|
2021-04-30 17:18:22 +08:00
|
|
|
var isValidPublicKey = Core.isValidPublicKey;
|
2019-09-27 20:28:59 +08:00
|
|
|
|
2020-02-19 03:16:07 +08:00
|
|
|
// isValidPublicKey is a better indication of what the above function does
|
|
|
|
// I'm preserving this function name in case we ever want to expand its
|
|
|
|
// criteria at a later time...
|
|
|
|
var isValidOwner = isValidPublicKey;
|
|
|
|
|
2020-02-17 22:26:36 +08:00
|
|
|
// ["RESTRICT_ACCESS", [true], 1561623438989]
|
|
|
|
// ["RESTRICT_ACCESS", [false], 1561623438989]
|
2020-02-18 00:54:27 +08:00
|
|
|
commands.RESTRICT_ACCESS = function (meta, args) {
|
|
|
|
if (!Array.isArray(args) || typeof(args[0]) !== 'boolean') {
|
|
|
|
throw new Error('INVALID_STATE');
|
|
|
|
}
|
|
|
|
|
|
|
|
var bool = args[0];
|
|
|
|
|
|
|
|
// reject the proposed command if there is no change in state
|
|
|
|
if (meta.restricted === bool) { return false; }
|
|
|
|
|
|
|
|
// apply the new state
|
|
|
|
meta.restricted = args[0];
|
|
|
|
|
|
|
|
// if you're disabling access restrictions then you can assume
|
|
|
|
// then there is nothing more to do. Leave the existing list as-is
|
|
|
|
if (!bool) { return true; }
|
|
|
|
|
|
|
|
// you're all set if an allow list already exists
|
|
|
|
if (Array.isArray(meta.allowed)) { return true; }
|
|
|
|
|
|
|
|
// otherwise define it
|
|
|
|
meta.allowed = [];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
2020-02-17 22:26:36 +08:00
|
|
|
|
|
|
|
// ["ADD_ALLOWED", ["7eEqelGso3EBr5jHlei6av4r9w2B9XZiGGwA1EgZ-5I=", ...], 1561623438989]
|
2020-02-18 00:54:27 +08:00
|
|
|
commands.ADD_ALLOWED = function (meta, args) {
|
2020-02-19 03:16:07 +08:00
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
throw new Error("INVALID_ARGS");
|
|
|
|
}
|
|
|
|
|
|
|
|
var allowed = meta.allowed || [];
|
|
|
|
|
|
|
|
var changed = false;
|
|
|
|
args.forEach(function (arg) {
|
|
|
|
// don't add invalid public keys
|
|
|
|
if (!isValidPublicKey(arg)) { return; }
|
|
|
|
// don't add owners to the allow list
|
|
|
|
if (meta.owners.indexOf(arg) >= 0) { return; }
|
|
|
|
// don't duplicate entries in the allow list
|
|
|
|
if (allowed.indexOf(arg) >= 0) { return; }
|
|
|
|
allowed.push(arg);
|
|
|
|
changed = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
meta.allowed = meta.allowed || allowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
2020-02-18 00:54:27 +08:00
|
|
|
};
|
2020-02-17 22:26:36 +08:00
|
|
|
|
|
|
|
// ["RM_ALLOWED", ["7eEqelGso3EBr5jHlei6av4r9w2B9XZiGGwA1EgZ-5I=", ...], 1561623438989]
|
2020-02-18 00:54:27 +08:00
|
|
|
commands.RM_ALLOWED = function (meta, args) {
|
2020-02-19 03:16:07 +08:00
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
throw new Error("INVALID_ARGS");
|
|
|
|
}
|
|
|
|
|
|
|
|
// there may not be anything to remove
|
|
|
|
if (!meta.allowed) { return false; }
|
|
|
|
|
|
|
|
var changed = false;
|
|
|
|
args.forEach(function (arg) {
|
|
|
|
var index = meta.allowed.indexOf(arg);
|
|
|
|
if (index < 0) { return; }
|
|
|
|
meta.allowed.splice(index, 1);
|
|
|
|
changed = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
};
|
|
|
|
|
|
|
|
var arrayHasChanged = function (A, B) {
|
|
|
|
var changed;
|
|
|
|
A.some(function (a) {
|
|
|
|
if (B.indexOf(a) < 0) { return (changed = true); }
|
|
|
|
});
|
|
|
|
if (changed) { return true; }
|
|
|
|
B.some(function (b) {
|
|
|
|
if (A.indexOf(b) < 0) { return (changed = true); }
|
|
|
|
});
|
|
|
|
return changed;
|
|
|
|
};
|
|
|
|
|
|
|
|
var filterInPlace = function (A, f) {
|
|
|
|
for (var i = A.length - 1; i >= 0; i--) {
|
|
|
|
if (f(A[i], i, A)) { A.splice(i, 1); }
|
|
|
|
}
|
2020-02-18 00:54:27 +08:00
|
|
|
};
|
2020-02-17 22:26:36 +08:00
|
|
|
|
|
|
|
// ["RESET_ALLOWED", ["7eEqelGso3EBr5jHlei6av4r9w2B9XZiGGwA1EgZ-5I=", ...], 1561623438989]
|
2020-02-18 00:54:27 +08:00
|
|
|
commands.RESET_ALLOWED = function (meta, args) {
|
2020-02-19 03:16:07 +08:00
|
|
|
if (!Array.isArray(args)) { throw new Error("INVALID_ARGS"); }
|
|
|
|
|
|
|
|
var updated = args.filter(function (arg) {
|
|
|
|
// don't allow invalid public keys
|
|
|
|
if (!isValidPublicKey(arg)) { return false; }
|
|
|
|
// don't ever add owners to the allow list
|
|
|
|
if (meta.owners.indexOf(arg)) { return false; }
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// this is strictly an optimization...
|
|
|
|
// a change in length is a clear indicator of a functional change
|
|
|
|
if (meta.allowed && meta.allowed.length !== updated.length) {
|
|
|
|
meta.allowed = updated;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise we must check that the arrays contain distinct elements
|
|
|
|
// if there is no functional change, then return false
|
|
|
|
if (!arrayHasChanged(meta.allowed, updated)) { return false; }
|
|
|
|
|
|
|
|
// otherwise overwrite the in-memory data and indicate that there was a change
|
|
|
|
meta.allowed = updated;
|
|
|
|
return true;
|
2020-02-18 00:54:27 +08:00
|
|
|
};
|
2020-02-17 22:26:36 +08:00
|
|
|
|
2019-06-27 17:11:22 +08:00
|
|
|
// ["ADD_OWNERS", ["7eEqelGso3EBr5jHlei6av4r9w2B9XZiGGwA1EgZ-5I="], 1561623438989]
|
|
|
|
commands.ADD_OWNERS = function (meta, args) {
|
2019-06-27 18:13:29 +08:00
|
|
|
// bail out if args isn't an array
|
2019-06-27 17:11:22 +08:00
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
throw new Error('METADATA_INVALID_OWNERS');
|
|
|
|
}
|
2019-06-27 18:13:29 +08:00
|
|
|
|
|
|
|
// you shouldn't be able to get here if there are no owners
|
|
|
|
// because only an owner should be able to change the owners
|
2019-06-27 17:11:22 +08:00
|
|
|
if (!Array.isArray(meta.owners)) {
|
|
|
|
throw new Error("METADATA_NONSENSE_OWNERS");
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:16:14 +08:00
|
|
|
var changed = false;
|
2019-06-27 17:11:22 +08:00
|
|
|
args.forEach(function (owner) {
|
2019-09-27 20:28:59 +08:00
|
|
|
if (!isValidOwner(owner)) { return; }
|
2019-06-27 17:11:22 +08:00
|
|
|
if (meta.owners.indexOf(owner) >= 0) { return; }
|
|
|
|
meta.owners.push(owner);
|
2019-09-02 23:16:14 +08:00
|
|
|
changed = true;
|
2019-06-27 17:11:22 +08:00
|
|
|
});
|
2019-09-02 23:16:14 +08:00
|
|
|
|
2020-02-19 03:16:07 +08:00
|
|
|
if (changed && Array.isArray(meta.allowed)) {
|
|
|
|
// make sure owners are not included in the allow list
|
|
|
|
filterInPlace(meta.allowed, function (member) {
|
|
|
|
return meta.owners.indexOf(member) !== -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:16:14 +08:00
|
|
|
return changed;
|
2019-06-27 17:11:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// ["RM_OWNERS", ["CrufexqXcY-z+eKJlEbNELVy5Sb7E-EAAEFI8GnEtZ0="], 1561623439989]
|
|
|
|
commands.RM_OWNERS = function (meta, args) {
|
2019-06-27 18:13:29 +08:00
|
|
|
// what are you doing if you don't have owners to remove?
|
2019-06-27 17:11:22 +08:00
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
throw new Error('METADATA_INVALID_OWNERS');
|
|
|
|
}
|
2019-06-27 18:13:29 +08:00
|
|
|
// if there aren't any owners to start, this is also pointless
|
2019-06-27 17:11:22 +08:00
|
|
|
if (!Array.isArray(meta.owners)) {
|
|
|
|
throw new Error("METADATA_NONSENSE_OWNERS");
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:16:14 +08:00
|
|
|
var changed = false;
|
2019-06-27 18:13:29 +08:00
|
|
|
// remove owners one by one
|
|
|
|
// we assume there are no duplicates
|
2019-06-27 17:11:22 +08:00
|
|
|
args.forEach(function (owner) {
|
|
|
|
var index = meta.owners.indexOf(owner);
|
2019-06-27 18:13:29 +08:00
|
|
|
if (index < 0) { return; }
|
2019-09-03 21:11:23 +08:00
|
|
|
if (meta.mailbox) {
|
|
|
|
if (typeof(meta.mailbox) === "string") {
|
|
|
|
delete meta.mailbox;
|
|
|
|
} else {
|
|
|
|
delete meta.mailbox[owner];
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 17:11:22 +08:00
|
|
|
meta.owners.splice(index, 1);
|
2019-09-02 23:16:14 +08:00
|
|
|
changed = true;
|
2019-06-27 17:11:22 +08:00
|
|
|
});
|
2019-09-02 23:16:14 +08:00
|
|
|
|
2020-02-20 03:39:28 +08:00
|
|
|
if (meta.owners.length === 0 && meta.restricted) {
|
|
|
|
meta.restricted = false;
|
|
|
|
}
|
2020-02-20 03:22:12 +08:00
|
|
|
|
2019-09-02 23:16:14 +08:00
|
|
|
return changed;
|
2019-06-27 17:11:22 +08:00
|
|
|
};
|
|
|
|
|
2019-08-30 18:07:03 +08:00
|
|
|
// ["ADD_PENDING_OWNERS", ["7eEqelGso3EBr5jHlei6av4r9w2B9XZiGGwA1EgZ-5I="], 1561623438989]
|
|
|
|
commands.ADD_PENDING_OWNERS = function (meta, args) {
|
|
|
|
// bail out if args isn't an array
|
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
throw new Error('METADATA_INVALID_PENDING_OWNERS');
|
|
|
|
}
|
|
|
|
|
|
|
|
// you shouldn't be able to get here if there are no owners
|
|
|
|
// because only an owner should be able to change the owners
|
|
|
|
if (meta.pending_owners && !Array.isArray(meta.pending_owners)) {
|
|
|
|
throw new Error("METADATA_NONSENSE_PENDING_OWNERS");
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:16:14 +08:00
|
|
|
var changed = false;
|
2019-08-30 18:07:03 +08:00
|
|
|
// Add pending_owners array if it doesn't exist
|
|
|
|
if (!meta.pending_owners) {
|
|
|
|
meta.pending_owners = deduplicate(args);
|
2019-09-02 23:16:14 +08:00
|
|
|
return true;
|
2019-08-30 18:07:03 +08:00
|
|
|
}
|
|
|
|
// or fill it
|
|
|
|
args.forEach(function (owner) {
|
2019-09-27 20:28:59 +08:00
|
|
|
if (!isValidOwner(owner)) { return; }
|
2019-08-30 18:07:03 +08:00
|
|
|
if (meta.pending_owners.indexOf(owner) >= 0) { return; }
|
|
|
|
meta.pending_owners.push(owner);
|
2019-09-02 23:16:14 +08:00
|
|
|
changed = true;
|
2019-08-30 18:07:03 +08:00
|
|
|
});
|
2019-09-02 23:16:14 +08:00
|
|
|
|
|
|
|
return changed;
|
2019-08-30 18:07:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// ["RM_PENDING_OWNERS", ["CrufexqXcY-z+eKJlEbNELVy5Sb7E-EAAEFI8GnEtZ0="], 1561623439989]
|
|
|
|
commands.RM_PENDING_OWNERS = function (meta, args) {
|
|
|
|
// what are you doing if you don't have owners to remove?
|
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
throw new Error('METADATA_INVALID_PENDING_OWNERS');
|
|
|
|
}
|
|
|
|
// if there aren't any owners to start, this is also pointless
|
|
|
|
if (!Array.isArray(meta.pending_owners)) {
|
|
|
|
throw new Error("METADATA_NONSENSE_PENDING_OWNERS");
|
|
|
|
}
|
|
|
|
|
2019-09-02 23:16:14 +08:00
|
|
|
var changed = false;
|
2019-08-30 18:07:03 +08:00
|
|
|
// remove owners one by one
|
|
|
|
// we assume there are no duplicates
|
|
|
|
args.forEach(function (owner) {
|
|
|
|
var index = meta.pending_owners.indexOf(owner);
|
|
|
|
if (index < 0) { return; }
|
|
|
|
meta.pending_owners.splice(index, 1);
|
2019-09-02 23:16:14 +08:00
|
|
|
changed = true;
|
2019-08-30 18:07:03 +08:00
|
|
|
});
|
2019-09-02 23:16:14 +08:00
|
|
|
|
|
|
|
return changed;
|
2019-08-30 18:07:03 +08:00
|
|
|
};
|
|
|
|
|
2019-06-27 18:13:29 +08:00
|
|
|
// ["RESET_OWNERS", ["7eEqelGso3EBr5jHlei6av4r9w2B9XZiGGwA1EgZ-5I="], 1561623439989]
|
|
|
|
commands.RESET_OWNERS = function (meta, args) {
|
|
|
|
// expect a new array, even if it's empty
|
|
|
|
if (!Array.isArray(args)) {
|
|
|
|
throw new Error('METADATA_INVALID_OWNERS');
|
|
|
|
}
|
|
|
|
// assume there are owners to start
|
|
|
|
if (!Array.isArray(meta.owners)) {
|
|
|
|
throw new Error("METADATA_NONSENSE_OWNERS");
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite the existing owners with the new one
|
2019-09-27 20:28:59 +08:00
|
|
|
meta.owners = deduplicate(args.filter(isValidOwner));
|
2020-02-19 03:16:07 +08:00
|
|
|
|
|
|
|
if (Array.isArray(meta.allowed)) {
|
|
|
|
// make sure owners are not included in the allow list
|
|
|
|
filterInPlace(meta.allowed, function (member) {
|
|
|
|
return meta.owners.indexOf(member) !== -1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-20 03:39:28 +08:00
|
|
|
if (meta.owners.length === 0 && meta.restricted) {
|
|
|
|
meta.restricted = false;
|
|
|
|
}
|
2020-02-20 03:22:12 +08:00
|
|
|
|
2019-09-02 23:16:14 +08:00
|
|
|
return true;
|
2019-06-27 18:13:29 +08:00
|
|
|
};
|
|
|
|
|
2019-09-03 21:11:23 +08:00
|
|
|
// ["ADD_MAILBOX", {"7eEqelGso3EBr5jHlei6av4r9w2B9XZiGGwA1EgZ-5I=": mailbox, ...}, 1561623439989]
|
|
|
|
commands.ADD_MAILBOX = function (meta, args) {
|
|
|
|
// expect a new array, even if it's empty
|
|
|
|
if (!args || typeof(args) !== "object") {
|
|
|
|
throw new Error('METADATA_INVALID_MAILBOX');
|
|
|
|
}
|
|
|
|
// assume there are owners to start
|
|
|
|
if (!Array.isArray(meta.owners)) {
|
|
|
|
throw new Error("METADATA_NONSENSE_OWNERS");
|
|
|
|
}
|
|
|
|
|
|
|
|
var changed = false;
|
|
|
|
|
|
|
|
// For each mailbox we try to add, check if the associated edPublic is an owner
|
|
|
|
// If they are, add or replace the mailbox
|
|
|
|
Object.keys(args).forEach(function (edPublic) {
|
|
|
|
if (meta.owners.indexOf(edPublic) === -1) { return; }
|
|
|
|
|
|
|
|
if (typeof(meta.mailbox) === "string") {
|
|
|
|
var str = meta.mailbox;
|
|
|
|
meta.mailbox = {};
|
|
|
|
meta.mailbox[meta.owners[0]] = str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure mailbox is defined
|
|
|
|
if (!meta.mailbox) { meta.mailbox = {}; }
|
|
|
|
|
|
|
|
meta.mailbox[edPublic] = args[edPublic];
|
|
|
|
changed = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
};
|
|
|
|
|
2020-02-19 03:16:07 +08:00
|
|
|
commands.RM_MAILBOX = function (meta, args) {
|
|
|
|
if (!Array.isArray(args)) { throw new Error("INVALID_ARGS"); }
|
|
|
|
if (!meta.mailbox || typeof(meta.mailbox) === 'undefined') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (typeof(meta.mailbox) === 'string' && args.length === 0) {
|
|
|
|
delete meta.mailbox;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var changed = false;
|
|
|
|
args.forEach(function (arg) {
|
|
|
|
if (meta.mailbox[arg] === 'undefined') { return; }
|
|
|
|
delete meta.mailbox[arg];
|
|
|
|
changed = true;
|
|
|
|
});
|
|
|
|
return changed;
|
|
|
|
};
|
|
|
|
|
2019-06-27 17:11:22 +08:00
|
|
|
commands.UPDATE_EXPIRATION = function () {
|
2019-08-21 18:28:19 +08:00
|
|
|
throw new Error("E_NOT_IMPLEMENTED");
|
2019-06-27 17:11:22 +08:00
|
|
|
};
|
|
|
|
|
2019-08-30 17:49:15 +08:00
|
|
|
var handleCommand = Meta.handleCommand = function (meta, line) {
|
2019-06-27 17:11:22 +08:00
|
|
|
var command = line[0];
|
|
|
|
var args = line[1];
|
|
|
|
//var time = line[2];
|
|
|
|
|
|
|
|
if (typeof(commands[command]) !== 'function') {
|
|
|
|
throw new Error("METADATA_UNSUPPORTED_COMMAND");
|
|
|
|
}
|
|
|
|
|
2019-09-03 19:29:57 +08:00
|
|
|
return commands[command](meta, args);
|
2019-06-27 17:11:22 +08:00
|
|
|
};
|
2019-08-30 17:49:15 +08:00
|
|
|
Meta.commands = Object.keys(commands);
|
2019-06-27 17:11:22 +08:00
|
|
|
|
|
|
|
Meta.createLineHandler = function (ref, errorHandler) {
|
|
|
|
ref.meta = {};
|
2019-08-12 21:26:23 +08:00
|
|
|
ref.index = 0;
|
2020-02-22 02:38:56 +08:00
|
|
|
ref.logged = {};
|
2019-06-27 17:11:22 +08:00
|
|
|
|
2019-06-28 00:21:12 +08:00
|
|
|
return function (err, line) {
|
2019-06-27 17:11:22 +08:00
|
|
|
if (err) {
|
2019-09-06 19:36:06 +08:00
|
|
|
// it's not abnormal that metadata exists without a corresponding log
|
|
|
|
// so ENOENT is fine
|
|
|
|
if (ref.index === 0 && err.code === 'ENOENT') { return; }
|
|
|
|
// any other errors are abnormal
|
2019-06-27 17:11:22 +08:00
|
|
|
return void errorHandler('METADATA_HANDLER_LINE_ERR', {
|
|
|
|
error: err,
|
2019-08-12 21:26:23 +08:00
|
|
|
index: ref.index,
|
2019-06-27 17:11:22 +08:00
|
|
|
line: JSON.stringify(line),
|
|
|
|
});
|
|
|
|
}
|
2020-02-05 06:33:51 +08:00
|
|
|
|
|
|
|
// the case above is special, everything else should increment the index
|
|
|
|
var index = ref.index++;
|
2019-09-11 17:00:01 +08:00
|
|
|
if (typeof(line) === 'undefined') { return; }
|
2019-06-27 17:11:22 +08:00
|
|
|
|
2020-02-22 02:38:56 +08:00
|
|
|
|
2019-06-27 17:11:22 +08:00
|
|
|
if (Array.isArray(line)) {
|
|
|
|
try {
|
|
|
|
handleCommand(ref.meta, line);
|
2019-06-28 00:21:12 +08:00
|
|
|
} catch (err2) {
|
2020-02-22 02:38:56 +08:00
|
|
|
var code = err2.message;
|
|
|
|
if (ref.logged[code]) { return; }
|
|
|
|
|
|
|
|
ref.logged[code] = true;
|
2019-06-27 17:11:22 +08:00
|
|
|
errorHandler("METADATA_COMMAND_ERR", {
|
2019-06-28 00:21:12 +08:00
|
|
|
error: err2.stack,
|
2019-06-27 17:11:22 +08:00
|
|
|
line: line,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-05 06:33:51 +08:00
|
|
|
// the first line of a channel is processed before the dedicated metadata log.
|
|
|
|
// it can contain a map, in which case it should be used as the initial state.
|
|
|
|
// it's possible that a trim-history command was interrupted, in which case
|
|
|
|
// this first message might exist in parallel with the more recent metadata log
|
|
|
|
// which will contain the computed state of the previous metadata log
|
|
|
|
// which has since been archived.
|
|
|
|
// Thus, accept both the first and second lines you process as valid initial state
|
|
|
|
// preferring the second if it exists
|
2020-02-05 18:28:27 +08:00
|
|
|
if (index < 2 && line && typeof(line) === 'object') {
|
2019-06-27 17:11:22 +08:00
|
|
|
// special case!
|
|
|
|
ref.meta = line;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
errorHandler("METADATA_HANDLER_WEIRDLINE", {
|
|
|
|
line: line,
|
2020-02-05 06:33:51 +08:00
|
|
|
index: index,
|
2019-06-27 17:11:22 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
2019-08-28 18:40:35 +08:00
|
|
|
|