@tonconnect/sdk 0.0.22 → 0.0.23

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.
@@ -14,6 +14,17 @@ const protocol_1 = require("@tonconnect/protocol");
14
14
  const errors_1 = require("../../errors");
15
15
  const http_bridge_gateway_storage_1 = require("../../storage/http-bridge-gateway-storage");
16
16
  const url_1 = require("../../utils/url");
17
+ const web_api_1 = require("../../utils/web-api");
18
+ if ((0, web_api_1.isNode)()) {
19
+ try {
20
+ // noinspection JSConstantReassignment
21
+ global.EventSource = require('eventsource');
22
+ global.fetch = require('node-fetch');
23
+ }
24
+ catch (err) {
25
+ console.error(err);
26
+ }
27
+ }
17
28
  class BridgeGateway {
18
29
  constructor(storage, bridgeUrl, sessionId, listener, errorsListener) {
19
30
  this.bridgeUrl = bridgeUrl;
@@ -34,7 +45,7 @@ class BridgeGateway {
34
45
  if (lastEventId) {
35
46
  url.searchParams.append('last_event_id', lastEventId);
36
47
  }
37
- this.eventSource = new EventSource(url);
48
+ this.eventSource = new EventSource(url.toString());
38
49
  return new Promise((resolve, reject) => {
39
50
  this.eventSource.onerror = reject;
40
51
  this.eventSource.onopen = () => {
@@ -131,7 +131,7 @@ class BridgeProvider {
131
131
  }
132
132
  gatewayErrorsListener(e) {
133
133
  return __awaiter(this, void 0, void 0, function* () {
134
- throw new ton_connect_error_1.TonConnectError(`Bridge error ${e}`);
134
+ throw new ton_connect_error_1.TonConnectError(`Bridge error ${JSON.stringify(e)}`);
135
135
  });
136
136
  }
137
137
  updateSession(connectEvent, walletPublicKey) {
@@ -36,6 +36,7 @@ exports.InjectedProvider = void 0;
36
36
  const wallet_not_injected_error_1 = require("../../errors/wallet/wallet-not-injected.error");
37
37
  const protocol = __importStar(require("../../resources/protocol.json"));
38
38
  const bridge_connection_storage_1 = require("../../storage/bridge-connection-storage");
39
+ const web_api_1 = require("../../utils/web-api");
39
40
  class InjectedProvider {
40
41
  constructor(injectedWalletKey) {
41
42
  this.type = 'injected';
@@ -65,7 +66,7 @@ class InjectedProvider {
65
66
  return false;
66
67
  }
67
68
  static isWindowContainsWallet(window, injectedWalletKey) {
68
- return (window &&
69
+ return (!!window &&
69
70
  injectedWalletKey in window &&
70
71
  typeof window[injectedWalletKey] === 'object' &&
71
72
  'tonconnect' in window[injectedWalletKey]);
@@ -144,4 +145,4 @@ class InjectedProvider {
144
145
  }
145
146
  }
146
147
  exports.InjectedProvider = InjectedProvider;
147
- InjectedProvider.window = window;
148
+ InjectedProvider.window = (0, web_api_1.getWindow)();
@@ -1,5 +1,6 @@
1
1
  import { IStorage } from "./models/storage.interface";
2
2
  export declare class DefaultStorage implements IStorage {
3
+ private readonly window;
3
4
  constructor();
4
5
  getItem(key: string): Promise<string | null>;
5
6
  removeItem(key: string): Promise<void>;
@@ -11,25 +11,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.DefaultStorage = void 0;
13
13
  const localstorage_not_found_error_1 = require("../errors/storage/localstorage-not-found.error");
14
+ const web_api_1 = require("../utils/web-api");
14
15
  class DefaultStorage {
15
16
  constructor() {
17
+ const window = (0, web_api_1.getWindow)();
16
18
  if (!(window === null || window === void 0 ? void 0 : window.localStorage)) {
17
19
  throw new localstorage_not_found_error_1.LocalstorageNotFoundError();
18
20
  }
21
+ this.window = window;
19
22
  }
20
23
  getItem(key) {
21
24
  return __awaiter(this, void 0, void 0, function* () {
22
- return Promise.resolve(window.localStorage.getItem(key));
25
+ return Promise.resolve(this.window.localStorage.getItem(key));
23
26
  });
24
27
  }
25
28
  removeItem(key) {
26
29
  return __awaiter(this, void 0, void 0, function* () {
27
- window.localStorage.removeItem(key);
30
+ this.window.localStorage.removeItem(key);
28
31
  return Promise.resolve();
29
32
  });
30
33
  }
31
34
  setItem(key, value) {
32
- window.localStorage.setItem(key, value);
35
+ this.window.localStorage.setItem(key, value);
33
36
  return Promise.resolve();
34
37
  }
35
38
  }
@@ -1,2 +1,5 @@
1
1
  import { DappMetadata } from "../models";
2
+ export declare function getWindow(): Window | undefined;
3
+ export declare function getDocument(): Document | undefined;
4
+ export declare function isNode(): boolean;
2
5
  export declare function getWebPageMetadata(): DappMetadata;
@@ -1,15 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getWebPageMetadata = void 0;
3
+ exports.getWebPageMetadata = exports.isNode = exports.getDocument = exports.getWindow = void 0;
4
+ function getWindow() {
5
+ if (typeof window === 'undefined') {
6
+ return undefined;
7
+ }
8
+ return window;
9
+ }
10
+ exports.getWindow = getWindow;
11
+ function getDocument() {
12
+ if (typeof document === 'undefined') {
13
+ return undefined;
14
+ }
15
+ return document;
16
+ }
17
+ exports.getDocument = getDocument;
18
+ function isNode() {
19
+ return typeof require === 'function' && typeof global === 'object';
20
+ }
21
+ exports.isNode = isNode;
4
22
  function getWebPageMetadata() {
23
+ var _a, _b;
5
24
  return {
6
- url: (window === null || window === void 0 ? void 0 : window.location.origin) || '',
25
+ url: ((_a = getWindow()) === null || _a === void 0 ? void 0 : _a.location.origin) || '',
7
26
  icon: getIconUrl(),
8
- name: (document === null || document === void 0 ? void 0 : document.title) || 'Unknown dapp'
27
+ name: ((_b = getDocument()) === null || _b === void 0 ? void 0 : _b.title) || 'Unknown dapp'
9
28
  };
10
29
  }
11
30
  exports.getWebPageMetadata = getWebPageMetadata;
12
31
  function getIconUrl() {
32
+ const document = getDocument();
33
+ if (!document) {
34
+ return '';
35
+ }
13
36
  const appleTouchIcons = document.querySelectorAll("link[rel='apple-touch-icon']");
14
37
  if (appleTouchIcons === null || appleTouchIcons === void 0 ? void 0 : appleTouchIcons.length) {
15
38
  return getLargestSizeIconUrl(Array.from(appleTouchIcons));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonconnect/sdk",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "scripts": {
5
5
  "build": "npx rimraf lib && ttsc",
6
6
  "build:production": "npx rimraf lib && ttsc --sourceMap false"
@@ -26,6 +26,8 @@
26
26
  "dependencies": {
27
27
  "deepmerge": "^4.2.2",
28
28
  "tweetnacl": "^1.0.3",
29
+ "eventsource": "^2.0.2",
30
+ "node-fetch": "^2.6.7",
29
31
  "@tonconnect/protocol": "^0.0.11"
30
32
  },
31
33
  "files": [