idb-kv-store
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -1,1 +0,1 @@ | ||
(function(e){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=e()}else if(typeof define==="function"&&define.amd){define([],e)}else{var t;if(typeof window!=="undefined"){t=window}else if(typeof global!=="undefined"){t=global}else if(typeof self!=="undefined"){t=self}else{t=this}t.IdbKvStore=e()}})(function(){var e,t,n;return function e(t,n,r){function i(s,u){if(!n[s]){if(!t[s]){var a=typeof require=="function"&&require;if(!u&&a)return a(s,!0);if(o)return o(s,!0);var l=new Error("Cannot find module '"+s+"'");throw l.code="MODULE_NOT_FOUND",l}var f=n[s]={exports:{}};t[s][0].call(f.exports,function(e){var n=t[s][1][e];return i(n?n:e)},f,f.exports,e,t,n,r)}return n[s].exports}var o=typeof require=="function"&&require;for(var s=0;s<r.length;s++)i(r[s]);return i}({1:[function(e,t,n){function r(){this._events=this._events||{};this._maxListeners=this._maxListeners||undefined}t.exports=r;r.EventEmitter=r;r.prototype._events=undefined;r.prototype._maxListeners=undefined;r.defaultMaxListeners=10;r.prototype.setMaxListeners=function(e){if(!o(e)||e<0||isNaN(e))throw TypeError("n must be a positive number");this._maxListeners=e;return this};r.prototype.emit=function(e){var t,n,r,o,a,l;if(!this._events)this._events={};if(e==="error"){if(!this._events.error||s(this._events.error)&&!this._events.error.length){t=arguments[1];if(t instanceof Error){throw t}else{var f=new Error('Uncaught, unspecified "error" event. ('+t+")");f.context=t;throw f}}}n=this._events[e];if(u(n))return false;if(i(n)){switch(arguments.length){case 1:n.call(this);break;case 2:n.call(this,arguments[1]);break;case 3:n.call(this,arguments[1],arguments[2]);break;default:o=Array.prototype.slice.call(arguments,1);n.apply(this,o)}}else if(s(n)){o=Array.prototype.slice.call(arguments,1);l=n.slice();r=l.length;for(a=0;a<r;a++)l[a].apply(this,o)}return true};r.prototype.addListener=function(e,t){var n;if(!i(t))throw TypeError("listener must be a function");if(!this._events)this._events={};if(this._events.newListener)this.emit("newListener",e,i(t.listener)?t.listener:t);if(!this._events[e])this._events[e]=t;else if(s(this._events[e]))this._events[e].push(t);else this._events[e]=[this._events[e],t];if(s(this._events[e])&&!this._events[e].warned){if(!u(this._maxListeners)){n=this._maxListeners}else{n=r.defaultMaxListeners}if(n&&n>0&&this._events[e].length>n){this._events[e].warned=true;console.error("(node) warning: possible EventEmitter memory "+"leak detected. %d listeners added. "+"Use emitter.setMaxListeners() to increase limit.",this._events[e].length);if(typeof console.trace==="function"){console.trace()}}}return this};r.prototype.on=r.prototype.addListener;r.prototype.once=function(e,t){if(!i(t))throw TypeError("listener must be a function");var n=false;function r(){this.removeListener(e,r);if(!n){n=true;t.apply(this,arguments)}}r.listener=t;this.on(e,r);return this};r.prototype.removeListener=function(e,t){var n,r,o,u;if(!i(t))throw TypeError("listener must be a function");if(!this._events||!this._events[e])return this;n=this._events[e];o=n.length;r=-1;if(n===t||i(n.listener)&&n.listener===t){delete this._events[e];if(this._events.removeListener)this.emit("removeListener",e,t)}else if(s(n)){for(u=o;u-- >0;){if(n[u]===t||n[u].listener&&n[u].listener===t){r=u;break}}if(r<0)return this;if(n.length===1){n.length=0;delete this._events[e]}else{n.splice(r,1)}if(this._events.removeListener)this.emit("removeListener",e,t)}return this};r.prototype.removeAllListeners=function(e){var t,n;if(!this._events)return this;if(!this._events.removeListener){if(arguments.length===0)this._events={};else if(this._events[e])delete this._events[e];return this}if(arguments.length===0){for(t in this._events){if(t==="removeListener")continue;this.removeAllListeners(t)}this.removeAllListeners("removeListener");this._events={};return this}n=this._events[e];if(i(n)){this.removeListener(e,n)}else if(n){while(n.length)this.removeListener(e,n[n.length-1])}delete this._events[e];return this};r.prototype.listeners=function(e){var t;if(!this._events||!this._events[e])t=[];else if(i(this._events[e]))t=[this._events[e]];else t=this._events[e].slice();return t};r.prototype.listenerCount=function(e){if(this._events){var t=this._events[e];if(i(t))return 1;else if(t)return t.length}return 0};r.listenerCount=function(e,t){return e.listenerCount(t)};function i(e){return typeof e==="function"}function o(e){return typeof e==="number"}function s(e){return typeof e==="object"&&e!==null}function u(e){return e===void 0}},{}],2:[function(e,t,n){if(typeof Object.create==="function"){t.exports=function e(t,n){t.super_=n;t.prototype=Object.create(n.prototype,{constructor:{value:t,enumerable:false,writable:true,configurable:true}})}}else{t.exports=function e(t,n){t.super_=n;var r=function(){};r.prototype=n.prototype;t.prototype=new r;t.prototype.constructor=t}}},{}],"/":[function(e,t,n){t.exports=u;var r=e("events").EventEmitter;var i=e("inherits");var o=typeof window==="undefined"?self:window;var s=o.indexedDB||o.mozIndexedDB||o.webkitIndexedDB||o.msIndexedDB;u.INDEXEDDB_SUPPORT=s!=null;u.BROADCAST_SUPPORT=o.BroadcastChannel!=null;i(u,r);function u(e,t,n){var i=this;if(typeof e!=="string")throw new Error("A name must be supplied of type string");if(!s)throw new Error("IndexedDB not supported");if(typeof t==="function")return new u(e,null,t);if(!(i instanceof u))return new u(e,t,n);if(!t)t={};r.call(i);i._db=null;i._closed=false;i._channel=null;i._waiters=[];var a=t.channel||o.BroadcastChannel;if(a){i._channel=new a(e);i._channel.onmessage=y}var f=s.open(e);f.onerror=c;f.onsuccess=d;f.onupgradeneeded=p;i.on("newListener",_);function c(e){l(e);i._close(e.target.error);if(n)n(e.target.error)}function h(e){l(e);i._close(e.target.error)}function d(e){if(i._closed){e.target.result.close()}else{i._db=e.target.result;i._db.onclose=v;i._db.onerror=h;for(var t in i._waiters)i._waiters[t]._init(null);i._waiters=null;if(n)n(null);i.emit("open")}}function p(e){var t=e.target.result;t.createObjectStore("kv",{autoIncrement:true})}function v(){i._close()}function _(e){if(e!=="add"&&e!=="set"&&e!=="remove")return;if(!i._channel)return i.emit("error",new Error("No BroadcastChannel support"))}function y(e){if(e.data.method==="add")i.emit("add",e.data);else if(e.data.method==="set")i.emit("set",e.data);else if(e.data.method==="remove")i.emit("remove",e.data)}}u.prototype.get=function(e,t){return this.transaction("readonly").get(e,t)};u.prototype.set=function(e,t,n){return this.transaction("readwrite").set(e,t,n)};u.prototype.json=function(e){return this.transaction("readonly").json(e)};u.prototype.keys=function(e){return this.transaction("readonly").keys(e)};u.prototype.values=function(e){return this.transaction("readonly").values(e)};u.prototype.remove=function(e,t){return this.transaction("readwrite").remove(e,t)};u.prototype.clear=function(e){return this.transaction("readwrite").clear(e)};u.prototype.count=function(e){return this.transaction("readonly").count(e)};u.prototype.add=function(e,t,n){return this.transaction("readwrite").add(e,t,n)};u.prototype.iterator=function(e){return this.transaction("readonly").iterator(e)};u.prototype.transaction=function(e){if(this._closed)throw new Error("Database is closed");var t=new a(this,e);if(this._db)t._init(null);else this._waiters.push(t);return t};u.prototype.close=function(){this._close()};u.prototype._close=function(e){if(this._closed)return;this._closed=true;if(this._db)this._db.close();if(this._channel)this._channel.close();this._db=null;this._channel=null;if(e)this.emit("error",e);this.emit("close");for(var t in this._waiters)this._waiters[t]._init(e||new Error("Database is closed"));this._waiters=null;this.removeAllListeners()};function a(e,t){this._kvStore=e;this._mode=t||"readwrite";this._objectStore=null;this._waiters=null;this.finished=false;this.onfinish=null;if(this._mode!=="readonly"&&this._mode!=="readwrite"){throw new Error('mode must be either "readonly" or "readwrite"')}}a.prototype._init=function(e){var t=this;if(t.finished)return;if(e)return t._close(e);var n=t._kvStore._db.transaction("kv",t._mode);n.oncomplete=i;n.onerror=o;t._objectStore=n.objectStore("kv");for(var r in t._waiters)t._waiters[r](null,t._objectStore);t._waiters=null;function i(){t._close(null)}function o(e){l(e);t._close(e.target.error)}};a.prototype._getObjectStore=function(e){if(this.finished)throw new Error("Transaction is finished");if(this._objectStore)return e(null,this._objectStore);this._waiters=this._waiters||[];this._waiters.push(e)};a.prototype.set=f(function(e,t,n){var r=this;if(e==null||t==null)throw new Error("A key and value must be given");r._getObjectStore(function(i,o){if(i)return n(i);try{var s=o.put(t,e)}catch(e){return n(e)}s.onerror=l.bind(this,n);s.onsuccess=function(){if(r._kvStore._channel){r._kvStore._channel.postMessage({method:"set",key:e,value:t})}n(null)}})});a.prototype.add=f(function(e,t,n){var r=this;if(t==null&&e!=null)return r.add(undefined,e,n);if(typeof t==="function"||t==null&&n==null)return r.add(undefined,e,t);if(t==null)throw new Error("A value must be provided as an argument");r._getObjectStore(function(i,o){if(i)return n(i);try{var s=e==null?o.add(t):o.add(t,e)}catch(e){return n(e)}s.onerror=l.bind(this,n);s.onsuccess=function(){if(r._kvStore._channel){r._kvStore._channel.postMessage({method:"add",key:e,value:t})}n(null)}})});a.prototype.get=f(function(e,t){var n=this;if(e==null)throw new Error("A key must be given as an argument");n._getObjectStore(function(n,r){if(n)return t(n);try{var i=r.get(e)}catch(e){return t(e)}i.onerror=l.bind(this,t);i.onsuccess=function(e){t(null,e.target.result)}})});a.prototype.json=f(function(e){var t=this;var n={};t.iterator(function(t,r){if(t)return e(t);if(r){n[r.key]=r.value;r.continue()}else{e(null,n)}})});a.prototype.keys=f(function(e){var t=this;var n=[];t.iterator(function(t,r){if(t)return e(t);if(r){n.push(r.key);r.continue()}else{e(null,n)}})});a.prototype.values=f(function(e){var t=this;var n=[];t.iterator(function(t,r){if(t)return e(t);if(r){n.push(r.value);r.continue()}else{e(null,n)}})});a.prototype.remove=f(function(e,t){var n=this;if(e==null)throw new Error("A key must be given as an argument");n._getObjectStore(function(r,i){if(r)return t(r);try{var o=i.delete(e)}catch(e){return t(e)}o.onerror=l.bind(this,t);o.onsuccess=function(){if(n._kvStore._channel){n._kvStore._channel.postMessage({method:"remove",key:e})}t(null)}})});a.prototype.clear=f(function(e){var t=this;t._getObjectStore(function(t,n){if(t)return e(t);try{var r=n.clear()}catch(t){return e(t)}r.onerror=l.bind(this,e);r.onsuccess=function(){e(null)}})});a.prototype.count=f(function(e){var t=this;t._getObjectStore(function(t,n){if(t)return e(t);try{var r=n.count()}catch(t){return e(t)}r.onerror=l.bind(this,e);r.onsuccess=function(t){e(null,t.target.result)}})});a.prototype.iterator=function(e){var t=this;if(typeof e!=="function")throw new Error("A function must be given");t._getObjectStore(function(t,n){if(t)return e(t);try{var r=n.openCursor()}catch(t){return e(t)}r.onerror=l.bind(this,e);r.onsuccess=function(t){var n=t.target.result;e(null,n)}})};a.prototype.abort=function(){if(this.finished)throw new Error("Transaction is finished");if(this._objectStore)this._objectStore.transaction.abort();this._close(new Error("Transaction aborted"))};a.prototype._close=function(e){if(this.finished)return;this.finished=true;this._kvStore=null;this._objectStore=null;for(var t in this._waiters)this._waiters[t](e||new Error("Transaction is finished"));if(this.onfinish)this.onfinish(e);this.onfinish=null;this._waiters=null};function l(e,t){if(t==null)return l(null,e);t.preventDefault();t.stopPropagation();if(e)e(t.target.error)}function f(e){return function(){var t=Array.prototype.slice.call(arguments);var n;var r;var i;var o=typeof t[t.length-1];if(o!=="function"){if(o==="undefined")t[t.length-1]=s;else t[t.length]=s;if(typeof Promise!=="undefined"){n=new Promise(function(e,t){r=e;i=t})}}e.apply(this,t);return n;function s(e,t){if(n){if(e)i(e);else r(t)}else{if(e)throw e}}}}},{events:1,inherits:2}]},{},[])("/")}); | ||
(function(e){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=e()}else if(typeof define==="function"&&define.amd){define([],e)}else{var t;if(typeof window!=="undefined"){t=window}else if(typeof global!=="undefined"){t=global}else if(typeof self!=="undefined"){t=self}else{t=this}t.IdbKvStore=e()}})(function(){var e,t,r;return function e(t,r,n){function i(s,u){if(!r[s]){if(!t[s]){var a=typeof require=="function"&&require;if(!u&&a)return a(s,!0);if(o)return o(s,!0);var l=new Error("Cannot find module '"+s+"'");throw l.code="MODULE_NOT_FOUND",l}var f=r[s]={exports:{}};t[s][0].call(f.exports,function(e){var r=t[s][1][e];return i(r?r:e)},f,f.exports,e,t,r,n)}return r[s].exports}var o=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i}({1:[function(e,t,r){function n(){this._events=this._events||{};this._maxListeners=this._maxListeners||undefined}t.exports=n;n.EventEmitter=n;n.prototype._events=undefined;n.prototype._maxListeners=undefined;n.defaultMaxListeners=10;n.prototype.setMaxListeners=function(e){if(!o(e)||e<0||isNaN(e))throw TypeError("n must be a positive number");this._maxListeners=e;return this};n.prototype.emit=function(e){var t,r,n,o,a,l;if(!this._events)this._events={};if(e==="error"){if(!this._events.error||s(this._events.error)&&!this._events.error.length){t=arguments[1];if(t instanceof Error){throw t}else{var f=new Error('Uncaught, unspecified "error" event. ('+t+")");f.context=t;throw f}}}r=this._events[e];if(u(r))return false;if(i(r)){switch(arguments.length){case 1:r.call(this);break;case 2:r.call(this,arguments[1]);break;case 3:r.call(this,arguments[1],arguments[2]);break;default:o=Array.prototype.slice.call(arguments,1);r.apply(this,o)}}else if(s(r)){o=Array.prototype.slice.call(arguments,1);l=r.slice();n=l.length;for(a=0;a<n;a++)l[a].apply(this,o)}return true};n.prototype.addListener=function(e,t){var r;if(!i(t))throw TypeError("listener must be a function");if(!this._events)this._events={};if(this._events.newListener)this.emit("newListener",e,i(t.listener)?t.listener:t);if(!this._events[e])this._events[e]=t;else if(s(this._events[e]))this._events[e].push(t);else this._events[e]=[this._events[e],t];if(s(this._events[e])&&!this._events[e].warned){if(!u(this._maxListeners)){r=this._maxListeners}else{r=n.defaultMaxListeners}if(r&&r>0&&this._events[e].length>r){this._events[e].warned=true;console.error("(node) warning: possible EventEmitter memory "+"leak detected. %d listeners added. "+"Use emitter.setMaxListeners() to increase limit.",this._events[e].length);if(typeof console.trace==="function"){console.trace()}}}return this};n.prototype.on=n.prototype.addListener;n.prototype.once=function(e,t){if(!i(t))throw TypeError("listener must be a function");var r=false;function n(){this.removeListener(e,n);if(!r){r=true;t.apply(this,arguments)}}n.listener=t;this.on(e,n);return this};n.prototype.removeListener=function(e,t){var r,n,o,u;if(!i(t))throw TypeError("listener must be a function");if(!this._events||!this._events[e])return this;r=this._events[e];o=r.length;n=-1;if(r===t||i(r.listener)&&r.listener===t){delete this._events[e];if(this._events.removeListener)this.emit("removeListener",e,t)}else if(s(r)){for(u=o;u-- >0;){if(r[u]===t||r[u].listener&&r[u].listener===t){n=u;break}}if(n<0)return this;if(r.length===1){r.length=0;delete this._events[e]}else{r.splice(n,1)}if(this._events.removeListener)this.emit("removeListener",e,t)}return this};n.prototype.removeAllListeners=function(e){var t,r;if(!this._events)return this;if(!this._events.removeListener){if(arguments.length===0)this._events={};else if(this._events[e])delete this._events[e];return this}if(arguments.length===0){for(t in this._events){if(t==="removeListener")continue;this.removeAllListeners(t)}this.removeAllListeners("removeListener");this._events={};return this}r=this._events[e];if(i(r)){this.removeListener(e,r)}else if(r){while(r.length)this.removeListener(e,r[r.length-1])}delete this._events[e];return this};n.prototype.listeners=function(e){var t;if(!this._events||!this._events[e])t=[];else if(i(this._events[e]))t=[this._events[e]];else t=this._events[e].slice();return t};n.prototype.listenerCount=function(e){if(this._events){var t=this._events[e];if(i(t))return 1;else if(t)return t.length}return 0};n.listenerCount=function(e,t){return e.listenerCount(t)};function i(e){return typeof e==="function"}function o(e){return typeof e==="number"}function s(e){return typeof e==="object"&&e!==null}function u(e){return e===void 0}},{}],2:[function(e,t,r){if(typeof Object.create==="function"){t.exports=function e(t,r){t.super_=r;t.prototype=Object.create(r.prototype,{constructor:{value:t,enumerable:false,writable:true,configurable:true}})}}else{t.exports=function e(t,r){t.super_=r;var n=function(){};n.prototype=r.prototype;t.prototype=new n;t.prototype.constructor=t}}},{}],"/":[function(e,t,r){t.exports=u;var n=e("events").EventEmitter;var i=e("inherits");var o=typeof window==="undefined"?self:window;var s=o.indexedDB||o.mozIndexedDB||o.webkitIndexedDB||o.msIndexedDB;u.INDEXEDDB_SUPPORT=s!=null;u.BROADCAST_SUPPORT=o.BroadcastChannel!=null;i(u,n);function u(e,t,r){var i=this;if(typeof e!=="string")throw new Error("A name must be supplied of type string");if(!s)throw new Error("IndexedDB not supported");if(typeof t==="function")return new u(e,null,t);if(!(i instanceof u))return new u(e,t,r);if(!t)t={};n.call(i);i._db=null;i._closed=false;i._channel=null;i._waiters=[];var a=t.channel||o.BroadcastChannel;if(a){i._channel=new a(e);i._channel.onmessage=y}var f=s.open(e);f.onerror=c;f.onsuccess=p;f.onupgradeneeded=d;i.on("newListener",_);function c(e){l(e);i._close(e.target.error);if(r)r(e.target.error)}function h(e){l(e);i._close(e.target.error)}function p(e){if(i._closed){e.target.result.close()}else{i._db=e.target.result;i._db.onclose=v;i._db.onerror=h;for(var t in i._waiters)i._waiters[t]._init(null);i._waiters=null;if(r)r(null);i.emit("open")}}function d(e){var t=e.target.result;t.createObjectStore("kv",{autoIncrement:true})}function v(){i._close()}function _(e){if(e!=="add"&&e!=="set"&&e!=="remove")return;if(!i._channel)return i.emit("error",new Error("No BroadcastChannel support"))}function y(e){if(e.data.method==="add")i.emit("add",e.data);else if(e.data.method==="set")i.emit("set",e.data);else if(e.data.method==="remove")i.emit("remove",e.data)}}u.prototype.get=function(e,t){return this.transaction("readonly").get(e,t)};u.prototype.set=function(e,t,r){return this.transaction("readwrite").set(e,t,r)};u.prototype.json=function(e,t){return this.transaction("readonly").json(e,t)};u.prototype.keys=function(e,t){return this.transaction("readonly").keys(e,t)};u.prototype.values=function(e,t){return this.transaction("readonly").values(e,t)};u.prototype.remove=function(e,t){return this.transaction("readwrite").remove(e,t)};u.prototype.clear=function(e){return this.transaction("readwrite").clear(e)};u.prototype.count=function(e){return this.transaction("readonly").count(e)};u.prototype.add=function(e,t,r){return this.transaction("readwrite").add(e,t,r)};u.prototype.iterator=function(e,t){return this.transaction("readonly").iterator(e,t)};u.prototype.transaction=function(e){if(this._closed)throw new Error("Database is closed");var t=new a(this,e);if(this._db)t._init(null);else this._waiters.push(t);return t};u.prototype.close=function(){this._close()};u.prototype._close=function(e){if(this._closed)return;this._closed=true;if(this._db)this._db.close();if(this._channel)this._channel.close();this._db=null;this._channel=null;if(e)this.emit("error",e);this.emit("close");for(var t in this._waiters)this._waiters[t]._init(e||new Error("Database is closed"));this._waiters=null;this.removeAllListeners()};function a(e,t){this._kvStore=e;this._mode=t||"readwrite";this._objectStore=null;this._waiters=null;this.finished=false;this.onfinish=null;if(this._mode!=="readonly"&&this._mode!=="readwrite"){throw new Error('mode must be either "readonly" or "readwrite"')}}a.prototype._init=function(e){var t=this;if(t.finished)return;if(e)return t._close(e);var r=t._kvStore._db.transaction("kv",t._mode);r.oncomplete=i;r.onerror=o;t._objectStore=r.objectStore("kv");for(var n in t._waiters)t._waiters[n](null,t._objectStore);t._waiters=null;function i(){t._close(null)}function o(e){l(e);t._close(e.target.error)}};a.prototype._getObjectStore=function(e){if(this.finished)throw new Error("Transaction is finished");if(this._objectStore)return e(null,this._objectStore);this._waiters=this._waiters||[];this._waiters.push(e)};a.prototype.set=function(e,t,r){var n=this;if(e==null||t==null)throw new Error("A key and value must be given");r=f(r);n._getObjectStore(function(i,o){if(i)return r(i);try{var s=o.put(t,e)}catch(e){return r(e)}s.onerror=l.bind(this,r);s.onsuccess=function(){if(n._kvStore._channel){n._kvStore._channel.postMessage({method:"set",key:e,value:t})}r(null)}});return r.promise};a.prototype.add=function(e,t,r){var n=this;if(t==null&&e!=null)return n.add(undefined,e,r);if(typeof t==="function"||t==null&&r==null)return n.add(undefined,e,t);if(t==null)throw new Error("A value must be provided as an argument");r=f(r);n._getObjectStore(function(i,o){if(i)return r(i);try{var s=e==null?o.add(t):o.add(t,e)}catch(e){return r(e)}s.onerror=l.bind(this,r);s.onsuccess=function(){if(n._kvStore._channel){n._kvStore._channel.postMessage({method:"add",key:e,value:t})}r(null)}});return r.promise};a.prototype.get=function(e,t){var r=this;if(e==null)throw new Error("A key must be given as an argument");t=f(t);r._getObjectStore(function(r,n){if(r)return t(r);try{var i=n.get(e)}catch(e){return t(e)}i.onerror=l.bind(this,t);i.onsuccess=function(e){t(null,e.target.result)}});return t.promise};a.prototype.json=function(e,t){var r=this;if(typeof e==="function")return r.json(null,e);t=f(t);var n={};r.iterator(e,function(e,r){if(e)return t(e);if(r){n[r.key]=r.value;r.continue()}else{t(null,n)}});return t.promise};a.prototype.keys=function(e,t){var r=this;if(typeof e==="function")return r.keys(null,e);t=f(t);var n=[];r.iterator(e,function(e,r){if(e)return t(e);if(r){n.push(r.key);r.continue()}else{t(null,n)}});return t.promise};a.prototype.values=function(e,t){var r=this;if(typeof e==="function")return r.values(null,e);t=f(t);var n=[];r.iterator(e,function(e,r){if(e)return t(e);if(r){n.push(r.value);r.continue()}else{t(null,n)}});return t.promise};a.prototype.remove=function(e,t){var r=this;if(e==null)throw new Error("A key must be given as an argument");t=f(t);r._getObjectStore(function(n,i){if(n)return t(n);try{var o=i.delete(e)}catch(e){return t(e)}o.onerror=l.bind(this,t);o.onsuccess=function(){if(r._kvStore._channel){r._kvStore._channel.postMessage({method:"remove",key:e})}t(null)}});return t.promise};a.prototype.clear=function(e){var t=this;e=f(e);t._getObjectStore(function(t,r){if(t)return e(t);try{var n=r.clear()}catch(t){return e(t)}n.onerror=l.bind(this,e);n.onsuccess=function(){e(null)}});return e.promise};a.prototype.count=function(e){var t=this;e=f(e);t._getObjectStore(function(t,r){if(t)return e(t);try{var n=r.count()}catch(t){return e(t)}n.onerror=l.bind(this,e);n.onsuccess=function(t){e(null,t.target.result)}});return e.promise};a.prototype.iterator=function(e,t){var r=this;if(typeof e==="function")return r.iterator(null,e);if(typeof t!=="function")throw new Error("A function must be given");r._getObjectStore(function(r,n){if(r)return t(r);try{var i=e==null?n.openCursor():n.openCursor(e)}catch(e){return t(e)}i.onerror=l.bind(this,t);i.onsuccess=function(e){var r=e.target.result;t(null,r)}})};a.prototype.abort=function(){if(this.finished)throw new Error("Transaction is finished");if(this._objectStore)this._objectStore.transaction.abort();this._close(new Error("Transaction aborted"))};a.prototype._close=function(e){if(this.finished)return;this.finished=true;this._kvStore=null;this._objectStore=null;for(var t in this._waiters)this._waiters[t](e||new Error("Transaction is finished"));if(this.onfinish)this.onfinish(e);this.onfinish=null;this._waiters=null};function l(e,t){if(t==null)return l(null,e);t.preventDefault();t.stopPropagation();if(e)e(t.target.error)}function f(e){var t;var r;var n;if(e!=null&&typeof e!=="function")throw new Error("cb must be a function");if(e==null&&typeof Promise!=="undefined"){t=new Promise(function(e,t){r=e;n=t})}function i(i,o){if(t){if(i)n(i);else r(o)}else{if(e)e(i,o);else if(i)throw i}}i.promise=t;return i}},{events:1,inherits:2}]},{},[])("/")}); |
150
index.js
@@ -97,12 +97,12 @@ /* eslint-env browser */ | ||
IdbKvStore.prototype.json = function (cb) { | ||
return this.transaction('readonly').json(cb) | ||
IdbKvStore.prototype.json = function (range, cb) { | ||
return this.transaction('readonly').json(range, cb) | ||
} | ||
IdbKvStore.prototype.keys = function (cb) { | ||
return this.transaction('readonly').keys(cb) | ||
IdbKvStore.prototype.keys = function (range, cb) { | ||
return this.transaction('readonly').keys(range, cb) | ||
} | ||
IdbKvStore.prototype.values = function (cb) { | ||
return this.transaction('readonly').values(cb) | ||
IdbKvStore.prototype.values = function (range, cb) { | ||
return this.transaction('readonly').values(range, cb) | ||
} | ||
@@ -126,4 +126,4 @@ | ||
IdbKvStore.prototype.iterator = function (next) { | ||
return this.transaction('readonly').iterator(next) | ||
IdbKvStore.prototype.iterator = function (range, next) { | ||
return this.transaction('readonly').iterator(range, next) | ||
} | ||
@@ -209,5 +209,7 @@ | ||
Transaction.prototype.set = promisify(function (key, value, cb) { | ||
Transaction.prototype.set = function (key, value, cb) { | ||
var self = this | ||
if (key == null || value == null) throw new Error('A key and value must be given') | ||
cb = promisify(cb) | ||
self._getObjectStore(function (err, objectStore) { | ||
@@ -234,5 +236,7 @@ if (err) return cb(err) | ||
}) | ||
}) | ||
Transaction.prototype.add = promisify(function (key, value, cb) { | ||
return cb.promise | ||
} | ||
Transaction.prototype.add = function (key, value, cb) { | ||
var self = this | ||
@@ -242,2 +246,4 @@ if (value == null && key != null) return self.add(undefined, key, cb) | ||
if (value == null) throw new Error('A value must be provided as an argument') | ||
cb = promisify(cb) | ||
self._getObjectStore(function (err, objectStore) { | ||
@@ -264,7 +270,11 @@ if (err) return cb(err) | ||
}) | ||
}) | ||
Transaction.prototype.get = promisify(function (key, cb) { | ||
return cb.promise | ||
} | ||
Transaction.prototype.get = function (key, cb) { | ||
var self = this | ||
if (key == null) throw new Error('A key must be given as an argument') | ||
cb = promisify(cb) | ||
self._getObjectStore(function (err, objectStore) { | ||
@@ -284,8 +294,13 @@ if (err) return cb(err) | ||
}) | ||
}) | ||
Transaction.prototype.json = promisify(function (cb) { | ||
return cb.promise | ||
} | ||
Transaction.prototype.json = function (range, cb) { | ||
var self = this | ||
if (typeof range === 'function') return self.json(null, range) | ||
cb = promisify(cb) | ||
var json = {} | ||
self.iterator(function (err, cursor) { | ||
self.iterator(range, function (err, cursor) { | ||
if (err) return cb(err) | ||
@@ -299,8 +314,13 @@ if (cursor) { | ||
}) | ||
}) | ||
Transaction.prototype.keys = promisify(function (cb) { | ||
return cb.promise | ||
} | ||
Transaction.prototype.keys = function (range, cb) { | ||
var self = this | ||
if (typeof range === 'function') return self.keys(null, range) | ||
cb = promisify(cb) | ||
var keys = [] | ||
self.iterator(function (err, cursor) { | ||
self.iterator(range, function (err, cursor) { | ||
if (err) return cb(err) | ||
@@ -314,8 +334,13 @@ if (cursor) { | ||
}) | ||
}) | ||
Transaction.prototype.values = promisify(function (cb) { | ||
return cb.promise | ||
} | ||
Transaction.prototype.values = function (range, cb) { | ||
var self = this | ||
if (typeof range === 'function') return self.values(null, range) | ||
cb = promisify(cb) | ||
var values = [] | ||
self.iterator(function (err, cursor) { | ||
self.iterator(range, function (err, cursor) { | ||
if (err) return cb(err) | ||
@@ -329,7 +354,11 @@ if (cursor) { | ||
}) | ||
}) | ||
Transaction.prototype.remove = promisify(function (key, cb) { | ||
return cb.promise | ||
} | ||
Transaction.prototype.remove = function (key, cb) { | ||
var self = this | ||
if (key == null) throw new Error('A key must be given as an argument') | ||
cb = promisify(cb) | ||
self._getObjectStore(function (err, objectStore) { | ||
@@ -355,6 +384,10 @@ if (err) return cb(err) | ||
}) | ||
}) | ||
Transaction.prototype.clear = promisify(function (cb) { | ||
return cb.promise | ||
} | ||
Transaction.prototype.clear = function (cb) { | ||
var self = this | ||
cb = promisify(cb) | ||
self._getObjectStore(function (err, objectStore) { | ||
@@ -374,6 +407,10 @@ if (err) return cb(err) | ||
}) | ||
}) | ||
Transaction.prototype.count = promisify(function (cb) { | ||
return cb.promise | ||
} | ||
Transaction.prototype.count = function (cb) { | ||
var self = this | ||
cb = promisify(cb) | ||
self._getObjectStore(function (err, objectStore) { | ||
@@ -393,6 +430,9 @@ if (err) return cb(err) | ||
}) | ||
}) | ||
Transaction.prototype.iterator = function (next) { | ||
return cb.promise | ||
} | ||
Transaction.prototype.iterator = function (range, next) { | ||
var self = this | ||
if (typeof range === 'function') return self.iterator(null, range) | ||
if (typeof next !== 'function') throw new Error('A function must be given') | ||
@@ -403,3 +443,3 @@ self._getObjectStore(function (err, objectStore) { | ||
try { | ||
var request = objectStore.openCursor() | ||
var request = range == null ? objectStore.openCursor() : objectStore.openCursor(range) | ||
} catch (e) { | ||
@@ -444,33 +484,29 @@ return next(e) | ||
function promisify (func) { | ||
return function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
var promise | ||
var pResolve | ||
var pReject | ||
function promisify (cb) { | ||
var promise | ||
var res | ||
var rej | ||
var cbType = typeof args[args.length - 1] | ||
if (cbType !== 'function') { | ||
if (cbType === 'undefined') args[args.length - 1] = cb | ||
else args[args.length] = cb | ||
if (typeof Promise !== 'undefined') { | ||
promise = new Promise(function (resolve, reject) { | ||
pResolve = resolve | ||
pReject = reject | ||
}) | ||
} | ||
} | ||
if (cb != null && typeof cb !== 'function') throw new Error('cb must be a function') | ||
func.apply(this, args) | ||
return promise | ||
if (cb == null && typeof Promise !== 'undefined') { | ||
promise = new Promise(function (resolve, reject) { | ||
res = resolve | ||
rej = reject | ||
}) | ||
} | ||
function cb (err, result) { | ||
if (promise) { | ||
if (err) pReject(err) | ||
else pResolve(result) | ||
} else { | ||
if (err) throw err | ||
} | ||
function intercept (err, result) { | ||
if (promise) { | ||
if (err) rej(err) | ||
else res(result) | ||
} else { | ||
if (cb) cb(err, result) | ||
else if (err) throw err | ||
} | ||
} | ||
intercept.promise = promise | ||
return intercept | ||
} |
{ | ||
"name": "idb-kv-store", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "Persistent key-value store for web browsers backed by IndexedDB", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -95,14 +95,20 @@ # idb-kv-store [![Build Status](https://travis-ci.org/xuset/idb-kv-store.svg?branch=master)](https://travis-ci.org/xuset/idb-kv-store) [![npm](https://img.shields.io/npm/v/idb-kv-store.svg)](https://npmjs.org/package/idb-kv-store) | ||
### `store.keys([cb])` | ||
### `store.keys([range], [cb])` | ||
Retrieves the list of keys stored. When the list is retrieved, `cb` is called with `cb(err, keys)`. If `cb` is undefined then a promise is returned. | ||
### `store.values([cb])` | ||
To only return a specific range, an [IDBKeyRange](https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange) can be passed into `range` | ||
### `store.values([range], [cb])` | ||
Retrieves the list of values stored. When the list is retrieved, `cb` is called with `cb(err, keys)`. If `cb` is undefined then a promise is returned. | ||
### `store.json([cb])` | ||
To only return a specific range, an [IDBKeyRange](https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange) can be passed into `range` | ||
### `store.json([range], [cb])` | ||
Retrieves the entire key-value store as a json object. When the json representation has been retrieved, `cb` is called with `cb(err, json)`. If `cb` is undefined, then a promise is returned. | ||
To only return a specific range, an [IDBKeyRange](https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange) can be passed into `range` | ||
### `store.count([cb])` | ||
@@ -112,3 +118,3 @@ | ||
### `store.iterator(function (err, cursor) {})` | ||
### `store.iterator([range], function (err, cursor) {})` | ||
@@ -126,2 +132,4 @@ Iterate over each item in the database. Example | ||
To only iterate over a specific range, an [IDBKeyRange](https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange) can be passed into `range` | ||
### `var transaction = store.transaction([mode])` | ||
@@ -128,0 +136,0 @@ |
60
test.js
@@ -386,2 +386,3 @@ /* eslint-env browser */ | ||
t.equal(err, null) | ||
store.json(console.log) | ||
t.deepEqual(values, ['A', 'B']) | ||
@@ -577,2 +578,61 @@ t.end() | ||
test('ranged iterator()', function (t) { | ||
t.timeoutAfter(3000) | ||
var store = createStore() | ||
var transaction = store.transaction() | ||
transaction.add(1) | ||
transaction.add(2) | ||
transaction.add(3) | ||
transaction.onfinish = function (err) { | ||
t.equal(err, null) | ||
var count = 1 | ||
store.iterator(IDBKeyRange.upperBound(2), function (err, cursor) { | ||
t.equal(err, null) | ||
if (count < 3) { | ||
t.notEqual(cursor, null) | ||
t.equal(cursor.key, count) | ||
t.equal(cursor.value, count) | ||
cursor.continue() | ||
} else if (count === 3) { | ||
t.equal(cursor, null) | ||
t.end() | ||
} else { | ||
t.fail() | ||
} | ||
count++ | ||
}) | ||
} | ||
}) | ||
test('ranged keys(), values(), and json()', function (t) { | ||
t.timeoutAfter(3000) | ||
var store = createStore() | ||
var transaction = store.transaction() | ||
transaction.add(1) | ||
transaction.add(2) | ||
transaction.add(3) | ||
transaction.onfinish = function (err) { | ||
t.equal(err, null) | ||
store.json(IDBKeyRange.only(2), function (err, json) { | ||
t.equal(err, null) | ||
t.deepEqual(json, {2: 2}) | ||
store.values(IDBKeyRange.lowerBound(2), function (err, values) { | ||
t.equal(err, null) | ||
t.deepEqual(values, [2, 3]) | ||
store.keys(IDBKeyRange.upperBound(2), function (err, keys) { | ||
t.equal(err, null) | ||
t.deepEqual(keys, [1, 2]) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
} | ||
}) | ||
test('transaction ordering on open', function (t) { | ||
@@ -579,0 +639,0 @@ t.timeoutAfter(3000) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55422
1123
210