-
Notifications
You must be signed in to change notification settings - Fork 24
/
secure-socket-router.js
196 lines (161 loc) · 3.6 KB
/
secure-socket-router.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
/**
* Copyright 2014 tj
* 2016 Edited by @Unitech
* http://github.com/tj/node-actorify
* Licence MIT
*/
/**
* Module dependencies.
*/
var Emitter = require('events').EventEmitter;
var Message = require('amp-message');
var fmt = require('util').format;
var amp = require('amp');
var cs = require('./crypto-service.js');
var debug = require('debug')('socket-router');
/**
* Slice ref.
*/
var slice = [].slice;
/**
* Actor ids.
*/
var ids = 0;
/**
* Expose `Actor`.
*/
module.exports = Actor;
/**
* Initialize an actor for the given `Stream`.
*
* @param {Stream} stream
* @api public
*/
function Actor(stream) {
if (!(this instanceof Actor)) return new Actor(stream);
var that = this;
this.parser = new amp.Stream;
this.parser.on('data', this.onmessage.bind(this));
stream.pipe(this.parser);
this.stream = stream;
this.callbacks = {};
this.ids = 0;
this.id = ++ids;
this.secret_key = null;
Actor.emit('actor', this);
}
/**
* Inherit from `Emitter.prototype`.
*/
Actor.prototype.__proto__ = Emitter.prototype;
Actor.__proto__ = Emitter.prototype;
Actor.prototype.setSecretKey = function(key) {
this.secret_key = key;
};
/**
* Inspect implementation.
*/
Actor.prototype.inspect = function(){
var cbs = Object.keys(this.callbacks).length;
return fmt('<Actor id=%d callbacks=%d>', this.id, cbs);
};
/**
* Handle message.
*/
Actor.prototype.onmessage = function(buf){
var msg = new Message(buf);
var args = msg.args;
var self = this;
if (this.secret_key) {
args.forEach(function(arg, i) {
args[i] = cs.verify(args[i], self.secret_key);
});
}
// reply message, invoke
// the given callback
if ('_reply_' == args[0]) {
args.shift();
var id = args.shift().toString();
var fn = this.callbacks[id];
delete this.callbacks[id];
if (fn) fn.apply(null, args);
return;
}
// request method, pass
// a trailing callback
if (isId(args[0])) {
var id = args.shift();
args.push(function(){
self.send.apply(self, reply(id, arguments));
});
}
this.emit.apply(this, args);
};
/**
* Send message.
*
* TODO: clean up... return a Message etc
*
* @param {String} msg
* @param {Mixed} ...
* @return {Object}
* @api public
*/
Actor.prototype.send = function(){
if ('string' != typeof arguments[0]) throw new Error('missing message name');
var args = slice.call(arguments);
var last = args[args.length - 1];
var timer;
var that = this;
if ('function' == typeof last) {
var id = 'i:' + this.ids++;
var fn = args.pop();
function callback(){
callback = function(){};
clearTimeout(timer);
fn.apply(this, arguments);
}
this.callbacks[id] = callback;
//var buffer = new Buffer(id);
args.unshift(id);
}
if (this.secret_key) {
args.forEach(function(arg, i) {
args[i] = cs.secure(args[i], that.secret_key);
});
}
var msg = new Message(args);
this.stream.write(msg.toBuffer());
return {
timeout: function(ms){
timer = setTimeout(function(){
var err = new Error('message response timeout exceeded');
err.timeout = ms;
callback(err);
}, ms);
}
}
};
/**
* Return a reply message for `id` and `args`.
*
* @param {String} id
* @param {Array} args
* @return {Array}
* @api private
*/
function reply(id, args) {
var msg = new Array(2 + args.length);
msg[0] = '_reply_';
msg[1] = id;
for (var i = 0; i < args.length; i++) {
msg[i + 2] = args[i];
}
return msg;
}
/**
* ID argument.
*/
function isId(arg) {
return 'i' == arg[0] && ':' == arg[1];
}