@wix/builder-utils 1.21.4 → 1.21.5
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/dist/bundles/debug/es/builder-utils.mjs +31 -1
- package/dist/bundles/debug/es/builder-utils.mjs.map +1 -1
- package/dist/bundles/debug/umd/builder-utils.js +30 -0
- package/dist/bundles/debug/umd/builder-utils.js.map +1 -1
- package/dist/bundles/es/builder-utils.mjs +215 -193
- package/dist/bundles/es/builder-utils.mjs.map +1 -1
- package/dist/bundles/umd/builder-utils.js +1 -1
- package/dist/bundles/umd/builder-utils.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/withRetry.d.ts +10 -0
- package/dist/withRetry.js +31 -0
- package/dist/withRetry.js.map +1 -0
- package/package.json +2 -2
- package/src/index.ts +2 -0
- package/src/withRetry.ts +43 -0
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA;AAClD,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAExD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface RetryOptions {
|
|
2
|
+
maxRetries: number;
|
|
3
|
+
getDelay: (attempt: number) => number;
|
|
4
|
+
shouldRetry?: (error: Error, attempt: number) => boolean;
|
|
5
|
+
onRetry?: (error: Error, attempt: number, delayMs: number) => void;
|
|
6
|
+
}
|
|
7
|
+
export declare const retryStrategies: {
|
|
8
|
+
exponentialJitter: (initialDelayMs: number) => (attempt: number) => number;
|
|
9
|
+
};
|
|
10
|
+
export declare function withRetry<T>(fn: () => Promise<T>, options: RetryOptions): Promise<T>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const retryStrategies = {
|
|
2
|
+
exponentialJitter: (initialDelayMs) => (attempt) => {
|
|
3
|
+
const base = initialDelayMs * Math.pow(2, attempt);
|
|
4
|
+
return base * (0.5 + Math.random() * 0.5);
|
|
5
|
+
},
|
|
6
|
+
};
|
|
7
|
+
function toError(error) {
|
|
8
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
9
|
+
}
|
|
10
|
+
export async function withRetry(fn, options) {
|
|
11
|
+
const { maxRetries, getDelay, shouldRetry = () => true, onRetry } = options;
|
|
12
|
+
let lastError;
|
|
13
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
14
|
+
try {
|
|
15
|
+
return await fn(); // eslint-disable-line no-await-in-loop
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
lastError = toError(error);
|
|
19
|
+
if (attempt < maxRetries && shouldRetry(lastError, attempt)) {
|
|
20
|
+
const delayMs = getDelay(attempt);
|
|
21
|
+
onRetry?.(lastError, attempt, delayMs);
|
|
22
|
+
await new Promise(resolve => setTimeout(resolve, delayMs)); // eslint-disable-line no-await-in-loop
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
throw lastError ?? new Error(`Operation failed after ${maxRetries} retries`);
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=withRetry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAA,CAAC,uCAAuC;YACtG,CAAC;iBAAM,CAAC;gBACJ,MAAK;YACT,CAAC;QACL,CAAC;IACL,CAAC;IAED,MAAM,SAAS,IAAI,IAAI,KAAK,CAAC,0BAA0B,UAAU,UAAU,CAAC,CAAA;AAChF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/builder-utils",
|
|
3
|
-
"version": "1.21.
|
|
3
|
+
"version": "1.21.5",
|
|
4
4
|
"description": "Builder utilities for merging props with custom logic",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -58,5 +58,5 @@
|
|
|
58
58
|
}
|
|
59
59
|
},
|
|
60
60
|
"module": "dist/bundles/es/builder-utils.mjs",
|
|
61
|
-
"falconPackageHash": "
|
|
61
|
+
"falconPackageHash": "783f0d1272c4a4c59a677cdae00429b0d5385f72797e577c19a69f9e"
|
|
62
62
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { mergeProps, removeElementProps } from './mergeUtils'
|
|
2
2
|
export { getSignalValueDeep } from './signalUtils'
|
|
3
|
+
export { withRetry, retryStrategies } from './withRetry'
|
|
4
|
+
export type { RetryOptions } from './withRetry'
|
|
3
5
|
export { routeToDynamicPage } from './routerUtils'
|
package/src/withRetry.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export interface RetryOptions {
|
|
2
|
+
maxRetries: number
|
|
3
|
+
getDelay: (attempt: number) => number
|
|
4
|
+
shouldRetry?: (error: Error, attempt: number) => boolean
|
|
5
|
+
onRetry?: (error: Error, attempt: number, delayMs: number) => void
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const retryStrategies = {
|
|
9
|
+
exponentialJitter:
|
|
10
|
+
(initialDelayMs: number) =>
|
|
11
|
+
(attempt: number): number => {
|
|
12
|
+
const base = initialDelayMs * Math.pow(2, attempt)
|
|
13
|
+
return base * (0.5 + Math.random() * 0.5)
|
|
14
|
+
},
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function toError(error: unknown): Error {
|
|
18
|
+
return error instanceof Error ? error : new Error(String(error))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function withRetry<T>(fn: () => Promise<T>, options: RetryOptions): Promise<T> {
|
|
22
|
+
const { maxRetries, getDelay, shouldRetry = () => true, onRetry } = options
|
|
23
|
+
|
|
24
|
+
let lastError: Error | undefined
|
|
25
|
+
|
|
26
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
27
|
+
try {
|
|
28
|
+
return await fn() // eslint-disable-line no-await-in-loop
|
|
29
|
+
} catch (error: unknown) {
|
|
30
|
+
lastError = toError(error)
|
|
31
|
+
|
|
32
|
+
if (attempt < maxRetries && shouldRetry(lastError, attempt)) {
|
|
33
|
+
const delayMs = getDelay(attempt)
|
|
34
|
+
onRetry?.(lastError, attempt, delayMs)
|
|
35
|
+
await new Promise(resolve => setTimeout(resolve, delayMs)) // eslint-disable-line no-await-in-loop
|
|
36
|
+
} else {
|
|
37
|
+
break
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
throw lastError ?? new Error(`Operation failed after ${maxRetries} retries`)
|
|
43
|
+
}
|