Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: err handling #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ module.exports = {
],
"no-underscore-dangle": [
"error",
{
"allow": ["_id"]
}
"never"
],
"indent": [
"error",
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
.vscode/
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ Transporter.publish('message 1', function(){

- [Google PubSub](https://github.com/myDevicesIoT/pubsub-transporter)
- [RabbitMQ](https://github.com/myDevicesIoT/rabbit-transporter)

### Testing

Install all dependencies and use `npm test`
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const transporter = exports;


transporter.Transport = require('./lib/transport').Transport;
transporter.Publisher = require('./lib/publisher').Publisher;
transporter.Redis = require('./lib/redis').Redis;
Expand All @@ -17,7 +16,8 @@ const methods = [
'add',
'remove',
'handleExceptions',
'unhandleExceptions'
'unhandleExceptions',
'on'
];

methods.forEach((method) => {
Expand Down
13 changes: 12 additions & 1 deletion lib/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,19 @@ Publisher.prototype.add = function func(transport, options, created) {
return this;
};

Publisher.prototype.remove = function(transportName) {
if (!this.transports[transportName]) {
throw new Error(`Transport not attached: ${transportName}`);
}

delete this.transports[transportName];
this._names = Object.keys(this.transports);

return this;
};

Publisher.prototype._onError = function func(transport, err) {
if (this.emitErrs) {
if (transport.options && transport.options.emitErrs) {
this.emit('error', err, transport);
}
};
24 changes: 11 additions & 13 deletions lib/redis.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
const _ = require('lodash');
const util = require('util');
const redis = require('redis');
const { Transport } = require('./transport');

const Redis = function func(options) {
Transport.call(this, options);

this.options = options || {
this.options = Object.assign({
host: 'localhost',
port: 6379,
password: null,
channel: 'data-change'
};
}, options)

this.redisClient = redis.createClient(_.pick(this.options, ['host', 'port', 'password']));
let redisOpts = {
host: this.options.host,
port: this.options.port,
password: this.options.password
}

this.redisClient = redis.createClient(redisOpts);
};

util.inherits(Redis, Transport);
Expand All @@ -25,24 +30,17 @@ Redis.prototype.name = 'Redis';
Redis.prototype.publish = function func(msg, callback) {
const self = this;
const str = JSON.stringify(msg);
let eventType = self.options.eventType;

if (msg.eventType) {
eventType = msg.eventType;
}

const bus = msg.bus;
if (_.isNil(bus)) {
console.error('Transporter: No bus specified');
if (!bus) {
this.emit('error', new Error('No bus specified'));
return callback();
}

return self.redisClient.GETBIT(msg.clientId, 0, (err, result) => {
if (result === 0) {
//console.info('Transporter: No clients connected, not publishing message');
return callback();
}
//console.log(`Transporter: Pub to ${eventType} stream ${self.name} - ${str}`);
return self.redisClient.publish(bus, str, () => callback());
});
};
Expand Down
Loading