aboutsummaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/bridge-idb.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2021-02-16 13:46:51 +0100
committerFlorian Dold <florian@dold.me>2021-02-16 13:47:01 +0100
commit987f22de02648485ec6f1d3c1558abcfa6d624a0 (patch)
tree5883ed1ae4a2debaff8795acadadf8d4b0e2e5be /packages/idb-bridge/src/bridge-idb.ts
parentdb59275b6b43f8fa7f36899ae81cb7139a2e80cb (diff)
downloadwallet-core-987f22de02648485ec6f1d3c1558abcfa6d624a0.tar.xz
next batch of test cases and fixes
Diffstat (limited to 'packages/idb-bridge/src/bridge-idb.ts')
-rw-r--r--packages/idb-bridge/src/bridge-idb.ts75
1 files changed, 70 insertions, 5 deletions
diff --git a/packages/idb-bridge/src/bridge-idb.ts b/packages/idb-bridge/src/bridge-idb.ts
index f519b9c24..f518b4768 100644
--- a/packages/idb-bridge/src/bridge-idb.ts
+++ b/packages/idb-bridge/src/bridge-idb.ts
@@ -58,6 +58,7 @@ import {
import { fakeDOMStringList } from "./util/fakeDOMStringList";
import FakeEvent from "./util/FakeEvent";
import FakeEventTarget from "./util/FakeEventTarget";
+import { makeStoreKeyValue } from "./util/makeStoreKeyValue";
import { normalizeKeyPath } from "./util/normalizeKeyPath";
import { openPromise } from "./util/openPromise";
import queueTask from "./util/queueTask";
@@ -605,7 +606,17 @@ export class BridgeIDBDatabase extends FakeEventTarget implements IDBDatabase {
throw new TypeError();
}
const transaction = confirmActiveVersionchangeTransaction(this);
- transaction._objectStoresCache.delete(name);
+ const backendTx = transaction._backendTransaction;
+ if (!backendTx) {
+ throw Error("invariant violated");
+ }
+ this._backend.deleteObjectStore(backendTx, name);
+ const os = transaction._objectStoresCache.get(name);
+ if (os) {
+ os._deleted = true;
+ transaction._objectStoresCache.delete(name);
+ }
+
}
public _internalTransaction(
@@ -866,7 +877,9 @@ export class BridgeIDBFactory {
event2.eventPath = [request];
request.dispatchEvent(event2);
} else {
- console.log(`dispatching success event, _active=${transaction._active}`);
+ console.log(
+ `dispatching success event, _active=${transaction._active}`,
+ );
const event2 = new FakeEvent("success", {
bubbles: false,
cancelable: false,
@@ -1064,8 +1077,23 @@ export class BridgeIDBIndex implements IDBIndex {
});
}
- public get(key: BridgeIDBKeyRange | IDBValidKey) {
+
+ private _confirmIndexExists() {
+ const storeSchema = this._schema.objectStores[this._objectStore._name];
+ if (!storeSchema) {
+ throw new InvalidStateError();
+ }
+ if (!storeSchema.indexes[this._name]) {
+ throw new InvalidStateError();
+ }
+ }
+
+ get(key: BridgeIDBKeyRange | IDBValidKey) {
confirmActiveTransaction(this);
+ this._confirmIndexExists();
+ if (this._deleted) {
+ throw new InvalidStateError();
+ }
if (!(key instanceof BridgeIDBKeyRange)) {
key = BridgeIDBKeyRange._valueToKeyRange(key);
@@ -1384,6 +1412,7 @@ export class BridgeIDBObjectStore implements IDBObjectStore {
);
}
+
public _store(value: any, key: IDBValidKey | undefined, overwrite: boolean) {
if (BridgeIDBFactory.enableTracing) {
console.log(`TRACE: IDBObjectStore._store`);
@@ -1391,6 +1420,22 @@ export class BridgeIDBObjectStore implements IDBObjectStore {
if (this._transaction.mode === "readonly") {
throw new ReadOnlyError();
}
+
+ const { keyPath, autoIncrement } = this._schema.objectStores[this._name];
+
+ if (key !== null && key !== undefined) {
+ valueToKey(key);
+ }
+
+ // We only call this to synchronously verify the request.
+ makeStoreKeyValue(
+ value,
+ key,
+ 1,
+ autoIncrement,
+ keyPath,
+ );
+
const operation = async () => {
const { btx } = this._confirmActiveTransaction();
const result = await this._backend.storeRecord(btx, {
@@ -1411,6 +1456,9 @@ export class BridgeIDBObjectStore implements IDBObjectStore {
if (arguments.length === 0) {
throw new TypeError();
}
+ if (this._deleted) {
+ throw new InvalidStateError("tried to call 'put' on a deleted object store");
+ }
return this._store(value, key, true);
}
@@ -1418,6 +1466,9 @@ export class BridgeIDBObjectStore implements IDBObjectStore {
if (arguments.length === 0) {
throw new TypeError();
}
+ if (!this._schema.objectStores[this._name]) {
+ throw new InvalidStateError("object store does not exist");
+ }
return this._store(value, key, false);
}
@@ -1425,7 +1476,9 @@ export class BridgeIDBObjectStore implements IDBObjectStore {
if (arguments.length === 0) {
throw new TypeError();
}
-
+ if (this._deleted) {
+ throw new InvalidStateError("tried to call 'delete' on a deleted object store");
+ }
if (this._transaction.mode === "readonly") {
throw new ReadOnlyError();
}
@@ -1458,6 +1511,10 @@ export class BridgeIDBObjectStore implements IDBObjectStore {
throw new TypeError();
}
+ if (this._deleted) {
+ throw new InvalidStateError("tried to call 'delete' on a deleted object store");
+ }
+
let keyRange: BridgeIDBKeyRange;
if (key instanceof BridgeIDBKeyRange) {
@@ -1541,6 +1598,9 @@ export class BridgeIDBObjectStore implements IDBObjectStore {
range?: IDBKeyRange | IDBValidKey,
direction: IDBCursorDirection = "next",
) {
+ if (this._deleted) {
+ throw new InvalidStateError("tried to call 'openCursor' on a deleted object store");
+ }
if (range === null) {
range = undefined;
}
@@ -1572,6 +1632,9 @@ export class BridgeIDBObjectStore implements IDBObjectStore {
range?: BridgeIDBKeyRange | IDBValidKey,
direction?: IDBCursorDirection,
) {
+ if (this._deleted) {
+ throw new InvalidStateError("tried to call 'openKeyCursor' on a deleted object store");
+ }
if (range === null) {
range = undefined;
}
@@ -2091,7 +2154,9 @@ export class BridgeIDBTransaction
request.dispatchEvent(event);
} catch (err) {
if (BridgeIDBFactory.enableTracing) {
- console.log("TRACING: caught error in transaction success event handler");
+ console.log(
+ "TRACING: caught error in transaction success event handler",
+ );
}
this._abort("AbortError");
this._active = false;