node-nats-streaming-buffered-client
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -55,9 +55,11 @@ "use strict"; | ||
*/ | ||
function NatsBufferedClient(bufferSize, reconnectTimeout, waitForInitialConnection) { | ||
function NatsBufferedClient(bufferSize, reconnectTimeout, waitForInitialConnection, logger) { | ||
if (bufferSize === void 0) { bufferSize = 10; } | ||
if (reconnectTimeout === void 0) { reconnectTimeout = 30000; } | ||
if (waitForInitialConnection === void 0) { waitForInitialConnection = false; } | ||
if (logger === void 0) { logger = console; } | ||
var _this = this; | ||
this.reconnectTimeout = reconnectTimeout; | ||
this.waitForInitialConnection = waitForInitialConnection; | ||
this.logger = logger; | ||
/** | ||
@@ -95,3 +97,3 @@ * Indicates if we're processing the buffer | ||
case 0: | ||
console.log('[NATS-BUFFERED-CLIENT] EXIT encountered'); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] EXIT encountered'); | ||
_a.label = 1; | ||
@@ -103,7 +105,7 @@ case 1: | ||
_a.sent(); | ||
console.log('[NATS-BUFFERED-CLIENT] Disconnected due to EXIT'); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] Disconnected due to EXIT'); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
error_1 = _a.sent(); | ||
console.error('[NATS-BUFFERED-CLIENT] Error during EXIT disconnect', error_1); | ||
this.logger.error('[NATS-BUFFERED-CLIENT] Error during EXIT disconnect', error_1); | ||
return [3 /*break*/, 4]; | ||
@@ -121,3 +123,3 @@ case 4: return [2 /*return*/]; | ||
case 0: | ||
console.log('[NATS-BUFFERED-CLIENT] SIGINT encountered'); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] SIGINT encountered'); | ||
// Stop any pending reconnect timers | ||
@@ -132,3 +134,3 @@ // | ||
_a.sent(); | ||
console.log('[NATS-BUFFERED-CLIENT] Disconnected due to SIGINT'); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] Disconnected due to SIGINT'); | ||
process.exit(); | ||
@@ -138,3 +140,3 @@ return [3 /*break*/, 4]; | ||
error_2 = _a.sent(); | ||
console.error('[NATS-BUFFERED-CLIENT] Error during SIGINT disconnect', error_2); | ||
this.logger.error('[NATS-BUFFERED-CLIENT] Error during SIGINT disconnect', error_2); | ||
process.exit(); | ||
@@ -166,3 +168,3 @@ return [3 /*break*/, 4]; | ||
this.clientConnected = newConnectedState; | ||
console.log('[NATS-BUFFERED-CLIENT] Client connected status', this.clientConnected); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] Client connected status', this.clientConnected); | ||
// This timer will try to reconnect to the server on prolonged absence | ||
@@ -172,5 +174,5 @@ // | ||
if (!this.clientConnected) { | ||
console.log('[NATS-BUFFERED-CLIENT] Starting reconnect timer', this.reconnectTimeout); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] Starting reconnect timer', this.reconnectTimeout); | ||
this.reconnectTimer = setInterval(function () { | ||
console.log('[NATS-BUFFERED-CLIENT] Timer triggered reconnect...'); | ||
_this.logger.log('[NATS-BUFFERED-CLIENT] Timer triggered reconnect...'); | ||
_this.reconnect(); | ||
@@ -203,3 +205,3 @@ }, this.reconnectTimeout); | ||
error_3 = _a.sent(); | ||
console.error('[NATS-BUFFERED-CLIENT] Error during disconnect', error_3); | ||
this.logger.error('[NATS-BUFFERED-CLIENT] Error during disconnect', error_3); | ||
return [3 /*break*/, 3]; | ||
@@ -213,3 +215,3 @@ case 3: | ||
// | ||
console.log('[NATS-BUFFERED-CLIENT] Connecting...'); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] Connecting...'); | ||
this.stan = nats.connect(clusterId, clientId, options); | ||
@@ -224,3 +226,3 @@ // Store connection parameters | ||
this.stan.on('connect', function () { | ||
console.log('[NATS-BUFFERED-CLIENT] Connected'); | ||
_this.logger.log('[NATS-BUFFERED-CLIENT] Connected'); | ||
_this.connected = true; | ||
@@ -236,3 +238,3 @@ _this.initialConnected = true; | ||
this.stan.on('error', function (error) { | ||
console.error('[NATS-BUFFERED-CLIENT] Server error', error); | ||
_this.logger.error('[NATS-BUFFERED-CLIENT] Server error', error); | ||
// Reject initial connection promise | ||
@@ -245,9 +247,9 @@ // | ||
this.stan.on('reconnecting', function () { | ||
console.log('[NATS-BUFFERED-CLIENT] Reconnecting'); | ||
_this.logger.log('[NATS-BUFFERED-CLIENT] Reconnecting'); | ||
}); | ||
this.stan.on('reconnect', function () { | ||
console.log('[NATS-BUFFERED-CLIENT] Reconnected'); | ||
_this.logger.log('[NATS-BUFFERED-CLIENT] Reconnected'); | ||
}); | ||
this.stan.on('disconnect', function () { | ||
console.log('[NATS-BUFFERED-CLIENT] Disconnected'); | ||
_this.logger.log('[NATS-BUFFERED-CLIENT] Disconnected'); | ||
}); | ||
@@ -283,3 +285,3 @@ return [2 /*return*/]; | ||
// | ||
console.log('[NATS-BUFFERED-CLIENT] Not connected so no need to disconnect', _this.connected, _this.stan === undefined); | ||
_this.logger.log('[NATS-BUFFERED-CLIENT] Not connected so no need to disconnect', _this.connected, _this.stan === undefined); | ||
resolve(); | ||
@@ -295,3 +297,3 @@ } | ||
NatsBufferedClient.prototype.reconnect = function () { | ||
console.log('[NATS-BUFFERED-CLIENT] Reconnecting...'); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] Reconnecting...'); | ||
this.connect(this.clusterId, this.clientId, this.clientOptions); | ||
@@ -316,3 +318,3 @@ }; | ||
this.buffer.push({ subject: subject, data: data }); | ||
console.log('[NATS-BUFFERED-CLIENT] Added message to buffer', subject, data); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] Added message to buffer', subject, data); | ||
// Resume buffer processing if needed | ||
@@ -337,3 +339,3 @@ // | ||
// | ||
console.log('[NATS-BUFFERED-CLIENT] Buffer is full. Dropping data:', data); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] Buffer is full. Dropping data:', data); | ||
}; | ||
@@ -356,11 +358,11 @@ /** | ||
if (error) { | ||
console.error('[NATS-BUFFERED-CLIENT] Publish failed', error); | ||
_this.logger.error('[NATS-BUFFERED-CLIENT] Publish failed', error); | ||
_this.buffer.unshift(pub_1); | ||
// Reconnect to the server on publish error | ||
// | ||
console.warn('[NATS-BUFFERED-CLIENT] Reconnect to server due to publish error'); | ||
_this.logger.warn('[NATS-BUFFERED-CLIENT] Reconnect to server due to publish error'); | ||
_this.reconnect(); | ||
} | ||
else { | ||
console.log('[NATS-BUFFERED-CLIENT] Publish done', pub_1); | ||
_this.logger.log('[NATS-BUFFERED-CLIENT] Publish done', pub_1); | ||
// If we can publish we are connected | ||
@@ -376,3 +378,3 @@ // | ||
else { | ||
console.log('[NATS-BUFFERED-CLIENT] Buffer is empty. Going to sleep'); | ||
this.logger.log('[NATS-BUFFERED-CLIENT] Buffer is empty. Going to sleep'); | ||
this.ticking = false; | ||
@@ -382,3 +384,3 @@ } | ||
else { | ||
console.warn('[NATS-BUFFERED-CLIENT] Buffer tick called when not connected'); | ||
this.logger.warn('[NATS-BUFFERED-CLIENT] Buffer tick called when not connected'); | ||
this.ticking = false; | ||
@@ -385,0 +387,0 @@ } |
@@ -0,1 +1,2 @@ | ||
/// <reference types="node" /> | ||
import * as nats from 'node-nats-streaming'; | ||
@@ -11,2 +12,3 @@ /** | ||
private waitForInitialConnection; | ||
private logger; | ||
/** | ||
@@ -102,3 +104,3 @@ * The connection to the NATS server | ||
*/ | ||
constructor(bufferSize?: number, reconnectTimeout?: number, waitForInitialConnection?: boolean); | ||
constructor(bufferSize?: number, reconnectTimeout?: number, waitForInitialConnection?: boolean, logger?: Console); | ||
/** | ||
@@ -105,0 +107,0 @@ * Connect to the NATS server |
{ | ||
"name": "node-nats-streaming-buffered-client", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -30,5 +30,13 @@ # NATS Streaming Buffered Client | ||
// | ||
// You can instruct the client to wait for the initial connect to succeed before | ||
// allowing any kind of publishing. | ||
// | ||
// You can also provide an alternate logger if you want to use something | ||
// like bunyan. The same interface as console is asumed | ||
// | ||
const bufferSize = 2000; | ||
const reconnectTimeout = 30000; | ||
const client = new NatsBufferedClient( bufferSize, reconnectTimeout ); | ||
const waitForInitialConnect = false; | ||
const logger = console; | ||
const client = new NatsBufferedClient( bufferSize, reconnectTimeout, waitForInitialConnect, logger ); | ||
@@ -35,0 +43,0 @@ // Connect to the NATS server |
Sorry, the diff of this file is not supported yet
32364
519
61