epicenter-libs 3.33.0 → 3.34.1

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 (76) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/browser/epicenter.js +105 -2
  3. package/dist/browser/epicenter.js.map +1 -1
  4. package/dist/cjs/epicenter.js +105 -2
  5. package/dist/cjs/epicenter.js.map +1 -1
  6. package/dist/epicenter.js +105 -2
  7. package/dist/epicenter.js.map +1 -1
  8. package/dist/epicenter.min.js +1 -1
  9. package/dist/epicenter.min.js.map +1 -1
  10. package/dist/module/epicenter.js +105 -2
  11. package/dist/module/epicenter.js.map +1 -1
  12. package/dist/types/adapters/chat.d.ts +63 -0
  13. package/package.json +1 -1
  14. package/src/adapters/chat.ts +95 -0
  15. package/src/utils/identification.ts +4 -0
  16. package/dist/test/_virtual/runtime.js +0 -3
  17. package/dist/test/_virtual/runtime2.js +0 -3
  18. package/dist/test/adapters/account.js +0 -169
  19. package/dist/test/adapters/admin.js +0 -53
  20. package/dist/test/adapters/asset.js +0 -425
  21. package/dist/test/adapters/authentication.js +0 -349
  22. package/dist/test/adapters/channel.js +0 -201
  23. package/dist/test/adapters/chat.js +0 -199
  24. package/dist/test/adapters/cometd.js +0 -514
  25. package/dist/test/adapters/consensus.js +0 -484
  26. package/dist/test/adapters/daily.js +0 -244
  27. package/dist/test/adapters/email.js +0 -179
  28. package/dist/test/adapters/episode.js +0 -175
  29. package/dist/test/adapters/group.js +0 -655
  30. package/dist/test/adapters/leaderboard.js +0 -162
  31. package/dist/test/adapters/matchmaker.js +0 -159
  32. package/dist/test/adapters/presence.js +0 -85
  33. package/dist/test/adapters/project.js +0 -94
  34. package/dist/test/adapters/recaptcha.js +0 -24
  35. package/dist/test/adapters/run.js +0 -1008
  36. package/dist/test/adapters/somebody.js +0 -154
  37. package/dist/test/adapters/task.js +0 -192
  38. package/dist/test/adapters/time.js +0 -28
  39. package/dist/test/adapters/user.js +0 -105
  40. package/dist/test/adapters/vault.js +0 -403
  41. package/dist/test/adapters/video.js +0 -249
  42. package/dist/test/adapters/vonage.js +0 -130
  43. package/dist/test/adapters/wallet.js +0 -130
  44. package/dist/test/adapters/world.js +0 -507
  45. package/dist/test/apis/video.js +0 -294
  46. package/dist/test/apis/vonage.js +0 -121
  47. package/dist/test/epicenter.js +0 -121
  48. package/dist/test/node_modules/@babel/runtime/helpers/esm/defineProperty.js +0 -12
  49. package/dist/test/node_modules/@babel/runtime/helpers/esm/toPrimitive.js +0 -14
  50. package/dist/test/node_modules/@babel/runtime/helpers/esm/toPropertyKey.js +0 -9
  51. package/dist/test/node_modules/@babel/runtime/helpers/esm/typeof.js +0 -11
  52. package/dist/test/node_modules/cometd/AckExtension.js +0 -91
  53. package/dist/test/node_modules/cometd/CallbackPollingTransport.js +0 -148
  54. package/dist/test/node_modules/cometd/Client.js +0 -2160
  55. package/dist/test/node_modules/cometd/Extension.js +0 -47
  56. package/dist/test/node_modules/cometd/LongPollingTransport.js +0 -135
  57. package/dist/test/node_modules/cometd/ReloadExtension.js +0 -214
  58. package/dist/test/node_modules/cometd/RequestTransport.js +0 -252
  59. package/dist/test/node_modules/cometd/Transport.js +0 -167
  60. package/dist/test/node_modules/cometd/TransportRegistry.js +0 -107
  61. package/dist/test/node_modules/cometd/WebSocketTransport.js +0 -384
  62. package/dist/test/node_modules/cometd/cometd.js +0 -9
  63. package/dist/test/node_modules/regenerator-runtime/runtime.js +0 -773
  64. package/dist/test/utilities.js +0 -17
  65. package/dist/test/utils/config.js +0 -189
  66. package/dist/test/utils/constants.js +0 -293
  67. package/dist/test/utils/cookies.js +0 -75
  68. package/dist/test/utils/error-manager.js +0 -57
  69. package/dist/test/utils/error.js +0 -12
  70. package/dist/test/utils/fault.js +0 -34
  71. package/dist/test/utils/filter-parser.js +0 -68
  72. package/dist/test/utils/helpers.js +0 -9
  73. package/dist/test/utils/identification.js +0 -161
  74. package/dist/test/utils/result.js +0 -20
  75. package/dist/test/utils/router.js +0 -551
  76. package/dist/test/utils/store.js +0 -79
@@ -1,2160 +0,0 @@
1
- import { TransportRegistry } from './TransportRegistry.js';
2
- import { CallbackPollingTransport } from './CallbackPollingTransport.js';
3
- import { LongPollingTransport } from './LongPollingTransport.js';
4
- import { WebSocketTransport } from './WebSocketTransport.js';
5
-
6
- var _CometD;
7
- function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
8
- function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
9
- function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
10
- function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
11
- function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
12
- function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
13
-
14
- /**
15
- * Browsers may throttle the Window scheduler,
16
- * so we may replace it with a Worker scheduler.
17
- */
18
- function Scheduler() {
19
- let _ids = 0;
20
- const _tasks = {};
21
- this.register = funktion => {
22
- const id = ++_ids;
23
- _tasks[id] = funktion;
24
- return id;
25
- };
26
- this.unregister = id => {
27
- const funktion = _tasks[id];
28
- delete _tasks[id];
29
- return funktion;
30
- };
31
- this.setTimeout = (funktion, delay) => window.setTimeout(funktion, delay);
32
- this.clearTimeout = id => {
33
- window.clearTimeout(id);
34
- };
35
- }
36
-
37
- /**
38
- * The scheduler code that will run in the Worker.
39
- * Workers have a built-in `self` variable similar to `window`.
40
- */
41
- function WorkerScheduler() {
42
- const _tasks = {};
43
- self.onmessage = e => {
44
- const cmd = e.data;
45
- const id = _tasks[cmd.id];
46
- switch (cmd.type) {
47
- case "setTimeout":
48
- _tasks[cmd.id] = self.setTimeout(() => {
49
- delete _tasks[cmd.id];
50
- self.postMessage({
51
- id: cmd.id
52
- });
53
- }, cmd.delay);
54
- break;
55
- case "clearTimeout":
56
- delete _tasks[cmd.id];
57
- if (id) {
58
- self.clearTimeout(id);
59
- }
60
- break;
61
- default:
62
- throw new Error("Unknown command " + cmd.type);
63
- }
64
- };
65
- }
66
-
67
- /**
68
- * The constructor for a CometD object, identified by an optional name.
69
- * The default name is the string "default".
70
- * @param name the optional name of this cometd object
71
- */
72
- var _scheduler = /*#__PURE__*/new WeakMap();
73
- var _name = /*#__PURE__*/new WeakMap();
74
- var _crossDomain = /*#__PURE__*/new WeakMap();
75
- var _transports = /*#__PURE__*/new WeakMap();
76
- var _transport = /*#__PURE__*/new WeakMap();
77
- var _status = /*#__PURE__*/new WeakMap();
78
- var _messageId = /*#__PURE__*/new WeakMap();
79
- var _clientId = /*#__PURE__*/new WeakMap();
80
- var _batch = /*#__PURE__*/new WeakMap();
81
- var _messageQueue = /*#__PURE__*/new WeakMap();
82
- var _internalBatch = /*#__PURE__*/new WeakMap();
83
- var _listenerId = /*#__PURE__*/new WeakMap();
84
- var _listeners = /*#__PURE__*/new WeakMap();
85
- var _transportListeners = /*#__PURE__*/new WeakMap();
86
- var _backoff = /*#__PURE__*/new WeakMap();
87
- var _scheduledSend = /*#__PURE__*/new WeakMap();
88
- var _extensions = /*#__PURE__*/new WeakMap();
89
- var _advice = /*#__PURE__*/new WeakMap();
90
- var _handshakeProps = /*#__PURE__*/new WeakMap();
91
- var _handshakeCallback = /*#__PURE__*/new WeakMap();
92
- var _callbacks = /*#__PURE__*/new WeakMap();
93
- var _remoteCalls = /*#__PURE__*/new WeakMap();
94
- var _reestablish = /*#__PURE__*/new WeakMap();
95
- var _connected = /*#__PURE__*/new WeakMap();
96
- var _unconnectTime = /*#__PURE__*/new WeakMap();
97
- var _handshakeMessages = /*#__PURE__*/new WeakMap();
98
- var _metaConnect = /*#__PURE__*/new WeakMap();
99
- var _config = /*#__PURE__*/new WeakMap();
100
- var _CometD_brand = /*#__PURE__*/new WeakSet();
101
- class CometD {
102
- constructor(_name2) {
103
- _classPrivateMethodInitSpec(this, _CometD_brand);
104
- _classPrivateFieldInitSpec(this, _scheduler, new Scheduler());
105
- _classPrivateFieldInitSpec(this, _name, void 0);
106
- _classPrivateFieldInitSpec(this, _crossDomain, false);
107
- _classPrivateFieldInitSpec(this, _transports, new TransportRegistry());
108
- _classPrivateFieldInitSpec(this, _transport, void 0);
109
- _classPrivateFieldInitSpec(this, _status, "disconnected");
110
- _classPrivateFieldInitSpec(this, _messageId, 0);
111
- _classPrivateFieldInitSpec(this, _clientId, null);
112
- _classPrivateFieldInitSpec(this, _batch, 0);
113
- _classPrivateFieldInitSpec(this, _messageQueue, []);
114
- _classPrivateFieldInitSpec(this, _internalBatch, false);
115
- _classPrivateFieldInitSpec(this, _listenerId, 0);
116
- _classPrivateFieldInitSpec(this, _listeners, {});
117
- _classPrivateFieldInitSpec(this, _transportListeners, {});
118
- _classPrivateFieldInitSpec(this, _backoff, 0);
119
- _classPrivateFieldInitSpec(this, _scheduledSend, null);
120
- _classPrivateFieldInitSpec(this, _extensions, []);
121
- _classPrivateFieldInitSpec(this, _advice, {});
122
- _classPrivateFieldInitSpec(this, _handshakeProps, void 0);
123
- _classPrivateFieldInitSpec(this, _handshakeCallback, void 0);
124
- _classPrivateFieldInitSpec(this, _callbacks, {});
125
- _classPrivateFieldInitSpec(this, _remoteCalls, {});
126
- _classPrivateFieldInitSpec(this, _reestablish, false);
127
- _classPrivateFieldInitSpec(this, _connected, false);
128
- _classPrivateFieldInitSpec(this, _unconnectTime, 0);
129
- _classPrivateFieldInitSpec(this, _handshakeMessages, 0);
130
- _classPrivateFieldInitSpec(this, _metaConnect, null);
131
- _classPrivateFieldInitSpec(this, _config, {
132
- useWorkerScheduler: true,
133
- protocol: null,
134
- stickyReconnect: true,
135
- connectTimeout: 0,
136
- maxConnections: 2,
137
- backoffIncrement: 1000,
138
- maxBackoff: 60000,
139
- logLevel: "info",
140
- maxNetworkDelay: 10000,
141
- requestHeaders: {},
142
- appendMessageTypeToURL: true,
143
- autoBatch: false,
144
- urls: {},
145
- maxURILength: 2000,
146
- maxSendBayeuxMessageSize: 8192,
147
- advice: {
148
- timeout: 60000,
149
- interval: 0,
150
- reconnect: undefined,
151
- maxInterval: 0
152
- }
153
- });
154
- _classPrivateFieldSet(_name, this, _name2 || "default");
155
- // Initialize transports.
156
- if (window.WebSocket) {
157
- this.registerTransport("websocket", new WebSocketTransport());
158
- }
159
- this.registerTransport("long-polling", new LongPollingTransport());
160
- this.registerTransport("callback-polling", new CallbackPollingTransport());
161
- }
162
- /**
163
- * Mixes in the given objects into the target object by copying the properties.
164
- * @param deep if the copy must be deep
165
- * @param target the target object
166
- * @param objects the objects whose properties are copied into the target
167
- */
168
- _mixin(deep, target, objects) {
169
- const result = target || {};
170
-
171
- // Skip first 2 parameters (deep and target), and loop over the others.
172
- for (let i = 2; i < arguments.length; ++i) {
173
- const object = arguments[i];
174
- if (object === undefined || object === null) {
175
- continue;
176
- }
177
- for (let propName in object) {
178
- if (object.hasOwnProperty(propName)) {
179
- const prop = _fieldValue.call(CometD, object, propName);
180
- const targ = _fieldValue.call(CometD, result, propName);
181
-
182
- // Avoid infinite loops.
183
- if (prop === target) {
184
- continue;
185
- }
186
- // Do not mixin undefined values.
187
- if (prop === undefined) {
188
- continue;
189
- }
190
- if (deep && typeof prop === "object" && prop !== null) {
191
- if (prop instanceof Array) {
192
- result[propName] = this._mixin(deep, targ instanceof Array ? targ : [], prop);
193
- } else {
194
- const source = typeof targ === "object" && !(targ instanceof Array) ? targ : {};
195
- result[propName] = this._mixin(deep, source, prop);
196
- }
197
- } else {
198
- result[propName] = prop;
199
- }
200
- }
201
- }
202
- }
203
- return result;
204
- }
205
- _warn() {
206
- _assertClassBrand(_CometD_brand, this, _log).call(this, "warn", arguments);
207
- }
208
- _info() {
209
- if (_classPrivateFieldGet(_config, this).logLevel !== "warn") {
210
- _assertClassBrand(_CometD_brand, this, _log).call(this, "info", arguments);
211
- }
212
- }
213
- _debug() {
214
- if (_classPrivateFieldGet(_config, this).logLevel === "debug") {
215
- _assertClassBrand(_CometD_brand, this, _log).call(this, "debug", arguments);
216
- }
217
- }
218
- /**
219
- * Sends a complete bayeux message.
220
- * This method is exposed as a public so that extensions may use it
221
- * to send bayeux message directly, for example in case of re-sending
222
- * messages that have already been sent but that for some reason must
223
- * be resent.
224
- */
225
- send(message) {
226
- _assertClassBrand(_CometD_brand, this, _queueSend).call(this, message);
227
- }
228
- _getCallback(messageId) {
229
- return _classPrivateFieldGet(_callbacks, this)[messageId];
230
- }
231
- _putCallback(messageId, callback) {
232
- const result = this._getCallback(messageId);
233
- if (_isFunction.call(CometD, callback)) {
234
- _classPrivateFieldGet(_callbacks, this)[messageId] = callback;
235
- }
236
- return result;
237
- }
238
- onTransportFailure(message, failureInfo, failureHandler) {
239
- this._debug("Transport failure", failureInfo, "for", message);
240
- const transports = this.getTransportRegistry();
241
- const url = this.getURL();
242
- const crossDomain = _assertClassBrand(_CometD_brand, this, _isCrossDomain).call(this, _splitURL.call(CometD, url)[2]);
243
- const version = "1.0";
244
- const transportTypes = transports.findTransportTypes(version, crossDomain, url);
245
- if (failureInfo.action === "none") {
246
- if (message.channel === "/meta/handshake") {
247
- if (!failureInfo.transport) {
248
- const failure = "Could not negotiate transport, client=[" + transportTypes + "], server=[" + message.supportedConnectionTypes + "]";
249
- this._warn(failure);
250
- const transport = this.getTransport();
251
- if (transport) {
252
- const transportType = transport.type;
253
- _assertClassBrand(_CometD_brand, this, _notifyTransportException).call(this, transportType, null, {
254
- reason: failure,
255
- connectionType: transportType,
256
- transport: transport
257
- });
258
- }
259
- }
260
- }
261
- } else {
262
- failureInfo.delay = this.getBackoffPeriod();
263
- // Different logic depending on whether we are handshaking or connecting.
264
- if (message.channel === "/meta/handshake") {
265
- if (!failureInfo.transport) {
266
- // The transport is invalid, try to negotiate again.
267
- const oldTransportType = _classPrivateFieldGet(_transport, this) ? _classPrivateFieldGet(_transport, this).type : null;
268
- const newTransport = transports.negotiateTransport(transportTypes, version, crossDomain, url);
269
- if (!newTransport) {
270
- this._warn("Could not negotiate transport, client=[" + transportTypes + "]");
271
- _assertClassBrand(_CometD_brand, this, _notifyTransportException).call(this, oldTransportType, null, message.failure);
272
- failureInfo.action = "none";
273
- } else {
274
- const newTransportType = newTransport.type;
275
- this._debug("Transport", oldTransportType, "->", newTransportType);
276
- _assertClassBrand(_CometD_brand, this, _notifyTransportException).call(this, oldTransportType, newTransportType, message.failure);
277
- failureInfo.action = "handshake";
278
- failureInfo.transport = newTransport;
279
- }
280
- }
281
- if (failureInfo.action !== "none") {
282
- this.increaseBackoffPeriod();
283
- }
284
- } else {
285
- const now = new Date().getTime();
286
- if (_classPrivateFieldGet(_unconnectTime, this) === 0) {
287
- _classPrivateFieldSet(_unconnectTime, this, now);
288
- }
289
- if (failureInfo.action === "retry") {
290
- failureInfo.delay = this.increaseBackoffPeriod();
291
- // Check whether we may switch to handshaking.
292
- const maxInterval = _classPrivateFieldGet(_advice, this).maxInterval;
293
- if (maxInterval > 0) {
294
- const expiration = _classPrivateFieldGet(_advice, this).timeout + _classPrivateFieldGet(_advice, this).interval + maxInterval;
295
- const unconnected = now - _classPrivateFieldGet(_unconnectTime, this);
296
- if (unconnected + _classPrivateFieldGet(_backoff, this) > expiration) {
297
- failureInfo.action = "handshake";
298
- }
299
- }
300
- }
301
- if (failureInfo.action === "handshake") {
302
- failureInfo.delay = 0;
303
- transports.reset(false);
304
- this.resetBackoffPeriod();
305
- }
306
- }
307
- }
308
- failureHandler.call(this, failureInfo);
309
- }
310
- /**
311
- * Receives a message.
312
- * This method is exposed as a public so that extensions may inject
313
- * messages simulating that they had been received.
314
- */
315
- receive(message) {
316
- _assertClassBrand(_CometD_brand, this, _receive).call(this, message);
317
- }
318
- //
319
- // PUBLIC API
320
- //
321
-
322
- /**
323
- * Registers the given transport under the given transport type.
324
- * The optional index parameter specifies the "priority" at which the
325
- * transport is registered (where 0 is the max priority).
326
- * If a transport with the same type is already registered, this function
327
- * does nothing and returns false.
328
- * @param type the transport type
329
- * @param transport the transport object
330
- * @param index the index at which this transport is to be registered
331
- * @return true if the transport has been registered, false otherwise
332
- */
333
- registerTransport(type, transport, index) {
334
- const result = _classPrivateFieldGet(_transports, this).add(type, transport, index);
335
- if (result) {
336
- this._debug("Registered transport", type);
337
- if (_isFunction.call(CometD, transport.registered)) {
338
- transport.registered(type, this);
339
- }
340
- }
341
- return result;
342
- }
343
- /**
344
- * Unregisters the transport with the given transport type.
345
- * @param type the transport type to unregister
346
- * @return the transport that has been unregistered,
347
- * or null if no transport was previously registered under the given transport type
348
- */
349
- unregisterTransport(type) {
350
- const transport = _classPrivateFieldGet(_transports, this).remove(type);
351
- if (transport !== null) {
352
- this._debug("Unregistered transport", type);
353
- if (_isFunction.call(CometD, transport.unregistered)) {
354
- transport.unregistered();
355
- }
356
- }
357
- return transport;
358
- }
359
- unregisterTransports() {
360
- _classPrivateFieldGet(_transports, this).clear();
361
- }
362
- /**
363
- * @return an array of all registered transport types
364
- */
365
- getTransportTypes() {
366
- return _classPrivateFieldGet(_transports, this).getTransportTypes();
367
- }
368
- findTransport(name) {
369
- return _classPrivateFieldGet(_transports, this).find(name);
370
- }
371
-
372
- /**
373
- * @returns the TransportRegistry object
374
- */
375
- getTransportRegistry() {
376
- return _classPrivateFieldGet(_transports, this);
377
- }
378
-
379
- /**
380
- * Configures the initial Bayeux communication with the Bayeux server.
381
- * Configuration is passed via an object that must contain a mandatory field <code>url</code>
382
- * of type string containing the URL of the Bayeux server.
383
- * @param configuration the configuration object
384
- */
385
- configure(configuration) {
386
- _assertClassBrand(_CometD_brand, this, _configure).call(this, configuration);
387
- }
388
- /**
389
- * Configures and establishes the Bayeux communication with the Bayeux server
390
- * via a handshake and a subsequent connect.
391
- * @param configuration the configuration object
392
- * @param handshakeProps an object to be merged with the handshake message
393
- */
394
- init(configuration, handshakeProps) {
395
- this.configure(configuration);
396
- this.handshake(handshakeProps);
397
- }
398
- /**
399
- * Establishes the Bayeux communication with the Bayeux server
400
- * via a handshake and a subsequent connect.
401
- * @param handshakeProps an object to be merged with the handshake message
402
- * @param handshakeCallback a function to be invoked when the handshake is acknowledged
403
- */
404
- handshake(handshakeProps, handshakeCallback) {
405
- const status = this.getStatus();
406
- if (status !== "disconnected") {
407
- throw new Error("Illegal state: " + status);
408
- }
409
- _assertClassBrand(_CometD_brand, this, _handshake).call(this, handshakeProps, handshakeCallback);
410
- }
411
- /**
412
- * Disconnects from the Bayeux server.
413
- * @param disconnectProps an object to be merged with the disconnect message
414
- * @param disconnectCallback a function to be invoked when the disconnect is acknowledged
415
- */
416
- disconnect(disconnectProps, disconnectCallback) {
417
- if (this.isDisconnected()) {
418
- return;
419
- }
420
- if (_isFunction.call(CometD, disconnectProps)) {
421
- disconnectCallback = disconnectProps;
422
- disconnectProps = undefined;
423
- }
424
- const bayeuxMessage = {
425
- id: _assertClassBrand(_CometD_brand, this, _nextMessageId).call(this),
426
- channel: "/meta/disconnect"
427
- };
428
- // Do not allow the user to override important fields.
429
- const message = this._mixin(false, {}, disconnectProps, bayeuxMessage);
430
-
431
- // Save the callback.
432
- this._putCallback(message.id, disconnectCallback);
433
- _assertClassBrand(_CometD_brand, this, _setStatus).call(this, "disconnecting");
434
- _assertClassBrand(_CometD_brand, this, _send).call(this, [message], false, "disconnect");
435
- }
436
- /**
437
- * Marks the start of a batch of application messages to be sent to the server
438
- * in a single request, obtaining a single response containing (possibly) many
439
- * application reply messages.
440
- * Messages are held in a queue and not sent until {@link #endBatch()} is called.
441
- * If startBatch() is called multiple times, then an equal number of endBatch()
442
- * calls must be made to close and send the batch of messages.
443
- */
444
- startBatch() {
445
- _assertClassBrand(_CometD_brand, this, _startBatch).call(this);
446
- }
447
- /**
448
- * Marks the end of a batch of application messages to be sent to the server
449
- * in a single request.
450
- */
451
- endBatch() {
452
- _assertClassBrand(_CometD_brand, this, _endBatch).call(this);
453
- }
454
- /**
455
- * Executes the given callback in the given scope, surrounded by a {@link #startBatch()}
456
- * and {@link #endBatch()} calls.
457
- * @param scope the scope of the callback, may be omitted
458
- * @param callback the callback to be executed within {@link #startBatch()} and {@link #endBatch()} calls
459
- */
460
- batch(scope, callback) {
461
- const delegate = _assertClassBrand(_CometD_brand, this, _resolveScopedCallback).call(this, scope, callback);
462
- this.startBatch();
463
- try {
464
- delegate.method.call(delegate.scope);
465
- this.endBatch();
466
- } catch (x) {
467
- this._info("Exception during execution of batch", x);
468
- this.endBatch();
469
- throw x;
470
- }
471
- }
472
- /**
473
- * Adds a transport listener for the specified transport event,
474
- * executing the given callback when the event happens.
475
- *
476
- * The currently supported event is "timeout".
477
- *
478
- * The callback function takes an array of messages for which
479
- * the event happened.
480
- *
481
- * For the "timeout" event, the callback function may return a
482
- * positive value that extends the wait for message replies by
483
- * the returned amount, in milliseconds.
484
- *
485
- * @param {String} event the type of transport event
486
- * @param {Function} callback the function associate to the given transport event
487
- */
488
- addTransportListener(event, callback) {
489
- if (event !== "timeout") {
490
- throw new Error("Unsupported event " + event);
491
- }
492
- let callbacks = _classPrivateFieldGet(_transportListeners, this)[event];
493
- if (!callbacks) {
494
- _classPrivateFieldGet(_transportListeners, this)[event] = callbacks = [];
495
- }
496
- callbacks.push(callback);
497
- }
498
- /**
499
- * Removes the transport listener for the specified transport event.
500
- * @param {String} event the type of transport event
501
- * @param {Function} callback the function disassociate from the given transport event
502
- * @return {boolean} whether the disassociation was successful
503
- */
504
- removeTransportListener(event, callback) {
505
- const callbacks = _classPrivateFieldGet(_transportListeners, this)[event];
506
- if (callbacks) {
507
- const index = callbacks.indexOf(callback);
508
- if (index >= 0) {
509
- callbacks.splice(index, 1);
510
- return true;
511
- }
512
- }
513
- return false;
514
- }
515
- _getTransportListeners(event) {
516
- return _classPrivateFieldGet(_transportListeners, this)[event];
517
- }
518
-
519
- /**
520
- * Adds a listener for bayeux messages, performing the given callback in the given scope
521
- * when a message for the given channel arrives.
522
- * @param channel the channel the listener is interested to
523
- * @param scope the scope of the callback, may be omitted
524
- * @param callback the callback to call when a message is sent to the channel
525
- * @returns the subscription handle to be passed to {@link #removeListener(object)}
526
- */
527
- addListener(channel, scope, callback) {
528
- if (arguments.length < 2) {
529
- throw new Error("Illegal arguments number: required 2, got " + arguments.length);
530
- }
531
- if (!_isString.call(CometD, channel)) {
532
- throw new Error("Illegal argument type: channel must be a string");
533
- }
534
- return _assertClassBrand(_CometD_brand, this, _addListener).call(this, channel, scope, callback, true);
535
- }
536
- /**
537
- * Removes the subscription obtained with a call to {@link #addListener(string, object, function)}.
538
- * @param subscription the subscription to unsubscribe.
539
- */
540
- removeListener(subscription) {
541
- // Beware of subscription.id == 0, which is falsy => cannot use !subscription.id
542
- if (!subscription || !subscription.channel || !("id" in subscription)) {
543
- throw new Error("Invalid argument: expected subscription, not " + subscription);
544
- }
545
- _assertClassBrand(_CometD_brand, this, _removeListener).call(this, subscription);
546
- }
547
- /**
548
- * Removes all listeners registered with {@link #addListener(channel, scope, callback)} or
549
- * {@link #subscribe(channel, scope, callback)}.
550
- */
551
- clearListeners() {
552
- _classPrivateFieldSet(_listeners, this, {});
553
- }
554
- /**
555
- * Subscribes to the given channel, performing the given callback in the given scope
556
- * when a message for the channel arrives.
557
- * @param channel the channel to subscribe to
558
- * @param scope the scope of the callback, may be omitted
559
- * @param callback the callback to call when a message is sent to the channel
560
- * @param subscribeProps an object to be merged with the subscribe message
561
- * @param subscribeCallback a function to be invoked when the subscription is acknowledged
562
- * @return the subscription handle to be passed to {@link #unsubscribe(object)}
563
- */
564
- subscribe(channel, scope, callback, subscribeProps, subscribeCallback) {
565
- if (arguments.length < 2) {
566
- throw new Error("Illegal arguments number: required 2, got " + arguments.length);
567
- }
568
- if (!_isValidChannel.call(CometD, channel)) {
569
- throw new Error("Illegal argument: invalid channel " + channel);
570
- }
571
- if (this.isDisconnected()) {
572
- throw new Error("Illegal state: disconnected");
573
- }
574
-
575
- // Normalize arguments
576
- if (_isFunction.call(CometD, scope)) {
577
- subscribeCallback = subscribeProps;
578
- subscribeProps = callback;
579
- callback = scope;
580
- scope = undefined;
581
- }
582
- if (_isFunction.call(CometD, subscribeProps)) {
583
- subscribeCallback = subscribeProps;
584
- subscribeProps = undefined;
585
- }
586
-
587
- // Only send the message to the server if this client has not yet subscribed to the channel
588
- const send = !_assertClassBrand(_CometD_brand, this, _hasSubscriptions).call(this, channel);
589
- const subscription = _assertClassBrand(_CometD_brand, this, _addListener).call(this, channel, scope, callback, false);
590
- if (send) {
591
- // Send the subscription message after the subscription registration to avoid
592
- // races where the server would send a message to the subscribers, but here
593
- // on the client the subscription has not been added yet to the data structures
594
- const bayeuxMessage = {
595
- id: _assertClassBrand(_CometD_brand, this, _nextMessageId).call(this),
596
- channel: "/meta/subscribe",
597
- subscription: channel
598
- };
599
- // Do not allow the user to override important fields.
600
- const message = this._mixin(false, {}, subscribeProps, bayeuxMessage);
601
-
602
- // Save the callback.
603
- this._putCallback(message.id, subscribeCallback);
604
- _assertClassBrand(_CometD_brand, this, _queueSend).call(this, message);
605
- } else {
606
- if (_isFunction.call(CometD, subscribeCallback)) {
607
- // Keep the semantic of calling callbacks asynchronously.
608
- this.setTimeout(() => {
609
- _assertClassBrand(_CometD_brand, this, _notifyCallback).call(this, subscribeCallback, {
610
- id: _assertClassBrand(_CometD_brand, this, _nextMessageId).call(this),
611
- successful: true,
612
- channel: "/meta/subscribe",
613
- subscription: channel
614
- });
615
- }, 0);
616
- }
617
- }
618
- return subscription;
619
- }
620
- /**
621
- * Unsubscribes the subscription obtained with a call to {@link #subscribe(string, object, function)}.
622
- * @param subscription the subscription to unsubscribe.
623
- * @param unsubscribeProps an object to be merged with the unsubscribe message
624
- * @param unsubscribeCallback a function to be invoked when the unsubscription is acknowledged
625
- */
626
- unsubscribe(subscription, unsubscribeProps, unsubscribeCallback) {
627
- if (arguments.length < 1) {
628
- throw new Error("Illegal arguments number: required 1, got " + arguments.length);
629
- }
630
- if (this.isDisconnected()) {
631
- throw new Error("Illegal state: disconnected");
632
- }
633
- if (_isFunction.call(CometD, unsubscribeProps)) {
634
- unsubscribeCallback = unsubscribeProps;
635
- unsubscribeProps = undefined;
636
- }
637
-
638
- // Remove the local listener before sending the message
639
- // This ensures that if the server fails, this client does not get notifications
640
- this.removeListener(subscription);
641
- const channel = subscription.channel;
642
- // Only send the message to the server if this client unsubscribes the last subscription
643
- if (!_assertClassBrand(_CometD_brand, this, _hasSubscriptions).call(this, channel)) {
644
- const bayeuxMessage = {
645
- id: _assertClassBrand(_CometD_brand, this, _nextMessageId).call(this),
646
- channel: "/meta/unsubscribe",
647
- subscription: channel
648
- };
649
- // Do not allow the user to override important fields.
650
- const message = this._mixin(false, {}, unsubscribeProps, bayeuxMessage);
651
-
652
- // Save the callback.
653
- this._putCallback(message.id, unsubscribeCallback);
654
- _assertClassBrand(_CometD_brand, this, _queueSend).call(this, message);
655
- } else {
656
- if (_isFunction.call(CometD, unsubscribeCallback)) {
657
- // Keep the semantic of calling callbacks asynchronously.
658
- this.setTimeout(() => {
659
- _assertClassBrand(_CometD_brand, this, _notifyCallback).call(this, unsubscribeCallback, {
660
- id: _assertClassBrand(_CometD_brand, this, _nextMessageId).call(this),
661
- successful: true,
662
- channel: "/meta/unsubscribe",
663
- subscription: channel
664
- });
665
- }, 0);
666
- }
667
- }
668
- }
669
- resubscribe(subscription, subscribeProps) {
670
- _assertClassBrand(_CometD_brand, this, _removeSubscription).call(this, subscription);
671
- if (subscription) {
672
- return this.subscribe(subscription.channel, subscription.scope, subscription.callback, subscribeProps);
673
- }
674
- return undefined;
675
- }
676
- /**
677
- * Removes all subscriptions added via {@link #subscribe(channel, scope, callback, subscribeProps)},
678
- * but does not remove the listeners added via {@link addListener(channel, scope, callback)}.
679
- */
680
- clearSubscriptions() {
681
- _assertClassBrand(_CometD_brand, this, _clearSubscriptions).call(this);
682
- }
683
- /**
684
- * Publishes a message on the given channel, containing the given content.
685
- * @param channel the channel to publish the message to
686
- * @param content the content of the message
687
- * @param publishProps an object to be merged with the publish message
688
- * @param publishCallback a function to be invoked when the publish is acknowledged by the server
689
- */
690
- publish(channel, content, publishProps, publishCallback) {
691
- if (arguments.length < 1) {
692
- throw new Error("Illegal arguments number: required 1, got " + arguments.length);
693
- }
694
- if (!_isValidChannel.call(CometD, channel)) {
695
- throw new Error("Illegal argument: invalid channel " + channel);
696
- }
697
- if (/^\/meta\//.test(channel)) {
698
- throw new Error("Illegal argument: cannot publish to meta channels");
699
- }
700
- if (this.isDisconnected()) {
701
- throw new Error("Illegal state: disconnected");
702
- }
703
- if (_isFunction.call(CometD, content)) {
704
- publishCallback = content;
705
- content = {};
706
- publishProps = undefined;
707
- } else if (_isFunction.call(CometD, publishProps)) {
708
- publishCallback = publishProps;
709
- publishProps = undefined;
710
- }
711
- const bayeuxMessage = {
712
- id: _assertClassBrand(_CometD_brand, this, _nextMessageId).call(this),
713
- channel: channel,
714
- data: content
715
- };
716
- // Do not allow the user to override important fields.
717
- const message = this._mixin(false, {}, publishProps, bayeuxMessage);
718
-
719
- // Save the callback.
720
- this._putCallback(message.id, publishCallback);
721
- _assertClassBrand(_CometD_brand, this, _queueSend).call(this, message);
722
- }
723
- /**
724
- * Publishes a message with binary data on the given channel.
725
- * The binary data chunk may be an ArrayBuffer, a DataView, a TypedArray
726
- * (such as Uint8Array) or a plain integer array.
727
- * The metadata object may contain additional application data such as
728
- * a file name, a mime type, etc.
729
- * @param channel the channel to publish the message to
730
- * @param data the binary data to publish
731
- * @param last whether the binary data chunk is the last
732
- * @param meta an object containing metadata associated to the binary chunk
733
- * @param publishProps an object to be merged with the publish message
734
- * @param publishCallback a function to be invoked when the publish is acknowledged by the server
735
- */
736
- publishBinary(channel, data, last, meta, publishProps, publishCallback) {
737
- if (_isFunction.call(CometD, data)) {
738
- publishCallback = data;
739
- data = new ArrayBuffer(0);
740
- last = true;
741
- meta = undefined;
742
- publishProps = undefined;
743
- } else if (_isFunction.call(CometD, last)) {
744
- publishCallback = last;
745
- last = true;
746
- meta = undefined;
747
- publishProps = undefined;
748
- } else if (_isFunction.call(CometD, meta)) {
749
- publishCallback = meta;
750
- meta = undefined;
751
- publishProps = undefined;
752
- } else if (_isFunction.call(CometD, publishProps)) {
753
- publishCallback = publishProps;
754
- publishProps = undefined;
755
- }
756
- const content = {
757
- meta: meta,
758
- data: data,
759
- last: last
760
- };
761
- const ext = this._mixin(false, publishProps, {
762
- ext: {
763
- binary: {}
764
- }
765
- });
766
- this.publish(channel, content, ext, publishCallback);
767
- }
768
- /**
769
- * Performs a remote call, a request with a response, to the given target with the given data.
770
- * The response returned by the server is notified the given callback function.
771
- * The invocation may specify a timeout in milliseconds, after which the call is aborted on
772
- * the client-side, causing a failed response to be passed to the given callback function.
773
- * @param target the remote call target
774
- * @param content the remote call content
775
- * @param timeout the remote call timeout, or 0 for no timeout
776
- * @param callProps an object to be merged with the remote call message
777
- * @param callback the function to be invoked with the response
778
- */
779
- remoteCall(target, content, timeout, callProps, callback) {
780
- if (arguments.length < 1) {
781
- throw new Error("Illegal arguments number: required 1, got " + arguments.length);
782
- }
783
- if (!_isString.call(CometD, target)) {
784
- throw new Error("Illegal argument type: target must be a string");
785
- }
786
- if (this.isDisconnected()) {
787
- throw new Error("Illegal state: disconnected");
788
- }
789
- if (_isFunction.call(CometD, content)) {
790
- callback = content;
791
- content = {};
792
- timeout = _classPrivateFieldGet(_config, this).maxNetworkDelay;
793
- callProps = undefined;
794
- } else if (_isFunction.call(CometD, timeout)) {
795
- callback = timeout;
796
- timeout = _classPrivateFieldGet(_config, this).maxNetworkDelay;
797
- callProps = undefined;
798
- } else if (_isFunction.call(CometD, callProps)) {
799
- callback = callProps;
800
- callProps = undefined;
801
- }
802
- if (typeof timeout !== "number") {
803
- throw new Error("Illegal argument type: timeout must be a number");
804
- }
805
- if (!target.match(/^\//)) {
806
- target = "/" + target;
807
- }
808
- const channel = "/service" + target;
809
- if (!_isValidChannel.call(CometD, channel)) {
810
- throw new Error("Illegal argument: invalid target " + target);
811
- }
812
- const bayeuxMessage = {
813
- id: _assertClassBrand(_CometD_brand, this, _nextMessageId).call(this),
814
- channel: channel,
815
- data: content
816
- };
817
- const message = this._mixin(false, {}, callProps, bayeuxMessage);
818
- const context = {
819
- callback: callback
820
- };
821
- if (timeout > 0) {
822
- context.timeout = this.setTimeout(() => {
823
- this._debug("Timing out remote call", message, "after", timeout, "ms");
824
- _assertClassBrand(_CometD_brand, this, _failMessage).call(this, {
825
- id: message.id,
826
- error: "406::timeout",
827
- successful: false,
828
- failure: {
829
- message: message,
830
- reason: "Remote Call Timeout"
831
- }
832
- });
833
- }, timeout);
834
- this._debug("Scheduled remote call timeout", message, "in", timeout, "ms");
835
- }
836
- _classPrivateFieldGet(_remoteCalls, this)[message.id] = context;
837
- _assertClassBrand(_CometD_brand, this, _queueSend).call(this, message);
838
- }
839
- /**
840
- * Performs a remote call with binary data.
841
- * @param target the remote call target
842
- * @param data the remote call binary data
843
- * @param last whether the binary data chunk is the last
844
- * @param meta an object containing meta data associated to the binary chunk
845
- * @param timeout the remote call timeout, or 0 for no timeout
846
- * @param callProps an object to be merged with the remote call message
847
- * @param callback the function to be invoked with the response
848
- */
849
- remoteCallBinary(target, data, last, meta, timeout, callProps, callback) {
850
- if (_isFunction.call(CometD, data)) {
851
- callback = data;
852
- data = new ArrayBuffer(0);
853
- last = true;
854
- meta = undefined;
855
- timeout = _classPrivateFieldGet(_config, this).maxNetworkDelay;
856
- callProps = undefined;
857
- } else if (_isFunction.call(CometD, last)) {
858
- callback = last;
859
- last = true;
860
- meta = undefined;
861
- timeout = _classPrivateFieldGet(_config, this).maxNetworkDelay;
862
- callProps = undefined;
863
- } else if (_isFunction.call(CometD, meta)) {
864
- callback = meta;
865
- meta = undefined;
866
- timeout = _classPrivateFieldGet(_config, this).maxNetworkDelay;
867
- callProps = undefined;
868
- } else if (_isFunction.call(CometD, timeout)) {
869
- callback = timeout;
870
- timeout = _classPrivateFieldGet(_config, this).maxNetworkDelay;
871
- callProps = undefined;
872
- } else if (_isFunction.call(CometD, callProps)) {
873
- callback = callProps;
874
- callProps = undefined;
875
- }
876
- const content = {
877
- meta: meta,
878
- data: data,
879
- last: last
880
- };
881
- const ext = this._mixin(false, callProps, {
882
- ext: {
883
- binary: {}
884
- }
885
- });
886
- this.remoteCall(target, content, timeout, ext, callback);
887
- }
888
- /**
889
- * Returns a string representing the status of the bayeux communication with the Bayeux server.
890
- */
891
- getStatus() {
892
- return _classPrivateFieldGet(_status, this);
893
- }
894
-
895
- /**
896
- * Returns whether this instance has been disconnected.
897
- */
898
- isDisconnected() {
899
- return _assertClassBrand(_CometD_brand, this, _isDisconnected).call(this);
900
- }
901
-
902
- /**
903
- * Sets the backoff period used to increase the backoff time when retrying an unsuccessful or failed message.
904
- * Default value is 1 second, which means if there is a persistent failure the retries will happen
905
- * after 1 second, then after 2 seconds, then after 3 seconds, etc. So for example with 15 seconds of
906
- * elapsed time, there will be 5 retries (at 1, 3, 6, 10 and 15 seconds elapsed).
907
- * @param period the backoff period to set
908
- */
909
- setBackoffIncrement(period) {
910
- _classPrivateFieldGet(_config, this).backoffIncrement = period;
911
- }
912
- /**
913
- * Returns the backoff period used to increase the backoff time when retrying an unsuccessful or failed message.
914
- */
915
- getBackoffIncrement() {
916
- return _classPrivateFieldGet(_config, this).backoffIncrement;
917
- }
918
-
919
- /**
920
- * Returns the backoff period to wait before retrying an unsuccessful or failed message.
921
- */
922
- getBackoffPeriod() {
923
- return _classPrivateFieldGet(_backoff, this);
924
- }
925
-
926
- /**
927
- * Increases the backoff period up to the maximum value configured.
928
- * @returns the backoff period after increment
929
- */
930
- increaseBackoffPeriod() {
931
- return _assertClassBrand(_CometD_brand, this, _increaseBackoff).call(this);
932
- }
933
-
934
- /**
935
- * Resets the backoff period to zero.
936
- */
937
- resetBackoffPeriod() {
938
- _assertClassBrand(_CometD_brand, this, _resetBackoff).call(this);
939
- }
940
- /**
941
- * Sets the log level for console logging.
942
- * Valid values are the strings "error", "warn", "info" and "debug", from
943
- * less verbose to more verbose.
944
- * @param level the log level string
945
- */
946
- setLogLevel(level) {
947
- _classPrivateFieldGet(_config, this).logLevel = level;
948
- }
949
- /**
950
- * Registers an extension whose callbacks are called for every incoming message
951
- * (that comes from the server to this client implementation) and for every
952
- * outgoing message (that originates from this client implementation for the
953
- * server).
954
- * The format of the extension object is the following:
955
- * <pre>
956
- * {
957
- * incoming: message => { ... },
958
- * outgoing: message => { ... }
959
- * }
960
- * </pre>
961
- * Both properties are optional, but if they are present they will be called
962
- * respectively for each incoming message and for each outgoing message.
963
- * @param name the name of the extension
964
- * @param extension the extension to register
965
- * @return true if the extension was registered, false otherwise
966
- */
967
- registerExtension(name, extension) {
968
- if (arguments.length < 2) {
969
- throw new Error("Illegal arguments number: required 2, got " + arguments.length);
970
- }
971
- if (!_isString.call(CometD, name)) {
972
- throw new Error("Illegal argument type: extension name must be a string");
973
- }
974
- let existing = false;
975
- for (let i = 0; i < _classPrivateFieldGet(_extensions, this).length; ++i) {
976
- const existingExtension = _classPrivateFieldGet(_extensions, this)[i];
977
- if (existingExtension.name === name) {
978
- existing = true;
979
- break;
980
- }
981
- }
982
- if (!existing) {
983
- _classPrivateFieldGet(_extensions, this).push({
984
- name: name,
985
- extension: extension
986
- });
987
- this._debug("Registered extension", name);
988
-
989
- // Callback for extensions
990
- if (_isFunction.call(CometD, extension.registered)) {
991
- extension.registered(name, this);
992
- }
993
- return true;
994
- } else {
995
- this._info("Could not register extension with name", name, "since another extension with the same name already exists");
996
- return false;
997
- }
998
- }
999
- /**
1000
- * Unregister an extension previously registered with
1001
- * {@link #registerExtension(name, extension)}.
1002
- * @param name the name of the extension to unregister.
1003
- * @return true if the extension was unregistered, false otherwise
1004
- */
1005
- unregisterExtension(name) {
1006
- if (!_isString.call(CometD, name)) {
1007
- throw new Error("Illegal argument type: extension name must be a string");
1008
- }
1009
- let unregistered = false;
1010
- for (let i = 0; i < _classPrivateFieldGet(_extensions, this).length; ++i) {
1011
- const extension = _classPrivateFieldGet(_extensions, this)[i];
1012
- if (extension.name === name) {
1013
- _classPrivateFieldGet(_extensions, this).splice(i, 1);
1014
- unregistered = true;
1015
- this._debug("Unregistered extension", name);
1016
-
1017
- // Callback for extensions
1018
- const ext = extension.extension;
1019
- if (_isFunction.call(CometD, ext.unregistered)) {
1020
- ext.unregistered();
1021
- }
1022
- break;
1023
- }
1024
- }
1025
- return unregistered;
1026
- }
1027
- /**
1028
- * Find the extension registered with the given name.
1029
- * @param name the name of the extension to find
1030
- * @return the extension found or null if no extension with the given name has been registered
1031
- */
1032
- getExtension(name) {
1033
- for (let i = 0; i < _classPrivateFieldGet(_extensions, this).length; ++i) {
1034
- const extension = _classPrivateFieldGet(_extensions, this)[i];
1035
- if (extension.name === name) {
1036
- return extension.extension;
1037
- }
1038
- }
1039
- return null;
1040
- }
1041
- /**
1042
- * Returns the name assigned to this CometD object, or the string "default"
1043
- * if no name has been explicitly passed as parameter to the constructor.
1044
- */
1045
- getName() {
1046
- return _classPrivateFieldGet(_name, this);
1047
- }
1048
-
1049
- /**
1050
- * Returns the clientId assigned by the Bayeux server during handshake.
1051
- */
1052
- getClientId() {
1053
- return _classPrivateFieldGet(_clientId, this);
1054
- }
1055
-
1056
- /**
1057
- * Returns the URL of the Bayeux server.
1058
- */
1059
- getURL() {
1060
- const transport = this.getTransport();
1061
- if (transport) {
1062
- let url = transport.url;
1063
- if (url) {
1064
- return url;
1065
- }
1066
- url = _classPrivateFieldGet(_config, this).urls[transport.type];
1067
- if (url) {
1068
- return url;
1069
- }
1070
- }
1071
- return _classPrivateFieldGet(_config, this).url;
1072
- }
1073
- getTransport() {
1074
- return _classPrivateFieldGet(_transport, this);
1075
- }
1076
- getConfiguration() {
1077
- return this._mixin(true, {}, _classPrivateFieldGet(_config, this));
1078
- }
1079
- getAdvice() {
1080
- return this._mixin(true, {}, _classPrivateFieldGet(_advice, this));
1081
- }
1082
- setTimeout(funktion, delay) {
1083
- return _classPrivateFieldGet(_scheduler, this).setTimeout(() => {
1084
- try {
1085
- this._debug("Invoking timed function", funktion);
1086
- funktion();
1087
- } catch (x) {
1088
- this._debug("Exception invoking timed function", funktion, x);
1089
- }
1090
- }, delay);
1091
- }
1092
- clearTimeout(id) {
1093
- _classPrivateFieldGet(_scheduler, this).clearTimeout(id);
1094
- }
1095
- }
1096
- _CometD = CometD;
1097
- function _fieldValue(object, name) {
1098
- try {
1099
- return object[name];
1100
- } catch (x) {
1101
- return undefined;
1102
- }
1103
- }
1104
- function _isString(value) {
1105
- if (value === undefined || value === null) {
1106
- return false;
1107
- }
1108
- return typeof value === "string" || value instanceof String;
1109
- }
1110
- function _isAlpha(char) {
1111
- if (char >= "A" && char <= "Z") {
1112
- return true;
1113
- }
1114
- return char >= "a" && char <= "z";
1115
- }
1116
- function _isNumeric(char) {
1117
- return char >= "0" && char <= "9";
1118
- }
1119
- function _isAllowed(char) {
1120
- switch (char) {
1121
- case " ":
1122
- case "!":
1123
- case "#":
1124
- case "$":
1125
- case "(":
1126
- case ")":
1127
- case "*":
1128
- case "+":
1129
- case "-":
1130
- case ".":
1131
- case "/":
1132
- case "@":
1133
- case "_":
1134
- case "{":
1135
- case "~":
1136
- case "}":
1137
- return true;
1138
- default:
1139
- return false;
1140
- }
1141
- }
1142
- function _isValidChannel(value) {
1143
- if (!_isString.call(_CometD, value)) {
1144
- return false;
1145
- }
1146
- if (value.length < 2) {
1147
- return false;
1148
- }
1149
- if (value.charAt(0) !== "/") {
1150
- return false;
1151
- }
1152
- for (let i = 1; i < value.length; ++i) {
1153
- const char = value.charAt(i);
1154
- if (_isAlpha.call(_CometD, char) || _isNumeric.call(_CometD, char) || _isAllowed.call(_CometD, char)) {
1155
- continue;
1156
- }
1157
- return false;
1158
- }
1159
- return true;
1160
- }
1161
- function _isFunction(value) {
1162
- if (value === undefined || value === null) {
1163
- return false;
1164
- }
1165
- return typeof value === "function";
1166
- }
1167
- function _zeroPad(value, length) {
1168
- let result = "";
1169
- while (--length > 0) {
1170
- if (value >= Math.pow(10, length)) {
1171
- break;
1172
- }
1173
- result += "0";
1174
- }
1175
- result += value;
1176
- return result;
1177
- }
1178
- function _log(level, args) {
1179
- if (window.console) {
1180
- const logger = window.console[level];
1181
- if (_isFunction.call(_CometD, logger)) {
1182
- const now = new Date();
1183
- [].splice.call(args, 0, 0, _zeroPad.call(_CometD, now.getHours(), 2) + ":" + _zeroPad.call(_CometD, now.getMinutes(), 2) + ":" + _zeroPad.call(_CometD, now.getSeconds(), 2) + "." + _zeroPad.call(_CometD, now.getMilliseconds(), 3));
1184
- logger.apply(window.console, args);
1185
- }
1186
- }
1187
- }
1188
- function _splitURL(url) {
1189
- // [1] = protocol://,
1190
- // [2] = host:port,
1191
- // [3] = host,
1192
- // [4] = IPv6_host,
1193
- // [5] = IPv4_host,
1194
- // [6] = :port,
1195
- // [7] = port,
1196
- // [8] = uri,
1197
- // [9] = rest (query / fragment)
1198
- return new RegExp("(^https?://)?(((\\[[^\\]]+])|([^:/?#]+))(:(\\d+))?)?([^?#]*)(.*)?").exec(url);
1199
- }
1200
- /**
1201
- * Returns whether the given hostAndPort is cross domain.
1202
- * The default implementation checks against window.location.host
1203
- * but this function can be overridden to make it work in non-browser
1204
- * environments.
1205
- *
1206
- * @param hostAndPort the host and port in format host:port
1207
- * @return whether the given hostAndPort is cross domain
1208
- */
1209
- function _isCrossDomain(hostAndPort) {
1210
- if (window.location && window.location.host) {
1211
- if (hostAndPort) {
1212
- return hostAndPort !== window.location.host;
1213
- }
1214
- }
1215
- return false;
1216
- }
1217
- function _configure(configuration) {
1218
- this._debug("Configuring cometd object with", configuration);
1219
- // Support old style param, where only the Bayeux server URL was passed.
1220
- if (_isString.call(_CometD, configuration)) {
1221
- configuration = {
1222
- url: configuration
1223
- };
1224
- }
1225
- if (!configuration) {
1226
- configuration = {};
1227
- }
1228
- _classPrivateFieldSet(_config, this, this._mixin(false, _classPrivateFieldGet(_config, this), configuration));
1229
- const url = this.getURL();
1230
- if (!url) {
1231
- throw new Error("Missing required configuration parameter 'url' specifying the Bayeux server URL");
1232
- }
1233
-
1234
- // Check if we're cross domain.
1235
- const urlParts = _splitURL.call(_CometD, url);
1236
- const hostAndPort = urlParts[2];
1237
- const uri = urlParts[8];
1238
- const afterURI = urlParts[9];
1239
- _classPrivateFieldSet(_crossDomain, this, _assertClassBrand(_CometD_brand, this, _isCrossDomain).call(this, hostAndPort));
1240
-
1241
- // Check if appending extra path is supported.
1242
- if (_classPrivateFieldGet(_config, this).appendMessageTypeToURL) {
1243
- if (afterURI !== undefined && afterURI.length > 0) {
1244
- this._info("Appending message type to URI", uri, afterURI, "is not supported, disabling 'appendMessageTypeToURL' configuration");
1245
- _classPrivateFieldGet(_config, this).appendMessageTypeToURL = false;
1246
- } else {
1247
- const uriSegments = uri.split("/");
1248
- let lastSegmentIndex = uriSegments.length - 1;
1249
- if (uri.match(/\/$/)) {
1250
- lastSegmentIndex -= 1;
1251
- }
1252
- if (uriSegments[lastSegmentIndex].indexOf(".") >= 0) {
1253
- // Very likely the CometD servlet's URL pattern is mapped to an extension,
1254
- // such as *.cometd, so cannot add the extra path in this case.
1255
- this._info("Appending message type to URI", uri, "is not supported, disabling 'appendMessageTypeToURL' configuration");
1256
- _classPrivateFieldGet(_config, this).appendMessageTypeToURL = false;
1257
- }
1258
- }
1259
- }
1260
- if (window.Worker && window.Blob && window.URL && _classPrivateFieldGet(_config, this).useWorkerScheduler) {
1261
- let code = WorkerScheduler.toString();
1262
- // Remove the function declaration, the opening brace and the closing brace.
1263
- code = code.substring(code.indexOf("{") + 1, code.lastIndexOf("}"));
1264
- const blob = new window.Blob([code], {
1265
- type: "application/json"
1266
- });
1267
- const blobURL = window.URL.createObjectURL(blob);
1268
- const worker = new window.Worker(blobURL);
1269
- // Replace setTimeout() and clearTimeout() with the worker implementation.
1270
- _classPrivateFieldGet(_scheduler, this).setTimeout = (funktion, delay) => {
1271
- const id = _classPrivateFieldGet(_scheduler, this).register(funktion);
1272
- worker.postMessage({
1273
- id: id,
1274
- type: "setTimeout",
1275
- delay: delay
1276
- });
1277
- return id;
1278
- };
1279
- _classPrivateFieldGet(_scheduler, this).clearTimeout = id => {
1280
- _classPrivateFieldGet(_scheduler, this).unregister(id);
1281
- worker.postMessage({
1282
- id: id,
1283
- type: "clearTimeout"
1284
- });
1285
- };
1286
- worker.onmessage = e => {
1287
- const id = e.data.id;
1288
- const funktion = _classPrivateFieldGet(_scheduler, this).unregister(id);
1289
- if (funktion) {
1290
- funktion();
1291
- }
1292
- };
1293
- }
1294
- }
1295
- function _removeListener(subscription) {
1296
- if (subscription) {
1297
- const subscriptions = _classPrivateFieldGet(_listeners, this)[subscription.channel];
1298
- if (subscriptions && subscriptions[subscription.id]) {
1299
- delete subscriptions[subscription.id];
1300
- this._debug("Removed", subscription.listener ? "listener" : "subscription", subscription);
1301
- }
1302
- }
1303
- }
1304
- function _removeSubscription(subscription) {
1305
- if (subscription && !subscription.listener) {
1306
- _assertClassBrand(_CometD_brand, this, _removeListener).call(this, subscription);
1307
- }
1308
- }
1309
- function _clearSubscriptions() {
1310
- for (let channel in _classPrivateFieldGet(_listeners, this)) {
1311
- if (_classPrivateFieldGet(_listeners, this).hasOwnProperty(channel)) {
1312
- const subscriptions = _classPrivateFieldGet(_listeners, this)[channel];
1313
- if (subscriptions) {
1314
- for (let id in subscriptions) {
1315
- if (subscriptions.hasOwnProperty(id)) {
1316
- _assertClassBrand(_CometD_brand, this, _removeSubscription).call(this, subscriptions[id]);
1317
- }
1318
- }
1319
- }
1320
- }
1321
- }
1322
- }
1323
- function _setStatus(newStatus) {
1324
- const oldStatus = this.getStatus();
1325
- if (oldStatus !== newStatus) {
1326
- this._debug("Status", oldStatus, "->", newStatus);
1327
- _classPrivateFieldSet(_status, this, newStatus);
1328
- }
1329
- }
1330
- function _isDisconnected() {
1331
- const status = _classPrivateFieldGet(_status, this);
1332
- return status === "disconnecting" || status === "disconnected";
1333
- }
1334
- function _nextMessageId() {
1335
- var _this$messageId;
1336
- const result = _classPrivateFieldSet(_messageId, this, (_this$messageId = _classPrivateFieldGet(_messageId, this), ++_this$messageId));
1337
- return "" + result;
1338
- }
1339
- function _applyExtension(scope, callback, name, message, outgoing) {
1340
- try {
1341
- return callback.call(scope, message);
1342
- } catch (x) {
1343
- const handler = this.onExtensionException;
1344
- if (_isFunction.call(_CometD, handler)) {
1345
- this._debug("Invoking extension exception handler", name, x);
1346
- try {
1347
- handler.call(this, x, name, outgoing, message);
1348
- } catch (xx) {
1349
- this._info("Exception during execution of extension exception handler", name, xx);
1350
- }
1351
- } else {
1352
- this._info("Exception during execution of extension", name, x);
1353
- }
1354
- return message;
1355
- }
1356
- }
1357
- function _applyIncomingExtensions(message) {
1358
- for (let i = 0; i < _classPrivateFieldGet(_extensions, this).length; ++i) {
1359
- if (message === undefined || message === null) {
1360
- break;
1361
- }
1362
- const extension = _classPrivateFieldGet(_extensions, this)[i];
1363
- const callback = extension.extension.incoming;
1364
- if (_isFunction.call(_CometD, callback)) {
1365
- const result = _assertClassBrand(_CometD_brand, this, _applyExtension).call(this, extension.extension, callback, extension.name, message, false);
1366
- message = result === undefined ? message : result;
1367
- }
1368
- }
1369
- return message;
1370
- }
1371
- function _applyOutgoingExtensions(message) {
1372
- for (let i = _classPrivateFieldGet(_extensions, this).length - 1; i >= 0; --i) {
1373
- if (message === undefined || message === null) {
1374
- break;
1375
- }
1376
- const extension = _classPrivateFieldGet(_extensions, this)[i];
1377
- const callback = extension.extension.outgoing;
1378
- if (_isFunction.call(_CometD, callback)) {
1379
- const result = _assertClassBrand(_CometD_brand, this, _applyExtension).call(this, extension.extension, callback, extension.name, message, true);
1380
- message = result === undefined ? message : result;
1381
- }
1382
- }
1383
- return message;
1384
- }
1385
- function _notify(channel, message) {
1386
- const subscriptions = _classPrivateFieldGet(_listeners, this)[channel];
1387
- if (subscriptions) {
1388
- for (let id in subscriptions) {
1389
- if (subscriptions.hasOwnProperty(id)) {
1390
- const subscription = subscriptions[id];
1391
- // Subscriptions may come and go, so the array may have holes.
1392
- if (subscription) {
1393
- try {
1394
- subscription.callback.call(subscription.scope, message);
1395
- } catch (x) {
1396
- const handler = this.onListenerException;
1397
- if (_isFunction.call(_CometD, handler)) {
1398
- this._debug("Invoking listener exception handler", subscription, x);
1399
- try {
1400
- handler.call(this, x, subscription, subscription.listener, message);
1401
- } catch (xx) {
1402
- this._info("Exception during execution of listener exception handler", subscription, xx);
1403
- }
1404
- } else {
1405
- this._info("Exception during execution of listener", subscription, message, x);
1406
- }
1407
- }
1408
- }
1409
- }
1410
- }
1411
- }
1412
- }
1413
- function _notifyListeners(channel, message) {
1414
- // Notify direct listeners
1415
- _assertClassBrand(_CometD_brand, this, _notify).call(this, channel, message);
1416
-
1417
- // Notify the globbing listeners
1418
- const channelParts = channel.split("/");
1419
- const last = channelParts.length - 1;
1420
- for (let i = last; i > 0; --i) {
1421
- let channelPart = channelParts.slice(0, i).join("/") + "/*";
1422
- // We don't want to notify /foo/* if the channel is /foo/bar/baz,
1423
- // so we stop at the first non-recursive globbing.
1424
- if (i === last) {
1425
- _assertClassBrand(_CometD_brand, this, _notify).call(this, channelPart, message);
1426
- }
1427
- // Add the recursive globber and notify.
1428
- channelPart += "*";
1429
- _assertClassBrand(_CometD_brand, this, _notify).call(this, channelPart, message);
1430
- }
1431
- }
1432
- function _cancelDelayedSend() {
1433
- if (_classPrivateFieldGet(_scheduledSend, this) !== null) {
1434
- this.clearTimeout(_classPrivateFieldGet(_scheduledSend, this));
1435
- }
1436
- _classPrivateFieldSet(_scheduledSend, this, null);
1437
- }
1438
- function _delayedSend(operation, delay) {
1439
- _assertClassBrand(_CometD_brand, this, _cancelDelayedSend).call(this);
1440
- const time = _classPrivateFieldGet(_advice, this).interval + delay;
1441
- this._debug("Function scheduled in", time, "ms, interval =", _classPrivateFieldGet(_advice, this).interval, "backoff =", _classPrivateFieldGet(_backoff, this), operation);
1442
- _classPrivateFieldSet(_scheduledSend, this, this.setTimeout(operation, time));
1443
- }
1444
- /**
1445
- * Delivers the messages to the CometD server
1446
- * @param messages the array of messages to send
1447
- * @param metaConnect true if this send is on /meta/connect
1448
- * @param extraPath an extra path to append to the Bayeux server URL
1449
- */
1450
- function _send(messages, metaConnect, extraPath) {
1451
- // We must be sure that the messages have a clientId.
1452
- // This is not guaranteed since the handshake may take time to return
1453
- // (and hence the clientId is not known yet) and the application
1454
- // may create other messages.
1455
- for (let i = 0; i < messages.length; ++i) {
1456
- let message = messages[i];
1457
- const messageId = message.id;
1458
- const clientId = this.getClientId();
1459
- if (clientId) {
1460
- message.clientId = clientId;
1461
- }
1462
- message = _assertClassBrand(_CometD_brand, this, _applyOutgoingExtensions).call(this, message);
1463
- if (message !== undefined && message !== null) {
1464
- // Extensions may have modified the message id, but we need to own it.
1465
- message.id = messageId;
1466
- messages[i] = message;
1467
- } else {
1468
- delete _classPrivateFieldGet(_callbacks, this)[messageId];
1469
- messages.splice(i--, 1);
1470
- }
1471
- }
1472
- if (messages.length === 0) {
1473
- return;
1474
- }
1475
- if (metaConnect) {
1476
- _classPrivateFieldSet(_metaConnect, this, messages[0]);
1477
- }
1478
- let url = this.getURL();
1479
- if (_classPrivateFieldGet(_config, this).appendMessageTypeToURL) {
1480
- // If url does not end with "/", then append it
1481
- if (!url.match(/\/$/)) {
1482
- url = url + "/";
1483
- }
1484
- if (extraPath) {
1485
- url = url + extraPath;
1486
- }
1487
- }
1488
- const envelope = {
1489
- url: url,
1490
- sync: false,
1491
- messages: messages,
1492
- onSuccess: rcvdMessages => {
1493
- try {
1494
- _assertClassBrand(_CometD_brand, this, _handleMessages).call(this, rcvdMessages);
1495
- } catch (x) {
1496
- this._info("Exception during handling of messages", x);
1497
- }
1498
- },
1499
- onFailure: (conduit, messages, failure) => {
1500
- try {
1501
- const transport = this.getTransport();
1502
- failure.connectionType = transport ? transport.type : "unknown";
1503
- _assertClassBrand(_CometD_brand, this, _handleFailure).call(this, conduit, messages, failure);
1504
- } catch (x) {
1505
- this._info("Exception during handling of failure", x);
1506
- }
1507
- }
1508
- };
1509
- this._debug("Send", envelope);
1510
- this.getTransport().send(envelope, metaConnect);
1511
- }
1512
- function _queueSend(message) {
1513
- if (_classPrivateFieldGet(_batch, this) > 0 || _classPrivateFieldGet(_internalBatch, this) === true) {
1514
- _classPrivateFieldGet(_messageQueue, this).push(message);
1515
- } else {
1516
- _assertClassBrand(_CometD_brand, this, _send).call(this, [message], false);
1517
- }
1518
- }
1519
- function _resetBackoff() {
1520
- _classPrivateFieldSet(_backoff, this, 0);
1521
- }
1522
- function _increaseBackoff() {
1523
- if (_classPrivateFieldGet(_backoff, this) < _classPrivateFieldGet(_config, this).maxBackoff) {
1524
- _classPrivateFieldSet(_backoff, this, _classPrivateFieldGet(_backoff, this) + this.getBackoffIncrement());
1525
- }
1526
- return _classPrivateFieldGet(_backoff, this);
1527
- }
1528
- /**
1529
- * Starts the batch of messages to be sent in a single request.
1530
- */
1531
- function _startBatch() {
1532
- var _this$batch;
1533
- _classPrivateFieldSet(_batch, this, (_this$batch = _classPrivateFieldGet(_batch, this), ++_this$batch));
1534
- this._debug("Starting batch, depth", _classPrivateFieldGet(_batch, this));
1535
- }
1536
- function _flushBatch() {
1537
- const messages = _classPrivateFieldGet(_messageQueue, this);
1538
- _classPrivateFieldSet(_messageQueue, this, []);
1539
- if (messages.length > 0) {
1540
- _assertClassBrand(_CometD_brand, this, _send).call(this, messages, false);
1541
- }
1542
- }
1543
- /**
1544
- * Ends the batch of messages to be sent in a single request,
1545
- * optionally sending messages present in the message queue depending
1546
- * on the given argument.
1547
- */
1548
- function _endBatch() {
1549
- var _this$batch2;
1550
- _classPrivateFieldSet(_batch, this, (_this$batch2 = _classPrivateFieldGet(_batch, this), --_this$batch2));
1551
- this._debug("Ending batch, depth", _classPrivateFieldGet(_batch, this));
1552
- if (_classPrivateFieldGet(_batch, this) < 0) {
1553
- throw new Error("Calls to startBatch() and endBatch() are not paired");
1554
- }
1555
- if (_classPrivateFieldGet(_batch, this) === 0 && !this.isDisconnected() && !_classPrivateFieldGet(_internalBatch, this)) {
1556
- _assertClassBrand(_CometD_brand, this, _flushBatch).call(this);
1557
- }
1558
- }
1559
- /**
1560
- * Sends the connect message
1561
- */
1562
- function _connect() {
1563
- if (!this.isDisconnected()) {
1564
- const bayeuxMessage = {
1565
- id: _assertClassBrand(_CometD_brand, this, _nextMessageId).call(this),
1566
- channel: "/meta/connect",
1567
- connectionType: this.getTransport().type
1568
- };
1569
-
1570
- // In case of reload or temporary loss of connection
1571
- // we want the next successful connect to return immediately
1572
- // instead of being held by the server, so that connect listeners
1573
- // can be notified that the connection has been re-established
1574
- if (!_classPrivateFieldGet(_connected, this)) {
1575
- bayeuxMessage.advice = {
1576
- timeout: 0
1577
- };
1578
- }
1579
- _assertClassBrand(_CometD_brand, this, _setStatus).call(this, "connecting");
1580
- this._debug("Connect sent", bayeuxMessage);
1581
- _assertClassBrand(_CometD_brand, this, _send).call(this, [bayeuxMessage], true, "connect");
1582
- _assertClassBrand(_CometD_brand, this, _setStatus).call(this, "connected");
1583
- }
1584
- }
1585
- function _delayedConnect(delay) {
1586
- _assertClassBrand(_CometD_brand, this, _setStatus).call(this, "connecting");
1587
- _assertClassBrand(_CometD_brand, this, _delayedSend).call(this, () => {
1588
- _assertClassBrand(_CometD_brand, this, _connect).call(this);
1589
- }, delay);
1590
- }
1591
- function _updateAdvice(newAdvice) {
1592
- if (newAdvice) {
1593
- _classPrivateFieldSet(_advice, this, this._mixin(false, {}, _classPrivateFieldGet(_config, this).advice, newAdvice));
1594
- this._debug("New advice", _classPrivateFieldGet(_advice, this));
1595
- }
1596
- }
1597
- function _disconnect(abort) {
1598
- _assertClassBrand(_CometD_brand, this, _cancelDelayedSend).call(this);
1599
- const transport = this.getTransport();
1600
- if (abort && transport) {
1601
- transport.abort();
1602
- }
1603
- _classPrivateFieldSet(_crossDomain, this, false);
1604
- _classPrivateFieldSet(_transport, this, null);
1605
- _assertClassBrand(_CometD_brand, this, _setStatus).call(this, "disconnected");
1606
- _classPrivateFieldSet(_clientId, this, null);
1607
- _classPrivateFieldSet(_batch, this, 0);
1608
- _assertClassBrand(_CometD_brand, this, _resetBackoff).call(this);
1609
- _classPrivateFieldSet(_reestablish, this, false);
1610
- _classPrivateFieldSet(_connected, this, false);
1611
- _classPrivateFieldSet(_unconnectTime, this, 0);
1612
- _classPrivateFieldSet(_metaConnect, this, null);
1613
-
1614
- // Fail any existing queued message
1615
- if (_classPrivateFieldGet(_messageQueue, this).length > 0) {
1616
- const messages = _classPrivateFieldGet(_messageQueue, this);
1617
- _classPrivateFieldSet(_messageQueue, this, []);
1618
- _assertClassBrand(_CometD_brand, this, _handleFailure).call(this, undefined, messages, {
1619
- reason: "Disconnected"
1620
- });
1621
- }
1622
- }
1623
- function _notifyTransportException(oldTransport, newTransport, failure) {
1624
- const handler = this.onTransportException;
1625
- if (_isFunction.call(_CometD, handler)) {
1626
- this._debug("Invoking transport exception handler", oldTransport, newTransport, failure);
1627
- try {
1628
- handler.call(this, failure, oldTransport, newTransport);
1629
- } catch (x) {
1630
- this._info("Exception during execution of transport exception handler", x);
1631
- }
1632
- }
1633
- }
1634
- /**
1635
- * Sends the initial handshake message
1636
- */
1637
- function _handshake(handshakeProps, handshakeCallback) {
1638
- if (_isFunction.call(_CometD, handshakeProps)) {
1639
- handshakeCallback = handshakeProps;
1640
- handshakeProps = undefined;
1641
- }
1642
- _classPrivateFieldSet(_clientId, this, null);
1643
- this.clearSubscriptions();
1644
-
1645
- // Reset the transports if we're not retrying the handshake
1646
- if (this.isDisconnected()) {
1647
- _classPrivateFieldGet(_transports, this).reset(true);
1648
- }
1649
-
1650
- // Reset the advice.
1651
- _assertClassBrand(_CometD_brand, this, _updateAdvice).call(this, {});
1652
- _classPrivateFieldSet(_batch, this, 0);
1653
-
1654
- // Mark the start of an internal batch.
1655
- // This is needed because handshake and connect are async.
1656
- // It may happen that the application calls init() then subscribe()
1657
- // and the subscribe message is sent before the connect message, if
1658
- // the subscribe message is not held until the connect message is sent.
1659
- // So here we start a batch to hold temporarily any message until
1660
- // the connection is fully established.
1661
- _classPrivateFieldSet(_internalBatch, this, true);
1662
-
1663
- // Save the properties provided by the user, so that
1664
- // we can reuse them during automatic re-handshake
1665
- _classPrivateFieldSet(_handshakeProps, this, handshakeProps);
1666
- _classPrivateFieldSet(_handshakeCallback, this, handshakeCallback);
1667
- const version = "1.0";
1668
-
1669
- // Figure out the transports to send to the server
1670
- const url = this.getURL();
1671
- const transportTypes = _classPrivateFieldGet(_transports, this).findTransportTypes(version, _classPrivateFieldGet(_crossDomain, this), url);
1672
- const bayeuxMessage = {
1673
- id: _assertClassBrand(_CometD_brand, this, _nextMessageId).call(this),
1674
- version: version,
1675
- minimumVersion: version,
1676
- channel: "/meta/handshake",
1677
- supportedConnectionTypes: transportTypes,
1678
- advice: {
1679
- timeout: _classPrivateFieldGet(_advice, this).timeout,
1680
- interval: _classPrivateFieldGet(_advice, this).interval
1681
- }
1682
- };
1683
- // Do not allow the user to override important fields.
1684
- const message = this._mixin(false, {}, _classPrivateFieldGet(_handshakeProps, this), bayeuxMessage);
1685
-
1686
- // Save the callback.
1687
- this._putCallback(message.id, handshakeCallback);
1688
-
1689
- // Pick up the first available transport as initial transport
1690
- // since we don't know if the server supports it
1691
- if (!_classPrivateFieldGet(_transport, this)) {
1692
- _classPrivateFieldSet(_transport, this, _classPrivateFieldGet(_transports, this).negotiateTransport(transportTypes, version, _classPrivateFieldGet(_crossDomain, this), url));
1693
- if (!_classPrivateFieldGet(_transport, this)) {
1694
- const failure = "Could not find initial transport among: " + this.getTransportTypes();
1695
- this._warn(failure);
1696
- throw new Error(failure);
1697
- }
1698
- }
1699
- this._debug("Initial transport is", _classPrivateFieldGet(_transport, this).type);
1700
-
1701
- // We started a batch to hold the application messages,
1702
- // so here we must bypass it and send immediately.
1703
- _assertClassBrand(_CometD_brand, this, _setStatus).call(this, "handshaking");
1704
- this._debug("Handshake sent", message);
1705
- _assertClassBrand(_CometD_brand, this, _send).call(this, [message], false, "handshake");
1706
- }
1707
- function _delayedHandshake(delay) {
1708
- _assertClassBrand(_CometD_brand, this, _setStatus).call(this, "handshaking");
1709
-
1710
- // We will call #handshake() which will reset #clientId, but we want to avoid
1711
- // that between the end of this method and the call to #handshake() someone may
1712
- // call publish() (or other methods that call #queueSend()).
1713
- _classPrivateFieldSet(_internalBatch, this, true);
1714
- _assertClassBrand(_CometD_brand, this, _delayedSend).call(this, () => {
1715
- _assertClassBrand(_CometD_brand, this, _handshake).call(this, _classPrivateFieldGet(_handshakeProps, this), _classPrivateFieldGet(_handshakeCallback, this));
1716
- }, delay);
1717
- }
1718
- function _notifyCallback(callback, message) {
1719
- try {
1720
- callback.call(this, message);
1721
- } catch (x) {
1722
- const handler = this.onCallbackException;
1723
- if (_isFunction.call(_CometD, handler)) {
1724
- this._debug("Invoking callback exception handler", x);
1725
- try {
1726
- handler.call(this, x, message);
1727
- } catch (xx) {
1728
- this._info("Exception during execution of callback exception handler", xx);
1729
- }
1730
- } else {
1731
- this._info("Exception during execution of message callback", x);
1732
- }
1733
- }
1734
- }
1735
- function _handleCallback(message) {
1736
- const callback = this._getCallback([message.id]);
1737
- if (_isFunction.call(_CometD, callback)) {
1738
- delete _classPrivateFieldGet(_callbacks, this)[message.id];
1739
- _assertClassBrand(_CometD_brand, this, _notifyCallback).call(this, callback, message);
1740
- }
1741
- }
1742
- function _handleRemoteCall(message) {
1743
- const context = _classPrivateFieldGet(_remoteCalls, this)[message.id];
1744
- delete _classPrivateFieldGet(_remoteCalls, this)[message.id];
1745
- if (context) {
1746
- this._debug("Handling remote call response for", message, "with context", context);
1747
-
1748
- // Clear the timeout, if present.
1749
- const timeout = context.timeout;
1750
- if (timeout) {
1751
- this.clearTimeout(timeout);
1752
- }
1753
- const callback = context.callback;
1754
- if (_isFunction.call(_CometD, callback)) {
1755
- _assertClassBrand(_CometD_brand, this, _notifyCallback).call(this, callback, message);
1756
- return true;
1757
- }
1758
- }
1759
- return false;
1760
- }
1761
- function _handleTransportFailure(failureInfo) {
1762
- this._debug("Transport failure handling", failureInfo);
1763
- if (failureInfo.transport) {
1764
- _classPrivateFieldSet(_transport, this, failureInfo.transport);
1765
- }
1766
- if (failureInfo.url) {
1767
- _classPrivateFieldGet(_transport, this).url = failureInfo.url;
1768
- }
1769
- const action = failureInfo.action;
1770
- const delay = failureInfo.delay || 0;
1771
- switch (action) {
1772
- case "handshake":
1773
- _assertClassBrand(_CometD_brand, this, _delayedHandshake).call(this, delay);
1774
- break;
1775
- case "retry":
1776
- _assertClassBrand(_CometD_brand, this, _delayedConnect).call(this, delay);
1777
- break;
1778
- case "none":
1779
- _assertClassBrand(_CometD_brand, this, _disconnect).call(this, true);
1780
- break;
1781
- default:
1782
- throw new Error("Unknown action " + action);
1783
- }
1784
- }
1785
- function _failHandshake(message, failureInfo) {
1786
- _assertClassBrand(_CometD_brand, this, _handleCallback).call(this, message);
1787
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/handshake", message);
1788
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/unsuccessful", message);
1789
-
1790
- // The listeners may have disconnected.
1791
- if (this.isDisconnected()) {
1792
- failureInfo.action = "none";
1793
- }
1794
- this.onTransportFailure(message, failureInfo, failureInfo => _assertClassBrand(_CometD_brand, this, _handleTransportFailure).call(this, failureInfo));
1795
- }
1796
- function _handshakeResponse(message) {
1797
- const url = this.getURL();
1798
- if (message.successful) {
1799
- const crossDomain = _assertClassBrand(_CometD_brand, this, _isCrossDomain).call(this, _splitURL.call(_CometD, url)[2]);
1800
- const newTransport = _classPrivateFieldGet(_transports, this).negotiateTransport(message.supportedConnectionTypes, message.version, crossDomain, url);
1801
- if (newTransport === null) {
1802
- message.successful = false;
1803
- _assertClassBrand(_CometD_brand, this, _failHandshake).call(this, message, {
1804
- cause: "negotiation",
1805
- action: "none",
1806
- transport: null
1807
- });
1808
- return;
1809
- } else {
1810
- const oldTransport = _classPrivateFieldGet(_transport, this);
1811
- if (oldTransport !== newTransport) {
1812
- this._debug("Transport", oldTransport && oldTransport.type, "->", newTransport.type);
1813
- _classPrivateFieldSet(_transport, this, newTransport);
1814
- }
1815
- }
1816
- _classPrivateFieldSet(_clientId, this, message.clientId);
1817
-
1818
- // End the internal batch and allow held messages from the application
1819
- // to go to the server (see #handshake() where we start the internal batch).
1820
- _classPrivateFieldSet(_internalBatch, this, false);
1821
- _assertClassBrand(_CometD_brand, this, _flushBatch).call(this);
1822
-
1823
- // Here the new transport is in place, as well as the clientId, so
1824
- // the listeners can perform a publish() if they want.
1825
- // Notify the listeners before the connect below.
1826
- message.reestablish = _classPrivateFieldGet(_reestablish, this);
1827
- _classPrivateFieldSet(_reestablish, this, true);
1828
- _assertClassBrand(_CometD_brand, this, _handleCallback).call(this, message);
1829
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/handshake", message);
1830
- _classPrivateFieldSet(_handshakeMessages, this, message["x-messages"] || 0);
1831
- const action = this.isDisconnected() ? "none" : _classPrivateFieldGet(_advice, this).reconnect || "retry";
1832
- switch (action) {
1833
- case "retry":
1834
- _assertClassBrand(_CometD_brand, this, _resetBackoff).call(this);
1835
- if (_classPrivateFieldGet(_handshakeMessages, this) === 0) {
1836
- _assertClassBrand(_CometD_brand, this, _delayedConnect).call(this, 0);
1837
- } else {
1838
- this._debug("Processing", _classPrivateFieldGet(_handshakeMessages, this), "handshake-delivered messages");
1839
- }
1840
- break;
1841
- case "none":
1842
- _assertClassBrand(_CometD_brand, this, _disconnect).call(this, true);
1843
- break;
1844
- default:
1845
- throw new Error("Unrecognized advice action " + action);
1846
- }
1847
- } else {
1848
- _assertClassBrand(_CometD_brand, this, _failHandshake).call(this, message, {
1849
- cause: "unsuccessful",
1850
- action: _classPrivateFieldGet(_advice, this).reconnect || "handshake",
1851
- transport: this.getTransport()
1852
- });
1853
- }
1854
- }
1855
- function _handshakeFailure(message) {
1856
- _assertClassBrand(_CometD_brand, this, _failHandshake).call(this, message, {
1857
- cause: "failure",
1858
- action: "handshake",
1859
- transport: null
1860
- });
1861
- }
1862
- function _matchMetaConnect(connect) {
1863
- if (this.getStatus() === "disconnected") {
1864
- return true;
1865
- }
1866
- if (_classPrivateFieldGet(_metaConnect, this) && _classPrivateFieldGet(_metaConnect, this).id === connect.id) {
1867
- _classPrivateFieldSet(_metaConnect, this, null);
1868
- return true;
1869
- }
1870
- return false;
1871
- }
1872
- function _failConnect(message, failureInfo) {
1873
- // Notify the listeners after the status change but before the next action.
1874
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/connect", message);
1875
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/unsuccessful", message);
1876
-
1877
- // The listeners may have disconnected.
1878
- if (this.isDisconnected()) {
1879
- failureInfo.action = "none";
1880
- }
1881
- this.onTransportFailure(message, failureInfo, failureInfo => _assertClassBrand(_CometD_brand, this, _handleTransportFailure).call(this, failureInfo));
1882
- }
1883
- function _connectResponse(message) {
1884
- if (_assertClassBrand(_CometD_brand, this, _matchMetaConnect).call(this, message)) {
1885
- _classPrivateFieldSet(_connected, this, message.successful);
1886
- if (_classPrivateFieldGet(_connected, this)) {
1887
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/connect", message);
1888
-
1889
- // Normally, the advice will say "reconnect: "retry", interval: 0"
1890
- // and the server will hold the request, so when a response returns
1891
- // we immediately call the server again (long polling).
1892
- // Listeners can call disconnect(), so check the state after they run.
1893
- const action = this.isDisconnected() ? "none" : _classPrivateFieldGet(_advice, this).reconnect || "retry";
1894
- switch (action) {
1895
- case "retry":
1896
- _assertClassBrand(_CometD_brand, this, _resetBackoff).call(this);
1897
- _assertClassBrand(_CometD_brand, this, _delayedConnect).call(this, _classPrivateFieldGet(_backoff, this));
1898
- break;
1899
- case "none":
1900
- _assertClassBrand(_CometD_brand, this, _disconnect).call(this, false);
1901
- break;
1902
- default:
1903
- throw new Error("Unrecognized advice action " + action);
1904
- }
1905
- } else {
1906
- _assertClassBrand(_CometD_brand, this, _failConnect).call(this, message, {
1907
- cause: "unsuccessful",
1908
- action: _classPrivateFieldGet(_advice, this).reconnect || "retry",
1909
- transport: this.getTransport()
1910
- });
1911
- }
1912
- } else {
1913
- this._debug("Mismatched /meta/connect reply", message);
1914
- }
1915
- }
1916
- function _connectFailure(message) {
1917
- if (_assertClassBrand(_CometD_brand, this, _matchMetaConnect).call(this, message)) {
1918
- _classPrivateFieldSet(_connected, this, false);
1919
- _assertClassBrand(_CometD_brand, this, _failConnect).call(this, message, {
1920
- cause: "failure",
1921
- action: "retry",
1922
- transport: null
1923
- });
1924
- } else {
1925
- this._debug("Mismatched /meta/connect failure", message);
1926
- }
1927
- }
1928
- function _failDisconnect(message) {
1929
- _assertClassBrand(_CometD_brand, this, _disconnect).call(this, true);
1930
- _assertClassBrand(_CometD_brand, this, _handleCallback).call(this, message);
1931
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/disconnect", message);
1932
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/unsuccessful", message);
1933
- }
1934
- function _disconnectResponse(message) {
1935
- if (message.successful) {
1936
- // Wait for the /meta/connect to arrive.
1937
- _assertClassBrand(_CometD_brand, this, _disconnect).call(this, false);
1938
- _assertClassBrand(_CometD_brand, this, _handleCallback).call(this, message);
1939
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/disconnect", message);
1940
- } else {
1941
- _assertClassBrand(_CometD_brand, this, _failDisconnect).call(this, message);
1942
- }
1943
- }
1944
- function _disconnectFailure(message) {
1945
- _assertClassBrand(_CometD_brand, this, _failDisconnect).call(this, message);
1946
- }
1947
- function _failSubscribe(message) {
1948
- const subscriptions = _classPrivateFieldGet(_listeners, this)[message.subscription];
1949
- if (subscriptions) {
1950
- for (let id in subscriptions) {
1951
- if (subscriptions.hasOwnProperty(id)) {
1952
- const subscription = subscriptions[id];
1953
- if (subscription && !subscription.listener) {
1954
- delete subscriptions[id];
1955
- this._debug("Removed failed subscription", subscription);
1956
- }
1957
- }
1958
- }
1959
- }
1960
- _assertClassBrand(_CometD_brand, this, _handleCallback).call(this, message);
1961
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/subscribe", message);
1962
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/unsuccessful", message);
1963
- }
1964
- function _subscribeResponse(message) {
1965
- if (message.successful) {
1966
- _assertClassBrand(_CometD_brand, this, _handleCallback).call(this, message);
1967
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/subscribe", message);
1968
- } else {
1969
- _assertClassBrand(_CometD_brand, this, _failSubscribe).call(this, message);
1970
- }
1971
- }
1972
- function _subscribeFailure(message) {
1973
- _assertClassBrand(_CometD_brand, this, _failSubscribe).call(this, message);
1974
- }
1975
- function _failUnsubscribe(message) {
1976
- _assertClassBrand(_CometD_brand, this, _handleCallback).call(this, message);
1977
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/unsubscribe", message);
1978
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/unsuccessful", message);
1979
- }
1980
- function _unsubscribeResponse(message) {
1981
- if (message.successful) {
1982
- _assertClassBrand(_CometD_brand, this, _handleCallback).call(this, message);
1983
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/unsubscribe", message);
1984
- } else {
1985
- _assertClassBrand(_CometD_brand, this, _failUnsubscribe).call(this, message);
1986
- }
1987
- }
1988
- function _unsubscribeFailure(message) {
1989
- _assertClassBrand(_CometD_brand, this, _failUnsubscribe).call(this, message);
1990
- }
1991
- function _failMessage(message) {
1992
- if (!_assertClassBrand(_CometD_brand, this, _handleRemoteCall).call(this, message)) {
1993
- _assertClassBrand(_CometD_brand, this, _handleCallback).call(this, message);
1994
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/publish", message);
1995
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/unsuccessful", message);
1996
- }
1997
- }
1998
- function _messageResponse(message) {
1999
- if (message.data !== undefined) {
2000
- if (!_assertClassBrand(_CometD_brand, this, _handleRemoteCall).call(this, message)) {
2001
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, message.channel, message);
2002
- if (_classPrivateFieldGet(_handshakeMessages, this) > 0) {
2003
- var _this$handshakeMessag;
2004
- _classPrivateFieldSet(_handshakeMessages, this, (_this$handshakeMessag = _classPrivateFieldGet(_handshakeMessages, this), --_this$handshakeMessag));
2005
- if (_classPrivateFieldGet(_handshakeMessages, this) === 0) {
2006
- this._debug("Processed last handshake-delivered message");
2007
- _assertClassBrand(_CometD_brand, this, _delayedConnect).call(this, 0);
2008
- }
2009
- }
2010
- }
2011
- } else {
2012
- if (message.successful === undefined) {
2013
- this._warn("Unknown Bayeux Message", message);
2014
- } else {
2015
- if (message.successful) {
2016
- _assertClassBrand(_CometD_brand, this, _handleCallback).call(this, message);
2017
- _assertClassBrand(_CometD_brand, this, _notifyListeners).call(this, "/meta/publish", message);
2018
- } else {
2019
- _assertClassBrand(_CometD_brand, this, _failMessage).call(this, message);
2020
- }
2021
- }
2022
- }
2023
- }
2024
- function _messageFailure(failure) {
2025
- _assertClassBrand(_CometD_brand, this, _failMessage).call(this, failure);
2026
- }
2027
- function _receive(message) {
2028
- _classPrivateFieldSet(_unconnectTime, this, 0);
2029
- message = _assertClassBrand(_CometD_brand, this, _applyIncomingExtensions).call(this, message);
2030
- if (message === undefined || message === null) {
2031
- return;
2032
- }
2033
- _assertClassBrand(_CometD_brand, this, _updateAdvice).call(this, message.advice);
2034
- const channel = message.channel;
2035
- switch (channel) {
2036
- case "/meta/handshake":
2037
- _assertClassBrand(_CometD_brand, this, _handshakeResponse).call(this, message);
2038
- break;
2039
- case "/meta/connect":
2040
- _assertClassBrand(_CometD_brand, this, _connectResponse).call(this, message);
2041
- break;
2042
- case "/meta/disconnect":
2043
- _assertClassBrand(_CometD_brand, this, _disconnectResponse).call(this, message);
2044
- break;
2045
- case "/meta/subscribe":
2046
- _assertClassBrand(_CometD_brand, this, _subscribeResponse).call(this, message);
2047
- break;
2048
- case "/meta/unsubscribe":
2049
- _assertClassBrand(_CometD_brand, this, _unsubscribeResponse).call(this, message);
2050
- break;
2051
- default:
2052
- _assertClassBrand(_CometD_brand, this, _messageResponse).call(this, message);
2053
- break;
2054
- }
2055
- }
2056
- function _handleMessages(rcvdMessages) {
2057
- this._debug("Received", rcvdMessages);
2058
- for (let i = 0; i < rcvdMessages.length; ++i) {
2059
- const message = rcvdMessages[i];
2060
- this.receive(message);
2061
- }
2062
- }
2063
- function _handleFailure(conduit, messages, failure) {
2064
- this._debug("handleFailure", conduit, messages, failure);
2065
- failure.transport = conduit;
2066
- for (let i = 0; i < messages.length; ++i) {
2067
- const message = messages[i];
2068
- const failureMessage = {
2069
- id: message.id,
2070
- successful: false,
2071
- channel: message.channel,
2072
- failure: failure
2073
- };
2074
- failure.message = message;
2075
- switch (message.channel) {
2076
- case "/meta/handshake":
2077
- _assertClassBrand(_CometD_brand, this, _handshakeFailure).call(this, failureMessage);
2078
- break;
2079
- case "/meta/connect":
2080
- _assertClassBrand(_CometD_brand, this, _connectFailure).call(this, failureMessage);
2081
- break;
2082
- case "/meta/disconnect":
2083
- _assertClassBrand(_CometD_brand, this, _disconnectFailure).call(this, failureMessage);
2084
- break;
2085
- case "/meta/subscribe":
2086
- failureMessage.subscription = message.subscription;
2087
- _assertClassBrand(_CometD_brand, this, _subscribeFailure).call(this, failureMessage);
2088
- break;
2089
- case "/meta/unsubscribe":
2090
- failureMessage.subscription = message.subscription;
2091
- _assertClassBrand(_CometD_brand, this, _unsubscribeFailure).call(this, failureMessage);
2092
- break;
2093
- default:
2094
- _assertClassBrand(_CometD_brand, this, _messageFailure).call(this, failureMessage);
2095
- break;
2096
- }
2097
- }
2098
- }
2099
- function _hasSubscriptions(channel) {
2100
- const subscriptions = _classPrivateFieldGet(_listeners, this)[channel];
2101
- if (subscriptions) {
2102
- for (let id in subscriptions) {
2103
- if (subscriptions.hasOwnProperty(id)) {
2104
- if (subscriptions[id]) {
2105
- return true;
2106
- }
2107
- }
2108
- }
2109
- }
2110
- return false;
2111
- }
2112
- function _resolveScopedCallback(scope, callback) {
2113
- const delegate = {
2114
- scope: scope,
2115
- method: callback
2116
- };
2117
- if (_isFunction.call(_CometD, scope)) {
2118
- delegate.scope = undefined;
2119
- delegate.method = scope;
2120
- } else {
2121
- if (_isString.call(_CometD, callback)) {
2122
- if (!scope) {
2123
- throw new Error("Invalid scope " + scope);
2124
- }
2125
- delegate.method = scope[callback];
2126
- if (!_isFunction.call(_CometD, delegate.method)) {
2127
- throw new Error("Invalid callback " + callback + " for scope " + scope);
2128
- }
2129
- } else if (!_isFunction.call(_CometD, callback)) {
2130
- throw new Error("Invalid callback " + callback);
2131
- }
2132
- }
2133
- return delegate;
2134
- }
2135
- function _addListener(channel, scope, callback, isListener) {
2136
- var _this$listenerId;
2137
- // The data structure is a map<channel, subscription[]>, where each subscription
2138
- // holds the callback to be called and its scope.
2139
-
2140
- const delegate = _assertClassBrand(_CometD_brand, this, _resolveScopedCallback).call(this, scope, callback);
2141
- this._debug("Adding", isListener ? "listener" : "subscription", "on", channel, "with scope", delegate.scope, "and callback", delegate.method);
2142
- const id = _classPrivateFieldSet(_listenerId, this, (_this$listenerId = _classPrivateFieldGet(_listenerId, this), ++_this$listenerId));
2143
- const subscription = {
2144
- id: id,
2145
- channel: channel,
2146
- scope: delegate.scope,
2147
- callback: delegate.method,
2148
- listener: isListener
2149
- };
2150
- let subscriptions = _classPrivateFieldGet(_listeners, this)[channel];
2151
- if (!subscriptions) {
2152
- subscriptions = {};
2153
- _classPrivateFieldGet(_listeners, this)[channel] = subscriptions;
2154
- }
2155
- subscriptions[id] = subscription;
2156
- this._debug("Added", isListener ? "listener" : "subscription", subscription);
2157
- return subscription;
2158
- }
2159
-
2160
- export { CometD };