Fix #105. Regression test for #105 had an error so I fixed that also. Updated refactoring-test example.

This commit is contained in:
Alan Kligman 2014-02-18 21:45:36 -05:00
parent 366c7b12d0
commit 49cec5df60
5 changed files with 128 additions and 39 deletions

147
dist/filer.js vendored
View File

@ -2977,9 +2977,11 @@ define('src/path',[],function() {
if (!path && !isAbsolute) { if (!path && !isAbsolute) {
path = '.'; path = '.';
} }
/*
if (path && trailingSlash) { if (path && trailingSlash) {
path += '/'; path += '/';
} }
*/
return (isAbsolute ? '/' : '') + path; return (isAbsolute ? '/' : '') + path;
} }
@ -3586,36 +3588,69 @@ define('src/providers/websql',['require','src/constants','src/constants','src/co
define('src/providers/memory',['require','src/constants'],function(require) { define('src/providers/memory',['require','src/constants'],function(require) {
var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME; var FILE_SYSTEM_NAME = require('src/constants').FILE_SYSTEM_NAME;
// Based on https://github.com/caolan/async/blob/master/lib/async.js
var nextTick = (function() {
if (typeof process === 'undefined' || !(process.nextTick)) {
if (typeof setImmediate === 'function') {
return function (fn) {
// not a direct alias for IE10 compatibility
setImmediate(fn);
};
} else {
return function (fn) {
setTimeout(fn, 0);
};
}
}
return process.nextTick;
}());
function asyncCallback(callback) {
nextTick(callback);
}
function MemoryContext(db, readOnly) { function MemoryContext(db, readOnly) {
this.readOnly = readOnly; this.readOnly = readOnly;
this.objectStore = db; this.objectStore = db;
} }
MemoryContext.prototype.clear = function(callback) { MemoryContext.prototype.clear = function(callback) {
if(this.readOnly) { if(this.readOnly) {
return callback("[MemoryContext] Error: write operation on read only context"); asyncCallback(function() {
callback("[MemoryContext] Error: write operation on read only context");
});
return;
} }
var objectStore = this.objectStore; var objectStore = this.objectStore;
Object.keys(objectStore).forEach(function(key){ Object.keys(objectStore).forEach(function(key){
delete objectStore[key]; delete objectStore[key];
}); });
callback(null); asyncCallback(callback);
}; };
MemoryContext.prototype.get = function(key, callback) { MemoryContext.prototype.get = function(key, callback) {
callback(null, this.objectStore[key]); var that = this;
asyncCallback(function() {
callback(null, that.objectStore[key]);
});
}; };
MemoryContext.prototype.put = function(key, value, callback) { MemoryContext.prototype.put = function(key, value, callback) {
if(this.readOnly) { if(this.readOnly) {
return callback("[MemoryContext] Error: write operation on read only context"); asyncCallback(function() {
callback("[MemoryContext] Error: write operation on read only context");
});
return;
} }
this.objectStore[key] = value; this.objectStore[key] = value;
callback(null); asyncCallback(callback);
}; };
MemoryContext.prototype.delete = function(key, callback) { MemoryContext.prototype.delete = function(key, callback) {
if(this.readOnly) { if(this.readOnly) {
return callback("[MemoryContext] Error: write operation on read only context"); asyncCallback(function() {
callback("[MemoryContext] Error: write operation on read only context");
});
return;
} }
delete this.objectStore[key]; delete this.objectStore[key];
callback(null); asyncCallback(callback);
}; };
@ -3628,7 +3663,9 @@ define('src/providers/memory',['require','src/constants'],function(require) {
}; };
Memory.prototype.open = function(callback) { Memory.prototype.open = function(callback) {
asyncCallback(function() {
callback(null, true); callback(null, true);
});
}; };
Memory.prototype.getReadOnlyContext = function() { Memory.prototype.getReadOnlyContext = function() {
return new MemoryContext(this.db, true); return new MemoryContext(this.db, true);
@ -5122,7 +5159,7 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
else if (!name) { else if (!name) {
callback(new EInvalid('attribute name cannot be an empty string')); callback(new EInvalid('attribute name cannot be an empty string'));
} }
else if (flag != null && else if (flag !== null &&
flag !== XATTR_CREATE && flag !== XATTR_REPLACE) { flag !== XATTR_CREATE && flag !== XATTR_REPLACE) {
callback(new EInvalid('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE')); callback(new EInvalid('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE'));
} }
@ -5139,7 +5176,7 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
else if (!name) { else if (!name) {
callback(new EInvalid('attribute name cannot be an empty string')); callback(new EInvalid('attribute name cannot be an empty string'));
} }
else if (flag != null && else if (flag !== null &&
flag !== XATTR_CREATE && flag !== XATTR_REPLACE) { flag !== XATTR_CREATE && flag !== XATTR_REPLACE) {
callback(new EInvalid('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE')); callback(new EInvalid('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE'));
} }
@ -5265,6 +5302,17 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
return O_FLAGS[flags]; return O_FLAGS[flags];
} }
function validate_file_options(options, enc, fileMode){
if(!options) {
options = { encoding: enc, flag: fileMode };
} else if(typeof options === "function") {
options = { encoding: enc, flag: fileMode };
} else if(typeof options === "string") {
options = { encoding: options, flag: fileMode };
}
return options;
}
// nullCheck from https://github.com/joyent/node/blob/master/lib/fs.js // nullCheck from https://github.com/joyent/node/blob/master/lib/fs.js
function nullCheck(path, callback) { function nullCheck(path, callback) {
if (('' + path).indexOf('\u0000') !== -1) { if (('' + path).indexOf('\u0000') !== -1) {
@ -5375,7 +5423,7 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
fs.readyState = FS_READY; fs.readyState = FS_READY;
runQueued(); runQueued();
} }
callback(error); callback(error, fs);
} }
if(err) { if(err) {
@ -5555,13 +5603,7 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
} }
function _readFile(fs, context, path, options, callback) { function _readFile(fs, context, path, options, callback) {
if(!options) { options = validate_file_options(options, null, 'r');
options = { encoding: null, flag: 'r' };
} else if(typeof options === "function") {
options = { encoding: null, flag: 'r' };
} else if(typeof options === "string") {
options = { encoding: options, flag: 'r' };
}
if(!nullCheck(path, callback)) return; if(!nullCheck(path, callback)) return;
@ -5601,7 +5643,6 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
callback(null, data); callback(null, data);
}); });
}); });
}); });
} }
@ -5631,13 +5672,7 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
} }
function _writeFile(fs, context, path, data, options, callback) { function _writeFile(fs, context, path, data, options, callback) {
if(!options) { options = validate_file_options(options, 'utf8', 'w');
options = { encoding: 'utf8', flag: 'w' };
} else if(typeof options === "function") {
options = { encoding: 'utf8', flag: 'w' };
} else if(typeof options === "string") {
options = { encoding: options, flag: 'w' };
}
if(!nullCheck(path, callback)) return; if(!nullCheck(path, callback)) return;
@ -5671,6 +5706,41 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
}); });
} }
function _appendFile(fs, context, path, data, options, callback) {
options = validate_file_options(options, 'utf8', 'a');
if(!nullCheck(path, callback)) return;
var flags = validate_flags(options.flag || 'a');
if(!flags) {
callback(new EInvalid('flags is not valid'));
}
data = data || '';
if(typeof data === "number") {
data = '' + data;
}
if(typeof data === "string" && options.encoding === 'utf8') {
data = new TextEncoder('utf-8').encode(data);
}
open_file(context, path, flags, function(err, fileNode) {
if(err) {
return callback(err);
}
var ofd = new OpenFileDescription(fileNode.id, flags, fileNode.size);
var fd = fs.allocDescriptor(ofd);
write_data(context, ofd, data, 0, data.length, ofd.position, function(err2, nbytes) {
if(err2) {
return callback(err2);
}
fs.releaseDescriptor(fd);
callback(null);
});
});
}
function _getxattr (context, path, name, callback) { function _getxattr (context, path, name, callback) {
if (!nullCheck(path, callback)) return; if (!nullCheck(path, callback)) return;
@ -5717,7 +5787,7 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
else { else {
callback(null); callback(null);
} }
}; }
setxattr_file(context, path, name, value, flag, check_result); setxattr_file(context, path, name, value, flag, check_result);
} }
@ -5862,7 +5932,7 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
callback(null); callback(null);
} }
} }
utimes_file(context, path, atime, mtime, check_result) utimes_file(context, path, atime, mtime, check_result);
} }
function _futimes(fs, context, fd, atime, mtime, callback) { function _futimes(fs, context, fd, atime, mtime, callback) {
@ -5875,7 +5945,7 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
} }
} }
var currentTime = Date.now() var currentTime = Date.now();
atime = (atime) ? atime : currentTime; atime = (atime) ? atime : currentTime;
mtime = (mtime) ? mtime : currentTime; mtime = (mtime) ? mtime : currentTime;
@ -6136,6 +6206,17 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
); );
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.appendFile = function(path, data, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
var fs = this;
var error = fs.queueOrRun(
function() {
var context = fs.provider.getReadWriteContext();
_appendFile(fs, context, path, data, options, callback);
}
);
if(error) callback(error);
};
FileSystem.prototype.lseek = function(fd, offset, whence, callback) { FileSystem.prototype.lseek = function(fd, offset, whence, callback) {
callback = maybeCallback(callback); callback = maybeCallback(callback);
var fs = this; var fs = this;
@ -6204,7 +6285,13 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.truncate = function(path, length, callback) { FileSystem.prototype.truncate = function(path, length, callback) {
// Follow node.js in allowing the `length` to be optional
if(typeof length === 'function') {
callback = length;
length = 0;
}
callback = maybeCallback(callback); callback = maybeCallback(callback);
length = typeof length === 'number' ? length : 0;
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -6254,7 +6341,7 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
} }
}; };
FileSystem.prototype.setxattr = function (path, name, value, flag, callback) { FileSystem.prototype.setxattr = function (path, name, value, flag, callback) {
var callback = maybeCallback(arguments[arguments.length - 1]); callback = maybeCallback(arguments[arguments.length - 1]);
var _flag = (typeof flag != 'function') ? flag : null; var _flag = (typeof flag != 'function') ? flag : null;
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
@ -6283,7 +6370,7 @@ define('src/fs',['require','nodash','encoding','src/path','src/path','src/path',
} }
}; };
FileSystem.prototype.fsetxattr = function (fd, name, value, flag, callback) { FileSystem.prototype.fsetxattr = function (fd, name, value, flag, callback) {
var callback = maybeCallback(arguments[arguments.length - 1]); callback = maybeCallback(arguments[arguments.length - 1]);
var _flag = (typeof flag != 'function') ? flag : null; var _flag = (typeof flag != 'function') ? flag : null;
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(

8
dist/filer.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -6,7 +6,7 @@
<body> <body>
<div id="stdout"></div> <div id="stdout"></div>
</body> </body>
<script src="../dist/idbfs.js"></script> <script src="../dist/filer.js"></script>
<script> <script>
window.Filer = Filer; window.Filer = Filer;
var fs = new Filer.FileSystem({ var fs = new Filer.FileSystem({

View File

@ -103,9 +103,11 @@ define(function() {
if (!path && !isAbsolute) { if (!path && !isAbsolute) {
path = '.'; path = '.';
} }
/*
if (path && trailingSlash) { if (path && trailingSlash) {
path += '/'; path += '/';
} }
*/
return (isAbsolute ? '/' : '') + path; return (isAbsolute ? '/' : '') + path;
} }

View File

@ -23,7 +23,7 @@ define(["Filer", "util"], function(Filer, util) {
fs.readdir('/tmp/', function(err, result2) { fs.readdir('/tmp/', function(err, result2) {
if(err) throw err; if(err) throw err;
expect(result2).to.exist; expect(result2).to.exist;
expect(result2[0]).to.equal('tmp'); expect(result2[0]).to.equal('foo');
expect(result1).to.deep.equal(result2); expect(result1).to.deep.equal(result2);
done(); done();
}); });