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
pull/3766/head
John McLear 2020-03-15 20:33:33 +00:00 committed by muxator
parent 90f9b8a3bd
commit 14ae2ee950
1 changed files with 67 additions and 101 deletions

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)
{
console.error("Use: node bin/checkPad.js $PADID");
if (process.argv.length != 3) {
console.error("Use: node bin/checkPadDeltas.js $PADID");
process.exit(1);
}
//get the padID
var padId = process.argv[2];
//initalize the variables
var db, settings, padManager;
var npm = require("ep_etherpad-lite/node_modules/npm");
var async = require("ep_etherpad-lite/node_modules/async");
// get the padID
const padId = process.argv[2];
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
// external dependencies
// load and initialize NPM;
var expect = require('expect.js')
var diff = require('diff')
var async = require('async')
async.series([
//load npm
function(callback) {
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');
let npm = require('../src/node_modules/npm');
var async = require("ep_etherpad-lite/node_modules/async");
var Changeset = require("ep_etherpad-lite/static/js/Changeset");
//intallize the database
db.init(callback);
},
//get the pad
function (callback)
{
padManager = require('ep_etherpad-lite/node/db/PadManager');
npm.load({}, async function() {
padManager.doesPadExists(padId, function(err, exists)
{
if(!exists)
{
console.error("Pad does not exist");
process.exit(1);
}
try {
// initialize database
let settings = require('../src/node/utils/Settings');
let db = require('../src/node/db/DB');
await db.init();
padManager.getPad(padId, function(err, _pad)
{
pad = _pad;
callback(err);
});
});
},
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");
// load modules
let Changeset = require('ep_etherpad-lite/static/js/Changeset');
let padManager = require('../src/node/db/PadManager');
let exists = await padManager.doesPadExists(padId);
if (!exists) {
console.error("Pad does not exist");
process.exit(1);
}
// get the pad
let pad = await padManager.getPad(padId);
//create an array with key revisions
//key revisions always save the full pad atext
@ -94,46 +61,46 @@ async.series([
//run trough all revisions
async.forEachSeries(revisions, function(revNum, callback)
{
//console.log('Fetching', revNum)
db.db.get("pad:"+padId+":revs:" + revNum, function(err, revision)
{
if(err) return callback(err);
//console.log('Fetching', revNum)
db.db.get("pad:"+padId+":revs:" + revNum, function(err, revision)
{
if(err) return callback(err);
//check if there is a atext in the keyRevisions
if(~keyRevisions.indexOf(revNum) && (revision === undefined || revision.meta === undefined || revision.meta.atext === undefined)) {
console.error("No atext in key revision " + revNum);
callback();
return;
}
//check if there is a atext in the keyRevisions
if(~keyRevisions.indexOf(revNum) && (revision === undefined || revision.meta === undefined || revision.meta.atext === undefined)) {
console.error("No atext in key revision " + revNum);
callback();
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 {
//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;
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
}
}
if(~keyRevisions.indexOf(revNum)) {
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)
});
setImmediate(callback)
});
}, function(er) {
if(pad.atext.text == atext.text) console.log('ok')
else {
@ -142,13 +109,12 @@ async.series([
}
callback(er)
});
}
], function (err)
{
if(err) throw err;
else
{
console.log("finished");
process.exit(0);
} catch (e) {
console.trace(e);
process.exit(1);
}
});