This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
smith.js
800 lines (704 loc) · 24.6 KB
/
smith.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
Copyright (c) 2012 Ajax.org B.V
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
( // Module boilerplate to support browser globals, node.js and AMD.
(typeof module !== "undefined" && function (m) { module.exports = m(require('events'), require('msgpack-js')); }) ||
(typeof define === "function" && function (m) { define("smith", ["./events-amd", "msgpack-js"], m); }) ||
(function (m) { window.smith = m(window.events, window.msgpack); })
)(function (events, msgpack) {
"use strict";
var EventEmitter = events.EventEmitter;
function inherits(Child, Parent) {
Child.prototype = Object.create(Parent.prototype, { constructor: { value: Child }});
}
var exports = {};
exports.msgpack = msgpack;
exports.Agent = Agent;
exports.Transport = Transport;
exports.deFramer = deFramer;
exports.liven = liven;
exports.freeze = freeze;
exports.getType = getType;
////////////////////////////////////////////////////////////////////////////////
// Transport is a connection between two Agents. It lives on top of a duplex,
// binary stream.
// @input - the stream we listen for data on
// @output - the stream we write to (can be the same object as input)
// @send(message) - send a message to the other side
// "message" - event emitted when we get a message from the other side.
// "disconnect" - the transport was disconnected
// "error" - event emitted for stream error or disconnect
// "drain" - drain event from output stream
function Transport(input, debug) {
var self = this;
this.debug = debug;
var output;
if (Array.isArray(input)) {
output = input[1];
input = input[0];
} else {
output = input;
}
this.input = input;
this.output = output;
if (!input.readable) throw new Error("Input is not readable");
if (!output.writable) throw new Error("Output is not writable");
// Attach event listeners
input.on("data", onData);
input.on("end", onDisconnect);
input.on("timeout", onDisconnect);
input.on("close", onDisconnect);
input.on("error", onError);
output.on("drain", onDrain);
if (output !== input) {
output.on("end", onDisconnect);
output.on("timeout", onDisconnect);
output.on("close", onDisconnect);
output.on("error", onError);
}
var parse = deFramer(function (frame) {
var message;
try {
message = msgpack.decode(frame);
} catch (err) {
return self.emit("error", err);
}
debug && console.log(process.pid + " <- " + require('util').inspect(message, false, 2, true));
self.emit("message", message);
});
// Route data chunks to the parser, but check for errors
function onData(chunk) {
try {
parse(chunk);
} catch (err) {
self.emit("error", err);
}
}
// Forward drain events from the writable stream
function onDrain() {
self.emit("drain");
}
// Forward all error events to the transport
function onError(err) {
self.emit("error", err);
}
function onDisconnect() {
// Remove all the listeners we added and destroy the streams
input.removeListener("data", onData);
input.removeListener("end", onDisconnect);
input.removeListener("timeout", onDisconnect);
input.removeListener("close", onDisconnect);
output.removeListener("drain", onDrain);
if (input.destroy) input.destroy();
if (input !== output) {
output.removeListener("end", onDisconnect);
output.removeListener("timeout", onDisconnect);
output.removeListener("close", onDisconnect);
if (output.destroy && output !== process.stdout) output.destroy();
}
self.emit("disconnect");
}
this.disconnect = onDisconnect;
}
inherits(Transport, EventEmitter);
Transport.prototype.send = function (message) {
// Uncomment to debug protocol
this.debug && console.log(process.pid + " -> " + require('util').inspect(message, false, 2, true));
// Serialize the messsage.
var frame = msgpack.encode(message);
// Send a 4 byte length header before the frame.
var header = new Buffer(10);
header.writeUInt32BE(frame.length, 0);
// Compute 4 byte jenkins hash
var a = frame.length >> 24,
b = (frame.length >> 16) & 0xff,
c = (frame.length >> 8) & 0xff,
d = frame.length & 0xff;
// Little bit inlined, but fast
var hash = 0;
hash += a;
hash += hash << 10;
hash += hash >> 6;
hash += b;
hash += hash << 10;
hash += hash >> 6;
hash += c;
hash += hash << 10;
hash += hash >> 6;
hash += d;
hash += hash << 10;
hash += hash >> 6;
// Shuffle bits
hash += hash << 3;
hash = hash ^ (hash >> 11);
hash += hash << 15;
hash |= 0;
header.writeInt32BE(hash, 4, true);
// 2 Reserved bytes for future usage
header.writeUInt16BE(0, 8);
this.output.write(header);
// Send the serialized message.
return this.output.write(frame);
};
// A simple state machine that consumes raw bytes and emits frame events.
// Returns a parser function that consumes buffers. It emits message buffers
// via onMessage callback passed in.
function deFramer(onFrame) {
var buffer;
var state = 0;
var length = 0;
var expected_hash = 0;
var hash = 0;
var offset;
return function parse(chunk) {
for (var i = 0, l = chunk.length; i < l; i++) {
switch (state) {
case 0:
length |= chunk[i] << 24;
expected_hash = 0;
state = 1;
break;
case 1: length |= chunk[i] << 16; state = 2; break;
case 2: length |= chunk[i] << 8; state = 3; break;
case 3:
length |= chunk[i];
expected_hash += chunk[i];
expected_hash += expected_hash << 10;
expected_hash += expected_hash >> 6;
// Shuffle bits
expected_hash += expected_hash << 3;
expected_hash = expected_hash ^ (expected_hash >> 11);
expected_hash += expected_hash << 15;
expected_hash |= 0;
hash = 0;
state = 4;
break;
case 4: hash |= chunk[i] << 24; state = 5; break;
case 5: hash |= chunk[i] << 16; state = 6; break;
case 6: hash |= chunk[i] << 8; state = 7; break;
case 7: hash |= chunk[i]; state = 8;
if (hash !== expected_hash) {
throw new Error("Hash mismatch, expected: " + expected_hash +
" got: " + hash + ", chunk: " + chunk);
}
if (length > 100 * 1024 * 1024) {
throw new Error("Too big buffer " + length +
", chunk: " + chunk);
}
buffer = new Buffer(length);
offset = 0;
state = 9;
break;
// Two reserved bytes
case 9: state = 10; break;
case 10: state = 11; break;
// Data itself
case 11:
var len = l - i;
var emit = false;
if (len + offset >= length) {
emit = true;
len = length - offset;
}
// TODO: optimize for case where a copy isn't needed can a slice can
// be used instead?
chunk.copy(buffer, offset, i, i + len);
offset += len;
i += len - 1;
if (emit) {
onFrame(buffer);
state = 0;
length = 0;
buffer = undefined;
offset = undefined;
}
break;
}
// Common case
if (state <= 3 && !emit) {
expected_hash += chunk[i];
expected_hash += expected_hash << 10;
expected_hash += expected_hash >> 6;
}
emit = false;
}
};
}
////////////////////////////////////////////////////////////////////////////////
// Agent is an API serving node in the architect-agent rpc mesh. It contains
// a table of functions that actually do the work and serve them to a Agent
// agent. An agent can connect to one other agent at a time.
function Agent(api) {
if (!this instanceof Agent) throw new Error("Forgot to use new with Agent constructor");
this.api = api || {};
// Bind event handlers and callbacks
this.disconnect = this.disconnect.bind(this);
this._onMessage = this._onMessage.bind(this);
this._onDrain = this._onDrain.bind(this);
this._onReady = this._onReady.bind(this);
this._getFunction = this._getFunction.bind(this);
this._storeFunction = this._storeFunction.bind(this);
this.remoteApi = {}; // Persist the API object between connections
this.transport = undefined;
this.callbacks = undefined;
this.nextKey = undefined;
}
inherits(Agent, EventEmitter);
// Time to wait for Agent connections to finish
Agent.prototype.connectionTimeout = 10000;
Agent.prototype.connect = function (transport, callback) {
// If they passed in a raw stream, wrap it.
if (!(transport instanceof Transport)) transport = new Transport(transport);
this.transport = transport;
this.callbacks = {};
this.nextKey = 1;
transport.on("error", this.disconnect);
transport.on("disconnect", this.disconnect);
transport.on("message", this._onMessage);
transport.on("drain", this._onDrain);
// Handshake with the other end
this.send(["ready", this._onReady]);
// Start timeout and route events to callback
this.on("connect", onConnect);
this.on("disconnect", onDisconnect);
this.on("error", onError);
var timeout;
if (this.connectionTimeout) {
timeout = setTimeout(onTimeout, this.connectionTimeout);
}
var self = this;
function onConnect(api) {
reset();
if (callback) callback(null, api);
}
function onDisconnect(err) {
onError(err || new Error("EDISCONNECT: Agent disconnected"));
}
function onError(err) {
reset();
if (callback) callback(err);
else self.emit("error", err);
}
function onTimeout() {
reset();
var err = new Error("ETIMEDOUT: Timeout while waiting for Agent agent to connect.");
err.code = "ETIMEDOUT";
if (callback) callback(err);
else self.emit("error", err);
}
// Only one event should happen, so stop event listeners on first event.
function reset() {
self.removeListener("connect", onConnect);
self.removeListener("disconnect", onDisconnect);
self.removeListener("error", onError);
clearTimeout(timeout);
}
};
Agent.prototype.send = function (message) {
message = freeze(message, this._storeFunction);
return this.transport.send(message);
};
Agent.prototype._onReady = function (names, env) {
if (!Array.isArray(names)) return;
var self = this;
names.forEach(function (name) {
// Ignore already set functions so that existing function references
// stay valid.
if (self.remoteApi[name]) return;
self.remoteApi[name] = function () {
// When disconnected we can't forward the call.
if (!self.transport) {
var callback = arguments[arguments.length - 1];
if (typeof callback === "function") {
var err = new Error("ENOTCONNECTED: Agent is offline, try again later");
err.code = "ENOTCONNECTED";
callback(err);
}
return;
}
var args = [name];
args.push.apply(args, arguments);
return self.send(args);
};
});
this.remoteEnv = env;
this._emitConnect();
};
Agent.prototype._emitConnect = function () {
this.emit("connect", this.remoteApi);
};
// Disconnect resets the state of the Agent, flushes callbacks and emits a
// "disconnect" event with optional error object.
Agent.prototype.disconnect = function (err) {
// if (!this.transport) {
// if (err) return this.emit("error", err);
// }
// Disconnect from transport
if (this.transport) {
this.transport.removeListener("error", this.disconnect);
this.transport.removeListener("disconnect", this.disconnect);
this.transport.removeListener("message", this._onMessage);
this.transport.removeListener("drain", this._onDrain);
this.transport.disconnect();
this.transport = undefined;
}
var cerr = err;
if (!cerr) {
cerr = new Error("EDISCONNECT: Agent disconnected");
cerr.code = "EDISCONNECT";
}
this.emit("disconnect", err);
// Flush any callbacks
if (this.callbacks) {
var callbacks = this.callbacks;
this.callbacks = undefined;
forEach(callbacks, function (callback) {
callback(cerr);
});
}
this.nextKey = undefined;
};
// Forward drain events
Agent.prototype._onDrain = function () {
this.emit("drain");
};
// Route incoming messages to the right functions
Agent.prototype._onMessage = function (message) {
if (!(Array.isArray(message) && message.length)) {
return this.emit("error", new Error("Message should be an array"));
}
message = liven(message, this._getFunction);
var id = message[0];
var fn;
if (id === "ready") {
var keys = Object.keys(this.api);
var env = typeof process !== "undefined" && process.env;
fn = function (callback) {
callback(keys, env);
};
}
else {
fn = typeof id === "string" ? this.api[id] : this.callbacks[id];
}
if (typeof fn !== "function") {
return this.emit("error", new Error("Should be function"));
}
fn.apply(this, message.slice(1));
};
// Create a proxy function that calls fn key on the Agent side.
// This is for when a Agent passes a callback to a local function.
Agent.prototype._getFunction = function (key) {
var transport = this.transport;
return function () {
// Call a Agent function using [key, args...]
var args = [key];
// Push is actually fast http://jsperf.com/array-push-vs-concat-vs-unshift
args.push.apply(args, arguments);
return transport.send(args);
};
};
// This is for when we call a Agent function and pass in a callback
Agent.prototype._storeFunction = function (fn) {
var key = this.nextKey;
while (this.callbacks.hasOwnProperty(key)) {
key = (key + 1) >> 0;
if (key === this.nextKey) {
throw new Error("Ran out of keys!!");
}
}
this.nextKey = (key + 1) >> 0;
var callbacks = this.callbacks;
var self = this;
// Wrap is a self cleaning function and store in the index
callbacks[key] = function () {
delete callbacks[key];
self.nextKey = key;
return fn.apply(this, arguments);
};
return key;
};
// Convert a js object into a serializable object when functions are
// encountered, the storeFunction callback is called for each one.
// storeFunction takes in a function and returns a unique id number. Cycles
// are stored as object with a single $ key and an array of strigs as the
// path. Functions are stored as objects with a single $ key and id as value.
// props. properties starting with "$" have an extra $ prepended.
function freeze(value, storeFunction) {
var seen = [];
var paths = [];
function find(value, path) {
// find the type of the value
var type = getType(value);
// pass primitives through as-is
if (type !== "function" && type !== "object" && type !== "array" && type !== "date") {
return value;
}
// Look for duplicates
var index = seen.indexOf(value);
if (index >= 0) {
return { "$": paths[index] };
}
// If not seen, put it in the registry
index = seen.length;
seen[index] = value;
paths[index] = path;
var o;
// Look for functions
if (type === "function") {
o = storeFunction(value);
}
if (type === "date") {
o = {d:value.getTime()};
}
if (o) return {$:o};
// Recurse on objects and arrays
return map(value, function (sub, key) {
return find(sub, path.concat([key]));
}, null, function (key) {
return key[0] === "$" ? "$" + key : key;
});
}
return find(value, []);
}
// Converts flat objects into live objects. Cycles are re-connected and
// functions are inserted. The getFunction callback is called whenever a
// frozen function is encountered. It expects an ID and returns the function
function liven(message, getFunction) {
function find(value, parent, key) {
// find the type of the value
var type = getType(value);
// Unescape $$+ escaped keys
if (key[0] === "$") key = key.substr(1);
// pass primitives through as-is
if (type !== "function" && type !== "object" && type !== "array") {
parent[key] = value;
return value;
}
// Load Specials
if (value.hasOwnProperty("$")) {
var special = value.$;
// Load backreferences
if (Array.isArray(special)) {
parent[key] = get(obj.root, special);
return parent[key];
}
if (typeof special === "object") {
parent[key] = new Date(special.d);
return parent[key];
}
// Load functions
parent[key] = getFunction(special);
return parent[key];
}
// Recurse on objects and arrays
var o = Array.isArray(value) ? [] : {};
parent[key] = o;
forEach(value, function (sub, key) {
find(sub, o, key);
});
return obj;
}
var obj = {};
find(message, obj, "root");
return obj.root;
}
////////////////////////////////////////////////////////////////////////////////
// Typeof is broken in javascript, add support for null and buffer types
function getType(value) {
if (value === null) {
return "null";
}
if (Array.isArray(value)) {
return "array";
}
if (typeof Buffer !== "undefined" && Buffer.isBuffer(value)) {
return "buffer";
}
// TODO: find a way to work with Date instances from other contexts.
if (value instanceof Date) {
return "date";
}
return typeof value;
}
// Traverse an object to get a value at a path
function get(root, path) {
var target = root;
for (var i = 0, l = path.length; i < l; i++) {
target = target[path[i]];
}
return target;
}
// forEach that works on both arrays and objects
function forEach(value, callback, thisp) {
if (typeof value.forEach === "function") {
return value.forEach.call(value, callback, thisp);
}
var keys = Object.keys(value);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
callback.call(thisp, value[key], key, value);
}
}
// map that works on both arrays and objects
function map(value, callback, thisp, keyMap) {
if (typeof value.map === "function") {
return value.map.call(value, callback, thisp);
}
var obj = {};
var keys = Object.keys(value);
for (var i = 0, l = keys.length; i < l; i++) {
var key = keys[i];
obj[keyMap ? keyMap(key) : key] = callback.call(thisp, value[key], key, value);
}
return obj;
}
exports.WebSocketTransport = WebSocketTransport;
inherits(WebSocketTransport, Transport);
// "message" - event emitted when we get a message from the other side.
// "disconnect" - the transport was disconnected
// "error" - event emitted for stream error or disconnect
// "drain" - drain event from output stream
function WebSocketTransport(socket, debug) {
this.socket = socket;
this.debug = debug;
var self = this;
socket.on("message", onMessage);
socket.on("close", onDisconnect);
socket.on("error", onError);
function onError(err) {
self.emit("error", err);
}
function onMessage(data) {
var message;
try { message = msgpack.decode(data); }
catch (err) { return onError(err); }
debug && console.log(process.pid + " <- " + require('util').inspect(message, false, 2, true));
self.emit("message", message);
}
function onDisconnect() {
// Remove all the listeners we added and destroy the socket
socket.removeListener("message", onMessage);
socket.removeListener("close", onDisconnect);
self.emit("disconnect");
}
this.disconnect = onDisconnect;
// TODO: Implement "drain" event, pause(), and resume() properly.
// function onDrain() {
// self.emit("drain");
// }
}
WebSocketTransport.prototype.send = function (message) {
// Uncomment to debug protocol
this.debug && console.log(process.pid + " -> " + require('util').inspect(message, false, 2, true));
var data;
try { data = msgpack.encode(message); }
catch (err) { return this.emit("error", err); }
this.socket.send(data, {binary: true});
};
exports.BrowserTransport = BrowserTransport;
inherits(BrowserTransport, Transport);
function BrowserTransport(websocket, debug) {
this.websocket = websocket;
this.debug = debug;
var self = this;
websocket.binaryType = 'arraybuffer';
websocket.onmessage = function (evt) {
var message;
try { message = msgpack.decode(evt.data); }
catch (err) { return onError(err); }
debug && console.log("<-", message);
self.emit("message", message);
};
websocket.onclose = function (evt) {
};
websocket.onerror = function (evt) {
onError(new Error(evt.data));
};
function onError(err) {
self.emit("error", err);
}
function onDisconnect() {
// Remove all the listeners we added and destroy the socket
delete websocket.onmessage;
delete websocket.onclose;
self.emit("disconnect");
}
this.disconnect = onDisconnect;
}
BrowserTransport.prototype.send = function (message) {
// Uncomment to debug protocol
this.debug && console.log("->", message);
var data;
try { data = msgpack.encode(message); }
catch (err) { return this.emit("error", err); }
this.websocket.send(data);
};
exports.EngineIoTransport = EngineIoTransport;
inherits(EngineIoTransport, Transport);
function EngineIoTransport(socket, debug) {
var self = this;
this.debug = debug;
// Route errors from socket to transport.
socket.on("error", function (err) {
self.emit("error", err);
});
// Parse and route messages from socket to transport.
socket.on("message", function (json) {
var message;
try {
message = JSON.parse(json);
}
catch (err) {
self.emit("error", err);
return;
}
if (Array.isArray(message)) {
if (debug) {
console.log("<-", message);
}
self.emit("message", message);
}
else {
self.emit("legacy", message);
}
});
// Route close events as disconnect events
socket.on("close", function (reason) {
self.emit("disconnect", reason);
});
this.disconnect = function () {
socket.close();
};
// Encode and route send calls to socket.
this.send = function (message) {
var json;
try {
json = JSON.stringify(message);
}
catch (err) {
self.emit("error", err);
return;
}
if (this.debug && Array.isArray(message)) {
console.log("->", message);
}
return socket.send(json);
};
}
return exports;
});