Skip to content

Commit

Permalink
(#8229) - indexeddb: ensure errors are propagated and handle database…
Browse files Browse the repository at this point in the history
… deletion

* indexeddb: propagate errors on setup and transactions

* indexeddb: don't reopen database with version=1 if it got deleted
  • Loading branch information
andrevmatos committed Nov 22, 2020
1 parent 7cc1c8e commit b8d3fc6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
18 changes: 8 additions & 10 deletions packages/node_modules/pouchdb-adapter-indexeddb/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function IdbPouch(dbOpts, callback) {
args.unshift(res.idb);
fun.apply(api, args);
}).catch(function (err) {
var last = args.unshift();
var last = args.pop();
if (typeof last === 'function') {
last(err);
} else {
Expand All @@ -53,14 +53,11 @@ function IdbPouch(dbOpts, callback) {
return function () {
var args = Array.prototype.slice.call(arguments);

return new Promise(function (resolve, reject) {
setup(openDatabases, api, dbOpts).then(function (res) {
metadata = res.metadata;
args.unshift(res.idb);
return setup(openDatabases, api, dbOpts).then(function (res) {
metadata = res.metadata;
args.unshift(res.idb);

return fun.apply(api, args);
}).then(resolve)
.catch(reject);
return fun.apply(api, args);
});
};
};
Expand All @@ -78,12 +75,13 @@ function IdbPouch(dbOpts, callback) {
setup(openDatabases, api, dbOpts).then(function (res) {
metadata = res.metadata;
txn.txn = res.idb.transaction(stores, mode);
args.unshift(txn);
fun.apply(api, args);
}).catch(function (err) {
console.error('Failed to establish transaction safely');
console.error(err);
txn.error = err;
}).then(function () {
args.unshift(txn);
fun.apply(api, args);
});
};
};
Expand Down
12 changes: 12 additions & 0 deletions packages/node_modules/pouchdb-adapter-indexeddb/src/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ function openDatabase(openDatabases, api, opts, resolve, reject) {
// migrate between the two. In the future, dependent on performance tests,
// we might silently migrate
throw new Error('Incorrect adapter: you should specify the "idb" adapter to open this DB');
} else if (e.oldVersion === 0 && e.newVersion < versionMultiplier) {
// Firefox still creates the database with version=1 even if we throw,
// so we need to be sure to destroy the empty database before throwing
indexedDB.deleteDatabase(opts.name);
throw new Error('Database was deleted while open');
}

var db = e.target.result;
Expand Down Expand Up @@ -148,6 +153,13 @@ function openDatabase(openDatabases, api, opts, resolve, reject) {
idb.close();
};

idb.onclose = function () {
console.log('Database was made stale, closing handle');
if (opts.name in openDatabases) {
openDatabases[opts.name].versionchanged = true;
}
};

var metadata = {id: META_STORE};
var txn = idb.transaction([META_STORE], 'readwrite');

Expand Down

0 comments on commit b8d3fc6

Please sign in to comment.