idb-kv-store
Advanced tools
Comparing version 4.0.2 to 4.1.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,r;return function e(t,r,n){function s(i,a){if(!r[i]){if(!t[i]){var u=typeof require=="function"&&require;if(!a&&u)return u(i,!0);if(o)return o(i,!0);var l=new Error("Cannot find module '"+i+"'");throw l.code="MODULE_NOT_FOUND",l}var c=r[i]={exports:{}};t[i][0].call(c.exports,function(e){var r=t[i][1][e];return s(r?r:e)},c,c.exports,e,t,r,n)}return r[i].exports}var o=typeof require=="function"&&require;for(var i=0;i<n.length;i++)s(n[i]);return s}({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,u,l;if(!this._events)this._events={};if(e==="error"){if(!this._events.error||i(this._events.error)&&!this._events.error.length){t=arguments[1];if(t instanceof Error){throw t}else{var c=new Error('Uncaught, unspecified "error" event. ('+t+")");c.context=t;throw c}}}r=this._events[e];if(a(r))return false;if(s(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(i(r)){o=Array.prototype.slice.call(arguments,1);l=r.slice();n=l.length;for(u=0;u<n;u++)l[u].apply(this,o)}return true};n.prototype.addListener=function(e,t){var r;if(!s(t))throw TypeError("listener must be a function");if(!this._events)this._events={};if(this._events.newListener)this.emit("newListener",e,s(t.listener)?t.listener:t);if(!this._events[e])this._events[e]=t;else if(i(this._events[e]))this._events[e].push(t);else this._events[e]=[this._events[e],t];if(i(this._events[e])&&!this._events[e].warned){if(!a(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(!s(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,a;if(!s(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||s(r.listener)&&r.listener===t){delete this._events[e];if(this._events.removeListener)this.emit("removeListener",e,t)}else if(i(r)){for(a=o;a-- >0;){if(r[a]===t||r[a].listener&&r[a].listener===t){n=a;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(s(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(s(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(s(t))return 1;else if(t)return t.length}return 0};n.listenerCount=function(e,t){return e.listenerCount(t)};function s(e){return typeof e==="function"}function o(e){return typeof e==="number"}function i(e){return typeof e==="object"&&e!==null}function a(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=a;var n=e("events").EventEmitter;var s=e("inherits");var o=typeof window==="undefined"?self:window;var i=o.indexedDB||o.mozIndexedDB||o.webkitIndexedDB||o.msIndexedDB;a.INDEXEDDB_SUPPORT=i!=null;a.BROADCAST_SUPPORT=o.BroadcastChannel!=null;s(a,n);function a(e,t,r){var s=this;if(typeof e!=="string")throw new Error("A name must be supplied of type string");if(!i)throw new Error("IndexedDB not supported");if(typeof t==="function")return new a(e,null,t);if(!(this instanceof a))return new a(e,t,r);if(!t)t={};n.call(s);s._db=null;s._closed=false;s._queue=[];s._channel=null;var u=t.channel||o.BroadcastChannel;if(u){s._channel=new u(e);s._channel.onmessage=p}var l=i.open(e);l.onerror=c;l.onsuccess=f;l.onupgradeneeded=h;s.on("newListener",d);function c(e){s.close();s._handleError(e,r)}function f(e){if(s._closed){e.target.result.close()}else{s._db=e.target.result;s._db.onclose=v;s._drainQueue();if(r)r(null);s.emit("open")}}function h(e){if(s._closed)return;var t=e.target.result;t.createObjectStore("kv",{autoIncrement:true})}function v(){s.close()}function d(e){if(e!=="add"&&e!=="set"&&e!=="remove")return;if(!s._channel)return s.emit("error",new Error("No BroadcastChannel support"))}function p(e){if(e.data.method==="add")s.emit("add",e.data);else if(e.data.method==="set")s.emit("set",e.data);else if(e.data.method==="remove")s.emit("remove",e.data)}}a.prototype.get=function(e,t){var r=this;if(r._closed)throw new Error("Database is closed");var n=u(t);if(!r._db){r._queue.push([r.get,e,n.cb])}else if(Array.isArray(e)){var s=[];var o=false;var i=0;e.forEach(function(t,a){r.get(e[a],function(t,r){if(o)return;if(t){o=true;n.cb(t)}s[a]=r;i++;if(i===e.length)n.cb(null,s)})})}else{var a=r._db.transaction("kv","readonly");var l=a.objectStore("kv").get(e);l.onsuccess=function(e){n.cb(null,e.target.result)};a.onerror=function(e){r._handleError(e,n.cb)}}return n.promise};a.prototype.set=function(e,t,r){var n=this;if(n._closed)throw new Error("Database is closed");var s=u(r);if(!n._db){n._queue.push([n.set,e,t,s.cb])}else{var o=n._db.transaction("kv","readwrite");var i=o.objectStore("kv").put(t,e);i.onsuccess=function(){if(n._channel){n._channel.postMessage({method:"set",key:e,value:t})}s.cb(null)};o.onerror=function(e){n._handleError(e,s.cb)}}return s.promise};a.prototype.json=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=u(e);if(!t._db){t._queue.push([t.json,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").openCursor();var o={};s.onsuccess=function(e){var t=e.target.result;if(t){o[t.key]=t.value;t.continue()}else{r.cb(null,o)}};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};a.prototype.keys=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=u(e);if(!t._db){t._queue.push([t.keys,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").openCursor();var o=[];s.onsuccess=function(e){var t=e.target.result;if(t){o.push(t.key);t.continue()}else{r.cb(null,o)}};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};a.prototype.values=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=u(e);if(!t._db){t._queue.push([t.values,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").openCursor();var o=[];s.onsuccess=function(e){var t=e.target.result;if(t){o.push(t.value);t.continue()}else{r.cb(null,o)}};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};a.prototype.remove=function(e,t){var r=this;if(r._closed)throw new Error("Database is closed");var n=u(t);if(!r._db){r._queue.push([r.remove,e,n.cb])}else{var s=r._db.transaction("kv","readwrite");var o=s.objectStore("kv").delete(e);o.onsuccess=function(t){if(r._channel){r._channel.postMessage({method:"remove",key:e})}n.cb(null)};s.onerror=function(e){r._handleError(e,n.cb)}}return n.promise};a.prototype.clear=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=u(e);if(!t._db){t._queue.push([t.clear,r.cb])}else{var n=t._db.transaction("kv","readwrite");var s=n.objectStore("kv").clear();s.onsuccess=function(e){r.cb(null)};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};a.prototype.count=function(e){var t=this;if(t._closed)throw new Error("Database is closed");var r=u(e);if(!t._db){t._queue.push([t.count,r.cb])}else{var n=t._db.transaction("kv","readonly");var s=n.objectStore("kv").count();s.onsuccess=function(e){r.cb(null,e.target.result)};n.onerror=function(e){t._handleError(e,r.cb)}}return r.promise};a.prototype.add=function(e,t,r){var n=this;if(n._closed)throw new Error("Database is closed");if(typeof t==="function"||arguments.length===1)return n.add(undefined,e,t);var s=u(r);if(!n._db){n._queue.push([n.add,e,t,s.cb])}else{var o=n._db.transaction("kv","readwrite");var i=e==null?o.objectStore("kv").add(t):o.objectStore("kv").add(t,e);i.onsuccess=function(r){if(n._channel){n._channel.postMessage({method:"add",key:e,value:t})}s.cb(null)};o.onerror=function(e){n._handleError(e,s.cb)}}return s.promise};a.prototype.close=function(){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;this._queue=null;this.emit("close");this.removeAllListeners()};a.prototype._drainQueue=function(){var e=this;for(var t=0;t<e._queue.length;t++){var r=e._queue[t];var n=r.splice(1);r[0].apply(e,n)}e._queue=null};a.prototype._handleError=function(e,t){var r=e.target.error;e.preventDefault();if(t){t(r)}else{this.emit("error",r)}};function u(e){var t={cb:e};if(typeof Promise==="function"&&e==null){t.promise=new Promise(function(e,r){t.cb=function(t,n){if(t)return r(t);else return e(n)}})}if(!t.cb)t.cb=function e(){};return t}},{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,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}]},{},[])("/")}); |
552
index.js
@@ -0,1 +1,3 @@ | ||
/* eslint-env browser */ | ||
module.exports = IdbKvStore | ||
@@ -6,3 +8,3 @@ | ||
var global = typeof window === 'undefined' ? self : window // eslint-disable-line | ||
var global = typeof window === 'undefined' ? self : window | ||
var IDB = global.indexedDB || global.mozIndexedDB || global.webkitIndexedDB || global.msIndexedDB | ||
@@ -19,3 +21,3 @@ | ||
if (typeof opts === 'function') return new IdbKvStore(name, null, opts) | ||
if (!(this instanceof IdbKvStore)) return new IdbKvStore(name, opts, cb) | ||
if (!(self instanceof IdbKvStore)) return new IdbKvStore(name, opts, cb) | ||
if (!opts) opts = {} | ||
@@ -27,4 +29,4 @@ | ||
self._closed = false | ||
self._queue = [] | ||
self._channel = null | ||
self._waiters = [] | ||
@@ -45,6 +47,12 @@ var Channel = opts.channel || global.BroadcastChannel | ||
function onerror (event) { | ||
self.close() | ||
self._handleError(event, cb) | ||
handleError(event) | ||
self._close(event.target.error) | ||
if (cb) cb(event.target.error) | ||
} | ||
function onDbError (event) { | ||
handleError(event) | ||
self._close(event.target.error) | ||
} | ||
function onsuccess (event) { | ||
@@ -56,3 +64,5 @@ if (self._closed) { | ||
self._db.onclose = onclose | ||
self._drainQueue() | ||
self._db.onerror = onDbError | ||
for (var i in self._waiters) self._waiters[i]._init(null) | ||
self._waiters = null | ||
if (cb) cb(null) | ||
@@ -64,3 +74,2 @@ self.emit('open') | ||
function onupgradeneeded (event) { | ||
if (self._closed) return | ||
var db = event.target.result | ||
@@ -71,3 +80,3 @@ db.createObjectStore('kv', {autoIncrement: true}) | ||
function onclose () { | ||
self.close() | ||
self._close() | ||
} | ||
@@ -88,320 +97,373 @@ | ||
IdbKvStore.prototype.get = function (key, cb) { | ||
var self = this | ||
if (self._closed) throw new Error('Database is closed') | ||
var defer = promisify(cb) | ||
return this.transaction('readonly').get(key, cb) | ||
} | ||
if (!self._db) { | ||
self._queue.push([self.get, key, defer.cb]) | ||
} else if (Array.isArray(key)) { | ||
var result = [] | ||
var erroredOut = false | ||
var successes = 0 | ||
key.forEach(function (_, index) { | ||
self.get(key[index], function (err, val) { | ||
if (erroredOut) return | ||
if (err) { | ||
erroredOut = true | ||
defer.cb(err) | ||
} | ||
result[index] = val | ||
successes++ | ||
if (successes === key.length) defer.cb(null, result) | ||
}) | ||
}) | ||
} else { | ||
var transaction = self._db.transaction('kv', 'readonly') | ||
var request = transaction.objectStore('kv').get(key) | ||
IdbKvStore.prototype.set = function (key, value, cb) { | ||
return this.transaction('readwrite').set(key, value, cb) | ||
} | ||
request.onsuccess = function (event) { | ||
defer.cb(null, event.target.result) | ||
} | ||
IdbKvStore.prototype.json = function (cb) { | ||
return this.transaction('readonly').json(cb) | ||
} | ||
transaction.onerror = function (event) { | ||
self._handleError(event, defer.cb) | ||
} | ||
} | ||
IdbKvStore.prototype.keys = function (cb) { | ||
return this.transaction('readonly').keys(cb) | ||
} | ||
return defer.promise | ||
IdbKvStore.prototype.values = function (cb) { | ||
return this.transaction('readonly').values(cb) | ||
} | ||
IdbKvStore.prototype.set = function (key, value, cb) { | ||
var self = this | ||
if (self._closed) throw new Error('Database is closed') | ||
var defer = promisify(cb) | ||
IdbKvStore.prototype.remove = function (key, cb) { | ||
return this.transaction('readwrite').remove(key, cb) | ||
} | ||
if (!self._db) { | ||
self._queue.push([self.set, key, value, defer.cb]) | ||
} else { | ||
var transaction = self._db.transaction('kv', 'readwrite') | ||
var request = transaction.objectStore('kv').put(value, key) | ||
IdbKvStore.prototype.clear = function (cb) { | ||
return this.transaction('readwrite').clear(cb) | ||
} | ||
request.onsuccess = function () { | ||
if (self._channel) { | ||
self._channel.postMessage({ | ||
method: 'set', | ||
key: key, | ||
value: value | ||
}) | ||
} | ||
defer.cb(null) | ||
} | ||
IdbKvStore.prototype.count = function (cb) { | ||
return this.transaction('readonly').count(cb) | ||
} | ||
transaction.onerror = function (event) { | ||
self._handleError(event, defer.cb) | ||
} | ||
} | ||
IdbKvStore.prototype.add = function (key, value, cb) { | ||
return this.transaction('readwrite').add(key, value, cb) | ||
} | ||
return defer.promise | ||
IdbKvStore.prototype.iterator = function (next) { | ||
return this.transaction('readonly').iterator(next) | ||
} | ||
IdbKvStore.prototype.json = function (cb) { | ||
var self = this | ||
if (self._closed) throw new Error('Database is closed') | ||
var defer = promisify(cb) | ||
IdbKvStore.prototype.transaction = function (mode) { | ||
if (this._closed) throw new Error('Database is closed') | ||
var transaction = new Transaction(this, mode) | ||
if (this._db) transaction._init(null) | ||
else this._waiters.push(transaction) | ||
return transaction | ||
} | ||
if (!self._db) { | ||
self._queue.push([self.json, defer.cb]) | ||
} else { | ||
var transaction = self._db.transaction('kv', 'readonly') | ||
var request = transaction.objectStore('kv').openCursor() | ||
IdbKvStore.prototype.close = function () { | ||
this._close() | ||
} | ||
var json = {} | ||
request.onsuccess = function (event) { | ||
var cursor = event.target.result | ||
if (cursor) { | ||
json[cursor.key] = cursor.value | ||
cursor.continue() | ||
} else { | ||
defer.cb(null, json) | ||
} | ||
} | ||
IdbKvStore.prototype._close = function (err) { | ||
if (this._closed) return | ||
this._closed = true | ||
transaction.onerror = function (event) { | ||
self._handleError(event, defer.cb) | ||
} | ||
} | ||
if (this._db) this._db.close() | ||
if (this._channel) this._channel.close() | ||
return defer.promise | ||
this._db = null | ||
this._channel = null | ||
if (err) this.emit('error', err) | ||
this.emit('close') | ||
for (var i in this._waiters) this._waiters[i]._init(err || new Error('Database is closed')) | ||
this._waiters = null | ||
this.removeAllListeners() | ||
} | ||
IdbKvStore.prototype.keys = function (cb) { | ||
var self = this | ||
if (self._closed) throw new Error('Database is closed') | ||
var defer = promisify(cb) | ||
function Transaction (kvStore, mode) { | ||
this._kvStore = kvStore | ||
this._mode = mode || 'readwrite' | ||
this._objectStore = null | ||
this._waiters = null | ||
if (!self._db) { | ||
self._queue.push([self.keys, defer.cb]) | ||
} else { | ||
var transaction = self._db.transaction('kv', 'readonly') | ||
var request = transaction.objectStore('kv').openCursor() | ||
this.finished = false | ||
this.onfinish = null | ||
var keys = [] | ||
request.onsuccess = function (event) { | ||
var cursor = event.target.result | ||
if (cursor) { | ||
keys.push(cursor.key) | ||
cursor.continue() | ||
} else { | ||
defer.cb(null, keys) | ||
} | ||
} | ||
transaction.onerror = function (event) { | ||
self._handleError(event, defer.cb) | ||
} | ||
if (this._mode !== 'readonly' && this._mode !== 'readwrite') { | ||
throw new Error('mode must be either "readonly" or "readwrite"') | ||
} | ||
return defer.promise | ||
} | ||
IdbKvStore.prototype.values = function (cb) { | ||
Transaction.prototype._init = function (err) { | ||
var self = this | ||
if (self._closed) throw new Error('Database is closed') | ||
var defer = promisify(cb) | ||
if (!self._db) { | ||
self._queue.push([self.values, defer.cb]) | ||
} else { | ||
var transaction = self._db.transaction('kv', 'readonly') | ||
var request = transaction.objectStore('kv').openCursor() | ||
if (self.finished) return | ||
if (err) return self._close(err) | ||
var values = [] | ||
request.onsuccess = function (event) { | ||
var cursor = event.target.result | ||
if (cursor) { | ||
values.push(cursor.value) | ||
cursor.continue() | ||
} else { | ||
defer.cb(null, values) | ||
} | ||
} | ||
var transaction = self._kvStore._db.transaction('kv', self._mode) | ||
transaction.oncomplete = oncomplete | ||
transaction.onerror = onerror | ||
transaction.onerror = function (event) { | ||
self._handleError(event, defer.cb) | ||
} | ||
self._objectStore = transaction.objectStore('kv') | ||
for (var i in self._waiters) self._waiters[i](null, self._objectStore) | ||
self._waiters = null | ||
function oncomplete () { | ||
self._close(null) | ||
} | ||
return defer.promise | ||
function onerror (event) { | ||
handleError(event) | ||
self._close(event.target.error) | ||
} | ||
} | ||
IdbKvStore.prototype.remove = function (key, cb) { | ||
Transaction.prototype._getObjectStore = function (cb) { | ||
if (this.finished) throw new Error('Transaction is finished') | ||
if (this._objectStore) return cb(null, this._objectStore) | ||
this._waiters = this._waiters || [] | ||
this._waiters.push(cb) | ||
} | ||
Transaction.prototype.set = promisify(function (key, value, cb) { | ||
var self = this | ||
if (self._closed) throw new Error('Database is closed') | ||
var defer = promisify(cb) | ||
if (key == null || value == null) throw new Error('A key and value must be given') | ||
self._getObjectStore(function (err, objectStore) { | ||
if (err) return cb(err) | ||
if (!self._db) { | ||
self._queue.push([self.remove, key, defer.cb]) | ||
} else { | ||
var transaction = self._db.transaction('kv', 'readwrite') | ||
var request = transaction.objectStore('kv').delete(key) | ||
try { | ||
var request = objectStore.put(value, key) | ||
} catch (e) { | ||
return cb(e) | ||
} | ||
request.onsuccess = function (event) { | ||
if (self._channel) { | ||
self._channel.postMessage({ | ||
method: 'remove', | ||
key: key | ||
request.onerror = handleError.bind(this, cb) | ||
request.onsuccess = function () { | ||
if (self._kvStore._channel) { | ||
self._kvStore._channel.postMessage({ | ||
method: 'set', | ||
key: key, | ||
value: value | ||
}) | ||
} | ||
defer.cb(null) | ||
cb(null) | ||
} | ||
}) | ||
}) | ||
transaction.onerror = function (event) { | ||
self._handleError(event, defer.cb) | ||
Transaction.prototype.add = promisify(function (key, value, cb) { | ||
var self = this | ||
if (value == null && key != null) return self.add(undefined, key, cb) | ||
if (typeof value === 'function' || value == null && cb == null) return self.add(undefined, key, value) | ||
if (value == null) throw new Error('A value must be provided as an argument') | ||
self._getObjectStore(function (err, objectStore) { | ||
if (err) return cb(err) | ||
try { | ||
var request = key == null ? objectStore.add(value) : objectStore.add(value, key) | ||
} catch (e) { | ||
return cb(e) | ||
} | ||
} | ||
return defer.promise | ||
} | ||
request.onerror = handleError.bind(this, cb) | ||
request.onsuccess = function () { | ||
if (self._kvStore._channel) { | ||
self._kvStore._channel.postMessage({ | ||
method: 'add', | ||
key: key, | ||
value: value | ||
}) | ||
} | ||
cb(null) | ||
} | ||
}) | ||
}) | ||
IdbKvStore.prototype.clear = function (cb) { | ||
Transaction.prototype.get = promisify(function (key, cb) { | ||
var self = this | ||
if (self._closed) throw new Error('Database is closed') | ||
var defer = promisify(cb) | ||
if (key == null) throw new Error('A key must be given as an argument') | ||
self._getObjectStore(function (err, objectStore) { | ||
if (err) return cb(err) | ||
if (!self._db) { | ||
self._queue.push([self.clear, defer.cb]) | ||
} else { | ||
var transaction = self._db.transaction('kv', 'readwrite') | ||
var request = transaction.objectStore('kv').clear() | ||
try { | ||
var request = objectStore.get(key) | ||
} catch (e) { | ||
return cb(e) | ||
} | ||
request.onerror = handleError.bind(this, cb) | ||
request.onsuccess = function (event) { | ||
defer.cb(null) | ||
cb(null, event.target.result) | ||
} | ||
}) | ||
}) | ||
transaction.onerror = function (event) { | ||
self._handleError(event, defer.cb) | ||
Transaction.prototype.json = promisify(function (cb) { | ||
var self = this | ||
var json = {} | ||
self.iterator(function (err, cursor) { | ||
if (err) return cb(err) | ||
if (cursor) { | ||
json[cursor.key] = cursor.value | ||
cursor.continue() | ||
} else { | ||
cb(null, json) | ||
} | ||
} | ||
}) | ||
}) | ||
return defer.promise | ||
} | ||
Transaction.prototype.keys = promisify(function (cb) { | ||
var self = this | ||
var keys = [] | ||
self.iterator(function (err, cursor) { | ||
if (err) return cb(err) | ||
if (cursor) { | ||
keys.push(cursor.key) | ||
cursor.continue() | ||
} else { | ||
cb(null, keys) | ||
} | ||
}) | ||
}) | ||
IdbKvStore.prototype.count = function (cb) { | ||
Transaction.prototype.values = promisify(function (cb) { | ||
var self = this | ||
if (self._closed) throw new Error('Database is closed') | ||
var defer = promisify(cb) | ||
var values = [] | ||
self.iterator(function (err, cursor) { | ||
if (err) return cb(err) | ||
if (cursor) { | ||
values.push(cursor.value) | ||
cursor.continue() | ||
} else { | ||
cb(null, values) | ||
} | ||
}) | ||
}) | ||
if (!self._db) { | ||
self._queue.push([self.count, defer.cb]) | ||
} else { | ||
var transaction = self._db.transaction('kv', 'readonly') | ||
var request = transaction.objectStore('kv').count() | ||
Transaction.prototype.remove = promisify(function (key, cb) { | ||
var self = this | ||
if (key == null) throw new Error('A key must be given as an argument') | ||
self._getObjectStore(function (err, objectStore) { | ||
if (err) return cb(err) | ||
request.onsuccess = function (event) { | ||
defer.cb(null, event.target.result) | ||
try { | ||
var request = objectStore.delete(key) | ||
} catch (e) { | ||
return cb(e) | ||
} | ||
transaction.onerror = function (event) { | ||
self._handleError(event, defer.cb) | ||
request.onerror = handleError.bind(this, cb) | ||
request.onsuccess = function () { | ||
if (self._kvStore._channel) { | ||
self._kvStore._channel.postMessage({ | ||
method: 'remove', | ||
key: key | ||
}) | ||
} | ||
cb(null) | ||
} | ||
} | ||
}) | ||
}) | ||
return defer.promise | ||
} | ||
Transaction.prototype.clear = promisify(function (cb) { | ||
var self = this | ||
self._getObjectStore(function (err, objectStore) { | ||
if (err) return cb(err) | ||
IdbKvStore.prototype.add = function (key, value, cb) { | ||
try { | ||
var request = objectStore.clear() | ||
} catch (e) { | ||
return cb(e) | ||
} | ||
request.onerror = handleError.bind(this, cb) | ||
request.onsuccess = function () { | ||
cb(null) | ||
} | ||
}) | ||
}) | ||
Transaction.prototype.count = promisify(function (cb) { | ||
var self = this | ||
if (self._closed) throw new Error('Database is closed') | ||
if (typeof value === 'function' || arguments.length === 1) return self.add(undefined, key, value) | ||
var defer = promisify(cb) | ||
self._getObjectStore(function (err, objectStore) { | ||
if (err) return cb(err) | ||
if (!self._db) { | ||
self._queue.push([self.add, key, value, defer.cb]) | ||
} else { | ||
var transaction = self._db.transaction('kv', 'readwrite') | ||
var request = key == null | ||
? transaction.objectStore('kv').add(value) | ||
: transaction.objectStore('kv').add(value, key) | ||
try { | ||
var request = objectStore.count() | ||
} catch (e) { | ||
return cb(e) | ||
} | ||
request.onerror = handleError.bind(this, cb) | ||
request.onsuccess = function (event) { | ||
if (self._channel) { | ||
self._channel.postMessage({ | ||
method: 'add', | ||
key: key, | ||
value: value | ||
}) | ||
} | ||
defer.cb(null) | ||
cb(null, event.target.result) | ||
} | ||
}) | ||
}) | ||
transaction.onerror = function (event) { | ||
self._handleError(event, defer.cb) | ||
Transaction.prototype.iterator = function (next) { | ||
var self = this | ||
if (typeof next !== 'function') throw new Error('A function must be given') | ||
self._getObjectStore(function (err, objectStore) { | ||
if (err) return next(err) | ||
try { | ||
var request = objectStore.openCursor() | ||
} catch (e) { | ||
return next(e) | ||
} | ||
} | ||
return defer.promise | ||
request.onerror = handleError.bind(this, next) | ||
request.onsuccess = function (event) { | ||
var cursor = event.target.result | ||
next(null, cursor) | ||
} | ||
}) | ||
} | ||
IdbKvStore.prototype.close = function () { | ||
if (this._closed) return | ||
this._closed = true | ||
Transaction.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')) | ||
} | ||
if (this._db) this._db.close() | ||
if (this._channel) this._channel.close() | ||
Transaction.prototype._close = function (err) { | ||
if (this.finished) return | ||
this.finished = true | ||
this._db = null | ||
this._channel = null | ||
this._queue = null | ||
this._kvStore = null | ||
this._objectStore = null | ||
this.emit('close') | ||
for (var i in this._waiters) this._waiters[i](err || new Error('Transaction is finished')) | ||
if (this.onfinish) this.onfinish(err) | ||
this.removeAllListeners() | ||
this.onfinish = null | ||
this._waiters = null | ||
} | ||
IdbKvStore.prototype._drainQueue = function () { | ||
var self = this | ||
for (var i = 0; i < self._queue.length; i++) { | ||
var item = self._queue[i] | ||
var args = item.splice(1) | ||
item[0].apply(self, args) | ||
} | ||
self._queue = null | ||
function handleError (cb, event) { | ||
if (event == null) return handleError(null, cb) | ||
event.preventDefault() | ||
event.stopPropagation() | ||
if (cb) cb(event.target.error) | ||
} | ||
IdbKvStore.prototype._handleError = function (event, cb) { | ||
var err = event.target.error | ||
event.preventDefault() | ||
function promisify (func) { | ||
return function () { | ||
var args = Array.prototype.slice.call(arguments) | ||
var promise | ||
var pResolve | ||
var pReject | ||
if (cb) { | ||
cb(err) | ||
} else { | ||
this.emit('error', err) | ||
} | ||
} | ||
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 | ||
}) | ||
} | ||
} | ||
function promisify (cb) { | ||
var defer = {cb: cb} | ||
func.apply(this, args) | ||
return promise | ||
if (typeof Promise === 'function' && cb == null) { | ||
defer.promise = new Promise(function (resolve, reject) { | ||
defer.cb = function (err, result) { | ||
if (err) return reject(err) | ||
else return resolve(result) | ||
function cb (err, result) { | ||
if (promise) { | ||
if (err) pReject(err) | ||
else pResolve(result) | ||
} else { | ||
if (err) throw err | ||
} | ||
}) | ||
} | ||
} | ||
if (!defer.cb) defer.cb = function noop () {} | ||
return defer | ||
} |
{ | ||
"name": "idb-kv-store", | ||
"version": "4.0.2", | ||
"version": "4.1.0", | ||
"description": "Persistent key-value store for web browsers backed by IndexedDB", | ||
@@ -39,2 +39,3 @@ "main": "index.js", | ||
"karma-tap": "^3.1.1", | ||
"run-parallel-limit": "^1.0.3", | ||
"standard": "^8.6.0", | ||
@@ -41,0 +42,0 @@ "tape": "^4.6.3", |
@@ -20,3 +20,5 @@ # 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.set('abc', 'def', function (err) { | ||
if (err) throw err | ||
store.get('abc', function (err, value) { | ||
if (err) throw err | ||
console.log('key=abc value=' + value) | ||
@@ -38,3 +40,3 @@ }) | ||
```js | ||
store.on('add', change => { | ||
store.on('add', change => { // add, set, and remove events supported | ||
console.log('key=' + change.key, 'value=' + change.value) | ||
@@ -47,2 +49,17 @@ }) | ||
Group multiple operations in an atomic and durable transaction | ||
```js | ||
var transaction = store.transaction('readwrite') | ||
transaction.add('value1') // key will be auto-generated | ||
transaction.add('value2') // key will be auto-generated | ||
transaction.onfinish = function (err) { | ||
if (err) throw err | ||
console.log('Everything is persisted to disk!') | ||
} | ||
``` | ||
## API | ||
@@ -97,2 +114,29 @@ | ||
### `store.iterator(function (err, cursor) {})` | ||
Iterate over each item in the database. Example | ||
```js | ||
store.iterator(function (err, cursor) { | ||
if (err) throw err | ||
if (cursor) { // If we haven't reached the end | ||
console.log('key=' + cursor.key, 'value=' + cursor.value) | ||
cursor.continue() // This method will be called with the next item | ||
} | ||
}) | ||
``` | ||
### `var transaction = store.transaction([mode])` | ||
Returns a Transaction that allows for multiple operations to be grouped together in a durable and atomic way. `mode` can take the strings `'readwrite'` or `'readonly'`, and defaults to `'readwrite'`. The methods of the Transaction object are identical to the ones in IdbKvStore, and include: `add`, `set`, `get`, `remove`, `clear`, `keys`, `values`, `json`, `count`, and `iterator`. | ||
Transactions automatically commit after the last callback of a request completes and no new requests are made. | ||
#### `transaction.onfinish = function (err) {}` | ||
Called when the transaction has either successfully completed or failed. If the transaction failed then `err` is non null. | ||
#### `transaction.abort()` | ||
Rolls back any changes made by the transaction. The transaction is considered finish now. | ||
### `store.close()` | ||
@@ -99,0 +143,0 @@ |
267
test.js
@@ -0,3 +1,6 @@ | ||
/* eslint-env browser */ | ||
var IdbKvStore = require('.') | ||
var test = require('tape') | ||
var runParallel = require('run-parallel-limit') | ||
@@ -60,18 +63,2 @@ test('create/get/set pre-ready', function (t) { | ||
test('get multiple', function (t) { | ||
t.timeoutAfter(3000) | ||
var store = createStore() | ||
store.set('a', 1, function (err) { | ||
t.equal(err, null) | ||
store.set('b', 2, function (err) { | ||
t.equal(err, null) | ||
store.get(['a', 'b'], function (err, values) { | ||
t.equal(err, null) | ||
t.deepEqual(values, [1, 2]) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('promises', function (t) { | ||
@@ -161,2 +148,10 @@ t.timeoutAfter(3000) | ||
t.throws(function () { return new IdbKvStore({}) }) | ||
var store = createStore() | ||
t.throws(function () { store.get() }) | ||
t.throws(function () { store.set() }) | ||
t.throws(function () { store.set('key') }) | ||
t.throws(function () { store.remove() }) | ||
t.throws(function () { store.iterator() }) | ||
t.throws(function () { store.add() }) | ||
t.throws(function () { store.transaction('foo') }) | ||
t.end() | ||
@@ -223,2 +218,5 @@ }) | ||
var store = createStore() | ||
t.throws(function () { store.add() }) | ||
store.add('abc', 'def', function (err) { | ||
@@ -233,2 +231,21 @@ t.equal(err, null) | ||
test('single arg add()', function (t) { | ||
t.timeoutAfter(3000) | ||
var name = '' + (Math.round(9e16 * Math.random())) | ||
var store = new IdbKvStore(name, function (err) { | ||
t.equal(err, null) | ||
store.add('foobar') | ||
store.close() | ||
}) | ||
store.on('close', function () { | ||
store = new IdbKvStore(name) | ||
store.json(function (err, json) { | ||
t.equal(err, null) | ||
t.deepEqual({1: 'foobar'}, json) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
test('close()', function (t) { | ||
@@ -238,4 +255,4 @@ t.timeoutAfter(3000) | ||
store.close() | ||
t.throws(function () { store.get() }) | ||
t.throws(function () { store.set() }) | ||
t.throws(function () { store.get('foo') }) | ||
t.throws(function () { store.set('foo', 'foo') }) | ||
t.end() | ||
@@ -402,2 +419,216 @@ }) | ||
test('transaction', function (t) { | ||
t.timeoutAfter(3000) | ||
var store = createStore() | ||
var transaction = store.transaction() | ||
transaction.onfinish = onfinish | ||
transaction.add('foo') | ||
transaction.set(1, 'bar') | ||
transaction.set(2, 'bar') | ||
transaction.remove(2) | ||
transaction.json(function (err, json) { | ||
t.equal(err, null) | ||
t.deepEqual({1: 'bar'}, json) | ||
}) | ||
function onfinish (err) { | ||
t.equal(err, null) | ||
t.end() | ||
} | ||
}) | ||
test('transaction abort', function (t) { | ||
t.timeoutAfter(3000) | ||
t.plan(5) | ||
var store = createStore() | ||
var transaction = store.transaction() | ||
transaction.onfinish = onfinish | ||
transaction.add('foo', function (err) { | ||
t.ok(err instanceof Error) | ||
}) | ||
transaction.abort() | ||
t.throws(function () { transaction.add('bar') }) | ||
function onfinish (err) { | ||
t.ok(err instanceof Error) | ||
store.json(function (err, json) { | ||
t.equal(err, null) | ||
t.deepEqual({}, json) | ||
}) | ||
} | ||
}) | ||
test('iterator()', function (t) { | ||
t.timeoutAfter(3000) | ||
var store = createStore() | ||
var transaction = store.transaction() | ||
var count = 0 | ||
transaction.onfinish = onfinish | ||
transaction.add('foo') | ||
transaction.add('bar') | ||
function onfinish (err) { | ||
t.equal(err, null) | ||
store.iterator(iter) | ||
} | ||
function iter (err, cursor) { | ||
t.equal(err, null) | ||
count++ | ||
if (count === 1) { | ||
t.notEqual(cursor, null) | ||
t.equal(cursor.key, 1) | ||
t.equal(cursor.value, 'foo') | ||
cursor.continue() | ||
} else if (count === 2) { | ||
t.notEqual(cursor, null) | ||
t.equal(cursor.key, 2) | ||
t.equal(cursor.value, 'bar') | ||
cursor.continue() | ||
} else if (count === 3) { | ||
t.equal(cursor, null) | ||
t.end() | ||
} | ||
} | ||
}) | ||
test('write on readonly fails', function (t) { | ||
t.timeoutAfter(3000) | ||
var store = createStore() | ||
var transaction = store.transaction('readonly') | ||
transaction.add('foobar', function (err) { | ||
t.ok(err instanceof DOMException) | ||
transaction.set('foo', 'bar', function (err) { | ||
t.ok(err instanceof DOMException) | ||
transaction.remove('foobar', function (err) { | ||
t.ok(err instanceof DOMException) | ||
transaction.clear(function (err) { | ||
t.ok(err instanceof DOMException) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
test('transaction cleanup', function (t) { | ||
t.timeoutAfter(3000) | ||
var store = createStore() | ||
var trans = store.transaction('readonly') | ||
trans.onfinish = function (err) { | ||
t.equal(err, null) | ||
t.equal(trans.finished, true) | ||
t.throws(function () { trans.get('foo') }) | ||
t.end() | ||
} | ||
}) | ||
test('close db closes transactions', function (t) { | ||
t.timeoutAfter(3000) | ||
t.plan(3) | ||
var store = createStore() | ||
var trans = store.transaction('readonly') | ||
trans.get('foo', function (err, result) { | ||
t.ok(err instanceof Error) | ||
t.equal(result, undefined) | ||
}) | ||
trans.onfinish = function (err) { | ||
t.ok(err instanceof Error) | ||
} | ||
store.close() | ||
}) | ||
test('transaction before close completes', function (t) { | ||
t.timeoutAfter(3000) | ||
t.plan(4) | ||
var name = '' + (Math.round(9e16 * Math.random())) | ||
var store = new IdbKvStore(name, function (err) { | ||
t.equal(err, null) | ||
store.add('foobar', function (err) { | ||
t.equal(err, null) | ||
}) | ||
store.close() | ||
store = new IdbKvStore(name) | ||
store.json(function (err, json) { | ||
t.equal(err, null) | ||
t.deepEqual({1: 'foobar'}, json) | ||
}) | ||
}) | ||
}) | ||
test('close then open is successful', function (t) { | ||
t.timeoutAfter(3000) | ||
var name = '' + (Math.round(9e16 * Math.random())) | ||
var store = new IdbKvStore(name) | ||
store.close() | ||
store.close() | ||
store = new IdbKvStore(name, function (err) { | ||
t.equal(err, null) | ||
t.end() | ||
}) | ||
}) | ||
test('transaction ordering on open', function (t) { | ||
t.timeoutAfter(3000) | ||
// NOTE: Sometimes IE & Edge will fail this due to not following idb spec | ||
var store = createStore(function (err) { | ||
t.equal(err, null) | ||
store.add('second') | ||
}) | ||
store.once('open', function () { | ||
store.add('third', function (err) { | ||
t.equal(err, null) | ||
store.json(function (err, json) { | ||
t.equal(err, null) | ||
t.deepEqual(json, { | ||
1: 'first', | ||
2: 'second', | ||
3: 'third' | ||
}) | ||
t.end() | ||
}) | ||
}) | ||
}) | ||
store.add('first') | ||
}) | ||
test.skip('benchmark', function (t) { | ||
var buffSize = 4 * 1024 | ||
var storeCount = 10000 | ||
var buffer = Buffer.alloc(buffSize) | ||
var store = createStore() | ||
var tasks = [] | ||
for (var i = 0; i < storeCount; i++) { | ||
tasks.push(function (cb) { store.add(buffer, cb) }) | ||
} | ||
console.log('Storing', storeCount, 'elements at', buffSize / 1024, 'kB per elements') | ||
var start = new Date().getTime() | ||
runParallel(tasks, 10, function (err) { | ||
t.equal(err, null) | ||
var totalTime = (new Date().getTime() - start) / 1000 | ||
var totalSize = buffSize * storeCount / 1024 | ||
var throughput = Math.round(totalSize / totalTime) | ||
console.log('total time =', totalTime, 'seconds. throughput =', throughput, 'kB/seconds') | ||
t.end() | ||
}) | ||
}) | ||
function createStore (cb) { | ||
@@ -404,0 +635,0 @@ var name = '' + (Math.round(9e16 * Math.random())) |
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
52503
1053
202
12