Skip to content

Commit

Permalink
Fixed #1014, #1089: Skip errors we can not control
Browse files Browse the repository at this point in the history
  • Loading branch information
zner0L committed Nov 6, 2024
1 parent 7bc7dd0 commit b26df09
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"brutusin-json-forms": "https://github.com/brutusin/json-forms",
"deepmerge": "^4.2.2",
"fast-deep-equal": "^3.1.3",
"idb": "^7.0.2",
"idb": "^8.0.0",
"immer": "^9.0.12",
"js-cookie": "^2.2.1",
"letter-generator": "^2.2.1",
Expand Down
40 changes: 29 additions & 11 deletions src/Utility/PrivacyAsyncStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ export type PrivacyAsyncStorageOption = {

type KeyValueDatabase = IDBPDatabase<{ [key: string]: string }>;

const errorFilter = (e: Error) =>
// These migh be caused if IndexedDB is disabled in Firefox
e.name === 'InvalidStateError' ||
e.name === 'SecurityError' ||
// We couldn’t identify the cause for this error, but it seems to be caused by problem in the browser, so there is
// no need to tell the user about it (we should fail gracefully anyway).
// See also: https://github.com/datenanfragen/website/issues/1014
e.message === 'Internal Error';

export class PrivacyAsyncStorage {
#db?: typeof localStorage | KeyValueDatabase;
#options: PrivacyAsyncStorageOption;
Expand Down Expand Up @@ -49,7 +58,7 @@ export class PrivacyAsyncStorage {
},
})
.catch((e: DOMException) => {
if (e.name === 'InvalidStateError') {
if (errorFilter(e)) {
// Database is not writable, we are probably in Firefox' private browsing mode
this.#storageType = 'localStorage';
this.#db = localStorage;
Expand Down Expand Up @@ -144,20 +153,29 @@ export class PrivacyAsyncStorage {
}

static async doesStoreExist(name: string, storeName: string) {
const db: IDBPDatabase | void = await openDB(name, undefined, { blocking: () => db?.close() }).catch((e) => {
if (e.name === 'InvalidStateError' && e.name === 'VersionError') {
db?.close();
try {
const db: IDBPDatabase | void = await openDB(name, undefined, { blocking: () => db?.close() }).catch(
(e) => {
if (errorFilter(e) || e.name === 'VersionError') {
db?.close();
return;
}
rethrow(e, 'Error in doesStoreExist', { name, storeName, db }, t('indexeddb-error', 'error-msg'));
}
);

if (db) {
const result = db.objectStoreNames.contains(storeName);
db.close();
return result;
}
} catch (e) {
if (e instanceof Error && errorFilter(e)) {
return;
}
rethrow(e, 'Error in doesStoreExist', { name, storeName, db }, t('indexeddb-error', 'error-msg'));
});

if (db) {
const result = db.objectStoreNames.contains(storeName);
db.close();
return result;
}
return (
localStorage &&
typeof Object.keys(localStorage).find((key) => new RegExp(`^${name}/${storeName}/`).test(key)) === 'string'
);
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6925,10 +6925,10 @@ iconv-lite@^0.6.3:
dependencies:
safer-buffer ">= 2.1.2 < 3.0.0"

idb@^7.0.2:
version "7.0.2"
resolved "https://registry.yarnpkg.com/idb/-/idb-7.0.2.tgz#7a067e20dd16539938e456814b7d714ba8db3892"
integrity sha512-jjKrT1EnyZewQ/gCBb/eyiYrhGzws2FeY92Yx8qT9S9GeQAmo4JFVIiWRIfKW/6Ob9A+UDAOW9j9jn58fy2HIg==
idb@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/idb/-/idb-8.0.0.tgz#33d7ed894ed36e23bcb542fb701ad579bfaad41f"
integrity sha512-l//qvlAKGmQO31Qn7xdzagVPPaHTxXx199MhrAFuVBTPqydcPYBWjkrbv4Y0ktB+GmWOiwHl237UUOrLmQxLvw==

ieee754@^1.1.13:
version "1.2.1"
Expand Down

0 comments on commit b26df09

Please sign in to comment.