-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
211 lines (185 loc) · 6.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright IBM Corp. 2015. All Rights Reserved.
// Node module: strong-pubsub-bridge
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
module.exports = Bridge;
var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits;
var debug = require('debug')('strong-pubsub-bridge');
var debugAction = require('debug')('strong-pubsub-bridge:action');
/**
* Forward the events of a `connection` using the provided `client`. Also
* forward the events of the provided `client` to the `connection`.
*
* **Example**
*
* ```js
* var net = require('net');
* var server = net.createServer();
*
* var Adapter = require('strong-pubsub-mqtt');
* var client = new Client('mqtt://my.mosquitto.org', Adapter);
* var Connection = require('strong-pubsub-connection-mqtt');
*
* server.on('connection', function(connection) {
* var mqttConnection = new Connection(connection);
* var bridge = new Bridge(mqttConnection, client);
* });
* ```
*
* @prop {Connection} connection The `Connection` instance provided to the `Bridge` constructor.
* @prop {Client} client The `Client` instance provided to the `Bridge` constructor.
* @prop {Object[]} hooks An array hook objects.
* @class
*/
function Bridge(connection, client) {
EventEmitter.call(this);
var bridge = this;
this.connection = connection;
this.client = client;
var hooks = this.hooks = {};
}
inherits(Bridge, EventEmitter);
Bridge.actions = ['connect', 'publish', 'subscribe', 'unsubscribe'];
/**
* Connect the bridge to the broker using the provided `client` and `connection`.
*/
Bridge.prototype.connect = function() {
var bridge = this;
var hooks = this.hooks;
var client = this.client;
var connection = this.connection;
client.on('message', function(topic, message, options) {
debug('message received from topic: %s', topic);
connection.publish(topic, message, options);
});
Bridge.actions.forEach(function(action) {
hooks[action] = hooks[action] || [];
connection.on(action, function(ctx) {
debugAction(action + ' %j', ctx);
bridge.trigger(action, ctx, function(err) {
ctx.error = err;
if(action === 'connect') {
return done();
}
if(err) {
return connection.onError(action, ctx, err);
}
if(ctx.authorized === false || ctx.reject) {
return done();
}
switch(action) {
case 'publish':
client.publish(ctx.topic, ctx.message, ctx.options, done);
break;
case 'subscribe':
client.subscribe(ctx.subscriptions || ctx.topic, ctx.options, done);
break;
case 'unsubscribe':
client.unsubscribe(ctx.unsubscriptions || ctx.topic, done);
break;
}
});
function done(err) {
if(err) {
// error interacting with broker
error(err);
client.end();
}
connection.ack(action, ctx, function(err) {
if(err) {
// error sending ack
debug('closing connection');
connection.close();
error(err);
}
});
}
});
});
client.on('error', error);
connection.on('error', error);
function error(err) {
bridge.emit('error', err);
}
}
/**
* Add a `hook` function before the given `action` is executed.
*
* @param {String} action Must be one of the following:
*
* - `connect`
* - `publish`
* - `subscribe`
* - `unsubscribe`
*
* @param {Function} hook The function to be called before the action.
*
* **Example**:
*
* ```js
* bridge.before('connect', function(ctx, next) {
* if(ctx.auth.password !== '1234') {
* ctx.badCredentials = true;
* }
* next();
* });
* ```
*
* **Action Context**
*
* The `ctx` object has the following properties for the specified actions.
*
* **Action: `connect`**
*
* - `ctx.auth` - `Object` containing auth information
* - `ctx.auth.username` - `String` containing client username
* - `ctx.auth.password` - `String` containing client password
* - `ctx.authorized` - `Boolean` Defaults to `true`. Set to `false` in a hook to send back an unathorized response.
* - `ctx.reject` - `Boolean` Defaults to false. Set to `true` to reject the action.
* - `ctx.clientId` - `String` containing the id of the client.
* - `ctx.badCredentials` - `Boolean` Defaults to false. Set to `true` if the provided credentials are invalid.
*
* **Action: `publish`**
*
* - `ctx.topic` - `String` the topic the client would like to publish the message to
* - `ctx.message` - `String` or `Buffer` the message to publish
* - `ctx.options` - `Object` protocol specific options
* - `ctx.authorized` - `Boolean` Defaults to `true`. Set to `false` in a hook to send back an unathorized response.
* - `ctx.reject` - `Boolean` Defaults to false. Set to `true` to reject the action.
* - `ctx.clientId` - `String` containing the id of the client.
*
* **Action: `subscribe`**
*
* - `ctx.topic` - `String` the topic the client would like to publish the message to
* - `ctx.subscriptions` - `Object` containing a topics as keys and options as values.
* Only `ctx.topic` or `ctx.subscriptions` will be set.
* - `ctx.options` - `Object` protocol specific options
* - `ctx.authorized` - `Boolean` Defaults to `true`. Set to `false` in a hook to send back an unathorized response.
* - `ctx.reject` - `Boolean` Defaults to false. Set to `true` to reject the action.
* - `ctx.clientId` - `String` containing the id of the client.
*
* **Event: `unsubscribe`**
*
* Emitted with a `ctx` object containing the following.
*
* - `ctx.topic` - `String` the topic the client would like to unsubscribe from.
*/
Bridge.prototype.before = function(action, hook) {
this.hooks[action].push(hook);
}
Bridge.prototype.trigger = function(action, ctx, cb) {
var hooks = this.hooks[action];
var numHooks = hooks && hooks.length;
var cur = 0;
if(!numHooks) {
return process.nextTick(cb);
}
hooks[0].hook(ctx, next);
function next(err) {
if(err || cur === numHooks) {
return cb(err);
}
hooks[++cur].hook(ctx, next);
}
}