diff --git a/lib/dns.js b/lib/dns.js index 66bb6ca23b..8d7eef2a64 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -9,12 +9,12 @@ var Timer = process.binding('timer').Timer; var timer = new Timer(); -timer.callback = function () { +timer.callback = function() { var sockets = Object.keys(activeWatchers); for (var i = 0, l = sockets.length; i < l; i++) { var socket = sockets[i]; 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); } updateTimer(); @@ -29,27 +29,26 @@ function updateTimer() { var max = 20000; var timeout = channel.timeout(max); 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; if (socket in watchers) { watcher = watchers[socket].watcher; } else { watcher = new IOWatcher(); - watchers[socket] = { read: read - , write: write - , watcher: watcher - }; + watchers[socket] = { read: read, + write: write, + watcher: watcher }; watcher.callback = function(read, write) { - channel.processFD(read ? socket : dns.SOCKET_BAD, + channel.processFD(read ? socket : dns.SOCKET_BAD, write ? socket : dns.SOCKET_BAD); updateTimer(); }; - }; + } watcher.stop(); @@ -65,7 +64,7 @@ var channel = new dns.Channel({SOCK_STATE_CB: function (socket, read, write) { updateTimer(); }}); -exports.resolve = function (domain, type_, callback_) { +exports.resolve = function(domain, type_, callback_) { var type, callback; if (typeof(type_) == 'string') { type = type_; @@ -90,16 +89,16 @@ function familyToSym(family) { family = (family === 6) ? dns.AF_INET6 : dns.AF_INET; } return family; -}; +} -exports.getHostByName = function (domain, family/*=4*/, callback) { +exports.getHostByName = function(domain, family/*=4*/, callback) { if (typeof family === 'function') { callback = family; family = null; } 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; } channel.getHostByAddr(address, familyToSym(family), callback); }; @@ -107,7 +106,7 @@ exports.getHostByAddr = function (address, family/*=4*/, callback) { // Easy DNS A/AAAA look up // lookup(domain, [family,] callback) -exports.lookup = function (domain, family, callback) { +exports.lookup = function(domain, family, callback) { // parse arguments if (arguments.length === 2) { callback = family; @@ -137,7 +136,7 @@ exports.lookup = function (domain, family, callback) { if (/\w\.local\.?$/.test(domain)) { // ANNOYING: In the case of mDNS domains use NSS in the thread pool. // 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); }); return; @@ -146,7 +145,7 @@ exports.lookup = function (domain, family, callback) { if (family) { // resolve names for explicit address 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 (family !== net.isIP(domains[0])) { callback(new Error('not found'), []); @@ -157,16 +156,15 @@ exports.lookup = function (domain, family, callback) { callback(err, []); } }); - return + return; } // 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) { callback(null, domains4[0], 4); } else { - channel.getHostByName(domain, dns.AF_INET6, - function (err, domains6) { + channel.getHostByName(domain, dns.AF_INET6, function(err, domains6) { if (domains6 && domains6.length) { callback(null, domains6[0], 6); } 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); }; -exports.resolve6 = function(domain, callback) { + + +exports.resolve6 = function(domain, callback) { channel.query(domain, dns.AAAA, callback); }; -exports.resolveMx = function(domain, callback) { + + +exports.resolveMx = function(domain, callback) { channel.query(domain, dns.MX, callback); }; -exports.resolveTxt = function(domain, callback) { + + +exports.resolveTxt = function(domain, callback) { channel.query(domain, dns.TXT, callback); }; -exports.resolveSrv = function(domain, callback) { + + +exports.resolveSrv = function(domain, callback) { channel.query(domain, dns.SRV, callback); }; -exports.reverse = function(domain, callback) { + + +exports.reverse = function(domain, callback) { channel.query(domain, dns.PTR, callback); }; -exports.resolveNs = function(domain, callback) { + + +exports.resolveNs = function(domain, callback) { channel.query(domain, dns.NS, callback); }; + + exports.resolveCname = function(domain, callback) { channel.query(domain, dns.CNAME, callback); }; -var resolveMap = { - 'A' : exports.resolve4, - 'AAAA' : exports.resolve6, - 'MX' : exports.resolveMx, - 'TXT' : exports.resolveTxt, - 'SRV' : exports.resolveSrv, - 'PTR' : exports.resolvePtr, - 'NS' : exports.resolveNs, - 'CNAME' : exports.resolveCname -}; +var resolveMap = { A: exports.resolve4, + AAAA: exports.resolve6, + MX: exports.resolveMx, + TXT: exports.resolveTxt, + SRV: exports.resolveSrv, + PTR: exports.resolvePtr, + NS: exports.resolveNs, + CNAME: exports.resolveCname }; // ERROR CODES -exports.NODATA = dns.NODATA; -exports.FORMERR = dns.FORMERR; -exports.BADRESP = dns.BADRESP; -exports.NOTFOUND = dns.NOTFOUND; -exports.BADNAME = dns.BADNAME; -exports.TIMEOUT = dns.TIMEOUT; +exports.NODATA = dns.NODATA; +exports.FORMERR = dns.FORMERR; +exports.BADRESP = dns.BADRESP; +exports.NOTFOUND = dns.NOTFOUND; +exports.BADNAME = dns.BADNAME; +exports.TIMEOUT = dns.TIMEOUT; exports.CONNREFUSED = dns.CONNREFUSED; -exports.NOMEM = dns.NOMEM; +exports.NOMEM = dns.NOMEM; exports.DESTRUCTION = dns.DESTRUCTION; - -exports.NOTIMP = dns.NOTIMP; -exports.EREFUSED = dns.EREFUSED; -exports.SERVFAIL = dns.SERVFAIL; +exports.NOTIMP = dns.NOTIMP; +exports.EREFUSED = dns.EREFUSED; +exports.SERVFAIL = dns.SERVFAIL; diff --git a/lib/events.js b/lib/events.js index ef277372de..26e0f06193 100644 --- a/lib/events.js +++ b/lib/events.js @@ -2,7 +2,7 @@ var EventEmitter = exports.EventEmitter = process.EventEmitter; var isArray = Array.isArray; -EventEmitter.prototype.emit = function (type) { +EventEmitter.prototype.emit = function(type) { // If there is no 'error' event listener then throw. if (type === '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.prototype.emit() is also defined there. -EventEmitter.prototype.addListener = function (type, listener) { +EventEmitter.prototype.addListener = function(type, listener) { if ('function' !== typeof listener) { 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 // adding it to the listeners, first emit "newListeners". - this.emit("newListener", type, listener); + this.emit('newListener', type, listener); if (!this._events[type]) { // 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.once = function (type, listener) { +EventEmitter.prototype.once = function(type, listener) { var self = this; - self.on(type, function g () { + self.on(type, function g() { self.removeListener(type, g); listener.apply(this, arguments); }); }; -EventEmitter.prototype.removeListener = function (type, listener) { +EventEmitter.prototype.removeListener = function(type, listener) { if ('function' !== typeof listener) { throw new Error('removeListener only takes instances of Function'); } @@ -114,13 +114,13 @@ EventEmitter.prototype.removeListener = function (type, listener) { return this; }; -EventEmitter.prototype.removeAllListeners = function (type) { +EventEmitter.prototype.removeAllListeners = function(type) { // does not use listeners(), so no side effect of creating _events[type] if (type && this._events && this._events[type]) this._events[type] = null; return this; }; -EventEmitter.prototype.listeners = function (type) { +EventEmitter.prototype.listeners = function(type) { if (!this._events) this._events = {}; if (!this._events[type]) this._events[type] = []; if (!isArray(this._events[type])) { diff --git a/lib/freelist.js b/lib/freelist.js index 67da85a791..31acea2f90 100644 --- a/lib/freelist.js +++ b/lib/freelist.js @@ -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); - return this.list.length ? this.list.shift() - : this.constructor.apply(this, arguments); + return this.list.length ? this.list.shift() : + this.constructor.apply(this, arguments); }; -exports.FreeList.prototype.free = function (obj) { +exports.FreeList.prototype.free = function(obj) { //debug("free " + this.name + " " + this.list.length); if (this.list.length < this.max) { this.list.push(obj); diff --git a/lib/fs.js b/lib/fs.js index 938ffdc81e..b9e6b2d716 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -10,54 +10,57 @@ var kPoolSize = 40 * 1024; fs.Stats = binding.Stats; -fs.Stats.prototype._checkModeProperty = function (property) { +fs.Stats.prototype._checkModeProperty = function(property) { return ((this.mode & constants.S_IFMT) === property); }; -fs.Stats.prototype.isDirectory = function () { +fs.Stats.prototype.isDirectory = function() { return this._checkModeProperty(constants.S_IFDIR); }; -fs.Stats.prototype.isFile = function () { +fs.Stats.prototype.isFile = function() { return this._checkModeProperty(constants.S_IFREG); }; -fs.Stats.prototype.isBlockDevice = function () { +fs.Stats.prototype.isBlockDevice = function() { return this._checkModeProperty(constants.S_IFBLK); }; -fs.Stats.prototype.isCharacterDevice = function () { +fs.Stats.prototype.isCharacterDevice = function() { return this._checkModeProperty(constants.S_IFCHR); }; -fs.Stats.prototype.isSymbolicLink = function () { +fs.Stats.prototype.isSymbolicLink = function() { return this._checkModeProperty(constants.S_IFLNK); }; -fs.Stats.prototype.isFIFO = function () { +fs.Stats.prototype.isFIFO = function() { return this._checkModeProperty(constants.S_IFIFO); }; -fs.Stats.prototype.isSocket = function () { +fs.Stats.prototype.isSocket = function() { return this._checkModeProperty(constants.S_IFSOCK); }; -fs.readFile = function (path, encoding_) { +fs.readFile = function(path, encoding_) { 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; var readStream = fs.createReadStream(path); var buffers = []; var nread = 0; - readStream.on("data", function (chunk) { + + readStream.on('data', function(chunk) { buffers.push(chunk); nread += chunk.length; }); - readStream.on("error", function (er) { + + readStream.on('error', function(er) { callback(er); readStream.destroy(); }); - readStream.on("end", function () { + + readStream.on('end', function() { // copy all the buffers into one var buffer; switch (buffers.length) { @@ -66,7 +69,7 @@ fs.readFile = function (path, encoding_) { default: // concat together buffer = new Buffer(nread); var n = 0; - buffers.forEach(function (b) { + buffers.forEach(function(b) { var l = b.length; b.copy(buffer, n, 0, 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 buffer = new Buffer(4048); var buffers = []; @@ -107,13 +110,14 @@ fs.readFileSync = function (path, encoding) { var offset = 0; var i; buffer = new Buffer(nread); - buffers.forEach(function (i) { + buffers.forEach(function(i) { if (!i._bytesRead) return; - i.copy(buffer,offset,0,i._bytesRead); + i.copy(buffer, offset, 0, i._bytesRead); offset += i._bytesRead; }); } 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); } else { buffer = new Buffer(0); @@ -131,30 +135,43 @@ function stringToFlags(flag) { return flag; } switch (flag) { - case "r": return constants.O_RDONLY; - case "r+": return constants.O_RDWR; - 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); + case 'r': + return constants.O_RDONLY; + + case 'r+': + return constants.O_RDWR; + + 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 // list to make the arguments clear. -fs.close = function (fd, callback) { +fs.close = function(fd, callback) { binding.close(fd, callback || noop); }; -fs.closeSync = function (fd) { +fs.closeSync = function(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 callback_ = arguments[arguments.length - 1]; 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); }; -fs.openSync = function (path, flags, mode) { +fs.openSync = function(path, flags, mode) { if (mode === undefined) { mode = 0666; } 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)) { // legacy string interface (fd, length, position, encoding, callback) var cb = arguments[4], @@ -180,9 +197,7 @@ fs.read = function (fd, buffer, offset, length, position, callback) { callback = function(err, bytesRead) { if (!cb) return; - var str = (bytesRead > 0) - ? buffer.toString(encoding, 0, bytesRead) - : ''; + var str = (bytesRead > 0) ? buffer.toString(encoding, 0, 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); }; -fs.readSync = function (fd, buffer, offset, length, position) { +fs.readSync = function(fd, buffer, offset, length, position) { var legacy = false; if (!Buffer.isBuffer(buffer)) { // legacy string interface (fd, length, position, encoding, callback) @@ -209,19 +224,17 @@ fs.readSync = function (fd, buffer, offset, length, position) { return r; } - var str = (r > 0) - ? buffer.toString(encoding, 0, r) - : ''; + var str = (r > 0) ? buffer.toString(encoding, 0, 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)) { // legacy string interface (fd, data, position, encoding, callback) callback = arguments[4]; position = arguments[2]; - buffer = new Buffer(''+arguments[1], arguments[3]); + buffer = new Buffer('' + arguments[1], arguments[3]); offset = 0; 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); }; -fs.writeSync = function (fd, buffer, offset, length, position) { +fs.writeSync = function(fd, buffer, offset, length, position) { if (!Buffer.isBuffer(buffer)) { // legacy string interface (fd, data, position, encoding) position = arguments[2]; - buffer = new Buffer(''+arguments[1], arguments[3]); + buffer = new Buffer('' + arguments[1], arguments[3]); offset = 0; length = buffer.length; } - if(!length) return 0; + if (!length) return 0; 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); }; -fs.renameSync = function (oldPath, newPath) { +fs.renameSync = function(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); }; -fs.truncateSync = function (fd, len) { +fs.truncateSync = function(fd, len) { return binding.truncate(fd, len); }; -fs.rmdir = function (path, callback) { +fs.rmdir = function(path, callback) { binding.rmdir(path, callback || noop); }; -fs.rmdirSync = function (path) { +fs.rmdirSync = function(path) { return binding.rmdir(path); }; -fs.fdatasync = function (fd, callback) { +fs.fdatasync = function(fd, callback) { binding.fdatasync(fd, callback || noop); }; -fs.fdatasyncSync = function (fd) { +fs.fdatasyncSync = function(fd) { return binding.fdatasync(fd); }; -fs.fsync = function (fd, callback) { +fs.fsync = function(fd, callback) { binding.fsync(fd, callback || noop); }; -fs.fsyncSync = function (fd) { +fs.fsyncSync = function(fd) { return binding.fsync(fd); }; -fs.mkdir = function (path, mode, callback) { +fs.mkdir = function(path, mode, callback) { binding.mkdir(path, mode, callback || noop); }; -fs.mkdirSync = function (path, mode) { +fs.mkdirSync = function(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); }; -fs.sendfileSync = function (outFd, inFd, inOffset, length) { +fs.sendfileSync = function(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); }; -fs.readdirSync = function (path) { +fs.readdirSync = function(path) { return binding.readdir(path); }; -fs.fstat = function (fd, callback) { +fs.fstat = function(fd, callback) { binding.fstat(fd, callback || noop); }; -fs.lstat = function (path, callback) { +fs.lstat = function(path, callback) { binding.lstat(path, callback || noop); }; -fs.stat = function (path, callback) { +fs.stat = function(path, callback) { binding.stat(path, callback || noop); }; -fs.fstatSync = function (fd) { +fs.fstatSync = function(fd) { return binding.fstat(fd); }; -fs.lstatSync = function (path) { +fs.lstatSync = function(path) { return binding.lstat(path); }; -fs.statSync = function (path) { +fs.statSync = function(path) { return binding.stat(path); }; -fs.readlink = function (path, callback) { +fs.readlink = function(path, callback) { binding.readlink(path, callback || noop); }; -fs.readlinkSync = function (path) { +fs.readlinkSync = function(path) { return binding.readlink(path); }; -fs.symlink = function (destination, path, callback) { +fs.symlink = function(destination, path, callback) { binding.symlink(destination, path, callback || noop); }; -fs.symlinkSync = function (destination, path) { +fs.symlinkSync = function(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); }; -fs.linkSync = function (srcpath, dstpath) { +fs.linkSync = function(srcpath, dstpath) { return binding.link(srcpath, dstpath); }; -fs.unlink = function (path, callback) { +fs.unlink = function(path, callback) { binding.unlink(path, callback || noop); }; -fs.unlinkSync = function (path) { +fs.unlinkSync = function(path) { return binding.unlink(path); }; -fs.chmod = function (path, mode, callback) { +fs.chmod = function(path, mode, callback) { binding.chmod(path, mode, callback || noop); }; -fs.chmodSync = function (path, mode) { +fs.chmodSync = function(path, mode) { return binding.chmod(path, mode); }; @@ -388,28 +401,28 @@ fs.chownSync = function(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) - fs.write(fd, buffer, offset, length, offset, function (writeErr, written) { + fs.write(fd, buffer, offset, length, offset, function(writeErr, written) { if (writeErr) { - fs.close(fd, function () { + fs.close(fd, function() { if (callback) callback(writeErr); }); } else { if (written === length) { fs.close(fd, callback); } 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 callback_ = arguments[arguments.length - 1]; 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 (callback) callback(openErr); } else { @@ -419,16 +432,16 @@ fs.writeFile = function (path, data, encoding_, callback) { }); }; -fs.writeFileSync = function (path, data, encoding) { - var fd = fs.openSync(path, "w"); +fs.writeFileSync = function(path, data, encoding) { + var fd = fs.openSync(path, 'w'); if (!Buffer.isBuffer(data)) { - data = new Buffer(data, encoding || "utf8") + data = new Buffer(data, encoding || 'utf8'); } var written = 0; var length = data.length; //writeSync(fd, buffer, offset, length, position) while (written < length) { - written += fs.writeSync(fd, data, written, length-written, written); + written += fs.writeSync(fd, data, written, length - written, written); } fs.closeSync(fd); }; @@ -437,12 +450,12 @@ fs.writeFileSync = function (path, data, encoding) { var statWatchers = {}; -fs.watchFile = function (filename) { +fs.watchFile = function(filename) { var stat; var options; var listener; - if ("object" == typeof arguments[1]) { + if ('object' == typeof arguments[1]) { options = arguments[1]; listener = arguments[2]; } else { @@ -460,11 +473,11 @@ fs.watchFile = function (filename) { stat = statWatchers[filename]; stat.start(filename, options.persistent, options.interval); } - stat.addListener("change", listener); + stat.addListener('change', listener); return stat; }; -fs.unwatchFile = function (filename) { +fs.unwatchFile = function(filename) { var stat; if (statWatchers[filename]) { stat = statWatchers[filename]; @@ -484,7 +497,7 @@ var normalizeArray = path.normalizeArray; // See: http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html fs.realpathSync = realpathSync; fs.realpath = realpath; -function realpathSync (p) { +function realpathSync(p) { if (p.charAt(0) !== '/') { p = path.join(process.cwd(), p); } @@ -496,26 +509,31 @@ function realpathSync (p) { // values, and pushing non-link path bits onto the buffer. // then return the buffer. // 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. if (p[i] === '') continue; + var part = path.join.apply(path, buf.concat(p[i])); + if (knownHard[part]) { - buf.push( p[i] ); + buf.push(p[i]); continue; } + var stat = fs.lstatSync(part); if (!stat.isSymbolicLink()) { // not a symlink. easy. - knownHard[ part ] = true; + knownHard[part] = true; buf.push(p[i]); continue; } - var id = stat.dev.toString(32)+':'+stat.ino.toString(32); + + var id = stat.dev.toString(32) + ':' + stat.ino.toString(32); if (!seenLinks[id]) { fs.statSync(part); seenLinks[id] = fs.readlinkSync(part); } + var target = seenLinks[id]; if (target.charAt(0) === '/') { // absolute. Start over. @@ -524,9 +542,10 @@ function realpathSync (p) { i = -1; continue; } + // not absolute. join and splice. - if (i === 0 && p[i].charAt(0) === "/") { - target = "/"+target; + if (i === 0 && p[i].charAt(0) === '/') { + target = '/' + target; } target = path.split(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) !== '/') { p = path.join(process.cwd(), p); } @@ -552,37 +571,41 @@ function realpath (p, cb) { // NB: path.length changes. var i = -1; var part; + LOOP(); - function LOOP () { - i ++; + function LOOP() { + i++; if (!(i < p.length)) return exit(); // skip over empty path parts. if (p[i] === '') return process.nextTick(LOOP); - part = path.join(buf.join('/')+'/'+p[i]); + part = path.join(buf.join('/') + '/' + p[i]); if (knownHard[part]) { - buf.push( p[i] ); + buf.push(p[i]); return process.nextTick(LOOP); } return fs.lstat(part, gotStat); } - function gotStat (er, stat) { + + function gotStat(er, stat) { if (er) return cb(er); if (!stat.isSymbolicLink()) { // not a symlink. easy. - knownHard[ part ] = true; + knownHard[part] = true; buf.push(p[i]); 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]); - fs.stat(part, function (er) { - if (er) return cb(er) - fs.readlink(part, function (er, target) { + fs.stat(part, function(er) { + if (er) return cb(er); + fs.readlink(part, function(er, target) { gotTarget(er, seenLinks[id] = target); }); - }) + }); } - function gotTarget (er, target) { + + function gotTarget(er, target) { if (er) return cb(er); if (target.charAt(0) === '/') { // absolute. Start over. @@ -592,8 +615,8 @@ function realpath (p, cb) { return process.nextTick(LOOP); } // not absolute. join and splice. - if (i === 0 && p[i].charAt(0) === "/") { - target = "/"+target; + if (i === 0 && p[i].charAt(0) === '/') { + target = '/' + target; } target = path.split(target); Array.prototype.splice.apply(p, [i, 1].concat(target)); @@ -602,13 +625,15 @@ function realpath (p, cb) { buf = []; return process.nextTick(LOOP); } - function exit () { + + function exit() { cb(null, path.join(buf.join('/') || '/')); } } var pool; -function allocNewPool () { + +function allocNewPool() { pool = new Buffer(kPoolSize); pool.used = 0; } @@ -644,12 +669,12 @@ var ReadStream = fs.ReadStream = function(path, options) { 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) { - this.emit('error', - new Error('Both start and end are needed for range streaming.')); + this.emit('error', new Error('Both start and end are needed ' + + 'for range streaming.')); } else if (this.start > this.end) { this.emit('error', new Error('start must be <= end')); } else { @@ -677,13 +702,13 @@ util.inherits(ReadStream, Stream); fs.FileReadStream = fs.ReadStream; // support the legacy name -ReadStream.prototype.setEncoding = function (encoding) { - var StringDecoder = require("string_decoder").StringDecoder; // lazy load +ReadStream.prototype.setEncoding = function(encoding) { + var StringDecoder = require('string_decoder').StringDecoder; // lazy load this._decoder = new StringDecoder(encoding); }; -ReadStream.prototype._read = function () { +ReadStream.prototype._read = function() { var self = this; if (!self.readable || self.paused) return; @@ -710,7 +735,7 @@ ReadStream.prototype._read = function () { toRead = Math.min(this.end - this.pos + 1, toRead); } - function afterRead (err, bytesRead) { + function afterRead(err, bytesRead) { if (err) { self.emit('error', err); self.readable = false; @@ -723,7 +748,7 @@ ReadStream.prototype._read = function () { return; } - var b = thisPool.slice(start, start+bytesRead); + var b = thisPool.slice(start, start + bytesRead); // Possible optimizition here? // 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) { var string = this._decoder.write(d); 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; this.readable = false; @@ -842,7 +867,7 @@ util.inherits(WriteStream, Stream); fs.FileWriteStream = fs.WriteStream; // support the legacy name -WriteStream.prototype.flush = function () { +WriteStream.prototype.flush = function() { if (this.busy) return; var self = this; @@ -900,7 +925,7 @@ WriteStream.prototype.flush = function () { method.apply(this, args); }; -WriteStream.prototype.write = function (data) { +WriteStream.prototype.write = function(data) { if (!this.writable) { throw new Error('stream not writable'); } @@ -908,8 +933,8 @@ WriteStream.prototype.write = function (data) { this.drainable = true; var cb; - if (typeof(arguments[arguments.length-1]) == 'function') { - cb = arguments[arguments.length-1]; + if (typeof(arguments[arguments.length - 1]) == 'function') { + cb = arguments[arguments.length - 1]; } if (Buffer.isBuffer(data)) { @@ -926,13 +951,13 @@ WriteStream.prototype.write = function (data) { return false; }; -WriteStream.prototype.end = function (cb) { +WriteStream.prototype.end = function(cb) { this.writable = false; this._queue.push([fs.close, cb]); this.flush(); }; -WriteStream.prototype.destroy = function (cb) { +WriteStream.prototype.destroy = function(cb) { var self = this; this.writable = false;