mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-11-02 02:42:28 +00:00
Merge pull request #1394 from PR0M3TH3AN/fix-dm-tests-and-leaks-8133857459641031321
Fix DM storage leaks and test environment issues
This commit is contained in:
5 files changed
+30
-17
No files matched your search
+14
-5
@@ -64,11 +64,14 @@ const MIGRATIONS = [
|
||||
return;
|
||||
}
|
||||
|
||||
// If transaction is provided (during upgrade), use it.
|
||||
// Otherwise create a new one (but this path is questionable during upgrade).
|
||||
const store = transaction
|
||||
? transaction.objectStore(STORES.CONVERSATIONS)
|
||||
: db.transaction(STORES.CONVERSATIONS, "readwrite").objectStore(
|
||||
STORES.CONVERSATIONS,
|
||||
);
|
||||
: db.createObjectStore(STORES.CONVERSATIONS, { keyPath: "conversation_id" });
|
||||
// Fallback: If store doesn't exist and no transaction provided (unlikely in upgrade), create it?
|
||||
// Actually, if transaction is missing in migration, we are in trouble.
|
||||
// But for safe-guarding against "store not found":
|
||||
if (!store.indexNames.contains("opened_until")) {
|
||||
store.createIndex("opened_until", "opened_until", { unique: false });
|
||||
}
|
||||
@@ -261,6 +264,7 @@ export async function writeConversationMetadata(conversation) {
|
||||
const tx = db.transaction(STORES.CONVERSATIONS, "readwrite");
|
||||
tx.objectStore(STORES.CONVERSATIONS).put(normalized);
|
||||
await finalizeTransaction(tx);
|
||||
db.close();
|
||||
return normalized;
|
||||
} catch (error) {
|
||||
userLogger.warn("[dmDb] Failed to write conversation metadata", error);
|
||||
@@ -292,6 +296,7 @@ export async function writeMessages(messages) {
|
||||
}
|
||||
|
||||
await finalizeTransaction(tx);
|
||||
db.close();
|
||||
return normalized;
|
||||
} catch (error) {
|
||||
userLogger.warn("[dmDb] Failed to persist messages", error);
|
||||
@@ -314,6 +319,7 @@ export async function getConversation(conversationId) {
|
||||
const store = tx.objectStore(STORES.CONVERSATIONS);
|
||||
const result = await runRequest(store.get(conversationId.trim()));
|
||||
await finalizeTransaction(tx);
|
||||
db.close();
|
||||
return result || null;
|
||||
} catch (error) {
|
||||
userLogger.warn("[dmDb] Failed to read conversation metadata", error);
|
||||
@@ -363,7 +369,7 @@ export async function listMessagesByConversation(conversationId, options = {}) {
|
||||
userLogger.warn("[dmDb] Failed to list messages", request.error);
|
||||
resolve(results);
|
||||
};
|
||||
});
|
||||
}).finally(() => db.close());
|
||||
} catch (error) {
|
||||
userLogger.warn("[dmDb] Failed to list messages", error);
|
||||
return [];
|
||||
@@ -423,6 +429,7 @@ export async function updateConversationFromMessage(message, options = {}) {
|
||||
|
||||
store.put(next);
|
||||
await finalizeTransaction(tx);
|
||||
db.close();
|
||||
return next;
|
||||
} catch (error) {
|
||||
userLogger.warn("[dmDb] Failed to update conversation metadata", error);
|
||||
@@ -471,6 +478,7 @@ export async function updateConversationOpenState(
|
||||
|
||||
store.put(next);
|
||||
await finalizeTransaction(tx);
|
||||
db.close();
|
||||
return next;
|
||||
} catch (error) {
|
||||
userLogger.warn("[dmDb] Failed to update conversation open state", error);
|
||||
@@ -504,7 +512,7 @@ export async function listConversations() {
|
||||
userLogger.warn("[dmDb] Failed to list conversations", request.error);
|
||||
resolve(results);
|
||||
};
|
||||
});
|
||||
}).finally(() => db.close());
|
||||
} catch (error) {
|
||||
userLogger.warn("[dmDb] Failed to list conversations", error);
|
||||
return [];
|
||||
@@ -543,6 +551,7 @@ export async function clearDmDb() {
|
||||
}
|
||||
|
||||
await finalizeTransaction(tx);
|
||||
db.close();
|
||||
return true;
|
||||
} catch (error) {
|
||||
devLogger.warn("[dmDb] Failed to clear DM database", error);
|
||||
|
||||
@@ -10,10 +10,10 @@ import {
|
||||
|
||||
const { indexedDB, IDBKeyRange } = await import("fake-indexeddb");
|
||||
|
||||
global.indexedDB = indexedDB;
|
||||
globalThis.indexedDB = indexedDB;
|
||||
global.IDBKeyRange = IDBKeyRange;
|
||||
globalThis.IDBKeyRange = IDBKeyRange;
|
||||
if (!globalThis.indexedDB) {
|
||||
globalThis.indexedDB = indexedDB;
|
||||
globalThis.IDBKeyRange = IDBKeyRange;
|
||||
}
|
||||
|
||||
async function deleteDatabase(name) {
|
||||
await new Promise((resolve) => {
|
||||
|
||||
@@ -5,10 +5,10 @@ import { NostrService } from "../js/services/nostrService.js";
|
||||
|
||||
const { indexedDB, IDBKeyRange } = await import("fake-indexeddb");
|
||||
|
||||
global.indexedDB = indexedDB;
|
||||
globalThis.indexedDB = indexedDB;
|
||||
global.IDBKeyRange = IDBKeyRange;
|
||||
globalThis.IDBKeyRange = IDBKeyRange;
|
||||
if (!globalThis.indexedDB) {
|
||||
globalThis.indexedDB = indexedDB;
|
||||
globalThis.IDBKeyRange = IDBKeyRange;
|
||||
}
|
||||
|
||||
async function deleteDatabase(name) {
|
||||
await new Promise((resolve) => {
|
||||
|
||||
@@ -7,10 +7,10 @@ import { listMessagesByConversation } from "../../js/storage/dmDb.js";
|
||||
|
||||
const { indexedDB, IDBKeyRange } = await import("fake-indexeddb");
|
||||
|
||||
global.indexedDB = indexedDB;
|
||||
globalThis.indexedDB = indexedDB;
|
||||
global.IDBKeyRange = IDBKeyRange;
|
||||
globalThis.IDBKeyRange = IDBKeyRange;
|
||||
if (!globalThis.indexedDB) {
|
||||
globalThis.indexedDB = indexedDB;
|
||||
globalThis.IDBKeyRange = IDBKeyRange;
|
||||
}
|
||||
|
||||
async function deleteDatabase(name) {
|
||||
await new Promise((resolve) => {
|
||||
|
||||
@@ -43,3 +43,7 @@ if (typeof globalThis.localStorage === "undefined") {
|
||||
if (typeof globalThis.window.localStorage === "undefined") {
|
||||
globalThis.window.localStorage = globalThis.localStorage;
|
||||
}
|
||||
|
||||
if (typeof globalThis.structuredClone === "undefined") {
|
||||
globalThis.structuredClone = (val) => JSON.parse(JSON.stringify(val));
|
||||
}
|
||||
Reference in new issue
Block a user