Files
notesnook/packages/core/database/backup.js

89 lines
1.9 KiB
JavaScript
Raw Normal View History

import Hashes from "jshashes";
const sha256 = new Hashes.SHA256();
const invalidKeys = ["user", "t"];
const validTypes = ["mobile", "web", "node"];
2020-09-13 13:24:24 +05:00
export default class Backup {
/**
*
* @param {import("../api/index.js").default} db
*/
constructor(db) {
this._db = db;
}
/**
*
* @param {"web"|"mobile"|"node"} type
* @param {boolean} encrypt
*/
async export(type, encrypt = false) {
if (!validTypes.some((t) => t === type))
throw new Error("Invalid type. It must be one of 'mobile' or 'web'.");
const keys = (await this._db.context.getAllKeys()).filter(
(key) => !(key in invalidKeys)
);
2020-09-13 13:24:24 +05:00
const db = Object.fromEntries(await this._db.context.readMulti(keys));
db.h = sha256.hex(JSON.stringify(db));
2020-09-13 13:24:24 +05:00
if (encrypt) {
const key = await this._db.user.key();
return JSON.stringify({
type,
date: Date.now(),
data: await this._db.context.encrypt(key, JSON.stringify(db)),
});
2020-09-13 13:24:24 +05:00
}
return JSON.stringify({
type,
date: Date.now(),
data: db,
});
2020-09-13 13:24:24 +05:00
}
/**
*
* @param {string} data the backup data
*/
2020-09-13 13:24:24 +05:00
async import(data) {
if (!data) return;
2020-09-13 13:24:24 +05:00
let backup = JSON.parse(data);
if (!this._validate(backup)) throw new Error("Invalid backup.");
let db = backup.data;
2020-09-13 13:24:24 +05:00
//check if we have encrypted data
if (db.salt && db.iv) {
2020-09-13 13:24:24 +05:00
const key = await this._db.user.key();
db = JSON.parse(await this._db.context.decrypt(key, db));
2020-09-13 13:24:24 +05:00
}
if (!this._verify(db))
throw new Error("Backup file has been tempered, aborting...");
for (let key in db) {
let value = db[key];
2020-09-13 13:24:24 +05:00
await this._db.context.write(key, value);
}
}
_validate(backup) {
return (
!!backup.date &&
!!backup.data &&
!!backup.type &&
validTypes.some((t) => t === backup.type)
);
}
_verify(db) {
const hash = db.h;
delete db.h;
return hash == sha256.hex(JSON.stringify(db));
}
2020-09-13 13:24:24 +05:00
}