Comparing version 0.6.8 to 0.7.2
@@ -7,2 +7,17 @@ | ||
/** | ||
* Error codes | ||
*/ | ||
export const BAD_SUBJECT: string; | ||
export const BAD_MSG: string; | ||
export const BAD_REPLY: string; | ||
export const CONN_CLOSED: string; | ||
export const BAD_JSON: string; | ||
export const BAD_AUTHENTICATION: string; | ||
export const INVALID_ENCODING: string; | ||
export const SECURE_CONN_REQ: string; | ||
export const NON_SECURE_CONN_REQ: string; | ||
export const CLIENT_CERT_REQ: string; | ||
export const NATS_PROTOCOL_ERR: string; | ||
/** | ||
* Create a properly formatted inbox subject. | ||
@@ -49,3 +64,4 @@ */ | ||
waitOnFirstConnect?: boolean, | ||
json?: boolean | ||
json?: boolean, | ||
preserveBuffers?: boolean | ||
} | ||
@@ -115,2 +131,11 @@ | ||
numSubscriptions(): number; | ||
} | ||
declare class NatsError implements Error { | ||
public name: string; | ||
public message: string; | ||
public code: string; | ||
public chainedError: Error; | ||
constructor(message:string, code:string, chainedError?:Error); | ||
} |
121
lib/nats.js
@@ -26,3 +26,3 @@ /*! | ||
var VERSION = '0.6.8', | ||
var VERSION = '0.7.2', | ||
@@ -56,3 +56,2 @@ DEFAULT_PORT = 4222, | ||
CR_LF_LEN = CR_LF.length, | ||
CR_LF_BUF = new Buffer(CR_LF), | ||
EMPTY = '', | ||
@@ -72,8 +71,25 @@ SPC = ' ', | ||
// Errors | ||
BAD_SUBJECT = 'Subject must be supplied', | ||
BAD_MSG = 'Message can\'t be a function', | ||
BAD_REPLY = 'Reply can\'t be a function', | ||
CONN_CLOSED = 'Connection closed', | ||
BAD_SUBJECT = 'BAD_SUBJECT', | ||
BAD_SUBJECT_MSG = 'Subject must be supplied', | ||
BAD_MSG = 'BAD_MSG', | ||
BAD_MSG_MSG = 'Message can\'t be a function', | ||
BAD_REPLY = 'BAD_REPLY', | ||
BAD_REPLY_MSG = 'Reply can\'t be a function', | ||
CONN_CLOSED = 'CONN_CLOSED', | ||
CONN_CLOSED_MSG = 'Connection closed', | ||
BAD_JSON = 'BAD_JSON', | ||
BAD_JSON_MSG = 'Message should be a JSON object', | ||
BAD_AUTHENTICATION = 'User and Token can not both be provided', | ||
BAD_AUTHENTICATION = 'BAD_AUTHENTICATION', | ||
BAD_AUTHENTICATION_MSG = 'User and Token can not both be provided', | ||
INVALID_ENCODING = 'INVALID_ENCODING', | ||
INVALID_ENCODING_MSG_PREFIX = 'Invalid Encoding:', | ||
SECURE_CONN_REQ = 'SECURE_CONN_REQ', | ||
SECURE_CONN_REQ_MSG = 'Server requires a secure connection.', | ||
NON_SECURE_CONN_REQ = 'NON_SECURE_CONN_REQ', | ||
NON_SECURE_CONN_REQ_MSG = 'Server does not support a secure connection.', | ||
CLIENT_CERT_REQ = 'CLIENT_CERT_REQ', | ||
CLIENT_CERT_REQ_MSG = 'Server requires a client certificate.', | ||
CONN_ERR = 'CONN_ERR', | ||
CONN_ERR_MSG_PREFIX = 'Could not connect to server: ', | ||
NATS_PROTOCOL_ERR = 'NATS_PROTOCOL_ERR', | ||
@@ -86,9 +102,36 @@ // Pedantic Mode support | ||
function NatsError(message, code, chainedError) { | ||
Error.captureStackTrace(this, this.constructor); | ||
this.name = this.constructor.name; | ||
this.message = message; | ||
this.code = code; | ||
this.chainedError = chainedError; | ||
} | ||
util.inherits(NatsError, Error); | ||
exports.NatsError = NatsError; | ||
/** | ||
* Library Version | ||
*/ | ||
exports.version = VERSION; | ||
/** | ||
* Error codes | ||
*/ | ||
exports.BAD_SUBJECT = BAD_SUBJECT; | ||
exports.BAD_MSG = BAD_MSG; | ||
exports.BAD_REPLY = BAD_REPLY; | ||
exports.CONN_CLOSED = CONN_CLOSED; | ||
exports.BAD_JSON = BAD_JSON; | ||
exports.BAD_AUTHENTICATION = BAD_AUTHENTICATION; | ||
exports.INVALID_ENCODING = INVALID_ENCODING; | ||
exports.SECURE_CONN_REQ = SECURE_CONN_REQ; | ||
exports.NON_SECURE_CONN_REQ = NON_SECURE_CONN_REQ; | ||
exports.CLIENT_CERT_REQ = CLIENT_CERT_REQ; | ||
exports.NATS_PROTOCOL_ERR = NATS_PROTOCOL_ERR; | ||
/** | ||
* Create a properly formatted inbox subject. | ||
@@ -218,2 +261,3 @@ * | ||
this.assignOption(opts, 'json'); | ||
this.assignOption(opts, 'preserveBuffers'); | ||
} | ||
@@ -232,3 +276,3 @@ | ||
if (client.user && client.token) { | ||
throw(new Error(BAD_AUTHENTICATION)); | ||
throw(new NatsError(BAD_AUTHENTICATION_MSG, BAD_AUTHENTICATION)); | ||
} | ||
@@ -240,3 +284,3 @@ | ||
} else { | ||
throw new Error('Invalid Encoding:' + options.encoding); | ||
throw new NatsError(INVALID_ENCODING_MSG_PREFIX + options.encoding, INVALID_ENCODING); | ||
} | ||
@@ -318,3 +362,3 @@ // For cluster support | ||
this.options.tls === false) { | ||
this.emit('error', 'Server requires a secure connection.'); | ||
this.emit('error', new NatsError(SECURE_CONN_REQ_MSG, SECURE_CONN_REQ)); | ||
this.closeStream(); | ||
@@ -326,3 +370,3 @@ return true; | ||
this.options.tls !== false) { | ||
this.emit('error', 'Server does not support a secure connection.'); | ||
this.emit('error', new NatsError(NON_SECURE_CONN_REQ_MSG, NON_SECURE_CONN_REQ)); | ||
this.closeStream(); | ||
@@ -334,3 +378,3 @@ return true; | ||
this.options.tls.cert === undefined) { | ||
this.emit('error', 'Server requires a client certificate.'); | ||
this.emit('error', new NatsError(CLIENT_CERT_REQ_MSG, CLIENT_CERT_REQ)); | ||
this.closeStream(); | ||
@@ -415,3 +459,3 @@ return true; | ||
if (client.wasConnected === false && client.servers.length === 0) { | ||
client.emit('error', 'Could not connect to server: ' + exception); | ||
client.emit('error', new NatsError(CONN_ERR_MSG_PREFIX + exception, CONN_ERR, exception)); | ||
} | ||
@@ -752,3 +796,3 @@ client.closeStream(); | ||
} else if ((m = ERR.exec(buf)) !== null) { | ||
client.emit('error', m[1]); | ||
client.emit('error', new NatsError(m[1], NATS_PROTOCOL_ERR)); | ||
} else if ((m = PONG.exec(buf)) !== null) { | ||
@@ -821,7 +865,20 @@ var cb = client.pongs && client.pongs.shift(); | ||
if (client.payload.chunks) { | ||
client.payload.chunks.push(client.inbound.slice(0, client.payload.psize)); | ||
var mbuf = Buffer.concat(client.payload.chunks, client.payload.size+CR_LF_LEN); | ||
client.payload.msg = mbuf.toString(client.encoding, 0, client.payload.size); | ||
client.payload.chunks.push(client.inbound.slice(0, client.payload.psize)); | ||
var mbuf = Buffer.concat(client.payload.chunks, client.payload.size + CR_LF_LEN); | ||
if (client.options.preserveBuffers) { | ||
client.payload.msg = mbuf; | ||
} else { | ||
client.payload.msg = mbuf.toString(client.encoding, 0, client.payload.size); | ||
} | ||
} else { | ||
client.payload.msg = client.inbound.toString(client.encoding, 0, client.payload.size); | ||
if (client.options.preserveBuffers) { | ||
client.payload.msg = client.inbound.slice(0, client.payload.size); | ||
} else { | ||
client.payload.msg = client.inbound.toString(client.encoding, 0, client.payload.size); | ||
} | ||
} | ||
@@ -900,3 +957,7 @@ | ||
try { | ||
msg = JSON.parse(new Buffer(this.payload.msg, this.options.encoding).toString()); | ||
if (this.options.preserveBuffers) { | ||
msg = JSON.parse(this.payload.msg.toString()); | ||
} else { | ||
msg = JSON.parse(this.payload.msg.toString(this.options.encoding)); | ||
} | ||
} catch (e) { | ||
@@ -937,6 +998,6 @@ msg = e; | ||
if (typeof opt_callback === 'function') { | ||
opt_callback(new Error(CONN_CLOSED)); | ||
opt_callback(new NatsError(CONN_CLOSED_MSG, CONN_CLOSED)); | ||
return; | ||
} else { | ||
throw(new Error(CONN_CLOSED)); | ||
throw(new NatsError(CONN_CLOSED_MSG, CONN_CLOSED)); | ||
} | ||
@@ -970,5 +1031,5 @@ } | ||
if (opt_callback) { | ||
opt_callback(new Error(BAD_SUBJECT)); | ||
opt_callback(new NatsError(BAD_SUBJECT_MSG, BAD_SUBJECT)); | ||
} else { | ||
throw(new Error(BAD_SUBJECT)); | ||
throw(new NatsError(BAD_SUBJECT_MSG, BAD_SUBJECT)); | ||
} | ||
@@ -978,3 +1039,3 @@ } | ||
if (opt_callback || opt_reply) { | ||
opt_callback(new Error(BAD_MSG)); | ||
opt_callback(new NatsError(BAD_MSG_MSG, BAD_MSG)); | ||
return; | ||
@@ -988,3 +1049,3 @@ } | ||
if (opt_callback) { | ||
opt_callback(new Error(BAD_REPLY)); | ||
opt_callback(new NatsError(BAD_REPLY_MSG, BAD_REPLY)); | ||
return; | ||
@@ -1009,3 +1070,3 @@ } | ||
if (typeof msg !== 'object' || Array.isArray(msg)) { | ||
throw(new Error(BAD_JSON_MSG)); | ||
throw(new NatsError(BAD_JSON_MSG, BAD_JSON)); | ||
} | ||
@@ -1015,3 +1076,3 @@ try { | ||
} catch (e) { | ||
throw(new Error(BAD_JSON_MSG)); | ||
throw(new NatsError(BAD_JSON_MSG, BAD_JSON)); | ||
} | ||
@@ -1031,3 +1092,3 @@ } | ||
} else if (this.closed) { | ||
throw(new Error(CONN_CLOSED)); | ||
throw(new NatsError(CONN_CLOSED_MSG, CONN_CLOSED)); | ||
} | ||
@@ -1049,3 +1110,3 @@ }; | ||
if (this.closed) { | ||
throw(new Error(CONN_CLOSED)); | ||
throw(new NatsError(CONN_CLOSED_MSG, CONN_CLOSED)); | ||
} | ||
@@ -1052,0 +1113,0 @@ var qgroup, max; |
{ | ||
"name": "nats", | ||
"version": "0.6.8", | ||
"version": "0.7.2", | ||
"description": "Node.js client for NATS, a lightweight, high-performance cloud native messaging system", | ||
@@ -32,3 +32,3 @@ "keywords": [ | ||
"scripts": { | ||
"lint": "jshint --reporter node_modules/jshint-stylish lib/*.js test/*.js test/support/*.js examples/*.js", | ||
"lint": "jshint --reporter node_modules/jshint-stylish lib test examples", | ||
"depcheck": "dependency-check . lib/*", | ||
@@ -35,0 +35,0 @@ "depcheck:unused": "dependency-check ./package.json --unused --no-dev lib/*", |
@@ -202,2 +202,11 @@ # NATS - Node.js Client | ||
// PreserveBuffers | ||
// To prevent payload conversion from a Buffer to a string, set the | ||
// preserveBuffers option to true. Message payload return will be a Buffer. | ||
nc = nats.connect({'preserveBuffers': true); | ||
``` | ||
@@ -204,0 +213,0 @@ |
@@ -80,2 +80,17 @@ /* jslint node: true */ | ||
it ('NatsErrors have code', function() { | ||
var err = new NATS.NatsError("hello", "helloid"); | ||
should.equal(err.message, 'hello'); | ||
should.equal(err.code, 'helloid'); | ||
}); | ||
it ('NatsErrors can chain an error', function() { | ||
var srcErr = new Error('foo'); | ||
var err = new NATS.NatsError("hello", "helloid", srcErr); | ||
should.equal(err.message, 'hello'); | ||
should.equal(err.code, 'helloid'); | ||
should.equal(err.chainedError, srcErr); | ||
should.equal(err.name, 'NatsError'); | ||
}); | ||
}); |
@@ -33,3 +33,3 @@ /* jslint node: true */ | ||
clearTimeout(timer); | ||
delta.should.within(10, 20); | ||
delta.should.within(10, 25); | ||
done(); | ||
@@ -36,0 +36,0 @@ }, 10); |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
126896
44
3440
239
4