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