localforage.js 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856
  1. /*!
  2. localForage -- Offline Storage, Improved
  3. Version 1.4.0
  4. https://mozilla.github.io/localForage
  5. (c) 2013-2015 Mozilla, Apache License 2.0
  6. */
  7. (function() {
  8. var define, requireModule, require, requirejs;
  9. (function() {
  10. var registry = {}, seen = {};
  11. define = function(name, deps, callback) {
  12. registry[name] = { deps: deps, callback: callback };
  13. };
  14. requirejs = require = requireModule = function(name) {
  15. requirejs._eak_seen = registry;
  16. if (seen[name]) { return seen[name]; }
  17. seen[name] = {};
  18. if (!registry[name]) {
  19. throw new Error("Could not find module " + name);
  20. }
  21. var mod = registry[name],
  22. deps = mod.deps,
  23. callback = mod.callback,
  24. reified = [],
  25. exports;
  26. for (var i=0, l=deps.length; i<l; i++) {
  27. if (deps[i] === 'exports') {
  28. reified.push(exports = {});
  29. } else {
  30. reified.push(requireModule(resolve(deps[i])));
  31. }
  32. }
  33. var value = callback.apply(this, reified);
  34. return seen[name] = exports || value;
  35. function resolve(child) {
  36. if (child.charAt(0) !== '.') { return child; }
  37. var parts = child.split("/");
  38. var parentBase = name.split("/").slice(0, -1);
  39. for (var i=0, l=parts.length; i<l; i++) {
  40. var part = parts[i];
  41. if (part === '..') { parentBase.pop(); }
  42. else if (part === '.') { continue; }
  43. else { parentBase.push(part); }
  44. }
  45. return parentBase.join("/");
  46. }
  47. };
  48. })();
  49. define("promise/all",
  50. ["./utils","exports"],
  51. function(__dependency1__, __exports__) {
  52. "use strict";
  53. /* global toString */
  54. var isArray = __dependency1__.isArray;
  55. var isFunction = __dependency1__.isFunction;
  56. /**
  57. Returns a promise that is fulfilled when all the given promises have been
  58. fulfilled, or rejected if any of them become rejected. The return promise
  59. is fulfilled with an array that gives all the values in the order they were
  60. passed in the `promises` array argument.
  61. Example:
  62. ```javascript
  63. var promise1 = RSVP.resolve(1);
  64. var promise2 = RSVP.resolve(2);
  65. var promise3 = RSVP.resolve(3);
  66. var promises = [ promise1, promise2, promise3 ];
  67. RSVP.all(promises).then(function(array){
  68. // The array here would be [ 1, 2, 3 ];
  69. });
  70. ```
  71. If any of the `promises` given to `RSVP.all` are rejected, the first promise
  72. that is rejected will be given as an argument to the returned promises's
  73. rejection handler. For example:
  74. Example:
  75. ```javascript
  76. var promise1 = RSVP.resolve(1);
  77. var promise2 = RSVP.reject(new Error("2"));
  78. var promise3 = RSVP.reject(new Error("3"));
  79. var promises = [ promise1, promise2, promise3 ];
  80. RSVP.all(promises).then(function(array){
  81. // Code here never runs because there are rejected promises!
  82. }, function(error) {
  83. // error.message === "2"
  84. });
  85. ```
  86. @method all
  87. @for RSVP
  88. @param {Array} promises
  89. @param {String} label
  90. @return {Promise} promise that is fulfilled when all `promises` have been
  91. fulfilled, or rejected if any of them become rejected.
  92. */
  93. function all(promises) {
  94. /*jshint validthis:true */
  95. var Promise = this;
  96. if (!isArray(promises)) {
  97. throw new TypeError('You must pass an array to all.');
  98. }
  99. return new Promise(function(resolve, reject) {
  100. var results = [], remaining = promises.length,
  101. promise;
  102. if (remaining === 0) {
  103. resolve([]);
  104. }
  105. function resolver(index) {
  106. return function(value) {
  107. resolveAll(index, value);
  108. };
  109. }
  110. function resolveAll(index, value) {
  111. results[index] = value;
  112. if (--remaining === 0) {
  113. resolve(results);
  114. }
  115. }
  116. for (var i = 0; i < promises.length; i++) {
  117. promise = promises[i];
  118. if (promise && isFunction(promise.then)) {
  119. promise.then(resolver(i), reject);
  120. } else {
  121. resolveAll(i, promise);
  122. }
  123. }
  124. });
  125. }
  126. __exports__.all = all;
  127. });
  128. define("promise/asap",
  129. ["exports"],
  130. function(__exports__) {
  131. "use strict";
  132. var browserGlobal = (typeof window !== 'undefined') ? window : {};
  133. var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
  134. var local = (typeof global !== 'undefined') ? global : (this === undefined? window:this);
  135. // node
  136. function useNextTick() {
  137. return function() {
  138. process.nextTick(flush);
  139. };
  140. }
  141. function useMutationObserver() {
  142. var iterations = 0;
  143. var observer = new BrowserMutationObserver(flush);
  144. var node = document.createTextNode('');
  145. observer.observe(node, { characterData: true });
  146. return function() {
  147. node.data = (iterations = ++iterations % 2);
  148. };
  149. }
  150. function useSetTimeout() {
  151. return function() {
  152. local.setTimeout(flush, 1);
  153. };
  154. }
  155. var queue = [];
  156. function flush() {
  157. for (var i = 0; i < queue.length; i++) {
  158. var tuple = queue[i];
  159. var callback = tuple[0], arg = tuple[1];
  160. callback(arg);
  161. }
  162. queue = [];
  163. }
  164. var scheduleFlush;
  165. // Decide what async method to use to triggering processing of queued callbacks:
  166. if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
  167. scheduleFlush = useNextTick();
  168. } else if (BrowserMutationObserver) {
  169. scheduleFlush = useMutationObserver();
  170. } else {
  171. scheduleFlush = useSetTimeout();
  172. }
  173. function asap(callback, arg) {
  174. var length = queue.push([callback, arg]);
  175. if (length === 1) {
  176. // If length is 1, that means that we need to schedule an async flush.
  177. // If additional callbacks are queued before the queue is flushed, they
  178. // will be processed by this flush that we are scheduling.
  179. scheduleFlush();
  180. }
  181. }
  182. __exports__.asap = asap;
  183. });
  184. define("promise/config",
  185. ["exports"],
  186. function(__exports__) {
  187. "use strict";
  188. var config = {
  189. instrument: false
  190. };
  191. function configure(name, value) {
  192. if (arguments.length === 2) {
  193. config[name] = value;
  194. } else {
  195. return config[name];
  196. }
  197. }
  198. __exports__.config = config;
  199. __exports__.configure = configure;
  200. });
  201. define("promise/polyfill",
  202. ["./promise","./utils","exports"],
  203. function(__dependency1__, __dependency2__, __exports__) {
  204. "use strict";
  205. /*global self*/
  206. var RSVPPromise = __dependency1__.Promise;
  207. var isFunction = __dependency2__.isFunction;
  208. function polyfill() {
  209. var local;
  210. if (typeof global !== 'undefined') {
  211. local = global;
  212. } else if (typeof window !== 'undefined' && window.document) {
  213. local = window;
  214. } else {
  215. local = self;
  216. }
  217. var es6PromiseSupport =
  218. "Promise" in local &&
  219. // Some of these methods are missing from
  220. // Firefox/Chrome experimental implementations
  221. "resolve" in local.Promise &&
  222. "reject" in local.Promise &&
  223. "all" in local.Promise &&
  224. "race" in local.Promise &&
  225. // Older version of the spec had a resolver object
  226. // as the arg rather than a function
  227. (function() {
  228. var resolve;
  229. new local.Promise(function(r) { resolve = r; });
  230. return isFunction(resolve);
  231. }());
  232. if (!es6PromiseSupport) {
  233. local.Promise = RSVPPromise;
  234. }
  235. }
  236. __exports__.polyfill = polyfill;
  237. });
  238. define("promise/promise",
  239. ["./config","./utils","./all","./race","./resolve","./reject","./asap","exports"],
  240. function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __exports__) {
  241. "use strict";
  242. var config = __dependency1__.config;
  243. var configure = __dependency1__.configure;
  244. var objectOrFunction = __dependency2__.objectOrFunction;
  245. var isFunction = __dependency2__.isFunction;
  246. var now = __dependency2__.now;
  247. var all = __dependency3__.all;
  248. var race = __dependency4__.race;
  249. var staticResolve = __dependency5__.resolve;
  250. var staticReject = __dependency6__.reject;
  251. var asap = __dependency7__.asap;
  252. var counter = 0;
  253. config.async = asap; // default async is asap;
  254. function Promise(resolver) {
  255. if (!isFunction(resolver)) {
  256. throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
  257. }
  258. if (!(this instanceof Promise)) {
  259. throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
  260. }
  261. this._subscribers = [];
  262. invokeResolver(resolver, this);
  263. }
  264. function invokeResolver(resolver, promise) {
  265. function resolvePromise(value) {
  266. resolve(promise, value);
  267. }
  268. function rejectPromise(reason) {
  269. reject(promise, reason);
  270. }
  271. try {
  272. resolver(resolvePromise, rejectPromise);
  273. } catch(e) {
  274. rejectPromise(e);
  275. }
  276. }
  277. function invokeCallback(settled, promise, callback, detail) {
  278. var hasCallback = isFunction(callback),
  279. value, error, succeeded, failed;
  280. if (hasCallback) {
  281. try {
  282. value = callback(detail);
  283. succeeded = true;
  284. } catch(e) {
  285. failed = true;
  286. error = e;
  287. }
  288. } else {
  289. value = detail;
  290. succeeded = true;
  291. }
  292. if (handleThenable(promise, value)) {
  293. return;
  294. } else if (hasCallback && succeeded) {
  295. resolve(promise, value);
  296. } else if (failed) {
  297. reject(promise, error);
  298. } else if (settled === FULFILLED) {
  299. resolve(promise, value);
  300. } else if (settled === REJECTED) {
  301. reject(promise, value);
  302. }
  303. }
  304. var PENDING = void 0;
  305. var SEALED = 0;
  306. var FULFILLED = 1;
  307. var REJECTED = 2;
  308. function subscribe(parent, child, onFulfillment, onRejection) {
  309. var subscribers = parent._subscribers;
  310. var length = subscribers.length;
  311. subscribers[length] = child;
  312. subscribers[length + FULFILLED] = onFulfillment;
  313. subscribers[length + REJECTED] = onRejection;
  314. }
  315. function publish(promise, settled) {
  316. var child, callback, subscribers = promise._subscribers, detail = promise._detail;
  317. for (var i = 0; i < subscribers.length; i += 3) {
  318. child = subscribers[i];
  319. callback = subscribers[i + settled];
  320. invokeCallback(settled, child, callback, detail);
  321. }
  322. promise._subscribers = null;
  323. }
  324. Promise.prototype = {
  325. constructor: Promise,
  326. _state: undefined,
  327. _detail: undefined,
  328. _subscribers: undefined,
  329. then: function(onFulfillment, onRejection) {
  330. var promise = this;
  331. var thenPromise = new this.constructor(function() {});
  332. if (this._state) {
  333. var callbacks = arguments;
  334. config.async(function invokePromiseCallback() {
  335. invokeCallback(promise._state, thenPromise, callbacks[promise._state - 1], promise._detail);
  336. });
  337. } else {
  338. subscribe(this, thenPromise, onFulfillment, onRejection);
  339. }
  340. return thenPromise;
  341. },
  342. 'catch': function(onRejection) {
  343. return this.then(null, onRejection);
  344. }
  345. };
  346. Promise.all = all;
  347. Promise.race = race;
  348. Promise.resolve = staticResolve;
  349. Promise.reject = staticReject;
  350. function handleThenable(promise, value) {
  351. var then = null,
  352. resolved;
  353. try {
  354. if (promise === value) {
  355. throw new TypeError("A promises callback cannot return that same promise.");
  356. }
  357. if (objectOrFunction(value)) {
  358. then = value.then;
  359. if (isFunction(then)) {
  360. then.call(value, function(val) {
  361. if (resolved) { return true; }
  362. resolved = true;
  363. if (value !== val) {
  364. resolve(promise, val);
  365. } else {
  366. fulfill(promise, val);
  367. }
  368. }, function(val) {
  369. if (resolved) { return true; }
  370. resolved = true;
  371. reject(promise, val);
  372. });
  373. return true;
  374. }
  375. }
  376. } catch (error) {
  377. if (resolved) { return true; }
  378. reject(promise, error);
  379. return true;
  380. }
  381. return false;
  382. }
  383. function resolve(promise, value) {
  384. if (promise === value) {
  385. fulfill(promise, value);
  386. } else if (!handleThenable(promise, value)) {
  387. fulfill(promise, value);
  388. }
  389. }
  390. function fulfill(promise, value) {
  391. if (promise._state !== PENDING) { return; }
  392. promise._state = SEALED;
  393. promise._detail = value;
  394. config.async(publishFulfillment, promise);
  395. }
  396. function reject(promise, reason) {
  397. if (promise._state !== PENDING) { return; }
  398. promise._state = SEALED;
  399. promise._detail = reason;
  400. config.async(publishRejection, promise);
  401. }
  402. function publishFulfillment(promise) {
  403. publish(promise, promise._state = FULFILLED);
  404. }
  405. function publishRejection(promise) {
  406. publish(promise, promise._state = REJECTED);
  407. }
  408. __exports__.Promise = Promise;
  409. });
  410. define("promise/race",
  411. ["./utils","exports"],
  412. function(__dependency1__, __exports__) {
  413. "use strict";
  414. /* global toString */
  415. var isArray = __dependency1__.isArray;
  416. /**
  417. `RSVP.race` allows you to watch a series of promises and act as soon as the
  418. first promise given to the `promises` argument fulfills or rejects.
  419. Example:
  420. ```javascript
  421. var promise1 = new RSVP.Promise(function(resolve, reject){
  422. setTimeout(function(){
  423. resolve("promise 1");
  424. }, 200);
  425. });
  426. var promise2 = new RSVP.Promise(function(resolve, reject){
  427. setTimeout(function(){
  428. resolve("promise 2");
  429. }, 100);
  430. });
  431. RSVP.race([promise1, promise2]).then(function(result){
  432. // result === "promise 2" because it was resolved before promise1
  433. // was resolved.
  434. });
  435. ```
  436. `RSVP.race` is deterministic in that only the state of the first completed
  437. promise matters. For example, even if other promises given to the `promises`
  438. array argument are resolved, but the first completed promise has become
  439. rejected before the other promises became fulfilled, the returned promise
  440. will become rejected:
  441. ```javascript
  442. var promise1 = new RSVP.Promise(function(resolve, reject){
  443. setTimeout(function(){
  444. resolve("promise 1");
  445. }, 200);
  446. });
  447. var promise2 = new RSVP.Promise(function(resolve, reject){
  448. setTimeout(function(){
  449. reject(new Error("promise 2"));
  450. }, 100);
  451. });
  452. RSVP.race([promise1, promise2]).then(function(result){
  453. // Code here never runs because there are rejected promises!
  454. }, function(reason){
  455. // reason.message === "promise2" because promise 2 became rejected before
  456. // promise 1 became fulfilled
  457. });
  458. ```
  459. @method race
  460. @for RSVP
  461. @param {Array} promises array of promises to observe
  462. @param {String} label optional string for describing the promise returned.
  463. Useful for tooling.
  464. @return {Promise} a promise that becomes fulfilled with the value the first
  465. completed promises is resolved with if the first completed promise was
  466. fulfilled, or rejected with the reason that the first completed promise
  467. was rejected with.
  468. */
  469. function race(promises) {
  470. /*jshint validthis:true */
  471. var Promise = this;
  472. if (!isArray(promises)) {
  473. throw new TypeError('You must pass an array to race.');
  474. }
  475. return new Promise(function(resolve, reject) {
  476. var results = [], promise;
  477. for (var i = 0; i < promises.length; i++) {
  478. promise = promises[i];
  479. if (promise && typeof promise.then === 'function') {
  480. promise.then(resolve, reject);
  481. } else {
  482. resolve(promise);
  483. }
  484. }
  485. });
  486. }
  487. __exports__.race = race;
  488. });
  489. define("promise/reject",
  490. ["exports"],
  491. function(__exports__) {
  492. "use strict";
  493. /**
  494. `RSVP.reject` returns a promise that will become rejected with the passed
  495. `reason`. `RSVP.reject` is essentially shorthand for the following:
  496. ```javascript
  497. var promise = new RSVP.Promise(function(resolve, reject){
  498. reject(new Error('WHOOPS'));
  499. });
  500. promise.then(function(value){
  501. // Code here doesn't run because the promise is rejected!
  502. }, function(reason){
  503. // reason.message === 'WHOOPS'
  504. });
  505. ```
  506. Instead of writing the above, your code now simply becomes the following:
  507. ```javascript
  508. var promise = RSVP.reject(new Error('WHOOPS'));
  509. promise.then(function(value){
  510. // Code here doesn't run because the promise is rejected!
  511. }, function(reason){
  512. // reason.message === 'WHOOPS'
  513. });
  514. ```
  515. @method reject
  516. @for RSVP
  517. @param {Any} reason value that the returned promise will be rejected with.
  518. @param {String} label optional string for identifying the returned promise.
  519. Useful for tooling.
  520. @return {Promise} a promise that will become rejected with the given
  521. `reason`.
  522. */
  523. function reject(reason) {
  524. /*jshint validthis:true */
  525. var Promise = this;
  526. return new Promise(function (resolve, reject) {
  527. reject(reason);
  528. });
  529. }
  530. __exports__.reject = reject;
  531. });
  532. define("promise/resolve",
  533. ["exports"],
  534. function(__exports__) {
  535. "use strict";
  536. function resolve(value) {
  537. /*jshint validthis:true */
  538. if (value && typeof value === 'object' && value.constructor === this) {
  539. return value;
  540. }
  541. var Promise = this;
  542. return new Promise(function(resolve) {
  543. resolve(value);
  544. });
  545. }
  546. __exports__.resolve = resolve;
  547. });
  548. define("promise/utils",
  549. ["exports"],
  550. function(__exports__) {
  551. "use strict";
  552. function objectOrFunction(x) {
  553. return isFunction(x) || (typeof x === "object" && x !== null);
  554. }
  555. function isFunction(x) {
  556. return typeof x === "function";
  557. }
  558. function isArray(x) {
  559. return Object.prototype.toString.call(x) === "[object Array]";
  560. }
  561. // Date.now is not available in browsers < IE9
  562. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now#Compatibility
  563. var now = Date.now || function() { return new Date().getTime(); };
  564. __exports__.objectOrFunction = objectOrFunction;
  565. __exports__.isFunction = isFunction;
  566. __exports__.isArray = isArray;
  567. __exports__.now = now;
  568. });
  569. requireModule('promise/polyfill').polyfill();
  570. }());(function webpackUniversalModuleDefinition(root, factory) {
  571. if(typeof exports === 'object' && typeof module === 'object')
  572. module.exports = factory();
  573. else if(typeof define === 'function' && define.amd)
  574. define([], factory);
  575. else if(typeof exports === 'object')
  576. exports["localforage"] = factory();
  577. else
  578. root["localforage"] = factory();
  579. })(this, function() {
  580. return /******/ (function(modules) { // webpackBootstrap
  581. /******/ // The module cache
  582. /******/ var installedModules = {};
  583. /******/ // The require function
  584. /******/ function __webpack_require__(moduleId) {
  585. /******/ // Check if module is in cache
  586. /******/ if(installedModules[moduleId])
  587. /******/ return installedModules[moduleId].exports;
  588. /******/ // Create a new module (and put it into the cache)
  589. /******/ var module = installedModules[moduleId] = {
  590. /******/ exports: {},
  591. /******/ id: moduleId,
  592. /******/ loaded: false
  593. /******/ };
  594. /******/ // Execute the module function
  595. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  596. /******/ // Flag the module as loaded
  597. /******/ module.loaded = true;
  598. /******/ // Return the exports of the module
  599. /******/ return module.exports;
  600. /******/ }
  601. /******/ // expose the modules object (__webpack_modules__)
  602. /******/ __webpack_require__.m = modules;
  603. /******/ // expose the module cache
  604. /******/ __webpack_require__.c = installedModules;
  605. /******/ // __webpack_public_path__
  606. /******/ __webpack_require__.p = "";
  607. /******/ // Load entry module and return exports
  608. /******/ return __webpack_require__(0);
  609. /******/ })
  610. /************************************************************************/
  611. /******/ ([
  612. /* 0 */
  613. /***/ function(module, exports, __webpack_require__) {
  614. 'use strict';
  615. exports.__esModule = true;
  616. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  617. var localForage = (function (globalObject) {
  618. 'use strict';
  619. // Custom drivers are stored here when `defineDriver()` is called.
  620. // They are shared across all instances of localForage.
  621. var CustomDrivers = {};
  622. var DriverType = {
  623. INDEXEDDB: 'asyncStorage',
  624. LOCALSTORAGE: 'localStorageWrapper',
  625. WEBSQL: 'webSQLStorage'
  626. };
  627. var DefaultDriverOrder = [DriverType.INDEXEDDB, DriverType.WEBSQL, DriverType.LOCALSTORAGE];
  628. var LibraryMethods = ['clear', 'getItem', 'iterate', 'key', 'keys', 'length', 'removeItem', 'setItem'];
  629. var DefaultConfig = {
  630. description: '',
  631. driver: DefaultDriverOrder.slice(),
  632. name: 'localforage',
  633. // Default DB size is _JUST UNDER_ 5MB, as it's the highest size
  634. // we can use without a prompt.
  635. size: 4980736,
  636. storeName: 'keyvaluepairs',
  637. version: 1.0
  638. };
  639. var driverSupport = (function (self) {
  640. var result = {};
  641. // Check to see if IndexedDB is available and if it is the latest
  642. // implementation; it's our preferred backend library. We use "_spec_test"
  643. // as the name of the database because it's not the one we'll operate on,
  644. // but it's useful to make sure its using the right spec.
  645. // See: https://github.com/mozilla/localForage/issues/128
  646. result[DriverType.INDEXEDDB] = !!(function () {
  647. try {
  648. // Initialize IndexedDB; fall back to vendor-prefixed versions
  649. // if needed.
  650. var indexedDB = indexedDB || self.indexedDB || self.webkitIndexedDB || self.mozIndexedDB || self.OIndexedDB || self.msIndexedDB;
  651. // We mimic PouchDB here; just UA test for Safari (which, as of
  652. // iOS 8/Yosemite, doesn't properly support IndexedDB).
  653. // IndexedDB support is broken and different from Blink's.
  654. // This is faster than the test case (and it's sync), so we just
  655. // do this. *SIGH*
  656. // http://bl.ocks.org/nolanlawson/raw/c83e9039edf2278047e9/
  657. //
  658. // We test for openDatabase because IE Mobile identifies itself
  659. // as Safari. Oh the lulz...
  660. if (typeof self.openDatabase !== 'undefined' && self.navigator && self.navigator.userAgent && /Safari/.test(self.navigator.userAgent) && !/Chrome/.test(self.navigator.userAgent)) {
  661. return false;
  662. }
  663. return indexedDB && typeof indexedDB.open === 'function' &&
  664. // Some Samsung/HTC Android 4.0-4.3 devices
  665. // have older IndexedDB specs; if this isn't available
  666. // their IndexedDB is too old for us to use.
  667. // (Replaces the onupgradeneeded test.)
  668. typeof self.IDBKeyRange !== 'undefined';
  669. } catch (e) {
  670. return false;
  671. }
  672. })();
  673. result[DriverType.WEBSQL] = !!(function () {
  674. try {
  675. return self.openDatabase;
  676. } catch (e) {
  677. return false;
  678. }
  679. })();
  680. result[DriverType.LOCALSTORAGE] = !!(function () {
  681. try {
  682. return self.localStorage && 'setItem' in self.localStorage && self.localStorage.setItem;
  683. } catch (e) {
  684. return false;
  685. }
  686. })();
  687. return result;
  688. })(globalObject);
  689. var isArray = Array.isArray || function (arg) {
  690. return Object.prototype.toString.call(arg) === '[object Array]';
  691. };
  692. function callWhenReady(localForageInstance, libraryMethod) {
  693. localForageInstance[libraryMethod] = function () {
  694. var _args = arguments;
  695. return localForageInstance.ready().then(function () {
  696. return localForageInstance[libraryMethod].apply(localForageInstance, _args);
  697. });
  698. };
  699. }
  700. function extend() {
  701. for (var i = 1; i < arguments.length; i++) {
  702. var arg = arguments[i];
  703. if (arg) {
  704. for (var key in arg) {
  705. if (arg.hasOwnProperty(key)) {
  706. if (isArray(arg[key])) {
  707. arguments[0][key] = arg[key].slice();
  708. } else {
  709. arguments[0][key] = arg[key];
  710. }
  711. }
  712. }
  713. }
  714. }
  715. return arguments[0];
  716. }
  717. function isLibraryDriver(driverName) {
  718. for (var driver in DriverType) {
  719. if (DriverType.hasOwnProperty(driver) && DriverType[driver] === driverName) {
  720. return true;
  721. }
  722. }
  723. return false;
  724. }
  725. var LocalForage = (function () {
  726. function LocalForage(options) {
  727. _classCallCheck(this, LocalForage);
  728. this.INDEXEDDB = DriverType.INDEXEDDB;
  729. this.LOCALSTORAGE = DriverType.LOCALSTORAGE;
  730. this.WEBSQL = DriverType.WEBSQL;
  731. this._defaultConfig = extend({}, DefaultConfig);
  732. this._config = extend({}, this._defaultConfig, options);
  733. this._driverSet = null;
  734. this._initDriver = null;
  735. this._ready = false;
  736. this._dbInfo = null;
  737. this._wrapLibraryMethodsWithReady();
  738. this.setDriver(this._config.driver);
  739. }
  740. // The actual localForage object that we expose as a module or via a
  741. // global. It's extended by pulling in one of our other libraries.
  742. // Set any config values for localForage; can be called anytime before
  743. // the first API call (e.g. `getItem`, `setItem`).
  744. // We loop through options so we don't overwrite existing config
  745. // values.
  746. LocalForage.prototype.config = function config(options) {
  747. // If the options argument is an object, we use it to set values.
  748. // Otherwise, we return either a specified config value or all
  749. // config values.
  750. if (typeof options === 'object') {
  751. // If localforage is ready and fully initialized, we can't set
  752. // any new configuration values. Instead, we return an error.
  753. if (this._ready) {
  754. return new Error("Can't call config() after localforage " + 'has been used.');
  755. }
  756. for (var i in options) {
  757. if (i === 'storeName') {
  758. options[i] = options[i].replace(/\W/g, '_');
  759. }
  760. this._config[i] = options[i];
  761. }
  762. // after all config options are set and
  763. // the driver option is used, try setting it
  764. if ('driver' in options && options.driver) {
  765. this.setDriver(this._config.driver);
  766. }
  767. return true;
  768. } else if (typeof options === 'string') {
  769. return this._config[options];
  770. } else {
  771. return this._config;
  772. }
  773. };
  774. // Used to define a custom driver, shared across all instances of
  775. // localForage.
  776. LocalForage.prototype.defineDriver = function defineDriver(driverObject, callback, errorCallback) {
  777. var promise = new Promise(function (resolve, reject) {
  778. try {
  779. var driverName = driverObject._driver;
  780. var complianceError = new Error('Custom driver not compliant; see ' + 'https://mozilla.github.io/localForage/#definedriver');
  781. var namingError = new Error('Custom driver name already in use: ' + driverObject._driver);
  782. // A driver name should be defined and not overlap with the
  783. // library-defined, default drivers.
  784. if (!driverObject._driver) {
  785. reject(complianceError);
  786. return;
  787. }
  788. if (isLibraryDriver(driverObject._driver)) {
  789. reject(namingError);
  790. return;
  791. }
  792. var customDriverMethods = LibraryMethods.concat('_initStorage');
  793. for (var i = 0; i < customDriverMethods.length; i++) {
  794. var customDriverMethod = customDriverMethods[i];
  795. if (!customDriverMethod || !driverObject[customDriverMethod] || typeof driverObject[customDriverMethod] !== 'function') {
  796. reject(complianceError);
  797. return;
  798. }
  799. }
  800. var supportPromise = Promise.resolve(true);
  801. if ('_support' in driverObject) {
  802. if (driverObject._support && typeof driverObject._support === 'function') {
  803. supportPromise = driverObject._support();
  804. } else {
  805. supportPromise = Promise.resolve(!!driverObject._support);
  806. }
  807. }
  808. supportPromise.then(function (supportResult) {
  809. driverSupport[driverName] = supportResult;
  810. CustomDrivers[driverName] = driverObject;
  811. resolve();
  812. }, reject);
  813. } catch (e) {
  814. reject(e);
  815. }
  816. });
  817. promise.then(callback, errorCallback);
  818. return promise;
  819. };
  820. LocalForage.prototype.driver = function driver() {
  821. return this._driver || null;
  822. };
  823. LocalForage.prototype.getDriver = function getDriver(driverName, callback, errorCallback) {
  824. var self = this;
  825. var getDriverPromise = (function () {
  826. if (isLibraryDriver(driverName)) {
  827. switch (driverName) {
  828. case self.INDEXEDDB:
  829. return new Promise(function (resolve, reject) {
  830. resolve(__webpack_require__(1));
  831. });
  832. case self.LOCALSTORAGE:
  833. return new Promise(function (resolve, reject) {
  834. resolve(__webpack_require__(2));
  835. });
  836. case self.WEBSQL:
  837. return new Promise(function (resolve, reject) {
  838. resolve(__webpack_require__(4));
  839. });
  840. }
  841. } else if (CustomDrivers[driverName]) {
  842. return Promise.resolve(CustomDrivers[driverName]);
  843. }
  844. return Promise.reject(new Error('Driver not found.'));
  845. })();
  846. getDriverPromise.then(callback, errorCallback);
  847. return getDriverPromise;
  848. };
  849. LocalForage.prototype.getSerializer = function getSerializer(callback) {
  850. var serializerPromise = new Promise(function (resolve, reject) {
  851. resolve(__webpack_require__(3));
  852. });
  853. if (callback && typeof callback === 'function') {
  854. serializerPromise.then(function (result) {
  855. callback(result);
  856. });
  857. }
  858. return serializerPromise;
  859. };
  860. LocalForage.prototype.ready = function ready(callback) {
  861. var self = this;
  862. var promise = self._driverSet.then(function () {
  863. if (self._ready === null) {
  864. self._ready = self._initDriver();
  865. }
  866. return self._ready;
  867. });
  868. promise.then(callback, callback);
  869. return promise;
  870. };
  871. LocalForage.prototype.setDriver = function setDriver(drivers, callback, errorCallback) {
  872. var self = this;
  873. if (!isArray(drivers)) {
  874. drivers = [drivers];
  875. }
  876. var supportedDrivers = this._getSupportedDrivers(drivers);
  877. function setDriverToConfig() {
  878. self._config.driver = self.driver();
  879. }
  880. function initDriver(supportedDrivers) {
  881. return function () {
  882. var currentDriverIndex = 0;
  883. function driverPromiseLoop() {
  884. while (currentDriverIndex < supportedDrivers.length) {
  885. var driverName = supportedDrivers[currentDriverIndex];
  886. currentDriverIndex++;
  887. self._dbInfo = null;
  888. self._ready = null;
  889. return self.getDriver(driverName).then(function (driver) {
  890. self._extend(driver);
  891. setDriverToConfig();
  892. self._ready = self._initStorage(self._config);
  893. return self._ready;
  894. })['catch'](driverPromiseLoop);
  895. }
  896. setDriverToConfig();
  897. var error = new Error('No available storage method found.');
  898. self._driverSet = Promise.reject(error);
  899. return self._driverSet;
  900. }
  901. return driverPromiseLoop();
  902. };
  903. }
  904. // There might be a driver initialization in progress
  905. // so wait for it to finish in order to avoid a possible
  906. // race condition to set _dbInfo
  907. var oldDriverSetDone = this._driverSet !== null ? this._driverSet['catch'](function () {
  908. return Promise.resolve();
  909. }) : Promise.resolve();
  910. this._driverSet = oldDriverSetDone.then(function () {
  911. var driverName = supportedDrivers[0];
  912. self._dbInfo = null;
  913. self._ready = null;
  914. return self.getDriver(driverName).then(function (driver) {
  915. self._driver = driver._driver;
  916. setDriverToConfig();
  917. self._wrapLibraryMethodsWithReady();
  918. self._initDriver = initDriver(supportedDrivers);
  919. });
  920. })['catch'](function () {
  921. setDriverToConfig();
  922. var error = new Error('No available storage method found.');
  923. self._driverSet = Promise.reject(error);
  924. return self._driverSet;
  925. });
  926. this._driverSet.then(callback, errorCallback);
  927. return this._driverSet;
  928. };
  929. LocalForage.prototype.supports = function supports(driverName) {
  930. return !!driverSupport[driverName];
  931. };
  932. LocalForage.prototype._extend = function _extend(libraryMethodsAndProperties) {
  933. extend(this, libraryMethodsAndProperties);
  934. };
  935. LocalForage.prototype._getSupportedDrivers = function _getSupportedDrivers(drivers) {
  936. var supportedDrivers = [];
  937. for (var i = 0, len = drivers.length; i < len; i++) {
  938. var driverName = drivers[i];
  939. if (this.supports(driverName)) {
  940. supportedDrivers.push(driverName);
  941. }
  942. }
  943. return supportedDrivers;
  944. };
  945. LocalForage.prototype._wrapLibraryMethodsWithReady = function _wrapLibraryMethodsWithReady() {
  946. // Add a stub for each driver API method that delays the call to the
  947. // corresponding driver method until localForage is ready. These stubs
  948. // will be replaced by the driver methods as soon as the driver is
  949. // loaded, so there is no performance impact.
  950. for (var i = 0; i < LibraryMethods.length; i++) {
  951. callWhenReady(this, LibraryMethods[i]);
  952. }
  953. };
  954. LocalForage.prototype.createInstance = function createInstance(options) {
  955. return new LocalForage(options);
  956. };
  957. return LocalForage;
  958. })();
  959. return new LocalForage();
  960. })(typeof window !== 'undefined' ? window : self);
  961. exports['default'] = localForage;
  962. module.exports = exports['default'];
  963. /***/ },
  964. /* 1 */
  965. /***/ function(module, exports) {
  966. // Some code originally from async_storage.js in
  967. // [Gaia](https://github.com/mozilla-b2g/gaia).
  968. 'use strict';
  969. exports.__esModule = true;
  970. var asyncStorage = (function (globalObject) {
  971. 'use strict';
  972. // Initialize IndexedDB; fall back to vendor-prefixed versions if needed.
  973. var indexedDB = indexedDB || globalObject.indexedDB || globalObject.webkitIndexedDB || globalObject.mozIndexedDB || globalObject.OIndexedDB || globalObject.msIndexedDB;
  974. // If IndexedDB isn't available, we get outta here!
  975. if (!indexedDB) {
  976. return;
  977. }
  978. var DETECT_BLOB_SUPPORT_STORE = 'local-forage-detect-blob-support';
  979. var supportsBlobs;
  980. var dbContexts;
  981. // Abstracts constructing a Blob object, so it also works in older
  982. // browsers that don't support the native Blob constructor. (i.e.
  983. // old QtWebKit versions, at least).
  984. function _createBlob(parts, properties) {
  985. parts = parts || [];
  986. properties = properties || {};
  987. try {
  988. return new Blob(parts, properties);
  989. } catch (e) {
  990. if (e.name !== 'TypeError') {
  991. throw e;
  992. }
  993. var BlobBuilder = globalObject.BlobBuilder || globalObject.MSBlobBuilder || globalObject.MozBlobBuilder || globalObject.WebKitBlobBuilder;
  994. var builder = new BlobBuilder();
  995. for (var i = 0; i < parts.length; i += 1) {
  996. builder.append(parts[i]);
  997. }
  998. return builder.getBlob(properties.type);
  999. }
  1000. }
  1001. // Transform a binary string to an array buffer, because otherwise
  1002. // weird stuff happens when you try to work with the binary string directly.
  1003. // It is known.
  1004. // From http://stackoverflow.com/questions/14967647/ (continues on next line)
  1005. // encode-decode-image-with-base64-breaks-image (2013-04-21)
  1006. function _binStringToArrayBuffer(bin) {
  1007. var length = bin.length;
  1008. var buf = new ArrayBuffer(length);
  1009. var arr = new Uint8Array(buf);
  1010. for (var i = 0; i < length; i++) {
  1011. arr[i] = bin.charCodeAt(i);
  1012. }
  1013. return buf;
  1014. }
  1015. // Fetch a blob using ajax. This reveals bugs in Chrome < 43.
  1016. // For details on all this junk:
  1017. // https://github.com/nolanlawson/state-of-binary-data-in-the-browser#readme
  1018. function _blobAjax(url) {
  1019. return new Promise(function (resolve, reject) {
  1020. var xhr = new XMLHttpRequest();
  1021. xhr.open('GET', url);
  1022. xhr.withCredentials = true;
  1023. xhr.responseType = 'arraybuffer';
  1024. xhr.onreadystatechange = function () {
  1025. if (xhr.readyState !== 4) {
  1026. return;
  1027. }
  1028. if (xhr.status === 200) {
  1029. return resolve({
  1030. response: xhr.response,
  1031. type: xhr.getResponseHeader('Content-Type')
  1032. });
  1033. }
  1034. reject({ status: xhr.status, response: xhr.response });
  1035. };
  1036. xhr.send();
  1037. });
  1038. }
  1039. //
  1040. // Detect blob support. Chrome didn't support it until version 38.
  1041. // In version 37 they had a broken version where PNGs (and possibly
  1042. // other binary types) aren't stored correctly, because when you fetch
  1043. // them, the content type is always null.
  1044. //
  1045. // Furthermore, they have some outstanding bugs where blobs occasionally
  1046. // are read by FileReader as null, or by ajax as 404s.
  1047. //
  1048. // Sadly we use the 404 bug to detect the FileReader bug, so if they
  1049. // get fixed independently and released in different versions of Chrome,
  1050. // then the bug could come back. So it's worthwhile to watch these issues:
  1051. // 404 bug: https://code.google.com/p/chromium/issues/detail?id=447916
  1052. // FileReader bug: https://code.google.com/p/chromium/issues/detail?id=447836
  1053. //
  1054. function _checkBlobSupportWithoutCaching(idb) {
  1055. return new Promise(function (resolve, reject) {
  1056. var blob = _createBlob([''], { type: 'image/png' });
  1057. var txn = idb.transaction([DETECT_BLOB_SUPPORT_STORE], 'readwrite');
  1058. txn.objectStore(DETECT_BLOB_SUPPORT_STORE).put(blob, 'key');
  1059. txn.oncomplete = function () {
  1060. // have to do it in a separate transaction, else the correct
  1061. // content type is always returned
  1062. var blobTxn = idb.transaction([DETECT_BLOB_SUPPORT_STORE], 'readwrite');
  1063. var getBlobReq = blobTxn.objectStore(DETECT_BLOB_SUPPORT_STORE).get('key');
  1064. getBlobReq.onerror = reject;
  1065. getBlobReq.onsuccess = function (e) {
  1066. var storedBlob = e.target.result;
  1067. var url = URL.createObjectURL(storedBlob);
  1068. _blobAjax(url).then(function (res) {
  1069. resolve(!!(res && res.type === 'image/png'));
  1070. }, function () {
  1071. resolve(false);
  1072. }).then(function () {
  1073. URL.revokeObjectURL(url);
  1074. });
  1075. };
  1076. };
  1077. txn.onerror = txn.onabort = reject;
  1078. })['catch'](function () {
  1079. return false; // error, so assume unsupported
  1080. });
  1081. }
  1082. function _checkBlobSupport(idb) {
  1083. if (typeof supportsBlobs === 'boolean') {
  1084. return Promise.resolve(supportsBlobs);
  1085. }
  1086. return _checkBlobSupportWithoutCaching(idb).then(function (value) {
  1087. supportsBlobs = value;
  1088. return supportsBlobs;
  1089. });
  1090. }
  1091. // encode a blob for indexeddb engines that don't support blobs
  1092. function _encodeBlob(blob) {
  1093. return new Promise(function (resolve, reject) {
  1094. var reader = new FileReader();
  1095. reader.onerror = reject;
  1096. reader.onloadend = function (e) {
  1097. var base64 = btoa(e.target.result || '');
  1098. resolve({
  1099. __local_forage_encoded_blob: true,
  1100. data: base64,
  1101. type: blob.type
  1102. });
  1103. };
  1104. reader.readAsBinaryString(blob);
  1105. });
  1106. }
  1107. // decode an encoded blob
  1108. function _decodeBlob(encodedBlob) {
  1109. var arrayBuff = _binStringToArrayBuffer(atob(encodedBlob.data));
  1110. return _createBlob([arrayBuff], { type: encodedBlob.type });
  1111. }
  1112. // is this one of our fancy encoded blobs?
  1113. function _isEncodedBlob(value) {
  1114. return value && value.__local_forage_encoded_blob;
  1115. }
  1116. // Specialize the default `ready()` function by making it dependent
  1117. // on the current database operations. Thus, the driver will be actually
  1118. // ready when it's been initialized (default) *and* there are no pending
  1119. // operations on the database (initiated by some other instances).
  1120. function _fullyReady(callback) {
  1121. var self = this;
  1122. var promise = self._initReady().then(function () {
  1123. var dbContext = dbContexts[self._dbInfo.name];
  1124. if (dbContext && dbContext.dbReady) {
  1125. return dbContext.dbReady;
  1126. }
  1127. });
  1128. promise.then(callback, callback);
  1129. return promise;
  1130. }
  1131. function _deferReadiness(dbInfo) {
  1132. var dbContext = dbContexts[dbInfo.name];
  1133. // Create a deferred object representing the current database operation.
  1134. var deferredOperation = {};
  1135. deferredOperation.promise = new Promise(function (resolve) {
  1136. deferredOperation.resolve = resolve;
  1137. });
  1138. // Enqueue the deferred operation.
  1139. dbContext.deferredOperations.push(deferredOperation);
  1140. // Chain its promise to the database readiness.
  1141. if (!dbContext.dbReady) {
  1142. dbContext.dbReady = deferredOperation.promise;
  1143. } else {
  1144. dbContext.dbReady = dbContext.dbReady.then(function () {
  1145. return deferredOperation.promise;
  1146. });
  1147. }
  1148. }
  1149. function _advanceReadiness(dbInfo) {
  1150. var dbContext = dbContexts[dbInfo.name];
  1151. // Dequeue a deferred operation.
  1152. var deferredOperation = dbContext.deferredOperations.pop();
  1153. // Resolve its promise (which is part of the database readiness
  1154. // chain of promises).
  1155. if (deferredOperation) {
  1156. deferredOperation.resolve();
  1157. }
  1158. }
  1159. // Open the IndexedDB database (automatically creates one if one didn't
  1160. // previously exist), using any options set in the config.
  1161. function _initStorage(options) {
  1162. var self = this;
  1163. var dbInfo = {
  1164. db: null
  1165. };
  1166. if (options) {
  1167. for (var i in options) {
  1168. dbInfo[i] = options[i];
  1169. }
  1170. }
  1171. // Initialize a singleton container for all running localForages.
  1172. if (!dbContexts) {
  1173. dbContexts = {};
  1174. }
  1175. // Get the current context of the database;
  1176. var dbContext = dbContexts[dbInfo.name];
  1177. // ...or create a new context.
  1178. if (!dbContext) {
  1179. dbContext = {
  1180. // Running localForages sharing a database.
  1181. forages: [],
  1182. // Shared database.
  1183. db: null,
  1184. // Database readiness (promise).
  1185. dbReady: null,
  1186. // Deferred operations on the database.
  1187. deferredOperations: []
  1188. };
  1189. // Register the new context in the global container.
  1190. dbContexts[dbInfo.name] = dbContext;
  1191. }
  1192. // Register itself as a running localForage in the current context.
  1193. dbContext.forages.push(self);
  1194. // Replace the default `ready()` function with the specialized one.
  1195. if (!self._initReady) {
  1196. self._initReady = self.ready;
  1197. self.ready = _fullyReady;
  1198. }
  1199. // Create an array of initialization states of the related localForages.
  1200. var initPromises = [];
  1201. function ignoreErrors() {
  1202. // Don't handle errors here,
  1203. // just makes sure related localForages aren't pending.
  1204. return Promise.resolve();
  1205. }
  1206. for (var j = 0; j < dbContext.forages.length; j++) {
  1207. var forage = dbContext.forages[j];
  1208. if (forage !== self) {
  1209. // Don't wait for itself...
  1210. initPromises.push(forage._initReady()['catch'](ignoreErrors));
  1211. }
  1212. }
  1213. // Take a snapshot of the related localForages.
  1214. var forages = dbContext.forages.slice(0);
  1215. // Initialize the connection process only when
  1216. // all the related localForages aren't pending.
  1217. return Promise.all(initPromises).then(function () {
  1218. dbInfo.db = dbContext.db;
  1219. // Get the connection or open a new one without upgrade.
  1220. return _getOriginalConnection(dbInfo);
  1221. }).then(function (db) {
  1222. dbInfo.db = db;
  1223. if (_isUpgradeNeeded(dbInfo, self._defaultConfig.version)) {
  1224. // Reopen the database for upgrading.
  1225. return _getUpgradedConnection(dbInfo);
  1226. }
  1227. return db;
  1228. }).then(function (db) {
  1229. dbInfo.db = dbContext.db = db;
  1230. self._dbInfo = dbInfo;
  1231. // Share the final connection amongst related localForages.
  1232. for (var k = 0; k < forages.length; k++) {
  1233. var forage = forages[k];
  1234. if (forage !== self) {
  1235. // Self is already up-to-date.
  1236. forage._dbInfo.db = dbInfo.db;
  1237. forage._dbInfo.version = dbInfo.version;
  1238. }
  1239. }
  1240. });
  1241. }
  1242. function _getOriginalConnection(dbInfo) {
  1243. return _getConnection(dbInfo, false);
  1244. }
  1245. function _getUpgradedConnection(dbInfo) {
  1246. return _getConnection(dbInfo, true);
  1247. }
  1248. function _getConnection(dbInfo, upgradeNeeded) {
  1249. return new Promise(function (resolve, reject) {
  1250. if (dbInfo.db) {
  1251. if (upgradeNeeded) {
  1252. _deferReadiness(dbInfo);
  1253. dbInfo.db.close();
  1254. } else {
  1255. return resolve(dbInfo.db);
  1256. }
  1257. }
  1258. var dbArgs = [dbInfo.name];
  1259. if (upgradeNeeded) {
  1260. dbArgs.push(dbInfo.version);
  1261. }
  1262. var openreq = indexedDB.open.apply(indexedDB, dbArgs);
  1263. if (upgradeNeeded) {
  1264. openreq.onupgradeneeded = function (e) {
  1265. var db = openreq.result;
  1266. try {
  1267. db.createObjectStore(dbInfo.storeName);
  1268. if (e.oldVersion <= 1) {
  1269. // Added when support for blob shims was added
  1270. db.createObjectStore(DETECT_BLOB_SUPPORT_STORE);
  1271. }
  1272. } catch (ex) {
  1273. if (ex.name === 'ConstraintError') {
  1274. globalObject.console.warn('The database "' + dbInfo.name + '"' + ' has been upgraded from version ' + e.oldVersion + ' to version ' + e.newVersion + ', but the storage "' + dbInfo.storeName + '" already exists.');
  1275. } else {
  1276. throw ex;
  1277. }
  1278. }
  1279. };
  1280. }
  1281. openreq.onerror = function () {
  1282. reject(openreq.error);
  1283. };
  1284. openreq.onsuccess = function () {
  1285. resolve(openreq.result);
  1286. _advanceReadiness(dbInfo);
  1287. };
  1288. });
  1289. }
  1290. function _isUpgradeNeeded(dbInfo, defaultVersion) {
  1291. if (!dbInfo.db) {
  1292. return true;
  1293. }
  1294. var isNewStore = !dbInfo.db.objectStoreNames.contains(dbInfo.storeName);
  1295. var isDowngrade = dbInfo.version < dbInfo.db.version;
  1296. var isUpgrade = dbInfo.version > dbInfo.db.version;
  1297. if (isDowngrade) {
  1298. // If the version is not the default one
  1299. // then warn for impossible downgrade.
  1300. if (dbInfo.version !== defaultVersion) {
  1301. globalObject.console.warn('The database "' + dbInfo.name + '"' + ' can\'t be downgraded from version ' + dbInfo.db.version + ' to version ' + dbInfo.version + '.');
  1302. }
  1303. // Align the versions to prevent errors.
  1304. dbInfo.version = dbInfo.db.version;
  1305. }
  1306. if (isUpgrade || isNewStore) {
  1307. // If the store is new then increment the version (if needed).
  1308. // This will trigger an "upgradeneeded" event which is required
  1309. // for creating a store.
  1310. if (isNewStore) {
  1311. var incVersion = dbInfo.db.version + 1;
  1312. if (incVersion > dbInfo.version) {
  1313. dbInfo.version = incVersion;
  1314. }
  1315. }
  1316. return true;
  1317. }
  1318. return false;
  1319. }
  1320. function getItem(key, callback) {
  1321. var self = this;
  1322. // Cast the key to a string, as that's all we can set as a key.
  1323. if (typeof key !== 'string') {
  1324. globalObject.console.warn(key + ' used as a key, but it is not a string.');
  1325. key = String(key);
  1326. }
  1327. var promise = new Promise(function (resolve, reject) {
  1328. self.ready().then(function () {
  1329. var dbInfo = self._dbInfo;
  1330. var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
  1331. var req = store.get(key);
  1332. req.onsuccess = function () {
  1333. var value = req.result;
  1334. if (value === undefined) {
  1335. value = null;
  1336. }
  1337. if (_isEncodedBlob(value)) {
  1338. value = _decodeBlob(value);
  1339. }
  1340. resolve(value);
  1341. };
  1342. req.onerror = function () {
  1343. reject(req.error);
  1344. };
  1345. })['catch'](reject);
  1346. });
  1347. executeCallback(promise, callback);
  1348. return promise;
  1349. }
  1350. // Iterate over all items stored in database.
  1351. function iterate(iterator, callback) {
  1352. var self = this;
  1353. var promise = new Promise(function (resolve, reject) {
  1354. self.ready().then(function () {
  1355. var dbInfo = self._dbInfo;
  1356. var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
  1357. var req = store.openCursor();
  1358. var iterationNumber = 1;
  1359. req.onsuccess = function () {
  1360. var cursor = req.result;
  1361. if (cursor) {
  1362. var value = cursor.value;
  1363. if (_isEncodedBlob(value)) {
  1364. value = _decodeBlob(value);
  1365. }
  1366. var result = iterator(value, cursor.key, iterationNumber++);
  1367. if (result !== void 0) {
  1368. resolve(result);
  1369. } else {
  1370. cursor['continue']();
  1371. }
  1372. } else {
  1373. resolve();
  1374. }
  1375. };
  1376. req.onerror = function () {
  1377. reject(req.error);
  1378. };
  1379. })['catch'](reject);
  1380. });
  1381. executeCallback(promise, callback);
  1382. return promise;
  1383. }
  1384. function setItem(key, value, callback) {
  1385. var self = this;
  1386. // Cast the key to a string, as that's all we can set as a key.
  1387. if (typeof key !== 'string') {
  1388. globalObject.console.warn(key + ' used as a key, but it is not a string.');
  1389. key = String(key);
  1390. }
  1391. var promise = new Promise(function (resolve, reject) {
  1392. var dbInfo;
  1393. self.ready().then(function () {
  1394. dbInfo = self._dbInfo;
  1395. if (value instanceof Blob) {
  1396. return _checkBlobSupport(dbInfo.db).then(function (blobSupport) {
  1397. if (blobSupport) {
  1398. return value;
  1399. }
  1400. return _encodeBlob(value);
  1401. });
  1402. }
  1403. return value;
  1404. }).then(function (value) {
  1405. var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite');
  1406. var store = transaction.objectStore(dbInfo.storeName);
  1407. // The reason we don't _save_ null is because IE 10 does
  1408. // not support saving the `null` type in IndexedDB. How
  1409. // ironic, given the bug below!
  1410. // See: https://github.com/mozilla/localForage/issues/161
  1411. if (value === null) {
  1412. value = undefined;
  1413. }
  1414. transaction.oncomplete = function () {
  1415. // Cast to undefined so the value passed to
  1416. // callback/promise is the same as what one would get out
  1417. // of `getItem()` later. This leads to some weirdness
  1418. // (setItem('foo', undefined) will return `null`), but
  1419. // it's not my fault localStorage is our baseline and that
  1420. // it's weird.
  1421. if (value === undefined) {
  1422. value = null;
  1423. }
  1424. resolve(value);
  1425. };
  1426. transaction.onabort = transaction.onerror = function () {
  1427. var err = req.error ? req.error : req.transaction.error;
  1428. reject(err);
  1429. };
  1430. var req = store.put(value, key);
  1431. })['catch'](reject);
  1432. });
  1433. executeCallback(promise, callback);
  1434. return promise;
  1435. }
  1436. function removeItem(key, callback) {
  1437. var self = this;
  1438. // Cast the key to a string, as that's all we can set as a key.
  1439. if (typeof key !== 'string') {
  1440. globalObject.console.warn(key + ' used as a key, but it is not a string.');
  1441. key = String(key);
  1442. }
  1443. var promise = new Promise(function (resolve, reject) {
  1444. self.ready().then(function () {
  1445. var dbInfo = self._dbInfo;
  1446. var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite');
  1447. var store = transaction.objectStore(dbInfo.storeName);
  1448. // We use a Grunt task to make this safe for IE and some
  1449. // versions of Android (including those used by Cordova).
  1450. // Normally IE won't like `.delete()` and will insist on
  1451. // using `['delete']()`, but we have a build step that
  1452. // fixes this for us now.
  1453. var req = store['delete'](key);
  1454. transaction.oncomplete = function () {
  1455. resolve();
  1456. };
  1457. transaction.onerror = function () {
  1458. reject(req.error);
  1459. };
  1460. // The request will be also be aborted if we've exceeded our storage
  1461. // space.
  1462. transaction.onabort = function () {
  1463. var err = req.error ? req.error : req.transaction.error;
  1464. reject(err);
  1465. };
  1466. })['catch'](reject);
  1467. });
  1468. executeCallback(promise, callback);
  1469. return promise;
  1470. }
  1471. function clear(callback) {
  1472. var self = this;
  1473. var promise = new Promise(function (resolve, reject) {
  1474. self.ready().then(function () {
  1475. var dbInfo = self._dbInfo;
  1476. var transaction = dbInfo.db.transaction(dbInfo.storeName, 'readwrite');
  1477. var store = transaction.objectStore(dbInfo.storeName);
  1478. var req = store.clear();
  1479. transaction.oncomplete = function () {
  1480. resolve();
  1481. };
  1482. transaction.onabort = transaction.onerror = function () {
  1483. var err = req.error ? req.error : req.transaction.error;
  1484. reject(err);
  1485. };
  1486. })['catch'](reject);
  1487. });
  1488. executeCallback(promise, callback);
  1489. return promise;
  1490. }
  1491. function length(callback) {
  1492. var self = this;
  1493. var promise = new Promise(function (resolve, reject) {
  1494. self.ready().then(function () {
  1495. var dbInfo = self._dbInfo;
  1496. var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
  1497. var req = store.count();
  1498. req.onsuccess = function () {
  1499. resolve(req.result);
  1500. };
  1501. req.onerror = function () {
  1502. reject(req.error);
  1503. };
  1504. })['catch'](reject);
  1505. });
  1506. executeCallback(promise, callback);
  1507. return promise;
  1508. }
  1509. function key(n, callback) {
  1510. var self = this;
  1511. var promise = new Promise(function (resolve, reject) {
  1512. if (n < 0) {
  1513. resolve(null);
  1514. return;
  1515. }
  1516. self.ready().then(function () {
  1517. var dbInfo = self._dbInfo;
  1518. var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
  1519. var advanced = false;
  1520. var req = store.openCursor();
  1521. req.onsuccess = function () {
  1522. var cursor = req.result;
  1523. if (!cursor) {
  1524. // this means there weren't enough keys
  1525. resolve(null);
  1526. return;
  1527. }
  1528. if (n === 0) {
  1529. // We have the first key, return it if that's what they
  1530. // wanted.
  1531. resolve(cursor.key);
  1532. } else {
  1533. if (!advanced) {
  1534. // Otherwise, ask the cursor to skip ahead n
  1535. // records.
  1536. advanced = true;
  1537. cursor.advance(n);
  1538. } else {
  1539. // When we get here, we've got the nth key.
  1540. resolve(cursor.key);
  1541. }
  1542. }
  1543. };
  1544. req.onerror = function () {
  1545. reject(req.error);
  1546. };
  1547. })['catch'](reject);
  1548. });
  1549. executeCallback(promise, callback);
  1550. return promise;
  1551. }
  1552. function keys(callback) {
  1553. var self = this;
  1554. var promise = new Promise(function (resolve, reject) {
  1555. self.ready().then(function () {
  1556. var dbInfo = self._dbInfo;
  1557. var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
  1558. var req = store.openCursor();
  1559. var keys = [];
  1560. req.onsuccess = function () {
  1561. var cursor = req.result;
  1562. if (!cursor) {
  1563. resolve(keys);
  1564. return;
  1565. }
  1566. keys.push(cursor.key);
  1567. cursor['continue']();
  1568. };
  1569. req.onerror = function () {
  1570. reject(req.error);
  1571. };
  1572. })['catch'](reject);
  1573. });
  1574. executeCallback(promise, callback);
  1575. return promise;
  1576. }
  1577. function executeCallback(promise, callback) {
  1578. if (callback) {
  1579. promise.then(function (result) {
  1580. callback(null, result);
  1581. }, function (error) {
  1582. callback(error);
  1583. });
  1584. }
  1585. }
  1586. var asyncStorage = {
  1587. _driver: 'asyncStorage',
  1588. _initStorage: _initStorage,
  1589. iterate: iterate,
  1590. getItem: getItem,
  1591. setItem: setItem,
  1592. removeItem: removeItem,
  1593. clear: clear,
  1594. length: length,
  1595. key: key,
  1596. keys: keys
  1597. };
  1598. return asyncStorage;
  1599. })(typeof window !== 'undefined' ? window : self);
  1600. exports['default'] = asyncStorage;
  1601. module.exports = exports['default'];
  1602. /***/ },
  1603. /* 2 */
  1604. /***/ function(module, exports, __webpack_require__) {
  1605. // If IndexedDB isn't available, we'll fall back to localStorage.
  1606. // Note that this will have considerable performance and storage
  1607. // side-effects (all data will be serialized on save and only data that
  1608. // can be converted to a string via `JSON.stringify()` will be saved).
  1609. 'use strict';
  1610. exports.__esModule = true;
  1611. var localStorageWrapper = (function (globalObject) {
  1612. 'use strict';
  1613. var localStorage = null;
  1614. // If the app is running inside a Google Chrome packaged webapp, or some
  1615. // other context where localStorage isn't available, we don't use
  1616. // localStorage. This feature detection is preferred over the old
  1617. // `if (window.chrome && window.chrome.runtime)` code.
  1618. // See: https://github.com/mozilla/localForage/issues/68
  1619. try {
  1620. // If localStorage isn't available, we get outta here!
  1621. // This should be inside a try catch
  1622. if (!globalObject.localStorage || !('setItem' in globalObject.localStorage)) {
  1623. return;
  1624. }
  1625. // Initialize localStorage and create a variable to use throughout
  1626. // the code.
  1627. localStorage = globalObject.localStorage;
  1628. } catch (e) {
  1629. return;
  1630. }
  1631. // Config the localStorage backend, using options set in the config.
  1632. function _initStorage(options) {
  1633. var self = this;
  1634. var dbInfo = {};
  1635. if (options) {
  1636. for (var i in options) {
  1637. dbInfo[i] = options[i];
  1638. }
  1639. }
  1640. dbInfo.keyPrefix = dbInfo.name + '/';
  1641. if (dbInfo.storeName !== self._defaultConfig.storeName) {
  1642. dbInfo.keyPrefix += dbInfo.storeName + '/';
  1643. }
  1644. self._dbInfo = dbInfo;
  1645. return new Promise(function (resolve, reject) {
  1646. resolve(__webpack_require__(3));
  1647. }).then(function (lib) {
  1648. dbInfo.serializer = lib;
  1649. return Promise.resolve();
  1650. });
  1651. }
  1652. // Remove all keys from the datastore, effectively destroying all data in
  1653. // the app's key/value store!
  1654. function clear(callback) {
  1655. var self = this;
  1656. var promise = self.ready().then(function () {
  1657. var keyPrefix = self._dbInfo.keyPrefix;
  1658. for (var i = localStorage.length - 1; i >= 0; i--) {
  1659. var key = localStorage.key(i);
  1660. if (key.indexOf(keyPrefix) === 0) {
  1661. localStorage.removeItem(key);
  1662. }
  1663. }
  1664. });
  1665. executeCallback(promise, callback);
  1666. return promise;
  1667. }
  1668. // Retrieve an item from the store. Unlike the original async_storage
  1669. // library in Gaia, we don't modify return values at all. If a key's value
  1670. // is `undefined`, we pass that value to the callback function.
  1671. function getItem(key, callback) {
  1672. var self = this;
  1673. // Cast the key to a string, as that's all we can set as a key.
  1674. if (typeof key !== 'string') {
  1675. globalObject.console.warn(key + ' used as a key, but it is not a string.');
  1676. key = String(key);
  1677. }
  1678. var promise = self.ready().then(function () {
  1679. var dbInfo = self._dbInfo;
  1680. var result = localStorage.getItem(dbInfo.keyPrefix + key);
  1681. // If a result was found, parse it from the serialized
  1682. // string into a JS object. If result isn't truthy, the key
  1683. // is likely undefined and we'll pass it straight to the
  1684. // callback.
  1685. if (result) {
  1686. result = dbInfo.serializer.deserialize(result);
  1687. }
  1688. return result;
  1689. });
  1690. executeCallback(promise, callback);
  1691. return promise;
  1692. }
  1693. // Iterate over all items in the store.
  1694. function iterate(iterator, callback) {
  1695. var self = this;
  1696. var promise = self.ready().then(function () {
  1697. var dbInfo = self._dbInfo;
  1698. var keyPrefix = dbInfo.keyPrefix;
  1699. var keyPrefixLength = keyPrefix.length;
  1700. var length = localStorage.length;
  1701. // We use a dedicated iterator instead of the `i` variable below
  1702. // so other keys we fetch in localStorage aren't counted in
  1703. // the `iterationNumber` argument passed to the `iterate()`
  1704. // callback.
  1705. //
  1706. // See: github.com/mozilla/localForage/pull/435#discussion_r38061530
  1707. var iterationNumber = 1;
  1708. for (var i = 0; i < length; i++) {
  1709. var key = localStorage.key(i);
  1710. if (key.indexOf(keyPrefix) !== 0) {
  1711. continue;
  1712. }
  1713. var value = localStorage.getItem(key);
  1714. // If a result was found, parse it from the serialized
  1715. // string into a JS object. If result isn't truthy, the
  1716. // key is likely undefined and we'll pass it straight
  1717. // to the iterator.
  1718. if (value) {
  1719. value = dbInfo.serializer.deserialize(value);
  1720. }
  1721. value = iterator(value, key.substring(keyPrefixLength), iterationNumber++);
  1722. if (value !== void 0) {
  1723. return value;
  1724. }
  1725. }
  1726. });
  1727. executeCallback(promise, callback);
  1728. return promise;
  1729. }
  1730. // Same as localStorage's key() method, except takes a callback.
  1731. function key(n, callback) {
  1732. var self = this;
  1733. var promise = self.ready().then(function () {
  1734. var dbInfo = self._dbInfo;
  1735. var result;
  1736. try {
  1737. result = localStorage.key(n);
  1738. } catch (error) {
  1739. result = null;
  1740. }
  1741. // Remove the prefix from the key, if a key is found.
  1742. if (result) {
  1743. result = result.substring(dbInfo.keyPrefix.length);
  1744. }
  1745. return result;
  1746. });
  1747. executeCallback(promise, callback);
  1748. return promise;
  1749. }
  1750. function keys(callback) {
  1751. var self = this;
  1752. var promise = self.ready().then(function () {
  1753. var dbInfo = self._dbInfo;
  1754. var length = localStorage.length;
  1755. var keys = [];
  1756. for (var i = 0; i < length; i++) {
  1757. if (localStorage.key(i).indexOf(dbInfo.keyPrefix) === 0) {
  1758. keys.push(localStorage.key(i).substring(dbInfo.keyPrefix.length));
  1759. }
  1760. }
  1761. return keys;
  1762. });
  1763. executeCallback(promise, callback);
  1764. return promise;
  1765. }
  1766. // Supply the number of keys in the datastore to the callback function.
  1767. function length(callback) {
  1768. var self = this;
  1769. var promise = self.keys().then(function (keys) {
  1770. return keys.length;
  1771. });
  1772. executeCallback(promise, callback);
  1773. return promise;
  1774. }
  1775. // Remove an item from the store, nice and simple.
  1776. function removeItem(key, callback) {
  1777. var self = this;
  1778. // Cast the key to a string, as that's all we can set as a key.
  1779. if (typeof key !== 'string') {
  1780. globalObject.console.warn(key + ' used as a key, but it is not a string.');
  1781. key = String(key);
  1782. }
  1783. var promise = self.ready().then(function () {
  1784. var dbInfo = self._dbInfo;
  1785. localStorage.removeItem(dbInfo.keyPrefix + key);
  1786. });
  1787. executeCallback(promise, callback);
  1788. return promise;
  1789. }
  1790. // Set a key's value and run an optional callback once the value is set.
  1791. // Unlike Gaia's implementation, the callback function is passed the value,
  1792. // in case you want to operate on that value only after you're sure it
  1793. // saved, or something like that.
  1794. function setItem(key, value, callback) {
  1795. var self = this;
  1796. // Cast the key to a string, as that's all we can set as a key.
  1797. if (typeof key !== 'string') {
  1798. globalObject.console.warn(key + ' used as a key, but it is not a string.');
  1799. key = String(key);
  1800. }
  1801. var promise = self.ready().then(function () {
  1802. // Convert undefined values to null.
  1803. // https://github.com/mozilla/localForage/pull/42
  1804. if (value === undefined) {
  1805. value = null;
  1806. }
  1807. // Save the original value to pass to the callback.
  1808. var originalValue = value;
  1809. return new Promise(function (resolve, reject) {
  1810. var dbInfo = self._dbInfo;
  1811. dbInfo.serializer.serialize(value, function (value, error) {
  1812. if (error) {
  1813. reject(error);
  1814. } else {
  1815. try {
  1816. localStorage.setItem(dbInfo.keyPrefix + key, value);
  1817. resolve(originalValue);
  1818. } catch (e) {
  1819. // localStorage capacity exceeded.
  1820. // TODO: Make this a specific error/event.
  1821. if (e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
  1822. reject(e);
  1823. }
  1824. reject(e);
  1825. }
  1826. }
  1827. });
  1828. });
  1829. });
  1830. executeCallback(promise, callback);
  1831. return promise;
  1832. }
  1833. function executeCallback(promise, callback) {
  1834. if (callback) {
  1835. promise.then(function (result) {
  1836. callback(null, result);
  1837. }, function (error) {
  1838. callback(error);
  1839. });
  1840. }
  1841. }
  1842. var localStorageWrapper = {
  1843. _driver: 'localStorageWrapper',
  1844. _initStorage: _initStorage,
  1845. // Default API, from Gaia/localStorage.
  1846. iterate: iterate,
  1847. getItem: getItem,
  1848. setItem: setItem,
  1849. removeItem: removeItem,
  1850. clear: clear,
  1851. length: length,
  1852. key: key,
  1853. keys: keys
  1854. };
  1855. return localStorageWrapper;
  1856. })(typeof window !== 'undefined' ? window : self);
  1857. exports['default'] = localStorageWrapper;
  1858. module.exports = exports['default'];
  1859. /***/ },
  1860. /* 3 */
  1861. /***/ function(module, exports) {
  1862. 'use strict';
  1863. exports.__esModule = true;
  1864. var localforageSerializer = (function (globalObject) {
  1865. 'use strict';
  1866. // Sadly, the best way to save binary data in WebSQL/localStorage is serializing
  1867. // it to Base64, so this is how we store it to prevent very strange errors with less
  1868. // verbose ways of binary <-> string data storage.
  1869. var BASE_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  1870. var BLOB_TYPE_PREFIX = '~~local_forage_type~';
  1871. var BLOB_TYPE_PREFIX_REGEX = /^~~local_forage_type~([^~]+)~/;
  1872. var SERIALIZED_MARKER = '__lfsc__:';
  1873. var SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER.length;
  1874. // OMG the serializations!
  1875. var TYPE_ARRAYBUFFER = 'arbf';
  1876. var TYPE_BLOB = 'blob';
  1877. var TYPE_INT8ARRAY = 'si08';
  1878. var TYPE_UINT8ARRAY = 'ui08';
  1879. var TYPE_UINT8CLAMPEDARRAY = 'uic8';
  1880. var TYPE_INT16ARRAY = 'si16';
  1881. var TYPE_INT32ARRAY = 'si32';
  1882. var TYPE_UINT16ARRAY = 'ur16';
  1883. var TYPE_UINT32ARRAY = 'ui32';
  1884. var TYPE_FLOAT32ARRAY = 'fl32';
  1885. var TYPE_FLOAT64ARRAY = 'fl64';
  1886. var TYPE_SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER_LENGTH + TYPE_ARRAYBUFFER.length;
  1887. // Abstracts constructing a Blob object, so it also works in older
  1888. // browsers that don't support the native Blob constructor. (i.e.
  1889. // old QtWebKit versions, at least).
  1890. function _createBlob(parts, properties) {
  1891. parts = parts || [];
  1892. properties = properties || {};
  1893. try {
  1894. return new Blob(parts, properties);
  1895. } catch (err) {
  1896. if (err.name !== 'TypeError') {
  1897. throw err;
  1898. }
  1899. var BlobBuilder = globalObject.BlobBuilder || globalObject.MSBlobBuilder || globalObject.MozBlobBuilder || globalObject.WebKitBlobBuilder;
  1900. var builder = new BlobBuilder();
  1901. for (var i = 0; i < parts.length; i += 1) {
  1902. builder.append(parts[i]);
  1903. }
  1904. return builder.getBlob(properties.type);
  1905. }
  1906. }
  1907. // Serialize a value, afterwards executing a callback (which usually
  1908. // instructs the `setItem()` callback/promise to be executed). This is how
  1909. // we store binary data with localStorage.
  1910. function serialize(value, callback) {
  1911. var valueString = '';
  1912. if (value) {
  1913. valueString = value.toString();
  1914. }
  1915. // Cannot use `value instanceof ArrayBuffer` or such here, as these
  1916. // checks fail when running the tests using casper.js...
  1917. //
  1918. // TODO: See why those tests fail and use a better solution.
  1919. if (value && (value.toString() === '[object ArrayBuffer]' || value.buffer && value.buffer.toString() === '[object ArrayBuffer]')) {
  1920. // Convert binary arrays to a string and prefix the string with
  1921. // a special marker.
  1922. var buffer;
  1923. var marker = SERIALIZED_MARKER;
  1924. if (value instanceof ArrayBuffer) {
  1925. buffer = value;
  1926. marker += TYPE_ARRAYBUFFER;
  1927. } else {
  1928. buffer = value.buffer;
  1929. if (valueString === '[object Int8Array]') {
  1930. marker += TYPE_INT8ARRAY;
  1931. } else if (valueString === '[object Uint8Array]') {
  1932. marker += TYPE_UINT8ARRAY;
  1933. } else if (valueString === '[object Uint8ClampedArray]') {
  1934. marker += TYPE_UINT8CLAMPEDARRAY;
  1935. } else if (valueString === '[object Int16Array]') {
  1936. marker += TYPE_INT16ARRAY;
  1937. } else if (valueString === '[object Uint16Array]') {
  1938. marker += TYPE_UINT16ARRAY;
  1939. } else if (valueString === '[object Int32Array]') {
  1940. marker += TYPE_INT32ARRAY;
  1941. } else if (valueString === '[object Uint32Array]') {
  1942. marker += TYPE_UINT32ARRAY;
  1943. } else if (valueString === '[object Float32Array]') {
  1944. marker += TYPE_FLOAT32ARRAY;
  1945. } else if (valueString === '[object Float64Array]') {
  1946. marker += TYPE_FLOAT64ARRAY;
  1947. } else {
  1948. callback(new Error('Failed to get type for BinaryArray'));
  1949. }
  1950. }
  1951. callback(marker + bufferToString(buffer));
  1952. } else if (valueString === '[object Blob]') {
  1953. // Conver the blob to a binaryArray and then to a string.
  1954. var fileReader = new FileReader();
  1955. fileReader.onload = function () {
  1956. // Backwards-compatible prefix for the blob type.
  1957. var str = BLOB_TYPE_PREFIX + value.type + '~' + bufferToString(this.result);
  1958. callback(SERIALIZED_MARKER + TYPE_BLOB + str);
  1959. };
  1960. fileReader.readAsArrayBuffer(value);
  1961. } else {
  1962. try {
  1963. callback(JSON.stringify(value));
  1964. } catch (e) {
  1965. console.error("Couldn't convert value into a JSON string: ", value);
  1966. callback(null, e);
  1967. }
  1968. }
  1969. }
  1970. // Deserialize data we've inserted into a value column/field. We place
  1971. // special markers into our strings to mark them as encoded; this isn't
  1972. // as nice as a meta field, but it's the only sane thing we can do whilst
  1973. // keeping localStorage support intact.
  1974. //
  1975. // Oftentimes this will just deserialize JSON content, but if we have a
  1976. // special marker (SERIALIZED_MARKER, defined above), we will extract
  1977. // some kind of arraybuffer/binary data/typed array out of the string.
  1978. function deserialize(value) {
  1979. // If we haven't marked this string as being specially serialized (i.e.
  1980. // something other than serialized JSON), we can just return it and be
  1981. // done with it.
  1982. if (value.substring(0, SERIALIZED_MARKER_LENGTH) !== SERIALIZED_MARKER) {
  1983. return JSON.parse(value);
  1984. }
  1985. // The following code deals with deserializing some kind of Blob or
  1986. // TypedArray. First we separate out the type of data we're dealing
  1987. // with from the data itself.
  1988. var serializedString = value.substring(TYPE_SERIALIZED_MARKER_LENGTH);
  1989. var type = value.substring(SERIALIZED_MARKER_LENGTH, TYPE_SERIALIZED_MARKER_LENGTH);
  1990. var blobType;
  1991. // Backwards-compatible blob type serialization strategy.
  1992. // DBs created with older versions of localForage will simply not have the blob type.
  1993. if (type === TYPE_BLOB && BLOB_TYPE_PREFIX_REGEX.test(serializedString)) {
  1994. var matcher = serializedString.match(BLOB_TYPE_PREFIX_REGEX);
  1995. blobType = matcher[1];
  1996. serializedString = serializedString.substring(matcher[0].length);
  1997. }
  1998. var buffer = stringToBuffer(serializedString);
  1999. // Return the right type based on the code/type set during
  2000. // serialization.
  2001. switch (type) {
  2002. case TYPE_ARRAYBUFFER:
  2003. return buffer;
  2004. case TYPE_BLOB:
  2005. return _createBlob([buffer], { type: blobType });
  2006. case TYPE_INT8ARRAY:
  2007. return new Int8Array(buffer);
  2008. case TYPE_UINT8ARRAY:
  2009. return new Uint8Array(buffer);
  2010. case TYPE_UINT8CLAMPEDARRAY:
  2011. return new Uint8ClampedArray(buffer);
  2012. case TYPE_INT16ARRAY:
  2013. return new Int16Array(buffer);
  2014. case TYPE_UINT16ARRAY:
  2015. return new Uint16Array(buffer);
  2016. case TYPE_INT32ARRAY:
  2017. return new Int32Array(buffer);
  2018. case TYPE_UINT32ARRAY:
  2019. return new Uint32Array(buffer);
  2020. case TYPE_FLOAT32ARRAY:
  2021. return new Float32Array(buffer);
  2022. case TYPE_FLOAT64ARRAY:
  2023. return new Float64Array(buffer);
  2024. default:
  2025. throw new Error('Unkown type: ' + type);
  2026. }
  2027. }
  2028. function stringToBuffer(serializedString) {
  2029. // Fill the string into a ArrayBuffer.
  2030. var bufferLength = serializedString.length * 0.75;
  2031. var len = serializedString.length;
  2032. var i;
  2033. var p = 0;
  2034. var encoded1, encoded2, encoded3, encoded4;
  2035. if (serializedString[serializedString.length - 1] === '=') {
  2036. bufferLength--;
  2037. if (serializedString[serializedString.length - 2] === '=') {
  2038. bufferLength--;
  2039. }
  2040. }
  2041. var buffer = new ArrayBuffer(bufferLength);
  2042. var bytes = new Uint8Array(buffer);
  2043. for (i = 0; i < len; i += 4) {
  2044. encoded1 = BASE_CHARS.indexOf(serializedString[i]);
  2045. encoded2 = BASE_CHARS.indexOf(serializedString[i + 1]);
  2046. encoded3 = BASE_CHARS.indexOf(serializedString[i + 2]);
  2047. encoded4 = BASE_CHARS.indexOf(serializedString[i + 3]);
  2048. /*jslint bitwise: true */
  2049. bytes[p++] = encoded1 << 2 | encoded2 >> 4;
  2050. bytes[p++] = (encoded2 & 15) << 4 | encoded3 >> 2;
  2051. bytes[p++] = (encoded3 & 3) << 6 | encoded4 & 63;
  2052. }
  2053. return buffer;
  2054. }
  2055. // Converts a buffer to a string to store, serialized, in the backend
  2056. // storage library.
  2057. function bufferToString(buffer) {
  2058. // base64-arraybuffer
  2059. var bytes = new Uint8Array(buffer);
  2060. var base64String = '';
  2061. var i;
  2062. for (i = 0; i < bytes.length; i += 3) {
  2063. /*jslint bitwise: true */
  2064. base64String += BASE_CHARS[bytes[i] >> 2];
  2065. base64String += BASE_CHARS[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4];
  2066. base64String += BASE_CHARS[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6];
  2067. base64String += BASE_CHARS[bytes[i + 2] & 63];
  2068. }
  2069. if (bytes.length % 3 === 2) {
  2070. base64String = base64String.substring(0, base64String.length - 1) + '=';
  2071. } else if (bytes.length % 3 === 1) {
  2072. base64String = base64String.substring(0, base64String.length - 2) + '==';
  2073. }
  2074. return base64String;
  2075. }
  2076. var localforageSerializer = {
  2077. serialize: serialize,
  2078. deserialize: deserialize,
  2079. stringToBuffer: stringToBuffer,
  2080. bufferToString: bufferToString
  2081. };
  2082. return localforageSerializer;
  2083. })(typeof window !== 'undefined' ? window : self);
  2084. exports['default'] = localforageSerializer;
  2085. module.exports = exports['default'];
  2086. /***/ },
  2087. /* 4 */
  2088. /***/ function(module, exports, __webpack_require__) {
  2089. /*
  2090. * Includes code from:
  2091. *
  2092. * base64-arraybuffer
  2093. * https://github.com/niklasvh/base64-arraybuffer
  2094. *
  2095. * Copyright (c) 2012 Niklas von Hertzen
  2096. * Licensed under the MIT license.
  2097. */
  2098. 'use strict';
  2099. exports.__esModule = true;
  2100. var webSQLStorage = (function (globalObject) {
  2101. 'use strict';
  2102. var openDatabase = globalObject.openDatabase;
  2103. // If WebSQL methods aren't available, we can stop now.
  2104. if (!openDatabase) {
  2105. return;
  2106. }
  2107. // Open the WebSQL database (automatically creates one if one didn't
  2108. // previously exist), using any options set in the config.
  2109. function _initStorage(options) {
  2110. var self = this;
  2111. var dbInfo = {
  2112. db: null
  2113. };
  2114. if (options) {
  2115. for (var i in options) {
  2116. dbInfo[i] = typeof options[i] !== 'string' ? options[i].toString() : options[i];
  2117. }
  2118. }
  2119. var dbInfoPromise = new Promise(function (resolve, reject) {
  2120. // Open the database; the openDatabase API will automatically
  2121. // create it for us if it doesn't exist.
  2122. try {
  2123. dbInfo.db = openDatabase(dbInfo.name, String(dbInfo.version), dbInfo.description, dbInfo.size);
  2124. } catch (e) {
  2125. return reject(e);
  2126. }
  2127. // Create our key/value table if it doesn't exist.
  2128. dbInfo.db.transaction(function (t) {
  2129. t.executeSql('CREATE TABLE IF NOT EXISTS ' + dbInfo.storeName + ' (id INTEGER PRIMARY KEY, key unique, value)', [], function () {
  2130. self._dbInfo = dbInfo;
  2131. resolve();
  2132. }, function (t, error) {
  2133. reject(error);
  2134. });
  2135. });
  2136. });
  2137. return new Promise(function (resolve, reject) {
  2138. resolve(__webpack_require__(3));
  2139. }).then(function (lib) {
  2140. dbInfo.serializer = lib;
  2141. return dbInfoPromise;
  2142. });
  2143. }
  2144. function getItem(key, callback) {
  2145. var self = this;
  2146. // Cast the key to a string, as that's all we can set as a key.
  2147. if (typeof key !== 'string') {
  2148. globalObject.console.warn(key + ' used as a key, but it is not a string.');
  2149. key = String(key);
  2150. }
  2151. var promise = new Promise(function (resolve, reject) {
  2152. self.ready().then(function () {
  2153. var dbInfo = self._dbInfo;
  2154. dbInfo.db.transaction(function (t) {
  2155. t.executeSql('SELECT * FROM ' + dbInfo.storeName + ' WHERE key = ? LIMIT 1', [key], function (t, results) {
  2156. var result = results.rows.length ? results.rows.item(0).value : null;
  2157. // Check to see if this is serialized content we need to
  2158. // unpack.
  2159. if (result) {
  2160. result = dbInfo.serializer.deserialize(result);
  2161. }
  2162. resolve(result);
  2163. }, function (t, error) {
  2164. reject(error);
  2165. });
  2166. });
  2167. })['catch'](reject);
  2168. });
  2169. executeCallback(promise, callback);
  2170. return promise;
  2171. }
  2172. function iterate(iterator, callback) {
  2173. var self = this;
  2174. var promise = new Promise(function (resolve, reject) {
  2175. self.ready().then(function () {
  2176. var dbInfo = self._dbInfo;
  2177. dbInfo.db.transaction(function (t) {
  2178. t.executeSql('SELECT * FROM ' + dbInfo.storeName, [], function (t, results) {
  2179. var rows = results.rows;
  2180. var length = rows.length;
  2181. for (var i = 0; i < length; i++) {
  2182. var item = rows.item(i);
  2183. var result = item.value;
  2184. // Check to see if this is serialized content
  2185. // we need to unpack.
  2186. if (result) {
  2187. result = dbInfo.serializer.deserialize(result);
  2188. }
  2189. result = iterator(result, item.key, i + 1);
  2190. // void(0) prevents problems with redefinition
  2191. // of `undefined`.
  2192. if (result !== void 0) {
  2193. resolve(result);
  2194. return;
  2195. }
  2196. }
  2197. resolve();
  2198. }, function (t, error) {
  2199. reject(error);
  2200. });
  2201. });
  2202. })['catch'](reject);
  2203. });
  2204. executeCallback(promise, callback);
  2205. return promise;
  2206. }
  2207. function setItem(key, value, callback) {
  2208. var self = this;
  2209. // Cast the key to a string, as that's all we can set as a key.
  2210. if (typeof key !== 'string') {
  2211. globalObject.console.warn(key + ' used as a key, but it is not a string.');
  2212. key = String(key);
  2213. }
  2214. var promise = new Promise(function (resolve, reject) {
  2215. self.ready().then(function () {
  2216. // The localStorage API doesn't return undefined values in an
  2217. // "expected" way, so undefined is always cast to null in all
  2218. // drivers. See: https://github.com/mozilla/localForage/pull/42
  2219. if (value === undefined) {
  2220. value = null;
  2221. }
  2222. // Save the original value to pass to the callback.
  2223. var originalValue = value;
  2224. var dbInfo = self._dbInfo;
  2225. dbInfo.serializer.serialize(value, function (value, error) {
  2226. if (error) {
  2227. reject(error);
  2228. } else {
  2229. dbInfo.db.transaction(function (t) {
  2230. t.executeSql('INSERT OR REPLACE INTO ' + dbInfo.storeName + ' (key, value) VALUES (?, ?)', [key, value], function () {
  2231. resolve(originalValue);
  2232. }, function (t, error) {
  2233. reject(error);
  2234. });
  2235. }, function (sqlError) {
  2236. // The transaction failed; check
  2237. // to see if it's a quota error.
  2238. if (sqlError.code === sqlError.QUOTA_ERR) {
  2239. // We reject the callback outright for now, but
  2240. // it's worth trying to re-run the transaction.
  2241. // Even if the user accepts the prompt to use
  2242. // more storage on Safari, this error will
  2243. // be called.
  2244. //
  2245. // TODO: Try to re-run the transaction.
  2246. reject(sqlError);
  2247. }
  2248. });
  2249. }
  2250. });
  2251. })['catch'](reject);
  2252. });
  2253. executeCallback(promise, callback);
  2254. return promise;
  2255. }
  2256. function removeItem(key, callback) {
  2257. var self = this;
  2258. // Cast the key to a string, as that's all we can set as a key.
  2259. if (typeof key !== 'string') {
  2260. globalObject.console.warn(key + ' used as a key, but it is not a string.');
  2261. key = String(key);
  2262. }
  2263. var promise = new Promise(function (resolve, reject) {
  2264. self.ready().then(function () {
  2265. var dbInfo = self._dbInfo;
  2266. dbInfo.db.transaction(function (t) {
  2267. t.executeSql('DELETE FROM ' + dbInfo.storeName + ' WHERE key = ?', [key], function () {
  2268. resolve();
  2269. }, function (t, error) {
  2270. reject(error);
  2271. });
  2272. });
  2273. })['catch'](reject);
  2274. });
  2275. executeCallback(promise, callback);
  2276. return promise;
  2277. }
  2278. // Deletes every item in the table.
  2279. // TODO: Find out if this resets the AUTO_INCREMENT number.
  2280. function clear(callback) {
  2281. var self = this;
  2282. var promise = new Promise(function (resolve, reject) {
  2283. self.ready().then(function () {
  2284. var dbInfo = self._dbInfo;
  2285. dbInfo.db.transaction(function (t) {
  2286. t.executeSql('DELETE FROM ' + dbInfo.storeName, [], function () {
  2287. resolve();
  2288. }, function (t, error) {
  2289. reject(error);
  2290. });
  2291. });
  2292. })['catch'](reject);
  2293. });
  2294. executeCallback(promise, callback);
  2295. return promise;
  2296. }
  2297. // Does a simple `COUNT(key)` to get the number of items stored in
  2298. // localForage.
  2299. function length(callback) {
  2300. var self = this;
  2301. var promise = new Promise(function (resolve, reject) {
  2302. self.ready().then(function () {
  2303. var dbInfo = self._dbInfo;
  2304. dbInfo.db.transaction(function (t) {
  2305. // Ahhh, SQL makes this one soooooo easy.
  2306. t.executeSql('SELECT COUNT(key) as c FROM ' + dbInfo.storeName, [], function (t, results) {
  2307. var result = results.rows.item(0).c;
  2308. resolve(result);
  2309. }, function (t, error) {
  2310. reject(error);
  2311. });
  2312. });
  2313. })['catch'](reject);
  2314. });
  2315. executeCallback(promise, callback);
  2316. return promise;
  2317. }
  2318. // Return the key located at key index X; essentially gets the key from a
  2319. // `WHERE id = ?`. This is the most efficient way I can think to implement
  2320. // this rarely-used (in my experience) part of the API, but it can seem
  2321. // inconsistent, because we do `INSERT OR REPLACE INTO` on `setItem()`, so
  2322. // the ID of each key will change every time it's updated. Perhaps a stored
  2323. // procedure for the `setItem()` SQL would solve this problem?
  2324. // TODO: Don't change ID on `setItem()`.
  2325. function key(n, callback) {
  2326. var self = this;
  2327. var promise = new Promise(function (resolve, reject) {
  2328. self.ready().then(function () {
  2329. var dbInfo = self._dbInfo;
  2330. dbInfo.db.transaction(function (t) {
  2331. t.executeSql('SELECT key FROM ' + dbInfo.storeName + ' WHERE id = ? LIMIT 1', [n + 1], function (t, results) {
  2332. var result = results.rows.length ? results.rows.item(0).key : null;
  2333. resolve(result);
  2334. }, function (t, error) {
  2335. reject(error);
  2336. });
  2337. });
  2338. })['catch'](reject);
  2339. });
  2340. executeCallback(promise, callback);
  2341. return promise;
  2342. }
  2343. function keys(callback) {
  2344. var self = this;
  2345. var promise = new Promise(function (resolve, reject) {
  2346. self.ready().then(function () {
  2347. var dbInfo = self._dbInfo;
  2348. dbInfo.db.transaction(function (t) {
  2349. t.executeSql('SELECT key FROM ' + dbInfo.storeName, [], function (t, results) {
  2350. var keys = [];
  2351. for (var i = 0; i < results.rows.length; i++) {
  2352. keys.push(results.rows.item(i).key);
  2353. }
  2354. resolve(keys);
  2355. }, function (t, error) {
  2356. reject(error);
  2357. });
  2358. });
  2359. })['catch'](reject);
  2360. });
  2361. executeCallback(promise, callback);
  2362. return promise;
  2363. }
  2364. function executeCallback(promise, callback) {
  2365. if (callback) {
  2366. promise.then(function (result) {
  2367. callback(null, result);
  2368. }, function (error) {
  2369. callback(error);
  2370. });
  2371. }
  2372. }
  2373. var webSQLStorage = {
  2374. _driver: 'webSQLStorage',
  2375. _initStorage: _initStorage,
  2376. iterate: iterate,
  2377. getItem: getItem,
  2378. setItem: setItem,
  2379. removeItem: removeItem,
  2380. clear: clear,
  2381. length: length,
  2382. key: key,
  2383. keys: keys
  2384. };
  2385. return webSQLStorage;
  2386. })(typeof window !== 'undefined' ? window : self);
  2387. exports['default'] = webSQLStorage;
  2388. module.exports = exports['default'];
  2389. /***/ }
  2390. /******/ ])
  2391. });
  2392. ;