@ibm-aspera/sdk 0.2.9 → 0.2.29

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.
Files changed (110) hide show
  1. package/.editorconfig +13 -0
  2. package/.github/CODEOWNERS +1 -0
  3. package/.github/CODE_OF_CONDUCT.md +128 -0
  4. package/.github/CONTRIBUTING.md +147 -0
  5. package/.github/dependabot.yml +10 -0
  6. package/.github/workflows/ci.yml +39 -0
  7. package/.github/workflows/documentation.yml +44 -0
  8. package/.github/workflows/publish.yml +23 -0
  9. package/.github/workflows/version.yml +32 -0
  10. package/CHANGELOG.md +204 -0
  11. package/docs/DEVELOPMENT.md +38 -0
  12. package/eslint.config.js +104 -0
  13. package/example/README.md +7 -0
  14. package/example/index.html +14 -0
  15. package/example/package-lock.json +2989 -0
  16. package/example/package.json +30 -0
  17. package/example/public/404.html +5 -0
  18. package/example/public/sdk-code.js +326 -0
  19. package/example/src/App/App.scss +40 -0
  20. package/example/src/App/index.tsx +196 -0
  21. package/example/src/Views/AllTogether.tsx +26 -0
  22. package/example/src/Views/DragDrop.tsx +23 -0
  23. package/example/src/Views/Home.tsx +10 -0
  24. package/example/src/Views/Initialize.tsx +31 -0
  25. package/example/src/Views/Installer.tsx +154 -0
  26. package/example/src/Views/MonitorTransfers.tsx +88 -0
  27. package/example/src/Views/Other.tsx +24 -0
  28. package/example/src/Views/SelectItems.tsx +46 -0
  29. package/example/src/Views/StartTransfer.tsx +37 -0
  30. package/example/src/Views/Test.tsx +20 -0
  31. package/example/src/Views/Views.scss +111 -0
  32. package/example/src/helpers/index.ts +19 -0
  33. package/example/src/index.scss +47 -0
  34. package/example/src/main.tsx +17 -0
  35. package/example/src/vite-env.d.ts +2 -0
  36. package/example/tsconfig.json +30 -0
  37. package/example/vite.config.ts +23 -0
  38. package/jest.config.js +19 -0
  39. package/jest.setup.js +0 -0
  40. package/package.json +15 -21
  41. package/renovate.json +12 -0
  42. package/scripts/version.sh +24 -0
  43. package/src/app/core.ts +765 -0
  44. package/src/app/installer.ts +53 -0
  45. package/src/connect/core.ts +83 -0
  46. package/src/constants/constants.ts +19 -0
  47. package/src/constants/messages.ts +35 -0
  48. package/src/helpers/client/client.ts +11 -0
  49. package/src/helpers/client/http-client.ts +92 -0
  50. package/src/helpers/client/safari-client.ts +334 -0
  51. package/src/helpers/helpers.ts +253 -0
  52. package/src/helpers/http.ts +39 -0
  53. package/src/helpers/ws.ts +191 -0
  54. package/src/http-gateway/core.ts +273 -0
  55. package/src/http-gateway/download.ts +217 -0
  56. package/src/http-gateway/index.ts +19 -0
  57. package/src/http-gateway/models.ts +20 -0
  58. package/src/http-gateway/upload.ts +148 -0
  59. package/src/index.ts +72 -0
  60. package/src/models/aspera-sdk.model.ts +446 -0
  61. package/src/models/models.ts +740 -0
  62. package/tests/client.spec.ts +52 -0
  63. package/tests/core.spec.ts +13 -0
  64. package/tests/helpers.spec.ts +127 -0
  65. package/tests/http.spec.ts +14 -0
  66. package/tests/installer.spec.ts +135 -0
  67. package/tests/mocks.ts +11 -0
  68. package/tsconfig.json +14 -0
  69. package/tsconfig.module.json +16 -0
  70. package/typedoc.js +7 -0
  71. package/webpack.config.js +35 -0
  72. package/dist/commonjs/app/core.d.ts +0 -205
  73. package/dist/commonjs/app/core.js +0 -615
  74. package/dist/commonjs/app/installer.d.ts +0 -9
  75. package/dist/commonjs/app/installer.js +0 -50
  76. package/dist/commonjs/constants/constants.d.ts +0 -8
  77. package/dist/commonjs/constants/constants.js +0 -11
  78. package/dist/commonjs/constants/messages.d.ts +0 -35
  79. package/dist/commonjs/constants/messages.js +0 -38
  80. package/dist/commonjs/helpers/client/client.d.ts +0 -5
  81. package/dist/commonjs/helpers/client/client.js +0 -7
  82. package/dist/commonjs/helpers/client/http-client.d.ts +0 -42
  83. package/dist/commonjs/helpers/client/http-client.js +0 -84
  84. package/dist/commonjs/helpers/client/safari-client.d.ts +0 -101
  85. package/dist/commonjs/helpers/client/safari-client.js +0 -264
  86. package/dist/commonjs/helpers/helpers.d.ts +0 -109
  87. package/dist/commonjs/helpers/helpers.js +0 -249
  88. package/dist/commonjs/helpers/http.d.ts +0 -16
  89. package/dist/commonjs/helpers/http.js +0 -42
  90. package/dist/commonjs/helpers/ws.d.ts +0 -62
  91. package/dist/commonjs/helpers/ws.js +0 -176
  92. package/dist/commonjs/http-gateway/core.d.ts +0 -76
  93. package/dist/commonjs/http-gateway/core.js +0 -254
  94. package/dist/commonjs/http-gateway/download.d.ts +0 -14
  95. package/dist/commonjs/http-gateway/download.js +0 -186
  96. package/dist/commonjs/http-gateway/index.d.ts +0 -11
  97. package/dist/commonjs/http-gateway/index.js +0 -11
  98. package/dist/commonjs/http-gateway/models.d.ts +0 -16
  99. package/dist/commonjs/http-gateway/models.js +0 -2
  100. package/dist/commonjs/http-gateway/upload.d.ts +0 -14
  101. package/dist/commonjs/http-gateway/upload.js +0 -124
  102. package/dist/commonjs/index.d.ts +0 -8
  103. package/dist/commonjs/index.js +0 -104
  104. package/dist/commonjs/models/aspera-sdk.model.d.ts +0 -244
  105. package/dist/commonjs/models/aspera-sdk.model.js +0 -324
  106. package/dist/commonjs/models/models.d.ts +0 -692
  107. package/dist/commonjs/models/models.js +0 -2
  108. package/dist/js/aspera-sdk.js +0 -3
  109. package/dist/js/aspera-sdk.js.LICENSE.txt +0 -15
  110. package/dist/js/aspera-sdk.js.map +0 -1
@@ -1,109 +0,0 @@
1
- import { ErrorResponse, InstallerUrlInfo, PromiseObject, TransferSpec } from '../models/models';
2
- /**
3
- * Generates promise object that can be resolved or rejected via functions
4
- *
5
- * @returns an object containing the promise, the resolver and rejecter
6
- */
7
- export declare const generatePromiseObjects: () => PromiseObject;
8
- /**
9
- * Log errors from Aspera SDK
10
- *
11
- * @param message the message indicating the error encountered
12
- * @param debugData the data with useful debugging information
13
- */
14
- export declare const errorLog: (message: string, debugData?: any) => void;
15
- /**
16
- * Generate error object for rejecter responses
17
- *
18
- * @param message the message indicating the error encountered
19
- * @param debugData the data with useful debugging information
20
- *
21
- * @returns object containing standardized error response
22
- */
23
- export declare const generateErrorBody: (message: string, debugData?: any) => ErrorResponse;
24
- /**
25
- * Validate if transferSpec is valid for server communication
26
- *
27
- * @param transferSpec the transferSpec to test
28
- *
29
- * @returns boolean indicating whether supplied transferSpec is valid
30
- */
31
- export declare const isValidTransferSpec: (transferSpec: TransferSpec) => boolean;
32
- /**
33
- * Returns a string indicating the websocket URL to use for talking to the server
34
- *
35
- * @returns a string of the full Websocket URL
36
- */
37
- export declare const getWebsocketUrl: (serverUrl: string) => string;
38
- /**
39
- * Simple function to get the current platform.
40
- *
41
- * @returns a string indicating the current platform
42
- */
43
- export declare const getCurrentPlatform: () => "macos" | "windows" | "linux" | "unknown";
44
- /**
45
- * Function used to create a random UUID
46
- *
47
- * @returns string
48
- */
49
- export declare const randomUUID: () => string;
50
- /**
51
- * Return a rejected promise
52
- *
53
- * @param message the message indicating the error encountered
54
- * @param debugData the data with useful debugging information
55
- *
56
- * @returns a rejected promise
57
- */
58
- export declare const throwError: (message: string, debugData?: any) => Promise<any>;
59
- /**
60
- * Check if the given string is a valid URL
61
- *
62
- * @param url string to check if valid URL
63
- *
64
- * @returns boolean
65
- */
66
- export declare const isValidURL: (url: string) => boolean;
67
- /**
68
- * Checks if the current browser is Safari.
69
- * @returns {boolean} Whether the browser is Safari.
70
- */
71
- export declare const isSafari: () => boolean;
72
- /**
73
- * Get the URLs for installer management.
74
- *
75
- * @returns Info on URLs where installers live
76
- */
77
- export declare const getInstallerUrls: () => InstallerUrlInfo;
78
- /**
79
- * Try to stringify a JSON string and log failures
80
- *
81
- * @param json - Object to make into a string
82
- *
83
- * @returns string representing JSON or empty string on error
84
- */
85
- export declare const safeJsonString: (json: unknown) => string;
86
- /**
87
- * Try to parse a JSON string and log failures
88
- *
89
- * @param json - String to make into an object
90
- *
91
- * @returns object or array from the JSON string. Or undefined
92
- */
93
- export declare const safeJsonParse: (json: string) => any | undefined;
94
- declare const _default: {
95
- errorLog: (message: string, debugData?: any) => void;
96
- generateErrorBody: (message: string, debugData?: any) => ErrorResponse;
97
- generatePromiseObjects: () => PromiseObject;
98
- getCurrentPlatform: () => "macos" | "windows" | "linux" | "unknown";
99
- getWebsocketUrl: (serverUrl: string) => string;
100
- isSafari: () => boolean;
101
- isValidURL: (url: string) => boolean;
102
- isValidTransferSpec: (transferSpec: TransferSpec) => boolean;
103
- randomUUID: () => string;
104
- throwError: (message: string, debugData?: any) => Promise<any>;
105
- getInstallerUrls: () => InstallerUrlInfo;
106
- safeJsonString: (json: unknown) => string;
107
- safeJsonParse: (json: string) => any | undefined;
108
- };
109
- export default _default;
@@ -1,249 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.safeJsonParse = exports.safeJsonString = exports.getInstallerUrls = exports.isSafari = exports.isValidURL = exports.throwError = exports.randomUUID = exports.getCurrentPlatform = exports.getWebsocketUrl = exports.isValidTransferSpec = exports.generateErrorBody = exports.errorLog = exports.generatePromiseObjects = void 0;
4
- var constants_1 = require("../constants/constants");
5
- /**
6
- * Generates promise object that can be resolved or rejected via functions
7
- *
8
- * @returns an object containing the promise, the resolver and rejecter
9
- */
10
- var generatePromiseObjects = function () {
11
- var resolver;
12
- var rejecter;
13
- var promise = new Promise(function (resolve, reject) {
14
- resolver = resolve;
15
- rejecter = reject;
16
- });
17
- return {
18
- promise: promise,
19
- resolver: resolver,
20
- rejecter: rejecter
21
- };
22
- };
23
- exports.generatePromiseObjects = generatePromiseObjects;
24
- /**
25
- * Log errors from Aspera SDK
26
- *
27
- * @param message the message indicating the error encountered
28
- * @param debugData the data with useful debugging information
29
- */
30
- var errorLog = function (message, debugData) {
31
- if (debugData && debugData.code && debugData.message) {
32
- debugData = {
33
- code: debugData.code,
34
- message: debugData.message,
35
- data: debugData.data
36
- };
37
- }
38
- if (typeof window === 'object') {
39
- if (!Array.isArray(window.asperaSdkLogs)) {
40
- window.asperaSdkLogs = [];
41
- }
42
- window.asperaSdkLogs.push({ message: message, debugData: debugData });
43
- }
44
- console.warn("Aspera SDK: ".concat(message), debugData);
45
- };
46
- exports.errorLog = errorLog;
47
- /**
48
- * Generate error object for rejecter responses
49
- *
50
- * @param message the message indicating the error encountered
51
- * @param debugData the data with useful debugging information
52
- *
53
- * @returns object containing standardized error response
54
- */
55
- var generateErrorBody = function (message, debugData) {
56
- var errorResponse = {
57
- error: true,
58
- message: message
59
- };
60
- if (debugData && debugData.code && debugData.message) {
61
- errorResponse.debugData = {
62
- code: debugData.code,
63
- message: debugData.message,
64
- data: debugData.data
65
- };
66
- }
67
- return errorResponse;
68
- };
69
- exports.generateErrorBody = generateErrorBody;
70
- /**
71
- * Validate if transferSpec is valid for server communication
72
- *
73
- * @param transferSpec the transferSpec to test
74
- *
75
- * @returns boolean indicating whether supplied transferSpec is valid
76
- */
77
- var isValidTransferSpec = function (transferSpec) {
78
- if (transferSpec &&
79
- typeof transferSpec === 'object' &&
80
- typeof transferSpec.direction === 'string' &&
81
- typeof transferSpec.remote_host === 'string' &&
82
- Array.isArray(transferSpec.paths)) {
83
- return true;
84
- }
85
- return false;
86
- };
87
- exports.isValidTransferSpec = isValidTransferSpec;
88
- /**
89
- * Returns a string indicating the websocket URL to use for talking to the server
90
- *
91
- * @returns a string of the full Websocket URL
92
- */
93
- var getWebsocketUrl = function (serverUrl) {
94
- var wsProtocol;
95
- if (serverUrl.indexOf('http:') === 0) {
96
- wsProtocol = 'ws';
97
- }
98
- else if (serverUrl.indexOf('https:') === 0) {
99
- wsProtocol = 'wss';
100
- }
101
- var url = serverUrl.replace('http://', '//').replace('https://', '//');
102
- return "".concat(wsProtocol, ":").concat(url);
103
- };
104
- exports.getWebsocketUrl = getWebsocketUrl;
105
- /**
106
- * Simple function to get the current platform.
107
- *
108
- * @returns a string indicating the current platform
109
- */
110
- var getCurrentPlatform = function () {
111
- var ua = navigator.userAgent;
112
- if (/Mac/.test(ua)) {
113
- return 'macos';
114
- }
115
- else if (/Win/.test(ua)) {
116
- return 'windows';
117
- }
118
- else if (/Linux/.test(ua)) {
119
- return 'linux';
120
- }
121
- else {
122
- return 'unknown';
123
- }
124
- };
125
- exports.getCurrentPlatform = getCurrentPlatform;
126
- /**
127
- * Function used to create a random UUID
128
- *
129
- * @returns string
130
- */
131
- var randomUUID = function () {
132
- var _a;
133
- var fallback = function () {
134
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
135
- var r = Number(((new Date().getTime() + 16) * Math.random()).toFixed()) % 16;
136
- if (c !== 'x') {
137
- // eslint-disable-next-line no-bitwise
138
- r = r & 0x3 | 0x8;
139
- }
140
- return r.toString(16);
141
- });
142
- };
143
- return ((_a = window.crypto) === null || _a === void 0 ? void 0 : _a.randomUUID) ? window.crypto.randomUUID() : fallback();
144
- };
145
- exports.randomUUID = randomUUID;
146
- /**
147
- * Return a rejected promise
148
- *
149
- * @param message the message indicating the error encountered
150
- * @param debugData the data with useful debugging information
151
- *
152
- * @returns a rejected promise
153
- */
154
- var throwError = function (message, debugData) {
155
- (0, exports.errorLog)(message, debugData);
156
- return new Promise(function (resolve, reject) {
157
- reject((0, exports.generateErrorBody)(message, debugData));
158
- });
159
- };
160
- exports.throwError = throwError;
161
- /**
162
- * Check if the given string is a valid URL
163
- *
164
- * @param url string to check if valid URL
165
- *
166
- * @returns boolean
167
- */
168
- var isValidURL = function (url) {
169
- try {
170
- new URL(url);
171
- return true;
172
- }
173
- catch (error) {
174
- return false;
175
- }
176
- };
177
- exports.isValidURL = isValidURL;
178
- /**
179
- * Checks if the current browser is Safari.
180
- * @returns {boolean} Whether the browser is Safari.
181
- */
182
- var isSafari = function () {
183
- return /^((?!chrome|android).)*safari/i.test(navigator.userAgent) && !window.MSStream;
184
- };
185
- exports.isSafari = isSafari;
186
- /**
187
- * Get the URLs for installer management.
188
- *
189
- * @returns Info on URLs where installers live
190
- */
191
- var getInstallerUrls = function () {
192
- return {
193
- base: constants_1.baseInstallerUrl,
194
- latest: constants_1.installerUrl,
195
- };
196
- };
197
- exports.getInstallerUrls = getInstallerUrls;
198
- /**
199
- * Try to stringify a JSON string and log failures
200
- *
201
- * @param json - Object to make into a string
202
- *
203
- * @returns string representing JSON or empty string on error
204
- */
205
- var safeJsonString = function (json) {
206
- try {
207
- return JSON.stringify(json);
208
- }
209
- catch (error) {
210
- (0, exports.errorLog)('safeJsonString: unable to stringify JSON', { error: error, json: json });
211
- return '';
212
- }
213
- };
214
- exports.safeJsonString = safeJsonString;
215
- /**
216
- * Try to parse a JSON string and log failures
217
- *
218
- * @param json - String to make into an object
219
- *
220
- * @returns object or array from the JSON string. Or undefined
221
- */
222
- var safeJsonParse = function (json) {
223
- if (json && typeof json === 'object') {
224
- return json;
225
- }
226
- try {
227
- return JSON.parse(json);
228
- }
229
- catch (error) {
230
- (0, exports.errorLog)('safeJsonParse: unable to parse JSON', { error: error, json: json });
231
- return undefined;
232
- }
233
- };
234
- exports.safeJsonParse = safeJsonParse;
235
- exports.default = {
236
- errorLog: exports.errorLog,
237
- generateErrorBody: exports.generateErrorBody,
238
- generatePromiseObjects: exports.generatePromiseObjects,
239
- getCurrentPlatform: exports.getCurrentPlatform,
240
- getWebsocketUrl: exports.getWebsocketUrl,
241
- isSafari: exports.isSafari,
242
- isValidURL: exports.isValidURL,
243
- isValidTransferSpec: exports.isValidTransferSpec,
244
- randomUUID: exports.randomUUID,
245
- throwError: exports.throwError,
246
- getInstallerUrls: exports.getInstallerUrls,
247
- safeJsonString: exports.safeJsonString,
248
- safeJsonParse: exports.safeJsonParse,
249
- };
@@ -1,16 +0,0 @@
1
- /**
2
- * Check HTTP promise response for server error response (non 2XX status) and reject promise is error
3
- *
4
- * @param promise the HTTP promise to check for HTTP status code of 2XX for success
5
- *
6
- * @returns promise for the HTTP connection with catch supporting error
7
- */
8
- export declare const handlePromiseErrors: (promise: Promise<any>) => Promise<any>;
9
- /**
10
- * Make a GET for retrieving data from a server
11
- *
12
- * @param url the url string of the resource on the server
13
- *
14
- * @returns a promise that will resolve with the response from the server or reject if network/server error
15
- */
16
- export declare const apiGet: (url: string) => Promise<any>;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.apiGet = exports.handlePromiseErrors = void 0;
4
- var helpers_1 = require("./helpers");
5
- /**
6
- * Check HTTP promise response for server error response (non 2XX status) and reject promise is error
7
- *
8
- * @param promise the HTTP promise to check for HTTP status code of 2XX for success
9
- *
10
- * @returns promise for the HTTP connection with catch supporting error
11
- */
12
- var handlePromiseErrors = function (promise) {
13
- var promiseInfo = (0, helpers_1.generatePromiseObjects)();
14
- promise.then(function (response) {
15
- if (response.ok) {
16
- promiseInfo.resolver(response);
17
- }
18
- else {
19
- promiseInfo.rejecter(response);
20
- }
21
- return response;
22
- }).catch(function (error) {
23
- promiseInfo.rejecter(error);
24
- });
25
- return promiseInfo.promise;
26
- };
27
- exports.handlePromiseErrors = handlePromiseErrors;
28
- /**
29
- * Make a GET for retrieving data from a server
30
- *
31
- * @param url the url string of the resource on the server
32
- *
33
- * @returns a promise that will resolve with the response from the server or reject if network/server error
34
- */
35
- var apiGet = function (url) {
36
- return (0, exports.handlePromiseErrors)(fetch(url, {
37
- headers: {
38
- 'Content-Type': 'application/json',
39
- },
40
- }));
41
- };
42
- exports.apiGet = apiGet;
@@ -1,62 +0,0 @@
1
- import { WebsocketTopics } from '../models/models';
2
- export declare class WebsocketService {
3
- /** The main websocket connection to Aspera App*/
4
- private globalSocket;
5
- /** A map of requested subscription names and the callback for them */
6
- private sockets;
7
- /** The callback for websocket events */
8
- private eventListener;
9
- /** Indicator if the websocket is already connected */
10
- private isConnected;
11
- /** Global promise object that resolves when init completes */
12
- private initPromise;
13
- /** Log call for not being ready */
14
- private handleNotReady;
15
- /**
16
- * This function handles when a connection is opened
17
- */
18
- private handleOpen;
19
- /**
20
- * This function handles completed subscription
21
- */
22
- private handleClose;
23
- /**
24
- * This function handles errors received from the websocket
25
- */
26
- private handleError;
27
- /**
28
- * This function handles messages received from the websocket
29
- */
30
- private handleMessage;
31
- /**
32
- * This function joins the channel to be able to subscribe to events
33
- */
34
- private joinChannel;
35
- /**
36
- * This function registers clients to listen to a certain message name. Returns any to allow functions to declare proper type
37
- *
38
- * @param messageName - the name of messages to listen to (one message name per subscription)
39
- * @param callback - the callback function
40
- */
41
- registerMessage(messageName: WebsocketTopics, callback: Function): void;
42
- /**
43
- *
44
- * @param callback This function registers clients to a certain WebSocket event.
45
- *
46
- * @param callback - the callback function to call with the event name.
47
- */
48
- registerEvent(callback: Function): void;
49
- /**
50
- * This function starts the websocket subscription with the websocket provider
51
- *
52
- * @returns a promise that resolves when the websocket connection is established
53
- */
54
- init(): Promise<unknown>;
55
- private connect;
56
- private reconnect;
57
- private getWebSocketConnection;
58
- private notifyEvent;
59
- private updateRpcPort;
60
- }
61
- export declare const websocketService: WebsocketService;
62
- export default WebsocketService;
@@ -1,176 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.websocketService = exports.WebsocketService = void 0;
4
- var helpers_1 = require("./helpers");
5
- var messages_1 = require("../constants/messages");
6
- var index_1 = require("../index");
7
- var WebsocketService = /** @class */ (function () {
8
- function WebsocketService() {
9
- var _this = this;
10
- /** A map of requested subscription names and the callback for them */
11
- this.sockets = new Map();
12
- /** Indicator if the websocket is already connected */
13
- this.isConnected = false;
14
- /** Global promise object that resolves when init completes */
15
- this.initPromise = (0, helpers_1.generatePromiseObjects)();
16
- /**
17
- * This function handles when a connection is opened
18
- */
19
- this.handleOpen = function () {
20
- if (_this.isConnected || !_this.joinChannel()) {
21
- return;
22
- }
23
- _this.isConnected = true;
24
- _this.updateRpcPort();
25
- _this.notifyEvent('RECONNECT');
26
- };
27
- /**
28
- * This function handles completed subscription
29
- */
30
- this.handleClose = function () {
31
- if (_this.isConnected) {
32
- _this.isConnected = false;
33
- _this.notifyEvent('CLOSED');
34
- }
35
- if (!_this.globalSocket) {
36
- _this.handleNotReady();
37
- return;
38
- }
39
- _this.reconnect();
40
- };
41
- /**
42
- * This function handles errors received from the websocket
43
- */
44
- this.handleError = function () {
45
- (0, helpers_1.errorLog)(messages_1.messages.websocketClosedError);
46
- };
47
- /**
48
- * This function handles messages received from the websocket
49
- */
50
- this.handleMessage = function (message) {
51
- var data = (0, helpers_1.safeJsonParse)(message.data);
52
- // Message we get on subscription
53
- if (data && data.id === 1) {
54
- _this.initPromise.resolver(data);
55
- return;
56
- }
57
- var socket = _this.sockets.get(data.method);
58
- if (socket && data.params) {
59
- socket(data.params);
60
- }
61
- };
62
- }
63
- /** Log call for not being ready */
64
- WebsocketService.prototype.handleNotReady = function () {
65
- (0, helpers_1.errorLog)(messages_1.messages.websocketNotReady);
66
- };
67
- /**
68
- * This function joins the channel to be able to subscribe to events
69
- */
70
- WebsocketService.prototype.joinChannel = function () {
71
- if (!this.globalSocket) {
72
- this.handleNotReady();
73
- return false;
74
- }
75
- this.globalSocket.send((0, helpers_1.safeJsonString)({ jsonrpc: '2.0', method: 'subscribe_transfer_activity', params: [index_1.asperaSdk.globals.appId], id: 1 }));
76
- return true;
77
- };
78
- /**
79
- * This function registers clients to listen to a certain message name. Returns any to allow functions to declare proper type
80
- *
81
- * @param messageName - the name of messages to listen to (one message name per subscription)
82
- * @param callback - the callback function
83
- */
84
- WebsocketService.prototype.registerMessage = function (messageName, callback) {
85
- if (!this.sockets.get(messageName)) {
86
- this.sockets.set(messageName, function (data) {
87
- callback(data.result);
88
- });
89
- }
90
- };
91
- /**
92
- *
93
- * @param callback This function registers clients to a certain WebSocket event.
94
- *
95
- * @param callback - the callback function to call with the event name.
96
- */
97
- WebsocketService.prototype.registerEvent = function (callback) {
98
- this.eventListener = callback;
99
- this.eventListener(this.isConnected ? 'RECONNECT' : 'CLOSED');
100
- };
101
- /**
102
- * This function starts the websocket subscription with the websocket provider
103
- *
104
- * @returns a promise that resolves when the websocket connection is established
105
- */
106
- WebsocketService.prototype.init = function () {
107
- this.connect();
108
- return this.initPromise.promise;
109
- };
110
- WebsocketService.prototype.connect = function () {
111
- var _this = this;
112
- this.getWebSocketConnection(33024, index_1.asperaSdk.globals.supportMultipleUsers ? 33029 : 33024)
113
- .then(function (webSocket) {
114
- _this.globalSocket = webSocket;
115
- _this.globalSocket.onerror = _this.handleError;
116
- _this.globalSocket.onclose = _this.handleClose;
117
- _this.globalSocket.onopen = _this.handleOpen;
118
- _this.globalSocket.onmessage = _this.handleMessage;
119
- _this.handleOpen();
120
- }).catch(function () {
121
- _this.reconnect();
122
- });
123
- };
124
- WebsocketService.prototype.reconnect = function () {
125
- var _this = this;
126
- if (this.globalSocket) {
127
- this.globalSocket.close();
128
- }
129
- setTimeout(function () {
130
- _this.connect();
131
- }, 1000);
132
- };
133
- WebsocketService.prototype.getWebSocketConnection = function (startPort, endPort) {
134
- var webSocketUrl = (0, helpers_1.getWebsocketUrl)(index_1.asperaSdk.globals.asperaAppUrl);
135
- var checkPort = function (port) {
136
- return new Promise(function (resolve, reject) {
137
- var webSocket = new WebSocket("".concat(webSocketUrl, ":").concat(port));
138
- webSocket.onopen = function () {
139
- resolve(webSocket);
140
- };
141
- webSocket.onerror = function () {
142
- reject("Connection failed on port ".concat(port));
143
- };
144
- });
145
- };
146
- return new Promise(function (resolve, reject) {
147
- var connectPort = function (port) {
148
- if (port > endPort) {
149
- return reject('No available WebSocket connection found');
150
- }
151
- checkPort(port)
152
- .then(function (ws) { return resolve(ws); })
153
- .catch(function (error) {
154
- connectPort(port + 1);
155
- });
156
- };
157
- connectPort(startPort);
158
- });
159
- };
160
- WebsocketService.prototype.notifyEvent = function (event) {
161
- if (typeof this.eventListener === 'function') {
162
- this.eventListener(event);
163
- }
164
- };
165
- WebsocketService.prototype.updateRpcPort = function () {
166
- if (!this.globalSocket) {
167
- return;
168
- }
169
- var url = new URL(this.globalSocket.url);
170
- index_1.asperaSdk.globals.rpcPort = Number(url.port);
171
- };
172
- return WebsocketService;
173
- }());
174
- exports.WebsocketService = WebsocketService;
175
- exports.websocketService = new WebsocketService();
176
- exports.default = WebsocketService;