Comparing version 2.0.0-14 to 2.0.0-15
102
lib/nats.js
@@ -142,2 +142,18 @@ /* | ||
const Events = {} | ||
Events.Error = 'error' | ||
Events.PublishPermissionError = 'pubError' | ||
Events.Subscribe = 'subscribe' | ||
Events.Unsubscribe = 'unsubscribe' | ||
Events.Reconnecting = 'reconnecting' | ||
Events.Close = 'close' | ||
Events.Reconnect = 'reconnect' | ||
Events.Connect = 'connect' | ||
Events.Disconnect = 'disconnect' | ||
const InternalEvents = {} | ||
InternalEvents.PingTimer = 'pingtimer' | ||
InternalEvents.PingCount = 'pingcount' | ||
InternalEvents.Servers = 'servers' | ||
/** | ||
@@ -449,3 +465,3 @@ * @param {String} message | ||
if ((this.info.proto === undefined || this.info.proto < 1) && this.options.noEcho) { | ||
this.emit('error', new NatsError(NO_ECHO_NOT_SUPPORTED_MSG, ErrorCode.NO_ECHO_NOT_SUPPORTED)) | ||
this.emit(Events.Error, new NatsError(NO_ECHO_NOT_SUPPORTED_MSG, ErrorCode.NO_ECHO_NOT_SUPPORTED)) | ||
this.closeStream() | ||
@@ -473,3 +489,3 @@ return true | ||
this.options.tls === false) { | ||
this.emit('error', new NatsError(SECURE_CONN_REQ_MSG, ErrorCode.SECURE_CONN_REQ)) | ||
this.emit(Events.Error, new NatsError(SECURE_CONN_REQ_MSG, ErrorCode.SECURE_CONN_REQ)) | ||
this.closeStream() | ||
@@ -480,3 +496,3 @@ return true | ||
if (!this.info.tls_required && this.options.tls !== false) { | ||
this.emit('error', new NatsError(NON_SECURE_CONN_REQ_MSG, ErrorCode.NON_SECURE_CONN_REQ)) | ||
this.emit(Events.Error, new NatsError(NON_SECURE_CONN_REQ_MSG, ErrorCode.NON_SECURE_CONN_REQ)) | ||
this.closeStream() | ||
@@ -488,3 +504,3 @@ return true | ||
this.options.tls.cert === undefined) { | ||
this.emit('error', new NatsError(CLIENT_CERT_REQ_MSG, ErrorCode.CLIENT_CERT_REQ)) | ||
this.emit(Events.Error, new NatsError(CLIENT_CERT_REQ_MSG, ErrorCode.CLIENT_CERT_REQ)) | ||
this.closeStream() | ||
@@ -506,3 +522,3 @@ return true | ||
if (m === null) { | ||
this.emit('error', new NatsError(NO_USER_JWT_IN_CREDS_MSG, ErrorCode.NO_USER_JWT_IN_CREDS)) | ||
this.emit(Events.Error, new NatsError(NO_USER_JWT_IN_CREDS_MSG, ErrorCode.NO_USER_JWT_IN_CREDS)) | ||
this.closeStream() | ||
@@ -526,3 +542,3 @@ return | ||
if (m === null) { | ||
this.emit('error', new NatsError(NO_SEED_IN_CREDS_MSG, ErrorCode.NO_SEED_IN_CREDS)) | ||
this.emit(Events.Error, new NatsError(NO_SEED_IN_CREDS_MSG, ErrorCode.NO_SEED_IN_CREDS)) | ||
this.closeStream() | ||
@@ -569,3 +585,3 @@ return | ||
if (CREDS.exec(contents) === null) { | ||
this.emit('error', new NatsError(BAD_CREDS_MSG, ErrorCode.BAD_CREDS)) | ||
this.emit(Events.Error, new NatsError(BAD_CREDS_MSG, ErrorCode.BAD_CREDS)) | ||
this.closeStream() | ||
@@ -586,3 +602,3 @@ return true | ||
if (undefined === this.options.nonceSigner) { | ||
this.emit('error', new NatsError(SIGNATURE_REQUIRED_MSG, ErrorCode.SIGNATURE_REQUIRED)) | ||
this.emit(Events.Error, new NatsError(SIGNATURE_REQUIRED_MSG, ErrorCode.SIGNATURE_REQUIRED)) | ||
this.closeStream() | ||
@@ -592,3 +608,3 @@ return true | ||
if (typeof (this.options.nonceSigner) !== 'function') { | ||
this.emit('error', new NatsError(SIGCB_NOTFUNC_MSG, ErrorCode.SIGCB_NOTFUNC)) | ||
this.emit(Events.Error, new NatsError(SIGCB_NOTFUNC_MSG, ErrorCode.SIGCB_NOTFUNC)) | ||
this.closeStream() | ||
@@ -598,3 +614,3 @@ return true | ||
if (undefined === this.options.nkey && undefined === this.options.userJWT) { | ||
this.emit('error', new NatsError(NKEY_OR_JWT_REQ_MSG, ErrorCode.NKEY_OR_JWT_REQ)) | ||
this.emit(Events.Error, new NatsError(NKEY_OR_JWT_REQ_MSG, ErrorCode.NKEY_OR_JWT_REQ)) | ||
this.closeStream() | ||
@@ -613,3 +629,3 @@ return true | ||
const wasReconnecting = this.reconnecting | ||
const event = (wasReconnecting === true) ? 'reconnect' : 'connect' | ||
const event = (wasReconnecting === true) ? Events.Reconnect : Events.Connect | ||
this.reconnecting = false | ||
@@ -640,3 +656,3 @@ this.reconnects = 0 | ||
this.pingTimer = setTimeout(function (client) { | ||
client.emit('pingtimer') | ||
client.emit(InternalEvents.PingTimer) | ||
if (client.closed) { | ||
@@ -647,3 +663,3 @@ return | ||
if (client.stream && !client.stream.connecting) { | ||
client.emit('pingcount', client.pout) | ||
client.emit(InternalEvents.PingCount, client.pout) | ||
client.pout++ | ||
@@ -718,7 +734,7 @@ if (client.pout > client.options.maxPingOut) { | ||
} | ||
this.emit('disconnect') | ||
this.emit(Events.Disconnect) | ||
} | ||
if (done) { | ||
this.cleanupTimers() | ||
this.emit('close') | ||
this.emit(Events.Close) | ||
} else { | ||
@@ -752,3 +768,3 @@ this.scheduleReconnect() | ||
if (this.wasConnected === false && this.servers.length === 0) { | ||
this.emit('error', new NatsError(CONN_ERR_MSG_PREFIX + exception, ErrorCode.CONN_ERR, exception)) | ||
this.emit(Events.Error, new NatsError(CONN_ERR_MSG_PREFIX + exception, ErrorCode.CONN_ERR, exception)) | ||
this.close() | ||
@@ -814,3 +830,3 @@ return | ||
} catch (err) { | ||
this.emit('error', new NatsError(BAD_AUTHENTICATION_TH_FAILED_MSG_PREFIX + err, ErrorCode.BAD_AUTHENTICATION, err)) | ||
this.emit(Events.Error, new NatsError(BAD_AUTHENTICATION_TH_FAILED_MSG_PREFIX + err, ErrorCode.BAD_AUTHENTICATION, err)) | ||
} | ||
@@ -1179,3 +1195,3 @@ cs.auth_token = token | ||
} catch (error) { | ||
this.emit('error', new NatsError(OPENSSL_ERR_MSG_PREFIX + error, ErrorCode.OPENSSL_ERR, error)) | ||
this.emit(Events.Error, new NatsError(OPENSSL_ERR_MSG_PREFIX + error, ErrorCode.OPENSSL_ERR, error)) | ||
return | ||
@@ -1339,6 +1355,3 @@ } | ||
if (newURLs.length) { | ||
// new reported servers useful for tests | ||
this.emit('serversDiscovered', newURLs) | ||
// simpler version | ||
this.emit('servers', newURLs) | ||
this.emit(InternalEvents.Servers, newURLs) | ||
} | ||
@@ -1369,3 +1382,3 @@ } | ||
delete this.subs[this.payload.sid] | ||
this.emit('unsubscribe', { sid: this.payload.sid, subject: sub.subject, queue: sub.queue }) | ||
this.emit(Events.Unsubscribe, { sid: this.payload.sid, subject: sub.subject, queue: sub.queue }) | ||
} else if (sub.received > sub.max) { | ||
@@ -1395,3 +1408,3 @@ this.unsubscribe(this.payload.sid) | ||
} catch (error) { | ||
this.emit('error', error) | ||
this.emit(Events.Error, error) | ||
} | ||
@@ -1416,10 +1429,10 @@ } | ||
} else if (m.indexOf(ErrorCode.PERMISSIONS_ERR) !== -1) { | ||
this.emit('permission_error', new NatsError(s, ErrorCode.NATS_PROTOCOL_ERR)) | ||
this.dispatchPermissionError(s) | ||
return false | ||
} else if (m.indexOf('authorization violation') !== -1) { | ||
this.emit('error', new NatsError(s, ErrorCode.BAD_AUTHENTICATION)) | ||
this.emit(Events.Error, new NatsError(s, ErrorCode.BAD_AUTHENTICATION)) | ||
this.closeStream() | ||
return true | ||
} else { | ||
this.emit('error', new NatsError(s, ErrorCode.NATS_PROTOCOL_ERR)) | ||
this.emit(Events.Error, new NatsError(s, ErrorCode.NATS_PROTOCOL_ERR)) | ||
this.closeStream() | ||
@@ -1430,2 +1443,25 @@ return true | ||
Client.prototype.dispatchPermissionError = function (srvError) { | ||
const err = new NatsError(srvError, ErrorCode.PERMISSIONS_ERR) | ||
const m = srvError.match(/^'Permissions Violation for (Subscription|Publish) to "(.+)"'/) | ||
if (m) { | ||
switch (m[1]) { | ||
case 'Subscription': | ||
for (const p in this.subs) { | ||
if (Object.hasOwnProperty.call(this.subs, p)) { | ||
const sub = this.subs[p] | ||
if (sub.subject === m[2]) { | ||
sub.callback(err) | ||
sub.s.unsubscribe() | ||
} | ||
} | ||
} | ||
break | ||
case 'Publish': | ||
this.emit(Events.PublishPermissionError, err) | ||
break | ||
} | ||
} | ||
} | ||
/** | ||
@@ -1708,3 +1744,3 @@ * Push a new cluster server. | ||
this.sendCommand(proto.join(SPC)) | ||
this.emit('subscribe', { sid: this.ssid, subject: subject, queue: opts.queue }) | ||
this.emit(Events.Subscribe, { sid: this.ssid, subject: subject, queue: opts.queue }) | ||
if (opts.max) { | ||
@@ -1756,3 +1792,3 @@ this.unsubscribe(this.ssid, opts.max) | ||
delete this.subs[sid] | ||
this.emit('unsubscribe', { sid: this.ssid, subject: sub.subject, queue: sub.queue }) | ||
this.emit(Events.Unsubscribe, { sid: this.ssid, subject: sub.subject, queue: sub.queue }) | ||
} | ||
@@ -1796,3 +1832,3 @@ } | ||
} | ||
this.emit('unsubscribe', { sid: this.ssid, subject: sub.subject, queue: sub.queue }) | ||
this.emit(Events.Unsubscribe, { sid: this.ssid, subject: sub.subject, queue: sub.queue }) | ||
}) | ||
@@ -1883,3 +1919,3 @@ } | ||
this.cancelMuxRequest(token) | ||
this.emit('unsubscribe', { sid: sid, subject: msg.subject }) | ||
this.emit(Events.Unsubscribe, { sid: sid, subject: msg.subject }) | ||
} | ||
@@ -1993,3 +2029,3 @@ } | ||
if (this.currentServer.didConnect === true) { | ||
this.emit('reconnecting') | ||
this.emit(Events.Reconnecting) | ||
} | ||
@@ -2051,3 +2087,3 @@ this.currentServer.lastConnect = Date.now() | ||
this.cleanupTimers() | ||
this.emit('close') | ||
this.emit(Events.Close) | ||
this.close() | ||
@@ -2054,0 +2090,0 @@ return |
{ | ||
"name": "nats", | ||
"version": "2.0.0-14", | ||
"version": "2.0.0-15", | ||
"description": "Node.js client for NATS, a lightweight, high-performance cloud native messaging system", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -43,3 +43,9 @@ # NATS.js - Node.js Client | ||
// Unsubscribing | ||
const sub = nc.subscribe('foo', (err, m) => {}) | ||
const sub = nc.subscribe('foo', (err, m) => { | ||
if(!err) { | ||
console.log(m) | ||
} else { | ||
console.error(err) | ||
} | ||
}) | ||
sub.unsubscribe() | ||
@@ -452,6 +458,7 @@ | ||
// emitted whenever the server returns a permission error for | ||
// a publish/subscription for the current user. This sort of error | ||
// means that the client cannot subscribe and/or publish/request | ||
// on the specific subject | ||
nc.on('permission_error', (err) => { | ||
// a publish for the current user. This sort of error | ||
// means that the client cannot publish/request | ||
// on the specific subject. Note that subscription permission | ||
// errors are delivered to the subscription's handler | ||
nc.on(pubError, (err) => { | ||
console.error('got a permissions error', err.message) | ||
@@ -458,0 +465,0 @@ }) |
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
117565
2370
534