fake-indexeddb 5.0.0 → 5.0.2

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 CHANGED
@@ -118,6 +118,48 @@ To use it on all Jest tests without having to include it in each file, add the a
118
118
  }
119
119
  ```
120
120
 
121
+ ### jsdom (often used with Jest)
122
+
123
+ As of version 5, fake-indexeddb no longer includes a `structuredClone` polyfill. This mostly affects old environments like unsupported versions of Node.js, but [it also affects jsdom](https://github.com/dumbmatter/fakeIndexedDB/issues/88), which is often used with Jest and other testing frameworks.
124
+
125
+ There are a few ways you could work around this. You could include your own `structuredClone` polyfill by installing core-js and importing its polyfill before you use fake-indexeddb:
126
+
127
+ ```js
128
+ import "core-js/stable/structured-clone";
129
+ import "fake-indexeddb/auto";
130
+ ```
131
+
132
+ Or, [you could manually include the Node.js `structuredClone` implementation in a jsdom environment](https://github.com/jsdom/jsdom/issues/3363#issuecomment-1467894943):
133
+
134
+ ```js
135
+ // FixJSDOMEnvironment.ts
136
+
137
+ import JSDOMEnvironment from 'jest-environment-jsdom';
138
+
139
+ // https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string
140
+ export default class FixJSDOMEnvironment extends JSDOMEnvironment {
141
+ constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
142
+ super(...args);
143
+
144
+ // FIXME https://github.com/jsdom/jsdom/issues/3363
145
+ this.global.structuredClone = structuredClone;
146
+ }
147
+ }
148
+ ```
149
+
150
+ ```js
151
+ // jest.config.js
152
+
153
+ /** @type {import('jest').Config} */
154
+ const config = {
155
+ testEnvironment: './FixJSDOMEnvironment.ts',
156
+ };
157
+
158
+ module.exports = config;
159
+ ```
160
+
161
+ Hopefully a future version of jsdom will no longer require these workarounds.
162
+
121
163
  ### Wiping/resetting the indexedDB for a fresh state
122
164
 
123
165
  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.
@@ -178,12 +178,7 @@ class FDBIndex {
178
178
  }
179
179
  return this.objectStore.transaction._execRequestAsync({
180
180
  operation: () => {
181
- let count = 0;
182
- const cursor = new _FDBCursor.default(this, key);
183
- while (cursor._iterate() !== null) {
184
- count += 1;
185
- }
186
- return count;
181
+ return this._rawIndex.count(key);
187
182
  },
188
183
  source: this
189
184
  });
@@ -370,12 +370,7 @@ class FDBObjectStore {
370
370
  }
371
371
  return this.transaction._execRequestAsync({
372
372
  operation: () => {
373
- let count = 0;
374
- const cursor = new _FDBCursor.default(this, key);
375
- while (cursor._iterate() !== null) {
376
- count += 1;
377
- }
378
- return count;
373
+ return this._rawObjectStore.count(key);
379
374
  },
380
375
  source: this
381
376
  });
@@ -150,6 +150,15 @@ class Index {
150
150
  source: null
151
151
  });
152
152
  }
153
+ count(range) {
154
+ let count = 0;
155
+
156
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
157
+ for (const record of this.records.values(range)) {
158
+ count += 1;
159
+ }
160
+ return count;
161
+ }
153
162
  }
154
163
  var _default = exports.default = Index;
155
164
  module.exports = exports.default;
@@ -166,6 +166,15 @@ class ObjectStore {
166
166
  rawIndex.records.clear();
167
167
  }
168
168
  }
169
+ count(range) {
170
+ let count = 0;
171
+
172
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
173
+ for (const record of this.records.values(range)) {
174
+ count += 1;
175
+ }
176
+ return count;
177
+ }
169
178
  }
170
179
  var _default = exports.default = ObjectStore;
171
180
  module.exports = exports.default;
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _errors = require("./errors.js");
8
- // https://w3c.github.io/IndexedDB/#convert-a-value-to-a-input
8
+ // https://w3c.github.io/IndexedDB/#convert-value-to-key
9
9
  const valueToKey = (input, seen) => {
10
10
  if (typeof input === "number") {
11
11
  if (isNaN(input)) {
@@ -20,11 +20,22 @@ const valueToKey = (input, seen) => {
20
20
  return new Date(ms);
21
21
  } else if (typeof input === "string") {
22
22
  return input;
23
- } else if (input instanceof ArrayBuffer || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
24
- if (input instanceof ArrayBuffer) {
25
- return new Uint8Array(input).buffer;
23
+ } else if (input instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
24
+ let arrayBuffer;
25
+ let offset = 0;
26
+ let length = 0;
27
+ if (input instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer) {
28
+ arrayBuffer = input;
29
+ length = input.byteLength;
30
+ } else {
31
+ arrayBuffer = input.buffer;
32
+ offset = input.byteOffset;
33
+ length = input.byteLength;
26
34
  }
27
- return new Uint8Array(input.buffer).buffer;
35
+ if (arrayBuffer.detached) {
36
+ return new ArrayBuffer(0);
37
+ }
38
+ return arrayBuffer.slice(offset, offset + length);
28
39
  } else if (Array.isArray(input)) {
29
40
  if (seen === undefined) {
30
41
  seen = new Set();
@@ -171,12 +171,7 @@ class FDBIndex {
171
171
  }
172
172
  return this.objectStore.transaction._execRequestAsync({
173
173
  operation: () => {
174
- let count = 0;
175
- const cursor = new FDBCursor(this, key);
176
- while (cursor._iterate() !== null) {
177
- count += 1;
178
- }
179
- return count;
174
+ return this._rawIndex.count(key);
180
175
  },
181
176
  source: this
182
177
  });
@@ -363,12 +363,7 @@ class FDBObjectStore {
363
363
  }
364
364
  return this.transaction._execRequestAsync({
365
365
  operation: () => {
366
- let count = 0;
367
- const cursor = new FDBCursor(this, key);
368
- while (cursor._iterate() !== null) {
369
- count += 1;
370
- }
371
- return count;
366
+ return this._rawObjectStore.count(key);
372
367
  },
373
368
  source: this
374
369
  });
@@ -144,5 +144,14 @@ class Index {
144
144
  source: null
145
145
  });
146
146
  }
147
+ count(range) {
148
+ let count = 0;
149
+
150
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
151
+ for (const record of this.records.values(range)) {
152
+ count += 1;
153
+ }
154
+ return count;
155
+ }
147
156
  }
148
157
  export default Index;
@@ -159,5 +159,14 @@ class ObjectStore {
159
159
  rawIndex.records.clear();
160
160
  }
161
161
  }
162
+ count(range) {
163
+ let count = 0;
164
+
165
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
166
+ for (const record of this.records.values(range)) {
167
+ count += 1;
168
+ }
169
+ return count;
170
+ }
162
171
  }
163
172
  export default ObjectStore;
@@ -1,5 +1,5 @@
1
1
  import { DataError } from "./errors.js";
2
- // https://w3c.github.io/IndexedDB/#convert-a-value-to-a-input
2
+ // https://w3c.github.io/IndexedDB/#convert-value-to-key
3
3
  const valueToKey = (input, seen) => {
4
4
  if (typeof input === "number") {
5
5
  if (isNaN(input)) {
@@ -14,11 +14,22 @@ const valueToKey = (input, seen) => {
14
14
  return new Date(ms);
15
15
  } else if (typeof input === "string") {
16
16
  return input;
17
- } else if (input instanceof ArrayBuffer || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
18
- if (input instanceof ArrayBuffer) {
19
- return new Uint8Array(input).buffer;
17
+ } else if (input instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer || typeof ArrayBuffer !== "undefined" && ArrayBuffer.isView && ArrayBuffer.isView(input)) {
18
+ let arrayBuffer;
19
+ let offset = 0;
20
+ let length = 0;
21
+ if (input instanceof ArrayBuffer || typeof SharedArrayBuffer !== "undefined" && input instanceof SharedArrayBuffer) {
22
+ arrayBuffer = input;
23
+ length = input.byteLength;
24
+ } else {
25
+ arrayBuffer = input.buffer;
26
+ offset = input.byteOffset;
27
+ length = input.byteLength;
20
28
  }
21
- return new Uint8Array(input.buffer).buffer;
29
+ if (arrayBuffer.detached) {
30
+ return new ArrayBuffer(0);
31
+ }
32
+ return arrayBuffer.slice(offset, offset + length);
22
33
  } else if (Array.isArray(input)) {
23
34
  if (seen === undefined) {
24
35
  seen = new Set();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fake-indexeddb",
3
- "version": "5.0.0",
3
+ "version": "5.0.2",
4
4
  "description": "Fake IndexedDB: a pure JS in-memory implementation of the IndexedDB API",
5
5
  "homepage": "https://github.com/dumbmatter/fakeIndexedDB",
6
6
  "repository": {