Comparing version 0.2.1 to 0.2.2
136
lib/nats.js
@@ -21,3 +21,3 @@ /*! | ||
var VERSION = '0.2.1', | ||
var VERSION = '0.2.2', | ||
@@ -70,17 +70,2 @@ DEFAULT_PORT = 4222, | ||
/** | ||
* Connect to a nats-server and return the client. | ||
* Argument can be a url, or an object with a 'url' | ||
* property and additional options. | ||
* | ||
* @params {Mixed} opts | ||
* | ||
* @api public | ||
*/ | ||
var connect = exports.connect = function(opts) { | ||
var client = new Client(opts); | ||
return client; | ||
} | ||
/** | ||
* Generate random hex strings for createInbox. | ||
@@ -92,3 +77,3 @@ * | ||
function hexRand(limit) { | ||
return parseInt(Math.random()*limit).toString(16) | ||
return (parseInt(Math.random()*limit, 16).toString(16)); | ||
} | ||
@@ -103,8 +88,8 @@ | ||
var createInbox = exports.createInbox = function() { | ||
return "_INBOX." + | ||
hexRand(0x0010000) + | ||
hexRand(0x0010000) + | ||
hexRand(0x0010000) + | ||
hexRand(0x0010000) + | ||
hexRand(0x1000000); | ||
return ('_INBOX.' + | ||
hexRand(0x0010000) + | ||
hexRand(0x0010000) + | ||
hexRand(0x0010000) + | ||
hexRand(0x0010000) + | ||
hexRand(0x1000000)); | ||
} | ||
@@ -127,2 +112,16 @@ | ||
/** | ||
* Connect to a nats-server and return the client. | ||
* Argument can be a url, or an object with a 'url' | ||
* property and additional options. | ||
* | ||
* @params {Mixed} opts | ||
* | ||
* @api public | ||
*/ | ||
var connect = exports.connect = function(opts) { | ||
return new Client(opts); | ||
} | ||
/** | ||
* Connected clients are event emitters. | ||
@@ -141,2 +140,11 @@ */ | ||
Client.prototype.assignOption = function(opts, prop, assign) { | ||
if (assign === undefined) { | ||
assign = prop; | ||
} | ||
if (opts[prop] !== undefined) { | ||
this.options[assign] = opts[prop]; | ||
} | ||
}; | ||
/** | ||
@@ -158,51 +166,33 @@ * Parse the conctructor/connect options. | ||
}; | ||
if ('number' == typeof opts) { | ||
if ('number' === typeof opts) { | ||
options.url = DEFAULT_PRE + opts; | ||
} else if ('string' == typeof opts) { | ||
} else if ('string' === typeof opts) { | ||
options.url = opts; | ||
} else if ('object' == typeof opts) { | ||
if (opts['port'] != undefined) { | ||
options.url = DEFAULT_PRE + opts['port']; | ||
} else if ('object' === typeof opts) { | ||
if (opts.port !== undefined) { | ||
options.url = DEFAULT_PRE + opts.port; | ||
} | ||
// Pull out various options here | ||
if (opts['url'] != undefined) { | ||
options.url = opts.url; | ||
} else if (opts['uri'] != undefined) { | ||
options.url = opts.uri; | ||
} | ||
if (opts['user'] != undefined) { | ||
options.user = opts.user; | ||
} | ||
if (opts['pass'] != undefined) { | ||
options.pass = opts.pass; | ||
} else if (opts['password'] != undefined) { | ||
options.pass = opts.password; | ||
} | ||
if (opts['verbose'] != undefined) { | ||
this.options.verbose = opts['verbose']; | ||
} | ||
if (opts['pedantic'] != undefined) { | ||
options.pedantic = opts['pedantic']; | ||
} | ||
if (opts['reconnect'] != undefined) { | ||
options.reconnect = opts['reconnect']; | ||
} | ||
if (opts['maxReconnectAttempts'] != undefined) { | ||
options.maxReconnectAttempts = opts['maxReconnectAttempts']; | ||
} | ||
if (opts['reconnectTimeWait'] != undefined) { | ||
options.reconnectTimeWait = opts['reconnectTimeWait']; | ||
} | ||
this.assignOption(opts, 'url'); | ||
this.assignOption(opts, 'uri', 'url'); | ||
this.assignOption(opts, 'user'); | ||
this.assignOption(opts, 'pass'); | ||
this.assignOption(opts, 'password', 'pass'); | ||
this.assignOption(opts, 'verbose'); | ||
this.assignOption(opts, 'pedantic'); | ||
this.assignOption(opts, 'reconnect'); | ||
this.assignOption(opts, 'maxReconnectAttempts'); | ||
this.assignOption(opts, 'reconnectTimeWait'); | ||
} | ||
options.uri = options.url; | ||
if (options.url != undefined) { | ||
if (options.url !== undefined) { | ||
// Parse the url | ||
this.url = url.parse(options.url); | ||
if (this.url.auth != undefined) { | ||
if (this.url.auth !== undefined) { | ||
var auth = this.url.auth.split(':'); | ||
if (options.user == undefined) { | ||
if (options.user === undefined) { | ||
options.user = auth[0]; | ||
} | ||
if (options.pass == undefined) { | ||
if (options.pass === undefined) { | ||
options.pass = auth[1]; | ||
@@ -250,3 +240,4 @@ } | ||
client.emit('disconnect'); | ||
if (client.closed === true || client.options.reconnect === false || | ||
if (client.closed === true || | ||
client.options.reconnect === false || | ||
client.reconnects >= client.options.maxReconnectAttempts) { | ||
@@ -284,3 +275,3 @@ client.emit('close'); | ||
if (m = MSG.exec(client.inbound)) { | ||
client.payload = {subj : m[1], sid : m[2], reply : m[4], size : parseInt(m[5])} | ||
client.payload = {subj : m[1], sid : m[2], reply : m[4], size : parseInt(m[5], 10)} | ||
client.pstate = AWAITING_MSG_PAYLOAD; | ||
@@ -334,3 +325,3 @@ } else if (m = OK.exec(client.inbound)) { | ||
var cs = { 'verbose':this.options.verbose, 'pedantic':this.options.pedantic }; | ||
if (this.options.user != undefined) { | ||
if (this.options.user !== undefined) { | ||
cs.user = this.options.user; | ||
@@ -363,5 +354,5 @@ cs.pass = this.options.pass; | ||
Client.prototype.close = function() { | ||
this.closed = true; | ||
this.removeAllListeners(); | ||
this.closeStream(); | ||
this.closed = true; | ||
this.ssid = -1; | ||
@@ -381,4 +372,3 @@ this.subs = null; | ||
Client.prototype.closeStream = function() { | ||
if (this.stream != undefined) { | ||
this.removeAllListeners(); | ||
if (this.stream != null) { | ||
this.stream.end(); | ||
@@ -388,3 +378,3 @@ this.stream.destroy(); | ||
} | ||
if (this.connected) { | ||
if (this.connected === true || this.closed === true) { | ||
this.pongs = null; | ||
@@ -403,3 +393,3 @@ this.pending = null; | ||
Client.prototype.flushPending = function() { | ||
if (this.connected === false || this.pending == undefined) { | ||
if (this.connected === false || this.pending == null) { | ||
return; | ||
@@ -453,3 +443,3 @@ } | ||
var sub = this.subs[this.payload.sid]; | ||
if (sub != undefined) { | ||
if (sub != null) { | ||
sub.received += 1; | ||
@@ -580,8 +570,8 @@ // Check for a timeout, and cancel if received >= expected | ||
var sub = this.subs[sid]; | ||
if (sub == undefined) { | ||
if (sub == null) { | ||
return; | ||
} | ||
sub['max'] = opt_max; | ||
if (sub['max'] == undefined || (sub.received >= sub.max)) { | ||
this.subs[sid] = null; | ||
if (sub['max'] === undefined || (sub.received >= sub.max)) { | ||
delete this.subs[sid]; | ||
} | ||
@@ -602,3 +592,3 @@ } | ||
var sub = this.subs[sid]; | ||
if (sub == undefined) { return; } | ||
if (sub == null) { return; } | ||
sub.expected = expected; | ||
@@ -605,0 +595,0 @@ sub.timeout = setTimeout(function() { callback(sid); }, timeout); |
{ | ||
"name": "nats", | ||
"description": "Node.js client for NATS, a lightweight messaging system", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "type" : "git", |
@@ -29,3 +29,2 @@ var NATS = require ('../'), | ||
}); | ||
for (var i=0; i<SEND; i++) { | ||
@@ -50,7 +49,5 @@ nc.publish('foo'); | ||
}); | ||
for (var i=0; i<SEND; i++) { | ||
nc.publish('foo'); | ||
} | ||
nc.unsubscribe(sid, WANT); | ||
@@ -143,3 +140,2 @@ | ||
received += 1; | ||
nc.flush(function() { | ||
@@ -150,3 +146,2 @@ received.should.equal(1); | ||
}); | ||
}); | ||
@@ -153,0 +148,0 @@ |
@@ -27,3 +27,3 @@ | ||
var maxWait = 5 * 1000; // 5 secs | ||
var delta = 50; | ||
var delta = 250; | ||
var socket; | ||
@@ -33,6 +33,6 @@ var timer; | ||
var resetSocket = function() { | ||
if (socket) { | ||
if (socket !== undefined) { | ||
socket.removeAllListeners(); | ||
socket.destroy(); | ||
socket = null; | ||
socket = undefined; | ||
} | ||
@@ -43,4 +43,9 @@ } | ||
resetSocket(); | ||
if (timer) { clearInterval(timer); } | ||
if (done) { done(err); } | ||
if (timer !== undefined) { | ||
clearInterval(timer); | ||
timer = undefined; | ||
} | ||
if (done) { | ||
done(err); | ||
} | ||
}; | ||
@@ -50,2 +55,4 @@ | ||
timer = setInterval(function() { | ||
resetSocket(); | ||
wait = new Date() - start; | ||
@@ -56,4 +63,2 @@ if (wait > maxWait) { | ||
resetSocket(); | ||
// Try to connect to the correct port. | ||
@@ -60,0 +65,0 @@ socket = net.createConnection(port); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43434
1345