2021-01-18 08:53:15 +00:00
|
|
|
'use strict';
|
2011-12-05 17:24:59 +00:00
|
|
|
/*
|
2019-02-08 22:20:57 +00:00
|
|
|
* This is a debug tool. It checks all revisions for data corruption
|
|
|
|
*/
|
2011-12-05 17:24:59 +00:00
|
|
|
|
2021-01-18 08:53:15 +00:00
|
|
|
// As of v14, Node.js does not exit when there is an unhandled Promise rejection. Convert an
|
|
|
|
// unhandled rejection into an uncaught exception, which does cause Node.js to exit.
|
|
|
|
process.on('unhandledRejection', (err) => { throw err; });
|
|
|
|
|
2021-01-09 07:44:59 +00:00
|
|
|
const npm = require('ep_etherpad-lite/node_modules/npm');
|
|
|
|
const util = require('util');
|
|
|
|
|
2021-01-18 08:53:15 +00:00
|
|
|
if (process.argv.length !== 3) throw new Error('Use: node bin/checkPad.js $PADID');
|
2019-02-08 22:20:57 +00:00
|
|
|
|
2019-01-26 23:52:02 +00:00
|
|
|
// get the padID
|
|
|
|
const padId = process.argv[2];
|
2021-01-18 08:53:15 +00:00
|
|
|
let checkRevisionCount = 0;
|
2019-01-26 23:52:02 +00:00
|
|
|
|
2021-01-09 07:44:59 +00:00
|
|
|
(async () => {
|
|
|
|
await util.promisify(npm.load)({});
|
|
|
|
|
2019-01-26 23:52:02 +00:00
|
|
|
try {
|
|
|
|
// initialize database
|
2021-01-18 08:53:15 +00:00
|
|
|
require('ep_etherpad-lite/node/utils/Settings');
|
|
|
|
const db = require('ep_etherpad-lite/node/db/DB');
|
2019-01-26 23:52:02 +00:00
|
|
|
await db.init();
|
|
|
|
|
|
|
|
// load modules
|
2020-11-23 18:21:51 +00:00
|
|
|
const Changeset = require('ep_etherpad-lite/static/js/Changeset');
|
2021-01-18 08:53:15 +00:00
|
|
|
const padManager = require('ep_etherpad-lite/node/db/PadManager');
|
2019-01-26 23:52:02 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const exists = await padManager.doesPadExists(padId);
|
2021-01-18 08:53:15 +00:00
|
|
|
if (!exists) throw new Error('Pad does not exist');
|
2019-02-08 22:20:57 +00:00
|
|
|
|
2019-01-26 23:52:02 +00:00
|
|
|
// get the pad
|
2020-11-23 18:21:51 +00:00
|
|
|
const pad = await padManager.getPad(padId);
|
2019-02-08 22:20:57 +00:00
|
|
|
|
|
|
|
// create an array with key revisions
|
|
|
|
// key revisions always save the full pad atext
|
2020-11-23 18:21:51 +00:00
|
|
|
const head = pad.getHeadRevisionNumber();
|
|
|
|
const keyRevisions = [];
|
2019-01-26 23:52:02 +00:00
|
|
|
for (let rev = 0; rev < head; rev += 100) {
|
|
|
|
keyRevisions.push(rev);
|
2011-12-05 17:24:59 +00:00
|
|
|
}
|
2019-02-08 22:20:57 +00:00
|
|
|
|
|
|
|
// run through all key revisions
|
2021-01-18 08:53:15 +00:00
|
|
|
for (let keyRev of keyRevisions) {
|
|
|
|
keyRev = parseInt(keyRev);
|
2019-02-08 22:20:57 +00:00
|
|
|
// create an array of revisions we need till the next keyRevision or the End
|
2020-11-23 18:21:51 +00:00
|
|
|
const revisionsNeeded = [];
|
2019-01-26 23:52:02 +00:00
|
|
|
for (let rev = keyRev; rev <= keyRev + 100 && rev <= head; rev++) {
|
|
|
|
revisionsNeeded.push(rev);
|
2011-12-05 17:24:59 +00:00
|
|
|
}
|
2019-02-08 22:20:57 +00:00
|
|
|
|
|
|
|
// this array will hold all revision changesets
|
2020-11-23 18:21:51 +00:00
|
|
|
const revisions = [];
|
2019-02-08 22:20:57 +00:00
|
|
|
|
|
|
|
// run through all needed revisions and get them from the database
|
2020-11-23 18:21:51 +00:00
|
|
|
for (const revNum of revisionsNeeded) {
|
|
|
|
const revision = await db.get(`pad:${padId}:revs:${revNum}`);
|
2019-01-26 23:52:02 +00:00
|
|
|
revisions[revNum] = revision;
|
|
|
|
}
|
2019-02-08 22:20:57 +00:00
|
|
|
|
2019-01-26 23:52:02 +00:00
|
|
|
// check if the pad has a pool
|
2021-01-08 22:47:38 +00:00
|
|
|
if (pad.pool == null) throw new Error('Attribute pool is missing');
|
2019-02-08 22:20:57 +00:00
|
|
|
|
2019-01-26 23:52:02 +00:00
|
|
|
// check if there is an atext in the keyRevisions
|
2021-01-09 07:28:45 +00:00
|
|
|
let {meta: {atext} = {}} = revisions[keyRev] || {};
|
|
|
|
if (atext == null) {
|
2020-11-23 18:21:51 +00:00
|
|
|
console.error(`No atext in key revision ${keyRev}`);
|
2019-01-26 23:52:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
2019-02-08 22:20:57 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
const apool = pad.pool;
|
2019-01-26 23:52:02 +00:00
|
|
|
|
|
|
|
for (let rev = keyRev + 1; rev <= keyRev + 100 && rev <= head; rev++) {
|
2021-01-18 08:53:15 +00:00
|
|
|
checkRevisionCount++;
|
2019-01-26 23:52:02 +00:00
|
|
|
try {
|
2020-11-23 18:21:51 +00:00
|
|
|
const cs = revisions[rev].changeset;
|
2019-01-26 23:52:02 +00:00
|
|
|
atext = Changeset.applyToAText(cs, atext, apool);
|
2020-11-23 18:21:51 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(`Bad changeset at revision ${rev} - ${e.message}`);
|
2019-01-26 23:52:02 +00:00
|
|
|
continue;
|
2011-12-05 17:24:59 +00:00
|
|
|
}
|
2019-01-26 23:52:02 +00:00
|
|
|
}
|
2021-01-18 08:53:15 +00:00
|
|
|
console.log(`Finished: Checked ${checkRevisionCount} revisions`);
|
2019-01-26 23:52:02 +00:00
|
|
|
}
|
2021-01-18 08:53:15 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.trace(err);
|
|
|
|
throw err;
|
2011-12-05 17:24:59 +00:00
|
|
|
}
|
2021-01-09 07:44:59 +00:00
|
|
|
})();
|