checkPadDeltas: version by JohnMcLear

From https://github.com/ether/etherpad-lite/pull/3717#issuecomment-602179127

> Afaik I used async / await that's pretty much all, I think I had to do some
> polish because something was broken, remember stuff like pad.getPadAuthors was
> b0rked in 1.7 or so
This commit is contained in:
John McLear 2020-03-15 20:33:33 +00:00 committed by muxator
parent 90f9b8a3bd
commit 14ae2ee950

View file

@ -1,77 +1,44 @@
/* /*
This is a debug tool. It checks all revisions for data corruption * This is a debug tool. It checks all revisions for data corruption
*/ */
if(process.argv.length != 3) if (process.argv.length != 3) {
{ console.error("Use: node bin/checkPadDeltas.js $PADID");
console.error("Use: node bin/checkPad.js $PADID");
process.exit(1); process.exit(1);
} }
//get the padID
var padId = process.argv[2];
//initalize the variables // get the padID
var db, settings, padManager; const padId = process.argv[2];
var npm = require("ep_etherpad-lite/node_modules/npm");
var async = require("ep_etherpad-lite/node_modules/async");
var Changeset = require("ep_etherpad-lite/static/js/Changeset"); // load and initialize NPM;
// external dependencies
var expect = require('expect.js') var expect = require('expect.js')
var diff = require('diff') var diff = require('diff')
var async = require('async')
async.series([ let npm = require('../src/node_modules/npm');
//load npm var async = require("ep_etherpad-lite/node_modules/async");
function(callback) { var Changeset = require("ep_etherpad-lite/static/js/Changeset");
npm.load({}, function(er) {
callback(er);
})
},
//load modules
function(callback) {
settings = require('ep_etherpad-lite/node/utils/Settings');
db = require('ep_etherpad-lite/node/db/DB');
//intallize the database npm.load({}, async function() {
db.init(callback);
},
//get the pad
function (callback)
{
padManager = require('ep_etherpad-lite/node/db/PadManager');
padManager.doesPadExists(padId, function(err, exists) try {
{ // initialize database
if(!exists) let settings = require('../src/node/utils/Settings');
{ let db = require('../src/node/db/DB');
console.error("Pad does not exist"); await db.init();
process.exit(1);
}
padManager.getPad(padId, function(err, _pad) // load modules
{ let Changeset = require('ep_etherpad-lite/static/js/Changeset');
pad = _pad; let padManager = require('../src/node/db/PadManager');
callback(err);
}); let exists = await padManager.doesPadExists(padId);
}); if (!exists) {
}, console.error("Pad does not exist");
function (callback)
{
//check if the pad has a pool
if(pad.pool === undefined )
{
console.error("Attribute pool is missing");
process.exit(1);
}
//check if the pad has atext
if(pad.atext === undefined )
{
console.error("AText is missing");
process.exit(1); process.exit(1);
} }
// get the pad
let pad = await padManager.getPad(padId);
//create an array with key revisions //create an array with key revisions
//key revisions always save the full pad atext //key revisions always save the full pad atext
@ -94,46 +61,46 @@ async.series([
//run trough all revisions //run trough all revisions
async.forEachSeries(revisions, function(revNum, callback) async.forEachSeries(revisions, function(revNum, callback)
{ {
//console.log('Fetching', revNum) //console.log('Fetching', revNum)
db.db.get("pad:"+padId+":revs:" + revNum, function(err, revision) db.db.get("pad:"+padId+":revs:" + revNum, function(err, revision)
{ {
if(err) return callback(err); if(err) return callback(err);
//check if there is a atext in the keyRevisions //check if there is a atext in the keyRevisions
if(~keyRevisions.indexOf(revNum) && (revision === undefined || revision.meta === undefined || revision.meta.atext === undefined)) { if(~keyRevisions.indexOf(revNum) && (revision === undefined || revision.meta === undefined || revision.meta.atext === undefined)) {
console.error("No atext in key revision " + revNum); console.error("No atext in key revision " + revNum);
callback(); callback();
return; return;
} }
try {
//console.log("check revision ", revNum);
var cs = revision.changeset;
atext = Changeset.applyToAText(cs, atext, pad.pool);
}
catch(e) {
console.error("Bad changeset at revision " + revNum + " - " + e.message);
callback();
return;
}
if(~keyRevisions.indexOf(revNum)) {
try { try {
//console.log("check revision ", revNum); expect(revision.meta.atext.text).to.eql(atext.text)
var cs = revision.changeset; expect(revision.meta.atext.attribs).to.eql(atext.attribs)
atext = Changeset.applyToAText(cs, atext, pad.pool); }catch(e) {
} console.error("Atext in key revision "+revNum+" doesn't match computed one.")
catch(e) { console.log(diff.diffChars(atext.text, revision.meta.atext.text).map(function(op) {if(!op.added && !op.removed) op.value = op.value.length; return op}))
console.error("Bad changeset at revision " + revNum + " - " + e.message); //console.error(e)
callback(); //console.log('KeyRev. :', revision.meta.atext)
return; //console.log('Computed:', atext)
callback()
return
} }
}
if(~keyRevisions.indexOf(revNum)) { setImmediate(callback)
try { });
expect(revision.meta.atext.text).to.eql(atext.text)
expect(revision.meta.atext.attribs).to.eql(atext.attribs)
}catch(e) {
console.error("Atext in key revision "+revNum+" doesn't match computed one.")
console.log(diff.diffChars(atext.text, revision.meta.atext.text).map(function(op) {if(!op.added && !op.removed) op.value = op.value.length; return op}))
//console.error(e)
//console.log('KeyRev. :', revision.meta.atext)
//console.log('Computed:', atext)
callback()
return
}
}
setImmediate(callback)
});
}, function(er) { }, function(er) {
if(pad.atext.text == atext.text) console.log('ok') if(pad.atext.text == atext.text) console.log('ok')
else { else {
@ -142,13 +109,12 @@ async.series([
} }
callback(er) callback(er)
}); });
}
], function (err)
{
if(err) throw err;
else
{
console.log("finished");
process.exit(0); process.exit(0);
} catch (e) {
console.trace(e);
process.exit(1);
} }
}); });