signal-desktop/js/modules/indexeddb.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2020-04-28 21:18:41 +00:00
/* global window, Whisper, setTimeout */
2018-10-18 01:01:21 +00:00
const MESSAGE_MINIMUM_VERSION = 7;
module.exports = {
doesDatabaseExist,
MESSAGE_MINIMUM_VERSION,
removeDatabase,
};
async function doesDatabaseExist() {
window.SignalContext.log.info(
'Checking for the existence of IndexedDB data...'
);
2018-10-18 01:01:21 +00:00
return new Promise((resolve, reject) => {
const { id } = Whisper.Database;
const req = window.indexedDB.open(id);
let existed = true;
setTimeout(() => {
window.SignalContext.log.warn(
'doesDatabaseExist: Timed out attempting to check IndexedDB status'
);
return resolve(false);
}, 1000);
2018-10-18 01:01:21 +00:00
req.onerror = reject;
req.onsuccess = () => {
req.result.close();
resolve(existed);
};
req.onupgradeneeded = () => {
if (req.result.version === 1) {
existed = false;
window.indexedDB.deleteDatabase(id);
}
};
});
}
function removeDatabase() {
window.SignalContext.log.info(
`Deleting IndexedDB database '${Whisper.Database.id}'`
);
2018-10-18 01:01:21 +00:00
window.indexedDB.deleteDatabase(Whisper.Database.id);
}