@palantir/pack.state.foundry-event 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/.turbo/turbo-lint.log +1 -1
  2. package/.turbo/turbo-transpileBrowser.log +1 -1
  3. package/.turbo/turbo-transpileCjs.log +1 -1
  4. package/.turbo/turbo-transpileEsm.log +1 -1
  5. package/.turbo/turbo-transpileTypes.log +1 -1
  6. package/.turbo/turbo-typecheck.log +1 -1
  7. package/CHANGELOG.md +8 -0
  8. package/build/browser/AckExtension-6O7M4KVV.js +82 -0
  9. package/build/browser/AckExtension-6O7M4KVV.js.map +1 -0
  10. package/build/browser/chunk-FOM3JB26.js +18 -0
  11. package/build/browser/chunk-FOM3JB26.js.map +1 -0
  12. package/build/browser/chunk-QN5LGQFJ.js +2709 -0
  13. package/build/browser/chunk-QN5LGQFJ.js.map +1 -0
  14. package/build/browser/cometd-6RUZ7WSU.js +8 -0
  15. package/build/browser/cometd-6RUZ7WSU.js.map +1 -0
  16. package/build/browser/index.js +28 -9
  17. package/build/browser/index.js.map +1 -1
  18. package/build/cjs/AckExtension-7H4TXQGN.cjs +84 -0
  19. package/build/cjs/AckExtension-7H4TXQGN.cjs.map +1 -0
  20. package/build/cjs/chunk-CKX2CJI3.cjs +20 -0
  21. package/build/cjs/chunk-CKX2CJI3.cjs.map +1 -0
  22. package/build/cjs/chunk-GVTP4433.cjs +2711 -0
  23. package/build/cjs/chunk-GVTP4433.cjs.map +1 -0
  24. package/build/cjs/cometd-OA3JWYX6.cjs +10 -0
  25. package/build/cjs/cometd-OA3JWYX6.cjs.map +1 -0
  26. package/build/cjs/index.cjs +28 -9
  27. package/build/cjs/index.cjs.map +1 -1
  28. package/build/cjs/index.d.cts +4 -4
  29. package/build/esm/AckExtension-6O7M4KVV.js +82 -0
  30. package/build/esm/AckExtension-6O7M4KVV.js.map +1 -0
  31. package/build/esm/chunk-FOM3JB26.js +18 -0
  32. package/build/esm/chunk-FOM3JB26.js.map +1 -0
  33. package/build/esm/chunk-QN5LGQFJ.js +2709 -0
  34. package/build/esm/chunk-QN5LGQFJ.js.map +1 -0
  35. package/build/esm/cometd-6RUZ7WSU.js +8 -0
  36. package/build/esm/cometd-6RUZ7WSU.js.map +1 -0
  37. package/build/esm/index.js +28 -9
  38. package/build/esm/index.js.map +1 -1
  39. package/build/types/FoundryEventService.d.ts +4 -4
  40. package/build/types/FoundryEventService.d.ts.map +1 -1
  41. package/package.json +8 -5
  42. package/src/FoundryEventService.ts +21 -0
  43. package/src/cometd/lazyLoadCometD.ts +13 -9
@@ -0,0 +1,2709 @@
1
+ import { __commonJS, init_esm_shims } from './chunk-FOM3JB26.js';
2
+
3
+ // ../../../node_modules/.pnpm/cometd@7.0.19/node_modules/cometd/cometd.js
4
+ var require_cometd = __commonJS({
5
+ "../../../node_modules/.pnpm/cometd@7.0.19/node_modules/cometd/cometd.js"(exports, module) {
6
+ init_esm_shims();
7
+ ((root, factory) => {
8
+ if (typeof exports === "object") {
9
+ module.exports = factory();
10
+ } else if (typeof define === "function" && define.amd) {
11
+ define([], factory);
12
+ } else {
13
+ root.org = root.org || {};
14
+ root.org.cometd = factory();
15
+ }
16
+ })(exports, () => {
17
+ function Scheduler() {
18
+ let _ids = 0;
19
+ const _tasks = {};
20
+ this.register = (funktion) => {
21
+ const id = ++_ids;
22
+ _tasks[id] = funktion;
23
+ return id;
24
+ };
25
+ this.unregister = (id) => {
26
+ const funktion = _tasks[id];
27
+ delete _tasks[id];
28
+ return funktion;
29
+ };
30
+ this.setTimeout = (funktion, delay) => window.setTimeout(funktion, delay);
31
+ this.clearTimeout = (id) => {
32
+ window.clearTimeout(id);
33
+ };
34
+ }
35
+ function WorkerScheduler() {
36
+ const _tasks = {};
37
+ self.onmessage = (e) => {
38
+ const cmd = e.data;
39
+ const id = _tasks[cmd.id];
40
+ switch (cmd.type) {
41
+ case "setTimeout":
42
+ _tasks[cmd.id] = self.setTimeout(() => {
43
+ delete _tasks[cmd.id];
44
+ self.postMessage({
45
+ id: cmd.id
46
+ });
47
+ }, cmd.delay);
48
+ break;
49
+ case "clearTimeout":
50
+ delete _tasks[cmd.id];
51
+ if (id) {
52
+ self.clearTimeout(id);
53
+ }
54
+ break;
55
+ default:
56
+ throw "Unknown command " + cmd.type;
57
+ }
58
+ };
59
+ }
60
+ const Utils = {
61
+ isString: (value) => {
62
+ if (value === void 0 || value === null) {
63
+ return false;
64
+ }
65
+ return typeof value === "string" || value instanceof String;
66
+ }
67
+ };
68
+ function TransportRegistry() {
69
+ let _types = [];
70
+ let _transports = {};
71
+ this.getTransportTypes = () => _types.slice(0);
72
+ this.findTransportTypes = (version, crossDomain, url) => {
73
+ const result = [];
74
+ for (let i = 0; i < _types.length; ++i) {
75
+ const type = _types[i];
76
+ if (_transports[type].accept(version, crossDomain, url) === true) {
77
+ result.push(type);
78
+ }
79
+ }
80
+ return result;
81
+ };
82
+ this.negotiateTransport = (types, version, crossDomain, url) => {
83
+ for (let i = 0; i < _types.length; ++i) {
84
+ const type = _types[i];
85
+ for (let j = 0; j < types.length; ++j) {
86
+ if (type === types[j]) {
87
+ const transport = _transports[type];
88
+ if (transport.accept(version, crossDomain, url) === true) {
89
+ return transport;
90
+ }
91
+ }
92
+ }
93
+ }
94
+ return null;
95
+ };
96
+ this.add = (type, transport, index) => {
97
+ let existing = false;
98
+ for (let i = 0; i < _types.length; ++i) {
99
+ if (_types[i] === type) {
100
+ existing = true;
101
+ break;
102
+ }
103
+ }
104
+ if (!existing) {
105
+ if (typeof index !== "number") {
106
+ _types.push(type);
107
+ } else {
108
+ _types.splice(index, 0, type);
109
+ }
110
+ _transports[type] = transport;
111
+ }
112
+ return !existing;
113
+ };
114
+ this.find = (type) => {
115
+ for (let i = 0; i < _types.length; ++i) {
116
+ if (_types[i] === type) {
117
+ return _transports[type];
118
+ }
119
+ }
120
+ return null;
121
+ };
122
+ this.remove = (type) => {
123
+ for (let i = 0; i < _types.length; ++i) {
124
+ if (_types[i] === type) {
125
+ _types.splice(i, 1);
126
+ const transport = _transports[type];
127
+ delete _transports[type];
128
+ return transport;
129
+ }
130
+ }
131
+ return null;
132
+ };
133
+ this.clear = () => {
134
+ _types = [];
135
+ _transports = {};
136
+ };
137
+ this.reset = (init) => {
138
+ for (let i = 0; i < _types.length; ++i) {
139
+ _transports[_types[i]].reset(init);
140
+ }
141
+ };
142
+ }
143
+ function Transport() {
144
+ let _type;
145
+ let _cometd;
146
+ let _url;
147
+ this.registered = (type, cometd) => {
148
+ _type = type;
149
+ _cometd = cometd;
150
+ };
151
+ this.unregistered = () => {
152
+ _type = null;
153
+ _cometd = null;
154
+ };
155
+ this._notifyTransportTimeout = function(messages) {
156
+ const callbacks = _cometd._getTransportListeners("timeout");
157
+ if (callbacks) {
158
+ for (let i = 0; i < callbacks.length; ++i) {
159
+ const listener = callbacks[i];
160
+ try {
161
+ const result = listener.call(this, messages);
162
+ if (typeof result === "number" && result > 0) {
163
+ return result;
164
+ }
165
+ } catch (x) {
166
+ this._info("Exception during execution of transport listener", listener, x);
167
+ }
168
+ }
169
+ }
170
+ return 0;
171
+ };
172
+ this._debug = function() {
173
+ _cometd._debug.apply(_cometd, arguments);
174
+ };
175
+ this._info = function() {
176
+ _cometd._info.apply(_cometd, arguments);
177
+ };
178
+ this._mixin = function() {
179
+ return _cometd._mixin.apply(_cometd, arguments);
180
+ };
181
+ this.getConfiguration = () => _cometd.getConfiguration();
182
+ this.getAdvice = () => _cometd.getAdvice();
183
+ this.setTimeout = (funktion, delay) => _cometd.setTimeout(funktion, delay);
184
+ this.clearTimeout = (id) => {
185
+ _cometd.clearTimeout(id);
186
+ };
187
+ this.convertToJSON = function(messages) {
188
+ const maxSize = this.getConfiguration().maxSendBayeuxMessageSize;
189
+ let result = "[";
190
+ for (let i = 0; i < messages.length; ++i) {
191
+ if (i > 0) {
192
+ result += ",";
193
+ }
194
+ const message = messages[i];
195
+ const json = JSON.stringify(message);
196
+ if (json.length > maxSize) {
197
+ throw "maxSendBayeuxMessageSize " + maxSize + " exceeded";
198
+ }
199
+ result += json;
200
+ }
201
+ result += "]";
202
+ return result;
203
+ };
204
+ this.convertToMessages = function(response) {
205
+ if (Utils.isString(response)) {
206
+ try {
207
+ return JSON.parse(response);
208
+ } catch (x) {
209
+ this._debug("Could not convert to JSON the following string", '"' + response + '"');
210
+ throw x;
211
+ }
212
+ }
213
+ if (Array.isArray(response)) {
214
+ return response;
215
+ }
216
+ if (response === void 0 || response === null) {
217
+ return [];
218
+ }
219
+ if (response instanceof Object) {
220
+ return [response];
221
+ }
222
+ throw "Conversion Error " + response + ", typeof " + typeof response;
223
+ };
224
+ this.accept = (version, crossDomain, url) => {
225
+ throw "Abstract";
226
+ };
227
+ this.getType = () => _type;
228
+ this.getURL = () => _url;
229
+ this.setURL = (url) => {
230
+ _url = url;
231
+ };
232
+ this.send = (envelope, metaConnect) => {
233
+ throw "Abstract";
234
+ };
235
+ this.reset = function(init) {
236
+ this._debug("Transport", _type, "reset", init ? "initial" : "retry");
237
+ };
238
+ this.abort = function() {
239
+ this._debug("Transport", _type, "aborted");
240
+ };
241
+ this.toString = function() {
242
+ return this.getType();
243
+ };
244
+ }
245
+ Transport.derive = (baseObject) => {
246
+ function F() {
247
+ }
248
+ F.prototype = baseObject;
249
+ return new F();
250
+ };
251
+ function RequestTransport() {
252
+ const _super = new Transport();
253
+ const _self = Transport.derive(_super);
254
+ let _requestIds = 0;
255
+ let _metaConnectRequest = null;
256
+ let _requests = [];
257
+ let _envelopes = [];
258
+ function _coalesceEnvelopes(envelope) {
259
+ while (_envelopes.length > 0) {
260
+ const envelopeAndRequest = _envelopes[0];
261
+ const newEnvelope = envelopeAndRequest[0];
262
+ const newRequest = envelopeAndRequest[1];
263
+ if (newEnvelope.url === envelope.url && newEnvelope.sync === envelope.sync) {
264
+ _envelopes.shift();
265
+ envelope.messages = envelope.messages.concat(newEnvelope.messages);
266
+ this._debug("Coalesced", newEnvelope.messages.length, "messages from request", newRequest.id);
267
+ continue;
268
+ }
269
+ break;
270
+ }
271
+ }
272
+ function _onTransportTimeout(envelope, request, delay) {
273
+ const result = this._notifyTransportTimeout(envelope.messages);
274
+ if (result > 0) {
275
+ this._debug("Transport", this.getType(), "extended waiting for message replies of request", request.id, ":", result, "ms");
276
+ request.timeout = this.setTimeout(() => {
277
+ _onTransportTimeout.call(this, envelope, request, delay + result);
278
+ }, result);
279
+ } else {
280
+ request.expired = true;
281
+ const errorMessage = "Transport " + this.getType() + " expired waiting for message replies of request " + request.id + ": " + delay + " ms";
282
+ const failure = {
283
+ reason: errorMessage
284
+ };
285
+ const xhr = request.xhr;
286
+ failure.httpCode = this.xhrStatus(xhr);
287
+ this.abortXHR(xhr);
288
+ this._debug(errorMessage);
289
+ this.complete(request, false, request.metaConnect);
290
+ envelope.onFailure(xhr, envelope.messages, failure);
291
+ }
292
+ }
293
+ function _transportSend(envelope, request) {
294
+ if (this.transportSend(envelope, request)) {
295
+ request.expired = false;
296
+ if (!envelope.sync) {
297
+ let delay = this.getConfiguration().maxNetworkDelay;
298
+ if (request.metaConnect === true) {
299
+ delay += this.getAdvice().timeout;
300
+ }
301
+ this._debug("Transport", this.getType(), "started waiting for message replies of request", request.id, ":", delay, "ms");
302
+ request.timeout = this.setTimeout(() => {
303
+ _onTransportTimeout.call(this, envelope, request, delay);
304
+ }, delay);
305
+ }
306
+ }
307
+ }
308
+ function _queueSend(envelope) {
309
+ const requestId = ++_requestIds;
310
+ const request = {
311
+ id: requestId,
312
+ metaConnect: false,
313
+ envelope
314
+ };
315
+ if (_requests.length < this.getConfiguration().maxConnections - 1) {
316
+ _requests.push(request);
317
+ _transportSend.call(this, envelope, request);
318
+ } else {
319
+ this._debug("Transport", this.getType(), "queueing request", requestId, "envelope", envelope);
320
+ _envelopes.push([envelope, request]);
321
+ }
322
+ }
323
+ function _metaConnectComplete(request) {
324
+ const requestId = request.id;
325
+ this._debug("Transport", this.getType(), "/meta/connect complete, request", requestId);
326
+ if (_metaConnectRequest !== null && _metaConnectRequest.id !== requestId) {
327
+ throw "/meta/connect request mismatch, completing request " + requestId;
328
+ }
329
+ _metaConnectRequest = null;
330
+ }
331
+ function _complete(request, success) {
332
+ const index = _requests.indexOf(request);
333
+ if (index >= 0) {
334
+ _requests.splice(index, 1);
335
+ }
336
+ if (_envelopes.length > 0) {
337
+ const envelopeAndRequest = _envelopes.shift();
338
+ const nextEnvelope = envelopeAndRequest[0];
339
+ const nextRequest = envelopeAndRequest[1];
340
+ this._debug("Transport dequeued request", nextRequest.id);
341
+ if (success) {
342
+ if (this.getConfiguration().autoBatch) {
343
+ _coalesceEnvelopes.call(this, nextEnvelope);
344
+ }
345
+ _queueSend.call(this, nextEnvelope);
346
+ this._debug("Transport completed request", request.id, nextEnvelope);
347
+ } else {
348
+ this.setTimeout(() => {
349
+ this.complete(nextRequest, false, nextRequest.metaConnect);
350
+ const failure = {
351
+ reason: "Previous request failed"
352
+ };
353
+ const xhr = nextRequest.xhr;
354
+ failure.httpCode = this.xhrStatus(xhr);
355
+ nextEnvelope.onFailure(xhr, nextEnvelope.messages, failure);
356
+ }, 0);
357
+ }
358
+ }
359
+ }
360
+ _self.complete = function(request, success, metaConnect) {
361
+ if (metaConnect) {
362
+ _metaConnectComplete.call(this, request);
363
+ } else {
364
+ _complete.call(this, request, success);
365
+ }
366
+ };
367
+ _self.transportSend = (envelope, request) => {
368
+ throw "Abstract";
369
+ };
370
+ _self.transportSuccess = function(envelope, request, responses) {
371
+ if (!request.expired) {
372
+ this.clearTimeout(request.timeout);
373
+ this._debug("Transport", this.getType(), "cancelled waiting for message replies");
374
+ this.complete(request, true, request.metaConnect);
375
+ if (responses && responses.length > 0) {
376
+ envelope.onSuccess(responses);
377
+ } else {
378
+ envelope.onFailure(request.xhr, envelope.messages, {
379
+ httpCode: 204
380
+ });
381
+ }
382
+ }
383
+ };
384
+ _self.transportFailure = function(envelope, request, failure) {
385
+ if (!request.expired) {
386
+ this.clearTimeout(request.timeout);
387
+ this._debug("Transport", this.getType(), "cancelled waiting for failed message replies");
388
+ this.complete(request, false, request.metaConnect);
389
+ envelope.onFailure(request.xhr, envelope.messages, failure);
390
+ }
391
+ };
392
+ function _metaConnectSend(envelope) {
393
+ if (_metaConnectRequest !== null) {
394
+ throw "Concurrent /meta/connect requests not allowed, request id=" + _metaConnectRequest.id + " not yet completed";
395
+ }
396
+ const requestId = ++_requestIds;
397
+ this._debug("Transport", this.getType(), "/meta/connect send, request", requestId, "envelope", envelope);
398
+ const request = {
399
+ id: requestId,
400
+ metaConnect: true,
401
+ envelope
402
+ };
403
+ _transportSend.call(this, envelope, request);
404
+ _metaConnectRequest = request;
405
+ }
406
+ _self.send = function(envelope, metaConnect) {
407
+ if (metaConnect) {
408
+ _metaConnectSend.call(this, envelope);
409
+ } else {
410
+ _queueSend.call(this, envelope);
411
+ }
412
+ };
413
+ _self.abort = function() {
414
+ _super.abort();
415
+ for (let i = 0; i < _requests.length; ++i) {
416
+ const request = _requests[i];
417
+ if (request) {
418
+ this._debug("Aborting request", request);
419
+ if (!this.abortXHR(request.xhr)) {
420
+ this.transportFailure(request.envelope, request, {
421
+ reason: "abort"
422
+ });
423
+ }
424
+ }
425
+ }
426
+ const metaConnectRequest = _metaConnectRequest;
427
+ if (metaConnectRequest) {
428
+ this._debug("Aborting /meta/connect request", metaConnectRequest);
429
+ if (!this.abortXHR(metaConnectRequest.xhr)) {
430
+ this.transportFailure(metaConnectRequest.envelope, metaConnectRequest, {
431
+ reason: "abort"
432
+ });
433
+ }
434
+ }
435
+ this.reset(true);
436
+ };
437
+ _self.reset = (init) => {
438
+ _super.reset(init);
439
+ _metaConnectRequest = null;
440
+ _requests = [];
441
+ _envelopes = [];
442
+ };
443
+ _self.abortXHR = function(xhr) {
444
+ if (xhr) {
445
+ try {
446
+ const state = xhr.readyState;
447
+ xhr.abort();
448
+ return state !== window.XMLHttpRequest.UNSENT;
449
+ } catch (x) {
450
+ this._debug(x);
451
+ }
452
+ }
453
+ return false;
454
+ };
455
+ _self.xhrStatus = function(xhr) {
456
+ if (xhr) {
457
+ try {
458
+ return xhr.status;
459
+ } catch (x) {
460
+ this._debug(x);
461
+ }
462
+ }
463
+ return -1;
464
+ };
465
+ return _self;
466
+ }
467
+ function LongPollingTransport() {
468
+ const _super = new RequestTransport();
469
+ const _self = Transport.derive(_super);
470
+ let _supportsCrossDomain = true;
471
+ _self.accept = (version, crossDomain, url) => _supportsCrossDomain || !crossDomain;
472
+ _self.newXMLHttpRequest = () => new window.XMLHttpRequest();
473
+ function _copyContext(xhr) {
474
+ try {
475
+ xhr.context = _self.context;
476
+ } catch (e) {
477
+ _self._debug("Could not copy transport context into XHR", e);
478
+ }
479
+ }
480
+ _self.xhrSend = (packet) => {
481
+ const xhr = _self.newXMLHttpRequest();
482
+ _copyContext(xhr);
483
+ xhr.withCredentials = true;
484
+ xhr.open("POST", packet.url, packet.sync !== true);
485
+ const headers = packet.headers;
486
+ if (headers) {
487
+ for (let headerName in headers) {
488
+ if (headers.hasOwnProperty(headerName)) {
489
+ xhr.setRequestHeader(headerName, headers[headerName]);
490
+ }
491
+ }
492
+ }
493
+ xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
494
+ xhr.onload = () => {
495
+ if (xhr.status === 200) {
496
+ packet.onSuccess(xhr.responseText);
497
+ } else {
498
+ packet.onError(xhr.statusText);
499
+ }
500
+ };
501
+ xhr.onabort = xhr.onerror = () => {
502
+ packet.onError(xhr.statusText);
503
+ };
504
+ xhr.send(packet.body);
505
+ return xhr;
506
+ };
507
+ _self.transportSend = function(envelope, request) {
508
+ this._debug("Transport", this.getType(), "sending request", request.id, "envelope", envelope);
509
+ try {
510
+ let sameStack = true;
511
+ request.xhr = this.xhrSend({
512
+ transport: this,
513
+ url: envelope.url,
514
+ sync: envelope.sync,
515
+ headers: this.getConfiguration().requestHeaders,
516
+ body: this.convertToJSON(envelope.messages),
517
+ onSuccess: (response) => {
518
+ this._debug("Transport", this.getType(), "received response", response);
519
+ let success = false;
520
+ try {
521
+ const received = this.convertToMessages(response);
522
+ if (received.length === 0) {
523
+ _supportsCrossDomain = false;
524
+ this.transportFailure(envelope, request, {
525
+ httpCode: 204
526
+ });
527
+ } else {
528
+ success = true;
529
+ this.transportSuccess(envelope, request, received);
530
+ }
531
+ } catch (x) {
532
+ this._debug(x);
533
+ if (!success) {
534
+ _supportsCrossDomain = false;
535
+ const failure = {
536
+ exception: x
537
+ };
538
+ failure.httpCode = this.xhrStatus(request.xhr);
539
+ this.transportFailure(envelope, request, failure);
540
+ }
541
+ }
542
+ },
543
+ onError: (reason, exception) => {
544
+ this._debug("Transport", this.getType(), "received error", reason, exception);
545
+ _supportsCrossDomain = false;
546
+ const failure = {
547
+ reason,
548
+ exception
549
+ };
550
+ failure.httpCode = this.xhrStatus(request.xhr);
551
+ if (sameStack) {
552
+ this.setTimeout(() => {
553
+ this.transportFailure(envelope, request, failure);
554
+ }, 0);
555
+ } else {
556
+ this.transportFailure(envelope, request, failure);
557
+ }
558
+ }
559
+ });
560
+ sameStack = false;
561
+ return true;
562
+ } catch (x) {
563
+ this._debug("Transport", this.getType(), "exception:", x);
564
+ _supportsCrossDomain = false;
565
+ this.setTimeout(() => {
566
+ this.transportFailure(envelope, request, {
567
+ exception: x
568
+ });
569
+ }, 0);
570
+ return false;
571
+ }
572
+ };
573
+ _self.reset = (init) => {
574
+ _super.reset(init);
575
+ _supportsCrossDomain = true;
576
+ };
577
+ return _self;
578
+ }
579
+ function CallbackPollingTransport() {
580
+ const _super = new RequestTransport();
581
+ const _self = Transport.derive(_super);
582
+ let jsonp = 0;
583
+ _self.accept = (version, crossDomain, url) => true;
584
+ _self.jsonpSend = (packet) => {
585
+ const head = document.getElementsByTagName("head")[0];
586
+ const script = document.createElement("script");
587
+ const callbackName = "_cometd_jsonp_" + jsonp++;
588
+ window[callbackName] = (responseText) => {
589
+ head.removeChild(script);
590
+ delete window[callbackName];
591
+ packet.onSuccess(responseText);
592
+ };
593
+ let url = packet.url;
594
+ url += url.indexOf("?") < 0 ? "?" : "&";
595
+ url += "jsonp=" + callbackName;
596
+ url += "&message=" + encodeURIComponent(packet.body);
597
+ script.src = url;
598
+ script.async = packet.sync !== true;
599
+ script.type = "application/javascript";
600
+ script.onerror = (e) => {
601
+ packet.onError("jsonp " + e.type);
602
+ };
603
+ head.appendChild(script);
604
+ };
605
+ function _failTransportFn(envelope, request, x) {
606
+ return () => {
607
+ this.transportFailure(envelope, request, "error", x);
608
+ };
609
+ }
610
+ _self.transportSend = function(envelope, request) {
611
+ let start = 0;
612
+ let length = envelope.messages.length;
613
+ const lengths = [];
614
+ while (length > 0) {
615
+ const json = JSON.stringify(envelope.messages.slice(start, start + length));
616
+ const urlLength = envelope.url.length + encodeURI(json).length;
617
+ const maxLength = this.getConfiguration().maxURILength;
618
+ if (urlLength > maxLength) {
619
+ if (length === 1) {
620
+ const x = "Bayeux message too big (" + urlLength + " bytes, max is " + maxLength + ") for transport " + this.getType();
621
+ this.setTimeout(_failTransportFn.call(this, envelope, request, x), 0);
622
+ return;
623
+ }
624
+ --length;
625
+ continue;
626
+ }
627
+ lengths.push(length);
628
+ start += length;
629
+ length = envelope.messages.length - start;
630
+ }
631
+ let envelopeToSend = envelope;
632
+ if (lengths.length > 1) {
633
+ let begin = 0;
634
+ let end = lengths[0];
635
+ this._debug("Transport", this.getType(), "split", envelope.messages.length, "messages into", lengths.join(" + "));
636
+ envelopeToSend = this._mixin(false, {}, envelope);
637
+ envelopeToSend.messages = envelope.messages.slice(begin, end);
638
+ envelopeToSend.onSuccess = envelope.onSuccess;
639
+ envelopeToSend.onFailure = envelope.onFailure;
640
+ for (let i = 1; i < lengths.length; ++i) {
641
+ const nextEnvelope = this._mixin(false, {}, envelope);
642
+ begin = end;
643
+ end += lengths[i];
644
+ nextEnvelope.messages = envelope.messages.slice(begin, end);
645
+ nextEnvelope.onSuccess = envelope.onSuccess;
646
+ nextEnvelope.onFailure = envelope.onFailure;
647
+ this.send(nextEnvelope, request.metaConnect);
648
+ }
649
+ }
650
+ this._debug("Transport", this.getType(), "sending request", request.id, "envelope", envelopeToSend);
651
+ try {
652
+ let sameStack = true;
653
+ this.jsonpSend({
654
+ transport: this,
655
+ url: envelopeToSend.url,
656
+ sync: envelopeToSend.sync,
657
+ headers: this.getConfiguration().requestHeaders,
658
+ body: JSON.stringify(envelopeToSend.messages),
659
+ onSuccess: (responses) => {
660
+ let success = false;
661
+ try {
662
+ const received = this.convertToMessages(responses);
663
+ if (received.length === 0) {
664
+ this.transportFailure(envelopeToSend, request, {
665
+ httpCode: 204
666
+ });
667
+ } else {
668
+ success = true;
669
+ this.transportSuccess(envelopeToSend, request, received);
670
+ }
671
+ } catch (x) {
672
+ this._debug(x);
673
+ if (!success) {
674
+ this.transportFailure(envelopeToSend, request, {
675
+ exception: x
676
+ });
677
+ }
678
+ }
679
+ },
680
+ onError: (reason, exception) => {
681
+ const failure = {
682
+ reason,
683
+ exception
684
+ };
685
+ if (sameStack) {
686
+ this.setTimeout(() => {
687
+ this.transportFailure(envelopeToSend, request, failure);
688
+ }, 0);
689
+ } else {
690
+ this.transportFailure(envelopeToSend, request, failure);
691
+ }
692
+ }
693
+ });
694
+ sameStack = false;
695
+ return true;
696
+ } catch (xx) {
697
+ this.setTimeout(() => {
698
+ this.transportFailure(envelopeToSend, request, {
699
+ exception: xx
700
+ });
701
+ }, 0);
702
+ return false;
703
+ }
704
+ };
705
+ return _self;
706
+ }
707
+ function WebSocketTransport() {
708
+ const _super = new Transport();
709
+ const _self = Transport.derive(_super);
710
+ let _cometd;
711
+ let _webSocketSupported = true;
712
+ let _webSocketConnected = false;
713
+ let _stickyReconnect = true;
714
+ let _context = null;
715
+ let _connecting = null;
716
+ let _connected = false;
717
+ let _successCallback = null;
718
+ _self.reset = (init) => {
719
+ _super.reset(init);
720
+ _webSocketSupported = true;
721
+ if (init) {
722
+ _webSocketConnected = false;
723
+ }
724
+ _stickyReconnect = true;
725
+ if (init) {
726
+ _context = null;
727
+ }
728
+ _connecting = null;
729
+ _connected = false;
730
+ };
731
+ function _forceClose(context, event) {
732
+ if (context) {
733
+ this.webSocketClose(context, event.code, event.reason);
734
+ this.onClose(context, event);
735
+ }
736
+ }
737
+ function _sameContext(context) {
738
+ return context === _connecting || context === _context;
739
+ }
740
+ function _storeEnvelope(context, envelope, metaConnect) {
741
+ const messageIds = [];
742
+ for (let i = 0; i < envelope.messages.length; ++i) {
743
+ const message = envelope.messages[i];
744
+ if (message.id) {
745
+ messageIds.push(message.id);
746
+ }
747
+ }
748
+ context.envelopes[messageIds.join(",")] = [envelope, metaConnect];
749
+ this._debug("Transport", this.getType(), "stored envelope, envelopes", context.envelopes);
750
+ }
751
+ function _removeEnvelope(context, messageIds) {
752
+ let removed = false;
753
+ const envelopes = context.envelopes;
754
+ for (let j = 0; j < messageIds.length; ++j) {
755
+ const id = messageIds[j];
756
+ for (let key in envelopes) {
757
+ if (envelopes.hasOwnProperty(key)) {
758
+ const ids = key.split(",");
759
+ const index = ids.indexOf(id);
760
+ if (index >= 0) {
761
+ removed = true;
762
+ ids.splice(index, 1);
763
+ const envelope = envelopes[key][0];
764
+ const metaConnect = envelopes[key][1];
765
+ delete envelopes[key];
766
+ if (ids.length > 0) {
767
+ envelopes[ids.join(",")] = [envelope, metaConnect];
768
+ }
769
+ break;
770
+ }
771
+ }
772
+ }
773
+ }
774
+ if (removed) {
775
+ this._debug("Transport", this.getType(), "removed envelope, envelopes", envelopes);
776
+ }
777
+ }
778
+ function _websocketConnect(context) {
779
+ if (_connecting) {
780
+ return;
781
+ }
782
+ const url = _cometd.getURL().replace(/^http/, "ws");
783
+ this._debug("Transport", this.getType(), "connecting to URL", url);
784
+ try {
785
+ const protocol = _cometd.getConfiguration().protocol;
786
+ context.webSocket = protocol ? new window.WebSocket(url, protocol) : new window.WebSocket(url);
787
+ _connecting = context;
788
+ } catch (x) {
789
+ _webSocketSupported = false;
790
+ this._debug("Exception while creating WebSocket object", x);
791
+ throw x;
792
+ }
793
+ _stickyReconnect = _cometd.getConfiguration().stickyReconnect !== false;
794
+ const connectTimeout = _cometd.getConfiguration().connectTimeout;
795
+ if (connectTimeout > 0) {
796
+ context.connectTimer = this.setTimeout(() => {
797
+ _cometd._debug("Transport", this.getType(), "timed out while connecting to URL", url, ":", connectTimeout, "ms");
798
+ _forceClose.call(this, context, {
799
+ code: 1e3,
800
+ reason: "Connect Timeout"
801
+ });
802
+ }, connectTimeout);
803
+ }
804
+ const onopen = () => {
805
+ _cometd._debug("WebSocket onopen", context);
806
+ if (context.connectTimer) {
807
+ this.clearTimeout(context.connectTimer);
808
+ }
809
+ if (_sameContext(context)) {
810
+ _connecting = null;
811
+ _context = context;
812
+ _webSocketConnected = true;
813
+ this.onOpen(context);
814
+ } else {
815
+ _cometd._warn("Closing extra WebSocket connection", this, "active connection", _context);
816
+ _forceClose.call(this, context, {
817
+ code: 1e3,
818
+ reason: "Extra Connection"
819
+ });
820
+ }
821
+ };
822
+ const onclose = (event) => {
823
+ event = event || {
824
+ code: 1e3
825
+ };
826
+ _cometd._debug("WebSocket onclose", context, event, "connecting", _connecting, "current", _context);
827
+ if (context.connectTimer) {
828
+ this.clearTimeout(context.connectTimer);
829
+ }
830
+ this.onClose(context, event);
831
+ };
832
+ const onmessage = (wsMessage) => {
833
+ _cometd._debug("WebSocket onmessage", wsMessage, context);
834
+ this.onMessage(context, wsMessage);
835
+ };
836
+ context.webSocket.onopen = onopen;
837
+ context.webSocket.onclose = onclose;
838
+ context.webSocket.onerror = () => {
839
+ onclose({
840
+ code: 1e3,
841
+ reason: "Error"
842
+ });
843
+ };
844
+ context.webSocket.onmessage = onmessage;
845
+ this._debug("Transport", this.getType(), "configured callbacks on", context);
846
+ }
847
+ function _onTransportTimeout(context, message, delay) {
848
+ const result = this._notifyTransportTimeout([message]);
849
+ if (result > 0) {
850
+ this._debug("Transport", this.getType(), "extended waiting for message replies:", result, "ms");
851
+ context.timeouts[message.id] = this.setTimeout(() => {
852
+ _onTransportTimeout.call(this, context, message, delay + result);
853
+ }, result);
854
+ } else {
855
+ this._debug("Transport", this.getType(), "expired waiting for message reply", message.id, ":", delay, "ms");
856
+ _forceClose.call(this, context, {
857
+ code: 1e3,
858
+ reason: "Message Timeout"
859
+ });
860
+ }
861
+ }
862
+ function _webSocketSend(context, envelope, metaConnect) {
863
+ let json;
864
+ try {
865
+ json = this.convertToJSON(envelope.messages);
866
+ } catch (x) {
867
+ this._debug("Transport", this.getType(), "exception:", x);
868
+ const mIds = [];
869
+ for (let j = 0; j < envelope.messages.length; ++j) {
870
+ const m = envelope.messages[j];
871
+ mIds.push(m.id);
872
+ }
873
+ _removeEnvelope.call(this, context, mIds);
874
+ this.setTimeout(() => {
875
+ this._notifyFailure(envelope.onFailure, context, envelope.messages, {
876
+ exception: x
877
+ });
878
+ }, 0);
879
+ return;
880
+ }
881
+ context.webSocket.send(json);
882
+ this._debug("Transport", this.getType(), "sent", envelope, "/meta/connect =", metaConnect);
883
+ let delay = this.getConfiguration().maxNetworkDelay;
884
+ if (metaConnect) {
885
+ delay += this.getAdvice().timeout;
886
+ _connected = true;
887
+ }
888
+ const messageIds = [];
889
+ for (let i = 0; i < envelope.messages.length; ++i) {
890
+ const message = envelope.messages[i];
891
+ if (message.id) {
892
+ messageIds.push(message.id);
893
+ context.timeouts[message.id] = this.setTimeout(() => {
894
+ _onTransportTimeout.call(this, context, message, delay);
895
+ }, delay);
896
+ }
897
+ }
898
+ this._debug("Transport", this.getType(), "started waiting for message replies", delay, "ms, messageIds:", messageIds, ", timeouts:", context.timeouts);
899
+ }
900
+ _self._notifySuccess = function(fn, messages) {
901
+ fn.call(this, messages);
902
+ };
903
+ _self._notifyFailure = function(fn, context, messages, failure) {
904
+ fn.call(this, context, messages, failure);
905
+ };
906
+ function _send(context, envelope, metaConnect) {
907
+ try {
908
+ if (context === null) {
909
+ context = _connecting || {
910
+ envelopes: {},
911
+ timeouts: {}
912
+ };
913
+ _storeEnvelope.call(this, context, envelope, metaConnect);
914
+ _websocketConnect.call(this, context);
915
+ } else {
916
+ _storeEnvelope.call(this, context, envelope, metaConnect);
917
+ _webSocketSend.call(this, context, envelope, metaConnect);
918
+ }
919
+ } catch (x) {
920
+ this.setTimeout(() => {
921
+ _forceClose.call(this, context, {
922
+ code: 1e3,
923
+ reason: "Exception",
924
+ exception: x
925
+ });
926
+ }, 0);
927
+ }
928
+ }
929
+ _self.onOpen = function(context) {
930
+ const envelopes = context.envelopes;
931
+ this._debug("Transport", this.getType(), "opened", context, "pending messages", envelopes);
932
+ for (let key in envelopes) {
933
+ if (envelopes.hasOwnProperty(key)) {
934
+ const element = envelopes[key];
935
+ const envelope = element[0];
936
+ const metaConnect = element[1];
937
+ _successCallback = envelope.onSuccess;
938
+ _webSocketSend.call(this, context, envelope, metaConnect);
939
+ }
940
+ }
941
+ };
942
+ _self.onMessage = function(context, wsMessage) {
943
+ this._debug("Transport", this.getType(), "received websocket message", wsMessage, context);
944
+ let close = false;
945
+ const messages = this.convertToMessages(wsMessage.data);
946
+ const messageIds = [];
947
+ for (let i = 0; i < messages.length; ++i) {
948
+ const message = messages[i];
949
+ if (/^\/meta\//.test(message.channel) || message.data === void 0) {
950
+ if (message.id) {
951
+ messageIds.push(message.id);
952
+ const timeout = context.timeouts[message.id];
953
+ if (timeout) {
954
+ this.clearTimeout(timeout);
955
+ delete context.timeouts[message.id];
956
+ this._debug("Transport", this.getType(), "removed timeout for message", message.id, ", timeouts", context.timeouts);
957
+ }
958
+ }
959
+ }
960
+ if ("/meta/connect" === message.channel) {
961
+ _connected = false;
962
+ }
963
+ if ("/meta/disconnect" === message.channel && !_connected) {
964
+ close = true;
965
+ }
966
+ }
967
+ _removeEnvelope.call(this, context, messageIds);
968
+ this._notifySuccess(_successCallback, messages);
969
+ if (close) {
970
+ this.webSocketClose(context, 1e3, "Disconnect");
971
+ }
972
+ };
973
+ _self.onClose = function(context, event) {
974
+ this._debug("Transport", this.getType(), "closed", context, event);
975
+ if (_sameContext(context)) {
976
+ _webSocketSupported = _stickyReconnect && _webSocketConnected;
977
+ _connecting = null;
978
+ _context = null;
979
+ }
980
+ const timeouts = context.timeouts;
981
+ context.timeouts = {};
982
+ for (let id in timeouts) {
983
+ if (timeouts.hasOwnProperty(id)) {
984
+ this.clearTimeout(timeouts[id]);
985
+ }
986
+ }
987
+ const envelopes = context.envelopes;
988
+ context.envelopes = {};
989
+ for (let key in envelopes) {
990
+ if (envelopes.hasOwnProperty(key)) {
991
+ const envelope = envelopes[key][0];
992
+ const metaConnect = envelopes[key][1];
993
+ if (metaConnect) {
994
+ _connected = false;
995
+ }
996
+ const failure = {
997
+ websocketCode: event.code,
998
+ reason: event.reason
999
+ };
1000
+ if (event.exception) {
1001
+ failure.exception = event.exception;
1002
+ }
1003
+ this._notifyFailure(envelope.onFailure, context, envelope.messages, failure);
1004
+ }
1005
+ }
1006
+ };
1007
+ _self.registered = (type, cometd) => {
1008
+ _super.registered(type, cometd);
1009
+ _cometd = cometd;
1010
+ };
1011
+ _self.accept = function(version, crossDomain, url) {
1012
+ this._debug("Transport", this.getType(), "accept, supported:", _webSocketSupported);
1013
+ return _webSocketSupported && !!window.WebSocket && _cometd.websocketEnabled !== false;
1014
+ };
1015
+ _self.send = function(envelope, metaConnect) {
1016
+ this._debug("Transport", this.getType(), "sending", envelope, "/meta/connect =", metaConnect);
1017
+ _send.call(this, _context, envelope, metaConnect);
1018
+ };
1019
+ _self.webSocketClose = function(context, code, reason) {
1020
+ try {
1021
+ if (context.webSocket) {
1022
+ context.webSocket.close(code, reason);
1023
+ }
1024
+ } catch (x) {
1025
+ this._debug(x);
1026
+ }
1027
+ };
1028
+ _self.abort = function() {
1029
+ _super.abort();
1030
+ _forceClose.call(this, _context, {
1031
+ code: 1e3,
1032
+ reason: "Abort"
1033
+ });
1034
+ this.reset(true);
1035
+ };
1036
+ return _self;
1037
+ }
1038
+ function CometD(name) {
1039
+ const _scheduler = new Scheduler();
1040
+ const _cometd = this;
1041
+ const _name = name || "default";
1042
+ let _crossDomain = false;
1043
+ const _transports = new TransportRegistry();
1044
+ let _transport;
1045
+ let _status = "disconnected";
1046
+ let _messageId = 0;
1047
+ let _clientId = null;
1048
+ let _batch = 0;
1049
+ let _messageQueue = [];
1050
+ let _internalBatch = false;
1051
+ let _listenerId = 0;
1052
+ let _listeners = {};
1053
+ const _transportListeners = {};
1054
+ let _backoff = 0;
1055
+ let _scheduledSend = null;
1056
+ const _extensions = [];
1057
+ let _advice = {};
1058
+ let _handshakeProps;
1059
+ let _handshakeCallback;
1060
+ const _callbacks = {};
1061
+ const _remoteCalls = {};
1062
+ let _reestablish = false;
1063
+ let _connected = false;
1064
+ let _unconnectTime = 0;
1065
+ let _handshakeMessages = 0;
1066
+ let _metaConnect = null;
1067
+ let _config = {
1068
+ useWorkerScheduler: true,
1069
+ protocol: null,
1070
+ stickyReconnect: true,
1071
+ connectTimeout: 0,
1072
+ maxConnections: 2,
1073
+ backoffIncrement: 1e3,
1074
+ maxBackoff: 6e4,
1075
+ logLevel: "info",
1076
+ maxNetworkDelay: 1e4,
1077
+ requestHeaders: {},
1078
+ appendMessageTypeToURL: true,
1079
+ autoBatch: false,
1080
+ urls: {},
1081
+ maxURILength: 2e3,
1082
+ maxSendBayeuxMessageSize: 8192,
1083
+ advice: {
1084
+ timeout: 6e4,
1085
+ interval: 0,
1086
+ reconnect: void 0,
1087
+ maxInterval: 0
1088
+ }
1089
+ };
1090
+ function _fieldValue(object, name2) {
1091
+ try {
1092
+ return object[name2];
1093
+ } catch (x) {
1094
+ return void 0;
1095
+ }
1096
+ }
1097
+ this._mixin = function(deep, target, objects) {
1098
+ const result = target || {};
1099
+ for (let i = 2; i < arguments.length; ++i) {
1100
+ const object = arguments[i];
1101
+ if (object === void 0 || object === null) {
1102
+ continue;
1103
+ }
1104
+ for (let propName in object) {
1105
+ if (object.hasOwnProperty(propName)) {
1106
+ const prop = _fieldValue(object, propName);
1107
+ const targ = _fieldValue(result, propName);
1108
+ if (prop === target) {
1109
+ continue;
1110
+ }
1111
+ if (prop === void 0) {
1112
+ continue;
1113
+ }
1114
+ if (deep && typeof prop === "object" && prop !== null) {
1115
+ if (prop instanceof Array) {
1116
+ result[propName] = this._mixin(deep, targ instanceof Array ? targ : [], prop);
1117
+ } else {
1118
+ const source = typeof targ === "object" && !(targ instanceof Array) ? targ : {};
1119
+ result[propName] = this._mixin(deep, source, prop);
1120
+ }
1121
+ } else {
1122
+ result[propName] = prop;
1123
+ }
1124
+ }
1125
+ }
1126
+ }
1127
+ return result;
1128
+ };
1129
+ function _isString(value) {
1130
+ return Utils.isString(value);
1131
+ }
1132
+ function _isAlpha(char) {
1133
+ if (char >= "A" && char <= "Z") {
1134
+ return true;
1135
+ }
1136
+ return char >= "a" && char <= "z";
1137
+ }
1138
+ function _isNumeric(char) {
1139
+ return char >= "0" && char <= "9";
1140
+ }
1141
+ function _isAllowed(char) {
1142
+ switch (char) {
1143
+ case " ":
1144
+ case "!":
1145
+ case "#":
1146
+ case "$":
1147
+ case "(":
1148
+ case ")":
1149
+ case "*":
1150
+ case "+":
1151
+ case "-":
1152
+ case ".":
1153
+ case "/":
1154
+ case "@":
1155
+ case "_":
1156
+ case "{":
1157
+ case "~":
1158
+ case "}":
1159
+ return true;
1160
+ default:
1161
+ return false;
1162
+ }
1163
+ }
1164
+ function _isValidChannel(value) {
1165
+ if (!_isString(value)) {
1166
+ return false;
1167
+ }
1168
+ if (value.length < 2) {
1169
+ return false;
1170
+ }
1171
+ if (value.charAt(0) !== "/") {
1172
+ return false;
1173
+ }
1174
+ for (let i = 1; i < value.length; ++i) {
1175
+ const char = value.charAt(i);
1176
+ if (_isAlpha(char) || _isNumeric(char) || _isAllowed(char)) {
1177
+ continue;
1178
+ }
1179
+ return false;
1180
+ }
1181
+ return true;
1182
+ }
1183
+ function _isFunction(value) {
1184
+ if (value === void 0 || value === null) {
1185
+ return false;
1186
+ }
1187
+ return typeof value === "function";
1188
+ }
1189
+ function _zeroPad(value, length) {
1190
+ let result = "";
1191
+ while (--length > 0) {
1192
+ if (value >= Math.pow(10, length)) {
1193
+ break;
1194
+ }
1195
+ result += "0";
1196
+ }
1197
+ result += value;
1198
+ return result;
1199
+ }
1200
+ function _log(level, args) {
1201
+ if (window.console) {
1202
+ const logger = window.console[level];
1203
+ if (_isFunction(logger)) {
1204
+ const now = /* @__PURE__ */ new Date();
1205
+ [].splice.call(args, 0, 0, _zeroPad(now.getHours(), 2) + ":" + _zeroPad(now.getMinutes(), 2) + ":" + _zeroPad(now.getSeconds(), 2) + "." + _zeroPad(now.getMilliseconds(), 3));
1206
+ logger.apply(window.console, args);
1207
+ }
1208
+ }
1209
+ }
1210
+ this._warn = function() {
1211
+ _log("warn", arguments);
1212
+ };
1213
+ this._info = function() {
1214
+ if (_config.logLevel !== "warn") {
1215
+ _log("info", arguments);
1216
+ }
1217
+ };
1218
+ this._debug = function() {
1219
+ if (_config.logLevel === "debug") {
1220
+ _log("debug", arguments);
1221
+ }
1222
+ };
1223
+ function _splitURL(url) {
1224
+ return new RegExp("(^https?://)?(((\\[[^\\]]+])|([^:/?#]+))(:(\\d+))?)?([^?#]*)(.*)?").exec(url);
1225
+ }
1226
+ this._isCrossDomain = (hostAndPort) => {
1227
+ if (window.location && window.location.host) {
1228
+ if (hostAndPort) {
1229
+ return hostAndPort !== window.location.host;
1230
+ }
1231
+ }
1232
+ return false;
1233
+ };
1234
+ function _configure(configuration) {
1235
+ _cometd._debug("Configuring cometd object with", configuration);
1236
+ if (_isString(configuration)) {
1237
+ configuration = {
1238
+ url: configuration
1239
+ };
1240
+ }
1241
+ if (!configuration) {
1242
+ configuration = {};
1243
+ }
1244
+ _config = _cometd._mixin(false, _config, configuration);
1245
+ const url = _cometd.getURL();
1246
+ if (!url) {
1247
+ throw "Missing required configuration parameter 'url' specifying the Bayeux server URL";
1248
+ }
1249
+ const urlParts = _splitURL(url);
1250
+ const hostAndPort = urlParts[2];
1251
+ const uri = urlParts[8];
1252
+ const afterURI = urlParts[9];
1253
+ _crossDomain = _cometd._isCrossDomain(hostAndPort);
1254
+ if (_config.appendMessageTypeToURL) {
1255
+ if (afterURI !== void 0 && afterURI.length > 0) {
1256
+ _cometd._info("Appending message type to URI " + uri + afterURI + " is not supported, disabling 'appendMessageTypeToURL' configuration");
1257
+ _config.appendMessageTypeToURL = false;
1258
+ } else {
1259
+ const uriSegments = uri.split("/");
1260
+ let lastSegmentIndex = uriSegments.length - 1;
1261
+ if (uri.match(/\/$/)) {
1262
+ lastSegmentIndex -= 1;
1263
+ }
1264
+ if (uriSegments[lastSegmentIndex].indexOf(".") >= 0) {
1265
+ _cometd._info("Appending message type to URI " + uri + " is not supported, disabling 'appendMessageTypeToURL' configuration");
1266
+ _config.appendMessageTypeToURL = false;
1267
+ }
1268
+ }
1269
+ }
1270
+ if (window.Worker && window.Blob && window.URL && _config.useWorkerScheduler) {
1271
+ let code = WorkerScheduler.toString();
1272
+ code = code.substring(code.indexOf("{") + 1, code.lastIndexOf("}"));
1273
+ const blob = new window.Blob([code], {
1274
+ type: "application/json"
1275
+ });
1276
+ const blobURL = window.URL.createObjectURL(blob);
1277
+ const worker = new window.Worker(blobURL);
1278
+ _scheduler.setTimeout = (funktion, delay) => {
1279
+ const id = _scheduler.register(funktion);
1280
+ worker.postMessage({
1281
+ id,
1282
+ type: "setTimeout",
1283
+ delay
1284
+ });
1285
+ return id;
1286
+ };
1287
+ _scheduler.clearTimeout = (id) => {
1288
+ _scheduler.unregister(id);
1289
+ worker.postMessage({
1290
+ id,
1291
+ type: "clearTimeout"
1292
+ });
1293
+ };
1294
+ worker.onmessage = (e) => {
1295
+ const id = e.data.id;
1296
+ const funktion = _scheduler.unregister(id);
1297
+ if (funktion) {
1298
+ funktion();
1299
+ }
1300
+ };
1301
+ }
1302
+ }
1303
+ function _removeListener(subscription) {
1304
+ if (subscription) {
1305
+ const subscriptions = _listeners[subscription.channel];
1306
+ if (subscriptions && subscriptions[subscription.id]) {
1307
+ delete subscriptions[subscription.id];
1308
+ _cometd._debug("Removed", subscription.listener ? "listener" : "subscription", subscription);
1309
+ }
1310
+ }
1311
+ }
1312
+ function _removeSubscription(subscription) {
1313
+ if (subscription && !subscription.listener) {
1314
+ _removeListener(subscription);
1315
+ }
1316
+ }
1317
+ function _clearSubscriptions() {
1318
+ for (let channel in _listeners) {
1319
+ if (_listeners.hasOwnProperty(channel)) {
1320
+ const subscriptions = _listeners[channel];
1321
+ if (subscriptions) {
1322
+ for (let id in subscriptions) {
1323
+ if (subscriptions.hasOwnProperty(id)) {
1324
+ _removeSubscription(subscriptions[id]);
1325
+ }
1326
+ }
1327
+ }
1328
+ }
1329
+ }
1330
+ }
1331
+ function _setStatus(newStatus) {
1332
+ if (_status !== newStatus) {
1333
+ _cometd._debug("Status", _status, "->", newStatus);
1334
+ _status = newStatus;
1335
+ }
1336
+ }
1337
+ function _isDisconnected() {
1338
+ return _status === "disconnecting" || _status === "disconnected";
1339
+ }
1340
+ function _nextMessageId() {
1341
+ const result = ++_messageId;
1342
+ return "" + result;
1343
+ }
1344
+ function _applyExtension(scope, callback, name2, message, outgoing) {
1345
+ try {
1346
+ return callback.call(scope, message);
1347
+ } catch (x) {
1348
+ const handler = _cometd.onExtensionException;
1349
+ if (_isFunction(handler)) {
1350
+ _cometd._debug("Invoking extension exception handler", name2, x);
1351
+ try {
1352
+ handler.call(_cometd, x, name2, outgoing, message);
1353
+ } catch (xx) {
1354
+ _cometd._info("Exception during execution of extension exception handler", name2, xx);
1355
+ }
1356
+ } else {
1357
+ _cometd._info("Exception during execution of extension", name2, x);
1358
+ }
1359
+ return message;
1360
+ }
1361
+ }
1362
+ function _applyIncomingExtensions(message) {
1363
+ for (let i = 0; i < _extensions.length; ++i) {
1364
+ if (message === void 0 || message === null) {
1365
+ break;
1366
+ }
1367
+ const extension = _extensions[i];
1368
+ const callback = extension.extension.incoming;
1369
+ if (_isFunction(callback)) {
1370
+ const result = _applyExtension(extension.extension, callback, extension.name, message, false);
1371
+ message = result === void 0 ? message : result;
1372
+ }
1373
+ }
1374
+ return message;
1375
+ }
1376
+ function _applyOutgoingExtensions(message) {
1377
+ for (let i = _extensions.length - 1; i >= 0; --i) {
1378
+ if (message === void 0 || message === null) {
1379
+ break;
1380
+ }
1381
+ const extension = _extensions[i];
1382
+ const callback = extension.extension.outgoing;
1383
+ if (_isFunction(callback)) {
1384
+ const result = _applyExtension(extension.extension, callback, extension.name, message, true);
1385
+ message = result === void 0 ? message : result;
1386
+ }
1387
+ }
1388
+ return message;
1389
+ }
1390
+ function _notify(channel, message) {
1391
+ const subscriptions = _listeners[channel];
1392
+ if (subscriptions) {
1393
+ for (let id in subscriptions) {
1394
+ if (subscriptions.hasOwnProperty(id)) {
1395
+ const subscription = subscriptions[id];
1396
+ if (subscription) {
1397
+ try {
1398
+ subscription.callback.call(subscription.scope, message);
1399
+ } catch (x) {
1400
+ const handler = _cometd.onListenerException;
1401
+ if (_isFunction(handler)) {
1402
+ _cometd._debug("Invoking listener exception handler", subscription, x);
1403
+ try {
1404
+ handler.call(_cometd, x, subscription, subscription.listener, message);
1405
+ } catch (xx) {
1406
+ _cometd._info("Exception during execution of listener exception handler", subscription, xx);
1407
+ }
1408
+ } else {
1409
+ _cometd._info("Exception during execution of listener", subscription, message, x);
1410
+ }
1411
+ }
1412
+ }
1413
+ }
1414
+ }
1415
+ }
1416
+ }
1417
+ function _notifyListeners(channel, message) {
1418
+ _notify(channel, message);
1419
+ const channelParts = channel.split("/");
1420
+ const last = channelParts.length - 1;
1421
+ for (let i = last; i > 0; --i) {
1422
+ let channelPart = channelParts.slice(0, i).join("/") + "/*";
1423
+ if (i === last) {
1424
+ _notify(channelPart, message);
1425
+ }
1426
+ channelPart += "*";
1427
+ _notify(channelPart, message);
1428
+ }
1429
+ }
1430
+ function _cancelDelayedSend() {
1431
+ if (_scheduledSend !== null) {
1432
+ _cometd.clearTimeout(_scheduledSend);
1433
+ }
1434
+ _scheduledSend = null;
1435
+ }
1436
+ function _delayedSend(operation, delay) {
1437
+ _cancelDelayedSend();
1438
+ const time = _advice.interval + delay;
1439
+ _cometd._debug("Function scheduled in", time, "ms, interval =", _advice.interval, "backoff =", _backoff, operation);
1440
+ _scheduledSend = _cometd.setTimeout(operation, time);
1441
+ }
1442
+ let _handleMessages;
1443
+ let _handleFailure;
1444
+ function _send(messages, metaConnect, extraPath) {
1445
+ for (let i = 0; i < messages.length; ++i) {
1446
+ let message = messages[i];
1447
+ const messageId = message.id;
1448
+ if (_clientId) {
1449
+ message.clientId = _clientId;
1450
+ }
1451
+ message = _applyOutgoingExtensions(message);
1452
+ if (message !== void 0 && message !== null) {
1453
+ message.id = messageId;
1454
+ messages[i] = message;
1455
+ } else {
1456
+ delete _callbacks[messageId];
1457
+ messages.splice(i--, 1);
1458
+ }
1459
+ }
1460
+ if (messages.length === 0) {
1461
+ return;
1462
+ }
1463
+ if (metaConnect) {
1464
+ _metaConnect = messages[0];
1465
+ }
1466
+ let url = _cometd.getURL();
1467
+ if (_config.appendMessageTypeToURL) {
1468
+ if (!url.match(/\/$/)) {
1469
+ url = url + "/";
1470
+ }
1471
+ if (extraPath) {
1472
+ url = url + extraPath;
1473
+ }
1474
+ }
1475
+ const envelope = {
1476
+ url,
1477
+ sync: false,
1478
+ messages,
1479
+ onSuccess: (rcvdMessages) => {
1480
+ try {
1481
+ _handleMessages.call(_cometd, rcvdMessages);
1482
+ } catch (x) {
1483
+ _cometd._info("Exception during handling of messages", x);
1484
+ }
1485
+ },
1486
+ onFailure: (conduit, messages2, failure) => {
1487
+ try {
1488
+ const transport = _cometd.getTransport();
1489
+ failure.connectionType = transport ? transport.getType() : "unknown";
1490
+ _handleFailure.call(_cometd, conduit, messages2, failure);
1491
+ } catch (x) {
1492
+ _cometd._info("Exception during handling of failure", x);
1493
+ }
1494
+ }
1495
+ };
1496
+ _cometd._debug("Send", envelope);
1497
+ _transport.send(envelope, metaConnect);
1498
+ }
1499
+ function _queueSend(message) {
1500
+ if (_batch > 0 || _internalBatch === true) {
1501
+ _messageQueue.push(message);
1502
+ } else {
1503
+ _send([message], false);
1504
+ }
1505
+ }
1506
+ this.send = _queueSend;
1507
+ function _resetBackoff() {
1508
+ _backoff = 0;
1509
+ }
1510
+ function _increaseBackoff() {
1511
+ if (_backoff < _config.maxBackoff) {
1512
+ _backoff += _config.backoffIncrement;
1513
+ }
1514
+ return _backoff;
1515
+ }
1516
+ function _startBatch() {
1517
+ ++_batch;
1518
+ _cometd._debug("Starting batch, depth", _batch);
1519
+ }
1520
+ function _flushBatch() {
1521
+ const messages = _messageQueue;
1522
+ _messageQueue = [];
1523
+ if (messages.length > 0) {
1524
+ _send(messages, false);
1525
+ }
1526
+ }
1527
+ function _endBatch() {
1528
+ --_batch;
1529
+ _cometd._debug("Ending batch, depth", _batch);
1530
+ if (_batch < 0) {
1531
+ throw "Calls to startBatch() and endBatch() are not paired";
1532
+ }
1533
+ if (_batch === 0 && !_isDisconnected() && !_internalBatch) {
1534
+ _flushBatch();
1535
+ }
1536
+ }
1537
+ function _connect() {
1538
+ if (!_isDisconnected()) {
1539
+ const bayeuxMessage = {
1540
+ id: _nextMessageId(),
1541
+ channel: "/meta/connect",
1542
+ connectionType: _transport.getType()
1543
+ };
1544
+ if (!_connected) {
1545
+ bayeuxMessage.advice = {
1546
+ timeout: 0
1547
+ };
1548
+ }
1549
+ _setStatus("connecting");
1550
+ _cometd._debug("Connect sent", bayeuxMessage);
1551
+ _send([bayeuxMessage], true, "connect");
1552
+ _setStatus("connected");
1553
+ }
1554
+ }
1555
+ function _delayedConnect(delay) {
1556
+ _setStatus("connecting");
1557
+ _delayedSend(() => {
1558
+ _connect();
1559
+ }, delay);
1560
+ }
1561
+ function _updateAdvice(newAdvice) {
1562
+ if (newAdvice) {
1563
+ _advice = _cometd._mixin(false, {}, _config.advice, newAdvice);
1564
+ _cometd._debug("New advice", _advice);
1565
+ }
1566
+ }
1567
+ function _disconnect(abort) {
1568
+ _cancelDelayedSend();
1569
+ if (abort && _transport) {
1570
+ _transport.abort();
1571
+ }
1572
+ _crossDomain = false;
1573
+ _transport = null;
1574
+ _setStatus("disconnected");
1575
+ _clientId = null;
1576
+ _batch = 0;
1577
+ _resetBackoff();
1578
+ _reestablish = false;
1579
+ _connected = false;
1580
+ _unconnectTime = 0;
1581
+ _metaConnect = null;
1582
+ if (_messageQueue.length > 0) {
1583
+ const messages = _messageQueue;
1584
+ _messageQueue = [];
1585
+ _handleFailure.call(_cometd, void 0, messages, {
1586
+ reason: "Disconnected"
1587
+ });
1588
+ }
1589
+ }
1590
+ function _notifyTransportException(oldTransport, newTransport, failure) {
1591
+ const handler = _cometd.onTransportException;
1592
+ if (_isFunction(handler)) {
1593
+ _cometd._debug("Invoking transport exception handler", oldTransport, newTransport, failure);
1594
+ try {
1595
+ handler.call(_cometd, failure, oldTransport, newTransport);
1596
+ } catch (x) {
1597
+ _cometd._info("Exception during execution of transport exception handler", x);
1598
+ }
1599
+ }
1600
+ }
1601
+ function _handshake(handshakeProps, handshakeCallback) {
1602
+ if (_isFunction(handshakeProps)) {
1603
+ handshakeCallback = handshakeProps;
1604
+ handshakeProps = void 0;
1605
+ }
1606
+ _clientId = null;
1607
+ _clearSubscriptions();
1608
+ if (_isDisconnected()) {
1609
+ _transports.reset(true);
1610
+ }
1611
+ _updateAdvice({});
1612
+ _batch = 0;
1613
+ _internalBatch = true;
1614
+ _handshakeProps = handshakeProps;
1615
+ _handshakeCallback = handshakeCallback;
1616
+ const version = "1.0";
1617
+ const url = _cometd.getURL();
1618
+ const transportTypes = _transports.findTransportTypes(version, _crossDomain, url);
1619
+ const bayeuxMessage = {
1620
+ id: _nextMessageId(),
1621
+ version,
1622
+ minimumVersion: version,
1623
+ channel: "/meta/handshake",
1624
+ supportedConnectionTypes: transportTypes,
1625
+ advice: {
1626
+ timeout: _advice.timeout,
1627
+ interval: _advice.interval
1628
+ }
1629
+ };
1630
+ const message = _cometd._mixin(false, {}, _handshakeProps, bayeuxMessage);
1631
+ _cometd._putCallback(message.id, handshakeCallback);
1632
+ if (!_transport) {
1633
+ _transport = _transports.negotiateTransport(transportTypes, version, _crossDomain, url);
1634
+ if (!_transport) {
1635
+ const failure = "Could not find initial transport among: " + _transports.getTransportTypes();
1636
+ _cometd._warn(failure);
1637
+ throw failure;
1638
+ }
1639
+ }
1640
+ _cometd._debug("Initial transport is", _transport.getType());
1641
+ _setStatus("handshaking");
1642
+ _cometd._debug("Handshake sent", message);
1643
+ _send([message], false, "handshake");
1644
+ }
1645
+ function _delayedHandshake(delay) {
1646
+ _setStatus("handshaking");
1647
+ _internalBatch = true;
1648
+ _delayedSend(() => {
1649
+ _handshake(_handshakeProps, _handshakeCallback);
1650
+ }, delay);
1651
+ }
1652
+ function _notifyCallback(callback, message) {
1653
+ try {
1654
+ callback.call(_cometd, message);
1655
+ } catch (x) {
1656
+ const handler = _cometd.onCallbackException;
1657
+ if (_isFunction(handler)) {
1658
+ _cometd._debug("Invoking callback exception handler", x);
1659
+ try {
1660
+ handler.call(_cometd, x, message);
1661
+ } catch (xx) {
1662
+ _cometd._info("Exception during execution of callback exception handler", xx);
1663
+ }
1664
+ } else {
1665
+ _cometd._info("Exception during execution of message callback", x);
1666
+ }
1667
+ }
1668
+ }
1669
+ this._getCallback = (messageId) => _callbacks[messageId];
1670
+ this._putCallback = function(messageId, callback) {
1671
+ const result = this._getCallback(messageId);
1672
+ if (_isFunction(callback)) {
1673
+ _callbacks[messageId] = callback;
1674
+ }
1675
+ return result;
1676
+ };
1677
+ function _handleCallback(message) {
1678
+ const callback = _cometd._getCallback([message.id]);
1679
+ if (_isFunction(callback)) {
1680
+ delete _callbacks[message.id];
1681
+ _notifyCallback(callback, message);
1682
+ }
1683
+ }
1684
+ function _handleRemoteCall(message) {
1685
+ const context = _remoteCalls[message.id];
1686
+ delete _remoteCalls[message.id];
1687
+ if (context) {
1688
+ _cometd._debug("Handling remote call response for", message, "with context", context);
1689
+ const timeout = context.timeout;
1690
+ if (timeout) {
1691
+ _cometd.clearTimeout(timeout);
1692
+ }
1693
+ const callback = context.callback;
1694
+ if (_isFunction(callback)) {
1695
+ _notifyCallback(callback, message);
1696
+ return true;
1697
+ }
1698
+ }
1699
+ return false;
1700
+ }
1701
+ this.onTransportFailure = function(message, failureInfo, failureHandler) {
1702
+ this._debug("Transport failure", failureInfo, "for", message);
1703
+ const transports = this.getTransportRegistry();
1704
+ const url = this.getURL();
1705
+ const crossDomain = this._isCrossDomain(_splitURL(url)[2]);
1706
+ const version = "1.0";
1707
+ const transportTypes = transports.findTransportTypes(version, crossDomain, url);
1708
+ if (failureInfo.action === "none") {
1709
+ if (message.channel === "/meta/handshake") {
1710
+ if (!failureInfo.transport) {
1711
+ const failure = "Could not negotiate transport, client=[" + transportTypes + "], server=[" + message.supportedConnectionTypes + "]";
1712
+ this._warn(failure);
1713
+ _notifyTransportException(_transport.getType(), null, {
1714
+ reason: failure,
1715
+ connectionType: _transport.getType(),
1716
+ transport: _transport
1717
+ });
1718
+ }
1719
+ }
1720
+ } else {
1721
+ failureInfo.delay = this.getBackoffPeriod();
1722
+ if (message.channel === "/meta/handshake") {
1723
+ if (!failureInfo.transport) {
1724
+ const oldTransportType = _transport ? _transport.getType() : null;
1725
+ const newTransport = transports.negotiateTransport(transportTypes, version, crossDomain, url);
1726
+ if (!newTransport) {
1727
+ this._warn("Could not negotiate transport, client=[" + transportTypes + "]");
1728
+ _notifyTransportException(oldTransportType, null, message.failure);
1729
+ failureInfo.action = "none";
1730
+ } else {
1731
+ const newTransportType = newTransport.getType();
1732
+ this._debug("Transport", oldTransportType, "->", newTransportType);
1733
+ _notifyTransportException(oldTransportType, newTransportType, message.failure);
1734
+ failureInfo.action = "handshake";
1735
+ failureInfo.transport = newTransport;
1736
+ }
1737
+ }
1738
+ if (failureInfo.action !== "none") {
1739
+ this.increaseBackoffPeriod();
1740
+ }
1741
+ } else {
1742
+ const now = (/* @__PURE__ */ new Date()).getTime();
1743
+ if (_unconnectTime === 0) {
1744
+ _unconnectTime = now;
1745
+ }
1746
+ if (failureInfo.action === "retry") {
1747
+ failureInfo.delay = this.increaseBackoffPeriod();
1748
+ const maxInterval = _advice.maxInterval;
1749
+ if (maxInterval > 0) {
1750
+ const expiration = _advice.timeout + _advice.interval + maxInterval;
1751
+ const unconnected = now - _unconnectTime;
1752
+ if (unconnected + _backoff > expiration) {
1753
+ failureInfo.action = "handshake";
1754
+ }
1755
+ }
1756
+ }
1757
+ if (failureInfo.action === "handshake") {
1758
+ failureInfo.delay = 0;
1759
+ transports.reset(false);
1760
+ this.resetBackoffPeriod();
1761
+ }
1762
+ }
1763
+ }
1764
+ failureHandler.call(_cometd, failureInfo);
1765
+ };
1766
+ function _handleTransportFailure(failureInfo) {
1767
+ _cometd._debug("Transport failure handling", failureInfo);
1768
+ if (failureInfo.transport) {
1769
+ _transport = failureInfo.transport;
1770
+ }
1771
+ if (failureInfo.url) {
1772
+ _transport.setURL(failureInfo.url);
1773
+ }
1774
+ const action = failureInfo.action;
1775
+ const delay = failureInfo.delay || 0;
1776
+ switch (action) {
1777
+ case "handshake":
1778
+ _delayedHandshake(delay);
1779
+ break;
1780
+ case "retry":
1781
+ _delayedConnect(delay);
1782
+ break;
1783
+ case "none":
1784
+ _disconnect(true);
1785
+ break;
1786
+ default:
1787
+ throw "Unknown action " + action;
1788
+ }
1789
+ }
1790
+ function _failHandshake(message, failureInfo) {
1791
+ _handleCallback(message);
1792
+ _notifyListeners("/meta/handshake", message);
1793
+ _notifyListeners("/meta/unsuccessful", message);
1794
+ if (_isDisconnected()) {
1795
+ failureInfo.action = "none";
1796
+ }
1797
+ _cometd.onTransportFailure.call(_cometd, message, failureInfo, _handleTransportFailure);
1798
+ }
1799
+ function _handshakeResponse(message) {
1800
+ const url = _cometd.getURL();
1801
+ if (message.successful) {
1802
+ const crossDomain = _cometd._isCrossDomain(_splitURL(url)[2]);
1803
+ const newTransport = _transports.negotiateTransport(message.supportedConnectionTypes, message.version, crossDomain, url);
1804
+ if (newTransport === null) {
1805
+ message.successful = false;
1806
+ _failHandshake(message, {
1807
+ cause: "negotiation",
1808
+ action: "none",
1809
+ transport: null
1810
+ });
1811
+ return;
1812
+ } else if (_transport !== newTransport) {
1813
+ _cometd._debug("Transport", _transport.getType(), "->", newTransport.getType());
1814
+ _transport = newTransport;
1815
+ }
1816
+ _clientId = message.clientId;
1817
+ _internalBatch = false;
1818
+ _flushBatch();
1819
+ message.reestablish = _reestablish;
1820
+ _reestablish = true;
1821
+ _handleCallback(message);
1822
+ _notifyListeners("/meta/handshake", message);
1823
+ _handshakeMessages = message["x-messages"] || 0;
1824
+ const action = _isDisconnected() ? "none" : _advice.reconnect || "retry";
1825
+ switch (action) {
1826
+ case "retry":
1827
+ _resetBackoff();
1828
+ if (_handshakeMessages === 0) {
1829
+ _delayedConnect(0);
1830
+ } else {
1831
+ _cometd._debug("Processing", _handshakeMessages, "handshake-delivered messages");
1832
+ }
1833
+ break;
1834
+ case "none":
1835
+ _disconnect(true);
1836
+ break;
1837
+ default:
1838
+ throw "Unrecognized advice action " + action;
1839
+ }
1840
+ } else {
1841
+ _failHandshake(message, {
1842
+ cause: "unsuccessful",
1843
+ action: _advice.reconnect || "handshake",
1844
+ transport: _transport
1845
+ });
1846
+ }
1847
+ }
1848
+ function _handshakeFailure(message) {
1849
+ _failHandshake(message, {
1850
+ cause: "failure",
1851
+ action: "handshake",
1852
+ transport: null
1853
+ });
1854
+ }
1855
+ function _matchMetaConnect(connect) {
1856
+ if (_status === "disconnected") {
1857
+ return true;
1858
+ }
1859
+ if (_metaConnect && _metaConnect.id === connect.id) {
1860
+ _metaConnect = null;
1861
+ return true;
1862
+ }
1863
+ return false;
1864
+ }
1865
+ function _failConnect(message, failureInfo) {
1866
+ _notifyListeners("/meta/connect", message);
1867
+ _notifyListeners("/meta/unsuccessful", message);
1868
+ if (_isDisconnected()) {
1869
+ failureInfo.action = "none";
1870
+ }
1871
+ _cometd.onTransportFailure.call(_cometd, message, failureInfo, _handleTransportFailure);
1872
+ }
1873
+ function _connectResponse(message) {
1874
+ if (_matchMetaConnect(message)) {
1875
+ _connected = message.successful;
1876
+ if (_connected) {
1877
+ _notifyListeners("/meta/connect", message);
1878
+ const action = _isDisconnected() ? "none" : _advice.reconnect || "retry";
1879
+ switch (action) {
1880
+ case "retry":
1881
+ _resetBackoff();
1882
+ _delayedConnect(_backoff);
1883
+ break;
1884
+ case "none":
1885
+ _disconnect(false);
1886
+ break;
1887
+ default:
1888
+ throw "Unrecognized advice action " + action;
1889
+ }
1890
+ } else {
1891
+ _failConnect(message, {
1892
+ cause: "unsuccessful",
1893
+ action: _advice.reconnect || "retry",
1894
+ transport: _transport
1895
+ });
1896
+ }
1897
+ } else {
1898
+ _cometd._debug("Mismatched /meta/connect reply", message);
1899
+ }
1900
+ }
1901
+ function _connectFailure(message) {
1902
+ if (_matchMetaConnect(message)) {
1903
+ _connected = false;
1904
+ _failConnect(message, {
1905
+ cause: "failure",
1906
+ action: "retry",
1907
+ transport: null
1908
+ });
1909
+ } else {
1910
+ _cometd._debug("Mismatched /meta/connect failure", message);
1911
+ }
1912
+ }
1913
+ function _failDisconnect(message) {
1914
+ _disconnect(true);
1915
+ _handleCallback(message);
1916
+ _notifyListeners("/meta/disconnect", message);
1917
+ _notifyListeners("/meta/unsuccessful", message);
1918
+ }
1919
+ function _disconnectResponse(message) {
1920
+ if (message.successful) {
1921
+ _disconnect(false);
1922
+ _handleCallback(message);
1923
+ _notifyListeners("/meta/disconnect", message);
1924
+ } else {
1925
+ _failDisconnect(message);
1926
+ }
1927
+ }
1928
+ function _disconnectFailure(message) {
1929
+ _failDisconnect(message);
1930
+ }
1931
+ function _failSubscribe(message) {
1932
+ const subscriptions = _listeners[message.subscription];
1933
+ if (subscriptions) {
1934
+ for (let id in subscriptions) {
1935
+ if (subscriptions.hasOwnProperty(id)) {
1936
+ const subscription = subscriptions[id];
1937
+ if (subscription && !subscription.listener) {
1938
+ delete subscriptions[id];
1939
+ _cometd._debug("Removed failed subscription", subscription);
1940
+ }
1941
+ }
1942
+ }
1943
+ }
1944
+ _handleCallback(message);
1945
+ _notifyListeners("/meta/subscribe", message);
1946
+ _notifyListeners("/meta/unsuccessful", message);
1947
+ }
1948
+ function _subscribeResponse(message) {
1949
+ if (message.successful) {
1950
+ _handleCallback(message);
1951
+ _notifyListeners("/meta/subscribe", message);
1952
+ } else {
1953
+ _failSubscribe(message);
1954
+ }
1955
+ }
1956
+ function _subscribeFailure(message) {
1957
+ _failSubscribe(message);
1958
+ }
1959
+ function _failUnsubscribe(message) {
1960
+ _handleCallback(message);
1961
+ _notifyListeners("/meta/unsubscribe", message);
1962
+ _notifyListeners("/meta/unsuccessful", message);
1963
+ }
1964
+ function _unsubscribeResponse(message) {
1965
+ if (message.successful) {
1966
+ _handleCallback(message);
1967
+ _notifyListeners("/meta/unsubscribe", message);
1968
+ } else {
1969
+ _failUnsubscribe(message);
1970
+ }
1971
+ }
1972
+ function _unsubscribeFailure(message) {
1973
+ _failUnsubscribe(message);
1974
+ }
1975
+ function _failMessage(message) {
1976
+ if (!_handleRemoteCall(message)) {
1977
+ _handleCallback(message);
1978
+ _notifyListeners("/meta/publish", message);
1979
+ _notifyListeners("/meta/unsuccessful", message);
1980
+ }
1981
+ }
1982
+ function _messageResponse(message) {
1983
+ if (message.data !== void 0) {
1984
+ if (!_handleRemoteCall(message)) {
1985
+ _notifyListeners(message.channel, message);
1986
+ if (_handshakeMessages > 0) {
1987
+ --_handshakeMessages;
1988
+ if (_handshakeMessages === 0) {
1989
+ _cometd._debug("Processed last handshake-delivered message");
1990
+ _delayedConnect(0);
1991
+ }
1992
+ }
1993
+ }
1994
+ } else {
1995
+ if (message.successful === void 0) {
1996
+ _cometd._warn("Unknown Bayeux Message", message);
1997
+ } else {
1998
+ if (message.successful) {
1999
+ _handleCallback(message);
2000
+ _notifyListeners("/meta/publish", message);
2001
+ } else {
2002
+ _failMessage(message);
2003
+ }
2004
+ }
2005
+ }
2006
+ }
2007
+ function _messageFailure(failure) {
2008
+ _failMessage(failure);
2009
+ }
2010
+ function _receive(message) {
2011
+ _unconnectTime = 0;
2012
+ message = _applyIncomingExtensions(message);
2013
+ if (message === void 0 || message === null) {
2014
+ return;
2015
+ }
2016
+ _updateAdvice(message.advice);
2017
+ const channel = message.channel;
2018
+ switch (channel) {
2019
+ case "/meta/handshake":
2020
+ _handshakeResponse(message);
2021
+ break;
2022
+ case "/meta/connect":
2023
+ _connectResponse(message);
2024
+ break;
2025
+ case "/meta/disconnect":
2026
+ _disconnectResponse(message);
2027
+ break;
2028
+ case "/meta/subscribe":
2029
+ _subscribeResponse(message);
2030
+ break;
2031
+ case "/meta/unsubscribe":
2032
+ _unsubscribeResponse(message);
2033
+ break;
2034
+ default:
2035
+ _messageResponse(message);
2036
+ break;
2037
+ }
2038
+ }
2039
+ this.receive = _receive;
2040
+ _handleMessages = (rcvdMessages) => {
2041
+ _cometd._debug("Received", rcvdMessages);
2042
+ for (let i = 0; i < rcvdMessages.length; ++i) {
2043
+ const message = rcvdMessages[i];
2044
+ _receive(message);
2045
+ }
2046
+ };
2047
+ _handleFailure = (conduit, messages, failure) => {
2048
+ _cometd._debug("handleFailure", conduit, messages, failure);
2049
+ failure.transport = conduit;
2050
+ for (let i = 0; i < messages.length; ++i) {
2051
+ const message = messages[i];
2052
+ const failureMessage = {
2053
+ id: message.id,
2054
+ successful: false,
2055
+ channel: message.channel,
2056
+ failure
2057
+ };
2058
+ failure.message = message;
2059
+ switch (message.channel) {
2060
+ case "/meta/handshake":
2061
+ _handshakeFailure(failureMessage);
2062
+ break;
2063
+ case "/meta/connect":
2064
+ _connectFailure(failureMessage);
2065
+ break;
2066
+ case "/meta/disconnect":
2067
+ _disconnectFailure(failureMessage);
2068
+ break;
2069
+ case "/meta/subscribe":
2070
+ failureMessage.subscription = message.subscription;
2071
+ _subscribeFailure(failureMessage);
2072
+ break;
2073
+ case "/meta/unsubscribe":
2074
+ failureMessage.subscription = message.subscription;
2075
+ _unsubscribeFailure(failureMessage);
2076
+ break;
2077
+ default:
2078
+ _messageFailure(failureMessage);
2079
+ break;
2080
+ }
2081
+ }
2082
+ };
2083
+ function _hasSubscriptions(channel) {
2084
+ const subscriptions = _listeners[channel];
2085
+ if (subscriptions) {
2086
+ for (let id in subscriptions) {
2087
+ if (subscriptions.hasOwnProperty(id)) {
2088
+ if (subscriptions[id]) {
2089
+ return true;
2090
+ }
2091
+ }
2092
+ }
2093
+ }
2094
+ return false;
2095
+ }
2096
+ function _resolveScopedCallback(scope, callback) {
2097
+ const delegate = {
2098
+ scope,
2099
+ method: callback
2100
+ };
2101
+ if (_isFunction(scope)) {
2102
+ delegate.scope = void 0;
2103
+ delegate.method = scope;
2104
+ } else {
2105
+ if (_isString(callback)) {
2106
+ if (!scope) {
2107
+ throw "Invalid scope " + scope;
2108
+ }
2109
+ delegate.method = scope[callback];
2110
+ if (!_isFunction(delegate.method)) {
2111
+ throw "Invalid callback " + callback + " for scope " + scope;
2112
+ }
2113
+ } else if (!_isFunction(callback)) {
2114
+ throw "Invalid callback " + callback;
2115
+ }
2116
+ }
2117
+ return delegate;
2118
+ }
2119
+ function _addListener(channel, scope, callback, isListener) {
2120
+ const delegate = _resolveScopedCallback(scope, callback);
2121
+ _cometd._debug("Adding", isListener ? "listener" : "subscription", "on", channel, "with scope", delegate.scope, "and callback", delegate.method);
2122
+ const id = ++_listenerId;
2123
+ const subscription = {
2124
+ id,
2125
+ channel,
2126
+ scope: delegate.scope,
2127
+ callback: delegate.method,
2128
+ listener: isListener
2129
+ };
2130
+ let subscriptions = _listeners[channel];
2131
+ if (!subscriptions) {
2132
+ subscriptions = {};
2133
+ _listeners[channel] = subscriptions;
2134
+ }
2135
+ subscriptions[id] = subscription;
2136
+ _cometd._debug("Added", isListener ? "listener" : "subscription", subscription);
2137
+ return subscription;
2138
+ }
2139
+ this.registerTransport = function(type, transport, index) {
2140
+ const result = _transports.add(type, transport, index);
2141
+ if (result) {
2142
+ this._debug("Registered transport", type);
2143
+ if (_isFunction(transport.registered)) {
2144
+ transport.registered(type, this);
2145
+ }
2146
+ }
2147
+ return result;
2148
+ };
2149
+ this.unregisterTransport = function(type) {
2150
+ const transport = _transports.remove(type);
2151
+ if (transport !== null) {
2152
+ this._debug("Unregistered transport", type);
2153
+ if (_isFunction(transport.unregistered)) {
2154
+ transport.unregistered();
2155
+ }
2156
+ }
2157
+ return transport;
2158
+ };
2159
+ this.unregisterTransports = () => {
2160
+ _transports.clear();
2161
+ };
2162
+ this.getTransportTypes = () => _transports.getTransportTypes();
2163
+ this.findTransport = (name2) => _transports.find(name2);
2164
+ this.getTransportRegistry = () => _transports;
2165
+ this.configure = function(configuration) {
2166
+ _configure.call(this, configuration);
2167
+ };
2168
+ this.init = function(configuration, handshakeProps) {
2169
+ this.configure(configuration);
2170
+ this.handshake(handshakeProps);
2171
+ };
2172
+ this.handshake = (handshakeProps, handshakeCallback) => {
2173
+ if (_status !== "disconnected") {
2174
+ throw "Illegal state: handshaken";
2175
+ }
2176
+ _handshake(handshakeProps, handshakeCallback);
2177
+ };
2178
+ this.disconnect = function(disconnectProps, disconnectCallback) {
2179
+ if (_isDisconnected()) {
2180
+ return;
2181
+ }
2182
+ if (_isFunction(disconnectProps)) {
2183
+ disconnectCallback = disconnectProps;
2184
+ disconnectProps = void 0;
2185
+ }
2186
+ const bayeuxMessage = {
2187
+ id: _nextMessageId(),
2188
+ channel: "/meta/disconnect"
2189
+ };
2190
+ const message = this._mixin(false, {}, disconnectProps, bayeuxMessage);
2191
+ _cometd._putCallback(message.id, disconnectCallback);
2192
+ _setStatus("disconnecting");
2193
+ _send([message], false, "disconnect");
2194
+ };
2195
+ this.startBatch = () => {
2196
+ _startBatch();
2197
+ };
2198
+ this.endBatch = () => {
2199
+ _endBatch();
2200
+ };
2201
+ this.batch = function(scope, callback) {
2202
+ const delegate = _resolveScopedCallback(scope, callback);
2203
+ this.startBatch();
2204
+ try {
2205
+ delegate.method.call(delegate.scope);
2206
+ this.endBatch();
2207
+ } catch (x) {
2208
+ this._info("Exception during execution of batch", x);
2209
+ this.endBatch();
2210
+ throw x;
2211
+ }
2212
+ };
2213
+ this.addTransportListener = (event, callback) => {
2214
+ if (event !== "timeout") {
2215
+ throw "Unsupported event " + event;
2216
+ }
2217
+ let callbacks = _transportListeners[event];
2218
+ if (!callbacks) {
2219
+ _transportListeners[event] = callbacks = [];
2220
+ }
2221
+ callbacks.push(callback);
2222
+ };
2223
+ this.removeTransportListener = (event, callback) => {
2224
+ const callbacks = _transportListeners[event];
2225
+ if (callbacks) {
2226
+ const index = callbacks.indexOf(callback);
2227
+ if (index >= 0) {
2228
+ callbacks.splice(index, 1);
2229
+ return true;
2230
+ }
2231
+ }
2232
+ return false;
2233
+ };
2234
+ this._getTransportListeners = (event) => _transportListeners[event];
2235
+ this.addListener = function(channel, scope, callback) {
2236
+ if (arguments.length < 2) {
2237
+ throw "Illegal arguments number: required 2, got " + arguments.length;
2238
+ }
2239
+ if (!_isString(channel)) {
2240
+ throw "Illegal argument type: channel must be a string";
2241
+ }
2242
+ return _addListener(channel, scope, callback, true);
2243
+ };
2244
+ this.removeListener = (subscription) => {
2245
+ if (!subscription || !subscription.channel || !("id" in subscription)) {
2246
+ throw "Invalid argument: expected subscription, not " + subscription;
2247
+ }
2248
+ _removeListener(subscription);
2249
+ };
2250
+ this.clearListeners = () => {
2251
+ _listeners = {};
2252
+ };
2253
+ this.subscribe = function(channel, scope, callback, subscribeProps, subscribeCallback) {
2254
+ if (arguments.length < 2) {
2255
+ throw "Illegal arguments number: required 2, got " + arguments.length;
2256
+ }
2257
+ if (!_isValidChannel(channel)) {
2258
+ throw "Illegal argument: invalid channel " + channel;
2259
+ }
2260
+ if (_isDisconnected()) {
2261
+ throw "Illegal state: disconnected";
2262
+ }
2263
+ if (_isFunction(scope)) {
2264
+ subscribeCallback = subscribeProps;
2265
+ subscribeProps = callback;
2266
+ callback = scope;
2267
+ scope = void 0;
2268
+ }
2269
+ if (_isFunction(subscribeProps)) {
2270
+ subscribeCallback = subscribeProps;
2271
+ subscribeProps = void 0;
2272
+ }
2273
+ const send = !_hasSubscriptions(channel);
2274
+ const subscription = _addListener(channel, scope, callback, false);
2275
+ if (send) {
2276
+ const bayeuxMessage = {
2277
+ id: _nextMessageId(),
2278
+ channel: "/meta/subscribe",
2279
+ subscription: channel
2280
+ };
2281
+ const message = this._mixin(false, {}, subscribeProps, bayeuxMessage);
2282
+ _cometd._putCallback(message.id, subscribeCallback);
2283
+ _queueSend(message);
2284
+ } else {
2285
+ if (_isFunction(subscribeCallback)) {
2286
+ _cometd.setTimeout(() => {
2287
+ _notifyCallback(subscribeCallback, {
2288
+ id: _nextMessageId(),
2289
+ successful: true,
2290
+ channel: "/meta/subscribe",
2291
+ subscription: channel
2292
+ });
2293
+ }, 0);
2294
+ }
2295
+ }
2296
+ return subscription;
2297
+ };
2298
+ this.unsubscribe = function(subscription, unsubscribeProps, unsubscribeCallback) {
2299
+ if (arguments.length < 1) {
2300
+ throw "Illegal arguments number: required 1, got " + arguments.length;
2301
+ }
2302
+ if (_isDisconnected()) {
2303
+ throw "Illegal state: disconnected";
2304
+ }
2305
+ if (_isFunction(unsubscribeProps)) {
2306
+ unsubscribeCallback = unsubscribeProps;
2307
+ unsubscribeProps = void 0;
2308
+ }
2309
+ this.removeListener(subscription);
2310
+ const channel = subscription.channel;
2311
+ if (!_hasSubscriptions(channel)) {
2312
+ const bayeuxMessage = {
2313
+ id: _nextMessageId(),
2314
+ channel: "/meta/unsubscribe",
2315
+ subscription: channel
2316
+ };
2317
+ const message = this._mixin(false, {}, unsubscribeProps, bayeuxMessage);
2318
+ _cometd._putCallback(message.id, unsubscribeCallback);
2319
+ _queueSend(message);
2320
+ } else {
2321
+ if (_isFunction(unsubscribeCallback)) {
2322
+ _cometd.setTimeout(() => {
2323
+ _notifyCallback(unsubscribeCallback, {
2324
+ id: _nextMessageId(),
2325
+ successful: true,
2326
+ channel: "/meta/unsubscribe",
2327
+ subscription: channel
2328
+ });
2329
+ }, 0);
2330
+ }
2331
+ }
2332
+ };
2333
+ this.resubscribe = function(subscription, subscribeProps) {
2334
+ _removeSubscription(subscription);
2335
+ if (subscription) {
2336
+ return this.subscribe(subscription.channel, subscription.scope, subscription.callback, subscribeProps);
2337
+ }
2338
+ return void 0;
2339
+ };
2340
+ this.clearSubscriptions = () => {
2341
+ _clearSubscriptions();
2342
+ };
2343
+ this.publish = function(channel, content, publishProps, publishCallback) {
2344
+ if (arguments.length < 1) {
2345
+ throw "Illegal arguments number: required 1, got " + arguments.length;
2346
+ }
2347
+ if (!_isValidChannel(channel)) {
2348
+ throw "Illegal argument: invalid channel " + channel;
2349
+ }
2350
+ if (/^\/meta\//.test(channel)) {
2351
+ throw "Illegal argument: cannot publish to meta channels";
2352
+ }
2353
+ if (_isDisconnected()) {
2354
+ throw "Illegal state: disconnected";
2355
+ }
2356
+ if (_isFunction(content)) {
2357
+ publishCallback = content;
2358
+ content = {};
2359
+ publishProps = void 0;
2360
+ } else if (_isFunction(publishProps)) {
2361
+ publishCallback = publishProps;
2362
+ publishProps = void 0;
2363
+ }
2364
+ const bayeuxMessage = {
2365
+ id: _nextMessageId(),
2366
+ channel,
2367
+ data: content
2368
+ };
2369
+ const message = this._mixin(false, {}, publishProps, bayeuxMessage);
2370
+ _cometd._putCallback(message.id, publishCallback);
2371
+ _queueSend(message);
2372
+ };
2373
+ this.publishBinary = function(channel, data, last, meta, publishProps, publishCallback) {
2374
+ if (_isFunction(data)) {
2375
+ publishCallback = data;
2376
+ data = new ArrayBuffer(0);
2377
+ last = true;
2378
+ meta = void 0;
2379
+ publishProps = void 0;
2380
+ } else if (_isFunction(last)) {
2381
+ publishCallback = last;
2382
+ last = true;
2383
+ meta = void 0;
2384
+ publishProps = void 0;
2385
+ } else if (_isFunction(meta)) {
2386
+ publishCallback = meta;
2387
+ meta = void 0;
2388
+ publishProps = void 0;
2389
+ } else if (_isFunction(publishProps)) {
2390
+ publishCallback = publishProps;
2391
+ publishProps = void 0;
2392
+ }
2393
+ const content = {
2394
+ meta,
2395
+ data,
2396
+ last
2397
+ };
2398
+ const ext = this._mixin(false, publishProps, {
2399
+ ext: {
2400
+ binary: {}
2401
+ }
2402
+ });
2403
+ this.publish(channel, content, ext, publishCallback);
2404
+ };
2405
+ this.remoteCall = function(target, content, timeout, callProps, callback) {
2406
+ if (arguments.length < 1) {
2407
+ throw "Illegal arguments number: required 1, got " + arguments.length;
2408
+ }
2409
+ if (!_isString(target)) {
2410
+ throw "Illegal argument type: target must be a string";
2411
+ }
2412
+ if (_isDisconnected()) {
2413
+ throw "Illegal state: disconnected";
2414
+ }
2415
+ if (_isFunction(content)) {
2416
+ callback = content;
2417
+ content = {};
2418
+ timeout = _config.maxNetworkDelay;
2419
+ callProps = void 0;
2420
+ } else if (_isFunction(timeout)) {
2421
+ callback = timeout;
2422
+ timeout = _config.maxNetworkDelay;
2423
+ callProps = void 0;
2424
+ } else if (_isFunction(callProps)) {
2425
+ callback = callProps;
2426
+ callProps = void 0;
2427
+ }
2428
+ if (typeof timeout !== "number") {
2429
+ throw "Illegal argument type: timeout must be a number";
2430
+ }
2431
+ if (!target.match(/^\//)) {
2432
+ target = "/" + target;
2433
+ }
2434
+ const channel = "/service" + target;
2435
+ if (!_isValidChannel(channel)) {
2436
+ throw "Illegal argument: invalid target " + target;
2437
+ }
2438
+ const bayeuxMessage = {
2439
+ id: _nextMessageId(),
2440
+ channel,
2441
+ data: content
2442
+ };
2443
+ const message = this._mixin(false, {}, callProps, bayeuxMessage);
2444
+ const context = {
2445
+ callback
2446
+ };
2447
+ if (timeout > 0) {
2448
+ context.timeout = _cometd.setTimeout(() => {
2449
+ _cometd._debug("Timing out remote call", message, "after", timeout, "ms");
2450
+ _failMessage({
2451
+ id: message.id,
2452
+ error: "406::timeout",
2453
+ successful: false,
2454
+ failure: {
2455
+ message,
2456
+ reason: "Remote Call Timeout"
2457
+ }
2458
+ });
2459
+ }, timeout);
2460
+ _cometd._debug("Scheduled remote call timeout", message, "in", timeout, "ms");
2461
+ }
2462
+ _remoteCalls[message.id] = context;
2463
+ _queueSend(message);
2464
+ };
2465
+ this.remoteCallBinary = function(target, data, last, meta, timeout, callProps, callback) {
2466
+ if (_isFunction(data)) {
2467
+ callback = data;
2468
+ data = new ArrayBuffer(0);
2469
+ last = true;
2470
+ meta = void 0;
2471
+ timeout = _config.maxNetworkDelay;
2472
+ callProps = void 0;
2473
+ } else if (_isFunction(last)) {
2474
+ callback = last;
2475
+ last = true;
2476
+ meta = void 0;
2477
+ timeout = _config.maxNetworkDelay;
2478
+ callProps = void 0;
2479
+ } else if (_isFunction(meta)) {
2480
+ callback = meta;
2481
+ meta = void 0;
2482
+ timeout = _config.maxNetworkDelay;
2483
+ callProps = void 0;
2484
+ } else if (_isFunction(timeout)) {
2485
+ callback = timeout;
2486
+ timeout = _config.maxNetworkDelay;
2487
+ callProps = void 0;
2488
+ } else if (_isFunction(callProps)) {
2489
+ callback = callProps;
2490
+ callProps = void 0;
2491
+ }
2492
+ const content = {
2493
+ meta,
2494
+ data,
2495
+ last
2496
+ };
2497
+ const ext = this._mixin(false, callProps, {
2498
+ ext: {
2499
+ binary: {}
2500
+ }
2501
+ });
2502
+ this.remoteCall(target, content, timeout, ext, callback);
2503
+ };
2504
+ this.getStatus = () => _status;
2505
+ this.isDisconnected = _isDisconnected;
2506
+ this.setBackoffIncrement = (period) => {
2507
+ _config.backoffIncrement = period;
2508
+ };
2509
+ this.getBackoffIncrement = () => _config.backoffIncrement;
2510
+ this.getBackoffPeriod = () => _backoff;
2511
+ this.increaseBackoffPeriod = () => _increaseBackoff();
2512
+ this.resetBackoffPeriod = () => {
2513
+ _resetBackoff();
2514
+ };
2515
+ this.setLogLevel = (level) => {
2516
+ _config.logLevel = level;
2517
+ };
2518
+ this.registerExtension = function(name2, extension) {
2519
+ if (arguments.length < 2) {
2520
+ throw "Illegal arguments number: required 2, got " + arguments.length;
2521
+ }
2522
+ if (!_isString(name2)) {
2523
+ throw "Illegal argument type: extension name must be a string";
2524
+ }
2525
+ let existing = false;
2526
+ for (let i = 0; i < _extensions.length; ++i) {
2527
+ const existingExtension = _extensions[i];
2528
+ if (existingExtension.name === name2) {
2529
+ existing = true;
2530
+ break;
2531
+ }
2532
+ }
2533
+ if (!existing) {
2534
+ _extensions.push({
2535
+ name: name2,
2536
+ extension
2537
+ });
2538
+ this._debug("Registered extension", name2);
2539
+ if (_isFunction(extension.registered)) {
2540
+ extension.registered(name2, this);
2541
+ }
2542
+ return true;
2543
+ } else {
2544
+ this._info("Could not register extension with name", name2, "since another extension with the same name already exists");
2545
+ return false;
2546
+ }
2547
+ };
2548
+ this.unregisterExtension = function(name2) {
2549
+ if (!_isString(name2)) {
2550
+ throw "Illegal argument type: extension name must be a string";
2551
+ }
2552
+ let unregistered = false;
2553
+ for (let i = 0; i < _extensions.length; ++i) {
2554
+ const extension = _extensions[i];
2555
+ if (extension.name === name2) {
2556
+ _extensions.splice(i, 1);
2557
+ unregistered = true;
2558
+ this._debug("Unregistered extension", name2);
2559
+ const ext = extension.extension;
2560
+ if (_isFunction(ext.unregistered)) {
2561
+ ext.unregistered();
2562
+ }
2563
+ break;
2564
+ }
2565
+ }
2566
+ return unregistered;
2567
+ };
2568
+ this.getExtension = (name2) => {
2569
+ for (let i = 0; i < _extensions.length; ++i) {
2570
+ const extension = _extensions[i];
2571
+ if (extension.name === name2) {
2572
+ return extension.extension;
2573
+ }
2574
+ }
2575
+ return null;
2576
+ };
2577
+ this.getName = () => _name;
2578
+ this.getClientId = () => _clientId;
2579
+ this.getURL = () => {
2580
+ if (_transport) {
2581
+ let url = _transport.getURL();
2582
+ if (url) {
2583
+ return url;
2584
+ }
2585
+ url = _config.urls[_transport.getType()];
2586
+ if (url) {
2587
+ return url;
2588
+ }
2589
+ }
2590
+ return _config.url;
2591
+ };
2592
+ this.getTransport = () => _transport;
2593
+ this.getConfiguration = function() {
2594
+ return this._mixin(true, {}, _config);
2595
+ };
2596
+ this.getAdvice = function() {
2597
+ return this._mixin(true, {}, _advice);
2598
+ };
2599
+ this.setTimeout = (funktion, delay) => _scheduler.setTimeout(() => {
2600
+ try {
2601
+ _cometd._debug("Invoking timed function", funktion);
2602
+ funktion();
2603
+ } catch (x) {
2604
+ _cometd._debug("Exception invoking timed function", funktion, x);
2605
+ }
2606
+ }, delay);
2607
+ this.clearTimeout = (id) => {
2608
+ _scheduler.clearTimeout(id);
2609
+ };
2610
+ if (window.WebSocket) {
2611
+ this.registerTransport("websocket", new WebSocketTransport());
2612
+ }
2613
+ this.registerTransport("long-polling", new LongPollingTransport());
2614
+ this.registerTransport("callback-polling", new CallbackPollingTransport());
2615
+ }
2616
+ const _z85EncodeTable = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", ".", "-", ":", "+", "=", "^", "!", "/", "*", "?", "&", "<", ">", "(", ")", "[", "]", "{", "}", "@", "%", "$", "#"];
2617
+ const _z85DecodeTable = [0, 68, 0, 84, 83, 82, 72, 0, 75, 76, 70, 65, 0, 63, 62, 69, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 64, 0, 73, 66, 74, 71, 81, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 0, 78, 67, 0, 0, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 79, 0, 80, 0, 0];
2618
+ const Z85 = {
2619
+ /**
2620
+ * Encodes the given bytes to a string.
2621
+ * @param bytes the bytes to encode, either a number[], or an ArrayBuffer, or a TypedArray.
2622
+ * @return {string} the bytes encoded as a string
2623
+ */
2624
+ encode: (bytes) => {
2625
+ let buffer = null;
2626
+ if (bytes instanceof ArrayBuffer) {
2627
+ buffer = bytes;
2628
+ } else if (bytes.buffer instanceof ArrayBuffer) {
2629
+ buffer = bytes.buffer;
2630
+ } else if (Array.isArray(bytes)) {
2631
+ buffer = new Uint8Array(bytes).buffer;
2632
+ }
2633
+ if (buffer == null) {
2634
+ throw "Cannot Z85 encode " + bytes;
2635
+ }
2636
+ const length = buffer.byteLength;
2637
+ const remainder = length % 4;
2638
+ const padding = 4 - (remainder === 0 ? 4 : remainder);
2639
+ const view = new DataView(buffer);
2640
+ let result = "";
2641
+ let value = 0;
2642
+ for (let i = 0; i < length + padding; ++i) {
2643
+ const isPadding = i >= length;
2644
+ value = value * 256 + (isPadding ? 0 : view.getUint8(i));
2645
+ if ((i + 1) % 4 === 0) {
2646
+ let divisor = 85 * 85 * 85 * 85;
2647
+ for (let j = 5; j > 0; --j) {
2648
+ if (!isPadding || j > padding) {
2649
+ const code = Math.floor(value / divisor) % 85;
2650
+ result += _z85EncodeTable[code];
2651
+ }
2652
+ divisor /= 85;
2653
+ }
2654
+ value = 0;
2655
+ }
2656
+ }
2657
+ return result;
2658
+ },
2659
+ /**
2660
+ * Decodes the given string into an ArrayBuffer.
2661
+ * @param string the string to decode
2662
+ * @return {ArrayBuffer} the decoded bytes
2663
+ */
2664
+ decode: (string) => {
2665
+ const remainder = string.length % 5;
2666
+ const padding = 5 - (remainder === 0 ? 5 : remainder);
2667
+ for (let p = 0; p < padding; ++p) {
2668
+ string += _z85EncodeTable[_z85EncodeTable.length - 1];
2669
+ }
2670
+ const length = string.length;
2671
+ const buffer = new ArrayBuffer(length * 4 / 5 - padding);
2672
+ const view = new DataView(buffer);
2673
+ let value = 0;
2674
+ let charIdx = 0;
2675
+ let byteIdx = 0;
2676
+ for (let i = 0; i < length; ++i) {
2677
+ const code = string.charCodeAt(charIdx++) - 32;
2678
+ value = value * 85 + _z85DecodeTable[code];
2679
+ if (charIdx % 5 === 0) {
2680
+ let divisor = 256 * 256 * 256;
2681
+ while (divisor >= 1) {
2682
+ if (byteIdx < view.byteLength) {
2683
+ view.setUint8(byteIdx++, Math.floor(value / divisor) % 256);
2684
+ }
2685
+ divisor /= 256;
2686
+ }
2687
+ value = 0;
2688
+ }
2689
+ }
2690
+ return buffer;
2691
+ }
2692
+ };
2693
+ return {
2694
+ CometD,
2695
+ Transport,
2696
+ RequestTransport,
2697
+ LongPollingTransport,
2698
+ CallbackPollingTransport,
2699
+ WebSocketTransport,
2700
+ Utils,
2701
+ Z85
2702
+ };
2703
+ });
2704
+ }
2705
+ });
2706
+
2707
+ export { require_cometd };
2708
+ //# sourceMappingURL=chunk-QN5LGQFJ.js.map
2709
+ //# sourceMappingURL=chunk-QN5LGQFJ.js.map