@dynamic-labs/utils 0.18.27 → 0.18.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.
package/CHANGELOG.md CHANGED
@@ -1,4 +1,20 @@
1
1
 
2
+ ### [0.18.29](https://github.com/dynamic-labs/DynamicAuth/compare/v0.18.28...v0.18.29) (2023-11-13)
3
+
4
+
5
+ ### Bug Fixes
6
+
7
+ * **bridge:** disconnect mm when locked offsite ([#3872](https://github.com/dynamic-labs/DynamicAuth/issues/3872)) ([4a34492](https://github.com/dynamic-labs/DynamicAuth/commit/4a344926d2987cd31123b9a5ac9c3dbf1efccbe0))
8
+ * only process oauth message coming from the expected origin ([#3805](https://github.com/dynamic-labs/DynamicAuth/issues/3805)) ([970003c](https://github.com/dynamic-labs/DynamicAuth/commit/970003c81cc101dcca2c4a56c48095303377d8ef))
9
+
10
+ ### [0.18.28](https://github.com/dynamic-labs/DynamicAuth/compare/v0.18.27...v0.18.28) (2023-10-30)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * better handling of Coinbase wallet disconnect + upgrade to 3.7.2 ([#3681](https://github.com/dynamic-labs/DynamicAuth/issues/3681)) ([075ca3c](https://github.com/dynamic-labs/DynamicAuth/commit/075ca3c0e6c34e8b75bc80cd1980efebdb41f35a))
16
+ * safely render message to sign ([#3655](https://github.com/dynamic-labs/DynamicAuth/issues/3655)) ([#3660](https://github.com/dynamic-labs/DynamicAuth/issues/3660)) ([c91ff85](https://github.com/dynamic-labs/DynamicAuth/commit/c91ff85fef2616cafae6681ebc9328aac783b294))
17
+
2
18
  ### [0.18.27](https://github.com/dynamic-labs/DynamicAuth/compare/v0.18.26...v0.18.27) (2023-10-19)
3
19
 
4
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dynamic-labs/utils",
3
- "version": "0.18.27",
3
+ "version": "0.18.29",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/dynamic-labs/DynamicAuth.git",
@@ -26,8 +26,8 @@
26
26
  "./package.json": "./package.json"
27
27
  },
28
28
  "dependencies": {
29
- "@dynamic-labs/logger": "0.18.27",
30
- "@dynamic-labs/types": "0.18.27"
29
+ "@dynamic-labs/logger": "0.18.29",
30
+ "@dynamic-labs/types": "0.18.29"
31
31
  },
32
32
  "peerDependencies": {}
33
33
  }
package/src/index.cjs CHANGED
@@ -25,6 +25,7 @@ var isMobile = require('./isMobile.cjs');
25
25
  var localStorageAsync = require('./localStorageAsync.cjs');
26
26
  var bufferToBase64 = require('./bufferToBase64.cjs');
27
27
  var last = require('./last.cjs');
28
+ var retryableFn = require('./retryableFn.cjs');
28
29
 
29
30
 
30
31
 
@@ -59,3 +60,5 @@ exports.removeItemAsync = localStorageAsync.removeItemAsync;
59
60
  exports.setItemAsync = localStorageAsync.setItemAsync;
60
61
  exports.bufferToBase64 = bufferToBase64.bufferToBase64;
61
62
  exports.last = last.last;
63
+ exports.FALLBACK_UNDEFINED = retryableFn.FALLBACK_UNDEFINED;
64
+ exports.retryableFn = retryableFn.retryableFn;
package/src/index.d.ts CHANGED
@@ -7,3 +7,4 @@ export * from './isMobile';
7
7
  export * from './localStorageAsync';
8
8
  export * from './bufferToBase64';
9
9
  export * from './last';
10
+ export * from './retryableFn';
package/src/index.js CHANGED
@@ -21,3 +21,4 @@ export { isAndroid, isIOS, isIPad, isIPhone, isLegacySafari, isMobile, isSamsung
21
21
  export { getItemAsync, removeItemAsync, setItemAsync } from './localStorageAsync.js';
22
22
  export { bufferToBase64 } from './bufferToBase64.js';
23
23
  export { last } from './last.js';
24
+ export { FALLBACK_UNDEFINED, retryableFn } from './retryableFn.js';
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('../_virtual/_tslib.cjs');
6
+
7
+ const FALLBACK_UNDEFINED = 'FALLBACK_UNDEFINED';
8
+ const retryableFn = (fn, options = {}) => _tslib.__awaiter(void 0, void 0, void 0, function* () {
9
+ const { maxRetries = 3, currentRetry = 0, timeoutMs = 100, fallbackValue = new Error('Max retries reached'), retryStrategy = 'timeout-only', } = options;
10
+ const timeoutPromise = new Promise((_, reject) => {
11
+ setTimeout(() => {
12
+ reject(new Error('Timeout'));
13
+ }, timeoutMs);
14
+ });
15
+ try {
16
+ const result = yield Promise.race([fn(), timeoutPromise]);
17
+ return result;
18
+ }
19
+ catch (err) {
20
+ if (currentRetry >= maxRetries) {
21
+ if (fallbackValue instanceof Error) {
22
+ throw fallbackValue;
23
+ }
24
+ else if (fallbackValue === FALLBACK_UNDEFINED) {
25
+ return undefined;
26
+ }
27
+ return fallbackValue;
28
+ }
29
+ const isTimeout = err.message === 'Timeout';
30
+ const shouldRetry = retryStrategy === 'timeout-and-rejection' ||
31
+ (retryStrategy === 'timeout-only' && isTimeout) ||
32
+ (retryStrategy === 'rejection-only' && !isTimeout);
33
+ if (!shouldRetry) {
34
+ if (fallbackValue instanceof Error) {
35
+ throw err;
36
+ }
37
+ else if (fallbackValue === FALLBACK_UNDEFINED) {
38
+ return undefined;
39
+ }
40
+ return fallbackValue;
41
+ }
42
+ return retryableFn(fn, {
43
+ currentRetry: currentRetry + 1,
44
+ fallbackValue,
45
+ maxRetries,
46
+ retryStrategy,
47
+ timeoutMs,
48
+ });
49
+ }
50
+ });
51
+
52
+ exports.FALLBACK_UNDEFINED = FALLBACK_UNDEFINED;
53
+ exports.retryableFn = retryableFn;
@@ -0,0 +1,14 @@
1
+ export declare const FALLBACK_UNDEFINED = "FALLBACK_UNDEFINED";
2
+ export type RetryableFnOptions<T> = {
3
+ /** Maximum number of retry attempts before returning fallbackValue */
4
+ maxRetries?: number;
5
+ /** Internal only. Do not use */
6
+ currentRetry?: number;
7
+ /** Timeout in ms to wait before executing retryStrategy */
8
+ timeoutMs?: number;
9
+ /** The value to fallback to when function rejects or times out */
10
+ fallbackValue?: T | Error | typeof FALLBACK_UNDEFINED;
11
+ /** Configure whether the fn should be retried only on timeout, only on rejection, or both */
12
+ retryStrategy?: 'timeout-only' | 'rejection-only' | 'timeout-and-rejection';
13
+ };
14
+ export declare const retryableFn: <T>(fn: () => Promise<T>, options?: RetryableFnOptions<T>) => Promise<T>;
@@ -0,0 +1,48 @@
1
+ import { __awaiter } from '../_virtual/_tslib.js';
2
+
3
+ const FALLBACK_UNDEFINED = 'FALLBACK_UNDEFINED';
4
+ const retryableFn = (fn, options = {}) => __awaiter(void 0, void 0, void 0, function* () {
5
+ const { maxRetries = 3, currentRetry = 0, timeoutMs = 100, fallbackValue = new Error('Max retries reached'), retryStrategy = 'timeout-only', } = options;
6
+ const timeoutPromise = new Promise((_, reject) => {
7
+ setTimeout(() => {
8
+ reject(new Error('Timeout'));
9
+ }, timeoutMs);
10
+ });
11
+ try {
12
+ const result = yield Promise.race([fn(), timeoutPromise]);
13
+ return result;
14
+ }
15
+ catch (err) {
16
+ if (currentRetry >= maxRetries) {
17
+ if (fallbackValue instanceof Error) {
18
+ throw fallbackValue;
19
+ }
20
+ else if (fallbackValue === FALLBACK_UNDEFINED) {
21
+ return undefined;
22
+ }
23
+ return fallbackValue;
24
+ }
25
+ const isTimeout = err.message === 'Timeout';
26
+ const shouldRetry = retryStrategy === 'timeout-and-rejection' ||
27
+ (retryStrategy === 'timeout-only' && isTimeout) ||
28
+ (retryStrategy === 'rejection-only' && !isTimeout);
29
+ if (!shouldRetry) {
30
+ if (fallbackValue instanceof Error) {
31
+ throw err;
32
+ }
33
+ else if (fallbackValue === FALLBACK_UNDEFINED) {
34
+ return undefined;
35
+ }
36
+ return fallbackValue;
37
+ }
38
+ return retryableFn(fn, {
39
+ currentRetry: currentRetry + 1,
40
+ fallbackValue,
41
+ maxRetries,
42
+ retryStrategy,
43
+ timeoutMs,
44
+ });
45
+ }
46
+ });
47
+
48
+ export { FALLBACK_UNDEFINED, retryableFn };