2018-06-28 23:26:08 +00:00
|
|
|
var Filer = require('../../src');
|
2015-03-30 14:30:50 +00:00
|
|
|
var IndexedDBTestProvider = require('./indexeddb.js');
|
|
|
|
var WebSQLTestProvider = require('./websql.js');
|
|
|
|
var MemoryTestProvider = require('./memory.js');
|
|
|
|
var Url = require('url');
|
|
|
|
|
|
|
|
var _provider;
|
|
|
|
var _fs;
|
|
|
|
|
|
|
|
function uniqueName() {
|
|
|
|
if(!uniqueName.seed) {
|
|
|
|
uniqueName.seed = Date.now();
|
|
|
|
}
|
|
|
|
return 'filer-testdb-' + uniqueName.seed++;
|
|
|
|
}
|
2014-01-21 21:25:09 +00:00
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
function findBestProvider() {
|
|
|
|
var providers = Filer.FileSystem.providers;
|
|
|
|
if(providers.IndexedDB.isSupported()) {
|
|
|
|
return IndexedDBTestProvider;
|
|
|
|
}
|
|
|
|
if(providers.WebSQL.isSupported()) {
|
|
|
|
return WebSQLTestProvider;
|
2014-01-21 21:25:09 +00:00
|
|
|
}
|
2015-03-30 14:30:50 +00:00
|
|
|
return MemoryTestProvider;
|
|
|
|
}
|
2014-01-21 21:25:09 +00:00
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
function getUrlParams() {
|
|
|
|
// Check if we are running in node
|
|
|
|
if(!global.location) {
|
|
|
|
return null;
|
2014-02-19 22:47:14 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
var url = Url.parse(global.location.href, true);
|
2014-01-21 21:25:09 +00:00
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
return url.query;
|
|
|
|
}
|
2014-01-21 21:25:09 +00:00
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
function getProviderType() {
|
2015-08-16 17:14:31 +00:00
|
|
|
var defaultProvider = 'default';
|
2015-03-30 14:30:50 +00:00
|
|
|
var queryString = getUrlParams();
|
2014-05-16 17:18:13 +00:00
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
// If the environment is node or the query string is empty,
|
|
|
|
// the memory provider will be used.
|
|
|
|
if(!queryString) {
|
|
|
|
return defaultProvider;
|
2014-01-21 21:25:09 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 17:14:31 +00:00
|
|
|
return queryString['filer-provider'] || defaultProvider;
|
2015-03-30 14:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setup(callback) {
|
|
|
|
// In browser we support specifying the provider via the query string
|
|
|
|
// (e.g., ?filer-provider=IndexedDB). If not specified, we use
|
|
|
|
// the Memory provider by default.
|
|
|
|
var providerType = getProviderType();
|
|
|
|
|
|
|
|
var name = uniqueName();
|
|
|
|
|
|
|
|
switch(providerType.toLowerCase()) {
|
2018-07-15 17:25:35 +00:00
|
|
|
case 'indexeddb':
|
|
|
|
_provider = new IndexedDBTestProvider(name);
|
|
|
|
break;
|
|
|
|
case 'websql':
|
|
|
|
_provider = new WebSQLTestProvider(name);
|
|
|
|
break;
|
|
|
|
case 'memory':
|
|
|
|
_provider = new MemoryTestProvider(name);
|
|
|
|
break;
|
|
|
|
case 'default':
|
|
|
|
default:
|
|
|
|
var BestProvider = findBestProvider();
|
|
|
|
_provider = new BestProvider(name);
|
|
|
|
break;
|
2014-01-21 21:25:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
// Allow passing FS flags on query string
|
|
|
|
var flags = global.filerArgs && global.filerArgs.flags ?
|
|
|
|
global.filerArgs.flags : 'FORMAT';
|
|
|
|
|
|
|
|
// Create a file system and wait for it to get setup
|
|
|
|
_provider.init();
|
|
|
|
|
|
|
|
function complete(err, fs) {
|
|
|
|
if(err) throw err;
|
|
|
|
_fs = fs;
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
return new Filer.FileSystem({
|
|
|
|
name: name,
|
|
|
|
provider: _provider.provider,
|
|
|
|
flags: flags
|
|
|
|
}, complete);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fs() {
|
|
|
|
if(!_fs) {
|
2018-11-29 00:24:03 +00:00
|
|
|
throw new Error('TestUtil: call setup() before fs()');
|
2014-01-21 21:25:09 +00:00
|
|
|
}
|
2015-03-30 14:30:50 +00:00
|
|
|
return _fs;
|
|
|
|
}
|
2014-01-21 21:25:09 +00:00
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
function provider() {
|
|
|
|
if(!_provider) {
|
2018-11-29 00:24:03 +00:00
|
|
|
throw new Error('TestUtil: call setup() before provider()');
|
2014-02-15 15:54:54 +00:00
|
|
|
}
|
2015-03-30 14:30:50 +00:00
|
|
|
return _provider;
|
|
|
|
}
|
2014-02-15 15:54:54 +00:00
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
function shell(options) {
|
|
|
|
var _fs = fs();
|
|
|
|
return new _fs.Shell(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup(callback) {
|
|
|
|
if(!_provider) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_provider.cleanup(function() {
|
|
|
|
_provider = null;
|
|
|
|
_fs = null;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function typedArrayEqual(a, b) {
|
|
|
|
if(!(a && b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(a.length !== b.length) {
|
|
|
|
return false;
|
2014-01-21 21:25:09 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
for(var i = 0; i < a.length; ++ i) {
|
|
|
|
if(a[i] !== b[i]) {
|
2014-02-19 17:53:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-05 03:36:06 +00:00
|
|
|
/**
|
|
|
|
* Parse JSON with serialized Buffers
|
|
|
|
*/
|
|
|
|
const parseBJSON = json =>
|
|
|
|
JSON.parse(json, (key, value) =>
|
|
|
|
value && value.type === 'Buffer' ?
|
|
|
|
Buffer.from(value.data) :
|
|
|
|
value
|
|
|
|
);
|
|
|
|
|
2015-03-30 14:30:50 +00:00
|
|
|
module.exports = {
|
|
|
|
uniqueName: uniqueName,
|
|
|
|
setup: setup,
|
|
|
|
fs: fs,
|
|
|
|
shell: shell,
|
|
|
|
provider: provider,
|
|
|
|
providers: {
|
|
|
|
IndexedDB: IndexedDBTestProvider,
|
|
|
|
WebSQL: WebSQLTestProvider,
|
|
|
|
Memory: MemoryTestProvider
|
|
|
|
},
|
|
|
|
cleanup: cleanup,
|
2018-12-05 03:36:06 +00:00
|
|
|
typedArrayEqual: typedArrayEqual,
|
|
|
|
parseBJSON
|
2015-03-30 14:30:50 +00:00
|
|
|
};
|