epicenter-libs 3.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/CHANGELOG.md +345 -0
  2. package/LICENSE.md +37 -0
  3. package/README.md +134 -0
  4. package/dist/browser/AckExtension-e67c6a28.js +129 -0
  5. package/dist/browser/AckExtension-e67c6a28.js.map +1 -0
  6. package/dist/browser/ReloadExtension-b1e50033.js +253 -0
  7. package/dist/browser/ReloadExtension-b1e50033.js.map +1 -0
  8. package/dist/browser/cometd-eeabdcd4.js +3438 -0
  9. package/dist/browser/cometd-eeabdcd4.js.map +1 -0
  10. package/dist/browser/epicenter-2cce2971.js +6086 -0
  11. package/dist/browser/epicenter-2cce2971.js.map +1 -0
  12. package/dist/browser/epicenter.js +2 -0
  13. package/dist/browser/epicenter.js.map +1 -0
  14. package/dist/cjs/AckExtension-f5178e19.js +131 -0
  15. package/dist/cjs/AckExtension-f5178e19.js.map +1 -0
  16. package/dist/cjs/ReloadExtension-65b036ba.js +255 -0
  17. package/dist/cjs/ReloadExtension-65b036ba.js.map +1 -0
  18. package/dist/cjs/cometd-473408f4.js +3441 -0
  19. package/dist/cjs/cometd-473408f4.js.map +1 -0
  20. package/dist/cjs/epicenter-12ceb814.js +7248 -0
  21. package/dist/cjs/epicenter-12ceb814.js.map +1 -0
  22. package/dist/cjs/epicenter.js +54 -0
  23. package/dist/cjs/epicenter.js.map +1 -0
  24. package/dist/epicenter.js +9895 -0
  25. package/dist/epicenter.js.map +1 -0
  26. package/dist/epicenter.min.js +2 -0
  27. package/dist/epicenter.min.js.map +1 -0
  28. package/dist/module/AckExtension-6181d8b5.js +129 -0
  29. package/dist/module/AckExtension-6181d8b5.js.map +1 -0
  30. package/dist/module/ReloadExtension-eaa8c42c.js +253 -0
  31. package/dist/module/ReloadExtension-eaa8c42c.js.map +1 -0
  32. package/dist/module/cometd-af78008d.js +3438 -0
  33. package/dist/module/cometd-af78008d.js.map +1 -0
  34. package/dist/module/epicenter-9b8c92a9.js +7213 -0
  35. package/dist/module/epicenter-9b8c92a9.js.map +1 -0
  36. package/dist/module/epicenter.js +7 -0
  37. package/dist/module/epicenter.js.map +1 -0
  38. package/dist/types/adapters/account.d.ts +44 -0
  39. package/dist/types/adapters/admin.d.ts +33 -0
  40. package/dist/types/adapters/asset.d.ts +46 -0
  41. package/dist/types/adapters/authentication.d.ts +62 -0
  42. package/dist/types/adapters/channel.d.ts +39 -0
  43. package/dist/types/adapters/chat.d.ts +105 -0
  44. package/dist/types/adapters/cometd.d.ts +25 -0
  45. package/dist/types/adapters/email.d.ts +86 -0
  46. package/dist/types/adapters/episode.d.ts +91 -0
  47. package/dist/types/adapters/group.d.ts +273 -0
  48. package/dist/types/adapters/index.d.ts +21 -0
  49. package/dist/types/adapters/leaderboard.d.ts +68 -0
  50. package/dist/types/adapters/presence.d.ts +35 -0
  51. package/dist/types/adapters/project.d.ts +99 -0
  52. package/dist/types/adapters/recaptcha.d.ts +1 -0
  53. package/dist/types/adapters/run.d.ts +253 -0
  54. package/dist/types/adapters/task.d.ts +154 -0
  55. package/dist/types/adapters/time.d.ts +2 -0
  56. package/dist/types/adapters/user.d.ts +38 -0
  57. package/dist/types/adapters/vault.d.ts +94 -0
  58. package/dist/types/adapters/world.d.ts +230 -0
  59. package/dist/types/epicenter.d.ts +10 -0
  60. package/dist/types/utils/config.d.ts +90 -0
  61. package/dist/types/utils/constants.d.ts +290 -0
  62. package/dist/types/utils/cookies.d.ts +16 -0
  63. package/dist/types/utils/error-manager.d.ts +21 -0
  64. package/dist/types/utils/error.d.ts +4 -0
  65. package/dist/types/utils/fault.d.ts +17 -0
  66. package/dist/types/utils/helpers.d.ts +4 -0
  67. package/dist/types/utils/identification.d.ts +47 -0
  68. package/dist/types/utils/index.d.ts +11 -0
  69. package/dist/types/utils/result.d.ts +6 -0
  70. package/dist/types/utils/router.d.ts +157 -0
  71. package/dist/types/utils/store.d.ts +31 -0
  72. package/package.json +103 -0
  73. package/src/adapters/account.ts +104 -0
  74. package/src/adapters/admin.ts +53 -0
  75. package/src/adapters/asset.ts +195 -0
  76. package/src/adapters/authentication.ts +173 -0
  77. package/src/adapters/channel.ts +83 -0
  78. package/src/adapters/chat.ts +186 -0
  79. package/src/adapters/cometd.ts +297 -0
  80. package/src/adapters/email.ts +146 -0
  81. package/src/adapters/episode.ts +163 -0
  82. package/src/adapters/group.ts +511 -0
  83. package/src/adapters/index.ts +43 -0
  84. package/src/adapters/leaderboard.ts +122 -0
  85. package/src/adapters/presence.ts +63 -0
  86. package/src/adapters/project.ts +123 -0
  87. package/src/adapters/recaptcha.ts +11 -0
  88. package/src/adapters/run.ts +726 -0
  89. package/src/adapters/task.ts +213 -0
  90. package/src/adapters/time.ts +36 -0
  91. package/src/adapters/user.ts +75 -0
  92. package/src/adapters/vault.ts +232 -0
  93. package/src/adapters/world.ts +412 -0
  94. package/src/epicenter.ts +96 -0
  95. package/src/globals.d.ts +16 -0
  96. package/src/utils/config.ts +168 -0
  97. package/src/utils/constants.ts +324 -0
  98. package/src/utils/cookies.ts +71 -0
  99. package/src/utils/error-manager.ts +66 -0
  100. package/src/utils/error.ts +9 -0
  101. package/src/utils/fault.ts +39 -0
  102. package/src/utils/helpers.ts +7 -0
  103. package/src/utils/identification.ts +128 -0
  104. package/src/utils/index.ts +11 -0
  105. package/src/utils/result.ts +15 -0
  106. package/src/utils/router.ts +547 -0
  107. package/src/utils/store.ts +82 -0
@@ -0,0 +1,3438 @@
1
+ import { c as createCommonjsModule, a as commonjsGlobal } from './epicenter-2cce2971.js';
2
+
3
+ function _mergeNamespaces(n, m) {
4
+ m.forEach(function (e) {
5
+ Object.keys(e).forEach(function (k) {
6
+ if (k !== 'default' && !(k in n)) {
7
+ var d = Object.getOwnPropertyDescriptor(e, k);
8
+ Object.defineProperty(n, k, d.get ? d : {
9
+ enumerable: true,
10
+ get: function () { return e[k]; }
11
+ });
12
+ }
13
+ });
14
+ });
15
+ return Object.freeze(n);
16
+ }
17
+
18
+ var cometd = createCommonjsModule(function (module, exports) {
19
+ /*
20
+ * Copyright (c) 2008-2020 the original author or authors.
21
+ *
22
+ * Licensed under the Apache License, Version 2.0 (the "License");
23
+ * you may not use this file except in compliance with the License.
24
+ * You may obtain a copy of the License at
25
+ *
26
+ * http://www.apache.org/licenses/LICENSE-2.0
27
+ *
28
+ * Unless required by applicable law or agreed to in writing, software
29
+ * distributed under the License is distributed on an "AS IS" BASIS,
30
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31
+ * See the License for the specific language governing permissions and
32
+ * limitations under the License.
33
+ */
34
+
35
+ /* CometD Version 4.0.9 */
36
+
37
+ (function(root, factory) {
38
+ {
39
+ // CommonJS.
40
+ module.exports = factory();
41
+ }
42
+ }(commonjsGlobal, function() {
43
+ /**
44
+ * Browsers may throttle the Window scheduler,
45
+ * so we may replace it with a Worker scheduler.
46
+ */
47
+ var Scheduler = function() {
48
+ var _ids = 0;
49
+ var _tasks = {};
50
+ this.register = function(funktion) {
51
+ var id = ++_ids;
52
+ _tasks[id] = funktion;
53
+ return id;
54
+ };
55
+ this.unregister = function(id) {
56
+ var funktion = _tasks[id];
57
+ delete _tasks[id];
58
+ return funktion;
59
+ };
60
+ this.setTimeout = function(funktion, delay) {
61
+ return window.setTimeout(funktion, delay);
62
+ };
63
+ this.clearTimeout = function(id) {
64
+ window.clearTimeout(id);
65
+ };
66
+ };
67
+
68
+ /**
69
+ * The scheduler code that will run in the Worker.
70
+ * Workers have a built-in `self` variable similar to `window`.
71
+ */
72
+ function WorkerScheduler() {
73
+ var _tasks = {};
74
+ self.onmessage = function(e) {
75
+ var cmd = e.data;
76
+ var id = _tasks[cmd.id];
77
+ switch (cmd.type) {
78
+ case 'setTimeout':
79
+ _tasks[cmd.id] = self.setTimeout(function() {
80
+ delete _tasks[cmd.id];
81
+ self.postMessage({
82
+ id: cmd.id
83
+ });
84
+ }, cmd.delay);
85
+ break;
86
+ case 'clearTimeout':
87
+ delete _tasks[cmd.id];
88
+ if (id) {
89
+ self.clearTimeout(id);
90
+ }
91
+ break;
92
+ default:
93
+ throw 'Unknown command ' + cmd.type;
94
+ }
95
+ };
96
+ }
97
+
98
+
99
+ /**
100
+ * Utility functions.
101
+ */
102
+ var Utils = {
103
+ isString: function(value) {
104
+ if (value === undefined || value === null) {
105
+ return false;
106
+ }
107
+ return typeof value === 'string' || value instanceof String;
108
+ },
109
+ isArray: function(value) {
110
+ if (value === undefined || value === null) {
111
+ return false;
112
+ }
113
+ return value instanceof Array;
114
+ },
115
+ /**
116
+ * Returns whether the given element is contained into the given array.
117
+ * @param element the element to check presence for
118
+ * @param array the array to check for the element presence
119
+ * @return the index of the element, if present, or a negative index if the element is not present
120
+ */
121
+ inArray: function(element, array) {
122
+ for (var i = 0; i < array.length; ++i) {
123
+ if (element === array[i]) {
124
+ return i;
125
+ }
126
+ }
127
+ return -1;
128
+ }
129
+ };
130
+
131
+
132
+ /**
133
+ * A registry for transports used by the CometD object.
134
+ */
135
+ var TransportRegistry = function() {
136
+ var _types = [];
137
+ var _transports = {};
138
+
139
+ this.getTransportTypes = function() {
140
+ return _types.slice(0);
141
+ };
142
+
143
+ this.findTransportTypes = function(version, crossDomain, url) {
144
+ var result = [];
145
+ for (var i = 0; i < _types.length; ++i) {
146
+ var type = _types[i];
147
+ if (_transports[type].accept(version, crossDomain, url) === true) {
148
+ result.push(type);
149
+ }
150
+ }
151
+ return result;
152
+ };
153
+
154
+ this.negotiateTransport = function(types, version, crossDomain, url) {
155
+ for (var i = 0; i < _types.length; ++i) {
156
+ var type = _types[i];
157
+ for (var j = 0; j < types.length; ++j) {
158
+ if (type === types[j]) {
159
+ var transport = _transports[type];
160
+ if (transport.accept(version, crossDomain, url) === true) {
161
+ return transport;
162
+ }
163
+ }
164
+ }
165
+ }
166
+ return null;
167
+ };
168
+
169
+ this.add = function(type, transport, index) {
170
+ var existing = false;
171
+ for (var i = 0; i < _types.length; ++i) {
172
+ if (_types[i] === type) {
173
+ existing = true;
174
+ break;
175
+ }
176
+ }
177
+
178
+ if (!existing) {
179
+ if (typeof index !== 'number') {
180
+ _types.push(type);
181
+ } else {
182
+ _types.splice(index, 0, type);
183
+ }
184
+ _transports[type] = transport;
185
+ }
186
+
187
+ return !existing;
188
+ };
189
+
190
+ this.find = function(type) {
191
+ for (var i = 0; i < _types.length; ++i) {
192
+ if (_types[i] === type) {
193
+ return _transports[type];
194
+ }
195
+ }
196
+ return null;
197
+ };
198
+
199
+ this.remove = function(type) {
200
+ for (var i = 0; i < _types.length; ++i) {
201
+ if (_types[i] === type) {
202
+ _types.splice(i, 1);
203
+ var transport = _transports[type];
204
+ delete _transports[type];
205
+ return transport;
206
+ }
207
+ }
208
+ return null;
209
+ };
210
+
211
+ this.clear = function() {
212
+ _types = [];
213
+ _transports = {};
214
+ };
215
+
216
+ this.reset = function(init) {
217
+ for (var i = 0; i < _types.length; ++i) {
218
+ _transports[_types[i]].reset(init);
219
+ }
220
+ };
221
+ };
222
+
223
+
224
+ /**
225
+ * Base object with the common functionality for transports.
226
+ */
227
+ var Transport = function() {
228
+ var _type;
229
+ var _cometd;
230
+ var _url;
231
+
232
+ /**
233
+ * Function invoked just after a transport has been successfully registered.
234
+ * @param type the type of transport (for example 'long-polling')
235
+ * @param cometd the cometd object this transport has been registered to
236
+ * @see #unregistered()
237
+ */
238
+ this.registered = function(type, cometd) {
239
+ _type = type;
240
+ _cometd = cometd;
241
+ };
242
+
243
+ /**
244
+ * Function invoked just after a transport has been successfully unregistered.
245
+ * @see #registered(type, cometd)
246
+ */
247
+ this.unregistered = function() {
248
+ _type = null;
249
+ _cometd = null;
250
+ };
251
+
252
+ this._debug = function() {
253
+ _cometd._debug.apply(_cometd, arguments);
254
+ };
255
+
256
+ this._mixin = function() {
257
+ return _cometd._mixin.apply(_cometd, arguments);
258
+ };
259
+
260
+ this.getConfiguration = function() {
261
+ return _cometd.getConfiguration();
262
+ };
263
+
264
+ this.getAdvice = function() {
265
+ return _cometd.getAdvice();
266
+ };
267
+
268
+ this.setTimeout = function(funktion, delay) {
269
+ return _cometd.setTimeout(funktion, delay);
270
+ };
271
+
272
+ this.clearTimeout = function(id) {
273
+ _cometd.clearTimeout(id);
274
+ };
275
+
276
+ /**
277
+ * Converts the given response into an array of bayeux messages
278
+ * @param response the response to convert
279
+ * @return an array of bayeux messages obtained by converting the response
280
+ */
281
+ this.convertToMessages = function(response) {
282
+ if (Utils.isString(response)) {
283
+ try {
284
+ return JSON.parse(response);
285
+ } catch (x) {
286
+ this._debug('Could not convert to JSON the following string', '"' + response + '"');
287
+ throw x;
288
+ }
289
+ }
290
+ if (Utils.isArray(response)) {
291
+ return response;
292
+ }
293
+ if (response === undefined || response === null) {
294
+ return [];
295
+ }
296
+ if (response instanceof Object) {
297
+ return [response];
298
+ }
299
+ throw 'Conversion Error ' + response + ', typeof ' + (typeof response);
300
+ };
301
+
302
+ /**
303
+ * Returns whether this transport can work for the given version and cross domain communication case.
304
+ * @param version a string indicating the transport version
305
+ * @param crossDomain a boolean indicating whether the communication is cross domain
306
+ * @param url the URL to connect to
307
+ * @return true if this transport can work for the given version and cross domain communication case,
308
+ * false otherwise
309
+ */
310
+ this.accept = function(version, crossDomain, url) {
311
+ throw 'Abstract';
312
+ };
313
+
314
+ /**
315
+ * Returns the type of this transport.
316
+ * @see #registered(type, cometd)
317
+ */
318
+ this.getType = function() {
319
+ return _type;
320
+ };
321
+
322
+ this.getURL = function() {
323
+ return _url;
324
+ };
325
+
326
+ this.setURL = function(url) {
327
+ _url = url;
328
+ };
329
+
330
+ this.send = function(envelope, metaConnect) {
331
+ throw 'Abstract';
332
+ };
333
+
334
+ this.reset = function(init) {
335
+ this._debug('Transport', _type, 'reset', init ? 'initial' : 'retry');
336
+ };
337
+
338
+ this.abort = function() {
339
+ this._debug('Transport', _type, 'aborted');
340
+ };
341
+
342
+ this.toString = function() {
343
+ return this.getType();
344
+ };
345
+ };
346
+
347
+ Transport.derive = function(baseObject) {
348
+ function F() {
349
+ }
350
+
351
+ F.prototype = baseObject;
352
+ return new F();
353
+ };
354
+
355
+
356
+ /**
357
+ * Base object with the common functionality for transports based on requests.
358
+ * The key responsibility is to allow at most 2 outstanding requests to the server,
359
+ * to avoid that requests are sent behind a long poll.
360
+ * To achieve this, we have one reserved request for the long poll, and all other
361
+ * requests are serialized one after the other.
362
+ */
363
+ var RequestTransport = function() {
364
+ var _super = new Transport();
365
+ var _self = Transport.derive(_super);
366
+ var _requestIds = 0;
367
+ var _metaConnectRequest = null;
368
+ var _requests = [];
369
+ var _envelopes = [];
370
+
371
+ function _coalesceEnvelopes(envelope) {
372
+ while (_envelopes.length > 0) {
373
+ var envelopeAndRequest = _envelopes[0];
374
+ var newEnvelope = envelopeAndRequest[0];
375
+ var newRequest = envelopeAndRequest[1];
376
+ if (newEnvelope.url === envelope.url &&
377
+ newEnvelope.sync === envelope.sync) {
378
+ _envelopes.shift();
379
+ envelope.messages = envelope.messages.concat(newEnvelope.messages);
380
+ this._debug('Coalesced', newEnvelope.messages.length, 'messages from request', newRequest.id);
381
+ continue;
382
+ }
383
+ break;
384
+ }
385
+ }
386
+
387
+ function _transportSend(envelope, request) {
388
+ this.transportSend(envelope, request);
389
+ request.expired = false;
390
+
391
+ if (!envelope.sync) {
392
+ var maxDelay = this.getConfiguration().maxNetworkDelay;
393
+ var delay = maxDelay;
394
+ if (request.metaConnect === true) {
395
+ delay += this.getAdvice().timeout;
396
+ }
397
+
398
+ this._debug('Transport', this.getType(), 'waiting at most', delay, 'ms for the response, maxNetworkDelay', maxDelay);
399
+
400
+ var self = this;
401
+ request.timeout = this.setTimeout(function() {
402
+ request.expired = true;
403
+ var errorMessage = 'Request ' + request.id + ' of transport ' + self.getType() + ' exceeded ' + delay + ' ms max network delay';
404
+ var failure = {
405
+ reason: errorMessage
406
+ };
407
+ var xhr = request.xhr;
408
+ failure.httpCode = self.xhrStatus(xhr);
409
+ self.abortXHR(xhr);
410
+ self._debug(errorMessage);
411
+ self.complete(request, false, request.metaConnect);
412
+ envelope.onFailure(xhr, envelope.messages, failure);
413
+ }, delay);
414
+ }
415
+ }
416
+
417
+ function _queueSend(envelope) {
418
+ var requestId = ++_requestIds;
419
+ var request = {
420
+ id: requestId,
421
+ metaConnect: false,
422
+ envelope: envelope
423
+ };
424
+
425
+ // Consider the /meta/connect requests which should always be present.
426
+ if (_requests.length < this.getConfiguration().maxConnections - 1) {
427
+ _requests.push(request);
428
+ _transportSend.call(this, envelope, request);
429
+ } else {
430
+ this._debug('Transport', this.getType(), 'queueing request', requestId, 'envelope', envelope);
431
+ _envelopes.push([envelope, request]);
432
+ }
433
+ }
434
+
435
+ function _metaConnectComplete(request) {
436
+ var requestId = request.id;
437
+ this._debug('Transport', this.getType(), '/meta/connect complete, request', requestId);
438
+ if (_metaConnectRequest !== null && _metaConnectRequest.id !== requestId) {
439
+ throw '/meta/connect request mismatch, completing request ' + requestId;
440
+ }
441
+ _metaConnectRequest = null;
442
+ }
443
+
444
+ function _complete(request, success) {
445
+ var index = Utils.inArray(request, _requests);
446
+ // The index can be negative if the request has been aborted
447
+ if (index >= 0) {
448
+ _requests.splice(index, 1);
449
+ }
450
+
451
+ if (_envelopes.length > 0) {
452
+ var envelopeAndRequest = _envelopes.shift();
453
+ var nextEnvelope = envelopeAndRequest[0];
454
+ var nextRequest = envelopeAndRequest[1];
455
+ this._debug('Transport dequeued request', nextRequest.id);
456
+ if (success) {
457
+ if (this.getConfiguration().autoBatch) {
458
+ _coalesceEnvelopes.call(this, nextEnvelope);
459
+ }
460
+ _queueSend.call(this, nextEnvelope);
461
+ this._debug('Transport completed request', request.id, nextEnvelope);
462
+ } else {
463
+ // Keep the semantic of calling response callbacks asynchronously after the request
464
+ var self = this;
465
+ this.setTimeout(function() {
466
+ self.complete(nextRequest, false, nextRequest.metaConnect);
467
+ var failure = {
468
+ reason: 'Previous request failed'
469
+ };
470
+ var xhr = nextRequest.xhr;
471
+ failure.httpCode = self.xhrStatus(xhr);
472
+ nextEnvelope.onFailure(xhr, nextEnvelope.messages, failure);
473
+ }, 0);
474
+ }
475
+ }
476
+ }
477
+
478
+ _self.complete = function(request, success, metaConnect) {
479
+ if (metaConnect) {
480
+ _metaConnectComplete.call(this, request);
481
+ } else {
482
+ _complete.call(this, request, success);
483
+ }
484
+ };
485
+
486
+ /**
487
+ * Performs the actual send depending on the transport type details.
488
+ * @param envelope the envelope to send
489
+ * @param request the request information
490
+ */
491
+ _self.transportSend = function(envelope, request) {
492
+ throw 'Abstract';
493
+ };
494
+
495
+ _self.transportSuccess = function(envelope, request, responses) {
496
+ if (!request.expired) {
497
+ this.clearTimeout(request.timeout);
498
+ this.complete(request, true, request.metaConnect);
499
+ if (responses && responses.length > 0) {
500
+ envelope.onSuccess(responses);
501
+ } else {
502
+ envelope.onFailure(request.xhr, envelope.messages, {
503
+ httpCode: 204
504
+ });
505
+ }
506
+ }
507
+ };
508
+
509
+ _self.transportFailure = function(envelope, request, failure) {
510
+ if (!request.expired) {
511
+ this.clearTimeout(request.timeout);
512
+ this.complete(request, false, request.metaConnect);
513
+ envelope.onFailure(request.xhr, envelope.messages, failure);
514
+ }
515
+ };
516
+
517
+ function _metaConnectSend(envelope) {
518
+ if (_metaConnectRequest !== null) {
519
+ throw 'Concurrent /meta/connect requests not allowed, request id=' + _metaConnectRequest.id + ' not yet completed';
520
+ }
521
+
522
+ var requestId = ++_requestIds;
523
+ this._debug('Transport', this.getType(), '/meta/connect send, request', requestId, 'envelope', envelope);
524
+ var request = {
525
+ id: requestId,
526
+ metaConnect: true,
527
+ envelope: envelope
528
+ };
529
+ _transportSend.call(this, envelope, request);
530
+ _metaConnectRequest = request;
531
+ }
532
+
533
+ _self.send = function(envelope, metaConnect) {
534
+ if (metaConnect) {
535
+ _metaConnectSend.call(this, envelope);
536
+ } else {
537
+ _queueSend.call(this, envelope);
538
+ }
539
+ };
540
+
541
+ _self.abort = function() {
542
+ _super.abort();
543
+ for (var i = 0; i < _requests.length; ++i) {
544
+ var request = _requests[i];
545
+ if (request) {
546
+ this._debug('Aborting request', request);
547
+ if (!this.abortXHR(request.xhr)) {
548
+ this.transportFailure(request.envelope, request, {reason: 'abort'});
549
+ }
550
+ }
551
+ }
552
+ var metaConnectRequest = _metaConnectRequest;
553
+ if (metaConnectRequest) {
554
+ this._debug('Aborting /meta/connect request', metaConnectRequest);
555
+ if (!this.abortXHR(metaConnectRequest.xhr)) {
556
+ this.transportFailure(metaConnectRequest.envelope, metaConnectRequest, {reason: 'abort'});
557
+ }
558
+ }
559
+ this.reset(true);
560
+ };
561
+
562
+ _self.reset = function(init) {
563
+ _super.reset(init);
564
+ _metaConnectRequest = null;
565
+ _requests = [];
566
+ _envelopes = [];
567
+ };
568
+
569
+ _self.abortXHR = function(xhr) {
570
+ if (xhr) {
571
+ try {
572
+ var state = xhr.readyState;
573
+ xhr.abort();
574
+ return state !== window.XMLHttpRequest.UNSENT;
575
+ } catch (x) {
576
+ this._debug(x);
577
+ }
578
+ }
579
+ return false;
580
+ };
581
+
582
+ _self.xhrStatus = function(xhr) {
583
+ if (xhr) {
584
+ try {
585
+ return xhr.status;
586
+ } catch (x) {
587
+ this._debug(x);
588
+ }
589
+ }
590
+ return -1;
591
+ };
592
+
593
+ return _self;
594
+ };
595
+
596
+
597
+ var LongPollingTransport = function() {
598
+ var _super = new RequestTransport();
599
+ var _self = Transport.derive(_super);
600
+ // By default, support cross domain
601
+ var _supportsCrossDomain = true;
602
+
603
+ _self.accept = function(version, crossDomain, url) {
604
+ return _supportsCrossDomain || !crossDomain;
605
+ };
606
+
607
+ _self.newXMLHttpRequest = function() {
608
+ return new window.XMLHttpRequest();
609
+ };
610
+
611
+ function _copyContext(xhr) {
612
+ try {
613
+ // Copy external context, to be used in other environments.
614
+ xhr.context = _self.context;
615
+ } catch (e) {
616
+ // May happen if XHR is wrapped by Object.seal(),
617
+ // Object.freeze(), or Object.preventExtensions().
618
+ this._debug('Could not copy transport context into XHR', e);
619
+ }
620
+ }
621
+
622
+ _self.xhrSend = function(packet) {
623
+ var xhr = _self.newXMLHttpRequest();
624
+ _copyContext(xhr);
625
+ xhr.withCredentials = true;
626
+ xhr.open('POST', packet.url, packet.sync !== true);
627
+ var headers = packet.headers;
628
+ if (headers) {
629
+ for (var headerName in headers) {
630
+ if (headers.hasOwnProperty(headerName)) {
631
+ xhr.setRequestHeader(headerName, headers[headerName]);
632
+ }
633
+ }
634
+ }
635
+ xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
636
+ xhr.onload = function() {
637
+ if (xhr.status === 200) {
638
+ packet.onSuccess(xhr.responseText);
639
+ } else {
640
+ packet.onError(xhr.statusText);
641
+ }
642
+ };
643
+ xhr.onabort = xhr.onerror = function() {
644
+ packet.onError(xhr.statusText);
645
+ };
646
+ xhr.send(packet.body);
647
+ return xhr;
648
+ };
649
+
650
+ _self.transportSend = function(envelope, request) {
651
+ this._debug('Transport', this.getType(), 'sending request', request.id, 'envelope', envelope);
652
+
653
+ var self = this;
654
+ try {
655
+ var sameStack = true;
656
+ request.xhr = this.xhrSend({
657
+ transport: this,
658
+ url: envelope.url,
659
+ sync: envelope.sync,
660
+ headers: this.getConfiguration().requestHeaders,
661
+ body: JSON.stringify(envelope.messages),
662
+ onSuccess: function(response) {
663
+ self._debug('Transport', self.getType(), 'received response', response);
664
+ var success = false;
665
+ try {
666
+ var received = self.convertToMessages(response);
667
+ if (received.length === 0) {
668
+ _supportsCrossDomain = false;
669
+ self.transportFailure(envelope, request, {
670
+ httpCode: 204
671
+ });
672
+ } else {
673
+ success = true;
674
+ self.transportSuccess(envelope, request, received);
675
+ }
676
+ } catch (x) {
677
+ self._debug(x);
678
+ if (!success) {
679
+ _supportsCrossDomain = false;
680
+ var failure = {
681
+ exception: x
682
+ };
683
+ failure.httpCode = self.xhrStatus(request.xhr);
684
+ self.transportFailure(envelope, request, failure);
685
+ }
686
+ }
687
+ },
688
+ onError: function(reason, exception) {
689
+ self._debug('Transport', self.getType(), 'received error', reason, exception);
690
+ _supportsCrossDomain = false;
691
+ var failure = {
692
+ reason: reason,
693
+ exception: exception
694
+ };
695
+ failure.httpCode = self.xhrStatus(request.xhr);
696
+ if (sameStack) {
697
+ // Keep the semantic of calling response callbacks asynchronously after the request
698
+ self.setTimeout(function() {
699
+ self.transportFailure(envelope, request, failure);
700
+ }, 0);
701
+ } else {
702
+ self.transportFailure(envelope, request, failure);
703
+ }
704
+ }
705
+ });
706
+ sameStack = false;
707
+ } catch (x) {
708
+ _supportsCrossDomain = false;
709
+ // Keep the semantic of calling response callbacks asynchronously after the request
710
+ this.setTimeout(function() {
711
+ self.transportFailure(envelope, request, {
712
+ exception: x
713
+ });
714
+ }, 0);
715
+ }
716
+ };
717
+
718
+ _self.reset = function(init) {
719
+ _super.reset(init);
720
+ _supportsCrossDomain = true;
721
+ };
722
+
723
+ return _self;
724
+ };
725
+
726
+
727
+ var CallbackPollingTransport = function() {
728
+ var _super = new RequestTransport();
729
+ var _self = Transport.derive(_super);
730
+ var jsonp = 0;
731
+
732
+ _self.accept = function(version, crossDomain, url) {
733
+ return true;
734
+ };
735
+
736
+ _self.jsonpSend = function(packet) {
737
+ var head = document.getElementsByTagName('head')[0];
738
+ var script = document.createElement('script');
739
+
740
+ var callbackName = '_cometd_jsonp_' + jsonp++;
741
+ window[callbackName] = function(responseText) {
742
+ head.removeChild(script);
743
+ delete window[callbackName];
744
+ packet.onSuccess(responseText);
745
+ };
746
+
747
+ var url = packet.url;
748
+ url += url.indexOf('?') < 0 ? '?' : '&';
749
+ url += 'jsonp=' + callbackName;
750
+ url += '&message=' + encodeURIComponent(packet.body);
751
+ script.src = url;
752
+ script.async = packet.sync !== true;
753
+ script.type = 'application/javascript';
754
+ script.onerror = function(e) {
755
+ packet.onError('jsonp ' + e.type);
756
+ };
757
+ head.appendChild(script);
758
+ };
759
+
760
+ function _failTransportFn(envelope, request, x) {
761
+ var self = this;
762
+ return function() {
763
+ self.transportFailure(envelope, request, 'error', x);
764
+ };
765
+ }
766
+
767
+ _self.transportSend = function(envelope, request) {
768
+ var self = this;
769
+
770
+ // Microsoft Internet Explorer has a 2083 URL max length
771
+ // We must ensure that we stay within that length
772
+ var start = 0;
773
+ var length = envelope.messages.length;
774
+ var lengths = [];
775
+ while (length > 0) {
776
+ // Encode the messages because all brackets, quotes, commas, colons, etc
777
+ // present in the JSON will be URL encoded, taking many more characters
778
+ var json = JSON.stringify(envelope.messages.slice(start, start + length));
779
+ var urlLength = envelope.url.length + encodeURI(json).length;
780
+
781
+ var maxLength = this.getConfiguration().maxURILength;
782
+ if (urlLength > maxLength) {
783
+ if (length === 1) {
784
+ var x = 'Bayeux message too big (' + urlLength + ' bytes, max is ' + maxLength + ') ' +
785
+ 'for transport ' + this.getType();
786
+ // Keep the semantic of calling response callbacks asynchronously after the request
787
+ this.setTimeout(_failTransportFn.call(this, envelope, request, x), 0);
788
+ return;
789
+ }
790
+
791
+ --length;
792
+ continue;
793
+ }
794
+
795
+ lengths.push(length);
796
+ start += length;
797
+ length = envelope.messages.length - start;
798
+ }
799
+
800
+ // Here we are sure that the messages can be sent within the URL limit
801
+
802
+ var envelopeToSend = envelope;
803
+ if (lengths.length > 1) {
804
+ var begin = 0;
805
+ var end = lengths[0];
806
+ this._debug('Transport', this.getType(), 'split', envelope.messages.length, 'messages into', lengths.join(' + '));
807
+ envelopeToSend = this._mixin(false, {}, envelope);
808
+ envelopeToSend.messages = envelope.messages.slice(begin, end);
809
+ envelopeToSend.onSuccess = envelope.onSuccess;
810
+ envelopeToSend.onFailure = envelope.onFailure;
811
+
812
+ for (var i = 1; i < lengths.length; ++i) {
813
+ var nextEnvelope = this._mixin(false, {}, envelope);
814
+ begin = end;
815
+ end += lengths[i];
816
+ nextEnvelope.messages = envelope.messages.slice(begin, end);
817
+ nextEnvelope.onSuccess = envelope.onSuccess;
818
+ nextEnvelope.onFailure = envelope.onFailure;
819
+ this.send(nextEnvelope, request.metaConnect);
820
+ }
821
+ }
822
+
823
+ this._debug('Transport', this.getType(), 'sending request', request.id, 'envelope', envelopeToSend);
824
+
825
+ try {
826
+ var sameStack = true;
827
+ this.jsonpSend({
828
+ transport: this,
829
+ url: envelopeToSend.url,
830
+ sync: envelopeToSend.sync,
831
+ headers: this.getConfiguration().requestHeaders,
832
+ body: JSON.stringify(envelopeToSend.messages),
833
+ onSuccess: function(responses) {
834
+ var success = false;
835
+ try {
836
+ var received = self.convertToMessages(responses);
837
+ if (received.length === 0) {
838
+ self.transportFailure(envelopeToSend, request, {
839
+ httpCode: 204
840
+ });
841
+ } else {
842
+ success = true;
843
+ self.transportSuccess(envelopeToSend, request, received);
844
+ }
845
+ } catch (x) {
846
+ self._debug(x);
847
+ if (!success) {
848
+ self.transportFailure(envelopeToSend, request, {
849
+ exception: x
850
+ });
851
+ }
852
+ }
853
+ },
854
+ onError: function(reason, exception) {
855
+ var failure = {
856
+ reason: reason,
857
+ exception: exception
858
+ };
859
+ if (sameStack) {
860
+ // Keep the semantic of calling response callbacks asynchronously after the request
861
+ self.setTimeout(function() {
862
+ self.transportFailure(envelopeToSend, request, failure);
863
+ }, 0);
864
+ } else {
865
+ self.transportFailure(envelopeToSend, request, failure);
866
+ }
867
+ }
868
+ });
869
+ sameStack = false;
870
+ } catch (xx) {
871
+ // Keep the semantic of calling response callbacks asynchronously after the request
872
+ this.setTimeout(function() {
873
+ self.transportFailure(envelopeToSend, request, {
874
+ exception: xx
875
+ });
876
+ }, 0);
877
+ }
878
+ };
879
+
880
+ return _self;
881
+ };
882
+
883
+
884
+ var WebSocketTransport = function() {
885
+ var _super = new Transport();
886
+ var _self = Transport.derive(_super);
887
+ var _cometd;
888
+ // By default WebSocket is supported
889
+ var _webSocketSupported = true;
890
+ // Whether we were able to establish a WebSocket connection
891
+ var _webSocketConnected = false;
892
+ var _stickyReconnect = true;
893
+ // The context contains the envelopes that have been sent
894
+ // and the timeouts for the messages that have been sent.
895
+ var _context = null;
896
+ var _connecting = null;
897
+ var _connected = false;
898
+ var _successCallback = null;
899
+
900
+ _self.reset = function(init) {
901
+ _super.reset(init);
902
+ _webSocketSupported = true;
903
+ if (init) {
904
+ _webSocketConnected = false;
905
+ }
906
+ _stickyReconnect = true;
907
+ _context = null;
908
+ _connecting = null;
909
+ _connected = false;
910
+ };
911
+
912
+ function _forceClose(context, event) {
913
+ if (context) {
914
+ this.webSocketClose(context, event.code, event.reason);
915
+ // Force immediate failure of pending messages to trigger reconnect.
916
+ // This is needed because the server may not reply to our close()
917
+ // and therefore the onclose function is never called.
918
+ this.onClose(context, event);
919
+ }
920
+ }
921
+
922
+ function _sameContext(context) {
923
+ return context === _connecting || context === _context;
924
+ }
925
+
926
+ function _storeEnvelope(context, envelope, metaConnect) {
927
+ var messageIds = [];
928
+ for (var i = 0; i < envelope.messages.length; ++i) {
929
+ var message = envelope.messages[i];
930
+ if (message.id) {
931
+ messageIds.push(message.id);
932
+ }
933
+ }
934
+ context.envelopes[messageIds.join(',')] = [envelope, metaConnect];
935
+ this._debug('Transport', this.getType(), 'stored envelope, envelopes', context.envelopes);
936
+ }
937
+
938
+ function _websocketConnect(context) {
939
+ // We may have multiple attempts to open a WebSocket
940
+ // connection, for example a /meta/connect request that
941
+ // may take time, along with a user-triggered publish.
942
+ // Early return if we are already connecting.
943
+ if (_connecting) {
944
+ return;
945
+ }
946
+
947
+ // Mangle the URL, changing the scheme from 'http' to 'ws'.
948
+ var url = _cometd.getURL().replace(/^http/, 'ws');
949
+ this._debug('Transport', this.getType(), 'connecting to URL', url);
950
+
951
+ try {
952
+ var protocol = _cometd.getConfiguration().protocol;
953
+ context.webSocket = protocol ? new window.WebSocket(url, protocol) : new window.WebSocket(url);
954
+ _connecting = context;
955
+ } catch (x) {
956
+ _webSocketSupported = false;
957
+ this._debug('Exception while creating WebSocket object', x);
958
+ throw x;
959
+ }
960
+
961
+ // By default use sticky reconnects.
962
+ _stickyReconnect = _cometd.getConfiguration().stickyReconnect !== false;
963
+
964
+ var self = this;
965
+ var connectTimeout = _cometd.getConfiguration().connectTimeout;
966
+ if (connectTimeout > 0) {
967
+ context.connectTimer = this.setTimeout(function() {
968
+ _cometd._debug('Transport', self.getType(), 'timed out while connecting to URL', url, ':', connectTimeout, 'ms');
969
+ // The connection was not opened, close anyway.
970
+ _forceClose.call(self, context, {code: 1000, reason: 'Connect Timeout'});
971
+ }, connectTimeout);
972
+ }
973
+
974
+ var onopen = function() {
975
+ _cometd._debug('WebSocket onopen', context);
976
+ if (context.connectTimer) {
977
+ self.clearTimeout(context.connectTimer);
978
+ }
979
+
980
+ if (_sameContext(context)) {
981
+ _connecting = null;
982
+ _context = context;
983
+ _webSocketConnected = true;
984
+ self.onOpen(context);
985
+ } else {
986
+ // We have a valid connection already, close this one.
987
+ _cometd._warn('Closing extra WebSocket connection', this, 'active connection', _context);
988
+ _forceClose.call(self, context, {code: 1000, reason: 'Extra Connection'});
989
+ }
990
+ };
991
+
992
+ // This callback is invoked when the server sends the close frame.
993
+ // The close frame for a connection may arrive *after* another
994
+ // connection has been opened, so we must make sure that actions
995
+ // are performed only if it's the same connection.
996
+ var onclose = function(event) {
997
+ event = event || {code: 1000};
998
+ _cometd._debug('WebSocket onclose', context, event, 'connecting', _connecting, 'current', _context);
999
+
1000
+ if (context.connectTimer) {
1001
+ self.clearTimeout(context.connectTimer);
1002
+ }
1003
+
1004
+ self.onClose(context, event);
1005
+ };
1006
+
1007
+ var onmessage = function(wsMessage) {
1008
+ _cometd._debug('WebSocket onmessage', wsMessage, context);
1009
+ self.onMessage(context, wsMessage);
1010
+ };
1011
+
1012
+ context.webSocket.onopen = onopen;
1013
+ context.webSocket.onclose = onclose;
1014
+ context.webSocket.onerror = function() {
1015
+ // Clients should call onclose(), but if they do not we do it here for safety.
1016
+ onclose({code: 1000, reason: 'Error'});
1017
+ };
1018
+ context.webSocket.onmessage = onmessage;
1019
+
1020
+ this._debug('Transport', this.getType(), 'configured callbacks on', context);
1021
+ }
1022
+
1023
+ function _webSocketSend(context, envelope, metaConnect) {
1024
+ var json = JSON.stringify(envelope.messages);
1025
+ context.webSocket.send(json);
1026
+ this._debug('Transport', this.getType(), 'sent', envelope, '/meta/connect =', metaConnect);
1027
+
1028
+ // Manage the timeout waiting for the response.
1029
+ var maxDelay = this.getConfiguration().maxNetworkDelay;
1030
+ var delay = maxDelay;
1031
+ if (metaConnect) {
1032
+ delay += this.getAdvice().timeout;
1033
+ _connected = true;
1034
+ }
1035
+
1036
+ var self = this;
1037
+ var messageIds = [];
1038
+ for (var i = 0; i < envelope.messages.length; ++i) {
1039
+ (function() {
1040
+ var message = envelope.messages[i];
1041
+ if (message.id) {
1042
+ messageIds.push(message.id);
1043
+ context.timeouts[message.id] = self.setTimeout(function() {
1044
+ _cometd._debug('Transport', self.getType(), 'timing out message', message.id, 'after', delay, 'on', context);
1045
+ _forceClose.call(self, context, {code: 1000, reason: 'Message Timeout'});
1046
+ }, delay);
1047
+ }
1048
+ })();
1049
+ }
1050
+
1051
+ this._debug('Transport', this.getType(), 'waiting at most', delay, 'ms for messages', messageIds, 'maxNetworkDelay', maxDelay, ', timeouts:', context.timeouts);
1052
+ }
1053
+
1054
+ _self._notifySuccess = function(fn, messages) {
1055
+ fn.call(this, messages);
1056
+ };
1057
+
1058
+ _self._notifyFailure = function(fn, context, messages, failure) {
1059
+ fn.call(this, context, messages, failure);
1060
+ };
1061
+
1062
+ function _send(context, envelope, metaConnect) {
1063
+ try {
1064
+ if (context === null) {
1065
+ context = _connecting || {
1066
+ envelopes: {},
1067
+ timeouts: {}
1068
+ };
1069
+ _storeEnvelope.call(this, context, envelope, metaConnect);
1070
+ _websocketConnect.call(this, context);
1071
+ } else {
1072
+ _storeEnvelope.call(this, context, envelope, metaConnect);
1073
+ _webSocketSend.call(this, context, envelope, metaConnect);
1074
+ }
1075
+ } catch (x) {
1076
+ // Keep the semantic of calling response callbacks asynchronously after the request.
1077
+ var self = this;
1078
+ this.setTimeout(function() {
1079
+ _forceClose.call(self, context, {
1080
+ code: 1000,
1081
+ reason: 'Exception',
1082
+ exception: x
1083
+ });
1084
+ }, 0);
1085
+ }
1086
+ }
1087
+
1088
+ _self.onOpen = function(context) {
1089
+ var envelopes = context.envelopes;
1090
+ this._debug('Transport', this.getType(), 'opened', context, 'pending messages', envelopes);
1091
+ for (var key in envelopes) {
1092
+ if (envelopes.hasOwnProperty(key)) {
1093
+ var element = envelopes[key];
1094
+ var envelope = element[0];
1095
+ var metaConnect = element[1];
1096
+ // Store the success callback, which is independent from the envelope,
1097
+ // so that it can be used to notify arrival of messages.
1098
+ _successCallback = envelope.onSuccess;
1099
+ _webSocketSend.call(this, context, envelope, metaConnect);
1100
+ }
1101
+ }
1102
+ };
1103
+
1104
+ _self.onMessage = function(context, wsMessage) {
1105
+ this._debug('Transport', this.getType(), 'received websocket message', wsMessage, context);
1106
+
1107
+ var close = false;
1108
+ var messages = this.convertToMessages(wsMessage.data);
1109
+ var messageIds = [];
1110
+ for (var i = 0; i < messages.length; ++i) {
1111
+ var message = messages[i];
1112
+
1113
+ // Detect if the message is a response to a request we made.
1114
+ // If it's a meta message, for sure it's a response; otherwise it's
1115
+ // a publish message and publish responses don't have the data field.
1116
+ if (/^\/meta\//.test(message.channel) || message.data === undefined) {
1117
+ if (message.id) {
1118
+ messageIds.push(message.id);
1119
+
1120
+ var timeout = context.timeouts[message.id];
1121
+ if (timeout) {
1122
+ this.clearTimeout(timeout);
1123
+ delete context.timeouts[message.id];
1124
+ this._debug('Transport', this.getType(), 'removed timeout for message', message.id, ', timeouts', context.timeouts);
1125
+ }
1126
+ }
1127
+ }
1128
+
1129
+ if ('/meta/connect' === message.channel) {
1130
+ _connected = false;
1131
+ }
1132
+ if ('/meta/disconnect' === message.channel && !_connected) {
1133
+ close = true;
1134
+ }
1135
+ }
1136
+
1137
+ // Remove the envelope corresponding to the messages.
1138
+ var removed = false;
1139
+ var envelopes = context.envelopes;
1140
+ for (var j = 0; j < messageIds.length; ++j) {
1141
+ var id = messageIds[j];
1142
+ for (var key in envelopes) {
1143
+ if (envelopes.hasOwnProperty(key)) {
1144
+ var ids = key.split(',');
1145
+ var index = Utils.inArray(id, ids);
1146
+ if (index >= 0) {
1147
+ removed = true;
1148
+ ids.splice(index, 1);
1149
+ var envelope = envelopes[key][0];
1150
+ var metaConnect = envelopes[key][1];
1151
+ delete envelopes[key];
1152
+ if (ids.length > 0) {
1153
+ envelopes[ids.join(',')] = [envelope, metaConnect];
1154
+ }
1155
+ break;
1156
+ }
1157
+ }
1158
+ }
1159
+ }
1160
+ if (removed) {
1161
+ this._debug('Transport', this.getType(), 'removed envelope, envelopes', envelopes);
1162
+ }
1163
+
1164
+ this._notifySuccess(_successCallback, messages);
1165
+
1166
+ if (close) {
1167
+ this.webSocketClose(context, 1000, 'Disconnect');
1168
+ }
1169
+ };
1170
+
1171
+ _self.onClose = function(context, event) {
1172
+ this._debug('Transport', this.getType(), 'closed', context, event);
1173
+
1174
+ if (_sameContext(context)) {
1175
+ // Remember if we were able to connect.
1176
+ // This close event could be due to server shutdown,
1177
+ // and if it restarts we want to try websocket again.
1178
+ _webSocketSupported = _stickyReconnect && _webSocketConnected;
1179
+ _connecting = null;
1180
+ _context = null;
1181
+ }
1182
+
1183
+ var timeouts = context.timeouts;
1184
+ context.timeouts = {};
1185
+ for (var id in timeouts) {
1186
+ if (timeouts.hasOwnProperty(id)) {
1187
+ this.clearTimeout(timeouts[id]);
1188
+ }
1189
+ }
1190
+
1191
+ var envelopes = context.envelopes;
1192
+ context.envelopes = {};
1193
+ for (var key in envelopes) {
1194
+ if (envelopes.hasOwnProperty(key)) {
1195
+ var envelope = envelopes[key][0];
1196
+ var metaConnect = envelopes[key][1];
1197
+ if (metaConnect) {
1198
+ _connected = false;
1199
+ }
1200
+ var failure = {
1201
+ websocketCode: event.code,
1202
+ reason: event.reason
1203
+ };
1204
+ if (event.exception) {
1205
+ failure.exception = event.exception;
1206
+ }
1207
+ this._notifyFailure(envelope.onFailure, context, envelope.messages, failure);
1208
+ }
1209
+ }
1210
+ };
1211
+
1212
+ _self.registered = function(type, cometd) {
1213
+ _super.registered(type, cometd);
1214
+ _cometd = cometd;
1215
+ };
1216
+
1217
+ _self.accept = function(version, crossDomain, url) {
1218
+ this._debug('Transport', this.getType(), 'accept, supported:', _webSocketSupported);
1219
+ // Using !! to return a boolean (and not the WebSocket object).
1220
+ return _webSocketSupported && !!window.WebSocket && _cometd.websocketEnabled !== false;
1221
+ };
1222
+
1223
+ _self.send = function(envelope, metaConnect) {
1224
+ this._debug('Transport', this.getType(), 'sending', envelope, '/meta/connect =', metaConnect);
1225
+ _send.call(this, _context, envelope, metaConnect);
1226
+ };
1227
+
1228
+ _self.webSocketClose = function(context, code, reason) {
1229
+ try {
1230
+ if (context.webSocket) {
1231
+ context.webSocket.close(code, reason);
1232
+ }
1233
+ } catch (x) {
1234
+ this._debug(x);
1235
+ }
1236
+ };
1237
+
1238
+ _self.abort = function() {
1239
+ _super.abort();
1240
+ _forceClose.call(this, _context, {code: 1000, reason: 'Abort'});
1241
+ this.reset(true);
1242
+ };
1243
+
1244
+ return _self;
1245
+ };
1246
+
1247
+
1248
+ /**
1249
+ * The constructor for a CometD object, identified by an optional name.
1250
+ * The default name is the string 'default'.
1251
+ * @param name the optional name of this cometd object
1252
+ */
1253
+ var CometD = function(name) {
1254
+ var _scheduler = new Scheduler();
1255
+ var _cometd = this;
1256
+ var _name = name || 'default';
1257
+ var _crossDomain = false;
1258
+ var _transports = new TransportRegistry();
1259
+ var _transport;
1260
+ var _status = 'disconnected';
1261
+ var _messageId = 0;
1262
+ var _clientId = null;
1263
+ var _batch = 0;
1264
+ var _messageQueue = [];
1265
+ var _internalBatch = false;
1266
+ var _listenerId = 0;
1267
+ var _listeners = {};
1268
+ var _backoff = 0;
1269
+ var _scheduledSend = null;
1270
+ var _extensions = [];
1271
+ var _advice = {};
1272
+ var _handshakeProps;
1273
+ var _handshakeCallback;
1274
+ var _callbacks = {};
1275
+ var _remoteCalls = {};
1276
+ var _reestablish = false;
1277
+ var _connected = false;
1278
+ var _unconnectTime = 0;
1279
+ var _handshakeMessages = 0;
1280
+ var _metaConnect = null;
1281
+ var _config = {
1282
+ useWorkerScheduler: true,
1283
+ protocol: null,
1284
+ stickyReconnect: true,
1285
+ connectTimeout: 0,
1286
+ maxConnections: 2,
1287
+ backoffIncrement: 1000,
1288
+ maxBackoff: 60000,
1289
+ logLevel: 'info',
1290
+ maxNetworkDelay: 10000,
1291
+ requestHeaders: {},
1292
+ appendMessageTypeToURL: true,
1293
+ autoBatch: false,
1294
+ urls: {},
1295
+ maxURILength: 2000,
1296
+ advice: {
1297
+ timeout: 60000,
1298
+ interval: 0,
1299
+ reconnect: undefined,
1300
+ maxInterval: 0
1301
+ }
1302
+ };
1303
+
1304
+ function _fieldValue(object, name) {
1305
+ try {
1306
+ return object[name];
1307
+ } catch (x) {
1308
+ return undefined;
1309
+ }
1310
+ }
1311
+
1312
+ /**
1313
+ * Mixes in the given objects into the target object by copying the properties.
1314
+ * @param deep if the copy must be deep
1315
+ * @param target the target object
1316
+ * @param objects the objects whose properties are copied into the target
1317
+ */
1318
+ this._mixin = function(deep, target, objects) {
1319
+ var result = target || {};
1320
+
1321
+ // Skip first 2 parameters (deep and target), and loop over the others
1322
+ for (var i = 2; i < arguments.length; ++i) {
1323
+ var object = arguments[i];
1324
+
1325
+ if (object === undefined || object === null) {
1326
+ continue;
1327
+ }
1328
+
1329
+ for (var propName in object) {
1330
+ if (object.hasOwnProperty(propName)) {
1331
+ var prop = _fieldValue(object, propName);
1332
+ var targ = _fieldValue(result, propName);
1333
+
1334
+ // Avoid infinite loops
1335
+ if (prop === target) {
1336
+ continue;
1337
+ }
1338
+ // Do not mixin undefined values
1339
+ if (prop === undefined) {
1340
+ continue;
1341
+ }
1342
+
1343
+ if (deep && typeof prop === 'object' && prop !== null) {
1344
+ if (prop instanceof Array) {
1345
+ result[propName] = this._mixin(deep, targ instanceof Array ? targ : [], prop);
1346
+ } else {
1347
+ var source = typeof targ === 'object' && !(targ instanceof Array) ? targ : {};
1348
+ result[propName] = this._mixin(deep, source, prop);
1349
+ }
1350
+ } else {
1351
+ result[propName] = prop;
1352
+ }
1353
+ }
1354
+ }
1355
+ }
1356
+
1357
+ return result;
1358
+ };
1359
+
1360
+ function _isString(value) {
1361
+ return Utils.isString(value);
1362
+ }
1363
+
1364
+ function _isFunction(value) {
1365
+ if (value === undefined || value === null) {
1366
+ return false;
1367
+ }
1368
+ return typeof value === 'function';
1369
+ }
1370
+
1371
+ function _zeroPad(value, length) {
1372
+ var result = '';
1373
+ while (--length > 0) {
1374
+ if (value >= Math.pow(10, length)) {
1375
+ break;
1376
+ }
1377
+ result += '0';
1378
+ }
1379
+ result += value;
1380
+ return result;
1381
+ }
1382
+
1383
+ function _log(level, args) {
1384
+ if (window.console) {
1385
+ var logger = window.console[level];
1386
+ if (_isFunction(logger)) {
1387
+ var now = new Date();
1388
+ [].splice.call(args, 0, 0, _zeroPad(now.getHours(), 2) + ':' + _zeroPad(now.getMinutes(), 2) + ':' +
1389
+ _zeroPad(now.getSeconds(), 2) + '.' + _zeroPad(now.getMilliseconds(), 3));
1390
+ logger.apply(window.console, args);
1391
+ }
1392
+ }
1393
+ }
1394
+
1395
+ this._warn = function() {
1396
+ _log('warn', arguments);
1397
+ };
1398
+
1399
+ this._info = function() {
1400
+ if (_config.logLevel !== 'warn') {
1401
+ _log('info', arguments);
1402
+ }
1403
+ };
1404
+
1405
+ this._debug = function() {
1406
+ if (_config.logLevel === 'debug') {
1407
+ _log('debug', arguments);
1408
+ }
1409
+ };
1410
+
1411
+ function _splitURL(url) {
1412
+ // [1] = protocol://,
1413
+ // [2] = host:port,
1414
+ // [3] = host,
1415
+ // [4] = IPv6_host,
1416
+ // [5] = IPv4_host,
1417
+ // [6] = :port,
1418
+ // [7] = port,
1419
+ // [8] = uri,
1420
+ // [9] = rest (query / fragment)
1421
+ return new RegExp('(^https?://)?(((\\[[^\\]]+])|([^:/?#]+))(:(\\d+))?)?([^?#]*)(.*)?').exec(url);
1422
+ }
1423
+
1424
+ /**
1425
+ * Returns whether the given hostAndPort is cross domain.
1426
+ * The default implementation checks against window.location.host
1427
+ * but this function can be overridden to make it work in non-browser
1428
+ * environments.
1429
+ *
1430
+ * @param hostAndPort the host and port in format host:port
1431
+ * @return whether the given hostAndPort is cross domain
1432
+ */
1433
+ this._isCrossDomain = function(hostAndPort) {
1434
+ if (window.location && window.location.host) {
1435
+ if (hostAndPort) {
1436
+ return hostAndPort !== window.location.host;
1437
+ }
1438
+ }
1439
+ return false;
1440
+ };
1441
+
1442
+ function _configure(configuration) {
1443
+ _cometd._debug('Configuring cometd object with', configuration);
1444
+ // Support old style param, where only the Bayeux server URL was passed.
1445
+ if (_isString(configuration)) {
1446
+ configuration = {
1447
+ url: configuration
1448
+ };
1449
+ }
1450
+ if (!configuration) {
1451
+ configuration = {};
1452
+ }
1453
+
1454
+ _config = _cometd._mixin(false, _config, configuration);
1455
+
1456
+ var url = _cometd.getURL();
1457
+ if (!url) {
1458
+ throw 'Missing required configuration parameter \'url\' specifying the Bayeux server URL';
1459
+ }
1460
+
1461
+ // Check if we're cross domain.
1462
+ var urlParts = _splitURL(url);
1463
+ var hostAndPort = urlParts[2];
1464
+ var uri = urlParts[8];
1465
+ var afterURI = urlParts[9];
1466
+ _crossDomain = _cometd._isCrossDomain(hostAndPort);
1467
+
1468
+ // Check if appending extra path is supported.
1469
+ if (_config.appendMessageTypeToURL) {
1470
+ if (afterURI !== undefined && afterURI.length > 0) {
1471
+ _cometd._info('Appending message type to URI ' + uri + afterURI + ' is not supported, disabling \'appendMessageTypeToURL\' configuration');
1472
+ _config.appendMessageTypeToURL = false;
1473
+ } else {
1474
+ var uriSegments = uri.split('/');
1475
+ var lastSegmentIndex = uriSegments.length - 1;
1476
+ if (uri.match(/\/$/)) {
1477
+ lastSegmentIndex -= 1;
1478
+ }
1479
+ if (uriSegments[lastSegmentIndex].indexOf('.') >= 0) {
1480
+ // Very likely the CometD servlet's URL pattern is mapped to an extension, such as *.cometd
1481
+ // It will be difficult to add the extra path in this case
1482
+ _cometd._info('Appending message type to URI ' + uri + ' is not supported, disabling \'appendMessageTypeToURL\' configuration');
1483
+ _config.appendMessageTypeToURL = false;
1484
+ }
1485
+ }
1486
+ }
1487
+
1488
+ if (window.Worker && window.Blob && window.URL && _config.useWorkerScheduler) {
1489
+ var code = WorkerScheduler.toString();
1490
+ // Remove the function declaration, the opening brace and the closing brace.
1491
+ code = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}'));
1492
+ var blob = new window.Blob([code], {
1493
+ type: 'application/json'
1494
+ });
1495
+ var blobURL = window.URL.createObjectURL(blob);
1496
+ var worker = new window.Worker(blobURL);
1497
+ _scheduler.setTimeout = function(funktion, delay) {
1498
+ var id = _scheduler.register(funktion);
1499
+ worker.postMessage({
1500
+ id: id,
1501
+ type: 'setTimeout',
1502
+ delay: delay
1503
+ });
1504
+ return id;
1505
+ };
1506
+ _scheduler.clearTimeout = function(id) {
1507
+ _scheduler.unregister(id);
1508
+ worker.postMessage({
1509
+ id: id,
1510
+ type: 'clearTimeout'
1511
+ });
1512
+ };
1513
+ worker.onmessage = function(e) {
1514
+ var id = e.data.id;
1515
+ var funktion = _scheduler.unregister(id);
1516
+ if (funktion) {
1517
+ funktion();
1518
+ }
1519
+ };
1520
+ }
1521
+ }
1522
+
1523
+ function _removeListener(subscription) {
1524
+ if (subscription) {
1525
+ var subscriptions = _listeners[subscription.channel];
1526
+ if (subscriptions && subscriptions[subscription.id]) {
1527
+ delete subscriptions[subscription.id];
1528
+ _cometd._debug('Removed', subscription.listener ? 'listener' : 'subscription', subscription);
1529
+ }
1530
+ }
1531
+ }
1532
+
1533
+ function _removeSubscription(subscription) {
1534
+ if (subscription && !subscription.listener) {
1535
+ _removeListener(subscription);
1536
+ }
1537
+ }
1538
+
1539
+ function _clearSubscriptions() {
1540
+ for (var channel in _listeners) {
1541
+ if (_listeners.hasOwnProperty(channel)) {
1542
+ var subscriptions = _listeners[channel];
1543
+ if (subscriptions) {
1544
+ for (var id in subscriptions) {
1545
+ if (subscriptions.hasOwnProperty(id)) {
1546
+ _removeSubscription(subscriptions[id]);
1547
+ }
1548
+ }
1549
+ }
1550
+ }
1551
+ }
1552
+ }
1553
+
1554
+ function _setStatus(newStatus) {
1555
+ if (_status !== newStatus) {
1556
+ _cometd._debug('Status', _status, '->', newStatus);
1557
+ _status = newStatus;
1558
+ }
1559
+ }
1560
+
1561
+ function _isDisconnected() {
1562
+ return _status === 'disconnecting' || _status === 'disconnected';
1563
+ }
1564
+
1565
+ function _nextMessageId() {
1566
+ var result = ++_messageId;
1567
+ return '' + result;
1568
+ }
1569
+
1570
+ function _applyExtension(scope, callback, name, message, outgoing) {
1571
+ try {
1572
+ return callback.call(scope, message);
1573
+ } catch (x) {
1574
+ var handler = _cometd.onExtensionException;
1575
+ if (_isFunction(handler)) {
1576
+ _cometd._debug('Invoking extension exception handler', name, x);
1577
+ try {
1578
+ handler.call(_cometd, x, name, outgoing, message);
1579
+ } catch (xx) {
1580
+ _cometd._info('Exception during execution of extension exception handler', name, xx);
1581
+ }
1582
+ } else {
1583
+ _cometd._info('Exception during execution of extension', name, x);
1584
+ }
1585
+ return message;
1586
+ }
1587
+ }
1588
+
1589
+ function _applyIncomingExtensions(message) {
1590
+ for (var i = 0; i < _extensions.length; ++i) {
1591
+ if (message === undefined || message === null) {
1592
+ break;
1593
+ }
1594
+
1595
+ var extension = _extensions[i];
1596
+ var callback = extension.extension.incoming;
1597
+ if (_isFunction(callback)) {
1598
+ var result = _applyExtension(extension.extension, callback, extension.name, message, false);
1599
+ message = result === undefined ? message : result;
1600
+ }
1601
+ }
1602
+ return message;
1603
+ }
1604
+
1605
+ function _applyOutgoingExtensions(message) {
1606
+ for (var i = _extensions.length - 1; i >= 0; --i) {
1607
+ if (message === undefined || message === null) {
1608
+ break;
1609
+ }
1610
+
1611
+ var extension = _extensions[i];
1612
+ var callback = extension.extension.outgoing;
1613
+ if (_isFunction(callback)) {
1614
+ var result = _applyExtension(extension.extension, callback, extension.name, message, true);
1615
+ message = result === undefined ? message : result;
1616
+ }
1617
+ }
1618
+ return message;
1619
+ }
1620
+
1621
+ function _notify(channel, message) {
1622
+ var subscriptions = _listeners[channel];
1623
+ if (subscriptions) {
1624
+ for (var id in subscriptions) {
1625
+ if (subscriptions.hasOwnProperty(id)) {
1626
+ var subscription = subscriptions[id];
1627
+ // Subscriptions may come and go, so the array may have 'holes'
1628
+ if (subscription) {
1629
+ try {
1630
+ subscription.callback.call(subscription.scope, message);
1631
+ } catch (x) {
1632
+ var handler = _cometd.onListenerException;
1633
+ if (_isFunction(handler)) {
1634
+ _cometd._debug('Invoking listener exception handler', subscription, x);
1635
+ try {
1636
+ handler.call(_cometd, x, subscription, subscription.listener, message);
1637
+ } catch (xx) {
1638
+ _cometd._info('Exception during execution of listener exception handler', subscription, xx);
1639
+ }
1640
+ } else {
1641
+ _cometd._info('Exception during execution of listener', subscription, message, x);
1642
+ }
1643
+ }
1644
+ }
1645
+ }
1646
+ }
1647
+ }
1648
+ }
1649
+
1650
+ function _notifyListeners(channel, message) {
1651
+ // Notify direct listeners
1652
+ _notify(channel, message);
1653
+
1654
+ // Notify the globbing listeners
1655
+ var channelParts = channel.split('/');
1656
+ var last = channelParts.length - 1;
1657
+ for (var i = last; i > 0; --i) {
1658
+ var channelPart = channelParts.slice(0, i).join('/') + '/*';
1659
+ // We don't want to notify /foo/* if the channel is /foo/bar/baz,
1660
+ // so we stop at the first non recursive globbing
1661
+ if (i === last) {
1662
+ _notify(channelPart, message);
1663
+ }
1664
+ // Add the recursive globber and notify
1665
+ channelPart += '*';
1666
+ _notify(channelPart, message);
1667
+ }
1668
+ }
1669
+
1670
+ function _cancelDelayedSend() {
1671
+ if (_scheduledSend !== null) {
1672
+ _cometd.clearTimeout(_scheduledSend);
1673
+ }
1674
+ _scheduledSend = null;
1675
+ }
1676
+
1677
+ function _delayedSend(operation, delay) {
1678
+ _cancelDelayedSend();
1679
+ var time = _advice.interval + delay;
1680
+ _cometd._debug('Function scheduled in', time, 'ms, interval =', _advice.interval, 'backoff =', _backoff, operation);
1681
+ _scheduledSend = _cometd.setTimeout(operation, time);
1682
+ }
1683
+
1684
+ // Needed to break cyclic dependencies between function definitions
1685
+ var _handleMessages;
1686
+ var _handleFailure;
1687
+
1688
+ /**
1689
+ * Delivers the messages to the CometD server
1690
+ * @param messages the array of messages to send
1691
+ * @param metaConnect true if this send is on /meta/connect
1692
+ * @param extraPath an extra path to append to the Bayeux server URL
1693
+ */
1694
+ function _send(messages, metaConnect, extraPath) {
1695
+ // We must be sure that the messages have a clientId.
1696
+ // This is not guaranteed since the handshake may take time to return
1697
+ // (and hence the clientId is not known yet) and the application
1698
+ // may create other messages.
1699
+ for (var i = 0; i < messages.length; ++i) {
1700
+ var message = messages[i];
1701
+ var messageId = message.id;
1702
+
1703
+ if (_clientId) {
1704
+ message.clientId = _clientId;
1705
+ }
1706
+
1707
+ message = _applyOutgoingExtensions(message);
1708
+ if (message !== undefined && message !== null) {
1709
+ // Extensions may have modified the message id, but we need to own it.
1710
+ message.id = messageId;
1711
+ messages[i] = message;
1712
+ } else {
1713
+ delete _callbacks[messageId];
1714
+ messages.splice(i--, 1);
1715
+ }
1716
+ }
1717
+
1718
+ if (messages.length === 0) {
1719
+ return;
1720
+ }
1721
+
1722
+ if (metaConnect) {
1723
+ _metaConnect = messages[0];
1724
+ }
1725
+
1726
+ var url = _cometd.getURL();
1727
+ if (_config.appendMessageTypeToURL) {
1728
+ // If url does not end with '/', then append it
1729
+ if (!url.match(/\/$/)) {
1730
+ url = url + '/';
1731
+ }
1732
+ if (extraPath) {
1733
+ url = url + extraPath;
1734
+ }
1735
+ }
1736
+
1737
+ var envelope = {
1738
+ url: url,
1739
+ sync: false,
1740
+ messages: messages,
1741
+ onSuccess: function(rcvdMessages) {
1742
+ try {
1743
+ _handleMessages.call(_cometd, rcvdMessages);
1744
+ } catch (x) {
1745
+ _cometd._info('Exception during handling of messages', x);
1746
+ }
1747
+ },
1748
+ onFailure: function(conduit, messages, failure) {
1749
+ try {
1750
+ var transport = _cometd.getTransport();
1751
+ failure.connectionType = transport ? transport.getType() : "unknown";
1752
+ _handleFailure.call(_cometd, conduit, messages, failure);
1753
+ } catch (x) {
1754
+ _cometd._info('Exception during handling of failure', x);
1755
+ }
1756
+ }
1757
+ };
1758
+ _cometd._debug('Send', envelope);
1759
+ _transport.send(envelope, metaConnect);
1760
+ }
1761
+
1762
+ function _queueSend(message) {
1763
+ if (_batch > 0 || _internalBatch === true) {
1764
+ _messageQueue.push(message);
1765
+ } else {
1766
+ _send([message], false);
1767
+ }
1768
+ }
1769
+
1770
+ /**
1771
+ * Sends a complete bayeux message.
1772
+ * This method is exposed as a public so that extensions may use it
1773
+ * to send bayeux message directly, for example in case of re-sending
1774
+ * messages that have already been sent but that for some reason must
1775
+ * be resent.
1776
+ */
1777
+ this.send = _queueSend;
1778
+
1779
+ function _resetBackoff() {
1780
+ _backoff = 0;
1781
+ }
1782
+
1783
+ function _increaseBackoff() {
1784
+ if (_backoff < _config.maxBackoff) {
1785
+ _backoff += _config.backoffIncrement;
1786
+ }
1787
+ return _backoff;
1788
+ }
1789
+
1790
+ /**
1791
+ * Starts a the batch of messages to be sent in a single request.
1792
+ * @see #_endBatch(sendMessages)
1793
+ */
1794
+ function _startBatch() {
1795
+ ++_batch;
1796
+ _cometd._debug('Starting batch, depth', _batch);
1797
+ }
1798
+
1799
+ function _flushBatch() {
1800
+ var messages = _messageQueue;
1801
+ _messageQueue = [];
1802
+ if (messages.length > 0) {
1803
+ _send(messages, false);
1804
+ }
1805
+ }
1806
+
1807
+ /**
1808
+ * Ends the batch of messages to be sent in a single request,
1809
+ * optionally sending messages present in the message queue depending
1810
+ * on the given argument.
1811
+ * @see #_startBatch()
1812
+ */
1813
+ function _endBatch() {
1814
+ --_batch;
1815
+ _cometd._debug('Ending batch, depth', _batch);
1816
+ if (_batch < 0) {
1817
+ throw 'Calls to startBatch() and endBatch() are not paired';
1818
+ }
1819
+
1820
+ if (_batch === 0 && !_isDisconnected() && !_internalBatch) {
1821
+ _flushBatch();
1822
+ }
1823
+ }
1824
+
1825
+ /**
1826
+ * Sends the connect message
1827
+ */
1828
+ function _connect() {
1829
+ if (!_isDisconnected()) {
1830
+ var bayeuxMessage = {
1831
+ id: _nextMessageId(),
1832
+ channel: '/meta/connect',
1833
+ connectionType: _transport.getType()
1834
+ };
1835
+
1836
+ // In case of reload or temporary loss of connection
1837
+ // we want the next successful connect to return immediately
1838
+ // instead of being held by the server, so that connect listeners
1839
+ // can be notified that the connection has been re-established
1840
+ if (!_connected) {
1841
+ bayeuxMessage.advice = {
1842
+ timeout: 0
1843
+ };
1844
+ }
1845
+
1846
+ _setStatus('connecting');
1847
+ _cometd._debug('Connect sent', bayeuxMessage);
1848
+ _send([bayeuxMessage], true, 'connect');
1849
+ _setStatus('connected');
1850
+ }
1851
+ }
1852
+
1853
+ function _delayedConnect(delay) {
1854
+ _setStatus('connecting');
1855
+ _delayedSend(function() {
1856
+ _connect();
1857
+ }, delay);
1858
+ }
1859
+
1860
+ function _updateAdvice(newAdvice) {
1861
+ if (newAdvice) {
1862
+ _advice = _cometd._mixin(false, {}, _config.advice, newAdvice);
1863
+ _cometd._debug('New advice', _advice);
1864
+ }
1865
+ }
1866
+
1867
+ function _disconnect(abort) {
1868
+ _cancelDelayedSend();
1869
+ if (abort && _transport) {
1870
+ _transport.abort();
1871
+ }
1872
+ _crossDomain = false;
1873
+ _transport = null;
1874
+ _setStatus('disconnected');
1875
+ _clientId = null;
1876
+ _batch = 0;
1877
+ _resetBackoff();
1878
+ _reestablish = false;
1879
+ _connected = false;
1880
+ _unconnectTime = 0;
1881
+ _metaConnect = null;
1882
+
1883
+ // Fail any existing queued message
1884
+ if (_messageQueue.length > 0) {
1885
+ var messages = _messageQueue;
1886
+ _messageQueue = [];
1887
+ _handleFailure.call(_cometd, undefined, messages, {
1888
+ reason: 'Disconnected'
1889
+ });
1890
+ }
1891
+ }
1892
+
1893
+ function _notifyTransportException(oldTransport, newTransport, failure) {
1894
+ var handler = _cometd.onTransportException;
1895
+ if (_isFunction(handler)) {
1896
+ _cometd._debug('Invoking transport exception handler', oldTransport, newTransport, failure);
1897
+ try {
1898
+ handler.call(_cometd, failure, oldTransport, newTransport);
1899
+ } catch (x) {
1900
+ _cometd._info('Exception during execution of transport exception handler', x);
1901
+ }
1902
+ }
1903
+ }
1904
+
1905
+ /**
1906
+ * Sends the initial handshake message
1907
+ */
1908
+ function _handshake(handshakeProps, handshakeCallback) {
1909
+ if (_isFunction(handshakeProps)) {
1910
+ handshakeCallback = handshakeProps;
1911
+ handshakeProps = undefined;
1912
+ }
1913
+
1914
+ _clientId = null;
1915
+
1916
+ _clearSubscriptions();
1917
+
1918
+ // Reset the transports if we're not retrying the handshake
1919
+ if (_isDisconnected()) {
1920
+ _transports.reset(true);
1921
+ }
1922
+
1923
+ // Reset the advice.
1924
+ _updateAdvice({});
1925
+
1926
+ _batch = 0;
1927
+
1928
+ // Mark the start of an internal batch.
1929
+ // This is needed because handshake and connect are async.
1930
+ // It may happen that the application calls init() then subscribe()
1931
+ // and the subscribe message is sent before the connect message, if
1932
+ // the subscribe message is not held until the connect message is sent.
1933
+ // So here we start a batch to hold temporarily any message until
1934
+ // the connection is fully established.
1935
+ _internalBatch = true;
1936
+
1937
+ // Save the properties provided by the user, so that
1938
+ // we can reuse them during automatic re-handshake
1939
+ _handshakeProps = handshakeProps;
1940
+ _handshakeCallback = handshakeCallback;
1941
+
1942
+ var version = '1.0';
1943
+
1944
+ // Figure out the transports to send to the server
1945
+ var url = _cometd.getURL();
1946
+ var transportTypes = _transports.findTransportTypes(version, _crossDomain, url);
1947
+
1948
+ var bayeuxMessage = {
1949
+ id: _nextMessageId(),
1950
+ version: version,
1951
+ minimumVersion: version,
1952
+ channel: '/meta/handshake',
1953
+ supportedConnectionTypes: transportTypes,
1954
+ advice: {
1955
+ timeout: _advice.timeout,
1956
+ interval: _advice.interval
1957
+ }
1958
+ };
1959
+ // Do not allow the user to override important fields.
1960
+ var message = _cometd._mixin(false, {}, _handshakeProps, bayeuxMessage);
1961
+
1962
+ // Save the callback.
1963
+ _cometd._putCallback(message.id, handshakeCallback);
1964
+
1965
+ // Pick up the first available transport as initial transport
1966
+ // since we don't know if the server supports it
1967
+ if (!_transport) {
1968
+ _transport = _transports.negotiateTransport(transportTypes, version, _crossDomain, url);
1969
+ if (!_transport) {
1970
+ var failure = 'Could not find initial transport among: ' + _transports.getTransportTypes();
1971
+ _cometd._warn(failure);
1972
+ throw failure;
1973
+ }
1974
+ }
1975
+
1976
+ _cometd._debug('Initial transport is', _transport.getType());
1977
+
1978
+ // We started a batch to hold the application messages,
1979
+ // so here we must bypass it and send immediately.
1980
+ _setStatus('handshaking');
1981
+ _cometd._debug('Handshake sent', message);
1982
+ _send([message], false, 'handshake');
1983
+ }
1984
+
1985
+ function _delayedHandshake(delay) {
1986
+ _setStatus('handshaking');
1987
+
1988
+ // We will call _handshake() which will reset _clientId, but we want to avoid
1989
+ // that between the end of this method and the call to _handshake() someone may
1990
+ // call publish() (or other methods that call _queueSend()).
1991
+ _internalBatch = true;
1992
+
1993
+ _delayedSend(function() {
1994
+ _handshake(_handshakeProps, _handshakeCallback);
1995
+ }, delay);
1996
+ }
1997
+
1998
+ function _notifyCallback(callback, message) {
1999
+ try {
2000
+ callback.call(_cometd, message);
2001
+ } catch (x) {
2002
+ var handler = _cometd.onCallbackException;
2003
+ if (_isFunction(handler)) {
2004
+ _cometd._debug('Invoking callback exception handler', x);
2005
+ try {
2006
+ handler.call(_cometd, x, message);
2007
+ } catch (xx) {
2008
+ _cometd._info('Exception during execution of callback exception handler', xx);
2009
+ }
2010
+ } else {
2011
+ _cometd._info('Exception during execution of message callback', x);
2012
+ }
2013
+ }
2014
+ }
2015
+
2016
+ this._getCallback = function(messageId) {
2017
+ return _callbacks[messageId];
2018
+ };
2019
+
2020
+ this._putCallback = function(messageId, callback) {
2021
+ var result = this._getCallback(messageId);
2022
+ if (_isFunction(callback)) {
2023
+ _callbacks[messageId] = callback;
2024
+ }
2025
+ return result;
2026
+ };
2027
+
2028
+ function _handleCallback(message) {
2029
+ var callback = _cometd._getCallback([message.id]);
2030
+ if (_isFunction(callback)) {
2031
+ delete _callbacks[message.id];
2032
+ _notifyCallback(callback, message);
2033
+ }
2034
+ }
2035
+
2036
+ function _handleRemoteCall(message) {
2037
+ var context = _remoteCalls[message.id];
2038
+ delete _remoteCalls[message.id];
2039
+ if (context) {
2040
+ _cometd._debug('Handling remote call response for', message, 'with context', context);
2041
+
2042
+ // Clear the timeout, if present.
2043
+ var timeout = context.timeout;
2044
+ if (timeout) {
2045
+ _cometd.clearTimeout(timeout);
2046
+ }
2047
+
2048
+ var callback = context.callback;
2049
+ if (_isFunction(callback)) {
2050
+ _notifyCallback(callback, message);
2051
+ return true;
2052
+ }
2053
+ }
2054
+ return false;
2055
+ }
2056
+
2057
+ this.onTransportFailure = function(message, failureInfo, failureHandler) {
2058
+ this._debug('Transport failure', failureInfo, 'for', message);
2059
+
2060
+ var transports = this.getTransportRegistry();
2061
+ var url = this.getURL();
2062
+ var crossDomain = this._isCrossDomain(_splitURL(url)[2]);
2063
+ var version = '1.0';
2064
+ var transportTypes = transports.findTransportTypes(version, crossDomain, url);
2065
+
2066
+ if (failureInfo.action === 'none') {
2067
+ if (message.channel === '/meta/handshake') {
2068
+ if (!failureInfo.transport) {
2069
+ var failure = 'Could not negotiate transport, client=[' + transportTypes + '], server=[' + message.supportedConnectionTypes + ']';
2070
+ this._warn(failure);
2071
+ _notifyTransportException(_transport.getType(), null, {
2072
+ reason: failure,
2073
+ connectionType: _transport.getType(),
2074
+ transport: _transport
2075
+ });
2076
+ }
2077
+ }
2078
+ } else {
2079
+ failureInfo.delay = this.getBackoffPeriod();
2080
+ // Different logic depending on whether we are handshaking or connecting.
2081
+ if (message.channel === '/meta/handshake') {
2082
+ if (!failureInfo.transport) {
2083
+ // The transport is invalid, try to negotiate again.
2084
+ var oldTransportType = _transport ? _transport.getType() : null;
2085
+ var newTransport = transports.negotiateTransport(transportTypes, version, crossDomain, url);
2086
+ if (!newTransport) {
2087
+ this._warn('Could not negotiate transport, client=[' + transportTypes + ']');
2088
+ _notifyTransportException(oldTransportType, null, message.failure);
2089
+ failureInfo.action = 'none';
2090
+ } else {
2091
+ var newTransportType = newTransport.getType();
2092
+ this._debug('Transport', oldTransportType, '->', newTransportType);
2093
+ _notifyTransportException(oldTransportType, newTransportType, message.failure);
2094
+ failureInfo.action = 'handshake';
2095
+ failureInfo.transport = newTransport;
2096
+ }
2097
+ }
2098
+
2099
+ if (failureInfo.action !== 'none') {
2100
+ this.increaseBackoffPeriod();
2101
+ }
2102
+ } else {
2103
+ var now = new Date().getTime();
2104
+
2105
+ if (_unconnectTime === 0) {
2106
+ _unconnectTime = now;
2107
+ }
2108
+
2109
+ if (failureInfo.action === 'retry') {
2110
+ failureInfo.delay = this.increaseBackoffPeriod();
2111
+ // Check whether we may switch to handshaking.
2112
+ var maxInterval = _advice.maxInterval;
2113
+ if (maxInterval > 0) {
2114
+ var expiration = _advice.timeout + _advice.interval + maxInterval;
2115
+ var unconnected = now - _unconnectTime;
2116
+ if (unconnected + _backoff > expiration) {
2117
+ failureInfo.action = 'handshake';
2118
+ }
2119
+ }
2120
+ }
2121
+
2122
+ if (failureInfo.action === 'handshake') {
2123
+ failureInfo.delay = 0;
2124
+ transports.reset(false);
2125
+ this.resetBackoffPeriod();
2126
+ }
2127
+ }
2128
+ }
2129
+
2130
+ failureHandler.call(_cometd, failureInfo);
2131
+ };
2132
+
2133
+ function _handleTransportFailure(failureInfo) {
2134
+ _cometd._debug('Transport failure handling', failureInfo);
2135
+
2136
+ if (failureInfo.transport) {
2137
+ _transport = failureInfo.transport;
2138
+ }
2139
+
2140
+ if (failureInfo.url) {
2141
+ _transport.setURL(failureInfo.url);
2142
+ }
2143
+
2144
+ var action = failureInfo.action;
2145
+ var delay = failureInfo.delay || 0;
2146
+ switch (action) {
2147
+ case 'handshake':
2148
+ _delayedHandshake(delay);
2149
+ break;
2150
+ case 'retry':
2151
+ _delayedConnect(delay);
2152
+ break;
2153
+ case 'none':
2154
+ _disconnect(true);
2155
+ break;
2156
+ default:
2157
+ throw 'Unknown action ' + action;
2158
+ }
2159
+ }
2160
+
2161
+ function _failHandshake(message, failureInfo) {
2162
+ _handleCallback(message);
2163
+ _notifyListeners('/meta/handshake', message);
2164
+ _notifyListeners('/meta/unsuccessful', message);
2165
+
2166
+ // The listeners may have disconnected.
2167
+ if (_isDisconnected()) {
2168
+ failureInfo.action = 'none';
2169
+ }
2170
+
2171
+ _cometd.onTransportFailure.call(_cometd, message, failureInfo, _handleTransportFailure);
2172
+ }
2173
+
2174
+ function _handshakeResponse(message) {
2175
+ var url = _cometd.getURL();
2176
+ if (message.successful) {
2177
+ var crossDomain = _cometd._isCrossDomain(_splitURL(url)[2]);
2178
+ var newTransport = _transports.negotiateTransport(message.supportedConnectionTypes, message.version, crossDomain, url);
2179
+ if (newTransport === null) {
2180
+ message.successful = false;
2181
+ _failHandshake(message, {
2182
+ cause: 'negotiation',
2183
+ action: 'none',
2184
+ transport: null
2185
+ });
2186
+ return;
2187
+ } else if (_transport !== newTransport) {
2188
+ _cometd._debug('Transport', _transport.getType(), '->', newTransport.getType());
2189
+ _transport = newTransport;
2190
+ }
2191
+
2192
+ _clientId = message.clientId;
2193
+
2194
+ // End the internal batch and allow held messages from the application
2195
+ // to go to the server (see _handshake() where we start the internal batch).
2196
+ _internalBatch = false;
2197
+ _flushBatch();
2198
+
2199
+ // Here the new transport is in place, as well as the clientId, so
2200
+ // the listeners can perform a publish() if they want.
2201
+ // Notify the listeners before the connect below.
2202
+ message.reestablish = _reestablish;
2203
+ _reestablish = true;
2204
+
2205
+ _handleCallback(message);
2206
+ _notifyListeners('/meta/handshake', message);
2207
+
2208
+ _handshakeMessages = message['x-messages'] || 0;
2209
+
2210
+ var action = _isDisconnected() ? 'none' : _advice.reconnect || 'retry';
2211
+ switch (action) {
2212
+ case 'retry':
2213
+ _resetBackoff();
2214
+ if (_handshakeMessages === 0) {
2215
+ _delayedConnect(0);
2216
+ } else {
2217
+ _cometd._debug('Processing', _handshakeMessages, 'handshake-delivered messages');
2218
+ }
2219
+ break;
2220
+ case 'none':
2221
+ _disconnect(true);
2222
+ break;
2223
+ default:
2224
+ throw 'Unrecognized advice action ' + action;
2225
+ }
2226
+ } else {
2227
+ _failHandshake(message, {
2228
+ cause: 'unsuccessful',
2229
+ action: _advice.reconnect || 'handshake',
2230
+ transport: _transport
2231
+ });
2232
+ }
2233
+ }
2234
+
2235
+ function _handshakeFailure(message) {
2236
+ _failHandshake(message, {
2237
+ cause: 'failure',
2238
+ action: 'handshake',
2239
+ transport: null
2240
+ });
2241
+ }
2242
+
2243
+ function _matchMetaConnect(connect) {
2244
+ if (_status === 'disconnected') {
2245
+ return true;
2246
+ }
2247
+ if (_metaConnect && _metaConnect.id === connect.id) {
2248
+ _metaConnect = null;
2249
+ return true;
2250
+ }
2251
+ return false;
2252
+ }
2253
+
2254
+ function _failConnect(message, failureInfo) {
2255
+ // Notify the listeners after the status change but before the next action.
2256
+ _notifyListeners('/meta/connect', message);
2257
+ _notifyListeners('/meta/unsuccessful', message);
2258
+
2259
+ // The listeners may have disconnected.
2260
+ if (_isDisconnected()) {
2261
+ failureInfo.action = 'none';
2262
+ }
2263
+
2264
+ _cometd.onTransportFailure.call(_cometd, message, failureInfo, _handleTransportFailure);
2265
+ }
2266
+
2267
+ function _connectResponse(message) {
2268
+ if (_matchMetaConnect(message)) {
2269
+ _connected = message.successful;
2270
+ if (_connected) {
2271
+ _notifyListeners('/meta/connect', message);
2272
+
2273
+ // Normally, the advice will say "reconnect: 'retry', interval: 0"
2274
+ // and the server will hold the request, so when a response returns
2275
+ // we immediately call the server again (long polling).
2276
+ // Listeners can call disconnect(), so check the state after they run.
2277
+ var action = _isDisconnected() ? 'none' : _advice.reconnect || 'retry';
2278
+ switch (action) {
2279
+ case 'retry':
2280
+ _resetBackoff();
2281
+ _delayedConnect(_backoff);
2282
+ break;
2283
+ case 'none':
2284
+ _disconnect(false);
2285
+ break;
2286
+ default:
2287
+ throw 'Unrecognized advice action ' + action;
2288
+ }
2289
+ } else {
2290
+ _failConnect(message, {
2291
+ cause: 'unsuccessful',
2292
+ action: _advice.reconnect || 'retry',
2293
+ transport: _transport
2294
+ });
2295
+ }
2296
+ } else {
2297
+ _cometd._debug('Mismatched /meta/connect reply', message);
2298
+ }
2299
+ }
2300
+
2301
+ function _connectFailure(message) {
2302
+ if (_matchMetaConnect(message)) {
2303
+ _connected = false;
2304
+ _failConnect(message, {
2305
+ cause: 'failure',
2306
+ action: 'retry',
2307
+ transport: null
2308
+ });
2309
+ } else {
2310
+ _cometd._debug('Mismatched /meta/connect failure', message);
2311
+ }
2312
+ }
2313
+
2314
+ function _failDisconnect(message) {
2315
+ _disconnect(true);
2316
+ _handleCallback(message);
2317
+ _notifyListeners('/meta/disconnect', message);
2318
+ _notifyListeners('/meta/unsuccessful', message);
2319
+ }
2320
+
2321
+ function _disconnectResponse(message) {
2322
+ if (message.successful) {
2323
+ // Wait for the /meta/connect to arrive.
2324
+ _disconnect(false);
2325
+ _handleCallback(message);
2326
+ _notifyListeners('/meta/disconnect', message);
2327
+ } else {
2328
+ _failDisconnect(message);
2329
+ }
2330
+ }
2331
+
2332
+ function _disconnectFailure(message) {
2333
+ _failDisconnect(message);
2334
+ }
2335
+
2336
+ function _failSubscribe(message) {
2337
+ var subscriptions = _listeners[message.subscription];
2338
+ if (subscriptions) {
2339
+ for (var id in subscriptions) {
2340
+ if (subscriptions.hasOwnProperty(id)) {
2341
+ var subscription = subscriptions[id];
2342
+ if (subscription && !subscription.listener) {
2343
+ delete subscriptions[id];
2344
+ _cometd._debug('Removed failed subscription', subscription);
2345
+ }
2346
+ }
2347
+ }
2348
+ }
2349
+ _handleCallback(message);
2350
+ _notifyListeners('/meta/subscribe', message);
2351
+ _notifyListeners('/meta/unsuccessful', message);
2352
+ }
2353
+
2354
+ function _subscribeResponse(message) {
2355
+ if (message.successful) {
2356
+ _handleCallback(message);
2357
+ _notifyListeners('/meta/subscribe', message);
2358
+ } else {
2359
+ _failSubscribe(message);
2360
+ }
2361
+ }
2362
+
2363
+ function _subscribeFailure(message) {
2364
+ _failSubscribe(message);
2365
+ }
2366
+
2367
+ function _failUnsubscribe(message) {
2368
+ _handleCallback(message);
2369
+ _notifyListeners('/meta/unsubscribe', message);
2370
+ _notifyListeners('/meta/unsuccessful', message);
2371
+ }
2372
+
2373
+ function _unsubscribeResponse(message) {
2374
+ if (message.successful) {
2375
+ _handleCallback(message);
2376
+ _notifyListeners('/meta/unsubscribe', message);
2377
+ } else {
2378
+ _failUnsubscribe(message);
2379
+ }
2380
+ }
2381
+
2382
+ function _unsubscribeFailure(message) {
2383
+ _failUnsubscribe(message);
2384
+ }
2385
+
2386
+ function _failMessage(message) {
2387
+ if (!_handleRemoteCall(message)) {
2388
+ _handleCallback(message);
2389
+ _notifyListeners('/meta/publish', message);
2390
+ _notifyListeners('/meta/unsuccessful', message);
2391
+ }
2392
+ }
2393
+
2394
+ function _messageResponse(message) {
2395
+ if (message.data !== undefined) {
2396
+ if (!_handleRemoteCall(message)) {
2397
+ _notifyListeners(message.channel, message);
2398
+ if (_handshakeMessages > 0) {
2399
+ --_handshakeMessages;
2400
+ if (_handshakeMessages === 0) {
2401
+ _cometd._debug('Processed last handshake-delivered message');
2402
+ _delayedConnect(0);
2403
+ }
2404
+ }
2405
+ }
2406
+ } else {
2407
+ if (message.successful === undefined) {
2408
+ _cometd._warn('Unknown Bayeux Message', message);
2409
+ } else {
2410
+ if (message.successful) {
2411
+ _handleCallback(message);
2412
+ _notifyListeners('/meta/publish', message);
2413
+ } else {
2414
+ _failMessage(message);
2415
+ }
2416
+ }
2417
+ }
2418
+ }
2419
+
2420
+ function _messageFailure(failure) {
2421
+ _failMessage(failure);
2422
+ }
2423
+
2424
+ function _receive(message) {
2425
+ _unconnectTime = 0;
2426
+
2427
+ message = _applyIncomingExtensions(message);
2428
+ if (message === undefined || message === null) {
2429
+ return;
2430
+ }
2431
+
2432
+ _updateAdvice(message.advice);
2433
+
2434
+ var channel = message.channel;
2435
+ switch (channel) {
2436
+ case '/meta/handshake':
2437
+ _handshakeResponse(message);
2438
+ break;
2439
+ case '/meta/connect':
2440
+ _connectResponse(message);
2441
+ break;
2442
+ case '/meta/disconnect':
2443
+ _disconnectResponse(message);
2444
+ break;
2445
+ case '/meta/subscribe':
2446
+ _subscribeResponse(message);
2447
+ break;
2448
+ case '/meta/unsubscribe':
2449
+ _unsubscribeResponse(message);
2450
+ break;
2451
+ default:
2452
+ _messageResponse(message);
2453
+ break;
2454
+ }
2455
+ }
2456
+
2457
+ /**
2458
+ * Receives a message.
2459
+ * This method is exposed as a public so that extensions may inject
2460
+ * messages simulating that they had been received.
2461
+ */
2462
+ this.receive = _receive;
2463
+
2464
+ _handleMessages = function(rcvdMessages) {
2465
+ _cometd._debug('Received', rcvdMessages);
2466
+
2467
+ for (var i = 0; i < rcvdMessages.length; ++i) {
2468
+ var message = rcvdMessages[i];
2469
+ _receive(message);
2470
+ }
2471
+ };
2472
+
2473
+ _handleFailure = function(conduit, messages, failure) {
2474
+ _cometd._debug('handleFailure', conduit, messages, failure);
2475
+
2476
+ failure.transport = conduit;
2477
+ for (var i = 0; i < messages.length; ++i) {
2478
+ var message = messages[i];
2479
+ var failureMessage = {
2480
+ id: message.id,
2481
+ successful: false,
2482
+ channel: message.channel,
2483
+ failure: failure
2484
+ };
2485
+ failure.message = message;
2486
+ switch (message.channel) {
2487
+ case '/meta/handshake':
2488
+ _handshakeFailure(failureMessage);
2489
+ break;
2490
+ case '/meta/connect':
2491
+ _connectFailure(failureMessage);
2492
+ break;
2493
+ case '/meta/disconnect':
2494
+ _disconnectFailure(failureMessage);
2495
+ break;
2496
+ case '/meta/subscribe':
2497
+ failureMessage.subscription = message.subscription;
2498
+ _subscribeFailure(failureMessage);
2499
+ break;
2500
+ case '/meta/unsubscribe':
2501
+ failureMessage.subscription = message.subscription;
2502
+ _unsubscribeFailure(failureMessage);
2503
+ break;
2504
+ default:
2505
+ _messageFailure(failureMessage);
2506
+ break;
2507
+ }
2508
+ }
2509
+ };
2510
+
2511
+ function _hasSubscriptions(channel) {
2512
+ var subscriptions = _listeners[channel];
2513
+ if (subscriptions) {
2514
+ for (var id in subscriptions) {
2515
+ if (subscriptions.hasOwnProperty(id)) {
2516
+ if (subscriptions[id]) {
2517
+ return true;
2518
+ }
2519
+ }
2520
+ }
2521
+ }
2522
+ return false;
2523
+ }
2524
+
2525
+ function _resolveScopedCallback(scope, callback) {
2526
+ var delegate = {
2527
+ scope: scope,
2528
+ method: callback
2529
+ };
2530
+ if (_isFunction(scope)) {
2531
+ delegate.scope = undefined;
2532
+ delegate.method = scope;
2533
+ } else {
2534
+ if (_isString(callback)) {
2535
+ if (!scope) {
2536
+ throw 'Invalid scope ' + scope;
2537
+ }
2538
+ delegate.method = scope[callback];
2539
+ if (!_isFunction(delegate.method)) {
2540
+ throw 'Invalid callback ' + callback + ' for scope ' + scope;
2541
+ }
2542
+ } else if (!_isFunction(callback)) {
2543
+ throw 'Invalid callback ' + callback;
2544
+ }
2545
+ }
2546
+ return delegate;
2547
+ }
2548
+
2549
+ function _addListener(channel, scope, callback, isListener) {
2550
+ // The data structure is a map<channel, subscription[]>, where each subscription
2551
+ // holds the callback to be called and its scope.
2552
+
2553
+ var delegate = _resolveScopedCallback(scope, callback);
2554
+ _cometd._debug('Adding', isListener ? 'listener' : 'subscription', 'on', channel, 'with scope', delegate.scope, 'and callback', delegate.method);
2555
+
2556
+ var id = ++_listenerId;
2557
+ var subscription = {
2558
+ id: id,
2559
+ channel: channel,
2560
+ scope: delegate.scope,
2561
+ callback: delegate.method,
2562
+ listener: isListener
2563
+ };
2564
+
2565
+ var subscriptions = _listeners[channel];
2566
+ if (!subscriptions) {
2567
+ subscriptions = {};
2568
+ _listeners[channel] = subscriptions;
2569
+ }
2570
+
2571
+ subscriptions[id] = subscription;
2572
+
2573
+ _cometd._debug('Added', isListener ? 'listener' : 'subscription', subscription);
2574
+
2575
+ return subscription;
2576
+ }
2577
+
2578
+ //
2579
+ // PUBLIC API
2580
+ //
2581
+
2582
+ /**
2583
+ * Registers the given transport under the given transport type.
2584
+ * The optional index parameter specifies the "priority" at which the
2585
+ * transport is registered (where 0 is the max priority).
2586
+ * If a transport with the same type is already registered, this function
2587
+ * does nothing and returns false.
2588
+ * @param type the transport type
2589
+ * @param transport the transport object
2590
+ * @param index the index at which this transport is to be registered
2591
+ * @return true if the transport has been registered, false otherwise
2592
+ * @see #unregisterTransport(type)
2593
+ */
2594
+ this.registerTransport = function(type, transport, index) {
2595
+ var result = _transports.add(type, transport, index);
2596
+ if (result) {
2597
+ this._debug('Registered transport', type);
2598
+
2599
+ if (_isFunction(transport.registered)) {
2600
+ transport.registered(type, this);
2601
+ }
2602
+ }
2603
+ return result;
2604
+ };
2605
+
2606
+ /**
2607
+ * Unregisters the transport with the given transport type.
2608
+ * @param type the transport type to unregister
2609
+ * @return the transport that has been unregistered,
2610
+ * or null if no transport was previously registered under the given transport type
2611
+ */
2612
+ this.unregisterTransport = function(type) {
2613
+ var transport = _transports.remove(type);
2614
+ if (transport !== null) {
2615
+ this._debug('Unregistered transport', type);
2616
+
2617
+ if (_isFunction(transport.unregistered)) {
2618
+ transport.unregistered();
2619
+ }
2620
+ }
2621
+ return transport;
2622
+ };
2623
+
2624
+ this.unregisterTransports = function() {
2625
+ _transports.clear();
2626
+ };
2627
+
2628
+ /**
2629
+ * @return an array of all registered transport types
2630
+ */
2631
+ this.getTransportTypes = function() {
2632
+ return _transports.getTransportTypes();
2633
+ };
2634
+
2635
+ this.findTransport = function(name) {
2636
+ return _transports.find(name);
2637
+ };
2638
+
2639
+ /**
2640
+ * @returns the TransportRegistry object
2641
+ */
2642
+ this.getTransportRegistry = function() {
2643
+ return _transports;
2644
+ };
2645
+
2646
+ /**
2647
+ * Configures the initial Bayeux communication with the Bayeux server.
2648
+ * Configuration is passed via an object that must contain a mandatory field <code>url</code>
2649
+ * of type string containing the URL of the Bayeux server.
2650
+ * @param configuration the configuration object
2651
+ */
2652
+ this.configure = function(configuration) {
2653
+ _configure.call(this, configuration);
2654
+ };
2655
+
2656
+ /**
2657
+ * Configures and establishes the Bayeux communication with the Bayeux server
2658
+ * via a handshake and a subsequent connect.
2659
+ * @param configuration the configuration object
2660
+ * @param handshakeProps an object to be merged with the handshake message
2661
+ * @see #configure(configuration)
2662
+ * @see #handshake(handshakeProps)
2663
+ */
2664
+ this.init = function(configuration, handshakeProps) {
2665
+ this.configure(configuration);
2666
+ this.handshake(handshakeProps);
2667
+ };
2668
+
2669
+ /**
2670
+ * Establishes the Bayeux communication with the Bayeux server
2671
+ * via a handshake and a subsequent connect.
2672
+ * @param handshakeProps an object to be merged with the handshake message
2673
+ * @param handshakeCallback a function to be invoked when the handshake is acknowledged
2674
+ */
2675
+ this.handshake = function(handshakeProps, handshakeCallback) {
2676
+ if (_status !== 'disconnected') {
2677
+ throw 'Illegal state: handshaken';
2678
+ }
2679
+ _handshake(handshakeProps, handshakeCallback);
2680
+ };
2681
+
2682
+ /**
2683
+ * Disconnects from the Bayeux server.
2684
+ * @param disconnectProps an object to be merged with the disconnect message
2685
+ * @param disconnectCallback a function to be invoked when the disconnect is acknowledged
2686
+ */
2687
+ this.disconnect = function(disconnectProps, disconnectCallback) {
2688
+ if (_isDisconnected()) {
2689
+ return;
2690
+ }
2691
+
2692
+ if (_isFunction(disconnectProps)) {
2693
+ disconnectCallback = disconnectProps;
2694
+ disconnectProps = undefined;
2695
+ }
2696
+
2697
+ var bayeuxMessage = {
2698
+ id: _nextMessageId(),
2699
+ channel: '/meta/disconnect'
2700
+ };
2701
+ // Do not allow the user to override important fields.
2702
+ var message = this._mixin(false, {}, disconnectProps, bayeuxMessage);
2703
+
2704
+ // Save the callback.
2705
+ _cometd._putCallback(message.id, disconnectCallback);
2706
+
2707
+ _setStatus('disconnecting');
2708
+ _send([message], false, 'disconnect');
2709
+ };
2710
+
2711
+ /**
2712
+ * Marks the start of a batch of application messages to be sent to the server
2713
+ * in a single request, obtaining a single response containing (possibly) many
2714
+ * application reply messages.
2715
+ * Messages are held in a queue and not sent until {@link #endBatch()} is called.
2716
+ * If startBatch() is called multiple times, then an equal number of endBatch()
2717
+ * calls must be made to close and send the batch of messages.
2718
+ * @see #endBatch()
2719
+ */
2720
+ this.startBatch = function() {
2721
+ _startBatch();
2722
+ };
2723
+
2724
+ /**
2725
+ * Marks the end of a batch of application messages to be sent to the server
2726
+ * in a single request.
2727
+ * @see #startBatch()
2728
+ */
2729
+ this.endBatch = function() {
2730
+ _endBatch();
2731
+ };
2732
+
2733
+ /**
2734
+ * Executes the given callback in the given scope, surrounded by a {@link #startBatch()}
2735
+ * and {@link #endBatch()} calls.
2736
+ * @param scope the scope of the callback, may be omitted
2737
+ * @param callback the callback to be executed within {@link #startBatch()} and {@link #endBatch()} calls
2738
+ */
2739
+ this.batch = function(scope, callback) {
2740
+ var delegate = _resolveScopedCallback(scope, callback);
2741
+ this.startBatch();
2742
+ try {
2743
+ delegate.method.call(delegate.scope);
2744
+ this.endBatch();
2745
+ } catch (x) {
2746
+ this._info('Exception during execution of batch', x);
2747
+ this.endBatch();
2748
+ throw x;
2749
+ }
2750
+ };
2751
+
2752
+ /**
2753
+ * Adds a listener for bayeux messages, performing the given callback in the given scope
2754
+ * when a message for the given channel arrives.
2755
+ * @param channel the channel the listener is interested to
2756
+ * @param scope the scope of the callback, may be omitted
2757
+ * @param callback the callback to call when a message is sent to the channel
2758
+ * @returns the subscription handle to be passed to {@link #removeListener(object)}
2759
+ * @see #removeListener(subscription)
2760
+ */
2761
+ this.addListener = function(channel, scope, callback) {
2762
+ if (arguments.length < 2) {
2763
+ throw 'Illegal arguments number: required 2, got ' + arguments.length;
2764
+ }
2765
+ if (!_isString(channel)) {
2766
+ throw 'Illegal argument type: channel must be a string';
2767
+ }
2768
+
2769
+ return _addListener(channel, scope, callback, true);
2770
+ };
2771
+
2772
+ /**
2773
+ * Removes the subscription obtained with a call to {@link #addListener(string, object, function)}.
2774
+ * @param subscription the subscription to unsubscribe.
2775
+ * @see #addListener(channel, scope, callback)
2776
+ */
2777
+ this.removeListener = function(subscription) {
2778
+ // Beware of subscription.id == 0, which is falsy => cannot use !subscription.id
2779
+ if (!subscription || !subscription.channel || !("id" in subscription)) {
2780
+ throw 'Invalid argument: expected subscription, not ' + subscription;
2781
+ }
2782
+
2783
+ _removeListener(subscription);
2784
+ };
2785
+
2786
+ /**
2787
+ * Removes all listeners registered with {@link #addListener(channel, scope, callback)} or
2788
+ * {@link #subscribe(channel, scope, callback)}.
2789
+ */
2790
+ this.clearListeners = function() {
2791
+ _listeners = {};
2792
+ };
2793
+
2794
+ /**
2795
+ * Subscribes to the given channel, performing the given callback in the given scope
2796
+ * when a message for the channel arrives.
2797
+ * @param channel the channel to subscribe to
2798
+ * @param scope the scope of the callback, may be omitted
2799
+ * @param callback the callback to call when a message is sent to the channel
2800
+ * @param subscribeProps an object to be merged with the subscribe message
2801
+ * @param subscribeCallback a function to be invoked when the subscription is acknowledged
2802
+ * @return the subscription handle to be passed to {@link #unsubscribe(object)}
2803
+ */
2804
+ this.subscribe = function(channel, scope, callback, subscribeProps, subscribeCallback) {
2805
+ if (arguments.length < 2) {
2806
+ throw 'Illegal arguments number: required 2, got ' + arguments.length;
2807
+ }
2808
+ if (!_isString(channel)) {
2809
+ throw 'Illegal argument type: channel must be a string';
2810
+ }
2811
+ if (_isDisconnected()) {
2812
+ throw 'Illegal state: disconnected';
2813
+ }
2814
+
2815
+ // Normalize arguments
2816
+ if (_isFunction(scope)) {
2817
+ subscribeCallback = subscribeProps;
2818
+ subscribeProps = callback;
2819
+ callback = scope;
2820
+ scope = undefined;
2821
+ }
2822
+ if (_isFunction(subscribeProps)) {
2823
+ subscribeCallback = subscribeProps;
2824
+ subscribeProps = undefined;
2825
+ }
2826
+
2827
+ // Only send the message to the server if this client has not yet subscribed to the channel
2828
+ var send = !_hasSubscriptions(channel);
2829
+
2830
+ var subscription = _addListener(channel, scope, callback, false);
2831
+
2832
+ if (send) {
2833
+ // Send the subscription message after the subscription registration to avoid
2834
+ // races where the server would send a message to the subscribers, but here
2835
+ // on the client the subscription has not been added yet to the data structures
2836
+ var bayeuxMessage = {
2837
+ id: _nextMessageId(),
2838
+ channel: '/meta/subscribe',
2839
+ subscription: channel
2840
+ };
2841
+ // Do not allow the user to override important fields.
2842
+ var message = this._mixin(false, {}, subscribeProps, bayeuxMessage);
2843
+
2844
+ // Save the callback.
2845
+ _cometd._putCallback(message.id, subscribeCallback);
2846
+
2847
+ _queueSend(message);
2848
+ }
2849
+
2850
+ return subscription;
2851
+ };
2852
+
2853
+ /**
2854
+ * Unsubscribes the subscription obtained with a call to {@link #subscribe(string, object, function)}.
2855
+ * @param subscription the subscription to unsubscribe.
2856
+ * @param unsubscribeProps an object to be merged with the unsubscribe message
2857
+ * @param unsubscribeCallback a function to be invoked when the unsubscription is acknowledged
2858
+ */
2859
+ this.unsubscribe = function(subscription, unsubscribeProps, unsubscribeCallback) {
2860
+ if (arguments.length < 1) {
2861
+ throw 'Illegal arguments number: required 1, got ' + arguments.length;
2862
+ }
2863
+ if (_isDisconnected()) {
2864
+ throw 'Illegal state: disconnected';
2865
+ }
2866
+
2867
+ if (_isFunction(unsubscribeProps)) {
2868
+ unsubscribeCallback = unsubscribeProps;
2869
+ unsubscribeProps = undefined;
2870
+ }
2871
+
2872
+ // Remove the local listener before sending the message
2873
+ // This ensures that if the server fails, this client does not get notifications
2874
+ this.removeListener(subscription);
2875
+
2876
+ var channel = subscription.channel;
2877
+ // Only send the message to the server if this client unsubscribes the last subscription
2878
+ if (!_hasSubscriptions(channel)) {
2879
+ var bayeuxMessage = {
2880
+ id: _nextMessageId(),
2881
+ channel: '/meta/unsubscribe',
2882
+ subscription: channel
2883
+ };
2884
+ // Do not allow the user to override important fields.
2885
+ var message = this._mixin(false, {}, unsubscribeProps, bayeuxMessage);
2886
+
2887
+ // Save the callback.
2888
+ _cometd._putCallback(message.id, unsubscribeCallback);
2889
+
2890
+ _queueSend(message);
2891
+ }
2892
+ };
2893
+
2894
+ this.resubscribe = function(subscription, subscribeProps) {
2895
+ _removeSubscription(subscription);
2896
+ if (subscription) {
2897
+ return this.subscribe(subscription.channel, subscription.scope, subscription.callback, subscribeProps);
2898
+ }
2899
+ return undefined;
2900
+ };
2901
+
2902
+ /**
2903
+ * Removes all subscriptions added via {@link #subscribe(channel, scope, callback, subscribeProps)},
2904
+ * but does not remove the listeners added via {@link addListener(channel, scope, callback)}.
2905
+ */
2906
+ this.clearSubscriptions = function() {
2907
+ _clearSubscriptions();
2908
+ };
2909
+
2910
+ /**
2911
+ * Publishes a message on the given channel, containing the given content.
2912
+ * @param channel the channel to publish the message to
2913
+ * @param content the content of the message
2914
+ * @param publishProps an object to be merged with the publish message
2915
+ * @param publishCallback a function to be invoked when the publish is acknowledged by the server
2916
+ */
2917
+ this.publish = function(channel, content, publishProps, publishCallback) {
2918
+ if (arguments.length < 1) {
2919
+ throw 'Illegal arguments number: required 1, got ' + arguments.length;
2920
+ }
2921
+ if (!_isString(channel)) {
2922
+ throw 'Illegal argument type: channel must be a string';
2923
+ }
2924
+ if (/^\/meta\//.test(channel)) {
2925
+ throw 'Illegal argument: cannot publish to meta channels';
2926
+ }
2927
+ if (_isDisconnected()) {
2928
+ throw 'Illegal state: disconnected';
2929
+ }
2930
+
2931
+ if (_isFunction(content)) {
2932
+ publishCallback = content;
2933
+ content = {};
2934
+ publishProps = undefined;
2935
+ } else if (_isFunction(publishProps)) {
2936
+ publishCallback = publishProps;
2937
+ publishProps = undefined;
2938
+ }
2939
+
2940
+ var bayeuxMessage = {
2941
+ id: _nextMessageId(),
2942
+ channel: channel,
2943
+ data: content
2944
+ };
2945
+ // Do not allow the user to override important fields.
2946
+ var message = this._mixin(false, {}, publishProps, bayeuxMessage);
2947
+
2948
+ // Save the callback.
2949
+ _cometd._putCallback(message.id, publishCallback);
2950
+
2951
+ _queueSend(message);
2952
+ };
2953
+
2954
+ /**
2955
+ * Publishes a message with binary data on the given channel.
2956
+ * The binary data chunk may be an ArrayBuffer, a DataView, a TypedArray
2957
+ * (such as Uint8Array) or a plain integer array.
2958
+ * The meta data object may contain additional application data such as
2959
+ * a file name, a mime type, etc.
2960
+ * @param channel the channel to publish the message to
2961
+ * @param data the binary data to publish
2962
+ * @param last whether the binary data chunk is the last
2963
+ * @param meta an object containing meta data associated to the binary chunk
2964
+ * @param callback a function to be invoked when the publish is acknowledged by the server
2965
+ */
2966
+ this.publishBinary = function(channel, data, last, meta, callback) {
2967
+ if (_isFunction(data)) {
2968
+ callback = data;
2969
+ data = new ArrayBuffer(0);
2970
+ last = true;
2971
+ meta = undefined;
2972
+ } else if (_isFunction(last)) {
2973
+ callback = last;
2974
+ last = true;
2975
+ meta = undefined;
2976
+ } else if (_isFunction(meta)) {
2977
+ callback = meta;
2978
+ meta = undefined;
2979
+ }
2980
+ var content = {
2981
+ meta: meta,
2982
+ data: data,
2983
+ last: last
2984
+ };
2985
+ var ext = {
2986
+ ext: {
2987
+ binary: {}
2988
+ }
2989
+ };
2990
+ this.publish(channel, content, ext, callback);
2991
+ };
2992
+
2993
+ this.remoteCall = function(target, content, timeout, callProps, callback) {
2994
+ if (arguments.length < 1) {
2995
+ throw 'Illegal arguments number: required 1, got ' + arguments.length;
2996
+ }
2997
+ if (!_isString(target)) {
2998
+ throw 'Illegal argument type: target must be a string';
2999
+ }
3000
+ if (_isDisconnected()) {
3001
+ throw 'Illegal state: disconnected';
3002
+ }
3003
+
3004
+ if (_isFunction(content)) {
3005
+ callback = content;
3006
+ content = {};
3007
+ timeout = _config.maxNetworkDelay;
3008
+ callProps = undefined;
3009
+ } else if (_isFunction(timeout)) {
3010
+ callback = timeout;
3011
+ timeout = _config.maxNetworkDelay;
3012
+ callProps = undefined;
3013
+ } else if (_isFunction(callProps)) {
3014
+ callback = callProps;
3015
+ callProps = undefined;
3016
+ }
3017
+
3018
+ if (typeof timeout !== 'number') {
3019
+ throw 'Illegal argument type: timeout must be a number';
3020
+ }
3021
+
3022
+ if (!target.match(/^\//)) {
3023
+ target = '/' + target;
3024
+ }
3025
+ var channel = '/service' + target;
3026
+
3027
+ var bayeuxMessage = {
3028
+ id: _nextMessageId(),
3029
+ channel: channel,
3030
+ data: content
3031
+ };
3032
+ var message = this._mixin(false, {}, callProps, bayeuxMessage);
3033
+
3034
+ var context = {
3035
+ callback: callback
3036
+ };
3037
+ if (timeout > 0) {
3038
+ context.timeout = _cometd.setTimeout(function() {
3039
+ _cometd._debug('Timing out remote call', message, 'after', timeout, 'ms');
3040
+ _failMessage({
3041
+ id: message.id,
3042
+ error: '406::timeout',
3043
+ successful: false,
3044
+ failure: {
3045
+ message: message,
3046
+ reason: 'Remote Call Timeout'
3047
+ }
3048
+ });
3049
+ }, timeout);
3050
+ _cometd._debug('Scheduled remote call timeout', message, 'in', timeout, 'ms');
3051
+ }
3052
+ _remoteCalls[message.id] = context;
3053
+
3054
+ _queueSend(message);
3055
+ };
3056
+
3057
+ this.remoteCallBinary = function(target, data, last, meta, timeout, callback) {
3058
+ if (_isFunction(data)) {
3059
+ callback = data;
3060
+ data = new ArrayBuffer(0);
3061
+ last = true;
3062
+ meta = undefined;
3063
+ timeout = _config.maxNetworkDelay;
3064
+ } else if (_isFunction(last)) {
3065
+ callback = last;
3066
+ last = true;
3067
+ meta = undefined;
3068
+ timeout = _config.maxNetworkDelay;
3069
+ } else if (_isFunction(meta)) {
3070
+ callback = meta;
3071
+ meta = undefined;
3072
+ timeout = _config.maxNetworkDelay;
3073
+ } else if (_isFunction(timeout)) {
3074
+ callback = timeout;
3075
+ timeout = _config.maxNetworkDelay;
3076
+ }
3077
+
3078
+ var content = {
3079
+ meta: meta,
3080
+ data: data,
3081
+ last: last
3082
+ };
3083
+ var ext = {
3084
+ ext: {
3085
+ binary: {}
3086
+ }
3087
+ };
3088
+
3089
+ this.remoteCall(target, content, timeout, ext, callback);
3090
+ };
3091
+
3092
+ /**
3093
+ * Returns a string representing the status of the bayeux communication with the Bayeux server.
3094
+ */
3095
+ this.getStatus = function() {
3096
+ return _status;
3097
+ };
3098
+
3099
+ /**
3100
+ * Returns whether this instance has been disconnected.
3101
+ */
3102
+ this.isDisconnected = _isDisconnected;
3103
+
3104
+ /**
3105
+ * Sets the backoff period used to increase the backoff time when retrying an unsuccessful or failed message.
3106
+ * Default value is 1 second, which means if there is a persistent failure the retries will happen
3107
+ * after 1 second, then after 2 seconds, then after 3 seconds, etc. So for example with 15 seconds of
3108
+ * elapsed time, there will be 5 retries (at 1, 3, 6, 10 and 15 seconds elapsed).
3109
+ * @param period the backoff period to set
3110
+ * @see #getBackoffIncrement()
3111
+ */
3112
+ this.setBackoffIncrement = function(period) {
3113
+ _config.backoffIncrement = period;
3114
+ };
3115
+
3116
+ /**
3117
+ * Returns the backoff period used to increase the backoff time when retrying an unsuccessful or failed message.
3118
+ * @see #setBackoffIncrement(period)
3119
+ */
3120
+ this.getBackoffIncrement = function() {
3121
+ return _config.backoffIncrement;
3122
+ };
3123
+
3124
+ /**
3125
+ * Returns the backoff period to wait before retrying an unsuccessful or failed message.
3126
+ */
3127
+ this.getBackoffPeriod = function() {
3128
+ return _backoff;
3129
+ };
3130
+
3131
+ /**
3132
+ * Increases the backoff period up to the maximum value configured.
3133
+ * @returns the backoff period after increment
3134
+ * @see getBackoffIncrement
3135
+ */
3136
+ this.increaseBackoffPeriod = function() {
3137
+ return _increaseBackoff();
3138
+ };
3139
+
3140
+ /**
3141
+ * Resets the backoff period to zero.
3142
+ */
3143
+ this.resetBackoffPeriod = function() {
3144
+ _resetBackoff();
3145
+ };
3146
+
3147
+ /**
3148
+ * Sets the log level for console logging.
3149
+ * Valid values are the strings 'error', 'warn', 'info' and 'debug', from
3150
+ * less verbose to more verbose.
3151
+ * @param level the log level string
3152
+ */
3153
+ this.setLogLevel = function(level) {
3154
+ _config.logLevel = level;
3155
+ };
3156
+
3157
+ /**
3158
+ * Registers an extension whose callbacks are called for every incoming message
3159
+ * (that comes from the server to this client implementation) and for every
3160
+ * outgoing message (that originates from this client implementation for the
3161
+ * server).
3162
+ * The format of the extension object is the following:
3163
+ * <pre>
3164
+ * {
3165
+ * incoming: function(message) { ... },
3166
+ * outgoing: function(message) { ... }
3167
+ * }
3168
+ * </pre>
3169
+ * Both properties are optional, but if they are present they will be called
3170
+ * respectively for each incoming message and for each outgoing message.
3171
+ * @param name the name of the extension
3172
+ * @param extension the extension to register
3173
+ * @return true if the extension was registered, false otherwise
3174
+ * @see #unregisterExtension(name)
3175
+ */
3176
+ this.registerExtension = function(name, extension) {
3177
+ if (arguments.length < 2) {
3178
+ throw 'Illegal arguments number: required 2, got ' + arguments.length;
3179
+ }
3180
+ if (!_isString(name)) {
3181
+ throw 'Illegal argument type: extension name must be a string';
3182
+ }
3183
+
3184
+ var existing = false;
3185
+ for (var i = 0; i < _extensions.length; ++i) {
3186
+ var existingExtension = _extensions[i];
3187
+ if (existingExtension.name === name) {
3188
+ existing = true;
3189
+ break;
3190
+ }
3191
+ }
3192
+ if (!existing) {
3193
+ _extensions.push({
3194
+ name: name,
3195
+ extension: extension
3196
+ });
3197
+ this._debug('Registered extension', name);
3198
+
3199
+ // Callback for extensions
3200
+ if (_isFunction(extension.registered)) {
3201
+ extension.registered(name, this);
3202
+ }
3203
+
3204
+ return true;
3205
+ } else {
3206
+ this._info('Could not register extension with name', name, 'since another extension with the same name already exists');
3207
+ return false;
3208
+ }
3209
+ };
3210
+
3211
+ /**
3212
+ * Unregister an extension previously registered with
3213
+ * {@link #registerExtension(name, extension)}.
3214
+ * @param name the name of the extension to unregister.
3215
+ * @return true if the extension was unregistered, false otherwise
3216
+ */
3217
+ this.unregisterExtension = function(name) {
3218
+ if (!_isString(name)) {
3219
+ throw 'Illegal argument type: extension name must be a string';
3220
+ }
3221
+
3222
+ var unregistered = false;
3223
+ for (var i = 0; i < _extensions.length; ++i) {
3224
+ var extension = _extensions[i];
3225
+ if (extension.name === name) {
3226
+ _extensions.splice(i, 1);
3227
+ unregistered = true;
3228
+ this._debug('Unregistered extension', name);
3229
+
3230
+ // Callback for extensions
3231
+ var ext = extension.extension;
3232
+ if (_isFunction(ext.unregistered)) {
3233
+ ext.unregistered();
3234
+ }
3235
+
3236
+ break;
3237
+ }
3238
+ }
3239
+ return unregistered;
3240
+ };
3241
+
3242
+ /**
3243
+ * Find the extension registered with the given name.
3244
+ * @param name the name of the extension to find
3245
+ * @return the extension found or null if no extension with the given name has been registered
3246
+ */
3247
+ this.getExtension = function(name) {
3248
+ for (var i = 0; i < _extensions.length; ++i) {
3249
+ var extension = _extensions[i];
3250
+ if (extension.name === name) {
3251
+ return extension.extension;
3252
+ }
3253
+ }
3254
+ return null;
3255
+ };
3256
+
3257
+ /**
3258
+ * Returns the name assigned to this CometD object, or the string 'default'
3259
+ * if no name has been explicitly passed as parameter to the constructor.
3260
+ */
3261
+ this.getName = function() {
3262
+ return _name;
3263
+ };
3264
+
3265
+ /**
3266
+ * Returns the clientId assigned by the Bayeux server during handshake.
3267
+ */
3268
+ this.getClientId = function() {
3269
+ return _clientId;
3270
+ };
3271
+
3272
+ /**
3273
+ * Returns the URL of the Bayeux server.
3274
+ */
3275
+ this.getURL = function() {
3276
+ if (_transport) {
3277
+ var url = _transport.getURL();
3278
+ if (url) {
3279
+ return url;
3280
+ }
3281
+ url = _config.urls[_transport.getType()];
3282
+ if (url) {
3283
+ return url;
3284
+ }
3285
+ }
3286
+ return _config.url;
3287
+ };
3288
+
3289
+ this.getTransport = function() {
3290
+ return _transport;
3291
+ };
3292
+
3293
+ this.getConfiguration = function() {
3294
+ return this._mixin(true, {}, _config);
3295
+ };
3296
+
3297
+ this.getAdvice = function() {
3298
+ return this._mixin(true, {}, _advice);
3299
+ };
3300
+
3301
+ this.setTimeout = function(funktion, delay) {
3302
+ return _scheduler.setTimeout(function() {
3303
+ try {
3304
+ _cometd._debug('Invoking timed function', funktion);
3305
+ funktion();
3306
+ } catch (x) {
3307
+ _cometd._debug('Exception invoking timed function', funktion, x);
3308
+ }
3309
+ }, delay);
3310
+ };
3311
+
3312
+ this.clearTimeout = function(id) {
3313
+ _scheduler.clearTimeout(id);
3314
+ };
3315
+
3316
+ // Initialize transports.
3317
+ if (window.WebSocket) {
3318
+ this.registerTransport('websocket', new WebSocketTransport());
3319
+ }
3320
+ this.registerTransport('long-polling', new LongPollingTransport());
3321
+ this.registerTransport('callback-polling', new CallbackPollingTransport());
3322
+ };
3323
+
3324
+ var _z85EncodeTable = [
3325
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
3326
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
3327
+ 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
3328
+ 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
3329
+ 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
3330
+ 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
3331
+ 'Y', 'Z', '.', '-', ':', '+', '=', '^', '!', '/',
3332
+ '*', '?', '&', '<', '>', '(', ')', '[', ']', '{',
3333
+ '}', '@', '%', '$', '#'
3334
+ ];
3335
+ var _z85DecodeTable = [
3336
+ 0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00,
3337
+ 0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45,
3338
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
3339
+ 0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47,
3340
+ 0x51, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
3341
+ 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
3342
+ 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
3343
+ 0x3B, 0x3C, 0x3D, 0x4D, 0x00, 0x4E, 0x43, 0x00,
3344
+ 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
3345
+ 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
3346
+ 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
3347
+ 0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00
3348
+ ];
3349
+ var Z85 = {
3350
+ encode: function(bytes) {
3351
+ var buffer = null;
3352
+ if (bytes instanceof ArrayBuffer) {
3353
+ buffer = bytes;
3354
+ } else if (bytes.buffer instanceof ArrayBuffer) {
3355
+ buffer = bytes.buffer;
3356
+ } else if (Array.isArray(bytes)) {
3357
+ buffer = new Uint8Array(bytes).buffer;
3358
+ }
3359
+ if (buffer == null) {
3360
+ throw 'Cannot Z85 encode ' + bytes;
3361
+ }
3362
+
3363
+ var length = buffer.byteLength;
3364
+ var remainder = length % 4;
3365
+ var padding = 4 - (remainder === 0 ? 4 : remainder);
3366
+ var view = new DataView(buffer);
3367
+ var result = '';
3368
+ var value = 0;
3369
+ for (var i = 0; i < length + padding; ++i) {
3370
+ var isPadding = i >= length;
3371
+ value = value * 256 + (isPadding ? 0 : view.getUint8(i));
3372
+ if ((i + 1) % 4 === 0) {
3373
+ var divisor = 85 * 85 * 85 * 85;
3374
+ for (var j = 5; j > 0; --j) {
3375
+ if (!isPadding || j > padding) {
3376
+ var code = Math.floor(value / divisor) % 85;
3377
+ result += _z85EncodeTable[code];
3378
+ }
3379
+ divisor /= 85;
3380
+ }
3381
+ value = 0;
3382
+ }
3383
+ }
3384
+
3385
+ return result;
3386
+ },
3387
+ decode: function(string) {
3388
+ var remainder = string.length % 5;
3389
+ var padding = 5 - (remainder === 0 ? 5 : remainder);
3390
+ for (var p = 0; p < padding; ++p) {
3391
+ string += _z85EncodeTable[_z85EncodeTable.length - 1];
3392
+ }
3393
+ var length = string.length;
3394
+
3395
+ var buffer = new ArrayBuffer((length * 4 / 5) - padding);
3396
+ var view = new DataView(buffer);
3397
+ var value = 0;
3398
+ var charIdx = 0;
3399
+ var byteIdx = 0;
3400
+ for (var i = 0; i < length; ++i) {
3401
+ var code = string.charCodeAt(charIdx++) - 32;
3402
+ value = value * 85 + _z85DecodeTable[code];
3403
+ if (charIdx % 5 === 0) {
3404
+ var divisor = 256 * 256 * 256;
3405
+ while (divisor >= 1) {
3406
+ if (byteIdx < view.byteLength) {
3407
+ view.setUint8(byteIdx++, Math.floor(value / divisor) % 256);
3408
+ }
3409
+ divisor /= 256;
3410
+ }
3411
+ value = 0;
3412
+ }
3413
+ }
3414
+
3415
+ return buffer;
3416
+ }
3417
+ };
3418
+
3419
+ return {
3420
+ CometD: CometD,
3421
+ Transport: Transport,
3422
+ RequestTransport: RequestTransport,
3423
+ LongPollingTransport: LongPollingTransport,
3424
+ CallbackPollingTransport: CallbackPollingTransport,
3425
+ WebSocketTransport: WebSocketTransport,
3426
+ Utils: Utils,
3427
+ Z85: Z85
3428
+ };
3429
+ }));
3430
+ });
3431
+
3432
+ var cometd$1 = /*#__PURE__*/Object.freeze(/*#__PURE__*/_mergeNamespaces({
3433
+ __proto__: null,
3434
+ 'default': cometd
3435
+ }, [cometd]));
3436
+
3437
+ export { cometd as a, cometd$1 as c };
3438
+ //# sourceMappingURL=cometd-eeabdcd4.js.map