44 lines
1.7 KiB
JavaScript
44 lines
1.7 KiB
JavaScript
(function (root, factory) {
|
|
if (typeof module === 'object' && module.exports) module.exports = factory;
|
|
else root.inspectStackchainPrivateDatabases = factory(root.indexedDB);
|
|
})(typeof globalThis !== 'undefined' ? globalThis : this, function createPrivateDataInspector(indexedDB) {
|
|
function requestResult(request) {
|
|
return new Promise((resolve, reject) => {
|
|
request.onsuccess = () => resolve(request.result);
|
|
request.onerror = () => reject(request.error || new Error('Private storage inventory failed.'));
|
|
request.onblocked = () => reject(new Error('Private storage inventory was blocked.'));
|
|
});
|
|
}
|
|
|
|
async function countDatabase(name) {
|
|
const database = await requestResult(indexedDB.open(name));
|
|
try {
|
|
const storeNames = Array.from(database.objectStoreNames);
|
|
if (!storeNames.length) return 0;
|
|
const transaction = database.transaction(storeNames, 'readonly');
|
|
const counts = await Promise.all(storeNames.map(storeName =>
|
|
requestResult(transaction.objectStore(storeName).count())
|
|
));
|
|
return counts.reduce((total, count) => total + Number(count || 0), 0);
|
|
} finally {
|
|
database.close();
|
|
}
|
|
}
|
|
|
|
return async function inspectPrivateDatabases(registeredNames) {
|
|
if (!indexedDB?.databases) return { recordCount: 0, unavailable: true };
|
|
try {
|
|
const existing = new Set((await indexedDB.databases()).map(database => database.name));
|
|
const counts = await Promise.all(
|
|
registeredNames.filter(name => existing.has(name)).map(countDatabase)
|
|
);
|
|
return {
|
|
recordCount: counts.reduce((total, count) => total + count, 0),
|
|
unavailable: false,
|
|
};
|
|
} catch (_error) {
|
|
return { recordCount: 0, unavailable: true };
|
|
}
|
|
};
|
|
});
|