@interopio/browser 3.2.0 → 3.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6536,7 +6536,7 @@
6536
6536
  }
6537
6537
  }
6538
6538
 
6539
- var version$2 = "3.2.0";
6539
+ var version$2 = "3.2.1";
6540
6540
 
6541
6541
  const createFactoryFunction = (coreFactoryFunction) => {
6542
6542
  return (userConfig) => __awaiter$1(void 0, void 0, void 0, function* () {
@@ -7881,6 +7881,7 @@
7881
7881
  return __generator(this, function (_b) {
7882
7882
  switch (_b.label) {
7883
7883
  case 0:
7884
+ this.logger.info("opening ws to ".concat(this.settings.ws, ", retryInterval: ").concat(retryInterval, ", retriesLeft: ").concat(retriesLeft, "..."));
7884
7885
  this.startupTimer.mark("opening-socket");
7885
7886
  if (retryInterval === undefined) {
7886
7887
  retryInterval = this.settings.reconnectInterval;
@@ -7918,9 +7919,10 @@
7918
7919
  };
7919
7920
  WS.prototype.initiateSocket = function () {
7920
7921
  var _this = this;
7922
+ var _a;
7921
7923
  var pw = new PromiseWrapper();
7922
7924
  this.logger.debug("initiating ws to ".concat(this.settings.ws, "..."));
7923
- this.ws = new WebSocketConstructor(this.settings.ws || "");
7925
+ this.ws = new WebSocketConstructor((_a = this.settings.ws) !== null && _a !== void 0 ? _a : "");
7924
7926
  this.ws.onerror = function (err) {
7925
7927
  var reason = "";
7926
7928
  try {
@@ -7939,11 +7941,12 @@
7939
7941
  };
7940
7942
  reason = JSON.stringify(err, replacer);
7941
7943
  }
7944
+ _this.logger.info("ws error - reason: ".concat(reason));
7942
7945
  pw.reject("error");
7943
7946
  _this.notifyStatusChanged(false, reason);
7944
7947
  };
7945
7948
  this.ws.onclose = function (err) {
7946
- _this.logger.info("ws closed ".concat(err));
7949
+ _this.logger.info("ws closed - code: ".concat(err === null || err === void 0 ? void 0 : err.code, " reason: ").concat(err === null || err === void 0 ? void 0 : err.reason));
7947
7950
  pw.reject("closed");
7948
7951
  _this.notifyStatusChanged(false);
7949
7952
  };
@@ -7978,6 +7981,80 @@
7978
7981
  return WS;
7979
7982
  }());
7980
7983
 
7984
+ var MessageReplayerImpl = (function () {
7985
+ function MessageReplayerImpl(specs) {
7986
+ this.specsNames = [];
7987
+ this.messages = {};
7988
+ this.subs = {};
7989
+ this.subsRefCount = {};
7990
+ this.specs = {};
7991
+ for (var _i = 0, specs_1 = specs; _i < specs_1.length; _i++) {
7992
+ var spec = specs_1[_i];
7993
+ this.specs[spec.name] = spec;
7994
+ this.specsNames.push(spec.name);
7995
+ }
7996
+ }
7997
+ MessageReplayerImpl.prototype.init = function (connection) {
7998
+ var _this = this;
7999
+ this.connection = connection;
8000
+ for (var _i = 0, _a = this.specsNames; _i < _a.length; _i++) {
8001
+ var name_1 = _a[_i];
8002
+ var _loop_1 = function (type) {
8003
+ var refCount = this_1.subsRefCount[type];
8004
+ if (!refCount) {
8005
+ refCount = 0;
8006
+ }
8007
+ refCount += 1;
8008
+ this_1.subsRefCount[type] = refCount;
8009
+ if (refCount > 1) {
8010
+ return "continue";
8011
+ }
8012
+ var sub = connection.on(type, function (msg) { return _this.processMessage(type, msg); });
8013
+ this_1.subs[type] = sub;
8014
+ };
8015
+ var this_1 = this;
8016
+ for (var _b = 0, _c = this.specs[name_1].types; _b < _c.length; _b++) {
8017
+ var type = _c[_b];
8018
+ _loop_1(type);
8019
+ }
8020
+ }
8021
+ };
8022
+ MessageReplayerImpl.prototype.processMessage = function (type, msg) {
8023
+ if (this.isDone || !msg) {
8024
+ return;
8025
+ }
8026
+ for (var _i = 0, _a = this.specsNames; _i < _a.length; _i++) {
8027
+ var name_2 = _a[_i];
8028
+ if (this.specs[name_2].types.indexOf(type) !== -1) {
8029
+ var messages = this.messages[name_2] || [];
8030
+ this.messages[name_2] = messages;
8031
+ messages.push(msg);
8032
+ }
8033
+ }
8034
+ };
8035
+ MessageReplayerImpl.prototype.drain = function (name, callback) {
8036
+ var _a;
8037
+ if (callback) {
8038
+ (this.messages[name] || []).forEach(callback);
8039
+ }
8040
+ delete this.messages[name];
8041
+ for (var _i = 0, _b = this.specs[name].types; _i < _b.length; _i++) {
8042
+ var type = _b[_i];
8043
+ this.subsRefCount[type] -= 1;
8044
+ if (this.subsRefCount[type] <= 0) {
8045
+ (_a = this.connection) === null || _a === void 0 ? void 0 : _a.off(this.subs[type]);
8046
+ delete this.subs[type];
8047
+ delete this.subsRefCount[type];
8048
+ }
8049
+ }
8050
+ delete this.specs[name];
8051
+ if (!this.specs.length) {
8052
+ this.isDone = true;
8053
+ }
8054
+ };
8055
+ return MessageReplayerImpl;
8056
+ }());
8057
+
7981
8058
  var shortidExports = {};
7982
8059
  var shortid$1 = {
7983
8060
  get exports(){ return shortidExports; },
@@ -8314,633 +8391,17 @@
8314
8391
  // Export all other functions as properties of the generate function
8315
8392
  module.exports = generate;
8316
8393
  module.exports.generate = generate;
8317
- module.exports.seed = seed;
8318
- module.exports.worker = worker;
8319
- module.exports.characters = characters;
8320
- module.exports.isValid = isValid$1;
8321
- } (lib));
8322
-
8323
- (function (module) {
8324
- module.exports = libExports;
8325
- } (shortid$1));
8326
-
8327
- var shortid = /*@__PURE__*/getDefaultExportFromCjs(shortidExports);
8328
-
8329
- function domainSession (domain, connection, logger, successMessages, errorMessages) {
8330
- if (domain == null) {
8331
- domain = "global";
8332
- }
8333
- successMessages = successMessages || ["success"];
8334
- errorMessages = errorMessages || ["error"];
8335
- var isJoined = false;
8336
- var tryReconnecting = false;
8337
- var _latestOptions;
8338
- var _connectionOn = false;
8339
- var callbacks = lib$1();
8340
- connection.disconnected(handleConnectionDisconnected);
8341
- connection.loggedIn(handleConnectionLoggedIn);
8342
- connection.on("success", function (msg) { return handleSuccessMessage(msg); });
8343
- connection.on("error", function (msg) { return handleErrorMessage(msg); });
8344
- connection.on("result", function (msg) { return handleSuccessMessage(msg); });
8345
- if (successMessages) {
8346
- successMessages.forEach(function (sm) {
8347
- connection.on(sm, function (msg) { return handleSuccessMessage(msg); });
8348
- });
8349
- }
8350
- if (errorMessages) {
8351
- errorMessages.forEach(function (sm) {
8352
- connection.on(sm, function (msg) { return handleErrorMessage(msg); });
8353
- });
8354
- }
8355
- var requestsMap = {};
8356
- function join(options) {
8357
- _latestOptions = options;
8358
- return new Promise(function (resolve, reject) {
8359
- if (isJoined) {
8360
- resolve({});
8361
- return;
8362
- }
8363
- var joinPromise;
8364
- if (domain === "global") {
8365
- joinPromise = _connectionOn ? Promise.resolve({}) : Promise.reject("not connected to gateway");
8366
- }
8367
- else {
8368
- logger.debug("joining domain ".concat(domain));
8369
- var joinMsg = {
8370
- type: "join",
8371
- destination: domain,
8372
- domain: "global",
8373
- options: options,
8374
- };
8375
- joinPromise = send(joinMsg);
8376
- }
8377
- joinPromise
8378
- .then(function () {
8379
- handleJoined();
8380
- resolve({});
8381
- })
8382
- .catch(function (err) {
8383
- logger.debug("error joining " + domain + " domain: " + JSON.stringify(err));
8384
- reject(err);
8385
- });
8386
- });
8387
- }
8388
- function leave() {
8389
- if (domain === "global") {
8390
- return Promise.resolve();
8391
- }
8392
- logger.debug("stopping session " + domain + "...");
8393
- var leaveMsg = {
8394
- type: "leave",
8395
- destination: domain,
8396
- domain: "global",
8397
- };
8398
- tryReconnecting = false;
8399
- return send(leaveMsg)
8400
- .then(function () {
8401
- isJoined = false;
8402
- callbacks.execute("onLeft");
8403
- })
8404
- .catch(function () {
8405
- isJoined = false;
8406
- callbacks.execute("onLeft");
8407
- });
8408
- }
8409
- function handleJoined() {
8410
- logger.debug("did join " + domain);
8411
- isJoined = true;
8412
- var wasReconnect = tryReconnecting;
8413
- tryReconnecting = false;
8414
- callbacks.execute("onJoined", wasReconnect);
8415
- }
8416
- function handleConnectionDisconnected() {
8417
- _connectionOn = false;
8418
- logger.debug("connection is down");
8419
- isJoined = false;
8420
- tryReconnecting = true;
8421
- callbacks.execute("onLeft", { disconnected: true });
8422
- }
8423
- function handleConnectionLoggedIn() {
8424
- _connectionOn = true;
8425
- if (tryReconnecting) {
8426
- logger.debug("connection is now up - trying to reconnect...");
8427
- join(_latestOptions);
8428
- }
8429
- }
8430
- function onJoined(callback) {
8431
- if (isJoined) {
8432
- callback(false);
8433
- }
8434
- return callbacks.add("onJoined", callback);
8435
- }
8436
- function onLeft(callback) {
8437
- if (!isJoined) {
8438
- callback();
8439
- }
8440
- return callbacks.add("onLeft", callback);
8441
- }
8442
- function handleErrorMessage(msg) {
8443
- if (domain !== msg.domain) {
8444
- return;
8445
- }
8446
- var requestId = msg.request_id;
8447
- if (!requestId) {
8448
- return;
8449
- }
8450
- var entry = requestsMap[requestId];
8451
- if (!entry) {
8452
- return;
8453
- }
8454
- entry.error(msg);
8455
- }
8456
- function handleSuccessMessage(msg) {
8457
- if (msg.domain !== domain) {
8458
- return;
8459
- }
8460
- var requestId = msg.request_id;
8461
- if (!requestId) {
8462
- return;
8463
- }
8464
- var entry = requestsMap[requestId];
8465
- if (!entry) {
8466
- return;
8467
- }
8468
- entry.success(msg);
8469
- }
8470
- function getNextRequestId() {
8471
- return shortid();
8472
- }
8473
- function send(msg, tag, options) {
8474
- options = options || {};
8475
- msg.request_id = msg.request_id || getNextRequestId();
8476
- msg.domain = msg.domain || domain;
8477
- if (!options.skipPeerId) {
8478
- msg.peer_id = connection.peerId;
8479
- }
8480
- var requestId = msg.request_id;
8481
- return new Promise(function (resolve, reject) {
8482
- requestsMap[requestId] = {
8483
- success: function (successMsg) {
8484
- delete requestsMap[requestId];
8485
- successMsg._tag = tag;
8486
- resolve(successMsg);
8487
- },
8488
- error: function (errorMsg) {
8489
- logger.warn("GW error - ".concat(JSON.stringify(errorMsg), " for request ").concat(JSON.stringify(msg)));
8490
- delete requestsMap[requestId];
8491
- errorMsg._tag = tag;
8492
- reject(errorMsg);
8493
- },
8494
- };
8495
- connection
8496
- .send(msg, options)
8497
- .catch(function (err) {
8498
- requestsMap[requestId].error({ err: err });
8499
- });
8500
- });
8501
- }
8502
- function sendFireAndForget(msg) {
8503
- msg.request_id = msg.request_id ? msg.request_id : getNextRequestId();
8504
- msg.domain = msg.domain || domain;
8505
- msg.peer_id = connection.peerId;
8506
- return connection.send(msg);
8507
- }
8508
- return {
8509
- join: join,
8510
- leave: leave,
8511
- onJoined: onJoined,
8512
- onLeft: onLeft,
8513
- send: send,
8514
- sendFireAndForget: sendFireAndForget,
8515
- on: function (type, callback) {
8516
- connection.on(type, function (msg) {
8517
- if (msg.domain !== domain) {
8518
- return;
8519
- }
8520
- try {
8521
- callback(msg);
8522
- }
8523
- catch (e) {
8524
- logger.error("Callback failed: ".concat(e, " \n ").concat(e.stack, " \n msg was: ").concat(JSON.stringify(msg)), e);
8525
- }
8526
- });
8527
- },
8528
- loggedIn: function (callback) { return connection.loggedIn(callback); },
8529
- connected: function (callback) { return connection.connected(callback); },
8530
- disconnected: function (callback) { return connection.disconnected(callback); },
8531
- get peerId() {
8532
- return connection.peerId;
8533
- },
8534
- get domain() {
8535
- return domain;
8536
- },
8537
- };
8538
- }
8539
-
8540
- var GW3ProtocolImpl = (function () {
8541
- function GW3ProtocolImpl(connection, settings, logger) {
8542
- var _this = this;
8543
- this.connection = connection;
8544
- this.settings = settings;
8545
- this.logger = logger;
8546
- this.protocolVersion = 3;
8547
- this.datePrefix = "#T42_DATE#";
8548
- this.datePrefixLen = this.datePrefix.length;
8549
- this.dateMinLen = this.datePrefixLen + 1;
8550
- this.datePrefixFirstChar = this.datePrefix[0];
8551
- this.registry = lib$1();
8552
- this._isLoggedIn = false;
8553
- this.shouldTryLogin = true;
8554
- this.initialLogin = true;
8555
- this.initialLoginAttempts = 3;
8556
- this.sessions = [];
8557
- connection.disconnected(function () {
8558
- _this.handleDisconnected();
8559
- });
8560
- this.ping();
8561
- }
8562
- Object.defineProperty(GW3ProtocolImpl.prototype, "isLoggedIn", {
8563
- get: function () {
8564
- return this._isLoggedIn;
8565
- },
8566
- enumerable: false,
8567
- configurable: true
8568
- });
8569
- GW3ProtocolImpl.prototype.processStringMessage = function (message) {
8570
- var _this = this;
8571
- var msg = JSON.parse(message, function (key, value) {
8572
- if (typeof value !== "string") {
8573
- return value;
8574
- }
8575
- if (value.length < _this.dateMinLen) {
8576
- return value;
8577
- }
8578
- if (value[0] !== _this.datePrefixFirstChar) {
8579
- return value;
8580
- }
8581
- if (value.substring(0, _this.datePrefixLen) !== _this.datePrefix) {
8582
- return value;
8583
- }
8584
- try {
8585
- var milliseconds = parseInt(value.substring(_this.datePrefixLen, value.length), 10);
8586
- if (isNaN(milliseconds)) {
8587
- return value;
8588
- }
8589
- return new Date(milliseconds);
8590
- }
8591
- catch (ex) {
8592
- return value;
8593
- }
8594
- });
8595
- return {
8596
- msg: msg,
8597
- msgType: msg.type,
8598
- };
8599
- };
8600
- GW3ProtocolImpl.prototype.createStringMessage = function (message) {
8601
- var oldToJson = Date.prototype.toJSON;
8602
- try {
8603
- var datePrefix_1 = this.datePrefix;
8604
- Date.prototype.toJSON = function () {
8605
- return datePrefix_1 + this.getTime();
8606
- };
8607
- var result = JSON.stringify(message);
8608
- return result;
8609
- }
8610
- finally {
8611
- Date.prototype.toJSON = oldToJson;
8612
- }
8613
- };
8614
- GW3ProtocolImpl.prototype.processObjectMessage = function (message) {
8615
- if (!message.type) {
8616
- throw new Error("Object should have type property");
8617
- }
8618
- return {
8619
- msg: message,
8620
- msgType: message.type,
8621
- };
8622
- };
8623
- GW3ProtocolImpl.prototype.createObjectMessage = function (message) {
8624
- return message;
8625
- };
8626
- GW3ProtocolImpl.prototype.login = function (config, reconnect) {
8627
- return __awaiter(this, void 0, void 0, function () {
8628
- var authentication, token, e_1, _a, helloMsg, sendOptions, welcomeMsg, msg, token, _b, err_1;
8629
- return __generator(this, function (_c) {
8630
- switch (_c.label) {
8631
- case 0:
8632
- this.logger.debug("logging in...");
8633
- this.loginConfig = config;
8634
- if (!this.loginConfig) {
8635
- this.loginConfig = { username: "", password: "" };
8636
- }
8637
- this.shouldTryLogin = true;
8638
- authentication = {};
8639
- this.connection.gatewayToken = config.gatewayToken;
8640
- if (!config.gatewayToken) return [3, 5];
8641
- if (!reconnect) return [3, 4];
8642
- _c.label = 1;
8643
- case 1:
8644
- _c.trys.push([1, 3, , 4]);
8645
- return [4, this.getNewGWToken()];
8646
- case 2:
8647
- token = _c.sent();
8648
- config.gatewayToken = token;
8649
- return [3, 4];
8650
- case 3:
8651
- e_1 = _c.sent();
8652
- this.logger.warn("failed to get GW token when reconnecting ".concat((e_1 === null || e_1 === void 0 ? void 0 : e_1.message) || e_1));
8653
- return [3, 4];
8654
- case 4:
8655
- authentication.method = "gateway-token";
8656
- authentication.token = config.gatewayToken;
8657
- this.connection.gatewayToken = config.gatewayToken;
8658
- return [3, 10];
8659
- case 5:
8660
- if (!(config.flowName === "sspi")) return [3, 9];
8661
- authentication.provider = "win";
8662
- authentication.method = "access-token";
8663
- if (!(config.flowCallback && config.sessionId)) return [3, 7];
8664
- _a = authentication;
8665
- return [4, config.flowCallback(config.sessionId, null)];
8666
- case 6:
8667
- _a.token =
8668
- (_c.sent())
8669
- .data
8670
- .toString("base64");
8671
- return [3, 8];
8672
- case 7: throw new Error("Invalid SSPI config");
8673
- case 8: return [3, 10];
8674
- case 9:
8675
- if (config.token) {
8676
- authentication.method = "access-token";
8677
- authentication.token = config.token;
8678
- }
8679
- else if (config.username) {
8680
- authentication.method = "secret";
8681
- authentication.login = config.username;
8682
- authentication.secret = config.password;
8683
- }
8684
- else if (config.provider) {
8685
- authentication.provider = config.provider;
8686
- authentication.providerContext = config.providerContext;
8687
- }
8688
- else {
8689
- throw new Error("invalid auth message" + JSON.stringify(config));
8690
- }
8691
- _c.label = 10;
8692
- case 10:
8693
- helloMsg = {
8694
- type: "hello",
8695
- identity: this.settings.identity,
8696
- authentication: authentication
8697
- };
8698
- if (config.sessionId) {
8699
- helloMsg.request_id = config.sessionId;
8700
- }
8701
- this.globalDomain = domainSession("global", this.connection, this.logger.subLogger("global-domain"), [
8702
- "welcome",
8703
- "token",
8704
- "authentication-request"
8705
- ]);
8706
- sendOptions = { skipPeerId: true };
8707
- if (this.initialLogin) {
8708
- sendOptions.retryInterval = this.settings.reconnectInterval;
8709
- sendOptions.maxRetries = this.settings.reconnectAttempts;
8710
- }
8711
- _c.label = 11;
8712
- case 11:
8713
- _c.trys.push([11, 19, 20, 21]);
8714
- welcomeMsg = void 0;
8715
- _c.label = 12;
8716
- case 12:
8717
- return [4, this.globalDomain.send(helloMsg, undefined, sendOptions)];
8718
- case 13:
8719
- msg = _c.sent();
8720
- if (!(msg.type === "authentication-request")) return [3, 16];
8721
- token = Buffer.from(msg.authentication.token, "base64");
8722
- if (!(config.flowCallback && config.sessionId)) return [3, 15];
8723
- _b = helloMsg.authentication;
8724
- return [4, config.flowCallback(config.sessionId, token)];
8725
- case 14:
8726
- _b.token =
8727
- (_c.sent())
8728
- .data
8729
- .toString("base64");
8730
- _c.label = 15;
8731
- case 15:
8732
- helloMsg.request_id = config.sessionId;
8733
- return [3, 12];
8734
- case 16:
8735
- if (msg.type === "welcome") {
8736
- welcomeMsg = msg;
8737
- return [3, 18];
8738
- }
8739
- else if (msg.type === "error") {
8740
- throw new Error("Authentication failed: " + msg.reason);
8741
- }
8742
- else {
8743
- throw new Error("Unexpected message type during authentication: " + msg.type);
8744
- }
8745
- case 17: return [3, 12];
8746
- case 18:
8747
- this.initialLogin = false;
8748
- this.logger.debug("login successful with peerId " + welcomeMsg.peer_id);
8749
- this.connection.peerId = welcomeMsg.peer_id;
8750
- this.connection.resolvedIdentity = welcomeMsg.resolved_identity;
8751
- this.connection.availableDomains = welcomeMsg.available_domains;
8752
- if (welcomeMsg.options) {
8753
- this.connection.token = welcomeMsg.options.access_token;
8754
- this.connection.info = welcomeMsg.options.info;
8755
- }
8756
- this.setLoggedIn(true);
8757
- return [2, welcomeMsg.resolved_identity];
8758
- case 19:
8759
- err_1 = _c.sent();
8760
- this.logger.error("error sending hello message - " + (err_1.message || err_1.msg || err_1.reason || err_1), err_1);
8761
- throw err_1;
8762
- case 20:
8763
- if (config && config.flowCallback && config.sessionId) {
8764
- config.flowCallback(config.sessionId, null);
8765
- }
8766
- return [7];
8767
- case 21: return [2];
8768
- }
8769
- });
8770
- });
8771
- };
8772
- GW3ProtocolImpl.prototype.logout = function () {
8773
- return __awaiter(this, void 0, void 0, function () {
8774
- var promises;
8775
- return __generator(this, function (_a) {
8776
- switch (_a.label) {
8777
- case 0:
8778
- this.logger.debug("logging out...");
8779
- this.shouldTryLogin = false;
8780
- if (this.pingTimer) {
8781
- clearTimeout(this.pingTimer);
8782
- }
8783
- promises = this.sessions.map(function (session) {
8784
- session.leave();
8785
- });
8786
- return [4, Promise.all(promises)];
8787
- case 1:
8788
- _a.sent();
8789
- return [2];
8790
- }
8791
- });
8792
- });
8793
- };
8794
- GW3ProtocolImpl.prototype.loggedIn = function (callback) {
8795
- if (this._isLoggedIn) {
8796
- callback();
8797
- }
8798
- return this.registry.add("onLoggedIn", callback);
8799
- };
8800
- GW3ProtocolImpl.prototype.domain = function (domainName, domainLogger, successMessages, errorMessages) {
8801
- var session = this.sessions.filter(function (s) { return s.domain === domainName; })[0];
8802
- if (!session) {
8803
- session = domainSession(domainName, this.connection, domainLogger, successMessages, errorMessages);
8804
- this.sessions.push(session);
8805
- }
8806
- return session;
8807
- };
8808
- GW3ProtocolImpl.prototype.handleDisconnected = function () {
8809
- var _this = this;
8810
- this.setLoggedIn(false);
8811
- var tryToLogin = this.shouldTryLogin;
8812
- if (tryToLogin && this.initialLogin) {
8813
- if (this.initialLoginAttempts <= 0) {
8814
- return;
8815
- }
8816
- this.initialLoginAttempts--;
8817
- }
8818
- this.logger.debug("disconnected - will try new login?" + this.shouldTryLogin);
8819
- if (this.shouldTryLogin) {
8820
- if (!this.loginConfig) {
8821
- throw new Error("no login info");
8822
- }
8823
- this.connection.login(this.loginConfig, true)
8824
- .catch(function () {
8825
- setTimeout(_this.handleDisconnected.bind(_this), _this.settings.reconnectInterval || 1000);
8826
- });
8827
- }
8828
- };
8829
- GW3ProtocolImpl.prototype.setLoggedIn = function (value) {
8830
- this._isLoggedIn = value;
8831
- if (this._isLoggedIn) {
8832
- this.registry.execute("onLoggedIn");
8833
- }
8834
- };
8835
- GW3ProtocolImpl.prototype.ping = function () {
8836
- var _this = this;
8837
- if (!this.shouldTryLogin) {
8838
- return;
8839
- }
8840
- if (this._isLoggedIn) {
8841
- this.connection.send({ type: "ping" });
8842
- }
8843
- this.pingTimer = setTimeout(function () {
8844
- _this.ping();
8845
- }, 30 * 1000);
8846
- };
8847
- GW3ProtocolImpl.prototype.authToken = function () {
8848
- var createTokenReq = {
8849
- type: "create-token"
8850
- };
8851
- if (!this.globalDomain) {
8852
- return Promise.reject(new Error("no global domain session"));
8853
- }
8854
- return this.globalDomain.send(createTokenReq)
8855
- .then(function (res) {
8856
- return res.token;
8857
- });
8858
- };
8859
- GW3ProtocolImpl.prototype.getNewGWToken = function () {
8860
- if (typeof window !== "undefined") {
8861
- var glue42gd = window.glue42gd;
8862
- if (glue42gd) {
8863
- return glue42gd.getGWToken();
8864
- }
8865
- }
8866
- return Promise.reject(new Error("not running in GD"));
8867
- };
8868
- return GW3ProtocolImpl;
8869
- }());
8870
-
8871
- var MessageReplayerImpl = (function () {
8872
- function MessageReplayerImpl(specs) {
8873
- this.specsNames = [];
8874
- this.messages = {};
8875
- this.subs = {};
8876
- this.subsRefCount = {};
8877
- this.specs = {};
8878
- for (var _i = 0, specs_1 = specs; _i < specs_1.length; _i++) {
8879
- var spec = specs_1[_i];
8880
- this.specs[spec.name] = spec;
8881
- this.specsNames.push(spec.name);
8882
- }
8883
- }
8884
- MessageReplayerImpl.prototype.init = function (connection) {
8885
- var _this = this;
8886
- this.connection = connection;
8887
- for (var _i = 0, _a = this.specsNames; _i < _a.length; _i++) {
8888
- var name_1 = _a[_i];
8889
- var _loop_1 = function (type) {
8890
- var refCount = this_1.subsRefCount[type];
8891
- if (!refCount) {
8892
- refCount = 0;
8893
- }
8894
- refCount += 1;
8895
- this_1.subsRefCount[type] = refCount;
8896
- if (refCount > 1) {
8897
- return "continue";
8898
- }
8899
- var sub = connection.on(type, function (msg) { return _this.processMessage(type, msg); });
8900
- this_1.subs[type] = sub;
8901
- };
8902
- var this_1 = this;
8903
- for (var _b = 0, _c = this.specs[name_1].types; _b < _c.length; _b++) {
8904
- var type = _c[_b];
8905
- _loop_1(type);
8906
- }
8907
- }
8908
- };
8909
- MessageReplayerImpl.prototype.processMessage = function (type, msg) {
8910
- if (this.isDone || !msg) {
8911
- return;
8912
- }
8913
- for (var _i = 0, _a = this.specsNames; _i < _a.length; _i++) {
8914
- var name_2 = _a[_i];
8915
- if (this.specs[name_2].types.indexOf(type) !== -1) {
8916
- var messages = this.messages[name_2] || [];
8917
- this.messages[name_2] = messages;
8918
- messages.push(msg);
8919
- }
8920
- }
8921
- };
8922
- MessageReplayerImpl.prototype.drain = function (name, callback) {
8923
- var _a;
8924
- if (callback) {
8925
- (this.messages[name] || []).forEach(callback);
8926
- }
8927
- delete this.messages[name];
8928
- for (var _i = 0, _b = this.specs[name].types; _i < _b.length; _i++) {
8929
- var type = _b[_i];
8930
- this.subsRefCount[type] -= 1;
8931
- if (this.subsRefCount[type] <= 0) {
8932
- (_a = this.connection) === null || _a === void 0 ? void 0 : _a.off(this.subs[type]);
8933
- delete this.subs[type];
8934
- delete this.subsRefCount[type];
8935
- }
8936
- }
8937
- delete this.specs[name];
8938
- if (!this.specs.length) {
8939
- this.isDone = true;
8940
- }
8941
- };
8942
- return MessageReplayerImpl;
8943
- }());
8394
+ module.exports.seed = seed;
8395
+ module.exports.worker = worker;
8396
+ module.exports.characters = characters;
8397
+ module.exports.isValid = isValid$1;
8398
+ } (lib));
8399
+
8400
+ (function (module) {
8401
+ module.exports = libExports;
8402
+ } (shortid$1));
8403
+
8404
+ var shortid = /*@__PURE__*/getDefaultExportFromCjs(shortidExports);
8944
8405
 
8945
8406
  var PromisePlus = function (executor, timeoutMilliseconds, timeoutMessage) {
8946
8407
  return new Promise(function (resolve, reject) {
@@ -9679,10 +9140,247 @@
9679
9140
  return AsyncSequelizer;
9680
9141
  }());
9681
9142
 
9143
+ function domainSession (domain, connection, logger, successMessages, errorMessages) {
9144
+ if (domain == null) {
9145
+ domain = "global";
9146
+ }
9147
+ successMessages = successMessages !== null && successMessages !== void 0 ? successMessages : ["success"];
9148
+ errorMessages = errorMessages !== null && errorMessages !== void 0 ? errorMessages : ["error"];
9149
+ var isJoined = domain === "global";
9150
+ var tryReconnecting = false;
9151
+ var _latestOptions;
9152
+ var _connectionOn = false;
9153
+ var callbacks = lib$1();
9154
+ connection.disconnected(handleConnectionDisconnected);
9155
+ connection.loggedIn(handleConnectionLoggedIn);
9156
+ connection.on("success", function (msg) { return handleSuccessMessage(msg); });
9157
+ connection.on("error", function (msg) { return handleErrorMessage(msg); });
9158
+ connection.on("result", function (msg) { return handleSuccessMessage(msg); });
9159
+ if (successMessages) {
9160
+ successMessages.forEach(function (sm) {
9161
+ connection.on(sm, function (msg) { return handleSuccessMessage(msg); });
9162
+ });
9163
+ }
9164
+ if (errorMessages) {
9165
+ errorMessages.forEach(function (sm) {
9166
+ connection.on(sm, function (msg) { return handleErrorMessage(msg); });
9167
+ });
9168
+ }
9169
+ var requestsMap = {};
9170
+ function join(options) {
9171
+ _latestOptions = options;
9172
+ return new Promise(function (resolve, reject) {
9173
+ if (isJoined) {
9174
+ resolve({});
9175
+ return;
9176
+ }
9177
+ var joinPromise;
9178
+ if (domain === "global") {
9179
+ joinPromise = _connectionOn ? Promise.resolve({}) : Promise.reject("not connected to gateway");
9180
+ }
9181
+ else {
9182
+ logger.debug("joining domain ".concat(domain));
9183
+ var joinMsg = {
9184
+ type: "join",
9185
+ destination: domain,
9186
+ domain: "global",
9187
+ options: options,
9188
+ };
9189
+ joinPromise = send(joinMsg);
9190
+ }
9191
+ joinPromise
9192
+ .then(function () {
9193
+ handleJoined();
9194
+ resolve({});
9195
+ })
9196
+ .catch(function (err) {
9197
+ logger.debug("error joining " + domain + " domain: " + JSON.stringify(err));
9198
+ reject(err);
9199
+ });
9200
+ });
9201
+ }
9202
+ function leave() {
9203
+ if (domain === "global") {
9204
+ return Promise.resolve();
9205
+ }
9206
+ logger.debug("stopping session " + domain + "...");
9207
+ var leaveMsg = {
9208
+ type: "leave",
9209
+ destination: domain,
9210
+ domain: "global",
9211
+ };
9212
+ tryReconnecting = false;
9213
+ return send(leaveMsg)
9214
+ .then(function () {
9215
+ isJoined = false;
9216
+ callbacks.execute("onLeft");
9217
+ })
9218
+ .catch(function () {
9219
+ isJoined = false;
9220
+ callbacks.execute("onLeft");
9221
+ });
9222
+ }
9223
+ function handleJoined() {
9224
+ logger.debug("did join " + domain);
9225
+ isJoined = true;
9226
+ var wasReconnect = tryReconnecting;
9227
+ tryReconnecting = false;
9228
+ callbacks.execute("onJoined", wasReconnect);
9229
+ }
9230
+ function handleConnectionDisconnected() {
9231
+ _connectionOn = false;
9232
+ logger.debug("connection is down");
9233
+ isJoined = false;
9234
+ tryReconnecting = true;
9235
+ callbacks.execute("onLeft", { disconnected: true });
9236
+ }
9237
+ function handleConnectionLoggedIn() {
9238
+ _connectionOn = true;
9239
+ if (tryReconnecting) {
9240
+ logger.debug("connection is now up - trying to reconnect...");
9241
+ join(_latestOptions);
9242
+ }
9243
+ }
9244
+ function onJoined(callback) {
9245
+ if (isJoined) {
9246
+ callback(false);
9247
+ }
9248
+ return callbacks.add("onJoined", callback);
9249
+ }
9250
+ function onLeft(callback) {
9251
+ if (!isJoined) {
9252
+ callback();
9253
+ }
9254
+ return callbacks.add("onLeft", callback);
9255
+ }
9256
+ function handleErrorMessage(msg) {
9257
+ if (domain !== msg.domain) {
9258
+ return;
9259
+ }
9260
+ var requestId = msg.request_id;
9261
+ if (!requestId) {
9262
+ return;
9263
+ }
9264
+ var entry = requestsMap[requestId];
9265
+ if (!entry) {
9266
+ return;
9267
+ }
9268
+ entry.error(msg);
9269
+ }
9270
+ function handleSuccessMessage(msg) {
9271
+ if (msg.domain !== domain) {
9272
+ return;
9273
+ }
9274
+ var requestId = msg.request_id;
9275
+ if (!requestId) {
9276
+ return;
9277
+ }
9278
+ var entry = requestsMap[requestId];
9279
+ if (!entry) {
9280
+ return;
9281
+ }
9282
+ entry.success(msg);
9283
+ }
9284
+ function getNextRequestId() {
9285
+ return shortid();
9286
+ }
9287
+ var queuedCalls = [];
9288
+ function send(msg, tag, options) {
9289
+ var _a, _b;
9290
+ var ignore = ["hello", "join"];
9291
+ if (msg.type && ignore.indexOf(msg.type) === -1) {
9292
+ if (!isJoined) {
9293
+ logger.warn("trying to send a message (".concat(msg.domain, " ").concat(msg.type, ") but not connected, will queue"));
9294
+ var pw = new PromiseWrapper();
9295
+ queuedCalls.push({ msg: msg, tag: tag, options: options, pw: pw });
9296
+ if (queuedCalls.length === 1) {
9297
+ var unsubscribe_1 = onJoined(function () {
9298
+ logger.info("joined - will now send queued messages (".concat(queuedCalls.length, " -> [").concat(queuedCalls.map(function (m) { return m.msg.type; }), "])"));
9299
+ queuedCalls.forEach(function (qm) {
9300
+ send(qm.msg, qm.tag, qm.options)
9301
+ .then(function (t) { return qm.pw.resolve(t); })
9302
+ .catch(function (e) { return qm.pw.reject(e); });
9303
+ });
9304
+ queuedCalls = [];
9305
+ unsubscribe_1();
9306
+ });
9307
+ }
9308
+ return pw.promise;
9309
+ }
9310
+ }
9311
+ options = options !== null && options !== void 0 ? options : {};
9312
+ msg.request_id = (_a = msg.request_id) !== null && _a !== void 0 ? _a : getNextRequestId();
9313
+ msg.domain = (_b = msg.domain) !== null && _b !== void 0 ? _b : domain;
9314
+ if (!options.skipPeerId) {
9315
+ msg.peer_id = connection.peerId;
9316
+ }
9317
+ var requestId = msg.request_id;
9318
+ return new Promise(function (resolve, reject) {
9319
+ requestsMap[requestId] = {
9320
+ success: function (successMsg) {
9321
+ delete requestsMap[requestId];
9322
+ successMsg._tag = tag;
9323
+ resolve(successMsg);
9324
+ },
9325
+ error: function (errorMsg) {
9326
+ logger.warn("GW error - ".concat(JSON.stringify(errorMsg), " for request ").concat(JSON.stringify(msg)));
9327
+ delete requestsMap[requestId];
9328
+ errorMsg._tag = tag;
9329
+ reject(errorMsg);
9330
+ },
9331
+ };
9332
+ connection
9333
+ .send(msg, options)
9334
+ .catch(function (err) {
9335
+ requestsMap[requestId].error({ err: err });
9336
+ });
9337
+ });
9338
+ }
9339
+ function sendFireAndForget(msg) {
9340
+ var _a;
9341
+ msg.request_id = msg.request_id ? msg.request_id : getNextRequestId();
9342
+ msg.domain = (_a = msg.domain) !== null && _a !== void 0 ? _a : domain;
9343
+ msg.peer_id = connection.peerId;
9344
+ return connection.send(msg);
9345
+ }
9346
+ return {
9347
+ join: join,
9348
+ leave: leave,
9349
+ onJoined: onJoined,
9350
+ onLeft: onLeft,
9351
+ send: send,
9352
+ sendFireAndForget: sendFireAndForget,
9353
+ on: function (type, callback) {
9354
+ connection.on(type, function (msg) {
9355
+ if (msg.domain !== domain) {
9356
+ return;
9357
+ }
9358
+ try {
9359
+ callback(msg);
9360
+ }
9361
+ catch (e) {
9362
+ logger.error("Callback failed: ".concat(e, " \n ").concat(e.stack, " \n msg was: ").concat(JSON.stringify(msg)), e);
9363
+ }
9364
+ });
9365
+ },
9366
+ loggedIn: function (callback) { return connection.loggedIn(callback); },
9367
+ connected: function (callback) { return connection.connected(callback); },
9368
+ disconnected: function (callback) { return connection.disconnected(callback); },
9369
+ get peerId() {
9370
+ return connection.peerId;
9371
+ },
9372
+ get domain() {
9373
+ return domain;
9374
+ },
9375
+ };
9376
+ }
9377
+
9682
9378
  var Connection = (function () {
9683
9379
  function Connection(settings, logger) {
9380
+ var _a, _b;
9684
9381
  this.settings = settings;
9685
9382
  this.logger = logger;
9383
+ this.protocolVersion = 3;
9686
9384
  this.messageHandlers = {};
9687
9385
  this.ids = 1;
9688
9386
  this.registry = lib$1();
@@ -9691,10 +9389,19 @@
9691
9389
  this._swapTransport = false;
9692
9390
  this._switchInProgress = false;
9693
9391
  this._transportSubscriptions = [];
9392
+ this.datePrefix = "#T42_DATE#";
9393
+ this.datePrefixLen = this.datePrefix.length;
9394
+ this.dateMinLen = this.datePrefixLen + 1;
9395
+ this.datePrefixFirstChar = this.datePrefix[0];
9694
9396
  this._sequelizer = new AsyncSequelizer();
9397
+ this._isLoggedIn = false;
9398
+ this.shouldTryLogin = true;
9399
+ this.sessions = [];
9400
+ this.initialLogin = true;
9401
+ this.initialLoginAttempts = 3;
9695
9402
  settings = settings || {};
9696
- settings.reconnectAttempts = settings.reconnectAttempts || 10;
9697
- settings.reconnectInterval = settings.reconnectInterval || 1000;
9403
+ settings.reconnectAttempts = (_a = settings.reconnectAttempts) !== null && _a !== void 0 ? _a : 10;
9404
+ settings.reconnectInterval = (_b = settings.reconnectInterval) !== null && _b !== void 0 ? _b : 1000;
9698
9405
  if (settings.inproc) {
9699
9406
  this.transport = new InProcTransport(settings.inproc, logger.subLogger("inMemory"));
9700
9407
  }
@@ -9712,21 +9419,13 @@
9712
9419
  }
9713
9420
  this.isTrace = logger.canPublish("trace");
9714
9421
  logger.debug("starting with ".concat(this.transport.name(), " transport"));
9715
- this.protocol = new GW3ProtocolImpl(this, settings, logger.subLogger("protocol"));
9716
9422
  var unsubConnectionChanged = this.transport.onConnectedChanged(this.handleConnectionChanged.bind(this));
9717
9423
  var unsubOnMessage = this.transport.onMessage(this.handleTransportMessage.bind(this));
9718
9424
  this._transportSubscriptions.push(unsubConnectionChanged);
9719
9425
  this._transportSubscriptions.push(unsubOnMessage);
9720
9426
  this._defaultTransport = this.transport;
9427
+ this.ping();
9721
9428
  }
9722
- Object.defineProperty(Connection.prototype, "protocolVersion", {
9723
- get: function () {
9724
- var _a;
9725
- return (_a = this.protocol) === null || _a === void 0 ? void 0 : _a.protocolVersion;
9726
- },
9727
- enumerable: false,
9728
- configurable: true
9729
- });
9730
9429
  Connection.prototype.switchTransport = function (settings) {
9731
9430
  return __awaiter(this, void 0, void 0, function () {
9732
9431
  var _this = this;
@@ -9785,14 +9484,14 @@
9785
9484
  Connection.prototype.send = function (message, options) {
9786
9485
  if (this.transport.sendObject &&
9787
9486
  this.transport.isObjectBasedTransport) {
9788
- var msg = this.protocol.createObjectMessage(message);
9487
+ var msg = this.createObjectMessage(message);
9789
9488
  if (this.isTrace) {
9790
9489
  this.logger.trace(">> ".concat(JSON.stringify(msg)));
9791
9490
  }
9792
9491
  return this.transport.sendObject(msg, options);
9793
9492
  }
9794
9493
  else {
9795
- var strMessage = this.protocol.createStringMessage(message);
9494
+ var strMessage = this.createStringMessage(message);
9796
9495
  if (this.isTrace) {
9797
9496
  this.logger.trace(">> ".concat(strMessage));
9798
9497
  }
@@ -9816,14 +9515,14 @@
9816
9515
  };
9817
9516
  Object.defineProperty(Connection.prototype, "isConnected", {
9818
9517
  get: function () {
9819
- return this.protocol.isLoggedIn;
9518
+ return this._isLoggedIn;
9820
9519
  },
9821
9520
  enumerable: false,
9822
9521
  configurable: true
9823
9522
  });
9824
9523
  Connection.prototype.connected = function (callback) {
9825
9524
  var _this = this;
9826
- return this.protocol.loggedIn(function () {
9525
+ return this.loggedIn(function () {
9827
9526
  var currentServer = _this.transport.name();
9828
9527
  callback(currentServer);
9829
9528
  });
@@ -9854,7 +9553,7 @@
9854
9553
  _a.sent();
9855
9554
  this.logger.trace("Transport: ".concat(this.transport.name(), " opened, logging in"));
9856
9555
  timer("connection").mark("transport-opened");
9857
- return [4, this.protocol.login(authRequest, reconnect)];
9556
+ return [4, this.loginCore(authRequest, reconnect)];
9858
9557
  case 3:
9859
9558
  identity = _a.sent();
9860
9559
  this.logger.trace("Logged in with identity: ".concat(JSON.stringify(identity)));
@@ -9876,7 +9575,7 @@
9876
9575
  return __awaiter(this, void 0, void 0, function () {
9877
9576
  return __generator(this, function (_a) {
9878
9577
  switch (_a.label) {
9879
- case 0: return [4, this.protocol.logout()];
9578
+ case 0: return [4, this.logoutCore()];
9880
9579
  case 1:
9881
9580
  _a.sent();
9882
9581
  return [4, this.transport.close()];
@@ -9888,17 +9587,41 @@
9888
9587
  });
9889
9588
  };
9890
9589
  Connection.prototype.loggedIn = function (callback) {
9891
- return this.protocol.loggedIn(callback);
9590
+ if (this._isLoggedIn) {
9591
+ callback();
9592
+ }
9593
+ return this.registry.add("onLoggedIn", callback);
9892
9594
  };
9893
9595
  Connection.prototype.domain = function (domain, successMessages, errorMessages) {
9894
- return this.protocol.domain(domain, this.logger.subLogger("domain=".concat(domain)), successMessages, errorMessages);
9596
+ var session = this.sessions.find(function (s) { return s.domain === domain; });
9597
+ if (!session) {
9598
+ session = domainSession(domain, this, this.logger.subLogger("domain=".concat(domain)), successMessages, errorMessages);
9599
+ this.sessions.push(session);
9600
+ }
9601
+ return session;
9895
9602
  };
9896
9603
  Connection.prototype.authToken = function () {
9897
- return this.protocol.authToken();
9604
+ var createTokenReq = {
9605
+ domain: "global",
9606
+ type: "create-token"
9607
+ };
9608
+ if (!this.globalDomain) {
9609
+ return Promise.reject(new Error("no global domain session"));
9610
+ }
9611
+ return this.globalDomain.send(createTokenReq)
9612
+ .then(function (res) {
9613
+ return res.token;
9614
+ });
9898
9615
  };
9899
9616
  Connection.prototype.reconnect = function () {
9900
9617
  return this.transport.reconnect();
9901
9618
  };
9619
+ Connection.prototype.setLoggedIn = function (value) {
9620
+ this._isLoggedIn = value;
9621
+ if (this._isLoggedIn) {
9622
+ this.registry.execute("onLoggedIn");
9623
+ }
9624
+ };
9902
9625
  Connection.prototype.distributeMessage = function (message, type) {
9903
9626
  var _this = this;
9904
9627
  var handlers = this.messageHandlers[type.toLowerCase()];
@@ -9922,28 +9645,51 @@
9922
9645
  }
9923
9646
  };
9924
9647
  Connection.prototype.handleConnectionChanged = function (connected) {
9648
+ var _a, _b;
9925
9649
  if (this._connected === connected) {
9926
9650
  return;
9927
9651
  }
9928
9652
  this._connected = connected;
9929
9653
  if (connected) {
9930
- if (this.settings.replaySpecs && this.settings.replaySpecs.length) {
9654
+ if ((_b = (_a = this.settings) === null || _a === void 0 ? void 0 : _a.replaySpecs) === null || _b === void 0 ? void 0 : _b.length) {
9931
9655
  this.replayer = new MessageReplayerImpl(this.settings.replaySpecs);
9932
9656
  this.replayer.init(this);
9933
9657
  }
9934
9658
  this.registry.execute("connected");
9935
9659
  }
9936
9660
  else {
9661
+ this.handleDisconnected();
9937
9662
  this.registry.execute("disconnected");
9938
9663
  }
9939
9664
  };
9665
+ Connection.prototype.handleDisconnected = function () {
9666
+ var _this = this;
9667
+ this.setLoggedIn(false);
9668
+ var tryToLogin = this.shouldTryLogin;
9669
+ if (tryToLogin && this.initialLogin) {
9670
+ if (this.initialLoginAttempts <= 0) {
9671
+ return;
9672
+ }
9673
+ this.initialLoginAttempts--;
9674
+ }
9675
+ this.logger.debug("disconnected - will try new login?" + this.shouldTryLogin);
9676
+ if (this.shouldTryLogin) {
9677
+ if (!this.loginConfig) {
9678
+ throw new Error("no login info");
9679
+ }
9680
+ this.login(this.loginConfig, true)
9681
+ .catch(function () {
9682
+ setTimeout(_this.handleDisconnected.bind(_this), _this.settings.reconnectInterval || 1000);
9683
+ });
9684
+ }
9685
+ };
9940
9686
  Connection.prototype.handleTransportMessage = function (msg) {
9941
9687
  var msgObj;
9942
9688
  if (typeof msg === "string") {
9943
- msgObj = this.protocol.processStringMessage(msg);
9689
+ msgObj = this.processStringMessage(msg);
9944
9690
  }
9945
9691
  else {
9946
- msgObj = this.protocol.processObjectMessage(msg);
9692
+ msgObj = this.processObjectMessage(msg);
9947
9693
  }
9948
9694
  if (this.isTrace) {
9949
9695
  this.logger.trace("<< ".concat(JSON.stringify(msgObj)));
@@ -10008,6 +9754,275 @@
10008
9754
  this._targetAuth = this._defaultAuth;
10009
9755
  this._swapTransport = true;
10010
9756
  };
9757
+ Connection.prototype.processStringMessage = function (message) {
9758
+ var _this = this;
9759
+ var msg = JSON.parse(message, function (key, value) {
9760
+ if (typeof value !== "string") {
9761
+ return value;
9762
+ }
9763
+ if (value.length < _this.dateMinLen) {
9764
+ return value;
9765
+ }
9766
+ if (!value.startsWith(_this.datePrefixFirstChar)) {
9767
+ return value;
9768
+ }
9769
+ if (value.substring(0, _this.datePrefixLen) !== _this.datePrefix) {
9770
+ return value;
9771
+ }
9772
+ try {
9773
+ var milliseconds = parseInt(value.substring(_this.datePrefixLen, value.length), 10);
9774
+ if (isNaN(milliseconds)) {
9775
+ return value;
9776
+ }
9777
+ return new Date(milliseconds);
9778
+ }
9779
+ catch (ex) {
9780
+ return value;
9781
+ }
9782
+ });
9783
+ return {
9784
+ msg: msg,
9785
+ msgType: msg.type,
9786
+ };
9787
+ };
9788
+ Connection.prototype.createStringMessage = function (message) {
9789
+ var oldToJson = Date.prototype.toJSON;
9790
+ try {
9791
+ var datePrefix_1 = this.datePrefix;
9792
+ Date.prototype.toJSON = function () {
9793
+ return datePrefix_1 + this.getTime();
9794
+ };
9795
+ var result = JSON.stringify(message);
9796
+ return result;
9797
+ }
9798
+ finally {
9799
+ Date.prototype.toJSON = oldToJson;
9800
+ }
9801
+ };
9802
+ Connection.prototype.processObjectMessage = function (message) {
9803
+ if (!message.type) {
9804
+ throw new Error("Object should have type property");
9805
+ }
9806
+ return {
9807
+ msg: message,
9808
+ msgType: message.type,
9809
+ };
9810
+ };
9811
+ Connection.prototype.createObjectMessage = function (message) {
9812
+ return message;
9813
+ };
9814
+ Connection.prototype.loginCore = function (config, reconnect) {
9815
+ return __awaiter(this, void 0, void 0, function () {
9816
+ var authentication, helloMsg, sendOptions, welcomeMsg, err_1;
9817
+ return __generator(this, function (_a) {
9818
+ switch (_a.label) {
9819
+ case 0:
9820
+ this.logger.info("logging in...");
9821
+ this.loginConfig = config;
9822
+ if (!this.loginConfig) {
9823
+ this.loginConfig = { username: "", password: "" };
9824
+ }
9825
+ this.shouldTryLogin = true;
9826
+ return [4, this.setupAuthConfig(config, reconnect)];
9827
+ case 1:
9828
+ authentication = _a.sent();
9829
+ helloMsg = {
9830
+ type: "hello",
9831
+ identity: this.settings.identity,
9832
+ authentication: authentication
9833
+ };
9834
+ if (config.sessionId) {
9835
+ helloMsg.request_id = config.sessionId;
9836
+ }
9837
+ this.globalDomain = domainSession("global", this, this.logger.subLogger("global-domain"), [
9838
+ "welcome",
9839
+ "token",
9840
+ "authentication-request"
9841
+ ]);
9842
+ sendOptions = { skipPeerId: true };
9843
+ if (this.initialLogin) {
9844
+ sendOptions.retryInterval = this.settings.reconnectInterval;
9845
+ sendOptions.maxRetries = this.settings.reconnectAttempts;
9846
+ }
9847
+ _a.label = 2;
9848
+ case 2:
9849
+ _a.trys.push([2, 4, 5, 6]);
9850
+ return [4, this.tryAuthenticate(this.globalDomain, helloMsg, sendOptions, config)];
9851
+ case 3:
9852
+ welcomeMsg = _a.sent();
9853
+ this.initialLogin = false;
9854
+ this.logger.info("login successful with peerId " + welcomeMsg.peer_id);
9855
+ this.peerId = welcomeMsg.peer_id;
9856
+ this.resolvedIdentity = welcomeMsg.resolved_identity;
9857
+ this.availableDomains = welcomeMsg.available_domains;
9858
+ if (welcomeMsg.options) {
9859
+ this.token = welcomeMsg.options.access_token;
9860
+ this.info = welcomeMsg.options.info;
9861
+ }
9862
+ this.setLoggedIn(true);
9863
+ return [2, welcomeMsg.resolved_identity];
9864
+ case 4:
9865
+ err_1 = _a.sent();
9866
+ this.logger.error("error sending hello message - " + (err_1.message || err_1.msg || err_1.reason || err_1), err_1);
9867
+ throw err_1;
9868
+ case 5:
9869
+ if ((config === null || config === void 0 ? void 0 : config.flowCallback) && config.sessionId) {
9870
+ config.flowCallback(config.sessionId, null);
9871
+ }
9872
+ return [7];
9873
+ case 6: return [2];
9874
+ }
9875
+ });
9876
+ });
9877
+ };
9878
+ Connection.prototype.tryAuthenticate = function (globalDomain, helloMsg, sendOptions, config) {
9879
+ return __awaiter(this, void 0, void 0, function () {
9880
+ var welcomeMsg, msg, token, _a;
9881
+ return __generator(this, function (_b) {
9882
+ switch (_b.label) {
9883
+ case 0:
9884
+ return [4, globalDomain.send(helloMsg, undefined, sendOptions)];
9885
+ case 1:
9886
+ msg = _b.sent();
9887
+ if (!(msg.type === "authentication-request")) return [3, 4];
9888
+ token = Buffer.from(msg.authentication.token, "base64");
9889
+ if (!(config.flowCallback && config.sessionId)) return [3, 3];
9890
+ _a = helloMsg.authentication;
9891
+ return [4, config.flowCallback(config.sessionId, token)];
9892
+ case 2:
9893
+ _a.token =
9894
+ (_b.sent())
9895
+ .data
9896
+ .toString("base64");
9897
+ _b.label = 3;
9898
+ case 3:
9899
+ helloMsg.request_id = config.sessionId;
9900
+ return [3, 5];
9901
+ case 4:
9902
+ if (msg.type === "welcome") {
9903
+ welcomeMsg = msg;
9904
+ return [3, 6];
9905
+ }
9906
+ else if (msg.type === "error") {
9907
+ throw new Error("Authentication failed: " + msg.reason);
9908
+ }
9909
+ else {
9910
+ throw new Error("Unexpected message type during authentication: " + msg.type);
9911
+ }
9912
+ case 5: return [3, 0];
9913
+ case 6: return [2, welcomeMsg];
9914
+ }
9915
+ });
9916
+ });
9917
+ };
9918
+ Connection.prototype.setupAuthConfig = function (config, reconnect) {
9919
+ return __awaiter(this, void 0, void 0, function () {
9920
+ var authentication, _a, e_1, _b;
9921
+ return __generator(this, function (_c) {
9922
+ switch (_c.label) {
9923
+ case 0:
9924
+ authentication = {};
9925
+ this.gatewayToken = config.gatewayToken;
9926
+ if (!config.gatewayToken) return [3, 5];
9927
+ if (!reconnect) return [3, 4];
9928
+ _c.label = 1;
9929
+ case 1:
9930
+ _c.trys.push([1, 3, , 4]);
9931
+ _a = config;
9932
+ return [4, this.getNewGWToken()];
9933
+ case 2:
9934
+ _a.gatewayToken = _c.sent();
9935
+ return [3, 4];
9936
+ case 3:
9937
+ e_1 = _c.sent();
9938
+ this.logger.warn("failed to get GW token when reconnecting ".concat((e_1 === null || e_1 === void 0 ? void 0 : e_1.message) || e_1));
9939
+ return [3, 4];
9940
+ case 4:
9941
+ authentication.method = "gateway-token";
9942
+ authentication.token = config.gatewayToken;
9943
+ this.gatewayToken = config.gatewayToken;
9944
+ return [3, 10];
9945
+ case 5:
9946
+ if (!(config.flowName === "sspi")) return [3, 9];
9947
+ authentication.provider = "win";
9948
+ authentication.method = "access-token";
9949
+ if (!(config.flowCallback && config.sessionId)) return [3, 7];
9950
+ _b = authentication;
9951
+ return [4, config.flowCallback(config.sessionId, null)];
9952
+ case 6:
9953
+ _b.token =
9954
+ (_c.sent())
9955
+ .data
9956
+ .toString("base64");
9957
+ return [3, 8];
9958
+ case 7: throw new Error("Invalid SSPI config");
9959
+ case 8: return [3, 10];
9960
+ case 9:
9961
+ if (config.token) {
9962
+ authentication.method = "access-token";
9963
+ authentication.token = config.token;
9964
+ }
9965
+ else if (config.username) {
9966
+ authentication.method = "secret";
9967
+ authentication.login = config.username;
9968
+ authentication.secret = config.password;
9969
+ }
9970
+ else if (config.provider) {
9971
+ authentication.provider = config.provider;
9972
+ authentication.providerContext = config.providerContext;
9973
+ }
9974
+ else {
9975
+ throw new Error("invalid auth message" + JSON.stringify(config));
9976
+ }
9977
+ _c.label = 10;
9978
+ case 10: return [2, authentication];
9979
+ }
9980
+ });
9981
+ });
9982
+ };
9983
+ Connection.prototype.logoutCore = function () {
9984
+ return __awaiter(this, void 0, void 0, function () {
9985
+ var promises;
9986
+ return __generator(this, function (_a) {
9987
+ switch (_a.label) {
9988
+ case 0:
9989
+ this.logger.debug("logging out...");
9990
+ this.shouldTryLogin = false;
9991
+ if (this.pingTimer) {
9992
+ clearTimeout(this.pingTimer);
9993
+ }
9994
+ promises = this.sessions.map(function (session) {
9995
+ session.leave();
9996
+ });
9997
+ return [4, Promise.all(promises)];
9998
+ case 1:
9999
+ _a.sent();
10000
+ return [2];
10001
+ }
10002
+ });
10003
+ });
10004
+ };
10005
+ Connection.prototype.getNewGWToken = function () {
10006
+ if (typeof window !== "undefined") {
10007
+ var glue42gd = window.glue42gd;
10008
+ if (glue42gd) {
10009
+ return glue42gd.getGWToken();
10010
+ }
10011
+ }
10012
+ return Promise.reject(new Error("not running in GD"));
10013
+ };
10014
+ Connection.prototype.ping = function () {
10015
+ var _this = this;
10016
+ if (!this.shouldTryLogin) {
10017
+ return;
10018
+ }
10019
+ if (this._isLoggedIn) {
10020
+ this.send({ type: "ping" });
10021
+ }
10022
+ this.pingTimer = setTimeout(function () {
10023
+ _this.ping();
10024
+ }, 30 * 1000);
10025
+ };
10011
10026
  return Connection;
10012
10027
  }());
10013
10028
 
@@ -10189,7 +10204,7 @@
10189
10204
  }
10190
10205
  };
10191
10206
 
10192
- var version = "6.2.0";
10207
+ var version = "6.2.1";
10193
10208
 
10194
10209
  function prepareConfig (configuration, ext, glue42gd) {
10195
10210
  var _a, _b, _c, _d;
@@ -15450,10 +15465,11 @@
15450
15465
  var server = new ServerProtocol(session, clientRepository, serverRepository, logger.subLogger("server"));
15451
15466
  var client = new ClientProtocol(session, clientRepository, logger.subLogger("client"));
15452
15467
  function handleReconnect() {
15468
+ var _a, _b;
15453
15469
  return __awaiter(this, void 0, void 0, function () {
15454
- var reconnectionPromises, existingSubscriptions, _loop_1, _i, existingSubscriptions_1, sub, registeredMethods, _loop_2, _a, registeredMethods_1, method;
15455
- return __generator(this, function (_b) {
15456
- switch (_b.label) {
15470
+ var reconnectionPromises, existingSubscriptions, _loop_1, _i, existingSubscriptions_1, sub, registeredMethods, _loop_2, _c, registeredMethods_1, method;
15471
+ return __generator(this, function (_d) {
15472
+ switch (_d.label) {
15457
15473
  case 0:
15458
15474
  logger.info("reconnected - will replay registered methods and subscriptions");
15459
15475
  client.drainSubscriptionsCache().forEach(function (sub) {
@@ -15478,25 +15494,29 @@
15478
15494
  serverRepository.reset();
15479
15495
  _loop_2 = function (method) {
15480
15496
  var def = method.definition;
15481
- logger.info("re-publishing method ".concat(def.name));
15482
15497
  if (method.stream) {
15483
- reconnectionPromises.push(interop.server.createStream(def, method.streamCallbacks, undefined, undefined, method.stream).then(function () { return logger.info("subscribing to method ".concat(def.name, " DONE")); }));
15498
+ reconnectionPromises.push(interop.server.createStream(def, method.streamCallbacks, undefined, undefined, method.stream)
15499
+ .then(function () { return logger.info("subscribing to method ".concat(def.name, " DONE")); })
15500
+ .catch(function () { return logger.warn("subscribing to method ".concat(def.name, " FAILED")); }));
15484
15501
  }
15485
- else if (method.theFunction && method.theFunction.userCallback) {
15486
- reconnectionPromises.push(interop.register(def, method.theFunction.userCallback).then(function () { return logger.info("subscribing to method ".concat(def.name, " DONE")); }));
15502
+ else if ((_a = method === null || method === void 0 ? void 0 : method.theFunction) === null || _a === void 0 ? void 0 : _a.userCallback) {
15503
+ reconnectionPromises.push(interop.register(def, method.theFunction.userCallback)
15504
+ .then(function () { return logger.info("registering method ".concat(def.name, " DONE")); })
15505
+ .catch(function () { return logger.warn("registering method ".concat(def.name, " FAILED")); }));
15487
15506
  }
15488
- else if (method.theFunction && method.theFunction.userCallbackAsync) {
15489
- reconnectionPromises.push(interop.registerAsync(def, method.theFunction.userCallbackAsync).then(function () { return logger.info("subscribing to method ".concat(def.name, " DONE")); }));
15507
+ else if ((_b = method === null || method === void 0 ? void 0 : method.theFunction) === null || _b === void 0 ? void 0 : _b.userCallbackAsync) {
15508
+ reconnectionPromises.push(interop.registerAsync(def, method.theFunction.userCallbackAsync)
15509
+ .then(function () { return logger.info("registering method ".concat(def.name, " DONE")); })
15510
+ .catch(function () { return logger.warn("registering method ".concat(def.name, " FAILED")); }));
15490
15511
  }
15491
- logger.info("re-publishing method ".concat(def.name, " DONE"));
15492
15512
  };
15493
- for (_a = 0, registeredMethods_1 = registeredMethods; _a < registeredMethods_1.length; _a++) {
15494
- method = registeredMethods_1[_a];
15513
+ for (_c = 0, registeredMethods_1 = registeredMethods; _c < registeredMethods_1.length; _c++) {
15514
+ method = registeredMethods_1[_c];
15495
15515
  _loop_2(method);
15496
15516
  }
15497
15517
  return [4, Promise.all(reconnectionPromises)];
15498
15518
  case 1:
15499
- _b.sent();
15519
+ _d.sent();
15500
15520
  logger.info("Interop is re-announced");
15501
15521
  return [2];
15502
15522
  }