Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Stan.prototype.subscribe = function(subject, qGroup, options) {
const args = {};
if (typeof qGroup === 'string') {
args.qGroup = qGroup;
} else if (typeof qGroup === 'object') {
args.options = qGroup;
}
if (typeof options === 'object') {
args.options = options;
}
if (!args.options) {
args.options = new SubscriptionOptions();
}
// in node-nats there's no Subscription object...
const retVal = new Subscription(this, subject, args.qGroup, nats.createInbox(), args.options, args.callback);
if (typeof subject !== 'string' || subject.length === 0) {
process.nextTick(() => {
retVal.emit('error', new Error(BAD_SUBJECT));
});
return retVal;
}
if (this.isClosed()) {
process.nextTick(() => {
retVal.emit('error', new Error(CONN_CLOSED));
});
return retVal;
}
this.subMap[retVal.inbox] = retVal;
this.nc.on('connect', () => {
// heartbeat processing
const hbInbox = nats.createInbox();
this.hbSubscription = this.nc.subscribe(hbInbox, (msg, reply) => {
this.nc.publish(reply);
});
this.pingInbox = nats.createInbox();
this.pingSubscription = this.nc.subscribe(this.pingInbox, (msg) => {
if (msg) {
const pingResponse = proto.pb.PingResponse.deserializeBinary(Buffer.from(msg, 'binary'));
const err = pingResponse.getError();
if (err) {
this.closeWithError('connection_lost', err);
return;
}
}
this.pingOut = 0;
});
this.ackSubscription = this.nc.subscribe(this.ackSubject, this.processAck());
const discoverSubject = this.options.discoverPrefix + '.' + this.clusterID;
//noinspection JSUnresolvedFunction
this.nc.on('connect', () => {
// heartbeat processing
const hbInbox = nats.createInbox();
this.hbSubscription = this.nc.subscribe(hbInbox, (msg, reply) => {
this.nc.publish(reply);
});
this.pingInbox = nats.createInbox();
this.pingSubscription = this.nc.subscribe(this.pingInbox, (msg) => {
if (msg) {
const pingResponse = proto.pb.PingResponse.deserializeBinary(Buffer.from(msg, 'binary'));
const err = pingResponse.getError();
if (err) {
this.closeWithError('connection_lost', err);
return;
}
}
this.pingOut = 0;
});