This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bus.io.js
11032 lines (9296 loc) · 276 KB
/
bus.io.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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.client=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
(function (global){
global.window.io = module.exports = _dereq_('./lib');
}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./lib":2}],2:[function(_dereq_,module,exports){
var debug = _dereq_('debug')('bus.io-client');
var slice = Array.prototype.slice;
var emit = _dereq_('component-emitter').prototype.emit;
var common = _dereq_('bus.io-common');
/*
* Wrap up socket.io-client
*/
var io = _dereq_('socket.io-client');
var lookup = io.connect;
var client = module.exports = function (uri, opts) {
var sock = lookup(uri, opts);
sock.on('connect', function () {
debug('sock connect');
// tells the server we are a bus.io client
sock.emit('_flag', 1);
});
sock.on('disconnect', function () {
debug('sock disconnect');
});
sock.onevent = function (packet) {
var args = packet.data || [];
debug('emitting event %j', args);
if (null != packet.id) {
debug('attaching ack callback to event');
args.push(this.ack(packet.id));
}
if (this.connected) {
trigger.apply(this, args);
} else {
this.receiveBuffer.push(args);
}
};
sock.msg = sock.message = function (data) {
var self = this;
var builder = common.Builder(data);
builder.on('built', function (msg) {
debug('built %s %s', msg.id(), msg.content());
self.emit(msg.data.action, msg);
});
return builder;
};
sock.deliver = function (data) {
this.message(data).deliver();
};
return sock;
};
for (var k in io) client[k] = io[k];
client.connect = client;
/*
* The onevent method will call this method to try and call "emit"
* with a message if we have one.
*/
function trigger () {
debug('trigger %j', arguments);
if (!arguments.length) return;
var args = slice.call(arguments);
switch(args.length) {
case 1:
debug('one argument');
emit.apply(this, args);
break;
case 2:
debug('two arguments');
if (args[1] && args[1].isMessage) {
debug('it is a message %s', args[1].data.id);
var msg = common.Message(args[1]);
debug('vivified %s', msg.id());
emit.apply(this, [args[0], msg ]);
}
else {
debug('it is not a message');
emit.apply(this, args);
}
break;
default:
debug('more than one argument');
emit.apply(this, args);
break;
}
};
},{"bus.io-common":3,"component-emitter":10,"debug":11,"socket.io-client":35}],3:[function(_dereq_,module,exports){
module.exports = _dereq_('./lib');
},{"./lib":6}],4:[function(_dereq_,module,exports){
var util = _dereq_('util')
, events = _dereq_('events')
, debug = _dereq_('debug')('bus.io-common:builder')
, Message = _dereq_('./message')
, slice = Array.prototype.slice
;
module.exports = Builder;
/**
* Builds a Message instance and provides a way to deliver the built message
*
* @param {object} data
*/
function Builder (data) {
if (!(this instanceof Builder)) return new Builder(data);
debug('new builder', data);
events.EventEmitter.call(this);
this.message = Message(data);
}
util.inherits(Builder, events.EventEmitter);
/**
* set up delegates
*/
'actor action target content id created reference published'.split(' ').forEach(function (name) {
Builder.prototype[name] = function () {
debug('delegating %s to the message instance', name);
var v = this.message[name].apply(this.message,slice.call(arguments));
if ('object' === typeof v && (v === this.message || v.isMessage)) {
return this;
}
else {
return v;
}
}
});
/**
* Sets or gets the actor
*
* @param {mixed} actor
* @return Object / Builder
*/
Builder.prototype.i = Builder.prototype.actor;
/**
* Sets or gets the action
*
* @param {mixed} action
* @return Object / Builder
*/
Builder.prototype.did = Builder.prototype.action;
/**
* Sets or gets the content
*
* @param {mixed} content
* @return Object / Builder
*/
Builder.prototype.what = Builder.prototype.content;
/**
* Sets or gets the data
*
* @return Object / Builder
*/
Builder.prototype.data = function (data) {
if (typeof data === 'object') {
this.message.data = data;
}
else {
return this.message.data;
}
return this;
};
/**
* Delivers the message to each passed target
*
* @return Builder
*/
Builder.prototype.to = Builder.prototype.deliver = function () {
if (arguments.length > 0) {
this.target(String(arguments[0]));
}
if (this.target()) {
this.emit('built', this.message);
}
if (arguments.length > 1) {
var targets = slice.call(arguments);
for (var i=1; i<targets.length; i++) {
var message = this.message.clone();
message.target(String(targets[i]));
this.emit('built', message);
}
}
return this;
};
},{"./message":7,"debug":11,"events":30,"util":34}],5:[function(_dereq_,module,exports){
var util = _dereq_('util')
, events = _dereq_('events')
, debug = _dereq_('debug')('bus.io-common:controller')
, Message = _dereq_('./message')
, slice = Array.prototype.slice
;
module.exports = Controller;
/**
* When handling a message we use a controller
*
* @param {Message} message
* @throws Error
*/
function Controller (message) {
if (!(message instanceof Message)) throw new Error('message must be an instanceof Message');
if (!(this instanceof Controller)) return new Controller(message);
debug('new controller');
events.EventEmitter.call(this);
this.message = message;
this.data = this.message.data;
}
util.inherits(Controller, events.EventEmitter);
/**
* Flags the message as consumed
*
* @return Controller
*/
Controller.prototype.consume = function () {
debug('consuming message %s', this.message.id());
this.message.consumed = new Date();
this.emit('consume', this.message);
return this;
};
/**
* Responds to the message with the given content
*
* @param {mixed} content
* @return Controller
*/
Controller.prototype.respond = function (content) {
debug('responding to message %s', this.message.id());
var message = this.message.clone();
debug('response message id %', message.id());
message.data.actor = this.message.target();
message.data.target = this.message.actor();
message.data.content = typeof content !== 'undefined' ? content : message.data.content;
message.data.created = new Date();
message.data.reference = this.message.id();
this.message.responded = new Date();
this.emit('respond', message);
return this;
};
/**
* Delivers the message
*
* @return Controller
*/
Controller.prototype.deliver = function () {
debug('delivering the message');
this.message.delivered = new Date();
if (arguments.length === 0) {
debug('to original target');
this.emit('deliver', this.message);
}
else if (arguments.length === 1) {
if (typeof arguments[0] === 'object' && arguments[0] instanceof Array) {
debug('to a list of targets');
deliverEach(this, arguments[0]);
}
else {
debug('to another target');
var message = this.message.clone();
message.data.target = String(arguments[0]);
this.emit('deliver', message);
}
}
else if (arguments.length > 1) {
debug('we have more than one arguments so deliver to each of them');
deliverEach(this, slice.call(arguments));
}
return this;
};
/**
* This method is a conveince for setting the content and as well as triggering
* the response, if we encounter an error.
*
* @param {mixed} content
* @return Controller
*/
Controller.prototype.errored = function (err) {
debug('responding with an error');
this.action(this.action() + ' errored').respond(err);
return this;
};
/**
* set up delegates
*/
'actor action target content id created reference published'.split(' ').forEach(function (name) {
Controller.prototype[name] = function () {
debug('delegating %s to the message instance', name);
var v = this.message[name].apply(this.message,slice.call(arguments));
if ('object' === typeof v && (v === this.message || v.isMessage)) {
return this;
}
else {
return v;
}
}
});
function deliverEach (controller, targets) {
targets.forEach(function (target) {
var message = controller.message.clone();
message.data.target = target;
controller.emit('deliver', message);
});
}
},{"./message":7,"debug":11,"events":30,"util":34}],6:[function(_dereq_,module,exports){
exports.Message = _dereq_('./message');
exports.Controller = _dereq_('./controller');
exports.Builder = _dereq_('./builder');
},{"./builder":4,"./controller":5,"./message":7}],7:[function(_dereq_,module,exports){
var extend = _dereq_('extend')
, debug = _dereq_('debug')('bus.io-common:message')
, uuid = _dereq_('node-uuid')
, slice = Array.prototype.slice
;
module.exports = Message;
/**
* A message represents an action performed by an actor on target with the content
*/
function Message () {
if (!(this instanceof Message)) {
if (typeof arguments[0] === 'object' && arguments[0] instanceof Message) {
debug('message is a message so return it');
return arguments[0];
}
else {
debug('creating new message and initializing with arguments');
var m = new Message();
Message.prototype.initialize.apply(m, slice.call(arguments));
return m;
}
}
else {
this.isMessage = true;
if (arguments.length) {
debug('initializing with arguments');
Message.prototype.initialize.apply(this, slice.call(arguments));
}
}
}
/**
* Initializes the message instance
*
* @param {string} a The actor
* @param {string} b The action
* @param {string} c The content
* @param {string} d The target
* @param {Date} e The created
* @param {String} f id of the message
* @param {string} g The referenced message id
* @param {Date} h The date it was published
*/
Message.prototype.initialize = function (a, b, c, d, e, f, g, h) {
if (arguments.length === 1 && typeof a === 'object') {
debug('it is an object');
if (a instanceof Message) {
debug('it is a Message');
this.data = a.clone().data;
}
else if (a.isMessage) {
debug('the object is not an instance but has the flag');
extend(this, a);
}
else if (a.data) {
debug('it has some data we may be able to use');
this.data = a.data
}
else {
debug('just using it as the data');
this.data = a;
}
}
else {
debug('initializing with positional arguments and or defaults');
this.data = {};
this.data.actor = a || 'unknown';
this.data.action = b || 'unknown';
this.data.content = c || [];
this.data.target = d || 'unknown';
this.data.created = e || new Date();
this.data.id = f || uuid.v1();
this.data.reference = g;
this.data.published = h;
}
if (!this.data) {
debug('no data setting to empty object');
this.data = {};
}
if (!this.data.created) {
debug('setting the created date');
this.data.created = new Date();
}
if (!this.data.id) {
debug('setting the id');
this.data.id = uuid.v1();
}
return this;
};
/**
* Clones the message's data into a new message, however the id is now different
*
* @return Message
*/
Message.prototype.clone = function () {
var m = new Message(extend({}, this.data));
m.data.id = uuid.v1();
debug('cloned the message %d', m.data.id);
return m;
};
// set / get these functions
Message.prototype.actor = setOrGet('actor', 'unknown');
Message.prototype.action = setOrGet('action', 'unknown');
Message.prototype.target = setOrGet('target', 'unknown');
Message.prototype.content = setOrGet('content', function () { return []; }, function (a) { if (typeof a==='object' && a instanceof Array && a.length === 1) { return a[0]; } else { return a; } });
Message.prototype.id = get('id', function () { return uuid.v1(); });
Message.prototype.created = get('created', function () { return new Date(); });
Message.prototype.reference = get('reference', null);
Message.prototype.published = get('published', false);
function get (name, def, onGet) {
onGet = onGet || f;
return function () {
if (!this.data) {
this.data = {};
}
this.data[name] = this.data[name] || (typeof def === 'function' ? def() : def);
if (typeof this.data[name] === 'undefined') {
this.data[name] = (typeof def === 'function' ? def() : def);
}
return onGet(this.data[name]);
}
}
function set (name, onSet) {
onSet = onSet || f;
return function (v) {
if (!this.data) {
this.data = {};
}
this.data[name] = onSet(v);
return this;
}
}
function setOrGet (name, def, onGet, onSet) {
var g = get(name, def, onGet), s = set(name, onSet);
return function (v) {
var self = this;
if (v) {
return s.call(this, v);
}
return g.call(this);
}
}
function f (a) { return a; }
},{"debug":11,"extend":8,"node-uuid":9}],8:[function(_dereq_,module,exports){
var hasOwn = Object.prototype.hasOwnProperty;
var toString = Object.prototype.toString;
var undefined;
var isPlainObject = function isPlainObject(obj) {
"use strict";
if (!obj || toString.call(obj) !== '[object Object]' || obj.nodeType || obj.setInterval) {
return false;
}
var has_own_constructor = hasOwn.call(obj, 'constructor');
var has_is_property_of_method = obj.constructor && obj.constructor.prototype && hasOwn.call(obj.constructor.prototype, 'isPrototypeOf');
// Not own constructor property must be Object
if (obj.constructor && !has_own_constructor && !has_is_property_of_method) {
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var key;
for (key in obj) {}
return key === undefined || hasOwn.call(obj, key);
};
module.exports = function extend() {
"use strict";
var options, name, src, copy, copyIsArray, clone,
target = arguments[0],
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
} else if (typeof target !== "object" && typeof target !== "function" || target == undefined) {
target = {};
}
for (; i < length; ++i) {
// Only deal with non-null/undefined values
if ((options = arguments[i]) != null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy) {
continue;
}
// Recurse if we're merging plain objects or arrays
if (deep && copy && (isPlainObject(copy) || (copyIsArray = Array.isArray(copy)))) {
if (copyIsArray) {
copyIsArray = false;
clone = src && Array.isArray(src) ? src : [];
} else {
clone = src && isPlainObject(src) ? src : {};
}
// Never move original objects, clone them
target[name] = extend(deep, clone, copy);
// Don't bring in undefined values
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
// Return the modified object
return target;
};
},{}],9:[function(_dereq_,module,exports){
(function (Buffer){
// uuid.js
//
// Copyright (c) 2010-2012 Robert Kieffer
// MIT License - http://opensource.org/licenses/mit-license.php
(function() {
var _global = this;
// Unique ID creation requires a high quality random # generator. We feature
// detect to determine the best RNG source, normalizing to a function that
// returns 128-bits of randomness, since that's what's usually required
var _rng;
// Node.js crypto-based RNG - http://nodejs.org/docs/v0.6.2/api/crypto.html
//
// Moderately fast, high quality
if (typeof(_dereq_) == 'function') {
try {
var _rb = _dereq_('crypto').randomBytes;
_rng = _rb && function() {return _rb(16);};
} catch(e) {}
}
if (!_rng && _global.crypto && crypto.getRandomValues) {
// WHATWG crypto-based RNG - http://wiki.whatwg.org/wiki/Crypto
//
// Moderately fast, high quality
var _rnds8 = new Uint8Array(16);
_rng = function whatwgRNG() {
crypto.getRandomValues(_rnds8);
return _rnds8;
};
}
if (!_rng) {
// Math.random()-based (RNG)
//
// If all else fails, use Math.random(). It's fast, but is of unspecified
// quality.
var _rnds = new Array(16);
_rng = function() {
for (var i = 0, r; i < 16; i++) {
if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
_rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
}
return _rnds;
};
}
// Buffer class to use
var BufferClass = typeof(Buffer) == 'function' ? Buffer : Array;
// Maps for number <-> hex string conversion
var _byteToHex = [];
var _hexToByte = {};
for (var i = 0; i < 256; i++) {
_byteToHex[i] = (i + 0x100).toString(16).substr(1);
_hexToByte[_byteToHex[i]] = i;
}
// **`parse()` - Parse a UUID into it's component bytes**
function parse(s, buf, offset) {
var i = (buf && offset) || 0, ii = 0;
buf = buf || [];
s.toLowerCase().replace(/[0-9a-f]{2}/g, function(oct) {
if (ii < 16) { // Don't overflow!
buf[i + ii++] = _hexToByte[oct];
}
});
// Zero out remaining bytes if string was short
while (ii < 16) {
buf[i + ii++] = 0;
}
return buf;
}
// **`unparse()` - Convert UUID byte array (ala parse()) into a string**
function unparse(buf, offset) {
var i = offset || 0, bth = _byteToHex;
return bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] + '-' +
bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]] +
bth[buf[i++]] + bth[buf[i++]];
}
// **`v1()` - Generate time-based UUID**
//
// Inspired by https://github.com/LiosK/UUID.js
// and http://docs.python.org/library/uuid.html
// random #'s we need to init node and clockseq
var _seedBytes = _rng();
// Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1)
var _nodeId = [
_seedBytes[0] | 0x01,
_seedBytes[1], _seedBytes[2], _seedBytes[3], _seedBytes[4], _seedBytes[5]
];
// Per 4.2.2, randomize (14 bit) clockseq
var _clockseq = (_seedBytes[6] << 8 | _seedBytes[7]) & 0x3fff;
// Previous uuid creation time
var _lastMSecs = 0, _lastNSecs = 0;
// See https://github.com/broofa/node-uuid for API details
function v1(options, buf, offset) {
var i = buf && offset || 0;
var b = buf || [];
options = options || {};
var clockseq = options.clockseq != null ? options.clockseq : _clockseq;
// UUID timestamps are 100 nano-second units since the Gregorian epoch,
// (1582-10-15 00:00). JSNumbers aren't precise enough for this, so
// time is handled internally as 'msecs' (integer milliseconds) and 'nsecs'
// (100-nanoseconds offset from msecs) since unix epoch, 1970-01-01 00:00.
var msecs = options.msecs != null ? options.msecs : new Date().getTime();
// Per 4.2.1.2, use count of uuid's generated during the current clock
// cycle to simulate higher resolution clock
var nsecs = options.nsecs != null ? options.nsecs : _lastNSecs + 1;
// Time since last uuid creation (in msecs)
var dt = (msecs - _lastMSecs) + (nsecs - _lastNSecs)/10000;
// Per 4.2.1.2, Bump clockseq on clock regression
if (dt < 0 && options.clockseq == null) {
clockseq = clockseq + 1 & 0x3fff;
}
// Reset nsecs if clock regresses (new clockseq) or we've moved onto a new
// time interval
if ((dt < 0 || msecs > _lastMSecs) && options.nsecs == null) {
nsecs = 0;
}
// Per 4.2.1.2 Throw error if too many uuids are requested
if (nsecs >= 10000) {
throw new Error('uuid.v1(): Can\'t create more than 10M uuids/sec');
}
_lastMSecs = msecs;
_lastNSecs = nsecs;
_clockseq = clockseq;
// Per 4.1.4 - Convert from unix epoch to Gregorian epoch
msecs += 12219292800000;
// `time_low`
var tl = ((msecs & 0xfffffff) * 10000 + nsecs) % 0x100000000;
b[i++] = tl >>> 24 & 0xff;
b[i++] = tl >>> 16 & 0xff;
b[i++] = tl >>> 8 & 0xff;
b[i++] = tl & 0xff;
// `time_mid`
var tmh = (msecs / 0x100000000 * 10000) & 0xfffffff;
b[i++] = tmh >>> 8 & 0xff;
b[i++] = tmh & 0xff;
// `time_high_and_version`
b[i++] = tmh >>> 24 & 0xf | 0x10; // include version
b[i++] = tmh >>> 16 & 0xff;
// `clock_seq_hi_and_reserved` (Per 4.2.2 - include variant)
b[i++] = clockseq >>> 8 | 0x80;
// `clock_seq_low`
b[i++] = clockseq & 0xff;
// `node`
var node = options.node || _nodeId;
for (var n = 0; n < 6; n++) {
b[i + n] = node[n];
}
return buf ? buf : unparse(b);
}
// **`v4()` - Generate random UUID**
// See https://github.com/broofa/node-uuid for API details
function v4(options, buf, offset) {
// Deprecated - 'format' argument, as supported in v1.2
var i = buf && offset || 0;
if (typeof(options) == 'string') {
buf = options == 'binary' ? new BufferClass(16) : null;
options = null;
}
options = options || {};
var rnds = options.random || (options.rng || _rng)();
// Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
rnds[6] = (rnds[6] & 0x0f) | 0x40;
rnds[8] = (rnds[8] & 0x3f) | 0x80;
// Copy bytes to buffer, if provided
if (buf) {
for (var ii = 0; ii < 16; ii++) {
buf[i + ii] = rnds[ii];
}
}
return buf || unparse(rnds);
}
// Export public API
var uuid = v4;
uuid.v1 = v1;
uuid.v4 = v4;
uuid.parse = parse;
uuid.unparse = unparse;
uuid.BufferClass = BufferClass;
if (typeof define === 'function' && define.amd) {
// Publish as AMD module
define(function() {return uuid;});
} else if (typeof(module) != 'undefined' && module.exports) {
// Publish as node.js module
module.exports = uuid;
} else {
// Publish as global (in browsers)
var _previousRoot = _global.uuid;
// **`noConflict()` - (browser only) to reset global 'uuid' var**
uuid.noConflict = function() {
_global.uuid = _previousRoot;
return uuid;
};
_global.uuid = uuid;
}
}).call(this);
}).call(this,_dereq_("buffer").Buffer)
},{"buffer":14,"crypto":20}],10:[function(_dereq_,module,exports){
/**
* Expose `Emitter`.
*/
module.exports = Emitter;
/**
* Initialize a new `Emitter`.
*
* @api public
*/
function Emitter(obj) {
if (obj) return mixin(obj);
};
/**
* Mixin the emitter properties.
*
* @param {Object} obj
* @return {Object}
* @api private
*/
function mixin(obj) {
for (var key in Emitter.prototype) {
obj[key] = Emitter.prototype[key];
}
return obj;
}
/**
* Listen on the given `event` with `fn`.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.on =
Emitter.prototype.addEventListener = function(event, fn){
this._callbacks = this._callbacks || {};
(this._callbacks[event] = this._callbacks[event] || [])
.push(fn);
return this;
};
/**
* Adds an `event` listener that will be invoked a single
* time then automatically removed.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.once = function(event, fn){
var self = this;
this._callbacks = this._callbacks || {};
function on() {
self.off(event, on);
fn.apply(this, arguments);
}
on.fn = fn;
this.on(event, on);
return this;
};
/**
* Remove the given callback for `event` or all
* registered callbacks.
*
* @param {String} event
* @param {Function} fn
* @return {Emitter}
* @api public
*/
Emitter.prototype.off =
Emitter.prototype.removeListener =
Emitter.prototype.removeAllListeners =
Emitter.prototype.removeEventListener = function(event, fn){
this._callbacks = this._callbacks || {};
// all
if (0 == arguments.length) {
this._callbacks = {};
return this;
}
// specific event
var callbacks = this._callbacks[event];
if (!callbacks) return this;
// remove all handlers
if (1 == arguments.length) {
delete this._callbacks[event];
return this;
}
// remove specific handler
var cb;
for (var i = 0; i < callbacks.length; i++) {
cb = callbacks[i];
if (cb === fn || cb.fn === fn) {
callbacks.splice(i, 1);
break;
}
}
return this;
};
/**
* Emit `event` with the given args.
*
* @param {String} event
* @param {Mixed} ...
* @return {Emitter}
*/
Emitter.prototype.emit = function(event){
this._callbacks = this._callbacks || {};
var args = [].slice.call(arguments, 1)
, callbacks = this._callbacks[event];
if (callbacks) {
callbacks = callbacks.slice(0);
for (var i = 0, len = callbacks.length; i < len; ++i) {
callbacks[i].apply(this, args);
}
}