@betit/orion-node-sdk
Version:
SDK for orion
154 lines • 4.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const nats = require("nats");
const utils_1 = require("../utils");
const DEBUG = utils_1.debugLog('orion:transport:nats');
/**
* NATS transport.
*/
class NatsTransport {
/**
* Create new NATS transport.
*/
constructor(_options) {
this._options = _options;
this._options = Object.assign({
url: process.env.NATS || 'nats://localhost:4222',
encoding: 'binary'
}, _options);
this._client = nats.connect(this._options);
this._client.on('error', function (e) {
DEBUG(e);
process.exit(1);
});
this._client.on('connect', () => DEBUG('nats initial connect'));
}
/**
* Transport listen.
*/
listen(callback) {
try {
this._client.flush((err) => {
if (err) {
throw err;
}
if (callback && callback instanceof Function) {
callback();
}
});
}
catch (e) {
if (e.code === nats.CONN_CLOSED && this._closeHandler) {
this._closeHandler();
}
throw e;
}
}
/**
* Publish to a topic.
*/
publish(topic, message) {
try {
this._client.publish(topic, message);
}
catch (e) {
if (e.code === nats.CONN_CLOSED && this._closeHandler) {
this._closeHandler();
}
throw e;
}
}
/**
* Subscribe to a topic.
*/
subscribe(topic, group, callback) {
try {
return this._client.subscribe(topic, { queue: group }, callback);
}
catch (e) {
if (e.code === nats.CONN_CLOSED && this._closeHandler) {
this._closeHandler();
}
throw e;
}
}
/**
* Unsubscribe from a topic.
*/
unsubscribe(sid, max) {
try {
this._client.unsubscribe(sid, max);
}
catch (e) {
if (e.code === nats.CONN_CLOSED && this._closeHandler) {
this._closeHandler();
}
throw e;
}
}
/**
* Transport handle.
*/
handle(route, group, callback) {
try {
DEBUG('register handler:', route);
this._client.subscribe(route, { queue: group }, (req, replyTo) => {
DEBUG('incoming request:', req, replyTo);
callback(req, res => {
DEBUG('sending response:', replyTo, res);
this._client.publish(replyTo, res);
});
});
}
catch (e) {
if (e.code === nats.CONN_CLOSED && this._closeHandler) {
this._closeHandler();
}
throw e;
}
}
/**
* Client request.
* Nats client implements this pattern out of the box.
* Publish a request with an implicit inbox listener as the response.
* This should be treated as a subscription.
*/
request(route, payload, callback, timeout = 200) {
try {
DEBUG('sending request:', route);
const SID = this._client.request(route, payload, { max: 1 }, response => {
DEBUG('got response:', response);
callback(response);
});
this._client.timeout(SID, timeout, 1, () => {
DEBUG('request timeout:', route);
callback(new Error(`Transport timeout: ${route}`));
});
}
catch (e) {
if (e.code === nats.CONN_CLOSED && this._closeHandler) {
this._closeHandler();
}
throw e;
}
}
/**
* Close connection.
*/
close() {
this._client.close();
}
/**
* Connection closed handler
* @param {(...args: any[]) => void} callback
*/
onClose(callback) {
if (callback) {
this._closeHandler = callback;
this._client.on('close', callback);
this._client.on('disconnect', callback);
}
}
}
exports.NatsTransport = NatsTransport;
//# sourceMappingURL=nats.js.map