-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
113 lines (95 loc) · 3.01 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
var net = require('net');
var util = require('util');
var events = require('events');
var hiredis = require('hiredis');
function Channel(port, host, chan) {
this.pub = hiredis.createConnection(port, host);
this.sub = hiredis.createConnection(port, host);
this.chan = chan;
this.raw = false;
this.ready = false;
this._pubReady = false;
this._subReady = false;
var emitError = function(err) {
this.emit('error', err);
this.destroy();
}.bind(this);
var badReply = function(side, reply) {
var err = new Error("Bad " + side + " reply");
err.reply = reply;
emitError(err);
};
// Publisher handling.
this.pub.on('connect', function() {
this._pubReady = true;
this.ready = this._subReady;
if (this.ready) this.emit('connect');
}.bind(this));
this.pub.on('reply', function(reply) {
if (reply instanceof Error)
return emitError(reply);
if (typeof(reply) !== 'number')
return badReply('pub', reply);
}.bind(this));
this.pub.on('close', function() {
this.pub = null;
this.destroy();
}.bind(this));
this.pub.on('error', emitError);
// Subscriber handling.
this.sub.on('connect', function() {
this.sub.write('subscribe', this.chan);
}.bind(this));
this.sub.on('reply', function(reply) {
if (reply instanceof Error)
return emitError(reply);
if (!Array.isArray(reply))
return badReply('sub', reply);
if (reply[0] === 'subscribe') {
if (this._subscribed || reply[1] !== this.chan || reply[2] !== 1)
return badReply('sub', reply);
this._subReady = true;
this.ready = this._pubReady;
if (this.ready) this.emit('connect');
return;
}
if (reply[0] === 'message') {
if (reply[1] !== this.chan)
return badReply('sub', reply);
var msg = reply[2];
if (!this.raw) {
try { msg = JSON.parse(msg); }
catch (err) { return emitError(err); }
}
return this.emit('message', msg);
}
return badReply('sub', reply);
}.bind(this));
this.sub.on('close', function() {
this.sub = null;
this.destroy();
}.bind(this));
this.sub.on('error', emitError);
}
util.inherits(Channel, events.EventEmitter);
Channel.prototype.destroy = function() {
var wasReady = this.ready;
this.ready = this._pubReady = this._subReady = false;
if (this.pub) {
this.pub.destroy();
this.pub = null;
}
if (this.sub) {
this.sub.destroy();
this.sub = null;
}
if (wasReady) this.emit('close');
};
Channel.prototype.send = function(message) {
if (!this.raw)
message = JSON.stringify(message);
this.pub.write('publish', this.chan, message);
};
exports.createChannel = function(port, host, chan) {
return new Channel(port, host, chan);
};