cordova-promise-fs
Advanced tools
Comparing version 0.13.0 to 1.0.0
@@ -46,3 +46,3 @@ var CordovaPromiseFS = | ||
/* 0 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ function(module, exports) { | ||
@@ -78,4 +78,27 @@ /** | ||
if(str[0] === '/') str = str.substr(1); | ||
if(!!str && str.indexOf('.') < 0 && str[str.length-1] !== '/') str += '/'; | ||
if(str === './') str = ''; | ||
var tokens = str.split('/'), last = tokens[0]; | ||
// check tokens for instances of .. and . | ||
for(var i=1;i < tokens.length;i++) { | ||
last = tokens[i]; | ||
if (tokens[i] === '..') { | ||
// remove the .. and the previous token | ||
tokens.splice(i-1,2); | ||
// rewind 'cursor' 2 tokens | ||
i = i - 2; | ||
} else if (tokens[i] === '.') { | ||
// remove the .. and the previous token | ||
tokens.splice(i,1); | ||
// rewind 'cursor' 1 token | ||
i--; | ||
} | ||
} | ||
str = tokens.join('/'); | ||
if(str === './') { | ||
str = ''; | ||
} else if(last && last.indexOf('.') < 0 && str[str.length - 1] != '/'){ | ||
str += '/'; | ||
} | ||
return str; | ||
@@ -96,3 +119,3 @@ } | ||
/* default options */ | ||
this.options = options = options || {}; | ||
options = options || {}; | ||
options.persistent = options.persistent !== undefined? options.persistent: true; | ||
@@ -152,3 +175,3 @@ options.storageSize = options.storageSize || 20*1024*1024; | ||
var type = options.persistent? 1: 0; | ||
if(typeof options.fileSystem === 'number'){ | ||
if(options.fileSystem){ | ||
type = options.fileSystem; | ||
@@ -161,11 +184,10 @@ } | ||
} | ||
// On chrome, request quota to store persistent files | ||
if (!isCordova && type === 1 && navigator.webkitPersistentStorage) { | ||
navigator.webkitPersistentStorage.requestQuota(options.storageSize, function(grantedBytes) { | ||
window.requestFileSystem(type, grantedBytes, resolve, reject); | ||
}); | ||
} | ||
else { | ||
window.requestFileSystem(type, options.storageSize, resolve, reject); | ||
} | ||
if(isNaN(type)) { | ||
window.resolveLocalFileSystemURL(type,function(directory){ | ||
resolve(directory.filesystem); | ||
},reject); | ||
}else{ | ||
window.requestFileSystem(type, options.storageSize, resolve, reject); | ||
} | ||
setTimeout(function(){ reject(new Error('Could not retrieve FileSystem after 5 seconds.')); },5100); | ||
@@ -242,20 +264,33 @@ },reject); | ||
var dirReader = dirEntry.createReader(); | ||
dirReader.readEntries(function(entries) { | ||
var promises = [ResolvedPromise(entries)]; | ||
if(recursive) { | ||
entries | ||
.filter(function(entry){return entry.isDirectory; }) | ||
.forEach(function(entry){ | ||
promises.push(list(entry.fullPath,'re')); | ||
}); | ||
var totalEntities=[]; | ||
var readDir = function(entries) { | ||
if(entries.length===0) { | ||
return ResolvedPromise(totalEntities).then(function(){resolve(totalEntities);}); | ||
} else { | ||
var promises = [ResolvedPromise(entries)]; | ||
if(recursive) { | ||
entries | ||
.filter(function(entry){return entry.isDirectory; }) | ||
.forEach(function(entry){ | ||
promises.push(list(entry.fullPath,'re')); | ||
}); | ||
} | ||
promises.push(new Promise(function(re,rej){ | ||
dirReader.readEntries(function(eents) { | ||
readDir(eents).then(function(){re(eents);},rej); | ||
}, reject); | ||
})); | ||
if(onlyFiles) entries = entries.filter(function(entry) { return entry.isFile; }); | ||
if(onlyDirs) entries = entries.filter(function(entry) { return entry.isDirectory; }); | ||
if(!getAsEntries) entries = entries.map(function(entry) { return entry.fullPath; }); | ||
totalEntities = totalEntities.concat.apply(totalEntities,entries); | ||
return Promise.all(promises); | ||
} | ||
Promise.all(promises).then(function(values){ | ||
var entries = []; | ||
entries = entries.concat.apply(entries,values); | ||
if(onlyFiles) entries = entries.filter(function(entry) { return entry.isFile; }); | ||
if(onlyDirs) entries = entries.filter(function(entry) { return entry.isDirectory; }); | ||
if(!getAsEntries) entries = entries.map(function(entry) { return entry.fullPath; }); | ||
resolve(entries); | ||
},reject); | ||
}, reject); | ||
}; | ||
dirReader.readEntries(readDir, reject); | ||
},reject); | ||
@@ -460,10 +495,10 @@ }); | ||
var args = transferQueue.pop(); | ||
var ft = args.shift(); | ||
var isDownload = args.shift(); | ||
var serverUrl = args.shift(); | ||
var localPath = args.shift(); | ||
var win = args.shift(); | ||
var fail = args.shift(); | ||
var trustAllHosts = args.shift(); | ||
var transferOptions = args.shift(); | ||
var ft = args.fileTransfer, | ||
isDownload = args.isDownload, | ||
serverUrl = args.serverUrl, | ||
localPath = args.localPath, | ||
trustAllHosts = args.trustAllHosts, | ||
transferOptions = args.transferOptions, | ||
win = args.win, | ||
fail = args.fail; | ||
@@ -514,3 +549,14 @@ if(ft._aborted) { | ||
} else { | ||
transferQueue.unshift([ft,isDownload,serverUrl,localPath,resolve,attempt,transferOptions.trustAllHosts || false,transferOptions]); | ||
var transferJob = { | ||
fileTransfer:ft, | ||
isDownload:isDownload, | ||
serverUrl:serverUrl, | ||
localPath:localPath, | ||
trustAllHosts:transferOptions.trustAllHosts || false, | ||
transferOptions:transferOptions, | ||
win:resolve, | ||
fail:attempt | ||
}; | ||
transferQueue.unshift(transferJob); | ||
var timeout = transferOptions.retry.shift(); | ||
@@ -517,0 +563,0 @@ if(timeout > 0) { |
124
index.js
@@ -30,4 +30,27 @@ /** | ||
if(str[0] === '/') str = str.substr(1); | ||
if(!!str && str.indexOf('.') < 0 && str[str.length-1] !== '/') str += '/'; | ||
if(str === './') str = ''; | ||
var tokens = str.split('/'), last = tokens[0]; | ||
// check tokens for instances of .. and . | ||
for(var i=1;i < tokens.length;i++) { | ||
last = tokens[i]; | ||
if (tokens[i] === '..') { | ||
// remove the .. and the previous token | ||
tokens.splice(i-1,2); | ||
// rewind 'cursor' 2 tokens | ||
i = i - 2; | ||
} else if (tokens[i] === '.') { | ||
// remove the .. and the previous token | ||
tokens.splice(i,1); | ||
// rewind 'cursor' 1 token | ||
i--; | ||
} | ||
} | ||
str = tokens.join('/'); | ||
if(str === './') { | ||
str = ''; | ||
} else if(last && last.indexOf('.') < 0 && str[str.length - 1] != '/'){ | ||
str += '/'; | ||
} | ||
return str; | ||
@@ -48,3 +71,3 @@ } | ||
/* default options */ | ||
this.options = options = options || {}; | ||
options = options || {}; | ||
options.persistent = options.persistent !== undefined? options.persistent: true; | ||
@@ -104,3 +127,3 @@ options.storageSize = options.storageSize || 20*1024*1024; | ||
var type = options.persistent? 1: 0; | ||
if(typeof options.fileSystem === 'number'){ | ||
if(options.fileSystem){ | ||
type = options.fileSystem; | ||
@@ -113,11 +136,10 @@ } | ||
} | ||
// On chrome, request quota to store persistent files | ||
if (!isCordova && type === 1 && navigator.webkitPersistentStorage) { | ||
navigator.webkitPersistentStorage.requestQuota(options.storageSize, function(grantedBytes) { | ||
window.requestFileSystem(type, grantedBytes, resolve, reject); | ||
}); | ||
} | ||
else { | ||
window.requestFileSystem(type, options.storageSize, resolve, reject); | ||
} | ||
if(isNaN(type)) { | ||
window.resolveLocalFileSystemURL(type,function(directory){ | ||
resolve(directory.filesystem); | ||
},reject); | ||
}else{ | ||
window.requestFileSystem(type, options.storageSize, resolve, reject); | ||
} | ||
setTimeout(function(){ reject(new Error('Could not retrieve FileSystem after 5 seconds.')); },5100); | ||
@@ -194,20 +216,33 @@ },reject); | ||
var dirReader = dirEntry.createReader(); | ||
dirReader.readEntries(function(entries) { | ||
var promises = [ResolvedPromise(entries)]; | ||
if(recursive) { | ||
entries | ||
.filter(function(entry){return entry.isDirectory; }) | ||
.forEach(function(entry){ | ||
promises.push(list(entry.fullPath,'re')); | ||
}); | ||
var totalEntities=[]; | ||
var readDir = function(entries) { | ||
if(entries.length===0) { | ||
return ResolvedPromise(totalEntities).then(function(){resolve(totalEntities);}); | ||
} else { | ||
var promises = [ResolvedPromise(entries)]; | ||
if(recursive) { | ||
entries | ||
.filter(function(entry){return entry.isDirectory; }) | ||
.forEach(function(entry){ | ||
promises.push(list(entry.fullPath,'re')); | ||
}); | ||
} | ||
promises.push(new Promise(function(re,rej){ | ||
dirReader.readEntries(function(eents) { | ||
readDir(eents).then(function(){re(eents);},rej); | ||
}, reject); | ||
})); | ||
if(onlyFiles) entries = entries.filter(function(entry) { return entry.isFile; }); | ||
if(onlyDirs) entries = entries.filter(function(entry) { return entry.isDirectory; }); | ||
if(!getAsEntries) entries = entries.map(function(entry) { return entry.fullPath; }); | ||
totalEntities = totalEntities.concat.apply(totalEntities,entries); | ||
return Promise.all(promises); | ||
} | ||
Promise.all(promises).then(function(values){ | ||
var entries = []; | ||
entries = entries.concat.apply(entries,values); | ||
if(onlyFiles) entries = entries.filter(function(entry) { return entry.isFile; }); | ||
if(onlyDirs) entries = entries.filter(function(entry) { return entry.isDirectory; }); | ||
if(!getAsEntries) entries = entries.map(function(entry) { return entry.fullPath; }); | ||
resolve(entries); | ||
},reject); | ||
}, reject); | ||
}; | ||
dirReader.readEntries(readDir, reject); | ||
},reject); | ||
@@ -412,10 +447,10 @@ }); | ||
var args = transferQueue.pop(); | ||
var ft = args.shift(); | ||
var isDownload = args.shift(); | ||
var serverUrl = args.shift(); | ||
var localPath = args.shift(); | ||
var win = args.shift(); | ||
var fail = args.shift(); | ||
var trustAllHosts = args.shift(); | ||
var transferOptions = args.shift(); | ||
var ft = args.fileTransfer, | ||
isDownload = args.isDownload, | ||
serverUrl = args.serverUrl, | ||
localPath = args.localPath, | ||
trustAllHosts = args.trustAllHosts, | ||
transferOptions = args.transferOptions, | ||
win = args.win, | ||
fail = args.fail; | ||
@@ -466,3 +501,14 @@ if(ft._aborted) { | ||
} else { | ||
transferQueue.unshift([ft,isDownload,serverUrl,localPath,resolve,attempt,transferOptions.trustAllHosts || false,transferOptions]); | ||
var transferJob = { | ||
fileTransfer:ft, | ||
isDownload:isDownload, | ||
serverUrl:serverUrl, | ||
localPath:localPath, | ||
trustAllHosts:transferOptions.trustAllHosts || false, | ||
transferOptions:transferOptions, | ||
win:resolve, | ||
fail:attempt | ||
}; | ||
transferQueue.unshift(transferJob); | ||
var timeout = transferOptions.retry.shift(); | ||
@@ -469,0 +515,0 @@ if(timeout > 0) { |
{ | ||
"name": "cordova-promise-fs", | ||
"version": "0.13.0", | ||
"version": "1.0.0", | ||
"description": "Cordova FileSystem convienence functions that return promises.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -77,2 +77,3 @@ cordova-promise-fs | ||
fs.move(src,dest) // move from src to dist. Ensures dest directory exists. | ||
fs.moveDir(src,dest) | ||
fs.copy(src,dest) // copy from src to dist. Ensures dest directory exists. | ||
@@ -123,2 +124,3 @@ fs.remove(src) // removes file. Resolves even if file was already removed. | ||
* `"./"` is converted to `""` | ||
* ".." and "." are resolved. | ||
@@ -143,2 +145,10 @@ This allows you to concatenate normalized paths, i.e. | ||
### 1.0.0 (07/02/2016) | ||
* Fixed bug in list function, thanks @sebastian-greco | ||
* Improved code readability of transfer, thanks @youjenli | ||
* There is no this, thanks @m0ppers | ||
* Accept string as FileSystem, thanks @dortzur | ||
* Normalize ".." in paths, thanks @starquake | ||
### 0.13.0 (16/09/2015) | ||
@@ -145,0 +155,0 @@ |
@@ -40,2 +40,4 @@ (function(){ | ||
assert.equal(fs.normalize('/dir/sub/sub/sub'), 'dir/sub/sub/sub/', 'subdirectories'); | ||
assert.equal(fs.normalize('/dir/sub/../sub'), 'dir/sub/', 'normalize ..'); | ||
assert.equal(fs.normalize('/dir/sub/./sub'), 'dir/sub/sub/', 'normalize .'); | ||
}); | ||
@@ -42,0 +44,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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
127453
3808
2
223