diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a308b5a..d5ebb3c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,8 +2,6 @@ name: Download dependencies and build a combined, minified source for release on: push: - branches: - - main jobs: build: @@ -16,6 +14,7 @@ jobs: - name: Install dependencies run: | npm install + mkdir lib dist mv node_modules/dexie/dist/dexie.js lib/dexie.js mv node_modules/dexie-export-import/dist/dexie-export-import.js lib/dexie-export-import.js mv node_modules/downloadjs/download.js lib/download.js @@ -24,25 +23,8 @@ jobs: run: | npm run build - - name: Remove node_modules - run: - rm -rf node_modules - - - name: Commit source to main branch - run: | - git config --local user.email "action@github.com" - git config --local user.name "GitHub Action" - echo `date` > .last_built_on - git add . -A - git commit -m "Minify and combine dependencies as of $(date +%s)." - git push - - - name: Commit minified dependencies to release branch - run: | - git config --local user.email "action@github.com" - git config --local user.name "GitHub Action" - git fetch - git checkout release - git checkout ${GITHUB_REF##*/} dist/yomichan-data-exporter.min.js .last_built_on - git commit -am "Release minified and combined dependencies as of $(date +%s)." - git push origin release + - name: Release + uses: softprops/action-gh-release@v2 + if: startsWith(github.ref, 'refs/tags/') + with: + files: dist/yomichan-data-exporter.min.js diff --git a/.last_built_on b/.last_built_on deleted file mode 100644 index 08e910c..0000000 --- a/.last_built_on +++ /dev/null @@ -1 +0,0 @@ -Sat Jul 15 05:06:38 UTC 2023 diff --git a/README.org b/README.org index d63f330..f058bba 100644 --- a/README.org +++ b/README.org @@ -32,7 +32,7 @@ to that end. + Paste the following into the console and wait for the export + download to finish: #+begin_src js -fetch("https://raw.githubusercontent.com/yomidevs/yomichan-data-exporter/release/dist/yomichan-data-exporter.min.js") +fetch("https://raw.githubusercontent.com/yomidevs/yomichan-data-exporter/releases/latest/yomichan-data-exporter.min.js") .then((resp) => resp.text()) .then((srcText) => { eval(srcText); @@ -50,11 +50,11 @@ fetch("https://raw.githubusercontent.com/yomidevs/yomichan-data-exporter/release + Unfortunately(?!), Yomichan released on Chrome doesn't have permission to actually inject code (I tried a whole bunch of different approaches and the permissions are just lacking) so you are going to have to paste the contents - of the [[https://raw.githubusercontent.com/yomidevs/yomichan-data-exporter/release/dist/yomichan-data-exporter.min.js][exporter code]] into the console. + of the [[https://raw.githubusercontent.com/yomidevs/yomichan-data-exporter/releases/latest/yomichan-data-exporter.min.js][exporter code]] into the console. It is a very long "one-liner" and apparently github is pretty bad at handling all of it being put into a code box so you may want to open the raw file, - select all, then copy and paste into the console. [[https://raw.githubusercontent.com/yomidevs/yomichan-data-exporter/release/dist/yomichan-data-exporter.min.js][Link to the raw]]. + select all, then copy and paste into the console. [[https://raw.githubusercontent.com/yomidevs/yomichan-data-exporter/releases/latest/yomichan-data-exporter.min.js][Link to the raw]]. + Finally, paste the following into the console and wait for the export + download to finish: diff --git a/dist/.gitkeep b/dist/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/dist/yomichan-data-exporter.min.js b/dist/yomichan-data-exporter.min.js deleted file mode 100644 index e00ba0b..0000000 --- a/dist/yomichan-data-exporter.min.js +++ /dev/null @@ -1 +0,0 @@ -(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,global.Dexie=factory())})(this,function(){"use strict";var __assign=function(){__assign=Object.assign||function __assign(t){for(var s,i=1,n=arguments.length;i=0){rv=any}else{var proto=getProto(any);rv=proto===Object.prototype?{}:Object.create(proto);circularRefs&&circularRefs.set(any,rv);for(var prop in any){if(hasOwn(any,prop)){rv[prop]=innerDeepClone(any[prop])}}}return rv}var toString={}.toString;function toStringTag(o){return toString.call(o).slice(8,-1)}var iteratorSymbol=typeof Symbol!=="undefined"?Symbol.iterator:"@@iterator";var getIteratorOf=typeof iteratorSymbol==="symbol"?function(x){var i;return x!=null&&(i=x[iteratorSymbol])&&i.apply(x)}:function(){return null};var NO_CHAR_ARRAY={};function getArrayOf(arrayLike){var i,a,x,it;if(arguments.length===1){if(isArray(arrayLike))return arrayLike.slice();if(this===NO_CHAR_ARRAY&&typeof arrayLike==="string")return[arrayLike];if(it=getIteratorOf(arrayLike)){a=[];while(x=it.next(),!x.done)a.push(x.value);return a}if(arrayLike==null)return[arrayLike];i=arrayLike.length;if(typeof i==="number"){a=new Array(i);while(i--)a[i]=arrayLike[i];return a}return[arrayLike]}i=arguments.length;a=new Array(i);while(i--)a[i]=arguments[i];return a}var isAsyncFunction=typeof Symbol!=="undefined"?function(fn){return fn[Symbol.toStringTag]==="AsyncFunction"}:function(){return false};var debug=typeof location!=="undefined"&&/^(http|https):\/\/(localhost|127\.0\.0\.1)/.test(location.href);function setDebug(value,filter){debug=value;libraryFilter=filter}var libraryFilter=function(){return true};var NEEDS_THROW_FOR_STACK=!new Error("").stack;function getErrorWithStack(){if(NEEDS_THROW_FOR_STACK)try{getErrorWithStack.arguments;throw new Error}catch(e){return e}return new Error}function prettyStack(exception,numIgnoredFrames){var stack=exception.stack;if(!stack)return"";numIgnoredFrames=numIgnoredFrames||0;if(stack.indexOf(exception.name)===0)numIgnoredFrames+=(exception.name+exception.message).split("\n").length;return stack.split("\n").slice(numIgnoredFrames).filter(libraryFilter).map(function(frame){return"\n"+frame}).join("")}var dexieErrorNames=["Modify","Bulk","OpenFailed","VersionChange","Schema","Upgrade","InvalidTable","MissingAPI","NoSuchDatabase","InvalidArgument","SubTransaction","Unsupported","Internal","DatabaseClosed","PrematureCommit","ForeignAwait"];var idbDomErrorNames=["Unknown","Constraint","Data","TransactionInactive","ReadOnly","Version","NotFound","InvalidState","InvalidAccess","Abort","Timeout","QuotaExceeded","Syntax","DataClone"];var errorList=dexieErrorNames.concat(idbDomErrorNames);var defaultTexts={VersionChanged:"Database version changed by other database connection",DatabaseClosed:"Database has been closed",Abort:"Transaction aborted",TransactionInactive:"Transaction has already completed or failed",MissingAPI:"IndexedDB API missing. Please visit https://tinyurl.com/y2uuvskb"};function DexieError(name,msg){this._e=getErrorWithStack();this.name=name;this.message=msg}derive(DexieError).from(Error).extend({stack:{get:function(){return this._stack||(this._stack=this.name+": "+this.message+prettyStack(this._e,2))}},toString:function(){return this.name+": "+this.message}});function getMultiErrorMessage(msg,failures){return msg+". Errors: "+Object.keys(failures).map(function(key){return failures[key].toString()}).filter(function(v,i,s){return s.indexOf(v)===i}).join("\n")}function ModifyError(msg,failures,successCount,failedKeys){this._e=getErrorWithStack();this.failures=failures;this.failedKeys=failedKeys;this.successCount=successCount;this.message=getMultiErrorMessage(msg,failures)}derive(ModifyError).from(DexieError);function BulkError(msg,failures){this._e=getErrorWithStack();this.name="BulkError";this.failures=Object.keys(failures).map(function(pos){return failures[pos]});this.failuresByPos=failures;this.message=getMultiErrorMessage(msg,failures)}derive(BulkError).from(DexieError);var errnames=errorList.reduce(function(obj,name){return obj[name]=name+"Error",obj},{});var BaseException=DexieError;var exceptions=errorList.reduce(function(obj,name){var fullName=name+"Error";function DexieError(msgOrInner,inner){this._e=getErrorWithStack();this.name=fullName;if(!msgOrInner){this.message=defaultTexts[name]||fullName;this.inner=null}else if(typeof msgOrInner==="string"){this.message=""+msgOrInner+(!inner?"":"\n "+inner);this.inner=inner||null}else if(typeof msgOrInner==="object"){this.message=msgOrInner.name+" "+msgOrInner.message;this.inner=msgOrInner}}derive(DexieError).from(BaseException);obj[name]=DexieError;return obj},{});exceptions.Syntax=SyntaxError;exceptions.Type=TypeError;exceptions.Range=RangeError;var exceptionMap=idbDomErrorNames.reduce(function(obj,name){obj[name+"Error"]=exceptions[name];return obj},{});function mapError(domError,message){if(!domError||domError instanceof DexieError||domError instanceof TypeError||domError instanceof SyntaxError||!domError.name||!exceptionMap[domError.name])return domError;var rv=new exceptionMap[domError.name](message||domError.message,domError);if("stack"in domError){setProp(rv,"stack",{get:function(){return this.inner.stack}})}return rv}var fullNameExceptions=errorList.reduce(function(obj,name){if(["Syntax","Type","Range"].indexOf(name)===-1)obj[name+"Error"]=exceptions[name];return obj},{});fullNameExceptions.ModifyError=ModifyError;fullNameExceptions.DexieError=DexieError;fullNameExceptions.BulkError=BulkError;function nop(){}function mirror(val){return val}function pureFunctionChain(f1,f2){if(f1==null||f1===mirror)return f2;return function(val){return f2(f1(val))}}function callBoth(on1,on2){return function(){on1.apply(this,arguments);on2.apply(this,arguments)}}function hookCreatingChain(f1,f2){if(f1===nop)return f2;return function(){var res=f1.apply(this,arguments);if(res!==undefined)arguments[0]=res;var onsuccess=this.onsuccess,onerror=this.onerror;this.onsuccess=null;this.onerror=null;var res2=f2.apply(this,arguments);if(onsuccess)this.onsuccess=this.onsuccess?callBoth(onsuccess,this.onsuccess):onsuccess;if(onerror)this.onerror=this.onerror?callBoth(onerror,this.onerror):onerror;return res2!==undefined?res2:res}}function hookDeletingChain(f1,f2){if(f1===nop)return f2;return function(){f1.apply(this,arguments);var onsuccess=this.onsuccess,onerror=this.onerror;this.onsuccess=this.onerror=null;f2.apply(this,arguments);if(onsuccess)this.onsuccess=this.onsuccess?callBoth(onsuccess,this.onsuccess):onsuccess;if(onerror)this.onerror=this.onerror?callBoth(onerror,this.onerror):onerror}}function hookUpdatingChain(f1,f2){if(f1===nop)return f2;return function(modifications){var res=f1.apply(this,arguments);extend(modifications,res);var onsuccess=this.onsuccess,onerror=this.onerror;this.onsuccess=null;this.onerror=null;var res2=f2.apply(this,arguments);if(onsuccess)this.onsuccess=this.onsuccess?callBoth(onsuccess,this.onsuccess):onsuccess;if(onerror)this.onerror=this.onerror?callBoth(onerror,this.onerror):onerror;return res===undefined?res2===undefined?undefined:res2:extend(res,res2)}}function reverseStoppableEventChain(f1,f2){if(f1===nop)return f2;return function(){if(f2.apply(this,arguments)===false)return false;return f1.apply(this,arguments)}}function promisableChain(f1,f2){if(f1===nop)return f2;return function(){var res=f1.apply(this,arguments);if(res&&typeof res.then==="function"){var thiz=this,i=arguments.length,args=new Array(i);while(i--)args[i]=arguments[i];return res.then(function(){return f2.apply(thiz,args)})}return f2.apply(this,arguments)}}var INTERNAL={};var LONG_STACKS_CLIP_LIMIT=100,MAX_LONG_STACKS=20,ZONE_ECHO_LIMIT=100,_a$1=typeof Promise==="undefined"?[]:function(){var globalP=Promise.resolve();if(typeof crypto==="undefined"||!crypto.subtle)return[globalP,getProto(globalP),globalP];var nativeP=crypto.subtle.digest("SHA-512",new Uint8Array([0]));return[nativeP,getProto(nativeP),globalP]}(),resolvedNativePromise=_a$1[0],nativePromiseProto=_a$1[1],resolvedGlobalPromise=_a$1[2],nativePromiseThen=nativePromiseProto&&nativePromiseProto.then;var NativePromise=resolvedNativePromise&&resolvedNativePromise.constructor;var patchGlobalPromise=!!resolvedGlobalPromise;var stack_being_generated=false;var schedulePhysicalTick=resolvedGlobalPromise?function(){resolvedGlobalPromise.then(physicalTick)}:_global.setImmediate?setImmediate.bind(null,physicalTick):_global.MutationObserver?function(){var hiddenDiv=document.createElement("div");new MutationObserver(function(){physicalTick();hiddenDiv=null}).observe(hiddenDiv,{attributes:true});hiddenDiv.setAttribute("i","1")}:function(){setTimeout(physicalTick,0)};var asap=function(callback,args){microtickQueue.push([callback,args]);if(needsNewPhysicalTick){schedulePhysicalTick();needsNewPhysicalTick=false}};var isOutsideMicroTick=true,needsNewPhysicalTick=true,unhandledErrors=[],rejectingErrors=[],currentFulfiller=null,rejectionMapper=mirror;var globalPSD={id:"global",global:true,ref:0,unhandleds:[],onunhandled:globalError,pgp:false,env:{},finalize:function(){this.unhandleds.forEach(function(uh){try{globalError(uh[0],uh[1])}catch(e){}})}};var PSD=globalPSD;var microtickQueue=[];var numScheduledCalls=0;var tickFinalizers=[];function DexiePromise(fn){if(typeof this!=="object")throw new TypeError("Promises must be constructed via new");this._listeners=[];this.onuncatched=nop;this._lib=false;var psd=this._PSD=PSD;if(debug){this._stackHolder=getErrorWithStack();this._prev=null;this._numPrev=0}if(typeof fn!=="function"){if(fn!==INTERNAL)throw new TypeError("Not a function");this._state=arguments[1];this._value=arguments[2];if(this._state===false)handleRejection(this,this._value);return}this._state=null;this._value=null;++psd.ref;executePromiseTask(this,fn)}var thenProp={get:function(){var psd=PSD,microTaskId=totalEchoes;function then(onFulfilled,onRejected){var _this=this;var possibleAwait=!psd.global&&(psd!==PSD||microTaskId!==totalEchoes);var cleanup=possibleAwait&&!decrementExpectedAwaits();var rv=new DexiePromise(function(resolve,reject){propagateToListener(_this,new Listener(nativeAwaitCompatibleWrap(onFulfilled,psd,possibleAwait,cleanup),nativeAwaitCompatibleWrap(onRejected,psd,possibleAwait,cleanup),resolve,reject,psd))});debug&&linkToPreviousPromise(rv,this);return rv}then.prototype=INTERNAL;return then},set:function(value){setProp(this,"then",value&&value.prototype===INTERNAL?thenProp:{get:function(){return value},set:thenProp.set})}};props(DexiePromise.prototype,{then:thenProp,_then:function(onFulfilled,onRejected){propagateToListener(this,new Listener(null,null,onFulfilled,onRejected,PSD))},catch:function(onRejected){if(arguments.length===1)return this.then(null,onRejected);var type=arguments[0],handler=arguments[1];return typeof type==="function"?this.then(null,function(err){return err instanceof type?handler(err):PromiseReject(err)}):this.then(null,function(err){return err&&err.name===type?handler(err):PromiseReject(err)})},finally:function(onFinally){return this.then(function(value){onFinally();return value},function(err){onFinally();return PromiseReject(err)})},stack:{get:function(){if(this._stack)return this._stack;try{stack_being_generated=true;var stacks=getStack(this,[],MAX_LONG_STACKS);var stack=stacks.join("\nFrom previous: ");if(this._state!==null)this._stack=stack;return stack}finally{stack_being_generated=false}}},timeout:function(ms,msg){var _this=this;return ms0){callbacks=microtickQueue;microtickQueue=[];l=callbacks.length;for(i=0;i0);isOutsideMicroTick=true;needsNewPhysicalTick=true}function finalizePhysicalTick(){var unhandledErrs=unhandledErrors;unhandledErrors=[];unhandledErrs.forEach(function(p){p._PSD.onunhandled.call(null,p._value,p)});var finalizers=tickFinalizers.slice(0);var i=finalizers.length;while(i)finalizers[--i]()}function run_at_end_of_this_or_next_physical_tick(fn){function finalizer(){fn();tickFinalizers.splice(tickFinalizers.indexOf(finalizer),1)}tickFinalizers.push(finalizer);++numScheduledCalls;asap(function(){if(--numScheduledCalls===0)finalizePhysicalTick()},[])}function addPossiblyUnhandledError(promise){if(!unhandledErrors.some(function(p){return p._value===promise._value}))unhandledErrors.push(promise)}function markErrorAsHandled(promise){var i=unhandledErrors.length;while(i)if(unhandledErrors[--i]._value===promise._value){unhandledErrors.splice(i,1);return}}function PromiseReject(reason){return new DexiePromise(INTERNAL,false,reason)}function wrap(fn,errorCatcher){var psd=PSD;return function(){var wasRootExec=beginMicroTickScope(),outerScope=PSD;try{switchToZone(psd,true);return fn.apply(this,arguments)}catch(e){errorCatcher&&errorCatcher(e)}finally{switchToZone(outerScope,false);if(wasRootExec)endMicroTickScope()}}}var task={awaits:0,echoes:0,id:0};var taskCounter=0;var zoneStack=[];var zoneEchoes=0;var totalEchoes=0;var zone_id_counter=0;function newScope(fn,props,a1,a2){var parent=PSD,psd=Object.create(parent);psd.parent=parent;psd.ref=0;psd.global=false;psd.id=++zone_id_counter;var globalEnv=globalPSD.env;psd.env=patchGlobalPromise?{Promise:DexiePromise,PromiseProp:{value:DexiePromise,configurable:true,writable:true},all:DexiePromise.all,race:DexiePromise.race,allSettled:DexiePromise.allSettled,any:DexiePromise.any,resolve:DexiePromise.resolve,reject:DexiePromise.reject,nthen:getPatchedPromiseThen(globalEnv.nthen,psd),gthen:getPatchedPromiseThen(globalEnv.gthen,psd)}:{};if(props)extend(psd,props);++parent.ref;psd.finalize=function(){--this.parent.ref||this.parent.finalize()};var rv=usePSD(psd,fn,a1,a2);if(psd.ref===0)psd.finalize();return rv}function incrementExpectedAwaits(){if(!task.id)task.id=++taskCounter;++task.awaits;task.echoes+=ZONE_ECHO_LIMIT;return task.id}function decrementExpectedAwaits(){if(!task.awaits)return false;if(--task.awaits===0)task.id=0;task.echoes=task.awaits*ZONE_ECHO_LIMIT;return true}if((""+nativePromiseThen).indexOf("[native code]")===-1){incrementExpectedAwaits=decrementExpectedAwaits=nop}function onPossibleParallellAsync(possiblePromise){if(task.echoes&&possiblePromise&&possiblePromise.constructor===NativePromise){incrementExpectedAwaits();return possiblePromise.then(function(x){decrementExpectedAwaits();return x},function(e){decrementExpectedAwaits();return rejection(e)})}return possiblePromise}function zoneEnterEcho(targetZone){++totalEchoes;if(!task.echoes||--task.echoes===0){task.echoes=task.id=0}zoneStack.push(PSD);switchToZone(targetZone,true)}function zoneLeaveEcho(){var zone=zoneStack[zoneStack.length-1];zoneStack.pop();switchToZone(zone,false)}function switchToZone(targetZone,bEnteringZone){var currentZone=PSD;if(bEnteringZone?task.echoes&&(!zoneEchoes++||targetZone!==PSD):zoneEchoes&&(!--zoneEchoes||targetZone!==PSD)){enqueueNativeMicroTask(bEnteringZone?zoneEnterEcho.bind(null,targetZone):zoneLeaveEcho)}if(targetZone===PSD)return;PSD=targetZone;if(currentZone===globalPSD)globalPSD.env=snapShot();if(patchGlobalPromise){var GlobalPromise_1=globalPSD.env.Promise;var targetEnv=targetZone.env;nativePromiseProto.then=targetEnv.nthen;GlobalPromise_1.prototype.then=targetEnv.gthen;if(currentZone.global||targetZone.global){Object.defineProperty(_global,"Promise",targetEnv.PromiseProp);GlobalPromise_1.all=targetEnv.all;GlobalPromise_1.race=targetEnv.race;GlobalPromise_1.resolve=targetEnv.resolve;GlobalPromise_1.reject=targetEnv.reject;if(targetEnv.allSettled)GlobalPromise_1.allSettled=targetEnv.allSettled;if(targetEnv.any)GlobalPromise_1.any=targetEnv.any}}}function snapShot(){var GlobalPromise=_global.Promise;return patchGlobalPromise?{Promise:GlobalPromise,PromiseProp:Object.getOwnPropertyDescriptor(_global,"Promise"),all:GlobalPromise.all,race:GlobalPromise.race,allSettled:GlobalPromise.allSettled,any:GlobalPromise.any,resolve:GlobalPromise.resolve,reject:GlobalPromise.reject,nthen:nativePromiseProto.then,gthen:GlobalPromise.prototype.then}:{}}function usePSD(psd,fn,a1,a2,a3){var outerScope=PSD;try{switchToZone(psd,true);return fn(a1,a2,a3)}finally{switchToZone(outerScope,false)}}function enqueueNativeMicroTask(job){nativePromiseThen.call(resolvedNativePromise,job)}function nativeAwaitCompatibleWrap(fn,zone,possibleAwait,cleanup){return typeof fn!=="function"?fn:function(){var outerZone=PSD;if(possibleAwait)incrementExpectedAwaits();switchToZone(zone,true);try{return fn.apply(this,arguments)}finally{switchToZone(outerZone,false);if(cleanup)enqueueNativeMicroTask(decrementExpectedAwaits)}}}function getPatchedPromiseThen(origThen,zone){return function(onResolved,onRejected){return origThen.call(this,nativeAwaitCompatibleWrap(onResolved,zone),nativeAwaitCompatibleWrap(onRejected,zone))}}var UNHANDLEDREJECTION="unhandledrejection";function globalError(err,promise){var rv;try{rv=promise.onuncatched(err)}catch(e){}if(rv!==false)try{var event,eventData={promise:promise,reason:err};if(_global.document&&document.createEvent){event=document.createEvent("Event");event.initEvent(UNHANDLEDREJECTION,true,true);extend(event,eventData)}else if(_global.CustomEvent){event=new CustomEvent(UNHANDLEDREJECTION,{detail:eventData});extend(event,eventData)}if(event&&_global.dispatchEvent){dispatchEvent(event);if(!_global.PromiseRejectionEvent&&_global.onunhandledrejection)try{_global.onunhandledrejection(event)}catch(_){}}if(debug&&event&&!event.defaultPrevented){console.warn("Unhandled rejection: "+(err.stack||err))}}catch(e){}}var rejection=DexiePromise.reject;function tempTransaction(db,mode,storeNames,fn){if(!db.idbdb||!db._state.openComplete&&(!PSD.letThrough&&!db._vip)){if(db._state.openComplete){return rejection(new exceptions.DatabaseClosed(db._state.dbOpenError))}if(!db._state.isBeingOpened){if(!db._options.autoOpen)return rejection(new exceptions.DatabaseClosed);db.open().catch(nop)}return db._state.dbReadyPromise.then(function(){return tempTransaction(db,mode,storeNames,fn)})}else{var trans=db._createTransaction(mode,storeNames,db._dbSchema);try{trans.create();db._state.PR1398_maxLoop=3}catch(ex){if(ex.name===errnames.InvalidState&&db.isOpen()&&--db._state.PR1398_maxLoop>0){console.warn("Dexie: Need to reopen db");db._close();return db.open().then(function(){return tempTransaction(db,mode,storeNames,fn)})}return rejection(ex)}return trans._promise(mode,function(resolve,reject){return newScope(function(){PSD.trans=trans;return fn(resolve,reject,trans)})}).then(function(result){return trans._completion.then(function(){return result})})}}var DEXIE_VERSION="3.2.4";var maxString=String.fromCharCode(65535);var minKey=-Infinity;var INVALID_KEY_ARGUMENT="Invalid key provided. Keys must be of type string, number, Date or Array.";var STRING_EXPECTED="String expected.";var connections=[];var isIEOrEdge=typeof navigator!=="undefined"&&/(MSIE|Trident|Edge)/.test(navigator.userAgent);var hasIEDeleteObjectStoreBug=isIEOrEdge;var hangsOnDeleteLargeKeyRange=isIEOrEdge;var dexieStackFrameFilter=function(frame){return!/(dexie\.js|dexie\.min\.js)/.test(frame)};var DBNAMES_DB="__dbnames";var READONLY="readonly";var READWRITE="readwrite";function combine(filter1,filter2){return filter1?filter2?function(){return filter1.apply(this,arguments)&&filter2.apply(this,arguments)}:filter1:filter2}var AnyRange={type:3,lower:-Infinity,lowerOpen:false,upper:[[]],upperOpen:false};function workaroundForUndefinedPrimKey(keyPath){return typeof keyPath==="string"&&!/\./.test(keyPath)?function(obj){if(obj[keyPath]===undefined&&keyPath in obj){obj=deepClone(obj);delete obj[keyPath]}return obj}:function(obj){return obj}}var Table=function(){function Table(){}Table.prototype._trans=function(mode,fn,writeLocked){var trans=this._tx||PSD.trans;var tableName=this.name;function checkTableInTransaction(resolve,reject,trans){if(!trans.schema[tableName])throw new exceptions.NotFound("Table "+tableName+" not part of transaction");return fn(trans.idbtrans,trans)}var wasRootExec=beginMicroTickScope();try{return trans&&trans.db===this.db?trans===PSD.trans?trans._promise(mode,checkTableInTransaction,writeLocked):newScope(function(){return trans._promise(mode,checkTableInTransaction,writeLocked)},{trans:trans,transless:PSD.transless||PSD}):tempTransaction(this.db,mode,[this.name],checkTableInTransaction)}finally{if(wasRootExec)endMicroTickScope()}};Table.prototype.get=function(keyOrCrit,cb){var _this=this;if(keyOrCrit&&keyOrCrit.constructor===Object)return this.where(keyOrCrit).first(cb);return this._trans("readonly",function(trans){return _this.core.get({trans:trans,key:keyOrCrit}).then(function(res){return _this.hook.reading.fire(res)})}).then(cb)};Table.prototype.where=function(indexOrCrit){if(typeof indexOrCrit==="string")return new this.db.WhereClause(this,indexOrCrit);if(isArray(indexOrCrit))return new this.db.WhereClause(this,"["+indexOrCrit.join("+")+"]");var keyPaths=keys(indexOrCrit);if(keyPaths.length===1)return this.where(keyPaths[0]).equals(indexOrCrit[keyPaths[0]]);var compoundIndex=this.schema.indexes.concat(this.schema.primKey).filter(function(ix){return ix.compound&&keyPaths.every(function(keyPath){return ix.keyPath.indexOf(keyPath)>=0})&&ix.keyPath.every(function(keyPath){return keyPaths.indexOf(keyPath)>=0})})[0];if(compoundIndex&&this.db._maxKey!==maxString)return this.where(compoundIndex.name).equals(compoundIndex.keyPath.map(function(kp){return indexOrCrit[kp]}));if(!compoundIndex&&debug)console.warn("The query "+JSON.stringify(indexOrCrit)+" on "+this.name+" would benefit of a "+("compound index ["+keyPaths.join("+")+"]"));var idxByName=this.schema.idxByName;var idb=this.db._deps.indexedDB;function equals(a,b){try{return idb.cmp(a,b)===0}catch(e){return false}}var _a=keyPaths.reduce(function(_a,keyPath){var prevIndex=_a[0],prevFilterFn=_a[1];var index=idxByName[keyPath];var value=indexOrCrit[keyPath];return[prevIndex||index,prevIndex||!index?combine(prevFilterFn,index&&index.multi?function(x){var prop=getByKeyPath(x,keyPath);return isArray(prop)&&prop.some(function(item){return equals(value,item)})}:function(x){return equals(value,getByKeyPath(x,keyPath))}):prevFilterFn]},[null,null]),idx=_a[0],filterFunction=_a[1];return idx?this.where(idx.name).equals(indexOrCrit[idx.keyPath]).filter(filterFunction):compoundIndex?this.filter(filterFunction):this.where(keyPaths).equals("")};Table.prototype.filter=function(filterFunction){return this.toCollection().and(filterFunction)};Table.prototype.count=function(thenShortcut){return this.toCollection().count(thenShortcut)};Table.prototype.offset=function(offset){return this.toCollection().offset(offset)};Table.prototype.limit=function(numRows){return this.toCollection().limit(numRows)};Table.prototype.each=function(callback){return this.toCollection().each(callback)};Table.prototype.toArray=function(thenShortcut){return this.toCollection().toArray(thenShortcut)};Table.prototype.toCollection=function(){return new this.db.Collection(new this.db.WhereClause(this))};Table.prototype.orderBy=function(index){return new this.db.Collection(new this.db.WhereClause(this,isArray(index)?"["+index.join("+")+"]":index))};Table.prototype.reverse=function(){return this.toCollection().reverse()};Table.prototype.mapToClass=function(constructor){this.schema.mappedClass=constructor;var readHook=function(obj){if(!obj)return obj;var res=Object.create(constructor.prototype);for(var m in obj)if(hasOwn(obj,m))try{res[m]=obj[m]}catch(_){}return res};if(this.schema.readHook){this.hook.reading.unsubscribe(this.schema.readHook)}this.schema.readHook=readHook;this.hook("reading",readHook);return constructor};Table.prototype.defineClass=function(){function Class(content){extend(this,content)}return this.mapToClass(Class)};Table.prototype.add=function(obj,key){var _this=this;var _a=this.schema.primKey,auto=_a.auto,keyPath=_a.keyPath;var objToAdd=obj;if(keyPath&&auto){objToAdd=workaroundForUndefinedPrimKey(keyPath)(obj)}return this._trans("readwrite",function(trans){return _this.core.mutate({trans:trans,type:"add",keys:key!=null?[key]:null,values:[objToAdd]})}).then(function(res){return res.numFailures?DexiePromise.reject(res.failures[0]):res.lastResult}).then(function(lastResult){if(keyPath){try{setByKeyPath(obj,keyPath,lastResult)}catch(_){}}return lastResult})};Table.prototype.update=function(keyOrObject,modifications){if(typeof keyOrObject==="object"&&!isArray(keyOrObject)){var key=getByKeyPath(keyOrObject,this.schema.primKey.keyPath);if(key===undefined)return rejection(new exceptions.InvalidArgument("Given object does not contain its primary key"));try{if(typeof modifications!=="function"){keys(modifications).forEach(function(keyPath){setByKeyPath(keyOrObject,keyPath,modifications[keyPath])})}else{modifications(keyOrObject,{value:keyOrObject,primKey:key})}}catch(_a){}return this.where(":id").equals(key).modify(modifications)}else{return this.where(":id").equals(keyOrObject).modify(modifications)}};Table.prototype.put=function(obj,key){var _this=this;var _a=this.schema.primKey,auto=_a.auto,keyPath=_a.keyPath;var objToAdd=obj;if(keyPath&&auto){objToAdd=workaroundForUndefinedPrimKey(keyPath)(obj)}return this._trans("readwrite",function(trans){return _this.core.mutate({trans:trans,type:"put",values:[objToAdd],keys:key!=null?[key]:null})}).then(function(res){return res.numFailures?DexiePromise.reject(res.failures[0]):res.lastResult}).then(function(lastResult){if(keyPath){try{setByKeyPath(obj,keyPath,lastResult)}catch(_){}}return lastResult})};Table.prototype.delete=function(key){var _this=this;return this._trans("readwrite",function(trans){return _this.core.mutate({trans:trans,type:"delete",keys:[key]})}).then(function(res){return res.numFailures?DexiePromise.reject(res.failures[0]):undefined})};Table.prototype.clear=function(){var _this=this;return this._trans("readwrite",function(trans){return _this.core.mutate({trans:trans,type:"deleteRange",range:AnyRange})}).then(function(res){return res.numFailures?DexiePromise.reject(res.failures[0]):undefined})};Table.prototype.bulkGet=function(keys){var _this=this;return this._trans("readonly",function(trans){return _this.core.getMany({keys:keys,trans:trans}).then(function(result){return result.map(function(res){return _this.hook.reading.fire(res)})})})};Table.prototype.bulkAdd=function(objects,keysOrOptions,options){var _this=this;var keys=Array.isArray(keysOrOptions)?keysOrOptions:undefined;options=options||(keys?undefined:keysOrOptions);var wantResults=options?options.allKeys:undefined;return this._trans("readwrite",function(trans){var _a=_this.schema.primKey,auto=_a.auto,keyPath=_a.keyPath;if(keyPath&&keys)throw new exceptions.InvalidArgument("bulkAdd(): keys argument invalid on tables with inbound keys");if(keys&&keys.length!==objects.length)throw new exceptions.InvalidArgument("Arguments objects and keys must have the same length");var numObjects=objects.length;var objectsToAdd=keyPath&&auto?objects.map(workaroundForUndefinedPrimKey(keyPath)):objects;return _this.core.mutate({trans:trans,type:"add",keys:keys,values:objectsToAdd,wantResults:wantResults}).then(function(_a){var numFailures=_a.numFailures,results=_a.results,lastResult=_a.lastResult,failures=_a.failures;var result=wantResults?results:lastResult;if(numFailures===0)return result;throw new BulkError(_this.name+".bulkAdd(): "+numFailures+" of "+numObjects+" operations failed",failures)})})};Table.prototype.bulkPut=function(objects,keysOrOptions,options){var _this=this;var keys=Array.isArray(keysOrOptions)?keysOrOptions:undefined;options=options||(keys?undefined:keysOrOptions);var wantResults=options?options.allKeys:undefined;return this._trans("readwrite",function(trans){var _a=_this.schema.primKey,auto=_a.auto,keyPath=_a.keyPath;if(keyPath&&keys)throw new exceptions.InvalidArgument("bulkPut(): keys argument invalid on tables with inbound keys");if(keys&&keys.length!==objects.length)throw new exceptions.InvalidArgument("Arguments objects and keys must have the same length");var numObjects=objects.length;var objectsToPut=keyPath&&auto?objects.map(workaroundForUndefinedPrimKey(keyPath)):objects;return _this.core.mutate({trans:trans,type:"put",keys:keys,values:objectsToPut,wantResults:wantResults}).then(function(_a){var numFailures=_a.numFailures,results=_a.results,lastResult=_a.lastResult,failures=_a.failures;var result=wantResults?results:lastResult;if(numFailures===0)return result;throw new BulkError(_this.name+".bulkPut(): "+numFailures+" of "+numObjects+" operations failed",failures)})})};Table.prototype.bulkDelete=function(keys){var _this=this;var numKeys=keys.length;return this._trans("readwrite",function(trans){return _this.core.mutate({trans:trans,type:"delete",keys:keys})}).then(function(_a){var numFailures=_a.numFailures,lastResult=_a.lastResult,failures=_a.failures;if(numFailures===0)return lastResult;throw new BulkError(_this.name+".bulkDelete(): "+numFailures+" of "+numKeys+" operations failed",failures)})};return Table}();function Events(ctx){var evs={};var rv=function(eventName,subscriber){if(subscriber){var i=arguments.length,args=new Array(i-1);while(--i)args[i-1]=arguments[i];evs[eventName].subscribe.apply(null,args);return ctx}else if(typeof eventName==="string"){return evs[eventName]}};rv.addEventType=add;for(var i=1,l=arguments.length;ib?1:abVal?order:0}return this.toArray(function(a){return a.sort(sorter)}).then(cb)};Collection.prototype.toArray=function(cb){var _this=this;return this._read(function(trans){var ctx=_this._ctx;if(ctx.dir==="next"&&isPlainKeyRange(ctx,true)&&ctx.limit>0){var valueMapper_1=ctx.valueMapper;var index=getIndexOrStore(ctx,ctx.table.core.schema);return ctx.table.core.query({trans:trans,limit:ctx.limit,values:true,query:{index:index,range:ctx.range}}).then(function(_a){var result=_a.result;return valueMapper_1?result.map(valueMapper_1):result})}else{var a_1=[];return iter(ctx,function(item){return a_1.push(item)},trans,ctx.table.core).then(function(){return a_1})}},cb)};Collection.prototype.offset=function(offset){var ctx=this._ctx;if(offset<=0)return this;ctx.offset+=offset;if(isPlainKeyRange(ctx)){addReplayFilter(ctx,function(){var offsetLeft=offset;return function(cursor,advance){if(offsetLeft===0)return true;if(offsetLeft===1){--offsetLeft;return false}advance(function(){cursor.advance(offsetLeft);offsetLeft=0});return false}})}else{addReplayFilter(ctx,function(){var offsetLeft=offset;return function(){return--offsetLeft<0}})}return this};Collection.prototype.limit=function(numRows){this._ctx.limit=Math.min(this._ctx.limit,numRows);addReplayFilter(this._ctx,function(){var rowsLeft=numRows;return function(cursor,advance,resolve){if(--rowsLeft<=0)advance(resolve);return rowsLeft>=0}},true);return this};Collection.prototype.until=function(filterFunction,bIncludeStopEntry){addFilter(this._ctx,function(cursor,advance,resolve){if(filterFunction(cursor.value)){advance(resolve);return bIncludeStopEntry}else{return true}});return this};Collection.prototype.first=function(cb){return this.limit(1).toArray(function(a){return a[0]}).then(cb)};Collection.prototype.last=function(cb){return this.reverse().first(cb)};Collection.prototype.filter=function(filterFunction){addFilter(this._ctx,function(cursor){return filterFunction(cursor.value)});addMatchFilter(this._ctx,filterFunction);return this};Collection.prototype.and=function(filter){return this.filter(filter)};Collection.prototype.or=function(indexName){return new this.db.WhereClause(this._ctx.table,indexName,this)};Collection.prototype.reverse=function(){this._ctx.dir=this._ctx.dir==="prev"?"next":"prev";if(this._ondirectionchange)this._ondirectionchange(this._ctx.dir);return this};Collection.prototype.desc=function(){return this.reverse()};Collection.prototype.eachKey=function(cb){var ctx=this._ctx;ctx.keysOnly=!ctx.isMatch;return this.each(function(val,cursor){cb(cursor.key,cursor)})};Collection.prototype.eachUniqueKey=function(cb){this._ctx.unique="unique";return this.eachKey(cb)};Collection.prototype.eachPrimaryKey=function(cb){var ctx=this._ctx;ctx.keysOnly=!ctx.isMatch;return this.each(function(val,cursor){cb(cursor.primaryKey,cursor)})};Collection.prototype.keys=function(cb){var ctx=this._ctx;ctx.keysOnly=!ctx.isMatch;var a=[];return this.each(function(item,cursor){a.push(cursor.key)}).then(function(){return a}).then(cb)};Collection.prototype.primaryKeys=function(cb){var ctx=this._ctx;if(ctx.dir==="next"&&isPlainKeyRange(ctx,true)&&ctx.limit>0){return this._read(function(trans){var index=getIndexOrStore(ctx,ctx.table.core.schema);return ctx.table.core.query({trans:trans,values:false,limit:ctx.limit,query:{index:index,range:ctx.range}})}).then(function(_a){var result=_a.result;return result}).then(cb)}ctx.keysOnly=!ctx.isMatch;var a=[];return this.each(function(item,cursor){a.push(cursor.primaryKey)}).then(function(){return a}).then(cb)};Collection.prototype.uniqueKeys=function(cb){this._ctx.unique="unique";return this.keys(cb)};Collection.prototype.firstKey=function(cb){return this.limit(1).keys(function(a){return a[0]}).then(cb)};Collection.prototype.lastKey=function(cb){return this.reverse().firstKey(cb)};Collection.prototype.distinct=function(){var ctx=this._ctx,idx=ctx.index&&ctx.table.schema.idxByName[ctx.index];if(!idx||!idx.multi)return this;var set={};addFilter(this._ctx,function(cursor){var strKey=cursor.primaryKey.toString();var found=hasOwn(set,strKey);set[strKey]=true;return!found});return this};Collection.prototype.modify=function(changes){var _this=this;var ctx=this._ctx;return this._write(function(trans){var modifyer;if(typeof changes==="function"){modifyer=changes}else{var keyPaths=keys(changes);var numKeys=keyPaths.length;modifyer=function(item){var anythingModified=false;for(var i=0;i0&&coreTable.mutate({trans:trans,type:"add",values:addValues}).then(function(res){for(var pos in res.failures){deleteKeys.splice(parseInt(pos),1)}applyMutateResult(addValues.length,res)})).then(function(){return(putValues.length>0||criteria&&typeof changes==="object")&&coreTable.mutate({trans:trans,type:"put",keys:putKeys,values:putValues,criteria:criteria,changeSpec:typeof changes!=="function"&&changes}).then(function(res){return applyMutateResult(putValues.length,res)})}).then(function(){return(deleteKeys.length>0||criteria&&changes===deleteCallback)&&coreTable.mutate({trans:trans,type:"delete",keys:deleteKeys,criteria:criteria}).then(function(res){return applyMutateResult(deleteKeys.length,res)})}).then(function(){return keys.length>offset+count&&nextChunk(offset+limit)})})};return nextChunk(0).then(function(){if(totalFailures.length>0)throw new ModifyError("Error modifying one or more objects",totalFailures,successCount,failedKeys);return keys.length})})})};Collection.prototype.delete=function(){var ctx=this._ctx,range=ctx.range;if(isPlainKeyRange(ctx)&&(ctx.isPrimKey&&!hangsOnDeleteLargeKeyRange||range.type===3)){return this._write(function(trans){var primaryKey=ctx.table.core.schema.primaryKey;var coreRange=range;return ctx.table.core.count({trans:trans,query:{index:primaryKey,range:coreRange}}).then(function(count){return ctx.table.core.mutate({trans:trans,type:"deleteRange",range:coreRange}).then(function(_a){var failures=_a.failures;_a.lastResult;_a.results;var numFailures=_a.numFailures;if(numFailures)throw new ModifyError("Could not delete some values",Object.keys(failures).map(function(pos){return failures[pos]}),count-numFailures);return count-numFailures})})})}return this.modify(deleteCallback)};return Collection}();var deleteCallback=function(value,ctx){return ctx.value=null};function createCollectionConstructor(db){return makeClassConstructor(Collection.prototype,function Collection(whereClause,keyRangeGenerator){this.db=db;var keyRange=AnyRange,error=null;if(keyRangeGenerator)try{keyRange=keyRangeGenerator()}catch(ex){error=ex}var whereCtx=whereClause._ctx;var table=whereCtx.table;var readingHook=table.hook.reading.fire;this._ctx={table:table,index:whereCtx.index,isPrimKey:!whereCtx.index||table.schema.primKey.keyPath&&whereCtx.index===table.schema.primKey.name,range:keyRange,keysOnly:false,dir:"next",unique:"",algorithm:null,filter:null,replayFilter:null,justLimit:true,isMatch:null,offset:0,limit:Infinity,error:error,or:whereCtx.or,valueMapper:readingHook!==mirror?readingHook:null}})}function simpleCompare(a,b){return ab?-1:a===b?0:1}function fail(collectionOrWhereClause,err,T){var collection=collectionOrWhereClause instanceof WhereClause?new collectionOrWhereClause.Collection(collectionOrWhereClause):collectionOrWhereClause;collection._ctx.error=T?new T(err):new TypeError(err);return collection}function emptyCollection(whereClause){return new whereClause.Collection(whereClause,function(){return rangeEqual("")}).limit(0)}function upperFactory(dir){return dir==="next"?function(s){return s.toUpperCase()}:function(s){return s.toLowerCase()}}function lowerFactory(dir){return dir==="next"?function(s){return s.toLowerCase()}:function(s){return s.toUpperCase()}}function nextCasing(key,lowerKey,upperNeedle,lowerNeedle,cmp,dir){var length=Math.min(key.length,lowerNeedle.length);var llp=-1;for(var i=0;i=0)return key.substr(0,llp)+lowerKey[llp]+upperNeedle.substr(llp+1);return null}if(cmp(key[i],lwrKeyChar)<0)llp=i}if(length0){lowestPossibleCasing=casing}}if(lowestPossibleCasing!==null){advance(function(){cursor.continue(lowestPossibleCasing+nextKeySuffix)})}else{advance(resolve)}return false}});return c}function createRange(lower,upper,lowerOpen,upperOpen){return{type:2,lower:lower,upper:upper,lowerOpen:lowerOpen,upperOpen:upperOpen}}function rangeEqual(value){return{type:1,lower:value,upper:value}}var WhereClause=function(){function WhereClause(){}Object.defineProperty(WhereClause.prototype,"Collection",{get:function(){return this._ctx.table.db.Collection},enumerable:false,configurable:true});WhereClause.prototype.between=function(lower,upper,includeLower,includeUpper){includeLower=includeLower!==false;includeUpper=includeUpper===true;try{if(this._cmp(lower,upper)>0||this._cmp(lower,upper)===0&&(includeLower||includeUpper)&&!(includeLower&&includeUpper))return emptyCollection(this);return new this.Collection(this,function(){return createRange(lower,upper,!includeLower,!includeUpper)})}catch(e){return fail(this,INVALID_KEY_ARGUMENT)}};WhereClause.prototype.equals=function(value){if(value==null)return fail(this,INVALID_KEY_ARGUMENT);return new this.Collection(this,function(){return rangeEqual(value)})};WhereClause.prototype.above=function(value){if(value==null)return fail(this,INVALID_KEY_ARGUMENT);return new this.Collection(this,function(){return createRange(value,undefined,true)})};WhereClause.prototype.aboveOrEqual=function(value){if(value==null)return fail(this,INVALID_KEY_ARGUMENT);return new this.Collection(this,function(){return createRange(value,undefined,false)})};WhereClause.prototype.below=function(value){if(value==null)return fail(this,INVALID_KEY_ARGUMENT);return new this.Collection(this,function(){return createRange(undefined,value,false,true)})};WhereClause.prototype.belowOrEqual=function(value){if(value==null)return fail(this,INVALID_KEY_ARGUMENT);return new this.Collection(this,function(){return createRange(undefined,value)})};WhereClause.prototype.startsWith=function(str){if(typeof str!=="string")return fail(this,STRING_EXPECTED);return this.between(str,str+maxString,true,true)};WhereClause.prototype.startsWithIgnoreCase=function(str){if(str==="")return this.startsWith(str);return addIgnoreCaseAlgorithm(this,function(x,a){return x.indexOf(a[0])===0},[str],maxString)};WhereClause.prototype.equalsIgnoreCase=function(str){return addIgnoreCaseAlgorithm(this,function(x,a){return x===a[0]},[str],"")};WhereClause.prototype.anyOfIgnoreCase=function(){var set=getArrayOf.apply(NO_CHAR_ARRAY,arguments);if(set.length===0)return emptyCollection(this);return addIgnoreCaseAlgorithm(this,function(x,a){return a.indexOf(x)!==-1},set,"")};WhereClause.prototype.startsWithAnyOfIgnoreCase=function(){var set=getArrayOf.apply(NO_CHAR_ARRAY,arguments);if(set.length===0)return emptyCollection(this);return addIgnoreCaseAlgorithm(this,function(x,a){return a.some(function(n){return x.indexOf(n)===0})},set,maxString)};WhereClause.prototype.anyOf=function(){var _this=this;var set=getArrayOf.apply(NO_CHAR_ARRAY,arguments);var compare=this._cmp;try{set.sort(compare)}catch(e){return fail(this,INVALID_KEY_ARGUMENT)}if(set.length===0)return emptyCollection(this);var c=new this.Collection(this,function(){return createRange(set[0],set[set.length-1])});c._ondirectionchange=function(direction){compare=direction==="next"?_this._ascending:_this._descending;set.sort(compare)};var i=0;c._addAlgorithm(function(cursor,advance,resolve){var key=cursor.key;while(compare(key,set[i])>0){++i;if(i===set.length){advance(resolve);return false}}if(compare(key,set[i])===0){return true}else{advance(function(){cursor.continue(set[i])});return false}});return c};WhereClause.prototype.notEqual=function(value){return this.inAnyRange([[minKey,value],[value,this.db._maxKey]],{includeLowers:false,includeUppers:false})};WhereClause.prototype.noneOf=function(){var set=getArrayOf.apply(NO_CHAR_ARRAY,arguments);if(set.length===0)return new this.Collection(this);try{set.sort(this._ascending)}catch(e){return fail(this,INVALID_KEY_ARGUMENT)}var ranges=set.reduce(function(res,val){return res?res.concat([[res[res.length-1][1],val]]):[[minKey,val]]},null);ranges.push([set[set.length-1],this.db._maxKey]);return this.inAnyRange(ranges,{includeLowers:false,includeUppers:false})};WhereClause.prototype.inAnyRange=function(ranges,options){var _this=this;var cmp=this._cmp,ascending=this._ascending,descending=this._descending,min=this._min,max=this._max;if(ranges.length===0)return emptyCollection(this);if(!ranges.every(function(range){return range[0]!==undefined&&range[1]!==undefined&&ascending(range[0],range[1])<=0})){return fail(this,"First argument to inAnyRange() must be an Array of two-value Arrays [lower,upper] where upper must not be lower than lower",exceptions.InvalidArgument)}var includeLowers=!options||options.includeLowers!==false;var includeUppers=options&&options.includeUppers===true;function addRange(ranges,newRange){var i=0,l=ranges.length;for(;i0){range[0]=min(range[0],newRange[0]);range[1]=max(range[1],newRange[1]);break}}if(i===l)ranges.push(newRange);return ranges}var sortDirection=ascending;function rangeSorter(a,b){return sortDirection(a[0],b[0])}var set;try{set=ranges.reduce(addRange,[]);set.sort(rangeSorter)}catch(ex){return fail(this,INVALID_KEY_ARGUMENT)}var rangePos=0;var keyIsBeyondCurrentEntry=includeUppers?function(key){return ascending(key,set[rangePos][1])>0}:function(key){return ascending(key,set[rangePos][1])>=0};var keyIsBeforeCurrentEntry=includeLowers?function(key){return descending(key,set[rangePos][0])>0}:function(key){return descending(key,set[rangePos][0])>=0};function keyWithinCurrentRange(key){return!keyIsBeyondCurrentEntry(key)&&!keyIsBeforeCurrentEntry(key)}var checkKey=keyIsBeyondCurrentEntry;var c=new this.Collection(this,function(){return createRange(set[0][0],set[set.length-1][1],!includeLowers,!includeUppers)});c._ondirectionchange=function(direction){if(direction==="next"){checkKey=keyIsBeyondCurrentEntry;sortDirection=ascending}else{checkKey=keyIsBeforeCurrentEntry;sortDirection=descending}set.sort(rangeSorter)};c._addAlgorithm(function(cursor,advance,resolve){var key=cursor.key;while(checkKey(key)){++rangePos;if(rangePos===set.length){advance(resolve);return false}}if(keyWithinCurrentRange(key)){return true}else if(_this._cmp(key,set[rangePos][1])===0||_this._cmp(key,set[rangePos][0])===0){return false}else{advance(function(){if(sortDirection===ascending)cursor.continue(set[rangePos][0]);else cursor.continue(set[rangePos][1])});return false}});return c};WhereClause.prototype.startsWithAnyOf=function(){var set=getArrayOf.apply(NO_CHAR_ARRAY,arguments);if(!set.every(function(s){return typeof s==="string"})){return fail(this,"startsWithAnyOf() only works with strings")}if(set.length===0)return emptyCollection(this);return this.inAnyRange(set.map(function(str){return[str,str+maxString]}))};return WhereClause}();function createWhereClauseConstructor(db){return makeClassConstructor(WhereClause.prototype,function WhereClause(table,index,orCollection){this.db=db;this._ctx={table:table,index:index===":id"?null:index,or:orCollection};var indexedDB=db._deps.indexedDB;if(!indexedDB)throw new exceptions.MissingAPI;this._cmp=this._ascending=indexedDB.cmp.bind(indexedDB);this._descending=function(a,b){return indexedDB.cmp(b,a)};this._max=function(a,b){return indexedDB.cmp(a,b)>0?a:b};this._min=function(a,b){return indexedDB.cmp(a,b)<0?a:b};this._IDBKeyRange=db._deps.IDBKeyRange})}function eventRejectHandler(reject){return wrap(function(event){preventDefault(event);reject(event.target.error);return false})}function preventDefault(event){if(event.stopPropagation)event.stopPropagation();if(event.preventDefault)event.preventDefault()}var DEXIE_STORAGE_MUTATED_EVENT_NAME="storagemutated";var STORAGE_MUTATED_DOM_EVENT_NAME="x-storagemutated-1";var globalEvents=Events(null,DEXIE_STORAGE_MUTATED_EVENT_NAME);var Transaction=function(){function Transaction(){}Transaction.prototype._lock=function(){assert(!PSD.global);++this._reculock;if(this._reculock===1&&!PSD.global)PSD.lockOwnerFor=this;return this};Transaction.prototype._unlock=function(){assert(!PSD.global);if(--this._reculock===0){if(!PSD.global)PSD.lockOwnerFor=null;while(this._blockedFuncs.length>0&&!this._locked()){var fnAndPSD=this._blockedFuncs.shift();try{usePSD(fnAndPSD[1],fnAndPSD[0])}catch(e){}}}return this};Transaction.prototype._locked=function(){return this._reculock&&PSD.lockOwnerFor!==this};Transaction.prototype.create=function(idbtrans){var _this=this;if(!this.mode)return this;var idbdb=this.db.idbdb;var dbOpenError=this.db._state.dbOpenError;assert(!this.idbtrans);if(!idbtrans&&!idbdb){switch(dbOpenError&&dbOpenError.name){case"DatabaseClosedError":throw new exceptions.DatabaseClosed(dbOpenError);case"MissingAPIError":throw new exceptions.MissingAPI(dbOpenError.message,dbOpenError);default:throw new exceptions.OpenFailed(dbOpenError)}}if(!this.active)throw new exceptions.TransactionInactive;assert(this._completion._state===null);idbtrans=this.idbtrans=idbtrans||(this.db.core?this.db.core.transaction(this.storeNames,this.mode,{durability:this.chromeTransactionDurability}):idbdb.transaction(this.storeNames,this.mode,{durability:this.chromeTransactionDurability}));idbtrans.onerror=wrap(function(ev){preventDefault(ev);_this._reject(idbtrans.error)});idbtrans.onabort=wrap(function(ev){preventDefault(ev);_this.active&&_this._reject(new exceptions.Abort(idbtrans.error));_this.active=false;_this.on("abort").fire(ev)});idbtrans.oncomplete=wrap(function(){_this.active=false;_this._resolve();if("mutatedParts"in idbtrans){globalEvents.storagemutated.fire(idbtrans["mutatedParts"])}});return this};Transaction.prototype._promise=function(mode,fn,bWriteLock){var _this=this;if(mode==="readwrite"&&this.mode!=="readwrite")return rejection(new exceptions.ReadOnly("Transaction is readonly"));if(!this.active)return rejection(new exceptions.TransactionInactive);if(this._locked()){return new DexiePromise(function(resolve,reject){_this._blockedFuncs.push([function(){_this._promise(mode,fn,bWriteLock).then(resolve,reject)},PSD])})}else if(bWriteLock){return newScope(function(){var p=new DexiePromise(function(resolve,reject){_this._lock();var rv=fn(resolve,reject,_this);if(rv&&rv.then)rv.then(resolve,reject)});p.finally(function(){return _this._unlock()});p._lib=true;return p})}else{var p=new DexiePromise(function(resolve,reject){var rv=fn(resolve,reject,_this);if(rv&&rv.then)rv.then(resolve,reject)});p._lib=true;return p}};Transaction.prototype._root=function(){return this.parent?this.parent._root():this};Transaction.prototype.waitFor=function(promiseLike){var root=this._root();var promise=DexiePromise.resolve(promiseLike);if(root._waitingFor){root._waitingFor=root._waitingFor.then(function(){return promise})}else{root._waitingFor=promise;root._waitingQueue=[];var store=root.idbtrans.objectStore(root.storeNames[0]);(function spin(){++root._spinCount;while(root._waitingQueue.length)root._waitingQueue.shift()();if(root._waitingFor)store.get(-Infinity).onsuccess=spin})()}var currentWaitPromise=root._waitingFor;return new DexiePromise(function(resolve,reject){promise.then(function(res){return root._waitingQueue.push(wrap(resolve.bind(null,res)))},function(err){return root._waitingQueue.push(wrap(reject.bind(null,err)))}).finally(function(){if(root._waitingFor===currentWaitPromise){root._waitingFor=null}})})};Transaction.prototype.abort=function(){if(this.active){this.active=false;if(this.idbtrans)this.idbtrans.abort();this._reject(new exceptions.Abort)}};Transaction.prototype.table=function(tableName){var memoizedTables=this._memoizedTables||(this._memoizedTables={});if(hasOwn(memoizedTables,tableName))return memoizedTables[tableName];var tableSchema=this.schema[tableName];if(!tableSchema){throw new exceptions.NotFound("Table "+tableName+" not part of transaction")}var transactionBoundTable=new this.db.Table(tableName,tableSchema,this);transactionBoundTable.core=this.db.core.table(tableName);memoizedTables[tableName]=transactionBoundTable;return transactionBoundTable};return Transaction}();function createTransactionConstructor(db){return makeClassConstructor(Transaction.prototype,function Transaction(mode,storeNames,dbschema,chromeTransactionDurability,parent){var _this=this;this.db=db;this.mode=mode;this.storeNames=storeNames;this.schema=dbschema;this.chromeTransactionDurability=chromeTransactionDurability;this.idbtrans=null;this.on=Events(this,"complete","error","abort");this.parent=parent||null;this.active=true;this._reculock=0;this._blockedFuncs=[];this._resolve=null;this._reject=null;this._waitingFor=null;this._waitingQueue=null;this._spinCount=0;this._completion=new DexiePromise(function(resolve,reject){_this._resolve=resolve;_this._reject=reject});this._completion.then(function(){_this.active=false;_this.on.complete.fire()},function(e){var wasActive=_this.active;_this.active=false;_this.on.error.fire(e);_this.parent?_this.parent._reject(e):wasActive&&_this.idbtrans&&_this.idbtrans.abort();return rejection(e)})})}function createIndexSpec(name,keyPath,unique,multi,auto,compound,isPrimKey){return{name:name,keyPath:keyPath,unique:unique,multi:multi,auto:auto,compound:compound,src:(unique&&!isPrimKey?"&":"")+(multi?"*":"")+(auto?"++":"")+nameFromKeyPath(keyPath)}}function nameFromKeyPath(keyPath){return typeof keyPath==="string"?keyPath:keyPath?"["+[].join.call(keyPath,"+")+"]":""}function createTableSchema(name,primKey,indexes){return{name:name,primKey:primKey,indexes:indexes,mappedClass:null,idxByName:arrayToObject(indexes,function(index){return[index.name,index]})}}function safariMultiStoreFix(storeNames){return storeNames.length===1?storeNames[0]:storeNames}var getMaxKey=function(IdbKeyRange){try{IdbKeyRange.only([[]]);getMaxKey=function(){return[[]]};return[[]]}catch(e){getMaxKey=function(){return maxString};return maxString}};function getKeyExtractor(keyPath){if(keyPath==null){return function(){return undefined}}else if(typeof keyPath==="string"){return getSinglePathKeyExtractor(keyPath)}else{return function(obj){return getByKeyPath(obj,keyPath)}}}function getSinglePathKeyExtractor(keyPath){var split=keyPath.split(".");if(split.length===1){return function(obj){return obj[keyPath]}}else{return function(obj){return getByKeyPath(obj,keyPath)}}}function arrayify(arrayLike){return[].slice.call(arrayLike)}var _id_counter=0;function getKeyPathAlias(keyPath){return keyPath==null?":id":typeof keyPath==="string"?keyPath:"["+keyPath.join("+")+"]"}function createDBCore(db,IdbKeyRange,tmpTrans){function extractSchema(db,trans){var tables=arrayify(db.objectStoreNames);return{schema:{name:db.name,tables:tables.map(function(table){return trans.objectStore(table)}).map(function(store){var keyPath=store.keyPath,autoIncrement=store.autoIncrement;var compound=isArray(keyPath);var outbound=keyPath==null;var indexByKeyPath={};var result={name:store.name,primaryKey:{name:null,isPrimaryKey:true,outbound:outbound,compound:compound,keyPath:keyPath,autoIncrement:autoIncrement,unique:true,extractKey:getKeyExtractor(keyPath)},indexes:arrayify(store.indexNames).map(function(indexName){return store.index(indexName)}).map(function(index){var name=index.name,unique=index.unique,multiEntry=index.multiEntry,keyPath=index.keyPath;var compound=isArray(keyPath);var result={name:name,compound:compound,keyPath:keyPath,unique:unique,multiEntry:multiEntry,extractKey:getKeyExtractor(keyPath)};indexByKeyPath[getKeyPathAlias(keyPath)]=result;return result}),getIndexByKeyPath:function(keyPath){return indexByKeyPath[getKeyPathAlias(keyPath)]}};indexByKeyPath[":id"]=result.primaryKey;if(keyPath!=null){indexByKeyPath[getKeyPathAlias(keyPath)]=result.primaryKey}return result})},hasGetAll:tables.length>0&&"getAll"in trans.objectStore(tables[0])&&!(typeof navigator!=="undefined"&&/Safari/.test(navigator.userAgent)&&!/(Chrome\/|Edge\/)/.test(navigator.userAgent)&&[].concat(navigator.userAgent.match(/Safari\/(\d*)/))[1]<604)}}function makeIDBKeyRange(range){if(range.type===3)return null;if(range.type===4)throw new Error("Cannot convert never type to IDBKeyRange");var lower=range.lower,upper=range.upper,lowerOpen=range.lowerOpen,upperOpen=range.upperOpen;var idbRange=lower===undefined?upper===undefined?null:IdbKeyRange.upperBound(upper,!!upperOpen):upper===undefined?IdbKeyRange.lowerBound(lower,!!lowerOpen):IdbKeyRange.bound(lower,upper,!!lowerOpen,!!upperOpen);return idbRange}function createDbCoreTable(tableSchema){var tableName=tableSchema.name;function mutate(_a){var trans=_a.trans,type=_a.type,keys=_a.keys,values=_a.values,range=_a.range;return new Promise(function(resolve,reject){resolve=wrap(resolve);var store=trans.objectStore(tableName);var outbound=store.keyPath==null;var isAddOrPut=type==="put"||type==="add";if(!isAddOrPut&&type!=="delete"&&type!=="deleteRange")throw new Error("Invalid operation type: "+type);var length=(keys||values||{length:1}).length;if(keys&&values&&keys.length!==values.length){throw new Error("Given keys array must have same length as given values array.")}if(length===0)return resolve({numFailures:0,failures:{},results:[],lastResult:undefined});var req;var reqs=[];var failures=[];var numFailures=0;var errorHandler=function(event){++numFailures;preventDefault(event)};if(type==="deleteRange"){if(range.type===4)return resolve({numFailures:numFailures,failures:failures,results:[],lastResult:undefined});if(range.type===3)reqs.push(req=store.clear());else reqs.push(req=store.delete(makeIDBKeyRange(range)))}else{var _a=isAddOrPut?outbound?[values,keys]:[values,null]:[keys,null],args1=_a[0],args2=_a[1];if(isAddOrPut){for(var i=0;i=oldVersion});versToRun.forEach(function(version){queue.push(function(){var oldSchema=globalSchema;var newSchema=version._cfg.dbschema;adjustToExistingIndexNames(db,oldSchema,idbUpgradeTrans);adjustToExistingIndexNames(db,newSchema,idbUpgradeTrans);globalSchema=db._dbSchema=newSchema;var diff=getSchemaDiff(oldSchema,newSchema);diff.add.forEach(function(tuple){createTable(idbUpgradeTrans,tuple[0],tuple[1].primKey,tuple[1].indexes)});diff.change.forEach(function(change){if(change.recreate){throw new exceptions.Upgrade("Not yet support for changing primary key")}else{var store_1=idbUpgradeTrans.objectStore(change.name);change.add.forEach(function(idx){return addIndex(store_1,idx)});change.change.forEach(function(idx){store_1.deleteIndex(idx.name);addIndex(store_1,idx)});change.del.forEach(function(idxName){return store_1.deleteIndex(idxName)})}});var contentUpgrade=version._cfg.contentUpgrade;if(contentUpgrade&&version._cfg.version>oldVersion){generateMiddlewareStacks(db,idbUpgradeTrans);trans._memoizedTables={};anyContentUpgraderHasRun=true;var upgradeSchema_1=shallowClone(newSchema);diff.del.forEach(function(table){upgradeSchema_1[table]=oldSchema[table]});removeTablesApi(db,[db.Transaction.prototype]);setApiOnPlace(db,[db.Transaction.prototype],keys(upgradeSchema_1),upgradeSchema_1);trans.schema=upgradeSchema_1;var contentUpgradeIsAsync_1=isAsyncFunction(contentUpgrade);if(contentUpgradeIsAsync_1){incrementExpectedAwaits()}var returnValue_1;var promiseFollowed=DexiePromise.follow(function(){returnValue_1=contentUpgrade(trans);if(returnValue_1){if(contentUpgradeIsAsync_1){var decrementor=decrementExpectedAwaits.bind(null,null);returnValue_1.then(decrementor,decrementor)}}});return returnValue_1&&typeof returnValue_1.then==="function"?DexiePromise.resolve(returnValue_1):promiseFollowed.then(function(){return returnValue_1})}});queue.push(function(idbtrans){if(!anyContentUpgraderHasRun||!hasIEDeleteObjectStoreBug){var newSchema=version._cfg.dbschema;deleteRemovedTables(newSchema,idbtrans)}removeTablesApi(db,[db.Transaction.prototype]);setApiOnPlace(db,[db.Transaction.prototype],db._storeNames,db._dbSchema);trans.schema=db._dbSchema})});function runQueue(){return queue.length?DexiePromise.resolve(queue.shift()(trans.idbtrans)).then(runQueue):DexiePromise.resolve()}return runQueue().then(function(){createMissingTables(globalSchema,idbUpgradeTrans)})}function getSchemaDiff(oldSchema,newSchema){var diff={del:[],add:[],change:[]};var table;for(table in oldSchema){if(!newSchema[table])diff.del.push(table)}for(table in newSchema){var oldDef=oldSchema[table],newDef=newSchema[table];if(!oldDef){diff.add.push([table,newDef])}else{var change={name:table,def:newDef,recreate:false,del:[],add:[],change:[]};if(""+(oldDef.primKey.keyPath||"")!==""+(newDef.primKey.keyPath||"")||oldDef.primKey.auto!==newDef.primKey.auto&&!isIEOrEdge){change.recreate=true;diff.change.push(change)}else{var oldIndexes=oldDef.idxByName;var newIndexes=newDef.idxByName;var idxName=void 0;for(idxName in oldIndexes){if(!newIndexes[idxName])change.del.push(idxName)}for(idxName in newIndexes){var oldIdx=oldIndexes[idxName],newIdx=newIndexes[idxName];if(!oldIdx)change.add.push(newIdx);else if(oldIdx.src!==newIdx.src)change.change.push(newIdx)}if(change.del.length>0||change.add.length>0||change.change.length>0){diff.change.push(change)}}}}return diff}function createTable(idbtrans,tableName,primKey,indexes){var store=idbtrans.db.createObjectStore(tableName,primKey.keyPath?{keyPath:primKey.keyPath,autoIncrement:primKey.auto}:{autoIncrement:primKey.auto});indexes.forEach(function(idx){return addIndex(store,idx)});return store}function createMissingTables(newSchema,idbtrans){keys(newSchema).forEach(function(tableName){if(!idbtrans.db.objectStoreNames.contains(tableName)){createTable(idbtrans,tableName,newSchema[tableName].primKey,newSchema[tableName].indexes)}})}function deleteRemovedTables(newSchema,idbtrans){[].slice.call(idbtrans.db.objectStoreNames).forEach(function(storeName){return newSchema[storeName]==null&&idbtrans.db.deleteObjectStore(storeName)})}function addIndex(store,idx){store.createIndex(idx.name,idx.keyPath,{unique:idx.unique,multiEntry:idx.multi})}function buildGlobalSchema(db,idbdb,tmpTrans){var globalSchema={};var dbStoreNames=slice(idbdb.objectStoreNames,0);dbStoreNames.forEach(function(storeName){var store=tmpTrans.objectStore(storeName);var keyPath=store.keyPath;var primKey=createIndexSpec(nameFromKeyPath(keyPath),keyPath||"",false,false,!!store.autoIncrement,keyPath&&typeof keyPath!=="string",true);var indexes=[];for(var j=0;jMath.pow(2,62)?0:e.oldVersion;wasCreated=oldVer<1;db._novip.idbdb=req.result;runUpgraders(db,oldVer/10,upgradeTransaction,reject)}},reject);req.onsuccess=wrap(function(){upgradeTransaction=null;var idbdb=db._novip.idbdb=req.result;var objectStoreNames=slice(idbdb.objectStoreNames);if(objectStoreNames.length>0)try{var tmpTrans=idbdb.transaction(safariMultiStoreFix(objectStoreNames),"readonly");if(state.autoSchema)readGlobalSchema(db,idbdb,tmpTrans);else{adjustToExistingIndexNames(db,db._dbSchema,tmpTrans);if(!verifyInstalledSchema(db,tmpTrans)){console.warn("Dexie SchemaDiff: Schema was extended without increasing the number passed to db.version(). Some queries may fail.")}}generateMiddlewareStacks(db,tmpTrans)}catch(e){}connections.push(db);idbdb.onversionchange=wrap(function(ev){state.vcFired=true;db.on("versionchange").fire(ev)});idbdb.onclose=wrap(function(ev){db.on("close").fire(ev)});if(wasCreated)_onDatabaseCreated(db._deps,dbName);resolve()},reject)})})]).then(function(){throwIfCancelled();state.onReadyBeingFired=[];return DexiePromise.resolve(vip(function(){return db.on.ready.fire(db.vip)})).then(function fireRemainders(){if(state.onReadyBeingFired.length>0){var remainders_1=state.onReadyBeingFired.reduce(promisableChain,nop);state.onReadyBeingFired=[];return DexiePromise.resolve(vip(function(){return remainders_1(db.vip)})).then(fireRemainders)}})}).finally(function(){state.onReadyBeingFired=null;state.isBeingOpened=false}).then(function(){return db}).catch(function(err){state.dbOpenError=err;try{upgradeTransaction&&upgradeTransaction.abort()}catch(_a){}if(openCanceller===state.openCanceller){db._close()}return rejection(err)}).finally(function(){state.openComplete=true;resolveDbReady()})}function awaitIterator(iterator){var callNext=function(result){return iterator.next(result)},doThrow=function(error){return iterator.throw(error)},onSuccess=step(callNext),onError=step(doThrow);function step(getNext){return function(val){var next=getNext(val),value=next.value;return next.done?value:!value||typeof value.then!=="function"?isArray(value)?Promise.all(value).then(onSuccess,onError):onSuccess(value):value.then(onSuccess,onError)}}return step(callNext)()}function extractTransactionArgs(mode,_tableArgs_,scopeFunc){var i=arguments.length;if(i<2)throw new exceptions.InvalidArgument("Too few arguments");var args=new Array(i-1);while(--i)args[i-1]=arguments[i];scopeFunc=args.pop();var tables=flatten(args);return[mode,tables,scopeFunc]}function enterTransactionScope(db,mode,storeNames,parentTransaction,scopeFunc){return DexiePromise.resolve().then(function(){var transless=PSD.transless||PSD;var trans=db._createTransaction(mode,storeNames,db._dbSchema,parentTransaction);var zoneProps={trans:trans,transless:transless};if(parentTransaction){trans.idbtrans=parentTransaction.idbtrans}else{try{trans.create();db._state.PR1398_maxLoop=3}catch(ex){if(ex.name===errnames.InvalidState&&db.isOpen()&&--db._state.PR1398_maxLoop>0){console.warn("Dexie: Need to reopen db");db._close();return db.open().then(function(){return enterTransactionScope(db,mode,storeNames,null,scopeFunc)})}return rejection(ex)}}var scopeFuncIsAsync=isAsyncFunction(scopeFunc);if(scopeFuncIsAsync){incrementExpectedAwaits()}var returnValue;var promiseFollowed=DexiePromise.follow(function(){returnValue=scopeFunc.call(trans,trans);if(returnValue){if(scopeFuncIsAsync){var decrementor=decrementExpectedAwaits.bind(null,null);returnValue.then(decrementor,decrementor)}else if(typeof returnValue.next==="function"&&typeof returnValue.throw==="function"){returnValue=awaitIterator(returnValue)}}},zoneProps);return(returnValue&&typeof returnValue.then==="function"?DexiePromise.resolve(returnValue).then(function(x){return trans.active?x:rejection(new exceptions.PrematureCommit("Transaction committed too early. See http://bit.ly/2kdckMn"))}):promiseFollowed.then(function(){return returnValue})).then(function(x){if(parentTransaction)trans._resolve();return trans._completion.then(function(){return x})}).catch(function(e){trans._reject(e);return rejection(e)})})}function pad(a,value,count){var result=isArray(a)?a.slice():[a];for(var i=0;i0;var virtualIndex=__assign(__assign({},lowLevelIndex),{isVirtual:isVirtual,keyTail:keyTail,keyLength:keyLength,extractKey:getKeyExtractor(keyPath),unique:!isVirtual&&lowLevelIndex.unique});indexList.push(virtualIndex);if(!virtualIndex.isPrimaryKey){allVirtualIndexes.push(virtualIndex)}if(keyLength>1){var virtualKeyPath=keyLength===2?keyPath[0]:keyPath.slice(0,keyLength-1);addVirtualIndexes(virtualKeyPath,keyTail+1,lowLevelIndex)}indexList.sort(function(a,b){return a.keyTail-b.keyTail});return virtualIndex}var primaryKey=addVirtualIndexes(schema.primaryKey.keyPath,0,schema.primaryKey);indexLookup[":id"]=[primaryKey];for(var _i=0,_a=schema.indexes;_i<_a.length;_i++){var index=_a[_i];addVirtualIndexes(index.keyPath,0,index)}function findBestIndex(keyPath){var result=indexLookup[getKeyPathAlias(keyPath)];return result&&result[0]}function translateRange(range,keyTail){return{type:range.type===1?2:range.type,lower:pad(range.lower,range.lowerOpen?down.MAX_KEY:down.MIN_KEY,keyTail),lowerOpen:true,upper:pad(range.upper,range.upperOpen?down.MIN_KEY:down.MAX_KEY,keyTail),upperOpen:true}}function translateRequest(req){var index=req.query.index;return index.isVirtual?__assign(__assign({},req),{query:{index:index,range:translateRange(req.query.range,index.keyTail)}}):req}var result=__assign(__assign({},table),{schema:__assign(__assign({},schema),{primaryKey:primaryKey,indexes:allVirtualIndexes,getIndexByKeyPath:findBestIndex}),count:function(req){return table.count(translateRequest(req))},query:function(req){return table.query(translateRequest(req))},openCursor:function(req){var _a=req.query.index,keyTail=_a.keyTail,isVirtual=_a.isVirtual,keyLength=_a.keyLength;if(!isVirtual)return table.openCursor(req);function createVirtualCursor(cursor){function _continue(key){key!=null?cursor.continue(pad(key,req.reverse?down.MAX_KEY:down.MIN_KEY,keyTail)):req.unique?cursor.continue(cursor.key.slice(0,keyLength).concat(req.reverse?down.MIN_KEY:down.MAX_KEY,keyTail)):cursor.continue()}var virtualCursor=Object.create(cursor,{continue:{value:_continue},continuePrimaryKey:{value:function(key,primaryKey){cursor.continuePrimaryKey(pad(key,down.MAX_KEY,keyTail),primaryKey)}},primaryKey:{get:function(){return cursor.primaryKey}},key:{get:function(){var key=cursor.key;return keyLength===1?key[0]:key.slice(0,keyLength)}},value:{get:function(){return cursor.value}}});return virtualCursor}return table.openCursor(translateRequest(req)).then(function(cursor){return cursor&&createVirtualCursor(cursor)})}});return result}})}var virtualIndexMiddleware={stack:"dbcore",name:"VirtualIndexMiddleware",level:1,create:createVirtualIndexMiddleware};function getObjectDiff(a,b,rv,prfx){rv=rv||{};prfx=prfx||"";keys(a).forEach(function(prop){if(!hasOwn(b,prop)){rv[prfx+prop]=undefined}else{var ap=a[prop],bp=b[prop];if(typeof ap==="object"&&typeof bp==="object"&&ap&&bp){var apTypeName=toStringTag(ap);var bpTypeName=toStringTag(bp);if(apTypeName!==bpTypeName){rv[prfx+prop]=b[prop]}else if(apTypeName==="Object"){getObjectDiff(ap,bp,rv,prfx+prop+".")}else if(ap!==bp){rv[prfx+prop]=b[prop]}}else if(ap!==bp)rv[prfx+prop]=b[prop]}});keys(b).forEach(function(prop){if(!hasOwn(a,prop)){rv[prfx+prop]=b[prop]}});return rv}function getEffectiveKeys(primaryKey,req){if(req.type==="delete")return req.keys;return req.keys||req.values.map(primaryKey.extractKey)}var hooksMiddleware={stack:"dbcore",name:"HooksMiddleware",level:2,create:function(downCore){return __assign(__assign({},downCore),{table:function(tableName){var downTable=downCore.table(tableName);var primaryKey=downTable.schema.primaryKey;var tableMiddleware=__assign(__assign({},downTable),{mutate:function(req){var dxTrans=PSD.trans;var _a=dxTrans.table(tableName).hook,deleting=_a.deleting,creating=_a.creating,updating=_a.updating;switch(req.type){case"add":if(creating.fire===nop)break;return dxTrans._promise("readwrite",function(){return addPutOrDelete(req)},true);case"put":if(creating.fire===nop&&updating.fire===nop)break;return dxTrans._promise("readwrite",function(){return addPutOrDelete(req)},true);case"delete":if(deleting.fire===nop)break;return dxTrans._promise("readwrite",function(){return addPutOrDelete(req)},true);case"deleteRange":if(deleting.fire===nop)break;return dxTrans._promise("readwrite",function(){return deleteRange(req)},true)}return downTable.mutate(req);function addPutOrDelete(req){var dxTrans=PSD.trans;var keys=req.keys||getEffectiveKeys(primaryKey,req);if(!keys)throw new Error("Keys missing");req=req.type==="add"||req.type==="put"?__assign(__assign({},req),{keys:keys}):__assign({},req);if(req.type!=="delete")req.values=__spreadArray([],req.values,true);if(req.keys)req.keys=__spreadArray([],req.keys,true);return getExistingValues(downTable,req,keys).then(function(existingValues){var contexts=keys.map(function(key,i){var existingValue=existingValues[i];var ctx={onerror:null,onsuccess:null};if(req.type==="delete"){deleting.fire.call(ctx,key,existingValue,dxTrans)}else if(req.type==="add"||existingValue===undefined){var generatedPrimaryKey=creating.fire.call(ctx,key,req.values[i],dxTrans);if(key==null&&generatedPrimaryKey!=null){key=generatedPrimaryKey;req.keys[i]=key;if(!primaryKey.outbound){setByKeyPath(req.values[i],primaryKey.keyPath,key)}}}else{var objectDiff=getObjectDiff(existingValue,req.values[i]);var additionalChanges_1=updating.fire.call(ctx,objectDiff,key,existingValue,dxTrans);if(additionalChanges_1){var requestedValue_1=req.values[i];Object.keys(additionalChanges_1).forEach(function(keyPath){if(hasOwn(requestedValue_1,keyPath)){requestedValue_1[keyPath]=additionalChanges_1[keyPath]}else{setByKeyPath(requestedValue_1,keyPath,additionalChanges_1[keyPath])}})}}return ctx});return downTable.mutate(req).then(function(_a){var failures=_a.failures,results=_a.results,numFailures=_a.numFailures,lastResult=_a.lastResult;for(var i=0;i0)return Promise.reject(res.failures[0]);if(result.length1?to:fromOrTree}:{d:0})}else{var rv=new RangeSet;if(fromOrTree&&"d"in fromOrTree){extend(rv,fromOrTree)}return rv}};props(RangeSet.prototype,(_a={add:function(rangeSet){mergeRanges(this,rangeSet);return this},addKey:function(key){addRange(this,key,key);return this},addKeys:function(keys){var _this=this;keys.forEach(function(key){return addRange(_this,key,key)});return this}},_a[iteratorSymbol]=function(){return getRangeSetIterator(this)},_a));function addRange(target,from,to){var diff=cmp(from,to);if(isNaN(diff))return;if(diff>0)throw RangeError();if(isEmptyRange(target))return extend(target,{from:from,to:to,d:1});var left=target.l;var right=target.r;if(cmp(to,target.from)<0){left?addRange(left,from,to):target.l={from:from,to:to,d:1,l:null,r:null};return rebalance(target)}if(cmp(from,target.to)>0){right?addRange(right,from,to):target.r={from:from,to:to,d:1,l:null,r:null};return rebalance(target)}if(cmp(from,target.from)<0){target.from=from;target.l=null;target.d=right?right.d+1:1}if(cmp(to,target.to)>0){target.to=to;target.r=null;target.d=target.l?target.l.d+1:1}var rightWasCutOff=!target.r;if(left&&!target.l){mergeRanges(target,left)}if(right&&rightWasCutOff){mergeRanges(target,right)}}function mergeRanges(target,newSet){function _addRangeSet(target,_a){var from=_a.from,to=_a.to,l=_a.l,r=_a.r;addRange(target,from,to);if(l)_addRangeSet(target,l);if(r)_addRangeSet(target,r)}if(!isEmptyRange(newSet))_addRangeSet(target,newSet)}function rangesOverlap(rangeSet1,rangeSet2){var i1=getRangeSetIterator(rangeSet2);var nextResult1=i1.next();if(nextResult1.done)return false;var a=nextResult1.value;var i2=getRangeSetIterator(rangeSet1);var nextResult2=i2.next(a.from);var b=nextResult2.value;while(!nextResult1.done&&!nextResult2.done){if(cmp(b.from,a.to)<=0&&cmp(b.to,a.from)>=0)return true;cmp(a.from,b.from)<0?a=(nextResult1=i1.next(b.from)).value:b=(nextResult2=i2.next(a.from)).value}return false}function getRangeSetIterator(node){var state=isEmptyRange(node)?null:{s:0,n:node};return{next:function(key){var keyProvided=arguments.length>0;while(state){switch(state.s){case 0:state.s=1;if(keyProvided){while(state.n.l&&cmp(key,state.n.from)<0)state={up:state,n:state.n.l,s:1}}else{while(state.n.l)state={up:state,n:state.n.l,s:1}}case 1:state.s=2;if(!keyProvided||cmp(key,state.n.to)<=0)return{value:state.n,done:false};case 2:if(state.n.r){state.s=3;state={up:state,n:state.n.r,s:0};continue}case 3:state=state.up}}return{done:true}}}}function rebalance(target){var _a,_b;var diff=(((_a=target.r)===null||_a===void 0?void 0:_a.d)||0)-(((_b=target.l)===null||_b===void 0?void 0:_b.d)||0);var r=diff>1?"r":diff<-1?"l":"";if(r){var l=r==="r"?"l":"r";var rootClone=__assign({},target);var oldRootRight=target[r];target.from=oldRootRight.from;target.to=oldRootRight.to;target[r]=oldRootRight[r];rootClone[r]=oldRootRight[l];target[l]=rootClone;rootClone.d=computeDepth(rootClone)}target.d=computeDepth(target)}function computeDepth(_a){var r=_a.r,l=_a.l;return(r?l?Math.max(r.d,l.d):r.d:l?l.d:0)+1}var observabilityMiddleware={stack:"dbcore",level:0,create:function(core){var dbName=core.schema.name;var FULL_RANGE=new RangeSet(core.MIN_KEY,core.MAX_KEY);return __assign(__assign({},core),{table:function(tableName){var table=core.table(tableName);var schema=table.schema;var primaryKey=schema.primaryKey;var extractKey=primaryKey.extractKey,outbound=primaryKey.outbound;var tableClone=__assign(__assign({},table),{mutate:function(req){var trans=req.trans;var mutatedParts=trans.mutatedParts||(trans.mutatedParts={});var getRangeSet=function(indexName){var part="idb://"+dbName+"/"+tableName+"/"+indexName;return mutatedParts[part]||(mutatedParts[part]=new RangeSet)};var pkRangeSet=getRangeSet("");var delsRangeSet=getRangeSet(":dels");var type=req.type;var _a=req.type==="deleteRange"?[req.range]:req.type==="delete"?[req.keys]:req.values.length<50?[[],req.values]:[],keys=_a[0],newObjs=_a[1];var oldCache=req.trans["_cache"];return table.mutate(req).then(function(res){if(isArray(keys)){if(type!=="delete")keys=res.results;pkRangeSet.addKeys(keys);var oldObjs=getFromTransactionCache(keys,oldCache);if(!oldObjs&&type!=="add"){delsRangeSet.addKeys(keys)}if(oldObjs||newObjs){trackAffectedIndexes(getRangeSet,schema,oldObjs,newObjs)}}else if(keys){var range={from:keys.lower,to:keys.upper};delsRangeSet.add(range);pkRangeSet.add(range)}else{pkRangeSet.add(FULL_RANGE);delsRangeSet.add(FULL_RANGE);schema.indexes.forEach(function(idx){return getRangeSet(idx.name).add(FULL_RANGE)})}return res})}});var getRange=function(_a){var _b,_c;var _d=_a.query,index=_d.index,range=_d.range;return[index,new RangeSet((_b=range.lower)!==null&&_b!==void 0?_b:core.MIN_KEY,(_c=range.upper)!==null&&_c!==void 0?_c:core.MAX_KEY)]};var readSubscribers={get:function(req){return[primaryKey,new RangeSet(req.key)]},getMany:function(req){return[primaryKey,(new RangeSet).addKeys(req.keys)]},count:getRange,query:getRange,openCursor:getRange};keys(readSubscribers).forEach(function(method){tableClone[method]=function(req){var subscr=PSD.subscr;if(subscr){var getRangeSet=function(indexName){var part="idb://"+dbName+"/"+tableName+"/"+indexName;return subscr[part]||(subscr[part]=new RangeSet)};var pkRangeSet_1=getRangeSet("");var delsRangeSet_1=getRangeSet(":dels");var _a=readSubscribers[method](req),queriedIndex=_a[0],queriedRanges=_a[1];getRangeSet(queriedIndex.name||"").add(queriedRanges);if(!queriedIndex.isPrimaryKey){if(method==="count"){delsRangeSet_1.add(FULL_RANGE)}else{var keysPromise_1=method==="query"&&outbound&&req.values&&table.query(__assign(__assign({},req),{values:false}));return table[method].apply(this,arguments).then(function(res){if(method==="query"){if(outbound&&req.values){return keysPromise_1.then(function(_a){var resultingKeys=_a.result;pkRangeSet_1.addKeys(resultingKeys);return res})}var pKeys=req.values?res.result.map(extractKey):res.result;if(req.values){pkRangeSet_1.addKeys(pKeys)}else{delsRangeSet_1.addKeys(pKeys)}}else if(method==="openCursor"){var cursor_1=res;var wantValues_1=req.values;return cursor_1&&Object.create(cursor_1,{key:{get:function(){delsRangeSet_1.addKey(cursor_1.primaryKey);return cursor_1.key}},primaryKey:{get:function(){var pkey=cursor_1.primaryKey;delsRangeSet_1.addKey(pkey);return pkey}},value:{get:function(){wantValues_1&&pkRangeSet_1.addKey(cursor_1.primaryKey);return cursor_1.value}}})}return res})}}}return table[method].apply(this,arguments)}});return tableClone}})}};function trackAffectedIndexes(getRangeSet,schema,oldObjs,newObjs){function addAffectedIndex(ix){var rangeSet=getRangeSet(ix.name||"");function extractKey(obj){return obj!=null?ix.extractKey(obj):null}var addKeyOrKeys=function(key){return ix.multiEntry&&isArray(key)?key.forEach(function(key){return rangeSet.addKey(key)}):rangeSet.addKey(key)};(oldObjs||newObjs).forEach(function(_,i){var oldKey=oldObjs&&extractKey(oldObjs[i]);var newKey=newObjs&&extractKey(newObjs[i]);if(cmp(oldKey,newKey)!==0){if(oldKey!=null)addKeyOrKeys(oldKey);if(newKey!=null)addKeyOrKeys(newKey)}})}schema.indexes.forEach(addAffectedIndex)}var Dexie$1=function(){function Dexie(name,options){var _this=this;this._middlewares={};this.verno=0;var deps=Dexie.dependencies;this._options=options=__assign({addons:Dexie.addons,autoOpen:true,indexedDB:deps.indexedDB,IDBKeyRange:deps.IDBKeyRange},options);this._deps={indexedDB:options.indexedDB,IDBKeyRange:options.IDBKeyRange};var addons=options.addons;this._dbSchema={};this._versions=[];this._storeNames=[];this._allTables={};this.idbdb=null;this._novip=this;var state={dbOpenError:null,isBeingOpened:false,onReadyBeingFired:null,openComplete:false,dbReadyResolve:nop,dbReadyPromise:null,cancelOpen:nop,openCanceller:null,autoSchema:true,PR1398_maxLoop:3};state.dbReadyPromise=new DexiePromise(function(resolve){state.dbReadyResolve=resolve});state.openCanceller=new DexiePromise(function(_,reject){state.cancelOpen=reject});this._state=state;this.name=name;this.on=Events(this,"populate","blocked","versionchange","close",{ready:[promisableChain,nop]});this.on.ready.subscribe=override(this.on.ready.subscribe,function(subscribe){return function(subscriber,bSticky){Dexie.vip(function(){var state=_this._state;if(state.openComplete){if(!state.dbOpenError)DexiePromise.resolve().then(subscriber);if(bSticky)subscribe(subscriber)}else if(state.onReadyBeingFired){state.onReadyBeingFired.push(subscriber);if(bSticky)subscribe(subscriber)}else{subscribe(subscriber);var db_1=_this;if(!bSticky)subscribe(function unsubscribe(){db_1.on.ready.unsubscribe(subscriber);db_1.on.ready.unsubscribe(unsubscribe)})}})}});this.Collection=createCollectionConstructor(this);this.Table=createTableConstructor(this);this.Transaction=createTransactionConstructor(this);this.Version=createVersionConstructor(this);this.WhereClause=createWhereClauseConstructor(this);this.on("versionchange",function(ev){if(ev.newVersion>0)console.warn("Another connection wants to upgrade database '"+_this.name+"'. Closing db now to resume the upgrade.");else console.warn("Another connection wants to delete database '"+_this.name+"'. Closing db now to resume the delete request.");_this.close()});this.on("blocked",function(ev){if(!ev.newVersion||ev.newVersion=0)connections.splice(idx,1);if(this.idbdb){try{this.idbdb.close()}catch(e){}this._novip.idbdb=null}state.dbReadyPromise=new DexiePromise(function(resolve){state.dbReadyResolve=resolve});state.openCanceller=new DexiePromise(function(_,reject){state.cancelOpen=reject})};Dexie.prototype.close=function(){this._close();var state=this._state;this._options.autoOpen=false;state.dbOpenError=new exceptions.DatabaseClosed;if(state.isBeingOpened)state.cancelOpen(state.dbOpenError)};Dexie.prototype.delete=function(){var _this=this;var hasArguments=arguments.length>0;var state=this._state;return new DexiePromise(function(resolve,reject){var doDelete=function(){_this.close();var req=_this._deps.indexedDB.deleteDatabase(_this.name);req.onsuccess=wrap(function(){_onDatabaseDeleted(_this._deps,_this.name);resolve()});req.onerror=eventRejectHandler(reject);req.onblocked=_this._fireOnBlocked};if(hasArguments)throw new exceptions.InvalidArgument("Arguments not allowed in db.delete()");if(state.isBeingOpened){state.dbReadyPromise.then(doDelete)}else{doDelete()}})};Dexie.prototype.backendDB=function(){return this.idbdb};Dexie.prototype.isOpen=function(){return this.idbdb!==null};Dexie.prototype.hasBeenClosed=function(){var dbOpenError=this._state.dbOpenError;return dbOpenError&&dbOpenError.name==="DatabaseClosed"};Dexie.prototype.hasFailed=function(){return this._state.dbOpenError!==null};Dexie.prototype.dynamicallyOpened=function(){return this._state.autoSchema};Object.defineProperty(Dexie.prototype,"tables",{get:function(){var _this=this;return keys(this._allTables).map(function(name){return _this._allTables[name]})},enumerable:false,configurable:true});Dexie.prototype.transaction=function(){var args=extractTransactionArgs.apply(this,arguments);return this._transaction.apply(this,args)};Dexie.prototype._transaction=function(mode,tables,scopeFunc){var _this=this;var parentTransaction=PSD.trans;if(!parentTransaction||parentTransaction.db!==this||mode.indexOf("!")!==-1)parentTransaction=null;var onlyIfCompatible=mode.indexOf("?")!==-1;mode=mode.replace("!","").replace("?","");var idbMode,storeNames;try{storeNames=tables.map(function(table){var storeName=table instanceof _this.Table?table.name:table;if(typeof storeName!=="string")throw new TypeError("Invalid table argument to Dexie.transaction(). Only Table or String are allowed");return storeName});if(mode=="r"||mode===READONLY)idbMode=READONLY;else if(mode=="rw"||mode==READWRITE)idbMode=READWRITE;else throw new exceptions.InvalidArgument("Invalid transaction mode: "+mode);if(parentTransaction){if(parentTransaction.mode===READONLY&&idbMode===READWRITE){if(onlyIfCompatible){parentTransaction=null}else throw new exceptions.SubTransaction("Cannot enter a sub-transaction with READWRITE mode when parent transaction is READONLY")}if(parentTransaction){storeNames.forEach(function(storeName){if(parentTransaction&&parentTransaction.storeNames.indexOf(storeName)===-1){if(onlyIfCompatible){parentTransaction=null}else throw new exceptions.SubTransaction("Table "+storeName+" not included in parent transaction.")}})}if(onlyIfCompatible&&parentTransaction&&!parentTransaction.active){parentTransaction=null}}}catch(e){return parentTransaction?parentTransaction._promise(null,function(_,reject){reject(e)}):rejection(e)}var enterTransaction=enterTransactionScope.bind(null,this,idbMode,storeNames,parentTransaction,scopeFunc);return parentTransaction?parentTransaction._promise(idbMode,enterTransaction,"lock"):PSD.trans?usePSD(PSD.transless,function(){return _this._whenReady(enterTransaction)}):this._whenReady(enterTransaction)};Dexie.prototype.table=function(tableName){if(!hasOwn(this._allTables,tableName)){throw new exceptions.InvalidTable("Table "+tableName+" does not exist")}return this._allTables[tableName]};return Dexie}();var symbolObservable=typeof Symbol!=="undefined"&&"observable"in Symbol?Symbol.observable:"@@observable";var Observable=function(){function Observable(subscribe){this._subscribe=subscribe}Observable.prototype.subscribe=function(x,error,complete){return this._subscribe(!x||typeof x==="function"?{next:x,error:error,complete:complete}:x)};Observable.prototype[symbolObservable]=function(){return this};return Observable}();function extendObservabilitySet(target,newSet){keys(newSet).forEach(function(part){var rangeSet=target[part]||(target[part]=new RangeSet);mergeRanges(rangeSet,newSet[part])});return target}function liveQuery(querier){var hasValue=false;var currentValue=undefined;var observable=new Observable(function(observer){var scopeFuncIsAsync=isAsyncFunction(querier);function execute(subscr){if(scopeFuncIsAsync){incrementExpectedAwaits()}var exec=function(){return newScope(querier,{subscr:subscr,trans:null})};var rv=PSD.trans?usePSD(PSD.transless,exec):exec();if(scopeFuncIsAsync){rv.then(decrementExpectedAwaits,decrementExpectedAwaits)}return rv}var closed=false;var accumMuts={};var currentObs={};var subscription={get closed(){return closed},unsubscribe:function(){closed=true;globalEvents.storagemutated.unsubscribe(mutationListener)}};observer.start&&observer.start(subscription);var querying=false,startedListening=false;function shouldNotify(){return keys(currentObs).some(function(key){return accumMuts[key]&&rangesOverlap(accumMuts[key],currentObs[key])})}var mutationListener=function(parts){extendObservabilitySet(accumMuts,parts);if(shouldNotify()){doQuery()}};var doQuery=function(){if(querying||closed)return;accumMuts={};var subscr={};var ret=execute(subscr);if(!startedListening){globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME,mutationListener);startedListening=true}querying=true;Promise.resolve(ret).then(function(result){hasValue=true;currentValue=result;querying=false;if(closed)return;if(shouldNotify()){doQuery()}else{accumMuts={};currentObs=subscr;observer.next&&observer.next(result)}},function(err){querying=false;hasValue=false;observer.error&&observer.error(err);subscription.unsubscribe()})};doQuery();return subscription});observable.hasValue=function(){return hasValue};observable.getValue=function(){return currentValue};return observable}var domDeps;try{domDeps={indexedDB:_global.indexedDB||_global.mozIndexedDB||_global.webkitIndexedDB||_global.msIndexedDB,IDBKeyRange:_global.IDBKeyRange||_global.webkitIDBKeyRange}}catch(e){domDeps={indexedDB:null,IDBKeyRange:null}}var Dexie=Dexie$1;props(Dexie,__assign(__assign({},fullNameExceptions),{delete:function(databaseName){var db=new Dexie(databaseName,{addons:[]});return db.delete()},exists:function(name){return new Dexie(name,{addons:[]}).open().then(function(db){db.close();return true}).catch("NoSuchDatabaseError",function(){return false})},getDatabaseNames:function(cb){try{return getDatabaseNames(Dexie.dependencies).then(cb)}catch(_a){return rejection(new exceptions.MissingAPI)}},defineClass:function(){function Class(content){extend(this,content)}return Class},ignoreTransaction:function(scopeFunc){return PSD.trans?usePSD(PSD.transless,scopeFunc):scopeFunc()},vip:vip,async:function(generatorFn){return function(){try{var rv=awaitIterator(generatorFn.apply(this,arguments));if(!rv||typeof rv.then!=="function")return DexiePromise.resolve(rv);return rv}catch(e){return rejection(e)}}},spawn:function(generatorFn,args,thiz){try{var rv=awaitIterator(generatorFn.apply(thiz,args||[]));if(!rv||typeof rv.then!=="function")return DexiePromise.resolve(rv);return rv}catch(e){return rejection(e)}},currentTransaction:{get:function(){return PSD.trans||null}},waitFor:function(promiseOrFunction,optionalTimeout){var promise=DexiePromise.resolve(typeof promiseOrFunction==="function"?Dexie.ignoreTransaction(promiseOrFunction):promiseOrFunction).timeout(optionalTimeout||6e4);return PSD.trans?PSD.trans.waitFor(promise):promise},Promise:DexiePromise,debug:{get:function(){return debug},set:function(value){setDebug(value,value==="dexie"?function(){return true}:dexieStackFrameFilter)}},derive:derive,extend:extend,props:props,override:override,Events:Events,on:globalEvents,liveQuery:liveQuery,extendObservabilitySet:extendObservabilitySet,getByKeyPath:getByKeyPath,setByKeyPath:setByKeyPath,delByKeyPath:delByKeyPath,shallowClone:shallowClone,deepClone:deepClone,getObjectDiff:getObjectDiff,cmp:cmp,asap:asap$1,minKey:minKey,addons:[],connections:connections,errnames:errnames,dependencies:domDeps,semVer:DEXIE_VERSION,version:DEXIE_VERSION.split(".").map(function(n){return parseInt(n)}).reduce(function(p,c,i){return p+c/Math.pow(10,i*2)})}));Dexie.maxKey=getMaxKey(Dexie.dependencies.IDBKeyRange);if(typeof dispatchEvent!=="undefined"&&typeof addEventListener!=="undefined"){globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME,function(updatedParts){if(!propagatingLocally){var event_1;if(isIEOrEdge){event_1=document.createEvent("CustomEvent");event_1.initCustomEvent(STORAGE_MUTATED_DOM_EVENT_NAME,true,true,updatedParts)}else{event_1=new CustomEvent(STORAGE_MUTATED_DOM_EVENT_NAME,{detail:updatedParts})}propagatingLocally=true;dispatchEvent(event_1);propagatingLocally=false}});addEventListener(STORAGE_MUTATED_DOM_EVENT_NAME,function(_a){var detail=_a.detail;if(!propagatingLocally){propagateLocally(detail)}})}function propagateLocally(updateParts){var wasMe=propagatingLocally;try{propagatingLocally=true;globalEvents.storagemutated.fire(updateParts)}finally{propagatingLocally=wasMe}}var propagatingLocally=false;if(typeof BroadcastChannel!=="undefined"){var bc_1=new BroadcastChannel(STORAGE_MUTATED_DOM_EVENT_NAME);if(typeof bc_1.unref==="function"){bc_1.unref()}globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME,function(changedParts){if(!propagatingLocally){bc_1.postMessage(changedParts)}});bc_1.onmessage=function(ev){if(ev.data)propagateLocally(ev.data)}}else if(typeof self!=="undefined"&&typeof navigator!=="undefined"){globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME,function(changedParts){try{if(!propagatingLocally){if(typeof localStorage!=="undefined"){localStorage.setItem(STORAGE_MUTATED_DOM_EVENT_NAME,JSON.stringify({trig:Math.random(),changedParts:changedParts}))}if(typeof self["clients"]==="object"){__spreadArray([],self["clients"].matchAll({includeUncontrolled:true}),true).forEach(function(client){return client.postMessage({type:STORAGE_MUTATED_DOM_EVENT_NAME,changedParts:changedParts})})}}}catch(_a){}});if(typeof addEventListener!=="undefined"){addEventListener("storage",function(ev){if(ev.key===STORAGE_MUTATED_DOM_EVENT_NAME){var data=JSON.parse(ev.newValue);if(data)propagateLocally(data.changedParts)}})}var swContainer=self.document&&navigator.serviceWorker;if(swContainer){swContainer.addEventListener("message",propagateMessageLocally)}}function propagateMessageLocally(_a){var data=_a.data;if(data&&data.type===STORAGE_MUTATED_DOM_EVENT_NAME){propagateLocally(data.changedParts)}}DexiePromise.rejectionMapper=mapError;setDebug(debug,dexieStackFrameFilter);var namedExports=Object.freeze({__proto__:null,Dexie:Dexie$1,liveQuery:liveQuery,default:Dexie$1,RangeSet:RangeSet,mergeRanges:mergeRanges,rangesOverlap:rangesOverlap});__assign(Dexie$1,namedExports,{default:Dexie$1});return Dexie$1});(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports,require("dexie")):typeof define==="function"&&define.amd?define(["exports","dexie"],factory):(global=typeof globalThis!=="undefined"?globalThis:global||self,factory(global.DexieExportImport={},global.Dexie))})(this,function(exports,Dexie){"use strict";function _interopDefaultLegacy(e){return e&&typeof e==="object"&&"default"in e?e:{default:e}}var Dexie__default=_interopDefaultLegacy(Dexie);function __awaiter(thisArg,_arguments,P,generator){function adopt(value){return value instanceof P?value:new P(function(resolve){resolve(value)})}return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):adopt(result.value).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})}function __generator(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},f,y,t,g;return g={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this}),g;function verb(n){return function(v){return step([n,v])}}function step(op){if(f)throw new TypeError("Generator is already executing.");while(_)try{if(f=1,y&&(t=op[0]&2?y["return"]:op[0]?y["throw"]||((t=y["return"])&&t.call(y),0):y.next)&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[op[0]&2,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]-1){var innerObj=obj[unescapeKeyPathComponent(keyPath.slice(0,period))];return innerObj===undefined?undefined:getByKeyPath(innerObj,keyPath.slice(period+1))}return obj[unescapeKeyPathComponent(keyPath)]}function setAtKeyPath(obj,keyPath,value){if(keyPath===""){return value}var period=keyPath.indexOf(".");if(period>-1){var innerObj=obj[unescapeKeyPathComponent(keyPath.slice(0,period))];return setAtKeyPath(innerObj,keyPath.slice(period+1),value)}obj[unescapeKeyPathComponent(keyPath)]=value;return obj}function getJSONType(value){return value===null?"null":Array.isArray(value)?"array":_typeof(value)}var keys=Object.keys,isArray=Array.isArray,hasOwn$1={}.hasOwnProperty,internalStateObjPropsToIgnore=["type","replaced","iterateIn","iterateUnsetNumeric"];function nestedPathsFirst(a,b){if(a.keypath===""){return-1}var as=a.keypath.match(/\./g)||0;var bs=b.keypath.match(/\./g)||0;if(as){as=as.length}if(bs){bs=bs.length}return as>bs?-1:asb.keypath}var Typeson=function(){function Typeson(options){_classCallCheck(this,Typeson);this.options=options;this.plainObjectReplacers=[];this.nonplainObjectReplacers=[];this.revivers={};this.types={}}_createClass(Typeson,[{key:"stringify",value:function stringify(obj,replacer,space,opts){opts=_objectSpread2({},this.options,{},opts,{stringification:true});var encapsulated=this.encapsulate(obj,null,opts);if(isArray(encapsulated)){return JSON.stringify(encapsulated[0],replacer,space)}return encapsulated.then(function(res){return JSON.stringify(res,replacer,space)})}},{key:"stringifySync",value:function stringifySync(obj,replacer,space,opts){return this.stringify(obj,replacer,space,_objectSpread2({throwOnBadSyncType:true},opts,{sync:true}))}},{key:"stringifyAsync",value:function stringifyAsync(obj,replacer,space,opts){return this.stringify(obj,replacer,space,_objectSpread2({throwOnBadSyncType:true},opts,{sync:false}))}},{key:"parse",value:function parse(text,reviver,opts){opts=_objectSpread2({},this.options,{},opts,{parse:true});return this.revive(JSON.parse(text,reviver),opts)}},{key:"parseSync",value:function parseSync(text,reviver,opts){return this.parse(text,reviver,_objectSpread2({throwOnBadSyncType:true},opts,{sync:true}))}},{key:"parseAsync",value:function parseAsync(text,reviver,opts){return this.parse(text,reviver,_objectSpread2({throwOnBadSyncType:true},opts,{sync:false}))}},{key:"specialTypeNames",value:function specialTypeNames(obj,stateObj){var opts=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};opts.returnTypeNames=true;return this.encapsulate(obj,stateObj,opts)}},{key:"rootTypeName",value:function rootTypeName(obj,stateObj){var opts=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};opts.iterateNone=true;return this.encapsulate(obj,stateObj,opts)}},{key:"encapsulate",value:function encapsulate(obj,stateObj,opts){opts=_objectSpread2({sync:true},this.options,{},opts);var _opts=opts,sync=_opts.sync;var that=this,types={},refObjs=[],refKeys=[],promisesDataRoot=[];var cyclic="cyclic"in opts?opts.cyclic:true;var _opts2=opts,encapsulateObserver=_opts2.encapsulateObserver;var ret=_encapsulate("",obj,cyclic,stateObj||{},promisesDataRoot);function finish(ret){var typeNames=Object.values(types);if(opts.iterateNone){if(typeNames.length){return typeNames[0]}return Typeson.getJSONType(ret)}if(typeNames.length){if(opts.returnTypeNames){return _toConsumableArray(new Set(typeNames))}if(!ret||!isPlainObject(ret)||hasOwn$1.call(ret,"$types")){ret={$:ret,$types:{$:types}}}else{ret.$types=types}}else if(isObject(ret)&&hasOwn$1.call(ret,"$types")){ret={$:ret,$types:true}}if(opts.returnTypeNames){return false}return ret}function checkPromises(_x,_x2){return _checkPromises.apply(this,arguments)}function _checkPromises(){_checkPromises=_asyncToGenerator(regeneratorRuntime.mark(function _callee2(ret,promisesData){var promResults;return regeneratorRuntime.wrap(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:_context2.next=2;return Promise.all(promisesData.map(function(pd){return pd[1].p}));case 2:promResults=_context2.sent;_context2.next=5;return Promise.all(promResults.map(function(){var _ref=_asyncToGenerator(regeneratorRuntime.mark(function _callee(promResult){var newPromisesData,_promisesData$splice,_promisesData$splice2,prData,_prData,keyPath,cyclic,stateObj,parentObj,key,detectedType,encaps,isTypesonPromise,encaps2;return regeneratorRuntime.wrap(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:newPromisesData=[];_promisesData$splice=promisesData.splice(0,1),_promisesData$splice2=_slicedToArray(_promisesData$splice,1),prData=_promisesData$splice2[0];_prData=_slicedToArray(prData,7),keyPath=_prData[0],cyclic=_prData[2],stateObj=_prData[3],parentObj=_prData[4],key=_prData[5],detectedType=_prData[6];encaps=_encapsulate(keyPath,promResult,cyclic,stateObj,newPromisesData,true,detectedType);isTypesonPromise=hasConstructorOf(encaps,TypesonPromise);if(!(keyPath&&isTypesonPromise)){_context.next=11;break}_context.next=8;return encaps.p;case 8:encaps2=_context.sent;parentObj[key]=encaps2;return _context.abrupt("return",checkPromises(ret,newPromisesData));case 11:if(keyPath){parentObj[key]=encaps}else if(isTypesonPromise){ret=encaps.p}else{ret=encaps}return _context.abrupt("return",checkPromises(ret,newPromisesData));case 13:case"end":return _context.stop()}}},_callee)}));return function(_x3){return _ref.apply(this,arguments)}}()));case 5:return _context2.abrupt("return",ret);case 6:case"end":return _context2.stop()}}},_callee2)}));return _checkPromises.apply(this,arguments)}function _adaptBuiltinStateObjectProperties(stateObj,ownKeysObj,cb){Object.assign(stateObj,ownKeysObj);var vals=internalStateObjPropsToIgnore.map(function(prop){var tmp=stateObj[prop];delete stateObj[prop];return tmp});cb();internalStateObjPropsToIgnore.forEach(function(prop,i){stateObj[prop]=vals[i]})}function _encapsulate(keypath,value,cyclic,stateObj,promisesData,resolvingTypesonPromise,detectedType){var ret;var observerData={};var $typeof=_typeof(value);var runObserver=encapsulateObserver?function(obj){var type=detectedType||stateObj.type||Typeson.getJSONType(value);encapsulateObserver(Object.assign(obj||observerData,{keypath:keypath,value:value,cyclic:cyclic,stateObj:stateObj,promisesData:promisesData,resolvingTypesonPromise:resolvingTypesonPromise,awaitingTypesonPromise:hasConstructorOf(value,TypesonPromise)},{type:type}))}:null;if(["string","boolean","number","undefined"].includes($typeof)){if(value===undefined||$typeof==="number"&&(isNaN(value)||value===-Infinity||value===Infinity)){if(stateObj.replaced){ret=value}else{ret=replace(keypath,value,stateObj,promisesData,false,resolvingTypesonPromise,runObserver)}if(ret!==value){observerData={replaced:ret}}}else{ret=value}if(runObserver){runObserver()}return ret}if(value===null){if(runObserver){runObserver()}return value}if(cyclic&&!stateObj.iterateIn&&!stateObj.iterateUnsetNumeric&&value&&_typeof(value)==="object"){var refIndex=refObjs.indexOf(value);if(refIndex<0){if(cyclic===true){refObjs.push(value);refKeys.push(keypath)}}else{types[keypath]="#";if(runObserver){runObserver({cyclicKeypath:refKeys[refIndex]})}return"#"+refKeys[refIndex]}}var isPlainObj=isPlainObject(value);var isArr=isArray(value);var replaced=(isPlainObj||isArr)&&(!that.plainObjectReplacers.length||stateObj.replaced)||stateObj.iterateIn?value:replace(keypath,value,stateObj,promisesData,isPlainObj||isArr,null,runObserver);var clone;if(replaced!==value){ret=replaced;observerData={replaced:replaced}}else{if(keypath===""&&hasConstructorOf(value,TypesonPromise)){promisesData.push([keypath,value,cyclic,stateObj,undefined,undefined,stateObj.type]);ret=value}else if(isArr&&stateObj.iterateIn!=="object"||stateObj.iterateIn==="array"){clone=new Array(value.length);observerData={clone:clone}}else if(!["function","symbol"].includes(_typeof(value))&&!("toJSON"in value)&&!hasConstructorOf(value,TypesonPromise)&&!hasConstructorOf(value,Promise)&&!hasConstructorOf(value,ArrayBuffer)||isPlainObj||stateObj.iterateIn==="object"){clone={};if(stateObj.addLength){clone.length=value.length}observerData={clone:clone}}else{ret=value}}if(runObserver){runObserver()}if(opts.iterateNone){return clone||ret}if(!clone){return ret}if(stateObj.iterateIn){var _loop=function _loop(key){var ownKeysObj={ownKeys:hasOwn$1.call(value,key)};_adaptBuiltinStateObjectProperties(stateObj,ownKeysObj,function(){var kp=keypath+(keypath?".":"")+escapeKeyPathComponent(key);var val=_encapsulate(kp,value[key],Boolean(cyclic),stateObj,promisesData,resolvingTypesonPromise);if(hasConstructorOf(val,TypesonPromise)){promisesData.push([kp,val,Boolean(cyclic),stateObj,clone,key,stateObj.type])}else if(val!==undefined){clone[key]=val}})};for(var key in value){_loop(key)}if(runObserver){runObserver({endIterateIn:true,end:true})}}else{keys(value).forEach(function(key){var kp=keypath+(keypath?".":"")+escapeKeyPathComponent(key);var ownKeysObj={ownKeys:true};_adaptBuiltinStateObjectProperties(stateObj,ownKeysObj,function(){var val=_encapsulate(kp,value[key],Boolean(cyclic),stateObj,promisesData,resolvingTypesonPromise);if(hasConstructorOf(val,TypesonPromise)){promisesData.push([kp,val,Boolean(cyclic),stateObj,clone,key,stateObj.type])}else if(val!==undefined){clone[key]=val}})});if(runObserver){runObserver({endIterateOwn:true,end:true})}}if(stateObj.iterateUnsetNumeric){var vl=value.length;var _loop2=function _loop2(i){if(!(i in value)){var kp=keypath+(keypath?".":"")+i;var ownKeysObj={ownKeys:false};_adaptBuiltinStateObjectProperties(stateObj,ownKeysObj,function(){var val=_encapsulate(kp,undefined,Boolean(cyclic),stateObj,promisesData,resolvingTypesonPromise);if(hasConstructorOf(val,TypesonPromise)){promisesData.push([kp,val,Boolean(cyclic),stateObj,clone,i,stateObj.type])}else if(val!==undefined){clone[i]=val}})}};for(var i=0;ie.length)&&(t=e.length);for(var r=0,n=new Array(t);re.length)&&(t=e.length);for(var r=0,n=new Array(t);r-1){var n=e[unescapeKeyPathComponent(t.slice(0,r))];return void 0===n?void 0:getByKeyPath(n,t.slice(r+1))}return e[unescapeKeyPathComponent(t)]}function setAtKeyPath(e,t,r){if(""===t)return r;var n=t.indexOf(".");return n>-1?setAtKeyPath(e[unescapeKeyPathComponent(t.slice(0,n))],t.slice(n+1),r):(e[unescapeKeyPathComponent(t)]=r,e)}function _await(e,t,r){return r?t?t(e):e:(e&&e.then||(e=Promise.resolve(e)),t?e.then(t):e)}var o=Object.keys,a=Array.isArray,c={}.hasOwnProperty,u=["type","replaced","iterateIn","iterateUnsetNumeric"];function _async(e){return function(){for(var t=[],r=0;rn?-1:rt.keypath}var s=function(){function Typeson(e){_classCallCheck(this,Typeson),this.options=e,this.plainObjectReplacers=[],this.nonplainObjectReplacers=[],this.revivers={},this.types={}}return function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),e}(Typeson,[{key:"stringify",value:function stringify(e,t,r,n){n=_objectSpread2(_objectSpread2(_objectSpread2({},this.options),n),{},{stringification:!0});var i=this.encapsulate(e,null,n);return a(i)?JSON.stringify(i[0],t,r):i.then(function(e){return JSON.stringify(e,t,r)})}},{key:"stringifySync",value:function stringifySync(e,t,r,n){return this.stringify(e,t,r,_objectSpread2(_objectSpread2({throwOnBadSyncType:!0},n),{},{sync:!0}))}},{key:"stringifyAsync",value:function stringifyAsync(e,t,r,n){return this.stringify(e,t,r,_objectSpread2(_objectSpread2({throwOnBadSyncType:!0},n),{},{sync:!1}))}},{key:"parse",value:function parse(e,t,r){return r=_objectSpread2(_objectSpread2(_objectSpread2({},this.options),r),{},{parse:!0}),this.revive(JSON.parse(e,t),r)}},{key:"parseSync",value:function parseSync(e,t,r){return this.parse(e,t,_objectSpread2(_objectSpread2({throwOnBadSyncType:!0},r),{},{sync:!0}))}},{key:"parseAsync",value:function parseAsync(e,t,r){return this.parse(e,t,_objectSpread2(_objectSpread2({throwOnBadSyncType:!0},r),{},{sync:!1}))}},{key:"specialTypeNames",value:function specialTypeNames(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return r.returnTypeNames=!0,this.encapsulate(e,t,r)}},{key:"rootTypeName",value:function rootTypeName(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return r.iterateNone=!0,this.encapsulate(e,t,r)}},{key:"encapsulate",value:function encapsulate(t,r,n){var i=_async(function(t,r){return _await(Promise.all(r.map(function(e){return e[1].p})),function(n){return _await(Promise.all(n.map(_async(function(n){var o=!1,a=[],c=_slicedToArray(r.splice(0,1),1),u=_slicedToArray(c[0],7),s=u[0],f=u[2],l=u[3],p=u[4],y=u[5],v=u[6],b=_encapsulate(s,n,f,l,a,!0,v),d=hasConstructorOf(b,e);return function _invoke(e,t){var r=e();return r&&r.then?r.then(t):t(r)}(function(){if(s&&d)return _await(b.p,function(e){return p[y]=e,o=!0,i(t,a)})},function(e){return o?e:(s?p[y]=b:t=d?b.p:b,i(t,a))})}))),function(){return t})})}),s=(n=_objectSpread2(_objectSpread2({sync:!0},this.options),n)).sync,f=this,l={},p=[],y=[],v=[],b=!("cyclic"in n)||n.cyclic,d=n.encapsulateObserver,h=_encapsulate("",t,b,r||{},v);function finish(e){var t=Object.values(l);if(n.iterateNone)return t.length?t[0]:Typeson.getJSONType(e);if(t.length){if(n.returnTypeNames)return _toConsumableArray(new Set(t));e&&isPlainObject(e)&&!c.call(e,"$types")?e.$types=l:e={$:e,$types:{$:l}}}else isObject(e)&&c.call(e,"$types")&&(e={$:e,$types:!0});return!n.returnTypeNames&&e}function _adaptBuiltinStateObjectProperties(e,t,r){Object.assign(e,t);var n=u.map(function(t){var r=e[t];return delete e[t],r});r(),u.forEach(function(t,r){e[t]=n[r]})}function _encapsulate(t,r,i,u,s,v,b){var h,g={},m=_typeof(r),O=d?function(n){var o=b||u.type||Typeson.getJSONType(r);d(Object.assign(n||g,{keypath:t,value:r,cyclic:i,stateObj:u,promisesData:s,resolvingTypesonPromise:v,awaitingTypesonPromise:hasConstructorOf(r,e)},{type:o}))}:null;if(["string","boolean","number","undefined"].includes(m))return void 0===r||Number.isNaN(r)||r===Number.NEGATIVE_INFINITY||r===Number.POSITIVE_INFINITY?(h=u.replaced?r:replace(t,r,u,s,!1,v,O))!==r&&(g={replaced:h}):h=r,O&&O(),h;if(null===r)return O&&O(),r;if(i&&!u.iterateIn&&!u.iterateUnsetNumeric&&r&&"object"===_typeof(r)){var _=p.indexOf(r);if(!(_<0))return l[t]="#",O&&O({cyclicKeypath:y[_]}),"#"+y[_];!0===i&&(p.push(r),y.push(t))}var j,S=isPlainObject(r),T=a(r),w=(S||T)&&(!f.plainObjectReplacers.length||u.replaced)||u.iterateIn?r:replace(t,r,u,s,S||T,null,O);if(w!==r?(h=w,g={replaced:w}):""===t&&hasConstructorOf(r,e)?(s.push([t,r,i,u,void 0,void 0,u.type]),h=r):T&&"object"!==u.iterateIn||"array"===u.iterateIn?(j=new Array(r.length),g={clone:j}):(["function","symbol"].includes(_typeof(r))||"toJSON"in r||hasConstructorOf(r,e)||hasConstructorOf(r,Promise)||hasConstructorOf(r,ArrayBuffer))&&!S&&"object"!==u.iterateIn?h=r:(j={},u.addLength&&(j.length=r.length),g={clone:j}),O&&O(),n.iterateNone)return j||h;if(!j)return h;if(u.iterateIn){var A=function _loop(n){var o={ownKeys:c.call(r,n)};_adaptBuiltinStateObjectProperties(u,o,function(){var o=t+(t?".":"")+escapeKeyPathComponent(n),a=_encapsulate(o,r[n],Boolean(i),u,s,v);hasConstructorOf(a,e)?s.push([o,a,Boolean(i),u,j,n,u.type]):void 0!==a&&(j[n]=a)})};for(var P in r)A(P);O&&O({endIterateIn:!0,end:!0})}else o(r).forEach(function(n){var o=t+(t?".":"")+escapeKeyPathComponent(n);_adaptBuiltinStateObjectProperties(u,{ownKeys:!0},function(){var t=_encapsulate(o,r[n],Boolean(i),u,s,v);hasConstructorOf(t,e)?s.push([o,t,Boolean(i),u,j,n,u.type]):void 0!==t&&(j[n]=t)})}),O&&O({endIterateOwn:!0,end:!0});if(u.iterateUnsetNumeric){for(var I=r.length,C=function _loop2(n){if(!(n in r)){var o=t+(t?".":"")+n;_adaptBuiltinStateObjectProperties(u,{ownKeys:!1},function(){var t=_encapsulate(o,void 0,Boolean(i),u,s,v);hasConstructorOf(t,e)?s.push([o,t,Boolean(i),u,j,n,u.type]):void 0!==t&&(j[n]=t)})}},N=0;N>2],o+=O[(3&n[a])<<4|n[a+1]>>4],o+=O[(15&n[a+1])<<2|n[a+2]>>6],o+=O[63&n[a+2]];return i%3==2?o=o.slice(0,-1)+"=":i%3==1&&(o=o.slice(0,-2)+"=="),o},T=function decode(e){var t,r,n,i,o=e.length,a=.75*e.length,c=0;"="===e[e.length-1]&&(a--,"="===e[e.length-2]&&a--);for(var u=new ArrayBuffer(a),s=new Uint8Array(u),f=0;f>4,s[c++]=(15&r)<<4|n>>2,s[c++]=(3&n)<<6|63&i;return u},w={arraybuffer:{test:function test(e){return"ArrayBuffer"===s.toStringTag(e)},replace:function replace(e,t){t.buffers||(t.buffers=[]);var r=t.buffers.indexOf(e);return r>-1?{index:r}:(t.buffers.push(e),S(e))},revive:function revive(e,t){if(t.buffers||(t.buffers=[]),"object"===_typeof$1(e))return t.buffers[e.index];var r=T(e);return t.buffers.push(r),r}}},A="undefined"==typeof self?commonjsGlobal:self,P={};["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array"].forEach(function(e){var t=e,r=A[t];r&&(P[e.toLowerCase()]={test:function test(e){return s.toStringTag(e)===t},replace:function replace(e,t){var r=e.buffer,n=e.byteOffset,i=e.length;t.buffers||(t.buffers=[]);var o=t.buffers.indexOf(r);return o>-1?{index:o,byteOffset:n,length:i}:(t.buffers.push(r),{encoded:S(r),byteOffset:n,length:i})},revive:function revive(e,t){t.buffers||(t.buffers=[]);var n,i=e.byteOffset,o=e.length,a=e.encoded,c=e.index;return"index"in e?n=t.buffers[c]:(n=T(a),t.buffers.push(n)),new r(n,i,o)}})});var I={dataview:{test:function test(e){return"DataView"===s.toStringTag(e)},replace:function replace(e,t){var r=e.buffer,n=e.byteOffset,i=e.byteLength;t.buffers||(t.buffers=[]);var o=t.buffers.indexOf(r);return o>-1?{index:o,byteOffset:n,byteLength:i}:(t.buffers.push(r),{encoded:S(r),byteOffset:n,byteLength:i})},revive:function revive(e,t){t.buffers||(t.buffers=[]);var r,n=e.byteOffset,i=e.byteLength,o=e.encoded,a=e.index;return"index"in e?r=t.buffers[a]:(r=T(o),t.buffers.push(r)),new DataView(r,n,i)}}},C={IntlCollator:{test:function test(e){return s.hasConstructorOf(e,Intl.Collator)},replace:function replace(e){return e.resolvedOptions()},revive:function revive(e){return new Intl.Collator(e.locale,e)}},IntlDateTimeFormat:{test:function test(e){return s.hasConstructorOf(e,Intl.DateTimeFormat)},replace:function replace(e){return e.resolvedOptions()},revive:function revive(e){return new Intl.DateTimeFormat(e.locale,e)}},IntlNumberFormat:{test:function test(e){return s.hasConstructorOf(e,Intl.NumberFormat)},replace:function replace(e){return e.resolvedOptions()},revive:function revive(e){return new Intl.NumberFormat(e.locale,e)}}};function string2arraybuffer(e){for(var t=new Uint8Array(e.length),r=0;r>2];base64+=chars[(bytes[_i]&3)<<4|bytes[_i+1]>>4];base64+=chars[(bytes[_i+1]&15)<<2|bytes[_i+2]>>6];base64+=chars[bytes[_i+2]&63]}if(len%3===2){base64=base64.slice(0,-1)+"="}else if(len%3===1){base64=base64.slice(0,-2)+"=="}return base64};var decode=function decode(base64){var len=base64.length;var bufferLength=base64.length*.75;var p=0;var encoded1,encoded2,encoded3,encoded4;if(base64[base64.length-1]==="="){bufferLength--;if(base64[base64.length-2]==="="){bufferLength--}}var arraybuffer=new ArrayBuffer(bufferLength),bytes=new Uint8Array(arraybuffer);for(var _i2=0;_i2>4;bytes[p++]=(encoded2&15)<<4|encoded3>>2;bytes[p++]=(encoded3&3)<<6|encoded4&63}return arraybuffer};var _global=typeof self==="undefined"?global:self;var exportObj={};["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array"].forEach(function(typeName){var arrType=typeName;var TypedArray=_global[arrType];if(TypedArray){exportObj[typeName.toLowerCase()+"2"]={test:function(x){return typeson.toStringTag(x)===arrType},replace:function(_a){var buffer=_a.buffer,byteOffset=_a.byteOffset,length=_a.length;return{buffer:buffer,byteOffset:byteOffset,length:length}},revive:function(b64Obj){var buffer=b64Obj.buffer,byteOffset=b64Obj.byteOffset,length=b64Obj.length;return new TypedArray(buffer,byteOffset,length)}}}});var arrayBuffer={arraybuffer:{test:function(x){return typeson.toStringTag(x)==="ArrayBuffer"},replace:function(b){return encode(b,0,b.byteLength)},revive:function(b64){var buffer=decode(b64);return buffer}}};var TSON=(new typeson).register(structuredCloning);var readBlobsSynchronously="FileReaderSync"in self;var blobsToAwait=[];var blobsToAwaitPos=0;TSON.register([arrayBuffer,exportObj,{blob2:{test:function(x){return typeson.toStringTag(x)==="Blob"},replace:function(b){if(b.isClosed){throw new Error("The Blob is closed")}if(readBlobsSynchronously){var data=readBlobSync(b,"binary");var base64=encode(data,0,data.byteLength);return{type:b.type,data:base64}}else{blobsToAwait.push(b);var result={type:b.type,data:{start:blobsToAwaitPos,end:blobsToAwaitPos+b.size}};blobsToAwaitPos+=b.size;return result}},finalize:function(b,ba){b.data=encode(ba,0,ba.byteLength)},revive:function(_a){var type=_a.type,data=_a.data;return new Blob([decode(data)],{type:type})}}}]);TSON.mustFinalize=function(){return blobsToAwait.length>0};TSON.finalize=function(items){return __awaiter(void 0,void 0,void 0,function(){var allChunks,_i,items_1,item,types,arrayType,keyPath,typeName,typeSpec,b;return __generator(this,function(_a){switch(_a.label){case 0:return[4,readBlobAsync(new Blob(blobsToAwait),"binary")];case 1:allChunks=_a.sent();if(items){for(_i=0,items_1=items;_i0){slices.push(",");if(prettyJson){slices.push("\n ")}}mayHaveMoreRows=values.length===LIMIT;if(!inbound)return[3,4];filteredValues=filter?values.filter(function(value){return filter(tableName,value)}):values;tsonValues=filteredValues.map(function(value){return TSON.encapsulate(value)});if(!TSON.mustFinalize())return[3,3];return[4,Dexie__default["default"].waitFor(TSON.finalize(tsonValues))];case 2:_c.sent();_c.label=3;case 3:json=JSON.stringify(tsonValues,undefined,prettyJson?2:undefined);if(prettyJson)json=json.split("\n").join("\n ");slices.push(new Blob([json.substring(1,json.length-1)]));lastNumRows=filteredValues.length;lastKey=values.length>0?Dexie__default["default"].getByKeyPath(values[values.length-1],primKey.keyPath):null;return[3,8];case 4:return[4,chunkedCollection.primaryKeys()];case 5:keys=_c.sent();keyvals=keys.map(function(key,i){return[key,values[i]]});if(filter)keyvals=keyvals.filter(function(_a){var key=_a[0],value=_a[1];return filter(tableName,value,key)});tsonTuples=keyvals.map(function(tuple){return TSON.encapsulate(tuple)});if(!TSON.mustFinalize())return[3,7];return[4,Dexie__default["default"].waitFor(TSON.finalize(tsonTuples))];case 6:_c.sent();_c.label=7;case 7:json=JSON.stringify(tsonTuples,undefined,prettyJson?2:undefined);if(prettyJson)json=json.split("\n").join("\n ");slices.push(new Blob([json.substring(1,json.length-1)]));lastNumRows=keyvals.length;lastKey=keys.length>0?keys[keys.length-1]:null;_c.label=8;case 8:progress.completedRows+=values.length;return[2]}})};_b.label=1;case 1:if(!mayHaveMoreRows)return[3,3];return[5,_loop_2()];case 2:state_1=_b.sent();if(state_1==="break")return[3,3];return[3,1];case 3:slices.push(emptyTableExportJson.substr(posEndRowsArray));progress.completedTables+=1;if(progress.completedTablesmaxAllowed){switch(buffer){case"text":closeText(parser);break;default:error(parser,"Max buffer length exceeded: "+buffer)}}maxActual=Math.max(maxActual,len)}parser.bufferCheckPosition=clarinet.MAX_BUFFER_LENGTH-maxActual+parser.position}function clearBuffers(parser){for(var buffer in buffers){parser[buffer]=buffers[buffer]}}var stringTokenPattern=/[\\"\n]/g;function CParser(opt){if(!(this instanceof CParser))return new CParser(opt);var parser=this;clearBuffers(parser);parser.bufferCheckPosition=clarinet.MAX_BUFFER_LENGTH;parser.q=parser.c=parser.p="";parser.opt=opt||{};parser.closed=parser.closedRoot=parser.sawRoot=false;parser.tag=parser.error=null;parser.state=S.BEGIN;parser.stack=new Array;parser.position=parser.column=0;parser.line=1;parser.slashed=false;parser.unicodeI=0;parser.unicodeS=null;parser.depth=0;emit(parser,"onready")}CParser.prototype={end:function(){end(this)},write:write,resume:function(){this.error=null;return this},close:function(){return this.write(null)}};try{Stream=fakeStream.Stream}catch(ex){Stream=function(){}}function createStream(opt){return new CStream(opt)}function CStream(opt){if(!(this instanceof CStream))return new CStream(opt);this._parser=new CParser(opt);this.writable=true;this.readable=true;this.bytes_remaining=0;this.bytes_in_sequence=0;this.temp_buffs={2:new Buffer(2),3:new Buffer(3),4:new Buffer(4)};this.string="";var me=this;Stream.apply(me);this._parser.onend=function(){me.emit("end")};this._parser.onerror=function(er){me.emit("error",er);me._parser.error=null};streamWraps.forEach(function(ev){Object.defineProperty(me,"on"+ev,{get:function(){return me._parser["on"+ev]},set:function(h){if(!h){me.removeAllListeners(ev);me._parser["on"+ev]=h;return h}me.on(ev,h)},enumerable:true,configurable:false})})}CStream.prototype=Object.create(Stream.prototype,{constructor:{value:CStream}});CStream.prototype.write=function(data){data=new Buffer(data);for(var i=0;i0){for(var j=0;j=128){if(n>=194&&n<=223)this.bytes_in_sequence=2;if(n>=224&&n<=239)this.bytes_in_sequence=3;if(n>=240&&n<=244)this.bytes_in_sequence=4;if(this.bytes_in_sequence+i>data.length){for(var k=0;k<=data.length-1-i;k++){this.temp_buffs[this.bytes_in_sequence][k]=data[i+k]}this.bytes_remaining=i+this.bytes_in_sequence-data.length;return true}else{this.string=data.slice(i,i+this.bytes_in_sequence).toString();i=i+this.bytes_in_sequence-1;this._parser.write(this.string);this.emit("data",this.string);continue}}for(var p=i;p=128)break}this.string=data.slice(i,p).toString();this._parser.write(this.string);this.emit("data",this.string);i=p-1;continue}};CStream.prototype.end=function(chunk){if(chunk&&chunk.length)this._parser.write(chunk.toString());this._parser.end();return true};CStream.prototype.on=function(ev,handler){var me=this;if(!me._parser["on"+ev]&&streamWraps.indexOf(ev)!==-1){me._parser["on"+ev]=function(){var args=arguments.length===1?[arguments[0]]:Array.apply(null,arguments);args.splice(0,0,ev);me.emit.apply(me,args)}}return Stream.prototype.on.call(me,ev,handler)};CStream.prototype.destroy=function(){clearBuffers(this._parser);this.emit("close")};function emit(parser,event,data){if(clarinet.INFO)console.log("-- emit",event,data);if(parser[event])parser[event](data)}function emitNode(parser,event,data){closeValue(parser);emit(parser,event,data)}function closeValue(parser,event){parser.textNode=textopts(parser.opt,parser.textNode);if(parser.textNode!==undefined){emit(parser,event?event:"onvalue",parser.textNode)}parser.textNode=undefined}function closeNumber(parser){if(parser.numberNode)emit(parser,"onvalue",parseFloat(parser.numberNode));parser.numberNode=""}function textopts(opt,text){if(text===undefined){return text}if(opt.trim)text=text.trim();if(opt.normalize)text=text.replace(/\s+/g," ");return text}function error(parser,er){closeValue(parser);er+="\nLine: "+parser.line+"\nColumn: "+parser.column+"\nChar: "+parser.c;er=new Error(er);parser.error=er;emit(parser,"onerror",er);return parser}function end(parser){if(parser.state!==S.VALUE||parser.depth!==0)error(parser,"Unexpected end");closeValue(parser);parser.c="";parser.closed=true;emit(parser,"onend");CParser.call(parser,parser.opt);return parser}function isWhitespace(c){return c===Char.carriageReturn||c===Char.lineFeed||c===Char.space||c===Char.tab}function write(chunk){var parser=this;if(this.error)throw this.error;if(parser.closed)return error(parser,"Cannot write after close. Assign an onready handler.");if(chunk===null)return end(parser);var i=0,c=chunk.charCodeAt(0),p=parser.p;if(clarinet.DEBUG)console.log("write -> ["+chunk+"]");while(c){p=c;parser.c=c=chunk.charCodeAt(i++);if(p!==c)parser.p=p;else p=parser.p;if(!c)break;if(clarinet.DEBUG)console.log(i,c,clarinet.STATE[parser.state]);parser.position++;if(c===Char.lineFeed){parser.line++;parser.column=0}else parser.column++;switch(parser.state){case S.BEGIN:if(c===Char.openBrace)parser.state=S.OPEN_OBJECT;else if(c===Char.openBracket)parser.state=S.OPEN_ARRAY;else if(!isWhitespace(c))error(parser,"Non-whitespace before {[.");continue;case S.OPEN_KEY:case S.OPEN_OBJECT:if(isWhitespace(c))continue;if(parser.state===S.OPEN_KEY)parser.stack.push(S.CLOSE_KEY);else{if(c===Char.closeBrace){emit(parser,"onopenobject");this.depth++;emit(parser,"oncloseobject");this.depth--;parser.state=parser.stack.pop()||S.VALUE;continue}else parser.stack.push(S.CLOSE_OBJECT)}if(c===Char.doubleQuote)parser.state=S.STRING;else error(parser,'Malformed object key should start with "');continue;case S.CLOSE_KEY:case S.CLOSE_OBJECT:if(isWhitespace(c))continue;parser.state===S.CLOSE_KEY?"key":"object";if(c===Char.colon){if(parser.state===S.CLOSE_OBJECT){parser.stack.push(S.CLOSE_OBJECT);closeValue(parser,"onopenobject");this.depth++}else closeValue(parser,"onkey");parser.state=S.VALUE}else if(c===Char.closeBrace){emitNode(parser,"oncloseobject");this.depth--;parser.state=parser.stack.pop()||S.VALUE}else if(c===Char.comma){if(parser.state===S.CLOSE_OBJECT)parser.stack.push(S.CLOSE_OBJECT);closeValue(parser);parser.state=S.OPEN_KEY}else error(parser,"Bad object");continue;case S.OPEN_ARRAY:case S.VALUE:if(isWhitespace(c))continue;if(parser.state===S.OPEN_ARRAY){emit(parser,"onopenarray");this.depth++;parser.state=S.VALUE;if(c===Char.closeBracket){emit(parser,"onclosearray");this.depth--;parser.state=parser.stack.pop()||S.VALUE;continue}else{parser.stack.push(S.CLOSE_ARRAY)}}if(c===Char.doubleQuote)parser.state=S.STRING;else if(c===Char.openBrace)parser.state=S.OPEN_OBJECT;else if(c===Char.openBracket)parser.state=S.OPEN_ARRAY;else if(c===Char.t)parser.state=S.TRUE;else if(c===Char.f)parser.state=S.FALSE;else if(c===Char.n)parser.state=S.NULL;else if(c===Char.minus){parser.numberNode+="-"}else if(Char._0<=c&&c<=Char._9){parser.numberNode+=String.fromCharCode(c);parser.state=S.NUMBER_DIGIT}else error(parser,"Bad value");continue;case S.CLOSE_ARRAY:if(c===Char.comma){parser.stack.push(S.CLOSE_ARRAY);closeValue(parser,"onvalue");parser.state=S.VALUE}else if(c===Char.closeBracket){emitNode(parser,"onclosearray");this.depth--;parser.state=parser.stack.pop()||S.VALUE}else if(isWhitespace(c))continue;else error(parser,"Bad array");continue;case S.STRING:if(parser.textNode===undefined){parser.textNode=""}var starti=i-1,slashed=parser.slashed,unicodeI=parser.unicodeI;STRING_BIGLOOP:while(true){if(clarinet.DEBUG)console.log(i,c,clarinet.STATE[parser.state],slashed);while(unicodeI>0){parser.unicodeS+=String.fromCharCode(c);c=chunk.charCodeAt(i++);parser.position++;if(unicodeI===4){parser.textNode+=String.fromCharCode(parseInt(parser.unicodeS,16));unicodeI=0;starti=i-1}else{unicodeI++}if(!c)break STRING_BIGLOOP}if(c===Char.doubleQuote&&!slashed){parser.state=parser.stack.pop()||S.VALUE;parser.textNode+=chunk.substring(starti,i-1);parser.position+=i-1-starti;break}if(c===Char.backslash&&!slashed){slashed=true;parser.textNode+=chunk.substring(starti,i-1);parser.position+=i-1-starti;c=chunk.charCodeAt(i++);parser.position++;if(!c)break}if(slashed){slashed=false;if(c===Char.n){parser.textNode+="\n"}else if(c===Char.r){parser.textNode+="\r"}else if(c===Char.t){parser.textNode+="\t"}else if(c===Char.f){parser.textNode+="\f"}else if(c===Char.b){parser.textNode+="\b"}else if(c===Char.u){unicodeI=1;parser.unicodeS=""}else{parser.textNode+=String.fromCharCode(c)}c=chunk.charCodeAt(i++);parser.position++;starti=i-1;if(!c)break;else continue}stringTokenPattern.lastIndex=i;var reResult=stringTokenPattern.exec(chunk);if(reResult===null){i=chunk.length+1;parser.textNode+=chunk.substring(starti,i-1);parser.position+=i-1-starti;break}i=reResult.index+1;c=chunk.charCodeAt(reResult.index);if(!c){parser.textNode+=chunk.substring(starti,i-1);parser.position+=i-1-starti;break}}parser.slashed=slashed;parser.unicodeI=unicodeI;continue;case S.TRUE:if(c===Char.r)parser.state=S.TRUE2;else error(parser,"Invalid true started with t"+c);continue;case S.TRUE2:if(c===Char.u)parser.state=S.TRUE3;else error(parser,"Invalid true started with tr"+c);continue;case S.TRUE3:if(c===Char.e){emit(parser,"onvalue",true);parser.state=parser.stack.pop()||S.VALUE}else error(parser,"Invalid true started with tru"+c);continue;case S.FALSE:if(c===Char.a)parser.state=S.FALSE2;else error(parser,"Invalid false started with f"+c);continue;case S.FALSE2:if(c===Char.l)parser.state=S.FALSE3;else error(parser,"Invalid false started with fa"+c);continue;case S.FALSE3:if(c===Char.s)parser.state=S.FALSE4;else error(parser,"Invalid false started with fal"+c);continue;case S.FALSE4:if(c===Char.e){emit(parser,"onvalue",false);parser.state=parser.stack.pop()||S.VALUE}else error(parser,"Invalid false started with fals"+c);continue;case S.NULL:if(c===Char.u)parser.state=S.NULL2;else error(parser,"Invalid null started with n"+c);continue;case S.NULL2:if(c===Char.l)parser.state=S.NULL3;else error(parser,"Invalid null started with nu"+c);continue;case S.NULL3:if(c===Char.l){emit(parser,"onvalue",null);parser.state=parser.stack.pop()||S.VALUE}else error(parser,"Invalid null started with nul"+c);continue;case S.NUMBER_DECIMAL_POINT:if(c===Char.period){parser.numberNode+=".";parser.state=S.NUMBER_DIGIT}else error(parser,"Leading zero not followed by .");continue;case S.NUMBER_DIGIT:if(Char._0<=c&&c<=Char._9)parser.numberNode+=String.fromCharCode(c);else if(c===Char.period){if(parser.numberNode.indexOf(".")!==-1)error(parser,"Invalid number has two dots");parser.numberNode+="."}else if(c===Char.e||c===Char.E){if(parser.numberNode.indexOf("e")!==-1||parser.numberNode.indexOf("E")!==-1)error(parser,"Invalid number has two exponential");parser.numberNode+="e"}else if(c===Char.plus||c===Char.minus){if(!(p===Char.e||p===Char.E))error(parser,"Invalid symbol in number");parser.numberNode+=String.fromCharCode(c)}else{closeNumber(parser);i--;parser.state=parser.stack.pop()||S.VALUE}continue;default:error(parser,"Unknown state: "+parser.state)}}if(parser.position>=parser.bufferCheckPosition)checkBufferLength(parser);return parser}})(exports)});function JsonStream(blob){var pos=0;var parser=JsonParser(true);var rv={pullAsync:function(numBytes){return __awaiter(this,void 0,void 0,function(){var slize,jsonPart,result;return __generator(this,function(_a){switch(_a.label){case 0:slize=blob.slice(pos,pos+numBytes);pos+=numBytes;return[4,readBlobAsync(slize,"text")];case 1:jsonPart=_a.sent();result=parser.write(jsonPart);rv.result=result||{};return[2,result]}})})},pullSync:function(numBytes){var slize=blob.slice(pos,pos+numBytes);pos+=numBytes;var jsonPart=readBlobSync(slize,"text");var result=parser.write(jsonPart);rv.result=result||{};return result},done:function(){return parser.done()},eof:function(){return pos>=blob.size},result:{}};return rv}function JsonParser(allowPartial){var parser=clarinet_1.parser();var level=0;var result;var stack=[];var obj;var key;var done=false;var array=false;parser.onopenobject=function(newKey){var newObj={};newObj.incomplete=true;if(!result)result=newObj;if(obj){stack.push([key,obj,array]);if(allowPartial){if(array){obj.push(newObj)}else{obj[key]=newObj}}}obj=newObj;key=newKey;array=false;++level};parser.onkey=function(newKey){return key=newKey};parser.onvalue=function(value){return array?obj.push(value):obj[key]=value};parser.oncloseobject=function(){var _a;delete obj.incomplete;key=null;if(--level===0){done=true}else{var completedObj=obj;_a=stack.pop(),key=_a[0],obj=_a[1],array=_a[2];if(!allowPartial){if(array){obj.push(completedObj)}else{obj[key]=completedObj}}}};parser.onopenarray=function(){var newObj=[];newObj.incomplete=true;if(!result)result=newObj;if(obj){stack.push([key,obj,array]);if(allowPartial){if(array){obj.push(newObj)}else{obj[key]=newObj}}}obj=newObj;array=true;key=null;++level};parser.onclosearray=function(){var _a;delete obj.incomplete;key=null;if(--level===0){done=true}else{var completedObj=obj;_a=stack.pop(),key=_a[0],obj=_a[1],array=_a[2];if(!allowPartial){if(array){obj.push(completedObj)}else{obj[key]=completedObj}}}};return{write:function(jsonPart){parser.write(jsonPart);return result},done:function(){return done}}}var DEFAULT_KILOBYTES_PER_CHUNK=1024;function importDB(exportedData,options){return __awaiter(this,void 0,void 0,function(){var CHUNK_SIZE,stream,dbExport,db;return __generator(this,function(_a){switch(_a.label){case 0:options=options||{};CHUNK_SIZE=options.chunkSizeBytes||DEFAULT_KILOBYTES_PER_CHUNK*1024;return[4,loadUntilWeGotEnoughData(exportedData,CHUNK_SIZE)];case 1:stream=_a.sent();dbExport=stream.result.data;db=new Dexie__default["default"](dbExport.databaseName);db.version(dbExport.databaseVersion).stores(extractDbSchema(dbExport));return[4,importInto(db,stream,options)];case 2:_a.sent();return[2,db]}})})}function peakImportFile(exportedData){return __awaiter(this,void 0,void 0,function(){var stream;return __generator(this,function(_a){switch(_a.label){case 0:stream=JsonStream(exportedData);_a.label=1;case 1:if(!!stream.eof())return[3,3];return[4,stream.pullAsync(5*1024)];case 2:_a.sent();if(stream.result.data&&stream.result.data.data){delete stream.result.data.data;return[3,3]}return[3,1];case 3:return[2,stream.result]}})})}function importInto(db,exportedData,options){return __awaiter(this,void 0,void 0,function(){function importAll(){return __awaiter(this,void 0,void 0,function(){var _loop_1,_i,_a,tableExport,state_1;return __generator(this,function(_b){switch(_b.label){case 0:_loop_1=function(tableExport){var tableName,table,tableSchemaStr,sourceRows,rows,i,obj,filter,filteredRows,_c,keys,values;return __generator(this,function(_d){switch(_d.label){case 0:if(!tableExport.rows)return[2,"break"];if(!tableExport.rows.incomplete&&tableExport.rows.length===0)return[2,"continue"];if(progressCallback){Dexie__default["default"].ignoreTransaction(function(){return progressCallback(progress)})}tableName=tableExport.tableName;table=db.table(tableName);tableSchemaStr=dbExport.tables.filter(function(t){return t.name===tableName})[0].schema;if(!table){if(!options.acceptMissingTables)throw new Error("Exported table ".concat(tableExport.tableName," is missing in installed database"));else return[2,"continue"]}if(!options.acceptChangedPrimaryKey&&tableSchemaStr.split(",")[0]!=table.schema.primKey.src){throw new Error("Primary key differs for table ".concat(tableExport.tableName,". "))}sourceRows=tableExport.rows;rows=[];for(i=0;i0&&dbExport.data[0].rows&&!dbExport.data[0].rows.incomplete){dbExport.data.splice(0,1)}if(!(!jsonStream.done()&&!jsonStream.eof()))return[3,8];if(!readBlobsSynchronously)return[3,5];jsonStream.pullSync(CHUNK_SIZE);return[3,7];case 5:return[4,Dexie__default["default"].waitFor(jsonStream.pullAsync(CHUNK_SIZE))];case 6:_b.sent();_b.label=7;case 7:return[3,9];case 8:return[3,10];case 9:return[3,0];case 10:return[2]}})})}var CHUNK_SIZE,jsonStream,dbExportFile,readBlobsSynchronously,dbExport,progressCallback,progress,_i,_a,table;return __generator(this,function(_b){switch(_b.label){case 0:options=options||{};CHUNK_SIZE=options.chunkSizeBytes||DEFAULT_KILOBYTES_PER_CHUNK*1024;return[4,loadUntilWeGotEnoughData(exportedData,CHUNK_SIZE)];case 1:jsonStream=_b.sent();dbExportFile=jsonStream.result;readBlobsSynchronously="FileReaderSync"in self;dbExport=dbExportFile.data;if(!options.acceptNameDiff&&db.name!==dbExport.databaseName)throw new Error("Name differs. Current database name is ".concat(db.name," but export is ").concat(dbExport.databaseName));if(!options.acceptVersionDiff&&db.verno!==dbExport.databaseVersion){throw new Error("Database version differs. Current database is in version ".concat(db.verno," but export is ").concat(dbExport.databaseVersion))}progressCallback=options.progressCallback;progress={done:false,completedRows:0,completedTables:0,totalRows:dbExport.tables.reduce(function(p,c){return p+c.rowCount},0),totalTables:dbExport.tables.length};if(progressCallback){Dexie__default["default"].ignoreTransaction(function(){return progressCallback(progress)})}if(!options.clearTablesBeforeImport)return[3,5];_i=0,_a=db.tables;_b.label=2;case 2:if(!(_i<_a.length))return[3,5];table=_a[_i];return[4,table.clear()];case 3:_b.sent();_b.label=4;case 4:_i++;return[3,2];case 5:if(!options.noTransaction)return[3,7];return[4,importAll()];case 6:_b.sent();return[3,9];case 7:return[4,db.transaction("rw",db.tables,importAll)];case 8:_b.sent();_b.label=9;case 9:progress.done=true;if(progressCallback){Dexie__default["default"].ignoreTransaction(function(){return progressCallback(progress)})}return[2]}})})}function loadUntilWeGotEnoughData(exportedData,CHUNK_SIZE){return __awaiter(this,void 0,void 0,function(){var stream,dbExportFile;return __generator(this,function(_a){switch(_a.label){case 0:stream="slice"in exportedData?JsonStream(exportedData):exportedData;_a.label=1;case 1:if(!!stream.eof())return[3,3];return[4,stream.pullAsync(CHUNK_SIZE)];case 2:_a.sent();if(stream.result.data&&stream.result.data.data)return[3,3];return[3,1];case 3:dbExportFile=stream.result;if(!dbExportFile||dbExportFile.formatName!="dexie")throw new Error("Given file is not a dexie export");if(dbExportFile.formatVersion>VERSION){throw new Error("Format version ".concat(dbExportFile.formatVersion," not supported"))}if(!dbExportFile.data){throw new Error("No data in export file")}if(!dbExportFile.data.databaseName){throw new Error("Missing databaseName in export file")}if(!dbExportFile.data.databaseVersion){throw new Error("Missing databaseVersion in export file")}if(!dbExportFile.data.tables){throw new Error("Missing tables in export file")}return[2,stream]}})})}Dexie__default["default"].prototype.export=function(options){return exportDB(this,options)};Dexie__default["default"].prototype.import=function(blob,options){return importInto(this,blob,options)};Dexie__default["default"].import=function(blob,options){return importDB(blob,options)};var dexieExportImport=function(){throw new Error("This addon extends Dexie.prototype globally and does not have be included in Dexie constructor's addons options.")};exports["default"]=dexieExportImport;exports.exportDB=exportDB;exports.importDB=importDB;exports.importInto=importInto;exports.peakImportFile=peakImportFile;Object.defineProperty(exports,"__esModule",{value:true})});(function(root,factory){if(typeof define==="function"&&define.amd){define([],factory)}else if(typeof exports==="object"){module.exports=factory()}else{root.download=factory()}})(this,function(){return function download(data,strFileName,strMimeType){var self=window,defaultMime="application/octet-stream",mimeType=strMimeType||defaultMime,payload=data,url=!strFileName&&!strMimeType&&payload,anchor=document.createElement("a"),toString=function(a){return String(a)},myBlob=self.Blob||self.MozBlob||self.WebKitBlob||toString,fileName=strFileName||"download",blob,reader;myBlob=myBlob.call?myBlob.bind(self):Blob;if(String(this)==="true"){payload=[payload,mimeType];mimeType=payload[0];payload=payload[1]}if(url&&url.length<2048){fileName=url.split("/").pop().split("?")[0];anchor.href=url;if(anchor.href.indexOf(url)!==-1){var ajax=new XMLHttpRequest;ajax.open("GET",url,true);ajax.responseType="blob";ajax.onload=function(e){download(e.target.response,fileName,defaultMime)};setTimeout(function(){ajax.send()},0);return ajax}}if(/^data:([\w+-]+\/[\w+.-]+)?[,;]/.test(payload)){if(payload.length>1024*1024*1.999&&myBlob!==toString){payload=dataUrlToBlob(payload);mimeType=payload.type||defaultMime}else{return navigator.msSaveBlob?navigator.msSaveBlob(dataUrlToBlob(payload),fileName):saver(payload)}}else{if(/([\x80-\xff])/.test(payload)){var i=0,tempUiArr=new Uint8Array(payload.length),mx=tempUiArr.length;for(i;i 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } - } - - function getSchemaString(table) { - var primKeyAndIndexes = [table.schema.primKey].concat(table.schema.indexes); - return primKeyAndIndexes.map(function (index) { return index.src; }).join(','); - } - function extractDbSchema(exportedDb) { - var schema = {}; - for (var _i = 0, _a = exportedDb.tables; _i < _a.length; _i++) { - var table = _a[_i]; - schema[table.name] = table.schema; - } - return schema; - } - function readBlobAsync(blob, type) { - return new Promise(function (resolve, reject) { - var reader = new FileReader(); - reader.onabort = function (ev) { return reject(new Error("file read aborted")); }; - reader.onerror = function (ev) { return reject(ev.target.error); }; - reader.onload = function (ev) { return resolve(ev.target.result); }; - if (type === 'binary') - reader.readAsArrayBuffer(blob); - else - reader.readAsText(blob); - }); - } - function readBlobSync(blob, type) { - if (typeof FileReaderSync === 'undefined') { - throw new Error('FileReaderSync missing. Reading blobs synchronously requires code to run from within a web worker. Use TSON.encapsulateAsync() to do it from the main thread.'); - } - var reader = new FileReaderSync(); // Requires worker environment - var data = type === 'binary' ? - reader.readAsArrayBuffer(blob) : - reader.readAsText(blob); - return data; - } - - var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; - - function createCommonjsModule(fn, module) { - return module = { exports: {} }, fn(module, module.exports), module.exports; - } - - var typeson = createCommonjsModule(function (module, exports) { - (function (global, factory) { - module.exports = factory() ; - }(commonjsGlobal, (function () { - function _typeof(obj) { - if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { - _typeof = function (obj) { - return typeof obj; - }; - } else { - _typeof = function (obj) { - return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; - }; - } - - return _typeof(obj); - } - - function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { - try { - var info = gen[key](arg); - var value = info.value; - } catch (error) { - reject(error); - return; - } - - if (info.done) { - resolve(value); - } else { - Promise.resolve(value).then(_next, _throw); - } - } - - function _asyncToGenerator(fn) { - return function () { - var self = this, - args = arguments; - return new Promise(function (resolve, reject) { - var gen = fn.apply(self, args); - - function _next(value) { - asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); - } - - function _throw(err) { - asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); - } - - _next(undefined); - }); - }; - } - - function _classCallCheck(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } - } - - function _defineProperties(target, props) { - for (var i = 0; i < props.length; i++) { - var descriptor = props[i]; - descriptor.enumerable = descriptor.enumerable || false; - descriptor.configurable = true; - if ("value" in descriptor) descriptor.writable = true; - Object.defineProperty(target, descriptor.key, descriptor); - } - } - - function _createClass(Constructor, protoProps, staticProps) { - if (protoProps) _defineProperties(Constructor.prototype, protoProps); - if (staticProps) _defineProperties(Constructor, staticProps); - return Constructor; - } - - function _defineProperty(obj, key, value) { - if (key in obj) { - Object.defineProperty(obj, key, { - value: value, - enumerable: true, - configurable: true, - writable: true - }); - } else { - obj[key] = value; - } - - return obj; - } - - function ownKeys(object, enumerableOnly) { - var keys = Object.keys(object); - - if (Object.getOwnPropertySymbols) { - var symbols = Object.getOwnPropertySymbols(object); - if (enumerableOnly) symbols = symbols.filter(function (sym) { - return Object.getOwnPropertyDescriptor(object, sym).enumerable; - }); - keys.push.apply(keys, symbols); - } - - return keys; - } - - function _objectSpread2(target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i] != null ? arguments[i] : {}; - - if (i % 2) { - ownKeys(Object(source), true).forEach(function (key) { - _defineProperty(target, key, source[key]); - }); - } else if (Object.getOwnPropertyDescriptors) { - Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); - } else { - ownKeys(Object(source)).forEach(function (key) { - Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); - }); - } - } - - return target; - } - - function _slicedToArray(arr, i) { - return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); - } - - function _toConsumableArray(arr) { - return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); - } - - function _arrayWithoutHoles(arr) { - if (Array.isArray(arr)) { - for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; - - return arr2; - } - } - - function _arrayWithHoles(arr) { - if (Array.isArray(arr)) return arr; - } - - function _iterableToArray(iter) { - if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); - } - - function _iterableToArrayLimit(arr, i) { - if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { - return; - } - - var _arr = []; - var _n = true; - var _d = false; - var _e = undefined; - - try { - for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { - _arr.push(_s.value); - - if (i && _arr.length === i) break; - } - } catch (err) { - _d = true; - _e = err; - } finally { - try { - if (!_n && _i["return"] != null) _i["return"](); - } finally { - if (_d) throw _e; - } - } - - return _arr; - } - - function _nonIterableSpread() { - throw new TypeError("Invalid attempt to spread non-iterable instance"); - } - - function _nonIterableRest() { - throw new TypeError("Invalid attempt to destructure non-iterable instance"); - } - - /** - * We keep this function minimized so if using two instances of this - * library, where one is minimized and one is not, it will still work - * with `hasConstructorOf`. - * With ES6 classes, we may be able to simply use `class TypesonPromise - * extends Promise` and add a string tag for detection. - * @param {function} f - */ - // eslint-disable-next-line max-len - // eslint-disable-next-line block-spacing, space-before-function-paren, space-before-blocks, space-infix-ops, semi, promise/avoid-new - var TypesonPromise = function TypesonPromise(f) { - _classCallCheck(this, TypesonPromise); - - this.p = new Promise(f); - }; // eslint-disable-next-line max-len - // class TypesonPromise extends Promise {get[Symbol.toStringTag](){return 'TypesonPromise'};} // eslint-disable-line keyword-spacing, space-before-function-paren, space-before-blocks, block-spacing, semi - - - TypesonPromise.__typeson__type__ = 'TypesonPromise'; // Note: core-js-bundle provides a `Symbol` polyfill - - /* istanbul ignore else */ - - if (typeof Symbol !== 'undefined') { - // Ensure `isUserObject` will return `false` for `TypesonPromise` - TypesonPromise.prototype[Symbol.toStringTag] = 'TypesonPromise'; - } - /** - * - * @param {function} [onFulfilled] - * @param {function} [onRejected] - * @returns {TypesonPromise} - */ - - - TypesonPromise.prototype.then = function (onFulfilled, onRejected) { - var _this = this; - - return new TypesonPromise(function (typesonResolve, typesonReject) { - // eslint-disable-next-line promise/catch-or-return - _this.p.then(function (res) { - // eslint-disable-next-line promise/always-return - typesonResolve(onFulfilled ? onFulfilled(res) : res); - })["catch"](function (res) { - return onRejected ? onRejected(res) : Promise.reject(res); - }).then(typesonResolve, typesonReject); - }); - }; - /** - * - * @param {function} onRejected - * @returns {TypesonPromise} - */ - - - TypesonPromise.prototype["catch"] = function (onRejected) { - return this.then(null, onRejected); - }; - /** - * - * @param {Any} v - * @returns {TypesonPromise} - */ - - - TypesonPromise.resolve = function (v) { - return new TypesonPromise(function (typesonResolve) { - typesonResolve(v); - }); - }; - /** - * - * @param {Any} v - * @returns {TypesonPromise} - */ - - - TypesonPromise.reject = function (v) { - return new TypesonPromise(function (typesonResolve, typesonReject) { - typesonReject(v); - }); - }; - - ['all', 'race'].forEach(function (meth) { - /** - * - * @param {Promise[]} promArr - * @returns {TypesonPromise} - */ - TypesonPromise[meth] = function (promArr) { - return new TypesonPromise(function (typesonResolve, typesonReject) { - // eslint-disable-next-line promise/catch-or-return - Promise[meth](promArr.map(function (prom) { - return prom && prom.constructor && prom.constructor.__typeson__type__ === 'TypesonPromise' ? prom.p : prom; - })).then(typesonResolve, typesonReject); - }); - }; - }); - - var _ref = {}, - toStr = _ref.toString, - hasOwn = {}.hasOwnProperty, - getProto = Object.getPrototypeOf, - fnToString = hasOwn.toString; - /** - * Second argument not in use internally, but provided for utility. - * @param {Any} v - * @param {boolean} catchCheck - * @returns {boolean} - */ - - function isThenable(v, catchCheck) { - return isObject(v) && typeof v.then === 'function' && (!catchCheck || typeof v["catch"] === 'function'); - } - /** - * - * @param {Any} val - * @returns {string} - */ - - - function toStringTag(val) { - return toStr.call(val).slice(8, -1); - } - /** - * This function is dependent on both constructors - * being identical so any minimization is expected of both. - * @param {Any} a - * @param {function} b - * @returns {boolean} - */ - - - function hasConstructorOf(a, b) { - if (!a || _typeof(a) !== 'object') { - return false; - } - - var proto = getProto(a); - - if (!proto) { - return b === null; - } - - var Ctor = hasOwn.call(proto, 'constructor') && proto.constructor; - - if (typeof Ctor !== 'function') { - return b === null; - } - - if (b === Ctor) { - return true; - } - - if (b !== null && fnToString.call(Ctor) === fnToString.call(b)) { - return true; - } - - if (typeof b === 'function' && typeof Ctor.__typeson__type__ === 'string' && Ctor.__typeson__type__ === b.__typeson__type__) { - return true; - } - - return false; - } - /** - * - * @param {Any} val - * @returns {boolean} - */ - - - function isPlainObject(val) { - // Mirrors jQuery's - if (!val || toStringTag(val) !== 'Object') { - return false; - } - - var proto = getProto(val); - - if (!proto) { - // `Object.create(null)` - return true; - } - - return hasConstructorOf(val, Object); - } - /** - * - * @param {Any} val - * @returns {boolean} - */ - - - function isUserObject(val) { - if (!val || toStringTag(val) !== 'Object') { - return false; - } - - var proto = getProto(val); - - if (!proto) { - // `Object.create(null)` - return true; - } - - return hasConstructorOf(val, Object) || isUserObject(proto); - } - /** - * - * @param {Any} v - * @returns {boolean} - */ - - - function isObject(v) { - return v && _typeof(v) === 'object'; - } - /** - * - * @param {string} keyPathComponent - * @returns {string} - */ - - - function escapeKeyPathComponent(keyPathComponent) { - return keyPathComponent.replace(/~/g, '~0').replace(/\./g, '~1'); - } - /** - * - * @param {string} keyPathComponent - * @returns {string} - */ - - - function unescapeKeyPathComponent(keyPathComponent) { - return keyPathComponent.replace(/~1/g, '.').replace(/~0/g, '~'); - } - /** - * @param {PlainObject|GenericArray} obj - * @param {string} keyPath - * @returns {Any} - */ - - - function getByKeyPath(obj, keyPath) { - if (keyPath === '') { - return obj; - } - - var period = keyPath.indexOf('.'); - - if (period > -1) { - var innerObj = obj[unescapeKeyPathComponent(keyPath.slice(0, period))]; - return innerObj === undefined ? undefined : getByKeyPath(innerObj, keyPath.slice(period + 1)); - } - - return obj[unescapeKeyPathComponent(keyPath)]; - } - /** - * - * @param {PlainObject} obj - * @param {string} keyPath - * @param {Any} value - * @returns {Any} - */ - - - function setAtKeyPath(obj, keyPath, value) { - if (keyPath === '') { - return value; - } - - var period = keyPath.indexOf('.'); - - if (period > -1) { - var innerObj = obj[unescapeKeyPathComponent(keyPath.slice(0, period))]; - return setAtKeyPath(innerObj, keyPath.slice(period + 1), value); - } - - obj[unescapeKeyPathComponent(keyPath)] = value; - return obj; - } - /** - * - * @param {external:JSON} value - * @returns {"null"|"array"|"undefined"|"boolean"|"number"|"string"| - * "object"|"symbol"} - */ - - - function getJSONType(value) { - return value === null ? 'null' : Array.isArray(value) ? 'array' : _typeof(value); - } - - var keys = Object.keys, - isArray = Array.isArray, - hasOwn$1 = {}.hasOwnProperty, - internalStateObjPropsToIgnore = ['type', 'replaced', 'iterateIn', 'iterateUnsetNumeric']; - /** - * Handle plain object revivers first so reference setting can use - * revived type (e.g., array instead of object); assumes revived - * has same structure or will otherwise break subsequent references. - * @param {PlainObjectType} a - * @param {PlainObjectType} b - * @returns {1|-1|boolean} - */ - - function nestedPathsFirst(a, b) { - if (a.keypath === '') { - return -1; - } - - var as = a.keypath.match(/\./g) || 0; - var bs = b.keypath.match(/\./g) || 0; - - if (as) { - as = as.length; - } - - if (bs) { - bs = bs.length; - } - - return as > bs ? -1 : as < bs ? 1 : a.keypath < b.keypath ? -1 : a.keypath > b.keypath; - } - /** - * An instance of this class can be used to call `stringify()` and `parse()`. - * Typeson resolves cyclic references by default. Can also be extended to - * support custom types using the register() method. - * - * @class - * @param {{cyclic: boolean}} [options] - if cyclic (default true), - * cyclic references will be handled gracefully. - */ - - - var Typeson = - /*#__PURE__*/ - function () { - function Typeson(options) { - _classCallCheck(this, Typeson); - - this.options = options; // Replacers signature: replace (value). Returns falsy if not - // replacing. Otherwise ['Date', value.getTime()] - - this.plainObjectReplacers = []; - this.nonplainObjectReplacers = []; // Revivers: [{type => reviver}, {plain: boolean}]. - // Sample: [{'Date': value => new Date(value)}, {plain: false}] - - this.revivers = {}; - /** Types registered via `register()`. */ - - this.types = {}; - } - /** - * @typedef {null|boolean|number|string|GenericArray|PlainObject} JSON - */ - - /** - * @callback JSONReplacer - * @param {""|string} key - * @param {JSON} value - * @returns {number|string|boolean|null|PlainObject|undefined} - * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The%20replacer%20parameter - */ - - /** - * Serialize given object to Typeson. - * Initial arguments work identical to those of `JSON.stringify`. - * The `replacer` argument has nothing to do with our replacers. - * @param {Any} obj - * @param {JSONReplacer|string[]} replacer - * @param {number|string} space - * @param {object} opts - * @returns {string|Promise} Promise resolves to a string - */ - - - _createClass(Typeson, [{ - key: "stringify", - value: function stringify(obj, replacer, space, opts) { - opts = _objectSpread2({}, this.options, {}, opts, { - stringification: true - }); - var encapsulated = this.encapsulate(obj, null, opts); - - if (isArray(encapsulated)) { - return JSON.stringify(encapsulated[0], replacer, space); - } - - return encapsulated.then(function (res) { - return JSON.stringify(res, replacer, space); - }); - } - /** - * Also sync but throws on non-sync result. - * @param {Any} obj - * @param {JSONReplacer|string[]} replacer - * @param {number|string} space - * @param {object} opts - * @returns {string} - */ - - }, { - key: "stringifySync", - value: function stringifySync(obj, replacer, space, opts) { - return this.stringify(obj, replacer, space, _objectSpread2({ - throwOnBadSyncType: true - }, opts, { - sync: true - })); - } - /** - * - * @param {Any} obj - * @param {JSONReplacer|string[]} replacer - * @param {number|string} space - * @param {object} opts - * @returns {Promise} - */ - - }, { - key: "stringifyAsync", - value: function stringifyAsync(obj, replacer, space, opts) { - return this.stringify(obj, replacer, space, _objectSpread2({ - throwOnBadSyncType: true - }, opts, { - sync: false - })); - } - /** - * Parse Typeson back into an obejct. - * Initial arguments works identical to those of `JSON.parse()`. - * @param {string} text - * @param {function} reviver This JSON reviver has nothing to do with - * our revivers. - * @param {object} opts - * @returns {external:JSON} - */ - - }, { - key: "parse", - value: function parse(text, reviver, opts) { - opts = _objectSpread2({}, this.options, {}, opts, { - parse: true - }); - return this.revive(JSON.parse(text, reviver), opts); - } - /** - * Also sync but throws on non-sync result. - * @param {string} text - * @param {function} reviver This JSON reviver has nothing to do with - * our revivers. - * @param {object} opts - * @returns {external:JSON} - */ - - }, { - key: "parseSync", - value: function parseSync(text, reviver, opts) { - return this.parse(text, reviver, _objectSpread2({ - throwOnBadSyncType: true - }, opts, { - sync: true - })); - } - /** - * @param {string} text - * @param {function} reviver This JSON reviver has nothing to do with - * our revivers. - * @param {object} opts - * @returns {Promise} Resolves to `external:JSON` - */ - - }, { - key: "parseAsync", - value: function parseAsync(text, reviver, opts) { - return this.parse(text, reviver, _objectSpread2({ - throwOnBadSyncType: true - }, opts, { - sync: false - })); - } - /** - * - * @param {Any} obj - * @param {object} stateObj - * @param {object} [opts={}] - * @returns {string[]|false} - */ - - }, { - key: "specialTypeNames", - value: function specialTypeNames(obj, stateObj) { - var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; - opts.returnTypeNames = true; - return this.encapsulate(obj, stateObj, opts); - } - /** - * - * @param {Any} obj - * @param {PlainObject} stateObj - * @param {PlainObject} [opts={}] - * @returns {Promise|GenericArray|PlainObject|string|false} - */ - - }, { - key: "rootTypeName", - value: function rootTypeName(obj, stateObj) { - var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; - opts.iterateNone = true; - return this.encapsulate(obj, stateObj, opts); - } - /** - * Encapsulate a complex object into a plain Object by replacing - * registered types with plain objects representing the types data. - * - * This method is used internally by `Typeson.stringify()`. - * @param {Any} obj - Object to encapsulate. - * @param {PlainObject} stateObj - * @param {PlainObject} opts - * @returns {Promise|GenericArray|PlainObject|string|false} - */ - - }, { - key: "encapsulate", - value: function encapsulate(obj, stateObj, opts) { - opts = _objectSpread2({ - sync: true - }, this.options, {}, opts); - var _opts = opts, - sync = _opts.sync; - var that = this, - types = {}, - refObjs = [], - // For checking cyclic references - refKeys = [], - // For checking cyclic references - promisesDataRoot = []; // Clone the object deeply while at the same time replacing any - // special types or cyclic reference: - - var cyclic = 'cyclic' in opts ? opts.cyclic : true; - var _opts2 = opts, - encapsulateObserver = _opts2.encapsulateObserver; - - var ret = _encapsulate('', obj, cyclic, stateObj || {}, promisesDataRoot); - /** - * - * @param {Any} ret - * @returns {GenericArray|PlainObject|string|false} - */ - - - function finish(ret) { - // Add `$types` to result only if we ever bumped into a - // special type (or special case where object has own `$types`) - var typeNames = Object.values(types); - - if (opts.iterateNone) { - if (typeNames.length) { - return typeNames[0]; - } - - return Typeson.getJSONType(ret); - } - - if (typeNames.length) { - if (opts.returnTypeNames) { - return _toConsumableArray(new Set(typeNames)); - } // Special if array (or a primitive) was serialized - // because JSON would ignore custom `$types` prop on it - - - if (!ret || !isPlainObject(ret) || // Also need to handle if this is an object with its - // own `$types` property (to avoid ambiguity) - hasOwn$1.call(ret, '$types')) { - ret = { - $: ret, - $types: { - $: types - } - }; - } else { - ret.$types = types; - } // No special types - - } else if (isObject(ret) && hasOwn$1.call(ret, '$types')) { - ret = { - $: ret, - $types: true - }; - } - - if (opts.returnTypeNames) { - return false; - } - - return ret; - } - /** - * - * @param {Any} ret - * @param {GenericArray} promisesData - * @returns {Promise} - */ - - - function checkPromises(_x, _x2) { - return _checkPromises.apply(this, arguments); - } - /** - * - * @param {object} stateObj - * @param {object} ownKeysObj - * @param {function} cb - * @returns {undefined} - */ - - - function _checkPromises() { - _checkPromises = _asyncToGenerator( - /*#__PURE__*/ - regeneratorRuntime.mark(function _callee2(ret, promisesData) { - var promResults; - return regeneratorRuntime.wrap(function _callee2$(_context2) { - while (1) { - switch (_context2.prev = _context2.next) { - case 0: - _context2.next = 2; - return Promise.all(promisesData.map(function (pd) { - return pd[1].p; - })); - - case 2: - promResults = _context2.sent; - _context2.next = 5; - return Promise.all(promResults.map( - /*#__PURE__*/ - function () { - var _ref = _asyncToGenerator( - /*#__PURE__*/ - regeneratorRuntime.mark(function _callee(promResult) { - var newPromisesData, _promisesData$splice, _promisesData$splice2, prData, _prData, keyPath, cyclic, stateObj, parentObj, key, detectedType, encaps, isTypesonPromise, encaps2; - - return regeneratorRuntime.wrap(function _callee$(_context) { - while (1) { - switch (_context.prev = _context.next) { - case 0: - newPromisesData = []; - _promisesData$splice = promisesData.splice(0, 1), _promisesData$splice2 = _slicedToArray(_promisesData$splice, 1), prData = _promisesData$splice2[0]; - _prData = _slicedToArray(prData, 7), keyPath = _prData[0], cyclic = _prData[2], stateObj = _prData[3], parentObj = _prData[4], key = _prData[5], detectedType = _prData[6]; - encaps = _encapsulate(keyPath, promResult, cyclic, stateObj, newPromisesData, true, detectedType); - isTypesonPromise = hasConstructorOf(encaps, TypesonPromise); // Handle case where an embedded custom type itself - // returns a `Typeson.Promise` - - if (!(keyPath && isTypesonPromise)) { - _context.next = 11; - break; - } - - _context.next = 8; - return encaps.p; - - case 8: - encaps2 = _context.sent; - parentObj[key] = encaps2; - return _context.abrupt("return", checkPromises(ret, newPromisesData)); - - case 11: - if (keyPath) { - parentObj[key] = encaps; - } else if (isTypesonPromise) { - ret = encaps.p; - } else { - // If this is itself a `Typeson.Promise` (because the - // original value supplied was a `Promise` or - // because the supplied custom type value resolved - // to one), returning it below will be fine since - // a `Promise` is expected anyways given current - // config (and if not a `Promise`, it will be ready - // as the resolve value) - ret = encaps; - } - - return _context.abrupt("return", checkPromises(ret, newPromisesData)); - - case 13: - case "end": - return _context.stop(); - } - } - }, _callee); - })); - - return function (_x3) { - return _ref.apply(this, arguments); - }; - }())); - - case 5: - return _context2.abrupt("return", ret); - - case 6: - case "end": - return _context2.stop(); - } - } - }, _callee2); - })); - return _checkPromises.apply(this, arguments); - } - - function _adaptBuiltinStateObjectProperties(stateObj, ownKeysObj, cb) { - Object.assign(stateObj, ownKeysObj); - var vals = internalStateObjPropsToIgnore.map(function (prop) { - var tmp = stateObj[prop]; - delete stateObj[prop]; - return tmp; - }); // eslint-disable-next-line callback-return - - cb(); - internalStateObjPropsToIgnore.forEach(function (prop, i) { - stateObj[prop] = vals[i]; - }); - } - /** - * - * @param {string} keypath - * @param {Any} value - * @param {boolean} cyclic - * @param {PlainObject} stateObj - * @param {boolean} promisesData - * @param {boolean} resolvingTypesonPromise - * @param {string} detectedType - * @returns {Any} - */ - - - function _encapsulate(keypath, value, cyclic, stateObj, promisesData, resolvingTypesonPromise, detectedType) { - var ret; - var observerData = {}; - - var $typeof = _typeof(value); - - var runObserver = encapsulateObserver ? function (obj) { - var type = detectedType || stateObj.type || Typeson.getJSONType(value); - encapsulateObserver(Object.assign(obj || observerData, { - keypath: keypath, - value: value, - cyclic: cyclic, - stateObj: stateObj, - promisesData: promisesData, - resolvingTypesonPromise: resolvingTypesonPromise, - awaitingTypesonPromise: hasConstructorOf(value, TypesonPromise) - }, { - type: type - })); - } : null; - - if (['string', 'boolean', 'number', 'undefined'].includes($typeof)) { - if (value === undefined || $typeof === 'number' && (isNaN(value) || value === -Infinity || value === Infinity)) { - if (stateObj.replaced) { - ret = value; - } else { - ret = replace(keypath, value, stateObj, promisesData, false, resolvingTypesonPromise, runObserver); - } - - if (ret !== value) { - observerData = { - replaced: ret - }; - } - } else { - ret = value; - } - - if (runObserver) { - runObserver(); - } - - return ret; - } - - if (value === null) { - if (runObserver) { - runObserver(); - } - - return value; - } - - if (cyclic && !stateObj.iterateIn && !stateObj.iterateUnsetNumeric && value && _typeof(value) === 'object') { - // Options set to detect cyclic references and be able - // to rewrite them. - var refIndex = refObjs.indexOf(value); - - if (refIndex < 0) { - if (cyclic === true) { - refObjs.push(value); - refKeys.push(keypath); - } - } else { - types[keypath] = '#'; - - if (runObserver) { - runObserver({ - cyclicKeypath: refKeys[refIndex] - }); - } - - return '#' + refKeys[refIndex]; - } - } - - var isPlainObj = isPlainObject(value); - var isArr = isArray(value); - var replaced = // Running replace will cause infinite loop as will test - // positive again - (isPlainObj || isArr) && (!that.plainObjectReplacers.length || stateObj.replaced) || stateObj.iterateIn ? // Optimization: if plain object and no plain-object - // replacers, don't try finding a replacer - value : replace(keypath, value, stateObj, promisesData, isPlainObj || isArr, null, runObserver); - var clone; - - if (replaced !== value) { - ret = replaced; - observerData = { - replaced: replaced - }; - } else { - // eslint-disable-next-line no-lonely-if - if (keypath === '' && hasConstructorOf(value, TypesonPromise)) { - promisesData.push([keypath, value, cyclic, stateObj, undefined, undefined, stateObj.type]); - ret = value; - } else if (isArr && stateObj.iterateIn !== 'object' || stateObj.iterateIn === 'array') { - clone = new Array(value.length); - observerData = { - clone: clone - }; - } else if (!['function', 'symbol'].includes(_typeof(value)) && !('toJSON' in value) && !hasConstructorOf(value, TypesonPromise) && !hasConstructorOf(value, Promise) && !hasConstructorOf(value, ArrayBuffer) || isPlainObj || stateObj.iterateIn === 'object') { - clone = {}; - - if (stateObj.addLength) { - clone.length = value.length; - } - - observerData = { - clone: clone - }; - } else { - ret = value; // Only clone vanilla objects and arrays - } - } - - if (runObserver) { - runObserver(); - } - - if (opts.iterateNone) { - return clone || ret; - } - - if (!clone) { - return ret; - } // Iterate object or array - - - if (stateObj.iterateIn) { - var _loop = function _loop(key) { - var ownKeysObj = { - ownKeys: hasOwn$1.call(value, key) - }; - - _adaptBuiltinStateObjectProperties(stateObj, ownKeysObj, function () { - var kp = keypath + (keypath ? '.' : '') + escapeKeyPathComponent(key); - - var val = _encapsulate(kp, value[key], Boolean(cyclic), stateObj, promisesData, resolvingTypesonPromise); - - if (hasConstructorOf(val, TypesonPromise)) { - promisesData.push([kp, val, Boolean(cyclic), stateObj, clone, key, stateObj.type]); - } else if (val !== undefined) { - clone[key] = val; - } - }); - }; - - // eslint-disable-next-line guard-for-in - for (var key in value) { - _loop(key); - } - - if (runObserver) { - runObserver({ - endIterateIn: true, - end: true - }); - } - } else { - // Note: Non-indexes on arrays won't survive stringify so - // somewhat wasteful for arrays, but so too is iterating - // all numeric indexes on sparse arrays when not wanted - // or filtering own keys for positive integers - keys(value).forEach(function (key) { - var kp = keypath + (keypath ? '.' : '') + escapeKeyPathComponent(key); - var ownKeysObj = { - ownKeys: true - }; - - _adaptBuiltinStateObjectProperties(stateObj, ownKeysObj, function () { - var val = _encapsulate(kp, value[key], Boolean(cyclic), stateObj, promisesData, resolvingTypesonPromise); - - if (hasConstructorOf(val, TypesonPromise)) { - promisesData.push([kp, val, Boolean(cyclic), stateObj, clone, key, stateObj.type]); - } else if (val !== undefined) { - clone[key] = val; - } - }); - }); - - if (runObserver) { - runObserver({ - endIterateOwn: true, - end: true - }); - } - } // Iterate array for non-own numeric properties (we can't - // replace the prior loop though as it iterates non-integer - // keys) - - - if (stateObj.iterateUnsetNumeric) { - var vl = value.length; - - var _loop2 = function _loop2(i) { - if (!(i in value)) { - // No need to escape numeric - var kp = keypath + (keypath ? '.' : '') + i; - var ownKeysObj = { - ownKeys: false - }; - - _adaptBuiltinStateObjectProperties(stateObj, ownKeysObj, function () { - var val = _encapsulate(kp, undefined, Boolean(cyclic), stateObj, promisesData, resolvingTypesonPromise); - - if (hasConstructorOf(val, TypesonPromise)) { - promisesData.push([kp, val, Boolean(cyclic), stateObj, clone, i, stateObj.type]); - } else if (val !== undefined) { - clone[i] = val; - } - }); - } - }; - - for (var i = 0; i < vl; i++) { - _loop2(i); - } - - if (runObserver) { - runObserver({ - endIterateUnsetNumeric: true, - end: true - }); - } - } - - return clone; - } - /** - * - * @param {string} keypath - * @param {Any} value - * @param {PlainObject} stateObj - * @param {GenericArray} promisesData - * @param {boolean} plainObject - * @param {boolean} resolvingTypesonPromise - * @param {function} [runObserver] - * @returns {*} - */ - - - function replace(keypath, value, stateObj, promisesData, plainObject, resolvingTypesonPromise, runObserver) { - // Encapsulate registered types - var replacers = plainObject ? that.plainObjectReplacers : that.nonplainObjectReplacers; - var i = replacers.length; - - while (i--) { - var replacer = replacers[i]; - - if (replacer.test(value, stateObj)) { - var type = replacer.type; - - if (that.revivers[type]) { - // Record the type only if a corresponding reviver - // exists. This is to support specs where only - // replacement is done. - // For example, ensuring deep cloning of the object, - // or replacing a type to its equivalent without - // the need to revive it. - var existing = types[keypath]; // type can comprise an array of types (see test - // "should support intermediate types") - - types[keypath] = existing ? [type].concat(existing) : type; - } - - Object.assign(stateObj, { - type: type, - replaced: true - }); - - if ((sync || !replacer.replaceAsync) && !replacer.replace) { - if (runObserver) { - runObserver({ - typeDetected: true - }); - } - - return _encapsulate(keypath, value, cyclic && 'readonly', stateObj, promisesData, resolvingTypesonPromise, type); - } - - if (runObserver) { - runObserver({ - replacing: true - }); - } // Now, also traverse the result in case it contains its - // own types to replace - - - var replaceMethod = sync || !replacer.replaceAsync ? 'replace' : 'replaceAsync'; - return _encapsulate(keypath, replacer[replaceMethod](value, stateObj), cyclic && 'readonly', stateObj, promisesData, resolvingTypesonPromise, type); - } - } - - return value; - } - - return promisesDataRoot.length ? sync && opts.throwOnBadSyncType ? function () { - throw new TypeError('Sync method requested but async result obtained'); - }() : Promise.resolve(checkPromises(ret, promisesDataRoot)).then(finish) : !sync && opts.throwOnBadSyncType ? function () { - throw new TypeError('Async method requested but sync result obtained'); - }() // If this is a synchronous request for stringification, yet - // a promise is the result, we don't want to resolve leading - // to an async result, so we return an array to avoid - // ambiguity - : opts.stringification && sync ? [finish(ret)] : sync ? finish(ret) : Promise.resolve(finish(ret)); - } - /** - * Also sync but throws on non-sync result. - * @param {*} obj - * @param {object} stateObj - * @param {object} opts - * @returns {*} - */ - - }, { - key: "encapsulateSync", - value: function encapsulateSync(obj, stateObj, opts) { - return this.encapsulate(obj, stateObj, _objectSpread2({ - throwOnBadSyncType: true - }, opts, { - sync: true - })); - } - /** - * @param {*} obj - * @param {object} stateObj - * @param {object} opts - * @returns {*} - */ - - }, { - key: "encapsulateAsync", - value: function encapsulateAsync(obj, stateObj, opts) { - return this.encapsulate(obj, stateObj, _objectSpread2({ - throwOnBadSyncType: true - }, opts, { - sync: false - })); - } - /** - * Revive an encapsulated object. - * This method is used internally by `Typeson.parse()`. - * @param {object} obj - Object to revive. If it has `$types` member, the - * properties that are listed there will be replaced with its true type - * instead of just plain objects. - * @param {object} opts - * @throws TypeError If mismatch between sync/async type and result - * @returns {Promise|*} If async, returns a Promise that resolves to `*` - */ - - }, { - key: "revive", - value: function revive(obj, opts) { - var types = obj && obj.$types; // No type info added. Revival not needed. - - if (!types) { - return obj; - } // Object happened to have own `$types` property but with - // no actual types, so we unescape and return that object - - - if (types === true) { - return obj.$; - } - - opts = _objectSpread2({ - sync: true - }, this.options, {}, opts); - var _opts3 = opts, - sync = _opts3.sync; - var keyPathResolutions = []; - var stateObj = {}; - var ignore$Types = true; // Special when root object is not a trivial Object, it will - // be encapsulated in `$`. It will also be encapsulated in - // `$` if it has its own `$` property to avoid ambiguity - - if (types.$ && isPlainObject(types.$)) { - obj = obj.$; - types = types.$; - ignore$Types = false; - } - - var that = this; - /** - * @callback RevivalReducer - * @param {Any} value - * @param {string} type - * @returns {Any} - */ - - /** - * - * @param {string} type - * @param {Any} val - * @returns {[type]} [description] - */ - - function executeReviver(type, val) { - var _ref2 = that.revivers[type] || [], - _ref3 = _slicedToArray(_ref2, 1), - reviver = _ref3[0]; - - if (!reviver) { - throw new Error('Unregistered type: ' + type); - } // Only `sync` expected here, as problematic async would - // be missing both `reviver` and `reviverAsync`, and - // encapsulation shouldn't have added types, so - // should have made an early exit - - - if (sync && !('revive' in reviver)) { - // Just return value as is - return val; - } - - return reviver[sync && reviver.revive ? 'revive' : !sync && reviver.reviveAsync ? 'reviveAsync' : 'revive'](val, stateObj); - } - /** - * - * @returns {void|TypesonPromise} - */ - - - function revivePlainObjects() { - // const references = []; - // const reviveTypes = []; - var plainObjectTypes = []; - Object.entries(types).forEach(function (_ref4) { - var _ref5 = _slicedToArray(_ref4, 2), - keypath = _ref5[0], - type = _ref5[1]; - - if (type === '#') { - /* - references.push({ - keypath, - reference: getByKeyPath(obj, keypath) - }); - */ - return; - } - - [].concat(type).forEach(function (type) { - var _ref6 = that.revivers[type] || [null, {}], - _ref7 = _slicedToArray(_ref6, 2), - plain = _ref7[1].plain; - - if (!plain) { - // reviveTypes.push({keypath, type}); - return; - } - - plainObjectTypes.push({ - keypath: keypath, - type: type - }); - delete types[keypath]; // Avoid repeating - }); - }); - - if (!plainObjectTypes.length) { - return undefined; - } // console.log(plainObjectTypes.sort(nestedPathsFirst)); - - /** - * @typedef {PlainObject} PlainObjectType - * @property {string} keypath - * @property {string} type - */ - - - return plainObjectTypes.sort(nestedPathsFirst).reduce(function reducer(possibleTypesonPromise, _ref8) { - var keypath = _ref8.keypath, - type = _ref8.type; - - if (isThenable(possibleTypesonPromise)) { - return possibleTypesonPromise.then(function (val) { - return reducer(val, { - keypath: keypath, - type: type - }); - }); - } // console.log('obj', JSON.stringify(keypath), obj); - - - var val = getByKeyPath(obj, keypath); - val = executeReviver(type, val); - - if (hasConstructorOf(val, TypesonPromise)) { - return val.then(function (v) { - var newVal = setAtKeyPath(obj, keypath, v); - - if (newVal === v) { - obj = newVal; - } - - return undefined; - }); - } - - var newVal = setAtKeyPath(obj, keypath, val); - - if (newVal === val) { - obj = newVal; - } - - return undefined; - }, undefined // This argument must be explicit - ); // references.forEach(({keypath, reference}) => {}); - // reviveTypes.sort(nestedPathsFirst).forEach(() => {}); - } - - var revivalPromises = []; - /** - * - * @param {string} keypath - * @param {Any} value - * @param {?(Array|object)} target - * @param {Array|object} [clone] - * @param {string} [key] - * @returns {Any} - */ - - function _revive(keypath, value, target, clone, key) { - if (ignore$Types && keypath === '$types') { - return undefined; - } - - var type = types[keypath]; - var isArr = isArray(value); - - if (isArr || isPlainObject(value)) { - var _clone = isArr ? new Array(value.length) : {}; // Iterate object or array - - - keys(value).forEach(function (k) { - var val = _revive(keypath + (keypath ? '.' : '') + escapeKeyPathComponent(k), value[k], target || _clone, _clone, k); - - var set = function set(v) { - if (hasConstructorOf(v, Undefined)) { - _clone[k] = undefined; - } else if (v !== undefined) { - _clone[k] = v; - } - - return v; - }; - - if (hasConstructorOf(val, TypesonPromise)) { - revivalPromises.push(val.then(function (ret) { - return set(ret); - })); - } else { - set(val); - } - }); - value = _clone; // Try to resolve cyclic reference as soon as available - - while (keyPathResolutions.length) { - var _keyPathResolutions$ = _slicedToArray(keyPathResolutions[0], 4), - _target = _keyPathResolutions$[0], - keyPath = _keyPathResolutions$[1], - _clone2 = _keyPathResolutions$[2], - k = _keyPathResolutions$[3]; - - var val = getByKeyPath(_target, keyPath); // Typeson.Undefined not expected here as not cyclic or - // `undefined` - - if (val !== undefined) { - _clone2[k] = val; - } else { - break; - } - - keyPathResolutions.splice(0, 1); - } - } - - if (!type) { - return value; - } - - if (type === '#') { - var _ret = getByKeyPath(target, value.slice(1)); - - if (_ret === undefined) { - // Cyclic reference not yet available - keyPathResolutions.push([target, value.slice(1), clone, key]); - } - - return _ret; - } // `type` can be an array here - - - return [].concat(type).reduce(function reducer(val, typ) { - if (hasConstructorOf(val, TypesonPromise)) { - return val.then(function (v) { - // TypesonPromise here too - return reducer(v, typ); - }); - } - - return executeReviver(typ, val); - }, value); - } - /** - * - * @param {Any} retrn - * @returns {undefined|Any} - */ - - - function checkUndefined(retrn) { - return hasConstructorOf(retrn, Undefined) ? undefined : retrn; - } - - var possibleTypesonPromise = revivePlainObjects(); - var ret; - - if (hasConstructorOf(possibleTypesonPromise, TypesonPromise)) { - ret = possibleTypesonPromise.then(function () { - return obj; - }); - } else { - ret = _revive('', obj, null); - - if (revivalPromises.length) { - // Ensure children resolved - ret = TypesonPromise.resolve(ret).then(function (r) { - return TypesonPromise.all([// May be a TypesonPromise or not - r].concat(revivalPromises)); - }).then(function (_ref9) { - var _ref10 = _slicedToArray(_ref9, 1), - r = _ref10[0]; - - return r; - }); - } - } - - return isThenable(ret) ? sync && opts.throwOnBadSyncType ? function () { - throw new TypeError('Sync method requested but async result obtained'); - }() : hasConstructorOf(ret, TypesonPromise) ? ret.p.then(checkUndefined) : ret : !sync && opts.throwOnBadSyncType ? function () { - throw new TypeError('Async method requested but sync result obtained'); - }() : sync ? checkUndefined(ret) : Promise.resolve(checkUndefined(ret)); - } - /** - * Also sync but throws on non-sync result. - * @param {Any} obj - * @param {object} opts - * @returns {Any} - */ - - }, { - key: "reviveSync", - value: function reviveSync(obj, opts) { - return this.revive(obj, _objectSpread2({ - throwOnBadSyncType: true - }, opts, { - sync: true - })); - } - /** - * @param {Any} obj - * @param {object} opts - * @returns {Promise} Resolves to `*` - */ - - }, { - key: "reviveAsync", - value: function reviveAsync(obj, opts) { - return this.revive(obj, _objectSpread2({ - throwOnBadSyncType: true - }, opts, { - sync: false - })); - } - /** - * Register types. - * For examples on how to use this method, see - * {@link https://github.com/dfahlander/typeson-registry/tree/master/types}. - * @param {object.[]} typeSpecSets - Types and - * their functions [test, encapsulate, revive]; - * @param {object} opts - * @returns {Typeson} - */ - - }, { - key: "register", - value: function register(typeSpecSets, opts) { - opts = opts || {}; - [].concat(typeSpecSets).forEach(function R(typeSpec) { - var _this = this; - - // Allow arrays of arrays of arrays... - if (isArray(typeSpec)) { - return typeSpec.map(function (typSpec) { - return R.call(_this, typSpec); - }); - } - - typeSpec && keys(typeSpec).forEach(function (typeId) { - if (typeId === '#') { - throw new TypeError('# cannot be used as a type name as it is reserved ' + 'for cyclic objects'); - } else if (Typeson.JSON_TYPES.includes(typeId)) { - throw new TypeError('Plain JSON object types are reserved as type names'); - } - - var spec = typeSpec[typeId]; - var replacers = spec && spec.testPlainObjects ? this.plainObjectReplacers : this.nonplainObjectReplacers; - var existingReplacer = replacers.filter(function (r) { - return r.type === typeId; - }); - - if (existingReplacer.length) { - // Remove existing spec and replace with this one. - replacers.splice(replacers.indexOf(existingReplacer[0]), 1); - delete this.revivers[typeId]; - delete this.types[typeId]; - } - - if (typeof spec === 'function') { - // Support registering just a class without replacer/reviver - var Class = spec; - spec = { - test: function test(x) { - return x && x.constructor === Class; - }, - replace: function replace(x) { - return _objectSpread2({}, x); - }, - revive: function revive(x) { - return Object.assign(Object.create(Class.prototype), x); - } - }; - } else if (isArray(spec)) { - var _spec = spec, - _spec2 = _slicedToArray(_spec, 3), - test = _spec2[0], - replace = _spec2[1], - revive = _spec2[2]; - - spec = { - test: test, - replace: replace, - revive: revive - }; - } - - if (!spec || !spec.test) { - return; - } - - var replacerObj = { - type: typeId, - test: spec.test.bind(spec) - }; - - if (spec.replace) { - replacerObj.replace = spec.replace.bind(spec); - } - - if (spec.replaceAsync) { - replacerObj.replaceAsync = spec.replaceAsync.bind(spec); - } - - var start = typeof opts.fallback === 'number' ? opts.fallback : opts.fallback ? 0 : Infinity; - - if (spec.testPlainObjects) { - this.plainObjectReplacers.splice(start, 0, replacerObj); - } else { - this.nonplainObjectReplacers.splice(start, 0, replacerObj); - } // Todo: We might consider a testAsync type - - - if (spec.revive || spec.reviveAsync) { - var reviverObj = {}; - - if (spec.revive) { - reviverObj.revive = spec.revive.bind(spec); - } - - if (spec.reviveAsync) { - reviverObj.reviveAsync = spec.reviveAsync.bind(spec); - } - - this.revivers[typeId] = [reviverObj, { - plain: spec.testPlainObjects - }]; - } // Record to be retrieved via public types property. - - - this.types[typeId] = spec; - }, this); - }, this); - return this; - } - }]); - - return Typeson; - }(); - /** - * We keep this function minimized so if using two instances of this - * library, where one is minimized and one is not, it will still work - * with `hasConstructorOf`. - * @class - */ - - - var Undefined = function Undefined() { - _classCallCheck(this, Undefined); - }; // eslint-disable-line space-before-blocks - - - Undefined.__typeson__type__ = 'TypesonUndefined'; // The following provide classes meant to avoid clashes with other values - // To insist `undefined` should be added - - Typeson.Undefined = Undefined; // To support async encapsulation/stringification - - Typeson.Promise = TypesonPromise; // Some fundamental type-checking utilities - - Typeson.isThenable = isThenable; - Typeson.toStringTag = toStringTag; - Typeson.hasConstructorOf = hasConstructorOf; - Typeson.isObject = isObject; - Typeson.isPlainObject = isPlainObject; - Typeson.isUserObject = isUserObject; - Typeson.escapeKeyPathComponent = escapeKeyPathComponent; - Typeson.unescapeKeyPathComponent = unescapeKeyPathComponent; - Typeson.getByKeyPath = getByKeyPath; - Typeson.getJSONType = getJSONType; - Typeson.JSON_TYPES = ['null', 'boolean', 'number', 'string', 'array', 'object']; - - return Typeson; - - }))); - }); - - var structuredCloning = createCommonjsModule(function (module, exports) { - !function(e,t){module.exports=t();}(commonjsGlobal,(function(){function _typeof$1(e){return (_typeof$1="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _classCallCheck$1(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties$1(e,t){for(var r=0;re.length)&&(t=e.length);for(var r=0,n=new Array(t);re.length)&&(t=e.length);for(var r=0,n=new Array(t);r-1){var n=e[unescapeKeyPathComponent(t.slice(0,r))];return void 0===n?void 0:getByKeyPath(n,t.slice(r+1))}return e[unescapeKeyPathComponent(t)]}function setAtKeyPath(e,t,r){if(""===t)return r;var n=t.indexOf(".");return n>-1?setAtKeyPath(e[unescapeKeyPathComponent(t.slice(0,n))],t.slice(n+1),r):(e[unescapeKeyPathComponent(t)]=r,e)}function _await(e,t,r){return r?t?t(e):e:(e&&e.then||(e=Promise.resolve(e)),t?e.then(t):e)}var o=Object.keys,a=Array.isArray,c={}.hasOwnProperty,u=["type","replaced","iterateIn","iterateUnsetNumeric"];function _async(e){return function(){for(var t=[],r=0;rn?-1:rt.keypath}var s=function(){function Typeson(e){_classCallCheck(this,Typeson),this.options=e,this.plainObjectReplacers=[],this.nonplainObjectReplacers=[],this.revivers={},this.types={};}return function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),e}(Typeson,[{key:"stringify",value:function stringify(e,t,r,n){n=_objectSpread2(_objectSpread2(_objectSpread2({},this.options),n),{},{stringification:!0});var i=this.encapsulate(e,null,n);return a(i)?JSON.stringify(i[0],t,r):i.then((function(e){return JSON.stringify(e,t,r)}))}},{key:"stringifySync",value:function stringifySync(e,t,r,n){return this.stringify(e,t,r,_objectSpread2(_objectSpread2({throwOnBadSyncType:!0},n),{},{sync:!0}))}},{key:"stringifyAsync",value:function stringifyAsync(e,t,r,n){return this.stringify(e,t,r,_objectSpread2(_objectSpread2({throwOnBadSyncType:!0},n),{},{sync:!1}))}},{key:"parse",value:function parse(e,t,r){return r=_objectSpread2(_objectSpread2(_objectSpread2({},this.options),r),{},{parse:!0}),this.revive(JSON.parse(e,t),r)}},{key:"parseSync",value:function parseSync(e,t,r){return this.parse(e,t,_objectSpread2(_objectSpread2({throwOnBadSyncType:!0},r),{},{sync:!0}))}},{key:"parseAsync",value:function parseAsync(e,t,r){return this.parse(e,t,_objectSpread2(_objectSpread2({throwOnBadSyncType:!0},r),{},{sync:!1}))}},{key:"specialTypeNames",value:function specialTypeNames(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return r.returnTypeNames=!0,this.encapsulate(e,t,r)}},{key:"rootTypeName",value:function rootTypeName(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return r.iterateNone=!0,this.encapsulate(e,t,r)}},{key:"encapsulate",value:function encapsulate(t,r,n){var i=_async((function(t,r){return _await(Promise.all(r.map((function(e){return e[1].p}))),(function(n){return _await(Promise.all(n.map(_async((function(n){var o=!1,a=[],c=_slicedToArray(r.splice(0,1),1),u=_slicedToArray(c[0],7),s=u[0],f=u[2],l=u[3],p=u[4],y=u[5],v=u[6],b=_encapsulate(s,n,f,l,a,!0,v),d=hasConstructorOf(b,e);return function _invoke(e,t){var r=e();return r&&r.then?r.then(t):t(r)}((function(){if(s&&d)return _await(b.p,(function(e){return p[y]=e,o=!0,i(t,a)}))}),(function(e){return o?e:(s?p[y]=b:t=d?b.p:b,i(t,a))}))})))),(function(){return t}))}))})),s=(n=_objectSpread2(_objectSpread2({sync:!0},this.options),n)).sync,f=this,l={},p=[],y=[],v=[],b=!("cyclic"in n)||n.cyclic,d=n.encapsulateObserver,h=_encapsulate("",t,b,r||{},v);function finish(e){var t=Object.values(l);if(n.iterateNone)return t.length?t[0]:Typeson.getJSONType(e);if(t.length){if(n.returnTypeNames)return _toConsumableArray(new Set(t));e&&isPlainObject(e)&&!c.call(e,"$types")?e.$types=l:e={$:e,$types:{$:l}};}else isObject(e)&&c.call(e,"$types")&&(e={$:e,$types:!0});return !n.returnTypeNames&&e}function _adaptBuiltinStateObjectProperties(e,t,r){Object.assign(e,t);var n=u.map((function(t){var r=e[t];return delete e[t],r}));r(),u.forEach((function(t,r){e[t]=n[r];}));}function _encapsulate(t,r,i,u,s,v,b){var h,g={},m=_typeof(r),O=d?function(n){var o=b||u.type||Typeson.getJSONType(r);d(Object.assign(n||g,{keypath:t,value:r,cyclic:i,stateObj:u,promisesData:s,resolvingTypesonPromise:v,awaitingTypesonPromise:hasConstructorOf(r,e)},{type:o}));}:null;if(["string","boolean","number","undefined"].includes(m))return void 0===r||Number.isNaN(r)||r===Number.NEGATIVE_INFINITY||r===Number.POSITIVE_INFINITY?(h=u.replaced?r:replace(t,r,u,s,!1,v,O))!==r&&(g={replaced:h}):h=r,O&&O(),h;if(null===r)return O&&O(),r;if(i&&!u.iterateIn&&!u.iterateUnsetNumeric&&r&&"object"===_typeof(r)){var _=p.indexOf(r);if(!(_<0))return l[t]="#",O&&O({cyclicKeypath:y[_]}),"#"+y[_];!0===i&&(p.push(r),y.push(t));}var j,S=isPlainObject(r),T=a(r),w=(S||T)&&(!f.plainObjectReplacers.length||u.replaced)||u.iterateIn?r:replace(t,r,u,s,S||T,null,O);if(w!==r?(h=w,g={replaced:w}):""===t&&hasConstructorOf(r,e)?(s.push([t,r,i,u,void 0,void 0,u.type]),h=r):T&&"object"!==u.iterateIn||"array"===u.iterateIn?(j=new Array(r.length),g={clone:j}):(["function","symbol"].includes(_typeof(r))||"toJSON"in r||hasConstructorOf(r,e)||hasConstructorOf(r,Promise)||hasConstructorOf(r,ArrayBuffer))&&!S&&"object"!==u.iterateIn?h=r:(j={},u.addLength&&(j.length=r.length),g={clone:j}),O&&O(),n.iterateNone)return j||h;if(!j)return h;if(u.iterateIn){var A=function _loop(n){var o={ownKeys:c.call(r,n)};_adaptBuiltinStateObjectProperties(u,o,(function(){var o=t+(t?".":"")+escapeKeyPathComponent(n),a=_encapsulate(o,r[n],Boolean(i),u,s,v);hasConstructorOf(a,e)?s.push([o,a,Boolean(i),u,j,n,u.type]):void 0!==a&&(j[n]=a);}));};for(var P in r)A(P);O&&O({endIterateIn:!0,end:!0});}else o(r).forEach((function(n){var o=t+(t?".":"")+escapeKeyPathComponent(n);_adaptBuiltinStateObjectProperties(u,{ownKeys:!0},(function(){var t=_encapsulate(o,r[n],Boolean(i),u,s,v);hasConstructorOf(t,e)?s.push([o,t,Boolean(i),u,j,n,u.type]):void 0!==t&&(j[n]=t);}));})),O&&O({endIterateOwn:!0,end:!0});if(u.iterateUnsetNumeric){for(var I=r.length,C=function _loop2(n){if(!(n in r)){var o=t+(t?".":"")+n;_adaptBuiltinStateObjectProperties(u,{ownKeys:!1},(function(){var t=_encapsulate(o,void 0,Boolean(i),u,s,v);hasConstructorOf(t,e)?s.push([o,t,Boolean(i),u,j,n,u.type]):void 0!==t&&(j[n]=t);}));}},N=0;N>2],o+=O[(3&n[a])<<4|n[a+1]>>4],o+=O[(15&n[a+1])<<2|n[a+2]>>6],o+=O[63&n[a+2]];return i%3==2?o=o.slice(0,-1)+"=":i%3==1&&(o=o.slice(0,-2)+"=="),o},T=function decode(e){var t,r,n,i,o=e.length,a=.75*e.length,c=0;"="===e[e.length-1]&&(a--,"="===e[e.length-2]&&a--);for(var u=new ArrayBuffer(a),s=new Uint8Array(u),f=0;f>4,s[c++]=(15&r)<<4|n>>2,s[c++]=(3&n)<<6|63&i;return u},w={arraybuffer:{test:function test(e){return "ArrayBuffer"===s.toStringTag(e)},replace:function replace(e,t){t.buffers||(t.buffers=[]);var r=t.buffers.indexOf(e);return r>-1?{index:r}:(t.buffers.push(e),S(e))},revive:function revive(e,t){if(t.buffers||(t.buffers=[]),"object"===_typeof$1(e))return t.buffers[e.index];var r=T(e);return t.buffers.push(r),r}}},A="undefined"==typeof self?commonjsGlobal:self,P={};["Int8Array","Uint8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array"].forEach((function(e){var t=e,r=A[t];r&&(P[e.toLowerCase()]={test:function test(e){return s.toStringTag(e)===t},replace:function replace(e,t){var r=e.buffer,n=e.byteOffset,i=e.length;t.buffers||(t.buffers=[]);var o=t.buffers.indexOf(r);return o>-1?{index:o,byteOffset:n,length:i}:(t.buffers.push(r),{encoded:S(r),byteOffset:n,length:i})},revive:function revive(e,t){t.buffers||(t.buffers=[]);var n,i=e.byteOffset,o=e.length,a=e.encoded,c=e.index;return "index"in e?n=t.buffers[c]:(n=T(a),t.buffers.push(n)),new r(n,i,o)}});}));var I={dataview:{test:function test(e){return "DataView"===s.toStringTag(e)},replace:function replace(e,t){var r=e.buffer,n=e.byteOffset,i=e.byteLength;t.buffers||(t.buffers=[]);var o=t.buffers.indexOf(r);return o>-1?{index:o,byteOffset:n,byteLength:i}:(t.buffers.push(r),{encoded:S(r),byteOffset:n,byteLength:i})},revive:function revive(e,t){t.buffers||(t.buffers=[]);var r,n=e.byteOffset,i=e.byteLength,o=e.encoded,a=e.index;return "index"in e?r=t.buffers[a]:(r=T(o),t.buffers.push(r)),new DataView(r,n,i)}}},C={IntlCollator:{test:function test(e){return s.hasConstructorOf(e,Intl.Collator)},replace:function replace(e){return e.resolvedOptions()},revive:function revive(e){return new Intl.Collator(e.locale,e)}},IntlDateTimeFormat:{test:function test(e){return s.hasConstructorOf(e,Intl.DateTimeFormat)},replace:function replace(e){return e.resolvedOptions()},revive:function revive(e){return new Intl.DateTimeFormat(e.locale,e)}},IntlNumberFormat:{test:function test(e){return s.hasConstructorOf(e,Intl.NumberFormat)},replace:function replace(e){return e.resolvedOptions()},revive:function revive(e){return new Intl.NumberFormat(e.locale,e)}}};function string2arraybuffer(e){for(var t=new Uint8Array(e.length),r=0;r> 2]; - base64 += chars[(bytes[_i] & 3) << 4 | bytes[_i + 1] >> 4]; - base64 += chars[(bytes[_i + 1] & 15) << 2 | bytes[_i + 2] >> 6]; - base64 += chars[bytes[_i + 2] & 63]; - } - - if (len % 3 === 2) { - base64 = base64.slice(0, -1) + '='; - } else if (len % 3 === 1) { - base64 = base64.slice(0, -2) + '=='; - } - - return base64; - }; - /** - * @param {string} base64 - * @returns {ArrayBuffer} - */ - - var decode = function decode(base64) { - var len = base64.length; - var bufferLength = base64.length * 0.75; - var p = 0; - var encoded1, encoded2, encoded3, encoded4; - - if (base64[base64.length - 1] === '=') { - bufferLength--; - - if (base64[base64.length - 2] === '=') { - bufferLength--; - } - } - - var arraybuffer = new ArrayBuffer(bufferLength), - bytes = new Uint8Array(arraybuffer); - - for (var _i2 = 0; _i2 < len; _i2 += 4) { - encoded1 = lookup[base64.codePointAt(_i2)]; - encoded2 = lookup[base64.codePointAt(_i2 + 1)]; - encoded3 = lookup[base64.codePointAt(_i2 + 2)]; - encoded4 = lookup[base64.codePointAt(_i2 + 3)]; - bytes[p++] = encoded1 << 2 | encoded2 >> 4; - bytes[p++] = (encoded2 & 15) << 4 | encoded3 >> 2; - bytes[p++] = (encoded3 & 3) << 6 | encoded4 & 63; - } - - return arraybuffer; - }; - - /* eslint-env browser, node */ - var _global = typeof self === 'undefined' ? global : self; - var exportObj = {}; - [ - 'Int8Array', - 'Uint8Array', - 'Uint8ClampedArray', - 'Int16Array', - 'Uint16Array', - 'Int32Array', - 'Uint32Array', - 'Float32Array', - 'Float64Array' - ].forEach(function (typeName) { - var arrType = typeName; - var TypedArray = _global[arrType]; - if (TypedArray) { - exportObj[typeName.toLowerCase() + "2"] = { - test: function (x) { return typeson.toStringTag(x) === arrType; }, - replace: function (_a) { - var buffer = _a.buffer, byteOffset = _a.byteOffset, length = _a.length; - return { - buffer: buffer, - byteOffset: byteOffset, - length: length - }; - }, - revive: function (b64Obj) { - var buffer = b64Obj.buffer, byteOffset = b64Obj.byteOffset, length = b64Obj.length; - return new TypedArray(buffer, byteOffset, length); - } - }; - } - }); - - var arrayBuffer = { - arraybuffer: { - test: function (x) { return typeson.toStringTag(x) === 'ArrayBuffer'; }, - replace: function (b) { - return encode(b, 0, b.byteLength); - }, - revive: function (b64) { - var buffer = decode(b64); - return buffer; - } - } - }; - // See also typed-arrays! - - var TSON = new typeson().register(structuredCloning); - var readBlobsSynchronously = 'FileReaderSync' in self; // true in workers only. - var blobsToAwait = []; - var blobsToAwaitPos = 0; - // Need to patch encapsulateAsync as it does not work as of typeson 5.8.2 - // Also, current version of typespn-registry-1.0.0-alpha.21 does not - // encapsulate/revive Blobs correctly (fails one of the unit tests in - // this library (test 'export-format')) - TSON.register([ - arrayBuffer, - exportObj, { - blob2: { - test: function (x) { return typeson.toStringTag(x) === 'Blob'; }, - replace: function (b) { - if (b.isClosed) { // On MDN, but not in https://w3c.github.io/FileAPI/#dfn-Blob - throw new Error('The Blob is closed'); - } - if (readBlobsSynchronously) { - var data = readBlobSync(b, 'binary'); - var base64 = encode(data, 0, data.byteLength); - return { - type: b.type, - data: base64 - }; - } - else { - blobsToAwait.push(b); // This will also make TSON.mustFinalize() return true. - var result = { - type: b.type, - data: { start: blobsToAwaitPos, end: blobsToAwaitPos + b.size } - }; - blobsToAwaitPos += b.size; - return result; - } - }, - finalize: function (b, ba) { - b.data = encode(ba, 0, ba.byteLength); - }, - revive: function (_a) { - var type = _a.type, data = _a.data; - return new Blob([decode(data)], { type: type }); - } - } - } - ]); - TSON.mustFinalize = function () { return blobsToAwait.length > 0; }; - TSON.finalize = function (items) { return __awaiter(void 0, void 0, void 0, function () { - var allChunks, _i, items_1, item, types, arrayType, keyPath, typeName, typeSpec, b; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: return [4 /*yield*/, readBlobAsync(new Blob(blobsToAwait), 'binary')]; - case 1: - allChunks = _a.sent(); - if (items) { - for (_i = 0, items_1 = items; _i < items_1.length; _i++) { - item = items_1[_i]; - // Manually go through all "blob" types in the result - // and lookup the data slice they point at. - if (item.$types) { - types = item.$types; - arrayType = types.$; - if (arrayType) - types = types.$; - for (keyPath in types) { - typeName = types[keyPath]; - typeSpec = TSON.types[typeName]; - if (typeSpec && typeSpec.finalize) { - b = Dexie__default["default"].getByKeyPath(item, arrayType ? "$." + keyPath : keyPath); - typeSpec.finalize(b, allChunks.slice(b.start, b.end)); - } - } - } - } - } - // Free up memory - blobsToAwait = []; - return [2 /*return*/]; - } - }); - }); }; - - var DEFAULT_ROWS_PER_CHUNK = 2000; - function exportDB(db, options) { - return __awaiter(this, void 0, void 0, function () { - function exportAll() { - return __awaiter(this, void 0, void 0, function () { - var tablesRowCounts, emptyExportJson, posEndDataArray, firstJsonSlice, filter, _loop_1, _i, tables_1, tableName; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: return [4 /*yield*/, Promise.all(db.tables.map(function (table) { return table.count(); }))]; - case 1: - tablesRowCounts = _a.sent(); - tablesRowCounts.forEach(function (rowCount, i) { return tables[i].rowCount = rowCount; }); - progress.totalRows = tablesRowCounts.reduce(function (p, c) { return p + c; }); - emptyExportJson = JSON.stringify(emptyExport, undefined, prettyJson ? 2 : undefined); - posEndDataArray = emptyExportJson.lastIndexOf(']'); - firstJsonSlice = emptyExportJson.substring(0, posEndDataArray); - slices.push(firstJsonSlice); - filter = options.filter; - _loop_1 = function (tableName) { - var table, primKey, inbound, LIMIT, emptyTableExport, emptyTableExportJson, posEndRowsArray, lastKey, lastNumRows, mayHaveMoreRows, _loop_2, state_1; - return __generator(this, function (_b) { - switch (_b.label) { - case 0: - table = db.table(tableName); - primKey = table.schema.primKey; - inbound = !!primKey.keyPath; - LIMIT = options.numRowsPerChunk || DEFAULT_ROWS_PER_CHUNK; - emptyTableExport = inbound ? { - tableName: table.name, - inbound: true, - rows: [] - } : { - tableName: table.name, - inbound: false, - rows: [] - }; - emptyTableExportJson = JSON.stringify(emptyTableExport, undefined, prettyJson ? 2 : undefined); - if (prettyJson) { - // Increase indentation according to this: - // { - // ... - // data: [ - // ... - // data: [ - // 123456<---- here - // ] - // ] - // } - emptyTableExportJson = emptyTableExportJson.split('\n').join('\n '); - } - posEndRowsArray = emptyTableExportJson.lastIndexOf(']'); - slices.push(emptyTableExportJson.substring(0, posEndRowsArray)); - lastKey = null; - lastNumRows = 0; - mayHaveMoreRows = true; - _loop_2 = function () { - var chunkedCollection, values, filteredValues, tsonValues, json, keys, keyvals, tsonTuples, json; - return __generator(this, function (_c) { - switch (_c.label) { - case 0: - if (progressCallback) { - // Keep ongoing transaction private - Dexie__default["default"].ignoreTransaction(function () { return progressCallback(progress); }); - } - chunkedCollection = lastKey == null ? - table.limit(LIMIT) : - table.where(':id').above(lastKey).limit(LIMIT); - return [4 /*yield*/, chunkedCollection.toArray()]; - case 1: - values = _c.sent(); - if (values.length === 0) - return [2 /*return*/, "break"]; - if (lastKey != null && lastNumRows > 0) { - // Not initial chunk. Must add a comma: - slices.push(","); - if (prettyJson) { - slices.push("\n "); - } - } - mayHaveMoreRows = values.length === LIMIT; - if (!inbound) return [3 /*break*/, 4]; - filteredValues = filter ? - values.filter(function (value) { return filter(tableName, value); }) : - values; - tsonValues = filteredValues.map(function (value) { return TSON.encapsulate(value); }); - if (!TSON.mustFinalize()) return [3 /*break*/, 3]; - return [4 /*yield*/, Dexie__default["default"].waitFor(TSON.finalize(tsonValues))]; - case 2: - _c.sent(); - _c.label = 3; - case 3: - json = JSON.stringify(tsonValues, undefined, prettyJson ? 2 : undefined); - if (prettyJson) - json = json.split('\n').join('\n '); - // By generating a blob here, we give web platform the opportunity to store the contents - // on disk and release RAM. - slices.push(new Blob([json.substring(1, json.length - 1)])); - lastNumRows = filteredValues.length; - lastKey = values.length > 0 ? - Dexie__default["default"].getByKeyPath(values[values.length - 1], primKey.keyPath) : - null; - return [3 /*break*/, 8]; - case 4: return [4 /*yield*/, chunkedCollection.primaryKeys()]; - case 5: - keys = _c.sent(); - keyvals = keys.map(function (key, i) { return [key, values[i]]; }); - if (filter) - keyvals = keyvals.filter(function (_a) { - var key = _a[0], value = _a[1]; - return filter(tableName, value, key); - }); - tsonTuples = keyvals.map(function (tuple) { return TSON.encapsulate(tuple); }); - if (!TSON.mustFinalize()) return [3 /*break*/, 7]; - return [4 /*yield*/, Dexie__default["default"].waitFor(TSON.finalize(tsonTuples))]; - case 6: - _c.sent(); - _c.label = 7; - case 7: - json = JSON.stringify(tsonTuples, undefined, prettyJson ? 2 : undefined); - if (prettyJson) - json = json.split('\n').join('\n '); - // By generating a blob here, we give web platform the opportunity to store the contents - // on disk and release RAM. - slices.push(new Blob([json.substring(1, json.length - 1)])); - lastNumRows = keyvals.length; - lastKey = keys.length > 0 ? - keys[keys.length - 1] : - null; - _c.label = 8; - case 8: - progress.completedRows += values.length; - return [2 /*return*/]; - } - }); - }; - _b.label = 1; - case 1: - if (!mayHaveMoreRows) return [3 /*break*/, 3]; - return [5 /*yield**/, _loop_2()]; - case 2: - state_1 = _b.sent(); - if (state_1 === "break") - return [3 /*break*/, 3]; - return [3 /*break*/, 1]; - case 3: - slices.push(emptyTableExportJson.substr(posEndRowsArray)); // "]}" - progress.completedTables += 1; - if (progress.completedTables < progress.totalTables) { - slices.push(","); - } - return [2 /*return*/]; - } - }); - }; - _i = 0, tables_1 = tables; - _a.label = 2; - case 2: - if (!(_i < tables_1.length)) return [3 /*break*/, 5]; - tableName = tables_1[_i].name; - return [5 /*yield**/, _loop_1(tableName)]; - case 3: - _a.sent(); - _a.label = 4; - case 4: - _i++; - return [3 /*break*/, 2]; - case 5: - slices.push(emptyExportJson.substr(posEndDataArray)); - progress.done = true; - if (progressCallback) { - // Keep ongoing transaction private - Dexie__default["default"].ignoreTransaction(function () { return progressCallback(progress); }); - } - return [2 /*return*/]; - } - }); - }); - } - var slices, tables, prettyJson, emptyExport, progressCallback, progress; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - options = options || {}; - slices = []; - tables = db.tables.map(function (table) { return ({ - name: table.name, - schema: getSchemaString(table), - rowCount: 0 - }); }); - prettyJson = options.prettyJson; - emptyExport = { - formatName: "dexie", - formatVersion: 1, - data: { - databaseName: db.name, - databaseVersion: db.verno, - tables: tables, - data: [] - } - }; - progressCallback = options.progressCallback; - progress = { - done: false, - completedRows: 0, - completedTables: 0, - totalRows: NaN, - totalTables: db.tables.length - }; - _a.label = 1; - case 1: - _a.trys.push([1, , 6, 7]); - if (!options.noTransaction) return [3 /*break*/, 3]; - return [4 /*yield*/, exportAll()]; - case 2: - _a.sent(); - return [3 /*break*/, 5]; - case 3: return [4 /*yield*/, db.transaction('r', db.tables, exportAll)]; - case 4: - _a.sent(); - _a.label = 5; - case 5: return [3 /*break*/, 7]; - case 6: - TSON.finalize(); // Free up mem if error has occurred - return [7 /*endfinally*/]; - case 7: return [2 /*return*/, new Blob(slices, { type: "text/json" })]; - } - }); - }); - } - - var VERSION = 1; - - var fakeStream = {Stream: function(){}}; - - var clarinet_1 = createCommonjsModule(function (module, exports) { - (function (clarinet) { - - // non node-js needs to set clarinet debug on root - var env =(typeof process === 'object' && process.env) - ? process.env - : self; - - clarinet.parser = function (opt) { return new CParser(opt);}; - clarinet.CParser = CParser; - clarinet.CStream = CStream; - clarinet.createStream = createStream; - clarinet.MAX_BUFFER_LENGTH = 10 * 1024 * 1024; - clarinet.DEBUG = (env.CDEBUG==='debug'); - clarinet.INFO = (env.CDEBUG==='debug' || env.CDEBUG==='info'); - clarinet.EVENTS = - [ "value" - , "string" - , "key" - , "openobject" - , "closeobject" - , "openarray" - , "closearray" - , "error" - , "end" - , "ready" - ]; - - var buffers = { - textNode: undefined, - numberNode: "" - } - , streamWraps = clarinet.EVENTS.filter(function (ev) { - return ev !== "error" && ev !== "end"; - }) - , S = 0 - , Stream - ; - - clarinet.STATE = - { BEGIN : S++ - , VALUE : S++ // general stuff - , OPEN_OBJECT : S++ // { - , CLOSE_OBJECT : S++ // } - , OPEN_ARRAY : S++ // [ - , CLOSE_ARRAY : S++ // ] - , TEXT_ESCAPE : S++ // \ stuff - , STRING : S++ // "" - , BACKSLASH : S++ - , END : S++ // No more stack - , OPEN_KEY : S++ // , "a" - , CLOSE_KEY : S++ // : - , TRUE : S++ // r - , TRUE2 : S++ // u - , TRUE3 : S++ // e - , FALSE : S++ // a - , FALSE2 : S++ // l - , FALSE3 : S++ // s - , FALSE4 : S++ // e - , NULL : S++ // u - , NULL2 : S++ // l - , NULL3 : S++ // l - , NUMBER_DECIMAL_POINT : S++ // . - , NUMBER_DIGIT : S++ // [0-9] - }; - - for (var s_ in clarinet.STATE) clarinet.STATE[clarinet.STATE[s_]] = s_; - - // switcharoo - S = clarinet.STATE; - - const Char = { - tab : 0x09, // \t - lineFeed : 0x0A, // \n - carriageReturn : 0x0D, // \r - space : 0x20, // " " - - doubleQuote : 0x22, // " - plus : 0x2B, // + - comma : 0x2C, // , - minus : 0x2D, // - - period : 0x2E, // . - - _0 : 0x30, // 0 - _9 : 0x39, // 9 - - colon : 0x3A, // : - - E : 0x45, // E - - openBracket : 0x5B, // [ - backslash : 0x5C, // \ - closeBracket : 0x5D, // ] - - a : 0x61, // a - b : 0x62, // b - e : 0x65, // e - f : 0x66, // f - l : 0x6C, // l - n : 0x6E, // n - r : 0x72, // r - s : 0x73, // s - t : 0x74, // t - u : 0x75, // u - - openBrace : 0x7B, // { - closeBrace : 0x7D, // } - }; - - if (!Object.create) { - Object.create = function (o) { - function f () { this["__proto__"] = o; } - f.prototype = o; - return new f; - }; - } - - if (!Object.getPrototypeOf) { - Object.getPrototypeOf = function (o) { - return o["__proto__"]; - }; - } - - if (!Object.keys) { - Object.keys = function (o) { - var a = []; - for (var i in o) if (o.hasOwnProperty(i)) a.push(i); - return a; - }; - } - - function checkBufferLength (parser) { - var maxAllowed = Math.max(clarinet.MAX_BUFFER_LENGTH, 10) - , maxActual = 0 - ; - for (var buffer in buffers) { - var len = parser[buffer] === undefined ? 0 : parser[buffer].length; - if (len > maxAllowed) { - switch (buffer) { - case "text": - closeText(parser); - break; - - default: - error(parser, "Max buffer length exceeded: "+ buffer); - } - } - maxActual = Math.max(maxActual, len); - } - parser.bufferCheckPosition = (clarinet.MAX_BUFFER_LENGTH - maxActual) - + parser.position; - } - - function clearBuffers (parser) { - for (var buffer in buffers) { - parser[buffer] = buffers[buffer]; - } - } - - var stringTokenPattern = /[\\"\n]/g; - - function CParser (opt) { - if (!(this instanceof CParser)) return new CParser (opt); - - var parser = this; - clearBuffers(parser); - parser.bufferCheckPosition = clarinet.MAX_BUFFER_LENGTH; - parser.q = parser.c = parser.p = ""; - parser.opt = opt || {}; - parser.closed = parser.closedRoot = parser.sawRoot = false; - parser.tag = parser.error = null; - parser.state = S.BEGIN; - parser.stack = new Array(); - // mostly just for error reporting - parser.position = parser.column = 0; - parser.line = 1; - parser.slashed = false; - parser.unicodeI = 0; - parser.unicodeS = null; - parser.depth = 0; - emit(parser, "onready"); - } - - CParser.prototype = - { end : function () { end(this); } - , write : write - , resume : function () { this.error = null; return this; } - , close : function () { return this.write(null); } - }; - - try { Stream = fakeStream.Stream; } - catch (ex) { Stream = function () {}; } - - function createStream (opt) { return new CStream(opt); } - - function CStream (opt) { - if (!(this instanceof CStream)) return new CStream(opt); - - this._parser = new CParser(opt); - this.writable = true; - this.readable = true; - - //var Buffer = this.Buffer || function Buffer () {}; // if we don't have Buffers, fake it so we can do `var instanceof Buffer` and not throw an error - this.bytes_remaining = 0; // number of bytes remaining in multi byte utf8 char to read after split boundary - this.bytes_in_sequence = 0; // bytes in multi byte utf8 char to read - this.temp_buffs = { "2": new Buffer(2), "3": new Buffer(3), "4": new Buffer(4) }; // for rebuilding chars split before boundary is reached - this.string = ''; - - var me = this; - Stream.apply(me); - - this._parser.onend = function () { me.emit("end"); }; - this._parser.onerror = function (er) { - me.emit("error", er); - me._parser.error = null; - }; - - streamWraps.forEach(function (ev) { - Object.defineProperty(me, "on" + ev, - { get : function () { return me._parser["on" + ev]; } - , set : function (h) { - if (!h) { - me.removeAllListeners(ev); - me._parser["on"+ev] = h; - return h; - } - me.on(ev, h); - } - , enumerable : true - , configurable : false - }); - }); - } - - CStream.prototype = Object.create(Stream.prototype, - { constructor: { value: CStream } }); - - CStream.prototype.write = function (data) { - data = new Buffer(data); - for (var i = 0; i < data.length; i++) { - var n = data[i]; - - // check for carry over of a multi byte char split between data chunks - // & fill temp buffer it with start of this data chunk up to the boundary limit set in the last iteration - if (this.bytes_remaining > 0) { - for (var j = 0; j < this.bytes_remaining; j++) { - this.temp_buffs[this.bytes_in_sequence][this.bytes_in_sequence - this.bytes_remaining + j] = data[j]; - } - this.string = this.temp_buffs[this.bytes_in_sequence].toString(); - this.bytes_in_sequence = this.bytes_remaining = 0; - - // move iterator forward by number of byte read during sequencing - i = i + j - 1; - - // pass data to parser and move forward to parse rest of data - this._parser.write(this.string); - this.emit("data", this.string); - continue; - } - - // if no remainder bytes carried over, parse multi byte (>=128) chars one at a time - if (this.bytes_remaining === 0 && n >= 128) { - if ((n >= 194) && (n <= 223)) this.bytes_in_sequence = 2; - if ((n >= 224) && (n <= 239)) this.bytes_in_sequence = 3; - if ((n >= 240) && (n <= 244)) this.bytes_in_sequence = 4; - if ((this.bytes_in_sequence + i) > data.length) { // if bytes needed to complete char fall outside data length, we have a boundary split - - for (var k = 0; k <= (data.length - 1 - i); k++) { - this.temp_buffs[this.bytes_in_sequence][k] = data[i + k]; // fill temp data of correct size with bytes available in this chunk - } - this.bytes_remaining = (i + this.bytes_in_sequence) - data.length; - - // immediately return as we need another chunk to sequence the character - return true; - } else { - this.string = data.slice(i, (i + this.bytes_in_sequence)).toString(); - i = i + this.bytes_in_sequence - 1; - - this._parser.write(this.string); - this.emit("data", this.string); - continue; - } - } - - // is there a range of characters that are immediately parsable? - for (var p = i; p < data.length; p++) { - if (data[p] >= 128) break; - } - this.string = data.slice(i, p).toString(); - this._parser.write(this.string); - this.emit("data", this.string); - i = p - 1; - - // handle any remaining characters using multibyte logic - continue; - } - }; - - CStream.prototype.end = function (chunk) { - if (chunk && chunk.length) this._parser.write(chunk.toString()); - this._parser.end(); - return true; - }; - - CStream.prototype.on = function (ev, handler) { - var me = this; - if (!me._parser["on"+ev] && streamWraps.indexOf(ev) !== -1) { - me._parser["on"+ev] = function () { - var args = arguments.length === 1 ? [arguments[0]] - : Array.apply(null, arguments); - args.splice(0, 0, ev); - me.emit.apply(me, args); - }; - } - return Stream.prototype.on.call(me, ev, handler); - }; - - CStream.prototype.destroy = function () { - clearBuffers(this._parser); - this.emit("close"); - }; - - function emit(parser, event, data) { - if(clarinet.INFO) console.log('-- emit', event, data); - if (parser[event]) parser[event](data); - } - - function emitNode(parser, event, data) { - closeValue(parser); - emit(parser, event, data); - } - - function closeValue(parser, event) { - parser.textNode = textopts(parser.opt, parser.textNode); - if (parser.textNode !== undefined) { - emit(parser, (event ? event : "onvalue"), parser.textNode); - } - parser.textNode = undefined; - } - - function closeNumber(parser) { - if (parser.numberNode) - emit(parser, "onvalue", parseFloat(parser.numberNode)); - parser.numberNode = ""; - } - - function textopts (opt, text) { - if (text === undefined) { - return text; - } - if (opt.trim) text = text.trim(); - if (opt.normalize) text = text.replace(/\s+/g, " "); - return text; - } - - function error (parser, er) { - closeValue(parser); - er += "\nLine: "+parser.line+ - "\nColumn: "+parser.column+ - "\nChar: "+parser.c; - er = new Error(er); - parser.error = er; - emit(parser, "onerror", er); - return parser; - } - - function end(parser) { - if (parser.state !== S.VALUE || parser.depth !== 0) - error(parser, "Unexpected end"); - - closeValue(parser); - parser.c = ""; - parser.closed = true; - emit(parser, "onend"); - CParser.call(parser, parser.opt); - return parser; - } - - function isWhitespace(c) { - return c === Char.carriageReturn || c === Char.lineFeed || c === Char.space || c === Char.tab; - } - - function write (chunk) { - var parser = this; - if (this.error) throw this.error; - if (parser.closed) return error(parser, - "Cannot write after close. Assign an onready handler."); - if (chunk === null) return end(parser); - var i = 0, c = chunk.charCodeAt(0), p = parser.p; - if (clarinet.DEBUG) console.log('write -> [' + chunk + ']'); - while (c) { - p = c; - parser.c = c = chunk.charCodeAt(i++); - // if chunk doesnt have next, like streaming char by char - // this way we need to check if previous is really previous - // if not we need to reset to what the parser says is the previous - // from buffer - if(p !== c ) parser.p = p; - else p = parser.p; - - if(!c) break; - - if (clarinet.DEBUG) console.log(i,c,clarinet.STATE[parser.state]); - parser.position ++; - if (c === Char.lineFeed) { - parser.line ++; - parser.column = 0; - } else parser.column ++; - switch (parser.state) { - - case S.BEGIN: - if (c === Char.openBrace) parser.state = S.OPEN_OBJECT; - else if (c === Char.openBracket) parser.state = S.OPEN_ARRAY; - else if (!isWhitespace(c)) - error(parser, "Non-whitespace before {[."); - continue; - - case S.OPEN_KEY: - case S.OPEN_OBJECT: - if (isWhitespace(c)) continue; - if(parser.state === S.OPEN_KEY) parser.stack.push(S.CLOSE_KEY); - else { - if(c === Char.closeBrace) { - emit(parser, 'onopenobject'); - this.depth++; - emit(parser, 'oncloseobject'); - this.depth--; - parser.state = parser.stack.pop() || S.VALUE; - continue; - } else parser.stack.push(S.CLOSE_OBJECT); - } - if(c === Char.doubleQuote) parser.state = S.STRING; - else error(parser, "Malformed object key should start with \""); - continue; - - case S.CLOSE_KEY: - case S.CLOSE_OBJECT: - if (isWhitespace(c)) continue; - (parser.state === S.CLOSE_KEY) ? 'key' : 'object'; - if(c === Char.colon) { - if(parser.state === S.CLOSE_OBJECT) { - parser.stack.push(S.CLOSE_OBJECT); - closeValue(parser, 'onopenobject'); - this.depth++; - } else closeValue(parser, 'onkey'); - parser.state = S.VALUE; - } else if (c === Char.closeBrace) { - emitNode(parser, 'oncloseobject'); - this.depth--; - parser.state = parser.stack.pop() || S.VALUE; - } else if(c === Char.comma) { - if(parser.state === S.CLOSE_OBJECT) - parser.stack.push(S.CLOSE_OBJECT); - closeValue(parser); - parser.state = S.OPEN_KEY; - } else error(parser, 'Bad object'); - continue; - - case S.OPEN_ARRAY: // after an array there always a value - case S.VALUE: - if (isWhitespace(c)) continue; - if(parser.state===S.OPEN_ARRAY) { - emit(parser, 'onopenarray'); - this.depth++; - parser.state = S.VALUE; - if(c === Char.closeBracket) { - emit(parser, 'onclosearray'); - this.depth--; - parser.state = parser.stack.pop() || S.VALUE; - continue; - } else { - parser.stack.push(S.CLOSE_ARRAY); - } - } - if(c === Char.doubleQuote) parser.state = S.STRING; - else if(c === Char.openBrace) parser.state = S.OPEN_OBJECT; - else if(c === Char.openBracket) parser.state = S.OPEN_ARRAY; - else if(c === Char.t) parser.state = S.TRUE; - else if(c === Char.f) parser.state = S.FALSE; - else if(c === Char.n) parser.state = S.NULL; - else if(c === Char.minus) { // keep and continue - parser.numberNode += "-"; - } else if(Char._0 <= c && c <= Char._9) { - parser.numberNode += String.fromCharCode(c); - parser.state = S.NUMBER_DIGIT; - } else error(parser, "Bad value"); - continue; - - case S.CLOSE_ARRAY: - if(c === Char.comma) { - parser.stack.push(S.CLOSE_ARRAY); - closeValue(parser, 'onvalue'); - parser.state = S.VALUE; - } else if (c === Char.closeBracket) { - emitNode(parser, 'onclosearray'); - this.depth--; - parser.state = parser.stack.pop() || S.VALUE; - } else if (isWhitespace(c)) - continue; - else error(parser, 'Bad array'); - continue; - - case S.STRING: - if (parser.textNode === undefined) { - parser.textNode = ""; - } - - // thanks thejh, this is an about 50% performance improvement. - var starti = i-1 - , slashed = parser.slashed - , unicodeI = parser.unicodeI - ; - STRING_BIGLOOP: while (true) { - if (clarinet.DEBUG) - console.log(i,c,clarinet.STATE[parser.state] - ,slashed); - // zero means "no unicode active". 1-4 mean "parse some more". end after 4. - while (unicodeI > 0) { - parser.unicodeS += String.fromCharCode(c); - c = chunk.charCodeAt(i++); - parser.position++; - if (unicodeI === 4) { - // TODO this might be slow? well, probably not used too often anyway - parser.textNode += String.fromCharCode(parseInt(parser.unicodeS, 16)); - unicodeI = 0; - starti = i-1; - } else { - unicodeI++; - } - // we can just break here: no stuff we skipped that still has to be sliced out or so - if (!c) break STRING_BIGLOOP; - } - if (c === Char.doubleQuote && !slashed) { - parser.state = parser.stack.pop() || S.VALUE; - parser.textNode += chunk.substring(starti, i-1); - parser.position += i - 1 - starti; - break; - } - if (c === Char.backslash && !slashed) { - slashed = true; - parser.textNode += chunk.substring(starti, i-1); - parser.position += i - 1 - starti; - c = chunk.charCodeAt(i++); - parser.position++; - if (!c) break; - } - if (slashed) { - slashed = false; - if (c === Char.n) { parser.textNode += '\n'; } - else if (c === Char.r) { parser.textNode += '\r'; } - else if (c === Char.t) { parser.textNode += '\t'; } - else if (c === Char.f) { parser.textNode += '\f'; } - else if (c === Char.b) { parser.textNode += '\b'; } - else if (c === Char.u) { - // \uxxxx. meh! - unicodeI = 1; - parser.unicodeS = ''; - } else { - parser.textNode += String.fromCharCode(c); - } - c = chunk.charCodeAt(i++); - parser.position++; - starti = i-1; - if (!c) break; - else continue; - } - - stringTokenPattern.lastIndex = i; - var reResult = stringTokenPattern.exec(chunk); - if (reResult === null) { - i = chunk.length+1; - parser.textNode += chunk.substring(starti, i-1); - parser.position += i - 1 - starti; - break; - } - i = reResult.index+1; - c = chunk.charCodeAt(reResult.index); - if (!c) { - parser.textNode += chunk.substring(starti, i-1); - parser.position += i - 1 - starti; - break; - } - } - parser.slashed = slashed; - parser.unicodeI = unicodeI; - continue; - - case S.TRUE: - if (c === Char.r) parser.state = S.TRUE2; - else error(parser, 'Invalid true started with t'+ c); - continue; - - case S.TRUE2: - if (c === Char.u) parser.state = S.TRUE3; - else error(parser, 'Invalid true started with tr'+ c); - continue; - - case S.TRUE3: - if(c === Char.e) { - emit(parser, "onvalue", true); - parser.state = parser.stack.pop() || S.VALUE; - } else error(parser, 'Invalid true started with tru'+ c); - continue; - - case S.FALSE: - if (c === Char.a) parser.state = S.FALSE2; - else error(parser, 'Invalid false started with f'+ c); - continue; - - case S.FALSE2: - if (c === Char.l) parser.state = S.FALSE3; - else error(parser, 'Invalid false started with fa'+ c); - continue; - - case S.FALSE3: - if (c === Char.s) parser.state = S.FALSE4; - else error(parser, 'Invalid false started with fal'+ c); - continue; - - case S.FALSE4: - if (c === Char.e) { - emit(parser, "onvalue", false); - parser.state = parser.stack.pop() || S.VALUE; - } else error(parser, 'Invalid false started with fals'+ c); - continue; - - case S.NULL: - if (c === Char.u) parser.state = S.NULL2; - else error(parser, 'Invalid null started with n'+ c); - continue; - - case S.NULL2: - if (c === Char.l) parser.state = S.NULL3; - else error(parser, 'Invalid null started with nu'+ c); - continue; - - case S.NULL3: - if(c === Char.l) { - emit(parser, "onvalue", null); - parser.state = parser.stack.pop() || S.VALUE; - } else error(parser, 'Invalid null started with nul'+ c); - continue; - - case S.NUMBER_DECIMAL_POINT: - if(c === Char.period) { - parser.numberNode += "."; - parser.state = S.NUMBER_DIGIT; - } else error(parser, 'Leading zero not followed by .'); - continue; - - case S.NUMBER_DIGIT: - if(Char._0 <= c && c <= Char._9) parser.numberNode += String.fromCharCode(c); - else if (c === Char.period) { - if(parser.numberNode.indexOf('.')!==-1) - error(parser, 'Invalid number has two dots'); - parser.numberNode += "."; - } else if (c === Char.e || c === Char.E) { - if(parser.numberNode.indexOf('e')!==-1 || - parser.numberNode.indexOf('E')!==-1 ) - error(parser, 'Invalid number has two exponential'); - parser.numberNode += "e"; - } else if (c === Char.plus || c === Char.minus) { - if(!(p === Char.e || p === Char.E)) - error(parser, 'Invalid symbol in number'); - parser.numberNode += String.fromCharCode(c); - } else { - closeNumber(parser); - i--; // go back one - parser.state = parser.stack.pop() || S.VALUE; - } - continue; - - default: - error(parser, "Unknown state: " + parser.state); - } - } - if (parser.position >= parser.bufferCheckPosition) - checkBufferLength(parser); - return parser; - } - - })(exports); - }); - - function JsonStream(blob) { - var pos = 0; - var parser = JsonParser(true); - var rv = { - pullAsync: function (numBytes) { - return __awaiter(this, void 0, void 0, function () { - var slize, jsonPart, result; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - slize = blob.slice(pos, pos + numBytes); - pos += numBytes; - return [4 /*yield*/, readBlobAsync(slize, 'text')]; - case 1: - jsonPart = _a.sent(); - result = parser.write(jsonPart); - rv.result = result || {}; - return [2 /*return*/, result]; - } - }); - }); - }, - pullSync: function (numBytes) { - var slize = blob.slice(pos, pos + numBytes); - pos += numBytes; - var jsonPart = readBlobSync(slize, 'text'); - var result = parser.write(jsonPart); - rv.result = result || {}; - return result; - }, - done: function () { - return parser.done(); - }, - eof: function () { - return pos >= blob.size; - }, - result: {} - }; - return rv; - } - function JsonParser(allowPartial) { - var parser = clarinet_1.parser(); - var level = 0; - var result; - var stack = []; - var obj; - var key; - var done = false; - var array = false; - parser.onopenobject = function (newKey) { - var newObj = {}; - newObj.incomplete = true; - if (!result) - result = newObj; - if (obj) { - stack.push([key, obj, array]); - if (allowPartial) { - if (array) { - obj.push(newObj); - } - else { - obj[key] = newObj; - } - } - } - obj = newObj; - key = newKey; - array = false; - ++level; - }; - parser.onkey = function (newKey) { return key = newKey; }; - parser.onvalue = function (value) { return array ? obj.push(value) : obj[key] = value; }; - parser.oncloseobject = function () { - var _a; - delete obj.incomplete; - key = null; - if (--level === 0) { - done = true; - } - else { - var completedObj = obj; - _a = stack.pop(), key = _a[0], obj = _a[1], array = _a[2]; - if (!allowPartial) { - if (array) { - obj.push(completedObj); - } - else { - obj[key] = completedObj; - } - } - } - }; - parser.onopenarray = function () { - var newObj = []; - newObj.incomplete = true; - if (!result) - result = newObj; - if (obj) { - stack.push([key, obj, array]); - if (allowPartial) { - if (array) { - obj.push(newObj); - } - else { - obj[key] = newObj; - } - } - } - obj = newObj; - array = true; - key = null; - ++level; - }; - parser.onclosearray = function () { - var _a; - delete obj.incomplete; - key = null; - if (--level === 0) { - done = true; - } - else { - var completedObj = obj; - _a = stack.pop(), key = _a[0], obj = _a[1], array = _a[2]; - if (!allowPartial) { - if (array) { - obj.push(completedObj); - } - else { - obj[key] = completedObj; - } - } - } - }; - return { - write: function (jsonPart) { - parser.write(jsonPart); - return result; - }, - done: function () { - return done; - } - }; - } - - var DEFAULT_KILOBYTES_PER_CHUNK = 1024; - function importDB(exportedData, options) { - return __awaiter(this, void 0, void 0, function () { - var CHUNK_SIZE, stream, dbExport, db; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - options = options || {}; // All booleans defaults to false. - CHUNK_SIZE = options.chunkSizeBytes || (DEFAULT_KILOBYTES_PER_CHUNK * 1024); - return [4 /*yield*/, loadUntilWeGotEnoughData(exportedData, CHUNK_SIZE)]; - case 1: - stream = _a.sent(); - dbExport = stream.result.data; - db = new Dexie__default["default"](dbExport.databaseName); - db.version(dbExport.databaseVersion).stores(extractDbSchema(dbExport)); - return [4 /*yield*/, importInto(db, stream, options)]; - case 2: - _a.sent(); - return [2 /*return*/, db]; - } - }); - }); - } - function peakImportFile(exportedData) { - return __awaiter(this, void 0, void 0, function () { - var stream; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - stream = JsonStream(exportedData); - _a.label = 1; - case 1: - if (!!stream.eof()) return [3 /*break*/, 3]; - return [4 /*yield*/, stream.pullAsync(5 * 1024)]; - case 2: - _a.sent(); // 5 k is normally enough for the headers. If not, it will just do another go. - if (stream.result.data && stream.result.data.data) { - // @ts-ignore - TS won't allow us to delete a required property - but we are going to cast it. - delete stream.result.data.data; // Don't return half-baked data array. - return [3 /*break*/, 3]; - } - return [3 /*break*/, 1]; - case 3: return [2 /*return*/, stream.result]; - } - }); - }); - } - function importInto(db, exportedData, options) { - return __awaiter(this, void 0, void 0, function () { - function importAll() { - return __awaiter(this, void 0, void 0, function () { - var _loop_1, _i, _a, tableExport, state_1; - return __generator(this, function (_b) { - switch (_b.label) { - case 0: - _loop_1 = function (tableExport) { - var tableName, table, tableSchemaStr, sourceRows, rows, i, obj, filter, filteredRows, _c, keys, values; - return __generator(this, function (_d) { - switch (_d.label) { - case 0: - if (!tableExport.rows) - return [2 /*return*/, "break"]; // Need to pull more! - if (!tableExport.rows.incomplete && tableExport.rows.length === 0) - return [2 /*return*/, "continue"]; - if (progressCallback) { - // Keep ongoing transaction private - Dexie__default["default"].ignoreTransaction(function () { return progressCallback(progress); }); - } - tableName = tableExport.tableName; - table = db.table(tableName); - tableSchemaStr = dbExport.tables.filter(function (t) { return t.name === tableName; })[0].schema; - if (!table) { - if (!options.acceptMissingTables) - throw new Error("Exported table ".concat(tableExport.tableName, " is missing in installed database")); - else - return [2 /*return*/, "continue"]; - } - if (!options.acceptChangedPrimaryKey && - tableSchemaStr.split(',')[0] != table.schema.primKey.src) { - throw new Error("Primary key differs for table ".concat(tableExport.tableName, ". ")); - } - sourceRows = tableExport.rows; - rows = []; - for (i = 0; i < sourceRows.length; i++) { - obj = sourceRows[i]; - if (!obj.incomplete) { - rows.push(TSON.revive(obj)); - } - else { - break; - } - } - filter = options.filter; - filteredRows = filter ? - tableExport.inbound ? - rows.filter(function (value) { return filter(tableName, value); }) : - rows.filter(function (_a) { - var key = _a[0], value = _a[1]; - return filter(tableName, value, key); - }) : - rows; - _c = tableExport.inbound ? - [undefined, filteredRows] : - [filteredRows.map(function (row) { return row[0]; }), rows.map(function (row) { return row[1]; })], keys = _c[0], values = _c[1]; - if (!options.overwriteValues) return [3 /*break*/, 2]; - return [4 /*yield*/, table.bulkPut(values, keys)]; - case 1: - _d.sent(); - return [3 /*break*/, 4]; - case 2: return [4 /*yield*/, table.bulkAdd(values, keys)]; - case 3: - _d.sent(); - _d.label = 4; - case 4: - progress.completedRows += rows.length; - if (!rows.incomplete) { - progress.completedTables += 1; - } - sourceRows.splice(0, rows.length); // Free up RAM, keep existing array instance. - return [2 /*return*/]; - } - }); - }; - _i = 0, _a = dbExport.data; - _b.label = 1; - case 1: - if (!(_i < _a.length)) return [3 /*break*/, 4]; - tableExport = _a[_i]; - return [5 /*yield**/, _loop_1(tableExport)]; - case 2: - state_1 = _b.sent(); - if (state_1 === "break") - return [3 /*break*/, 4]; - _b.label = 3; - case 3: - _i++; - return [3 /*break*/, 1]; - case 4: - // Avoid unnescessary loops in "for (const tableExport of dbExport.data)" - while (dbExport.data.length > 0 && dbExport.data[0].rows && !dbExport.data[0].rows.incomplete) { - // We've already imported all rows from the first table. Delete its occurrence - dbExport.data.splice(0, 1); - } - if (!(!jsonStream.done() && !jsonStream.eof())) return [3 /*break*/, 8]; - if (!readBlobsSynchronously) return [3 /*break*/, 5]; - // If we can pull from blob synchronically, we don't have to - // keep transaction alive using Dexie.waitFor(). - // This will only be possible in workers. - jsonStream.pullSync(CHUNK_SIZE); - return [3 /*break*/, 7]; - case 5: return [4 /*yield*/, Dexie__default["default"].waitFor(jsonStream.pullAsync(CHUNK_SIZE))]; - case 6: - _b.sent(); - _b.label = 7; - case 7: return [3 /*break*/, 9]; - case 8: return [3 /*break*/, 10]; - case 9: - return [3 /*break*/, 0]; - case 10: return [2 /*return*/]; - } - }); - }); - } - var CHUNK_SIZE, jsonStream, dbExportFile, readBlobsSynchronously, dbExport, progressCallback, progress, _i, _a, table; - return __generator(this, function (_b) { - switch (_b.label) { - case 0: - options = options || {}; // All booleans defaults to false. - CHUNK_SIZE = options.chunkSizeBytes || (DEFAULT_KILOBYTES_PER_CHUNK * 1024); - return [4 /*yield*/, loadUntilWeGotEnoughData(exportedData, CHUNK_SIZE)]; - case 1: - jsonStream = _b.sent(); - dbExportFile = jsonStream.result; - readBlobsSynchronously = 'FileReaderSync' in self; - dbExport = dbExportFile.data; - if (!options.acceptNameDiff && db.name !== dbExport.databaseName) - throw new Error("Name differs. Current database name is ".concat(db.name, " but export is ").concat(dbExport.databaseName)); - if (!options.acceptVersionDiff && db.verno !== dbExport.databaseVersion) { - // Possible feature: Call upgraders in some isolated way if this happens... ? - throw new Error("Database version differs. Current database is in version ".concat(db.verno, " but export is ").concat(dbExport.databaseVersion)); - } - progressCallback = options.progressCallback; - progress = { - done: false, - completedRows: 0, - completedTables: 0, - totalRows: dbExport.tables.reduce(function (p, c) { return p + c.rowCount; }, 0), - totalTables: dbExport.tables.length - }; - if (progressCallback) { - // Keep ongoing transaction private - Dexie__default["default"].ignoreTransaction(function () { return progressCallback(progress); }); - } - if (!options.clearTablesBeforeImport) return [3 /*break*/, 5]; - _i = 0, _a = db.tables; - _b.label = 2; - case 2: - if (!(_i < _a.length)) return [3 /*break*/, 5]; - table = _a[_i]; - return [4 /*yield*/, table.clear()]; - case 3: - _b.sent(); - _b.label = 4; - case 4: - _i++; - return [3 /*break*/, 2]; - case 5: - if (!options.noTransaction) return [3 /*break*/, 7]; - return [4 /*yield*/, importAll()]; - case 6: - _b.sent(); - return [3 /*break*/, 9]; - case 7: return [4 /*yield*/, db.transaction('rw', db.tables, importAll)]; - case 8: - _b.sent(); - _b.label = 9; - case 9: - progress.done = true; - if (progressCallback) { - // Keep ongoing transaction private - Dexie__default["default"].ignoreTransaction(function () { return progressCallback(progress); }); - } - return [2 /*return*/]; - } - }); - }); - } - function loadUntilWeGotEnoughData(exportedData, CHUNK_SIZE) { - return __awaiter(this, void 0, void 0, function () { - var stream, dbExportFile; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - stream = ('slice' in exportedData ? - JsonStream(exportedData) : - exportedData); - _a.label = 1; - case 1: - if (!!stream.eof()) return [3 /*break*/, 3]; - return [4 /*yield*/, stream.pullAsync(CHUNK_SIZE)]; - case 2: - _a.sent(); - if (stream.result.data && stream.result.data.data) - return [3 /*break*/, 3]; - return [3 /*break*/, 1]; - case 3: - dbExportFile = stream.result; - if (!dbExportFile || dbExportFile.formatName != "dexie") - throw new Error("Given file is not a dexie export"); - if (dbExportFile.formatVersion > VERSION) { - throw new Error("Format version ".concat(dbExportFile.formatVersion, " not supported")); - } - if (!dbExportFile.data) { - throw new Error("No data in export file"); - } - if (!dbExportFile.data.databaseName) { - throw new Error("Missing databaseName in export file"); - } - if (!dbExportFile.data.databaseVersion) { - throw new Error("Missing databaseVersion in export file"); - } - if (!dbExportFile.data.tables) { - throw new Error("Missing tables in export file"); - } - return [2 /*return*/, stream]; - } - }); - }); - } - - // - // Extend Dexie interface (runtime wise) - // - Dexie__default["default"].prototype.export = function (options) { - return exportDB(this, options); - }; - Dexie__default["default"].prototype.import = function (blob, options) { - return importInto(this, blob, options); - }; - Dexie__default["default"].import = function (blob, options) { return importDB(blob, options); }; - var dexieExportImport = (function () { - throw new Error("This addon extends Dexie.prototype globally and does not have be included in Dexie constructor's addons options."); - }); - - exports["default"] = dexieExportImport; - exports.exportDB = exportDB; - exports.importDB = importDB; - exports.importInto = importInto; - exports.peakImportFile = peakImportFile; - - Object.defineProperty(exports, '__esModule', { value: true }); - -})); -//# sourceMappingURL=dexie-export-import.js.map diff --git a/lib/dexie.js b/lib/dexie.js deleted file mode 100644 index 08fce7b..0000000 --- a/lib/dexie.js +++ /dev/null @@ -1,5200 +0,0 @@ -/* - * Dexie.js - a minimalistic wrapper for IndexedDB - * =============================================== - * - * By David Fahlander, david.fahlander@gmail.com - * - * Version 3.2.4, Tue May 30 2023 - * - * https://dexie.org - * - * Apache License Version 2.0, January 2004, http://www.apache.org/licenses/ - */ - -(function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Dexie = factory()); -})(this, (function () { 'use strict'; - - /*! ***************************************************************************** - Copyright (c) Microsoft Corporation. - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted. - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH - REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, - INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR - OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - PERFORMANCE OF THIS SOFTWARE. - ***************************************************************************** */ - var __assign = function() { - __assign = Object.assign || function __assign(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); - }; - function __spreadArray(to, from, pack) { - if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { - if (ar || !(i in from)) { - if (!ar) ar = Array.prototype.slice.call(from, 0, i); - ar[i] = from[i]; - } - } - return to.concat(ar || Array.prototype.slice.call(from)); - } - - var _global = typeof globalThis !== 'undefined' ? globalThis : - typeof self !== 'undefined' ? self : - typeof window !== 'undefined' ? window : - global; - - var keys = Object.keys; - var isArray = Array.isArray; - if (typeof Promise !== 'undefined' && !_global.Promise) { - _global.Promise = Promise; - } - function extend(obj, extension) { - if (typeof extension !== 'object') - return obj; - keys(extension).forEach(function (key) { - obj[key] = extension[key]; - }); - return obj; - } - var getProto = Object.getPrototypeOf; - var _hasOwn = {}.hasOwnProperty; - function hasOwn(obj, prop) { - return _hasOwn.call(obj, prop); - } - function props(proto, extension) { - if (typeof extension === 'function') - extension = extension(getProto(proto)); - (typeof Reflect === "undefined" ? keys : Reflect.ownKeys)(extension).forEach(function (key) { - setProp(proto, key, extension[key]); - }); - } - var defineProperty = Object.defineProperty; - function setProp(obj, prop, functionOrGetSet, options) { - defineProperty(obj, prop, extend(functionOrGetSet && hasOwn(functionOrGetSet, "get") && typeof functionOrGetSet.get === 'function' ? - { get: functionOrGetSet.get, set: functionOrGetSet.set, configurable: true } : - { value: functionOrGetSet, configurable: true, writable: true }, options)); - } - function derive(Child) { - return { - from: function (Parent) { - Child.prototype = Object.create(Parent.prototype); - setProp(Child.prototype, "constructor", Child); - return { - extend: props.bind(null, Child.prototype) - }; - } - }; - } - var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor; - function getPropertyDescriptor(obj, prop) { - var pd = getOwnPropertyDescriptor(obj, prop); - var proto; - return pd || (proto = getProto(obj)) && getPropertyDescriptor(proto, prop); - } - var _slice = [].slice; - function slice(args, start, end) { - return _slice.call(args, start, end); - } - function override(origFunc, overridedFactory) { - return overridedFactory(origFunc); - } - function assert(b) { - if (!b) - throw new Error("Assertion Failed"); - } - function asap$1(fn) { - if (_global.setImmediate) - setImmediate(fn); - else - setTimeout(fn, 0); - } - function arrayToObject(array, extractor) { - return array.reduce(function (result, item, i) { - var nameAndValue = extractor(item, i); - if (nameAndValue) - result[nameAndValue[0]] = nameAndValue[1]; - return result; - }, {}); - } - function tryCatch(fn, onerror, args) { - try { - fn.apply(null, args); - } - catch (ex) { - onerror && onerror(ex); - } - } - function getByKeyPath(obj, keyPath) { - if (hasOwn(obj, keyPath)) - return obj[keyPath]; - if (!keyPath) - return obj; - if (typeof keyPath !== 'string') { - var rv = []; - for (var i = 0, l = keyPath.length; i < l; ++i) { - var val = getByKeyPath(obj, keyPath[i]); - rv.push(val); - } - return rv; - } - var period = keyPath.indexOf('.'); - if (period !== -1) { - var innerObj = obj[keyPath.substr(0, period)]; - return innerObj === undefined ? undefined : getByKeyPath(innerObj, keyPath.substr(period + 1)); - } - return undefined; - } - function setByKeyPath(obj, keyPath, value) { - if (!obj || keyPath === undefined) - return; - if ('isFrozen' in Object && Object.isFrozen(obj)) - return; - if (typeof keyPath !== 'string' && 'length' in keyPath) { - assert(typeof value !== 'string' && 'length' in value); - for (var i = 0, l = keyPath.length; i < l; ++i) { - setByKeyPath(obj, keyPath[i], value[i]); - } - } - else { - var period = keyPath.indexOf('.'); - if (period !== -1) { - var currentKeyPath = keyPath.substr(0, period); - var remainingKeyPath = keyPath.substr(period + 1); - if (remainingKeyPath === "") - if (value === undefined) { - if (isArray(obj) && !isNaN(parseInt(currentKeyPath))) - obj.splice(currentKeyPath, 1); - else - delete obj[currentKeyPath]; - } - else - obj[currentKeyPath] = value; - else { - var innerObj = obj[currentKeyPath]; - if (!innerObj || !hasOwn(obj, currentKeyPath)) - innerObj = (obj[currentKeyPath] = {}); - setByKeyPath(innerObj, remainingKeyPath, value); - } - } - else { - if (value === undefined) { - if (isArray(obj) && !isNaN(parseInt(keyPath))) - obj.splice(keyPath, 1); - else - delete obj[keyPath]; - } - else - obj[keyPath] = value; - } - } - } - function delByKeyPath(obj, keyPath) { - if (typeof keyPath === 'string') - setByKeyPath(obj, keyPath, undefined); - else if ('length' in keyPath) - [].map.call(keyPath, function (kp) { - setByKeyPath(obj, kp, undefined); - }); - } - function shallowClone(obj) { - var rv = {}; - for (var m in obj) { - if (hasOwn(obj, m)) - rv[m] = obj[m]; - } - return rv; - } - var concat = [].concat; - function flatten(a) { - return concat.apply([], a); - } - var intrinsicTypeNames = "Boolean,String,Date,RegExp,Blob,File,FileList,FileSystemFileHandle,ArrayBuffer,DataView,Uint8ClampedArray,ImageBitmap,ImageData,Map,Set,CryptoKey" - .split(',').concat(flatten([8, 16, 32, 64].map(function (num) { return ["Int", "Uint", "Float"].map(function (t) { return t + num + "Array"; }); }))).filter(function (t) { return _global[t]; }); - var intrinsicTypes = intrinsicTypeNames.map(function (t) { return _global[t]; }); - arrayToObject(intrinsicTypeNames, function (x) { return [x, true]; }); - var circularRefs = null; - function deepClone(any) { - circularRefs = typeof WeakMap !== 'undefined' && new WeakMap(); - var rv = innerDeepClone(any); - circularRefs = null; - return rv; - } - function innerDeepClone(any) { - if (!any || typeof any !== 'object') - return any; - var rv = circularRefs && circularRefs.get(any); - if (rv) - return rv; - if (isArray(any)) { - rv = []; - circularRefs && circularRefs.set(any, rv); - for (var i = 0, l = any.length; i < l; ++i) { - rv.push(innerDeepClone(any[i])); - } - } - else if (intrinsicTypes.indexOf(any.constructor) >= 0) { - rv = any; - } - else { - var proto = getProto(any); - rv = proto === Object.prototype ? {} : Object.create(proto); - circularRefs && circularRefs.set(any, rv); - for (var prop in any) { - if (hasOwn(any, prop)) { - rv[prop] = innerDeepClone(any[prop]); - } - } - } - return rv; - } - var toString = {}.toString; - function toStringTag(o) { - return toString.call(o).slice(8, -1); - } - var iteratorSymbol = typeof Symbol !== 'undefined' ? - Symbol.iterator : - '@@iterator'; - var getIteratorOf = typeof iteratorSymbol === "symbol" ? function (x) { - var i; - return x != null && (i = x[iteratorSymbol]) && i.apply(x); - } : function () { return null; }; - var NO_CHAR_ARRAY = {}; - function getArrayOf(arrayLike) { - var i, a, x, it; - if (arguments.length === 1) { - if (isArray(arrayLike)) - return arrayLike.slice(); - if (this === NO_CHAR_ARRAY && typeof arrayLike === 'string') - return [arrayLike]; - if ((it = getIteratorOf(arrayLike))) { - a = []; - while ((x = it.next()), !x.done) - a.push(x.value); - return a; - } - if (arrayLike == null) - return [arrayLike]; - i = arrayLike.length; - if (typeof i === 'number') { - a = new Array(i); - while (i--) - a[i] = arrayLike[i]; - return a; - } - return [arrayLike]; - } - i = arguments.length; - a = new Array(i); - while (i--) - a[i] = arguments[i]; - return a; - } - var isAsyncFunction = typeof Symbol !== 'undefined' - ? function (fn) { return fn[Symbol.toStringTag] === 'AsyncFunction'; } - : function () { return false; }; - - var debug = typeof location !== 'undefined' && - /^(http|https):\/\/(localhost|127\.0\.0\.1)/.test(location.href); - function setDebug(value, filter) { - debug = value; - libraryFilter = filter; - } - var libraryFilter = function () { return true; }; - var NEEDS_THROW_FOR_STACK = !new Error("").stack; - function getErrorWithStack() { - if (NEEDS_THROW_FOR_STACK) - try { - getErrorWithStack.arguments; - throw new Error(); - } - catch (e) { - return e; - } - return new Error(); - } - function prettyStack(exception, numIgnoredFrames) { - var stack = exception.stack; - if (!stack) - return ""; - numIgnoredFrames = (numIgnoredFrames || 0); - if (stack.indexOf(exception.name) === 0) - numIgnoredFrames += (exception.name + exception.message).split('\n').length; - return stack.split('\n') - .slice(numIgnoredFrames) - .filter(libraryFilter) - .map(function (frame) { return "\n" + frame; }) - .join(''); - } - - var dexieErrorNames = [ - 'Modify', - 'Bulk', - 'OpenFailed', - 'VersionChange', - 'Schema', - 'Upgrade', - 'InvalidTable', - 'MissingAPI', - 'NoSuchDatabase', - 'InvalidArgument', - 'SubTransaction', - 'Unsupported', - 'Internal', - 'DatabaseClosed', - 'PrematureCommit', - 'ForeignAwait' - ]; - var idbDomErrorNames = [ - 'Unknown', - 'Constraint', - 'Data', - 'TransactionInactive', - 'ReadOnly', - 'Version', - 'NotFound', - 'InvalidState', - 'InvalidAccess', - 'Abort', - 'Timeout', - 'QuotaExceeded', - 'Syntax', - 'DataClone' - ]; - var errorList = dexieErrorNames.concat(idbDomErrorNames); - var defaultTexts = { - VersionChanged: "Database version changed by other database connection", - DatabaseClosed: "Database has been closed", - Abort: "Transaction aborted", - TransactionInactive: "Transaction has already completed or failed", - MissingAPI: "IndexedDB API missing. Please visit https://tinyurl.com/y2uuvskb" - }; - function DexieError(name, msg) { - this._e = getErrorWithStack(); - this.name = name; - this.message = msg; - } - derive(DexieError).from(Error).extend({ - stack: { - get: function () { - return this._stack || - (this._stack = this.name + ": " + this.message + prettyStack(this._e, 2)); - } - }, - toString: function () { return this.name + ": " + this.message; } - }); - function getMultiErrorMessage(msg, failures) { - return msg + ". Errors: " + Object.keys(failures) - .map(function (key) { return failures[key].toString(); }) - .filter(function (v, i, s) { return s.indexOf(v) === i; }) - .join('\n'); - } - function ModifyError(msg, failures, successCount, failedKeys) { - this._e = getErrorWithStack(); - this.failures = failures; - this.failedKeys = failedKeys; - this.successCount = successCount; - this.message = getMultiErrorMessage(msg, failures); - } - derive(ModifyError).from(DexieError); - function BulkError(msg, failures) { - this._e = getErrorWithStack(); - this.name = "BulkError"; - this.failures = Object.keys(failures).map(function (pos) { return failures[pos]; }); - this.failuresByPos = failures; - this.message = getMultiErrorMessage(msg, failures); - } - derive(BulkError).from(DexieError); - var errnames = errorList.reduce(function (obj, name) { return (obj[name] = name + "Error", obj); }, {}); - var BaseException = DexieError; - var exceptions = errorList.reduce(function (obj, name) { - var fullName = name + "Error"; - function DexieError(msgOrInner, inner) { - this._e = getErrorWithStack(); - this.name = fullName; - if (!msgOrInner) { - this.message = defaultTexts[name] || fullName; - this.inner = null; - } - else if (typeof msgOrInner === 'string') { - this.message = "" + msgOrInner + (!inner ? '' : '\n ' + inner); - this.inner = inner || null; - } - else if (typeof msgOrInner === 'object') { - this.message = msgOrInner.name + " " + msgOrInner.message; - this.inner = msgOrInner; - } - } - derive(DexieError).from(BaseException); - obj[name] = DexieError; - return obj; - }, {}); - exceptions.Syntax = SyntaxError; - exceptions.Type = TypeError; - exceptions.Range = RangeError; - var exceptionMap = idbDomErrorNames.reduce(function (obj, name) { - obj[name + "Error"] = exceptions[name]; - return obj; - }, {}); - function mapError(domError, message) { - if (!domError || domError instanceof DexieError || domError instanceof TypeError || domError instanceof SyntaxError || !domError.name || !exceptionMap[domError.name]) - return domError; - var rv = new exceptionMap[domError.name](message || domError.message, domError); - if ("stack" in domError) { - setProp(rv, "stack", { get: function () { - return this.inner.stack; - } }); - } - return rv; - } - var fullNameExceptions = errorList.reduce(function (obj, name) { - if (["Syntax", "Type", "Range"].indexOf(name) === -1) - obj[name + "Error"] = exceptions[name]; - return obj; - }, {}); - fullNameExceptions.ModifyError = ModifyError; - fullNameExceptions.DexieError = DexieError; - fullNameExceptions.BulkError = BulkError; - - function nop() { } - function mirror(val) { return val; } - function pureFunctionChain(f1, f2) { - if (f1 == null || f1 === mirror) - return f2; - return function (val) { - return f2(f1(val)); - }; - } - function callBoth(on1, on2) { - return function () { - on1.apply(this, arguments); - on2.apply(this, arguments); - }; - } - function hookCreatingChain(f1, f2) { - if (f1 === nop) - return f2; - return function () { - var res = f1.apply(this, arguments); - if (res !== undefined) - arguments[0] = res; - var onsuccess = this.onsuccess, - onerror = this.onerror; - this.onsuccess = null; - this.onerror = null; - var res2 = f2.apply(this, arguments); - if (onsuccess) - this.onsuccess = this.onsuccess ? callBoth(onsuccess, this.onsuccess) : onsuccess; - if (onerror) - this.onerror = this.onerror ? callBoth(onerror, this.onerror) : onerror; - return res2 !== undefined ? res2 : res; - }; - } - function hookDeletingChain(f1, f2) { - if (f1 === nop) - return f2; - return function () { - f1.apply(this, arguments); - var onsuccess = this.onsuccess, - onerror = this.onerror; - this.onsuccess = this.onerror = null; - f2.apply(this, arguments); - if (onsuccess) - this.onsuccess = this.onsuccess ? callBoth(onsuccess, this.onsuccess) : onsuccess; - if (onerror) - this.onerror = this.onerror ? callBoth(onerror, this.onerror) : onerror; - }; - } - function hookUpdatingChain(f1, f2) { - if (f1 === nop) - return f2; - return function (modifications) { - var res = f1.apply(this, arguments); - extend(modifications, res); - var onsuccess = this.onsuccess, - onerror = this.onerror; - this.onsuccess = null; - this.onerror = null; - var res2 = f2.apply(this, arguments); - if (onsuccess) - this.onsuccess = this.onsuccess ? callBoth(onsuccess, this.onsuccess) : onsuccess; - if (onerror) - this.onerror = this.onerror ? callBoth(onerror, this.onerror) : onerror; - return res === undefined ? - (res2 === undefined ? undefined : res2) : - (extend(res, res2)); - }; - } - function reverseStoppableEventChain(f1, f2) { - if (f1 === nop) - return f2; - return function () { - if (f2.apply(this, arguments) === false) - return false; - return f1.apply(this, arguments); - }; - } - function promisableChain(f1, f2) { - if (f1 === nop) - return f2; - return function () { - var res = f1.apply(this, arguments); - if (res && typeof res.then === 'function') { - var thiz = this, i = arguments.length, args = new Array(i); - while (i--) - args[i] = arguments[i]; - return res.then(function () { - return f2.apply(thiz, args); - }); - } - return f2.apply(this, arguments); - }; - } - - var INTERNAL = {}; - var LONG_STACKS_CLIP_LIMIT = 100, - MAX_LONG_STACKS = 20, ZONE_ECHO_LIMIT = 100, _a$1 = typeof Promise === 'undefined' ? - [] : - (function () { - var globalP = Promise.resolve(); - if (typeof crypto === 'undefined' || !crypto.subtle) - return [globalP, getProto(globalP), globalP]; - var nativeP = crypto.subtle.digest("SHA-512", new Uint8Array([0])); - return [ - nativeP, - getProto(nativeP), - globalP - ]; - })(), resolvedNativePromise = _a$1[0], nativePromiseProto = _a$1[1], resolvedGlobalPromise = _a$1[2], nativePromiseThen = nativePromiseProto && nativePromiseProto.then; - var NativePromise = resolvedNativePromise && resolvedNativePromise.constructor; - var patchGlobalPromise = !!resolvedGlobalPromise; - var stack_being_generated = false; - var schedulePhysicalTick = resolvedGlobalPromise ? - function () { resolvedGlobalPromise.then(physicalTick); } - : - _global.setImmediate ? - setImmediate.bind(null, physicalTick) : - _global.MutationObserver ? - function () { - var hiddenDiv = document.createElement("div"); - (new MutationObserver(function () { - physicalTick(); - hiddenDiv = null; - })).observe(hiddenDiv, { attributes: true }); - hiddenDiv.setAttribute('i', '1'); - } : - function () { setTimeout(physicalTick, 0); }; - var asap = function (callback, args) { - microtickQueue.push([callback, args]); - if (needsNewPhysicalTick) { - schedulePhysicalTick(); - needsNewPhysicalTick = false; - } - }; - var isOutsideMicroTick = true, - needsNewPhysicalTick = true, - unhandledErrors = [], - rejectingErrors = [], - currentFulfiller = null, rejectionMapper = mirror; - var globalPSD = { - id: 'global', - global: true, - ref: 0, - unhandleds: [], - onunhandled: globalError, - pgp: false, - env: {}, - finalize: function () { - this.unhandleds.forEach(function (uh) { - try { - globalError(uh[0], uh[1]); - } - catch (e) { } - }); - } - }; - var PSD = globalPSD; - var microtickQueue = []; - var numScheduledCalls = 0; - var tickFinalizers = []; - function DexiePromise(fn) { - if (typeof this !== 'object') - throw new TypeError('Promises must be constructed via new'); - this._listeners = []; - this.onuncatched = nop; - this._lib = false; - var psd = (this._PSD = PSD); - if (debug) { - this._stackHolder = getErrorWithStack(); - this._prev = null; - this._numPrev = 0; - } - if (typeof fn !== 'function') { - if (fn !== INTERNAL) - throw new TypeError('Not a function'); - this._state = arguments[1]; - this._value = arguments[2]; - if (this._state === false) - handleRejection(this, this._value); - return; - } - this._state = null; - this._value = null; - ++psd.ref; - executePromiseTask(this, fn); - } - var thenProp = { - get: function () { - var psd = PSD, microTaskId = totalEchoes; - function then(onFulfilled, onRejected) { - var _this = this; - var possibleAwait = !psd.global && (psd !== PSD || microTaskId !== totalEchoes); - var cleanup = possibleAwait && !decrementExpectedAwaits(); - var rv = new DexiePromise(function (resolve, reject) { - propagateToListener(_this, new Listener(nativeAwaitCompatibleWrap(onFulfilled, psd, possibleAwait, cleanup), nativeAwaitCompatibleWrap(onRejected, psd, possibleAwait, cleanup), resolve, reject, psd)); - }); - debug && linkToPreviousPromise(rv, this); - return rv; - } - then.prototype = INTERNAL; - return then; - }, - set: function (value) { - setProp(this, 'then', value && value.prototype === INTERNAL ? - thenProp : - { - get: function () { - return value; - }, - set: thenProp.set - }); - } - }; - props(DexiePromise.prototype, { - then: thenProp, - _then: function (onFulfilled, onRejected) { - propagateToListener(this, new Listener(null, null, onFulfilled, onRejected, PSD)); - }, - catch: function (onRejected) { - if (arguments.length === 1) - return this.then(null, onRejected); - var type = arguments[0], handler = arguments[1]; - return typeof type === 'function' ? this.then(null, function (err) { - return err instanceof type ? handler(err) : PromiseReject(err); - }) - : this.then(null, function (err) { - return err && err.name === type ? handler(err) : PromiseReject(err); - }); - }, - finally: function (onFinally) { - return this.then(function (value) { - onFinally(); - return value; - }, function (err) { - onFinally(); - return PromiseReject(err); - }); - }, - stack: { - get: function () { - if (this._stack) - return this._stack; - try { - stack_being_generated = true; - var stacks = getStack(this, [], MAX_LONG_STACKS); - var stack = stacks.join("\nFrom previous: "); - if (this._state !== null) - this._stack = stack; - return stack; - } - finally { - stack_being_generated = false; - } - } - }, - timeout: function (ms, msg) { - var _this = this; - return ms < Infinity ? - new DexiePromise(function (resolve, reject) { - var handle = setTimeout(function () { return reject(new exceptions.Timeout(msg)); }, ms); - _this.then(resolve, reject).finally(clearTimeout.bind(null, handle)); - }) : this; - } - }); - if (typeof Symbol !== 'undefined' && Symbol.toStringTag) - setProp(DexiePromise.prototype, Symbol.toStringTag, 'Dexie.Promise'); - globalPSD.env = snapShot(); - function Listener(onFulfilled, onRejected, resolve, reject, zone) { - this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null; - this.onRejected = typeof onRejected === 'function' ? onRejected : null; - this.resolve = resolve; - this.reject = reject; - this.psd = zone; - } - props(DexiePromise, { - all: function () { - var values = getArrayOf.apply(null, arguments) - .map(onPossibleParallellAsync); - return new DexiePromise(function (resolve, reject) { - if (values.length === 0) - resolve([]); - var remaining = values.length; - values.forEach(function (a, i) { return DexiePromise.resolve(a).then(function (x) { - values[i] = x; - if (!--remaining) - resolve(values); - }, reject); }); - }); - }, - resolve: function (value) { - if (value instanceof DexiePromise) - return value; - if (value && typeof value.then === 'function') - return new DexiePromise(function (resolve, reject) { - value.then(resolve, reject); - }); - var rv = new DexiePromise(INTERNAL, true, value); - linkToPreviousPromise(rv, currentFulfiller); - return rv; - }, - reject: PromiseReject, - race: function () { - var values = getArrayOf.apply(null, arguments).map(onPossibleParallellAsync); - return new DexiePromise(function (resolve, reject) { - values.map(function (value) { return DexiePromise.resolve(value).then(resolve, reject); }); - }); - }, - PSD: { - get: function () { return PSD; }, - set: function (value) { return PSD = value; } - }, - totalEchoes: { get: function () { return totalEchoes; } }, - newPSD: newScope, - usePSD: usePSD, - scheduler: { - get: function () { return asap; }, - set: function (value) { asap = value; } - }, - rejectionMapper: { - get: function () { return rejectionMapper; }, - set: function (value) { rejectionMapper = value; } - }, - follow: function (fn, zoneProps) { - return new DexiePromise(function (resolve, reject) { - return newScope(function (resolve, reject) { - var psd = PSD; - psd.unhandleds = []; - psd.onunhandled = reject; - psd.finalize = callBoth(function () { - var _this = this; - run_at_end_of_this_or_next_physical_tick(function () { - _this.unhandleds.length === 0 ? resolve() : reject(_this.unhandleds[0]); - }); - }, psd.finalize); - fn(); - }, zoneProps, resolve, reject); - }); - } - }); - if (NativePromise) { - if (NativePromise.allSettled) - setProp(DexiePromise, "allSettled", function () { - var possiblePromises = getArrayOf.apply(null, arguments).map(onPossibleParallellAsync); - return new DexiePromise(function (resolve) { - if (possiblePromises.length === 0) - resolve([]); - var remaining = possiblePromises.length; - var results = new Array(remaining); - possiblePromises.forEach(function (p, i) { return DexiePromise.resolve(p).then(function (value) { return results[i] = { status: "fulfilled", value: value }; }, function (reason) { return results[i] = { status: "rejected", reason: reason }; }) - .then(function () { return --remaining || resolve(results); }); }); - }); - }); - if (NativePromise.any && typeof AggregateError !== 'undefined') - setProp(DexiePromise, "any", function () { - var possiblePromises = getArrayOf.apply(null, arguments).map(onPossibleParallellAsync); - return new DexiePromise(function (resolve, reject) { - if (possiblePromises.length === 0) - reject(new AggregateError([])); - var remaining = possiblePromises.length; - var failures = new Array(remaining); - possiblePromises.forEach(function (p, i) { return DexiePromise.resolve(p).then(function (value) { return resolve(value); }, function (failure) { - failures[i] = failure; - if (!--remaining) - reject(new AggregateError(failures)); - }); }); - }); - }); - } - function executePromiseTask(promise, fn) { - try { - fn(function (value) { - if (promise._state !== null) - return; - if (value === promise) - throw new TypeError('A promise cannot be resolved with itself.'); - var shouldExecuteTick = promise._lib && beginMicroTickScope(); - if (value && typeof value.then === 'function') { - executePromiseTask(promise, function (resolve, reject) { - value instanceof DexiePromise ? - value._then(resolve, reject) : - value.then(resolve, reject); - }); - } - else { - promise._state = true; - promise._value = value; - propagateAllListeners(promise); - } - if (shouldExecuteTick) - endMicroTickScope(); - }, handleRejection.bind(null, promise)); - } - catch (ex) { - handleRejection(promise, ex); - } - } - function handleRejection(promise, reason) { - rejectingErrors.push(reason); - if (promise._state !== null) - return; - var shouldExecuteTick = promise._lib && beginMicroTickScope(); - reason = rejectionMapper(reason); - promise._state = false; - promise._value = reason; - debug && reason !== null && typeof reason === 'object' && !reason._promise && tryCatch(function () { - var origProp = getPropertyDescriptor(reason, "stack"); - reason._promise = promise; - setProp(reason, "stack", { - get: function () { - return stack_being_generated ? - origProp && (origProp.get ? - origProp.get.apply(reason) : - origProp.value) : - promise.stack; - } - }); - }); - addPossiblyUnhandledError(promise); - propagateAllListeners(promise); - if (shouldExecuteTick) - endMicroTickScope(); - } - function propagateAllListeners(promise) { - var listeners = promise._listeners; - promise._listeners = []; - for (var i = 0, len = listeners.length; i < len; ++i) { - propagateToListener(promise, listeners[i]); - } - var psd = promise._PSD; - --psd.ref || psd.finalize(); - if (numScheduledCalls === 0) { - ++numScheduledCalls; - asap(function () { - if (--numScheduledCalls === 0) - finalizePhysicalTick(); - }, []); - } - } - function propagateToListener(promise, listener) { - if (promise._state === null) { - promise._listeners.push(listener); - return; - } - var cb = promise._state ? listener.onFulfilled : listener.onRejected; - if (cb === null) { - return (promise._state ? listener.resolve : listener.reject)(promise._value); - } - ++listener.psd.ref; - ++numScheduledCalls; - asap(callListener, [cb, promise, listener]); - } - function callListener(cb, promise, listener) { - try { - currentFulfiller = promise; - var ret, value = promise._value; - if (promise._state) { - ret = cb(value); - } - else { - if (rejectingErrors.length) - rejectingErrors = []; - ret = cb(value); - if (rejectingErrors.indexOf(value) === -1) - markErrorAsHandled(promise); - } - listener.resolve(ret); - } - catch (e) { - listener.reject(e); - } - finally { - currentFulfiller = null; - if (--numScheduledCalls === 0) - finalizePhysicalTick(); - --listener.psd.ref || listener.psd.finalize(); - } - } - function getStack(promise, stacks, limit) { - if (stacks.length === limit) - return stacks; - var stack = ""; - if (promise._state === false) { - var failure = promise._value, errorName, message; - if (failure != null) { - errorName = failure.name || "Error"; - message = failure.message || failure; - stack = prettyStack(failure, 0); - } - else { - errorName = failure; - message = ""; - } - stacks.push(errorName + (message ? ": " + message : "") + stack); - } - if (debug) { - stack = prettyStack(promise._stackHolder, 2); - if (stack && stacks.indexOf(stack) === -1) - stacks.push(stack); - if (promise._prev) - getStack(promise._prev, stacks, limit); - } - return stacks; - } - function linkToPreviousPromise(promise, prev) { - var numPrev = prev ? prev._numPrev + 1 : 0; - if (numPrev < LONG_STACKS_CLIP_LIMIT) { - promise._prev = prev; - promise._numPrev = numPrev; - } - } - function physicalTick() { - beginMicroTickScope() && endMicroTickScope(); - } - function beginMicroTickScope() { - var wasRootExec = isOutsideMicroTick; - isOutsideMicroTick = false; - needsNewPhysicalTick = false; - return wasRootExec; - } - function endMicroTickScope() { - var callbacks, i, l; - do { - while (microtickQueue.length > 0) { - callbacks = microtickQueue; - microtickQueue = []; - l = callbacks.length; - for (i = 0; i < l; ++i) { - var item = callbacks[i]; - item[0].apply(null, item[1]); - } - } - } while (microtickQueue.length > 0); - isOutsideMicroTick = true; - needsNewPhysicalTick = true; - } - function finalizePhysicalTick() { - var unhandledErrs = unhandledErrors; - unhandledErrors = []; - unhandledErrs.forEach(function (p) { - p._PSD.onunhandled.call(null, p._value, p); - }); - var finalizers = tickFinalizers.slice(0); - var i = finalizers.length; - while (i) - finalizers[--i](); - } - function run_at_end_of_this_or_next_physical_tick(fn) { - function finalizer() { - fn(); - tickFinalizers.splice(tickFinalizers.indexOf(finalizer), 1); - } - tickFinalizers.push(finalizer); - ++numScheduledCalls; - asap(function () { - if (--numScheduledCalls === 0) - finalizePhysicalTick(); - }, []); - } - function addPossiblyUnhandledError(promise) { - if (!unhandledErrors.some(function (p) { return p._value === promise._value; })) - unhandledErrors.push(promise); - } - function markErrorAsHandled(promise) { - var i = unhandledErrors.length; - while (i) - if (unhandledErrors[--i]._value === promise._value) { - unhandledErrors.splice(i, 1); - return; - } - } - function PromiseReject(reason) { - return new DexiePromise(INTERNAL, false, reason); - } - function wrap(fn, errorCatcher) { - var psd = PSD; - return function () { - var wasRootExec = beginMicroTickScope(), outerScope = PSD; - try { - switchToZone(psd, true); - return fn.apply(this, arguments); - } - catch (e) { - errorCatcher && errorCatcher(e); - } - finally { - switchToZone(outerScope, false); - if (wasRootExec) - endMicroTickScope(); - } - }; - } - var task = { awaits: 0, echoes: 0, id: 0 }; - var taskCounter = 0; - var zoneStack = []; - var zoneEchoes = 0; - var totalEchoes = 0; - var zone_id_counter = 0; - function newScope(fn, props, a1, a2) { - var parent = PSD, psd = Object.create(parent); - psd.parent = parent; - psd.ref = 0; - psd.global = false; - psd.id = ++zone_id_counter; - var globalEnv = globalPSD.env; - psd.env = patchGlobalPromise ? { - Promise: DexiePromise, - PromiseProp: { value: DexiePromise, configurable: true, writable: true }, - all: DexiePromise.all, - race: DexiePromise.race, - allSettled: DexiePromise.allSettled, - any: DexiePromise.any, - resolve: DexiePromise.resolve, - reject: DexiePromise.reject, - nthen: getPatchedPromiseThen(globalEnv.nthen, psd), - gthen: getPatchedPromiseThen(globalEnv.gthen, psd) - } : {}; - if (props) - extend(psd, props); - ++parent.ref; - psd.finalize = function () { - --this.parent.ref || this.parent.finalize(); - }; - var rv = usePSD(psd, fn, a1, a2); - if (psd.ref === 0) - psd.finalize(); - return rv; - } - function incrementExpectedAwaits() { - if (!task.id) - task.id = ++taskCounter; - ++task.awaits; - task.echoes += ZONE_ECHO_LIMIT; - return task.id; - } - function decrementExpectedAwaits() { - if (!task.awaits) - return false; - if (--task.awaits === 0) - task.id = 0; - task.echoes = task.awaits * ZONE_ECHO_LIMIT; - return true; - } - if (('' + nativePromiseThen).indexOf('[native code]') === -1) { - incrementExpectedAwaits = decrementExpectedAwaits = nop; - } - function onPossibleParallellAsync(possiblePromise) { - if (task.echoes && possiblePromise && possiblePromise.constructor === NativePromise) { - incrementExpectedAwaits(); - return possiblePromise.then(function (x) { - decrementExpectedAwaits(); - return x; - }, function (e) { - decrementExpectedAwaits(); - return rejection(e); - }); - } - return possiblePromise; - } - function zoneEnterEcho(targetZone) { - ++totalEchoes; - if (!task.echoes || --task.echoes === 0) { - task.echoes = task.id = 0; - } - zoneStack.push(PSD); - switchToZone(targetZone, true); - } - function zoneLeaveEcho() { - var zone = zoneStack[zoneStack.length - 1]; - zoneStack.pop(); - switchToZone(zone, false); - } - function switchToZone(targetZone, bEnteringZone) { - var currentZone = PSD; - if (bEnteringZone ? task.echoes && (!zoneEchoes++ || targetZone !== PSD) : zoneEchoes && (!--zoneEchoes || targetZone !== PSD)) { - enqueueNativeMicroTask(bEnteringZone ? zoneEnterEcho.bind(null, targetZone) : zoneLeaveEcho); - } - if (targetZone === PSD) - return; - PSD = targetZone; - if (currentZone === globalPSD) - globalPSD.env = snapShot(); - if (patchGlobalPromise) { - var GlobalPromise_1 = globalPSD.env.Promise; - var targetEnv = targetZone.env; - nativePromiseProto.then = targetEnv.nthen; - GlobalPromise_1.prototype.then = targetEnv.gthen; - if (currentZone.global || targetZone.global) { - Object.defineProperty(_global, 'Promise', targetEnv.PromiseProp); - GlobalPromise_1.all = targetEnv.all; - GlobalPromise_1.race = targetEnv.race; - GlobalPromise_1.resolve = targetEnv.resolve; - GlobalPromise_1.reject = targetEnv.reject; - if (targetEnv.allSettled) - GlobalPromise_1.allSettled = targetEnv.allSettled; - if (targetEnv.any) - GlobalPromise_1.any = targetEnv.any; - } - } - } - function snapShot() { - var GlobalPromise = _global.Promise; - return patchGlobalPromise ? { - Promise: GlobalPromise, - PromiseProp: Object.getOwnPropertyDescriptor(_global, "Promise"), - all: GlobalPromise.all, - race: GlobalPromise.race, - allSettled: GlobalPromise.allSettled, - any: GlobalPromise.any, - resolve: GlobalPromise.resolve, - reject: GlobalPromise.reject, - nthen: nativePromiseProto.then, - gthen: GlobalPromise.prototype.then - } : {}; - } - function usePSD(psd, fn, a1, a2, a3) { - var outerScope = PSD; - try { - switchToZone(psd, true); - return fn(a1, a2, a3); - } - finally { - switchToZone(outerScope, false); - } - } - function enqueueNativeMicroTask(job) { - nativePromiseThen.call(resolvedNativePromise, job); - } - function nativeAwaitCompatibleWrap(fn, zone, possibleAwait, cleanup) { - return typeof fn !== 'function' ? fn : function () { - var outerZone = PSD; - if (possibleAwait) - incrementExpectedAwaits(); - switchToZone(zone, true); - try { - return fn.apply(this, arguments); - } - finally { - switchToZone(outerZone, false); - if (cleanup) - enqueueNativeMicroTask(decrementExpectedAwaits); - } - }; - } - function getPatchedPromiseThen(origThen, zone) { - return function (onResolved, onRejected) { - return origThen.call(this, nativeAwaitCompatibleWrap(onResolved, zone), nativeAwaitCompatibleWrap(onRejected, zone)); - }; - } - var UNHANDLEDREJECTION = "unhandledrejection"; - function globalError(err, promise) { - var rv; - try { - rv = promise.onuncatched(err); - } - catch (e) { } - if (rv !== false) - try { - var event, eventData = { promise: promise, reason: err }; - if (_global.document && document.createEvent) { - event = document.createEvent('Event'); - event.initEvent(UNHANDLEDREJECTION, true, true); - extend(event, eventData); - } - else if (_global.CustomEvent) { - event = new CustomEvent(UNHANDLEDREJECTION, { detail: eventData }); - extend(event, eventData); - } - if (event && _global.dispatchEvent) { - dispatchEvent(event); - if (!_global.PromiseRejectionEvent && _global.onunhandledrejection) - try { - _global.onunhandledrejection(event); - } - catch (_) { } - } - if (debug && event && !event.defaultPrevented) { - console.warn("Unhandled rejection: " + (err.stack || err)); - } - } - catch (e) { } - } - var rejection = DexiePromise.reject; - - function tempTransaction(db, mode, storeNames, fn) { - if (!db.idbdb || (!db._state.openComplete && (!PSD.letThrough && !db._vip))) { - if (db._state.openComplete) { - return rejection(new exceptions.DatabaseClosed(db._state.dbOpenError)); - } - if (!db._state.isBeingOpened) { - if (!db._options.autoOpen) - return rejection(new exceptions.DatabaseClosed()); - db.open().catch(nop); - } - return db._state.dbReadyPromise.then(function () { return tempTransaction(db, mode, storeNames, fn); }); - } - else { - var trans = db._createTransaction(mode, storeNames, db._dbSchema); - try { - trans.create(); - db._state.PR1398_maxLoop = 3; - } - catch (ex) { - if (ex.name === errnames.InvalidState && db.isOpen() && --db._state.PR1398_maxLoop > 0) { - console.warn('Dexie: Need to reopen db'); - db._close(); - return db.open().then(function () { return tempTransaction(db, mode, storeNames, fn); }); - } - return rejection(ex); - } - return trans._promise(mode, function (resolve, reject) { - return newScope(function () { - PSD.trans = trans; - return fn(resolve, reject, trans); - }); - }).then(function (result) { - return trans._completion.then(function () { return result; }); - }); - } - } - - var DEXIE_VERSION = '3.2.4'; - var maxString = String.fromCharCode(65535); - var minKey = -Infinity; - var INVALID_KEY_ARGUMENT = "Invalid key provided. Keys must be of type string, number, Date or Array."; - var STRING_EXPECTED = "String expected."; - var connections = []; - var isIEOrEdge = typeof navigator !== 'undefined' && /(MSIE|Trident|Edge)/.test(navigator.userAgent); - var hasIEDeleteObjectStoreBug = isIEOrEdge; - var hangsOnDeleteLargeKeyRange = isIEOrEdge; - var dexieStackFrameFilter = function (frame) { return !/(dexie\.js|dexie\.min\.js)/.test(frame); }; - var DBNAMES_DB = '__dbnames'; - var READONLY = 'readonly'; - var READWRITE = 'readwrite'; - - function combine(filter1, filter2) { - return filter1 ? - filter2 ? - function () { return filter1.apply(this, arguments) && filter2.apply(this, arguments); } : - filter1 : - filter2; - } - - var AnyRange = { - type: 3 , - lower: -Infinity, - lowerOpen: false, - upper: [[]], - upperOpen: false - }; - - function workaroundForUndefinedPrimKey(keyPath) { - return typeof keyPath === "string" && !/\./.test(keyPath) - ? function (obj) { - if (obj[keyPath] === undefined && (keyPath in obj)) { - obj = deepClone(obj); - delete obj[keyPath]; - } - return obj; - } - : function (obj) { return obj; }; - } - - var Table = (function () { - function Table() { - } - Table.prototype._trans = function (mode, fn, writeLocked) { - var trans = this._tx || PSD.trans; - var tableName = this.name; - function checkTableInTransaction(resolve, reject, trans) { - if (!trans.schema[tableName]) - throw new exceptions.NotFound("Table " + tableName + " not part of transaction"); - return fn(trans.idbtrans, trans); - } - var wasRootExec = beginMicroTickScope(); - try { - return trans && trans.db === this.db ? - trans === PSD.trans ? - trans._promise(mode, checkTableInTransaction, writeLocked) : - newScope(function () { return trans._promise(mode, checkTableInTransaction, writeLocked); }, { trans: trans, transless: PSD.transless || PSD }) : - tempTransaction(this.db, mode, [this.name], checkTableInTransaction); - } - finally { - if (wasRootExec) - endMicroTickScope(); - } - }; - Table.prototype.get = function (keyOrCrit, cb) { - var _this = this; - if (keyOrCrit && keyOrCrit.constructor === Object) - return this.where(keyOrCrit).first(cb); - return this._trans('readonly', function (trans) { - return _this.core.get({ trans: trans, key: keyOrCrit }) - .then(function (res) { return _this.hook.reading.fire(res); }); - }).then(cb); - }; - Table.prototype.where = function (indexOrCrit) { - if (typeof indexOrCrit === 'string') - return new this.db.WhereClause(this, indexOrCrit); - if (isArray(indexOrCrit)) - return new this.db.WhereClause(this, "[" + indexOrCrit.join('+') + "]"); - var keyPaths = keys(indexOrCrit); - if (keyPaths.length === 1) - return this - .where(keyPaths[0]) - .equals(indexOrCrit[keyPaths[0]]); - var compoundIndex = this.schema.indexes.concat(this.schema.primKey).filter(function (ix) { - return ix.compound && - keyPaths.every(function (keyPath) { return ix.keyPath.indexOf(keyPath) >= 0; }) && - ix.keyPath.every(function (keyPath) { return keyPaths.indexOf(keyPath) >= 0; }); - })[0]; - if (compoundIndex && this.db._maxKey !== maxString) - return this - .where(compoundIndex.name) - .equals(compoundIndex.keyPath.map(function (kp) { return indexOrCrit[kp]; })); - if (!compoundIndex && debug) - console.warn("The query " + JSON.stringify(indexOrCrit) + " on " + this.name + " would benefit of a " + - ("compound index [" + keyPaths.join('+') + "]")); - var idxByName = this.schema.idxByName; - var idb = this.db._deps.indexedDB; - function equals(a, b) { - try { - return idb.cmp(a, b) === 0; - } - catch (e) { - return false; - } - } - var _a = keyPaths.reduce(function (_a, keyPath) { - var prevIndex = _a[0], prevFilterFn = _a[1]; - var index = idxByName[keyPath]; - var value = indexOrCrit[keyPath]; - return [ - prevIndex || index, - prevIndex || !index ? - combine(prevFilterFn, index && index.multi ? - function (x) { - var prop = getByKeyPath(x, keyPath); - return isArray(prop) && prop.some(function (item) { return equals(value, item); }); - } : function (x) { return equals(value, getByKeyPath(x, keyPath)); }) - : prevFilterFn - ]; - }, [null, null]), idx = _a[0], filterFunction = _a[1]; - return idx ? - this.where(idx.name).equals(indexOrCrit[idx.keyPath]) - .filter(filterFunction) : - compoundIndex ? - this.filter(filterFunction) : - this.where(keyPaths).equals(''); - }; - Table.prototype.filter = function (filterFunction) { - return this.toCollection().and(filterFunction); - }; - Table.prototype.count = function (thenShortcut) { - return this.toCollection().count(thenShortcut); - }; - Table.prototype.offset = function (offset) { - return this.toCollection().offset(offset); - }; - Table.prototype.limit = function (numRows) { - return this.toCollection().limit(numRows); - }; - Table.prototype.each = function (callback) { - return this.toCollection().each(callback); - }; - Table.prototype.toArray = function (thenShortcut) { - return this.toCollection().toArray(thenShortcut); - }; - Table.prototype.toCollection = function () { - return new this.db.Collection(new this.db.WhereClause(this)); - }; - Table.prototype.orderBy = function (index) { - return new this.db.Collection(new this.db.WhereClause(this, isArray(index) ? - "[" + index.join('+') + "]" : - index)); - }; - Table.prototype.reverse = function () { - return this.toCollection().reverse(); - }; - Table.prototype.mapToClass = function (constructor) { - this.schema.mappedClass = constructor; - var readHook = function (obj) { - if (!obj) - return obj; - var res = Object.create(constructor.prototype); - for (var m in obj) - if (hasOwn(obj, m)) - try { - res[m] = obj[m]; - } - catch (_) { } - return res; - }; - if (this.schema.readHook) { - this.hook.reading.unsubscribe(this.schema.readHook); - } - this.schema.readHook = readHook; - this.hook("reading", readHook); - return constructor; - }; - Table.prototype.defineClass = function () { - function Class(content) { - extend(this, content); - } - return this.mapToClass(Class); - }; - Table.prototype.add = function (obj, key) { - var _this = this; - var _a = this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath; - var objToAdd = obj; - if (keyPath && auto) { - objToAdd = workaroundForUndefinedPrimKey(keyPath)(obj); - } - return this._trans('readwrite', function (trans) { - return _this.core.mutate({ trans: trans, type: 'add', keys: key != null ? [key] : null, values: [objToAdd] }); - }).then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : res.lastResult; }) - .then(function (lastResult) { - if (keyPath) { - try { - setByKeyPath(obj, keyPath, lastResult); - } - catch (_) { } - } - return lastResult; - }); - }; - Table.prototype.update = function (keyOrObject, modifications) { - if (typeof keyOrObject === 'object' && !isArray(keyOrObject)) { - var key = getByKeyPath(keyOrObject, this.schema.primKey.keyPath); - if (key === undefined) - return rejection(new exceptions.InvalidArgument("Given object does not contain its primary key")); - try { - if (typeof modifications !== "function") { - keys(modifications).forEach(function (keyPath) { - setByKeyPath(keyOrObject, keyPath, modifications[keyPath]); - }); - } - else { - modifications(keyOrObject, { value: keyOrObject, primKey: key }); - } - } - catch (_a) { - } - return this.where(":id").equals(key).modify(modifications); - } - else { - return this.where(":id").equals(keyOrObject).modify(modifications); - } - }; - Table.prototype.put = function (obj, key) { - var _this = this; - var _a = this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath; - var objToAdd = obj; - if (keyPath && auto) { - objToAdd = workaroundForUndefinedPrimKey(keyPath)(obj); - } - return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'put', values: [objToAdd], keys: key != null ? [key] : null }); }) - .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : res.lastResult; }) - .then(function (lastResult) { - if (keyPath) { - try { - setByKeyPath(obj, keyPath, lastResult); - } - catch (_) { } - } - return lastResult; - }); - }; - Table.prototype.delete = function (key) { - var _this = this; - return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'delete', keys: [key] }); }) - .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : undefined; }); - }; - Table.prototype.clear = function () { - var _this = this; - return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'deleteRange', range: AnyRange }); }) - .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : undefined; }); - }; - Table.prototype.bulkGet = function (keys) { - var _this = this; - return this._trans('readonly', function (trans) { - return _this.core.getMany({ - keys: keys, - trans: trans - }).then(function (result) { return result.map(function (res) { return _this.hook.reading.fire(res); }); }); - }); - }; - Table.prototype.bulkAdd = function (objects, keysOrOptions, options) { - var _this = this; - var keys = Array.isArray(keysOrOptions) ? keysOrOptions : undefined; - options = options || (keys ? undefined : keysOrOptions); - var wantResults = options ? options.allKeys : undefined; - return this._trans('readwrite', function (trans) { - var _a = _this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath; - if (keyPath && keys) - throw new exceptions.InvalidArgument("bulkAdd(): keys argument invalid on tables with inbound keys"); - if (keys && keys.length !== objects.length) - throw new exceptions.InvalidArgument("Arguments objects and keys must have the same length"); - var numObjects = objects.length; - var objectsToAdd = keyPath && auto ? - objects.map(workaroundForUndefinedPrimKey(keyPath)) : - objects; - return _this.core.mutate({ trans: trans, type: 'add', keys: keys, values: objectsToAdd, wantResults: wantResults }) - .then(function (_a) { - var numFailures = _a.numFailures, results = _a.results, lastResult = _a.lastResult, failures = _a.failures; - var result = wantResults ? results : lastResult; - if (numFailures === 0) - return result; - throw new BulkError(_this.name + ".bulkAdd(): " + numFailures + " of " + numObjects + " operations failed", failures); - }); - }); - }; - Table.prototype.bulkPut = function (objects, keysOrOptions, options) { - var _this = this; - var keys = Array.isArray(keysOrOptions) ? keysOrOptions : undefined; - options = options || (keys ? undefined : keysOrOptions); - var wantResults = options ? options.allKeys : undefined; - return this._trans('readwrite', function (trans) { - var _a = _this.schema.primKey, auto = _a.auto, keyPath = _a.keyPath; - if (keyPath && keys) - throw new exceptions.InvalidArgument("bulkPut(): keys argument invalid on tables with inbound keys"); - if (keys && keys.length !== objects.length) - throw new exceptions.InvalidArgument("Arguments objects and keys must have the same length"); - var numObjects = objects.length; - var objectsToPut = keyPath && auto ? - objects.map(workaroundForUndefinedPrimKey(keyPath)) : - objects; - return _this.core.mutate({ trans: trans, type: 'put', keys: keys, values: objectsToPut, wantResults: wantResults }) - .then(function (_a) { - var numFailures = _a.numFailures, results = _a.results, lastResult = _a.lastResult, failures = _a.failures; - var result = wantResults ? results : lastResult; - if (numFailures === 0) - return result; - throw new BulkError(_this.name + ".bulkPut(): " + numFailures + " of " + numObjects + " operations failed", failures); - }); - }); - }; - Table.prototype.bulkDelete = function (keys) { - var _this = this; - var numKeys = keys.length; - return this._trans('readwrite', function (trans) { - return _this.core.mutate({ trans: trans, type: 'delete', keys: keys }); - }).then(function (_a) { - var numFailures = _a.numFailures, lastResult = _a.lastResult, failures = _a.failures; - if (numFailures === 0) - return lastResult; - throw new BulkError(_this.name + ".bulkDelete(): " + numFailures + " of " + numKeys + " operations failed", failures); - }); - }; - return Table; - }()); - - function Events(ctx) { - var evs = {}; - var rv = function (eventName, subscriber) { - if (subscriber) { - var i = arguments.length, args = new Array(i - 1); - while (--i) - args[i - 1] = arguments[i]; - evs[eventName].subscribe.apply(null, args); - return ctx; - } - else if (typeof (eventName) === 'string') { - return evs[eventName]; - } - }; - rv.addEventType = add; - for (var i = 1, l = arguments.length; i < l; ++i) { - add(arguments[i]); - } - return rv; - function add(eventName, chainFunction, defaultFunction) { - if (typeof eventName === 'object') - return addConfiguredEvents(eventName); - if (!chainFunction) - chainFunction = reverseStoppableEventChain; - if (!defaultFunction) - defaultFunction = nop; - var context = { - subscribers: [], - fire: defaultFunction, - subscribe: function (cb) { - if (context.subscribers.indexOf(cb) === -1) { - context.subscribers.push(cb); - context.fire = chainFunction(context.fire, cb); - } - }, - unsubscribe: function (cb) { - context.subscribers = context.subscribers.filter(function (fn) { return fn !== cb; }); - context.fire = context.subscribers.reduce(chainFunction, defaultFunction); - } - }; - evs[eventName] = rv[eventName] = context; - return context; - } - function addConfiguredEvents(cfg) { - keys(cfg).forEach(function (eventName) { - var args = cfg[eventName]; - if (isArray(args)) { - add(eventName, cfg[eventName][0], cfg[eventName][1]); - } - else if (args === 'asap') { - var context = add(eventName, mirror, function fire() { - var i = arguments.length, args = new Array(i); - while (i--) - args[i] = arguments[i]; - context.subscribers.forEach(function (fn) { - asap$1(function fireEvent() { - fn.apply(null, args); - }); - }); - }); - } - else - throw new exceptions.InvalidArgument("Invalid event config"); - }); - } - } - - function makeClassConstructor(prototype, constructor) { - derive(constructor).from({ prototype: prototype }); - return constructor; - } - - function createTableConstructor(db) { - return makeClassConstructor(Table.prototype, function Table(name, tableSchema, trans) { - this.db = db; - this._tx = trans; - this.name = name; - this.schema = tableSchema; - this.hook = db._allTables[name] ? db._allTables[name].hook : Events(null, { - "creating": [hookCreatingChain, nop], - "reading": [pureFunctionChain, mirror], - "updating": [hookUpdatingChain, nop], - "deleting": [hookDeletingChain, nop] - }); - }); - } - - function isPlainKeyRange(ctx, ignoreLimitFilter) { - return !(ctx.filter || ctx.algorithm || ctx.or) && - (ignoreLimitFilter ? ctx.justLimit : !ctx.replayFilter); - } - function addFilter(ctx, fn) { - ctx.filter = combine(ctx.filter, fn); - } - function addReplayFilter(ctx, factory, isLimitFilter) { - var curr = ctx.replayFilter; - ctx.replayFilter = curr ? function () { return combine(curr(), factory()); } : factory; - ctx.justLimit = isLimitFilter && !curr; - } - function addMatchFilter(ctx, fn) { - ctx.isMatch = combine(ctx.isMatch, fn); - } - function getIndexOrStore(ctx, coreSchema) { - if (ctx.isPrimKey) - return coreSchema.primaryKey; - var index = coreSchema.getIndexByKeyPath(ctx.index); - if (!index) - throw new exceptions.Schema("KeyPath " + ctx.index + " on object store " + coreSchema.name + " is not indexed"); - return index; - } - function openCursor(ctx, coreTable, trans) { - var index = getIndexOrStore(ctx, coreTable.schema); - return coreTable.openCursor({ - trans: trans, - values: !ctx.keysOnly, - reverse: ctx.dir === 'prev', - unique: !!ctx.unique, - query: { - index: index, - range: ctx.range - } - }); - } - function iter(ctx, fn, coreTrans, coreTable) { - var filter = ctx.replayFilter ? combine(ctx.filter, ctx.replayFilter()) : ctx.filter; - if (!ctx.or) { - return iterate(openCursor(ctx, coreTable, coreTrans), combine(ctx.algorithm, filter), fn, !ctx.keysOnly && ctx.valueMapper); - } - else { - var set_1 = {}; - var union = function (item, cursor, advance) { - if (!filter || filter(cursor, advance, function (result) { return cursor.stop(result); }, function (err) { return cursor.fail(err); })) { - var primaryKey = cursor.primaryKey; - var key = '' + primaryKey; - if (key === '[object ArrayBuffer]') - key = '' + new Uint8Array(primaryKey); - if (!hasOwn(set_1, key)) { - set_1[key] = true; - fn(item, cursor, advance); - } - } - }; - return Promise.all([ - ctx.or._iterate(union, coreTrans), - iterate(openCursor(ctx, coreTable, coreTrans), ctx.algorithm, union, !ctx.keysOnly && ctx.valueMapper) - ]); - } - } - function iterate(cursorPromise, filter, fn, valueMapper) { - var mappedFn = valueMapper ? function (x, c, a) { return fn(valueMapper(x), c, a); } : fn; - var wrappedFn = wrap(mappedFn); - return cursorPromise.then(function (cursor) { - if (cursor) { - return cursor.start(function () { - var c = function () { return cursor.continue(); }; - if (!filter || filter(cursor, function (advancer) { return c = advancer; }, function (val) { cursor.stop(val); c = nop; }, function (e) { cursor.fail(e); c = nop; })) - wrappedFn(cursor.value, cursor, function (advancer) { return c = advancer; }); - c(); - }); - } - }); - } - - function cmp(a, b) { - try { - var ta = type(a); - var tb = type(b); - if (ta !== tb) { - if (ta === 'Array') - return 1; - if (tb === 'Array') - return -1; - if (ta === 'binary') - return 1; - if (tb === 'binary') - return -1; - if (ta === 'string') - return 1; - if (tb === 'string') - return -1; - if (ta === 'Date') - return 1; - if (tb !== 'Date') - return NaN; - return -1; - } - switch (ta) { - case 'number': - case 'Date': - case 'string': - return a > b ? 1 : a < b ? -1 : 0; - case 'binary': { - return compareUint8Arrays(getUint8Array(a), getUint8Array(b)); - } - case 'Array': - return compareArrays(a, b); - } - } - catch (_a) { } - return NaN; - } - function compareArrays(a, b) { - var al = a.length; - var bl = b.length; - var l = al < bl ? al : bl; - for (var i = 0; i < l; ++i) { - var res = cmp(a[i], b[i]); - if (res !== 0) - return res; - } - return al === bl ? 0 : al < bl ? -1 : 1; - } - function compareUint8Arrays(a, b) { - var al = a.length; - var bl = b.length; - var l = al < bl ? al : bl; - for (var i = 0; i < l; ++i) { - if (a[i] !== b[i]) - return a[i] < b[i] ? -1 : 1; - } - return al === bl ? 0 : al < bl ? -1 : 1; - } - function type(x) { - var t = typeof x; - if (t !== 'object') - return t; - if (ArrayBuffer.isView(x)) - return 'binary'; - var tsTag = toStringTag(x); - return tsTag === 'ArrayBuffer' ? 'binary' : tsTag; - } - function getUint8Array(a) { - if (a instanceof Uint8Array) - return a; - if (ArrayBuffer.isView(a)) - return new Uint8Array(a.buffer, a.byteOffset, a.byteLength); - return new Uint8Array(a); - } - - var Collection = (function () { - function Collection() { - } - Collection.prototype._read = function (fn, cb) { - var ctx = this._ctx; - return ctx.error ? - ctx.table._trans(null, rejection.bind(null, ctx.error)) : - ctx.table._trans('readonly', fn).then(cb); - }; - Collection.prototype._write = function (fn) { - var ctx = this._ctx; - return ctx.error ? - ctx.table._trans(null, rejection.bind(null, ctx.error)) : - ctx.table._trans('readwrite', fn, "locked"); - }; - Collection.prototype._addAlgorithm = function (fn) { - var ctx = this._ctx; - ctx.algorithm = combine(ctx.algorithm, fn); - }; - Collection.prototype._iterate = function (fn, coreTrans) { - return iter(this._ctx, fn, coreTrans, this._ctx.table.core); - }; - Collection.prototype.clone = function (props) { - var rv = Object.create(this.constructor.prototype), ctx = Object.create(this._ctx); - if (props) - extend(ctx, props); - rv._ctx = ctx; - return rv; - }; - Collection.prototype.raw = function () { - this._ctx.valueMapper = null; - return this; - }; - Collection.prototype.each = function (fn) { - var ctx = this._ctx; - return this._read(function (trans) { return iter(ctx, fn, trans, ctx.table.core); }); - }; - Collection.prototype.count = function (cb) { - var _this = this; - return this._read(function (trans) { - var ctx = _this._ctx; - var coreTable = ctx.table.core; - if (isPlainKeyRange(ctx, true)) { - return coreTable.count({ - trans: trans, - query: { - index: getIndexOrStore(ctx, coreTable.schema), - range: ctx.range - } - }).then(function (count) { return Math.min(count, ctx.limit); }); - } - else { - var count = 0; - return iter(ctx, function () { ++count; return false; }, trans, coreTable) - .then(function () { return count; }); - } - }).then(cb); - }; - Collection.prototype.sortBy = function (keyPath, cb) { - var parts = keyPath.split('.').reverse(), lastPart = parts[0], lastIndex = parts.length - 1; - function getval(obj, i) { - if (i) - return getval(obj[parts[i]], i - 1); - return obj[lastPart]; - } - var order = this._ctx.dir === "next" ? 1 : -1; - function sorter(a, b) { - var aVal = getval(a, lastIndex), bVal = getval(b, lastIndex); - return aVal < bVal ? -order : aVal > bVal ? order : 0; - } - return this.toArray(function (a) { - return a.sort(sorter); - }).then(cb); - }; - Collection.prototype.toArray = function (cb) { - var _this = this; - return this._read(function (trans) { - var ctx = _this._ctx; - if (ctx.dir === 'next' && isPlainKeyRange(ctx, true) && ctx.limit > 0) { - var valueMapper_1 = ctx.valueMapper; - var index = getIndexOrStore(ctx, ctx.table.core.schema); - return ctx.table.core.query({ - trans: trans, - limit: ctx.limit, - values: true, - query: { - index: index, - range: ctx.range - } - }).then(function (_a) { - var result = _a.result; - return valueMapper_1 ? result.map(valueMapper_1) : result; - }); - } - else { - var a_1 = []; - return iter(ctx, function (item) { return a_1.push(item); }, trans, ctx.table.core).then(function () { return a_1; }); - } - }, cb); - }; - Collection.prototype.offset = function (offset) { - var ctx = this._ctx; - if (offset <= 0) - return this; - ctx.offset += offset; - if (isPlainKeyRange(ctx)) { - addReplayFilter(ctx, function () { - var offsetLeft = offset; - return function (cursor, advance) { - if (offsetLeft === 0) - return true; - if (offsetLeft === 1) { - --offsetLeft; - return false; - } - advance(function () { - cursor.advance(offsetLeft); - offsetLeft = 0; - }); - return false; - }; - }); - } - else { - addReplayFilter(ctx, function () { - var offsetLeft = offset; - return function () { return (--offsetLeft < 0); }; - }); - } - return this; - }; - Collection.prototype.limit = function (numRows) { - this._ctx.limit = Math.min(this._ctx.limit, numRows); - addReplayFilter(this._ctx, function () { - var rowsLeft = numRows; - return function (cursor, advance, resolve) { - if (--rowsLeft <= 0) - advance(resolve); - return rowsLeft >= 0; - }; - }, true); - return this; - }; - Collection.prototype.until = function (filterFunction, bIncludeStopEntry) { - addFilter(this._ctx, function (cursor, advance, resolve) { - if (filterFunction(cursor.value)) { - advance(resolve); - return bIncludeStopEntry; - } - else { - return true; - } - }); - return this; - }; - Collection.prototype.first = function (cb) { - return this.limit(1).toArray(function (a) { return a[0]; }).then(cb); - }; - Collection.prototype.last = function (cb) { - return this.reverse().first(cb); - }; - Collection.prototype.filter = function (filterFunction) { - addFilter(this._ctx, function (cursor) { - return filterFunction(cursor.value); - }); - addMatchFilter(this._ctx, filterFunction); - return this; - }; - Collection.prototype.and = function (filter) { - return this.filter(filter); - }; - Collection.prototype.or = function (indexName) { - return new this.db.WhereClause(this._ctx.table, indexName, this); - }; - Collection.prototype.reverse = function () { - this._ctx.dir = (this._ctx.dir === "prev" ? "next" : "prev"); - if (this._ondirectionchange) - this._ondirectionchange(this._ctx.dir); - return this; - }; - Collection.prototype.desc = function () { - return this.reverse(); - }; - Collection.prototype.eachKey = function (cb) { - var ctx = this._ctx; - ctx.keysOnly = !ctx.isMatch; - return this.each(function (val, cursor) { cb(cursor.key, cursor); }); - }; - Collection.prototype.eachUniqueKey = function (cb) { - this._ctx.unique = "unique"; - return this.eachKey(cb); - }; - Collection.prototype.eachPrimaryKey = function (cb) { - var ctx = this._ctx; - ctx.keysOnly = !ctx.isMatch; - return this.each(function (val, cursor) { cb(cursor.primaryKey, cursor); }); - }; - Collection.prototype.keys = function (cb) { - var ctx = this._ctx; - ctx.keysOnly = !ctx.isMatch; - var a = []; - return this.each(function (item, cursor) { - a.push(cursor.key); - }).then(function () { - return a; - }).then(cb); - }; - Collection.prototype.primaryKeys = function (cb) { - var ctx = this._ctx; - if (ctx.dir === 'next' && isPlainKeyRange(ctx, true) && ctx.limit > 0) { - return this._read(function (trans) { - var index = getIndexOrStore(ctx, ctx.table.core.schema); - return ctx.table.core.query({ - trans: trans, - values: false, - limit: ctx.limit, - query: { - index: index, - range: ctx.range - } - }); - }).then(function (_a) { - var result = _a.result; - return result; - }).then(cb); - } - ctx.keysOnly = !ctx.isMatch; - var a = []; - return this.each(function (item, cursor) { - a.push(cursor.primaryKey); - }).then(function () { - return a; - }).then(cb); - }; - Collection.prototype.uniqueKeys = function (cb) { - this._ctx.unique = "unique"; - return this.keys(cb); - }; - Collection.prototype.firstKey = function (cb) { - return this.limit(1).keys(function (a) { return a[0]; }).then(cb); - }; - Collection.prototype.lastKey = function (cb) { - return this.reverse().firstKey(cb); - }; - Collection.prototype.distinct = function () { - var ctx = this._ctx, idx = ctx.index && ctx.table.schema.idxByName[ctx.index]; - if (!idx || !idx.multi) - return this; - var set = {}; - addFilter(this._ctx, function (cursor) { - var strKey = cursor.primaryKey.toString(); - var found = hasOwn(set, strKey); - set[strKey] = true; - return !found; - }); - return this; - }; - Collection.prototype.modify = function (changes) { - var _this = this; - var ctx = this._ctx; - return this._write(function (trans) { - var modifyer; - if (typeof changes === 'function') { - modifyer = changes; - } - else { - var keyPaths = keys(changes); - var numKeys = keyPaths.length; - modifyer = function (item) { - var anythingModified = false; - for (var i = 0; i < numKeys; ++i) { - var keyPath = keyPaths[i], val = changes[keyPath]; - if (getByKeyPath(item, keyPath) !== val) { - setByKeyPath(item, keyPath, val); - anythingModified = true; - } - } - return anythingModified; - }; - } - var coreTable = ctx.table.core; - var _a = coreTable.schema.primaryKey, outbound = _a.outbound, extractKey = _a.extractKey; - var limit = _this.db._options.modifyChunkSize || 200; - var totalFailures = []; - var successCount = 0; - var failedKeys = []; - var applyMutateResult = function (expectedCount, res) { - var failures = res.failures, numFailures = res.numFailures; - successCount += expectedCount - numFailures; - for (var _i = 0, _a = keys(failures); _i < _a.length; _i++) { - var pos = _a[_i]; - totalFailures.push(failures[pos]); - } - }; - return _this.clone().primaryKeys().then(function (keys) { - var nextChunk = function (offset) { - var count = Math.min(limit, keys.length - offset); - return coreTable.getMany({ - trans: trans, - keys: keys.slice(offset, offset + count), - cache: "immutable" - }).then(function (values) { - var addValues = []; - var putValues = []; - var putKeys = outbound ? [] : null; - var deleteKeys = []; - for (var i = 0; i < count; ++i) { - var origValue = values[i]; - var ctx_1 = { - value: deepClone(origValue), - primKey: keys[offset + i] - }; - if (modifyer.call(ctx_1, ctx_1.value, ctx_1) !== false) { - if (ctx_1.value == null) { - deleteKeys.push(keys[offset + i]); - } - else if (!outbound && cmp(extractKey(origValue), extractKey(ctx_1.value)) !== 0) { - deleteKeys.push(keys[offset + i]); - addValues.push(ctx_1.value); - } - else { - putValues.push(ctx_1.value); - if (outbound) - putKeys.push(keys[offset + i]); - } - } - } - var criteria = isPlainKeyRange(ctx) && - ctx.limit === Infinity && - (typeof changes !== 'function' || changes === deleteCallback) && { - index: ctx.index, - range: ctx.range - }; - return Promise.resolve(addValues.length > 0 && - coreTable.mutate({ trans: trans, type: 'add', values: addValues }) - .then(function (res) { - for (var pos in res.failures) { - deleteKeys.splice(parseInt(pos), 1); - } - applyMutateResult(addValues.length, res); - })).then(function () { return (putValues.length > 0 || (criteria && typeof changes === 'object')) && - coreTable.mutate({ - trans: trans, - type: 'put', - keys: putKeys, - values: putValues, - criteria: criteria, - changeSpec: typeof changes !== 'function' - && changes - }).then(function (res) { return applyMutateResult(putValues.length, res); }); }).then(function () { return (deleteKeys.length > 0 || (criteria && changes === deleteCallback)) && - coreTable.mutate({ - trans: trans, - type: 'delete', - keys: deleteKeys, - criteria: criteria - }).then(function (res) { return applyMutateResult(deleteKeys.length, res); }); }).then(function () { - return keys.length > offset + count && nextChunk(offset + limit); - }); - }); - }; - return nextChunk(0).then(function () { - if (totalFailures.length > 0) - throw new ModifyError("Error modifying one or more objects", totalFailures, successCount, failedKeys); - return keys.length; - }); - }); - }); - }; - Collection.prototype.delete = function () { - var ctx = this._ctx, range = ctx.range; - if (isPlainKeyRange(ctx) && - ((ctx.isPrimKey && !hangsOnDeleteLargeKeyRange) || range.type === 3 )) - { - return this._write(function (trans) { - var primaryKey = ctx.table.core.schema.primaryKey; - var coreRange = range; - return ctx.table.core.count({ trans: trans, query: { index: primaryKey, range: coreRange } }).then(function (count) { - return ctx.table.core.mutate({ trans: trans, type: 'deleteRange', range: coreRange }) - .then(function (_a) { - var failures = _a.failures; _a.lastResult; _a.results; var numFailures = _a.numFailures; - if (numFailures) - throw new ModifyError("Could not delete some values", Object.keys(failures).map(function (pos) { return failures[pos]; }), count - numFailures); - return count - numFailures; - }); - }); - }); - } - return this.modify(deleteCallback); - }; - return Collection; - }()); - var deleteCallback = function (value, ctx) { return ctx.value = null; }; - - function createCollectionConstructor(db) { - return makeClassConstructor(Collection.prototype, function Collection(whereClause, keyRangeGenerator) { - this.db = db; - var keyRange = AnyRange, error = null; - if (keyRangeGenerator) - try { - keyRange = keyRangeGenerator(); - } - catch (ex) { - error = ex; - } - var whereCtx = whereClause._ctx; - var table = whereCtx.table; - var readingHook = table.hook.reading.fire; - this._ctx = { - table: table, - index: whereCtx.index, - isPrimKey: (!whereCtx.index || (table.schema.primKey.keyPath && whereCtx.index === table.schema.primKey.name)), - range: keyRange, - keysOnly: false, - dir: "next", - unique: "", - algorithm: null, - filter: null, - replayFilter: null, - justLimit: true, - isMatch: null, - offset: 0, - limit: Infinity, - error: error, - or: whereCtx.or, - valueMapper: readingHook !== mirror ? readingHook : null - }; - }); - } - - function simpleCompare(a, b) { - return a < b ? -1 : a === b ? 0 : 1; - } - function simpleCompareReverse(a, b) { - return a > b ? -1 : a === b ? 0 : 1; - } - - function fail(collectionOrWhereClause, err, T) { - var collection = collectionOrWhereClause instanceof WhereClause ? - new collectionOrWhereClause.Collection(collectionOrWhereClause) : - collectionOrWhereClause; - collection._ctx.error = T ? new T(err) : new TypeError(err); - return collection; - } - function emptyCollection(whereClause) { - return new whereClause.Collection(whereClause, function () { return rangeEqual(""); }).limit(0); - } - function upperFactory(dir) { - return dir === "next" ? - function (s) { return s.toUpperCase(); } : - function (s) { return s.toLowerCase(); }; - } - function lowerFactory(dir) { - return dir === "next" ? - function (s) { return s.toLowerCase(); } : - function (s) { return s.toUpperCase(); }; - } - function nextCasing(key, lowerKey, upperNeedle, lowerNeedle, cmp, dir) { - var length = Math.min(key.length, lowerNeedle.length); - var llp = -1; - for (var i = 0; i < length; ++i) { - var lwrKeyChar = lowerKey[i]; - if (lwrKeyChar !== lowerNeedle[i]) { - if (cmp(key[i], upperNeedle[i]) < 0) - return key.substr(0, i) + upperNeedle[i] + upperNeedle.substr(i + 1); - if (cmp(key[i], lowerNeedle[i]) < 0) - return key.substr(0, i) + lowerNeedle[i] + upperNeedle.substr(i + 1); - if (llp >= 0) - return key.substr(0, llp) + lowerKey[llp] + upperNeedle.substr(llp + 1); - return null; - } - if (cmp(key[i], lwrKeyChar) < 0) - llp = i; - } - if (length < lowerNeedle.length && dir === "next") - return key + upperNeedle.substr(key.length); - if (length < key.length && dir === "prev") - return key.substr(0, upperNeedle.length); - return (llp < 0 ? null : key.substr(0, llp) + lowerNeedle[llp] + upperNeedle.substr(llp + 1)); - } - function addIgnoreCaseAlgorithm(whereClause, match, needles, suffix) { - var upper, lower, compare, upperNeedles, lowerNeedles, direction, nextKeySuffix, needlesLen = needles.length; - if (!needles.every(function (s) { return typeof s === 'string'; })) { - return fail(whereClause, STRING_EXPECTED); - } - function initDirection(dir) { - upper = upperFactory(dir); - lower = lowerFactory(dir); - compare = (dir === "next" ? simpleCompare : simpleCompareReverse); - var needleBounds = needles.map(function (needle) { - return { lower: lower(needle), upper: upper(needle) }; - }).sort(function (a, b) { - return compare(a.lower, b.lower); - }); - upperNeedles = needleBounds.map(function (nb) { return nb.upper; }); - lowerNeedles = needleBounds.map(function (nb) { return nb.lower; }); - direction = dir; - nextKeySuffix = (dir === "next" ? "" : suffix); - } - initDirection("next"); - var c = new whereClause.Collection(whereClause, function () { return createRange(upperNeedles[0], lowerNeedles[needlesLen - 1] + suffix); }); - c._ondirectionchange = function (direction) { - initDirection(direction); - }; - var firstPossibleNeedle = 0; - c._addAlgorithm(function (cursor, advance, resolve) { - var key = cursor.key; - if (typeof key !== 'string') - return false; - var lowerKey = lower(key); - if (match(lowerKey, lowerNeedles, firstPossibleNeedle)) { - return true; - } - else { - var lowestPossibleCasing = null; - for (var i = firstPossibleNeedle; i < needlesLen; ++i) { - var casing = nextCasing(key, lowerKey, upperNeedles[i], lowerNeedles[i], compare, direction); - if (casing === null && lowestPossibleCasing === null) - firstPossibleNeedle = i + 1; - else if (lowestPossibleCasing === null || compare(lowestPossibleCasing, casing) > 0) { - lowestPossibleCasing = casing; - } - } - if (lowestPossibleCasing !== null) { - advance(function () { cursor.continue(lowestPossibleCasing + nextKeySuffix); }); - } - else { - advance(resolve); - } - return false; - } - }); - return c; - } - function createRange(lower, upper, lowerOpen, upperOpen) { - return { - type: 2 , - lower: lower, - upper: upper, - lowerOpen: lowerOpen, - upperOpen: upperOpen - }; - } - function rangeEqual(value) { - return { - type: 1 , - lower: value, - upper: value - }; - } - - var WhereClause = (function () { - function WhereClause() { - } - Object.defineProperty(WhereClause.prototype, "Collection", { - get: function () { - return this._ctx.table.db.Collection; - }, - enumerable: false, - configurable: true - }); - WhereClause.prototype.between = function (lower, upper, includeLower, includeUpper) { - includeLower = includeLower !== false; - includeUpper = includeUpper === true; - try { - if ((this._cmp(lower, upper) > 0) || - (this._cmp(lower, upper) === 0 && (includeLower || includeUpper) && !(includeLower && includeUpper))) - return emptyCollection(this); - return new this.Collection(this, function () { return createRange(lower, upper, !includeLower, !includeUpper); }); - } - catch (e) { - return fail(this, INVALID_KEY_ARGUMENT); - } - }; - WhereClause.prototype.equals = function (value) { - if (value == null) - return fail(this, INVALID_KEY_ARGUMENT); - return new this.Collection(this, function () { return rangeEqual(value); }); - }; - WhereClause.prototype.above = function (value) { - if (value == null) - return fail(this, INVALID_KEY_ARGUMENT); - return new this.Collection(this, function () { return createRange(value, undefined, true); }); - }; - WhereClause.prototype.aboveOrEqual = function (value) { - if (value == null) - return fail(this, INVALID_KEY_ARGUMENT); - return new this.Collection(this, function () { return createRange(value, undefined, false); }); - }; - WhereClause.prototype.below = function (value) { - if (value == null) - return fail(this, INVALID_KEY_ARGUMENT); - return new this.Collection(this, function () { return createRange(undefined, value, false, true); }); - }; - WhereClause.prototype.belowOrEqual = function (value) { - if (value == null) - return fail(this, INVALID_KEY_ARGUMENT); - return new this.Collection(this, function () { return createRange(undefined, value); }); - }; - WhereClause.prototype.startsWith = function (str) { - if (typeof str !== 'string') - return fail(this, STRING_EXPECTED); - return this.between(str, str + maxString, true, true); - }; - WhereClause.prototype.startsWithIgnoreCase = function (str) { - if (str === "") - return this.startsWith(str); - return addIgnoreCaseAlgorithm(this, function (x, a) { return x.indexOf(a[0]) === 0; }, [str], maxString); - }; - WhereClause.prototype.equalsIgnoreCase = function (str) { - return addIgnoreCaseAlgorithm(this, function (x, a) { return x === a[0]; }, [str], ""); - }; - WhereClause.prototype.anyOfIgnoreCase = function () { - var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments); - if (set.length === 0) - return emptyCollection(this); - return addIgnoreCaseAlgorithm(this, function (x, a) { return a.indexOf(x) !== -1; }, set, ""); - }; - WhereClause.prototype.startsWithAnyOfIgnoreCase = function () { - var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments); - if (set.length === 0) - return emptyCollection(this); - return addIgnoreCaseAlgorithm(this, function (x, a) { return a.some(function (n) { return x.indexOf(n) === 0; }); }, set, maxString); - }; - WhereClause.prototype.anyOf = function () { - var _this = this; - var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments); - var compare = this._cmp; - try { - set.sort(compare); - } - catch (e) { - return fail(this, INVALID_KEY_ARGUMENT); - } - if (set.length === 0) - return emptyCollection(this); - var c = new this.Collection(this, function () { return createRange(set[0], set[set.length - 1]); }); - c._ondirectionchange = function (direction) { - compare = (direction === "next" ? - _this._ascending : - _this._descending); - set.sort(compare); - }; - var i = 0; - c._addAlgorithm(function (cursor, advance, resolve) { - var key = cursor.key; - while (compare(key, set[i]) > 0) { - ++i; - if (i === set.length) { - advance(resolve); - return false; - } - } - if (compare(key, set[i]) === 0) { - return true; - } - else { - advance(function () { cursor.continue(set[i]); }); - return false; - } - }); - return c; - }; - WhereClause.prototype.notEqual = function (value) { - return this.inAnyRange([[minKey, value], [value, this.db._maxKey]], { includeLowers: false, includeUppers: false }); - }; - WhereClause.prototype.noneOf = function () { - var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments); - if (set.length === 0) - return new this.Collection(this); - try { - set.sort(this._ascending); - } - catch (e) { - return fail(this, INVALID_KEY_ARGUMENT); - } - var ranges = set.reduce(function (res, val) { return res ? - res.concat([[res[res.length - 1][1], val]]) : - [[minKey, val]]; }, null); - ranges.push([set[set.length - 1], this.db._maxKey]); - return this.inAnyRange(ranges, { includeLowers: false, includeUppers: false }); - }; - WhereClause.prototype.inAnyRange = function (ranges, options) { - var _this = this; - var cmp = this._cmp, ascending = this._ascending, descending = this._descending, min = this._min, max = this._max; - if (ranges.length === 0) - return emptyCollection(this); - if (!ranges.every(function (range) { - return range[0] !== undefined && - range[1] !== undefined && - ascending(range[0], range[1]) <= 0; - })) { - return fail(this, "First argument to inAnyRange() must be an Array of two-value Arrays [lower,upper] where upper must not be lower than lower", exceptions.InvalidArgument); - } - var includeLowers = !options || options.includeLowers !== false; - var includeUppers = options && options.includeUppers === true; - function addRange(ranges, newRange) { - var i = 0, l = ranges.length; - for (; i < l; ++i) { - var range = ranges[i]; - if (cmp(newRange[0], range[1]) < 0 && cmp(newRange[1], range[0]) > 0) { - range[0] = min(range[0], newRange[0]); - range[1] = max(range[1], newRange[1]); - break; - } - } - if (i === l) - ranges.push(newRange); - return ranges; - } - var sortDirection = ascending; - function rangeSorter(a, b) { return sortDirection(a[0], b[0]); } - var set; - try { - set = ranges.reduce(addRange, []); - set.sort(rangeSorter); - } - catch (ex) { - return fail(this, INVALID_KEY_ARGUMENT); - } - var rangePos = 0; - var keyIsBeyondCurrentEntry = includeUppers ? - function (key) { return ascending(key, set[rangePos][1]) > 0; } : - function (key) { return ascending(key, set[rangePos][1]) >= 0; }; - var keyIsBeforeCurrentEntry = includeLowers ? - function (key) { return descending(key, set[rangePos][0]) > 0; } : - function (key) { return descending(key, set[rangePos][0]) >= 0; }; - function keyWithinCurrentRange(key) { - return !keyIsBeyondCurrentEntry(key) && !keyIsBeforeCurrentEntry(key); - } - var checkKey = keyIsBeyondCurrentEntry; - var c = new this.Collection(this, function () { return createRange(set[0][0], set[set.length - 1][1], !includeLowers, !includeUppers); }); - c._ondirectionchange = function (direction) { - if (direction === "next") { - checkKey = keyIsBeyondCurrentEntry; - sortDirection = ascending; - } - else { - checkKey = keyIsBeforeCurrentEntry; - sortDirection = descending; - } - set.sort(rangeSorter); - }; - c._addAlgorithm(function (cursor, advance, resolve) { - var key = cursor.key; - while (checkKey(key)) { - ++rangePos; - if (rangePos === set.length) { - advance(resolve); - return false; - } - } - if (keyWithinCurrentRange(key)) { - return true; - } - else if (_this._cmp(key, set[rangePos][1]) === 0 || _this._cmp(key, set[rangePos][0]) === 0) { - return false; - } - else { - advance(function () { - if (sortDirection === ascending) - cursor.continue(set[rangePos][0]); - else - cursor.continue(set[rangePos][1]); - }); - return false; - } - }); - return c; - }; - WhereClause.prototype.startsWithAnyOf = function () { - var set = getArrayOf.apply(NO_CHAR_ARRAY, arguments); - if (!set.every(function (s) { return typeof s === 'string'; })) { - return fail(this, "startsWithAnyOf() only works with strings"); - } - if (set.length === 0) - return emptyCollection(this); - return this.inAnyRange(set.map(function (str) { return [str, str + maxString]; })); - }; - return WhereClause; - }()); - - function createWhereClauseConstructor(db) { - return makeClassConstructor(WhereClause.prototype, function WhereClause(table, index, orCollection) { - this.db = db; - this._ctx = { - table: table, - index: index === ":id" ? null : index, - or: orCollection - }; - var indexedDB = db._deps.indexedDB; - if (!indexedDB) - throw new exceptions.MissingAPI(); - this._cmp = this._ascending = indexedDB.cmp.bind(indexedDB); - this._descending = function (a, b) { return indexedDB.cmp(b, a); }; - this._max = function (a, b) { return indexedDB.cmp(a, b) > 0 ? a : b; }; - this._min = function (a, b) { return indexedDB.cmp(a, b) < 0 ? a : b; }; - this._IDBKeyRange = db._deps.IDBKeyRange; - }); - } - - function eventRejectHandler(reject) { - return wrap(function (event) { - preventDefault(event); - reject(event.target.error); - return false; - }); - } - function preventDefault(event) { - if (event.stopPropagation) - event.stopPropagation(); - if (event.preventDefault) - event.preventDefault(); - } - - var DEXIE_STORAGE_MUTATED_EVENT_NAME = 'storagemutated'; - var STORAGE_MUTATED_DOM_EVENT_NAME = 'x-storagemutated-1'; - var globalEvents = Events(null, DEXIE_STORAGE_MUTATED_EVENT_NAME); - - var Transaction = (function () { - function Transaction() { - } - Transaction.prototype._lock = function () { - assert(!PSD.global); - ++this._reculock; - if (this._reculock === 1 && !PSD.global) - PSD.lockOwnerFor = this; - return this; - }; - Transaction.prototype._unlock = function () { - assert(!PSD.global); - if (--this._reculock === 0) { - if (!PSD.global) - PSD.lockOwnerFor = null; - while (this._blockedFuncs.length > 0 && !this._locked()) { - var fnAndPSD = this._blockedFuncs.shift(); - try { - usePSD(fnAndPSD[1], fnAndPSD[0]); - } - catch (e) { } - } - } - return this; - }; - Transaction.prototype._locked = function () { - return this._reculock && PSD.lockOwnerFor !== this; - }; - Transaction.prototype.create = function (idbtrans) { - var _this = this; - if (!this.mode) - return this; - var idbdb = this.db.idbdb; - var dbOpenError = this.db._state.dbOpenError; - assert(!this.idbtrans); - if (!idbtrans && !idbdb) { - switch (dbOpenError && dbOpenError.name) { - case "DatabaseClosedError": - throw new exceptions.DatabaseClosed(dbOpenError); - case "MissingAPIError": - throw new exceptions.MissingAPI(dbOpenError.message, dbOpenError); - default: - throw new exceptions.OpenFailed(dbOpenError); - } - } - if (!this.active) - throw new exceptions.TransactionInactive(); - assert(this._completion._state === null); - idbtrans = this.idbtrans = idbtrans || - (this.db.core - ? this.db.core.transaction(this.storeNames, this.mode, { durability: this.chromeTransactionDurability }) - : idbdb.transaction(this.storeNames, this.mode, { durability: this.chromeTransactionDurability })); - idbtrans.onerror = wrap(function (ev) { - preventDefault(ev); - _this._reject(idbtrans.error); - }); - idbtrans.onabort = wrap(function (ev) { - preventDefault(ev); - _this.active && _this._reject(new exceptions.Abort(idbtrans.error)); - _this.active = false; - _this.on("abort").fire(ev); - }); - idbtrans.oncomplete = wrap(function () { - _this.active = false; - _this._resolve(); - if ('mutatedParts' in idbtrans) { - globalEvents.storagemutated.fire(idbtrans["mutatedParts"]); - } - }); - return this; - }; - Transaction.prototype._promise = function (mode, fn, bWriteLock) { - var _this = this; - if (mode === 'readwrite' && this.mode !== 'readwrite') - return rejection(new exceptions.ReadOnly("Transaction is readonly")); - if (!this.active) - return rejection(new exceptions.TransactionInactive()); - if (this._locked()) { - return new DexiePromise(function (resolve, reject) { - _this._blockedFuncs.push([function () { - _this._promise(mode, fn, bWriteLock).then(resolve, reject); - }, PSD]); - }); - } - else if (bWriteLock) { - return newScope(function () { - var p = new DexiePromise(function (resolve, reject) { - _this._lock(); - var rv = fn(resolve, reject, _this); - if (rv && rv.then) - rv.then(resolve, reject); - }); - p.finally(function () { return _this._unlock(); }); - p._lib = true; - return p; - }); - } - else { - var p = new DexiePromise(function (resolve, reject) { - var rv = fn(resolve, reject, _this); - if (rv && rv.then) - rv.then(resolve, reject); - }); - p._lib = true; - return p; - } - }; - Transaction.prototype._root = function () { - return this.parent ? this.parent._root() : this; - }; - Transaction.prototype.waitFor = function (promiseLike) { - var root = this._root(); - var promise = DexiePromise.resolve(promiseLike); - if (root._waitingFor) { - root._waitingFor = root._waitingFor.then(function () { return promise; }); - } - else { - root._waitingFor = promise; - root._waitingQueue = []; - var store = root.idbtrans.objectStore(root.storeNames[0]); - (function spin() { - ++root._spinCount; - while (root._waitingQueue.length) - (root._waitingQueue.shift())(); - if (root._waitingFor) - store.get(-Infinity).onsuccess = spin; - }()); - } - var currentWaitPromise = root._waitingFor; - return new DexiePromise(function (resolve, reject) { - promise.then(function (res) { return root._waitingQueue.push(wrap(resolve.bind(null, res))); }, function (err) { return root._waitingQueue.push(wrap(reject.bind(null, err))); }).finally(function () { - if (root._waitingFor === currentWaitPromise) { - root._waitingFor = null; - } - }); - }); - }; - Transaction.prototype.abort = function () { - if (this.active) { - this.active = false; - if (this.idbtrans) - this.idbtrans.abort(); - this._reject(new exceptions.Abort()); - } - }; - Transaction.prototype.table = function (tableName) { - var memoizedTables = (this._memoizedTables || (this._memoizedTables = {})); - if (hasOwn(memoizedTables, tableName)) - return memoizedTables[tableName]; - var tableSchema = this.schema[tableName]; - if (!tableSchema) { - throw new exceptions.NotFound("Table " + tableName + " not part of transaction"); - } - var transactionBoundTable = new this.db.Table(tableName, tableSchema, this); - transactionBoundTable.core = this.db.core.table(tableName); - memoizedTables[tableName] = transactionBoundTable; - return transactionBoundTable; - }; - return Transaction; - }()); - - function createTransactionConstructor(db) { - return makeClassConstructor(Transaction.prototype, function Transaction(mode, storeNames, dbschema, chromeTransactionDurability, parent) { - var _this = this; - this.db = db; - this.mode = mode; - this.storeNames = storeNames; - this.schema = dbschema; - this.chromeTransactionDurability = chromeTransactionDurability; - this.idbtrans = null; - this.on = Events(this, "complete", "error", "abort"); - this.parent = parent || null; - this.active = true; - this._reculock = 0; - this._blockedFuncs = []; - this._resolve = null; - this._reject = null; - this._waitingFor = null; - this._waitingQueue = null; - this._spinCount = 0; - this._completion = new DexiePromise(function (resolve, reject) { - _this._resolve = resolve; - _this._reject = reject; - }); - this._completion.then(function () { - _this.active = false; - _this.on.complete.fire(); - }, function (e) { - var wasActive = _this.active; - _this.active = false; - _this.on.error.fire(e); - _this.parent ? - _this.parent._reject(e) : - wasActive && _this.idbtrans && _this.idbtrans.abort(); - return rejection(e); - }); - }); - } - - function createIndexSpec(name, keyPath, unique, multi, auto, compound, isPrimKey) { - return { - name: name, - keyPath: keyPath, - unique: unique, - multi: multi, - auto: auto, - compound: compound, - src: (unique && !isPrimKey ? '&' : '') + (multi ? '*' : '') + (auto ? "++" : "") + nameFromKeyPath(keyPath) - }; - } - function nameFromKeyPath(keyPath) { - return typeof keyPath === 'string' ? - keyPath : - keyPath ? ('[' + [].join.call(keyPath, '+') + ']') : ""; - } - - function createTableSchema(name, primKey, indexes) { - return { - name: name, - primKey: primKey, - indexes: indexes, - mappedClass: null, - idxByName: arrayToObject(indexes, function (index) { return [index.name, index]; }) - }; - } - - function safariMultiStoreFix(storeNames) { - return storeNames.length === 1 ? storeNames[0] : storeNames; - } - var getMaxKey = function (IdbKeyRange) { - try { - IdbKeyRange.only([[]]); - getMaxKey = function () { return [[]]; }; - return [[]]; - } - catch (e) { - getMaxKey = function () { return maxString; }; - return maxString; - } - }; - - function getKeyExtractor(keyPath) { - if (keyPath == null) { - return function () { return undefined; }; - } - else if (typeof keyPath === 'string') { - return getSinglePathKeyExtractor(keyPath); - } - else { - return function (obj) { return getByKeyPath(obj, keyPath); }; - } - } - function getSinglePathKeyExtractor(keyPath) { - var split = keyPath.split('.'); - if (split.length === 1) { - return function (obj) { return obj[keyPath]; }; - } - else { - return function (obj) { return getByKeyPath(obj, keyPath); }; - } - } - - function arrayify(arrayLike) { - return [].slice.call(arrayLike); - } - var _id_counter = 0; - function getKeyPathAlias(keyPath) { - return keyPath == null ? - ":id" : - typeof keyPath === 'string' ? - keyPath : - "[" + keyPath.join('+') + "]"; - } - function createDBCore(db, IdbKeyRange, tmpTrans) { - function extractSchema(db, trans) { - var tables = arrayify(db.objectStoreNames); - return { - schema: { - name: db.name, - tables: tables.map(function (table) { return trans.objectStore(table); }).map(function (store) { - var keyPath = store.keyPath, autoIncrement = store.autoIncrement; - var compound = isArray(keyPath); - var outbound = keyPath == null; - var indexByKeyPath = {}; - var result = { - name: store.name, - primaryKey: { - name: null, - isPrimaryKey: true, - outbound: outbound, - compound: compound, - keyPath: keyPath, - autoIncrement: autoIncrement, - unique: true, - extractKey: getKeyExtractor(keyPath) - }, - indexes: arrayify(store.indexNames).map(function (indexName) { return store.index(indexName); }) - .map(function (index) { - var name = index.name, unique = index.unique, multiEntry = index.multiEntry, keyPath = index.keyPath; - var compound = isArray(keyPath); - var result = { - name: name, - compound: compound, - keyPath: keyPath, - unique: unique, - multiEntry: multiEntry, - extractKey: getKeyExtractor(keyPath) - }; - indexByKeyPath[getKeyPathAlias(keyPath)] = result; - return result; - }), - getIndexByKeyPath: function (keyPath) { return indexByKeyPath[getKeyPathAlias(keyPath)]; } - }; - indexByKeyPath[":id"] = result.primaryKey; - if (keyPath != null) { - indexByKeyPath[getKeyPathAlias(keyPath)] = result.primaryKey; - } - return result; - }) - }, - hasGetAll: tables.length > 0 && ('getAll' in trans.objectStore(tables[0])) && - !(typeof navigator !== 'undefined' && /Safari/.test(navigator.userAgent) && - !/(Chrome\/|Edge\/)/.test(navigator.userAgent) && - [].concat(navigator.userAgent.match(/Safari\/(\d*)/))[1] < 604) - }; - } - function makeIDBKeyRange(range) { - if (range.type === 3 ) - return null; - if (range.type === 4 ) - throw new Error("Cannot convert never type to IDBKeyRange"); - var lower = range.lower, upper = range.upper, lowerOpen = range.lowerOpen, upperOpen = range.upperOpen; - var idbRange = lower === undefined ? - upper === undefined ? - null : - IdbKeyRange.upperBound(upper, !!upperOpen) : - upper === undefined ? - IdbKeyRange.lowerBound(lower, !!lowerOpen) : - IdbKeyRange.bound(lower, upper, !!lowerOpen, !!upperOpen); - return idbRange; - } - function createDbCoreTable(tableSchema) { - var tableName = tableSchema.name; - function mutate(_a) { - var trans = _a.trans, type = _a.type, keys = _a.keys, values = _a.values, range = _a.range; - return new Promise(function (resolve, reject) { - resolve = wrap(resolve); - var store = trans.objectStore(tableName); - var outbound = store.keyPath == null; - var isAddOrPut = type === "put" || type === "add"; - if (!isAddOrPut && type !== 'delete' && type !== 'deleteRange') - throw new Error("Invalid operation type: " + type); - var length = (keys || values || { length: 1 }).length; - if (keys && values && keys.length !== values.length) { - throw new Error("Given keys array must have same length as given values array."); - } - if (length === 0) - return resolve({ numFailures: 0, failures: {}, results: [], lastResult: undefined }); - var req; - var reqs = []; - var failures = []; - var numFailures = 0; - var errorHandler = function (event) { - ++numFailures; - preventDefault(event); - }; - if (type === 'deleteRange') { - if (range.type === 4 ) - return resolve({ numFailures: numFailures, failures: failures, results: [], lastResult: undefined }); - if (range.type === 3 ) - reqs.push(req = store.clear()); - else - reqs.push(req = store.delete(makeIDBKeyRange(range))); - } - else { - var _a = isAddOrPut ? - outbound ? - [values, keys] : - [values, null] : - [keys, null], args1 = _a[0], args2 = _a[1]; - if (isAddOrPut) { - for (var i = 0; i < length; ++i) { - reqs.push(req = (args2 && args2[i] !== undefined ? - store[type](args1[i], args2[i]) : - store[type](args1[i]))); - req.onerror = errorHandler; - } - } - else { - for (var i = 0; i < length; ++i) { - reqs.push(req = store[type](args1[i])); - req.onerror = errorHandler; - } - } - } - var done = function (event) { - var lastResult = event.target.result; - reqs.forEach(function (req, i) { return req.error != null && (failures[i] = req.error); }); - resolve({ - numFailures: numFailures, - failures: failures, - results: type === "delete" ? keys : reqs.map(function (req) { return req.result; }), - lastResult: lastResult - }); - }; - req.onerror = function (event) { - errorHandler(event); - done(event); - }; - req.onsuccess = done; - }); - } - function openCursor(_a) { - var trans = _a.trans, values = _a.values, query = _a.query, reverse = _a.reverse, unique = _a.unique; - return new Promise(function (resolve, reject) { - resolve = wrap(resolve); - var index = query.index, range = query.range; - var store = trans.objectStore(tableName); - var source = index.isPrimaryKey ? - store : - store.index(index.name); - var direction = reverse ? - unique ? - "prevunique" : - "prev" : - unique ? - "nextunique" : - "next"; - var req = values || !('openKeyCursor' in source) ? - source.openCursor(makeIDBKeyRange(range), direction) : - source.openKeyCursor(makeIDBKeyRange(range), direction); - req.onerror = eventRejectHandler(reject); - req.onsuccess = wrap(function (ev) { - var cursor = req.result; - if (!cursor) { - resolve(null); - return; - } - cursor.___id = ++_id_counter; - cursor.done = false; - var _cursorContinue = cursor.continue.bind(cursor); - var _cursorContinuePrimaryKey = cursor.continuePrimaryKey; - if (_cursorContinuePrimaryKey) - _cursorContinuePrimaryKey = _cursorContinuePrimaryKey.bind(cursor); - var _cursorAdvance = cursor.advance.bind(cursor); - var doThrowCursorIsNotStarted = function () { throw new Error("Cursor not started"); }; - var doThrowCursorIsStopped = function () { throw new Error("Cursor not stopped"); }; - cursor.trans = trans; - cursor.stop = cursor.continue = cursor.continuePrimaryKey = cursor.advance = doThrowCursorIsNotStarted; - cursor.fail = wrap(reject); - cursor.next = function () { - var _this = this; - var gotOne = 1; - return this.start(function () { return gotOne-- ? _this.continue() : _this.stop(); }).then(function () { return _this; }); - }; - cursor.start = function (callback) { - var iterationPromise = new Promise(function (resolveIteration, rejectIteration) { - resolveIteration = wrap(resolveIteration); - req.onerror = eventRejectHandler(rejectIteration); - cursor.fail = rejectIteration; - cursor.stop = function (value) { - cursor.stop = cursor.continue = cursor.continuePrimaryKey = cursor.advance = doThrowCursorIsStopped; - resolveIteration(value); - }; - }); - var guardedCallback = function () { - if (req.result) { - try { - callback(); - } - catch (err) { - cursor.fail(err); - } - } - else { - cursor.done = true; - cursor.start = function () { throw new Error("Cursor behind last entry"); }; - cursor.stop(); - } - }; - req.onsuccess = wrap(function (ev) { - req.onsuccess = guardedCallback; - guardedCallback(); - }); - cursor.continue = _cursorContinue; - cursor.continuePrimaryKey = _cursorContinuePrimaryKey; - cursor.advance = _cursorAdvance; - guardedCallback(); - return iterationPromise; - }; - resolve(cursor); - }, reject); - }); - } - function query(hasGetAll) { - return function (request) { - return new Promise(function (resolve, reject) { - resolve = wrap(resolve); - var trans = request.trans, values = request.values, limit = request.limit, query = request.query; - var nonInfinitLimit = limit === Infinity ? undefined : limit; - var index = query.index, range = query.range; - var store = trans.objectStore(tableName); - var source = index.isPrimaryKey ? store : store.index(index.name); - var idbKeyRange = makeIDBKeyRange(range); - if (limit === 0) - return resolve({ result: [] }); - if (hasGetAll) { - var req = values ? - source.getAll(idbKeyRange, nonInfinitLimit) : - source.getAllKeys(idbKeyRange, nonInfinitLimit); - req.onsuccess = function (event) { return resolve({ result: event.target.result }); }; - req.onerror = eventRejectHandler(reject); - } - else { - var count_1 = 0; - var req_1 = values || !('openKeyCursor' in source) ? - source.openCursor(idbKeyRange) : - source.openKeyCursor(idbKeyRange); - var result_1 = []; - req_1.onsuccess = function (event) { - var cursor = req_1.result; - if (!cursor) - return resolve({ result: result_1 }); - result_1.push(values ? cursor.value : cursor.primaryKey); - if (++count_1 === limit) - return resolve({ result: result_1 }); - cursor.continue(); - }; - req_1.onerror = eventRejectHandler(reject); - } - }); - }; - } - return { - name: tableName, - schema: tableSchema, - mutate: mutate, - getMany: function (_a) { - var trans = _a.trans, keys = _a.keys; - return new Promise(function (resolve, reject) { - resolve = wrap(resolve); - var store = trans.objectStore(tableName); - var length = keys.length; - var result = new Array(length); - var keyCount = 0; - var callbackCount = 0; - var req; - var successHandler = function (event) { - var req = event.target; - if ((result[req._pos] = req.result) != null) - ; - if (++callbackCount === keyCount) - resolve(result); - }; - var errorHandler = eventRejectHandler(reject); - for (var i = 0; i < length; ++i) { - var key = keys[i]; - if (key != null) { - req = store.get(keys[i]); - req._pos = i; - req.onsuccess = successHandler; - req.onerror = errorHandler; - ++keyCount; - } - } - if (keyCount === 0) - resolve(result); - }); - }, - get: function (_a) { - var trans = _a.trans, key = _a.key; - return new Promise(function (resolve, reject) { - resolve = wrap(resolve); - var store = trans.objectStore(tableName); - var req = store.get(key); - req.onsuccess = function (event) { return resolve(event.target.result); }; - req.onerror = eventRejectHandler(reject); - }); - }, - query: query(hasGetAll), - openCursor: openCursor, - count: function (_a) { - var query = _a.query, trans = _a.trans; - var index = query.index, range = query.range; - return new Promise(function (resolve, reject) { - var store = trans.objectStore(tableName); - var source = index.isPrimaryKey ? store : store.index(index.name); - var idbKeyRange = makeIDBKeyRange(range); - var req = idbKeyRange ? source.count(idbKeyRange) : source.count(); - req.onsuccess = wrap(function (ev) { return resolve(ev.target.result); }); - req.onerror = eventRejectHandler(reject); - }); - } - }; - } - var _a = extractSchema(db, tmpTrans), schema = _a.schema, hasGetAll = _a.hasGetAll; - var tables = schema.tables.map(function (tableSchema) { return createDbCoreTable(tableSchema); }); - var tableMap = {}; - tables.forEach(function (table) { return tableMap[table.name] = table; }); - return { - stack: "dbcore", - transaction: db.transaction.bind(db), - table: function (name) { - var result = tableMap[name]; - if (!result) - throw new Error("Table '" + name + "' not found"); - return tableMap[name]; - }, - MIN_KEY: -Infinity, - MAX_KEY: getMaxKey(IdbKeyRange), - schema: schema - }; - } - - function createMiddlewareStack(stackImpl, middlewares) { - return middlewares.reduce(function (down, _a) { - var create = _a.create; - return (__assign(__assign({}, down), create(down))); - }, stackImpl); - } - function createMiddlewareStacks(middlewares, idbdb, _a, tmpTrans) { - var IDBKeyRange = _a.IDBKeyRange; _a.indexedDB; - var dbcore = createMiddlewareStack(createDBCore(idbdb, IDBKeyRange, tmpTrans), middlewares.dbcore); - return { - dbcore: dbcore - }; - } - function generateMiddlewareStacks(_a, tmpTrans) { - var db = _a._novip; - var idbdb = tmpTrans.db; - var stacks = createMiddlewareStacks(db._middlewares, idbdb, db._deps, tmpTrans); - db.core = stacks.dbcore; - db.tables.forEach(function (table) { - var tableName = table.name; - if (db.core.schema.tables.some(function (tbl) { return tbl.name === tableName; })) { - table.core = db.core.table(tableName); - if (db[tableName] instanceof db.Table) { - db[tableName].core = table.core; - } - } - }); - } - - function setApiOnPlace(_a, objs, tableNames, dbschema) { - var db = _a._novip; - tableNames.forEach(function (tableName) { - var schema = dbschema[tableName]; - objs.forEach(function (obj) { - var propDesc = getPropertyDescriptor(obj, tableName); - if (!propDesc || ("value" in propDesc && propDesc.value === undefined)) { - if (obj === db.Transaction.prototype || obj instanceof db.Transaction) { - setProp(obj, tableName, { - get: function () { return this.table(tableName); }, - set: function (value) { - defineProperty(this, tableName, { value: value, writable: true, configurable: true, enumerable: true }); - } - }); - } - else { - obj[tableName] = new db.Table(tableName, schema); - } - } - }); - }); - } - function removeTablesApi(_a, objs) { - var db = _a._novip; - objs.forEach(function (obj) { - for (var key in obj) { - if (obj[key] instanceof db.Table) - delete obj[key]; - } - }); - } - function lowerVersionFirst(a, b) { - return a._cfg.version - b._cfg.version; - } - function runUpgraders(db, oldVersion, idbUpgradeTrans, reject) { - var globalSchema = db._dbSchema; - var trans = db._createTransaction('readwrite', db._storeNames, globalSchema); - trans.create(idbUpgradeTrans); - trans._completion.catch(reject); - var rejectTransaction = trans._reject.bind(trans); - var transless = PSD.transless || PSD; - newScope(function () { - PSD.trans = trans; - PSD.transless = transless; - if (oldVersion === 0) { - keys(globalSchema).forEach(function (tableName) { - createTable(idbUpgradeTrans, tableName, globalSchema[tableName].primKey, globalSchema[tableName].indexes); - }); - generateMiddlewareStacks(db, idbUpgradeTrans); - DexiePromise.follow(function () { return db.on.populate.fire(trans); }).catch(rejectTransaction); - } - else - updateTablesAndIndexes(db, oldVersion, trans, idbUpgradeTrans).catch(rejectTransaction); - }); - } - function updateTablesAndIndexes(_a, oldVersion, trans, idbUpgradeTrans) { - var db = _a._novip; - var queue = []; - var versions = db._versions; - var globalSchema = db._dbSchema = buildGlobalSchema(db, db.idbdb, idbUpgradeTrans); - var anyContentUpgraderHasRun = false; - var versToRun = versions.filter(function (v) { return v._cfg.version >= oldVersion; }); - versToRun.forEach(function (version) { - queue.push(function () { - var oldSchema = globalSchema; - var newSchema = version._cfg.dbschema; - adjustToExistingIndexNames(db, oldSchema, idbUpgradeTrans); - adjustToExistingIndexNames(db, newSchema, idbUpgradeTrans); - globalSchema = db._dbSchema = newSchema; - var diff = getSchemaDiff(oldSchema, newSchema); - diff.add.forEach(function (tuple) { - createTable(idbUpgradeTrans, tuple[0], tuple[1].primKey, tuple[1].indexes); - }); - diff.change.forEach(function (change) { - if (change.recreate) { - throw new exceptions.Upgrade("Not yet support for changing primary key"); - } - else { - var store_1 = idbUpgradeTrans.objectStore(change.name); - change.add.forEach(function (idx) { return addIndex(store_1, idx); }); - change.change.forEach(function (idx) { - store_1.deleteIndex(idx.name); - addIndex(store_1, idx); - }); - change.del.forEach(function (idxName) { return store_1.deleteIndex(idxName); }); - } - }); - var contentUpgrade = version._cfg.contentUpgrade; - if (contentUpgrade && version._cfg.version > oldVersion) { - generateMiddlewareStacks(db, idbUpgradeTrans); - trans._memoizedTables = {}; - anyContentUpgraderHasRun = true; - var upgradeSchema_1 = shallowClone(newSchema); - diff.del.forEach(function (table) { - upgradeSchema_1[table] = oldSchema[table]; - }); - removeTablesApi(db, [db.Transaction.prototype]); - setApiOnPlace(db, [db.Transaction.prototype], keys(upgradeSchema_1), upgradeSchema_1); - trans.schema = upgradeSchema_1; - var contentUpgradeIsAsync_1 = isAsyncFunction(contentUpgrade); - if (contentUpgradeIsAsync_1) { - incrementExpectedAwaits(); - } - var returnValue_1; - var promiseFollowed = DexiePromise.follow(function () { - returnValue_1 = contentUpgrade(trans); - if (returnValue_1) { - if (contentUpgradeIsAsync_1) { - var decrementor = decrementExpectedAwaits.bind(null, null); - returnValue_1.then(decrementor, decrementor); - } - } - }); - return (returnValue_1 && typeof returnValue_1.then === 'function' ? - DexiePromise.resolve(returnValue_1) : promiseFollowed.then(function () { return returnValue_1; })); - } - }); - queue.push(function (idbtrans) { - if (!anyContentUpgraderHasRun || !hasIEDeleteObjectStoreBug) { - var newSchema = version._cfg.dbschema; - deleteRemovedTables(newSchema, idbtrans); - } - removeTablesApi(db, [db.Transaction.prototype]); - setApiOnPlace(db, [db.Transaction.prototype], db._storeNames, db._dbSchema); - trans.schema = db._dbSchema; - }); - }); - function runQueue() { - return queue.length ? DexiePromise.resolve(queue.shift()(trans.idbtrans)).then(runQueue) : - DexiePromise.resolve(); - } - return runQueue().then(function () { - createMissingTables(globalSchema, idbUpgradeTrans); - }); - } - function getSchemaDiff(oldSchema, newSchema) { - var diff = { - del: [], - add: [], - change: [] - }; - var table; - for (table in oldSchema) { - if (!newSchema[table]) - diff.del.push(table); - } - for (table in newSchema) { - var oldDef = oldSchema[table], newDef = newSchema[table]; - if (!oldDef) { - diff.add.push([table, newDef]); - } - else { - var change = { - name: table, - def: newDef, - recreate: false, - del: [], - add: [], - change: [] - }; - if (( - '' + (oldDef.primKey.keyPath || '')) !== ('' + (newDef.primKey.keyPath || '')) || - (oldDef.primKey.auto !== newDef.primKey.auto && !isIEOrEdge)) - { - change.recreate = true; - diff.change.push(change); - } - else { - var oldIndexes = oldDef.idxByName; - var newIndexes = newDef.idxByName; - var idxName = void 0; - for (idxName in oldIndexes) { - if (!newIndexes[idxName]) - change.del.push(idxName); - } - for (idxName in newIndexes) { - var oldIdx = oldIndexes[idxName], newIdx = newIndexes[idxName]; - if (!oldIdx) - change.add.push(newIdx); - else if (oldIdx.src !== newIdx.src) - change.change.push(newIdx); - } - if (change.del.length > 0 || change.add.length > 0 || change.change.length > 0) { - diff.change.push(change); - } - } - } - } - return diff; - } - function createTable(idbtrans, tableName, primKey, indexes) { - var store = idbtrans.db.createObjectStore(tableName, primKey.keyPath ? - { keyPath: primKey.keyPath, autoIncrement: primKey.auto } : - { autoIncrement: primKey.auto }); - indexes.forEach(function (idx) { return addIndex(store, idx); }); - return store; - } - function createMissingTables(newSchema, idbtrans) { - keys(newSchema).forEach(function (tableName) { - if (!idbtrans.db.objectStoreNames.contains(tableName)) { - createTable(idbtrans, tableName, newSchema[tableName].primKey, newSchema[tableName].indexes); - } - }); - } - function deleteRemovedTables(newSchema, idbtrans) { - [].slice.call(idbtrans.db.objectStoreNames).forEach(function (storeName) { - return newSchema[storeName] == null && idbtrans.db.deleteObjectStore(storeName); - }); - } - function addIndex(store, idx) { - store.createIndex(idx.name, idx.keyPath, { unique: idx.unique, multiEntry: idx.multi }); - } - function buildGlobalSchema(db, idbdb, tmpTrans) { - var globalSchema = {}; - var dbStoreNames = slice(idbdb.objectStoreNames, 0); - dbStoreNames.forEach(function (storeName) { - var store = tmpTrans.objectStore(storeName); - var keyPath = store.keyPath; - var primKey = createIndexSpec(nameFromKeyPath(keyPath), keyPath || "", false, false, !!store.autoIncrement, keyPath && typeof keyPath !== "string", true); - var indexes = []; - for (var j = 0; j < store.indexNames.length; ++j) { - var idbindex = store.index(store.indexNames[j]); - keyPath = idbindex.keyPath; - var index = createIndexSpec(idbindex.name, keyPath, !!idbindex.unique, !!idbindex.multiEntry, false, keyPath && typeof keyPath !== "string", false); - indexes.push(index); - } - globalSchema[storeName] = createTableSchema(storeName, primKey, indexes); - }); - return globalSchema; - } - function readGlobalSchema(_a, idbdb, tmpTrans) { - var db = _a._novip; - db.verno = idbdb.version / 10; - var globalSchema = db._dbSchema = buildGlobalSchema(db, idbdb, tmpTrans); - db._storeNames = slice(idbdb.objectStoreNames, 0); - setApiOnPlace(db, [db._allTables], keys(globalSchema), globalSchema); - } - function verifyInstalledSchema(db, tmpTrans) { - var installedSchema = buildGlobalSchema(db, db.idbdb, tmpTrans); - var diff = getSchemaDiff(installedSchema, db._dbSchema); - return !(diff.add.length || diff.change.some(function (ch) { return ch.add.length || ch.change.length; })); - } - function adjustToExistingIndexNames(_a, schema, idbtrans) { - var db = _a._novip; - var storeNames = idbtrans.db.objectStoreNames; - for (var i = 0; i < storeNames.length; ++i) { - var storeName = storeNames[i]; - var store = idbtrans.objectStore(storeName); - db._hasGetAll = 'getAll' in store; - for (var j = 0; j < store.indexNames.length; ++j) { - var indexName = store.indexNames[j]; - var keyPath = store.index(indexName).keyPath; - var dexieName = typeof keyPath === 'string' ? keyPath : "[" + slice(keyPath).join('+') + "]"; - if (schema[storeName]) { - var indexSpec = schema[storeName].idxByName[dexieName]; - if (indexSpec) { - indexSpec.name = indexName; - delete schema[storeName].idxByName[dexieName]; - schema[storeName].idxByName[indexName] = indexSpec; - } - } - } - } - if (typeof navigator !== 'undefined' && /Safari/.test(navigator.userAgent) && - !/(Chrome\/|Edge\/)/.test(navigator.userAgent) && - _global.WorkerGlobalScope && _global instanceof _global.WorkerGlobalScope && - [].concat(navigator.userAgent.match(/Safari\/(\d*)/))[1] < 604) { - db._hasGetAll = false; - } - } - function parseIndexSyntax(primKeyAndIndexes) { - return primKeyAndIndexes.split(',').map(function (index, indexNum) { - index = index.trim(); - var name = index.replace(/([&*]|\+\+)/g, ""); - var keyPath = /^\[/.test(name) ? name.match(/^\[(.*)\]$/)[1].split('+') : name; - return createIndexSpec(name, keyPath || null, /\&/.test(index), /\*/.test(index), /\+\+/.test(index), isArray(keyPath), indexNum === 0); - }); - } - - var Version = (function () { - function Version() { - } - Version.prototype._parseStoresSpec = function (stores, outSchema) { - keys(stores).forEach(function (tableName) { - if (stores[tableName] !== null) { - var indexes = parseIndexSyntax(stores[tableName]); - var primKey = indexes.shift(); - if (primKey.multi) - throw new exceptions.Schema("Primary key cannot be multi-valued"); - indexes.forEach(function (idx) { - if (idx.auto) - throw new exceptions.Schema("Only primary key can be marked as autoIncrement (++)"); - if (!idx.keyPath) - throw new exceptions.Schema("Index must have a name and cannot be an empty string"); - }); - outSchema[tableName] = createTableSchema(tableName, primKey, indexes); - } - }); - }; - Version.prototype.stores = function (stores) { - var db = this.db; - this._cfg.storesSource = this._cfg.storesSource ? - extend(this._cfg.storesSource, stores) : - stores; - var versions = db._versions; - var storesSpec = {}; - var dbschema = {}; - versions.forEach(function (version) { - extend(storesSpec, version._cfg.storesSource); - dbschema = (version._cfg.dbschema = {}); - version._parseStoresSpec(storesSpec, dbschema); - }); - db._dbSchema = dbschema; - removeTablesApi(db, [db._allTables, db, db.Transaction.prototype]); - setApiOnPlace(db, [db._allTables, db, db.Transaction.prototype, this._cfg.tables], keys(dbschema), dbschema); - db._storeNames = keys(dbschema); - return this; - }; - Version.prototype.upgrade = function (upgradeFunction) { - this._cfg.contentUpgrade = promisableChain(this._cfg.contentUpgrade || nop, upgradeFunction); - return this; - }; - return Version; - }()); - - function createVersionConstructor(db) { - return makeClassConstructor(Version.prototype, function Version(versionNumber) { - this.db = db; - this._cfg = { - version: versionNumber, - storesSource: null, - dbschema: {}, - tables: {}, - contentUpgrade: null - }; - }); - } - - function getDbNamesTable(indexedDB, IDBKeyRange) { - var dbNamesDB = indexedDB["_dbNamesDB"]; - if (!dbNamesDB) { - dbNamesDB = indexedDB["_dbNamesDB"] = new Dexie$1(DBNAMES_DB, { - addons: [], - indexedDB: indexedDB, - IDBKeyRange: IDBKeyRange, - }); - dbNamesDB.version(1).stores({ dbnames: "name" }); - } - return dbNamesDB.table("dbnames"); - } - function hasDatabasesNative(indexedDB) { - return indexedDB && typeof indexedDB.databases === "function"; - } - function getDatabaseNames(_a) { - var indexedDB = _a.indexedDB, IDBKeyRange = _a.IDBKeyRange; - return hasDatabasesNative(indexedDB) - ? Promise.resolve(indexedDB.databases()).then(function (infos) { - return infos - .map(function (info) { return info.name; }) - .filter(function (name) { return name !== DBNAMES_DB; }); - }) - : getDbNamesTable(indexedDB, IDBKeyRange).toCollection().primaryKeys(); - } - function _onDatabaseCreated(_a, name) { - var indexedDB = _a.indexedDB, IDBKeyRange = _a.IDBKeyRange; - !hasDatabasesNative(indexedDB) && - name !== DBNAMES_DB && - getDbNamesTable(indexedDB, IDBKeyRange).put({ name: name }).catch(nop); - } - function _onDatabaseDeleted(_a, name) { - var indexedDB = _a.indexedDB, IDBKeyRange = _a.IDBKeyRange; - !hasDatabasesNative(indexedDB) && - name !== DBNAMES_DB && - getDbNamesTable(indexedDB, IDBKeyRange).delete(name).catch(nop); - } - - function vip(fn) { - return newScope(function () { - PSD.letThrough = true; - return fn(); - }); - } - - function idbReady() { - var isSafari = !navigator.userAgentData && - /Safari\//.test(navigator.userAgent) && - !/Chrom(e|ium)\//.test(navigator.userAgent); - if (!isSafari || !indexedDB.databases) - return Promise.resolve(); - var intervalId; - return new Promise(function (resolve) { - var tryIdb = function () { return indexedDB.databases().finally(resolve); }; - intervalId = setInterval(tryIdb, 100); - tryIdb(); - }).finally(function () { return clearInterval(intervalId); }); - } - - function dexieOpen(db) { - var state = db._state; - var indexedDB = db._deps.indexedDB; - if (state.isBeingOpened || db.idbdb) - return state.dbReadyPromise.then(function () { return state.dbOpenError ? - rejection(state.dbOpenError) : - db; }); - debug && (state.openCanceller._stackHolder = getErrorWithStack()); - state.isBeingOpened = true; - state.dbOpenError = null; - state.openComplete = false; - var openCanceller = state.openCanceller; - function throwIfCancelled() { - if (state.openCanceller !== openCanceller) - throw new exceptions.DatabaseClosed('db.open() was cancelled'); - } - var resolveDbReady = state.dbReadyResolve, - upgradeTransaction = null, wasCreated = false; - return DexiePromise.race([openCanceller, (typeof navigator === 'undefined' ? DexiePromise.resolve() : idbReady()).then(function () { return new DexiePromise(function (resolve, reject) { - throwIfCancelled(); - if (!indexedDB) - throw new exceptions.MissingAPI(); - var dbName = db.name; - var req = state.autoSchema ? - indexedDB.open(dbName) : - indexedDB.open(dbName, Math.round(db.verno * 10)); - if (!req) - throw new exceptions.MissingAPI(); - req.onerror = eventRejectHandler(reject); - req.onblocked = wrap(db._fireOnBlocked); - req.onupgradeneeded = wrap(function (e) { - upgradeTransaction = req.transaction; - if (state.autoSchema && !db._options.allowEmptyDB) { - req.onerror = preventDefault; - upgradeTransaction.abort(); - req.result.close(); - var delreq = indexedDB.deleteDatabase(dbName); - delreq.onsuccess = delreq.onerror = wrap(function () { - reject(new exceptions.NoSuchDatabase("Database " + dbName + " doesnt exist")); - }); - } - else { - upgradeTransaction.onerror = eventRejectHandler(reject); - var oldVer = e.oldVersion > Math.pow(2, 62) ? 0 : e.oldVersion; - wasCreated = oldVer < 1; - db._novip.idbdb = req.result; - runUpgraders(db, oldVer / 10, upgradeTransaction, reject); - } - }, reject); - req.onsuccess = wrap(function () { - upgradeTransaction = null; - var idbdb = db._novip.idbdb = req.result; - var objectStoreNames = slice(idbdb.objectStoreNames); - if (objectStoreNames.length > 0) - try { - var tmpTrans = idbdb.transaction(safariMultiStoreFix(objectStoreNames), 'readonly'); - if (state.autoSchema) - readGlobalSchema(db, idbdb, tmpTrans); - else { - adjustToExistingIndexNames(db, db._dbSchema, tmpTrans); - if (!verifyInstalledSchema(db, tmpTrans)) { - console.warn("Dexie SchemaDiff: Schema was extended without increasing the number passed to db.version(). Some queries may fail."); - } - } - generateMiddlewareStacks(db, tmpTrans); - } - catch (e) { - } - connections.push(db); - idbdb.onversionchange = wrap(function (ev) { - state.vcFired = true; - db.on("versionchange").fire(ev); - }); - idbdb.onclose = wrap(function (ev) { - db.on("close").fire(ev); - }); - if (wasCreated) - _onDatabaseCreated(db._deps, dbName); - resolve(); - }, reject); - }); })]).then(function () { - throwIfCancelled(); - state.onReadyBeingFired = []; - return DexiePromise.resolve(vip(function () { return db.on.ready.fire(db.vip); })).then(function fireRemainders() { - if (state.onReadyBeingFired.length > 0) { - var remainders_1 = state.onReadyBeingFired.reduce(promisableChain, nop); - state.onReadyBeingFired = []; - return DexiePromise.resolve(vip(function () { return remainders_1(db.vip); })).then(fireRemainders); - } - }); - }).finally(function () { - state.onReadyBeingFired = null; - state.isBeingOpened = false; - }).then(function () { - return db; - }).catch(function (err) { - state.dbOpenError = err; - try { - upgradeTransaction && upgradeTransaction.abort(); - } - catch (_a) { } - if (openCanceller === state.openCanceller) { - db._close(); - } - return rejection(err); - }).finally(function () { - state.openComplete = true; - resolveDbReady(); - }); - } - - function awaitIterator(iterator) { - var callNext = function (result) { return iterator.next(result); }, doThrow = function (error) { return iterator.throw(error); }, onSuccess = step(callNext), onError = step(doThrow); - function step(getNext) { - return function (val) { - var next = getNext(val), value = next.value; - return next.done ? value : - (!value || typeof value.then !== 'function' ? - isArray(value) ? Promise.all(value).then(onSuccess, onError) : onSuccess(value) : - value.then(onSuccess, onError)); - }; - } - return step(callNext)(); - } - - function extractTransactionArgs(mode, _tableArgs_, scopeFunc) { - var i = arguments.length; - if (i < 2) - throw new exceptions.InvalidArgument("Too few arguments"); - var args = new Array(i - 1); - while (--i) - args[i - 1] = arguments[i]; - scopeFunc = args.pop(); - var tables = flatten(args); - return [mode, tables, scopeFunc]; - } - function enterTransactionScope(db, mode, storeNames, parentTransaction, scopeFunc) { - return DexiePromise.resolve().then(function () { - var transless = PSD.transless || PSD; - var trans = db._createTransaction(mode, storeNames, db._dbSchema, parentTransaction); - var zoneProps = { - trans: trans, - transless: transless - }; - if (parentTransaction) { - trans.idbtrans = parentTransaction.idbtrans; - } - else { - try { - trans.create(); - db._state.PR1398_maxLoop = 3; - } - catch (ex) { - if (ex.name === errnames.InvalidState && db.isOpen() && --db._state.PR1398_maxLoop > 0) { - console.warn('Dexie: Need to reopen db'); - db._close(); - return db.open().then(function () { return enterTransactionScope(db, mode, storeNames, null, scopeFunc); }); - } - return rejection(ex); - } - } - var scopeFuncIsAsync = isAsyncFunction(scopeFunc); - if (scopeFuncIsAsync) { - incrementExpectedAwaits(); - } - var returnValue; - var promiseFollowed = DexiePromise.follow(function () { - returnValue = scopeFunc.call(trans, trans); - if (returnValue) { - if (scopeFuncIsAsync) { - var decrementor = decrementExpectedAwaits.bind(null, null); - returnValue.then(decrementor, decrementor); - } - else if (typeof returnValue.next === 'function' && typeof returnValue.throw === 'function') { - returnValue = awaitIterator(returnValue); - } - } - }, zoneProps); - return (returnValue && typeof returnValue.then === 'function' ? - DexiePromise.resolve(returnValue).then(function (x) { return trans.active ? - x - : rejection(new exceptions.PrematureCommit("Transaction committed too early. See http://bit.ly/2kdckMn")); }) - : promiseFollowed.then(function () { return returnValue; })).then(function (x) { - if (parentTransaction) - trans._resolve(); - return trans._completion.then(function () { return x; }); - }).catch(function (e) { - trans._reject(e); - return rejection(e); - }); - }); - } - - function pad(a, value, count) { - var result = isArray(a) ? a.slice() : [a]; - for (var i = 0; i < count; ++i) - result.push(value); - return result; - } - function createVirtualIndexMiddleware(down) { - return __assign(__assign({}, down), { table: function (tableName) { - var table = down.table(tableName); - var schema = table.schema; - var indexLookup = {}; - var allVirtualIndexes = []; - function addVirtualIndexes(keyPath, keyTail, lowLevelIndex) { - var keyPathAlias = getKeyPathAlias(keyPath); - var indexList = (indexLookup[keyPathAlias] = indexLookup[keyPathAlias] || []); - var keyLength = keyPath == null ? 0 : typeof keyPath === 'string' ? 1 : keyPath.length; - var isVirtual = keyTail > 0; - var virtualIndex = __assign(__assign({}, lowLevelIndex), { isVirtual: isVirtual, keyTail: keyTail, keyLength: keyLength, extractKey: getKeyExtractor(keyPath), unique: !isVirtual && lowLevelIndex.unique }); - indexList.push(virtualIndex); - if (!virtualIndex.isPrimaryKey) { - allVirtualIndexes.push(virtualIndex); - } - if (keyLength > 1) { - var virtualKeyPath = keyLength === 2 ? - keyPath[0] : - keyPath.slice(0, keyLength - 1); - addVirtualIndexes(virtualKeyPath, keyTail + 1, lowLevelIndex); - } - indexList.sort(function (a, b) { return a.keyTail - b.keyTail; }); - return virtualIndex; - } - var primaryKey = addVirtualIndexes(schema.primaryKey.keyPath, 0, schema.primaryKey); - indexLookup[":id"] = [primaryKey]; - for (var _i = 0, _a = schema.indexes; _i < _a.length; _i++) { - var index = _a[_i]; - addVirtualIndexes(index.keyPath, 0, index); - } - function findBestIndex(keyPath) { - var result = indexLookup[getKeyPathAlias(keyPath)]; - return result && result[0]; - } - function translateRange(range, keyTail) { - return { - type: range.type === 1 ? - 2 : - range.type, - lower: pad(range.lower, range.lowerOpen ? down.MAX_KEY : down.MIN_KEY, keyTail), - lowerOpen: true, - upper: pad(range.upper, range.upperOpen ? down.MIN_KEY : down.MAX_KEY, keyTail), - upperOpen: true - }; - } - function translateRequest(req) { - var index = req.query.index; - return index.isVirtual ? __assign(__assign({}, req), { query: { - index: index, - range: translateRange(req.query.range, index.keyTail) - } }) : req; - } - var result = __assign(__assign({}, table), { schema: __assign(__assign({}, schema), { primaryKey: primaryKey, indexes: allVirtualIndexes, getIndexByKeyPath: findBestIndex }), count: function (req) { - return table.count(translateRequest(req)); - }, query: function (req) { - return table.query(translateRequest(req)); - }, openCursor: function (req) { - var _a = req.query.index, keyTail = _a.keyTail, isVirtual = _a.isVirtual, keyLength = _a.keyLength; - if (!isVirtual) - return table.openCursor(req); - function createVirtualCursor(cursor) { - function _continue(key) { - key != null ? - cursor.continue(pad(key, req.reverse ? down.MAX_KEY : down.MIN_KEY, keyTail)) : - req.unique ? - cursor.continue(cursor.key.slice(0, keyLength) - .concat(req.reverse - ? down.MIN_KEY - : down.MAX_KEY, keyTail)) : - cursor.continue(); - } - var virtualCursor = Object.create(cursor, { - continue: { value: _continue }, - continuePrimaryKey: { - value: function (key, primaryKey) { - cursor.continuePrimaryKey(pad(key, down.MAX_KEY, keyTail), primaryKey); - } - }, - primaryKey: { - get: function () { - return cursor.primaryKey; - } - }, - key: { - get: function () { - var key = cursor.key; - return keyLength === 1 ? - key[0] : - key.slice(0, keyLength); - } - }, - value: { - get: function () { - return cursor.value; - } - } - }); - return virtualCursor; - } - return table.openCursor(translateRequest(req)) - .then(function (cursor) { return cursor && createVirtualCursor(cursor); }); - } }); - return result; - } }); - } - var virtualIndexMiddleware = { - stack: "dbcore", - name: "VirtualIndexMiddleware", - level: 1, - create: createVirtualIndexMiddleware - }; - - function getObjectDiff(a, b, rv, prfx) { - rv = rv || {}; - prfx = prfx || ''; - keys(a).forEach(function (prop) { - if (!hasOwn(b, prop)) { - rv[prfx + prop] = undefined; - } - else { - var ap = a[prop], bp = b[prop]; - if (typeof ap === 'object' && typeof bp === 'object' && ap && bp) { - var apTypeName = toStringTag(ap); - var bpTypeName = toStringTag(bp); - if (apTypeName !== bpTypeName) { - rv[prfx + prop] = b[prop]; - } - else if (apTypeName === 'Object') { - getObjectDiff(ap, bp, rv, prfx + prop + '.'); - } - else if (ap !== bp) { - rv[prfx + prop] = b[prop]; - } - } - else if (ap !== bp) - rv[prfx + prop] = b[prop]; - } - }); - keys(b).forEach(function (prop) { - if (!hasOwn(a, prop)) { - rv[prfx + prop] = b[prop]; - } - }); - return rv; - } - - function getEffectiveKeys(primaryKey, req) { - if (req.type === 'delete') - return req.keys; - return req.keys || req.values.map(primaryKey.extractKey); - } - - var hooksMiddleware = { - stack: "dbcore", - name: "HooksMiddleware", - level: 2, - create: function (downCore) { return (__assign(__assign({}, downCore), { table: function (tableName) { - var downTable = downCore.table(tableName); - var primaryKey = downTable.schema.primaryKey; - var tableMiddleware = __assign(__assign({}, downTable), { mutate: function (req) { - var dxTrans = PSD.trans; - var _a = dxTrans.table(tableName).hook, deleting = _a.deleting, creating = _a.creating, updating = _a.updating; - switch (req.type) { - case 'add': - if (creating.fire === nop) - break; - return dxTrans._promise('readwrite', function () { return addPutOrDelete(req); }, true); - case 'put': - if (creating.fire === nop && updating.fire === nop) - break; - return dxTrans._promise('readwrite', function () { return addPutOrDelete(req); }, true); - case 'delete': - if (deleting.fire === nop) - break; - return dxTrans._promise('readwrite', function () { return addPutOrDelete(req); }, true); - case 'deleteRange': - if (deleting.fire === nop) - break; - return dxTrans._promise('readwrite', function () { return deleteRange(req); }, true); - } - return downTable.mutate(req); - function addPutOrDelete(req) { - var dxTrans = PSD.trans; - var keys = req.keys || getEffectiveKeys(primaryKey, req); - if (!keys) - throw new Error("Keys missing"); - req = req.type === 'add' || req.type === 'put' ? __assign(__assign({}, req), { keys: keys }) : __assign({}, req); - if (req.type !== 'delete') - req.values = __spreadArray([], req.values, true); - if (req.keys) - req.keys = __spreadArray([], req.keys, true); - return getExistingValues(downTable, req, keys).then(function (existingValues) { - var contexts = keys.map(function (key, i) { - var existingValue = existingValues[i]; - var ctx = { onerror: null, onsuccess: null }; - if (req.type === 'delete') { - deleting.fire.call(ctx, key, existingValue, dxTrans); - } - else if (req.type === 'add' || existingValue === undefined) { - var generatedPrimaryKey = creating.fire.call(ctx, key, req.values[i], dxTrans); - if (key == null && generatedPrimaryKey != null) { - key = generatedPrimaryKey; - req.keys[i] = key; - if (!primaryKey.outbound) { - setByKeyPath(req.values[i], primaryKey.keyPath, key); - } - } - } - else { - var objectDiff = getObjectDiff(existingValue, req.values[i]); - var additionalChanges_1 = updating.fire.call(ctx, objectDiff, key, existingValue, dxTrans); - if (additionalChanges_1) { - var requestedValue_1 = req.values[i]; - Object.keys(additionalChanges_1).forEach(function (keyPath) { - if (hasOwn(requestedValue_1, keyPath)) { - requestedValue_1[keyPath] = additionalChanges_1[keyPath]; - } - else { - setByKeyPath(requestedValue_1, keyPath, additionalChanges_1[keyPath]); - } - }); - } - } - return ctx; - }); - return downTable.mutate(req).then(function (_a) { - var failures = _a.failures, results = _a.results, numFailures = _a.numFailures, lastResult = _a.lastResult; - for (var i = 0; i < keys.length; ++i) { - var primKey = results ? results[i] : keys[i]; - var ctx = contexts[i]; - if (primKey == null) { - ctx.onerror && ctx.onerror(failures[i]); - } - else { - ctx.onsuccess && ctx.onsuccess(req.type === 'put' && existingValues[i] ? - req.values[i] : - primKey - ); - } - } - return { failures: failures, results: results, numFailures: numFailures, lastResult: lastResult }; - }).catch(function (error) { - contexts.forEach(function (ctx) { return ctx.onerror && ctx.onerror(error); }); - return Promise.reject(error); - }); - }); - } - function deleteRange(req) { - return deleteNextChunk(req.trans, req.range, 10000); - } - function deleteNextChunk(trans, range, limit) { - return downTable.query({ trans: trans, values: false, query: { index: primaryKey, range: range }, limit: limit }) - .then(function (_a) { - var result = _a.result; - return addPutOrDelete({ type: 'delete', keys: result, trans: trans }).then(function (res) { - if (res.numFailures > 0) - return Promise.reject(res.failures[0]); - if (result.length < limit) { - return { failures: [], numFailures: 0, lastResult: undefined }; - } - else { - return deleteNextChunk(trans, __assign(__assign({}, range), { lower: result[result.length - 1], lowerOpen: true }), limit); - } - }); - }); - } - } }); - return tableMiddleware; - } })); } - }; - function getExistingValues(table, req, effectiveKeys) { - return req.type === "add" - ? Promise.resolve([]) - : table.getMany({ trans: req.trans, keys: effectiveKeys, cache: "immutable" }); - } - - function getFromTransactionCache(keys, cache, clone) { - try { - if (!cache) - return null; - if (cache.keys.length < keys.length) - return null; - var result = []; - for (var i = 0, j = 0; i < cache.keys.length && j < keys.length; ++i) { - if (cmp(cache.keys[i], keys[j]) !== 0) - continue; - result.push(clone ? deepClone(cache.values[i]) : cache.values[i]); - ++j; - } - return result.length === keys.length ? result : null; - } - catch (_a) { - return null; - } - } - var cacheExistingValuesMiddleware = { - stack: "dbcore", - level: -1, - create: function (core) { - return { - table: function (tableName) { - var table = core.table(tableName); - return __assign(__assign({}, table), { getMany: function (req) { - if (!req.cache) { - return table.getMany(req); - } - var cachedResult = getFromTransactionCache(req.keys, req.trans["_cache"], req.cache === "clone"); - if (cachedResult) { - return DexiePromise.resolve(cachedResult); - } - return table.getMany(req).then(function (res) { - req.trans["_cache"] = { - keys: req.keys, - values: req.cache === "clone" ? deepClone(res) : res, - }; - return res; - }); - }, mutate: function (req) { - if (req.type !== "add") - req.trans["_cache"] = null; - return table.mutate(req); - } }); - }, - }; - }, - }; - - var _a; - function isEmptyRange(node) { - return !("from" in node); - } - var RangeSet = function (fromOrTree, to) { - if (this) { - extend(this, arguments.length ? { d: 1, from: fromOrTree, to: arguments.length > 1 ? to : fromOrTree } : { d: 0 }); - } - else { - var rv = new RangeSet(); - if (fromOrTree && ("d" in fromOrTree)) { - extend(rv, fromOrTree); - } - return rv; - } - }; - props(RangeSet.prototype, (_a = { - add: function (rangeSet) { - mergeRanges(this, rangeSet); - return this; - }, - addKey: function (key) { - addRange(this, key, key); - return this; - }, - addKeys: function (keys) { - var _this = this; - keys.forEach(function (key) { return addRange(_this, key, key); }); - return this; - } - }, - _a[iteratorSymbol] = function () { - return getRangeSetIterator(this); - }, - _a)); - function addRange(target, from, to) { - var diff = cmp(from, to); - if (isNaN(diff)) - return; - if (diff > 0) - throw RangeError(); - if (isEmptyRange(target)) - return extend(target, { from: from, to: to, d: 1 }); - var left = target.l; - var right = target.r; - if (cmp(to, target.from) < 0) { - left - ? addRange(left, from, to) - : (target.l = { from: from, to: to, d: 1, l: null, r: null }); - return rebalance(target); - } - if (cmp(from, target.to) > 0) { - right - ? addRange(right, from, to) - : (target.r = { from: from, to: to, d: 1, l: null, r: null }); - return rebalance(target); - } - if (cmp(from, target.from) < 0) { - target.from = from; - target.l = null; - target.d = right ? right.d + 1 : 1; - } - if (cmp(to, target.to) > 0) { - target.to = to; - target.r = null; - target.d = target.l ? target.l.d + 1 : 1; - } - var rightWasCutOff = !target.r; - if (left && !target.l) { - mergeRanges(target, left); - } - if (right && rightWasCutOff) { - mergeRanges(target, right); - } - } - function mergeRanges(target, newSet) { - function _addRangeSet(target, _a) { - var from = _a.from, to = _a.to, l = _a.l, r = _a.r; - addRange(target, from, to); - if (l) - _addRangeSet(target, l); - if (r) - _addRangeSet(target, r); - } - if (!isEmptyRange(newSet)) - _addRangeSet(target, newSet); - } - function rangesOverlap(rangeSet1, rangeSet2) { - var i1 = getRangeSetIterator(rangeSet2); - var nextResult1 = i1.next(); - if (nextResult1.done) - return false; - var a = nextResult1.value; - var i2 = getRangeSetIterator(rangeSet1); - var nextResult2 = i2.next(a.from); - var b = nextResult2.value; - while (!nextResult1.done && !nextResult2.done) { - if (cmp(b.from, a.to) <= 0 && cmp(b.to, a.from) >= 0) - return true; - cmp(a.from, b.from) < 0 - ? (a = (nextResult1 = i1.next(b.from)).value) - : (b = (nextResult2 = i2.next(a.from)).value); - } - return false; - } - function getRangeSetIterator(node) { - var state = isEmptyRange(node) ? null : { s: 0, n: node }; - return { - next: function (key) { - var keyProvided = arguments.length > 0; - while (state) { - switch (state.s) { - case 0: - state.s = 1; - if (keyProvided) { - while (state.n.l && cmp(key, state.n.from) < 0) - state = { up: state, n: state.n.l, s: 1 }; - } - else { - while (state.n.l) - state = { up: state, n: state.n.l, s: 1 }; - } - case 1: - state.s = 2; - if (!keyProvided || cmp(key, state.n.to) <= 0) - return { value: state.n, done: false }; - case 2: - if (state.n.r) { - state.s = 3; - state = { up: state, n: state.n.r, s: 0 }; - continue; - } - case 3: - state = state.up; - } - } - return { done: true }; - }, - }; - } - function rebalance(target) { - var _a, _b; - var diff = (((_a = target.r) === null || _a === void 0 ? void 0 : _a.d) || 0) - (((_b = target.l) === null || _b === void 0 ? void 0 : _b.d) || 0); - var r = diff > 1 ? "r" : diff < -1 ? "l" : ""; - if (r) { - var l = r === "r" ? "l" : "r"; - var rootClone = __assign({}, target); - var oldRootRight = target[r]; - target.from = oldRootRight.from; - target.to = oldRootRight.to; - target[r] = oldRootRight[r]; - rootClone[r] = oldRootRight[l]; - target[l] = rootClone; - rootClone.d = computeDepth(rootClone); - } - target.d = computeDepth(target); - } - function computeDepth(_a) { - var r = _a.r, l = _a.l; - return (r ? (l ? Math.max(r.d, l.d) : r.d) : l ? l.d : 0) + 1; - } - - var observabilityMiddleware = { - stack: "dbcore", - level: 0, - create: function (core) { - var dbName = core.schema.name; - var FULL_RANGE = new RangeSet(core.MIN_KEY, core.MAX_KEY); - return __assign(__assign({}, core), { table: function (tableName) { - var table = core.table(tableName); - var schema = table.schema; - var primaryKey = schema.primaryKey; - var extractKey = primaryKey.extractKey, outbound = primaryKey.outbound; - var tableClone = __assign(__assign({}, table), { mutate: function (req) { - var trans = req.trans; - var mutatedParts = trans.mutatedParts || (trans.mutatedParts = {}); - var getRangeSet = function (indexName) { - var part = "idb://" + dbName + "/" + tableName + "/" + indexName; - return (mutatedParts[part] || - (mutatedParts[part] = new RangeSet())); - }; - var pkRangeSet = getRangeSet(""); - var delsRangeSet = getRangeSet(":dels"); - var type = req.type; - var _a = req.type === "deleteRange" - ? [req.range] - : req.type === "delete" - ? [req.keys] - : req.values.length < 50 - ? [[], req.values] - : [], keys = _a[0], newObjs = _a[1]; - var oldCache = req.trans["_cache"]; - return table.mutate(req).then(function (res) { - if (isArray(keys)) { - if (type !== "delete") - keys = res.results; - pkRangeSet.addKeys(keys); - var oldObjs = getFromTransactionCache(keys, oldCache); - if (!oldObjs && type !== "add") { - delsRangeSet.addKeys(keys); - } - if (oldObjs || newObjs) { - trackAffectedIndexes(getRangeSet, schema, oldObjs, newObjs); - } - } - else if (keys) { - var range = { from: keys.lower, to: keys.upper }; - delsRangeSet.add(range); - pkRangeSet.add(range); - } - else { - pkRangeSet.add(FULL_RANGE); - delsRangeSet.add(FULL_RANGE); - schema.indexes.forEach(function (idx) { return getRangeSet(idx.name).add(FULL_RANGE); }); - } - return res; - }); - } }); - var getRange = function (_a) { - var _b, _c; - var _d = _a.query, index = _d.index, range = _d.range; - return [ - index, - new RangeSet((_b = range.lower) !== null && _b !== void 0 ? _b : core.MIN_KEY, (_c = range.upper) !== null && _c !== void 0 ? _c : core.MAX_KEY), - ]; - }; - var readSubscribers = { - get: function (req) { return [primaryKey, new RangeSet(req.key)]; }, - getMany: function (req) { return [primaryKey, new RangeSet().addKeys(req.keys)]; }, - count: getRange, - query: getRange, - openCursor: getRange, - }; - keys(readSubscribers).forEach(function (method) { - tableClone[method] = function (req) { - var subscr = PSD.subscr; - if (subscr) { - var getRangeSet = function (indexName) { - var part = "idb://" + dbName + "/" + tableName + "/" + indexName; - return (subscr[part] || - (subscr[part] = new RangeSet())); - }; - var pkRangeSet_1 = getRangeSet(""); - var delsRangeSet_1 = getRangeSet(":dels"); - var _a = readSubscribers[method](req), queriedIndex = _a[0], queriedRanges = _a[1]; - getRangeSet(queriedIndex.name || "").add(queriedRanges); - if (!queriedIndex.isPrimaryKey) { - if (method === "count") { - delsRangeSet_1.add(FULL_RANGE); - } - else { - var keysPromise_1 = method === "query" && - outbound && - req.values && - table.query(__assign(__assign({}, req), { values: false })); - return table[method].apply(this, arguments).then(function (res) { - if (method === "query") { - if (outbound && req.values) { - return keysPromise_1.then(function (_a) { - var resultingKeys = _a.result; - pkRangeSet_1.addKeys(resultingKeys); - return res; - }); - } - var pKeys = req.values - ? res.result.map(extractKey) - : res.result; - if (req.values) { - pkRangeSet_1.addKeys(pKeys); - } - else { - delsRangeSet_1.addKeys(pKeys); - } - } - else if (method === "openCursor") { - var cursor_1 = res; - var wantValues_1 = req.values; - return (cursor_1 && - Object.create(cursor_1, { - key: { - get: function () { - delsRangeSet_1.addKey(cursor_1.primaryKey); - return cursor_1.key; - }, - }, - primaryKey: { - get: function () { - var pkey = cursor_1.primaryKey; - delsRangeSet_1.addKey(pkey); - return pkey; - }, - }, - value: { - get: function () { - wantValues_1 && pkRangeSet_1.addKey(cursor_1.primaryKey); - return cursor_1.value; - }, - }, - })); - } - return res; - }); - } - } - } - return table[method].apply(this, arguments); - }; - }); - return tableClone; - } }); - }, - }; - function trackAffectedIndexes(getRangeSet, schema, oldObjs, newObjs) { - function addAffectedIndex(ix) { - var rangeSet = getRangeSet(ix.name || ""); - function extractKey(obj) { - return obj != null ? ix.extractKey(obj) : null; - } - var addKeyOrKeys = function (key) { return ix.multiEntry && isArray(key) - ? key.forEach(function (key) { return rangeSet.addKey(key); }) - : rangeSet.addKey(key); }; - (oldObjs || newObjs).forEach(function (_, i) { - var oldKey = oldObjs && extractKey(oldObjs[i]); - var newKey = newObjs && extractKey(newObjs[i]); - if (cmp(oldKey, newKey) !== 0) { - if (oldKey != null) - addKeyOrKeys(oldKey); - if (newKey != null) - addKeyOrKeys(newKey); - } - }); - } - schema.indexes.forEach(addAffectedIndex); - } - - var Dexie$1 = (function () { - function Dexie(name, options) { - var _this = this; - this._middlewares = {}; - this.verno = 0; - var deps = Dexie.dependencies; - this._options = options = __assign({ - addons: Dexie.addons, autoOpen: true, - indexedDB: deps.indexedDB, IDBKeyRange: deps.IDBKeyRange }, options); - this._deps = { - indexedDB: options.indexedDB, - IDBKeyRange: options.IDBKeyRange - }; - var addons = options.addons; - this._dbSchema = {}; - this._versions = []; - this._storeNames = []; - this._allTables = {}; - this.idbdb = null; - this._novip = this; - var state = { - dbOpenError: null, - isBeingOpened: false, - onReadyBeingFired: null, - openComplete: false, - dbReadyResolve: nop, - dbReadyPromise: null, - cancelOpen: nop, - openCanceller: null, - autoSchema: true, - PR1398_maxLoop: 3 - }; - state.dbReadyPromise = new DexiePromise(function (resolve) { - state.dbReadyResolve = resolve; - }); - state.openCanceller = new DexiePromise(function (_, reject) { - state.cancelOpen = reject; - }); - this._state = state; - this.name = name; - this.on = Events(this, "populate", "blocked", "versionchange", "close", { ready: [promisableChain, nop] }); - this.on.ready.subscribe = override(this.on.ready.subscribe, function (subscribe) { - return function (subscriber, bSticky) { - Dexie.vip(function () { - var state = _this._state; - if (state.openComplete) { - if (!state.dbOpenError) - DexiePromise.resolve().then(subscriber); - if (bSticky) - subscribe(subscriber); - } - else if (state.onReadyBeingFired) { - state.onReadyBeingFired.push(subscriber); - if (bSticky) - subscribe(subscriber); - } - else { - subscribe(subscriber); - var db_1 = _this; - if (!bSticky) - subscribe(function unsubscribe() { - db_1.on.ready.unsubscribe(subscriber); - db_1.on.ready.unsubscribe(unsubscribe); - }); - } - }); - }; - }); - this.Collection = createCollectionConstructor(this); - this.Table = createTableConstructor(this); - this.Transaction = createTransactionConstructor(this); - this.Version = createVersionConstructor(this); - this.WhereClause = createWhereClauseConstructor(this); - this.on("versionchange", function (ev) { - if (ev.newVersion > 0) - console.warn("Another connection wants to upgrade database '" + _this.name + "'. Closing db now to resume the upgrade."); - else - console.warn("Another connection wants to delete database '" + _this.name + "'. Closing db now to resume the delete request."); - _this.close(); - }); - this.on("blocked", function (ev) { - if (!ev.newVersion || ev.newVersion < ev.oldVersion) - console.warn("Dexie.delete('" + _this.name + "') was blocked"); - else - console.warn("Upgrade '" + _this.name + "' blocked by other connection holding version " + ev.oldVersion / 10); - }); - this._maxKey = getMaxKey(options.IDBKeyRange); - this._createTransaction = function (mode, storeNames, dbschema, parentTransaction) { return new _this.Transaction(mode, storeNames, dbschema, _this._options.chromeTransactionDurability, parentTransaction); }; - this._fireOnBlocked = function (ev) { - _this.on("blocked").fire(ev); - connections - .filter(function (c) { return c.name === _this.name && c !== _this && !c._state.vcFired; }) - .map(function (c) { return c.on("versionchange").fire(ev); }); - }; - this.use(virtualIndexMiddleware); - this.use(hooksMiddleware); - this.use(observabilityMiddleware); - this.use(cacheExistingValuesMiddleware); - this.vip = Object.create(this, { _vip: { value: true } }); - addons.forEach(function (addon) { return addon(_this); }); - } - Dexie.prototype.version = function (versionNumber) { - if (isNaN(versionNumber) || versionNumber < 0.1) - throw new exceptions.Type("Given version is not a positive number"); - versionNumber = Math.round(versionNumber * 10) / 10; - if (this.idbdb || this._state.isBeingOpened) - throw new exceptions.Schema("Cannot add version when database is open"); - this.verno = Math.max(this.verno, versionNumber); - var versions = this._versions; - var versionInstance = versions.filter(function (v) { return v._cfg.version === versionNumber; })[0]; - if (versionInstance) - return versionInstance; - versionInstance = new this.Version(versionNumber); - versions.push(versionInstance); - versions.sort(lowerVersionFirst); - versionInstance.stores({}); - this._state.autoSchema = false; - return versionInstance; - }; - Dexie.prototype._whenReady = function (fn) { - var _this = this; - return (this.idbdb && (this._state.openComplete || PSD.letThrough || this._vip)) ? fn() : new DexiePromise(function (resolve, reject) { - if (_this._state.openComplete) { - return reject(new exceptions.DatabaseClosed(_this._state.dbOpenError)); - } - if (!_this._state.isBeingOpened) { - if (!_this._options.autoOpen) { - reject(new exceptions.DatabaseClosed()); - return; - } - _this.open().catch(nop); - } - _this._state.dbReadyPromise.then(resolve, reject); - }).then(fn); - }; - Dexie.prototype.use = function (_a) { - var stack = _a.stack, create = _a.create, level = _a.level, name = _a.name; - if (name) - this.unuse({ stack: stack, name: name }); - var middlewares = this._middlewares[stack] || (this._middlewares[stack] = []); - middlewares.push({ stack: stack, create: create, level: level == null ? 10 : level, name: name }); - middlewares.sort(function (a, b) { return a.level - b.level; }); - return this; - }; - Dexie.prototype.unuse = function (_a) { - var stack = _a.stack, name = _a.name, create = _a.create; - if (stack && this._middlewares[stack]) { - this._middlewares[stack] = this._middlewares[stack].filter(function (mw) { - return create ? mw.create !== create : - name ? mw.name !== name : - false; - }); - } - return this; - }; - Dexie.prototype.open = function () { - return dexieOpen(this); - }; - Dexie.prototype._close = function () { - var state = this._state; - var idx = connections.indexOf(this); - if (idx >= 0) - connections.splice(idx, 1); - if (this.idbdb) { - try { - this.idbdb.close(); - } - catch (e) { } - this._novip.idbdb = null; - } - state.dbReadyPromise = new DexiePromise(function (resolve) { - state.dbReadyResolve = resolve; - }); - state.openCanceller = new DexiePromise(function (_, reject) { - state.cancelOpen = reject; - }); - }; - Dexie.prototype.close = function () { - this._close(); - var state = this._state; - this._options.autoOpen = false; - state.dbOpenError = new exceptions.DatabaseClosed(); - if (state.isBeingOpened) - state.cancelOpen(state.dbOpenError); - }; - Dexie.prototype.delete = function () { - var _this = this; - var hasArguments = arguments.length > 0; - var state = this._state; - return new DexiePromise(function (resolve, reject) { - var doDelete = function () { - _this.close(); - var req = _this._deps.indexedDB.deleteDatabase(_this.name); - req.onsuccess = wrap(function () { - _onDatabaseDeleted(_this._deps, _this.name); - resolve(); - }); - req.onerror = eventRejectHandler(reject); - req.onblocked = _this._fireOnBlocked; - }; - if (hasArguments) - throw new exceptions.InvalidArgument("Arguments not allowed in db.delete()"); - if (state.isBeingOpened) { - state.dbReadyPromise.then(doDelete); - } - else { - doDelete(); - } - }); - }; - Dexie.prototype.backendDB = function () { - return this.idbdb; - }; - Dexie.prototype.isOpen = function () { - return this.idbdb !== null; - }; - Dexie.prototype.hasBeenClosed = function () { - var dbOpenError = this._state.dbOpenError; - return dbOpenError && (dbOpenError.name === 'DatabaseClosed'); - }; - Dexie.prototype.hasFailed = function () { - return this._state.dbOpenError !== null; - }; - Dexie.prototype.dynamicallyOpened = function () { - return this._state.autoSchema; - }; - Object.defineProperty(Dexie.prototype, "tables", { - get: function () { - var _this = this; - return keys(this._allTables).map(function (name) { return _this._allTables[name]; }); - }, - enumerable: false, - configurable: true - }); - Dexie.prototype.transaction = function () { - var args = extractTransactionArgs.apply(this, arguments); - return this._transaction.apply(this, args); - }; - Dexie.prototype._transaction = function (mode, tables, scopeFunc) { - var _this = this; - var parentTransaction = PSD.trans; - if (!parentTransaction || parentTransaction.db !== this || mode.indexOf('!') !== -1) - parentTransaction = null; - var onlyIfCompatible = mode.indexOf('?') !== -1; - mode = mode.replace('!', '').replace('?', ''); - var idbMode, storeNames; - try { - storeNames = tables.map(function (table) { - var storeName = table instanceof _this.Table ? table.name : table; - if (typeof storeName !== 'string') - throw new TypeError("Invalid table argument to Dexie.transaction(). Only Table or String are allowed"); - return storeName; - }); - if (mode == "r" || mode === READONLY) - idbMode = READONLY; - else if (mode == "rw" || mode == READWRITE) - idbMode = READWRITE; - else - throw new exceptions.InvalidArgument("Invalid transaction mode: " + mode); - if (parentTransaction) { - if (parentTransaction.mode === READONLY && idbMode === READWRITE) { - if (onlyIfCompatible) { - parentTransaction = null; - } - else - throw new exceptions.SubTransaction("Cannot enter a sub-transaction with READWRITE mode when parent transaction is READONLY"); - } - if (parentTransaction) { - storeNames.forEach(function (storeName) { - if (parentTransaction && parentTransaction.storeNames.indexOf(storeName) === -1) { - if (onlyIfCompatible) { - parentTransaction = null; - } - else - throw new exceptions.SubTransaction("Table " + storeName + - " not included in parent transaction."); - } - }); - } - if (onlyIfCompatible && parentTransaction && !parentTransaction.active) { - parentTransaction = null; - } - } - } - catch (e) { - return parentTransaction ? - parentTransaction._promise(null, function (_, reject) { reject(e); }) : - rejection(e); - } - var enterTransaction = enterTransactionScope.bind(null, this, idbMode, storeNames, parentTransaction, scopeFunc); - return (parentTransaction ? - parentTransaction._promise(idbMode, enterTransaction, "lock") : - PSD.trans ? - usePSD(PSD.transless, function () { return _this._whenReady(enterTransaction); }) : - this._whenReady(enterTransaction)); - }; - Dexie.prototype.table = function (tableName) { - if (!hasOwn(this._allTables, tableName)) { - throw new exceptions.InvalidTable("Table " + tableName + " does not exist"); - } - return this._allTables[tableName]; - }; - return Dexie; - }()); - - var symbolObservable = typeof Symbol !== "undefined" && "observable" in Symbol - ? Symbol.observable - : "@@observable"; - var Observable = (function () { - function Observable(subscribe) { - this._subscribe = subscribe; - } - Observable.prototype.subscribe = function (x, error, complete) { - return this._subscribe(!x || typeof x === "function" ? { next: x, error: error, complete: complete } : x); - }; - Observable.prototype[symbolObservable] = function () { - return this; - }; - return Observable; - }()); - - function extendObservabilitySet(target, newSet) { - keys(newSet).forEach(function (part) { - var rangeSet = target[part] || (target[part] = new RangeSet()); - mergeRanges(rangeSet, newSet[part]); - }); - return target; - } - - function liveQuery(querier) { - var hasValue = false; - var currentValue = undefined; - var observable = new Observable(function (observer) { - var scopeFuncIsAsync = isAsyncFunction(querier); - function execute(subscr) { - if (scopeFuncIsAsync) { - incrementExpectedAwaits(); - } - var exec = function () { return newScope(querier, { subscr: subscr, trans: null }); }; - var rv = PSD.trans - ? - usePSD(PSD.transless, exec) - : exec(); - if (scopeFuncIsAsync) { - rv.then(decrementExpectedAwaits, decrementExpectedAwaits); - } - return rv; - } - var closed = false; - var accumMuts = {}; - var currentObs = {}; - var subscription = { - get closed() { - return closed; - }, - unsubscribe: function () { - closed = true; - globalEvents.storagemutated.unsubscribe(mutationListener); - }, - }; - observer.start && observer.start(subscription); - var querying = false, startedListening = false; - function shouldNotify() { - return keys(currentObs).some(function (key) { - return accumMuts[key] && rangesOverlap(accumMuts[key], currentObs[key]); - }); - } - var mutationListener = function (parts) { - extendObservabilitySet(accumMuts, parts); - if (shouldNotify()) { - doQuery(); - } - }; - var doQuery = function () { - if (querying || closed) - return; - accumMuts = {}; - var subscr = {}; - var ret = execute(subscr); - if (!startedListening) { - globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, mutationListener); - startedListening = true; - } - querying = true; - Promise.resolve(ret).then(function (result) { - hasValue = true; - currentValue = result; - querying = false; - if (closed) - return; - if (shouldNotify()) { - doQuery(); - } - else { - accumMuts = {}; - currentObs = subscr; - observer.next && observer.next(result); - } - }, function (err) { - querying = false; - hasValue = false; - observer.error && observer.error(err); - subscription.unsubscribe(); - }); - }; - doQuery(); - return subscription; - }); - observable.hasValue = function () { return hasValue; }; - observable.getValue = function () { return currentValue; }; - return observable; - } - - var domDeps; - try { - domDeps = { - indexedDB: _global.indexedDB || _global.mozIndexedDB || _global.webkitIndexedDB || _global.msIndexedDB, - IDBKeyRange: _global.IDBKeyRange || _global.webkitIDBKeyRange - }; - } - catch (e) { - domDeps = { indexedDB: null, IDBKeyRange: null }; - } - - var Dexie = Dexie$1; - props(Dexie, __assign(__assign({}, fullNameExceptions), { - delete: function (databaseName) { - var db = new Dexie(databaseName, { addons: [] }); - return db.delete(); - }, - exists: function (name) { - return new Dexie(name, { addons: [] }).open().then(function (db) { - db.close(); - return true; - }).catch('NoSuchDatabaseError', function () { return false; }); - }, - getDatabaseNames: function (cb) { - try { - return getDatabaseNames(Dexie.dependencies).then(cb); - } - catch (_a) { - return rejection(new exceptions.MissingAPI()); - } - }, - defineClass: function () { - function Class(content) { - extend(this, content); - } - return Class; - }, ignoreTransaction: function (scopeFunc) { - return PSD.trans ? - usePSD(PSD.transless, scopeFunc) : - scopeFunc(); - }, vip: vip, async: function (generatorFn) { - return function () { - try { - var rv = awaitIterator(generatorFn.apply(this, arguments)); - if (!rv || typeof rv.then !== 'function') - return DexiePromise.resolve(rv); - return rv; - } - catch (e) { - return rejection(e); - } - }; - }, spawn: function (generatorFn, args, thiz) { - try { - var rv = awaitIterator(generatorFn.apply(thiz, args || [])); - if (!rv || typeof rv.then !== 'function') - return DexiePromise.resolve(rv); - return rv; - } - catch (e) { - return rejection(e); - } - }, - currentTransaction: { - get: function () { return PSD.trans || null; } - }, waitFor: function (promiseOrFunction, optionalTimeout) { - var promise = DexiePromise.resolve(typeof promiseOrFunction === 'function' ? - Dexie.ignoreTransaction(promiseOrFunction) : - promiseOrFunction) - .timeout(optionalTimeout || 60000); - return PSD.trans ? - PSD.trans.waitFor(promise) : - promise; - }, - Promise: DexiePromise, - debug: { - get: function () { return debug; }, - set: function (value) { - setDebug(value, value === 'dexie' ? function () { return true; } : dexieStackFrameFilter); - } - }, - derive: derive, extend: extend, props: props, override: override, - Events: Events, on: globalEvents, liveQuery: liveQuery, extendObservabilitySet: extendObservabilitySet, - getByKeyPath: getByKeyPath, setByKeyPath: setByKeyPath, delByKeyPath: delByKeyPath, shallowClone: shallowClone, deepClone: deepClone, getObjectDiff: getObjectDiff, cmp: cmp, asap: asap$1, - minKey: minKey, - addons: [], - connections: connections, - errnames: errnames, - dependencies: domDeps, - semVer: DEXIE_VERSION, version: DEXIE_VERSION.split('.') - .map(function (n) { return parseInt(n); }) - .reduce(function (p, c, i) { return p + (c / Math.pow(10, i * 2)); }) })); - Dexie.maxKey = getMaxKey(Dexie.dependencies.IDBKeyRange); - - if (typeof dispatchEvent !== 'undefined' && typeof addEventListener !== 'undefined') { - globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, function (updatedParts) { - if (!propagatingLocally) { - var event_1; - if (isIEOrEdge) { - event_1 = document.createEvent('CustomEvent'); - event_1.initCustomEvent(STORAGE_MUTATED_DOM_EVENT_NAME, true, true, updatedParts); - } - else { - event_1 = new CustomEvent(STORAGE_MUTATED_DOM_EVENT_NAME, { - detail: updatedParts - }); - } - propagatingLocally = true; - dispatchEvent(event_1); - propagatingLocally = false; - } - }); - addEventListener(STORAGE_MUTATED_DOM_EVENT_NAME, function (_a) { - var detail = _a.detail; - if (!propagatingLocally) { - propagateLocally(detail); - } - }); - } - function propagateLocally(updateParts) { - var wasMe = propagatingLocally; - try { - propagatingLocally = true; - globalEvents.storagemutated.fire(updateParts); - } - finally { - propagatingLocally = wasMe; - } - } - var propagatingLocally = false; - - if (typeof BroadcastChannel !== 'undefined') { - var bc_1 = new BroadcastChannel(STORAGE_MUTATED_DOM_EVENT_NAME); - if (typeof bc_1.unref === 'function') { - bc_1.unref(); - } - globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, function (changedParts) { - if (!propagatingLocally) { - bc_1.postMessage(changedParts); - } - }); - bc_1.onmessage = function (ev) { - if (ev.data) - propagateLocally(ev.data); - }; - } - else if (typeof self !== 'undefined' && typeof navigator !== 'undefined') { - globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, function (changedParts) { - try { - if (!propagatingLocally) { - if (typeof localStorage !== 'undefined') { - localStorage.setItem(STORAGE_MUTATED_DOM_EVENT_NAME, JSON.stringify({ - trig: Math.random(), - changedParts: changedParts, - })); - } - if (typeof self['clients'] === 'object') { - __spreadArray([], self['clients'].matchAll({ includeUncontrolled: true }), true).forEach(function (client) { - return client.postMessage({ - type: STORAGE_MUTATED_DOM_EVENT_NAME, - changedParts: changedParts, - }); - }); - } - } - } - catch (_a) { } - }); - if (typeof addEventListener !== 'undefined') { - addEventListener('storage', function (ev) { - if (ev.key === STORAGE_MUTATED_DOM_EVENT_NAME) { - var data = JSON.parse(ev.newValue); - if (data) - propagateLocally(data.changedParts); - } - }); - } - var swContainer = self.document && navigator.serviceWorker; - if (swContainer) { - swContainer.addEventListener('message', propagateMessageLocally); - } - } - function propagateMessageLocally(_a) { - var data = _a.data; - if (data && data.type === STORAGE_MUTATED_DOM_EVENT_NAME) { - propagateLocally(data.changedParts); - } - } - - DexiePromise.rejectionMapper = mapError; - setDebug(debug, dexieStackFrameFilter); - - var namedExports = /*#__PURE__*/Object.freeze({ - __proto__: null, - Dexie: Dexie$1, - liveQuery: liveQuery, - 'default': Dexie$1, - RangeSet: RangeSet, - mergeRanges: mergeRanges, - rangesOverlap: rangesOverlap - }); - - __assign(Dexie$1, namedExports, { default: Dexie$1 }); - - return Dexie$1; - -})); -//# sourceMappingURL=dexie.js.map diff --git a/lib/download.js b/lib/download.js deleted file mode 100644 index 230f91b..0000000 --- a/lib/download.js +++ /dev/null @@ -1,167 +0,0 @@ -//download.js v4.2, by dandavis; 2008-2016. [MIT] see http://danml.com/download.html for tests/usage -// v1 landed a FF+Chrome compat way of downloading strings to local un-named files, upgraded to use a hidden frame and optional mime -// v2 added named files via a[download], msSaveBlob, IE (10+) support, and window.URL support for larger+faster saves than dataURLs -// v3 added dataURL and Blob Input, bind-toggle arity, and legacy dataURL fallback was improved with force-download mime and base64 support. 3.1 improved safari handling. -// v4 adds AMD/UMD, commonJS, and plain browser support -// v4.1 adds url download capability via solo URL argument (same domain/CORS only) -// v4.2 adds semantic variable names, long (over 2MB) dataURL support, and hidden by default temp anchors -// https://github.com/rndme/download - -(function (root, factory) { - if (typeof define === 'function' && define.amd) { - // AMD. Register as an anonymous module. - define([], factory); - } else if (typeof exports === 'object') { - // Node. Does not work with strict CommonJS, but - // only CommonJS-like environments that support module.exports, - // like Node. - module.exports = factory(); - } else { - // Browser globals (root is window) - root.download = factory(); - } -}(this, function () { - - return function download(data, strFileName, strMimeType) { - - var self = window, // this script is only for browsers anyway... - defaultMime = "application/octet-stream", // this default mime also triggers iframe downloads - mimeType = strMimeType || defaultMime, - payload = data, - url = !strFileName && !strMimeType && payload, - anchor = document.createElement("a"), - toString = function(a){return String(a);}, - myBlob = (self.Blob || self.MozBlob || self.WebKitBlob || toString), - fileName = strFileName || "download", - blob, - reader; - myBlob= myBlob.call ? myBlob.bind(self) : Blob ; - - if(String(this)==="true"){ //reverse arguments, allowing download.bind(true, "text/xml", "export.xml") to act as a callback - payload=[payload, mimeType]; - mimeType=payload[0]; - payload=payload[1]; - } - - - if(url && url.length< 2048){ // if no filename and no mime, assume a url was passed as the only argument - fileName = url.split("/").pop().split("?")[0]; - anchor.href = url; // assign href prop to temp anchor - if(anchor.href.indexOf(url) !== -1){ // if the browser determines that it's a potentially valid url path: - var ajax=new XMLHttpRequest(); - ajax.open( "GET", url, true); - ajax.responseType = 'blob'; - ajax.onload= function(e){ - download(e.target.response, fileName, defaultMime); - }; - setTimeout(function(){ ajax.send();}, 0); // allows setting custom ajax headers using the return: - return ajax; - } // end if valid url? - } // end if url? - - - //go ahead and download dataURLs right away - if(/^data:([\w+-]+\/[\w+.-]+)?[,;]/.test(payload)){ - - if(payload.length > (1024*1024*1.999) && myBlob !== toString ){ - payload=dataUrlToBlob(payload); - mimeType=payload.type || defaultMime; - }else{ - return navigator.msSaveBlob ? // IE10 can't do a[download], only Blobs: - navigator.msSaveBlob(dataUrlToBlob(payload), fileName) : - saver(payload) ; // everyone else can save dataURLs un-processed - } - - }else{//not data url, is it a string with special needs? - if(/([\x80-\xff])/.test(payload)){ - var i=0, tempUiArr= new Uint8Array(payload.length), mx=tempUiArr.length; - for(i;i