reloadlive 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2194 @@
1
+ 'use strict';
2
+
3
+ var Faye = {
4
+ VERSION: '0.8.9',
5
+
6
+ BAYEUX_VERSION: '1.0',
7
+ ID_LENGTH: 160,
8
+ JSONP_CALLBACK: 'jsonpcallback',
9
+ CONNECTION_TYPES: ['long-polling', 'cross-origin-long-polling', 'callback-polling', 'websocket', 'eventsource', 'in-process'],
10
+
11
+ MANDATORY_CONNECTION_TYPES: ['long-polling', 'callback-polling', 'in-process'],
12
+
13
+ ENV: (typeof global === 'undefined') ? window : global,
14
+
15
+ extend: function(dest, source, overwrite) {
16
+ if (!source) return dest;
17
+ for (var key in source) {
18
+ if (!source.hasOwnProperty(key)) continue;
19
+ if (dest.hasOwnProperty(key) && overwrite === false) continue;
20
+ if (dest[key] !== source[key])
21
+ dest[key] = source[key];
22
+ }
23
+ return dest;
24
+ },
25
+
26
+ random: function(bitlength) {
27
+ bitlength = bitlength || this.ID_LENGTH;
28
+ if (bitlength > 32) {
29
+ var parts = Math.ceil(bitlength / 32),
30
+ string = '';
31
+ while (parts--) string += this.random(32);
32
+ var chars = string.split(''), result = '';
33
+ while (chars.length > 0) result += chars.pop();
34
+ return result;
35
+ }
36
+ var limit = Math.pow(2, bitlength) - 1,
37
+ maxSize = limit.toString(36).length,
38
+ string = Math.floor(Math.random() * limit).toString(36);
39
+
40
+ while (string.length < maxSize) string = '0' + string;
41
+ return string;
42
+ },
43
+
44
+ clientIdFromMessages: function(messages) {
45
+ var first = [].concat(messages)[0];
46
+ return first && first.clientId;
47
+ },
48
+
49
+ copyObject: function(object) {
50
+ var clone, i, key;
51
+ if (object instanceof Array) {
52
+ clone = [];
53
+ i = object.length;
54
+ while (i--) clone[i] = Faye.copyObject(object[i]);
55
+ return clone;
56
+ } else if (typeof object === 'object') {
57
+ clone = (object === null) ? null : {};
58
+ for (key in object) clone[key] = Faye.copyObject(object[key]);
59
+ return clone;
60
+ } else {
61
+ return object;
62
+ }
63
+ },
64
+
65
+ commonElement: function(lista, listb) {
66
+ for (var i = 0, n = lista.length; i < n; i++) {
67
+ if (this.indexOf(listb, lista[i]) !== -1)
68
+ return lista[i];
69
+ }
70
+ return null;
71
+ },
72
+
73
+ indexOf: function(list, needle) {
74
+ if (list.indexOf) return list.indexOf(needle);
75
+
76
+ for (var i = 0, n = list.length; i < n; i++) {
77
+ if (list[i] === needle) return i;
78
+ }
79
+ return -1;
80
+ },
81
+
82
+ map: function(object, callback, context) {
83
+ if (object.map) return object.map(callback, context);
84
+ var result = [];
85
+
86
+ if (object instanceof Array) {
87
+ for (var i = 0, n = object.length; i < n; i++) {
88
+ result.push(callback.call(context || null, object[i], i));
89
+ }
90
+ } else {
91
+ for (var key in object) {
92
+ if (!object.hasOwnProperty(key)) continue;
93
+ result.push(callback.call(context || null, key, object[key]));
94
+ }
95
+ }
96
+ return result;
97
+ },
98
+
99
+ filter: function(array, callback, context) {
100
+ var result = [];
101
+ for (var i = 0, n = array.length; i < n; i++) {
102
+ if (callback.call(context || null, array[i], i))
103
+ result.push(array[i]);
104
+ }
105
+ return result;
106
+ },
107
+
108
+ asyncEach: function(list, iterator, callback, context) {
109
+ var n = list.length,
110
+ i = -1,
111
+ calls = 0,
112
+ looping = false;
113
+
114
+ var iterate = function() {
115
+ calls -= 1;
116
+ i += 1;
117
+ if (i === n) return callback && callback.call(context);
118
+ iterator(list[i], resume);
119
+ };
120
+
121
+ var loop = function() {
122
+ if (looping) return;
123
+ looping = true;
124
+ while (calls > 0) iterate();
125
+ looping = false;
126
+ };
127
+
128
+ var resume = function() {
129
+ calls += 1;
130
+ loop();
131
+ };
132
+ resume();
133
+ },
134
+
135
+ // http://assanka.net/content/tech/2009/09/02/json2-js-vs-prototype/
136
+ toJSON: function(object) {
137
+ if (this.stringify)
138
+ return this.stringify(object, function(key, value) {
139
+ return (this[key] instanceof Array)
140
+ ? this[key]
141
+ : value;
142
+ });
143
+
144
+ return JSON.stringify(object);
145
+ },
146
+
147
+ logger: function(message) {
148
+ if (typeof console !== 'undefined') console.log(message);
149
+ },
150
+
151
+ timestamp: function() {
152
+ var date = new Date(),
153
+ year = date.getFullYear(),
154
+ month = date.getMonth() + 1,
155
+ day = date.getDate(),
156
+ hour = date.getHours(),
157
+ minute = date.getMinutes(),
158
+ second = date.getSeconds();
159
+
160
+ var pad = function(n) {
161
+ return n < 10 ? '0' + n : String(n);
162
+ };
163
+
164
+ return pad(year) + '-' + pad(month) + '-' + pad(day) + ' ' +
165
+ pad(hour) + ':' + pad(minute) + ':' + pad(second);
166
+ }
167
+ };
168
+
169
+ if (typeof window !== 'undefined')
170
+ window.Faye = Faye;
171
+
172
+
173
+ Faye.Class = function(parent, methods) {
174
+ if (typeof parent !== 'function') {
175
+ methods = parent;
176
+ parent = Object;
177
+ }
178
+
179
+ var klass = function() {
180
+ if (!this.initialize) return this;
181
+ return this.initialize.apply(this, arguments) || this;
182
+ };
183
+
184
+ var bridge = function() {};
185
+ bridge.prototype = parent.prototype;
186
+
187
+ klass.prototype = new bridge();
188
+ Faye.extend(klass.prototype, methods);
189
+
190
+ return klass;
191
+ };
192
+
193
+
194
+ Faye.Namespace = Faye.Class({
195
+ initialize: function() {
196
+ this._used = {};
197
+ },
198
+
199
+ exists: function(id) {
200
+ return this._used.hasOwnProperty(id);
201
+ },
202
+
203
+ generate: function() {
204
+ var name = Faye.random();
205
+ while (this._used.hasOwnProperty(name))
206
+ name = Faye.random();
207
+ return this._used[name] = name;
208
+ },
209
+
210
+ release: function(id) {
211
+ delete this._used[id];
212
+ }
213
+ });
214
+
215
+
216
+ Faye.Error = Faye.Class({
217
+ initialize: function(code, params, message) {
218
+ this.code = code;
219
+ this.params = Array.prototype.slice.call(params);
220
+ this.message = message;
221
+ },
222
+
223
+ toString: function() {
224
+ return this.code + ':' +
225
+ this.params.join(',') + ':' +
226
+ this.message;
227
+ }
228
+ });
229
+
230
+ Faye.Error.parse = function(message) {
231
+ message = message || '';
232
+ if (!Faye.Grammar.ERROR.test(message)) return new this(null, [], message);
233
+
234
+ var parts = message.split(':'),
235
+ code = parseInt(parts[0]),
236
+ params = parts[1].split(','),
237
+ message = parts[2];
238
+
239
+ return new this(code, params, message);
240
+ };
241
+
242
+
243
+ Faye.Error.versionMismatch = function() {
244
+ return new this(300, arguments, "Version mismatch").toString();
245
+ };
246
+
247
+ Faye.Error.conntypeMismatch = function() {
248
+ return new this(301, arguments, "Connection types not supported").toString();
249
+ };
250
+
251
+ Faye.Error.extMismatch = function() {
252
+ return new this(302, arguments, "Extension mismatch").toString();
253
+ };
254
+
255
+ Faye.Error.badRequest = function() {
256
+ return new this(400, arguments, "Bad request").toString();
257
+ };
258
+
259
+ Faye.Error.clientUnknown = function() {
260
+ return new this(401, arguments, "Unknown client").toString();
261
+ };
262
+
263
+ Faye.Error.parameterMissing = function() {
264
+ return new this(402, arguments, "Missing required parameter").toString();
265
+ };
266
+
267
+ Faye.Error.channelForbidden = function() {
268
+ return new this(403, arguments, "Forbidden channel").toString();
269
+ };
270
+
271
+ Faye.Error.channelUnknown = function() {
272
+ return new this(404, arguments, "Unknown channel").toString();
273
+ };
274
+
275
+ Faye.Error.channelInvalid = function() {
276
+ return new this(405, arguments, "Invalid channel").toString();
277
+ };
278
+
279
+ Faye.Error.extUnknown = function() {
280
+ return new this(406, arguments, "Unknown extension").toString();
281
+ };
282
+
283
+ Faye.Error.publishFailed = function() {
284
+ return new this(407, arguments, "Failed to publish").toString();
285
+ };
286
+
287
+ Faye.Error.serverError = function() {
288
+ return new this(500, arguments, "Internal server error").toString();
289
+ };
290
+
291
+
292
+
293
+ Faye.Deferrable = {
294
+ callback: function(callback, context) {
295
+ if (!callback) return;
296
+
297
+ if (this._deferredStatus === 'succeeded')
298
+ return callback.apply(context, this._deferredArgs);
299
+
300
+ this._callbacks = this._callbacks || [];
301
+ this._callbacks.push([callback, context]);
302
+ },
303
+
304
+ timeout: function(seconds, message) {
305
+ var _this = this;
306
+ var timer = Faye.ENV.setTimeout(function() {
307
+ _this.setDeferredStatus('failed', message);
308
+ }, seconds * 1000);
309
+ this._timer = timer;
310
+ },
311
+
312
+ errback: function(callback, context) {
313
+ if (!callback) return;
314
+
315
+ if (this._deferredStatus === 'failed')
316
+ return callback.apply(context, this._deferredArgs);
317
+
318
+ this._errbacks = this._errbacks || [];
319
+ this._errbacks.push([callback, context]);
320
+ },
321
+
322
+ setDeferredStatus: function() {
323
+ if (this._timer)
324
+ Faye.ENV.clearTimeout(this._timer);
325
+
326
+ var args = Array.prototype.slice.call(arguments),
327
+ status = args.shift(),
328
+ callbacks;
329
+
330
+ this._deferredStatus = status;
331
+ this._deferredArgs = args;
332
+
333
+ if (status === 'succeeded')
334
+ callbacks = this._callbacks;
335
+ else if (status === 'failed')
336
+ callbacks = this._errbacks;
337
+
338
+ if (!callbacks) return;
339
+
340
+ var callback;
341
+ while (callback = callbacks.shift())
342
+ callback[0].apply(callback[1], this._deferredArgs);
343
+ }
344
+ };
345
+
346
+
347
+ Faye.Publisher = {
348
+ countListeners: function(eventType) {
349
+ if (!this._subscribers || !this._subscribers[eventType]) return 0;
350
+ return this._subscribers[eventType].length;
351
+ },
352
+
353
+ bind: function(eventType, listener, context) {
354
+ this._subscribers = this._subscribers || {};
355
+ var list = this._subscribers[eventType] = this._subscribers[eventType] || [];
356
+ list.push([listener, context]);
357
+ },
358
+
359
+ unbind: function(eventType, listener, context) {
360
+ if (!this._subscribers || !this._subscribers[eventType]) return;
361
+
362
+ if (!listener) {
363
+ delete this._subscribers[eventType];
364
+ return;
365
+ }
366
+ var list = this._subscribers[eventType],
367
+ i = list.length;
368
+
369
+ while (i--) {
370
+ if (listener !== list[i][0]) continue;
371
+ if (context && list[i][1] !== context) continue;
372
+ list.splice(i,1);
373
+ }
374
+ },
375
+
376
+ trigger: function() {
377
+ var args = Array.prototype.slice.call(arguments),
378
+ eventType = args.shift();
379
+
380
+ if (!this._subscribers || !this._subscribers[eventType]) return;
381
+
382
+ var listeners = this._subscribers[eventType].slice(),
383
+ listener;
384
+
385
+ for (var i = 0, n = listeners.length; i < n; i++) {
386
+ listener = listeners[i];
387
+ listener[0].apply(listener[1], args);
388
+ }
389
+ }
390
+ };
391
+
392
+
393
+ Faye.Timeouts = {
394
+ addTimeout: function(name, delay, callback, context) {
395
+ this._timeouts = this._timeouts || {};
396
+ if (this._timeouts.hasOwnProperty(name)) return;
397
+ var self = this;
398
+ this._timeouts[name] = Faye.ENV.setTimeout(function() {
399
+ delete self._timeouts[name];
400
+ callback.call(context);
401
+ }, 1000 * delay);
402
+ },
403
+
404
+ removeTimeout: function(name) {
405
+ this._timeouts = this._timeouts || {};
406
+ var timeout = this._timeouts[name];
407
+ if (!timeout) return;
408
+ clearTimeout(timeout);
409
+ delete this._timeouts[name];
410
+ }
411
+ };
412
+
413
+
414
+ Faye.Logging = {
415
+ LOG_LEVELS: {
416
+ error: 3,
417
+ warn: 2,
418
+ info: 1,
419
+ debug: 0
420
+ },
421
+
422
+ logLevel: 'error',
423
+
424
+ log: function(messageArgs, level) {
425
+ if (!Faye.logger) return;
426
+
427
+ var levels = Faye.Logging.LOG_LEVELS;
428
+ if (levels[Faye.Logging.logLevel] > levels[level]) return;
429
+
430
+ var messageArgs = Array.prototype.slice.apply(messageArgs),
431
+ banner = ' [' + level.toUpperCase() + '] [Faye',
432
+ klass = this.className,
433
+
434
+ message = messageArgs.shift().replace(/\?/g, function() {
435
+ try {
436
+ return Faye.toJSON(messageArgs.shift());
437
+ } catch (e) {
438
+ return '[Object]';
439
+ }
440
+ });
441
+
442
+ for (var key in Faye) {
443
+ if (klass) continue;
444
+ if (typeof Faye[key] !== 'function') continue;
445
+ if (this instanceof Faye[key]) klass = key;
446
+ }
447
+ if (klass) banner += '.' + klass;
448
+ banner += '] ';
449
+
450
+ Faye.logger(Faye.timestamp() + banner + message);
451
+ }
452
+ };
453
+
454
+ (function() {
455
+ for (var key in Faye.Logging.LOG_LEVELS)
456
+ (function(level, value) {
457
+ Faye.Logging[level] = function() {
458
+ this.log(arguments, level);
459
+ };
460
+ })(key, Faye.Logging.LOG_LEVELS[key]);
461
+ })();
462
+
463
+
464
+ Faye.Grammar = {
465
+
466
+ LOWALPHA: /^[a-z]$/,
467
+
468
+ UPALPHA: /^[A-Z]$/,
469
+
470
+ ALPHA: /^([a-z]|[A-Z])$/,
471
+
472
+ DIGIT: /^[0-9]$/,
473
+
474
+ ALPHANUM: /^(([a-z]|[A-Z])|[0-9])$/,
475
+
476
+ MARK: /^(\-|\_|\!|\~|\(|\)|\$|\@)$/,
477
+
478
+ STRING: /^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*$/,
479
+
480
+ TOKEN: /^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+$/,
481
+
482
+ INTEGER: /^([0-9])+$/,
483
+
484
+ CHANNEL_SEGMENT: /^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+$/,
485
+
486
+ CHANNEL_SEGMENTS: /^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*$/,
487
+
488
+ CHANNEL_NAME: /^\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*$/,
489
+
490
+ WILD_CARD: /^\*{1,2}$/,
491
+
492
+ CHANNEL_PATTERN: /^(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*\/\*{1,2}$/,
493
+
494
+ VERSION_ELEMENT: /^(([a-z]|[A-Z])|[0-9])(((([a-z]|[A-Z])|[0-9])|\-|\_))*$/,
495
+
496
+ VERSION: /^([0-9])+(\.(([a-z]|[A-Z])|[0-9])(((([a-z]|[A-Z])|[0-9])|\-|\_))*)*$/,
497
+
498
+ CLIENT_ID: /^((([a-z]|[A-Z])|[0-9]))+$/,
499
+
500
+ ID: /^((([a-z]|[A-Z])|[0-9]))+$/,
501
+
502
+ ERROR_MESSAGE: /^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*$/,
503
+
504
+ ERROR_ARGS: /^(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*(,(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)*$/,
505
+
506
+ ERROR_CODE: /^[0-9][0-9][0-9]$/,
507
+
508
+ ERROR: /^([0-9][0-9][0-9]:(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*(,(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)*:(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*|[0-9][0-9][0-9]::(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)| |\/|\*|\.))*)$/
509
+
510
+ };
511
+
512
+
513
+ Faye.Extensible = {
514
+ addExtension: function(extension) {
515
+ this._extensions = this._extensions || [];
516
+ this._extensions.push(extension);
517
+ if (extension.added) extension.added(this);
518
+ },
519
+
520
+ removeExtension: function(extension) {
521
+ if (!this._extensions) return;
522
+ var i = this._extensions.length;
523
+ while (i--) {
524
+ if (this._extensions[i] !== extension) continue;
525
+ this._extensions.splice(i,1);
526
+ if (extension.removed) extension.removed(this);
527
+ }
528
+ },
529
+
530
+ pipeThroughExtensions: function(stage, message, callback, context) {
531
+ this.debug('Passing through ? extensions: ?', stage, message);
532
+
533
+ if (!this._extensions) return callback.call(context, message);
534
+ var extensions = this._extensions.slice();
535
+
536
+ var pipe = function(message) {
537
+ if (!message) return callback.call(context, message);
538
+
539
+ var extension = extensions.shift();
540
+ if (!extension) return callback.call(context, message);
541
+
542
+ if (extension[stage]) extension[stage](message, pipe);
543
+ else pipe(message);
544
+ };
545
+ pipe(message);
546
+ }
547
+ };
548
+
549
+ Faye.extend(Faye.Extensible, Faye.Logging);
550
+
551
+ Faye.Channel = Faye.Class({
552
+ initialize: function(name) {
553
+ this.id = this.name = name;
554
+ },
555
+
556
+ push: function(message) {
557
+ this.trigger('message', message);
558
+ },
559
+
560
+ isUnused: function() {
561
+ return this.countListeners('message') === 0;
562
+ }
563
+ });
564
+
565
+ Faye.extend(Faye.Channel.prototype, Faye.Publisher);
566
+
567
+ Faye.extend(Faye.Channel, {
568
+ HANDSHAKE: '/meta/handshake',
569
+ CONNECT: '/meta/connect',
570
+ SUBSCRIBE: '/meta/subscribe',
571
+ UNSUBSCRIBE: '/meta/unsubscribe',
572
+ DISCONNECT: '/meta/disconnect',
573
+
574
+ META: 'meta',
575
+ SERVICE: 'service',
576
+
577
+ expand: function(name) {
578
+ var segments = this.parse(name),
579
+ channels = ['/**', name];
580
+
581
+ var copy = segments.slice();
582
+ copy[copy.length - 1] = '*';
583
+ channels.push(this.unparse(copy));
584
+
585
+ for (var i = 1, n = segments.length; i < n; i++) {
586
+ copy = segments.slice(0, i);
587
+ copy.push('**');
588
+ channels.push(this.unparse(copy));
589
+ }
590
+
591
+ return channels;
592
+ },
593
+
594
+ isValid: function(name) {
595
+ return Faye.Grammar.CHANNEL_NAME.test(name) ||
596
+ Faye.Grammar.CHANNEL_PATTERN.test(name);
597
+ },
598
+
599
+ parse: function(name) {
600
+ if (!this.isValid(name)) return null;
601
+ return name.split('/').slice(1);
602
+ },
603
+
604
+ unparse: function(segments) {
605
+ return '/' + segments.join('/');
606
+ },
607
+
608
+ isMeta: function(name) {
609
+ var segments = this.parse(name);
610
+ return segments ? (segments[0] === this.META) : null;
611
+ },
612
+
613
+ isService: function(name) {
614
+ var segments = this.parse(name);
615
+ return segments ? (segments[0] === this.SERVICE) : null;
616
+ },
617
+
618
+ isSubscribable: function(name) {
619
+ if (!this.isValid(name)) return null;
620
+ return !this.isMeta(name) && !this.isService(name);
621
+ },
622
+
623
+ Set: Faye.Class({
624
+ initialize: function() {
625
+ this._channels = {};
626
+ },
627
+
628
+ getKeys: function() {
629
+ var keys = [];
630
+ for (var key in this._channels) keys.push(key);
631
+ return keys;
632
+ },
633
+
634
+ remove: function(name) {
635
+ delete this._channels[name];
636
+ },
637
+
638
+ hasSubscription: function(name) {
639
+ return this._channels.hasOwnProperty(name);
640
+ },
641
+
642
+ subscribe: function(names, callback, context) {
643
+ if (!callback) return;
644
+ var name;
645
+ for (var i = 0, n = names.length; i < n; i++) {
646
+ name = names[i];
647
+ var channel = this._channels[name] = this._channels[name] || new Faye.Channel(name);
648
+ channel.bind('message', callback, context);
649
+ }
650
+ },
651
+
652
+ unsubscribe: function(name, callback, context) {
653
+ var channel = this._channels[name];
654
+ if (!channel) return false;
655
+ channel.unbind('message', callback, context);
656
+
657
+ if (channel.isUnused()) {
658
+ this.remove(name);
659
+ return true;
660
+ } else {
661
+ return false;
662
+ }
663
+ },
664
+
665
+ distributeMessage: function(message) {
666
+ var channels = Faye.Channel.expand(message.channel);
667
+
668
+ for (var i = 0, n = channels.length; i < n; i++) {
669
+ var channel = this._channels[channels[i]];
670
+ if (channel) channel.trigger('message', message.data);
671
+ }
672
+ }
673
+ })
674
+ });
675
+
676
+
677
+ Faye.Publication = Faye.Class(Faye.Deferrable);
678
+
679
+
680
+ Faye.Subscription = Faye.Class({
681
+ initialize: function(client, channels, callback, context) {
682
+ this._client = client;
683
+ this._channels = channels;
684
+ this._callback = callback;
685
+ this._context = context;
686
+ this._cancelled = false;
687
+ },
688
+
689
+ cancel: function() {
690
+ if (this._cancelled) return;
691
+ this._client.unsubscribe(this._channels, this._callback, this._context);
692
+ this._cancelled = true;
693
+ },
694
+
695
+ unsubscribe: function() {
696
+ this.cancel();
697
+ }
698
+ });
699
+
700
+ Faye.extend(Faye.Subscription.prototype, Faye.Deferrable);
701
+
702
+
703
+ Faye.Client = Faye.Class({
704
+ UNCONNECTED: 1,
705
+ CONNECTING: 2,
706
+ CONNECTED: 3,
707
+ DISCONNECTED: 4,
708
+
709
+ HANDSHAKE: 'handshake',
710
+ RETRY: 'retry',
711
+ NONE: 'none',
712
+
713
+ CONNECTION_TIMEOUT: 60.0,
714
+ DEFAULT_RETRY: 5.0,
715
+
716
+ DEFAULT_ENDPOINT: '/bayeux',
717
+ INTERVAL: 0.0,
718
+
719
+ initialize: function(endpoint, options) {
720
+ this.info('New client created for ?', endpoint);
721
+
722
+ this._options = options || {};
723
+ this.endpoint = endpoint || this.DEFAULT_ENDPOINT;
724
+ this.endpoints = this._options.endpoints || {};
725
+ this.transports = {};
726
+ this._cookies = Faye.CookieJar && new Faye.CookieJar();
727
+ this._headers = {};
728
+ this._disabled = [];
729
+ this.retry = this._options.retry || this.DEFAULT_RETRY;
730
+
731
+ this._state = this.UNCONNECTED;
732
+ this._channels = new Faye.Channel.Set();
733
+ this._messageId = 0;
734
+
735
+ this._responseCallbacks = {};
736
+
737
+ this._advice = {
738
+ reconnect: this.RETRY,
739
+ interval: 1000 * (this._options.interval || this.INTERVAL),
740
+ timeout: 1000 * (this._options.timeout || this.CONNECTION_TIMEOUT)
741
+ };
742
+
743
+ if (Faye.Event)
744
+ Faye.Event.on(Faye.ENV, 'beforeunload', function() {
745
+ if (Faye.indexOf(this._disabled, 'autodisconnect') < 0)
746
+ this.disconnect();
747
+ }, this);
748
+ },
749
+
750
+ disable: function(feature) {
751
+ this._disabled.push(feature);
752
+ },
753
+
754
+ setHeader: function(name, value) {
755
+ this._headers[name] = value;
756
+ },
757
+
758
+ getClientId: function() {
759
+ return this._clientId;
760
+ },
761
+
762
+ getState: function() {
763
+ switch (this._state) {
764
+ case this.UNCONNECTED: return 'UNCONNECTED';
765
+ case this.CONNECTING: return 'CONNECTING';
766
+ case this.CONNECTED: return 'CONNECTED';
767
+ case this.DISCONNECTED: return 'DISCONNECTED';
768
+ }
769
+ },
770
+
771
+ // Request
772
+ // MUST include: * channel
773
+ // * version
774
+ // * supportedConnectionTypes
775
+ // MAY include: * minimumVersion
776
+ // * ext
777
+ // * id
778
+ //
779
+ // Success Response Failed Response
780
+ // MUST include: * channel MUST include: * channel
781
+ // * version * successful
782
+ // * supportedConnectionTypes * error
783
+ // * clientId MAY include: * supportedConnectionTypes
784
+ // * successful * advice
785
+ // MAY include: * minimumVersion * version
786
+ // * advice * minimumVersion
787
+ // * ext * ext
788
+ // * id * id
789
+ // * authSuccessful
790
+ handshake: function(callback, context) {
791
+ if (this._advice.reconnect === this.NONE) return;
792
+ if (this._state !== this.UNCONNECTED) return;
793
+
794
+ this._state = this.CONNECTING;
795
+ var self = this;
796
+
797
+ this.info('Initiating handshake with ?', this.endpoint);
798
+ this._selectTransport(Faye.MANDATORY_CONNECTION_TYPES);
799
+
800
+ this._send({
801
+ channel: Faye.Channel.HANDSHAKE,
802
+ version: Faye.BAYEUX_VERSION,
803
+ supportedConnectionTypes: [this._transport.connectionType]
804
+
805
+ }, function(response) {
806
+
807
+ if (response.successful) {
808
+ this._state = this.CONNECTED;
809
+ this._clientId = response.clientId;
810
+
811
+ this._selectTransport(response.supportedConnectionTypes);
812
+
813
+ this.info('Handshake successful: ?', this._clientId);
814
+
815
+ this.subscribe(this._channels.getKeys(), true);
816
+ if (callback) callback.call(context);
817
+
818
+ } else {
819
+ this.info('Handshake unsuccessful');
820
+ Faye.ENV.setTimeout(function() { self.handshake(callback, context) }, this._advice.interval);
821
+ this._state = this.UNCONNECTED;
822
+ }
823
+ }, this);
824
+ },
825
+
826
+ // Request Response
827
+ // MUST include: * channel MUST include: * channel
828
+ // * clientId * successful
829
+ // * connectionType * clientId
830
+ // MAY include: * ext MAY include: * error
831
+ // * id * advice
832
+ // * ext
833
+ // * id
834
+ // * timestamp
835
+ connect: function(callback, context) {
836
+ if (this._advice.reconnect === this.NONE) return;
837
+ if (this._state === this.DISCONNECTED) return;
838
+
839
+ if (this._state === this.UNCONNECTED)
840
+ return this.handshake(function() { this.connect(callback, context) }, this);
841
+
842
+ this.callback(callback, context);
843
+ if (this._state !== this.CONNECTED) return;
844
+
845
+ this.info('Calling deferred actions for ?', this._clientId);
846
+ this.setDeferredStatus('succeeded');
847
+ this.setDeferredStatus('deferred');
848
+
849
+ if (this._connectRequest) return;
850
+ this._connectRequest = true;
851
+
852
+ this.info('Initiating connection for ?', this._clientId);
853
+
854
+ this._send({
855
+ channel: Faye.Channel.CONNECT,
856
+ clientId: this._clientId,
857
+ connectionType: this._transport.connectionType
858
+
859
+ }, this._cycleConnection, this);
860
+ },
861
+
862
+ // Request Response
863
+ // MUST include: * channel MUST include: * channel
864
+ // * clientId * successful
865
+ // MAY include: * ext * clientId
866
+ // * id MAY include: * error
867
+ // * ext
868
+ // * id
869
+ disconnect: function() {
870
+ if (this._state !== this.CONNECTED) return;
871
+ this._state = this.DISCONNECTED;
872
+
873
+ this.info('Disconnecting ?', this._clientId);
874
+
875
+ this._send({
876
+ channel: Faye.Channel.DISCONNECT,
877
+ clientId: this._clientId
878
+
879
+ }, function(response) {
880
+ if (response.successful) this._transport.close();
881
+ }, this);
882
+
883
+ this.info('Clearing channel listeners for ?', this._clientId);
884
+ this._channels = new Faye.Channel.Set();
885
+ },
886
+
887
+ // Request Response
888
+ // MUST include: * channel MUST include: * channel
889
+ // * clientId * successful
890
+ // * subscription * clientId
891
+ // MAY include: * ext * subscription
892
+ // * id MAY include: * error
893
+ // * advice
894
+ // * ext
895
+ // * id
896
+ // * timestamp
897
+ subscribe: function(channel, callback, context) {
898
+ if (channel instanceof Array)
899
+ return Faye.map(channel, function(c) {
900
+ return this.subscribe(c, callback, context);
901
+ }, this);
902
+
903
+ var subscription = new Faye.Subscription(this, channel, callback, context),
904
+ force = (callback === true),
905
+ hasSubscribe = this._channels.hasSubscription(channel);
906
+
907
+ if (hasSubscribe && !force) {
908
+ this._channels.subscribe([channel], callback, context);
909
+ subscription.setDeferredStatus('succeeded');
910
+ return subscription;
911
+ }
912
+
913
+ this.connect(function() {
914
+ this.info('Client ? attempting to subscribe to ?', this._clientId, channel);
915
+ if (!force) this._channels.subscribe([channel], callback, context);
916
+
917
+ this._send({
918
+ channel: Faye.Channel.SUBSCRIBE,
919
+ clientId: this._clientId,
920
+ subscription: channel
921
+
922
+ }, function(response) {
923
+ if (!response.successful) {
924
+ subscription.setDeferredStatus('failed', Faye.Error.parse(response.error));
925
+ return this._channels.unsubscribe(channel, callback, context);
926
+ }
927
+
928
+ var channels = [].concat(response.subscription);
929
+ this.info('Subscription acknowledged for ? to ?', this._clientId, channels);
930
+ subscription.setDeferredStatus('succeeded');
931
+ }, this);
932
+ }, this);
933
+
934
+ return subscription;
935
+ },
936
+
937
+ // Request Response
938
+ // MUST include: * channel MUST include: * channel
939
+ // * clientId * successful
940
+ // * subscription * clientId
941
+ // MAY include: * ext * subscription
942
+ // * id MAY include: * error
943
+ // * advice
944
+ // * ext
945
+ // * id
946
+ // * timestamp
947
+ unsubscribe: function(channel, callback, context) {
948
+ if (channel instanceof Array)
949
+ return Faye.map(channel, function(c) {
950
+ return this.unsubscribe(c, callback, context);
951
+ }, this);
952
+
953
+ var dead = this._channels.unsubscribe(channel, callback, context);
954
+ if (!dead) return;
955
+
956
+ this.connect(function() {
957
+ this.info('Client ? attempting to unsubscribe from ?', this._clientId, channel);
958
+
959
+ this._send({
960
+ channel: Faye.Channel.UNSUBSCRIBE,
961
+ clientId: this._clientId,
962
+ subscription: channel
963
+
964
+ }, function(response) {
965
+ if (!response.successful) return;
966
+
967
+ var channels = [].concat(response.subscription);
968
+ this.info('Unsubscription acknowledged for ? from ?', this._clientId, channels);
969
+ }, this);
970
+ }, this);
971
+ },
972
+
973
+ // Request Response
974
+ // MUST include: * channel MUST include: * channel
975
+ // * data * successful
976
+ // MAY include: * clientId MAY include: * id
977
+ // * id * error
978
+ // * ext * ext
979
+ publish: function(channel, data) {
980
+ var publication = new Faye.Publication();
981
+
982
+ this.connect(function() {
983
+ this.info('Client ? queueing published message to ?: ?', this._clientId, channel, data);
984
+
985
+ this._send({
986
+ channel: channel,
987
+ data: data,
988
+ clientId: this._clientId
989
+ }, function(response) {
990
+ if (response.successful)
991
+ publication.setDeferredStatus('succeeded');
992
+ else
993
+ publication.setDeferredStatus('failed', Faye.Error.parse(response.error));
994
+ }, this);
995
+ }, this);
996
+
997
+ return publication;
998
+ },
999
+
1000
+ receiveMessage: function(message) {
1001
+ this.pipeThroughExtensions('incoming', message, function(message) {
1002
+ if (!message) return;
1003
+
1004
+ if (message.advice) this._handleAdvice(message.advice);
1005
+ this._deliverMessage(message);
1006
+
1007
+ if (message.successful === undefined) return;
1008
+
1009
+ var callback = this._responseCallbacks[message.id];
1010
+ if (!callback) return;
1011
+
1012
+ delete this._responseCallbacks[message.id];
1013
+ callback[0].call(callback[1], message);
1014
+ }, this);
1015
+ },
1016
+
1017
+ _selectTransport: function(transportTypes) {
1018
+ Faye.Transport.get(this, transportTypes, this._disabled, function(transport) {
1019
+ this.debug('Selected ? transport for ?', transport.connectionType, transport.endpoint);
1020
+
1021
+ if (transport === this._transport) return;
1022
+ if (this._transport) this._transport.close();
1023
+
1024
+ this._transport = transport;
1025
+ this._transport.cookies = this._cookies;
1026
+ this._transport.headers = this._headers;
1027
+
1028
+ transport.bind('down', function() {
1029
+ if (this._transportUp !== undefined && !this._transportUp) return;
1030
+ this._transportUp = false;
1031
+ this.trigger('transport:down');
1032
+ }, this);
1033
+
1034
+ transport.bind('up', function() {
1035
+ if (this._transportUp !== undefined && this._transportUp) return;
1036
+ this._transportUp = true;
1037
+ this.trigger('transport:up');
1038
+ }, this);
1039
+ }, this);
1040
+ },
1041
+
1042
+ _send: function(message, callback, context) {
1043
+ message.id = this._generateMessageId();
1044
+ if (callback) this._responseCallbacks[message.id] = [callback, context];
1045
+
1046
+ this.pipeThroughExtensions('outgoing', message, function(message) {
1047
+ if (!message) return;
1048
+ this._transport.send(message, this._advice.timeout / 1000);
1049
+ }, this);
1050
+ },
1051
+
1052
+ _generateMessageId: function() {
1053
+ this._messageId += 1;
1054
+ if (this._messageId >= Math.pow(2,32)) this._messageId = 0;
1055
+ return this._messageId.toString(36);
1056
+ },
1057
+
1058
+ _handleAdvice: function(advice) {
1059
+ Faye.extend(this._advice, advice);
1060
+
1061
+ if (this._advice.reconnect === this.HANDSHAKE && this._state !== this.DISCONNECTED) {
1062
+ this._state = this.UNCONNECTED;
1063
+ this._clientId = null;
1064
+ this._cycleConnection();
1065
+ }
1066
+ },
1067
+
1068
+ _deliverMessage: function(message) {
1069
+ if (!message.channel || message.data === undefined) return;
1070
+ this.info('Client ? calling listeners for ? with ?', this._clientId, message.channel, message.data);
1071
+ this._channels.distributeMessage(message);
1072
+ },
1073
+
1074
+ _teardownConnection: function() {
1075
+ if (!this._connectRequest) return;
1076
+ this._connectRequest = null;
1077
+ this.info('Closed connection for ?', this._clientId);
1078
+ },
1079
+
1080
+ _cycleConnection: function() {
1081
+ this._teardownConnection();
1082
+ var self = this;
1083
+ Faye.ENV.setTimeout(function() { self.connect() }, this._advice.interval);
1084
+ }
1085
+ });
1086
+
1087
+ Faye.extend(Faye.Client.prototype, Faye.Deferrable);
1088
+ Faye.extend(Faye.Client.prototype, Faye.Publisher);
1089
+ Faye.extend(Faye.Client.prototype, Faye.Logging);
1090
+ Faye.extend(Faye.Client.prototype, Faye.Extensible);
1091
+
1092
+
1093
+ Faye.Transport = Faye.extend(Faye.Class({
1094
+ MAX_DELAY: 0.0,
1095
+ batching: true,
1096
+
1097
+ initialize: function(client, endpoint) {
1098
+ this._client = client;
1099
+ this.endpoint = endpoint;
1100
+ this._outbox = [];
1101
+ },
1102
+
1103
+ close: function() {},
1104
+
1105
+ send: function(message, timeout) {
1106
+ this.debug('Client ? sending message to ?: ?',
1107
+ this._client._clientId, this.endpoint, message);
1108
+
1109
+ if (!this.batching) return this.request([message], timeout);
1110
+
1111
+ this._outbox.push(message);
1112
+ this._timeout = timeout;
1113
+
1114
+ if (message.channel === Faye.Channel.HANDSHAKE)
1115
+ return this.addTimeout('publish', 0.01, this.flush, this);
1116
+
1117
+ if (message.channel === Faye.Channel.CONNECT)
1118
+ this._connectMessage = message;
1119
+
1120
+ if (this.shouldFlush && this.shouldFlush(this._outbox))
1121
+ return this.flush();
1122
+
1123
+ this.addTimeout('publish', this.MAX_DELAY, this.flush, this);
1124
+ },
1125
+
1126
+ flush: function() {
1127
+ this.removeTimeout('publish');
1128
+
1129
+ if (this._outbox.length > 1 && this._connectMessage)
1130
+ this._connectMessage.advice = {timeout: 0};
1131
+
1132
+ this.request(this._outbox, this._timeout);
1133
+
1134
+ this._connectMessage = null;
1135
+ this._outbox = [];
1136
+ },
1137
+
1138
+ receive: function(responses) {
1139
+ this.debug('Client ? received from ?: ?',
1140
+ this._client._clientId, this.endpoint, responses);
1141
+
1142
+ for (var i = 0, n = responses.length; i < n; i++) {
1143
+ this._client.receiveMessage(responses[i]);
1144
+ }
1145
+ },
1146
+
1147
+ retry: function(message, timeout) {
1148
+ var called = false,
1149
+ retry = this._client.retry * 1000,
1150
+ self = this;
1151
+
1152
+ return function() {
1153
+ if (called) return;
1154
+ called = true;
1155
+ Faye.ENV.setTimeout(function() { self.request(message, timeout) }, retry);
1156
+ };
1157
+ }
1158
+
1159
+ }), {
1160
+ MAX_URL_LENGTH: 2048,
1161
+
1162
+ get: function(client, allowed, disabled, callback, context) {
1163
+ var endpoint = client.endpoint;
1164
+
1165
+ Faye.asyncEach(this._transports, function(pair, resume) {
1166
+ var connType = pair[0], klass = pair[1],
1167
+ connEndpoint = client.endpoints[connType] || endpoint;
1168
+
1169
+ if (Faye.indexOf(disabled, connType) >= 0)
1170
+ return resume();
1171
+
1172
+ if (Faye.indexOf(allowed, connType) < 0) {
1173
+ klass.isUsable(client, connEndpoint, function() {});
1174
+ return resume();
1175
+ }
1176
+
1177
+ klass.isUsable(client, connEndpoint, function(isUsable) {
1178
+ if (!isUsable) return resume();
1179
+ var transport = klass.hasOwnProperty('create') ? klass.create(client, connEndpoint) : new klass(client, connEndpoint);
1180
+ callback.call(context, transport);
1181
+ });
1182
+ }, function() {
1183
+ throw new Error('Could not find a usable connection type for ' + endpoint);
1184
+ });
1185
+ },
1186
+
1187
+ register: function(type, klass) {
1188
+ this._transports.push([type, klass]);
1189
+ klass.prototype.connectionType = type;
1190
+ },
1191
+
1192
+ _transports: []
1193
+ });
1194
+
1195
+ Faye.extend(Faye.Transport.prototype, Faye.Logging);
1196
+ Faye.extend(Faye.Transport.prototype, Faye.Publisher);
1197
+ Faye.extend(Faye.Transport.prototype, Faye.Timeouts);
1198
+
1199
+
1200
+ Faye.Event = {
1201
+ _registry: [],
1202
+
1203
+ on: function(element, eventName, callback, context) {
1204
+ var wrapped = function() { callback.call(context) };
1205
+
1206
+ if (element.addEventListener)
1207
+ element.addEventListener(eventName, wrapped, false);
1208
+ else
1209
+ element.attachEvent('on' + eventName, wrapped);
1210
+
1211
+ this._registry.push({
1212
+ _element: element,
1213
+ _type: eventName,
1214
+ _callback: callback,
1215
+ _context: context,
1216
+ _handler: wrapped
1217
+ });
1218
+ },
1219
+
1220
+ detach: function(element, eventName, callback, context) {
1221
+ var i = this._registry.length, register;
1222
+ while (i--) {
1223
+ register = this._registry[i];
1224
+
1225
+ if ((element && element !== register._element) ||
1226
+ (eventName && eventName !== register._type) ||
1227
+ (callback && callback !== register._callback) ||
1228
+ (context && context !== register._context))
1229
+ continue;
1230
+
1231
+ if (register._element.removeEventListener)
1232
+ register._element.removeEventListener(register._type, register._handler, false);
1233
+ else
1234
+ register._element.detachEvent('on' + register._type, register._handler);
1235
+
1236
+ this._registry.splice(i,1);
1237
+ register = null;
1238
+ }
1239
+ }
1240
+ };
1241
+
1242
+ Faye.Event.on(Faye.ENV, 'unload', Faye.Event.detach, Faye.Event);
1243
+
1244
+
1245
+ Faye.URI = Faye.extend(Faye.Class({
1246
+ queryString: function() {
1247
+ var pairs = [];
1248
+ for (var key in this.params) {
1249
+ if (!this.params.hasOwnProperty(key)) continue;
1250
+ pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(this.params[key]));
1251
+ }
1252
+ return pairs.join('&');
1253
+ },
1254
+
1255
+ isSameOrigin: function() {
1256
+ var host = Faye.URI.parse(Faye.ENV.location.href, false);
1257
+
1258
+ var external = (host.hostname !== this.hostname) ||
1259
+ (host.port !== this.port) ||
1260
+ (host.protocol !== this.protocol);
1261
+
1262
+ return !external;
1263
+ },
1264
+
1265
+ toURL: function() {
1266
+ var query = this.queryString();
1267
+ return this.protocol + '//' + this.hostname + (this.port ? ':' + this.port : '') +
1268
+ this.pathname + (query ? '?' + query : '') + this.hash;
1269
+ }
1270
+ }), {
1271
+ parse: function(url, params) {
1272
+ if (typeof url !== 'string') return url;
1273
+ var uri = new this(), parts;
1274
+
1275
+ var consume = function(name, pattern, infer) {
1276
+ url = url.replace(pattern, function(match) {
1277
+ uri[name] = match;
1278
+ return '';
1279
+ });
1280
+ if (uri[name] === undefined)
1281
+ uri[name] = infer ? Faye.ENV.location[name] : '';
1282
+ };
1283
+
1284
+ consume('protocol', /^https?\:/, true);
1285
+ consume('host', /^\/\/[^\/]+/, true);
1286
+
1287
+ if (!/^\//.test(url)) url = Faye.ENV.location.pathname.replace(/[^\/]*$/, '') + url;
1288
+ consume('pathname', /^\/[^\?#]*/);
1289
+ consume('search', /^\?[^#]*/);
1290
+ consume('hash', /^#.*/);
1291
+
1292
+ if (/^\/\//.test(uri.host)) {
1293
+ uri.host = uri.host.substr(2);
1294
+ parts = uri.host.split(':');
1295
+ uri.hostname = parts[0];
1296
+ uri.port = parts[1] || '';
1297
+ } else {
1298
+ uri.hostname = Faye.ENV.location.hostname;
1299
+ uri.port = Faye.ENV.location.port;
1300
+ }
1301
+
1302
+ if (params === false) {
1303
+ uri.params = {};
1304
+ } else {
1305
+ var query = uri.search.replace(/^\?/, ''),
1306
+ pairs = query ? query.split('&') : [],
1307
+ n = pairs.length,
1308
+ data = {};
1309
+
1310
+ while (n--) {
1311
+ parts = pairs[n].split('=');
1312
+ data[decodeURIComponent(parts[0] || '')] = decodeURIComponent(parts[1] || '');
1313
+ }
1314
+ if (typeof params === 'object') Faye.extend(data, params);
1315
+
1316
+ uri.params = data;
1317
+ }
1318
+
1319
+ return uri;
1320
+ }
1321
+ });
1322
+
1323
+
1324
+ /*
1325
+ http://www.JSON.org/json2.js
1326
+ 2009-04-16
1327
+
1328
+ Public Domain.
1329
+
1330
+ NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
1331
+
1332
+ See http://www.JSON.org/js.html
1333
+
1334
+ This file creates a global JSON object containing two methods: stringify
1335
+ and parse.
1336
+
1337
+ JSON.stringify(value, replacer, space)
1338
+ value any JavaScript value, usually an object or array.
1339
+
1340
+ replacer an optional parameter that determines how object
1341
+ values are stringified for objects. It can be a
1342
+ function or an array of strings.
1343
+
1344
+ space an optional parameter that specifies the indentation
1345
+ of nested structures. If it is omitted, the text will
1346
+ be packed without extra whitespace. If it is a number,
1347
+ it will specify the number of spaces to indent at each
1348
+ level. If it is a string (such as '\t' or '&nbsp;'),
1349
+ it contains the characters used to indent at each level.
1350
+
1351
+ This method produces a JSON text from a JavaScript value.
1352
+
1353
+ When an object value is found, if the object contains a toJSON
1354
+ method, its toJSON method will be called and the result will be
1355
+ stringified. A toJSON method does not serialize: it returns the
1356
+ value represented by the name/value pair that should be serialized,
1357
+ or undefined if nothing should be serialized. The toJSON method
1358
+ will be passed the key associated with the value, and this will be
1359
+ bound to the object holding the key.
1360
+
1361
+ For example, this would serialize Dates as ISO strings.
1362
+
1363
+ Date.prototype.toJSON = function (key) {
1364
+ function f(n) {
1365
+ // Format integers to have at least two digits.
1366
+ return n < 10 ? '0' + n : n;
1367
+ }
1368
+
1369
+ return this.getUTCFullYear() + '-' +
1370
+ f(this.getUTCMonth() + 1) + '-' +
1371
+ f(this.getUTCDate()) + 'T' +
1372
+ f(this.getUTCHours()) + ':' +
1373
+ f(this.getUTCMinutes()) + ':' +
1374
+ f(this.getUTCSeconds()) + 'Z';
1375
+ };
1376
+
1377
+ You can provide an optional replacer method. It will be passed the
1378
+ key and value of each member, with this bound to the containing
1379
+ object. The value that is returned from your method will be
1380
+ serialized. If your method returns undefined, then the member will
1381
+ be excluded from the serialization.
1382
+
1383
+ If the replacer parameter is an array of strings, then it will be
1384
+ used to select the members to be serialized. It filters the results
1385
+ such that only members with keys listed in the replacer array are
1386
+ stringified.
1387
+
1388
+ Values that do not have JSON representations, such as undefined or
1389
+ functions, will not be serialized. Such values in objects will be
1390
+ dropped; in arrays they will be replaced with null. You can use
1391
+ a replacer function to replace those with JSON values.
1392
+ JSON.stringify(undefined) returns undefined.
1393
+
1394
+ The optional space parameter produces a stringification of the
1395
+ value that is filled with line breaks and indentation to make it
1396
+ easier to read.
1397
+
1398
+ If the space parameter is a non-empty string, then that string will
1399
+ be used for indentation. If the space parameter is a number, then
1400
+ the indentation will be that many spaces.
1401
+
1402
+ Example:
1403
+
1404
+ text = JSON.stringify(['e', {pluribus: 'unum'}]);
1405
+ // text is '["e",{"pluribus":"unum"}]'
1406
+
1407
+
1408
+ text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t');
1409
+ // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
1410
+
1411
+ text = JSON.stringify([new Date()], function (key, value) {
1412
+ return this[key] instanceof Date ?
1413
+ 'Date(' + this[key] + ')' : value;
1414
+ });
1415
+ // text is '["Date(---current time---)"]'
1416
+
1417
+
1418
+ JSON.parse(text, reviver)
1419
+ This method parses a JSON text to produce an object or array.
1420
+ It can throw a SyntaxError exception.
1421
+
1422
+ The optional reviver parameter is a function that can filter and
1423
+ transform the results. It receives each of the keys and values,
1424
+ and its return value is used instead of the original value.
1425
+ If it returns what it received, then the structure is not modified.
1426
+ If it returns undefined then the member is deleted.
1427
+
1428
+ Example:
1429
+
1430
+ // Parse the text. Values that look like ISO date strings will
1431
+ // be converted to Date objects.
1432
+
1433
+ myData = JSON.parse(text, function (key, value) {
1434
+ var a;
1435
+ if (typeof value === 'string') {
1436
+ a =
1437
+ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
1438
+ if (a) {
1439
+ return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4],
1440
+ +a[5], +a[6]));
1441
+ }
1442
+ }
1443
+ return value;
1444
+ });
1445
+
1446
+ myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) {
1447
+ var d;
1448
+ if (typeof value === 'string' &&
1449
+ value.slice(0, 5) === 'Date(' &&
1450
+ value.slice(-1) === ')') {
1451
+ d = new Date(value.slice(5, -1));
1452
+ if (d) {
1453
+ return d;
1454
+ }
1455
+ }
1456
+ return value;
1457
+ });
1458
+
1459
+
1460
+ This is a reference implementation. You are free to copy, modify, or
1461
+ redistribute.
1462
+
1463
+ This code should be minified before deployment.
1464
+ See http://javascript.crockford.com/jsmin.html
1465
+
1466
+ USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
1467
+ NOT CONTROL.
1468
+ */
1469
+
1470
+ /*jslint evil: true */
1471
+
1472
+ /*global JSON */
1473
+
1474
+ /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply,
1475
+ call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
1476
+ getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
1477
+ lastIndex, length, parse, prototype, push, replace, slice, stringify,
1478
+ test, toJSON, toString, valueOf
1479
+ */
1480
+
1481
+ // Create a JSON object only if one does not already exist. We create the
1482
+ // methods in a closure to avoid creating global variables.
1483
+
1484
+ if (!this.JSON) {
1485
+ JSON = {};
1486
+ }
1487
+ (function () {
1488
+
1489
+ function f(n) {
1490
+ // Format integers to have at least two digits.
1491
+ return n < 10 ? '0' + n : n;
1492
+ }
1493
+
1494
+ if (typeof Date.prototype.toJSON !== 'function') {
1495
+
1496
+ Date.prototype.toJSON = function (key) {
1497
+
1498
+ return this.getUTCFullYear() + '-' +
1499
+ f(this.getUTCMonth() + 1) + '-' +
1500
+ f(this.getUTCDate()) + 'T' +
1501
+ f(this.getUTCHours()) + ':' +
1502
+ f(this.getUTCMinutes()) + ':' +
1503
+ f(this.getUTCSeconds()) + 'Z';
1504
+ };
1505
+
1506
+ String.prototype.toJSON =
1507
+ Number.prototype.toJSON =
1508
+ Boolean.prototype.toJSON = function (key) {
1509
+ return this.valueOf();
1510
+ };
1511
+ }
1512
+
1513
+ var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
1514
+ escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
1515
+ gap,
1516
+ indent,
1517
+ meta = { // table of character substitutions
1518
+ '\b': '\\b',
1519
+ '\t': '\\t',
1520
+ '\n': '\\n',
1521
+ '\f': '\\f',
1522
+ '\r': '\\r',
1523
+ '"' : '\\"',
1524
+ '\\': '\\\\'
1525
+ },
1526
+ rep;
1527
+
1528
+
1529
+ function quote(string) {
1530
+
1531
+ // If the string contains no control characters, no quote characters, and no
1532
+ // backslash characters, then we can safely slap some quotes around it.
1533
+ // Otherwise we must also replace the offending characters with safe escape
1534
+ // sequences.
1535
+
1536
+ escapable.lastIndex = 0;
1537
+ return escapable.test(string) ?
1538
+ '"' + string.replace(escapable, function (a) {
1539
+ var c = meta[a];
1540
+ return typeof c === 'string' ? c :
1541
+ '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
1542
+ }) + '"' :
1543
+ '"' + string + '"';
1544
+ }
1545
+
1546
+
1547
+ function str(key, holder) {
1548
+
1549
+ // Produce a string from holder[key].
1550
+
1551
+ var i, // The loop counter.
1552
+ k, // The member key.
1553
+ v, // The member value.
1554
+ length,
1555
+ mind = gap,
1556
+ partial,
1557
+ value = holder[key];
1558
+
1559
+ // If the value has a toJSON method, call it to obtain a replacement value.
1560
+
1561
+ if (value && typeof value === 'object' &&
1562
+ typeof value.toJSON === 'function') {
1563
+ value = value.toJSON(key);
1564
+ }
1565
+
1566
+ // If we were called with a replacer function, then call the replacer to
1567
+ // obtain a replacement value.
1568
+
1569
+ if (typeof rep === 'function') {
1570
+ value = rep.call(holder, key, value);
1571
+ }
1572
+
1573
+ // What happens next depends on the value's type.
1574
+
1575
+ switch (typeof value) {
1576
+ case 'string':
1577
+ return quote(value);
1578
+
1579
+ case 'number':
1580
+
1581
+ // JSON numbers must be finite. Encode non-finite numbers as null.
1582
+
1583
+ return isFinite(value) ? String(value) : 'null';
1584
+
1585
+ case 'boolean':
1586
+ case 'null':
1587
+
1588
+ // If the value is a boolean or null, convert it to a string. Note:
1589
+ // typeof null does not produce 'null'. The case is included here in
1590
+ // the remote chance that this gets fixed someday.
1591
+
1592
+ return String(value);
1593
+
1594
+ // If the type is 'object', we might be dealing with an object or an array or
1595
+ // null.
1596
+
1597
+ case 'object':
1598
+
1599
+ // Due to a specification blunder in ECMAScript, typeof null is 'object',
1600
+ // so watch out for that case.
1601
+
1602
+ if (!value) {
1603
+ return 'null';
1604
+ }
1605
+
1606
+ // Make an array to hold the partial results of stringifying this object value.
1607
+
1608
+ gap += indent;
1609
+ partial = [];
1610
+
1611
+ // Is the value an array?
1612
+
1613
+ if (Object.prototype.toString.apply(value) === '[object Array]') {
1614
+
1615
+ // The value is an array. Stringify every element. Use null as a placeholder
1616
+ // for non-JSON values.
1617
+
1618
+ length = value.length;
1619
+ for (i = 0; i < length; i += 1) {
1620
+ partial[i] = str(i, value) || 'null';
1621
+ }
1622
+
1623
+ // Join all of the elements together, separated with commas, and wrap them in
1624
+ // brackets.
1625
+
1626
+ v = partial.length === 0 ? '[]' :
1627
+ gap ? '[\n' + gap +
1628
+ partial.join(',\n' + gap) + '\n' +
1629
+ mind + ']' :
1630
+ '[' + partial.join(',') + ']';
1631
+ gap = mind;
1632
+ return v;
1633
+ }
1634
+
1635
+ // If the replacer is an array, use it to select the members to be stringified.
1636
+
1637
+ if (rep && typeof rep === 'object') {
1638
+ length = rep.length;
1639
+ for (i = 0; i < length; i += 1) {
1640
+ k = rep[i];
1641
+ if (typeof k === 'string') {
1642
+ v = str(k, value);
1643
+ if (v) {
1644
+ partial.push(quote(k) + (gap ? ': ' : ':') + v);
1645
+ }
1646
+ }
1647
+ }
1648
+ } else {
1649
+
1650
+ // Otherwise, iterate through all of the keys in the object.
1651
+
1652
+ for (k in value) {
1653
+ if (Object.hasOwnProperty.call(value, k)) {
1654
+ v = str(k, value);
1655
+ if (v) {
1656
+ partial.push(quote(k) + (gap ? ': ' : ':') + v);
1657
+ }
1658
+ }
1659
+ }
1660
+ }
1661
+
1662
+ // Join all of the member texts together, separated with commas,
1663
+ // and wrap them in braces.
1664
+
1665
+ v = partial.length === 0 ? '{}' :
1666
+ gap ? '{\n' + gap + partial.join(',\n' + gap) + '\n' +
1667
+ mind + '}' : '{' + partial.join(',') + '}';
1668
+ gap = mind;
1669
+ return v;
1670
+ }
1671
+ }
1672
+
1673
+ // If the JSON object does not yet have a stringify method, give it one.
1674
+
1675
+ // NOTE we've hacked this to expose this method to Faye. We need to use this
1676
+ // to avoid problems with buggy Firefox version and bad #toJSON implementations
1677
+
1678
+ Faye.stringify = function (value, replacer, space) {
1679
+
1680
+ // The stringify method takes a value and an optional replacer, and an optional
1681
+ // space parameter, and returns a JSON text. The replacer can be a function
1682
+ // that can replace values, or an array of strings that will select the keys.
1683
+ // A default replacer method can be provided. Use of the space parameter can
1684
+ // produce text that is more easily readable.
1685
+
1686
+ var i;
1687
+ gap = '';
1688
+ indent = '';
1689
+
1690
+ // If the space parameter is a number, make an indent string containing that
1691
+ // many spaces.
1692
+
1693
+ if (typeof space === 'number') {
1694
+ for (i = 0; i < space; i += 1) {
1695
+ indent += ' ';
1696
+ }
1697
+
1698
+ // If the space parameter is a string, it will be used as the indent string.
1699
+
1700
+ } else if (typeof space === 'string') {
1701
+ indent = space;
1702
+ }
1703
+
1704
+ // If there is a replacer, it must be a function or an array.
1705
+ // Otherwise, throw an error.
1706
+
1707
+ rep = replacer;
1708
+ if (replacer && typeof replacer !== 'function' &&
1709
+ (typeof replacer !== 'object' ||
1710
+ typeof replacer.length !== 'number')) {
1711
+ throw new Error('JSON.stringify');
1712
+ }
1713
+
1714
+ // Make a fake root object containing our value under the key of ''.
1715
+ // Return the result of stringifying the value.
1716
+
1717
+ return str('', {'': value});
1718
+ };
1719
+
1720
+ if (typeof JSON.stringify !== 'function') {
1721
+ JSON.stringify = Faye.stringify;
1722
+ }
1723
+
1724
+
1725
+ // If the JSON object does not yet have a parse method, give it one.
1726
+
1727
+ if (typeof JSON.parse !== 'function') {
1728
+ JSON.parse = function (text, reviver) {
1729
+
1730
+ // The parse method takes a text and an optional reviver function, and returns
1731
+ // a JavaScript value if the text is a valid JSON text.
1732
+
1733
+ var j;
1734
+
1735
+ function walk(holder, key) {
1736
+
1737
+ // The walk method is used to recursively walk the resulting structure so
1738
+ // that modifications can be made.
1739
+
1740
+ var k, v, value = holder[key];
1741
+ if (value && typeof value === 'object') {
1742
+ for (k in value) {
1743
+ if (Object.hasOwnProperty.call(value, k)) {
1744
+ v = walk(value, k);
1745
+ if (v !== undefined) {
1746
+ value[k] = v;
1747
+ } else {
1748
+ delete value[k];
1749
+ }
1750
+ }
1751
+ }
1752
+ }
1753
+ return reviver.call(holder, key, value);
1754
+ }
1755
+
1756
+
1757
+ // Parsing happens in four stages. In the first stage, we replace certain
1758
+ // Unicode characters with escape sequences. JavaScript handles many characters
1759
+ // incorrectly, either silently deleting them, or treating them as line endings.
1760
+
1761
+ cx.lastIndex = 0;
1762
+ if (cx.test(text)) {
1763
+ text = text.replace(cx, function (a) {
1764
+ return '\\u' +
1765
+ ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
1766
+ });
1767
+ }
1768
+
1769
+ // In the second stage, we run the text against regular expressions that look
1770
+ // for non-JSON patterns. We are especially concerned with '()' and 'new'
1771
+ // because they can cause invocation, and '=' because it can cause mutation.
1772
+ // But just to be safe, we want to reject all unexpected forms.
1773
+
1774
+ // We split the second stage into 4 regexp operations in order to work around
1775
+ // crippling inefficiencies in IE's and Safari's regexp engines. First we
1776
+ // replace the JSON backslash pairs with '@' (a non-JSON character). Second, we
1777
+ // replace all simple value tokens with ']' characters. Third, we delete all
1778
+ // open brackets that follow a colon or comma or that begin the text. Finally,
1779
+ // we look to see that the remaining characters are only whitespace or ']' or
1780
+ // ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.
1781
+
1782
+ if (/^[\],:{}\s]*$/.
1783
+ test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
1784
+ replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
1785
+ replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
1786
+
1787
+ // In the third stage we use the eval function to compile the text into a
1788
+ // JavaScript structure. The '{' operator is subject to a syntactic ambiguity
1789
+ // in JavaScript: it can begin a block or an object literal. We wrap the text
1790
+ // in parens to eliminate the ambiguity.
1791
+
1792
+ j = eval('(' + text + ')');
1793
+
1794
+ // In the optional fourth stage, we recursively walk the new structure, passing
1795
+ // each name/value pair to a reviver function for possible transformation.
1796
+
1797
+ return typeof reviver === 'function' ?
1798
+ walk({'': j}, '') : j;
1799
+ }
1800
+
1801
+ // If the text is not JSON parseable, then a SyntaxError is thrown.
1802
+
1803
+ throw new SyntaxError('JSON.parse');
1804
+ };
1805
+ }
1806
+ }());
1807
+
1808
+
1809
+ Faye.Transport.WebSocket = Faye.extend(Faye.Class(Faye.Transport, {
1810
+ UNCONNECTED: 1,
1811
+ CONNECTING: 2,
1812
+ CONNECTED: 3,
1813
+
1814
+ batching: false,
1815
+
1816
+ isUsable: function(callback, context) {
1817
+ this.callback(function() { callback.call(context, true) });
1818
+ this.errback(function() { callback.call(context, false) });
1819
+ this.connect();
1820
+ },
1821
+
1822
+ request: function(messages, timeout) {
1823
+ if (messages.length === 0) return;
1824
+ this._messages = this._messages || {};
1825
+
1826
+ for (var i = 0, n = messages.length; i < n; i++) {
1827
+ this._messages[messages[i].id] = messages[i];
1828
+ }
1829
+ this.callback(function(socket) { socket.send(Faye.toJSON(messages)) });
1830
+ this.connect();
1831
+ },
1832
+
1833
+ close: function() {
1834
+ if (!this._socket) return;
1835
+ this._socket.onclose = this._socket.onerror = null;
1836
+ this._socket.close();
1837
+ delete this._socket;
1838
+ this.setDeferredStatus('deferred');
1839
+ this._state = this.UNCONNECTED;
1840
+ },
1841
+
1842
+ connect: function() {
1843
+ if (Faye.Transport.WebSocket._unloaded) return;
1844
+
1845
+ this._state = this._state || this.UNCONNECTED;
1846
+ if (this._state !== this.UNCONNECTED) return;
1847
+
1848
+ this._state = this.CONNECTING;
1849
+
1850
+ var ws = Faye.Transport.WebSocket.getClass();
1851
+ if (!ws) return this.setDeferredStatus('failed');
1852
+
1853
+ this._socket = new ws(Faye.Transport.WebSocket.getSocketUrl(this.endpoint));
1854
+ var self = this;
1855
+
1856
+ this._socket.onopen = function() {
1857
+ self._state = self.CONNECTED;
1858
+ self._everConnected = true;
1859
+ self.setDeferredStatus('succeeded', self._socket);
1860
+ self.trigger('up');
1861
+ };
1862
+
1863
+ this._socket.onmessage = function(event) {
1864
+ var messages = JSON.parse(event.data);
1865
+ if (!messages) return;
1866
+ messages = [].concat(messages);
1867
+
1868
+ for (var i = 0, n = messages.length; i < n; i++) {
1869
+ delete self._messages[messages[i].id];
1870
+ }
1871
+ self.receive(messages);
1872
+ };
1873
+
1874
+ this._socket.onclose = this._socket.onerror = function() {
1875
+ var wasConnected = (self._state === self.CONNECTED);
1876
+ self.setDeferredStatus('deferred');
1877
+ self._state = self.UNCONNECTED;
1878
+
1879
+ self.close();
1880
+
1881
+ if (wasConnected) return self.resend();
1882
+ if (!self._everConnected) return self.setDeferredStatus('failed');
1883
+
1884
+ var retry = self._client.retry * 1000;
1885
+ Faye.ENV.setTimeout(function() { self.connect() }, retry);
1886
+ self.trigger('down');
1887
+ };
1888
+ },
1889
+
1890
+ resend: function() {
1891
+ if (!this._messages) return;
1892
+ var messages = Faye.map(this._messages, function(id, msg) { return msg });
1893
+ this.request(messages);
1894
+ }
1895
+ }), {
1896
+ getSocketUrl: function(endpoint) {
1897
+ if (Faye.URI) endpoint = Faye.URI.parse(endpoint).toURL();
1898
+ return endpoint.replace(/^http(s?):/ig, 'ws$1:');
1899
+ },
1900
+
1901
+ getClass: function() {
1902
+ return (Faye.WebSocket && Faye.WebSocket.Client) ||
1903
+ Faye.ENV.WebSocket ||
1904
+ Faye.ENV.MozWebSocket;
1905
+ },
1906
+
1907
+ isUsable: function(client, endpoint, callback, context) {
1908
+ this.create(client, endpoint).isUsable(callback, context);
1909
+ },
1910
+
1911
+ create: function(client, endpoint) {
1912
+ var sockets = client.transports.websocket = client.transports.websocket || {};
1913
+ sockets[endpoint] = sockets[endpoint] || new this(client, endpoint);
1914
+ return sockets[endpoint];
1915
+ }
1916
+ });
1917
+
1918
+ Faye.extend(Faye.Transport.WebSocket.prototype, Faye.Deferrable);
1919
+ Faye.Transport.register('websocket', Faye.Transport.WebSocket);
1920
+
1921
+ if (Faye.Event)
1922
+ Faye.Event.on(Faye.ENV, 'beforeunload', function() {
1923
+ Faye.Transport.WebSocket._unloaded = true;
1924
+ });
1925
+
1926
+
1927
+ Faye.Transport.EventSource = Faye.extend(Faye.Class(Faye.Transport, {
1928
+ initialize: function(client, endpoint) {
1929
+ Faye.Transport.prototype.initialize.call(this, client, endpoint);
1930
+ if (!Faye.ENV.EventSource) return this.setDeferredStatus('failed');
1931
+
1932
+ this._xhr = new Faye.Transport.XHR(client, endpoint);
1933
+
1934
+ var socket = new EventSource(endpoint + '/' + client.getClientId()),
1935
+ self = this;
1936
+
1937
+ socket.onopen = function() {
1938
+ self._everConnected = true;
1939
+ self.setDeferredStatus('succeeded');
1940
+ self.trigger('up');
1941
+ };
1942
+
1943
+ socket.onerror = function() {
1944
+ if (self._everConnected) {
1945
+ self.trigger('down');
1946
+ } else {
1947
+ self.setDeferredStatus('failed');
1948
+ socket.close();
1949
+ }
1950
+ };
1951
+
1952
+ socket.onmessage = function(event) {
1953
+ self.receive(JSON.parse(event.data));
1954
+ self.trigger('up');
1955
+ };
1956
+
1957
+ this._socket = socket;
1958
+ },
1959
+
1960
+ isUsable: function(callback, context) {
1961
+ this.callback(function() { callback.call(context, true) });
1962
+ this.errback(function() { callback.call(context, false) });
1963
+ },
1964
+
1965
+ request: function(message, timeout) {
1966
+ this._xhr.request(message, timeout);
1967
+ },
1968
+
1969
+ close: function() {
1970
+ if (!this._socket) return;
1971
+ this._socket.onerror = null;
1972
+ this._socket.close();
1973
+ delete this._socket;
1974
+ }
1975
+ }), {
1976
+ isUsable: function(client, endpoint, callback, context) {
1977
+ var id = client.getClientId();
1978
+ if (!id) return callback.call(context, false);
1979
+
1980
+ Faye.Transport.XHR.isUsable(client, endpoint, function(usable) {
1981
+ if (!usable) return callback.call(context, false);
1982
+ this.create(client, endpoint).isUsable(callback, context);
1983
+ }, this);
1984
+ },
1985
+
1986
+ create: function(client, endpoint) {
1987
+ var sockets = client.transports.eventsource = client.transports.eventsource || {},
1988
+ id = client.getClientId(),
1989
+ endpoint = endpoint + '/' + (id || '');
1990
+
1991
+ sockets[endpoint] = sockets[endpoint] || new this(client, endpoint);
1992
+ return sockets[endpoint];
1993
+ }
1994
+ });
1995
+
1996
+ Faye.extend(Faye.Transport.EventSource.prototype, Faye.Deferrable);
1997
+ Faye.Transport.register('eventsource', Faye.Transport.EventSource);
1998
+
1999
+
2000
+ Faye.Transport.XHR = Faye.extend(Faye.Class(Faye.Transport, {
2001
+ request: function(message, timeout) {
2002
+ var retry = this.retry(message, timeout),
2003
+ path = Faye.URI.parse(this.endpoint).pathname,
2004
+ self = this,
2005
+ xhr = Faye.ENV.ActiveXObject
2006
+ ? new ActiveXObject("Microsoft.XMLHTTP")
2007
+ : new XMLHttpRequest();
2008
+
2009
+ xhr.open('POST', path, true);
2010
+ xhr.setRequestHeader('Content-Type', 'application/json');
2011
+ xhr.setRequestHeader('Pragma', 'no-cache');
2012
+ xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
2013
+
2014
+ var headers = this.headers;
2015
+ for (var key in headers) {
2016
+ if (!headers.hasOwnProperty(key)) continue;
2017
+ xhr.setRequestHeader(key, headers[key]);
2018
+ }
2019
+
2020
+ var abort = function() { xhr.abort() };
2021
+ Faye.Event.on(Faye.ENV, 'beforeunload', abort);
2022
+
2023
+ var cleanUp = function() {
2024
+ Faye.Event.detach(Faye.ENV, 'beforeunload', abort);
2025
+ xhr.onreadystatechange = function() {};
2026
+ xhr = null;
2027
+ };
2028
+
2029
+ xhr.onreadystatechange = function() {
2030
+ if (xhr.readyState !== 4) return;
2031
+
2032
+ var parsedMessage = null,
2033
+ status = xhr.status,
2034
+ successful = ((status >= 200 && status < 300) ||
2035
+ status === 304 ||
2036
+ status === 1223);
2037
+
2038
+ if (!successful) {
2039
+ cleanUp();
2040
+ retry();
2041
+ return self.trigger('down');
2042
+ }
2043
+
2044
+ try {
2045
+ parsedMessage = JSON.parse(xhr.responseText);
2046
+ } catch (e) {}
2047
+
2048
+ cleanUp();
2049
+
2050
+ if (parsedMessage) {
2051
+ self.receive(parsedMessage);
2052
+ self.trigger('up');
2053
+ } else {
2054
+ retry();
2055
+ self.trigger('down');
2056
+ }
2057
+ };
2058
+
2059
+ xhr.send(Faye.toJSON(message));
2060
+ }
2061
+ }), {
2062
+ isUsable: function(client, endpoint, callback, context) {
2063
+ callback.call(context, Faye.URI.parse(endpoint).isSameOrigin());
2064
+ }
2065
+ });
2066
+
2067
+ Faye.Transport.register('long-polling', Faye.Transport.XHR);
2068
+
2069
+ Faye.Transport.CORS = Faye.extend(Faye.Class(Faye.Transport, {
2070
+ request: function(message, timeout) {
2071
+ var xhrClass = Faye.ENV.XDomainRequest ? XDomainRequest : XMLHttpRequest,
2072
+ xhr = new xhrClass(),
2073
+ retry = this.retry(message, timeout),
2074
+ self = this;
2075
+
2076
+ xhr.open('POST', this.endpoint, true);
2077
+ if (xhr.setRequestHeader) xhr.setRequestHeader('Pragma', 'no-cache');
2078
+
2079
+ var cleanUp = function() {
2080
+ if (!xhr) return false;
2081
+ xhr.onload = xhr.onerror = xhr.ontimeout = xhr.onprogress = null;
2082
+ xhr = null;
2083
+ Faye.ENV.clearTimeout(timer);
2084
+ return true;
2085
+ };
2086
+
2087
+ xhr.onload = function() {
2088
+ var parsedMessage = null;
2089
+ try {
2090
+ parsedMessage = JSON.parse(xhr.responseText);
2091
+ } catch (e) {}
2092
+
2093
+ cleanUp();
2094
+
2095
+ if (parsedMessage) {
2096
+ self.receive(parsedMessage);
2097
+ self.trigger('up');
2098
+ } else {
2099
+ retry();
2100
+ self.trigger('down');
2101
+ }
2102
+ };
2103
+
2104
+ var onerror = function() {
2105
+ cleanUp();
2106
+ retry();
2107
+ self.trigger('down');
2108
+ };
2109
+ var timer = Faye.ENV.setTimeout(onerror, 1.5 * 1000 * timeout);
2110
+ xhr.onerror = onerror;
2111
+ xhr.ontimeout = onerror;
2112
+
2113
+ xhr.onprogress = function() {};
2114
+ xhr.send('message=' + encodeURIComponent(Faye.toJSON(message)));
2115
+ }
2116
+ }), {
2117
+ isUsable: function(client, endpoint, callback, context) {
2118
+ if (Faye.URI.parse(endpoint).isSameOrigin())
2119
+ return callback.call(context, false);
2120
+
2121
+ if (Faye.ENV.XDomainRequest)
2122
+ return callback.call(context, Faye.URI.parse(endpoint).protocol ===
2123
+ Faye.URI.parse(Faye.ENV.location).protocol);
2124
+
2125
+ if (Faye.ENV.XMLHttpRequest) {
2126
+ var xhr = new Faye.ENV.XMLHttpRequest();
2127
+ return callback.call(context, xhr.withCredentials !== undefined);
2128
+ }
2129
+ return callback.call(context, false);
2130
+ }
2131
+ });
2132
+
2133
+ Faye.Transport.register('cross-origin-long-polling', Faye.Transport.CORS);
2134
+
2135
+
2136
+ Faye.Transport.JSONP = Faye.extend(Faye.Class(Faye.Transport, {
2137
+ shouldFlush: function(messages) {
2138
+ var params = {
2139
+ message: Faye.toJSON(messages),
2140
+ jsonp: '__jsonp' + Faye.Transport.JSONP._cbCount + '__'
2141
+ };
2142
+ var location = Faye.URI.parse(this.endpoint, params).toURL();
2143
+ return location.length >= Faye.Transport.MAX_URL_LENGTH;
2144
+ },
2145
+
2146
+ request: function(messages, timeout) {
2147
+ var params = {message: Faye.toJSON(messages)},
2148
+ head = document.getElementsByTagName('head')[0],
2149
+ script = document.createElement('script'),
2150
+ callbackName = Faye.Transport.JSONP.getCallbackName(),
2151
+ location = Faye.URI.parse(this.endpoint, params),
2152
+ retry = this.retry(messages, timeout),
2153
+ self = this;
2154
+
2155
+ Faye.ENV[callbackName] = function(data) {
2156
+ cleanUp();
2157
+ self.receive(data);
2158
+ self.trigger('up');
2159
+ };
2160
+
2161
+ var timer = Faye.ENV.setTimeout(function() {
2162
+ cleanUp();
2163
+ retry();
2164
+ self.trigger('down');
2165
+ }, 1.5 * 1000 * timeout);
2166
+
2167
+ var cleanUp = function() {
2168
+ if (!Faye.ENV[callbackName]) return false;
2169
+ Faye.ENV[callbackName] = undefined;
2170
+ try { delete Faye.ENV[callbackName] } catch (e) {}
2171
+ Faye.ENV.clearTimeout(timer);
2172
+ script.parentNode.removeChild(script);
2173
+ return true;
2174
+ };
2175
+
2176
+ location.params.jsonp = callbackName;
2177
+ script.type = 'text/javascript';
2178
+ script.src = location.toURL();
2179
+ head.appendChild(script);
2180
+ }
2181
+ }), {
2182
+ _cbCount: 0,
2183
+
2184
+ getCallbackName: function() {
2185
+ this._cbCount += 1;
2186
+ return '__jsonp' + this._cbCount + '__';
2187
+ },
2188
+
2189
+ isUsable: function(client, endpoint, callback, context) {
2190
+ callback.call(context, true);
2191
+ }
2192
+ });
2193
+
2194
+ Faye.Transport.register('callback-polling', Faye.Transport.JSONP);