|
1 | | -var idb=function(e){"use strict";let t,n;const r=new WeakMap,o=new WeakMap,s=new WeakMap,a=new WeakMap,i=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 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 u(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(f(this),t),p(r.get(this))}:function(...t){return p(e.apply(f(this),t))}:function(t,...n){const r=e.call(f(this),t,...n);return s.set(r,t.sort?t.sort():[t]),p(r)}}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),n=e,(t||(t=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction])).some((e=>n instanceof e))?new Proxy(e,c):e);var n}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&&r.set(t,e)})).catch((()=>{})),i.set(t,e),t}(e);if(a.has(e))return a.get(e);const t=d(e);return t!==e&&(a.set(e,t),i.set(t,e)),t}const f=e=>i.get(e);const l=["get","getKey","getAll","getAllKeys","count"],D=["put","add","delete","clear"],v=new Map;function b(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(v.get(t))return v.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 a=s.store;return r&&(a=a.index(t.shift())),(await Promise.all([a[n](...t),o&&s.done]))[0]};return v.set(t,s),s}return c=(e=>({...e,get:(t,n,r)=>b(t,n)||e.get(t,n,r),has:(t,n)=>!!b(t,n)||e.has(t,n)}))(c),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 a=indexedDB.open(e,t),i=p(a);return r&&a.addEventListener("upgradeneeded",(e=>{r(p(a.result),e.oldVersion,e.newVersion,p(a.transaction))})),n&&a.addEventListener("blocked",(()=>n())),i.then((e=>{s&&e.addEventListener("close",(()=>s())),o&&e.addEventListener("versionchange",(()=>o()))})).catch((()=>{})),i},e.unwrap=f,e.wrap=p,e}({}); |
| 1 | +const idb = function (e) { |
| 2 | + "use strict"; |
| 3 | + let t, n; |
| 4 | + const r = new WeakMap, o = new WeakMap, s = new WeakMap, a = new WeakMap, i = new WeakMap; |
| 5 | + let c = { |
| 6 | + get(e, t, n) { |
| 7 | + if (e instanceof IDBTransaction) { |
| 8 | + if ("done" === t) return o.get(e); |
| 9 | + if ("objectStoreNames" === t) return e.objectStoreNames || s.get(e); |
| 10 | + if ("store" === t) return n.objectStoreNames[1] ? void 0 : n.objectStore(n.objectStoreNames[0]) |
| 11 | + } |
| 12 | + return p(e[t]) |
| 13 | + }, |
| 14 | + set: (e, t, n) => (e[t] = n, !0), |
| 15 | + has: (e, t) => e instanceof IDBTransaction && ("done" === t || "store" === t) || t in e |
| 16 | + }; |
| 17 | + |
| 18 | + function u(e) { |
| 19 | + return e !== IDBDatabase.prototype.transaction || "objectStoreNames" in IDBTransaction.prototype ? (n || (n = [IDBCursor.prototype.advance, IDBCursor.prototype.continue, IDBCursor.prototype.continuePrimaryKey])).includes(e) ? function (...t) { |
| 20 | + return e.apply(f(this), t), p(r.get(this)) |
| 21 | + } : function (...t) { |
| 22 | + return p(e.apply(f(this), t)) |
| 23 | + } : function (t, ...n) { |
| 24 | + const r = e.call(f(this), t, ...n); |
| 25 | + return s.set(r, t.sort ? t.sort() : [t]), p(r) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + function d(e) { |
| 30 | + return "function" == typeof e ? u(e) : (e instanceof IDBTransaction && function (e) { |
| 31 | + if (o.has(e)) return; |
| 32 | + const t = new Promise(((t, n) => { |
| 33 | + const r = () => { |
| 34 | + e.removeEventListener("complete", o), e.removeEventListener("error", s), e.removeEventListener("abort", s) |
| 35 | + }, o = () => { |
| 36 | + t(), r() |
| 37 | + }, s = () => { |
| 38 | + n(e.error || new DOMException("AbortError", "AbortError")), r() |
| 39 | + }; |
| 40 | + e.addEventListener("complete", o), e.addEventListener("error", s), e.addEventListener("abort", s) |
| 41 | + })); |
| 42 | + o.set(e, t) |
| 43 | + }(e), n = e, (t || (t = [IDBDatabase, IDBObjectStore, IDBIndex, IDBCursor, IDBTransaction])).some((e => n instanceof e)) ? new Proxy(e, c) : e); |
| 44 | + var n |
| 45 | + } |
| 46 | + |
| 47 | + function p(e) { |
| 48 | + if (e instanceof IDBRequest) return function (e) { |
| 49 | + const t = new Promise(((t, n) => { |
| 50 | + const r = () => { |
| 51 | + e.removeEventListener("success", o), e.removeEventListener("error", s) |
| 52 | + }, o = () => { |
| 53 | + t(p(e.result)), r() |
| 54 | + }, s = () => { |
| 55 | + n(e.error), r() |
| 56 | + }; |
| 57 | + e.addEventListener("success", o), e.addEventListener("error", s) |
| 58 | + })); |
| 59 | + return t.then((t => { |
| 60 | + t instanceof IDBCursor && r.set(t, e) |
| 61 | + })).catch((() => { |
| 62 | + })), i.set(t, e), t |
| 63 | + }(e); |
| 64 | + if (a.has(e)) return a.get(e); |
| 65 | + const t = d(e); |
| 66 | + return t !== e && (a.set(e, t), i.set(t, e)), t |
| 67 | + } |
| 68 | + |
| 69 | + const f = e => i.get(e); |
| 70 | + const l = ["get", "getKey", "getAll", "getAllKeys", "count"], D = ["put", "add", "delete", "clear"], v = new Map; |
| 71 | + |
| 72 | + function b(e, t) { |
| 73 | + if (!(e instanceof IDBDatabase) || t in e || "string" != typeof t) return; |
| 74 | + if (v.get(t)) return v.get(t); |
| 75 | + const n = t.replace(/FromIndex$/, ""), r = t !== n, o = D.includes(n); |
| 76 | + if (!(n in (r ? IDBIndex : IDBObjectStore).prototype) || !o && !l.includes(n)) return; |
| 77 | + const s = async function (e, ...t) { |
| 78 | + const s = this.transaction(e, o ? "readwrite" : "readonly"); |
| 79 | + let a = s.store; |
| 80 | + return r && (a = a.index(t.shift())), (await Promise.all([a[n](...t), o && s.done]))[0] |
| 81 | + }; |
| 82 | + return v.set(t, s), s |
| 83 | + } |
| 84 | + |
| 85 | + return c = (e => ({ |
| 86 | + ...e, |
| 87 | + get: (t, n, r) => b(t, n) || e.get(t, n, r), |
| 88 | + has: (t, n) => !!b(t, n) || e.has(t, n) |
| 89 | + }))(c), e.deleteDB = function (e, {blocked: t} = {}) { |
| 90 | + const n = indexedDB.deleteDatabase(e); |
| 91 | + return t && n.addEventListener("blocked", (() => t())), p(n).then((() => { |
| 92 | + })) |
| 93 | + }, e.openDB = function (e, t, {blocked: n, upgrade: r, blocking: o, terminated: s} = {}) { |
| 94 | + const a = indexedDB.open(e, t), i = p(a); |
| 95 | + return r && a.addEventListener("upgradeneeded", (e => { |
| 96 | + r(p(a.result), e.oldVersion, e.newVersion, p(a.transaction)) |
| 97 | + })), n && a.addEventListener("blocked", (() => n())), i.then((e => { |
| 98 | + s && e.addEventListener("close", (() => s())), o && e.addEventListener("versionchange", (() => o())) |
| 99 | + })).catch((() => { |
| 100 | + })), i |
| 101 | + }, e.unwrap = f, e.wrap = p, e |
| 102 | +}({}); |
| 103 | + |
| 104 | +module.exports ={ |
| 105 | + idb |
| 106 | +}; |
0 commit comments