From 0d6962068733d338a6a337361cf1802cff175da1 Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 4 Feb 2020 17:33:51 -0500 Subject: [PATCH] tweak the metadata line handler to handle an edge case in trim history --- lib/metadata.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/metadata.js b/lib/metadata.js index de40043af..677b2e5b5 100644 --- a/lib/metadata.js +++ b/lib/metadata.js @@ -211,12 +211,14 @@ Meta.createLineHandler = function (ref, errorHandler) { line: JSON.stringify(line), }); } + + // the case above is special, everything else should increment the index + var index = ref.index++; if (typeof(line) === 'undefined') { return; } if (Array.isArray(line)) { try { handleCommand(ref.meta, line); - ref.index++; } catch (err2) { errorHandler("METADATA_COMMAND_ERR", { error: err2.stack, @@ -226,8 +228,15 @@ Meta.createLineHandler = function (ref, errorHandler) { return; } - if (ref.index === 0 && typeof(line) === 'object') { - ref.index++; + // 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 + if (index < 2 && line typeof(line) === 'object') { // special case! ref.meta = line; return; @@ -235,7 +244,7 @@ Meta.createLineHandler = function (ref, errorHandler) { errorHandler("METADATA_HANDLER_WEIRDLINE", { line: line, - index: ref.index++, + index: index, }); }; };