@ls-stack/utils 2.5.0 → 2.6.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/dist/main.d.ts CHANGED
@@ -9,6 +9,7 @@
9
9
  ///<reference path="deepEqual.d.ts" />
10
10
  ///<reference path="enhancedMap.d.ts" />
11
11
  ///<reference path="exhaustiveMatch.d.ts" />
12
+ ///<reference path="internalUtils.d.ts" />
12
13
  ///<reference path="interpolate.d.ts" />
13
14
  ///<reference path="levenshtein.d.ts" />
14
15
  ///<reference path="main.d.ts" />
@@ -16,6 +17,7 @@
16
17
  ///<reference path="objUtils.d.ts" />
17
18
  ///<reference path="parallelAsyncCalls.d.ts" />
18
19
  ///<reference path="promiseUtils.d.ts" />
20
+ ///<reference path="retryOnError.d.ts" />
19
21
  ///<reference path="rsResult.d.ts" />
20
22
  ///<reference path="runShellCmd.d.ts" />
21
23
  ///<reference path="safeJson.d.ts" />
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/retryOnError.ts
21
+ var retryOnError_exports = {};
22
+ __export(retryOnError_exports, {
23
+ retryOnError: () => retryOnError
24
+ });
25
+ module.exports = __toCommonJS(retryOnError_exports);
26
+
27
+ // src/sleep.ts
28
+ function sleep(ms) {
29
+ return new Promise((resolve) => setTimeout(resolve, ms));
30
+ }
31
+
32
+ // src/retryOnError.ts
33
+ async function retryOnError(fn, maxRetries, options = {}, retry = 0) {
34
+ const {
35
+ delayBetweenRetriesMs,
36
+ retryCondition,
37
+ maxErrorDurationMs = 400
38
+ } = options;
39
+ if (options.debugId) {
40
+ if (retry > 0) {
41
+ console.info(
42
+ `Retrying ${options.debugId} (retry ${retry}/${maxRetries}) after error`
43
+ );
44
+ }
45
+ }
46
+ const startTime = Date.now();
47
+ try {
48
+ return await fn();
49
+ } catch (error) {
50
+ if (maxRetries > 0) {
51
+ const errorDuration = Date.now() - startTime;
52
+ const shouldRetry = retryCondition ? retryCondition(error) : true;
53
+ let maxErrorDurationMsToUse = maxErrorDurationMs;
54
+ if (typeof shouldRetry === "boolean") {
55
+ if (!shouldRetry) throw error;
56
+ } else {
57
+ maxErrorDurationMsToUse = shouldRetry.maxErrorDurationMs;
58
+ }
59
+ if (errorDuration > maxErrorDurationMsToUse) {
60
+ throw error;
61
+ }
62
+ if (delayBetweenRetriesMs) {
63
+ await sleep(
64
+ typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(retry) : delayBetweenRetriesMs
65
+ );
66
+ }
67
+ return retryOnError(fn, maxRetries - 1, options, retry + 1);
68
+ } else {
69
+ throw error;
70
+ }
71
+ }
72
+ }
73
+ // Annotate the CommonJS export names for ESM import in node:
74
+ 0 && (module.exports = {
75
+ retryOnError
76
+ });
@@ -0,0 +1,10 @@
1
+ declare function retryOnError<T>(fn: () => Promise<T>, maxRetries: number, options?: {
2
+ delayBetweenRetriesMs?: number | ((retry: number) => number);
3
+ retryCondition?: (error: unknown) => boolean | {
4
+ maxErrorDurationMs: number;
5
+ };
6
+ maxErrorDurationMs?: number;
7
+ debugId?: string;
8
+ }, retry?: number): Promise<T>;
9
+
10
+ export { retryOnError };
@@ -0,0 +1,10 @@
1
+ declare function retryOnError<T>(fn: () => Promise<T>, maxRetries: number, options?: {
2
+ delayBetweenRetriesMs?: number | ((retry: number) => number);
3
+ retryCondition?: (error: unknown) => boolean | {
4
+ maxErrorDurationMs: number;
5
+ };
6
+ maxErrorDurationMs?: number;
7
+ debugId?: string;
8
+ }, retry?: number): Promise<T>;
9
+
10
+ export { retryOnError };
@@ -0,0 +1,48 @@
1
+ import {
2
+ sleep
3
+ } from "./chunk-5DZT3Z5Z.js";
4
+
5
+ // src/retryOnError.ts
6
+ async function retryOnError(fn, maxRetries, options = {}, retry = 0) {
7
+ const {
8
+ delayBetweenRetriesMs,
9
+ retryCondition,
10
+ maxErrorDurationMs = 400
11
+ } = options;
12
+ if (options.debugId) {
13
+ if (retry > 0) {
14
+ console.info(
15
+ `Retrying ${options.debugId} (retry ${retry}/${maxRetries}) after error`
16
+ );
17
+ }
18
+ }
19
+ const startTime = Date.now();
20
+ try {
21
+ return await fn();
22
+ } catch (error) {
23
+ if (maxRetries > 0) {
24
+ const errorDuration = Date.now() - startTime;
25
+ const shouldRetry = retryCondition ? retryCondition(error) : true;
26
+ let maxErrorDurationMsToUse = maxErrorDurationMs;
27
+ if (typeof shouldRetry === "boolean") {
28
+ if (!shouldRetry) throw error;
29
+ } else {
30
+ maxErrorDurationMsToUse = shouldRetry.maxErrorDurationMs;
31
+ }
32
+ if (errorDuration > maxErrorDurationMsToUse) {
33
+ throw error;
34
+ }
35
+ if (delayBetweenRetriesMs) {
36
+ await sleep(
37
+ typeof delayBetweenRetriesMs === "function" ? delayBetweenRetriesMs(retry) : delayBetweenRetriesMs
38
+ );
39
+ }
40
+ return retryOnError(fn, maxRetries - 1, options, retry + 1);
41
+ } else {
42
+ throw error;
43
+ }
44
+ }
45
+ }
46
+ export {
47
+ retryOnError
48
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "2.5.0",
4
+ "version": "2.6.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"
@@ -74,6 +74,11 @@
74
74
  "types": "./dist/exhaustiveMatch.d.ts",
75
75
  "require": "./dist/exhaustiveMatch.cjs"
76
76
  },
77
+ "./internalUtils": {
78
+ "import": "./dist/internalUtils.js",
79
+ "types": "./dist/internalUtils.d.ts",
80
+ "require": "./dist/internalUtils.cjs"
81
+ },
77
82
  "./interpolate": {
78
83
  "import": "./dist/interpolate.js",
79
84
  "types": "./dist/interpolate.d.ts",
@@ -109,6 +114,11 @@
109
114
  "types": "./dist/promiseUtils.d.ts",
110
115
  "require": "./dist/promiseUtils.cjs"
111
116
  },
117
+ "./retryOnError": {
118
+ "import": "./dist/retryOnError.js",
119
+ "types": "./dist/retryOnError.d.ts",
120
+ "require": "./dist/retryOnError.cjs"
121
+ },
112
122
  "./rsResult": {
113
123
  "import": "./dist/rsResult.js",
114
124
  "types": "./dist/rsResult.d.ts",
@@ -225,6 +235,9 @@
225
235
  "exhaustiveMatch": [
226
236
  "./dist/exhaustiveMatch.d.ts"
227
237
  ],
238
+ "internalUtils": [
239
+ "./dist/internalUtils.d.ts"
240
+ ],
228
241
  "interpolate": [
229
242
  "./dist/interpolate.d.ts"
230
243
  ],
@@ -246,6 +259,9 @@
246
259
  "promiseUtils": [
247
260
  "./dist/promiseUtils.d.ts"
248
261
  ],
262
+ "retryOnError": [
263
+ "./dist/retryOnError.d.ts"
264
+ ],
249
265
  "rsResult": [
250
266
  "./dist/rsResult.d.ts"
251
267
  ],