more lint

This commit is contained in:
Ryan Dahl 2010-12-01 17:29:11 -08:00
parent ac58d3a665
commit f22c248e4c
4 changed files with 91 additions and 76 deletions

View File

@ -2,7 +2,7 @@ var writeError = process.binding('stdio').writeError;
// console object // console object
var formatRegExp = /%[sdj]/g; var formatRegExp = /%[sdj]/g;
function format (f) { function format(f) {
if (typeof f !== 'string') { if (typeof f !== 'string') {
var objects = [], util = require('util'); var objects = [], util = require('util');
for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < arguments.length; i++) {
@ -14,7 +14,7 @@ function format (f) {
var i = 1; var i = 1;
var args = arguments; var args = arguments;
var str = String(f).replace(formatRegExp, function (x) { var str = String(f).replace(formatRegExp, function(x) {
switch (x) { switch (x) {
case '%s': return args[i++]; case '%s': return args[i++];
case '%d': return +args[i++]; case '%d': return +args[i++];
@ -30,7 +30,7 @@ function format (f) {
} }
exports.log = function () { exports.log = function() {
process.stdout.write(format.apply(this, arguments) + '\n'); process.stdout.write(format.apply(this, arguments) + '\n');
}; };
@ -38,7 +38,7 @@ exports.log = function () {
exports.info = exports.log; exports.info = exports.log;
exports.warn = function () { exports.warn = function() {
writeError(format.apply(this, arguments) + '\n'); writeError(format.apply(this, arguments) + '\n');
}; };
@ -46,25 +46,25 @@ exports.warn = function () {
exports.error = exports.warn; exports.error = exports.warn;
exports.dir = function(object){ exports.dir = function(object) {
var util = require('util'); var util = require('util');
process.stdout.write(util.inspect(object) + '\n'); process.stdout.write(util.inspect(object) + '\n');
}; };
var times = {}; var times = {};
exports.time = function(label){ exports.time = function(label) {
times[label] = Date.now(); times[label] = Date.now();
}; };
exports.timeEnd = function(label){ exports.timeEnd = function(label) {
var duration = Date.now() - times[label]; var duration = Date.now() - times[label];
exports.log('%s: %dms', label, duration); exports.log('%s: %dms', label, duration);
}; };
exports.trace = function(label){ exports.trace = function(label) {
// TODO probably can to do this better with V8's debug object once that is // TODO probably can to do this better with V8's debug object once that is
// exposed. // exposed.
var err = new Error; var err = new Error;
@ -75,8 +75,8 @@ exports.trace = function(label){
}; };
exports.assert = function(expression){ exports.assert = function(expression) {
if(!expression){ if (!expression) {
var arr = Array.prototype.slice.call(arguments, 1); var arr = Array.prototype.slice.call(arguments, 1);
process.assert(false, format.apply(this, arr)); process.assert(false, format.apply(this, arr));
} }

View File

@ -1 +1 @@
module.exports = process.binding("constants"); module.exports = process.binding('constants');

View File

@ -16,7 +16,7 @@ try {
} }
function Credentials (method) { function Credentials(method) {
if (!(this instanceof Credentials)) { if (!(this instanceof Credentials)) {
return new Credentials(method); return new Credentials(method);
} }
@ -38,7 +38,7 @@ function Credentials (method) {
exports.Credentials = Credentials; exports.Credentials = Credentials;
exports.createCredentials = function (options) { exports.createCredentials = function(options) {
if (!options) options = {}; if (!options) options = {};
var c = new Credentials(options.method); var c = new Credentials(options.method);
@ -63,46 +63,46 @@ exports.createCredentials = function (options) {
exports.Hash = Hash; exports.Hash = Hash;
exports.createHash = function (hash) { exports.createHash = function(hash) {
return new Hash(hash); return new Hash(hash);
}; };
exports.Hmac = Hmac; exports.Hmac = Hmac;
exports.createHmac = function (hmac, key) { exports.createHmac = function(hmac, key) {
return (new Hmac).init(hmac, key); return (new Hmac).init(hmac, key);
}; };
exports.Cipher = Cipher; exports.Cipher = Cipher;
exports.createCipher = function (cipher, key) { exports.createCipher = function(cipher, key) {
return (new Cipher).init(cipher, key); return (new Cipher).init(cipher, key);
}; };
exports.createCipheriv = function (cipher, key, iv) { exports.createCipheriv = function(cipher, key, iv) {
return (new Cipher).initiv(cipher, key, iv); return (new Cipher).initiv(cipher, key, iv);
}; };
exports.Decipher = Decipher; exports.Decipher = Decipher;
exports.createDecipher = function (cipher, key) { exports.createDecipher = function(cipher, key) {
return (new Decipher).init(cipher, key); return (new Decipher).init(cipher, key);
}; };
exports.createDecipheriv = function (cipher, key, iv) { exports.createDecipheriv = function(cipher, key, iv) {
return (new Decipher).initiv(cipher, key, iv); return (new Decipher).initiv(cipher, key, iv);
}; };
exports.Sign = Sign; exports.Sign = Sign;
exports.createSign = function (algorithm) { exports.createSign = function(algorithm) {
return (new Sign).init(algorithm); return (new Sign).init(algorithm);
}; };
exports.Verify = Verify; exports.Verify = Verify;
exports.createVerify = function (algorithm) { exports.createVerify = function(algorithm) {
return (new Verify).init(algorithm); return (new Verify).init(algorithm);
}; };

View File

@ -1,19 +1,19 @@
var util = require("util"); var util = require('util');
var fs = require("fs"); var fs = require('fs');
var events = require("events"); var events = require('events');
var dns = require('dns'); var dns = require('dns');
var IOWatcher = process.binding('io_watcher').IOWatcher; var IOWatcher = process.binding('io_watcher').IOWatcher;
var binding = process.binding('net'); var binding = process.binding('net');
var constants = process.binding('constants'); var constants = process.binding('constants');
var socket = binding.socket; var socket = binding.socket;
var recvfrom = binding.recvfrom; var recvfrom = binding.recvfrom;
var close = binding.close; var close = binding.close;
var ENOENT = constants.ENOENT; var ENOENT = constants.ENOENT;
function isPort (x) { return parseInt(x) >= 0; } function isPort(x) { return parseInt(x) >= 0; }
var pool = null; var pool = null;
function getPool() { function getPool() {
@ -22,7 +22,7 @@ function getPool() {
var poolSize = 1024 * 64; var poolSize = 1024 * 64;
if (pool === null || (pool.used + minPoolAvail > pool.length)) { if (pool === null || (pool.used + minPoolAvail > pool.length)) {
pool = new Buffer(poolSize); pool = new Buffer(poolSize);
pool.used = 0; pool.used = 0;
} }
@ -31,24 +31,26 @@ function getPool() {
} }
function dnsLookup(type, hostname, callback) { function dnsLookup(type, hostname, callback) {
var family = (type ? ((type === "udp6") ? 6 : 4) : null); var family = (type ? ((type === 'udp6') ? 6 : 4) : null);
dns.lookup(hostname, family, function (err, ip, addressFamily) { dns.lookup(hostname, family, function(err, ip, addressFamily) {
if (!err && family && addressFamily !== family) { if (!err && family && addressFamily !== family) {
err = new Error('no address found in family '+type+' for '+hostname); err = new Error('no address found in family ' + type +
' for ' + hostname);
} }
callback(err, ip, addressFamily); callback(err, ip, addressFamily);
}); });
} }
function Socket (type, listener) { function Socket(type, listener) {
events.EventEmitter.call(this); events.EventEmitter.call(this);
var self = this; var self = this;
self.type = type; self.type = type;
if (type === "unix_dgram" || type === "udp4" || type === "udp6") { if (type === 'unix_dgram' || type === 'udp4' || type === 'udp6') {
self.fd = socket(self.type); self.fd = socket(self.type);
} else { } else {
throw new Error("Bad socket type specified. Valid types are: unix_dgram, udp4, udp6"); throw new Error('Bad socket type specified. Valid types are: ' +
'unix_dgram, udp4, udp6');
} }
if (typeof listener === 'function') { if (typeof listener === 'function') {
@ -57,7 +59,7 @@ function Socket (type, listener) {
self.watcher = new IOWatcher(); self.watcher = new IOWatcher();
self.watcher.host = self; self.watcher.host = self;
self.watcher.callback = function () { self.watcher.callback = function() {
while (self.fd) { while (self.fd) {
var p = getPool(); var p = getPool();
var rinfo = recvfrom(self.fd, p, p.used, p.length - p.used, 0); var rinfo = recvfrom(self.fd, p, p.used, p.length - p.used, 0);
@ -69,8 +71,8 @@ function Socket (type, listener) {
p.used += rinfo.size; p.used += rinfo.size;
} }
}; };
if (self.type === "udp4" || self.type === "udp6") { if (self.type === 'udp4' || self.type === 'udp6') {
self._startWatcher(); self._startWatcher();
} }
} }
@ -78,65 +80,70 @@ function Socket (type, listener) {
util.inherits(Socket, events.EventEmitter); util.inherits(Socket, events.EventEmitter);
exports.Socket = Socket; exports.Socket = Socket;
exports.createSocket = function (type, listener) { exports.createSocket = function(type, listener) {
return new Socket(type, listener); return new Socket(type, listener);
}; };
Socket.prototype.bind = function () { Socket.prototype.bind = function() {
var self = this; var self = this;
if (this.type === "unix_dgram") { // bind(path) if (this.type === 'unix_dgram') {
if (typeof arguments[0] !== "string") { // bind(path)
throw new Error("unix_dgram sockets must be bound to a path in the filesystem"); if (typeof arguments[0] !== 'string') {
throw new Error('unix_dgram sockets must be bound to a path in ' +
'the filesystem');
} }
this.path = arguments[0]; this.path = arguments[0];
fs.unlink(this.path, function (err) { // unlink old file, OK if it doesn't exist // unlink old file, OK if it doesn't exist
fs.unlink(this.path, function(err) {
if (err && err.errno !== ENOENT) { if (err && err.errno !== ENOENT) {
throw err; throw err;
} else { } else {
try { try {
binding.bind(self.fd, self.path); binding.bind(self.fd, self.path);
self._startWatcher(); self._startWatcher();
self.emit("listening"); self.emit('listening');
} catch (err) { } catch (err) {
console.log("Error in unix_dgram bind of " + self.path); console.log('Error in unix_dgram bind of ' + self.path);
console.log(err.stack); console.log(err.stack);
throw err; throw err;
} }
} }
}); });
} else if (this.type === "udp4" || this.type === "udp6") { // bind(port, [address]) } else if (this.type === 'udp4' || this.type === 'udp6') {
// bind(port, [address])
if (arguments[1] === undefined) { if (arguments[1] === undefined) {
// Not bind()ing a specific address. Use INADDR_ANY and OS will pick one. // Not bind()ing a specific address. Use INADDR_ANY and OS will pick one.
// The address can be found with server.address() // The address can be found with server.address()
binding.bind(self.fd, arguments[0]); binding.bind(self.fd, arguments[0]);
this.emit("listening"); this.emit('listening');
} else { } else {
// the first argument is the port, the second an address // the first argument is the port, the second an address
this.port = arguments[0]; this.port = arguments[0];
dnsLookup(this.type, arguments[1], function (err, ip, addressFamily) { dnsLookup(this.type, arguments[1], function(err, ip, addressFamily) {
if (err) { if (err) {
self.emit('error', err); self.emit('error', err);
} else { } else {
self.ip = ip; self.ip = ip;
binding.bind(self.fd, self.port, ip); binding.bind(self.fd, self.port, ip);
self.emit("listening"); self.emit('listening');
} }
}); });
} }
} }
}; };
Socket.prototype._startWatcher = function () { Socket.prototype._startWatcher = function() {
if (! this._watcherStarted) { if (! this._watcherStarted) {
this.watcher.set(this.fd, true, false); // listen for read ready, not write ready // listen for read ready, not write ready
this.watcher.set(this.fd, true, false);
this.watcher.start(); this.watcher.start();
this._watcherStarted = true; this._watcherStarted = true;
} }
}; };
Socket.prototype.address = function () { Socket.prototype.address = function() {
return binding.getsockname(this.fd); return binding.getsockname(this.fd);
}; };
@ -150,11 +157,11 @@ Socket.prototype.setBroadcast = function(arg) {
Socket.prototype.setTTL = function(arg) { Socket.prototype.setTTL = function(arg) {
var newttl = parseInt(arg); var newttl = parseInt(arg);
if (newttl > 0 && newttl < 256) { if (newttl > 0 && newttl < 256) {
return binding.setTTL(this.fd, newttl); return binding.setTTL(this.fd, newttl);
} else { } else {
throw new Error("New TTL must be between 1 and 255"); throw new Error('New TTL must be between 1 and 255');
} }
}; };
@ -162,28 +169,31 @@ Socket.prototype.setTTL = function(arg) {
Socket.prototype.send = function(buffer, offset, length) { Socket.prototype.send = function(buffer, offset, length) {
var self = this; var self = this;
if (typeof offset !== "number" || typeof length !== "number") { if (typeof offset !== 'number' || typeof length !== 'number') {
throw new Error("send takes offset and length as args 2 and 3"); throw new Error('send takes offset and length as args 2 and 3');
} }
if (this.type === "unix_dgram") { // send(buffer, offset, length, path [, callback]) if (this.type === 'unix_dgram') {
if (typeof arguments[3] !== "string") { // send(buffer, offset, length, path [, callback])
throw new Error("unix_dgram sockets must send to a path in the filesystem"); if (typeof arguments[3] !== 'string') {
throw new Error('unix_dgram sockets must send to a path ' +
'in the filesystem');
} }
self.sendto(buffer, offset, length, arguments[3], null, arguments[4]); self.sendto(buffer, offset, length, arguments[3], null, arguments[4]);
} else if (this.type === "udp4" || this.type === "udp6") { // send(buffer, offset, length, port, address [, callback]) } else if (this.type === 'udp4' || this.type === 'udp6') {
if (typeof arguments[4] !== "string") { // send(buffer, offset, length, port, address [, callback])
throw new Error(this.type + " sockets must send to port, address"); if (typeof arguments[4] !== 'string') {
throw new Error(this.type + ' sockets must send to port, address');
} }
if (binding.isIP(arguments[4])) { if (binding.isIP(arguments[4])) {
self.sendto(arguments[0], arguments[1], arguments[2], arguments[3], self.sendto(arguments[0], arguments[1], arguments[2], arguments[3],
arguments[4], arguments[5]); arguments[4], arguments[5]);
} else { } else {
var port = arguments[3], var port = arguments[3],
callback = arguments[5]; callback = arguments[5];
dnsLookup(this.type, arguments[4], function (err, ip, addressFamily) { dnsLookup(this.type, arguments[4], function(err, ip, addressFamily) {
if (err) { // DNS error if (err) { // DNS error
if (callback) { if (callback) {
callback(err); callback(err);
@ -197,7 +207,12 @@ Socket.prototype.send = function(buffer, offset, length) {
} }
}; };
Socket.prototype.sendto = function(buffer, offset, length, port, addr, callback) { Socket.prototype.sendto = function(buffer,
offset,
length,
port,
addr,
callback) {
try { try {
var bytes = binding.sendto(this.fd, buffer, offset, length, 0, port, addr); var bytes = binding.sendto(this.fd, buffer, offset, length, 0, port, addr);
} catch (err) { } catch (err) {
@ -212,7 +227,7 @@ Socket.prototype.sendto = function(buffer, offset, length, port, addr, callback)
} }
}; };
Socket.prototype.close = function () { Socket.prototype.close = function() {
var self = this; var self = this;
if (!this.fd) throw new Error('Not running'); if (!this.fd) throw new Error('Not running');
@ -223,11 +238,11 @@ Socket.prototype.close = function () {
close(this.fd); close(this.fd);
this.fd = null; this.fd = null;
if (this.type === "unix_dgram" && this.path) { if (this.type === 'unix_dgram' && this.path) {
fs.unlink(this.path, function () { fs.unlink(this.path, function() {
self.emit("close"); self.emit('close');
}); });
} else { } else {
this.emit("close"); this.emit('close');
} }
}; };