@dumbmatter/idb 7.0.0 → 8.0.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/build/index.js CHANGED
@@ -1,5 +1,161 @@
1
- import { w as wrap, r as replaceTraps } from './wrap-idb-value.js';
2
- export { u as unwrap, w as wrap } from './wrap-idb-value.js';
1
+ const instanceOfAny = (object, constructors) => constructors.some((c) => object instanceof c);
2
+
3
+ let idbProxyableTypes;
4
+ let cursorAdvanceMethods;
5
+ // This is a function to prevent it throwing up in node environments.
6
+ function getIdbProxyableTypes() {
7
+ return (idbProxyableTypes ||
8
+ (idbProxyableTypes = [
9
+ IDBDatabase,
10
+ IDBObjectStore,
11
+ IDBIndex,
12
+ IDBCursor,
13
+ IDBTransaction,
14
+ ]));
15
+ }
16
+ // This is a function to prevent it throwing up in node environments.
17
+ function getCursorAdvanceMethods() {
18
+ return (cursorAdvanceMethods ||
19
+ (cursorAdvanceMethods = [
20
+ IDBCursor.prototype.advance,
21
+ IDBCursor.prototype.continue,
22
+ IDBCursor.prototype.continuePrimaryKey,
23
+ ]));
24
+ }
25
+ const transactionDoneMap = new WeakMap();
26
+ const transformCache = new WeakMap();
27
+ const reverseTransformCache = new WeakMap();
28
+ function promisifyRequest(request) {
29
+ const promise = new Promise((resolve, reject) => {
30
+ const unlisten = () => {
31
+ request.removeEventListener('success', success);
32
+ request.removeEventListener('error', error);
33
+ };
34
+ const success = () => {
35
+ resolve(wrap(request.result));
36
+ unlisten();
37
+ };
38
+ const error = () => {
39
+ reject(request.error);
40
+ unlisten();
41
+ };
42
+ request.addEventListener('success', success);
43
+ request.addEventListener('error', error);
44
+ });
45
+ // This mapping exists in reverseTransformCache but doesn't exist in transformCache. This
46
+ // is because we create many promises from a single IDBRequest.
47
+ reverseTransformCache.set(promise, request);
48
+ return promise;
49
+ }
50
+ function cacheDonePromiseForTransaction(tx) {
51
+ // Early bail if we've already created a done promise for this transaction.
52
+ if (transactionDoneMap.has(tx))
53
+ return;
54
+ const done = new Promise((resolve, reject) => {
55
+ const unlisten = () => {
56
+ tx.removeEventListener('complete', complete);
57
+ tx.removeEventListener('error', error);
58
+ tx.removeEventListener('abort', error);
59
+ };
60
+ const complete = () => {
61
+ resolve();
62
+ unlisten();
63
+ };
64
+ const error = () => {
65
+ reject(tx.error || new DOMException('AbortError', 'AbortError'));
66
+ unlisten();
67
+ };
68
+ tx.addEventListener('complete', complete);
69
+ tx.addEventListener('error', error);
70
+ tx.addEventListener('abort', error);
71
+ });
72
+ // Cache it for later retrieval.
73
+ transactionDoneMap.set(tx, done);
74
+ }
75
+ let idbProxyTraps = {
76
+ get(target, prop, receiver) {
77
+ if (target instanceof IDBTransaction) {
78
+ // Special handling for transaction.done.
79
+ if (prop === 'done')
80
+ return transactionDoneMap.get(target);
81
+ // Make tx.store return the only store in the transaction, or undefined if there are many.
82
+ if (prop === 'store') {
83
+ return receiver.objectStoreNames[1]
84
+ ? undefined
85
+ : receiver.objectStore(receiver.objectStoreNames[0]);
86
+ }
87
+ }
88
+ // Else transform whatever we get back.
89
+ return wrap(target[prop]);
90
+ },
91
+ set(target, prop, value) {
92
+ target[prop] = value;
93
+ return true;
94
+ },
95
+ has(target, prop) {
96
+ if (target instanceof IDBTransaction &&
97
+ (prop === 'done' || prop === 'store')) {
98
+ return true;
99
+ }
100
+ return prop in target;
101
+ },
102
+ };
103
+ function replaceTraps(callback) {
104
+ idbProxyTraps = callback(idbProxyTraps);
105
+ }
106
+ function wrapFunction(func) {
107
+ // Due to expected object equality (which is enforced by the caching in `wrap`), we
108
+ // only create one new func per func.
109
+ // Cursor methods are special, as the behaviour is a little more different to standard IDB. In
110
+ // IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
111
+ // cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
112
+ // with real promises, so each advance methods returns a new promise for the cursor object, or
113
+ // undefined if the end of the cursor has been reached.
114
+ if (getCursorAdvanceMethods().includes(func)) {
115
+ return function (...args) {
116
+ // Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
117
+ // the original object.
118
+ func.apply(unwrap(this), args);
119
+ return wrap(this.request);
120
+ };
121
+ }
122
+ return function (...args) {
123
+ // Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
124
+ // the original object.
125
+ return wrap(func.apply(unwrap(this), args));
126
+ };
127
+ }
128
+ function transformCachableValue(value) {
129
+ if (typeof value === 'function')
130
+ return wrapFunction(value);
131
+ // This doesn't return, it just creates a 'done' promise for the transaction,
132
+ // which is later returned for transaction.done (see idbObjectHandler).
133
+ if (value instanceof IDBTransaction)
134
+ cacheDonePromiseForTransaction(value);
135
+ if (instanceOfAny(value, getIdbProxyableTypes()))
136
+ return new Proxy(value, idbProxyTraps);
137
+ // Return the same value back if we're not going to transform it.
138
+ return value;
139
+ }
140
+ function wrap(value) {
141
+ // We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
142
+ // IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
143
+ if (value instanceof IDBRequest)
144
+ return promisifyRequest(value);
145
+ // If we've already transformed this value before, reuse the transformed value.
146
+ // This is faster, but it also provides object equality.
147
+ if (transformCache.has(value))
148
+ return transformCache.get(value);
149
+ const newValue = transformCachableValue(value);
150
+ // Not all types are transformed.
151
+ // These may be primitive types, so they can't be WeakMap keys.
152
+ if (newValue !== value) {
153
+ transformCache.set(value, newValue);
154
+ reverseTransformCache.set(newValue, value);
155
+ }
156
+ return newValue;
157
+ }
158
+ const unwrap = (value) => reverseTransformCache.get(value);
3
159
 
4
160
  /**
5
161
  * Open a database.
@@ -13,17 +169,21 @@ function openDB(name, version, { blocked, upgrade, blocking, terminated } = {})
13
169
  const openPromise = wrap(request);
14
170
  if (upgrade) {
15
171
  request.addEventListener('upgradeneeded', (event) => {
16
- upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction));
172
+ upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction), event);
17
173
  });
18
174
  }
19
- if (blocked)
20
- request.addEventListener('blocked', () => blocked());
175
+ if (blocked) {
176
+ request.addEventListener('blocked', (event) => blocked(
177
+ // Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
178
+ event.oldVersion, event.newVersion, event));
179
+ }
21
180
  openPromise
22
181
  .then((db) => {
23
182
  if (terminated)
24
183
  db.addEventListener('close', () => terminated());
25
- if (blocking)
26
- db.addEventListener('versionchange', () => blocking());
184
+ if (blocking) {
185
+ db.addEventListener('versionchange', (event) => blocking(event.oldVersion, event.newVersion, event));
186
+ }
27
187
  })
28
188
  .catch(() => { });
29
189
  return openPromise;
@@ -35,8 +195,11 @@ function openDB(name, version, { blocked, upgrade, blocking, terminated } = {})
35
195
  */
36
196
  function deleteDB(name, { blocked } = {}) {
37
197
  const request = indexedDB.deleteDatabase(name);
38
- if (blocked)
39
- request.addEventListener('blocked', () => blocked());
198
+ if (blocked) {
199
+ request.addEventListener('blocked', (event) => blocked(
200
+ // Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
201
+ event.oldVersion, event));
202
+ }
40
203
  return wrap(request).then(() => undefined);
41
204
  }
42
205
 
@@ -85,4 +248,58 @@ replaceTraps((oldTraps) => ({
85
248
  has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop),
86
249
  }));
87
250
 
88
- export { deleteDB, openDB };
251
+ const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];
252
+ const methodMap = {};
253
+ const advanceResults = new WeakMap();
254
+ const ittrProxiedCursorToOriginalProxy = new WeakMap();
255
+ const cursorIteratorTraps = {
256
+ get(target, prop) {
257
+ if (!advanceMethodProps.includes(prop))
258
+ return target[prop];
259
+ let cachedFunc = methodMap[prop];
260
+ if (!cachedFunc) {
261
+ cachedFunc = methodMap[prop] = function (...args) {
262
+ advanceResults.set(this, ittrProxiedCursorToOriginalProxy.get(this)[prop](...args));
263
+ };
264
+ }
265
+ return cachedFunc;
266
+ },
267
+ };
268
+ async function* iterate(...args) {
269
+ // tslint:disable-next-line:no-this-assignment
270
+ let cursor = this;
271
+ if (!(cursor instanceof IDBCursor)) {
272
+ cursor = await cursor.openCursor(...args);
273
+ }
274
+ if (!cursor)
275
+ return;
276
+ cursor = cursor;
277
+ const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);
278
+ ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);
279
+ // Map this double-proxy back to the original, so other cursor methods work.
280
+ reverseTransformCache.set(proxiedCursor, unwrap(cursor));
281
+ while (cursor) {
282
+ yield proxiedCursor;
283
+ // If one of the advancing methods was not called, call continue().
284
+ cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());
285
+ advanceResults.delete(proxiedCursor);
286
+ }
287
+ }
288
+ function isIteratorProp(target, prop) {
289
+ return ((prop === Symbol.asyncIterator &&
290
+ instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||
291
+ (prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore])));
292
+ }
293
+ replaceTraps((oldTraps) => ({
294
+ ...oldTraps,
295
+ get(target, prop, receiver) {
296
+ if (isIteratorProp(target, prop))
297
+ return iterate;
298
+ return oldTraps.get(target, prop, receiver);
299
+ },
300
+ has(target, prop) {
301
+ return isIteratorProp(target, prop) || oldTraps.has(target, prop);
302
+ },
303
+ }));
304
+
305
+ export { deleteDB, openDB, unwrap, wrap };
package/build/umd.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).idb={})}(this,(function(e){"use strict";let t,n;const r=new WeakMap,o=new WeakMap,s=new WeakMap,i=new WeakMap,a=new WeakMap;let c={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return o.get(e);if("objectStoreNames"===t)return e.objectStoreNames||s.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return f(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function d(e){return e!==IDBDatabase.prototype.transaction||"objectStoreNames"in IDBTransaction.prototype?(n||(n=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(p(this),t),f(r.get(this))}:function(...t){return f(e.apply(p(this),t))}:function(t,...n){const r=e.call(p(this),t,...n);return s.set(r,t.sort?t.sort():[t]),f(r)}}function u(e){return"function"==typeof e?d(e):(e instanceof IDBTransaction&&function(e){if(o.has(e))return;const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",s),e.removeEventListener("abort",s)},o=()=>{t(),r()},s=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",s),e.addEventListener("abort",s)}));o.set(e,t)}(e),n=e,(t||(t=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction])).some((e=>n instanceof e))?new Proxy(e,c):e);var n}function f(e){if(e instanceof IDBRequest)return function(e){const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",s)},o=()=>{t(f(e.result)),r()},s=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",s)}));return t.then((t=>{t instanceof IDBCursor&&r.set(t,e)})).catch((()=>{})),a.set(t,e),t}(e);if(i.has(e))return i.get(e);const t=u(e);return t!==e&&(i.set(e,t),a.set(t,e)),t}const p=e=>a.get(e);const l=["get","getKey","getAll","getAllKeys","count"],D=["put","add","delete","clear"],b=new Map;function v(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(b.get(t))return b.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=D.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!l.includes(n))return;const s=async function(e,...t){const s=this.transaction(e,o?"readwrite":"readonly");let i=s.store;return r&&(i=i.index(t.shift())),(await Promise.all([i[n](...t),o&&s.done]))[0]};return b.set(t,s),s}c=(e=>({...e,get:(t,n,r)=>v(t,n)||e.get(t,n,r),has:(t,n)=>!!v(t,n)||e.has(t,n)}))(c),e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",(()=>t())),f(n).then((()=>{}))},e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o,terminated:s}={}){const i=indexedDB.open(e,t),a=f(i);return r&&i.addEventListener("upgradeneeded",(e=>{r(f(i.result),e.oldVersion,e.newVersion,f(i.transaction))})),n&&i.addEventListener("blocked",(()=>n())),a.then((e=>{s&&e.addEventListener("close",(()=>s())),o&&e.addEventListener("versionchange",(()=>o()))})).catch((()=>{})),a},e.unwrap=p,e.wrap=f}));
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).idb={})}(this,(function(e){"use strict";const t=(e,t)=>t.some((t=>e instanceof t));let n,r;const o=new WeakMap,s=new WeakMap,i=new WeakMap;let a={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return o.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return f(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function c(e){a=e(a)}function u(e){return(r||(r=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(l(this),t),f(this.request)}:function(...t){return f(e.apply(l(this),t))}}function d(e){return"function"==typeof e?u(e):(e instanceof IDBTransaction&&function(e){if(o.has(e))return;const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",s),e.removeEventListener("abort",s)},o=()=>{t(),r()},s=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",s),e.addEventListener("abort",s)}));o.set(e,t)}(e),t(e,n||(n=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(e,a):e)}function f(e){if(e instanceof IDBRequest)return function(e){const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",s)},o=()=>{t(f(e.result)),r()},s=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",s)}));return i.set(t,e),t}(e);if(s.has(e))return s.get(e);const t=d(e);return t!==e&&(s.set(e,t),i.set(t,e)),t}const l=e=>i.get(e);const p=["get","getKey","getAll","getAllKeys","count"],D=["put","add","delete","clear"],I=new Map;function y(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(I.get(t))return I.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=D.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!p.includes(n))return;const s=async function(e,...t){const s=this.transaction(e,o?"readwrite":"readonly");let i=s.store;return r&&(i=i.index(t.shift())),(await Promise.all([i[n](...t),o&&s.done]))[0]};return I.set(t,s),s}c((e=>({...e,get:(t,n,r)=>y(t,n)||e.get(t,n,r),has:(t,n)=>!!y(t,n)||e.has(t,n)})));const B=["continue","continuePrimaryKey","advance"],b={},g=new WeakMap,v=new WeakMap,h={get(e,t){if(!B.includes(t))return e[t];let n=b[t];return n||(n=b[t]=function(...e){g.set(this,v.get(this)[t](...e))}),n}};async function*m(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;const n=new Proxy(t,h);for(v.set(n,t),i.set(n,l(t));t;)yield n,t=await(g.get(n)||t.continue()),g.delete(n)}function w(e,n){return n===Symbol.asyncIterator&&t(e,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===n&&t(e,[IDBIndex,IDBObjectStore])}c((e=>({...e,get:(t,n,r)=>w(t,n)?m:e.get(t,n,r),has:(t,n)=>w(t,n)||e.has(t,n)}))),e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",(e=>t(e.oldVersion,e))),f(n).then((()=>{}))},e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o,terminated:s}={}){const i=indexedDB.open(e,t),a=f(i);return r&&i.addEventListener("upgradeneeded",(e=>{r(f(i.result),e.oldVersion,e.newVersion,f(i.transaction),e)})),n&&i.addEventListener("blocked",(e=>n(e.oldVersion,e.newVersion,e))),a.then((e=>{s&&e.addEventListener("close",(()=>s())),o&&e.addEventListener("versionchange",(e=>o(e.oldVersion,e.newVersion,e)))})).catch((()=>{})),a},e.unwrap=l,e.wrap=f}));
package/build/util.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export declare type Constructor = new (...args: any[]) => any;
2
- export declare type Func = (...args: any[]) => any;
1
+ export type Constructor = new (...args: any[]) => any;
2
+ export type Func = (...args: any[]) => any;
3
3
  export declare const instanceOfAny: (object: any, constructors: Constructor[]) => boolean;
@@ -1,5 +1,5 @@
1
- import { IDBPCursor, IDBPCursorWithValue, IDBPDatabase, IDBPIndex, IDBPObjectStore, IDBPTransaction } from './entry';
2
- export declare const reverseTransformCache: WeakMap<object, any>;
1
+ import { IDBPCursor, IDBPCursorWithValue, IDBPDatabase, IDBPIndex, IDBPObjectStore, IDBPTransaction } from './entry.js';
2
+ export declare const reverseTransformCache: WeakMap<WeakKey, any>;
3
3
  export declare function replaceTraps(callback: (currentTraps: ProxyHandler<any>) => ProxyHandler<any>): void;
4
4
  /**
5
5
  * Enhance an IDB object with helpers.
@@ -22,7 +22,7 @@ export declare function wrap<T>(value: IDBRequest<T>): Promise<T>;
22
22
  interface Unwrap {
23
23
  (value: IDBPCursorWithValue<any, any, any, any, any>): IDBCursorWithValue;
24
24
  (value: IDBPCursor<any, any, any, any, any>): IDBCursor;
25
- (value: IDBPDatabase): IDBDatabase;
25
+ (value: IDBPDatabase<any>): IDBDatabase;
26
26
  (value: IDBPIndex<any, any, any, any, any>): IDBIndex;
27
27
  (value: IDBPObjectStore<any, any, any, any>): IDBObjectStore;
28
28
  (value: IDBPTransaction<any, any, any>): IDBTransaction;
package/package.json CHANGED
@@ -1,21 +1,17 @@
1
1
  {
2
2
  "name": "@dumbmatter/idb",
3
- "version": "7.0.0",
3
+ "version": "8.0.3",
4
4
  "description": "A small wrapper that makes IndexedDB usable",
5
5
  "main": "./build/index.cjs",
6
6
  "module": "./build/index.js",
7
7
  "types": "./build/index.d.ts",
8
8
  "exports": {
9
9
  ".": {
10
+ "types": "./build/index.d.ts",
10
11
  "module": "./build/index.js",
11
12
  "import": "./build/index.js",
12
13
  "default": "./build/index.cjs"
13
14
  },
14
- "./with-async-ittr": {
15
- "module": "./with-async-ittr.js",
16
- "import": "./with-async-ittr.js",
17
- "default": "./with-async-ittr.cjs"
18
- },
19
15
  "./build/*": "./build/*",
20
16
  "./package.json": "./package.json"
21
17
  },
@@ -26,30 +22,35 @@
26
22
  ],
27
23
  "type": "module",
28
24
  "scripts": {
29
- "build": "PRODUCTION=1 rollup -c && node --experimental-modules lib/size-report.mjs",
25
+ "build": "cross-env PRODUCTION=1 rollup -c && node --experimental-modules lib/size-report.mjs",
30
26
  "dev": "rollup -c --watch",
31
27
  "prepack": "npm run build"
32
28
  },
33
29
  "repository": {
34
30
  "type": "git",
35
- "url": "git://github.com/jakearchibald/idb.git"
31
+ "url": "git://github.com/dumbmatter/idb.git"
36
32
  },
37
33
  "author": "Jake Archibald",
38
34
  "license": "ISC",
39
35
  "devDependencies": {
40
- "@rollup/plugin-commonjs": "^21.0.1",
41
- "@types/chai": "^4.2.22",
42
- "@types/mocha": "^9.0.0",
43
- "chai": "^4.3.4",
44
- "conditional-type-checks": "^1.0.5",
45
- "del": "^6.0.0",
46
- "filesize": "^8.0.6",
47
- "glob": "^7.2.0",
48
- "mocha": "^9.1.3",
49
- "prettier": "^2.4.1",
50
- "rollup": "^2.59.0",
51
- "rollup-plugin-node-resolve": "^5.2.0",
52
- "rollup-plugin-terser": "^7.0.2",
53
- "typescript": "^4.3.1-rc"
36
+ "@rollup/plugin-commonjs": "^28.0.3",
37
+ "@rollup/plugin-node-resolve": "^16.0.1",
38
+ "@rollup/plugin-terser": "^0.4.4",
39
+ "@rollup/plugin-typescript": "^12.1.2",
40
+ "@types/chai": "^5.2.2",
41
+ "@types/estree": "^1.0.7",
42
+ "@types/mocha": "^10.0.10",
43
+ "@types/node": "^22.15.14",
44
+ "chai": "^5.2.0",
45
+ "conditional-type-checks": "^1.0.6",
46
+ "cross-env": "^7.0.3",
47
+ "del": "^8.0.0",
48
+ "filesize": "^10.1.6",
49
+ "glob": "^11.0.2",
50
+ "mocha": "^11.2.2",
51
+ "prettier": "^3.5.3",
52
+ "rollup": "^4.40.2",
53
+ "tslib": "^2.8.1",
54
+ "typescript": "^5.8.3"
54
55
  }
55
56
  }
@@ -1,57 +0,0 @@
1
- 'use strict';
2
-
3
- var wrapIdbValue = require('./wrap-idb-value.cjs');
4
-
5
- const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];
6
- const methodMap = {};
7
- const advanceResults = new WeakMap();
8
- const ittrProxiedCursorToOriginalProxy = new WeakMap();
9
- const cursorIteratorTraps = {
10
- get(target, prop) {
11
- if (!advanceMethodProps.includes(prop))
12
- return target[prop];
13
- let cachedFunc = methodMap[prop];
14
- if (!cachedFunc) {
15
- cachedFunc = methodMap[prop] = function (...args) {
16
- advanceResults.set(this, ittrProxiedCursorToOriginalProxy.get(this)[prop](...args));
17
- };
18
- }
19
- return cachedFunc;
20
- },
21
- };
22
- async function* iterate(...args) {
23
- // tslint:disable-next-line:no-this-assignment
24
- let cursor = this;
25
- if (!(cursor instanceof IDBCursor)) {
26
- cursor = await cursor.openCursor(...args);
27
- }
28
- if (!cursor)
29
- return;
30
- cursor = cursor;
31
- const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);
32
- ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);
33
- // Map this double-proxy back to the original, so other cursor methods work.
34
- wrapIdbValue.reverseTransformCache.set(proxiedCursor, wrapIdbValue.unwrap(cursor));
35
- while (cursor) {
36
- yield proxiedCursor;
37
- // If one of the advancing methods was not called, call continue().
38
- cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());
39
- advanceResults.delete(proxiedCursor);
40
- }
41
- }
42
- function isIteratorProp(target, prop) {
43
- return ((prop === Symbol.asyncIterator &&
44
- wrapIdbValue.instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||
45
- (prop === 'iterate' && wrapIdbValue.instanceOfAny(target, [IDBIndex, IDBObjectStore])));
46
- }
47
- wrapIdbValue.replaceTraps((oldTraps) => ({
48
- ...oldTraps,
49
- get(target, prop, receiver) {
50
- if (isIteratorProp(target, prop))
51
- return iterate;
52
- return oldTraps.get(target, prop, receiver);
53
- },
54
- has(target, prop) {
55
- return isIteratorProp(target, prop) || oldTraps.has(target, prop);
56
- },
57
- }));
@@ -1,55 +0,0 @@
1
- import { r as replaceTraps, i as instanceOfAny, a as reverseTransformCache, u as unwrap } from './wrap-idb-value.js';
2
-
3
- const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];
4
- const methodMap = {};
5
- const advanceResults = new WeakMap();
6
- const ittrProxiedCursorToOriginalProxy = new WeakMap();
7
- const cursorIteratorTraps = {
8
- get(target, prop) {
9
- if (!advanceMethodProps.includes(prop))
10
- return target[prop];
11
- let cachedFunc = methodMap[prop];
12
- if (!cachedFunc) {
13
- cachedFunc = methodMap[prop] = function (...args) {
14
- advanceResults.set(this, ittrProxiedCursorToOriginalProxy.get(this)[prop](...args));
15
- };
16
- }
17
- return cachedFunc;
18
- },
19
- };
20
- async function* iterate(...args) {
21
- // tslint:disable-next-line:no-this-assignment
22
- let cursor = this;
23
- if (!(cursor instanceof IDBCursor)) {
24
- cursor = await cursor.openCursor(...args);
25
- }
26
- if (!cursor)
27
- return;
28
- cursor = cursor;
29
- const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);
30
- ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);
31
- // Map this double-proxy back to the original, so other cursor methods work.
32
- reverseTransformCache.set(proxiedCursor, unwrap(cursor));
33
- while (cursor) {
34
- yield proxiedCursor;
35
- // If one of the advancing methods was not called, call continue().
36
- cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());
37
- advanceResults.delete(proxiedCursor);
38
- }
39
- }
40
- function isIteratorProp(target, prop) {
41
- return ((prop === Symbol.asyncIterator &&
42
- instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor])) ||
43
- (prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore])));
44
- }
45
- replaceTraps((oldTraps) => ({
46
- ...oldTraps,
47
- get(target, prop, receiver) {
48
- if (isIteratorProp(target, prop))
49
- return iterate;
50
- return oldTraps.get(target, prop, receiver);
51
- },
52
- has(target, prop) {
53
- return isIteratorProp(target, prop) || oldTraps.has(target, prop);
54
- },
55
- }));
@@ -1 +0,0 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).idb={})}(this,(function(e){"use strict";const t=(e,t)=>t.some((t=>e instanceof t));let n,r;const o=new WeakMap,s=new WeakMap,i=new WeakMap,a=new WeakMap,c=new WeakMap;let u={get(e,t,n){if(e instanceof IDBTransaction){if("done"===t)return s.get(e);if("objectStoreNames"===t)return e.objectStoreNames||i.get(e);if("store"===t)return n.objectStoreNames[1]?void 0:n.objectStore(n.objectStoreNames[0])}return p(e[t])},set:(e,t,n)=>(e[t]=n,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function d(e){u=e(u)}function f(e){return e!==IDBDatabase.prototype.transaction||"objectStoreNames"in IDBTransaction.prototype?(r||(r=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(D(this),t),p(o.get(this))}:function(...t){return p(e.apply(D(this),t))}:function(t,...n){const r=e.call(D(this),t,...n);return i.set(r,t.sort?t.sort():[t]),p(r)}}function l(e){return"function"==typeof e?f(e):(e instanceof IDBTransaction&&function(e){if(s.has(e))return;const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("complete",o),e.removeEventListener("error",s),e.removeEventListener("abort",s)},o=()=>{t(),r()},s=()=>{n(e.error||new DOMException("AbortError","AbortError")),r()};e.addEventListener("complete",o),e.addEventListener("error",s),e.addEventListener("abort",s)}));s.set(e,t)}(e),t(e,n||(n=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(e,u):e)}function p(e){if(e instanceof IDBRequest)return function(e){const t=new Promise(((t,n)=>{const r=()=>{e.removeEventListener("success",o),e.removeEventListener("error",s)},o=()=>{t(p(e.result)),r()},s=()=>{n(e.error),r()};e.addEventListener("success",o),e.addEventListener("error",s)}));return t.then((t=>{t instanceof IDBCursor&&o.set(t,e)})).catch((()=>{})),c.set(t,e),t}(e);if(a.has(e))return a.get(e);const t=l(e);return t!==e&&(a.set(e,t),c.set(t,e)),t}const D=e=>c.get(e);const I=["get","getKey","getAll","getAllKeys","count"],b=["put","add","delete","clear"],y=new Map;function B(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(y.get(t))return y.get(t);const n=t.replace(/FromIndex$/,""),r=t!==n,o=b.includes(n);if(!(n in(r?IDBIndex:IDBObjectStore).prototype)||!o&&!I.includes(n))return;const s=async function(e,...t){const s=this.transaction(e,o?"readwrite":"readonly");let i=s.store;return r&&(i=i.index(t.shift())),(await Promise.all([i[n](...t),o&&s.done]))[0]};return y.set(t,s),s}d((e=>({...e,get:(t,n,r)=>B(t,n)||e.get(t,n,r),has:(t,n)=>!!B(t,n)||e.has(t,n)})));const g=["continue","continuePrimaryKey","advance"],h={},v=new WeakMap,m=new WeakMap,w={get(e,t){if(!g.includes(t))return e[t];let n=h[t];return n||(n=h[t]=function(...e){v.set(this,m.get(this)[t](...e))}),n}};async function*E(...e){let t=this;if(t instanceof IDBCursor||(t=await t.openCursor(...e)),!t)return;t=t;const n=new Proxy(t,w);for(m.set(n,t),c.set(n,D(t));t;)yield n,t=await(v.get(n)||t.continue()),v.delete(n)}function L(e,n){return n===Symbol.asyncIterator&&t(e,[IDBIndex,IDBObjectStore,IDBCursor])||"iterate"===n&&t(e,[IDBIndex,IDBObjectStore])}d((e=>({...e,get:(t,n,r)=>L(t,n)?E:e.get(t,n,r),has:(t,n)=>L(t,n)||e.has(t,n)}))),e.deleteDB=function(e,{blocked:t}={}){const n=indexedDB.deleteDatabase(e);return t&&n.addEventListener("blocked",(()=>t())),p(n).then((()=>{}))},e.openDB=function(e,t,{blocked:n,upgrade:r,blocking:o,terminated:s}={}){const i=indexedDB.open(e,t),a=p(i);return r&&i.addEventListener("upgradeneeded",(e=>{r(p(i.result),e.oldVersion,e.newVersion,p(i.transaction))})),n&&i.addEventListener("blocked",(()=>n())),a.then((e=>{s&&e.addEventListener("close",(()=>s())),o&&e.addEventListener("versionchange",(()=>o()))})).catch((()=>{})),a},e.unwrap=D,e.wrap=p}));