etherpad-lite/bin/checkPadDeltas.js

112 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-11-21 05:57:42 +01:00
/*
* This is a debug tool. It checks all revisions for data corruption
*/
if (process.argv.length != 3) {
console.error('Use: node bin/checkPadDeltas.js $PADID');
2020-11-21 05:57:42 +01:00
process.exit(1);
}
// get the padID
const padId = process.argv[2];
// load and initialize NPM;
const expect = require('expect.js');
const diff = require('diff');
var async = require('async');
2020-11-21 05:57:42 +01:00
const npm = require('../src/node_modules/npm');
var async = require('ep_etherpad-lite/node_modules/async');
const Changeset = require('ep_etherpad-lite/static/js/Changeset');
2020-11-21 05:57:42 +01:00
npm.load({}, async () => {
2020-11-21 05:57:42 +01:00
try {
// initialize database
const settings = require('../src/node/utils/Settings');
const db = require('../src/node/db/DB');
2020-11-21 05:57:42 +01:00
await db.init();
// load modules
const Changeset = require('ep_etherpad-lite/static/js/Changeset');
const padManager = require('../src/node/db/PadManager');
2020-11-21 05:57:42 +01:00
const exists = await padManager.doesPadExists(padId);
2020-11-21 05:57:42 +01:00
if (!exists) {
console.error('Pad does not exist');
2020-11-21 05:57:42 +01:00
process.exit(1);
}
// get the pad
const pad = await padManager.getPad(padId);
// create an array with key revisions
// key revisions always save the full pad atext
const head = pad.getHeadRevisionNumber();
const keyRevisions = [];
for (var i = 0; i < head; i += 100) {
2020-11-21 05:57:42 +01:00
keyRevisions.push(i);
}
// create an array with all revisions
const revisions = [];
for (var i = 0; i <= head; i++) {
2020-11-21 05:57:42 +01:00
revisions.push(i);
}
let atext = Changeset.makeAText('\n');
2020-11-21 05:57:42 +01:00
// run trough all revisions
async.forEachSeries(revisions, (revNum, callback) => {
// console.log('Fetching', revNum)
db.db.get(`pad:${padId}:revs:${revNum}`, (err, revision) => {
if (err) return callback(err);
2020-11-21 05:57:42 +01:00
// 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}`);
2020-11-21 05:57:42 +01:00
callback();
return;
}
try {
// console.log("check revision ", revNum);
const cs = revision.changeset;
2020-11-21 05:57:42 +01:00
atext = Changeset.applyToAText(cs, atext, pad.pool);
} catch (e) {
console.error(`Bad changeset at revision ${revNum} - ${e.message}`);
2020-11-21 05:57:42 +01:00
callback();
return;
}
if (~keyRevisions.indexOf(revNum)) {
2020-11-21 05:57:42 +01:00
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((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;
2020-11-21 05:57:42 +01:00
}
}
setImmediate(callback);
2020-11-21 05:57:42 +01:00
});
}, (er) => {
if (pad.atext.text == atext.text) { console.log('ok'); } else {
console.error('Pad AText doesn\'t match computed one! (Computed ', atext.text.length, ', db', pad.atext.text.length, ')');
console.log(diff.diffChars(atext.text, pad.atext.text).map((op) => { if (!op.added && !op.removed) op.value = op.value.length; return op; }));
2020-11-21 05:57:42 +01:00
}
callback(er);
2020-11-21 05:57:42 +01:00
});
process.exit(0);
} catch (e) {
console.trace(e);
process.exit(1);
}
});