signal-desktop/js/modules/indexeddb.js

62 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-05-27 22:12:01 +00:00
// Copyright 2018-2022 Signal Messenger, LLC
2020-10-30 15:34:04 -05:00
// SPDX-License-Identifier: AGPL-3.0-only
2022-05-27 22:12:01 +00:00
/* global window, clearTimeout, setTimeout */
2018-10-17 18:01:21 -07:00
2022-05-27 22:12:01 +00:00
const LEGACY_DATABASE_ID = 'signal';
2018-10-17 18:01:21 -07: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-17 18:01:21 -07:00
return new Promise((resolve, reject) => {
2022-05-27 22:12:01 +00:00
const req = window.indexedDB.open(LEGACY_DATABASE_ID);
2018-10-17 18:01:21 -07:00
let existed = true;
let timer = setTimeout(() => {
window.SignalContext.log.warn(
'doesDatabaseExist: Timed out attempting to check IndexedDB status'
);
return resolve(false);
}, 1000);
const clearTimer = () => {
if (timer !== undefined) {
clearTimeout(timer);
timer = undefined;
}
};
req.onerror = error => {
clearTimer();
reject(error);
};
2018-10-17 18:01:21 -07:00
req.onsuccess = () => {
clearTimer();
2018-10-17 18:01:21 -07:00
req.result.close();
resolve(existed);
};
req.onupgradeneeded = () => {
if (req.result.version === 1) {
existed = false;
2022-05-27 22:12:01 +00:00
window.indexedDB.deleteDatabase(LEGACY_DATABASE_ID);
2018-10-17 18:01:21 -07:00
}
};
});
}
function removeDatabase() {
window.SignalContext.log.info(
2022-05-27 22:12:01 +00:00
`Deleting IndexedDB database '${LEGACY_DATABASE_ID}'`
);
2022-05-27 22:12:01 +00:00
window.indexedDB.deleteDatabase(LEGACY_DATABASE_ID);
2018-10-17 18:01:21 -07:00
}