fake-indexeddb 4.0.0-beta.2 → 4.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -10
- package/auto.cjs +35 -0
- package/auto.js +12 -12
- package/build/cjs/FDBCursor.js +591 -0
- package/build/cjs/FDBCursorWithValue.js +27 -0
- package/build/cjs/FDBDatabase.js +226 -0
- package/build/cjs/FDBFactory.js +310 -0
- package/build/cjs/FDBIndex.js +252 -0
- package/build/cjs/FDBKeyRange.js +103 -0
- package/build/cjs/FDBObjectStore.js +508 -0
- package/build/cjs/FDBOpenDBRequest.js +24 -0
- package/build/cjs/FDBRequest.js +55 -0
- package/build/cjs/FDBTransaction.js +260 -0
- package/build/cjs/FDBVersionChangeEvent.js +27 -0
- package/build/cjs/fakeIndexedDB.js +15 -0
- package/build/cjs/index.js +107 -0
- package/build/cjs/lib/Database.js +48 -0
- package/build/cjs/lib/FakeDOMStringList.js +53 -0
- package/build/cjs/lib/FakeEvent.js +52 -0
- package/build/cjs/lib/FakeEventTarget.js +128 -0
- package/build/cjs/lib/Index.js +187 -0
- package/build/cjs/lib/KeyGenerator.js +38 -0
- package/build/cjs/lib/ObjectStore.js +210 -0
- package/build/cjs/lib/RecordStore.js +180 -0
- package/build/cjs/lib/binarySearch.js +104 -0
- package/build/cjs/lib/canInjectKey.js +42 -0
- package/build/cjs/lib/cmp.js +111 -0
- package/build/cjs/lib/enforceRange.js +24 -0
- package/build/cjs/lib/errors.js +130 -0
- package/build/cjs/lib/extractKey.js +61 -0
- package/build/cjs/lib/scheduling.js +35 -0
- package/build/cjs/lib/structuredClone.js +32 -0
- package/build/cjs/lib/types.js +1 -0
- package/build/cjs/lib/validateKeyPath.js +65 -0
- package/build/cjs/lib/valueToKey.js +64 -0
- package/build/cjs/lib/valueToKeyRange.js +36 -0
- package/build/cjs/package.json +1 -0
- package/build/esm/FDBCursor.js +574 -0
- package/build/esm/FDBCursorWithValue.js +16 -0
- package/build/esm/FDBDatabase.js +209 -0
- package/build/esm/FDBFactory.js +291 -0
- package/build/esm/FDBIndex.js +233 -0
- package/build/esm/FDBKeyRange.js +89 -0
- package/build/esm/FDBObjectStore.js +483 -0
- package/build/esm/FDBOpenDBRequest.js +13 -0
- package/build/esm/FDBRequest.js +43 -0
- package/build/esm/FDBTransaction.js +243 -0
- package/build/esm/FDBVersionChangeEvent.js +16 -0
- package/build/{fakeIndexedDB.js → esm/fakeIndexedDB.js} +1 -1
- package/build/{index.js → esm/index.js} +1 -1
- package/build/esm/lib/Database.js +38 -0
- package/build/esm/lib/FakeDOMStringList.js +44 -0
- package/build/esm/lib/FakeEvent.js +43 -0
- package/build/esm/lib/FakeEventTarget.js +119 -0
- package/build/esm/lib/Index.js +171 -0
- package/build/esm/lib/KeyGenerator.js +28 -0
- package/build/esm/lib/ObjectStore.js +195 -0
- package/build/esm/lib/RecordStore.js +167 -0
- package/build/esm/lib/binarySearch.js +87 -0
- package/build/esm/lib/canInjectKey.js +33 -0
- package/build/esm/lib/cmp.js +99 -0
- package/build/esm/lib/enforceRange.js +15 -0
- package/build/esm/lib/errors.js +93 -0
- package/build/esm/lib/extractKey.js +49 -0
- package/build/{lib → esm/lib}/scheduling.js +10 -12
- package/build/esm/lib/structuredClone.js +20 -0
- package/build/esm/lib/types.js +1 -0
- package/build/esm/lib/validateKeyPath.js +56 -0
- package/build/esm/lib/valueToKey.js +55 -0
- package/build/esm/lib/valueToKeyRange.js +22 -0
- package/package.json +89 -36
- package/build/FDBCursor.js +0 -486
- package/build/FDBCursorWithValue.js +0 -11
- package/build/FDBDatabase.js +0 -160
- package/build/FDBFactory.js +0 -256
- package/build/FDBIndex.js +0 -185
- package/build/FDBKeyRange.js +0 -69
- package/build/FDBObjectStore.js +0 -381
- package/build/FDBOpenDBRequest.js +0 -12
- package/build/FDBRequest.js +0 -36
- package/build/FDBTransaction.js +0 -204
- package/build/FDBVersionChangeEvent.js +0 -14
- package/build/lib/Database.js +0 -32
- package/build/lib/FakeDOMStringList.js +0 -66
- package/build/lib/FakeEvent.js +0 -41
- package/build/lib/FakeEventTarget.js +0 -103
- package/build/lib/Index.js +0 -152
- package/build/lib/KeyGenerator.js +0 -23
- package/build/lib/ObjectStore.js +0 -157
- package/build/lib/RecordStore.js +0 -152
- package/build/lib/binarySearch.js +0 -75
- package/build/lib/canInjectKey.js +0 -24
- package/build/lib/cmp.js +0 -78
- package/build/lib/enforceRange.js +0 -12
- package/build/lib/errors.js +0 -83
- package/build/lib/extractKey.js +0 -42
- package/build/lib/structuredClone.js +0 -18
- package/build/lib/types.js +0 -1
- package/build/lib/validateKeyPath.js +0 -51
- package/build/lib/valueToKey.js +0 -53
- package/build/lib/valueToKeyRange.js +0 -18
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ yarn add --dev fake-indexeddb
|
|
|
18
18
|
|
|
19
19
|
Functionally, it works exactly like IndexedDB except data is not persisted to disk.
|
|
20
20
|
|
|
21
|
-
The easiest way to use it is to import `fake-indexeddb/auto`, which will put all the IndexedDB variables in the global scope
|
|
21
|
+
The easiest way to use it is to import `fake-indexeddb/auto`, which will put all the IndexedDB variables in the global scope. (Both `import` and `require` are supported, use whichever you like, but the examples here are all `import`.)
|
|
22
22
|
|
|
23
23
|
```js
|
|
24
24
|
import "fake-indexeddb/auto";
|
|
@@ -83,10 +83,6 @@ import {
|
|
|
83
83
|
} from "fake-indexeddb";
|
|
84
84
|
```
|
|
85
85
|
|
|
86
|
-
### ECMAScript modules (`import`) vs CommonJS (`require`)
|
|
87
|
-
|
|
88
|
-
Starting with version 4, fake-indexeddb uses ES modules rather than CommonJS. That means you need to `import` it, not `require` it. If that's a problem for you, stick to version 3 of fake-indexeddb, which supports `require("fake-indexeddb")`. Version 3 has basically all the same functionality as version 4, and I'll try to make any important bug fixes in both versions.
|
|
89
|
-
|
|
90
86
|
### TypeScript
|
|
91
87
|
|
|
92
88
|
As of version 4, fake-indexeddb includes TypeScript types. As you can see in types.d.ts, it's just using TypeScript's built-in IndexedDB types, rather than generating types from the fake-indexeddb code base. The reason I did this is for compatibility with your application code that may already be using TypeScript's IndexedDB types, so if I used something different for fake-indexeddb, it could lead to spurious type errors. In theory this could lead to other errors if there are differences between Typescript's IndexedDB types and fake-indexeddb's API, but currently I'm not aware of any difference. See [issue #23](https://github.com/dumbmatter/fakeIndexedDB/issues/23) for more discussion.
|
|
@@ -115,22 +111,22 @@ const db = new Dexie("MyDatabase", { indexedDB: indexedDB, IDBKeyRange: IDBKeyRa
|
|
|
115
111
|
|
|
116
112
|
### Jest
|
|
117
113
|
|
|
118
|
-
Out of the box, Jest v27 only supports CommonJS, not ES modules, so you'll have to do a little extra work to opt in to ES modules, either [for all your test files](https://jestjs.io/docs/ecmascript-modules) or by configuring Jest to [transpile fake-indexeddb](https://stackoverflow.com/a/43197503/786644). If you can't do that, you should use fake-indexeddb v3, see the above "ECMAScript modules" section for more info.
|
|
119
|
-
|
|
120
114
|
To use fake-indexeddb in a single Jest test suite, require `fake-indexeddb/auto` at the beginning of the test
|
|
121
115
|
file, as described above.
|
|
122
116
|
|
|
123
|
-
To use it on all Jest tests without having to
|
|
117
|
+
To use it on all Jest tests without having to include it in each file, add the auto setup script to the `setupFiles` in your Jest config:
|
|
124
118
|
|
|
125
119
|
```json
|
|
126
|
-
|
|
120
|
+
{
|
|
127
121
|
...
|
|
128
122
|
"setupFiles": [
|
|
129
|
-
"fake-indexeddb/auto"
|
|
123
|
+
"fake-indexeddb/auto.cjs"
|
|
130
124
|
]
|
|
131
125
|
}
|
|
132
126
|
```
|
|
133
127
|
|
|
128
|
+
That's "auto.cjs" rather than just "auto" to make sure it picks up the CommonJS version. Otherwise it would use the ESM version, which Jest does not support without [some extra configuration](https://jestjs.io/docs/ecmascript-modules).
|
|
129
|
+
|
|
134
130
|
### Wiping/resetting the indexedDB for a fresh state
|
|
135
131
|
|
|
136
132
|
If you are keeping your tests completely isolated you might want to "reset" the state of the mocked indexedDB. You can do this by creating a new instance of `IDBFactory`, which lets you have a totally fresh start.
|
package/auto.cjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const fakeIndexedDB = require("./build/cjs/fakeIndexedDB.js");
|
|
2
|
+
const FDBCursor = require("./build/cjs/FDBCursor.js");
|
|
3
|
+
const FDBCursorWithValue = require("./build/cjs/FDBCursorWithValue.js");
|
|
4
|
+
const FDBDatabase = require("./build/cjs/FDBDatabase.js");
|
|
5
|
+
const FDBFactory = require("./build/cjs/FDBFactory.js");
|
|
6
|
+
const FDBIndex = require("./build/cjs/FDBIndex.js");
|
|
7
|
+
const FDBKeyRange = require("./build/cjs/FDBKeyRange.js");
|
|
8
|
+
const FDBObjectStore = require("./build/cjs/FDBObjectStore.js");
|
|
9
|
+
const FDBOpenDBRequest = require("./build/cjs/FDBOpenDBRequest.js");
|
|
10
|
+
const FDBRequest = require("./build/cjs/FDBRequest.js");
|
|
11
|
+
const FDBTransaction = require("./build/cjs/FDBTransaction.js");
|
|
12
|
+
const FDBVersionChangeEvent = require("./build/cjs/FDBVersionChangeEvent.js");
|
|
13
|
+
|
|
14
|
+
// http://stackoverflow.com/a/33268326/786644 - works in browser, worker, and Node.js
|
|
15
|
+
var globalVar =
|
|
16
|
+
typeof window !== "undefined"
|
|
17
|
+
? window
|
|
18
|
+
: typeof WorkerGlobalScope !== "undefined"
|
|
19
|
+
? self
|
|
20
|
+
: typeof global !== "undefined"
|
|
21
|
+
? global
|
|
22
|
+
: Function("return this;")();
|
|
23
|
+
|
|
24
|
+
globalVar.indexedDB = fakeIndexedDB;
|
|
25
|
+
globalVar.IDBCursor = FDBCursor;
|
|
26
|
+
globalVar.IDBCursorWithValue = FDBCursorWithValue;
|
|
27
|
+
globalVar.IDBDatabase = FDBDatabase;
|
|
28
|
+
globalVar.IDBFactory = FDBFactory;
|
|
29
|
+
globalVar.IDBIndex = FDBIndex;
|
|
30
|
+
globalVar.IDBKeyRange = FDBKeyRange;
|
|
31
|
+
globalVar.IDBObjectStore = FDBObjectStore;
|
|
32
|
+
globalVar.IDBOpenDBRequest = FDBOpenDBRequest;
|
|
33
|
+
globalVar.IDBRequest = FDBRequest;
|
|
34
|
+
globalVar.IDBTransaction = FDBTransaction;
|
|
35
|
+
globalVar.IDBVersionChangeEvent = FDBVersionChangeEvent;
|
package/auto.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import fakeIndexedDB from "./build/fakeIndexedDB.js";
|
|
2
|
-
import FDBCursor from "./build/FDBCursor.js";
|
|
3
|
-
import FDBCursorWithValue from "./build/FDBCursorWithValue.js";
|
|
4
|
-
import FDBDatabase from "./build/FDBDatabase.js";
|
|
5
|
-
import FDBFactory from "./build/FDBFactory.js";
|
|
6
|
-
import FDBIndex from "./build/FDBIndex.js";
|
|
7
|
-
import FDBKeyRange from "./build/FDBKeyRange.js";
|
|
8
|
-
import FDBObjectStore from "./build/FDBObjectStore.js";
|
|
9
|
-
import FDBOpenDBRequest from "./build/FDBOpenDBRequest.js";
|
|
10
|
-
import FDBRequest from "./build/FDBRequest.js";
|
|
11
|
-
import FDBTransaction from "./build/FDBTransaction.js";
|
|
12
|
-
import FDBVersionChangeEvent from "./build/FDBVersionChangeEvent.js";
|
|
1
|
+
import fakeIndexedDB from "./build/esm/fakeIndexedDB.js";
|
|
2
|
+
import FDBCursor from "./build/esm/FDBCursor.js";
|
|
3
|
+
import FDBCursorWithValue from "./build/esm/FDBCursorWithValue.js";
|
|
4
|
+
import FDBDatabase from "./build/esm/FDBDatabase.js";
|
|
5
|
+
import FDBFactory from "./build/esm/FDBFactory.js";
|
|
6
|
+
import FDBIndex from "./build/esm/FDBIndex.js";
|
|
7
|
+
import FDBKeyRange from "./build/esm/FDBKeyRange.js";
|
|
8
|
+
import FDBObjectStore from "./build/esm/FDBObjectStore.js";
|
|
9
|
+
import FDBOpenDBRequest from "./build/esm/FDBOpenDBRequest.js";
|
|
10
|
+
import FDBRequest from "./build/esm/FDBRequest.js";
|
|
11
|
+
import FDBTransaction from "./build/esm/FDBTransaction.js";
|
|
12
|
+
import FDBVersionChangeEvent from "./build/esm/FDBVersionChangeEvent.js";
|
|
13
13
|
|
|
14
14
|
// http://stackoverflow.com/a/33268326/786644 - works in browser, worker, and Node.js
|
|
15
15
|
var globalVar =
|
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _FDBKeyRange = _interopRequireDefault(require("./FDBKeyRange.js"));
|
|
9
|
+
|
|
10
|
+
var _FDBObjectStore = _interopRequireDefault(require("./FDBObjectStore.js"));
|
|
11
|
+
|
|
12
|
+
var _cmp = _interopRequireDefault(require("./lib/cmp.js"));
|
|
13
|
+
|
|
14
|
+
var _errors = require("./lib/errors.js");
|
|
15
|
+
|
|
16
|
+
var _extractKey = _interopRequireDefault(require("./lib/extractKey.js"));
|
|
17
|
+
|
|
18
|
+
var _structuredClone = _interopRequireDefault(require("./lib/structuredClone.js"));
|
|
19
|
+
|
|
20
|
+
var _valueToKey = _interopRequireDefault(require("./lib/valueToKey.js"));
|
|
21
|
+
|
|
22
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
+
|
|
24
|
+
const getEffectiveObjectStore = cursor => {
|
|
25
|
+
if (cursor.source instanceof _FDBObjectStore.default) {
|
|
26
|
+
return cursor.source;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return cursor.source.objectStore;
|
|
30
|
+
}; // This takes a key range, a list of lower bounds, and a list of upper bounds and combines them all into a single key
|
|
31
|
+
// range. It does not handle gt/gte distinctions, because it doesn't really matter much anyway, since for next/prev
|
|
32
|
+
// cursor iteration it'd also have to look at values to be precise, which would be complicated. This should get us 99%
|
|
33
|
+
// of the way there.
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
const makeKeyRange = (range, lowers, uppers) => {
|
|
37
|
+
// Start with bounds from range
|
|
38
|
+
let lower = range !== undefined ? range.lower : undefined;
|
|
39
|
+
let upper = range !== undefined ? range.upper : undefined; // Augment with values from lowers and uppers
|
|
40
|
+
|
|
41
|
+
for (const lowerTemp of lowers) {
|
|
42
|
+
if (lowerTemp === undefined) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (lower === undefined || (0, _cmp.default)(lower, lowerTemp) === 1) {
|
|
47
|
+
lower = lowerTemp;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
for (const upperTemp of uppers) {
|
|
52
|
+
if (upperTemp === undefined) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (upper === undefined || (0, _cmp.default)(upper, upperTemp) === -1) {
|
|
57
|
+
upper = upperTemp;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (lower !== undefined && upper !== undefined) {
|
|
62
|
+
return _FDBKeyRange.default.bound(lower, upper);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (lower !== undefined) {
|
|
66
|
+
return _FDBKeyRange.default.lowerBound(lower);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (upper !== undefined) {
|
|
70
|
+
return _FDBKeyRange.default.upperBound(upper);
|
|
71
|
+
}
|
|
72
|
+
}; // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#cursor
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class FDBCursor {
|
|
76
|
+
_gotValue = false;
|
|
77
|
+
_position = undefined; // Key of previously returned record
|
|
78
|
+
|
|
79
|
+
_objectStorePosition = undefined;
|
|
80
|
+
_keyOnly = false;
|
|
81
|
+
_key = undefined;
|
|
82
|
+
_primaryKey = undefined;
|
|
83
|
+
|
|
84
|
+
constructor(source, range, direction = "next", request, keyOnly = false) {
|
|
85
|
+
this._range = range;
|
|
86
|
+
this._source = source;
|
|
87
|
+
this._direction = direction;
|
|
88
|
+
this._request = request;
|
|
89
|
+
this._keyOnly = keyOnly;
|
|
90
|
+
} // Read only properties
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
get source() {
|
|
94
|
+
return this._source;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
set source(val) {
|
|
98
|
+
/* For babel */
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
get direction() {
|
|
102
|
+
return this._direction;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
set direction(val) {
|
|
106
|
+
/* For babel */
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
get key() {
|
|
110
|
+
return this._key;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
set key(val) {
|
|
114
|
+
/* For babel */
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
get primaryKey() {
|
|
118
|
+
return this._primaryKey;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
set primaryKey(val) {
|
|
122
|
+
/* For babel */
|
|
123
|
+
} // https://w3c.github.io/IndexedDB/#iterate-a-cursor
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
_iterate(key, primaryKey) {
|
|
127
|
+
const sourceIsObjectStore = this.source instanceof _FDBObjectStore.default; // Can't use sourceIsObjectStore because TypeScript
|
|
128
|
+
|
|
129
|
+
const records = this.source instanceof _FDBObjectStore.default ? this.source._rawObjectStore.records : this.source._rawIndex.records;
|
|
130
|
+
let foundRecord;
|
|
131
|
+
|
|
132
|
+
if (this.direction === "next") {
|
|
133
|
+
const range = makeKeyRange(this._range, [key, this._position], []);
|
|
134
|
+
|
|
135
|
+
for (const record of records.values(range)) {
|
|
136
|
+
const cmpResultKey = key !== undefined ? (0, _cmp.default)(record.key, key) : undefined;
|
|
137
|
+
const cmpResultPosition = this._position !== undefined ? (0, _cmp.default)(record.key, this._position) : undefined;
|
|
138
|
+
|
|
139
|
+
if (key !== undefined) {
|
|
140
|
+
if (cmpResultKey === -1) {
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (primaryKey !== undefined) {
|
|
146
|
+
if (cmpResultKey === -1) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const cmpResultPrimaryKey = (0, _cmp.default)(record.value, primaryKey);
|
|
151
|
+
|
|
152
|
+
if (cmpResultKey === 0 && cmpResultPrimaryKey === -1) {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (this._position !== undefined && sourceIsObjectStore) {
|
|
158
|
+
if (cmpResultPosition !== 1) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (this._position !== undefined && !sourceIsObjectStore) {
|
|
164
|
+
if (cmpResultPosition === -1) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (cmpResultPosition === 0 && (0, _cmp.default)(record.value, this._objectStorePosition) !== 1) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (this._range !== undefined) {
|
|
174
|
+
if (!this._range.includes(record.key)) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
foundRecord = record;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
} else if (this.direction === "nextunique") {
|
|
183
|
+
// This could be done without iterating, if the range was defined slightly better (to handle gt/gte cases).
|
|
184
|
+
// But the performance difference should be small, and that wouldn't work anyway for directions where the
|
|
185
|
+
// value needs to be used (like next and prev).
|
|
186
|
+
const range = makeKeyRange(this._range, [key, this._position], []);
|
|
187
|
+
|
|
188
|
+
for (const record of records.values(range)) {
|
|
189
|
+
if (key !== undefined) {
|
|
190
|
+
if ((0, _cmp.default)(record.key, key) === -1) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (this._position !== undefined) {
|
|
196
|
+
if ((0, _cmp.default)(record.key, this._position) !== 1) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (this._range !== undefined) {
|
|
202
|
+
if (!this._range.includes(record.key)) {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
foundRecord = record;
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
} else if (this.direction === "prev") {
|
|
211
|
+
const range = makeKeyRange(this._range, [], [key, this._position]);
|
|
212
|
+
|
|
213
|
+
for (const record of records.values(range, "prev")) {
|
|
214
|
+
const cmpResultKey = key !== undefined ? (0, _cmp.default)(record.key, key) : undefined;
|
|
215
|
+
const cmpResultPosition = this._position !== undefined ? (0, _cmp.default)(record.key, this._position) : undefined;
|
|
216
|
+
|
|
217
|
+
if (key !== undefined) {
|
|
218
|
+
if (cmpResultKey === 1) {
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (primaryKey !== undefined) {
|
|
224
|
+
if (cmpResultKey === 1) {
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const cmpResultPrimaryKey = (0, _cmp.default)(record.value, primaryKey);
|
|
229
|
+
|
|
230
|
+
if (cmpResultKey === 0 && cmpResultPrimaryKey === 1) {
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (this._position !== undefined && sourceIsObjectStore) {
|
|
236
|
+
if (cmpResultPosition !== -1) {
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (this._position !== undefined && !sourceIsObjectStore) {
|
|
242
|
+
if (cmpResultPosition === 1) {
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (cmpResultPosition === 0 && (0, _cmp.default)(record.value, this._objectStorePosition) !== -1) {
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (this._range !== undefined) {
|
|
252
|
+
if (!this._range.includes(record.key)) {
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
foundRecord = record;
|
|
258
|
+
break;
|
|
259
|
+
}
|
|
260
|
+
} else if (this.direction === "prevunique") {
|
|
261
|
+
let tempRecord;
|
|
262
|
+
const range = makeKeyRange(this._range, [], [key, this._position]);
|
|
263
|
+
|
|
264
|
+
for (const record of records.values(range, "prev")) {
|
|
265
|
+
if (key !== undefined) {
|
|
266
|
+
if ((0, _cmp.default)(record.key, key) === 1) {
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (this._position !== undefined) {
|
|
272
|
+
if ((0, _cmp.default)(record.key, this._position) !== -1) {
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (this._range !== undefined) {
|
|
278
|
+
if (!this._range.includes(record.key)) {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
tempRecord = record;
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (tempRecord) {
|
|
288
|
+
foundRecord = records.get(tempRecord.key);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
let result;
|
|
293
|
+
|
|
294
|
+
if (!foundRecord) {
|
|
295
|
+
this._key = undefined;
|
|
296
|
+
|
|
297
|
+
if (!sourceIsObjectStore) {
|
|
298
|
+
this._objectStorePosition = undefined;
|
|
299
|
+
} // "this instanceof FDBCursorWithValue" would be better and not require (this as any), but causes runtime
|
|
300
|
+
// error due to circular dependency.
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
if (!this._keyOnly && this.toString() === "[object IDBCursorWithValue]") {
|
|
304
|
+
this.value = undefined;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
result = null;
|
|
308
|
+
} else {
|
|
309
|
+
this._position = foundRecord.key;
|
|
310
|
+
|
|
311
|
+
if (!sourceIsObjectStore) {
|
|
312
|
+
this._objectStorePosition = foundRecord.value;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
this._key = foundRecord.key;
|
|
316
|
+
|
|
317
|
+
if (sourceIsObjectStore) {
|
|
318
|
+
this._primaryKey = (0, _structuredClone.default)(foundRecord.key);
|
|
319
|
+
|
|
320
|
+
if (!this._keyOnly && this.toString() === "[object IDBCursorWithValue]") {
|
|
321
|
+
this.value = (0, _structuredClone.default)(foundRecord.value);
|
|
322
|
+
}
|
|
323
|
+
} else {
|
|
324
|
+
this._primaryKey = (0, _structuredClone.default)(foundRecord.value);
|
|
325
|
+
|
|
326
|
+
if (!this._keyOnly && this.toString() === "[object IDBCursorWithValue]") {
|
|
327
|
+
if (this.source instanceof _FDBObjectStore.default) {
|
|
328
|
+
// Can't use sourceIsObjectStore because TypeScript
|
|
329
|
+
throw new Error("This should never happen");
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const value = this.source.objectStore._rawObjectStore.getValue(foundRecord.value);
|
|
333
|
+
|
|
334
|
+
this.value = (0, _structuredClone.default)(value);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
this._gotValue = true;
|
|
339
|
+
result = this;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return result;
|
|
343
|
+
} // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBCursor-update-IDBRequest-any-value
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
update(value) {
|
|
347
|
+
if (value === undefined) {
|
|
348
|
+
throw new TypeError();
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const effectiveObjectStore = getEffectiveObjectStore(this);
|
|
352
|
+
const effectiveKey = this.source.hasOwnProperty("_rawIndex") ? this.primaryKey : this._position;
|
|
353
|
+
const transaction = effectiveObjectStore.transaction;
|
|
354
|
+
|
|
355
|
+
if (transaction._state !== "active") {
|
|
356
|
+
throw new _errors.TransactionInactiveError();
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (transaction.mode === "readonly") {
|
|
360
|
+
throw new _errors.ReadOnlyError();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (effectiveObjectStore._rawObjectStore.deleted) {
|
|
364
|
+
throw new _errors.InvalidStateError();
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (!(this.source instanceof _FDBObjectStore.default) && this.source._rawIndex.deleted) {
|
|
368
|
+
throw new _errors.InvalidStateError();
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
if (!this._gotValue || !this.hasOwnProperty("value")) {
|
|
372
|
+
throw new _errors.InvalidStateError();
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const clone = (0, _structuredClone.default)(value);
|
|
376
|
+
|
|
377
|
+
if (effectiveObjectStore.keyPath !== null) {
|
|
378
|
+
let tempKey;
|
|
379
|
+
|
|
380
|
+
try {
|
|
381
|
+
tempKey = (0, _extractKey.default)(effectiveObjectStore.keyPath, clone);
|
|
382
|
+
} catch (err) {
|
|
383
|
+
/* Handled immediately below */
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if ((0, _cmp.default)(tempKey, effectiveKey) !== 0) {
|
|
387
|
+
throw new _errors.DataError();
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const record = {
|
|
392
|
+
key: effectiveKey,
|
|
393
|
+
value: clone
|
|
394
|
+
};
|
|
395
|
+
return transaction._execRequestAsync({
|
|
396
|
+
operation: effectiveObjectStore._rawObjectStore.storeRecord.bind(effectiveObjectStore._rawObjectStore, record, false, transaction._rollbackLog),
|
|
397
|
+
source: this
|
|
398
|
+
});
|
|
399
|
+
} // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBCursor-advance-void-unsigned-long-count
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
advance(count) {
|
|
403
|
+
if (!Number.isInteger(count) || count <= 0) {
|
|
404
|
+
throw new TypeError();
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const effectiveObjectStore = getEffectiveObjectStore(this);
|
|
408
|
+
const transaction = effectiveObjectStore.transaction;
|
|
409
|
+
|
|
410
|
+
if (transaction._state !== "active") {
|
|
411
|
+
throw new _errors.TransactionInactiveError();
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (effectiveObjectStore._rawObjectStore.deleted) {
|
|
415
|
+
throw new _errors.InvalidStateError();
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
if (!(this.source instanceof _FDBObjectStore.default) && this.source._rawIndex.deleted) {
|
|
419
|
+
throw new _errors.InvalidStateError();
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (!this._gotValue) {
|
|
423
|
+
throw new _errors.InvalidStateError();
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (this._request) {
|
|
427
|
+
this._request.readyState = "pending";
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
transaction._execRequestAsync({
|
|
431
|
+
operation: () => {
|
|
432
|
+
let result;
|
|
433
|
+
|
|
434
|
+
for (let i = 0; i < count; i++) {
|
|
435
|
+
result = this._iterate(); // Not sure why this is needed
|
|
436
|
+
|
|
437
|
+
if (!result) {
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
return result;
|
|
443
|
+
},
|
|
444
|
+
request: this._request,
|
|
445
|
+
source: this.source
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
this._gotValue = false;
|
|
449
|
+
} // http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBCursor-continue-void-any-key
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
continue(key) {
|
|
453
|
+
const effectiveObjectStore = getEffectiveObjectStore(this);
|
|
454
|
+
const transaction = effectiveObjectStore.transaction;
|
|
455
|
+
|
|
456
|
+
if (transaction._state !== "active") {
|
|
457
|
+
throw new _errors.TransactionInactiveError();
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
if (effectiveObjectStore._rawObjectStore.deleted) {
|
|
461
|
+
throw new _errors.InvalidStateError();
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (!(this.source instanceof _FDBObjectStore.default) && this.source._rawIndex.deleted) {
|
|
465
|
+
throw new _errors.InvalidStateError();
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (!this._gotValue) {
|
|
469
|
+
throw new _errors.InvalidStateError();
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (key !== undefined) {
|
|
473
|
+
key = (0, _valueToKey.default)(key);
|
|
474
|
+
const cmpResult = (0, _cmp.default)(key, this._position);
|
|
475
|
+
|
|
476
|
+
if (cmpResult <= 0 && (this.direction === "next" || this.direction === "nextunique") || cmpResult >= 0 && (this.direction === "prev" || this.direction === "prevunique")) {
|
|
477
|
+
throw new _errors.DataError();
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (this._request) {
|
|
482
|
+
this._request.readyState = "pending";
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
transaction._execRequestAsync({
|
|
486
|
+
operation: this._iterate.bind(this, key),
|
|
487
|
+
request: this._request,
|
|
488
|
+
source: this.source
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
this._gotValue = false;
|
|
492
|
+
} // hthttps://w3c.github.io/IndexedDB/#dom-idbcursor-continueprimarykey
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
continuePrimaryKey(key, primaryKey) {
|
|
496
|
+
const effectiveObjectStore = getEffectiveObjectStore(this);
|
|
497
|
+
const transaction = effectiveObjectStore.transaction;
|
|
498
|
+
|
|
499
|
+
if (transaction._state !== "active") {
|
|
500
|
+
throw new _errors.TransactionInactiveError();
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
if (effectiveObjectStore._rawObjectStore.deleted) {
|
|
504
|
+
throw new _errors.InvalidStateError();
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if (!(this.source instanceof _FDBObjectStore.default) && this.source._rawIndex.deleted) {
|
|
508
|
+
throw new _errors.InvalidStateError();
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
if (this.source instanceof _FDBObjectStore.default || this.direction !== "next" && this.direction !== "prev") {
|
|
512
|
+
throw new _errors.InvalidAccessError();
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (!this._gotValue) {
|
|
516
|
+
throw new _errors.InvalidStateError();
|
|
517
|
+
} // Not sure about this
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
if (key === undefined || primaryKey === undefined) {
|
|
521
|
+
throw new _errors.DataError();
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
key = (0, _valueToKey.default)(key);
|
|
525
|
+
const cmpResult = (0, _cmp.default)(key, this._position);
|
|
526
|
+
|
|
527
|
+
if (cmpResult === -1 && this.direction === "next" || cmpResult === 1 && this.direction === "prev") {
|
|
528
|
+
throw new _errors.DataError();
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
const cmpResult2 = (0, _cmp.default)(primaryKey, this._objectStorePosition);
|
|
532
|
+
|
|
533
|
+
if (cmpResult === 0) {
|
|
534
|
+
if (cmpResult2 <= 0 && this.direction === "next" || cmpResult2 >= 0 && this.direction === "prev") {
|
|
535
|
+
throw new _errors.DataError();
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (this._request) {
|
|
540
|
+
this._request.readyState = "pending";
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
transaction._execRequestAsync({
|
|
544
|
+
operation: this._iterate.bind(this, key, primaryKey),
|
|
545
|
+
request: this._request,
|
|
546
|
+
source: this.source
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
this._gotValue = false;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
delete() {
|
|
553
|
+
const effectiveObjectStore = getEffectiveObjectStore(this);
|
|
554
|
+
const effectiveKey = this.source.hasOwnProperty("_rawIndex") ? this.primaryKey : this._position;
|
|
555
|
+
const transaction = effectiveObjectStore.transaction;
|
|
556
|
+
|
|
557
|
+
if (transaction._state !== "active") {
|
|
558
|
+
throw new _errors.TransactionInactiveError();
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
if (transaction.mode === "readonly") {
|
|
562
|
+
throw new _errors.ReadOnlyError();
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
if (effectiveObjectStore._rawObjectStore.deleted) {
|
|
566
|
+
throw new _errors.InvalidStateError();
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
if (!(this.source instanceof _FDBObjectStore.default) && this.source._rawIndex.deleted) {
|
|
570
|
+
throw new _errors.InvalidStateError();
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
if (!this._gotValue || !this.hasOwnProperty("value")) {
|
|
574
|
+
throw new _errors.InvalidStateError();
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
return transaction._execRequestAsync({
|
|
578
|
+
operation: effectiveObjectStore._rawObjectStore.deleteRecord.bind(effectiveObjectStore._rawObjectStore, effectiveKey, transaction._rollbackLog),
|
|
579
|
+
source: this
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
toString() {
|
|
584
|
+
return "[object IDBCursor]";
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
var _default = FDBCursor;
|
|
590
|
+
exports.default = _default;
|
|
591
|
+
module.exports = exports.default;
|