tiendu 0.4.0 → 0.5.0

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/retry.mjs ADDED
@@ -0,0 +1,69 @@
1
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
2
+
3
+ /**
4
+ * @template T
5
+ * @param {(attempt: number) => Promise<T>} operation
6
+ * @param {{
7
+ * attempts?: number,
8
+ * baseDelayMs?: number,
9
+ * maxDelayMs?: number,
10
+ * shouldRetry?: (result: T, attempt: number) => boolean,
11
+ * shouldRetryError?: (error: unknown, attempt: number) => boolean,
12
+ * onRetry?: (result: T, nextAttempt: number, delayMs: number) => void | Promise<void>,
13
+ * onRetryError?: (error: unknown, nextAttempt: number, delayMs: number) => void | Promise<void>,
14
+ * }} [options]
15
+ * @returns {Promise<T>}
16
+ */
17
+ export const retryAsync = async (operation, options = {}) => {
18
+ const {
19
+ attempts = 3,
20
+ baseDelayMs = 300,
21
+ maxDelayMs = 2000,
22
+ shouldRetry = () => false,
23
+ shouldRetryError = () => true,
24
+ onRetry,
25
+ onRetryError,
26
+ } = options;
27
+
28
+ let lastResult;
29
+ let lastError;
30
+
31
+ const getDelayMs = (attempt) =>
32
+ Math.min(maxDelayMs, baseDelayMs * 2 ** (attempt - 1)) +
33
+ Math.floor(Math.random() * 100);
34
+
35
+ for (let attempt = 1; attempt <= attempts; attempt += 1) {
36
+ try {
37
+ const result = await operation(attempt);
38
+ lastResult = result;
39
+
40
+ if (!shouldRetry(result, attempt) || attempt === attempts) {
41
+ return result;
42
+ }
43
+
44
+ const delayMs = getDelayMs(attempt);
45
+
46
+ if (onRetry) {
47
+ await onRetry(result, attempt + 1, delayMs);
48
+ }
49
+
50
+ await sleep(delayMs);
51
+ } catch (error) {
52
+ lastError = error;
53
+
54
+ if (!shouldRetryError(error, attempt) || attempt === attempts) {
55
+ throw error;
56
+ }
57
+
58
+ const delayMs = getDelayMs(attempt);
59
+ if (onRetryError) {
60
+ await onRetryError(error, attempt + 1, delayMs);
61
+ }
62
+
63
+ await sleep(delayMs);
64
+ }
65
+ }
66
+
67
+ if (lastError) throw lastError;
68
+ return lastResult;
69
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiendu",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "CLI para desarrollar y publicar temas en Tiendu",
5
5
  "type": "module",
6
6
  "bin": {
@@ -13,7 +13,7 @@
13
13
  "README.md"
14
14
  ],
15
15
  "scripts": {
16
- "dev": "node bin/tiendu.mjs"
16
+ "dev": "node bin/tiendu.js"
17
17
  },
18
18
  "engines": {
19
19
  "node": ">=20"