@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.
@@ -1,191 +0,0 @@
1
- 'use strict';
2
-
3
- const instanceOfAny = (object, constructors) => constructors.some((c) => object instanceof c);
4
-
5
- let idbProxyableTypes;
6
- let cursorAdvanceMethods;
7
- // This is a function to prevent it throwing up in node environments.
8
- function getIdbProxyableTypes() {
9
- return (idbProxyableTypes ||
10
- (idbProxyableTypes = [
11
- IDBDatabase,
12
- IDBObjectStore,
13
- IDBIndex,
14
- IDBCursor,
15
- IDBTransaction,
16
- ]));
17
- }
18
- // This is a function to prevent it throwing up in node environments.
19
- function getCursorAdvanceMethods() {
20
- return (cursorAdvanceMethods ||
21
- (cursorAdvanceMethods = [
22
- IDBCursor.prototype.advance,
23
- IDBCursor.prototype.continue,
24
- IDBCursor.prototype.continuePrimaryKey,
25
- ]));
26
- }
27
- const cursorRequestMap = new WeakMap();
28
- const transactionDoneMap = new WeakMap();
29
- const transactionStoreNamesMap = new WeakMap();
30
- const transformCache = new WeakMap();
31
- const reverseTransformCache = new WeakMap();
32
- function promisifyRequest(request) {
33
- const promise = new Promise((resolve, reject) => {
34
- const unlisten = () => {
35
- request.removeEventListener('success', success);
36
- request.removeEventListener('error', error);
37
- };
38
- const success = () => {
39
- resolve(wrap(request.result));
40
- unlisten();
41
- };
42
- const error = () => {
43
- reject(request.error);
44
- unlisten();
45
- };
46
- request.addEventListener('success', success);
47
- request.addEventListener('error', error);
48
- });
49
- promise
50
- .then((value) => {
51
- // Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval
52
- // (see wrapFunction).
53
- if (value instanceof IDBCursor) {
54
- cursorRequestMap.set(value, request);
55
- }
56
- // Catching to avoid "Uncaught Promise exceptions"
57
- })
58
- .catch(() => { });
59
- // This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
60
- // is because we create many promises from a single IDBRequest.
61
- reverseTransformCache.set(promise, request);
62
- return promise;
63
- }
64
- function cacheDonePromiseForTransaction(tx) {
65
- // Early bail if we've already created a done promise for this transaction.
66
- if (transactionDoneMap.has(tx))
67
- return;
68
- const done = new Promise((resolve, reject) => {
69
- const unlisten = () => {
70
- tx.removeEventListener('complete', complete);
71
- tx.removeEventListener('error', error);
72
- tx.removeEventListener('abort', error);
73
- };
74
- const complete = () => {
75
- resolve();
76
- unlisten();
77
- };
78
- const error = () => {
79
- reject(tx.error || new DOMException('AbortError', 'AbortError'));
80
- unlisten();
81
- };
82
- tx.addEventListener('complete', complete);
83
- tx.addEventListener('error', error);
84
- tx.addEventListener('abort', error);
85
- });
86
- // Cache it for later retrieval.
87
- transactionDoneMap.set(tx, done);
88
- }
89
- let idbProxyTraps = {
90
- get(target, prop, receiver) {
91
- if (target instanceof IDBTransaction) {
92
- // Special handling for transaction.done.
93
- if (prop === 'done')
94
- return transactionDoneMap.get(target);
95
- // Polyfill for objectStoreNames because of Edge.
96
- if (prop === 'objectStoreNames') {
97
- return target.objectStoreNames || transactionStoreNamesMap.get(target);
98
- }
99
- // Make tx.store return the only store in the transaction, or undefined if there are many.
100
- if (prop === 'store') {
101
- return receiver.objectStoreNames[1]
102
- ? undefined
103
- : receiver.objectStore(receiver.objectStoreNames[0]);
104
- }
105
- }
106
- // Else transform whatever we get back.
107
- return wrap(target[prop]);
108
- },
109
- set(target, prop, value) {
110
- target[prop] = value;
111
- return true;
112
- },
113
- has(target, prop) {
114
- if (target instanceof IDBTransaction &&
115
- (prop === 'done' || prop === 'store')) {
116
- return true;
117
- }
118
- return prop in target;
119
- },
120
- };
121
- function replaceTraps(callback) {
122
- idbProxyTraps = callback(idbProxyTraps);
123
- }
124
- function wrapFunction(func) {
125
- // Due to expected object equality (which is enforced by the caching in `wrap`), we
126
- // only create one new func per func.
127
- // Edge doesn't support objectStoreNames (booo), so we polyfill it here.
128
- if (func === IDBDatabase.prototype.transaction &&
129
- !('objectStoreNames' in IDBTransaction.prototype)) {
130
- return function (storeNames, ...args) {
131
- const tx = func.call(unwrap(this), storeNames, ...args);
132
- transactionStoreNamesMap.set(tx, storeNames.sort ? storeNames.sort() : [storeNames]);
133
- return wrap(tx);
134
- };
135
- }
136
- // Cursor methods are special, as the behaviour is a little more different to standard IDB. In
137
- // IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
138
- // cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
139
- // with real promises, so each advance methods returns a new promise for the cursor object, or
140
- // undefined if the end of the cursor has been reached.
141
- if (getCursorAdvanceMethods().includes(func)) {
142
- return function (...args) {
143
- // Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
144
- // the original object.
145
- func.apply(unwrap(this), args);
146
- return wrap(cursorRequestMap.get(this));
147
- };
148
- }
149
- return function (...args) {
150
- // Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
151
- // the original object.
152
- return wrap(func.apply(unwrap(this), args));
153
- };
154
- }
155
- function transformCachableValue(value) {
156
- if (typeof value === 'function')
157
- return wrapFunction(value);
158
- // This doesn't return, it just creates a 'done' promise for the transaction,
159
- // which is later returned for transaction.done (see idbObjectHandler).
160
- if (value instanceof IDBTransaction)
161
- cacheDonePromiseForTransaction(value);
162
- if (instanceOfAny(value, getIdbProxyableTypes()))
163
- return new Proxy(value, idbProxyTraps);
164
- // Return the same value back if we're not going to transform it.
165
- return value;
166
- }
167
- function wrap(value) {
168
- // We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
169
- // IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
170
- if (value instanceof IDBRequest)
171
- return promisifyRequest(value);
172
- // If we've already transformed this value before, reuse the transformed value.
173
- // This is faster, but it also provides object equality.
174
- if (transformCache.has(value))
175
- return transformCache.get(value);
176
- const newValue = transformCachableValue(value);
177
- // Not all types are transformed.
178
- // These may be primitive types, so they can't be WeakMap keys.
179
- if (newValue !== value) {
180
- transformCache.set(value, newValue);
181
- reverseTransformCache.set(newValue, value);
182
- }
183
- return newValue;
184
- }
185
- const unwrap = (value) => reverseTransformCache.get(value);
186
-
187
- exports.instanceOfAny = instanceOfAny;
188
- exports.replaceTraps = replaceTraps;
189
- exports.reverseTransformCache = reverseTransformCache;
190
- exports.unwrap = unwrap;
191
- exports.wrap = wrap;
@@ -1,185 +0,0 @@
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 cursorRequestMap = new WeakMap();
26
- const transactionDoneMap = new WeakMap();
27
- const transactionStoreNamesMap = new WeakMap();
28
- const transformCache = new WeakMap();
29
- const reverseTransformCache = new WeakMap();
30
- function promisifyRequest(request) {
31
- const promise = new Promise((resolve, reject) => {
32
- const unlisten = () => {
33
- request.removeEventListener('success', success);
34
- request.removeEventListener('error', error);
35
- };
36
- const success = () => {
37
- resolve(wrap(request.result));
38
- unlisten();
39
- };
40
- const error = () => {
41
- reject(request.error);
42
- unlisten();
43
- };
44
- request.addEventListener('success', success);
45
- request.addEventListener('error', error);
46
- });
47
- promise
48
- .then((value) => {
49
- // Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval
50
- // (see wrapFunction).
51
- if (value instanceof IDBCursor) {
52
- cursorRequestMap.set(value, request);
53
- }
54
- // Catching to avoid "Uncaught Promise exceptions"
55
- })
56
- .catch(() => { });
57
- // This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
58
- // is because we create many promises from a single IDBRequest.
59
- reverseTransformCache.set(promise, request);
60
- return promise;
61
- }
62
- function cacheDonePromiseForTransaction(tx) {
63
- // Early bail if we've already created a done promise for this transaction.
64
- if (transactionDoneMap.has(tx))
65
- return;
66
- const done = new Promise((resolve, reject) => {
67
- const unlisten = () => {
68
- tx.removeEventListener('complete', complete);
69
- tx.removeEventListener('error', error);
70
- tx.removeEventListener('abort', error);
71
- };
72
- const complete = () => {
73
- resolve();
74
- unlisten();
75
- };
76
- const error = () => {
77
- reject(tx.error || new DOMException('AbortError', 'AbortError'));
78
- unlisten();
79
- };
80
- tx.addEventListener('complete', complete);
81
- tx.addEventListener('error', error);
82
- tx.addEventListener('abort', error);
83
- });
84
- // Cache it for later retrieval.
85
- transactionDoneMap.set(tx, done);
86
- }
87
- let idbProxyTraps = {
88
- get(target, prop, receiver) {
89
- if (target instanceof IDBTransaction) {
90
- // Special handling for transaction.done.
91
- if (prop === 'done')
92
- return transactionDoneMap.get(target);
93
- // Polyfill for objectStoreNames because of Edge.
94
- if (prop === 'objectStoreNames') {
95
- return target.objectStoreNames || transactionStoreNamesMap.get(target);
96
- }
97
- // Make tx.store return the only store in the transaction, or undefined if there are many.
98
- if (prop === 'store') {
99
- return receiver.objectStoreNames[1]
100
- ? undefined
101
- : receiver.objectStore(receiver.objectStoreNames[0]);
102
- }
103
- }
104
- // Else transform whatever we get back.
105
- return wrap(target[prop]);
106
- },
107
- set(target, prop, value) {
108
- target[prop] = value;
109
- return true;
110
- },
111
- has(target, prop) {
112
- if (target instanceof IDBTransaction &&
113
- (prop === 'done' || prop === 'store')) {
114
- return true;
115
- }
116
- return prop in target;
117
- },
118
- };
119
- function replaceTraps(callback) {
120
- idbProxyTraps = callback(idbProxyTraps);
121
- }
122
- function wrapFunction(func) {
123
- // Due to expected object equality (which is enforced by the caching in `wrap`), we
124
- // only create one new func per func.
125
- // Edge doesn't support objectStoreNames (booo), so we polyfill it here.
126
- if (func === IDBDatabase.prototype.transaction &&
127
- !('objectStoreNames' in IDBTransaction.prototype)) {
128
- return function (storeNames, ...args) {
129
- const tx = func.call(unwrap(this), storeNames, ...args);
130
- transactionStoreNamesMap.set(tx, storeNames.sort ? storeNames.sort() : [storeNames]);
131
- return wrap(tx);
132
- };
133
- }
134
- // Cursor methods are special, as the behaviour is a little more different to standard IDB. In
135
- // IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
136
- // cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
137
- // with real promises, so each advance methods returns a new promise for the cursor object, or
138
- // undefined if the end of the cursor has been reached.
139
- if (getCursorAdvanceMethods().includes(func)) {
140
- return function (...args) {
141
- // Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
142
- // the original object.
143
- func.apply(unwrap(this), args);
144
- return wrap(cursorRequestMap.get(this));
145
- };
146
- }
147
- return function (...args) {
148
- // Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
149
- // the original object.
150
- return wrap(func.apply(unwrap(this), args));
151
- };
152
- }
153
- function transformCachableValue(value) {
154
- if (typeof value === 'function')
155
- return wrapFunction(value);
156
- // This doesn't return, it just creates a 'done' promise for the transaction,
157
- // which is later returned for transaction.done (see idbObjectHandler).
158
- if (value instanceof IDBTransaction)
159
- cacheDonePromiseForTransaction(value);
160
- if (instanceOfAny(value, getIdbProxyableTypes()))
161
- return new Proxy(value, idbProxyTraps);
162
- // Return the same value back if we're not going to transform it.
163
- return value;
164
- }
165
- function wrap(value) {
166
- // We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
167
- // IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
168
- if (value instanceof IDBRequest)
169
- return promisifyRequest(value);
170
- // If we've already transformed this value before, reuse the transformed value.
171
- // This is faster, but it also provides object equality.
172
- if (transformCache.has(value))
173
- return transformCache.get(value);
174
- const newValue = transformCachableValue(value);
175
- // Not all types are transformed.
176
- // These may be primitive types, so they can't be WeakMap keys.
177
- if (newValue !== value) {
178
- transformCache.set(value, newValue);
179
- reverseTransformCache.set(newValue, value);
180
- }
181
- return newValue;
182
- }
183
- const unwrap = (value) => reverseTransformCache.get(value);
184
-
185
- export { reverseTransformCache as a, instanceOfAny as i, replaceTraps as r, unwrap as u, wrap as w };
@@ -1,2 +0,0 @@
1
- module.exports = require('./build/cjs/index.js');
2
- require('./build/cjs/async-iterators.js');
@@ -1 +0,0 @@
1
- export * from './build';
@@ -1,2 +0,0 @@
1
- export * from './build/index.js';
2
- import './build/async-iterators.js';