-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
171 lines (138 loc) · 3.02 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
/**
* Module dependencies.
*/
var Emitter = require('events').EventEmitter;
var Message = require('amp-message');
var fmt = require('util').format;
var parse = require('ms');
var amp = require('amp');
/**
* 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);
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;
Actor.emit('actor', this);
}
/**
* Inherit from `Emitter.prototype`.
*/
Actor.prototype.__proto__ = Emitter.prototype;
Actor.__proto__ = Emitter.prototype;
/**
* 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;
// 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;
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;
args.unshift(Buffer.from(id));
}
var msg = new Message(args);
this.stream.write(msg.toBuffer());
return {
timeout: function(ms){
if ('string' == typeof ms) ms = parse(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 105 == arg[0] && 58 == arg[1];
}