@tonconnect/sdk 3.0.3 → 3.0.4-beta.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.
package/lib/cjs/index.cjs CHANGED
@@ -537,7 +537,7 @@ function timeout(fn, options) {
537
537
  const timeout = options === null || options === void 0 ? void 0 : options.timeout;
538
538
  const signal = options === null || options === void 0 ? void 0 : options.signal;
539
539
  const abortController = createAbortController(signal);
540
- return new Promise((resolve, reject) => {
540
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
541
541
  if (abortController.signal.aborted) {
542
542
  reject(new TonConnectError('Operation aborted'));
543
543
  return;
@@ -554,8 +554,14 @@ function timeout(fn, options) {
554
554
  reject(new TonConnectError('Operation aborted'));
555
555
  }, { once: true });
556
556
  const deferOptions = { timeout, abort: abortController.signal };
557
- fn(resolve, reject, deferOptions).finally(() => clearTimeout(timeoutId));
558
- });
557
+ yield fn((...args) => {
558
+ clearTimeout(timeoutId);
559
+ resolve(...args);
560
+ }, () => {
561
+ clearTimeout(timeoutId);
562
+ reject();
563
+ }, deferOptions);
564
+ }));
559
565
  }
560
566
 
561
567
  class BridgeGateway {
@@ -568,7 +574,8 @@ class BridgeGateway {
568
574
  this.postPath = 'message';
569
575
  this.heartbeatMessage = 'heartbeat';
570
576
  this.defaultTtl = 300;
571
- this.defaultReconnectDelay = 5000;
577
+ this.defaultReconnectDelay = 2000;
578
+ this.defaultResendDelay = 5000;
572
579
  this.eventSource = createResource((signal, openingDeadlineMS) => __awaiter(this, void 0, void 0, function* () {
573
580
  const eventSourceConfig = {
574
581
  bridgeUrl: this.bridgeUrl,
@@ -629,7 +636,7 @@ class BridgeGateway {
629
636
  }
630
637
  }), {
631
638
  attempts: (_a = options === null || options === void 0 ? void 0 : options.attempts) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER,
632
- delayMs: 5000,
639
+ delayMs: this.defaultResendDelay,
633
640
  signal: options === null || options === void 0 ? void 0 : options.signal
634
641
  });
635
642
  });
@@ -667,11 +674,11 @@ class BridgeGateway {
667
674
  return response;
668
675
  });
669
676
  }
670
- errorsHandler(eventSource, e) {
677
+ errorsHandler(eventSource, e, connectionEstablished) {
671
678
  return __awaiter(this, void 0, void 0, function* () {
672
679
  if (this.isConnecting) {
673
- logError('Bridge error', JSON.stringify(e));
674
- return;
680
+ eventSource.close();
681
+ throw new TonConnectError('Bridge error, failed to connect');
675
682
  }
676
683
  if (this.isReady) {
677
684
  try {
@@ -680,11 +687,15 @@ class BridgeGateway {
680
687
  catch (e) { }
681
688
  return;
682
689
  }
683
- if (this.isClosed) {
690
+ if (this.isClosed && connectionEstablished) {
684
691
  eventSource.close();
685
692
  logDebug(`Bridge reconnecting, ${this.defaultReconnectDelay}ms delay`);
686
693
  return yield this.eventSource.recreate(this.defaultReconnectDelay);
687
694
  }
695
+ if (this.isClosed && !connectionEstablished) {
696
+ eventSource.close();
697
+ throw new TonConnectError('Bridge error, failed to connect');
698
+ }
688
699
  throw new TonConnectError('Bridge error, unknown state');
689
700
  });
690
701
  }
@@ -732,6 +743,7 @@ function createEventSource(config) {
732
743
  reject(new TonConnectError('Bridge connection aborted'));
733
744
  return;
734
745
  }
746
+ let connectionEstablished = false;
735
747
  const eventSource = new EventSource(url.toString());
736
748
  eventSource.onerror = (reason) => __awaiter(this, void 0, void 0, function* () {
737
749
  if (signal.aborted) {
@@ -740,7 +752,7 @@ function createEventSource(config) {
740
752
  return;
741
753
  }
742
754
  try {
743
- const newInstance = yield config.errorHandler(eventSource, reason);
755
+ const newInstance = yield config.errorHandler(eventSource, reason, connectionEstablished);
744
756
  if (newInstance !== eventSource) {
745
757
  eventSource.close();
746
758
  }
@@ -754,6 +766,7 @@ function createEventSource(config) {
754
766
  }
755
767
  });
756
768
  eventSource.onopen = () => {
769
+ connectionEstablished = true;
757
770
  if (signal.aborted) {
758
771
  eventSource.close();
759
772
  reject(new TonConnectError('Bridge connection aborted'));
@@ -951,7 +964,8 @@ class BridgeProvider {
951
964
  this.gateway = null;
952
965
  this.pendingGateways = [];
953
966
  this.listeners = [];
954
- this.defaultOpeningDeadlineMS = 5000;
967
+ this.defaultOpeningDeadlineMS = 12000;
968
+ this.defaultRetryTimeoutMS = 2000;
955
969
  this.connectionStorage = new BridgeConnectionStorage(storage);
956
970
  }
957
971
  static fromStorage(storage) {
@@ -987,12 +1001,15 @@ class BridgeProvider {
987
1001
  if (abortController.signal.aborted) {
988
1002
  return;
989
1003
  }
990
- yield callForSuccess(_options => this.openGateways(sessionCrypto, {
991
- openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
992
- signal: _options === null || _options === void 0 ? void 0 : _options.signal
993
- }), {
1004
+ yield callForSuccess(_options => {
1005
+ var _a;
1006
+ return this.openGateways(sessionCrypto, {
1007
+ openingDeadlineMS: (_a = options === null || options === void 0 ? void 0 : options.openingDeadlineMS) !== null && _a !== void 0 ? _a : this.defaultOpeningDeadlineMS,
1008
+ signal: _options === null || _options === void 0 ? void 0 : _options.signal
1009
+ });
1010
+ }, {
994
1011
  attempts: Number.MAX_SAFE_INTEGER,
995
- delayMs: 5000,
1012
+ delayMs: this.defaultRetryTimeoutMS,
996
1013
  signal: abortController.signal
997
1014
  });
998
1015
  }));
@@ -1053,7 +1070,7 @@ class BridgeProvider {
1053
1070
  signal: options.signal
1054
1071
  }), {
1055
1072
  attempts: Number.MAX_SAFE_INTEGER,
1056
- delayMs: 5000,
1073
+ delayMs: this.defaultRetryTimeoutMS,
1057
1074
  signal: abortController.signal
1058
1075
  });
1059
1076
  }
@@ -1119,7 +1136,7 @@ class BridgeProvider {
1119
1136
  yield this.sendRequest({ method: 'disconnect', params: [] }, {
1120
1137
  onRequestSent: onRequestSent,
1121
1138
  signal: abortController.signal,
1122
- attempts: 1,
1139
+ attempts: 1
1123
1140
  });
1124
1141
  }
1125
1142
  catch (e) {
@@ -1279,16 +1296,17 @@ class BridgeProvider {
1279
1296
  return gateway;
1280
1297
  });
1281
1298
  yield Promise.allSettled(this.pendingGateways.map(bridge => callForSuccess((_options) => {
1299
+ var _a;
1282
1300
  if (!this.pendingGateways.some(item => item === bridge)) {
1283
1301
  return bridge.close();
1284
1302
  }
1285
1303
  return bridge.registerSession({
1286
- openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
1304
+ openingDeadlineMS: (_a = options === null || options === void 0 ? void 0 : options.openingDeadlineMS) !== null && _a !== void 0 ? _a : this.defaultOpeningDeadlineMS,
1287
1305
  signal: _options.signal
1288
1306
  });
1289
1307
  }, {
1290
1308
  attempts: Number.MAX_SAFE_INTEGER,
1291
- delayMs: 5000,
1309
+ delayMs: this.defaultRetryTimeoutMS,
1292
1310
  signal: options === null || options === void 0 ? void 0 : options.signal
1293
1311
  })));
1294
1312
  return;
@@ -2423,7 +2441,7 @@ class TonConnectTracker {
2423
2441
  }
2424
2442
  }
2425
2443
 
2426
- const tonConnectSdkVersion = "3.0.3";
2444
+ const tonConnectSdkVersion = "3.0.4-beta.1";
2427
2445
 
2428
2446
  class TonConnect {
2429
2447
  constructor(options) {
@@ -2609,7 +2627,7 @@ class TonConnect {
2609
2627
  provider = null;
2610
2628
  };
2611
2629
  abortController.signal.addEventListener('abort', onAbortRestore);
2612
- return yield callForSuccess((_options) => __awaiter(this, void 0, void 0, function* () {
2630
+ const restoreConnectionTask = callForSuccess((_options) => __awaiter(this, void 0, void 0, function* () {
2613
2631
  yield (provider === null || provider === void 0 ? void 0 : provider.restoreConnection({
2614
2632
  openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
2615
2633
  signal: _options.signal
@@ -2623,9 +2641,12 @@ class TonConnect {
2623
2641
  }
2624
2642
  }), {
2625
2643
  attempts: Number.MAX_SAFE_INTEGER,
2626
- delayMs: 5000,
2644
+ delayMs: 2000,
2627
2645
  signal: options === null || options === void 0 ? void 0 : options.signal
2628
2646
  });
2647
+ const restoreConnectionTimeout = new Promise(resolve => setTimeout(() => resolve(), 12000) // connection deadline
2648
+ );
2649
+ return Promise.race([restoreConnectionTask, restoreConnectionTimeout]);
2629
2650
  });
2630
2651
  }
2631
2652
  sendTransaction(transaction, optionsOrOnRequestSent) {
@@ -2956,7 +2977,7 @@ exports.createTransactionSentForSignatureEvent = createTransactionSentForSignatu
2956
2977
  exports.createTransactionSignedEvent = createTransactionSignedEvent;
2957
2978
  exports.createTransactionSigningFailedEvent = createTransactionSigningFailedEvent;
2958
2979
  exports.createVersionInfo = createVersionInfo;
2959
- exports["default"] = TonConnect;
2980
+ exports.default = TonConnect;
2960
2981
  exports.encodeTelegramUrlParameters = encodeTelegramUrlParameters;
2961
2982
  exports.isTelegramUrl = isTelegramUrl;
2962
2983
  exports.isWalletInfoCurrentlyEmbedded = isWalletInfoCurrentlyEmbedded;