signal-desktop/ts/indexeddb.ts

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2018 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> {
window.SignalContext.log.info(
'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(() => {
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-18 01:01:21 +00:00
req.onsuccess = () => {
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
}
};
});
}
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
}