Comparing version
91
index.js
@@ -15,2 +15,3 @@ module.exports = new (function() { | ||
var self = this; | ||
var mocked = false; | ||
@@ -107,2 +108,12 @@ function toArray(arr) | ||
/** | ||
* Sets the filesystem to a mocking in-memory filesystem. | ||
*/ | ||
this.mock = function() | ||
{ | ||
mocked = true; | ||
require('./mock')(); | ||
console.warn('Using in memory mock for filesystem now.'); | ||
}; | ||
this.open = function(path, flags, callback) | ||
@@ -150,2 +161,13 @@ { | ||
if (mocked) { | ||
var storage = getStorageForPath(path); | ||
if (storage) { | ||
callback(null, (storage.files[path]) ? true : false); | ||
} | ||
else { | ||
callback(new Error('Unable to find entry point for ' + path + '.')); | ||
} | ||
return; | ||
} | ||
this.open(path, 'r', function(error, file) { | ||
@@ -206,3 +228,3 @@ callback(null, (error) ? false : true); | ||
var options = { | ||
encoding: opts.encoding || null, | ||
encoding: opts.encoding || 'utf8', | ||
format: opts.format || 'text', | ||
@@ -212,2 +234,49 @@ flag: opts.flag || 'r' | ||
if (mocked) { | ||
var storage = getStorageForPath(filename); | ||
if (!storage) { | ||
callback(new Error('Unable to find entry point for ' + filename + '.')); | ||
return; | ||
} | ||
var file = storage.files[filename] || null; | ||
if (file) { | ||
switch(options.format) { | ||
case 'text': | ||
file = file.toString(); | ||
break; | ||
case 'binary': | ||
file = file.toString(); | ||
break; | ||
case 'dataURL': | ||
if (typeof file === 'string') { | ||
var buffer = new ArrayBuffer(file.length); | ||
for (var i = 0; i < file.length; i++) { | ||
buffer[i] = file.charCodeAt(i); | ||
} | ||
file = buffer.toDataURL(); | ||
} | ||
case 'buffer': | ||
if (typeof file === 'string') { | ||
var buffer = new ArrayBuffer(file.length); | ||
for (var i = 0; i < file.length; i++) { | ||
buffer[i] = file.charCodeAt(i); | ||
} | ||
file = buffer; | ||
} | ||
default: | ||
file = file.toString(); | ||
} | ||
} | ||
callback(null, file); | ||
return; | ||
} | ||
this.open(filename, options.flag, function(error, fd) { | ||
@@ -227,3 +296,3 @@ if (error) { | ||
reader.onerror = function() | ||
reader.onerror = function(error) | ||
{ | ||
@@ -337,2 +406,8 @@ callback(error); | ||
if (mocked) { | ||
storage.files[filename] = data; | ||
callback(null); | ||
return; | ||
} | ||
var file = (data instanceof Blob) ? data : new Blob([data], { type: options.mimetype }); | ||
@@ -363,2 +438,8 @@ var filepath = getPathWithoutStorageType(filename); | ||
} | ||
if (mocked) { | ||
callback(null, storage.readdir(path)); | ||
return; | ||
} | ||
var dirpath = getPathWithoutStorageType(path); | ||
@@ -398,2 +479,8 @@ var cursor = storage.enumerate(dirpath); | ||
if (mocked) { | ||
delete storage.files[path]; | ||
callback(null); | ||
return; | ||
} | ||
var request = storage.delete(filepath); | ||
@@ -400,0 +487,0 @@ request.onsuccess = function() |
{ | ||
"name": "ffos-fs", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "fs module for the Firefox OS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -115,2 +115,23 @@ ffos-fs | ||
-------------------- | ||
### Mocking the filesystem | ||
It's possible to mock the filesystem to an in-memory filesystem. | ||
```javascript | ||
fs.mock(); | ||
``` | ||
Currently there is no support for the Lower level functions in mock mode. | ||
You can however use: | ||
- fs.writeFile | ||
- fs.exists | ||
- fs.readFile | ||
- fs.readdir | ||
- fs.unlink | ||
If you need to prepopulate your mocked filesystem with files just use ```fs.writeFile``` to create some files. | ||
17203
21.1%6
20%440
29.03%137
18.1%