@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/esm/index.mjs CHANGED
@@ -534,7 +534,7 @@ function timeout(fn, options) {
534
534
  const timeout = options === null || options === void 0 ? void 0 : options.timeout;
535
535
  const signal = options === null || options === void 0 ? void 0 : options.signal;
536
536
  const abortController = createAbortController(signal);
537
- return new Promise((resolve, reject) => {
537
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
538
538
  if (abortController.signal.aborted) {
539
539
  reject(new TonConnectError('Operation aborted'));
540
540
  return;
@@ -551,8 +551,14 @@ function timeout(fn, options) {
551
551
  reject(new TonConnectError('Operation aborted'));
552
552
  }, { once: true });
553
553
  const deferOptions = { timeout, abort: abortController.signal };
554
- fn(resolve, reject, deferOptions).finally(() => clearTimeout(timeoutId));
555
- });
554
+ yield fn((...args) => {
555
+ clearTimeout(timeoutId);
556
+ resolve(...args);
557
+ }, () => {
558
+ clearTimeout(timeoutId);
559
+ reject();
560
+ }, deferOptions);
561
+ }));
556
562
  }
557
563
 
558
564
  class BridgeGateway {
@@ -565,7 +571,8 @@ class BridgeGateway {
565
571
  this.postPath = 'message';
566
572
  this.heartbeatMessage = 'heartbeat';
567
573
  this.defaultTtl = 300;
568
- this.defaultReconnectDelay = 5000;
574
+ this.defaultReconnectDelay = 2000;
575
+ this.defaultResendDelay = 5000;
569
576
  this.eventSource = createResource((signal, openingDeadlineMS) => __awaiter(this, void 0, void 0, function* () {
570
577
  const eventSourceConfig = {
571
578
  bridgeUrl: this.bridgeUrl,
@@ -626,7 +633,7 @@ class BridgeGateway {
626
633
  }
627
634
  }), {
628
635
  attempts: (_a = options === null || options === void 0 ? void 0 : options.attempts) !== null && _a !== void 0 ? _a : Number.MAX_SAFE_INTEGER,
629
- delayMs: 5000,
636
+ delayMs: this.defaultResendDelay,
630
637
  signal: options === null || options === void 0 ? void 0 : options.signal
631
638
  });
632
639
  });
@@ -664,11 +671,11 @@ class BridgeGateway {
664
671
  return response;
665
672
  });
666
673
  }
667
- errorsHandler(eventSource, e) {
674
+ errorsHandler(eventSource, e, connectionEstablished) {
668
675
  return __awaiter(this, void 0, void 0, function* () {
669
676
  if (this.isConnecting) {
670
- logError('Bridge error', JSON.stringify(e));
671
- return;
677
+ eventSource.close();
678
+ throw new TonConnectError('Bridge error, failed to connect');
672
679
  }
673
680
  if (this.isReady) {
674
681
  try {
@@ -677,11 +684,15 @@ class BridgeGateway {
677
684
  catch (e) { }
678
685
  return;
679
686
  }
680
- if (this.isClosed) {
687
+ if (this.isClosed && connectionEstablished) {
681
688
  eventSource.close();
682
689
  logDebug(`Bridge reconnecting, ${this.defaultReconnectDelay}ms delay`);
683
690
  return yield this.eventSource.recreate(this.defaultReconnectDelay);
684
691
  }
692
+ if (this.isClosed && !connectionEstablished) {
693
+ eventSource.close();
694
+ throw new TonConnectError('Bridge error, failed to connect');
695
+ }
685
696
  throw new TonConnectError('Bridge error, unknown state');
686
697
  });
687
698
  }
@@ -729,6 +740,7 @@ function createEventSource(config) {
729
740
  reject(new TonConnectError('Bridge connection aborted'));
730
741
  return;
731
742
  }
743
+ let connectionEstablished = false;
732
744
  const eventSource = new EventSource(url.toString());
733
745
  eventSource.onerror = (reason) => __awaiter(this, void 0, void 0, function* () {
734
746
  if (signal.aborted) {
@@ -737,7 +749,7 @@ function createEventSource(config) {
737
749
  return;
738
750
  }
739
751
  try {
740
- const newInstance = yield config.errorHandler(eventSource, reason);
752
+ const newInstance = yield config.errorHandler(eventSource, reason, connectionEstablished);
741
753
  if (newInstance !== eventSource) {
742
754
  eventSource.close();
743
755
  }
@@ -751,6 +763,7 @@ function createEventSource(config) {
751
763
  }
752
764
  });
753
765
  eventSource.onopen = () => {
766
+ connectionEstablished = true;
754
767
  if (signal.aborted) {
755
768
  eventSource.close();
756
769
  reject(new TonConnectError('Bridge connection aborted'));
@@ -948,7 +961,8 @@ class BridgeProvider {
948
961
  this.gateway = null;
949
962
  this.pendingGateways = [];
950
963
  this.listeners = [];
951
- this.defaultOpeningDeadlineMS = 5000;
964
+ this.defaultOpeningDeadlineMS = 12000;
965
+ this.defaultRetryTimeoutMS = 2000;
952
966
  this.connectionStorage = new BridgeConnectionStorage(storage);
953
967
  }
954
968
  static fromStorage(storage) {
@@ -984,12 +998,15 @@ class BridgeProvider {
984
998
  if (abortController.signal.aborted) {
985
999
  return;
986
1000
  }
987
- yield callForSuccess(_options => this.openGateways(sessionCrypto, {
988
- openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
989
- signal: _options === null || _options === void 0 ? void 0 : _options.signal
990
- }), {
1001
+ yield callForSuccess(_options => {
1002
+ var _a;
1003
+ return this.openGateways(sessionCrypto, {
1004
+ openingDeadlineMS: (_a = options === null || options === void 0 ? void 0 : options.openingDeadlineMS) !== null && _a !== void 0 ? _a : this.defaultOpeningDeadlineMS,
1005
+ signal: _options === null || _options === void 0 ? void 0 : _options.signal
1006
+ });
1007
+ }, {
991
1008
  attempts: Number.MAX_SAFE_INTEGER,
992
- delayMs: 5000,
1009
+ delayMs: this.defaultRetryTimeoutMS,
993
1010
  signal: abortController.signal
994
1011
  });
995
1012
  }));
@@ -1050,7 +1067,7 @@ class BridgeProvider {
1050
1067
  signal: options.signal
1051
1068
  }), {
1052
1069
  attempts: Number.MAX_SAFE_INTEGER,
1053
- delayMs: 5000,
1070
+ delayMs: this.defaultRetryTimeoutMS,
1054
1071
  signal: abortController.signal
1055
1072
  });
1056
1073
  }
@@ -1116,7 +1133,7 @@ class BridgeProvider {
1116
1133
  yield this.sendRequest({ method: 'disconnect', params: [] }, {
1117
1134
  onRequestSent: onRequestSent,
1118
1135
  signal: abortController.signal,
1119
- attempts: 1,
1136
+ attempts: 1
1120
1137
  });
1121
1138
  }
1122
1139
  catch (e) {
@@ -1276,16 +1293,17 @@ class BridgeProvider {
1276
1293
  return gateway;
1277
1294
  });
1278
1295
  yield Promise.allSettled(this.pendingGateways.map(bridge => callForSuccess((_options) => {
1296
+ var _a;
1279
1297
  if (!this.pendingGateways.some(item => item === bridge)) {
1280
1298
  return bridge.close();
1281
1299
  }
1282
1300
  return bridge.registerSession({
1283
- openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
1301
+ openingDeadlineMS: (_a = options === null || options === void 0 ? void 0 : options.openingDeadlineMS) !== null && _a !== void 0 ? _a : this.defaultOpeningDeadlineMS,
1284
1302
  signal: _options.signal
1285
1303
  });
1286
1304
  }, {
1287
1305
  attempts: Number.MAX_SAFE_INTEGER,
1288
- delayMs: 5000,
1306
+ delayMs: this.defaultRetryTimeoutMS,
1289
1307
  signal: options === null || options === void 0 ? void 0 : options.signal
1290
1308
  })));
1291
1309
  return;
@@ -2420,7 +2438,7 @@ class TonConnectTracker {
2420
2438
  }
2421
2439
  }
2422
2440
 
2423
- const tonConnectSdkVersion = "3.0.3";
2441
+ const tonConnectSdkVersion = "3.0.4-beta.1";
2424
2442
 
2425
2443
  class TonConnect {
2426
2444
  constructor(options) {
@@ -2606,7 +2624,7 @@ class TonConnect {
2606
2624
  provider = null;
2607
2625
  };
2608
2626
  abortController.signal.addEventListener('abort', onAbortRestore);
2609
- return yield callForSuccess((_options) => __awaiter(this, void 0, void 0, function* () {
2627
+ const restoreConnectionTask = callForSuccess((_options) => __awaiter(this, void 0, void 0, function* () {
2610
2628
  yield (provider === null || provider === void 0 ? void 0 : provider.restoreConnection({
2611
2629
  openingDeadlineMS: options === null || options === void 0 ? void 0 : options.openingDeadlineMS,
2612
2630
  signal: _options.signal
@@ -2620,9 +2638,12 @@ class TonConnect {
2620
2638
  }
2621
2639
  }), {
2622
2640
  attempts: Number.MAX_SAFE_INTEGER,
2623
- delayMs: 5000,
2641
+ delayMs: 2000,
2624
2642
  signal: options === null || options === void 0 ? void 0 : options.signal
2625
2643
  });
2644
+ const restoreConnectionTimeout = new Promise(resolve => setTimeout(() => resolve(), 12000) // connection deadline
2645
+ );
2646
+ return Promise.race([restoreConnectionTask, restoreConnectionTimeout]);
2626
2647
  });
2627
2648
  }
2628
2649
  sendTransaction(transaction, optionsOrOnRequestSent) {