@bluecopa/core 0.1.6 → 0.1.8
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.
- package/README.md +157 -8
- package/dist/config.d.ts +1 -0
- package/dist/index.es.js +3569 -3
- package/dist/index.es.js.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/websockets/centrifugoWebsocket.d.ts +18 -0
- package/dist/utils/websockets/websocketProviderFactory.d.ts +13 -0
- package/package.json +3 -2
package/dist/index.es.js
CHANGED
|
@@ -8,7 +8,8 @@ class ConfigSingleton {
|
|
|
8
8
|
this.config = {
|
|
9
9
|
apiBaseUrl: "",
|
|
10
10
|
accessToken: "",
|
|
11
|
-
workspaceId: ""
|
|
11
|
+
workspaceId: "",
|
|
12
|
+
userId: ""
|
|
12
13
|
};
|
|
13
14
|
}
|
|
14
15
|
static getInstance() {
|
|
@@ -27,7 +28,8 @@ class ConfigSingleton {
|
|
|
27
28
|
this.config = {
|
|
28
29
|
apiBaseUrl: "",
|
|
29
30
|
accessToken: "",
|
|
30
|
-
workspaceId: ""
|
|
31
|
+
workspaceId: "",
|
|
32
|
+
userId: ""
|
|
31
33
|
};
|
|
32
34
|
}
|
|
33
35
|
}
|
|
@@ -1357,11 +1359,3575 @@ const inputTableDefinition = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Objec
|
|
|
1357
1359
|
getMetricSeries,
|
|
1358
1360
|
sortConfigFromValue
|
|
1359
1361
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
1362
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
1363
|
+
function adopt(value) {
|
|
1364
|
+
return value instanceof P ? value : new P(function(resolve) {
|
|
1365
|
+
resolve(value);
|
|
1366
|
+
});
|
|
1367
|
+
}
|
|
1368
|
+
return new (P || (P = Promise))(function(resolve, reject) {
|
|
1369
|
+
function fulfilled(value) {
|
|
1370
|
+
try {
|
|
1371
|
+
step(generator.next(value));
|
|
1372
|
+
} catch (e) {
|
|
1373
|
+
reject(e);
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
function rejected(value) {
|
|
1377
|
+
try {
|
|
1378
|
+
step(generator["throw"](value));
|
|
1379
|
+
} catch (e) {
|
|
1380
|
+
reject(e);
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
function step(result) {
|
|
1384
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
1385
|
+
}
|
|
1386
|
+
step((generator = generator.apply(thisArg, [])).next());
|
|
1387
|
+
});
|
|
1388
|
+
}
|
|
1389
|
+
typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
1390
|
+
var e = new Error(message);
|
|
1391
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
1392
|
+
};
|
|
1393
|
+
function getDefaultExportFromCjs(x) {
|
|
1394
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
|
|
1395
|
+
}
|
|
1396
|
+
var events = { exports: {} };
|
|
1397
|
+
var R = typeof Reflect === "object" ? Reflect : null;
|
|
1398
|
+
var ReflectApply = R && typeof R.apply === "function" ? R.apply : function ReflectApply2(target, receiver, args) {
|
|
1399
|
+
return Function.prototype.apply.call(target, receiver, args);
|
|
1400
|
+
};
|
|
1401
|
+
var ReflectOwnKeys;
|
|
1402
|
+
if (R && typeof R.ownKeys === "function") {
|
|
1403
|
+
ReflectOwnKeys = R.ownKeys;
|
|
1404
|
+
} else if (Object.getOwnPropertySymbols) {
|
|
1405
|
+
ReflectOwnKeys = function ReflectOwnKeys2(target) {
|
|
1406
|
+
return Object.getOwnPropertyNames(target).concat(Object.getOwnPropertySymbols(target));
|
|
1407
|
+
};
|
|
1408
|
+
} else {
|
|
1409
|
+
ReflectOwnKeys = function ReflectOwnKeys2(target) {
|
|
1410
|
+
return Object.getOwnPropertyNames(target);
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
function ProcessEmitWarning(warning) {
|
|
1414
|
+
if (console && console.warn) console.warn(warning);
|
|
1415
|
+
}
|
|
1416
|
+
var NumberIsNaN = Number.isNaN || function NumberIsNaN2(value) {
|
|
1417
|
+
return value !== value;
|
|
1418
|
+
};
|
|
1419
|
+
function EventEmitter() {
|
|
1420
|
+
EventEmitter.init.call(this);
|
|
1421
|
+
}
|
|
1422
|
+
events.exports = EventEmitter;
|
|
1423
|
+
events.exports.once = once2;
|
|
1424
|
+
EventEmitter.EventEmitter = EventEmitter;
|
|
1425
|
+
EventEmitter.prototype._events = void 0;
|
|
1426
|
+
EventEmitter.prototype._eventsCount = 0;
|
|
1427
|
+
EventEmitter.prototype._maxListeners = void 0;
|
|
1428
|
+
var defaultMaxListeners = 10;
|
|
1429
|
+
function checkListener(listener) {
|
|
1430
|
+
if (typeof listener !== "function") {
|
|
1431
|
+
throw new TypeError('The "listener" argument must be of type Function. Received type ' + typeof listener);
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
Object.defineProperty(EventEmitter, "defaultMaxListeners", {
|
|
1435
|
+
enumerable: true,
|
|
1436
|
+
get: function() {
|
|
1437
|
+
return defaultMaxListeners;
|
|
1438
|
+
},
|
|
1439
|
+
set: function(arg) {
|
|
1440
|
+
if (typeof arg !== "number" || arg < 0 || NumberIsNaN(arg)) {
|
|
1441
|
+
throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received ' + arg + ".");
|
|
1442
|
+
}
|
|
1443
|
+
defaultMaxListeners = arg;
|
|
1444
|
+
}
|
|
1445
|
+
});
|
|
1446
|
+
EventEmitter.init = function() {
|
|
1447
|
+
if (this._events === void 0 || this._events === Object.getPrototypeOf(this)._events) {
|
|
1448
|
+
this._events = /* @__PURE__ */ Object.create(null);
|
|
1449
|
+
this._eventsCount = 0;
|
|
1450
|
+
}
|
|
1451
|
+
this._maxListeners = this._maxListeners || void 0;
|
|
1452
|
+
};
|
|
1453
|
+
EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
|
|
1454
|
+
if (typeof n !== "number" || n < 0 || NumberIsNaN(n)) {
|
|
1455
|
+
throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received ' + n + ".");
|
|
1456
|
+
}
|
|
1457
|
+
this._maxListeners = n;
|
|
1458
|
+
return this;
|
|
1459
|
+
};
|
|
1460
|
+
function _getMaxListeners(that) {
|
|
1461
|
+
if (that._maxListeners === void 0)
|
|
1462
|
+
return EventEmitter.defaultMaxListeners;
|
|
1463
|
+
return that._maxListeners;
|
|
1464
|
+
}
|
|
1465
|
+
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
|
|
1466
|
+
return _getMaxListeners(this);
|
|
1467
|
+
};
|
|
1468
|
+
EventEmitter.prototype.emit = function emit(type) {
|
|
1469
|
+
var args = [];
|
|
1470
|
+
for (var i = 1; i < arguments.length; i++) args.push(arguments[i]);
|
|
1471
|
+
var doError = type === "error";
|
|
1472
|
+
var events2 = this._events;
|
|
1473
|
+
if (events2 !== void 0)
|
|
1474
|
+
doError = doError && events2.error === void 0;
|
|
1475
|
+
else if (!doError)
|
|
1476
|
+
return false;
|
|
1477
|
+
if (doError) {
|
|
1478
|
+
var er;
|
|
1479
|
+
if (args.length > 0)
|
|
1480
|
+
er = args[0];
|
|
1481
|
+
if (er instanceof Error) {
|
|
1482
|
+
throw er;
|
|
1483
|
+
}
|
|
1484
|
+
var err = new Error("Unhandled error." + (er ? " (" + er.message + ")" : ""));
|
|
1485
|
+
err.context = er;
|
|
1486
|
+
throw err;
|
|
1487
|
+
}
|
|
1488
|
+
var handler = events2[type];
|
|
1489
|
+
if (handler === void 0)
|
|
1490
|
+
return false;
|
|
1491
|
+
if (typeof handler === "function") {
|
|
1492
|
+
ReflectApply(handler, this, args);
|
|
1493
|
+
} else {
|
|
1494
|
+
var len = handler.length;
|
|
1495
|
+
var listeners2 = arrayClone(handler, len);
|
|
1496
|
+
for (var i = 0; i < len; ++i)
|
|
1497
|
+
ReflectApply(listeners2[i], this, args);
|
|
1498
|
+
}
|
|
1499
|
+
return true;
|
|
1500
|
+
};
|
|
1501
|
+
function _addListener(target, type, listener, prepend) {
|
|
1502
|
+
var m;
|
|
1503
|
+
var events2;
|
|
1504
|
+
var existing;
|
|
1505
|
+
checkListener(listener);
|
|
1506
|
+
events2 = target._events;
|
|
1507
|
+
if (events2 === void 0) {
|
|
1508
|
+
events2 = target._events = /* @__PURE__ */ Object.create(null);
|
|
1509
|
+
target._eventsCount = 0;
|
|
1510
|
+
} else {
|
|
1511
|
+
if (events2.newListener !== void 0) {
|
|
1512
|
+
target.emit(
|
|
1513
|
+
"newListener",
|
|
1514
|
+
type,
|
|
1515
|
+
listener.listener ? listener.listener : listener
|
|
1516
|
+
);
|
|
1517
|
+
events2 = target._events;
|
|
1518
|
+
}
|
|
1519
|
+
existing = events2[type];
|
|
1520
|
+
}
|
|
1521
|
+
if (existing === void 0) {
|
|
1522
|
+
existing = events2[type] = listener;
|
|
1523
|
+
++target._eventsCount;
|
|
1524
|
+
} else {
|
|
1525
|
+
if (typeof existing === "function") {
|
|
1526
|
+
existing = events2[type] = prepend ? [listener, existing] : [existing, listener];
|
|
1527
|
+
} else if (prepend) {
|
|
1528
|
+
existing.unshift(listener);
|
|
1529
|
+
} else {
|
|
1530
|
+
existing.push(listener);
|
|
1531
|
+
}
|
|
1532
|
+
m = _getMaxListeners(target);
|
|
1533
|
+
if (m > 0 && existing.length > m && !existing.warned) {
|
|
1534
|
+
existing.warned = true;
|
|
1535
|
+
var w = new Error("Possible EventEmitter memory leak detected. " + existing.length + " " + String(type) + " listeners added. Use emitter.setMaxListeners() to increase limit");
|
|
1536
|
+
w.name = "MaxListenersExceededWarning";
|
|
1537
|
+
w.emitter = target;
|
|
1538
|
+
w.type = type;
|
|
1539
|
+
w.count = existing.length;
|
|
1540
|
+
ProcessEmitWarning(w);
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
return target;
|
|
1544
|
+
}
|
|
1545
|
+
EventEmitter.prototype.addListener = function addListener(type, listener) {
|
|
1546
|
+
return _addListener(this, type, listener, false);
|
|
1547
|
+
};
|
|
1548
|
+
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
|
1549
|
+
EventEmitter.prototype.prependListener = function prependListener(type, listener) {
|
|
1550
|
+
return _addListener(this, type, listener, true);
|
|
1551
|
+
};
|
|
1552
|
+
function onceWrapper() {
|
|
1553
|
+
if (!this.fired) {
|
|
1554
|
+
this.target.removeListener(this.type, this.wrapFn);
|
|
1555
|
+
this.fired = true;
|
|
1556
|
+
if (arguments.length === 0)
|
|
1557
|
+
return this.listener.call(this.target);
|
|
1558
|
+
return this.listener.apply(this.target, arguments);
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
function _onceWrap(target, type, listener) {
|
|
1562
|
+
var state = { fired: false, wrapFn: void 0, target, type, listener };
|
|
1563
|
+
var wrapped = onceWrapper.bind(state);
|
|
1564
|
+
wrapped.listener = listener;
|
|
1565
|
+
state.wrapFn = wrapped;
|
|
1566
|
+
return wrapped;
|
|
1567
|
+
}
|
|
1568
|
+
EventEmitter.prototype.once = function once(type, listener) {
|
|
1569
|
+
checkListener(listener);
|
|
1570
|
+
this.on(type, _onceWrap(this, type, listener));
|
|
1571
|
+
return this;
|
|
1572
|
+
};
|
|
1573
|
+
EventEmitter.prototype.prependOnceListener = function prependOnceListener(type, listener) {
|
|
1574
|
+
checkListener(listener);
|
|
1575
|
+
this.prependListener(type, _onceWrap(this, type, listener));
|
|
1576
|
+
return this;
|
|
1577
|
+
};
|
|
1578
|
+
EventEmitter.prototype.removeListener = function removeListener(type, listener) {
|
|
1579
|
+
var list, events2, position, i, originalListener;
|
|
1580
|
+
checkListener(listener);
|
|
1581
|
+
events2 = this._events;
|
|
1582
|
+
if (events2 === void 0)
|
|
1583
|
+
return this;
|
|
1584
|
+
list = events2[type];
|
|
1585
|
+
if (list === void 0)
|
|
1586
|
+
return this;
|
|
1587
|
+
if (list === listener || list.listener === listener) {
|
|
1588
|
+
if (--this._eventsCount === 0)
|
|
1589
|
+
this._events = /* @__PURE__ */ Object.create(null);
|
|
1590
|
+
else {
|
|
1591
|
+
delete events2[type];
|
|
1592
|
+
if (events2.removeListener)
|
|
1593
|
+
this.emit("removeListener", type, list.listener || listener);
|
|
1594
|
+
}
|
|
1595
|
+
} else if (typeof list !== "function") {
|
|
1596
|
+
position = -1;
|
|
1597
|
+
for (i = list.length - 1; i >= 0; i--) {
|
|
1598
|
+
if (list[i] === listener || list[i].listener === listener) {
|
|
1599
|
+
originalListener = list[i].listener;
|
|
1600
|
+
position = i;
|
|
1601
|
+
break;
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
if (position < 0)
|
|
1605
|
+
return this;
|
|
1606
|
+
if (position === 0)
|
|
1607
|
+
list.shift();
|
|
1608
|
+
else {
|
|
1609
|
+
spliceOne(list, position);
|
|
1610
|
+
}
|
|
1611
|
+
if (list.length === 1)
|
|
1612
|
+
events2[type] = list[0];
|
|
1613
|
+
if (events2.removeListener !== void 0)
|
|
1614
|
+
this.emit("removeListener", type, originalListener || listener);
|
|
1615
|
+
}
|
|
1616
|
+
return this;
|
|
1617
|
+
};
|
|
1618
|
+
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
|
|
1619
|
+
EventEmitter.prototype.removeAllListeners = function removeAllListeners(type) {
|
|
1620
|
+
var listeners2, events2, i;
|
|
1621
|
+
events2 = this._events;
|
|
1622
|
+
if (events2 === void 0)
|
|
1623
|
+
return this;
|
|
1624
|
+
if (events2.removeListener === void 0) {
|
|
1625
|
+
if (arguments.length === 0) {
|
|
1626
|
+
this._events = /* @__PURE__ */ Object.create(null);
|
|
1627
|
+
this._eventsCount = 0;
|
|
1628
|
+
} else if (events2[type] !== void 0) {
|
|
1629
|
+
if (--this._eventsCount === 0)
|
|
1630
|
+
this._events = /* @__PURE__ */ Object.create(null);
|
|
1631
|
+
else
|
|
1632
|
+
delete events2[type];
|
|
1633
|
+
}
|
|
1634
|
+
return this;
|
|
1635
|
+
}
|
|
1636
|
+
if (arguments.length === 0) {
|
|
1637
|
+
var keys = Object.keys(events2);
|
|
1638
|
+
var key;
|
|
1639
|
+
for (i = 0; i < keys.length; ++i) {
|
|
1640
|
+
key = keys[i];
|
|
1641
|
+
if (key === "removeListener") continue;
|
|
1642
|
+
this.removeAllListeners(key);
|
|
1643
|
+
}
|
|
1644
|
+
this.removeAllListeners("removeListener");
|
|
1645
|
+
this._events = /* @__PURE__ */ Object.create(null);
|
|
1646
|
+
this._eventsCount = 0;
|
|
1647
|
+
return this;
|
|
1648
|
+
}
|
|
1649
|
+
listeners2 = events2[type];
|
|
1650
|
+
if (typeof listeners2 === "function") {
|
|
1651
|
+
this.removeListener(type, listeners2);
|
|
1652
|
+
} else if (listeners2 !== void 0) {
|
|
1653
|
+
for (i = listeners2.length - 1; i >= 0; i--) {
|
|
1654
|
+
this.removeListener(type, listeners2[i]);
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
return this;
|
|
1658
|
+
};
|
|
1659
|
+
function _listeners(target, type, unwrap) {
|
|
1660
|
+
var events2 = target._events;
|
|
1661
|
+
if (events2 === void 0)
|
|
1662
|
+
return [];
|
|
1663
|
+
var evlistener = events2[type];
|
|
1664
|
+
if (evlistener === void 0)
|
|
1665
|
+
return [];
|
|
1666
|
+
if (typeof evlistener === "function")
|
|
1667
|
+
return unwrap ? [evlistener.listener || evlistener] : [evlistener];
|
|
1668
|
+
return unwrap ? unwrapListeners(evlistener) : arrayClone(evlistener, evlistener.length);
|
|
1669
|
+
}
|
|
1670
|
+
EventEmitter.prototype.listeners = function listeners(type) {
|
|
1671
|
+
return _listeners(this, type, true);
|
|
1672
|
+
};
|
|
1673
|
+
EventEmitter.prototype.rawListeners = function rawListeners(type) {
|
|
1674
|
+
return _listeners(this, type, false);
|
|
1675
|
+
};
|
|
1676
|
+
EventEmitter.listenerCount = function(emitter, type) {
|
|
1677
|
+
if (typeof emitter.listenerCount === "function") {
|
|
1678
|
+
return emitter.listenerCount(type);
|
|
1679
|
+
} else {
|
|
1680
|
+
return listenerCount.call(emitter, type);
|
|
1681
|
+
}
|
|
1682
|
+
};
|
|
1683
|
+
EventEmitter.prototype.listenerCount = listenerCount;
|
|
1684
|
+
function listenerCount(type) {
|
|
1685
|
+
var events2 = this._events;
|
|
1686
|
+
if (events2 !== void 0) {
|
|
1687
|
+
var evlistener = events2[type];
|
|
1688
|
+
if (typeof evlistener === "function") {
|
|
1689
|
+
return 1;
|
|
1690
|
+
} else if (evlistener !== void 0) {
|
|
1691
|
+
return evlistener.length;
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
return 0;
|
|
1695
|
+
}
|
|
1696
|
+
EventEmitter.prototype.eventNames = function eventNames() {
|
|
1697
|
+
return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : [];
|
|
1698
|
+
};
|
|
1699
|
+
function arrayClone(arr, n) {
|
|
1700
|
+
var copy = new Array(n);
|
|
1701
|
+
for (var i = 0; i < n; ++i)
|
|
1702
|
+
copy[i] = arr[i];
|
|
1703
|
+
return copy;
|
|
1704
|
+
}
|
|
1705
|
+
function spliceOne(list, index2) {
|
|
1706
|
+
for (; index2 + 1 < list.length; index2++)
|
|
1707
|
+
list[index2] = list[index2 + 1];
|
|
1708
|
+
list.pop();
|
|
1709
|
+
}
|
|
1710
|
+
function unwrapListeners(arr) {
|
|
1711
|
+
var ret = new Array(arr.length);
|
|
1712
|
+
for (var i = 0; i < ret.length; ++i) {
|
|
1713
|
+
ret[i] = arr[i].listener || arr[i];
|
|
1714
|
+
}
|
|
1715
|
+
return ret;
|
|
1716
|
+
}
|
|
1717
|
+
function once2(emitter, name) {
|
|
1718
|
+
return new Promise(function(resolve, reject) {
|
|
1719
|
+
function errorListener(err) {
|
|
1720
|
+
emitter.removeListener(name, resolver);
|
|
1721
|
+
reject(err);
|
|
1722
|
+
}
|
|
1723
|
+
function resolver() {
|
|
1724
|
+
if (typeof emitter.removeListener === "function") {
|
|
1725
|
+
emitter.removeListener("error", errorListener);
|
|
1726
|
+
}
|
|
1727
|
+
resolve([].slice.call(arguments));
|
|
1728
|
+
}
|
|
1729
|
+
eventTargetAgnosticAddListener(emitter, name, resolver, { once: true });
|
|
1730
|
+
if (name !== "error") {
|
|
1731
|
+
addErrorHandlerIfEventEmitter(emitter, errorListener, { once: true });
|
|
1732
|
+
}
|
|
1733
|
+
});
|
|
1734
|
+
}
|
|
1735
|
+
function addErrorHandlerIfEventEmitter(emitter, handler, flags) {
|
|
1736
|
+
if (typeof emitter.on === "function") {
|
|
1737
|
+
eventTargetAgnosticAddListener(emitter, "error", handler, flags);
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
function eventTargetAgnosticAddListener(emitter, name, listener, flags) {
|
|
1741
|
+
if (typeof emitter.on === "function") {
|
|
1742
|
+
if (flags.once) {
|
|
1743
|
+
emitter.once(name, listener);
|
|
1744
|
+
} else {
|
|
1745
|
+
emitter.on(name, listener);
|
|
1746
|
+
}
|
|
1747
|
+
} else if (typeof emitter.addEventListener === "function") {
|
|
1748
|
+
emitter.addEventListener(name, function wrapListener(arg) {
|
|
1749
|
+
if (flags.once) {
|
|
1750
|
+
emitter.removeEventListener(name, wrapListener);
|
|
1751
|
+
}
|
|
1752
|
+
listener(arg);
|
|
1753
|
+
});
|
|
1754
|
+
} else {
|
|
1755
|
+
throw new TypeError('The "emitter" argument must be of type EventEmitter. Received type ' + typeof emitter);
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
var eventsExports = events.exports;
|
|
1759
|
+
var EventEmitter$1 = /* @__PURE__ */ getDefaultExportFromCjs(eventsExports);
|
|
1760
|
+
var errorCodes;
|
|
1761
|
+
(function(errorCodes2) {
|
|
1762
|
+
errorCodes2[errorCodes2["timeout"] = 1] = "timeout";
|
|
1763
|
+
errorCodes2[errorCodes2["transportClosed"] = 2] = "transportClosed";
|
|
1764
|
+
errorCodes2[errorCodes2["clientDisconnected"] = 3] = "clientDisconnected";
|
|
1765
|
+
errorCodes2[errorCodes2["clientClosed"] = 4] = "clientClosed";
|
|
1766
|
+
errorCodes2[errorCodes2["clientConnectToken"] = 5] = "clientConnectToken";
|
|
1767
|
+
errorCodes2[errorCodes2["clientRefreshToken"] = 6] = "clientRefreshToken";
|
|
1768
|
+
errorCodes2[errorCodes2["subscriptionUnsubscribed"] = 7] = "subscriptionUnsubscribed";
|
|
1769
|
+
errorCodes2[errorCodes2["subscriptionSubscribeToken"] = 8] = "subscriptionSubscribeToken";
|
|
1770
|
+
errorCodes2[errorCodes2["subscriptionRefreshToken"] = 9] = "subscriptionRefreshToken";
|
|
1771
|
+
errorCodes2[errorCodes2["transportWriteError"] = 10] = "transportWriteError";
|
|
1772
|
+
errorCodes2[errorCodes2["connectionClosed"] = 11] = "connectionClosed";
|
|
1773
|
+
errorCodes2[errorCodes2["badConfiguration"] = 12] = "badConfiguration";
|
|
1774
|
+
})(errorCodes || (errorCodes = {}));
|
|
1775
|
+
var connectingCodes;
|
|
1776
|
+
(function(connectingCodes2) {
|
|
1777
|
+
connectingCodes2[connectingCodes2["connectCalled"] = 0] = "connectCalled";
|
|
1778
|
+
connectingCodes2[connectingCodes2["transportClosed"] = 1] = "transportClosed";
|
|
1779
|
+
connectingCodes2[connectingCodes2["noPing"] = 2] = "noPing";
|
|
1780
|
+
connectingCodes2[connectingCodes2["subscribeTimeout"] = 3] = "subscribeTimeout";
|
|
1781
|
+
connectingCodes2[connectingCodes2["unsubscribeError"] = 4] = "unsubscribeError";
|
|
1782
|
+
})(connectingCodes || (connectingCodes = {}));
|
|
1783
|
+
var disconnectedCodes;
|
|
1784
|
+
(function(disconnectedCodes2) {
|
|
1785
|
+
disconnectedCodes2[disconnectedCodes2["disconnectCalled"] = 0] = "disconnectCalled";
|
|
1786
|
+
disconnectedCodes2[disconnectedCodes2["unauthorized"] = 1] = "unauthorized";
|
|
1787
|
+
disconnectedCodes2[disconnectedCodes2["badProtocol"] = 2] = "badProtocol";
|
|
1788
|
+
disconnectedCodes2[disconnectedCodes2["messageSizeLimit"] = 3] = "messageSizeLimit";
|
|
1789
|
+
})(disconnectedCodes || (disconnectedCodes = {}));
|
|
1790
|
+
var subscribingCodes;
|
|
1791
|
+
(function(subscribingCodes2) {
|
|
1792
|
+
subscribingCodes2[subscribingCodes2["subscribeCalled"] = 0] = "subscribeCalled";
|
|
1793
|
+
subscribingCodes2[subscribingCodes2["transportClosed"] = 1] = "transportClosed";
|
|
1794
|
+
})(subscribingCodes || (subscribingCodes = {}));
|
|
1795
|
+
var unsubscribedCodes;
|
|
1796
|
+
(function(unsubscribedCodes2) {
|
|
1797
|
+
unsubscribedCodes2[unsubscribedCodes2["unsubscribeCalled"] = 0] = "unsubscribeCalled";
|
|
1798
|
+
unsubscribedCodes2[unsubscribedCodes2["unauthorized"] = 1] = "unauthorized";
|
|
1799
|
+
unsubscribedCodes2[unsubscribedCodes2["clientClosed"] = 2] = "clientClosed";
|
|
1800
|
+
})(unsubscribedCodes || (unsubscribedCodes = {}));
|
|
1801
|
+
var State;
|
|
1802
|
+
(function(State2) {
|
|
1803
|
+
State2["Disconnected"] = "disconnected";
|
|
1804
|
+
State2["Connecting"] = "connecting";
|
|
1805
|
+
State2["Connected"] = "connected";
|
|
1806
|
+
})(State || (State = {}));
|
|
1807
|
+
var SubscriptionState;
|
|
1808
|
+
(function(SubscriptionState2) {
|
|
1809
|
+
SubscriptionState2["Unsubscribed"] = "unsubscribed";
|
|
1810
|
+
SubscriptionState2["Subscribing"] = "subscribing";
|
|
1811
|
+
SubscriptionState2["Subscribed"] = "subscribed";
|
|
1812
|
+
})(SubscriptionState || (SubscriptionState = {}));
|
|
1813
|
+
function startsWith(value, prefix) {
|
|
1814
|
+
return value.lastIndexOf(prefix, 0) === 0;
|
|
1815
|
+
}
|
|
1816
|
+
function isFunction(value) {
|
|
1817
|
+
if (value === void 0 || value === null) {
|
|
1818
|
+
return false;
|
|
1819
|
+
}
|
|
1820
|
+
return typeof value === "function";
|
|
1821
|
+
}
|
|
1822
|
+
function log(level, args) {
|
|
1823
|
+
if (globalThis.console) {
|
|
1824
|
+
const logger = globalThis.console[level];
|
|
1825
|
+
if (isFunction(logger)) {
|
|
1826
|
+
logger.apply(globalThis.console, args);
|
|
1827
|
+
}
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
function randomInt(min, max) {
|
|
1831
|
+
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
1832
|
+
}
|
|
1833
|
+
function backoff(step, min, max) {
|
|
1834
|
+
if (step > 31) {
|
|
1835
|
+
step = 31;
|
|
1836
|
+
}
|
|
1837
|
+
const interval = randomInt(0, Math.min(max, min * Math.pow(2, step)));
|
|
1838
|
+
return Math.min(max, min + interval);
|
|
1839
|
+
}
|
|
1840
|
+
function errorExists(data) {
|
|
1841
|
+
return "error" in data && data.error !== null;
|
|
1842
|
+
}
|
|
1843
|
+
function ttlMilliseconds(ttl) {
|
|
1844
|
+
return Math.min(ttl * 1e3, 2147483647);
|
|
1845
|
+
}
|
|
1846
|
+
class Subscription extends EventEmitter$1 {
|
|
1847
|
+
/** Subscription constructor should not be used directly, create subscriptions using Client method. */
|
|
1848
|
+
constructor(centrifuge, channel, options) {
|
|
1849
|
+
super();
|
|
1850
|
+
this._resubscribeTimeout = null;
|
|
1851
|
+
this._refreshTimeout = null;
|
|
1852
|
+
this.channel = channel;
|
|
1853
|
+
this.state = SubscriptionState.Unsubscribed;
|
|
1854
|
+
this._centrifuge = centrifuge;
|
|
1855
|
+
this._token = "";
|
|
1856
|
+
this._getToken = null;
|
|
1857
|
+
this._data = null;
|
|
1858
|
+
this._getData = null;
|
|
1859
|
+
this._recover = false;
|
|
1860
|
+
this._offset = null;
|
|
1861
|
+
this._epoch = null;
|
|
1862
|
+
this._recoverable = false;
|
|
1863
|
+
this._positioned = false;
|
|
1864
|
+
this._joinLeave = false;
|
|
1865
|
+
this._minResubscribeDelay = 500;
|
|
1866
|
+
this._maxResubscribeDelay = 2e4;
|
|
1867
|
+
this._resubscribeTimeout = null;
|
|
1868
|
+
this._resubscribeAttempts = 0;
|
|
1869
|
+
this._promises = {};
|
|
1870
|
+
this._promiseId = 0;
|
|
1871
|
+
this._inflight = false;
|
|
1872
|
+
this._refreshTimeout = null;
|
|
1873
|
+
this._delta = "";
|
|
1874
|
+
this._delta_negotiated = false;
|
|
1875
|
+
this._prevValue = null;
|
|
1876
|
+
this._unsubPromise = Promise.resolve();
|
|
1877
|
+
this._setOptions(options);
|
|
1878
|
+
if (this._centrifuge._debugEnabled) {
|
|
1879
|
+
this.on("state", (ctx) => {
|
|
1880
|
+
this._centrifuge._debug("subscription state", channel, ctx.oldState, "->", ctx.newState);
|
|
1881
|
+
});
|
|
1882
|
+
this.on("error", (ctx) => {
|
|
1883
|
+
this._centrifuge._debug("subscription error", channel, ctx);
|
|
1884
|
+
});
|
|
1885
|
+
} else {
|
|
1886
|
+
this.on("error", function() {
|
|
1887
|
+
Function.prototype();
|
|
1888
|
+
});
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
/** ready returns a Promise which resolves upon subscription goes to Subscribed
|
|
1892
|
+
* state and rejects in case of subscription goes to Unsubscribed state.
|
|
1893
|
+
* Optional timeout can be passed.*/
|
|
1894
|
+
ready(timeout) {
|
|
1895
|
+
if (this.state === SubscriptionState.Unsubscribed) {
|
|
1896
|
+
return Promise.reject({ code: errorCodes.subscriptionUnsubscribed, message: this.state });
|
|
1897
|
+
}
|
|
1898
|
+
if (this.state === SubscriptionState.Subscribed) {
|
|
1899
|
+
return Promise.resolve();
|
|
1900
|
+
}
|
|
1901
|
+
return new Promise((res, rej) => {
|
|
1902
|
+
const ctx = {
|
|
1903
|
+
resolve: res,
|
|
1904
|
+
reject: rej
|
|
1905
|
+
};
|
|
1906
|
+
if (timeout) {
|
|
1907
|
+
ctx.timeout = setTimeout(function() {
|
|
1908
|
+
rej({ code: errorCodes.timeout, message: "timeout" });
|
|
1909
|
+
}, timeout);
|
|
1910
|
+
}
|
|
1911
|
+
this._promises[this._nextPromiseId()] = ctx;
|
|
1912
|
+
});
|
|
1913
|
+
}
|
|
1914
|
+
/** subscribe to a channel.*/
|
|
1915
|
+
subscribe() {
|
|
1916
|
+
if (this._isSubscribed()) {
|
|
1917
|
+
return;
|
|
1918
|
+
}
|
|
1919
|
+
this._resubscribeAttempts = 0;
|
|
1920
|
+
this._setSubscribing(subscribingCodes.subscribeCalled, "subscribe called");
|
|
1921
|
+
}
|
|
1922
|
+
/** unsubscribe from a channel, keeping position state.*/
|
|
1923
|
+
unsubscribe() {
|
|
1924
|
+
this._unsubPromise = this._setUnsubscribed(unsubscribedCodes.unsubscribeCalled, "unsubscribe called", true);
|
|
1925
|
+
}
|
|
1926
|
+
/** publish data to a channel.*/
|
|
1927
|
+
publish(data) {
|
|
1928
|
+
const self = this;
|
|
1929
|
+
return this._methodCall().then(function() {
|
|
1930
|
+
return self._centrifuge.publish(self.channel, data);
|
|
1931
|
+
});
|
|
1932
|
+
}
|
|
1933
|
+
/** get online presence for a channel.*/
|
|
1934
|
+
presence() {
|
|
1935
|
+
const self = this;
|
|
1936
|
+
return this._methodCall().then(function() {
|
|
1937
|
+
return self._centrifuge.presence(self.channel);
|
|
1938
|
+
});
|
|
1939
|
+
}
|
|
1940
|
+
/** presence stats for a channel (num clients and unique users).*/
|
|
1941
|
+
presenceStats() {
|
|
1942
|
+
const self = this;
|
|
1943
|
+
return this._methodCall().then(function() {
|
|
1944
|
+
return self._centrifuge.presenceStats(self.channel);
|
|
1945
|
+
});
|
|
1946
|
+
}
|
|
1947
|
+
/** history for a channel. By default it does not return publications (only current
|
|
1948
|
+
* StreamPosition data) – provide an explicit limit > 0 to load publications.*/
|
|
1949
|
+
history(opts) {
|
|
1950
|
+
const self = this;
|
|
1951
|
+
return this._methodCall().then(function() {
|
|
1952
|
+
return self._centrifuge.history(self.channel, opts);
|
|
1953
|
+
});
|
|
1954
|
+
}
|
|
1955
|
+
_methodCall() {
|
|
1956
|
+
if (this._isSubscribed()) {
|
|
1957
|
+
return Promise.resolve();
|
|
1958
|
+
}
|
|
1959
|
+
if (this._isUnsubscribed()) {
|
|
1960
|
+
return Promise.reject({ code: errorCodes.subscriptionUnsubscribed, message: this.state });
|
|
1961
|
+
}
|
|
1962
|
+
return new Promise((res, rej) => {
|
|
1963
|
+
const timeout = setTimeout(function() {
|
|
1964
|
+
rej({ code: errorCodes.timeout, message: "timeout" });
|
|
1965
|
+
}, this._centrifuge._config.timeout);
|
|
1966
|
+
this._promises[this._nextPromiseId()] = {
|
|
1967
|
+
timeout,
|
|
1968
|
+
resolve: res,
|
|
1969
|
+
reject: rej
|
|
1970
|
+
};
|
|
1971
|
+
});
|
|
1972
|
+
}
|
|
1973
|
+
_nextPromiseId() {
|
|
1974
|
+
return ++this._promiseId;
|
|
1975
|
+
}
|
|
1976
|
+
_needRecover() {
|
|
1977
|
+
return this._recover === true;
|
|
1978
|
+
}
|
|
1979
|
+
_isUnsubscribed() {
|
|
1980
|
+
return this.state === SubscriptionState.Unsubscribed;
|
|
1981
|
+
}
|
|
1982
|
+
_isSubscribing() {
|
|
1983
|
+
return this.state === SubscriptionState.Subscribing;
|
|
1984
|
+
}
|
|
1985
|
+
_isSubscribed() {
|
|
1986
|
+
return this.state === SubscriptionState.Subscribed;
|
|
1987
|
+
}
|
|
1988
|
+
_setState(newState) {
|
|
1989
|
+
if (this.state !== newState) {
|
|
1990
|
+
const oldState = this.state;
|
|
1991
|
+
this.state = newState;
|
|
1992
|
+
this.emit("state", { newState, oldState, channel: this.channel });
|
|
1993
|
+
return true;
|
|
1994
|
+
}
|
|
1995
|
+
return false;
|
|
1996
|
+
}
|
|
1997
|
+
_usesToken() {
|
|
1998
|
+
return this._token !== "" || this._getToken !== null;
|
|
1999
|
+
}
|
|
2000
|
+
_clearSubscribingState() {
|
|
2001
|
+
this._resubscribeAttempts = 0;
|
|
2002
|
+
this._clearResubscribeTimeout();
|
|
2003
|
+
}
|
|
2004
|
+
_clearSubscribedState() {
|
|
2005
|
+
this._clearRefreshTimeout();
|
|
2006
|
+
}
|
|
2007
|
+
_setSubscribed(result) {
|
|
2008
|
+
if (!this._isSubscribing()) {
|
|
2009
|
+
return;
|
|
2010
|
+
}
|
|
2011
|
+
this._clearSubscribingState();
|
|
2012
|
+
if (result.recoverable) {
|
|
2013
|
+
this._recover = true;
|
|
2014
|
+
this._offset = result.offset || 0;
|
|
2015
|
+
this._epoch = result.epoch || "";
|
|
2016
|
+
}
|
|
2017
|
+
if (result.delta) {
|
|
2018
|
+
this._delta_negotiated = true;
|
|
2019
|
+
} else {
|
|
2020
|
+
this._delta_negotiated = false;
|
|
2021
|
+
}
|
|
2022
|
+
this._setState(SubscriptionState.Subscribed);
|
|
2023
|
+
const ctx = this._centrifuge._getSubscribeContext(this.channel, result);
|
|
2024
|
+
this.emit("subscribed", ctx);
|
|
2025
|
+
this._resolvePromises();
|
|
2026
|
+
const pubs = result.publications;
|
|
2027
|
+
if (pubs && pubs.length > 0) {
|
|
2028
|
+
for (const i in pubs) {
|
|
2029
|
+
if (!pubs.hasOwnProperty(i)) {
|
|
2030
|
+
continue;
|
|
2031
|
+
}
|
|
2032
|
+
this._handlePublication(pubs[i]);
|
|
2033
|
+
}
|
|
2034
|
+
}
|
|
2035
|
+
if (result.expires === true) {
|
|
2036
|
+
this._refreshTimeout = setTimeout(() => this._refresh(), ttlMilliseconds(result.ttl));
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
_setSubscribing(code, reason) {
|
|
2040
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2041
|
+
if (this._isSubscribing()) {
|
|
2042
|
+
return;
|
|
2043
|
+
}
|
|
2044
|
+
if (this._isSubscribed()) {
|
|
2045
|
+
this._clearSubscribedState();
|
|
2046
|
+
}
|
|
2047
|
+
if (this._setState(SubscriptionState.Subscribing)) {
|
|
2048
|
+
this.emit("subscribing", { channel: this.channel, code, reason });
|
|
2049
|
+
}
|
|
2050
|
+
if (this._centrifuge._transport && this._centrifuge._transport.emulation()) {
|
|
2051
|
+
yield this._unsubPromise;
|
|
2052
|
+
}
|
|
2053
|
+
if (!this._isSubscribing()) {
|
|
2054
|
+
return;
|
|
2055
|
+
}
|
|
2056
|
+
this._subscribe();
|
|
2057
|
+
});
|
|
2058
|
+
}
|
|
2059
|
+
_subscribe() {
|
|
2060
|
+
this._centrifuge._debug("subscribing on", this.channel);
|
|
2061
|
+
if (!this._centrifuge._transportIsOpen) {
|
|
2062
|
+
this._centrifuge._debug("delay subscribe on", this.channel, "till connected");
|
|
2063
|
+
return null;
|
|
2064
|
+
}
|
|
2065
|
+
const self = this;
|
|
2066
|
+
const getDataCtx = {
|
|
2067
|
+
channel: self.channel
|
|
2068
|
+
};
|
|
2069
|
+
if (!this._usesToken() || this._token) {
|
|
2070
|
+
if (self._getData) {
|
|
2071
|
+
self._getData(getDataCtx).then(function(data) {
|
|
2072
|
+
if (!self._isSubscribing()) {
|
|
2073
|
+
return;
|
|
2074
|
+
}
|
|
2075
|
+
self._data = data;
|
|
2076
|
+
self._sendSubscribe(self._token);
|
|
2077
|
+
});
|
|
2078
|
+
return null;
|
|
2079
|
+
} else {
|
|
2080
|
+
return self._sendSubscribe(self._token);
|
|
2081
|
+
}
|
|
2082
|
+
}
|
|
2083
|
+
this._getSubscriptionToken().then(function(token) {
|
|
2084
|
+
if (!self._isSubscribing()) {
|
|
2085
|
+
return;
|
|
2086
|
+
}
|
|
2087
|
+
if (!token) {
|
|
2088
|
+
self._failUnauthorized();
|
|
2089
|
+
return;
|
|
2090
|
+
}
|
|
2091
|
+
self._token = token;
|
|
2092
|
+
if (self._getData) {
|
|
2093
|
+
self._getData(getDataCtx).then(function(data) {
|
|
2094
|
+
if (!self._isSubscribing()) {
|
|
2095
|
+
return;
|
|
2096
|
+
}
|
|
2097
|
+
self._data = data;
|
|
2098
|
+
self._sendSubscribe(token);
|
|
2099
|
+
});
|
|
2100
|
+
} else {
|
|
2101
|
+
self._sendSubscribe(token);
|
|
2102
|
+
}
|
|
2103
|
+
}).catch(function(e) {
|
|
2104
|
+
if (!self._isSubscribing()) {
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
if (e instanceof UnauthorizedError) {
|
|
2108
|
+
self._failUnauthorized();
|
|
2109
|
+
return;
|
|
2110
|
+
}
|
|
2111
|
+
self.emit("error", {
|
|
2112
|
+
type: "subscribeToken",
|
|
2113
|
+
channel: self.channel,
|
|
2114
|
+
error: {
|
|
2115
|
+
code: errorCodes.subscriptionSubscribeToken,
|
|
2116
|
+
message: e !== void 0 ? e.toString() : ""
|
|
2117
|
+
}
|
|
2118
|
+
});
|
|
2119
|
+
self._scheduleResubscribe();
|
|
2120
|
+
});
|
|
2121
|
+
return null;
|
|
2122
|
+
}
|
|
2123
|
+
_sendSubscribe(token) {
|
|
2124
|
+
if (!this._centrifuge._transportIsOpen) {
|
|
2125
|
+
return null;
|
|
2126
|
+
}
|
|
2127
|
+
const channel = this.channel;
|
|
2128
|
+
const req = {
|
|
2129
|
+
channel
|
|
2130
|
+
};
|
|
2131
|
+
if (token) {
|
|
2132
|
+
req.token = token;
|
|
2133
|
+
}
|
|
2134
|
+
if (this._data) {
|
|
2135
|
+
req.data = this._data;
|
|
2136
|
+
}
|
|
2137
|
+
if (this._positioned) {
|
|
2138
|
+
req.positioned = true;
|
|
2139
|
+
}
|
|
2140
|
+
if (this._recoverable) {
|
|
2141
|
+
req.recoverable = true;
|
|
2142
|
+
}
|
|
2143
|
+
if (this._joinLeave) {
|
|
2144
|
+
req.join_leave = true;
|
|
2145
|
+
}
|
|
2146
|
+
if (this._needRecover()) {
|
|
2147
|
+
req.recover = true;
|
|
2148
|
+
const offset = this._getOffset();
|
|
2149
|
+
if (offset) {
|
|
2150
|
+
req.offset = offset;
|
|
2151
|
+
}
|
|
2152
|
+
const epoch = this._getEpoch();
|
|
2153
|
+
if (epoch) {
|
|
2154
|
+
req.epoch = epoch;
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
if (this._delta) {
|
|
2158
|
+
req.delta = this._delta;
|
|
2159
|
+
}
|
|
2160
|
+
const cmd = { subscribe: req };
|
|
2161
|
+
this._inflight = true;
|
|
2162
|
+
this._centrifuge._call(cmd).then((resolveCtx) => {
|
|
2163
|
+
this._inflight = false;
|
|
2164
|
+
const result = resolveCtx.reply.subscribe;
|
|
2165
|
+
this._handleSubscribeResponse(result);
|
|
2166
|
+
if (resolveCtx.next) {
|
|
2167
|
+
resolveCtx.next();
|
|
2168
|
+
}
|
|
2169
|
+
}, (rejectCtx) => {
|
|
2170
|
+
this._inflight = false;
|
|
2171
|
+
this._handleSubscribeError(rejectCtx.error);
|
|
2172
|
+
if (rejectCtx.next) {
|
|
2173
|
+
rejectCtx.next();
|
|
2174
|
+
}
|
|
2175
|
+
});
|
|
2176
|
+
return cmd;
|
|
2177
|
+
}
|
|
2178
|
+
_handleSubscribeError(error) {
|
|
2179
|
+
if (!this._isSubscribing()) {
|
|
2180
|
+
return;
|
|
2181
|
+
}
|
|
2182
|
+
if (error.code === errorCodes.timeout) {
|
|
2183
|
+
this._centrifuge._disconnect(connectingCodes.subscribeTimeout, "subscribe timeout", true);
|
|
2184
|
+
return;
|
|
2185
|
+
}
|
|
2186
|
+
this._subscribeError(error);
|
|
2187
|
+
}
|
|
2188
|
+
_handleSubscribeResponse(result) {
|
|
2189
|
+
if (!this._isSubscribing()) {
|
|
2190
|
+
return;
|
|
2191
|
+
}
|
|
2192
|
+
this._setSubscribed(result);
|
|
2193
|
+
}
|
|
2194
|
+
_setUnsubscribed(code, reason, sendUnsubscribe) {
|
|
2195
|
+
if (this._isUnsubscribed()) {
|
|
2196
|
+
return Promise.resolve();
|
|
2197
|
+
}
|
|
2198
|
+
let promise = Promise.resolve();
|
|
2199
|
+
if (this._isSubscribed()) {
|
|
2200
|
+
if (sendUnsubscribe) {
|
|
2201
|
+
promise = this._centrifuge._unsubscribe(this);
|
|
2202
|
+
}
|
|
2203
|
+
this._clearSubscribedState();
|
|
2204
|
+
} else if (this._isSubscribing()) {
|
|
2205
|
+
if (this._inflight && sendUnsubscribe) {
|
|
2206
|
+
promise = this._centrifuge._unsubscribe(this);
|
|
2207
|
+
}
|
|
2208
|
+
this._clearSubscribingState();
|
|
2209
|
+
}
|
|
2210
|
+
if (this._setState(SubscriptionState.Unsubscribed)) {
|
|
2211
|
+
this.emit("unsubscribed", { channel: this.channel, code, reason });
|
|
2212
|
+
}
|
|
2213
|
+
this._rejectPromises({ code: errorCodes.subscriptionUnsubscribed, message: this.state });
|
|
2214
|
+
return promise;
|
|
2215
|
+
}
|
|
2216
|
+
_handlePublication(pub) {
|
|
2217
|
+
if (this._delta && this._delta_negotiated) {
|
|
2218
|
+
const { newData, newPrevValue } = this._centrifuge._codec.applyDeltaIfNeeded(pub, this._prevValue);
|
|
2219
|
+
pub.data = newData;
|
|
2220
|
+
this._prevValue = newPrevValue;
|
|
2221
|
+
}
|
|
2222
|
+
const ctx = this._centrifuge._getPublicationContext(this.channel, pub);
|
|
2223
|
+
this.emit("publication", ctx);
|
|
2224
|
+
if (pub.offset) {
|
|
2225
|
+
this._offset = pub.offset;
|
|
2226
|
+
}
|
|
2227
|
+
}
|
|
2228
|
+
_handleJoin(join) {
|
|
2229
|
+
const info = this._centrifuge._getJoinLeaveContext(join.info);
|
|
2230
|
+
this.emit("join", { channel: this.channel, info });
|
|
2231
|
+
}
|
|
2232
|
+
_handleLeave(leave) {
|
|
2233
|
+
const info = this._centrifuge._getJoinLeaveContext(leave.info);
|
|
2234
|
+
this.emit("leave", { channel: this.channel, info });
|
|
2235
|
+
}
|
|
2236
|
+
_resolvePromises() {
|
|
2237
|
+
for (const id in this._promises) {
|
|
2238
|
+
if (!this._promises.hasOwnProperty(id)) {
|
|
2239
|
+
continue;
|
|
2240
|
+
}
|
|
2241
|
+
if (this._promises[id].timeout) {
|
|
2242
|
+
clearTimeout(this._promises[id].timeout);
|
|
2243
|
+
}
|
|
2244
|
+
this._promises[id].resolve();
|
|
2245
|
+
delete this._promises[id];
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
_rejectPromises(err) {
|
|
2249
|
+
for (const id in this._promises) {
|
|
2250
|
+
if (!this._promises.hasOwnProperty(id)) {
|
|
2251
|
+
continue;
|
|
2252
|
+
}
|
|
2253
|
+
if (this._promises[id].timeout) {
|
|
2254
|
+
clearTimeout(this._promises[id].timeout);
|
|
2255
|
+
}
|
|
2256
|
+
this._promises[id].reject(err);
|
|
2257
|
+
delete this._promises[id];
|
|
2258
|
+
}
|
|
2259
|
+
}
|
|
2260
|
+
_scheduleResubscribe() {
|
|
2261
|
+
const self = this;
|
|
2262
|
+
const delay = this._getResubscribeDelay();
|
|
2263
|
+
this._resubscribeTimeout = setTimeout(function() {
|
|
2264
|
+
if (self._isSubscribing()) {
|
|
2265
|
+
self._subscribe();
|
|
2266
|
+
}
|
|
2267
|
+
}, delay);
|
|
2268
|
+
}
|
|
2269
|
+
_subscribeError(err) {
|
|
2270
|
+
if (!this._isSubscribing()) {
|
|
2271
|
+
return;
|
|
2272
|
+
}
|
|
2273
|
+
if (err.code < 100 || err.code === 109 || err.temporary === true) {
|
|
2274
|
+
if (err.code === 109) {
|
|
2275
|
+
this._token = "";
|
|
2276
|
+
}
|
|
2277
|
+
const errContext = {
|
|
2278
|
+
channel: this.channel,
|
|
2279
|
+
type: "subscribe",
|
|
2280
|
+
error: err
|
|
2281
|
+
};
|
|
2282
|
+
if (this._centrifuge.state === State.Connected) {
|
|
2283
|
+
this.emit("error", errContext);
|
|
2284
|
+
}
|
|
2285
|
+
this._scheduleResubscribe();
|
|
2286
|
+
} else {
|
|
2287
|
+
this._setUnsubscribed(err.code, err.message, false);
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
_getResubscribeDelay() {
|
|
2291
|
+
const delay = backoff(this._resubscribeAttempts, this._minResubscribeDelay, this._maxResubscribeDelay);
|
|
2292
|
+
this._resubscribeAttempts++;
|
|
2293
|
+
return delay;
|
|
2294
|
+
}
|
|
2295
|
+
_setOptions(options) {
|
|
2296
|
+
if (!options) {
|
|
2297
|
+
return;
|
|
2298
|
+
}
|
|
2299
|
+
if (options.since) {
|
|
2300
|
+
this._offset = options.since.offset;
|
|
2301
|
+
this._epoch = options.since.epoch;
|
|
2302
|
+
this._recover = true;
|
|
2303
|
+
}
|
|
2304
|
+
if (options.data) {
|
|
2305
|
+
this._data = options.data;
|
|
2306
|
+
}
|
|
2307
|
+
if (options.getData) {
|
|
2308
|
+
this._getData = options.getData;
|
|
2309
|
+
}
|
|
2310
|
+
if (options.minResubscribeDelay !== void 0) {
|
|
2311
|
+
this._minResubscribeDelay = options.minResubscribeDelay;
|
|
2312
|
+
}
|
|
2313
|
+
if (options.maxResubscribeDelay !== void 0) {
|
|
2314
|
+
this._maxResubscribeDelay = options.maxResubscribeDelay;
|
|
2315
|
+
}
|
|
2316
|
+
if (options.token) {
|
|
2317
|
+
this._token = options.token;
|
|
2318
|
+
}
|
|
2319
|
+
if (options.getToken) {
|
|
2320
|
+
this._getToken = options.getToken;
|
|
2321
|
+
}
|
|
2322
|
+
if (options.positioned === true) {
|
|
2323
|
+
this._positioned = true;
|
|
2324
|
+
}
|
|
2325
|
+
if (options.recoverable === true) {
|
|
2326
|
+
this._recoverable = true;
|
|
2327
|
+
}
|
|
2328
|
+
if (options.joinLeave === true) {
|
|
2329
|
+
this._joinLeave = true;
|
|
2330
|
+
}
|
|
2331
|
+
if (options.delta) {
|
|
2332
|
+
if (options.delta !== "fossil") {
|
|
2333
|
+
throw new Error("unsupported delta format");
|
|
2334
|
+
}
|
|
2335
|
+
this._delta = options.delta;
|
|
2336
|
+
}
|
|
2337
|
+
}
|
|
2338
|
+
_getOffset() {
|
|
2339
|
+
const offset = this._offset;
|
|
2340
|
+
if (offset !== null) {
|
|
2341
|
+
return offset;
|
|
2342
|
+
}
|
|
2343
|
+
return 0;
|
|
2344
|
+
}
|
|
2345
|
+
_getEpoch() {
|
|
2346
|
+
const epoch = this._epoch;
|
|
2347
|
+
if (epoch !== null) {
|
|
2348
|
+
return epoch;
|
|
2349
|
+
}
|
|
2350
|
+
return "";
|
|
2351
|
+
}
|
|
2352
|
+
_clearRefreshTimeout() {
|
|
2353
|
+
if (this._refreshTimeout !== null) {
|
|
2354
|
+
clearTimeout(this._refreshTimeout);
|
|
2355
|
+
this._refreshTimeout = null;
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
_clearResubscribeTimeout() {
|
|
2359
|
+
if (this._resubscribeTimeout !== null) {
|
|
2360
|
+
clearTimeout(this._resubscribeTimeout);
|
|
2361
|
+
this._resubscribeTimeout = null;
|
|
2362
|
+
}
|
|
2363
|
+
}
|
|
2364
|
+
_getSubscriptionToken() {
|
|
2365
|
+
this._centrifuge._debug("get subscription token for channel", this.channel);
|
|
2366
|
+
const ctx = {
|
|
2367
|
+
channel: this.channel
|
|
2368
|
+
};
|
|
2369
|
+
const getToken = this._getToken;
|
|
2370
|
+
if (getToken === null) {
|
|
2371
|
+
this.emit("error", {
|
|
2372
|
+
type: "configuration",
|
|
2373
|
+
channel: this.channel,
|
|
2374
|
+
error: {
|
|
2375
|
+
code: errorCodes.badConfiguration,
|
|
2376
|
+
message: "provide a function to get channel subscription token"
|
|
2377
|
+
}
|
|
2378
|
+
});
|
|
2379
|
+
throw new UnauthorizedError("");
|
|
2380
|
+
}
|
|
2381
|
+
return getToken(ctx);
|
|
2382
|
+
}
|
|
2383
|
+
_refresh() {
|
|
2384
|
+
this._clearRefreshTimeout();
|
|
2385
|
+
const self = this;
|
|
2386
|
+
this._getSubscriptionToken().then(function(token) {
|
|
2387
|
+
if (!self._isSubscribed()) {
|
|
2388
|
+
return;
|
|
2389
|
+
}
|
|
2390
|
+
if (!token) {
|
|
2391
|
+
self._failUnauthorized();
|
|
2392
|
+
return;
|
|
2393
|
+
}
|
|
2394
|
+
self._token = token;
|
|
2395
|
+
const req = {
|
|
2396
|
+
channel: self.channel,
|
|
2397
|
+
token
|
|
2398
|
+
};
|
|
2399
|
+
const msg = {
|
|
2400
|
+
"sub_refresh": req
|
|
2401
|
+
};
|
|
2402
|
+
self._centrifuge._call(msg).then((resolveCtx) => {
|
|
2403
|
+
const result = resolveCtx.reply.sub_refresh;
|
|
2404
|
+
self._refreshResponse(result);
|
|
2405
|
+
if (resolveCtx.next) {
|
|
2406
|
+
resolveCtx.next();
|
|
2407
|
+
}
|
|
2408
|
+
}, (rejectCtx) => {
|
|
2409
|
+
self._refreshError(rejectCtx.error);
|
|
2410
|
+
if (rejectCtx.next) {
|
|
2411
|
+
rejectCtx.next();
|
|
2412
|
+
}
|
|
2413
|
+
});
|
|
2414
|
+
}).catch(function(e) {
|
|
2415
|
+
if (e instanceof UnauthorizedError) {
|
|
2416
|
+
self._failUnauthorized();
|
|
2417
|
+
return;
|
|
2418
|
+
}
|
|
2419
|
+
self.emit("error", {
|
|
2420
|
+
type: "refreshToken",
|
|
2421
|
+
channel: self.channel,
|
|
2422
|
+
error: {
|
|
2423
|
+
code: errorCodes.subscriptionRefreshToken,
|
|
2424
|
+
message: e !== void 0 ? e.toString() : ""
|
|
2425
|
+
}
|
|
2426
|
+
});
|
|
2427
|
+
self._refreshTimeout = setTimeout(() => self._refresh(), self._getRefreshRetryDelay());
|
|
2428
|
+
});
|
|
2429
|
+
}
|
|
2430
|
+
_refreshResponse(result) {
|
|
2431
|
+
if (!this._isSubscribed()) {
|
|
2432
|
+
return;
|
|
2433
|
+
}
|
|
2434
|
+
this._centrifuge._debug("subscription token refreshed, channel", this.channel);
|
|
2435
|
+
this._clearRefreshTimeout();
|
|
2436
|
+
if (result.expires === true) {
|
|
2437
|
+
this._refreshTimeout = setTimeout(() => this._refresh(), ttlMilliseconds(result.ttl));
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
_refreshError(err) {
|
|
2441
|
+
if (!this._isSubscribed()) {
|
|
2442
|
+
return;
|
|
2443
|
+
}
|
|
2444
|
+
if (err.code < 100 || err.temporary === true) {
|
|
2445
|
+
this.emit("error", {
|
|
2446
|
+
type: "refresh",
|
|
2447
|
+
channel: this.channel,
|
|
2448
|
+
error: err
|
|
2449
|
+
});
|
|
2450
|
+
this._refreshTimeout = setTimeout(() => this._refresh(), this._getRefreshRetryDelay());
|
|
2451
|
+
} else {
|
|
2452
|
+
this._setUnsubscribed(err.code, err.message, true);
|
|
2453
|
+
}
|
|
2454
|
+
}
|
|
2455
|
+
_getRefreshRetryDelay() {
|
|
2456
|
+
return backoff(0, 1e4, 2e4);
|
|
2457
|
+
}
|
|
2458
|
+
_failUnauthorized() {
|
|
2459
|
+
this._setUnsubscribed(unsubscribedCodes.unauthorized, "unauthorized", true);
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
class SockjsTransport {
|
|
2463
|
+
constructor(endpoint, options) {
|
|
2464
|
+
this.endpoint = endpoint;
|
|
2465
|
+
this.options = options;
|
|
2466
|
+
this._transport = null;
|
|
2467
|
+
}
|
|
2468
|
+
name() {
|
|
2469
|
+
return "sockjs";
|
|
2470
|
+
}
|
|
2471
|
+
subName() {
|
|
2472
|
+
return "sockjs-" + this._transport.transport;
|
|
2473
|
+
}
|
|
2474
|
+
emulation() {
|
|
2475
|
+
return false;
|
|
2476
|
+
}
|
|
2477
|
+
supported() {
|
|
2478
|
+
return this.options.sockjs !== null;
|
|
2479
|
+
}
|
|
2480
|
+
initialize(_protocol, callbacks) {
|
|
2481
|
+
this._transport = new this.options.sockjs(this.endpoint, null, this.options.sockjsOptions);
|
|
2482
|
+
this._transport.onopen = () => {
|
|
2483
|
+
callbacks.onOpen();
|
|
2484
|
+
};
|
|
2485
|
+
this._transport.onerror = (e) => {
|
|
2486
|
+
callbacks.onError(e);
|
|
2487
|
+
};
|
|
2488
|
+
this._transport.onclose = (closeEvent) => {
|
|
2489
|
+
callbacks.onClose(closeEvent);
|
|
2490
|
+
};
|
|
2491
|
+
this._transport.onmessage = (event) => {
|
|
2492
|
+
callbacks.onMessage(event.data);
|
|
2493
|
+
};
|
|
2494
|
+
}
|
|
2495
|
+
close() {
|
|
2496
|
+
this._transport.close();
|
|
2497
|
+
}
|
|
2498
|
+
send(data) {
|
|
2499
|
+
this._transport.send(data);
|
|
2500
|
+
}
|
|
2501
|
+
}
|
|
2502
|
+
class WebsocketTransport {
|
|
2503
|
+
constructor(endpoint, options) {
|
|
2504
|
+
this.endpoint = endpoint;
|
|
2505
|
+
this.options = options;
|
|
2506
|
+
this._transport = null;
|
|
2507
|
+
}
|
|
2508
|
+
name() {
|
|
2509
|
+
return "websocket";
|
|
2510
|
+
}
|
|
2511
|
+
subName() {
|
|
2512
|
+
return "websocket";
|
|
2513
|
+
}
|
|
2514
|
+
emulation() {
|
|
2515
|
+
return false;
|
|
2516
|
+
}
|
|
2517
|
+
supported() {
|
|
2518
|
+
return this.options.websocket !== void 0 && this.options.websocket !== null;
|
|
2519
|
+
}
|
|
2520
|
+
initialize(protocol, callbacks) {
|
|
2521
|
+
let subProtocol = "";
|
|
2522
|
+
if (protocol === "protobuf") {
|
|
2523
|
+
subProtocol = "centrifuge-protobuf";
|
|
2524
|
+
}
|
|
2525
|
+
if (subProtocol !== "") {
|
|
2526
|
+
this._transport = new this.options.websocket(this.endpoint, subProtocol);
|
|
2527
|
+
} else {
|
|
2528
|
+
this._transport = new this.options.websocket(this.endpoint);
|
|
2529
|
+
}
|
|
2530
|
+
if (protocol === "protobuf") {
|
|
2531
|
+
this._transport.binaryType = "arraybuffer";
|
|
2532
|
+
}
|
|
2533
|
+
this._transport.onopen = () => {
|
|
2534
|
+
callbacks.onOpen();
|
|
2535
|
+
};
|
|
2536
|
+
this._transport.onerror = (e) => {
|
|
2537
|
+
callbacks.onError(e);
|
|
2538
|
+
};
|
|
2539
|
+
this._transport.onclose = (closeEvent) => {
|
|
2540
|
+
callbacks.onClose(closeEvent);
|
|
2541
|
+
};
|
|
2542
|
+
this._transport.onmessage = (event) => {
|
|
2543
|
+
callbacks.onMessage(event.data);
|
|
2544
|
+
};
|
|
2545
|
+
}
|
|
2546
|
+
close() {
|
|
2547
|
+
this._transport.close();
|
|
2548
|
+
}
|
|
2549
|
+
send(data) {
|
|
2550
|
+
this._transport.send(data);
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
class HttpStreamTransport {
|
|
2554
|
+
constructor(endpoint, options) {
|
|
2555
|
+
this.endpoint = endpoint;
|
|
2556
|
+
this.options = options;
|
|
2557
|
+
this._abortController = null;
|
|
2558
|
+
this._utf8decoder = new TextDecoder();
|
|
2559
|
+
this._protocol = "json";
|
|
2560
|
+
}
|
|
2561
|
+
name() {
|
|
2562
|
+
return "http_stream";
|
|
2563
|
+
}
|
|
2564
|
+
subName() {
|
|
2565
|
+
return "http_stream";
|
|
2566
|
+
}
|
|
2567
|
+
emulation() {
|
|
2568
|
+
return true;
|
|
2569
|
+
}
|
|
2570
|
+
_handleErrors(response) {
|
|
2571
|
+
if (!response.ok)
|
|
2572
|
+
throw new Error(response.status);
|
|
2573
|
+
return response;
|
|
2574
|
+
}
|
|
2575
|
+
_fetchEventTarget(self, endpoint, options) {
|
|
2576
|
+
const eventTarget = new EventTarget();
|
|
2577
|
+
const fetchFunc = self.options.fetch;
|
|
2578
|
+
fetchFunc(endpoint, options).then(self._handleErrors).then((response) => {
|
|
2579
|
+
eventTarget.dispatchEvent(new Event("open"));
|
|
2580
|
+
let jsonStreamBuf = "";
|
|
2581
|
+
let jsonStreamPos = 0;
|
|
2582
|
+
let protoStreamBuf = new Uint8Array();
|
|
2583
|
+
const reader = response.body.getReader();
|
|
2584
|
+
return new self.options.readableStream({
|
|
2585
|
+
start(controller) {
|
|
2586
|
+
function pump() {
|
|
2587
|
+
return reader.read().then(({ done, value }) => {
|
|
2588
|
+
if (done) {
|
|
2589
|
+
eventTarget.dispatchEvent(new Event("close"));
|
|
2590
|
+
controller.close();
|
|
2591
|
+
return;
|
|
2592
|
+
}
|
|
2593
|
+
try {
|
|
2594
|
+
if (self._protocol === "json") {
|
|
2595
|
+
jsonStreamBuf += self._utf8decoder.decode(value);
|
|
2596
|
+
while (jsonStreamPos < jsonStreamBuf.length) {
|
|
2597
|
+
if (jsonStreamBuf[jsonStreamPos] === "\n") {
|
|
2598
|
+
const line = jsonStreamBuf.substring(0, jsonStreamPos);
|
|
2599
|
+
eventTarget.dispatchEvent(new MessageEvent("message", { data: line }));
|
|
2600
|
+
jsonStreamBuf = jsonStreamBuf.substring(jsonStreamPos + 1);
|
|
2601
|
+
jsonStreamPos = 0;
|
|
2602
|
+
} else {
|
|
2603
|
+
++jsonStreamPos;
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
} else {
|
|
2607
|
+
const mergedArray = new Uint8Array(protoStreamBuf.length + value.length);
|
|
2608
|
+
mergedArray.set(protoStreamBuf);
|
|
2609
|
+
mergedArray.set(value, protoStreamBuf.length);
|
|
2610
|
+
protoStreamBuf = mergedArray;
|
|
2611
|
+
while (true) {
|
|
2612
|
+
const result = self.options.decoder.decodeReply(protoStreamBuf);
|
|
2613
|
+
if (result.ok) {
|
|
2614
|
+
const data = protoStreamBuf.slice(0, result.pos);
|
|
2615
|
+
eventTarget.dispatchEvent(new MessageEvent("message", { data }));
|
|
2616
|
+
protoStreamBuf = protoStreamBuf.slice(result.pos);
|
|
2617
|
+
continue;
|
|
2618
|
+
}
|
|
2619
|
+
break;
|
|
2620
|
+
}
|
|
2621
|
+
}
|
|
2622
|
+
} catch (error) {
|
|
2623
|
+
eventTarget.dispatchEvent(new Event("error", { detail: error }));
|
|
2624
|
+
eventTarget.dispatchEvent(new Event("close"));
|
|
2625
|
+
controller.close();
|
|
2626
|
+
return;
|
|
2627
|
+
}
|
|
2628
|
+
pump();
|
|
2629
|
+
}).catch(function(e) {
|
|
2630
|
+
eventTarget.dispatchEvent(new Event("error", { detail: e }));
|
|
2631
|
+
eventTarget.dispatchEvent(new Event("close"));
|
|
2632
|
+
controller.close();
|
|
2633
|
+
return;
|
|
2634
|
+
});
|
|
2635
|
+
}
|
|
2636
|
+
return pump();
|
|
2637
|
+
}
|
|
2638
|
+
});
|
|
2639
|
+
}).catch((error) => {
|
|
2640
|
+
eventTarget.dispatchEvent(new Event("error", { detail: error }));
|
|
2641
|
+
eventTarget.dispatchEvent(new Event("close"));
|
|
2642
|
+
});
|
|
2643
|
+
return eventTarget;
|
|
2644
|
+
}
|
|
2645
|
+
supported() {
|
|
2646
|
+
return this.options.fetch !== null && this.options.readableStream !== null && typeof TextDecoder !== "undefined" && typeof AbortController !== "undefined" && typeof EventTarget !== "undefined" && typeof Event !== "undefined" && typeof MessageEvent !== "undefined" && typeof Error !== "undefined";
|
|
2647
|
+
}
|
|
2648
|
+
initialize(protocol, callbacks, initialData) {
|
|
2649
|
+
this._protocol = protocol;
|
|
2650
|
+
this._abortController = new AbortController();
|
|
2651
|
+
let headers;
|
|
2652
|
+
let body;
|
|
2653
|
+
if (protocol === "json") {
|
|
2654
|
+
headers = {
|
|
2655
|
+
"Accept": "application/json",
|
|
2656
|
+
"Content-Type": "application/json"
|
|
2657
|
+
};
|
|
2658
|
+
body = initialData;
|
|
2659
|
+
} else {
|
|
2660
|
+
headers = {
|
|
2661
|
+
"Accept": "application/octet-stream",
|
|
2662
|
+
"Content-Type": "application/octet-stream"
|
|
2663
|
+
};
|
|
2664
|
+
body = initialData;
|
|
2665
|
+
}
|
|
2666
|
+
const fetchOptions = {
|
|
2667
|
+
method: "POST",
|
|
2668
|
+
headers,
|
|
2669
|
+
body,
|
|
2670
|
+
mode: "cors",
|
|
2671
|
+
credentials: "same-origin",
|
|
2672
|
+
cache: "no-cache",
|
|
2673
|
+
signal: this._abortController.signal
|
|
2674
|
+
};
|
|
2675
|
+
const eventTarget = this._fetchEventTarget(this, this.endpoint, fetchOptions);
|
|
2676
|
+
eventTarget.addEventListener("open", () => {
|
|
2677
|
+
callbacks.onOpen();
|
|
2678
|
+
});
|
|
2679
|
+
eventTarget.addEventListener("error", (e) => {
|
|
2680
|
+
this._abortController.abort();
|
|
2681
|
+
callbacks.onError(e);
|
|
2682
|
+
});
|
|
2683
|
+
eventTarget.addEventListener("close", () => {
|
|
2684
|
+
this._abortController.abort();
|
|
2685
|
+
callbacks.onClose({
|
|
2686
|
+
code: 4,
|
|
2687
|
+
reason: "connection closed"
|
|
2688
|
+
});
|
|
2689
|
+
});
|
|
2690
|
+
eventTarget.addEventListener("message", (e) => {
|
|
2691
|
+
callbacks.onMessage(e.data);
|
|
2692
|
+
});
|
|
2693
|
+
}
|
|
2694
|
+
close() {
|
|
2695
|
+
this._abortController.abort();
|
|
2696
|
+
}
|
|
2697
|
+
send(data, session, node) {
|
|
2698
|
+
let headers;
|
|
2699
|
+
let body;
|
|
2700
|
+
const req = {
|
|
2701
|
+
session,
|
|
2702
|
+
node,
|
|
2703
|
+
data
|
|
2704
|
+
};
|
|
2705
|
+
if (this._protocol === "json") {
|
|
2706
|
+
headers = {
|
|
2707
|
+
"Content-Type": "application/json"
|
|
2708
|
+
};
|
|
2709
|
+
body = JSON.stringify(req);
|
|
2710
|
+
} else {
|
|
2711
|
+
headers = {
|
|
2712
|
+
"Content-Type": "application/octet-stream"
|
|
2713
|
+
};
|
|
2714
|
+
body = this.options.encoder.encodeEmulationRequest(req);
|
|
2715
|
+
}
|
|
2716
|
+
const fetchFunc = this.options.fetch;
|
|
2717
|
+
const fetchOptions = {
|
|
2718
|
+
method: "POST",
|
|
2719
|
+
headers,
|
|
2720
|
+
body,
|
|
2721
|
+
mode: "cors",
|
|
2722
|
+
credentials: "same-origin",
|
|
2723
|
+
cache: "no-cache"
|
|
2724
|
+
};
|
|
2725
|
+
fetchFunc(this.options.emulationEndpoint, fetchOptions);
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
class SseTransport {
|
|
2729
|
+
constructor(endpoint, options) {
|
|
2730
|
+
this.endpoint = endpoint;
|
|
2731
|
+
this.options = options;
|
|
2732
|
+
this._protocol = "json";
|
|
2733
|
+
this._transport = null;
|
|
2734
|
+
this._onClose = null;
|
|
2735
|
+
}
|
|
2736
|
+
name() {
|
|
2737
|
+
return "sse";
|
|
2738
|
+
}
|
|
2739
|
+
subName() {
|
|
2740
|
+
return "sse";
|
|
2741
|
+
}
|
|
2742
|
+
emulation() {
|
|
2743
|
+
return true;
|
|
2744
|
+
}
|
|
2745
|
+
supported() {
|
|
2746
|
+
return this.options.eventsource !== null && this.options.fetch !== null;
|
|
2747
|
+
}
|
|
2748
|
+
initialize(_protocol, callbacks, initialData) {
|
|
2749
|
+
let url;
|
|
2750
|
+
if (globalThis && globalThis.document && globalThis.document.baseURI) {
|
|
2751
|
+
url = new URL(this.endpoint, globalThis.document.baseURI);
|
|
2752
|
+
} else {
|
|
2753
|
+
url = new URL(this.endpoint);
|
|
2754
|
+
}
|
|
2755
|
+
url.searchParams.append("cf_connect", initialData);
|
|
2756
|
+
const eventsourceOptions = {};
|
|
2757
|
+
const eventSource = new this.options.eventsource(url.toString(), eventsourceOptions);
|
|
2758
|
+
this._transport = eventSource;
|
|
2759
|
+
const self = this;
|
|
2760
|
+
eventSource.onopen = function() {
|
|
2761
|
+
callbacks.onOpen();
|
|
2762
|
+
};
|
|
2763
|
+
eventSource.onerror = function(e) {
|
|
2764
|
+
eventSource.close();
|
|
2765
|
+
callbacks.onError(e);
|
|
2766
|
+
callbacks.onClose({
|
|
2767
|
+
code: 4,
|
|
2768
|
+
reason: "connection closed"
|
|
2769
|
+
});
|
|
2770
|
+
};
|
|
2771
|
+
eventSource.onmessage = function(e) {
|
|
2772
|
+
callbacks.onMessage(e.data);
|
|
2773
|
+
};
|
|
2774
|
+
self._onClose = function() {
|
|
2775
|
+
callbacks.onClose({
|
|
2776
|
+
code: 4,
|
|
2777
|
+
reason: "connection closed"
|
|
2778
|
+
});
|
|
2779
|
+
};
|
|
2780
|
+
}
|
|
2781
|
+
close() {
|
|
2782
|
+
this._transport.close();
|
|
2783
|
+
if (this._onClose !== null) {
|
|
2784
|
+
this._onClose();
|
|
2785
|
+
}
|
|
2786
|
+
}
|
|
2787
|
+
send(data, session, node) {
|
|
2788
|
+
const req = {
|
|
2789
|
+
session,
|
|
2790
|
+
node,
|
|
2791
|
+
data
|
|
2792
|
+
};
|
|
2793
|
+
const headers = {
|
|
2794
|
+
"Content-Type": "application/json"
|
|
2795
|
+
};
|
|
2796
|
+
const body = JSON.stringify(req);
|
|
2797
|
+
const fetchFunc = this.options.fetch;
|
|
2798
|
+
const fetchOptions = {
|
|
2799
|
+
method: "POST",
|
|
2800
|
+
headers,
|
|
2801
|
+
body,
|
|
2802
|
+
mode: "cors",
|
|
2803
|
+
credentials: "same-origin",
|
|
2804
|
+
cache: "no-cache"
|
|
2805
|
+
};
|
|
2806
|
+
fetchFunc(this.options.emulationEndpoint, fetchOptions);
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
class WebtransportTransport {
|
|
2810
|
+
constructor(endpoint, options) {
|
|
2811
|
+
this.endpoint = endpoint;
|
|
2812
|
+
this.options = options;
|
|
2813
|
+
this._transport = null;
|
|
2814
|
+
this._stream = null;
|
|
2815
|
+
this._writer = null;
|
|
2816
|
+
this._utf8decoder = new TextDecoder();
|
|
2817
|
+
this._protocol = "json";
|
|
2818
|
+
}
|
|
2819
|
+
name() {
|
|
2820
|
+
return "webtransport";
|
|
2821
|
+
}
|
|
2822
|
+
subName() {
|
|
2823
|
+
return "webtransport";
|
|
2824
|
+
}
|
|
2825
|
+
emulation() {
|
|
2826
|
+
return false;
|
|
2827
|
+
}
|
|
2828
|
+
supported() {
|
|
2829
|
+
return this.options.webtransport !== void 0 && this.options.webtransport !== null;
|
|
2830
|
+
}
|
|
2831
|
+
initialize(protocol, callbacks) {
|
|
2832
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2833
|
+
let url;
|
|
2834
|
+
if (globalThis && globalThis.document && globalThis.document.baseURI) {
|
|
2835
|
+
url = new URL(this.endpoint, globalThis.document.baseURI);
|
|
2836
|
+
} else {
|
|
2837
|
+
url = new URL(this.endpoint);
|
|
2838
|
+
}
|
|
2839
|
+
if (protocol === "protobuf") {
|
|
2840
|
+
url.searchParams.append("cf_protocol", "protobuf");
|
|
2841
|
+
}
|
|
2842
|
+
this._protocol = protocol;
|
|
2843
|
+
const eventTarget = new EventTarget();
|
|
2844
|
+
this._transport = new this.options.webtransport(url.toString());
|
|
2845
|
+
this._transport.closed.then(() => {
|
|
2846
|
+
callbacks.onClose({
|
|
2847
|
+
code: 4,
|
|
2848
|
+
reason: "connection closed"
|
|
2849
|
+
});
|
|
2850
|
+
}).catch(() => {
|
|
2851
|
+
callbacks.onClose({
|
|
2852
|
+
code: 4,
|
|
2853
|
+
reason: "connection closed"
|
|
2854
|
+
});
|
|
2855
|
+
});
|
|
2856
|
+
try {
|
|
2857
|
+
yield this._transport.ready;
|
|
2858
|
+
} catch (_a) {
|
|
2859
|
+
this.close();
|
|
2860
|
+
return;
|
|
2861
|
+
}
|
|
2862
|
+
let stream;
|
|
2863
|
+
try {
|
|
2864
|
+
stream = yield this._transport.createBidirectionalStream();
|
|
2865
|
+
} catch (_b) {
|
|
2866
|
+
this.close();
|
|
2867
|
+
return;
|
|
2868
|
+
}
|
|
2869
|
+
this._stream = stream;
|
|
2870
|
+
this._writer = this._stream.writable.getWriter();
|
|
2871
|
+
eventTarget.addEventListener("close", () => {
|
|
2872
|
+
callbacks.onClose({
|
|
2873
|
+
code: 4,
|
|
2874
|
+
reason: "connection closed"
|
|
2875
|
+
});
|
|
2876
|
+
});
|
|
2877
|
+
eventTarget.addEventListener("message", (e) => {
|
|
2878
|
+
callbacks.onMessage(e.data);
|
|
2879
|
+
});
|
|
2880
|
+
this._startReading(eventTarget);
|
|
2881
|
+
callbacks.onOpen();
|
|
2882
|
+
});
|
|
2883
|
+
}
|
|
2884
|
+
_startReading(eventTarget) {
|
|
2885
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2886
|
+
const reader = this._stream.readable.getReader();
|
|
2887
|
+
let jsonStreamBuf = "";
|
|
2888
|
+
let jsonStreamPos = 0;
|
|
2889
|
+
let protoStreamBuf = new Uint8Array();
|
|
2890
|
+
try {
|
|
2891
|
+
while (true) {
|
|
2892
|
+
const { done, value } = yield reader.read();
|
|
2893
|
+
if (value.length > 0) {
|
|
2894
|
+
if (this._protocol === "json") {
|
|
2895
|
+
jsonStreamBuf += this._utf8decoder.decode(value);
|
|
2896
|
+
while (jsonStreamPos < jsonStreamBuf.length) {
|
|
2897
|
+
if (jsonStreamBuf[jsonStreamPos] === "\n") {
|
|
2898
|
+
const line = jsonStreamBuf.substring(0, jsonStreamPos);
|
|
2899
|
+
eventTarget.dispatchEvent(new MessageEvent("message", { data: line }));
|
|
2900
|
+
jsonStreamBuf = jsonStreamBuf.substring(jsonStreamPos + 1);
|
|
2901
|
+
jsonStreamPos = 0;
|
|
2902
|
+
} else {
|
|
2903
|
+
++jsonStreamPos;
|
|
2904
|
+
}
|
|
2905
|
+
}
|
|
2906
|
+
} else {
|
|
2907
|
+
const mergedArray = new Uint8Array(protoStreamBuf.length + value.length);
|
|
2908
|
+
mergedArray.set(protoStreamBuf);
|
|
2909
|
+
mergedArray.set(value, protoStreamBuf.length);
|
|
2910
|
+
protoStreamBuf = mergedArray;
|
|
2911
|
+
while (true) {
|
|
2912
|
+
const result = this.options.decoder.decodeReply(protoStreamBuf);
|
|
2913
|
+
if (result.ok) {
|
|
2914
|
+
const data = protoStreamBuf.slice(0, result.pos);
|
|
2915
|
+
eventTarget.dispatchEvent(new MessageEvent("message", { data }));
|
|
2916
|
+
protoStreamBuf = protoStreamBuf.slice(result.pos);
|
|
2917
|
+
continue;
|
|
2918
|
+
}
|
|
2919
|
+
break;
|
|
2920
|
+
}
|
|
2921
|
+
}
|
|
2922
|
+
}
|
|
2923
|
+
if (done) {
|
|
2924
|
+
break;
|
|
2925
|
+
}
|
|
2926
|
+
}
|
|
2927
|
+
} catch (_a) {
|
|
2928
|
+
eventTarget.dispatchEvent(new Event("close"));
|
|
2929
|
+
}
|
|
2930
|
+
});
|
|
2931
|
+
}
|
|
2932
|
+
close() {
|
|
2933
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2934
|
+
try {
|
|
2935
|
+
if (this._writer) {
|
|
2936
|
+
yield this._writer.close();
|
|
2937
|
+
}
|
|
2938
|
+
this._transport.close();
|
|
2939
|
+
} catch (e) {
|
|
2940
|
+
}
|
|
2941
|
+
});
|
|
2942
|
+
}
|
|
2943
|
+
send(data) {
|
|
2944
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2945
|
+
let binary;
|
|
2946
|
+
if (this._protocol === "json") {
|
|
2947
|
+
binary = new TextEncoder().encode(data + "\n");
|
|
2948
|
+
} else {
|
|
2949
|
+
binary = data;
|
|
2950
|
+
}
|
|
2951
|
+
try {
|
|
2952
|
+
yield this._writer.write(binary);
|
|
2953
|
+
} catch (e) {
|
|
2954
|
+
this.close();
|
|
2955
|
+
}
|
|
2956
|
+
});
|
|
2957
|
+
}
|
|
2958
|
+
}
|
|
2959
|
+
const zValue = [
|
|
2960
|
+
-1,
|
|
2961
|
+
-1,
|
|
2962
|
+
-1,
|
|
2963
|
+
-1,
|
|
2964
|
+
-1,
|
|
2965
|
+
-1,
|
|
2966
|
+
-1,
|
|
2967
|
+
-1,
|
|
2968
|
+
-1,
|
|
2969
|
+
-1,
|
|
2970
|
+
-1,
|
|
2971
|
+
-1,
|
|
2972
|
+
-1,
|
|
2973
|
+
-1,
|
|
2974
|
+
-1,
|
|
2975
|
+
-1,
|
|
2976
|
+
-1,
|
|
2977
|
+
-1,
|
|
2978
|
+
-1,
|
|
2979
|
+
-1,
|
|
2980
|
+
-1,
|
|
2981
|
+
-1,
|
|
2982
|
+
-1,
|
|
2983
|
+
-1,
|
|
2984
|
+
-1,
|
|
2985
|
+
-1,
|
|
2986
|
+
-1,
|
|
2987
|
+
-1,
|
|
2988
|
+
-1,
|
|
2989
|
+
-1,
|
|
2990
|
+
-1,
|
|
2991
|
+
-1,
|
|
2992
|
+
-1,
|
|
2993
|
+
-1,
|
|
2994
|
+
-1,
|
|
2995
|
+
-1,
|
|
2996
|
+
-1,
|
|
2997
|
+
-1,
|
|
2998
|
+
-1,
|
|
2999
|
+
-1,
|
|
3000
|
+
-1,
|
|
3001
|
+
-1,
|
|
3002
|
+
-1,
|
|
3003
|
+
-1,
|
|
3004
|
+
-1,
|
|
3005
|
+
-1,
|
|
3006
|
+
-1,
|
|
3007
|
+
-1,
|
|
3008
|
+
0,
|
|
3009
|
+
1,
|
|
3010
|
+
2,
|
|
3011
|
+
3,
|
|
3012
|
+
4,
|
|
3013
|
+
5,
|
|
3014
|
+
6,
|
|
3015
|
+
7,
|
|
3016
|
+
8,
|
|
3017
|
+
9,
|
|
3018
|
+
-1,
|
|
3019
|
+
-1,
|
|
3020
|
+
-1,
|
|
3021
|
+
-1,
|
|
3022
|
+
-1,
|
|
3023
|
+
-1,
|
|
3024
|
+
-1,
|
|
3025
|
+
10,
|
|
3026
|
+
11,
|
|
3027
|
+
12,
|
|
3028
|
+
13,
|
|
3029
|
+
14,
|
|
3030
|
+
15,
|
|
3031
|
+
16,
|
|
3032
|
+
17,
|
|
3033
|
+
18,
|
|
3034
|
+
19,
|
|
3035
|
+
20,
|
|
3036
|
+
21,
|
|
3037
|
+
22,
|
|
3038
|
+
23,
|
|
3039
|
+
24,
|
|
3040
|
+
25,
|
|
3041
|
+
26,
|
|
3042
|
+
27,
|
|
3043
|
+
28,
|
|
3044
|
+
29,
|
|
3045
|
+
30,
|
|
3046
|
+
31,
|
|
3047
|
+
32,
|
|
3048
|
+
33,
|
|
3049
|
+
34,
|
|
3050
|
+
35,
|
|
3051
|
+
-1,
|
|
3052
|
+
-1,
|
|
3053
|
+
-1,
|
|
3054
|
+
-1,
|
|
3055
|
+
36,
|
|
3056
|
+
-1,
|
|
3057
|
+
37,
|
|
3058
|
+
38,
|
|
3059
|
+
39,
|
|
3060
|
+
40,
|
|
3061
|
+
41,
|
|
3062
|
+
42,
|
|
3063
|
+
43,
|
|
3064
|
+
44,
|
|
3065
|
+
45,
|
|
3066
|
+
46,
|
|
3067
|
+
47,
|
|
3068
|
+
48,
|
|
3069
|
+
49,
|
|
3070
|
+
50,
|
|
3071
|
+
51,
|
|
3072
|
+
52,
|
|
3073
|
+
53,
|
|
3074
|
+
54,
|
|
3075
|
+
55,
|
|
3076
|
+
56,
|
|
3077
|
+
57,
|
|
3078
|
+
58,
|
|
3079
|
+
59,
|
|
3080
|
+
60,
|
|
3081
|
+
61,
|
|
3082
|
+
62,
|
|
3083
|
+
-1,
|
|
3084
|
+
-1,
|
|
3085
|
+
-1,
|
|
3086
|
+
63,
|
|
3087
|
+
-1
|
|
3088
|
+
];
|
|
3089
|
+
class Reader {
|
|
3090
|
+
constructor(array) {
|
|
3091
|
+
this.a = array;
|
|
3092
|
+
this.pos = 0;
|
|
3093
|
+
}
|
|
3094
|
+
haveBytes() {
|
|
3095
|
+
return this.pos < this.a.length;
|
|
3096
|
+
}
|
|
3097
|
+
getByte() {
|
|
3098
|
+
const b = this.a[this.pos];
|
|
3099
|
+
this.pos++;
|
|
3100
|
+
if (this.pos > this.a.length)
|
|
3101
|
+
throw new RangeError("out of bounds");
|
|
3102
|
+
return b;
|
|
3103
|
+
}
|
|
3104
|
+
getChar() {
|
|
3105
|
+
return String.fromCharCode(this.getByte());
|
|
3106
|
+
}
|
|
3107
|
+
// Read base64-encoded unsigned integer.
|
|
3108
|
+
getInt() {
|
|
3109
|
+
let v = 0;
|
|
3110
|
+
let c;
|
|
3111
|
+
while (this.haveBytes() && (c = zValue[127 & this.getByte()]) >= 0) {
|
|
3112
|
+
v = (v << 6) + c;
|
|
3113
|
+
}
|
|
3114
|
+
this.pos--;
|
|
3115
|
+
return v >>> 0;
|
|
3116
|
+
}
|
|
3117
|
+
}
|
|
3118
|
+
class Writer {
|
|
3119
|
+
constructor() {
|
|
3120
|
+
this.a = [];
|
|
3121
|
+
}
|
|
3122
|
+
toByteArray(sourceType) {
|
|
3123
|
+
if (Array.isArray(sourceType)) {
|
|
3124
|
+
return this.a;
|
|
3125
|
+
}
|
|
3126
|
+
return new Uint8Array(this.a);
|
|
3127
|
+
}
|
|
3128
|
+
// Copy from array at start to end.
|
|
3129
|
+
putArray(a, start, end) {
|
|
3130
|
+
for (let i = start; i < end; i++)
|
|
3131
|
+
this.a.push(a[i]);
|
|
3132
|
+
}
|
|
3133
|
+
}
|
|
3134
|
+
function checksum(arr) {
|
|
3135
|
+
let sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0, z = 0, N = arr.length;
|
|
3136
|
+
while (N >= 16) {
|
|
3137
|
+
sum0 = sum0 + arr[z + 0] | 0;
|
|
3138
|
+
sum1 = sum1 + arr[z + 1] | 0;
|
|
3139
|
+
sum2 = sum2 + arr[z + 2] | 0;
|
|
3140
|
+
sum3 = sum3 + arr[z + 3] | 0;
|
|
3141
|
+
sum0 = sum0 + arr[z + 4] | 0;
|
|
3142
|
+
sum1 = sum1 + arr[z + 5] | 0;
|
|
3143
|
+
sum2 = sum2 + arr[z + 6] | 0;
|
|
3144
|
+
sum3 = sum3 + arr[z + 7] | 0;
|
|
3145
|
+
sum0 = sum0 + arr[z + 8] | 0;
|
|
3146
|
+
sum1 = sum1 + arr[z + 9] | 0;
|
|
3147
|
+
sum2 = sum2 + arr[z + 10] | 0;
|
|
3148
|
+
sum3 = sum3 + arr[z + 11] | 0;
|
|
3149
|
+
sum0 = sum0 + arr[z + 12] | 0;
|
|
3150
|
+
sum1 = sum1 + arr[z + 13] | 0;
|
|
3151
|
+
sum2 = sum2 + arr[z + 14] | 0;
|
|
3152
|
+
sum3 = sum3 + arr[z + 15] | 0;
|
|
3153
|
+
z += 16;
|
|
3154
|
+
N -= 16;
|
|
3155
|
+
}
|
|
3156
|
+
while (N >= 4) {
|
|
3157
|
+
sum0 = sum0 + arr[z + 0] | 0;
|
|
3158
|
+
sum1 = sum1 + arr[z + 1] | 0;
|
|
3159
|
+
sum2 = sum2 + arr[z + 2] | 0;
|
|
3160
|
+
sum3 = sum3 + arr[z + 3] | 0;
|
|
3161
|
+
z += 4;
|
|
3162
|
+
N -= 4;
|
|
3163
|
+
}
|
|
3164
|
+
sum3 = ((sum3 + (sum2 << 8) | 0) + (sum1 << 16) | 0) + (sum0 << 24) | 0;
|
|
3165
|
+
switch (N) {
|
|
3166
|
+
//@ts-ignore fallthrough is needed.
|
|
3167
|
+
case 3:
|
|
3168
|
+
sum3 = sum3 + (arr[z + 2] << 8) | 0;
|
|
3169
|
+
/* falls through */
|
|
3170
|
+
//@ts-ignore fallthrough is needed.
|
|
3171
|
+
case 2:
|
|
3172
|
+
sum3 = sum3 + (arr[z + 1] << 16) | 0;
|
|
3173
|
+
/* falls through */
|
|
3174
|
+
case 1:
|
|
3175
|
+
sum3 = sum3 + (arr[z + 0] << 24) | 0;
|
|
3176
|
+
}
|
|
3177
|
+
return sum3 >>> 0;
|
|
3178
|
+
}
|
|
3179
|
+
function applyDelta(source, delta) {
|
|
3180
|
+
let total = 0;
|
|
3181
|
+
const zDelta = new Reader(delta);
|
|
3182
|
+
const lenSrc = source.length;
|
|
3183
|
+
const lenDelta = delta.length;
|
|
3184
|
+
const limit = zDelta.getInt();
|
|
3185
|
+
if (zDelta.getChar() !== "\n")
|
|
3186
|
+
throw new Error("size integer not terminated by '\\n'");
|
|
3187
|
+
const zOut = new Writer();
|
|
3188
|
+
while (zDelta.haveBytes()) {
|
|
3189
|
+
const cnt = zDelta.getInt();
|
|
3190
|
+
let ofst;
|
|
3191
|
+
switch (zDelta.getChar()) {
|
|
3192
|
+
case "@":
|
|
3193
|
+
ofst = zDelta.getInt();
|
|
3194
|
+
if (zDelta.haveBytes() && zDelta.getChar() !== ",")
|
|
3195
|
+
throw new Error("copy command not terminated by ','");
|
|
3196
|
+
total += cnt;
|
|
3197
|
+
if (total > limit)
|
|
3198
|
+
throw new Error("copy exceeds output file size");
|
|
3199
|
+
if (ofst + cnt > lenSrc)
|
|
3200
|
+
throw new Error("copy extends past end of input");
|
|
3201
|
+
zOut.putArray(source, ofst, ofst + cnt);
|
|
3202
|
+
break;
|
|
3203
|
+
case ":":
|
|
3204
|
+
total += cnt;
|
|
3205
|
+
if (total > limit)
|
|
3206
|
+
throw new Error("insert command gives an output larger than predicted");
|
|
3207
|
+
if (cnt > lenDelta)
|
|
3208
|
+
throw new Error("insert count exceeds size of delta");
|
|
3209
|
+
zOut.putArray(zDelta.a, zDelta.pos, zDelta.pos + cnt);
|
|
3210
|
+
zDelta.pos += cnt;
|
|
3211
|
+
break;
|
|
3212
|
+
case ";": {
|
|
3213
|
+
const out = zOut.toByteArray(source);
|
|
3214
|
+
if (cnt !== checksum(out))
|
|
3215
|
+
throw new Error("bad checksum");
|
|
3216
|
+
if (total !== limit)
|
|
3217
|
+
throw new Error("generated size does not match predicted size");
|
|
3218
|
+
return out;
|
|
3219
|
+
}
|
|
3220
|
+
default:
|
|
3221
|
+
throw new Error("unknown delta operator");
|
|
3222
|
+
}
|
|
3223
|
+
}
|
|
3224
|
+
throw new Error("unterminated delta");
|
|
3225
|
+
}
|
|
3226
|
+
class JsonCodec {
|
|
3227
|
+
name() {
|
|
3228
|
+
return "json";
|
|
3229
|
+
}
|
|
3230
|
+
encodeCommands(commands) {
|
|
3231
|
+
return commands.map((c) => JSON.stringify(c)).join("\n");
|
|
3232
|
+
}
|
|
3233
|
+
decodeReplies(data) {
|
|
3234
|
+
return data.trim().split("\n").map((r) => JSON.parse(r));
|
|
3235
|
+
}
|
|
3236
|
+
applyDeltaIfNeeded(pub, prevValue) {
|
|
3237
|
+
let newData, newPrevValue;
|
|
3238
|
+
if (pub.delta) {
|
|
3239
|
+
const valueArray = applyDelta(prevValue, new TextEncoder().encode(pub.data));
|
|
3240
|
+
newData = JSON.parse(new TextDecoder().decode(valueArray));
|
|
3241
|
+
newPrevValue = valueArray;
|
|
3242
|
+
} else {
|
|
3243
|
+
newData = JSON.parse(pub.data);
|
|
3244
|
+
newPrevValue = new TextEncoder().encode(pub.data);
|
|
3245
|
+
}
|
|
3246
|
+
return { newData, newPrevValue };
|
|
3247
|
+
}
|
|
3248
|
+
}
|
|
3249
|
+
const defaults = {
|
|
3250
|
+
token: "",
|
|
3251
|
+
getToken: null,
|
|
3252
|
+
data: null,
|
|
3253
|
+
getData: null,
|
|
3254
|
+
debug: false,
|
|
3255
|
+
name: "js",
|
|
3256
|
+
version: "",
|
|
3257
|
+
fetch: null,
|
|
3258
|
+
readableStream: null,
|
|
3259
|
+
websocket: null,
|
|
3260
|
+
eventsource: null,
|
|
3261
|
+
sockjs: null,
|
|
3262
|
+
sockjsOptions: {},
|
|
3263
|
+
emulationEndpoint: "/emulation",
|
|
3264
|
+
minReconnectDelay: 500,
|
|
3265
|
+
maxReconnectDelay: 2e4,
|
|
3266
|
+
timeout: 5e3,
|
|
3267
|
+
maxServerPingDelay: 1e4,
|
|
3268
|
+
networkEventTarget: null
|
|
3269
|
+
};
|
|
3270
|
+
class UnauthorizedError extends Error {
|
|
3271
|
+
constructor(message) {
|
|
3272
|
+
super(message);
|
|
3273
|
+
this.name = this.constructor.name;
|
|
3274
|
+
}
|
|
3275
|
+
}
|
|
3276
|
+
class Centrifuge extends EventEmitter$1 {
|
|
3277
|
+
/** Constructs Centrifuge client. Call connect() method to start connecting. */
|
|
3278
|
+
constructor(endpoint, options) {
|
|
3279
|
+
super();
|
|
3280
|
+
this._reconnectTimeout = null;
|
|
3281
|
+
this._refreshTimeout = null;
|
|
3282
|
+
this._serverPingTimeout = null;
|
|
3283
|
+
this.state = State.Disconnected;
|
|
3284
|
+
this._transportIsOpen = false;
|
|
3285
|
+
this._endpoint = endpoint;
|
|
3286
|
+
this._emulation = false;
|
|
3287
|
+
this._transports = [];
|
|
3288
|
+
this._currentTransportIndex = 0;
|
|
3289
|
+
this._triedAllTransports = false;
|
|
3290
|
+
this._transportWasOpen = false;
|
|
3291
|
+
this._transport = null;
|
|
3292
|
+
this._transportId = 0;
|
|
3293
|
+
this._deviceWentOffline = false;
|
|
3294
|
+
this._transportClosed = true;
|
|
3295
|
+
this._codec = new JsonCodec();
|
|
3296
|
+
this._reconnecting = false;
|
|
3297
|
+
this._reconnectTimeout = null;
|
|
3298
|
+
this._reconnectAttempts = 0;
|
|
3299
|
+
this._client = null;
|
|
3300
|
+
this._session = "";
|
|
3301
|
+
this._node = "";
|
|
3302
|
+
this._subs = {};
|
|
3303
|
+
this._serverSubs = {};
|
|
3304
|
+
this._commandId = 0;
|
|
3305
|
+
this._commands = [];
|
|
3306
|
+
this._batching = false;
|
|
3307
|
+
this._refreshRequired = false;
|
|
3308
|
+
this._refreshTimeout = null;
|
|
3309
|
+
this._callbacks = {};
|
|
3310
|
+
this._token = "";
|
|
3311
|
+
this._data = null;
|
|
3312
|
+
this._dispatchPromise = Promise.resolve();
|
|
3313
|
+
this._serverPing = 0;
|
|
3314
|
+
this._serverPingTimeout = null;
|
|
3315
|
+
this._sendPong = false;
|
|
3316
|
+
this._promises = {};
|
|
3317
|
+
this._promiseId = 0;
|
|
3318
|
+
this._debugEnabled = false;
|
|
3319
|
+
this._networkEventsSet = false;
|
|
3320
|
+
this._config = Object.assign(Object.assign({}, defaults), options);
|
|
3321
|
+
this._configure();
|
|
3322
|
+
if (this._debugEnabled) {
|
|
3323
|
+
this.on("state", (ctx) => {
|
|
3324
|
+
this._debug("client state", ctx.oldState, "->", ctx.newState);
|
|
3325
|
+
});
|
|
3326
|
+
this.on("error", (ctx) => {
|
|
3327
|
+
this._debug("client error", ctx);
|
|
3328
|
+
});
|
|
3329
|
+
} else {
|
|
3330
|
+
this.on("error", function() {
|
|
3331
|
+
Function.prototype();
|
|
3332
|
+
});
|
|
3333
|
+
}
|
|
3334
|
+
}
|
|
3335
|
+
/** newSubscription allocates new Subscription to a channel. Since server only allows
|
|
3336
|
+
* one subscription per channel per client this method throws if client already has
|
|
3337
|
+
* channel subscription in internal registry.
|
|
3338
|
+
* */
|
|
3339
|
+
newSubscription(channel, options) {
|
|
3340
|
+
if (this.getSubscription(channel) !== null) {
|
|
3341
|
+
throw new Error("Subscription to the channel " + channel + " already exists");
|
|
3342
|
+
}
|
|
3343
|
+
const sub = new Subscription(this, channel, options);
|
|
3344
|
+
this._subs[channel] = sub;
|
|
3345
|
+
return sub;
|
|
3346
|
+
}
|
|
3347
|
+
/** getSubscription returns Subscription if it's registered in the internal
|
|
3348
|
+
* registry or null. */
|
|
3349
|
+
getSubscription(channel) {
|
|
3350
|
+
return this._getSub(channel);
|
|
3351
|
+
}
|
|
3352
|
+
/** removeSubscription allows removing Subcription from the internal registry. Subscrption
|
|
3353
|
+
* must be in unsubscribed state. */
|
|
3354
|
+
removeSubscription(sub) {
|
|
3355
|
+
if (!sub) {
|
|
3356
|
+
return;
|
|
3357
|
+
}
|
|
3358
|
+
if (sub.state !== SubscriptionState.Unsubscribed) {
|
|
3359
|
+
sub.unsubscribe();
|
|
3360
|
+
}
|
|
3361
|
+
this._removeSubscription(sub);
|
|
3362
|
+
}
|
|
3363
|
+
/** Get a map with all current client-side subscriptions. */
|
|
3364
|
+
subscriptions() {
|
|
3365
|
+
return this._subs;
|
|
3366
|
+
}
|
|
3367
|
+
/** ready returns a Promise which resolves upon client goes to Connected
|
|
3368
|
+
* state and rejects in case of client goes to Disconnected or Failed state.
|
|
3369
|
+
* Users can provide optional timeout in milliseconds. */
|
|
3370
|
+
ready(timeout) {
|
|
3371
|
+
if (this.state === State.Disconnected) {
|
|
3372
|
+
return Promise.reject({ code: errorCodes.clientDisconnected, message: "client disconnected" });
|
|
3373
|
+
}
|
|
3374
|
+
if (this.state === State.Connected) {
|
|
3375
|
+
return Promise.resolve();
|
|
3376
|
+
}
|
|
3377
|
+
return new Promise((res, rej) => {
|
|
3378
|
+
const ctx = {
|
|
3379
|
+
resolve: res,
|
|
3380
|
+
reject: rej
|
|
3381
|
+
};
|
|
3382
|
+
if (timeout) {
|
|
3383
|
+
ctx.timeout = setTimeout(function() {
|
|
3384
|
+
rej({ code: errorCodes.timeout, message: "timeout" });
|
|
3385
|
+
}, timeout);
|
|
3386
|
+
}
|
|
3387
|
+
this._promises[this._nextPromiseId()] = ctx;
|
|
3388
|
+
});
|
|
3389
|
+
}
|
|
3390
|
+
/** connect to a server. */
|
|
3391
|
+
connect() {
|
|
3392
|
+
if (this._isConnected()) {
|
|
3393
|
+
this._debug("connect called when already connected");
|
|
3394
|
+
return;
|
|
3395
|
+
}
|
|
3396
|
+
if (this._isConnecting()) {
|
|
3397
|
+
this._debug("connect called when already connecting");
|
|
3398
|
+
return;
|
|
3399
|
+
}
|
|
3400
|
+
this._debug("connect called");
|
|
3401
|
+
this._reconnectAttempts = 0;
|
|
3402
|
+
this._startConnecting();
|
|
3403
|
+
}
|
|
3404
|
+
/** disconnect from a server. */
|
|
3405
|
+
disconnect() {
|
|
3406
|
+
this._disconnect(disconnectedCodes.disconnectCalled, "disconnect called", false);
|
|
3407
|
+
}
|
|
3408
|
+
/** setToken allows setting connection token. Or resetting used token to be empty. */
|
|
3409
|
+
setToken(token) {
|
|
3410
|
+
this._token = token;
|
|
3411
|
+
}
|
|
3412
|
+
/** send asynchronous data to a server (without any response from a server
|
|
3413
|
+
* expected, see rpc method if you need response). */
|
|
3414
|
+
send(data) {
|
|
3415
|
+
const cmd = {
|
|
3416
|
+
send: {
|
|
3417
|
+
data
|
|
3418
|
+
}
|
|
3419
|
+
};
|
|
3420
|
+
const self = this;
|
|
3421
|
+
return this._methodCall().then(function() {
|
|
3422
|
+
const sent = self._transportSendCommands([cmd]);
|
|
3423
|
+
if (!sent) {
|
|
3424
|
+
return Promise.reject(self._createErrorObject(errorCodes.transportWriteError, "transport write error"));
|
|
3425
|
+
}
|
|
3426
|
+
return Promise.resolve();
|
|
3427
|
+
});
|
|
3428
|
+
}
|
|
3429
|
+
/** rpc to a server - i.e. a call which waits for a response with data. */
|
|
3430
|
+
rpc(method, data) {
|
|
3431
|
+
const cmd = {
|
|
3432
|
+
rpc: {
|
|
3433
|
+
method,
|
|
3434
|
+
data
|
|
3435
|
+
}
|
|
3436
|
+
};
|
|
3437
|
+
const self = this;
|
|
3438
|
+
return this._methodCall().then(function() {
|
|
3439
|
+
return self._callPromise(cmd, function(reply) {
|
|
3440
|
+
return {
|
|
3441
|
+
"data": reply.rpc.data
|
|
3442
|
+
};
|
|
3443
|
+
});
|
|
3444
|
+
});
|
|
3445
|
+
}
|
|
3446
|
+
/** publish data to a channel. */
|
|
3447
|
+
publish(channel, data) {
|
|
3448
|
+
const cmd = {
|
|
3449
|
+
publish: {
|
|
3450
|
+
channel,
|
|
3451
|
+
data
|
|
3452
|
+
}
|
|
3453
|
+
};
|
|
3454
|
+
const self = this;
|
|
3455
|
+
return this._methodCall().then(function() {
|
|
3456
|
+
return self._callPromise(cmd, function() {
|
|
3457
|
+
return {};
|
|
3458
|
+
});
|
|
3459
|
+
});
|
|
3460
|
+
}
|
|
3461
|
+
/** history for a channel. By default it does not return publications (only current
|
|
3462
|
+
* StreamPosition data) – provide an explicit limit > 0 to load publications.*/
|
|
3463
|
+
history(channel, options) {
|
|
3464
|
+
const cmd = {
|
|
3465
|
+
history: this._getHistoryRequest(channel, options)
|
|
3466
|
+
};
|
|
3467
|
+
const self = this;
|
|
3468
|
+
return this._methodCall().then(function() {
|
|
3469
|
+
return self._callPromise(cmd, function(reply) {
|
|
3470
|
+
const result = reply.history;
|
|
3471
|
+
const publications = [];
|
|
3472
|
+
if (result.publications) {
|
|
3473
|
+
for (let i = 0; i < result.publications.length; i++) {
|
|
3474
|
+
publications.push(self._getPublicationContext(channel, result.publications[i]));
|
|
3475
|
+
}
|
|
3476
|
+
}
|
|
3477
|
+
return {
|
|
3478
|
+
"publications": publications,
|
|
3479
|
+
"epoch": result.epoch || "",
|
|
3480
|
+
"offset": result.offset || 0
|
|
3481
|
+
};
|
|
3482
|
+
});
|
|
3483
|
+
});
|
|
3484
|
+
}
|
|
3485
|
+
/** presence for a channel. */
|
|
3486
|
+
presence(channel) {
|
|
3487
|
+
const cmd = {
|
|
3488
|
+
presence: {
|
|
3489
|
+
channel
|
|
3490
|
+
}
|
|
3491
|
+
};
|
|
3492
|
+
const self = this;
|
|
3493
|
+
return this._methodCall().then(function() {
|
|
3494
|
+
return self._callPromise(cmd, function(reply) {
|
|
3495
|
+
const clients = reply.presence.presence;
|
|
3496
|
+
for (const clientId in clients) {
|
|
3497
|
+
if (clients.hasOwnProperty(clientId)) {
|
|
3498
|
+
const connInfo = clients[clientId]["conn_info"];
|
|
3499
|
+
const chanInfo = clients[clientId]["chan_info"];
|
|
3500
|
+
if (connInfo) {
|
|
3501
|
+
clients[clientId].connInfo = connInfo;
|
|
3502
|
+
}
|
|
3503
|
+
if (chanInfo) {
|
|
3504
|
+
clients[clientId].chanInfo = chanInfo;
|
|
3505
|
+
}
|
|
3506
|
+
}
|
|
3507
|
+
}
|
|
3508
|
+
return {
|
|
3509
|
+
"clients": clients
|
|
3510
|
+
};
|
|
3511
|
+
});
|
|
3512
|
+
});
|
|
3513
|
+
}
|
|
3514
|
+
/** presence stats for a channel. */
|
|
3515
|
+
presenceStats(channel) {
|
|
3516
|
+
const cmd = {
|
|
3517
|
+
"presence_stats": {
|
|
3518
|
+
channel
|
|
3519
|
+
}
|
|
3520
|
+
};
|
|
3521
|
+
const self = this;
|
|
3522
|
+
return this._methodCall().then(function() {
|
|
3523
|
+
return self._callPromise(cmd, function(reply) {
|
|
3524
|
+
const result = reply.presence_stats;
|
|
3525
|
+
return {
|
|
3526
|
+
"numUsers": result.num_users,
|
|
3527
|
+
"numClients": result.num_clients
|
|
3528
|
+
};
|
|
3529
|
+
});
|
|
3530
|
+
});
|
|
3531
|
+
}
|
|
3532
|
+
/** start command batching (collect into temporary buffer without sending to a server)
|
|
3533
|
+
* until stopBatching called.*/
|
|
3534
|
+
startBatching() {
|
|
3535
|
+
this._batching = true;
|
|
3536
|
+
}
|
|
3537
|
+
/** stop batching commands and flush collected commands to the
|
|
3538
|
+
* network (all in one request/frame).*/
|
|
3539
|
+
stopBatching() {
|
|
3540
|
+
const self = this;
|
|
3541
|
+
Promise.resolve().then(function() {
|
|
3542
|
+
Promise.resolve().then(function() {
|
|
3543
|
+
self._batching = false;
|
|
3544
|
+
self._flush();
|
|
3545
|
+
});
|
|
3546
|
+
});
|
|
3547
|
+
}
|
|
3548
|
+
_debug(...args) {
|
|
3549
|
+
if (!this._debugEnabled) {
|
|
3550
|
+
return;
|
|
3551
|
+
}
|
|
3552
|
+
log("debug", args);
|
|
3553
|
+
}
|
|
3554
|
+
/** @internal */
|
|
3555
|
+
_formatOverride() {
|
|
3556
|
+
return;
|
|
3557
|
+
}
|
|
3558
|
+
_configure() {
|
|
3559
|
+
if (!("Promise" in globalThis)) {
|
|
3560
|
+
throw new Error("Promise polyfill required");
|
|
3561
|
+
}
|
|
3562
|
+
if (!this._endpoint) {
|
|
3563
|
+
throw new Error("endpoint configuration required");
|
|
3564
|
+
}
|
|
3565
|
+
if (this._config.token !== null) {
|
|
3566
|
+
this._token = this._config.token;
|
|
3567
|
+
}
|
|
3568
|
+
if (this._config.data !== null) {
|
|
3569
|
+
this._data = this._config.data;
|
|
3570
|
+
}
|
|
3571
|
+
this._codec = new JsonCodec();
|
|
3572
|
+
this._formatOverride();
|
|
3573
|
+
if (this._config.debug === true || typeof localStorage !== "undefined" && localStorage.getItem("centrifuge.debug")) {
|
|
3574
|
+
this._debugEnabled = true;
|
|
3575
|
+
}
|
|
3576
|
+
this._debug("config", this._config);
|
|
3577
|
+
if (typeof this._endpoint === "string") ;
|
|
3578
|
+
else if (typeof this._endpoint === "object" && this._endpoint instanceof Array) {
|
|
3579
|
+
this._transports = this._endpoint;
|
|
3580
|
+
this._emulation = true;
|
|
3581
|
+
for (const i in this._transports) {
|
|
3582
|
+
if (this._transports.hasOwnProperty(i)) {
|
|
3583
|
+
const transportConfig = this._transports[i];
|
|
3584
|
+
if (!transportConfig.endpoint || !transportConfig.transport) {
|
|
3585
|
+
throw new Error("malformed transport configuration");
|
|
3586
|
+
}
|
|
3587
|
+
const transportName = transportConfig.transport;
|
|
3588
|
+
if (["websocket", "http_stream", "sse", "sockjs", "webtransport"].indexOf(transportName) < 0) {
|
|
3589
|
+
throw new Error("unsupported transport name: " + transportName);
|
|
3590
|
+
}
|
|
3591
|
+
}
|
|
3592
|
+
}
|
|
3593
|
+
} else {
|
|
3594
|
+
throw new Error("unsupported url configuration type: only string or array of objects are supported");
|
|
3595
|
+
}
|
|
3596
|
+
}
|
|
3597
|
+
_setState(newState) {
|
|
3598
|
+
if (this.state !== newState) {
|
|
3599
|
+
this._reconnecting = false;
|
|
3600
|
+
const oldState = this.state;
|
|
3601
|
+
this.state = newState;
|
|
3602
|
+
this.emit("state", { newState, oldState });
|
|
3603
|
+
return true;
|
|
3604
|
+
}
|
|
3605
|
+
return false;
|
|
3606
|
+
}
|
|
3607
|
+
_isDisconnected() {
|
|
3608
|
+
return this.state === State.Disconnected;
|
|
3609
|
+
}
|
|
3610
|
+
_isConnecting() {
|
|
3611
|
+
return this.state === State.Connecting;
|
|
3612
|
+
}
|
|
3613
|
+
_isConnected() {
|
|
3614
|
+
return this.state === State.Connected;
|
|
3615
|
+
}
|
|
3616
|
+
_nextCommandId() {
|
|
3617
|
+
return ++this._commandId;
|
|
3618
|
+
}
|
|
3619
|
+
_setNetworkEvents() {
|
|
3620
|
+
if (this._networkEventsSet) {
|
|
3621
|
+
return;
|
|
3622
|
+
}
|
|
3623
|
+
let eventTarget = null;
|
|
3624
|
+
if (this._config.networkEventTarget !== null) {
|
|
3625
|
+
eventTarget = this._config.networkEventTarget;
|
|
3626
|
+
} else if (typeof globalThis.addEventListener !== "undefined") {
|
|
3627
|
+
eventTarget = globalThis;
|
|
3628
|
+
}
|
|
3629
|
+
if (eventTarget) {
|
|
3630
|
+
eventTarget.addEventListener("offline", () => {
|
|
3631
|
+
this._debug("offline event triggered");
|
|
3632
|
+
if (this.state === State.Connected || this.state === State.Connecting) {
|
|
3633
|
+
this._disconnect(connectingCodes.transportClosed, "transport closed", true);
|
|
3634
|
+
this._deviceWentOffline = true;
|
|
3635
|
+
}
|
|
3636
|
+
});
|
|
3637
|
+
eventTarget.addEventListener("online", () => {
|
|
3638
|
+
this._debug("online event triggered");
|
|
3639
|
+
if (this.state !== State.Connecting) {
|
|
3640
|
+
return;
|
|
3641
|
+
}
|
|
3642
|
+
if (this._deviceWentOffline && !this._transportClosed) {
|
|
3643
|
+
this._deviceWentOffline = false;
|
|
3644
|
+
this._transportClosed = true;
|
|
3645
|
+
}
|
|
3646
|
+
this._clearReconnectTimeout();
|
|
3647
|
+
this._startReconnecting();
|
|
3648
|
+
});
|
|
3649
|
+
this._networkEventsSet = true;
|
|
3650
|
+
}
|
|
3651
|
+
}
|
|
3652
|
+
_getReconnectDelay() {
|
|
3653
|
+
const delay = backoff(this._reconnectAttempts, this._config.minReconnectDelay, this._config.maxReconnectDelay);
|
|
3654
|
+
this._reconnectAttempts += 1;
|
|
3655
|
+
return delay;
|
|
3656
|
+
}
|
|
3657
|
+
_clearOutgoingRequests() {
|
|
3658
|
+
for (const id in this._callbacks) {
|
|
3659
|
+
if (this._callbacks.hasOwnProperty(id)) {
|
|
3660
|
+
const callbacks = this._callbacks[id];
|
|
3661
|
+
clearTimeout(callbacks.timeout);
|
|
3662
|
+
const errback = callbacks.errback;
|
|
3663
|
+
if (!errback) {
|
|
3664
|
+
continue;
|
|
3665
|
+
}
|
|
3666
|
+
errback({ error: this._createErrorObject(errorCodes.connectionClosed, "connection closed") });
|
|
3667
|
+
}
|
|
3668
|
+
}
|
|
3669
|
+
this._callbacks = {};
|
|
3670
|
+
}
|
|
3671
|
+
_clearConnectedState() {
|
|
3672
|
+
this._client = null;
|
|
3673
|
+
this._clearServerPingTimeout();
|
|
3674
|
+
this._clearRefreshTimeout();
|
|
3675
|
+
for (const channel in this._subs) {
|
|
3676
|
+
if (!this._subs.hasOwnProperty(channel)) {
|
|
3677
|
+
continue;
|
|
3678
|
+
}
|
|
3679
|
+
const sub = this._subs[channel];
|
|
3680
|
+
if (sub.state === SubscriptionState.Subscribed) {
|
|
3681
|
+
sub._setSubscribing(subscribingCodes.transportClosed, "transport closed");
|
|
3682
|
+
}
|
|
3683
|
+
}
|
|
3684
|
+
for (const channel in this._serverSubs) {
|
|
3685
|
+
if (this._serverSubs.hasOwnProperty(channel)) {
|
|
3686
|
+
this.emit("subscribing", { channel });
|
|
3687
|
+
}
|
|
3688
|
+
}
|
|
3689
|
+
}
|
|
3690
|
+
_handleWriteError(commands) {
|
|
3691
|
+
for (const command of commands) {
|
|
3692
|
+
const id = command.id;
|
|
3693
|
+
if (!(id in this._callbacks)) {
|
|
3694
|
+
continue;
|
|
3695
|
+
}
|
|
3696
|
+
const callbacks = this._callbacks[id];
|
|
3697
|
+
clearTimeout(this._callbacks[id].timeout);
|
|
3698
|
+
delete this._callbacks[id];
|
|
3699
|
+
const errback = callbacks.errback;
|
|
3700
|
+
errback({ error: this._createErrorObject(errorCodes.transportWriteError, "transport write error") });
|
|
3701
|
+
}
|
|
3702
|
+
}
|
|
3703
|
+
_transportSendCommands(commands) {
|
|
3704
|
+
if (!commands.length) {
|
|
3705
|
+
return true;
|
|
3706
|
+
}
|
|
3707
|
+
if (!this._transport) {
|
|
3708
|
+
return false;
|
|
3709
|
+
}
|
|
3710
|
+
try {
|
|
3711
|
+
this._transport.send(this._codec.encodeCommands(commands), this._session, this._node);
|
|
3712
|
+
} catch (e) {
|
|
3713
|
+
this._debug("error writing commands", e);
|
|
3714
|
+
this._handleWriteError(commands);
|
|
3715
|
+
return false;
|
|
3716
|
+
}
|
|
3717
|
+
return true;
|
|
3718
|
+
}
|
|
3719
|
+
_initializeTransport() {
|
|
3720
|
+
let websocket;
|
|
3721
|
+
if (this._config.websocket !== null) {
|
|
3722
|
+
websocket = this._config.websocket;
|
|
3723
|
+
} else {
|
|
3724
|
+
if (!(typeof globalThis.WebSocket !== "function" && typeof globalThis.WebSocket !== "object")) {
|
|
3725
|
+
websocket = globalThis.WebSocket;
|
|
3726
|
+
}
|
|
3727
|
+
}
|
|
3728
|
+
let sockjs = null;
|
|
3729
|
+
if (this._config.sockjs !== null) {
|
|
3730
|
+
sockjs = this._config.sockjs;
|
|
3731
|
+
} else {
|
|
3732
|
+
if (typeof globalThis.SockJS !== "undefined") {
|
|
3733
|
+
sockjs = globalThis.SockJS;
|
|
3734
|
+
}
|
|
3735
|
+
}
|
|
3736
|
+
let eventsource = null;
|
|
3737
|
+
if (this._config.eventsource !== null) {
|
|
3738
|
+
eventsource = this._config.eventsource;
|
|
3739
|
+
} else {
|
|
3740
|
+
if (typeof globalThis.EventSource !== "undefined") {
|
|
3741
|
+
eventsource = globalThis.EventSource;
|
|
3742
|
+
}
|
|
3743
|
+
}
|
|
3744
|
+
let fetchFunc = null;
|
|
3745
|
+
if (this._config.fetch !== null) {
|
|
3746
|
+
fetchFunc = this._config.fetch;
|
|
3747
|
+
} else {
|
|
3748
|
+
if (typeof globalThis.fetch !== "undefined") {
|
|
3749
|
+
fetchFunc = globalThis.fetch;
|
|
3750
|
+
}
|
|
3751
|
+
}
|
|
3752
|
+
let readableStream = null;
|
|
3753
|
+
if (this._config.readableStream !== null) {
|
|
3754
|
+
readableStream = this._config.readableStream;
|
|
3755
|
+
} else {
|
|
3756
|
+
if (typeof globalThis.ReadableStream !== "undefined") {
|
|
3757
|
+
readableStream = globalThis.ReadableStream;
|
|
3758
|
+
}
|
|
3759
|
+
}
|
|
3760
|
+
if (!this._emulation) {
|
|
3761
|
+
if (startsWith(this._endpoint, "http")) {
|
|
3762
|
+
throw new Error("Provide explicit transport endpoints configuration in case of using HTTP (i.e. using array of TransportEndpoint instead of a single string), or use ws(s):// scheme in an endpoint if you aimed using WebSocket transport");
|
|
3763
|
+
} else {
|
|
3764
|
+
this._debug("client will use websocket");
|
|
3765
|
+
this._transport = new WebsocketTransport(this._endpoint, {
|
|
3766
|
+
websocket
|
|
3767
|
+
});
|
|
3768
|
+
if (!this._transport.supported()) {
|
|
3769
|
+
throw new Error("WebSocket not available");
|
|
3770
|
+
}
|
|
3771
|
+
}
|
|
3772
|
+
} else {
|
|
3773
|
+
if (this._currentTransportIndex >= this._transports.length) {
|
|
3774
|
+
this._triedAllTransports = true;
|
|
3775
|
+
this._currentTransportIndex = 0;
|
|
3776
|
+
}
|
|
3777
|
+
let count = 0;
|
|
3778
|
+
while (true) {
|
|
3779
|
+
if (count >= this._transports.length) {
|
|
3780
|
+
throw new Error("no supported transport found");
|
|
3781
|
+
}
|
|
3782
|
+
const transportConfig = this._transports[this._currentTransportIndex];
|
|
3783
|
+
const transportName = transportConfig.transport;
|
|
3784
|
+
const transportEndpoint = transportConfig.endpoint;
|
|
3785
|
+
if (transportName === "websocket") {
|
|
3786
|
+
this._debug("trying websocket transport");
|
|
3787
|
+
this._transport = new WebsocketTransport(transportEndpoint, {
|
|
3788
|
+
websocket
|
|
3789
|
+
});
|
|
3790
|
+
if (!this._transport.supported()) {
|
|
3791
|
+
this._debug("websocket transport not available");
|
|
3792
|
+
this._currentTransportIndex++;
|
|
3793
|
+
count++;
|
|
3794
|
+
continue;
|
|
3795
|
+
}
|
|
3796
|
+
} else if (transportName === "webtransport") {
|
|
3797
|
+
this._debug("trying webtransport transport");
|
|
3798
|
+
this._transport = new WebtransportTransport(transportEndpoint, {
|
|
3799
|
+
webtransport: globalThis.WebTransport,
|
|
3800
|
+
decoder: this._codec,
|
|
3801
|
+
encoder: this._codec
|
|
3802
|
+
});
|
|
3803
|
+
if (!this._transport.supported()) {
|
|
3804
|
+
this._debug("webtransport transport not available");
|
|
3805
|
+
this._currentTransportIndex++;
|
|
3806
|
+
count++;
|
|
3807
|
+
continue;
|
|
3808
|
+
}
|
|
3809
|
+
} else if (transportName === "http_stream") {
|
|
3810
|
+
this._debug("trying http_stream transport");
|
|
3811
|
+
this._transport = new HttpStreamTransport(transportEndpoint, {
|
|
3812
|
+
fetch: fetchFunc,
|
|
3813
|
+
readableStream,
|
|
3814
|
+
emulationEndpoint: this._config.emulationEndpoint,
|
|
3815
|
+
decoder: this._codec,
|
|
3816
|
+
encoder: this._codec
|
|
3817
|
+
});
|
|
3818
|
+
if (!this._transport.supported()) {
|
|
3819
|
+
this._debug("http_stream transport not available");
|
|
3820
|
+
this._currentTransportIndex++;
|
|
3821
|
+
count++;
|
|
3822
|
+
continue;
|
|
3823
|
+
}
|
|
3824
|
+
} else if (transportName === "sse") {
|
|
3825
|
+
this._debug("trying sse transport");
|
|
3826
|
+
this._transport = new SseTransport(transportEndpoint, {
|
|
3827
|
+
eventsource,
|
|
3828
|
+
fetch: fetchFunc,
|
|
3829
|
+
emulationEndpoint: this._config.emulationEndpoint
|
|
3830
|
+
});
|
|
3831
|
+
if (!this._transport.supported()) {
|
|
3832
|
+
this._debug("sse transport not available");
|
|
3833
|
+
this._currentTransportIndex++;
|
|
3834
|
+
count++;
|
|
3835
|
+
continue;
|
|
3836
|
+
}
|
|
3837
|
+
} else if (transportName === "sockjs") {
|
|
3838
|
+
this._debug("trying sockjs");
|
|
3839
|
+
this._transport = new SockjsTransport(transportEndpoint, {
|
|
3840
|
+
sockjs,
|
|
3841
|
+
sockjsOptions: this._config.sockjsOptions
|
|
3842
|
+
});
|
|
3843
|
+
if (!this._transport.supported()) {
|
|
3844
|
+
this._debug("sockjs transport not available");
|
|
3845
|
+
this._currentTransportIndex++;
|
|
3846
|
+
count++;
|
|
3847
|
+
continue;
|
|
3848
|
+
}
|
|
3849
|
+
} else {
|
|
3850
|
+
throw new Error("unknown transport " + transportName);
|
|
3851
|
+
}
|
|
3852
|
+
break;
|
|
3853
|
+
}
|
|
3854
|
+
}
|
|
3855
|
+
const self = this;
|
|
3856
|
+
const transport = this._transport;
|
|
3857
|
+
const transportId = this._nextTransportId();
|
|
3858
|
+
self._debug("id of transport", transportId);
|
|
3859
|
+
let wasOpen = false;
|
|
3860
|
+
const initialCommands = [];
|
|
3861
|
+
if (this._transport.emulation()) {
|
|
3862
|
+
const connectCommand = self._sendConnect(true);
|
|
3863
|
+
initialCommands.push(connectCommand);
|
|
3864
|
+
}
|
|
3865
|
+
this._setNetworkEvents();
|
|
3866
|
+
const initialData = this._codec.encodeCommands(initialCommands);
|
|
3867
|
+
this._transportClosed = false;
|
|
3868
|
+
let connectTimeout;
|
|
3869
|
+
connectTimeout = setTimeout(function() {
|
|
3870
|
+
transport.close();
|
|
3871
|
+
}, this._config.timeout);
|
|
3872
|
+
this._transport.initialize(this._codec.name(), {
|
|
3873
|
+
onOpen: function() {
|
|
3874
|
+
if (connectTimeout) {
|
|
3875
|
+
clearTimeout(connectTimeout);
|
|
3876
|
+
connectTimeout = null;
|
|
3877
|
+
}
|
|
3878
|
+
if (self._transportId != transportId) {
|
|
3879
|
+
self._debug("open callback from non-actual transport");
|
|
3880
|
+
transport.close();
|
|
3881
|
+
return;
|
|
3882
|
+
}
|
|
3883
|
+
wasOpen = true;
|
|
3884
|
+
self._debug(transport.subName(), "transport open");
|
|
3885
|
+
if (transport.emulation()) {
|
|
3886
|
+
return;
|
|
3887
|
+
}
|
|
3888
|
+
self._transportIsOpen = true;
|
|
3889
|
+
self._transportWasOpen = true;
|
|
3890
|
+
self.startBatching();
|
|
3891
|
+
self._sendConnect(false);
|
|
3892
|
+
self._sendSubscribeCommands();
|
|
3893
|
+
self.stopBatching();
|
|
3894
|
+
self.emit("__centrifuge_debug:connect_frame_sent", {});
|
|
3895
|
+
},
|
|
3896
|
+
onError: function(e) {
|
|
3897
|
+
if (self._transportId != transportId) {
|
|
3898
|
+
self._debug("error callback from non-actual transport");
|
|
3899
|
+
return;
|
|
3900
|
+
}
|
|
3901
|
+
self._debug("transport level error", e);
|
|
3902
|
+
},
|
|
3903
|
+
onClose: function(closeEvent) {
|
|
3904
|
+
if (connectTimeout) {
|
|
3905
|
+
clearTimeout(connectTimeout);
|
|
3906
|
+
connectTimeout = null;
|
|
3907
|
+
}
|
|
3908
|
+
if (self._transportId != transportId) {
|
|
3909
|
+
self._debug("close callback from non-actual transport");
|
|
3910
|
+
return;
|
|
3911
|
+
}
|
|
3912
|
+
self._debug(transport.subName(), "transport closed");
|
|
3913
|
+
self._transportClosed = true;
|
|
3914
|
+
self._transportIsOpen = false;
|
|
3915
|
+
let reason = "connection closed";
|
|
3916
|
+
let needReconnect = true;
|
|
3917
|
+
let code = 0;
|
|
3918
|
+
if (closeEvent && "code" in closeEvent && closeEvent.code) {
|
|
3919
|
+
code = closeEvent.code;
|
|
3920
|
+
}
|
|
3921
|
+
if (closeEvent && closeEvent.reason) {
|
|
3922
|
+
try {
|
|
3923
|
+
const advice = JSON.parse(closeEvent.reason);
|
|
3924
|
+
reason = advice.reason;
|
|
3925
|
+
needReconnect = advice.reconnect;
|
|
3926
|
+
} catch (e) {
|
|
3927
|
+
reason = closeEvent.reason;
|
|
3928
|
+
if (code >= 3500 && code < 4e3 || code >= 4500 && code < 5e3) {
|
|
3929
|
+
needReconnect = false;
|
|
3930
|
+
}
|
|
3931
|
+
}
|
|
3932
|
+
}
|
|
3933
|
+
if (code < 3e3) {
|
|
3934
|
+
if (code === 1009) {
|
|
3935
|
+
code = disconnectedCodes.messageSizeLimit;
|
|
3936
|
+
reason = "message size limit exceeded";
|
|
3937
|
+
needReconnect = false;
|
|
3938
|
+
} else {
|
|
3939
|
+
code = connectingCodes.transportClosed;
|
|
3940
|
+
reason = "transport closed";
|
|
3941
|
+
}
|
|
3942
|
+
if (self._emulation && !self._transportWasOpen) {
|
|
3943
|
+
self._currentTransportIndex++;
|
|
3944
|
+
if (self._currentTransportIndex >= self._transports.length) {
|
|
3945
|
+
self._triedAllTransports = true;
|
|
3946
|
+
self._currentTransportIndex = 0;
|
|
3947
|
+
}
|
|
3948
|
+
}
|
|
3949
|
+
} else {
|
|
3950
|
+
self._transportWasOpen = true;
|
|
3951
|
+
}
|
|
3952
|
+
if (self._isConnecting() && !wasOpen) {
|
|
3953
|
+
self.emit("error", {
|
|
3954
|
+
type: "transport",
|
|
3955
|
+
error: {
|
|
3956
|
+
code: errorCodes.transportClosed,
|
|
3957
|
+
message: "transport closed"
|
|
3958
|
+
},
|
|
3959
|
+
transport: transport.name()
|
|
3960
|
+
});
|
|
3961
|
+
}
|
|
3962
|
+
self._reconnecting = false;
|
|
3963
|
+
self._disconnect(code, reason, needReconnect);
|
|
3964
|
+
},
|
|
3965
|
+
onMessage: function(data) {
|
|
3966
|
+
self._dataReceived(data);
|
|
3967
|
+
}
|
|
3968
|
+
}, initialData);
|
|
3969
|
+
self.emit("__centrifuge_debug:transport_initialized", {});
|
|
3970
|
+
}
|
|
3971
|
+
_sendConnect(skipSending) {
|
|
3972
|
+
const connectCommand = this._constructConnectCommand();
|
|
3973
|
+
const self = this;
|
|
3974
|
+
this._call(connectCommand, skipSending).then((resolveCtx) => {
|
|
3975
|
+
const result = resolveCtx.reply.connect;
|
|
3976
|
+
self._connectResponse(result);
|
|
3977
|
+
if (resolveCtx.next) {
|
|
3978
|
+
resolveCtx.next();
|
|
3979
|
+
}
|
|
3980
|
+
}, (rejectCtx) => {
|
|
3981
|
+
self._connectError(rejectCtx.error);
|
|
3982
|
+
if (rejectCtx.next) {
|
|
3983
|
+
rejectCtx.next();
|
|
3984
|
+
}
|
|
3985
|
+
});
|
|
3986
|
+
return connectCommand;
|
|
3987
|
+
}
|
|
3988
|
+
_startReconnecting() {
|
|
3989
|
+
this._debug("start reconnecting");
|
|
3990
|
+
if (!this._isConnecting()) {
|
|
3991
|
+
this._debug("stop reconnecting: client not in connecting state");
|
|
3992
|
+
return;
|
|
3993
|
+
}
|
|
3994
|
+
if (this._reconnecting) {
|
|
3995
|
+
this._debug("reconnect already in progress, return from reconnect routine");
|
|
3996
|
+
return;
|
|
3997
|
+
}
|
|
3998
|
+
if (this._transportClosed === false) {
|
|
3999
|
+
this._debug("waiting for transport close");
|
|
4000
|
+
return;
|
|
4001
|
+
}
|
|
4002
|
+
this._reconnecting = true;
|
|
4003
|
+
const self = this;
|
|
4004
|
+
const emptyToken = this._token === "";
|
|
4005
|
+
const needTokenRefresh = this._refreshRequired || emptyToken && this._config.getToken !== null;
|
|
4006
|
+
if (!needTokenRefresh) {
|
|
4007
|
+
if (this._config.getData) {
|
|
4008
|
+
this._config.getData().then(function(data) {
|
|
4009
|
+
if (!self._isConnecting()) {
|
|
4010
|
+
return;
|
|
4011
|
+
}
|
|
4012
|
+
self._data = data;
|
|
4013
|
+
self._initializeTransport();
|
|
4014
|
+
});
|
|
4015
|
+
} else {
|
|
4016
|
+
this._initializeTransport();
|
|
4017
|
+
}
|
|
4018
|
+
return;
|
|
4019
|
+
}
|
|
4020
|
+
this._getToken().then(function(token) {
|
|
4021
|
+
if (!self._isConnecting()) {
|
|
4022
|
+
return;
|
|
4023
|
+
}
|
|
4024
|
+
if (token == null || token == void 0) {
|
|
4025
|
+
self._failUnauthorized();
|
|
4026
|
+
return;
|
|
4027
|
+
}
|
|
4028
|
+
self._token = token;
|
|
4029
|
+
self._debug("connection token refreshed");
|
|
4030
|
+
if (self._config.getData) {
|
|
4031
|
+
self._config.getData().then(function(data) {
|
|
4032
|
+
if (!self._isConnecting()) {
|
|
4033
|
+
return;
|
|
4034
|
+
}
|
|
4035
|
+
self._data = data;
|
|
4036
|
+
self._initializeTransport();
|
|
4037
|
+
});
|
|
4038
|
+
} else {
|
|
4039
|
+
self._initializeTransport();
|
|
4040
|
+
}
|
|
4041
|
+
}).catch(function(e) {
|
|
4042
|
+
if (!self._isConnecting()) {
|
|
4043
|
+
return;
|
|
4044
|
+
}
|
|
4045
|
+
if (e instanceof UnauthorizedError) {
|
|
4046
|
+
self._failUnauthorized();
|
|
4047
|
+
return;
|
|
4048
|
+
}
|
|
4049
|
+
self.emit("error", {
|
|
4050
|
+
"type": "connectToken",
|
|
4051
|
+
"error": {
|
|
4052
|
+
code: errorCodes.clientConnectToken,
|
|
4053
|
+
message: e !== void 0 ? e.toString() : ""
|
|
4054
|
+
}
|
|
4055
|
+
});
|
|
4056
|
+
const delay = self._getReconnectDelay();
|
|
4057
|
+
self._debug("error on connection token refresh, reconnect after " + delay + " milliseconds", e);
|
|
4058
|
+
self._reconnecting = false;
|
|
4059
|
+
self._reconnectTimeout = setTimeout(() => {
|
|
4060
|
+
self._startReconnecting();
|
|
4061
|
+
}, delay);
|
|
4062
|
+
});
|
|
4063
|
+
}
|
|
4064
|
+
_connectError(err) {
|
|
4065
|
+
if (this.state !== State.Connecting) {
|
|
4066
|
+
return;
|
|
4067
|
+
}
|
|
4068
|
+
if (err.code === 109) {
|
|
4069
|
+
this._refreshRequired = true;
|
|
4070
|
+
}
|
|
4071
|
+
if (err.code < 100 || err.temporary === true || err.code === 109) {
|
|
4072
|
+
this.emit("error", {
|
|
4073
|
+
"type": "connect",
|
|
4074
|
+
"error": err
|
|
4075
|
+
});
|
|
4076
|
+
this._debug("closing transport due to connect error");
|
|
4077
|
+
this._disconnect(err.code, err.message, true);
|
|
4078
|
+
} else {
|
|
4079
|
+
this._disconnect(err.code, err.message, false);
|
|
4080
|
+
}
|
|
4081
|
+
}
|
|
4082
|
+
_scheduleReconnect() {
|
|
4083
|
+
if (!this._isConnecting()) {
|
|
4084
|
+
return;
|
|
4085
|
+
}
|
|
4086
|
+
let isInitialHandshake = false;
|
|
4087
|
+
if (this._emulation && !this._transportWasOpen && !this._triedAllTransports) {
|
|
4088
|
+
isInitialHandshake = true;
|
|
4089
|
+
}
|
|
4090
|
+
let delay = this._getReconnectDelay();
|
|
4091
|
+
if (isInitialHandshake) {
|
|
4092
|
+
delay = 0;
|
|
4093
|
+
}
|
|
4094
|
+
this._debug("reconnect after " + delay + " milliseconds");
|
|
4095
|
+
this._clearReconnectTimeout();
|
|
4096
|
+
this._reconnectTimeout = setTimeout(() => {
|
|
4097
|
+
this._startReconnecting();
|
|
4098
|
+
}, delay);
|
|
4099
|
+
}
|
|
4100
|
+
_constructConnectCommand() {
|
|
4101
|
+
const req = {};
|
|
4102
|
+
if (this._token) {
|
|
4103
|
+
req.token = this._token;
|
|
4104
|
+
}
|
|
4105
|
+
if (this._data) {
|
|
4106
|
+
req.data = this._data;
|
|
4107
|
+
}
|
|
4108
|
+
if (this._config.name) {
|
|
4109
|
+
req.name = this._config.name;
|
|
4110
|
+
}
|
|
4111
|
+
if (this._config.version) {
|
|
4112
|
+
req.version = this._config.version;
|
|
4113
|
+
}
|
|
4114
|
+
const subs = {};
|
|
4115
|
+
let hasSubs = false;
|
|
4116
|
+
for (const channel in this._serverSubs) {
|
|
4117
|
+
if (this._serverSubs.hasOwnProperty(channel) && this._serverSubs[channel].recoverable) {
|
|
4118
|
+
hasSubs = true;
|
|
4119
|
+
const sub = {
|
|
4120
|
+
"recover": true
|
|
4121
|
+
};
|
|
4122
|
+
if (this._serverSubs[channel].offset) {
|
|
4123
|
+
sub["offset"] = this._serverSubs[channel].offset;
|
|
4124
|
+
}
|
|
4125
|
+
if (this._serverSubs[channel].epoch) {
|
|
4126
|
+
sub["epoch"] = this._serverSubs[channel].epoch;
|
|
4127
|
+
}
|
|
4128
|
+
subs[channel] = sub;
|
|
4129
|
+
}
|
|
4130
|
+
}
|
|
4131
|
+
if (hasSubs) {
|
|
4132
|
+
req.subs = subs;
|
|
4133
|
+
}
|
|
4134
|
+
return {
|
|
4135
|
+
connect: req
|
|
4136
|
+
};
|
|
4137
|
+
}
|
|
4138
|
+
_getHistoryRequest(channel, options) {
|
|
4139
|
+
const req = {
|
|
4140
|
+
channel
|
|
4141
|
+
};
|
|
4142
|
+
if (options !== void 0) {
|
|
4143
|
+
if (options.since) {
|
|
4144
|
+
req.since = {
|
|
4145
|
+
offset: options.since.offset
|
|
4146
|
+
};
|
|
4147
|
+
if (options.since.epoch) {
|
|
4148
|
+
req.since.epoch = options.since.epoch;
|
|
4149
|
+
}
|
|
4150
|
+
}
|
|
4151
|
+
if (options.limit !== void 0) {
|
|
4152
|
+
req.limit = options.limit;
|
|
4153
|
+
}
|
|
4154
|
+
if (options.reverse === true) {
|
|
4155
|
+
req.reverse = true;
|
|
4156
|
+
}
|
|
4157
|
+
}
|
|
4158
|
+
return req;
|
|
4159
|
+
}
|
|
4160
|
+
_methodCall() {
|
|
4161
|
+
if (this._isConnected()) {
|
|
4162
|
+
return Promise.resolve();
|
|
4163
|
+
}
|
|
4164
|
+
return new Promise((res, rej) => {
|
|
4165
|
+
const timeout = setTimeout(function() {
|
|
4166
|
+
rej({ code: errorCodes.timeout, message: "timeout" });
|
|
4167
|
+
}, this._config.timeout);
|
|
4168
|
+
this._promises[this._nextPromiseId()] = {
|
|
4169
|
+
timeout,
|
|
4170
|
+
resolve: res,
|
|
4171
|
+
reject: rej
|
|
4172
|
+
};
|
|
4173
|
+
});
|
|
4174
|
+
}
|
|
4175
|
+
_callPromise(cmd, resultCB) {
|
|
4176
|
+
return new Promise((resolve, reject) => {
|
|
4177
|
+
this._call(cmd, false).then((resolveCtx) => {
|
|
4178
|
+
resolve(resultCB(resolveCtx.reply));
|
|
4179
|
+
if (resolveCtx.next) {
|
|
4180
|
+
resolveCtx.next();
|
|
4181
|
+
}
|
|
4182
|
+
}, (rejectCtx) => {
|
|
4183
|
+
reject(rejectCtx.error);
|
|
4184
|
+
if (rejectCtx.next) {
|
|
4185
|
+
rejectCtx.next();
|
|
4186
|
+
}
|
|
4187
|
+
});
|
|
4188
|
+
});
|
|
4189
|
+
}
|
|
4190
|
+
_dataReceived(data) {
|
|
4191
|
+
if (this._serverPing > 0) {
|
|
4192
|
+
this._waitServerPing();
|
|
4193
|
+
}
|
|
4194
|
+
const replies = this._codec.decodeReplies(data);
|
|
4195
|
+
this._dispatchPromise = this._dispatchPromise.then(() => {
|
|
4196
|
+
let finishDispatch;
|
|
4197
|
+
this._dispatchPromise = new Promise((resolve) => {
|
|
4198
|
+
finishDispatch = resolve;
|
|
4199
|
+
});
|
|
4200
|
+
this._dispatchSynchronized(replies, finishDispatch);
|
|
4201
|
+
});
|
|
4202
|
+
}
|
|
4203
|
+
_dispatchSynchronized(replies, finishDispatch) {
|
|
4204
|
+
let p = Promise.resolve();
|
|
4205
|
+
for (const i in replies) {
|
|
4206
|
+
if (replies.hasOwnProperty(i)) {
|
|
4207
|
+
p = p.then(() => {
|
|
4208
|
+
return this._dispatchReply(replies[i]);
|
|
4209
|
+
});
|
|
4210
|
+
}
|
|
4211
|
+
}
|
|
4212
|
+
p = p.then(() => {
|
|
4213
|
+
finishDispatch();
|
|
4214
|
+
});
|
|
4215
|
+
}
|
|
4216
|
+
_dispatchReply(reply) {
|
|
4217
|
+
let next;
|
|
4218
|
+
const p = new Promise((resolve) => {
|
|
4219
|
+
next = resolve;
|
|
4220
|
+
});
|
|
4221
|
+
if (reply === void 0 || reply === null) {
|
|
4222
|
+
this._debug("dispatch: got undefined or null reply");
|
|
4223
|
+
next();
|
|
4224
|
+
return p;
|
|
4225
|
+
}
|
|
4226
|
+
const id = reply.id;
|
|
4227
|
+
if (id && id > 0) {
|
|
4228
|
+
this._handleReply(reply, next);
|
|
4229
|
+
} else {
|
|
4230
|
+
if (!reply.push) {
|
|
4231
|
+
this._handleServerPing(next);
|
|
4232
|
+
} else {
|
|
4233
|
+
this._handlePush(reply.push, next);
|
|
4234
|
+
}
|
|
4235
|
+
}
|
|
4236
|
+
return p;
|
|
4237
|
+
}
|
|
4238
|
+
_call(cmd, skipSending) {
|
|
4239
|
+
return new Promise((resolve, reject) => {
|
|
4240
|
+
cmd.id = this._nextCommandId();
|
|
4241
|
+
this._registerCall(cmd.id, resolve, reject);
|
|
4242
|
+
if (!skipSending) {
|
|
4243
|
+
this._addCommand(cmd);
|
|
4244
|
+
}
|
|
4245
|
+
});
|
|
4246
|
+
}
|
|
4247
|
+
_startConnecting() {
|
|
4248
|
+
this._debug("start connecting");
|
|
4249
|
+
if (this._setState(State.Connecting)) {
|
|
4250
|
+
this.emit("connecting", { code: connectingCodes.connectCalled, reason: "connect called" });
|
|
4251
|
+
}
|
|
4252
|
+
this._client = null;
|
|
4253
|
+
this._startReconnecting();
|
|
4254
|
+
}
|
|
4255
|
+
_disconnect(code, reason, reconnect) {
|
|
4256
|
+
if (this._isDisconnected()) {
|
|
4257
|
+
return;
|
|
4258
|
+
}
|
|
4259
|
+
this._transportIsOpen = false;
|
|
4260
|
+
const previousState = this.state;
|
|
4261
|
+
this._reconnecting = false;
|
|
4262
|
+
const ctx = {
|
|
4263
|
+
code,
|
|
4264
|
+
reason
|
|
4265
|
+
};
|
|
4266
|
+
let needEvent = false;
|
|
4267
|
+
if (reconnect) {
|
|
4268
|
+
needEvent = this._setState(State.Connecting);
|
|
4269
|
+
} else {
|
|
4270
|
+
needEvent = this._setState(State.Disconnected);
|
|
4271
|
+
this._rejectPromises({ code: errorCodes.clientDisconnected, message: "disconnected" });
|
|
4272
|
+
}
|
|
4273
|
+
this._clearOutgoingRequests();
|
|
4274
|
+
if (previousState === State.Connecting) {
|
|
4275
|
+
this._clearReconnectTimeout();
|
|
4276
|
+
}
|
|
4277
|
+
if (previousState === State.Connected) {
|
|
4278
|
+
this._clearConnectedState();
|
|
4279
|
+
}
|
|
4280
|
+
if (needEvent) {
|
|
4281
|
+
if (this._isConnecting()) {
|
|
4282
|
+
this.emit("connecting", ctx);
|
|
4283
|
+
} else {
|
|
4284
|
+
this.emit("disconnected", ctx);
|
|
4285
|
+
}
|
|
4286
|
+
}
|
|
4287
|
+
if (this._transport) {
|
|
4288
|
+
this._debug("closing existing transport");
|
|
4289
|
+
const transport = this._transport;
|
|
4290
|
+
this._transport = null;
|
|
4291
|
+
transport.close();
|
|
4292
|
+
this._transportClosed = true;
|
|
4293
|
+
this._nextTransportId();
|
|
4294
|
+
} else {
|
|
4295
|
+
this._debug("no transport to close");
|
|
4296
|
+
}
|
|
4297
|
+
this._scheduleReconnect();
|
|
4298
|
+
}
|
|
4299
|
+
_failUnauthorized() {
|
|
4300
|
+
this._disconnect(disconnectedCodes.unauthorized, "unauthorized", false);
|
|
4301
|
+
}
|
|
4302
|
+
_getToken() {
|
|
4303
|
+
this._debug("get connection token");
|
|
4304
|
+
if (!this._config.getToken) {
|
|
4305
|
+
this.emit("error", {
|
|
4306
|
+
type: "configuration",
|
|
4307
|
+
error: {
|
|
4308
|
+
code: errorCodes.badConfiguration,
|
|
4309
|
+
message: "token expired but no getToken function set in the configuration"
|
|
4310
|
+
}
|
|
4311
|
+
});
|
|
4312
|
+
throw new UnauthorizedError("");
|
|
4313
|
+
}
|
|
4314
|
+
return this._config.getToken({});
|
|
4315
|
+
}
|
|
4316
|
+
_refresh() {
|
|
4317
|
+
const clientId = this._client;
|
|
4318
|
+
const self = this;
|
|
4319
|
+
this._getToken().then(function(token) {
|
|
4320
|
+
if (clientId !== self._client) {
|
|
4321
|
+
return;
|
|
4322
|
+
}
|
|
4323
|
+
if (!token) {
|
|
4324
|
+
self._failUnauthorized();
|
|
4325
|
+
return;
|
|
4326
|
+
}
|
|
4327
|
+
self._token = token;
|
|
4328
|
+
self._debug("connection token refreshed");
|
|
4329
|
+
if (!self._isConnected()) {
|
|
4330
|
+
return;
|
|
4331
|
+
}
|
|
4332
|
+
const cmd = {
|
|
4333
|
+
refresh: { token: self._token }
|
|
4334
|
+
};
|
|
4335
|
+
self._call(cmd, false).then((resolveCtx) => {
|
|
4336
|
+
const result = resolveCtx.reply.refresh;
|
|
4337
|
+
self._refreshResponse(result);
|
|
4338
|
+
if (resolveCtx.next) {
|
|
4339
|
+
resolveCtx.next();
|
|
4340
|
+
}
|
|
4341
|
+
}, (rejectCtx) => {
|
|
4342
|
+
self._refreshError(rejectCtx.error);
|
|
4343
|
+
if (rejectCtx.next) {
|
|
4344
|
+
rejectCtx.next();
|
|
4345
|
+
}
|
|
4346
|
+
});
|
|
4347
|
+
}).catch(function(e) {
|
|
4348
|
+
if (!self._isConnected()) {
|
|
4349
|
+
return;
|
|
4350
|
+
}
|
|
4351
|
+
if (e instanceof UnauthorizedError) {
|
|
4352
|
+
self._failUnauthorized();
|
|
4353
|
+
return;
|
|
4354
|
+
}
|
|
4355
|
+
self.emit("error", {
|
|
4356
|
+
type: "refreshToken",
|
|
4357
|
+
error: {
|
|
4358
|
+
code: errorCodes.clientRefreshToken,
|
|
4359
|
+
message: e !== void 0 ? e.toString() : ""
|
|
4360
|
+
}
|
|
4361
|
+
});
|
|
4362
|
+
self._refreshTimeout = setTimeout(() => self._refresh(), self._getRefreshRetryDelay());
|
|
4363
|
+
});
|
|
4364
|
+
}
|
|
4365
|
+
_refreshError(err) {
|
|
4366
|
+
if (err.code < 100 || err.temporary === true) {
|
|
4367
|
+
this.emit("error", {
|
|
4368
|
+
type: "refresh",
|
|
4369
|
+
error: err
|
|
4370
|
+
});
|
|
4371
|
+
this._refreshTimeout = setTimeout(() => this._refresh(), this._getRefreshRetryDelay());
|
|
4372
|
+
} else {
|
|
4373
|
+
this._disconnect(err.code, err.message, false);
|
|
4374
|
+
}
|
|
4375
|
+
}
|
|
4376
|
+
_getRefreshRetryDelay() {
|
|
4377
|
+
return backoff(0, 5e3, 1e4);
|
|
4378
|
+
}
|
|
4379
|
+
_refreshResponse(result) {
|
|
4380
|
+
if (this._refreshTimeout) {
|
|
4381
|
+
clearTimeout(this._refreshTimeout);
|
|
4382
|
+
this._refreshTimeout = null;
|
|
4383
|
+
}
|
|
4384
|
+
if (result.expires) {
|
|
4385
|
+
this._client = result.client;
|
|
4386
|
+
this._refreshTimeout = setTimeout(() => this._refresh(), ttlMilliseconds(result.ttl));
|
|
4387
|
+
}
|
|
4388
|
+
}
|
|
4389
|
+
_removeSubscription(sub) {
|
|
4390
|
+
if (sub === null) {
|
|
4391
|
+
return;
|
|
4392
|
+
}
|
|
4393
|
+
delete this._subs[sub.channel];
|
|
4394
|
+
}
|
|
4395
|
+
_unsubscribe(sub) {
|
|
4396
|
+
if (!this._transportIsOpen) {
|
|
4397
|
+
return Promise.resolve();
|
|
4398
|
+
}
|
|
4399
|
+
const req = {
|
|
4400
|
+
channel: sub.channel
|
|
4401
|
+
};
|
|
4402
|
+
const cmd = { unsubscribe: req };
|
|
4403
|
+
const self = this;
|
|
4404
|
+
const unsubscribePromise = new Promise((resolve, _2) => {
|
|
4405
|
+
this._call(cmd, false).then((resolveCtx) => {
|
|
4406
|
+
resolve();
|
|
4407
|
+
if (resolveCtx.next) {
|
|
4408
|
+
resolveCtx.next();
|
|
4409
|
+
}
|
|
4410
|
+
}, (rejectCtx) => {
|
|
4411
|
+
resolve();
|
|
4412
|
+
if (rejectCtx.next) {
|
|
4413
|
+
rejectCtx.next();
|
|
4414
|
+
}
|
|
4415
|
+
self._disconnect(connectingCodes.unsubscribeError, "unsubscribe error", true);
|
|
4416
|
+
});
|
|
4417
|
+
});
|
|
4418
|
+
return unsubscribePromise;
|
|
4419
|
+
}
|
|
4420
|
+
_getSub(channel) {
|
|
4421
|
+
const sub = this._subs[channel];
|
|
4422
|
+
if (!sub) {
|
|
4423
|
+
return null;
|
|
4424
|
+
}
|
|
4425
|
+
return sub;
|
|
4426
|
+
}
|
|
4427
|
+
_isServerSub(channel) {
|
|
4428
|
+
return this._serverSubs[channel] !== void 0;
|
|
4429
|
+
}
|
|
4430
|
+
_sendSubscribeCommands() {
|
|
4431
|
+
const commands = [];
|
|
4432
|
+
for (const channel in this._subs) {
|
|
4433
|
+
if (!this._subs.hasOwnProperty(channel)) {
|
|
4434
|
+
continue;
|
|
4435
|
+
}
|
|
4436
|
+
const sub = this._subs[channel];
|
|
4437
|
+
if (sub._inflight === true) {
|
|
4438
|
+
continue;
|
|
4439
|
+
}
|
|
4440
|
+
if (sub.state === SubscriptionState.Subscribing) {
|
|
4441
|
+
const cmd = sub._subscribe();
|
|
4442
|
+
if (cmd) {
|
|
4443
|
+
commands.push(cmd);
|
|
4444
|
+
}
|
|
4445
|
+
}
|
|
4446
|
+
}
|
|
4447
|
+
return commands;
|
|
4448
|
+
}
|
|
4449
|
+
_connectResponse(result) {
|
|
4450
|
+
this._transportIsOpen = true;
|
|
4451
|
+
this._transportWasOpen = true;
|
|
4452
|
+
this._reconnectAttempts = 0;
|
|
4453
|
+
this._refreshRequired = false;
|
|
4454
|
+
if (this._isConnected()) {
|
|
4455
|
+
return;
|
|
4456
|
+
}
|
|
4457
|
+
this._client = result.client;
|
|
4458
|
+
this._setState(State.Connected);
|
|
4459
|
+
if (this._refreshTimeout) {
|
|
4460
|
+
clearTimeout(this._refreshTimeout);
|
|
4461
|
+
}
|
|
4462
|
+
if (result.expires) {
|
|
4463
|
+
this._refreshTimeout = setTimeout(() => this._refresh(), ttlMilliseconds(result.ttl));
|
|
4464
|
+
}
|
|
4465
|
+
this._session = result.session;
|
|
4466
|
+
this._node = result.node;
|
|
4467
|
+
this.startBatching();
|
|
4468
|
+
this._sendSubscribeCommands();
|
|
4469
|
+
this.stopBatching();
|
|
4470
|
+
const ctx = {
|
|
4471
|
+
client: result.client,
|
|
4472
|
+
transport: this._transport.subName()
|
|
4473
|
+
};
|
|
4474
|
+
if (result.data) {
|
|
4475
|
+
ctx.data = result.data;
|
|
4476
|
+
}
|
|
4477
|
+
this.emit("connected", ctx);
|
|
4478
|
+
this._resolvePromises();
|
|
4479
|
+
this._processServerSubs(result.subs || {});
|
|
4480
|
+
if (result.ping && result.ping > 0) {
|
|
4481
|
+
this._serverPing = result.ping * 1e3;
|
|
4482
|
+
this._sendPong = result.pong === true;
|
|
4483
|
+
this._waitServerPing();
|
|
4484
|
+
} else {
|
|
4485
|
+
this._serverPing = 0;
|
|
4486
|
+
}
|
|
4487
|
+
}
|
|
4488
|
+
_processServerSubs(subs) {
|
|
4489
|
+
for (const channel in subs) {
|
|
4490
|
+
if (!subs.hasOwnProperty(channel)) {
|
|
4491
|
+
continue;
|
|
4492
|
+
}
|
|
4493
|
+
const sub = subs[channel];
|
|
4494
|
+
this._serverSubs[channel] = {
|
|
4495
|
+
"offset": sub.offset,
|
|
4496
|
+
"epoch": sub.epoch,
|
|
4497
|
+
"recoverable": sub.recoverable || false
|
|
4498
|
+
};
|
|
4499
|
+
const subCtx = this._getSubscribeContext(channel, sub);
|
|
4500
|
+
this.emit("subscribed", subCtx);
|
|
4501
|
+
}
|
|
4502
|
+
for (const channel in subs) {
|
|
4503
|
+
if (!subs.hasOwnProperty(channel)) {
|
|
4504
|
+
continue;
|
|
4505
|
+
}
|
|
4506
|
+
const sub = subs[channel];
|
|
4507
|
+
if (sub.recovered) {
|
|
4508
|
+
const pubs = sub.publications;
|
|
4509
|
+
if (pubs && pubs.length > 0) {
|
|
4510
|
+
for (const i in pubs) {
|
|
4511
|
+
if (pubs.hasOwnProperty(i)) {
|
|
4512
|
+
this._handlePublication(channel, pubs[i]);
|
|
4513
|
+
}
|
|
4514
|
+
}
|
|
4515
|
+
}
|
|
4516
|
+
}
|
|
4517
|
+
}
|
|
4518
|
+
for (const channel in this._serverSubs) {
|
|
4519
|
+
if (!this._serverSubs.hasOwnProperty(channel)) {
|
|
4520
|
+
continue;
|
|
4521
|
+
}
|
|
4522
|
+
if (!subs[channel]) {
|
|
4523
|
+
this.emit("unsubscribed", { channel });
|
|
4524
|
+
delete this._serverSubs[channel];
|
|
4525
|
+
}
|
|
4526
|
+
}
|
|
4527
|
+
}
|
|
4528
|
+
_clearRefreshTimeout() {
|
|
4529
|
+
if (this._refreshTimeout !== null) {
|
|
4530
|
+
clearTimeout(this._refreshTimeout);
|
|
4531
|
+
this._refreshTimeout = null;
|
|
4532
|
+
}
|
|
4533
|
+
}
|
|
4534
|
+
_clearReconnectTimeout() {
|
|
4535
|
+
if (this._reconnectTimeout !== null) {
|
|
4536
|
+
clearTimeout(this._reconnectTimeout);
|
|
4537
|
+
this._reconnectTimeout = null;
|
|
4538
|
+
}
|
|
4539
|
+
}
|
|
4540
|
+
_clearServerPingTimeout() {
|
|
4541
|
+
if (this._serverPingTimeout !== null) {
|
|
4542
|
+
clearTimeout(this._serverPingTimeout);
|
|
4543
|
+
this._serverPingTimeout = null;
|
|
4544
|
+
}
|
|
4545
|
+
}
|
|
4546
|
+
_waitServerPing() {
|
|
4547
|
+
if (this._config.maxServerPingDelay === 0) {
|
|
4548
|
+
return;
|
|
4549
|
+
}
|
|
4550
|
+
if (!this._isConnected()) {
|
|
4551
|
+
return;
|
|
4552
|
+
}
|
|
4553
|
+
this._clearServerPingTimeout();
|
|
4554
|
+
this._serverPingTimeout = setTimeout(() => {
|
|
4555
|
+
if (!this._isConnected()) {
|
|
4556
|
+
return;
|
|
4557
|
+
}
|
|
4558
|
+
this._disconnect(connectingCodes.noPing, "no ping", true);
|
|
4559
|
+
}, this._serverPing + this._config.maxServerPingDelay);
|
|
4560
|
+
}
|
|
4561
|
+
_getSubscribeContext(channel, result) {
|
|
4562
|
+
const ctx = {
|
|
4563
|
+
channel,
|
|
4564
|
+
positioned: false,
|
|
4565
|
+
recoverable: false,
|
|
4566
|
+
wasRecovering: false,
|
|
4567
|
+
recovered: false
|
|
4568
|
+
};
|
|
4569
|
+
if (result.recovered) {
|
|
4570
|
+
ctx.recovered = true;
|
|
4571
|
+
}
|
|
4572
|
+
if (result.positioned) {
|
|
4573
|
+
ctx.positioned = true;
|
|
4574
|
+
}
|
|
4575
|
+
if (result.recoverable) {
|
|
4576
|
+
ctx.recoverable = true;
|
|
4577
|
+
}
|
|
4578
|
+
if (result.was_recovering) {
|
|
4579
|
+
ctx.wasRecovering = true;
|
|
4580
|
+
}
|
|
4581
|
+
let epoch = "";
|
|
4582
|
+
if ("epoch" in result) {
|
|
4583
|
+
epoch = result.epoch;
|
|
4584
|
+
}
|
|
4585
|
+
let offset = 0;
|
|
4586
|
+
if ("offset" in result) {
|
|
4587
|
+
offset = result.offset;
|
|
4588
|
+
}
|
|
4589
|
+
if (ctx.positioned || ctx.recoverable) {
|
|
4590
|
+
ctx.streamPosition = {
|
|
4591
|
+
"offset": offset,
|
|
4592
|
+
"epoch": epoch
|
|
4593
|
+
};
|
|
4594
|
+
}
|
|
4595
|
+
if (result.data) {
|
|
4596
|
+
ctx.data = result.data;
|
|
4597
|
+
}
|
|
4598
|
+
return ctx;
|
|
4599
|
+
}
|
|
4600
|
+
_handleReply(reply, next) {
|
|
4601
|
+
const id = reply.id;
|
|
4602
|
+
if (!(id in this._callbacks)) {
|
|
4603
|
+
next();
|
|
4604
|
+
return;
|
|
4605
|
+
}
|
|
4606
|
+
const callbacks = this._callbacks[id];
|
|
4607
|
+
clearTimeout(this._callbacks[id].timeout);
|
|
4608
|
+
delete this._callbacks[id];
|
|
4609
|
+
if (!errorExists(reply)) {
|
|
4610
|
+
const callback = callbacks.callback;
|
|
4611
|
+
if (!callback) {
|
|
4612
|
+
return;
|
|
4613
|
+
}
|
|
4614
|
+
callback({ reply, next });
|
|
4615
|
+
} else {
|
|
4616
|
+
const errback = callbacks.errback;
|
|
4617
|
+
if (!errback) {
|
|
4618
|
+
next();
|
|
4619
|
+
return;
|
|
4620
|
+
}
|
|
4621
|
+
const error = reply.error;
|
|
4622
|
+
errback({ error, next });
|
|
4623
|
+
}
|
|
4624
|
+
}
|
|
4625
|
+
_handleJoin(channel, join) {
|
|
4626
|
+
const sub = this._getSub(channel);
|
|
4627
|
+
if (!sub) {
|
|
4628
|
+
if (this._isServerSub(channel)) {
|
|
4629
|
+
const ctx = { channel, info: this._getJoinLeaveContext(join.info) };
|
|
4630
|
+
this.emit("join", ctx);
|
|
4631
|
+
}
|
|
4632
|
+
return;
|
|
4633
|
+
}
|
|
4634
|
+
sub._handleJoin(join);
|
|
4635
|
+
}
|
|
4636
|
+
_handleLeave(channel, leave) {
|
|
4637
|
+
const sub = this._getSub(channel);
|
|
4638
|
+
if (!sub) {
|
|
4639
|
+
if (this._isServerSub(channel)) {
|
|
4640
|
+
const ctx = { channel, info: this._getJoinLeaveContext(leave.info) };
|
|
4641
|
+
this.emit("leave", ctx);
|
|
4642
|
+
}
|
|
4643
|
+
return;
|
|
4644
|
+
}
|
|
4645
|
+
sub._handleLeave(leave);
|
|
4646
|
+
}
|
|
4647
|
+
_handleUnsubscribe(channel, unsubscribe) {
|
|
4648
|
+
const sub = this._getSub(channel);
|
|
4649
|
+
if (!sub) {
|
|
4650
|
+
if (this._isServerSub(channel)) {
|
|
4651
|
+
delete this._serverSubs[channel];
|
|
4652
|
+
this.emit("unsubscribed", { channel });
|
|
4653
|
+
}
|
|
4654
|
+
return;
|
|
4655
|
+
}
|
|
4656
|
+
if (unsubscribe.code < 2500) {
|
|
4657
|
+
sub._setUnsubscribed(unsubscribe.code, unsubscribe.reason, false);
|
|
4658
|
+
} else {
|
|
4659
|
+
sub._setSubscribing(unsubscribe.code, unsubscribe.reason);
|
|
4660
|
+
}
|
|
4661
|
+
}
|
|
4662
|
+
_handleSubscribe(channel, sub) {
|
|
4663
|
+
this._serverSubs[channel] = {
|
|
4664
|
+
"offset": sub.offset,
|
|
4665
|
+
"epoch": sub.epoch,
|
|
4666
|
+
"recoverable": sub.recoverable || false
|
|
4667
|
+
};
|
|
4668
|
+
this.emit("subscribed", this._getSubscribeContext(channel, sub));
|
|
4669
|
+
}
|
|
4670
|
+
_handleDisconnect(disconnect) {
|
|
4671
|
+
const code = disconnect.code;
|
|
4672
|
+
let reconnect = true;
|
|
4673
|
+
if (code >= 3500 && code < 4e3 || code >= 4500 && code < 5e3) {
|
|
4674
|
+
reconnect = false;
|
|
4675
|
+
}
|
|
4676
|
+
this._disconnect(code, disconnect.reason, reconnect);
|
|
4677
|
+
}
|
|
4678
|
+
_getPublicationContext(channel, pub) {
|
|
4679
|
+
const ctx = {
|
|
4680
|
+
channel,
|
|
4681
|
+
data: pub.data
|
|
4682
|
+
};
|
|
4683
|
+
if (pub.offset) {
|
|
4684
|
+
ctx.offset = pub.offset;
|
|
4685
|
+
}
|
|
4686
|
+
if (pub.info) {
|
|
4687
|
+
ctx.info = this._getJoinLeaveContext(pub.info);
|
|
4688
|
+
}
|
|
4689
|
+
if (pub.tags) {
|
|
4690
|
+
ctx.tags = pub.tags;
|
|
4691
|
+
}
|
|
4692
|
+
return ctx;
|
|
4693
|
+
}
|
|
4694
|
+
_getJoinLeaveContext(clientInfo) {
|
|
4695
|
+
const info = {
|
|
4696
|
+
client: clientInfo.client,
|
|
4697
|
+
user: clientInfo.user
|
|
4698
|
+
};
|
|
4699
|
+
if (clientInfo.conn_info) {
|
|
4700
|
+
info.connInfo = clientInfo.conn_info;
|
|
4701
|
+
}
|
|
4702
|
+
if (clientInfo.chan_info) {
|
|
4703
|
+
info.chanInfo = clientInfo.chan_info;
|
|
4704
|
+
}
|
|
4705
|
+
return info;
|
|
4706
|
+
}
|
|
4707
|
+
_handlePublication(channel, pub) {
|
|
4708
|
+
const sub = this._getSub(channel);
|
|
4709
|
+
if (!sub) {
|
|
4710
|
+
if (this._isServerSub(channel)) {
|
|
4711
|
+
const ctx = this._getPublicationContext(channel, pub);
|
|
4712
|
+
this.emit("publication", ctx);
|
|
4713
|
+
if (pub.offset !== void 0) {
|
|
4714
|
+
this._serverSubs[channel].offset = pub.offset;
|
|
4715
|
+
}
|
|
4716
|
+
}
|
|
4717
|
+
return;
|
|
4718
|
+
}
|
|
4719
|
+
sub._handlePublication(pub);
|
|
4720
|
+
}
|
|
4721
|
+
_handleMessage(message) {
|
|
4722
|
+
this.emit("message", { data: message.data });
|
|
4723
|
+
}
|
|
4724
|
+
_handleServerPing(next) {
|
|
4725
|
+
if (this._sendPong) {
|
|
4726
|
+
const cmd = {};
|
|
4727
|
+
this._transportSendCommands([cmd]);
|
|
4728
|
+
}
|
|
4729
|
+
next();
|
|
4730
|
+
}
|
|
4731
|
+
_handlePush(data, next) {
|
|
4732
|
+
const channel = data.channel;
|
|
4733
|
+
if (data.pub) {
|
|
4734
|
+
this._handlePublication(channel, data.pub);
|
|
4735
|
+
} else if (data.message) {
|
|
4736
|
+
this._handleMessage(data.message);
|
|
4737
|
+
} else if (data.join) {
|
|
4738
|
+
this._handleJoin(channel, data.join);
|
|
4739
|
+
} else if (data.leave) {
|
|
4740
|
+
this._handleLeave(channel, data.leave);
|
|
4741
|
+
} else if (data.unsubscribe) {
|
|
4742
|
+
this._handleUnsubscribe(channel, data.unsubscribe);
|
|
4743
|
+
} else if (data.subscribe) {
|
|
4744
|
+
this._handleSubscribe(channel, data.subscribe);
|
|
4745
|
+
} else if (data.disconnect) {
|
|
4746
|
+
this._handleDisconnect(data.disconnect);
|
|
4747
|
+
}
|
|
4748
|
+
next();
|
|
4749
|
+
}
|
|
4750
|
+
_flush() {
|
|
4751
|
+
const commands = this._commands.slice(0);
|
|
4752
|
+
this._commands = [];
|
|
4753
|
+
this._transportSendCommands(commands);
|
|
4754
|
+
}
|
|
4755
|
+
_createErrorObject(code, message, temporary) {
|
|
4756
|
+
const errObject = {
|
|
4757
|
+
code,
|
|
4758
|
+
message
|
|
4759
|
+
};
|
|
4760
|
+
if (temporary) {
|
|
4761
|
+
errObject.temporary = true;
|
|
4762
|
+
}
|
|
4763
|
+
return errObject;
|
|
4764
|
+
}
|
|
4765
|
+
_registerCall(id, callback, errback) {
|
|
4766
|
+
this._callbacks[id] = {
|
|
4767
|
+
callback,
|
|
4768
|
+
errback,
|
|
4769
|
+
timeout: null
|
|
4770
|
+
};
|
|
4771
|
+
this._callbacks[id].timeout = setTimeout(() => {
|
|
4772
|
+
delete this._callbacks[id];
|
|
4773
|
+
if (isFunction(errback)) {
|
|
4774
|
+
errback({ error: this._createErrorObject(errorCodes.timeout, "timeout") });
|
|
4775
|
+
}
|
|
4776
|
+
}, this._config.timeout);
|
|
4777
|
+
}
|
|
4778
|
+
_addCommand(command) {
|
|
4779
|
+
if (this._batching) {
|
|
4780
|
+
this._commands.push(command);
|
|
4781
|
+
} else {
|
|
4782
|
+
this._transportSendCommands([command]);
|
|
4783
|
+
}
|
|
4784
|
+
}
|
|
4785
|
+
_nextPromiseId() {
|
|
4786
|
+
return ++this._promiseId;
|
|
4787
|
+
}
|
|
4788
|
+
_nextTransportId() {
|
|
4789
|
+
return ++this._transportId;
|
|
4790
|
+
}
|
|
4791
|
+
_resolvePromises() {
|
|
4792
|
+
for (const id in this._promises) {
|
|
4793
|
+
if (!this._promises.hasOwnProperty(id)) {
|
|
4794
|
+
continue;
|
|
4795
|
+
}
|
|
4796
|
+
if (this._promises[id].timeout) {
|
|
4797
|
+
clearTimeout(this._promises[id].timeout);
|
|
4798
|
+
}
|
|
4799
|
+
this._promises[id].resolve();
|
|
4800
|
+
delete this._promises[id];
|
|
4801
|
+
}
|
|
4802
|
+
}
|
|
4803
|
+
_rejectPromises(err) {
|
|
4804
|
+
for (const id in this._promises) {
|
|
4805
|
+
if (!this._promises.hasOwnProperty(id)) {
|
|
4806
|
+
continue;
|
|
4807
|
+
}
|
|
4808
|
+
if (this._promises[id].timeout) {
|
|
4809
|
+
clearTimeout(this._promises[id].timeout);
|
|
4810
|
+
}
|
|
4811
|
+
this._promises[id].reject(err);
|
|
4812
|
+
delete this._promises[id];
|
|
4813
|
+
}
|
|
4814
|
+
}
|
|
4815
|
+
}
|
|
4816
|
+
Centrifuge.SubscriptionState = SubscriptionState;
|
|
4817
|
+
Centrifuge.State = State;
|
|
4818
|
+
Centrifuge.UnauthorizedError = UnauthorizedError;
|
|
4819
|
+
class CentrifugoWebsocket {
|
|
4820
|
+
constructor(connectionUrl) {
|
|
4821
|
+
this.connectionUrl = connectionUrl;
|
|
4822
|
+
this.centrifuge = null;
|
|
4823
|
+
this.subscriptions = {};
|
|
4824
|
+
this.isConnected = false;
|
|
4825
|
+
const config = getConfig();
|
|
4826
|
+
this.accessToken = config.accessToken;
|
|
4827
|
+
this.userId = config.userId;
|
|
4828
|
+
}
|
|
4829
|
+
connect() {
|
|
4830
|
+
if (!this.accessToken) {
|
|
4831
|
+
throw new Error("Unable to connect to websocket, missing info");
|
|
4832
|
+
}
|
|
4833
|
+
if (!this.connectionUrl) {
|
|
4834
|
+
throw new Error("Unable to connect to websocket, missing url");
|
|
4835
|
+
}
|
|
4836
|
+
if (!this.centrifuge) {
|
|
4837
|
+
this.centrifuge = new Centrifuge(this.connectionUrl, {
|
|
4838
|
+
token: this.accessToken
|
|
4839
|
+
});
|
|
4840
|
+
this.centrifuge.on("connecting", () => {
|
|
4841
|
+
console.log("Connecting to centrifuge", this.connectionUrl);
|
|
4842
|
+
});
|
|
4843
|
+
this.centrifuge.on("error", (err) => {
|
|
4844
|
+
console.error("Error connecting to centrifuge", err);
|
|
4845
|
+
});
|
|
4846
|
+
this.centrifuge.on("connected", (data) => {
|
|
4847
|
+
console.log("Connected to centrifuge", data);
|
|
4848
|
+
console.log("subscriptions", this.subscriptions);
|
|
4849
|
+
this.isConnected = true;
|
|
4850
|
+
});
|
|
4851
|
+
this.centrifuge.on("disconnected", () => {
|
|
4852
|
+
this.isConnected = false;
|
|
4853
|
+
});
|
|
4854
|
+
this.centrifuge.connect();
|
|
4855
|
+
}
|
|
4856
|
+
}
|
|
4857
|
+
_bind(channelName, callback) {
|
|
4858
|
+
if (!this.isConnected) {
|
|
4859
|
+
throw new Error("Centrifuge is not initialized. Call connect() first.");
|
|
4860
|
+
}
|
|
4861
|
+
if (!this.subscriptions[channelName]) {
|
|
4862
|
+
this.subscriptions[channelName] = this.centrifuge.newSubscription(channelName);
|
|
4863
|
+
}
|
|
4864
|
+
const subscription = this.subscriptions[channelName];
|
|
4865
|
+
subscription.on("publication", (data) => {
|
|
4866
|
+
callback(_.get(data, "data", data));
|
|
4867
|
+
});
|
|
4868
|
+
subscription.on("subscribing", (data) => {
|
|
4869
|
+
console.log("subscribing", data);
|
|
4870
|
+
});
|
|
4871
|
+
subscription.on("subscribed", (data) => {
|
|
4872
|
+
console.log("subscribed to", channelName, subscription);
|
|
4873
|
+
console.log("subscribed", data);
|
|
4874
|
+
});
|
|
4875
|
+
subscription.subscribe();
|
|
4876
|
+
}
|
|
4877
|
+
bind(channel, event, callback) {
|
|
4878
|
+
this._bind(`private-${channel}-${this.userId}#${event}`, callback);
|
|
4879
|
+
}
|
|
4880
|
+
bindGlobal(channel, callback) {
|
|
4881
|
+
if (!this.isConnected) {
|
|
4882
|
+
throw new Error("Centrifuge is not initialized. Call connect() first.");
|
|
4883
|
+
}
|
|
4884
|
+
this._bind(channel, callback);
|
|
4885
|
+
}
|
|
4886
|
+
unbind(channel, event) {
|
|
4887
|
+
const channelName = `${channel}#${event}`;
|
|
4888
|
+
if (this.subscriptions[channelName]) {
|
|
4889
|
+
this.subscriptions[channelName].removeAllListeners();
|
|
4890
|
+
this.subscriptions[channelName].unsubscribe();
|
|
4891
|
+
delete this.subscriptions[channelName];
|
|
4892
|
+
}
|
|
4893
|
+
}
|
|
4894
|
+
unbindAll(channel) {
|
|
4895
|
+
Object.keys(this.subscriptions).forEach((key) => {
|
|
4896
|
+
if (key.startsWith(channel)) {
|
|
4897
|
+
this.unbind(key, "");
|
|
4898
|
+
delete this.subscriptions[key];
|
|
4899
|
+
}
|
|
4900
|
+
});
|
|
4901
|
+
}
|
|
4902
|
+
disconnect() {
|
|
4903
|
+
if (this.isConnected) {
|
|
4904
|
+
this.centrifuge.disconnect();
|
|
4905
|
+
this.centrifuge = null;
|
|
4906
|
+
this.subscriptions = {};
|
|
4907
|
+
}
|
|
4908
|
+
this.isConnected = false;
|
|
4909
|
+
}
|
|
4910
|
+
}
|
|
4911
|
+
class WebsocketContextFactory {
|
|
4912
|
+
static create(mechanism, args) {
|
|
4913
|
+
let websocketProvider;
|
|
4914
|
+
if (mechanism === "centrifugo") {
|
|
4915
|
+
websocketProvider = new CentrifugoWebsocket(args.connectionUrl);
|
|
4916
|
+
}
|
|
4917
|
+
websocketProvider == null ? void 0 : websocketProvider.connect();
|
|
4918
|
+
return websocketProvider;
|
|
4919
|
+
}
|
|
4920
|
+
}
|
|
4921
|
+
const websocketProviderFactory = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
4922
|
+
__proto__: null,
|
|
4923
|
+
WebsocketContextFactory
|
|
4924
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
1360
4925
|
const index$5 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
1361
4926
|
__proto__: null,
|
|
1362
4927
|
formatDate,
|
|
1363
4928
|
getMetricDefinition,
|
|
1364
|
-
inputTableUtils: inputTableDefinition
|
|
4929
|
+
inputTableUtils: inputTableDefinition,
|
|
4930
|
+
websocketUtils: websocketProviderFactory
|
|
1365
4931
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
1366
4932
|
async function downloadFile(loc, contentType) {
|
|
1367
4933
|
if (!loc) return null;
|