cordova-file-cache
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -5,39 +5,65 @@ var CordovaFileCache = | ||
/******/ var installedModules = {}; | ||
/******/ | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
/******/ | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) | ||
/******/ return installedModules[moduleId].exports; | ||
/******/ | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ exports: {}, | ||
/******/ id: moduleId, | ||
/******/ loaded: false | ||
/******/ i: moduleId, | ||
/******/ l: false, | ||
/******/ exports: {} | ||
/******/ }; | ||
/******/ | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
/******/ | ||
/******/ // Flag the module as loaded | ||
/******/ module.loaded = true; | ||
/******/ module.l = true; | ||
/******/ | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
/******/ | ||
/******/ | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
/******/ | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
/******/ | ||
/******/ // identity function for calling harmony imports with the correct context | ||
/******/ __webpack_require__.i = function(value) { return value; }; | ||
/******/ | ||
/******/ // define getter function for harmony exports | ||
/******/ __webpack_require__.d = function(exports, name, getter) { | ||
/******/ if(!__webpack_require__.o(exports, name)) { | ||
/******/ Object.defineProperty(exports, name, { | ||
/******/ configurable: false, | ||
/******/ enumerable: true, | ||
/******/ get: getter | ||
/******/ }); | ||
/******/ } | ||
/******/ }; | ||
/******/ | ||
/******/ // getDefaultExport function for compatibility with non-harmony modules | ||
/******/ __webpack_require__.n = function(module) { | ||
/******/ var getter = module && module.__esModule ? | ||
/******/ function getDefault() { return module['default']; } : | ||
/******/ function getModuleExports() { return module; }; | ||
/******/ __webpack_require__.d(getter, 'a', getter); | ||
/******/ return getter; | ||
/******/ }; | ||
/******/ | ||
/******/ // Object.prototype.hasOwnProperty.call | ||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | ||
/******/ | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
/******/ | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(0); | ||
/******/ return __webpack_require__(__webpack_require__.s = 1); | ||
/******/ }) | ||
@@ -47,349 +73,349 @@ /************************************************************************/ | ||
/* 0 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ (function(module, exports) { | ||
var hash = __webpack_require__(1); | ||
var Promise = null; | ||
/** | ||
* JS Implementation of MurmurHash3 (r136) (as of May 20, 2011) | ||
* | ||
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a> | ||
* @see http://github.com/garycourt/murmurhash-js | ||
* @author <a href="mailto:aappleby@gmail.com">Austin Appleby</a> | ||
* @see http://sites.google.com/site/murmurhash/ | ||
* | ||
* @param {string} key ASCII only | ||
* @param {number} seed Positive integer only | ||
* @return {number} 32-bit positive integer hash | ||
*/ | ||
/* Cordova File Cache x */ | ||
function FileCache(options){ | ||
var self = this; | ||
// cordova-promise-fs | ||
this._fs = options.fs; | ||
if(!this._fs) { | ||
throw new Error('Missing required option "fs". Add an instance of cordova-promise-fs.'); | ||
} | ||
// Use Promises from fs. | ||
Promise = this._fs.Promise; | ||
function murmurhash3_32_gc(key, seed) { | ||
var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i; | ||
remainder = key.length & 3; // key.length % 4 | ||
bytes = key.length - remainder; | ||
h1 = seed; | ||
c1 = 0xcc9e2d51; | ||
c2 = 0x1b873593; | ||
i = 0; | ||
while (i < bytes) { | ||
k1 = | ||
((key.charCodeAt(i) & 0xff)) | | ||
((key.charCodeAt(++i) & 0xff) << 8) | | ||
((key.charCodeAt(++i) & 0xff) << 16) | | ||
((key.charCodeAt(++i) & 0xff) << 24); | ||
++i; | ||
k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff; | ||
k1 = (k1 << 15) | (k1 >>> 17); | ||
k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff; | ||
// 'mirror' mirrors files structure from "serverRoot" to "localRoot" | ||
// 'hash' creates a 1-deep filestructure, where the filenames are hashed server urls (with extension) | ||
this._mirrorMode = options.mode !== 'hash'; | ||
this._retry = options.retry || [500,1500,8000]; | ||
this._cacheBuster = !!options.cacheBuster; | ||
h1 ^= k1; | ||
h1 = (h1 << 13) | (h1 >>> 19); | ||
h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff; | ||
h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16)); | ||
} | ||
k1 = 0; | ||
switch (remainder) { | ||
case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16; | ||
case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8; | ||
case 1: k1 ^= (key.charCodeAt(i) & 0xff); | ||
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff; | ||
k1 = (k1 << 15) | (k1 >>> 17); | ||
k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff; | ||
h1 ^= k1; | ||
} | ||
h1 ^= key.length; | ||
// normalize path | ||
this.localRoot = this._fs.normalize(options.localRoot || 'data'); | ||
this.serverRoot = this._fs.normalize(options.serverRoot || ''); | ||
h1 ^= h1 >>> 16; | ||
h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff; | ||
h1 ^= h1 >>> 13; | ||
h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff; | ||
h1 ^= h1 >>> 16; | ||
// set internal variables | ||
this._downloading = []; // download promises | ||
this._added = []; // added files | ||
this._cached = {}; // cached files | ||
return h1 >>> 0; | ||
} | ||
// list existing cache contents | ||
this.ready = this._fs.ensure(this.localRoot) | ||
.then(function(entry){ | ||
self.localInternalURL = typeof entry.toInternalURL === 'function'? entry.toInternalURL(): entry.toURL(); | ||
self.localUrl = entry.toURL(); | ||
return self.list(); | ||
}); | ||
} | ||
module.exports = murmurhash3_32_gc; | ||
FileCache.hash = hash; | ||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
/** | ||
* Helper to cache all 'internalURL' and 'URL' for quick synchronous access | ||
* to the cached files. | ||
*/ | ||
FileCache.prototype.list = function list(){ | ||
var self = this; | ||
return new Promise(function(resolve,reject){ | ||
self._fs.list(self.localRoot,'rfe').then(function(entries){ | ||
self._cached = {}; | ||
entries = entries.map(function(entry){ | ||
var fullPath = self._fs.normalize(entry.fullPath); | ||
self._cached[fullPath] = { | ||
toInternalURL: typeof entry.toInternalURL === 'function'? entry.toInternalURL(): entry.toURL(), | ||
toURL: entry.toURL(), | ||
}; | ||
return fullPath; | ||
}); | ||
resolve(entries); | ||
},function(){ | ||
resolve([]); | ||
}); | ||
}); | ||
}; | ||
var hash = __webpack_require__(0); | ||
var Promise = null; | ||
FileCache.prototype.add = function add(urls){ | ||
if(!urls) urls = []; | ||
if(typeof urls === 'string') urls = [urls]; | ||
var self = this; | ||
urls.forEach(function(url){ | ||
url = self.toServerURL(url); | ||
if(self._added.indexOf(url) === -1) { | ||
self._added.push(url); | ||
} | ||
}); | ||
return self.isDirty(); | ||
}; | ||
/* Cordova File Cache x */ | ||
function FileCache(options){ | ||
var self = this; | ||
// cordova-promise-fs | ||
this._fs = options.fs; | ||
if(!this._fs) { | ||
throw new Error('Missing required option "fs". Add an instance of cordova-promise-fs.'); | ||
} | ||
// Use Promises from fs. | ||
Promise = this._fs.Promise; | ||
FileCache.prototype.remove = function remove(urls,returnPromises){ | ||
if(!urls) urls = []; | ||
var promises = []; | ||
if(typeof urls === 'string') urls = [urls]; | ||
var self = this; | ||
urls.forEach(function(url){ | ||
var index = self._added.indexOf(self.toServerURL(url)); | ||
if(index >= 0) self._added.splice(index,1); | ||
var path = self.toPath(url); | ||
promises.push(self._fs.remove(path)); | ||
delete self._cached[path]; | ||
}); | ||
return returnPromises? Promise.all(promises): self.isDirty(); | ||
}; | ||
// 'mirror' mirrors files structure from "serverRoot" to "localRoot" | ||
// 'hash' creates a 1-deep filestructure, where the filenames are hashed server urls (with extension) | ||
this._mirrorMode = options.mode !== 'hash'; | ||
this._retry = options.retry || [500,1500,8000]; | ||
this._cacheBuster = !!options.cacheBuster; | ||
FileCache.prototype.getDownloadQueue = function(){ | ||
var self = this; | ||
var queue = self._added.filter(function(url){ | ||
return !self.isCached(url); | ||
}); | ||
return queue; | ||
}; | ||
// normalize path | ||
this.localRoot = this._fs.normalize(options.localRoot || 'data'); | ||
this.serverRoot = this._fs.normalize(options.serverRoot || ''); | ||
FileCache.prototype.getAdded = function() { | ||
return this._added; | ||
}; | ||
// set internal variables | ||
this._downloading = []; // download promises | ||
this._added = []; // added files | ||
this._cached = {}; // cached files | ||
FileCache.prototype.isDirty = function isDirty(){ | ||
return this.getDownloadQueue().length > 0; | ||
}; | ||
// list existing cache contents | ||
this.ready = this._fs.ensure(this.localRoot) | ||
.then(function(entry){ | ||
self.localInternalURL = typeof entry.toInternalURL === 'function'? entry.toInternalURL(): entry.toURL(); | ||
self.localUrl = entry.toURL(); | ||
return self.list(); | ||
}); | ||
} | ||
FileCache.prototype.download = function download(onprogress,includeFileProgressEvents){ | ||
var fs = this._fs; | ||
var self = this; | ||
includeFileProgressEvents = includeFileProgressEvents || false; | ||
self.abort(); | ||
FileCache.hash = hash; | ||
return new Promise(function(resolve,reject){ | ||
// make sure cache directory exists and that | ||
// we have retrieved the latest cache contents | ||
// to avoid downloading files we already have! | ||
fs.ensure(self.localRoot).then(function(){ | ||
return self.list(); | ||
}).then(function(){ | ||
// no dowloads needed, resolve | ||
if(!self.isDirty()) { | ||
resolve(self); | ||
return; | ||
} | ||
/** | ||
* Helper to cache all 'internalURL' and 'URL' for quick synchronous access | ||
* to the cached files. | ||
*/ | ||
FileCache.prototype.list = function list(){ | ||
var self = this; | ||
return new Promise(function(resolve,reject){ | ||
self._fs.list(self.localRoot,'rfe').then(function(entries){ | ||
self._cached = {}; | ||
entries = entries.map(function(entry){ | ||
var fullPath = self._fs.normalize(entry.fullPath); | ||
self._cached[fullPath] = { | ||
toInternalURL: typeof entry.toInternalURL === 'function'? entry.toInternalURL(): entry.toURL(), | ||
toURL: entry.toURL(), | ||
}; | ||
return fullPath; | ||
}); | ||
resolve(entries); | ||
},function(){ | ||
resolve([]); | ||
}); | ||
}); | ||
}; | ||
// keep track of number of downloads! | ||
var queue = self.getDownloadQueue(); | ||
var done = self._downloading.length; | ||
var total = self._downloading.length + queue.length; | ||
var percentage = 0; | ||
var errors = []; | ||
FileCache.prototype.add = function add(urls){ | ||
if(!urls) urls = []; | ||
if(typeof urls === 'string') urls = [urls]; | ||
var self = this; | ||
urls.forEach(function(url){ | ||
url = self.toServerURL(url); | ||
if(self._added.indexOf(url) === -1) { | ||
self._added.push(url); | ||
} | ||
}); | ||
return self.isDirty(); | ||
}; | ||
// download every file in the queue (which is the diff from _added with _cached) | ||
queue.forEach(function(url){ | ||
var path = self.toPath(url); | ||
// augment progress event with done/total stats | ||
var onSingleDownloadProgress; | ||
if(typeof onprogress === 'function') { | ||
onSingleDownloadProgress = function(ev){ | ||
ev.queueIndex = done; | ||
ev.queueSize = total; | ||
ev.url = url; | ||
ev.path = path; | ||
ev.percentage = done / total; | ||
if(ev.loaded > 0 && ev.total > 0 && done !== total){ | ||
ev.percentage += (ev.loaded / ev.total) / total; | ||
} | ||
ev.percentage = Math.max(percentage,ev.percentage); | ||
percentage = ev.percentage; | ||
onprogress(ev); | ||
}; | ||
} | ||
FileCache.prototype.remove = function remove(urls,returnPromises){ | ||
if(!urls) urls = []; | ||
var promises = []; | ||
if(typeof urls === 'string') urls = [urls]; | ||
var self = this; | ||
urls.forEach(function(url){ | ||
var index = self._added.indexOf(self.toServerURL(url)); | ||
if(index >= 0) self._added.splice(index,1); | ||
var path = self.toPath(url); | ||
promises.push(self._fs.remove(path)); | ||
delete self._cached[path]; | ||
}); | ||
return returnPromises? Promise.all(promises): self.isDirty(); | ||
}; | ||
// callback | ||
var onDone = function(){ | ||
done++; | ||
if(onSingleDownloadProgress) onSingleDownloadProgress(new ProgressEvent()); | ||
FileCache.prototype.getDownloadQueue = function(){ | ||
var self = this; | ||
var queue = self._added.filter(function(url){ | ||
return !self.isCached(url); | ||
}); | ||
return queue; | ||
}; | ||
// when we're done | ||
if(done === total) { | ||
// reset downloads | ||
self._downloading = []; | ||
// check if we got everything | ||
self.list().then(function(){ | ||
// final progress event! | ||
if(onSingleDownloadProgress) onSingleDownloadProgress(new ProgressEvent()); | ||
// Yes, we're not dirty anymore! | ||
if(!self.isDirty()) { | ||
resolve(self); | ||
// Aye, some files got left behind! | ||
} else { | ||
reject(errors); | ||
} | ||
},reject); | ||
} | ||
}; | ||
var onErr = function(err){ | ||
if(err && err.target && err.target.error) err = err.target.error; | ||
errors.push(err); | ||
onDone(); | ||
}; | ||
FileCache.prototype.getAdded = function() { | ||
return this._added; | ||
}; | ||
var downloadUrl = url; | ||
if(self._cacheBuster) downloadUrl += "?"+Date.now(); | ||
var download = fs.download(downloadUrl,path,{retry:self._retry},includeFileProgressEvents && onSingleDownloadProgress? onSingleDownloadProgress: undefined); | ||
download.then(onDone,onErr); | ||
self._downloading.push(download); | ||
}); | ||
},reject); | ||
}); | ||
}; | ||
FileCache.prototype.isDirty = function isDirty(){ | ||
return this.getDownloadQueue().length > 0; | ||
}; | ||
FileCache.prototype.abort = function abort(){ | ||
this._downloading.forEach(function(download){ | ||
download.abort(); | ||
}); | ||
this._downloading = []; | ||
}; | ||
FileCache.prototype.download = function download(onprogress,includeFileProgressEvents){ | ||
var fs = this._fs; | ||
var self = this; | ||
includeFileProgressEvents = includeFileProgressEvents || false; | ||
self.abort(); | ||
FileCache.prototype.isCached = function isCached(url){ | ||
url = this.toPath(url); | ||
return !!this._cached[url]; | ||
}; | ||
return new Promise(function(resolve,reject){ | ||
// make sure cache directory exists and that | ||
// we have retrieved the latest cache contents | ||
// to avoid downloading files we already have! | ||
fs.ensure(self.localRoot).then(function(){ | ||
return self.list(); | ||
}).then(function(){ | ||
// no dowloads needed, resolve | ||
if(!self.isDirty()) { | ||
resolve(self); | ||
return; | ||
} | ||
FileCache.prototype.clear = function clear(){ | ||
var self = this; | ||
this._cached = {}; | ||
return this._fs.removeDir(this.localRoot).then(function(){ | ||
return self._fs.ensure(self.localRoot); | ||
}); | ||
}; | ||
// keep track of number of downloads! | ||
var queue = self.getDownloadQueue(); | ||
var done = self._downloading.length; | ||
var total = self._downloading.length + queue.length; | ||
var percentage = 0; | ||
var errors = []; | ||
/** | ||
* Helpers to output to various formats | ||
*/ | ||
FileCache.prototype.toInternalURL = function toInternalURL(url){ | ||
var path = this.toPath(url); | ||
if(this._cached[path]) return this._cached[path].toInternalURL; | ||
return url; | ||
}; | ||
// download every file in the queue (which is the diff from _added with _cached) | ||
queue.forEach(function(url){ | ||
var path = self.toPath(url); | ||
// augment progress event with done/total stats | ||
var onSingleDownloadProgress; | ||
if(typeof onprogress === 'function') { | ||
onSingleDownloadProgress = function(ev){ | ||
ev.queueIndex = done; | ||
ev.queueSize = total; | ||
ev.url = url; | ||
ev.path = path; | ||
ev.percentage = done / total; | ||
if(ev.loaded > 0 && ev.total > 0 && done !== total){ | ||
ev.percentage += (ev.loaded / ev.total) / total; | ||
} | ||
ev.percentage = Math.max(percentage,ev.percentage); | ||
percentage = ev.percentage; | ||
onprogress(ev); | ||
}; | ||
} | ||
FileCache.prototype.get = function get(url){ | ||
var path = this.toPath(url); | ||
if(this._cached[path]) return this._cached[path].toURL; | ||
return this.toServerURL(url); | ||
}; | ||
// callback | ||
var onDone = function(){ | ||
done++; | ||
if(onSingleDownloadProgress) onSingleDownloadProgress(new ProgressEvent()); | ||
FileCache.prototype.toDataURL = function toDataURL(url){ | ||
return this._fs.toDataURL(this.toPath(url)); | ||
}; | ||
// when we're done | ||
if(done === total) { | ||
// reset downloads | ||
self._downloading = []; | ||
// check if we got everything | ||
self.list().then(function(){ | ||
// final progress event! | ||
if(onSingleDownloadProgress) onSingleDownloadProgress(new ProgressEvent()); | ||
// Yes, we're not dirty anymore! | ||
if(errors.length === 0) { | ||
resolve(self); | ||
// Aye, some files got left behind! | ||
} else { | ||
reject(errors); | ||
} | ||
},reject); | ||
} | ||
}; | ||
var onErr = function(err){ | ||
if(err && err.target && err.target.error) err = err.target.error; | ||
errors.push(err); | ||
onDone(); | ||
}; | ||
FileCache.prototype.toURL = function toURL(url){ | ||
var path = this.toPath(url); | ||
return this._cached[path]? this._cached[path].toURL: url; | ||
}; | ||
var downloadUrl = url; | ||
if(self._cacheBuster) downloadUrl += "?"+Date.now(); | ||
var download = fs.download(downloadUrl,path,{retry:self._retry},includeFileProgressEvents && onSingleDownloadProgress? onSingleDownloadProgress: undefined); | ||
download.then(onDone,onErr); | ||
self._downloading.push(download); | ||
}); | ||
},reject); | ||
}); | ||
}; | ||
FileCache.prototype.toServerURL = function toServerURL(path){ | ||
var path = this._fs.normalize(path); | ||
return path.indexOf('://') < 0? this.serverRoot + path: path; | ||
}; | ||
FileCache.prototype.abort = function abort(){ | ||
this._downloading.forEach(function(download){ | ||
download.abort(); | ||
}); | ||
this._downloading = []; | ||
}; | ||
/** | ||
* Helper to transform remote URL to a local path (for cordova-promise-fs) | ||
*/ | ||
FileCache.prototype.toPath = function toPath(url){ | ||
if(this._mirrorMode) { | ||
var query = url.indexOf('?'); | ||
if(query > -1){ | ||
url = url.substr(0,query); | ||
} | ||
url = this._fs.normalize(url || ''); | ||
var len = this.serverRoot.length; | ||
if(url.substr(0,len) !== this.serverRoot) { | ||
return this.localRoot + url; | ||
} else { | ||
return this.localRoot + url.substr(len); | ||
} | ||
} else { | ||
var ext = url.match(/\.[a-z]{1,}/g); | ||
if (ext) { | ||
ext = ext[ext.length-1]; | ||
} else { | ||
ext = '.txt'; | ||
} | ||
return this.localRoot + hash(url) + ext; | ||
} | ||
}; | ||
FileCache.prototype.isCached = function isCached(url){ | ||
url = this.toPath(url); | ||
return !!this._cached[url]; | ||
}; | ||
module.exports = FileCache; | ||
FileCache.prototype.clear = function clear(){ | ||
var self = this; | ||
this._cached = {}; | ||
return this._fs.removeDir(this.localRoot).then(function(){ | ||
return self._fs.ensure(self.localRoot); | ||
}); | ||
}; | ||
/** | ||
* Helpers to output to various formats | ||
*/ | ||
FileCache.prototype.toInternalURL = function toInternalURL(url){ | ||
var path = this.toPath(url); | ||
if(this._cached[path]) return this._cached[path].toInternalURL; | ||
return url; | ||
}; | ||
/***/ }, | ||
/* 1 */ | ||
/***/ function(module, exports) { | ||
FileCache.prototype.get = function get(url){ | ||
var path = this.toPath(url); | ||
if(this._cached[path]) return this._cached[path].toURL; | ||
return this.toServerURL(url); | ||
}; | ||
/** | ||
* JS Implementation of MurmurHash3 (r136) (as of May 20, 2011) | ||
* | ||
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a> | ||
* @see http://github.com/garycourt/murmurhash-js | ||
* @author <a href="mailto:aappleby@gmail.com">Austin Appleby</a> | ||
* @see http://sites.google.com/site/murmurhash/ | ||
* | ||
* @param {string} key ASCII only | ||
* @param {number} seed Positive integer only | ||
* @return {number} 32-bit positive integer hash | ||
*/ | ||
FileCache.prototype.toDataURL = function toDataURL(url){ | ||
return this._fs.toDataURL(this.toPath(url)); | ||
}; | ||
function murmurhash3_32_gc(key, seed) { | ||
var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i; | ||
remainder = key.length & 3; // key.length % 4 | ||
bytes = key.length - remainder; | ||
h1 = seed; | ||
c1 = 0xcc9e2d51; | ||
c2 = 0x1b873593; | ||
i = 0; | ||
while (i < bytes) { | ||
k1 = | ||
((key.charCodeAt(i) & 0xff)) | | ||
((key.charCodeAt(++i) & 0xff) << 8) | | ||
((key.charCodeAt(++i) & 0xff) << 16) | | ||
((key.charCodeAt(++i) & 0xff) << 24); | ||
++i; | ||
k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff; | ||
k1 = (k1 << 15) | (k1 >>> 17); | ||
k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff; | ||
FileCache.prototype.toURL = function toURL(url){ | ||
var path = this.toPath(url); | ||
return this._cached[path]? this._cached[path].toURL: url; | ||
}; | ||
h1 ^= k1; | ||
h1 = (h1 << 13) | (h1 >>> 19); | ||
h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff; | ||
h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16)); | ||
} | ||
k1 = 0; | ||
switch (remainder) { | ||
case 3: k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16; | ||
case 2: k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8; | ||
case 1: k1 ^= (key.charCodeAt(i) & 0xff); | ||
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff; | ||
k1 = (k1 << 15) | (k1 >>> 17); | ||
k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff; | ||
h1 ^= k1; | ||
} | ||
h1 ^= key.length; | ||
FileCache.prototype.toServerURL = function toServerURL(path){ | ||
var path = this._fs.normalize(path); | ||
return path.indexOf('://') < 0? this.serverRoot + path: path; | ||
}; | ||
h1 ^= h1 >>> 16; | ||
h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff; | ||
h1 ^= h1 >>> 13; | ||
h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff; | ||
h1 ^= h1 >>> 16; | ||
/** | ||
* Helper to transform remote URL to a local path (for cordova-promise-fs) | ||
*/ | ||
FileCache.prototype.toPath = function toPath(url){ | ||
if(this._mirrorMode) { | ||
var query = url.indexOf('?'); | ||
if(query > -1){ | ||
url = url.substr(0,query); | ||
} | ||
url = this._fs.normalize(url || ''); | ||
var len = this.serverRoot.length; | ||
if(url.substr(0,len) !== this.serverRoot) { | ||
return this.localRoot + url; | ||
} else { | ||
return this.localRoot + url.substr(len); | ||
} | ||
} else { | ||
var ext = url.match(/\.[a-z]{1,}/g); | ||
if (ext) { | ||
ext = ext[ext.length-1]; | ||
} else { | ||
ext = '.txt'; | ||
} | ||
return this.localRoot + hash(url) + ext; | ||
} | ||
}; | ||
return h1 >>> 0; | ||
} | ||
module.exports = FileCache; | ||
module.exports = murmurhash3_32_gc; | ||
/***/ } | ||
/***/ }) | ||
/******/ ]); |
@@ -170,3 +170,3 @@ var hash = require('./murmerhash'); | ||
// Yes, we're not dirty anymore! | ||
if(!self.isDirty()) { | ||
if(errors.length === 0) { | ||
resolve(self); | ||
@@ -173,0 +173,0 @@ // Aye, some files got left behind! |
{ | ||
"name": "cordova-file-cache", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Cordova File Cache", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,3 +14,3 @@ cordova-file-cache | ||
npm install cordova-file-cache cordova-promise-fs | ||
# install Cordova and plugins | ||
@@ -34,3 +34,3 @@ cordova platform add ios | ||
Promise: Promise // <-- your favorite Promise lib (REQUIRED) | ||
}), | ||
}), | ||
mode: 'hash', // or 'mirror', optional | ||
@@ -45,3 +45,3 @@ localRoot: 'data', //optional | ||
// Returns a list of paths on the FileSystem that are cached. | ||
}) | ||
}) | ||
``` | ||
@@ -68,5 +68,5 @@ | ||
// cache.add also returns if the cache is dirty. | ||
var dirty = cache.add(['photo3.jpg']) | ||
var dirty = cache.add(['photo3.jpg']) | ||
// Downloading files. | ||
// Downloading files. | ||
// The optional 'onprogress' event handler is enhanced with information | ||
@@ -82,9 +82,9 @@ // about the total download queue. | ||
var onprogress = function(e) { | ||
var progress ="Progress: " | ||
+ e.queueIndex // current download index | ||
+ " " | ||
var progress ="Progress: " | ||
+ e.queueIndex // current download index | ||
+ " " | ||
+ e.queueSize; // total files to download | ||
// Download files. | ||
cache.download(onprogress,includeFileProgress).then(function(cache){ ... },function(failedDownloads) { ... }) | ||
// Download files. | ||
cache.download(onprogress,includeFileProgress).then(function(cache){ ... },function(failedDownloads) { ... }) | ||
@@ -96,6 +96,6 @@ } | ||
```javascript | ||
// Get the cached internalURL of the file: "cdvfile://localhost/persisent/cache/photo3.jpg" | ||
// Get the cached internalURL of the file: "cdvfile://localhost/persisent/cache/photo3.jpg" | ||
cache.get('photo3.jpg'); | ||
cache.toInternalURL('photo3.jpg'); | ||
cache.toInternalURL('http://yourserver.com/photo3.jpg'); | ||
cache.toInternalURL('photo3.jpg'); | ||
cache.toInternalURL('http://yourserver.com/photo3.jpg'); | ||
@@ -128,6 +128,6 @@ // Get the file URL of the file: "file://.../photo3.jpg"; | ||
// Returns server URL to download, i.e. "http://yourserver.com/photo3.jpg"; | ||
cache.toServerURL('photo3.jpg'); | ||
cache.toServerURL('photo3.jpg'); | ||
// Needs a download? | ||
cache.isDirty(); | ||
cache.isDirty(); | ||
@@ -138,3 +138,3 @@ // Returns a list of server URLs that need to be downloaded. | ||
// Return a list of paths that are cached (i.e. ["/cache/photo3.jpg"]) | ||
cache.list().then(function(list){...},function(err){...}) | ||
cache.list().then(function(list){...},function(err){...}) | ||
@@ -145,2 +145,6 @@ ``` | ||
### 1.2.2 - bugfixes | ||
* When download is ready, fail if there are no errors instead of fail if cache is dirty (again) | ||
### 1.2.0 | ||
@@ -147,0 +151,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
112769
3327
228