UNPKG

cordova-app-loader

Version:

Cordova App Loader - remote update your cordova app

1 lines 16.8 kB
var CordovaAppLoader=function(e){function t(o){if(n[o])return n[o].exports;var r=n[o]={exports:{},id:o,loaded:!1};return e[o].call(r.exports,r,r.exports,t),r.loaded=!0,r.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){function o(e){var t=Object.keys(e);t.sort();var n="";return t.forEach(function(t){e[t]&&e[t].version,n+="@"+e[t].version}),i.hash(n)+""}function r(e){if(!e)throw new Error("CordovaAppLoader has no options!");if(!e.fs)throw new Error('CordovaAppLoader has no "fs" option (cordova-promise-fs)');if(!e.serverRoot)throw new Error('CordovaAppLoader has no "serverRoot" option.');if(!window.pegasus||!window.Manifest)throw new Error("CordovaAppLoader bootstrap.js is missing.");this.allowServerRootFromManifest=e.allowServerRootFromManifest===!0,a=e.fs.Promise,this.manifest=window.Manifest,this.newManifest=null,this.bundledManifest=null,this.preventAutoUpdateLoop=e.preventAutoUpdateLoop===!0,this._lastUpdateFiles=localStorage.getItem("last_update_files"),e.serverRoot=e.serverRoot||"",e.serverRoot&&"/"!==e.serverRoot[e.serverRoot.length-1]&&(e.serverRoot+="/"),this.newManifestUrl=e.manifestUrl||e.serverRoot+(e.manifest||"manifest.json"),e.mode&&(e.mode="mirror"),this.cache=new i(e),this.corruptNewManifest=!1,this._toBeCopied=[],this._toBeDeleted=[],this._toBeDownloaded=[],this._updateReady=!1,this._checkTimeout=e.checkTimeout||1e4}var i=n(1),a=(n(3),null),s=location.href.replace(location.hash,"");s=s.substr(0,s.lastIndexOf("/")+1),/ip(hone|ad|od)/i.test(navigator.userAgent)&&(s=location.pathname.substr(location.pathname.indexOf("/www/")),s=s.substr(0,s.lastIndexOf("/")+1),s="cdvfile://localhost/bundle"+s),r.prototype._createFilemap=function(e){var t={},n=this.cache._fs.normalize;return Object.keys(e).forEach(function(o){e[o].filename=n(e[o].filename),t[e[o].filename]=e[o]}),t},r.prototype.copyFromBundle=function(e){var t=s+e;return this.cache._fs.download(t,this.cache.localRoot+e)},r.prototype.getBundledManifest=function(){var e=this,t=document.querySelector("script[manifest]"),n=(t?t.getAttribute("manifest"):null)||"manifest.json";return new a(function(t,o){e.bundledManifest?t(e.bundledManifest):(pegasus(n).then(function(n){e.bundledManifest=n,t(n)},o),setTimeout(function(){o(new Error("bundled manifest timeout"))},e._checkTimeout))})},r.prototype.check=function(e){var t=this,n=this.manifest;"string"==typeof e&&(t.newManifestUrl=e,e=void 0);var r=new a(function(n,o){"object"==typeof e?n(e):(pegasus(t.newManifestUrl).then(n,o),setTimeout(function(){o(new Error("new manifest timeout"))},t._checkTimeout))});return new a(function(e,i){a.all([r,t.getBundledManifest(),t.cache.list()]).then(function(r){var a=r[0],s=r[1],u=o(a.files);if(t.preventAutoUpdateLoop===!0&&u===t._lastUpdateFiles){var c=o(Manifest.files);return t._lastUpdateFiles!==c&&(console.warn("New manifest available, but an earlier update attempt failed. Will not download."),t.corruptNewManifest=!0,e(null)),void e(!1)}if(!a.files)return void i(new Error('Downloaded Manifest has no "files" attribute.'));var f=r[2],l=t._createFilemap(n.files),u=t._createFilemap(a.files),d=t._createFilemap(s.files);t._toBeDownloaded=[],t._toBeCopied=[],t._toBeDeleted=[];var h=t.cache._fs.isCordova,p=0;Object.keys(u).filter(function(e){return!l[e]||l[e].version!==u[e].version}).forEach(function(e){h&&d[e]&&d[e].version===u[e].version?t._toBeCopied.push(e):t._toBeDownloaded.push(e),d[e]&&d[e].version===u[e].version||p++}),t._toBeDeleted=f.map(function(e){return e.substr(t.cache.localRoot.length)}).filter(function(e){return!u[e]||t._toBeDownloaded.indexOf(e)>=0||t._toBeCopied.indexOf(e)>=0}),p+=t._toBeDeleted.length,p>0?(t.newManifest=a,t.newManifest.root=t.cache.localUrl,e(!0)):e(!1)},function(e){i(e)})})},r.prototype.canDownload=function(){return!!this.newManifest&&!this._updateReady},r.prototype.canUpdate=function(){return this._updateReady},r.prototype.download=function(e,t){var n=this;return n.canDownload()?(localStorage.removeItem("manifest"),localStorage.setItem("last_update_files",o(this.newManifest.files)),this.manifest.files=Manifest.files={},n.cache.remove(n._toBeDeleted,!0).then(function(){return a.all(n._toBeCopied.map(function(e){return n.cache._fs.download(s+e,n.cache.localRoot+e)}))}).then(function(){return n.allowServerRootFromManifest&&n.newManifest.serverRoot&&(n.cache.serverRoot=n.newManifest.serverRoot),n.cache.add(n._toBeDownloaded),n.cache.download(e,t)}).then(function(){return n._toBeDeleted=[],n._toBeDownloaded=[],n._updateReady=!0,n.newManifest},function(e){return e&&e.length&&n.cache.remove(e),e})):new a(function(e){e(null)})},r.prototype.update=function(e){return this._updateReady?(localStorage.setItem("manifest",JSON.stringify(this.newManifest)),e!==!1&&location.reload(),!0):!1},r.prototype.clear=function(){return localStorage.removeItem("last_update_files"),localStorage.removeItem("manifest"),this.cache.clear()},r.prototype.reset=function(){return this.clear().then(function(){location.reload()},function(){location.reload()})},e.exports=r},function(e,t,n){function o(e){var t=this;if(this._fs=e.fs,!this._fs)throw new Error('Missing required option "fs". Add an instance of cordova-promise-fs.');i=this._fs.Promise,this._mirrorMode="hash"!==e.mode,this._retry=e.retry||[500,1500,8e3],this._cacheBuster=!!e.cacheBuster,this.localRoot=this._fs.normalize(e.localRoot||"data"),this.serverRoot=this._fs.normalize(e.serverRoot||""),this._downloading=[],this._added=[],this._cached={},this.ready=this._fs.ensure(this.localRoot).then(function(e){return t.localInternalURL=a?e.toInternalURL():e.toURL(),t.localUrl=e.toURL(),t.list()})}var r=n(2),i=null,a="undefined"!=typeof cordova;o.hash=r,o.prototype.list=function(){var e=this;return new i(function(t){e._fs.list(e.localRoot,"rfe").then(function(n){e._cached={},n=n.map(function(t){var n=e._fs.normalize(t.fullPath);return e._cached[n]={toInternalURL:a?t.toInternalURL():t.toURL(),toURL:t.toURL()},n}),t(n)},function(){t([])})})},o.prototype.add=function(e){e||(e=[]),"string"==typeof e&&(e=[e]);var t=this;return e.forEach(function(e){e=t.toServerURL(e),-1===t._added.indexOf(e)&&t._added.push(e)}),t.isDirty()},o.prototype.remove=function(e,t){e||(e=[]);var n=[];"string"==typeof e&&(e=[e]);var o=this;return e.forEach(function(e){var t=o._added.indexOf(o.toServerURL(e));t>=0&&o._added.splice(t,1);var r=o.toPath(e);n.push(o._fs.remove(r)),delete o._cached[r]}),t?i.all(n):o.isDirty()},o.prototype.getDownloadQueue=function(){var e=this,t=e._added.filter(function(t){return!e.isCached(t)});return t},o.prototype.getAdded=function(){return this._added},o.prototype.isDirty=function(){return this.getDownloadQueue().length>0},o.prototype.download=function(e,t){var n=this._fs,o=this;return t=t||!1,o.abort(),new i(function(r,i){n.ensure(o.localRoot).then(function(){return o.list()}).then(function(){if(!o.isDirty())return void r(o);var a=o.getDownloadQueue(),s=o._downloading.length,u=o._downloading.length+a.length,c=0;a.forEach(function(a){var f,l=o.toPath(a);"function"==typeof e&&(f=function(t){t.queueIndex=s,t.queueSize=u,t.url=a,t.path=l,t.percentage=s/u,t.loaded>0&&t.total>0&&s!==u&&(t.percentage+=t.loaded/t.total/u),t.percentage=Math.max(c,t.percentage),c=t.percentage,e(t)});var d=function(){s++,f(new ProgressEvent),s===u&&(o._downloading=[],o.list().then(function(){f&&f(new ProgressEvent),o.isDirty()?i(o.getDownloadQueue()):r(o)},i))},h=a;o._cacheBuster&&(h+="?"+Date.now());var p=n.download(h,l,{retry:o._retry},t?f:void 0);p.then(d,d),o._downloading.push(p)})},i)})},o.prototype.abort=function(){this._downloading.forEach(function(e){e.abort()}),this._downloading=[]},o.prototype.isCached=function(e){return e=this.toPath(e),!!this._cached[e]},o.prototype.clear=function(){var e=this;return this._cached={},this._fs.removeDir(this.localRoot).then(function(){return e._fs.ensure(e.localRoot)})},o.prototype.toInternalURL=function(e){return path=this.toPath(e),this._cached[path]?this._cached[path].toInternalURL:e},o.prototype.get=function(e){return path=this.toPath(e),this._cached[path]?this._cached[path].toURL:this.toServerURL(e)},o.prototype.toDataURL=function(e){return this._fs.toDataURL(this.toPath(e))},o.prototype.toURL=function(e){return path=this.toPath(e),this._cached[path]?this._cached[path].toURL:e},o.prototype.toServerURL=function(e){return e=this._fs.normalize(e),e.indexOf("://")<0?this.serverRoot+e:e},o.prototype.toPath=function(e){if(this._mirrorMode){var t=e.indexOf("?");return t>-1&&(e=e.substr(0,t)),e=e=this._fs.normalize(e||""),len=this.serverRoot.length,e.substr(0,len)!==this.serverRoot?this.localRoot+e:this.localRoot+e.substr(len)}var n=e.substr(e.lastIndexOf("."));return(n.indexOf("?")>0||n.indexOf("/")>0)&&(n=".txt"),this.localRoot+r(e)+n},e.exports=o},function(e){function t(e,t){var n,o,r,i,a,s,u,c;for(n=3&e.length,o=e.length-n,r=t,a=3432918353,s=461845907,c=0;o>c;)u=255&e.charCodeAt(c)|(255&e.charCodeAt(++c))<<8|(255&e.charCodeAt(++c))<<16|(255&e.charCodeAt(++c))<<24,++c,u=(65535&u)*a+(((u>>>16)*a&65535)<<16)&4294967295,u=u<<15|u>>>17,u=(65535&u)*s+(((u>>>16)*s&65535)<<16)&4294967295,r^=u,r=r<<13|r>>>19,i=5*(65535&r)+((5*(r>>>16)&65535)<<16)&4294967295,r=(65535&i)+27492+(((i>>>16)+58964&65535)<<16);switch(u=0,n){case 3:u^=(255&e.charCodeAt(c+2))<<16;case 2:u^=(255&e.charCodeAt(c+1))<<8;case 1:u^=255&e.charCodeAt(c),u=(65535&u)*a+(((u>>>16)*a&65535)<<16)&4294967295,u=u<<15|u>>>17,u=(65535&u)*s+(((u>>>16)*s&65535)<<16)&4294967295,r^=u}return r^=e.length,r^=r>>>16,r=2246822507*(65535&r)+((2246822507*(r>>>16)&65535)<<16)&4294967295,r^=r>>>13,r=3266489909*(65535&r)+((3266489909*(r>>>16)&65535)<<16)&4294967295,r^=r>>>16,r>>>0}e.exports=t},function(e){function t(e,n,o,r){e.getDirectory(n[0],{create:!0},function(e){n.length>1?t(e,n.slice(1),o,r):o(e)},r)}function n(e){return e=e.substr(0,e.lastIndexOf("/")+1),"/"===e[0]&&(e=e.substr(1)),e}function o(e){return e.substr(e.lastIndexOf("/")+1)}function r(e){e=e||"","/"===e[0]&&(e=e.substr(1));for(var t=e.split("/"),n=t[0],o=1;o<t.length;o++)n=t[o],".."===t[o]?(t.splice(o-1,2),o-=2):"."===t[o]&&(t.splice(o,1),o--);return e=t.join("/"),"./"===e?e="":n&&n.indexOf(".")<0&&"/"!=e[e.length-1]&&(e+="/"),e}var i=[],a=0;e.exports=function(e){function s(e){return new E(function(t){return t(e)})}function u(e){return new E(function(n,o){return F.then(function(r){e?(e=e.split("/").filter(function(e){return e&&e.length>0&&"."!==e&&".."!==e}),t(r.root,e,n,o)):n(r.root)},o)})}function c(e,t){return new E(function(n,o){return"object"==typeof e?n(e):(e=r(e),t=t||{},F.then(function(r){r.root.getFile(e,t,n,o)},o))})}function f(e,t){return e=r(e),t=t||{},new E(function(n,o){return F.then(function(r){e&&"/"!==e?r.root.getDirectory(e,t,n,o):n(r.root)},o)})}function l(e,t){t=t||"";var n=t.indexOf("r")>-1,o=t.indexOf("e")>-1,r=t.indexOf("f")>-1,i=t.indexOf("d")>-1;return r&&i&&(r=!1,i=!1),new E(function(t,a){return f(e).then(function(e){var u=e.createReader();u.readEntries(function(e){var u=[s(e)];n&&e.filter(function(e){return e.isDirectory}).forEach(function(e){u.push(l(e.fullPath,"re"))}),E.all(u).then(function(e){var n=[];n=n.concat.apply(n,e),r&&(n=n.filter(function(e){return e.isFile})),i&&(n=n.filter(function(e){return e.isDirectory})),o||(n=n.map(function(e){return e.fullPath})),t(n)},a)},a)},a)})}function d(e){return new E(function(t,n){c(e).then(function(e){t(e)},function(e){1===e.code?t(!1):n(e)})})}function h(e){return new E(function(t,n){f(e).then(function(e){t(e)},function(e){1===e.code?t(!1):n(e)})})}function p(e){return u(n(e)).then(function(){return c(e,{create:!0})})}function w(e){return c(e).then(function(e){return e.toURL()})}function v(e,t){return t=t||"readAsText",c(e).then(function(e){return new E(function(n,o){e.file(function(e){var o=new FileReader;o.onloadend=function(){n(this.result)},o[t](e)},o)})})}function m(e){return v(e,"readAsDataURL")}function y(e){return v(e).then(JSON.parse)}function _(e,t,o){return u(n(e)).then(function(){return c(e,{create:!0})}).then(function(e){return new E(function(n,r){e.createWriter(function(e){e.onwriteend=n,e.onerror=r,"string"==typeof t?t=g([t],o||"text/plain"):t instanceof Blob!=!0&&(t=g([JSON.stringify(t,null,4)],o||"application/json")),e.write(t)},r)})})}function g(e,t){var n,o;try{return new Blob(e,{type:t})}catch(r){if(n=window.BlobBuilder||window.WebKitBlobBuilder||window.MozBlobBuilder||window.MSBlobBuilder)return o=new n,o.append(e),o.getBlob(t);throw new Error("Unable to create blob")}}function R(e,t){return u(n(t)).then(function(n){return c(e).then(function(e){return new E(function(r,i){e.moveTo(n,o(t),r,i)})})})}function b(e,t){return e=e.replace(/\/$/,""),t=t.replace(/\/$/,""),u(n(t)).then(function(n){return f(e).then(function(e){return new E(function(r,i){e.moveTo(n,o(t),r,i)})})})}function U(e,t){return u(n(t)).then(function(n){return c(e).then(function(e){return new E(function(r,i){e.copyTo(n,o(t),r,i)})})})}function L(e,t){var n=t?c:d;return new E(function(t,o){n(e).then(function(e){e!==!1?e.remove(t,o):t(1)},o)}).then(function(e){return 1===e?!1:!0})}function D(e){return f(e).then(function(e){return new E(function(t,n){e.removeRecursively(t,n)})})}function O(){for(;i.length>0&&a<e.concurrency;){a++;var t=i.pop(),n=t.fileTransfer,o=t.isDownload,r=t.serverUrl,s=t.localPath,u=t.trustAllHosts,c=t.transferOptions,f=t.win,l=t.fail;n._aborted?a--:o?(n.download.call(n,r,s,f,l,u,c),n.onprogress&&n.onprogress(new ProgressEvent)):n.upload.call(n,s,r,f,l,c,u)}}function S(e){return a--,O(),e}function x(t,n,r,s,u){"function"==typeof s&&(u=s,s={}),C&&r.indexOf("://")<0&&(r=I(r)),s=s||{},s.retry&&s.retry.length||(s.retry=e.retry),s.retry=s.retry.concat(),s.file||t||(s.fileName=o(r));var c=new FileTransfer;u=u||s.onprogress,"function"==typeof u&&(c.onprogress=u);var f=new E(function(o,u){var f=function(a){if(0===s.retry.length)e.debug&&console.log("FileTransfer Error: "+n,a),u(a);else{var l={fileTransfer:c,isDownload:t,serverUrl:n,localPath:r,trustAllHosts:s.trustAllHosts||!1,transferOptions:s,win:o,fail:f};i.unshift(l);var d=s.retry.shift();d>0?setTimeout(S,d):S()}};s.retry.unshift(0),a++,f()});return f.then(S,S),f.progress=function(e){return c.onprogress=e,f},f.abort=function(){return c._aborted=!0,c.abort(),f},f}function M(e,t,n,o){return x(!0,e,t,n,o)}function B(e,t,n,o){return x(!1,t,e,n,o)}var E=e.Promise||window.Promise;if(!E)throw new Error("No Promise library given in options.Promise");e=e||{},e.persistent=void 0!==e.persistent?e.persistent:!0,e.storageSize=e.storageSize||20971520,e.concurrency=e.concurrency||3,e.retry=e.retry||[],e.debug=!!e.debug;var T,C="undefined"!=typeof cordova;C?T=new E(function(e,t){document.addEventListener("deviceready",e,!1),setTimeout(function(){t(new Error("deviceready has not fired after 5 seconds."))},5100)}):(T=s(!0),"undefined"!=typeof webkitRequestFileSystem?(window.requestFileSystem=webkitRequestFileSystem,window.FileTransfer=function(){},FileTransfer.prototype.download=function(e,t,n,o){var r=new XMLHttpRequest;return r.open("GET",e),r.responseType="blob",r.onreadystatechange=function(){4==r.readyState&&(200!==r.status||this._aborted?o(r.status):_(t,r.response).then(n,o))},r.send(),r},FileTransfer.prototype.abort=function(){this._aborted=!0},window.ProgressEvent=function(){},window.FileEntry=function(){}):window.requestFileSystem=function(e,t,n,o){o(new Error("requestFileSystem not supported!"))});var F=new E(function(t,n){T.then(function(){var o=e.persistent?1:0;e.fileSystem&&C&&(o=e.fileSystem),!C&&1===o&&navigator.webkitPersistentStorage?navigator.webkitPersistentStorage.requestQuota(e.storageSize,function(e){window.requestFileSystem(o,e,t,n)},n):isNaN(o)?window.resolveLocalFileSystemURL(o,function(e){t(e.filesystem)},n):window.requestFileSystem(o,e.storageSize,t,n),setTimeout(function(){n(new Error("Could not retrieve FileSystem after 5 seconds."))},5100)},n)});F.then(function(e){CDV_INTERNAL_URL_ROOT=e.root.toInternalURL(),CDV_URL_ROOT=e.root.toURL(),window.__fs=e},function(e){console.error("Could not get Cordova FileSystem:",e)});var A,P,I;return CDV_INTERNAL_URL_ROOT="cdvfile://localhost/"+(e.persistent?"persistent/":"temporary/"),CDV_URL_ROOT="",C?(P=function(e){return e=r(e),e.indexOf("://")<0?CDV_INTERNAL_URL_ROOT+e:e},I=function(e){return e=r(e),e.indexOf("://")<0?CDV_URL_ROOT+e:e},A=function(e){return c(e).then(function(e){return e.toInternalURL()})}):(P=function(t){return t=r(t),"filesystem:"+location.origin+(e.persistent?"/persistent/":"/temporary/")+t},A=function(e){return c(e).then(function(e){return e.toURL()})},I=P),{fs:F,normalize:r,file:c,filename:o,dir:f,dirname:n,create:p,read:v,readJSON:y,write:_,move:R,moveDir:b,copy:U,remove:L,removeDir:D,list:l,ensure:u,exists:d,existsDir:h,download:M,upload:B,toURL:w,isCordova:C,toInternalURLSync:P,toInternalURL:A,toDataURL:m,deviceready:T,options:e,Promise:E}}}]);