Switch to node.js' callback pattrn, update public API to match node.js

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2013-11-27 10:34:19 -05:00
parent b7733a591d
commit d3a7750d75
3 changed files with 88 additions and 19 deletions

51
dist/idbfs.js vendored
View File

@ -8509,6 +8509,19 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
return true; return true;
} }
// node.js supports a calling pattern that leaves off a callback.
function maybeCallback(callback) {
if(typeof callback === "function") {
return callback;
}
return function(err) {
if(err) {
throw err;
}
};
}
/* /*
* FileSystem * FileSystem
* *
@ -8776,7 +8789,6 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(!options) { if(!options) {
options = { encoding: null, flag: 'r' }; options = { encoding: null, flag: 'r' };
} else if(typeof options === "function") { } else if(typeof options === "function") {
callback = options;
options = { encoding: null, flag: 'r' }; options = { encoding: null, flag: 'r' };
} else if(typeof options === "string") { } else if(typeof options === "string") {
options = { encoding: options, flag: 'r' }; options = { encoding: options, flag: 'r' };
@ -8853,7 +8865,6 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(!options) { if(!options) {
options = { encoding: 'utf8', flag: 'w' }; options = { encoding: 'utf8', flag: 'w' };
} else if(typeof options === "function") { } else if(typeof options === "function") {
callback = options;
options = { encoding: 'utf8', flag: 'w' }; options = { encoding: 'utf8', flag: 'w' };
} else if(typeof options === "string") { } else if(typeof options === "string") {
options = { encoding: options, flag: 'w' }; options = { encoding: options, flag: 'w' };
@ -9055,7 +9066,10 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
* Public API for FileSystem * Public API for FileSystem
*/ */
FileSystem.prototype.open = function(path, flags, callback) { FileSystem.prototype.open = function(path, flags, mode, callback) {
// We support the same signature as node with a `mode` arg, but
// ignore it. Find the callback.
callback = maybeCallback(arguments[arguments.length - 1]);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9066,13 +9080,14 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.close = function(fd, callback) { FileSystem.prototype.close = function(fd, callback) {
_close(this, fd, callback); _close(this, fd, maybeCallback(callback));
}; };
FileSystem.prototype.mkdir = function(path, mode, callback) { FileSystem.prototype.mkdir = function(path, mode, callback) {
// Support passing a mode arg, but we ignore it internally for now. // Support passing a mode arg, but we ignore it internally for now.
if(typeof mode === 'function') { if(typeof mode === 'function') {
callback = mode; callback = mode;
} }
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9083,6 +9098,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.rmdir = function(path, callback) { FileSystem.prototype.rmdir = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9093,6 +9109,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.stat = function(path, callback) { FileSystem.prototype.stat = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9103,6 +9120,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.fstat = function(fd, callback) { FileSystem.prototype.fstat = function(fd, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9113,6 +9131,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.link = function(oldpath, newpath, callback) { FileSystem.prototype.link = function(oldpath, newpath, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9123,6 +9142,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.unlink = function(path, callback) { FileSystem.prototype.unlink = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9133,16 +9153,22 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.read = function(fd, buffer, offset, length, position, callback) { FileSystem.prototype.read = function(fd, buffer, offset, length, position, callback) {
// Follow how node.js does this
function wrapper(err, bytesRead) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
callback && callback(err, bytesRead || 0, buffer);
}
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
var context = fs.provider.getReadWriteContext(); var context = fs.provider.getReadWriteContext();
_read(fs, context, fd, buffer, offset, length, position, callback); _read(fs, context, fd, buffer, offset, length, position, wrapper);
} }
); );
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.readFile = function(path, options, callback) { FileSystem.prototype.readFile = function(path, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9153,6 +9179,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.write = function(fd, buffer, offset, length, position, callback) { FileSystem.prototype.write = function(fd, buffer, offset, length, position, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9163,7 +9190,8 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.writeFile = function(path, data, options, callback) { FileSystem.prototype.writeFile = function(path, data, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9174,6 +9202,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.lseek = function(fd, offset, whence, callback) { FileSystem.prototype.lseek = function(fd, offset, whence, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9184,6 +9213,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.readdir = function(path, callback) { FileSystem.prototype.readdir = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9194,6 +9224,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.rename = function(oldpath, newpath, callback) { FileSystem.prototype.rename = function(oldpath, newpath, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9204,6 +9235,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.readlink = function(path, callback) { FileSystem.prototype.readlink = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9213,7 +9245,9 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
); );
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.symlink = function(srcpath, dstpath, callback) { FileSystem.prototype.symlink = function(srcpath, dstpath, type, callback_) {
// Follow node.js in allowing the `type` arg to be passed, but we ignore it.
var callback = maybeCallback(arguments[arguments.length - 1]);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -9224,6 +9258,7 @@ define('src/fs',['require','lodash','encoding-indexes','encoding','src/path','sr
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.lstat = function(path, callback) { FileSystem.prototype.lstat = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {

View File

@ -960,6 +960,19 @@ define(function(require) {
return true; return true;
} }
// node.js supports a calling pattern that leaves off a callback.
function maybeCallback(callback) {
if(typeof callback === "function") {
return callback;
}
return function(err) {
if(err) {
throw err;
}
};
}
/* /*
* FileSystem * FileSystem
* *
@ -1227,7 +1240,6 @@ define(function(require) {
if(!options) { if(!options) {
options = { encoding: null, flag: 'r' }; options = { encoding: null, flag: 'r' };
} else if(typeof options === "function") { } else if(typeof options === "function") {
callback = options;
options = { encoding: null, flag: 'r' }; options = { encoding: null, flag: 'r' };
} else if(typeof options === "string") { } else if(typeof options === "string") {
options = { encoding: options, flag: 'r' }; options = { encoding: options, flag: 'r' };
@ -1304,7 +1316,6 @@ define(function(require) {
if(!options) { if(!options) {
options = { encoding: 'utf8', flag: 'w' }; options = { encoding: 'utf8', flag: 'w' };
} else if(typeof options === "function") { } else if(typeof options === "function") {
callback = options;
options = { encoding: 'utf8', flag: 'w' }; options = { encoding: 'utf8', flag: 'w' };
} else if(typeof options === "string") { } else if(typeof options === "string") {
options = { encoding: options, flag: 'w' }; options = { encoding: options, flag: 'w' };
@ -1506,7 +1517,10 @@ define(function(require) {
* Public API for FileSystem * Public API for FileSystem
*/ */
FileSystem.prototype.open = function(path, flags, callback) { FileSystem.prototype.open = function(path, flags, mode, callback) {
// We support the same signature as node with a `mode` arg, but
// ignore it. Find the callback.
callback = maybeCallback(arguments[arguments.length - 1]);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1517,13 +1531,14 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.close = function(fd, callback) { FileSystem.prototype.close = function(fd, callback) {
_close(this, fd, callback); _close(this, fd, maybeCallback(callback));
}; };
FileSystem.prototype.mkdir = function(path, mode, callback) { FileSystem.prototype.mkdir = function(path, mode, callback) {
// Support passing a mode arg, but we ignore it internally for now. // Support passing a mode arg, but we ignore it internally for now.
if(typeof mode === 'function') { if(typeof mode === 'function') {
callback = mode; callback = mode;
} }
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1534,6 +1549,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.rmdir = function(path, callback) { FileSystem.prototype.rmdir = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1544,6 +1560,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.stat = function(path, callback) { FileSystem.prototype.stat = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1554,6 +1571,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.fstat = function(fd, callback) { FileSystem.prototype.fstat = function(fd, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1564,6 +1582,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.link = function(oldpath, newpath, callback) { FileSystem.prototype.link = function(oldpath, newpath, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1574,6 +1593,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.unlink = function(path, callback) { FileSystem.prototype.unlink = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1584,16 +1604,23 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.read = function(fd, buffer, offset, length, position, callback) { FileSystem.prototype.read = function(fd, buffer, offset, length, position, callback) {
// Follow how node.js does this
callback = maybeCallback(callback);
function wrapper(err, bytesRead) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
callback(err, bytesRead || 0, buffer);
}
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
var context = fs.provider.getReadWriteContext(); var context = fs.provider.getReadWriteContext();
_read(fs, context, fd, buffer, offset, length, position, callback); _read(fs, context, fd, buffer, offset, length, position, wrapper);
} }
); );
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.readFile = function(path, options, callback) { FileSystem.prototype.readFile = function(path, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1604,6 +1631,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.write = function(fd, buffer, offset, length, position, callback) { FileSystem.prototype.write = function(fd, buffer, offset, length, position, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1614,7 +1642,8 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.writeFile = function(path, data, options, callback) { FileSystem.prototype.writeFile = function(path, data, options, callback_) {
var callback = maybeCallback(arguments[arguments.length - 1]);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1625,6 +1654,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.lseek = function(fd, offset, whence, callback) { FileSystem.prototype.lseek = function(fd, offset, whence, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1635,6 +1665,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.readdir = function(path, callback) { FileSystem.prototype.readdir = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1645,6 +1676,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.rename = function(oldpath, newpath, callback) { FileSystem.prototype.rename = function(oldpath, newpath, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1655,6 +1687,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.readlink = function(path, callback) { FileSystem.prototype.readlink = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1664,7 +1697,9 @@ define(function(require) {
); );
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.symlink = function(srcpath, dstpath, callback) { FileSystem.prototype.symlink = function(srcpath, dstpath, type, callback_) {
// Follow node.js in allowing the `type` arg to be passed, but we ignore it.
var callback = maybeCallback(arguments[arguments.length - 1]);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {
@ -1675,6 +1710,7 @@ define(function(require) {
if(error) callback(error); if(error) callback(error);
}; };
FileSystem.prototype.lstat = function(path, callback) { FileSystem.prototype.lstat = function(path, callback) {
callback = maybeCallback(callback);
var fs = this; var fs = this;
var error = fs.queueOrRun( var error = fs.queueOrRun(
function() { function() {

View File

@ -108,9 +108,7 @@ describe("node.js tests from https://github.com/joyent/node/blob/master/test", f
check(fs.symlink, 'foo\u0000bar', 'foobar'); check(fs.symlink, 'foo\u0000bar', 'foobar');
check(fs.symlink, 'foobar', 'foo\u0000bar'); check(fs.symlink, 'foobar', 'foo\u0000bar');
check(fs.unlink, 'foo\u0000bar'); check(fs.unlink, 'foo\u0000bar');
// NOTE: the node.js test doesn't pass anything for the data arg check(fs.writeFile, 'foo\u0000bar');
// but this just seems wrong based on their docs. Here I use one.
check(fs.writeFile, 'foo\u0000bar', 'foobar');
// TODO - need to be implemented still... // TODO - need to be implemented still...
// check(fs.appendFile, 'foo\u0000bar'); // check(fs.appendFile, 'foo\u0000bar');
// check(fs.realpath, 'foo\u0000bar'); // check(fs.realpath, 'foo\u0000bar');