more lint

This commit is contained in:
Ryan Dahl 2010-12-01 17:43:30 -08:00
parent f22c248e4c
commit 09329cbb04
4 changed files with 229 additions and 195 deletions

View File

@ -9,12 +9,12 @@ var Timer = process.binding('timer').Timer;
var timer = new Timer(); var timer = new Timer();
timer.callback = function () { timer.callback = function() {
var sockets = Object.keys(activeWatchers); var sockets = Object.keys(activeWatchers);
for (var i = 0, l = sockets.length; i < l; i++) { for (var i = 0, l = sockets.length; i < l; i++) {
var socket = sockets[i]; var socket = sockets[i];
var s = parseInt(socket, 10); var s = parseInt(socket, 10);
channel.processFD(watchers[socket].read ? s : dns.SOCKET_BAD, channel.processFD(watchers[socket].read ? s : dns.SOCKET_BAD,
watchers[socket].write ? s : dns.SOCKET_BAD); watchers[socket].write ? s : dns.SOCKET_BAD);
} }
updateTimer(); updateTimer();
@ -29,27 +29,26 @@ function updateTimer() {
var max = 20000; var max = 20000;
var timeout = channel.timeout(max); var timeout = channel.timeout(max);
timer.start(timeout, 0); timer.start(timeout, 0);
}; }
var channel = new dns.Channel({SOCK_STATE_CB: function (socket, read, write) { var channel = new dns.Channel({SOCK_STATE_CB: function(socket, read, write) {
var watcher; var watcher;
if (socket in watchers) { if (socket in watchers) {
watcher = watchers[socket].watcher; watcher = watchers[socket].watcher;
} else { } else {
watcher = new IOWatcher(); watcher = new IOWatcher();
watchers[socket] = { read: read watchers[socket] = { read: read,
, write: write write: write,
, watcher: watcher watcher: watcher };
};
watcher.callback = function(read, write) { watcher.callback = function(read, write) {
channel.processFD(read ? socket : dns.SOCKET_BAD, channel.processFD(read ? socket : dns.SOCKET_BAD,
write ? socket : dns.SOCKET_BAD); write ? socket : dns.SOCKET_BAD);
updateTimer(); updateTimer();
}; };
}; }
watcher.stop(); watcher.stop();
@ -65,7 +64,7 @@ var channel = new dns.Channel({SOCK_STATE_CB: function (socket, read, write) {
updateTimer(); updateTimer();
}}); }});
exports.resolve = function (domain, type_, callback_) { exports.resolve = function(domain, type_, callback_) {
var type, callback; var type, callback;
if (typeof(type_) == 'string') { if (typeof(type_) == 'string') {
type = type_; type = type_;
@ -90,16 +89,16 @@ function familyToSym(family) {
family = (family === 6) ? dns.AF_INET6 : dns.AF_INET; family = (family === 6) ? dns.AF_INET6 : dns.AF_INET;
} }
return family; return family;
}; }
exports.getHostByName = function (domain, family/*=4*/, callback) { exports.getHostByName = function(domain, family/*=4*/, callback) {
if (typeof family === 'function') { callback = family; family = null; } if (typeof family === 'function') { callback = family; family = null; }
channel.getHostByName(domain, familyToSym(family), callback); channel.getHostByName(domain, familyToSym(family), callback);
}; };
exports.getHostByAddr = function (address, family/*=4*/, callback) { exports.getHostByAddr = function(address, family/*=4*/, callback) {
if (typeof family === 'function') { callback = family; family = null; } if (typeof family === 'function') { callback = family; family = null; }
channel.getHostByAddr(address, familyToSym(family), callback); channel.getHostByAddr(address, familyToSym(family), callback);
}; };
@ -107,7 +106,7 @@ exports.getHostByAddr = function (address, family/*=4*/, callback) {
// Easy DNS A/AAAA look up // Easy DNS A/AAAA look up
// lookup(domain, [family,] callback) // lookup(domain, [family,] callback)
exports.lookup = function (domain, family, callback) { exports.lookup = function(domain, family, callback) {
// parse arguments // parse arguments
if (arguments.length === 2) { if (arguments.length === 2) {
callback = family; callback = family;
@ -137,7 +136,7 @@ exports.lookup = function (domain, family, callback) {
if (/\w\.local\.?$/.test(domain)) { if (/\w\.local\.?$/.test(domain)) {
// ANNOYING: In the case of mDNS domains use NSS in the thread pool. // ANNOYING: In the case of mDNS domains use NSS in the thread pool.
// I wish c-ares had better support. // I wish c-ares had better support.
process.binding('net').getaddrinfo(domain, 4, function (err, domains4) { process.binding('net').getaddrinfo(domain, 4, function(err, domains4) {
callback(err, domains4[0], 4); callback(err, domains4[0], 4);
}); });
return; return;
@ -146,7 +145,7 @@ exports.lookup = function (domain, family, callback) {
if (family) { if (family) {
// resolve names for explicit address family // resolve names for explicit address family
var af = familyToSym(family); var af = familyToSym(family);
channel.getHostByName(domain, af, function (err, domains) { channel.getHostByName(domain, af, function(err, domains) {
if (!err && domains && domains.length) { if (!err && domains && domains.length) {
if (family !== net.isIP(domains[0])) { if (family !== net.isIP(domains[0])) {
callback(new Error('not found'), []); callback(new Error('not found'), []);
@ -157,16 +156,15 @@ exports.lookup = function (domain, family, callback) {
callback(err, []); callback(err, []);
} }
}); });
return return;
} }
// first resolve names for v4 and if that fails, try v6 // first resolve names for v4 and if that fails, try v6
channel.getHostByName(domain, dns.AF_INET, function (err, domains4) { channel.getHostByName(domain, dns.AF_INET, function(err, domains4) {
if (domains4 && domains4.length) { if (domains4 && domains4.length) {
callback(null, domains4[0], 4); callback(null, domains4[0], 4);
} else { } else {
channel.getHostByName(domain, dns.AF_INET6, channel.getHostByName(domain, dns.AF_INET6, function(err, domains6) {
function (err, domains6) {
if (domains6 && domains6.length) { if (domains6 && domains6.length) {
callback(null, domains6[0], 6); callback(null, domains6[0], 6);
} else { } else {
@ -178,53 +176,64 @@ exports.lookup = function (domain, family, callback) {
}; };
exports.resolve4 = function(domain, callback) { exports.resolve4 = function(domain, callback) {
channel.query(domain, dns.A, callback); channel.query(domain, dns.A, callback);
}; };
exports.resolve6 = function(domain, callback) {
exports.resolve6 = function(domain, callback) {
channel.query(domain, dns.AAAA, callback); channel.query(domain, dns.AAAA, callback);
}; };
exports.resolveMx = function(domain, callback) {
exports.resolveMx = function(domain, callback) {
channel.query(domain, dns.MX, callback); channel.query(domain, dns.MX, callback);
}; };
exports.resolveTxt = function(domain, callback) {
exports.resolveTxt = function(domain, callback) {
channel.query(domain, dns.TXT, callback); channel.query(domain, dns.TXT, callback);
}; };
exports.resolveSrv = function(domain, callback) {
exports.resolveSrv = function(domain, callback) {
channel.query(domain, dns.SRV, callback); channel.query(domain, dns.SRV, callback);
}; };
exports.reverse = function(domain, callback) {
exports.reverse = function(domain, callback) {
channel.query(domain, dns.PTR, callback); channel.query(domain, dns.PTR, callback);
}; };
exports.resolveNs = function(domain, callback) {
exports.resolveNs = function(domain, callback) {
channel.query(domain, dns.NS, callback); channel.query(domain, dns.NS, callback);
}; };
exports.resolveCname = function(domain, callback) { exports.resolveCname = function(domain, callback) {
channel.query(domain, dns.CNAME, callback); channel.query(domain, dns.CNAME, callback);
}; };
var resolveMap = { var resolveMap = { A: exports.resolve4,
'A' : exports.resolve4, AAAA: exports.resolve6,
'AAAA' : exports.resolve6, MX: exports.resolveMx,
'MX' : exports.resolveMx, TXT: exports.resolveTxt,
'TXT' : exports.resolveTxt, SRV: exports.resolveSrv,
'SRV' : exports.resolveSrv, PTR: exports.resolvePtr,
'PTR' : exports.resolvePtr, NS: exports.resolveNs,
'NS' : exports.resolveNs, CNAME: exports.resolveCname };
'CNAME' : exports.resolveCname
};
// ERROR CODES // ERROR CODES
exports.NODATA = dns.NODATA; exports.NODATA = dns.NODATA;
exports.FORMERR = dns.FORMERR; exports.FORMERR = dns.FORMERR;
exports.BADRESP = dns.BADRESP; exports.BADRESP = dns.BADRESP;
exports.NOTFOUND = dns.NOTFOUND; exports.NOTFOUND = dns.NOTFOUND;
exports.BADNAME = dns.BADNAME; exports.BADNAME = dns.BADNAME;
exports.TIMEOUT = dns.TIMEOUT; exports.TIMEOUT = dns.TIMEOUT;
exports.CONNREFUSED = dns.CONNREFUSED; exports.CONNREFUSED = dns.CONNREFUSED;
exports.NOMEM = dns.NOMEM; exports.NOMEM = dns.NOMEM;
exports.DESTRUCTION = dns.DESTRUCTION; exports.DESTRUCTION = dns.DESTRUCTION;
exports.NOTIMP = dns.NOTIMP;
exports.NOTIMP = dns.NOTIMP; exports.EREFUSED = dns.EREFUSED;
exports.EREFUSED = dns.EREFUSED; exports.SERVFAIL = dns.SERVFAIL;
exports.SERVFAIL = dns.SERVFAIL;

View File

@ -2,7 +2,7 @@ var EventEmitter = exports.EventEmitter = process.EventEmitter;
var isArray = Array.isArray; var isArray = Array.isArray;
EventEmitter.prototype.emit = function (type) { EventEmitter.prototype.emit = function(type) {
// If there is no 'error' event listener then throw. // If there is no 'error' event listener then throw.
if (type === 'error') { if (type === 'error') {
if (!this._events || !this._events.error || if (!this._events || !this._events.error ||
@ -56,7 +56,7 @@ EventEmitter.prototype.emit = function (type) {
// EventEmitter is defined in src/node_events.cc // EventEmitter is defined in src/node_events.cc
// EventEmitter.prototype.emit() is also defined there. // EventEmitter.prototype.emit() is also defined there.
EventEmitter.prototype.addListener = function (type, listener) { EventEmitter.prototype.addListener = function(type, listener) {
if ('function' !== typeof listener) { if ('function' !== typeof listener) {
throw new Error('addListener only takes instances of Function'); throw new Error('addListener only takes instances of Function');
} }
@ -65,7 +65,7 @@ EventEmitter.prototype.addListener = function (type, listener) {
// To avoid recursion in the case that type == "newListeners"! Before // To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners". // adding it to the listeners, first emit "newListeners".
this.emit("newListener", type, listener); this.emit('newListener', type, listener);
if (!this._events[type]) { if (!this._events[type]) {
// Optimize the case of one listener. Don't need the extra array object. // Optimize the case of one listener. Don't need the extra array object.
@ -83,15 +83,15 @@ EventEmitter.prototype.addListener = function (type, listener) {
EventEmitter.prototype.on = EventEmitter.prototype.addListener; EventEmitter.prototype.on = EventEmitter.prototype.addListener;
EventEmitter.prototype.once = function (type, listener) { EventEmitter.prototype.once = function(type, listener) {
var self = this; var self = this;
self.on(type, function g () { self.on(type, function g() {
self.removeListener(type, g); self.removeListener(type, g);
listener.apply(this, arguments); listener.apply(this, arguments);
}); });
}; };
EventEmitter.prototype.removeListener = function (type, listener) { EventEmitter.prototype.removeListener = function(type, listener) {
if ('function' !== typeof listener) { if ('function' !== typeof listener) {
throw new Error('removeListener only takes instances of Function'); throw new Error('removeListener only takes instances of Function');
} }
@ -114,13 +114,13 @@ EventEmitter.prototype.removeListener = function (type, listener) {
return this; return this;
}; };
EventEmitter.prototype.removeAllListeners = function (type) { EventEmitter.prototype.removeAllListeners = function(type) {
// does not use listeners(), so no side effect of creating _events[type] // does not use listeners(), so no side effect of creating _events[type]
if (type && this._events && this._events[type]) this._events[type] = null; if (type && this._events && this._events[type]) this._events[type] = null;
return this; return this;
}; };
EventEmitter.prototype.listeners = function (type) { EventEmitter.prototype.listeners = function(type) {
if (!this._events) this._events = {}; if (!this._events) this._events = {};
if (!this._events[type]) this._events[type] = []; if (!this._events[type]) this._events[type] = [];
if (!isArray(this._events[type])) { if (!isArray(this._events[type])) {

View File

@ -7,14 +7,14 @@ exports.FreeList = function(name, max, constructor) {
}; };
exports.FreeList.prototype.alloc = function () { exports.FreeList.prototype.alloc = function() {
//debug("alloc " + this.name + " " + this.list.length); //debug("alloc " + this.name + " " + this.list.length);
return this.list.length ? this.list.shift() return this.list.length ? this.list.shift() :
: this.constructor.apply(this, arguments); this.constructor.apply(this, arguments);
}; };
exports.FreeList.prototype.free = function (obj) { exports.FreeList.prototype.free = function(obj) {
//debug("free " + this.name + " " + this.list.length); //debug("free " + this.name + " " + this.list.length);
if (this.list.length < this.max) { if (this.list.length < this.max) {
this.list.push(obj); this.list.push(obj);

293
lib/fs.js
View File

@ -10,54 +10,57 @@ var kPoolSize = 40 * 1024;
fs.Stats = binding.Stats; fs.Stats = binding.Stats;
fs.Stats.prototype._checkModeProperty = function (property) { fs.Stats.prototype._checkModeProperty = function(property) {
return ((this.mode & constants.S_IFMT) === property); return ((this.mode & constants.S_IFMT) === property);
}; };
fs.Stats.prototype.isDirectory = function () { fs.Stats.prototype.isDirectory = function() {
return this._checkModeProperty(constants.S_IFDIR); return this._checkModeProperty(constants.S_IFDIR);
}; };
fs.Stats.prototype.isFile = function () { fs.Stats.prototype.isFile = function() {
return this._checkModeProperty(constants.S_IFREG); return this._checkModeProperty(constants.S_IFREG);
}; };
fs.Stats.prototype.isBlockDevice = function () { fs.Stats.prototype.isBlockDevice = function() {
return this._checkModeProperty(constants.S_IFBLK); return this._checkModeProperty(constants.S_IFBLK);
}; };
fs.Stats.prototype.isCharacterDevice = function () { fs.Stats.prototype.isCharacterDevice = function() {
return this._checkModeProperty(constants.S_IFCHR); return this._checkModeProperty(constants.S_IFCHR);
}; };
fs.Stats.prototype.isSymbolicLink = function () { fs.Stats.prototype.isSymbolicLink = function() {
return this._checkModeProperty(constants.S_IFLNK); return this._checkModeProperty(constants.S_IFLNK);
}; };
fs.Stats.prototype.isFIFO = function () { fs.Stats.prototype.isFIFO = function() {
return this._checkModeProperty(constants.S_IFIFO); return this._checkModeProperty(constants.S_IFIFO);
}; };
fs.Stats.prototype.isSocket = function () { fs.Stats.prototype.isSocket = function() {
return this._checkModeProperty(constants.S_IFSOCK); return this._checkModeProperty(constants.S_IFSOCK);
}; };
fs.readFile = function (path, encoding_) { fs.readFile = function(path, encoding_) {
var encoding = typeof(encoding_) === 'string' ? encoding_ : null; var encoding = typeof(encoding_) === 'string' ? encoding_ : null;
var callback = arguments[arguments.length-1]; var callback = arguments[arguments.length - 1];
if (typeof(callback) !== 'function') callback = noop; if (typeof(callback) !== 'function') callback = noop;
var readStream = fs.createReadStream(path); var readStream = fs.createReadStream(path);
var buffers = []; var buffers = [];
var nread = 0; var nread = 0;
readStream.on("data", function (chunk) {
readStream.on('data', function(chunk) {
buffers.push(chunk); buffers.push(chunk);
nread += chunk.length; nread += chunk.length;
}); });
readStream.on("error", function (er) {
readStream.on('error', function(er) {
callback(er); callback(er);
readStream.destroy(); readStream.destroy();
}); });
readStream.on("end", function () {
readStream.on('end', function() {
// copy all the buffers into one // copy all the buffers into one
var buffer; var buffer;
switch (buffers.length) { switch (buffers.length) {
@ -66,7 +69,7 @@ fs.readFile = function (path, encoding_) {
default: // concat together default: // concat together
buffer = new Buffer(nread); buffer = new Buffer(nread);
var n = 0; var n = 0;
buffers.forEach(function (b) { buffers.forEach(function(b) {
var l = b.length; var l = b.length;
b.copy(buffer, n, 0, l); b.copy(buffer, n, 0, l);
n += l; n += l;
@ -84,7 +87,7 @@ fs.readFile = function (path, encoding_) {
}); });
}; };
fs.readFileSync = function (path, encoding) { fs.readFileSync = function(path, encoding) {
var fd = fs.openSync(path, constants.O_RDONLY, 0666); var fd = fs.openSync(path, constants.O_RDONLY, 0666);
var buffer = new Buffer(4048); var buffer = new Buffer(4048);
var buffers = []; var buffers = [];
@ -107,13 +110,14 @@ fs.readFileSync = function (path, encoding) {
var offset = 0; var offset = 0;
var i; var i;
buffer = new Buffer(nread); buffer = new Buffer(nread);
buffers.forEach(function (i) { buffers.forEach(function(i) {
if (!i._bytesRead) return; if (!i._bytesRead) return;
i.copy(buffer,offset,0,i._bytesRead); i.copy(buffer, offset, 0, i._bytesRead);
offset += i._bytesRead; offset += i._bytesRead;
}); });
} else if (buffers.length) { } else if (buffers.length) {
//buffers has exactly 1 (possibly zero length) buffer, so this should be a shortcut // buffers has exactly 1 (possibly zero length) buffer, so this should
// be a shortcut
buffer = buffers[0].slice(0, buffers[0]._bytesRead); buffer = buffers[0].slice(0, buffers[0]._bytesRead);
} else { } else {
buffer = new Buffer(0); buffer = new Buffer(0);
@ -131,30 +135,43 @@ function stringToFlags(flag) {
return flag; return flag;
} }
switch (flag) { switch (flag) {
case "r": return constants.O_RDONLY; case 'r':
case "r+": return constants.O_RDWR; return constants.O_RDONLY;
case "w": return constants.O_CREAT | constants.O_TRUNC | constants.O_WRONLY;
case "w+": return constants.O_CREAT | constants.O_TRUNC | constants.O_RDWR; case 'r+':
case "a": return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY; return constants.O_RDWR;
case "a+": return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR;
default: throw new Error("Unknown file open flag: " + flag); case 'w':
return constants.O_CREAT | constants.O_TRUNC | constants.O_WRONLY;
case 'w+':
return constants.O_CREAT | constants.O_TRUNC | constants.O_RDWR;
case 'a':
return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY;
case 'a+':
return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR;
default:
throw new Error('Unknown file open flag: ' + flag);
} }
} }
function noop () {} function noop() {}
// Yes, the follow could be easily DRYed up but I provide the explicit // Yes, the follow could be easily DRYed up but I provide the explicit
// list to make the arguments clear. // list to make the arguments clear.
fs.close = function (fd, callback) { fs.close = function(fd, callback) {
binding.close(fd, callback || noop); binding.close(fd, callback || noop);
}; };
fs.closeSync = function (fd) { fs.closeSync = function(fd) {
return binding.close(fd); return binding.close(fd);
}; };
fs.open = function (path, flags, mode_, callback) { fs.open = function(path, flags, mode_, callback) {
var mode = (typeof(mode_) == 'number' ? mode_ : 0666); var mode = (typeof(mode_) == 'number' ? mode_ : 0666);
var callback_ = arguments[arguments.length - 1]; var callback_ = arguments[arguments.length - 1];
var callback = (typeof(callback_) == 'function' ? callback_ : null); var callback = (typeof(callback_) == 'function' ? callback_ : null);
@ -162,12 +179,12 @@ fs.open = function (path, flags, mode_, callback) {
binding.open(path, stringToFlags(flags), mode, callback || noop); binding.open(path, stringToFlags(flags), mode, callback || noop);
}; };
fs.openSync = function (path, flags, mode) { fs.openSync = function(path, flags, mode) {
if (mode === undefined) { mode = 0666; } if (mode === undefined) { mode = 0666; }
return binding.open(path, stringToFlags(flags), mode); return binding.open(path, stringToFlags(flags), mode);
}; };
fs.read = function (fd, buffer, offset, length, position, callback) { fs.read = function(fd, buffer, offset, length, position, callback) {
if (!Buffer.isBuffer(buffer)) { if (!Buffer.isBuffer(buffer)) {
// legacy string interface (fd, length, position, encoding, callback) // legacy string interface (fd, length, position, encoding, callback)
var cb = arguments[4], var cb = arguments[4],
@ -180,9 +197,7 @@ fs.read = function (fd, buffer, offset, length, position, callback) {
callback = function(err, bytesRead) { callback = function(err, bytesRead) {
if (!cb) return; if (!cb) return;
var str = (bytesRead > 0) var str = (bytesRead > 0) ? buffer.toString(encoding, 0, bytesRead) : '';
? buffer.toString(encoding, 0, bytesRead)
: '';
(cb)(err, str, bytesRead); (cb)(err, str, bytesRead);
}; };
@ -191,7 +206,7 @@ fs.read = function (fd, buffer, offset, length, position, callback) {
binding.read(fd, buffer, offset, length, position, callback || noop); binding.read(fd, buffer, offset, length, position, callback || noop);
}; };
fs.readSync = function (fd, buffer, offset, length, position) { fs.readSync = function(fd, buffer, offset, length, position) {
var legacy = false; var legacy = false;
if (!Buffer.isBuffer(buffer)) { if (!Buffer.isBuffer(buffer)) {
// legacy string interface (fd, length, position, encoding, callback) // legacy string interface (fd, length, position, encoding, callback)
@ -209,19 +224,17 @@ fs.readSync = function (fd, buffer, offset, length, position) {
return r; return r;
} }
var str = (r > 0) var str = (r > 0) ? buffer.toString(encoding, 0, r) : '';
? buffer.toString(encoding, 0, r)
: '';
return [str, r]; return [str, r];
}; };
fs.write = function (fd, buffer, offset, length, position, callback) { fs.write = function(fd, buffer, offset, length, position, callback) {
if (!Buffer.isBuffer(buffer)) { if (!Buffer.isBuffer(buffer)) {
// legacy string interface (fd, data, position, encoding, callback) // legacy string interface (fd, data, position, encoding, callback)
callback = arguments[4]; callback = arguments[4];
position = arguments[2]; position = arguments[2];
buffer = new Buffer(''+arguments[1], arguments[3]); buffer = new Buffer('' + arguments[1], arguments[3]);
offset = 0; offset = 0;
length = buffer.length; length = buffer.length;
} }
@ -238,145 +251,145 @@ fs.write = function (fd, buffer, offset, length, position, callback) {
binding.write(fd, buffer, offset, length, position, callback || noop); binding.write(fd, buffer, offset, length, position, callback || noop);
}; };
fs.writeSync = function (fd, buffer, offset, length, position) { fs.writeSync = function(fd, buffer, offset, length, position) {
if (!Buffer.isBuffer(buffer)) { if (!Buffer.isBuffer(buffer)) {
// legacy string interface (fd, data, position, encoding) // legacy string interface (fd, data, position, encoding)
position = arguments[2]; position = arguments[2];
buffer = new Buffer(''+arguments[1], arguments[3]); buffer = new Buffer('' + arguments[1], arguments[3]);
offset = 0; offset = 0;
length = buffer.length; length = buffer.length;
} }
if(!length) return 0; if (!length) return 0;
return binding.write(fd, buffer, offset, length, position); return binding.write(fd, buffer, offset, length, position);
}; };
fs.rename = function (oldPath, newPath, callback) { fs.rename = function(oldPath, newPath, callback) {
binding.rename(oldPath, newPath, callback || noop); binding.rename(oldPath, newPath, callback || noop);
}; };
fs.renameSync = function (oldPath, newPath) { fs.renameSync = function(oldPath, newPath) {
return binding.rename(oldPath, newPath); return binding.rename(oldPath, newPath);
}; };
fs.truncate = function (fd, len, callback) { fs.truncate = function(fd, len, callback) {
binding.truncate(fd, len, callback || noop); binding.truncate(fd, len, callback || noop);
}; };
fs.truncateSync = function (fd, len) { fs.truncateSync = function(fd, len) {
return binding.truncate(fd, len); return binding.truncate(fd, len);
}; };
fs.rmdir = function (path, callback) { fs.rmdir = function(path, callback) {
binding.rmdir(path, callback || noop); binding.rmdir(path, callback || noop);
}; };
fs.rmdirSync = function (path) { fs.rmdirSync = function(path) {
return binding.rmdir(path); return binding.rmdir(path);
}; };
fs.fdatasync = function (fd, callback) { fs.fdatasync = function(fd, callback) {
binding.fdatasync(fd, callback || noop); binding.fdatasync(fd, callback || noop);
}; };
fs.fdatasyncSync = function (fd) { fs.fdatasyncSync = function(fd) {
return binding.fdatasync(fd); return binding.fdatasync(fd);
}; };
fs.fsync = function (fd, callback) { fs.fsync = function(fd, callback) {
binding.fsync(fd, callback || noop); binding.fsync(fd, callback || noop);
}; };
fs.fsyncSync = function (fd) { fs.fsyncSync = function(fd) {
return binding.fsync(fd); return binding.fsync(fd);
}; };
fs.mkdir = function (path, mode, callback) { fs.mkdir = function(path, mode, callback) {
binding.mkdir(path, mode, callback || noop); binding.mkdir(path, mode, callback || noop);
}; };
fs.mkdirSync = function (path, mode) { fs.mkdirSync = function(path, mode) {
return binding.mkdir(path, mode); return binding.mkdir(path, mode);
}; };
fs.sendfile = function (outFd, inFd, inOffset, length, callback) { fs.sendfile = function(outFd, inFd, inOffset, length, callback) {
binding.sendfile(outFd, inFd, inOffset, length, callback || noop); binding.sendfile(outFd, inFd, inOffset, length, callback || noop);
}; };
fs.sendfileSync = function (outFd, inFd, inOffset, length) { fs.sendfileSync = function(outFd, inFd, inOffset, length) {
return binding.sendfile(outFd, inFd, inOffset, length); return binding.sendfile(outFd, inFd, inOffset, length);
}; };
fs.readdir = function (path, callback) { fs.readdir = function(path, callback) {
binding.readdir(path, callback || noop); binding.readdir(path, callback || noop);
}; };
fs.readdirSync = function (path) { fs.readdirSync = function(path) {
return binding.readdir(path); return binding.readdir(path);
}; };
fs.fstat = function (fd, callback) { fs.fstat = function(fd, callback) {
binding.fstat(fd, callback || noop); binding.fstat(fd, callback || noop);
}; };
fs.lstat = function (path, callback) { fs.lstat = function(path, callback) {
binding.lstat(path, callback || noop); binding.lstat(path, callback || noop);
}; };
fs.stat = function (path, callback) { fs.stat = function(path, callback) {
binding.stat(path, callback || noop); binding.stat(path, callback || noop);
}; };
fs.fstatSync = function (fd) { fs.fstatSync = function(fd) {
return binding.fstat(fd); return binding.fstat(fd);
}; };
fs.lstatSync = function (path) { fs.lstatSync = function(path) {
return binding.lstat(path); return binding.lstat(path);
}; };
fs.statSync = function (path) { fs.statSync = function(path) {
return binding.stat(path); return binding.stat(path);
}; };
fs.readlink = function (path, callback) { fs.readlink = function(path, callback) {
binding.readlink(path, callback || noop); binding.readlink(path, callback || noop);
}; };
fs.readlinkSync = function (path) { fs.readlinkSync = function(path) {
return binding.readlink(path); return binding.readlink(path);
}; };
fs.symlink = function (destination, path, callback) { fs.symlink = function(destination, path, callback) {
binding.symlink(destination, path, callback || noop); binding.symlink(destination, path, callback || noop);
}; };
fs.symlinkSync = function (destination, path) { fs.symlinkSync = function(destination, path) {
return binding.symlink(destination, path); return binding.symlink(destination, path);
}; };
fs.link = function (srcpath, dstpath, callback) { fs.link = function(srcpath, dstpath, callback) {
binding.link(srcpath, dstpath, callback || noop); binding.link(srcpath, dstpath, callback || noop);
}; };
fs.linkSync = function (srcpath, dstpath) { fs.linkSync = function(srcpath, dstpath) {
return binding.link(srcpath, dstpath); return binding.link(srcpath, dstpath);
}; };
fs.unlink = function (path, callback) { fs.unlink = function(path, callback) {
binding.unlink(path, callback || noop); binding.unlink(path, callback || noop);
}; };
fs.unlinkSync = function (path) { fs.unlinkSync = function(path) {
return binding.unlink(path); return binding.unlink(path);
}; };
fs.chmod = function (path, mode, callback) { fs.chmod = function(path, mode, callback) {
binding.chmod(path, mode, callback || noop); binding.chmod(path, mode, callback || noop);
}; };
fs.chmodSync = function (path, mode) { fs.chmodSync = function(path, mode) {
return binding.chmod(path, mode); return binding.chmod(path, mode);
}; };
@ -388,28 +401,28 @@ fs.chownSync = function(path, uid, gid) {
return binding.chown(path, uid, gid); return binding.chown(path, uid, gid);
}; };
function writeAll (fd, buffer, offset, length, callback) { function writeAll(fd, buffer, offset, length, callback) {
// write(fd, buffer, offset, length, position, callback) // write(fd, buffer, offset, length, position, callback)
fs.write(fd, buffer, offset, length, offset, function (writeErr, written) { fs.write(fd, buffer, offset, length, offset, function(writeErr, written) {
if (writeErr) { if (writeErr) {
fs.close(fd, function () { fs.close(fd, function() {
if (callback) callback(writeErr); if (callback) callback(writeErr);
}); });
} else { } else {
if (written === length) { if (written === length) {
fs.close(fd, callback); fs.close(fd, callback);
} else { } else {
writeAll(fd, buffer, offset+written, length-written, callback); writeAll(fd, buffer, offset + written, length - written, callback);
} }
} }
}); });
} }
fs.writeFile = function (path, data, encoding_, callback) { fs.writeFile = function(path, data, encoding_, callback) {
var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8'); var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8');
var callback_ = arguments[arguments.length - 1]; var callback_ = arguments[arguments.length - 1];
var callback = (typeof(callback_) == 'function' ? callback_ : null); var callback = (typeof(callback_) == 'function' ? callback_ : null);
fs.open(path, 'w', 0666, function (openErr, fd) { fs.open(path, 'w', 0666, function(openErr, fd) {
if (openErr) { if (openErr) {
if (callback) callback(openErr); if (callback) callback(openErr);
} else { } else {
@ -419,16 +432,16 @@ fs.writeFile = function (path, data, encoding_, callback) {
}); });
}; };
fs.writeFileSync = function (path, data, encoding) { fs.writeFileSync = function(path, data, encoding) {
var fd = fs.openSync(path, "w"); var fd = fs.openSync(path, 'w');
if (!Buffer.isBuffer(data)) { if (!Buffer.isBuffer(data)) {
data = new Buffer(data, encoding || "utf8") data = new Buffer(data, encoding || 'utf8');
} }
var written = 0; var written = 0;
var length = data.length; var length = data.length;
//writeSync(fd, buffer, offset, length, position) //writeSync(fd, buffer, offset, length, position)
while (written < length) { while (written < length) {
written += fs.writeSync(fd, data, written, length-written, written); written += fs.writeSync(fd, data, written, length - written, written);
} }
fs.closeSync(fd); fs.closeSync(fd);
}; };
@ -437,12 +450,12 @@ fs.writeFileSync = function (path, data, encoding) {
var statWatchers = {}; var statWatchers = {};
fs.watchFile = function (filename) { fs.watchFile = function(filename) {
var stat; var stat;
var options; var options;
var listener; var listener;
if ("object" == typeof arguments[1]) { if ('object' == typeof arguments[1]) {
options = arguments[1]; options = arguments[1];
listener = arguments[2]; listener = arguments[2];
} else { } else {
@ -460,11 +473,11 @@ fs.watchFile = function (filename) {
stat = statWatchers[filename]; stat = statWatchers[filename];
stat.start(filename, options.persistent, options.interval); stat.start(filename, options.persistent, options.interval);
} }
stat.addListener("change", listener); stat.addListener('change', listener);
return stat; return stat;
}; };
fs.unwatchFile = function (filename) { fs.unwatchFile = function(filename) {
var stat; var stat;
if (statWatchers[filename]) { if (statWatchers[filename]) {
stat = statWatchers[filename]; stat = statWatchers[filename];
@ -484,7 +497,7 @@ var normalizeArray = path.normalizeArray;
// See: http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html // See: http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
fs.realpathSync = realpathSync; fs.realpathSync = realpathSync;
fs.realpath = realpath; fs.realpath = realpath;
function realpathSync (p) { function realpathSync(p) {
if (p.charAt(0) !== '/') { if (p.charAt(0) !== '/') {
p = path.join(process.cwd(), p); p = path.join(process.cwd(), p);
} }
@ -496,26 +509,31 @@ function realpathSync (p) {
// values, and pushing non-link path bits onto the buffer. // values, and pushing non-link path bits onto the buffer.
// then return the buffer. // then return the buffer.
// NB: path.length changes. // NB: path.length changes.
for (var i = 0; i < p.length; i ++) { for (var i = 0; i < p.length; i++) {
// skip over empty path parts. // skip over empty path parts.
if (p[i] === '') continue; if (p[i] === '') continue;
var part = path.join.apply(path, buf.concat(p[i])); var part = path.join.apply(path, buf.concat(p[i]));
if (knownHard[part]) { if (knownHard[part]) {
buf.push( p[i] );
continue;
}
var stat = fs.lstatSync(part);
if (!stat.isSymbolicLink()) {
// not a symlink. easy.
knownHard[ part ] = true;
buf.push(p[i]); buf.push(p[i]);
continue; continue;
} }
var id = stat.dev.toString(32)+':'+stat.ino.toString(32);
var stat = fs.lstatSync(part);
if (!stat.isSymbolicLink()) {
// not a symlink. easy.
knownHard[part] = true;
buf.push(p[i]);
continue;
}
var id = stat.dev.toString(32) + ':' + stat.ino.toString(32);
if (!seenLinks[id]) { if (!seenLinks[id]) {
fs.statSync(part); fs.statSync(part);
seenLinks[id] = fs.readlinkSync(part); seenLinks[id] = fs.readlinkSync(part);
} }
var target = seenLinks[id]; var target = seenLinks[id];
if (target.charAt(0) === '/') { if (target.charAt(0) === '/') {
// absolute. Start over. // absolute. Start over.
@ -524,9 +542,10 @@ function realpathSync (p) {
i = -1; i = -1;
continue; continue;
} }
// not absolute. join and splice. // not absolute. join and splice.
if (i === 0 && p[i].charAt(0) === "/") { if (i === 0 && p[i].charAt(0) === '/') {
target = "/"+target; target = '/' + target;
} }
target = path.split(target); target = path.split(target);
Array.prototype.splice.apply(p, [i, 1].concat(target)); Array.prototype.splice.apply(p, [i, 1].concat(target));
@ -538,7 +557,7 @@ function realpathSync (p) {
} }
function realpath (p, cb) { function realpath(p, cb) {
if (p.charAt(0) !== '/') { if (p.charAt(0) !== '/') {
p = path.join(process.cwd(), p); p = path.join(process.cwd(), p);
} }
@ -552,37 +571,41 @@ function realpath (p, cb) {
// NB: path.length changes. // NB: path.length changes.
var i = -1; var i = -1;
var part; var part;
LOOP(); LOOP();
function LOOP () { function LOOP() {
i ++; i++;
if (!(i < p.length)) return exit(); if (!(i < p.length)) return exit();
// skip over empty path parts. // skip over empty path parts.
if (p[i] === '') return process.nextTick(LOOP); if (p[i] === '') return process.nextTick(LOOP);
part = path.join(buf.join('/')+'/'+p[i]); part = path.join(buf.join('/') + '/' + p[i]);
if (knownHard[part]) { if (knownHard[part]) {
buf.push( p[i] ); buf.push(p[i]);
return process.nextTick(LOOP); return process.nextTick(LOOP);
} }
return fs.lstat(part, gotStat); return fs.lstat(part, gotStat);
} }
function gotStat (er, stat) {
function gotStat(er, stat) {
if (er) return cb(er); if (er) return cb(er);
if (!stat.isSymbolicLink()) { if (!stat.isSymbolicLink()) {
// not a symlink. easy. // not a symlink. easy.
knownHard[ part ] = true; knownHard[part] = true;
buf.push(p[i]); buf.push(p[i]);
return process.nextTick(LOOP); return process.nextTick(LOOP);
} }
var id = stat.dev.toString(32)+':'+stat.ino.toString(32);
var id = stat.dev.toString(32) + ':' + stat.ino.toString(32);
if (seenLinks[id]) return gotTarget(null, seenLinks[id]); if (seenLinks[id]) return gotTarget(null, seenLinks[id]);
fs.stat(part, function (er) { fs.stat(part, function(er) {
if (er) return cb(er) if (er) return cb(er);
fs.readlink(part, function (er, target) { fs.readlink(part, function(er, target) {
gotTarget(er, seenLinks[id] = target); gotTarget(er, seenLinks[id] = target);
}); });
}) });
} }
function gotTarget (er, target) {
function gotTarget(er, target) {
if (er) return cb(er); if (er) return cb(er);
if (target.charAt(0) === '/') { if (target.charAt(0) === '/') {
// absolute. Start over. // absolute. Start over.
@ -592,8 +615,8 @@ function realpath (p, cb) {
return process.nextTick(LOOP); return process.nextTick(LOOP);
} }
// not absolute. join and splice. // not absolute. join and splice.
if (i === 0 && p[i].charAt(0) === "/") { if (i === 0 && p[i].charAt(0) === '/') {
target = "/"+target; target = '/' + target;
} }
target = path.split(target); target = path.split(target);
Array.prototype.splice.apply(p, [i, 1].concat(target)); Array.prototype.splice.apply(p, [i, 1].concat(target));
@ -602,13 +625,15 @@ function realpath (p, cb) {
buf = []; buf = [];
return process.nextTick(LOOP); return process.nextTick(LOOP);
} }
function exit () {
function exit() {
cb(null, path.join(buf.join('/') || '/')); cb(null, path.join(buf.join('/') || '/'));
} }
} }
var pool; var pool;
function allocNewPool () {
function allocNewPool() {
pool = new Buffer(kPoolSize); pool = new Buffer(kPoolSize);
pool.used = 0; pool.used = 0;
} }
@ -644,12 +669,12 @@ var ReadStream = fs.ReadStream = function(path, options) {
this[key] = options[key]; this[key] = options[key];
} }
if(this.encoding) this.setEncoding(this.encoding); if (this.encoding) this.setEncoding(this.encoding);
if (this.start !== undefined || this.end !== undefined) { if (this.start !== undefined || this.end !== undefined) {
if (this.start === undefined || this.end === undefined) { if (this.start === undefined || this.end === undefined) {
this.emit('error', this.emit('error', new Error('Both start and end are needed ' +
new Error('Both start and end are needed for range streaming.')); 'for range streaming.'));
} else if (this.start > this.end) { } else if (this.start > this.end) {
this.emit('error', new Error('start must be <= end')); this.emit('error', new Error('start must be <= end'));
} else { } else {
@ -677,13 +702,13 @@ util.inherits(ReadStream, Stream);
fs.FileReadStream = fs.ReadStream; // support the legacy name fs.FileReadStream = fs.ReadStream; // support the legacy name
ReadStream.prototype.setEncoding = function (encoding) { ReadStream.prototype.setEncoding = function(encoding) {
var StringDecoder = require("string_decoder").StringDecoder; // lazy load var StringDecoder = require('string_decoder').StringDecoder; // lazy load
this._decoder = new StringDecoder(encoding); this._decoder = new StringDecoder(encoding);
}; };
ReadStream.prototype._read = function () { ReadStream.prototype._read = function() {
var self = this; var self = this;
if (!self.readable || self.paused) return; if (!self.readable || self.paused) return;
@ -710,7 +735,7 @@ ReadStream.prototype._read = function () {
toRead = Math.min(this.end - this.pos + 1, toRead); toRead = Math.min(this.end - this.pos + 1, toRead);
} }
function afterRead (err, bytesRead) { function afterRead(err, bytesRead) {
if (err) { if (err) {
self.emit('error', err); self.emit('error', err);
self.readable = false; self.readable = false;
@ -723,7 +748,7 @@ ReadStream.prototype._read = function () {
return; return;
} }
var b = thisPool.slice(start, start+bytesRead); var b = thisPool.slice(start, start + bytesRead);
// Possible optimizition here? // Possible optimizition here?
// Reclaim some bytes if bytesRead < toRead? // Reclaim some bytes if bytesRead < toRead?
@ -751,7 +776,7 @@ ReadStream.prototype._read = function () {
}; };
ReadStream.prototype._emitData = function (d) { ReadStream.prototype._emitData = function(d) {
if (this._decoder) { if (this._decoder) {
var string = this._decoder.write(d); var string = this._decoder.write(d);
if (string.length) this.emit('data', string); if (string.length) this.emit('data', string);
@ -761,7 +786,7 @@ ReadStream.prototype._emitData = function (d) {
}; };
ReadStream.prototype.destroy = function (cb) { ReadStream.prototype.destroy = function(cb) {
var self = this; var self = this;
this.readable = false; this.readable = false;
@ -842,7 +867,7 @@ util.inherits(WriteStream, Stream);
fs.FileWriteStream = fs.WriteStream; // support the legacy name fs.FileWriteStream = fs.WriteStream; // support the legacy name
WriteStream.prototype.flush = function () { WriteStream.prototype.flush = function() {
if (this.busy) return; if (this.busy) return;
var self = this; var self = this;
@ -900,7 +925,7 @@ WriteStream.prototype.flush = function () {
method.apply(this, args); method.apply(this, args);
}; };
WriteStream.prototype.write = function (data) { WriteStream.prototype.write = function(data) {
if (!this.writable) { if (!this.writable) {
throw new Error('stream not writable'); throw new Error('stream not writable');
} }
@ -908,8 +933,8 @@ WriteStream.prototype.write = function (data) {
this.drainable = true; this.drainable = true;
var cb; var cb;
if (typeof(arguments[arguments.length-1]) == 'function') { if (typeof(arguments[arguments.length - 1]) == 'function') {
cb = arguments[arguments.length-1]; cb = arguments[arguments.length - 1];
} }
if (Buffer.isBuffer(data)) { if (Buffer.isBuffer(data)) {
@ -926,13 +951,13 @@ WriteStream.prototype.write = function (data) {
return false; return false;
}; };
WriteStream.prototype.end = function (cb) { WriteStream.prototype.end = function(cb) {
this.writable = false; this.writable = false;
this._queue.push([fs.close, cb]); this._queue.push([fs.close, cb]);
this.flush(); this.flush();
}; };
WriteStream.prototype.destroy = function (cb) { WriteStream.prototype.destroy = function(cb) {
var self = this; var self = this;
this.writable = false; this.writable = false;