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