verify the integrity of the data
This commit is contained in:
parent
7e33b7da85
commit
b4133bb4f8
1 changed files with 45 additions and 14 deletions
|
@ -4,6 +4,7 @@ Manage the local cache.
|
||||||
|
|
||||||
import logging from "/scripts/logging.js";
|
import logging from "/scripts/logging.js";
|
||||||
import texts from "/scripts/strings/read.js";
|
import texts from "/scripts/strings/read.js";
|
||||||
|
import hash from "/scripts/strings/hash.js";
|
||||||
|
|
||||||
/* Read all stored data in the browser cache.
|
/* Read all stored data in the browser cache.
|
||||||
|
|
||||||
|
@ -109,6 +110,7 @@ export async function read(DATA_NAME, CLOUD = 0) {
|
||||||
CLOUD = (CLOUD > 0) ? 1 : -1;
|
CLOUD = (CLOUD > 0) ? 1 : -1;
|
||||||
DATA = await read_database(CLOUD);
|
DATA = await read_database(CLOUD);
|
||||||
DATA_RETURNED = (DATA_NAME) ? find_data(DATA, DATA_NAME) : DATA;
|
DATA_RETURNED = (DATA_NAME) ? find_data(DATA, DATA_NAME) : DATA;
|
||||||
|
|
||||||
return(DATA_RETURNED);
|
return(DATA_RETURNED);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -216,14 +218,9 @@ export async function write(PATH, DATA, CLOUD = -1) {
|
||||||
@param {object} DATA the data
|
@param {object} DATA the data
|
||||||
@param {number} CLOUD the storage
|
@param {number} CLOUD the storage
|
||||||
*/
|
*/
|
||||||
function write_database(DATA, CLOUD = 0) {
|
const write_database = async (DATA, CLOUD = 0) => {
|
||||||
// If CLOUD is set to 0, it should automatically determine where the previous source of data was taken from.
|
// If CLOUD is set to 0, it should automatically determine where the previous source of data was taken from.
|
||||||
|
return((CLOUD > 0) ? chrome.storage.sync.set(DATA) : chrome.storage.local.set(DATA));
|
||||||
if (CLOUD > 0) {
|
|
||||||
chrome.storage.sync.set(DATA);
|
|
||||||
} else if (CLOUD < 0) {
|
|
||||||
chrome.storage.local.set(DATA);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Appropriately nest and merge the data.
|
/* Appropriately nest and merge the data.
|
||||||
|
@ -245,11 +242,7 @@ export async function write(PATH, DATA, CLOUD = -1) {
|
||||||
if (DATABASE[PATH[`current`]] == null) {
|
if (DATABASE[PATH[`current`]] == null) {
|
||||||
DATABASE[PATH[`current`]] = {};
|
DATABASE[PATH[`current`]] = {};
|
||||||
}
|
}
|
||||||
DATABASE[PATH[`current`]] = nest(
|
DATABASE[PATH[`current`]] = nest(DATABASE[PATH[`current`]], PATH[`target`], VALUE);
|
||||||
DATABASE[PATH[`current`]],
|
|
||||||
PATH[`target`],
|
|
||||||
VALUE,
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
DATABASE[PATH[`current`]] = VALUE;
|
DATABASE[PATH[`current`]] = VALUE;
|
||||||
}
|
}
|
||||||
|
@ -257,6 +250,19 @@ export async function write(PATH, DATA, CLOUD = -1) {
|
||||||
return DATABASE;
|
return DATABASE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const verify = async () => {
|
||||||
|
let DATA_CHECK = {};
|
||||||
|
|
||||||
|
// Verify the presence of the data.
|
||||||
|
DATA_CHECK[`state`] = await compare([...PATH], DATA);
|
||||||
|
|
||||||
|
if (!DATA_CHECK[`state`]) {logging.error((new texts(`error_msg_save_failed`)).localized, String(PATH), JSON.stringify(DATA))} else {
|
||||||
|
// Inform the user that the saving operation is completed.
|
||||||
|
notification = new logging (new texts(`saving_done`).localized);
|
||||||
|
};
|
||||||
|
return (DATA_CHECK[`state`]);
|
||||||
|
}
|
||||||
|
|
||||||
let DATA_ALL = await read(null, CLOUD);
|
let DATA_ALL = await read(null, CLOUD);
|
||||||
if ((DATA_ALL != null && (typeof DATA_ALL).includes(`obj`)) ? Object.keys(DATA_ALL).length <= 0 : true) {
|
if ((DATA_ALL != null && (typeof DATA_ALL).includes(`obj`)) ? Object.keys(DATA_ALL).length <= 0 : true) {
|
||||||
DATA_ALL = {};
|
DATA_ALL = {};
|
||||||
|
@ -276,8 +282,33 @@ export async function write(PATH, DATA, CLOUD = -1) {
|
||||||
// Write!
|
// Write!
|
||||||
write_database(DATA_INJECTED, CLOUD);
|
write_database(DATA_INJECTED, CLOUD);
|
||||||
|
|
||||||
// Inform the user that the saving operation is completed.
|
return (verify());
|
||||||
notification = new logging ((new texts(`saving_done`)).localized);
|
}
|
||||||
|
|
||||||
|
/* Compare a data against the stored data. Useful when comparing dictionaries.
|
||||||
|
|
||||||
|
@param {string} PATH the name
|
||||||
|
@param {object} DATA the data to compare to
|
||||||
|
*/
|
||||||
|
export async function compare(PATH, DATA) {
|
||||||
|
/* The actual comparison of data. */
|
||||||
|
async function comparison(DATA_ONE, DATA_TWO) {
|
||||||
|
let RESULT = true;
|
||||||
|
|
||||||
|
// The first round of checking is on the data type.
|
||||||
|
RESULT = (typeof DATA_ONE == typeof DATA_TWO) ? ((Array.isArray(DATA_TWO) == Array.isArray(DATA_ONE)) && !((DATA_ONE == null && DATA_TWO != null) || (DATA_ONE != null && DATA_TWO == null))) : false;
|
||||||
|
RESULT = await hash.digest(DATA_ONE, {"output": "Number"}) == await hash.digest(DATA_TWO, {"output": "Number"});
|
||||||
|
|
||||||
|
return (RESULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
let COMPARISON = {};
|
||||||
|
COMPARISON[`test`] = (PATH) ? DATA : DATA[1];
|
||||||
|
COMPARISON[`against`] = (PATH) ? (await read(PATH)) : DATA[0];
|
||||||
|
COMPARISON[`result`] = comparison(COMPARISON[`against`], COMPARISON[`test`]);
|
||||||
|
|
||||||
|
// Return the result.
|
||||||
|
return (COMPARISON[`result`]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Dangerous: Resets all data or a domain's data.
|
/* Dangerous: Resets all data or a domain's data.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue