diff --git a/src/app/components/ContentLinkBlockImage.svelte b/src/app/components/ContentLinkBlockImage.svelte index 57580a7b..efdc2ccb 100644 --- a/src/app/components/ContentLinkBlockImage.svelte +++ b/src/app/components/ContentLinkBlockImage.svelte @@ -59,13 +59,14 @@ onMount(async () => { // If we have an encryption algorithm, fetch and decrypt if (algorithm === "aes-gcm" && key && nonce) { - const response = await fetch(url) - - if (response.ok) { + try { + const response = await fetch(url) const ciphertext = new Uint8Array(await response.arrayBuffer()) const decryptedData = await decryptFile({ciphertext, key, nonce, algorithm}) setBlobSrc(new Uint8Array(decryptedData), mime) + } catch { + hasError = true } } else { src = url diff --git a/src/lib/indexeddb.ts b/src/lib/indexeddb.ts index 60898b6a..55f2e804 100644 --- a/src/lib/indexeddb.ts +++ b/src/lib/indexeddb.ts @@ -16,6 +16,7 @@ export type IDBOptions = { export class IDB { connection: Maybe>> failedToConnect = false + private generation = 0 constructor(readonly options: IDBOptions) {} @@ -70,10 +71,20 @@ export class IDB { return this.connection } + // Deleting an account closes the database out from under writes that already awaited it, and + // the transaction they go on to open throws where nobody is catching. What they were going to + // read or write is gone with the connection, so hand them nothing instead. + private live = async () => { + const generation = this.generation + const connection = await this.connect() + + return generation === this.generation ? connection : undefined + } + table = (name: string) => new IDBTable(this, name) getAll = async (table: string): Promise => { - const connection = await this.connect() + const connection = await this.live() if (!connection) return [] @@ -87,7 +98,7 @@ export class IDB { } bulkPut = async (table: string, data: Iterable) => { - const connection = await this.connect() + const connection = await this.live() if (!connection) return @@ -108,7 +119,7 @@ export class IDB { } bulkDelete = async (table: string, ids: Iterable) => { - const connection = await this.connect() + const connection = await this.live() if (!connection) return @@ -123,6 +134,7 @@ export class IDB { const connection = this.connection this.connection = undefined + this.generation += 1 await connection?.then(c => c?.close()) }