Comparing version 0.0.6 to 0.0.7
178
index.js
@@ -66,3 +66,3 @@ var mock = require('./mock'); | ||
{ | ||
self.open(fd.name, 'w', callback); | ||
self.open(fd.fullname || fd.name, 'w', callback); | ||
return; | ||
@@ -163,9 +163,3 @@ } | ||
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 + '.')); | ||
} | ||
existsMock(path, callback); | ||
return; | ||
@@ -179,2 +173,13 @@ } | ||
function existsMock(path, callback) | ||
{ | ||
var storage = getStorageForPath(path); | ||
if (storage) { | ||
callback(null, (storage.files[path]) ? true : false); | ||
} | ||
else { | ||
callback(new Error('Unable to find entry point for ' + path + '.')); | ||
} | ||
} | ||
this.read = function(fd, blob, offset, length, position, callback) | ||
@@ -236,45 +241,3 @@ { | ||
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; | ||
var data = null; | ||
if (file) { | ||
switch(options.format) { | ||
case 'text': | ||
data = file.data.toString(); | ||
break; | ||
case 'binary': | ||
data = file.data.toString(); | ||
break; | ||
case 'dataURL': | ||
if (typeof file === 'string') { | ||
var buffer = new ArrayBuffer(file.data.length); | ||
for (var i = 0; i < file.data.length; i++) { | ||
buffer[i] = file.data.charCodeAt(i); | ||
} | ||
data = buffer.toDataURL(); | ||
} | ||
case 'buffer': | ||
if (typeof file === 'string') { | ||
var buffer = new ArrayBuffer(file.data.length); | ||
for (var i = 0; i < file.data.length; i++) { | ||
buffer[i] = file.data.charCodeAt(i); | ||
} | ||
data = buffer; | ||
} | ||
default: | ||
data = file.data.toString(); | ||
} | ||
} | ||
callback(null, data); | ||
readFileMock(filename, options, callback); | ||
return; | ||
@@ -329,2 +292,39 @@ } | ||
function readFileMock(filename, options, callback) | ||
{ | ||
var storage = getStorageForPath(filename); | ||
if (!storage) { | ||
callback(new Error('Unable to find entry point for ' + filename + '.')); | ||
return; | ||
} | ||
var file = storage.files[filename] || null; | ||
var data = null; | ||
if (file) { | ||
switch(options.format) { | ||
case 'text': | ||
data = file.toText(); | ||
break; | ||
case 'binary': | ||
data = file.toBinaryString(); | ||
break; | ||
case 'dataURL': | ||
data = file.toDataURL(); | ||
break; | ||
case 'buffer': | ||
data = file.toArrayBuffer(); | ||
break; | ||
default: | ||
data = file.toText(); | ||
} | ||
} | ||
callback(null, data); | ||
} | ||
this.write = function(fd, buffer, offset, length, position, callback) | ||
@@ -340,3 +340,4 @@ { | ||
} | ||
if (!buffer || !(buffer instanceof ArrayBuffer) || typeof buffer !== 'string') { | ||
if (!buffer || (!(buffer instanceof ArrayBuffer) && typeof buffer !== 'string')) { | ||
throw new Error('Missing or invalid Buffer.'); | ||
@@ -353,4 +354,4 @@ return; | ||
var offset = offset || 0; | ||
var length = length || buffer.lenght; | ||
if (length > buffer.lenght) { | ||
var length = length || buffer.length; | ||
if (length > buffer.length) { | ||
length = buffer.length; | ||
@@ -402,2 +403,7 @@ } | ||
if (mocked) { | ||
writeFileMock(filename, data, options, callback); | ||
return; | ||
} | ||
var storage = getStorageForPath(filename); | ||
@@ -410,23 +416,55 @@ | ||
if (mocked) { | ||
storage.files[filename] = new mock.FileMock(filename, data); | ||
callback(null); | ||
return; | ||
// get existing file for writing | ||
if (exists) { | ||
self.open(filename, options, function(err, fd) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
// TODO: convert blob data to ArrayBuffer here? | ||
var buffer = data; | ||
fd.fullname = filename; | ||
self.write(fd, buffer, 0, buffer.length, 0, function(err) { | ||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
callback(null); | ||
}); | ||
}); | ||
} | ||
// create new file | ||
else { | ||
var file = (data instanceof Blob) ? data : new Blob([data], { type: options.mimetype }); | ||
var filepath = getPathWithoutStorageType(filename); | ||
var file = (data instanceof Blob) ? data : new Blob([data], { type: options.mimetype }); | ||
var filepath = getPathWithoutStorageType(filename); | ||
var request = storage.addNamed(file, filepath); | ||
request.onsuccess = function() | ||
{ | ||
callback(null); | ||
}; | ||
request.onerror = function() | ||
{ | ||
callback(this.error); | ||
}; | ||
var request = storage.addNamed(file, filepath); | ||
request.onsuccess = function() | ||
{ | ||
callback(null); | ||
}; | ||
request.onerror = function() | ||
{ | ||
callback(this.error); | ||
}; | ||
} | ||
}); | ||
}; | ||
function writeFileMock(filename, data, options, callback) | ||
{ | ||
var storage = getStorageForPath(filename); | ||
if (!storage) { | ||
callback(new Error('Unable to find entry point for ' + filename + '.')); | ||
return; | ||
} | ||
storage.files[filename] = new mock.FileMock(filename, options.mimetype, data); | ||
callback(null); | ||
} | ||
this.readdir = function(path, callback) | ||
@@ -433,0 +471,0 @@ { |
41
mock.js
"use strict"; | ||
var FileMock = function(filename, data) | ||
var FileMock = function(filename, type, data) | ||
{ | ||
this.name = filename; | ||
var self = this; | ||
this.name = filename.split(':', 2)[1]; | ||
this.lastModifiedDate = new Date(); | ||
this.size = 0; | ||
this.type = 'text/unkown'; | ||
this.type = 'text/plain'; | ||
this.data = data; | ||
this.blob = new Blob(Array.prototype.slice.call(data), { | ||
this.blob = new Blob([data], { | ||
type: this.type | ||
}); | ||
this.buffer = getArrayBuffer(); | ||
this.slice = this.blob.slice.bind(this.blob); | ||
this.toText = function() | ||
{ | ||
return this.data.toString(); | ||
}; | ||
this.toBinaryString = function() | ||
{ | ||
return this.data.toString(); | ||
}; | ||
this.toDataURL = function() | ||
{ | ||
return this.data.toString(); | ||
}; | ||
this.toArrayBuffer = function() | ||
{ | ||
return this.buffer; | ||
}; | ||
function getArrayBuffer() | ||
{ | ||
var buffer = new ArrayBuffer(self.data.length); | ||
for(var i = 0; i < self.data.length; i++) { | ||
buffer[i] = self.data[i]; | ||
} | ||
return buffer; | ||
} | ||
}; | ||
@@ -15,0 +48,0 @@ |
{ | ||
"name": "ffos-fs", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "fs module for the Firefox OS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
18834
511