2022-05-27 22:12:01 +00:00
|
|
|
// Copyright 2018-2022 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-05-27 22:12:01 +00:00
|
|
|
const LEGACY_DATABASE_ID = 'signal';
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2022-06-01 16:51:30 +00:00
|
|
|
export async function doesDatabaseExist(): Promise<boolean> {
|
2021-10-07 23:28:47 +00:00
|
|
|
window.SignalContext.log.info(
|
2021-09-17 18:27:53 +00:00
|
|
|
'Checking for the existence of IndexedDB data...'
|
|
|
|
);
|
2018-10-18 01:01:21 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-05-27 22:12:01 +00:00
|
|
|
const req = window.indexedDB.open(LEGACY_DATABASE_ID);
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
let existed = true;
|
|
|
|
|
2022-06-01 16:51:30 +00:00
|
|
|
let timer: undefined | ReturnType<typeof setTimeout> = setTimeout(() => {
|
2021-10-07 23:28:47 +00:00
|
|
|
window.SignalContext.log.warn(
|
2019-06-27 20:21:52 +00:00
|
|
|
'doesDatabaseExist: Timed out attempting to check IndexedDB status'
|
|
|
|
);
|
|
|
|
return resolve(false);
|
2019-08-06 18:42:57 +00:00
|
|
|
}, 1000);
|
2019-06-27 20:21:52 +00:00
|
|
|
|
2021-12-03 23:04:34 +00:00
|
|
|
const clearTimer = () => {
|
|
|
|
if (timer !== undefined) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
req.onerror = error => {
|
|
|
|
clearTimer();
|
|
|
|
reject(error);
|
|
|
|
};
|
2018-10-18 01:01:21 +00:00
|
|
|
req.onsuccess = () => {
|
2021-12-03 23:04:34 +00:00
|
|
|
clearTimer();
|
2018-10-18 01:01:21 +00: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-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-27 16:46:43 +00:00
|
|
|
export function removeDatabase(): Promise<void> {
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
window.SignalContext.log.info(
|
|
|
|
`removeDatabase: Deleting IndexedDB database '${LEGACY_DATABASE_ID}'`
|
|
|
|
);
|
|
|
|
const request = window.indexedDB.deleteDatabase(LEGACY_DATABASE_ID);
|
|
|
|
request.onerror = () => {
|
|
|
|
window.SignalContext.log.error(
|
|
|
|
'removeDatabase: Error deleting database.'
|
|
|
|
);
|
|
|
|
reject(new Error('Error deleting database'));
|
|
|
|
};
|
|
|
|
request.onsuccess = () => {
|
|
|
|
window.SignalContext.log.info(
|
|
|
|
'removeDatabase: Database deleted successfully'
|
|
|
|
);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
});
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|