@ls-stack/utils 2.5.0 → 2.7.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/cache.cjs ADDED
@@ -0,0 +1,125 @@
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/cache.ts
21
+ var cache_exports = {};
22
+ __export(cache_exports, {
23
+ cachedGetter: () => cachedGetter,
24
+ createCache: () => createCache
25
+ });
26
+ module.exports = __toCommonJS(cache_exports);
27
+
28
+ // src/assertions.ts
29
+ function isObject(value) {
30
+ return typeof value === "object" && value !== null && !Array.isArray(value);
31
+ }
32
+ function isPromise(value) {
33
+ return isObject(value) && "then" in value;
34
+ }
35
+
36
+ // src/cache.ts
37
+ function cachedGetter(getter) {
38
+ return {
39
+ get value() {
40
+ const value = getter();
41
+ Object.defineProperty(this, "value", { value });
42
+ return value;
43
+ }
44
+ };
45
+ }
46
+ function createCache({
47
+ maxCacheSize = 1e3,
48
+ maxItemAge,
49
+ expirationThrottle = 1e4
50
+ } = {}) {
51
+ const cache = /* @__PURE__ */ new Map();
52
+ let lastExpirationCheck = 0;
53
+ function checkExpiredItems() {
54
+ const now = Date.now();
55
+ if (!maxItemAge || now - lastExpirationCheck < expirationThrottle) return;
56
+ lastExpirationCheck = now;
57
+ const maxAgeMs = maxItemAge * 1e3;
58
+ for (const [key, item] of cache.entries()) {
59
+ if (now - item.timestamp > maxAgeMs) {
60
+ cache.delete(key);
61
+ }
62
+ }
63
+ }
64
+ function trimToSize() {
65
+ const currentSize = cache.size;
66
+ if (currentSize > maxCacheSize) {
67
+ const keysToRemove = currentSize - maxCacheSize;
68
+ const iterator = cache.keys();
69
+ for (let i = 0; i < keysToRemove; i++) {
70
+ const { value: key } = iterator.next();
71
+ if (key) {
72
+ cache.delete(key);
73
+ }
74
+ }
75
+ }
76
+ }
77
+ function isExpired(timestamp, now) {
78
+ return maxItemAge !== void 0 && now - timestamp > maxItemAge * 1e3;
79
+ }
80
+ function getOrInsert(cacheKey, val) {
81
+ const now = Date.now();
82
+ const entry = cache.get(cacheKey);
83
+ if (!entry || isExpired(entry.timestamp, now)) {
84
+ const value = val();
85
+ cache.set(cacheKey, { value, timestamp: now });
86
+ trimToSize();
87
+ checkExpiredItems();
88
+ return value;
89
+ }
90
+ return entry.value;
91
+ }
92
+ async function getOrInsertAsync(cacheKey, val) {
93
+ const entry = cache.get(cacheKey);
94
+ if (entry && isPromise(entry.value)) {
95
+ return entry.value;
96
+ }
97
+ const now = Date.now();
98
+ if (entry && !isExpired(entry.timestamp, now)) {
99
+ return entry.value;
100
+ }
101
+ const promise = val().then((result) => {
102
+ cache.set(cacheKey, { value: result, timestamp: Date.now() });
103
+ return result;
104
+ }).catch((error) => {
105
+ cache.delete(cacheKey);
106
+ throw error;
107
+ });
108
+ cache.set(cacheKey, { value: promise, timestamp: now });
109
+ trimToSize();
110
+ checkExpiredItems();
111
+ return promise;
112
+ }
113
+ return {
114
+ getOrInsert,
115
+ getOrInsertAsync,
116
+ clear: () => cache.clear(),
117
+ /** @internal */
118
+ "~cache": cache
119
+ };
120
+ }
121
+ // Annotate the CommonJS export names for ESM import in node:
122
+ 0 && (module.exports = {
123
+ cachedGetter,
124
+ createCache
125
+ });
@@ -0,0 +1,31 @@
1
+ declare function cachedGetter<T>(getter: () => T): {
2
+ value: T;
3
+ };
4
+ type Options = {
5
+ /**
6
+ * The maximum number of items in the cache.
7
+ * @default 1000
8
+ */
9
+ maxCacheSize?: number;
10
+ /**
11
+ * The maximum age of items in the cache in seconds.
12
+ */
13
+ maxItemAge?: number;
14
+ /**
15
+ * The maximum number of items to check for expiration in a single call.
16
+ * @default 10_000
17
+ */
18
+ expirationThrottle?: number;
19
+ };
20
+ declare function createCache({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): {
21
+ getOrInsert: <T>(cacheKey: string, val: () => T) => T;
22
+ getOrInsertAsync: <T>(cacheKey: string, val: () => Promise<T>) => Promise<T>;
23
+ clear: () => void;
24
+ /** @internal */
25
+ '~cache': Map<string, {
26
+ value: unknown;
27
+ timestamp: number;
28
+ }>;
29
+ };
30
+
31
+ export { cachedGetter, createCache };
@@ -0,0 +1,31 @@
1
+ declare function cachedGetter<T>(getter: () => T): {
2
+ value: T;
3
+ };
4
+ type Options = {
5
+ /**
6
+ * The maximum number of items in the cache.
7
+ * @default 1000
8
+ */
9
+ maxCacheSize?: number;
10
+ /**
11
+ * The maximum age of items in the cache in seconds.
12
+ */
13
+ maxItemAge?: number;
14
+ /**
15
+ * The maximum number of items to check for expiration in a single call.
16
+ * @default 10_000
17
+ */
18
+ expirationThrottle?: number;
19
+ };
20
+ declare function createCache({ maxCacheSize, maxItemAge, expirationThrottle, }?: Options): {
21
+ getOrInsert: <T>(cacheKey: string, val: () => T) => T;
22
+ getOrInsertAsync: <T>(cacheKey: string, val: () => Promise<T>) => Promise<T>;
23
+ clear: () => void;
24
+ /** @internal */
25
+ '~cache': Map<string, {
26
+ value: unknown;
27
+ timestamp: number;
28
+ }>;
29
+ };
30
+
31
+ export { cachedGetter, createCache };
package/dist/cache.js ADDED
@@ -0,0 +1,93 @@
1
+ import {
2
+ isPromise
3
+ } from "./chunk-4UGSP3L3.js";
4
+
5
+ // src/cache.ts
6
+ function cachedGetter(getter) {
7
+ return {
8
+ get value() {
9
+ const value = getter();
10
+ Object.defineProperty(this, "value", { value });
11
+ return value;
12
+ }
13
+ };
14
+ }
15
+ function createCache({
16
+ maxCacheSize = 1e3,
17
+ maxItemAge,
18
+ expirationThrottle = 1e4
19
+ } = {}) {
20
+ const cache = /* @__PURE__ */ new Map();
21
+ let lastExpirationCheck = 0;
22
+ function checkExpiredItems() {
23
+ const now = Date.now();
24
+ if (!maxItemAge || now - lastExpirationCheck < expirationThrottle) return;
25
+ lastExpirationCheck = now;
26
+ const maxAgeMs = maxItemAge * 1e3;
27
+ for (const [key, item] of cache.entries()) {
28
+ if (now - item.timestamp > maxAgeMs) {
29
+ cache.delete(key);
30
+ }
31
+ }
32
+ }
33
+ function trimToSize() {
34
+ const currentSize = cache.size;
35
+ if (currentSize > maxCacheSize) {
36
+ const keysToRemove = currentSize - maxCacheSize;
37
+ const iterator = cache.keys();
38
+ for (let i = 0; i < keysToRemove; i++) {
39
+ const { value: key } = iterator.next();
40
+ if (key) {
41
+ cache.delete(key);
42
+ }
43
+ }
44
+ }
45
+ }
46
+ function isExpired(timestamp, now) {
47
+ return maxItemAge !== void 0 && now - timestamp > maxItemAge * 1e3;
48
+ }
49
+ function getOrInsert(cacheKey, val) {
50
+ const now = Date.now();
51
+ const entry = cache.get(cacheKey);
52
+ if (!entry || isExpired(entry.timestamp, now)) {
53
+ const value = val();
54
+ cache.set(cacheKey, { value, timestamp: now });
55
+ trimToSize();
56
+ checkExpiredItems();
57
+ return value;
58
+ }
59
+ return entry.value;
60
+ }
61
+ async function getOrInsertAsync(cacheKey, val) {
62
+ const entry = cache.get(cacheKey);
63
+ if (entry && isPromise(entry.value)) {
64
+ return entry.value;
65
+ }
66
+ const now = Date.now();
67
+ if (entry && !isExpired(entry.timestamp, now)) {
68
+ return entry.value;
69
+ }
70
+ const promise = val().then((result) => {
71
+ cache.set(cacheKey, { value: result, timestamp: Date.now() });
72
+ return result;
73
+ }).catch((error) => {
74
+ cache.delete(cacheKey);
75
+ throw error;
76
+ });
77
+ cache.set(cacheKey, { value: promise, timestamp: now });
78
+ trimToSize();
79
+ checkExpiredItems();
80
+ return promise;
81
+ }
82
+ return {
83
+ getOrInsert,
84
+ getOrInsertAsync,
85
+ clear: () => cache.clear(),
86
+ /** @internal */
87
+ "~cache": cache
88
+ };
89
+ }
90
+ export {
91
+ cachedGetter,
92
+ createCache
93
+ };
package/dist/main.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  ///<reference path="arrayUtils.d.ts" />
2
2
  ///<reference path="assertions.d.ts" />
3
+ ///<reference path="cache.d.ts" />
3
4
  ///<reference path="castValues.d.ts" />
4
5
  ///<reference path="consoleFmt.d.ts" />
5
6
  ///<reference path="conversions.d.ts" />
@@ -9,6 +10,7 @@
9
10
  ///<reference path="deepEqual.d.ts" />
10
11
  ///<reference path="enhancedMap.d.ts" />
11
12
  ///<reference path="exhaustiveMatch.d.ts" />
13
+ ///<reference path="internalUtils.d.ts" />
12
14
  ///<reference path="interpolate.d.ts" />
13
15
  ///<reference path="levenshtein.d.ts" />
14
16
  ///<reference path="main.d.ts" />
@@ -16,6 +18,7 @@
16
18
  ///<reference path="objUtils.d.ts" />
17
19
  ///<reference path="parallelAsyncCalls.d.ts" />
18
20
  ///<reference path="promiseUtils.d.ts" />
21
+ ///<reference path="retryOnError.d.ts" />
19
22
  ///<reference path="rsResult.d.ts" />
20
23
  ///<reference path="runShellCmd.d.ts" />
21
24
  ///<reference path="safeJson.d.ts" />
@@ -1,11 +1,11 @@
1
+ import {
2
+ sleep
3
+ } from "./chunk-5DZT3Z5Z.js";
1
4
  import {
2
5
  Result,
3
6
  unknownToError
4
7
  } from "./chunk-KBFP7INB.js";
5
8
  import "./chunk-VAAMRG4K.js";
6
- import {
7
- sleep
8
- } from "./chunk-5DZT3Z5Z.js";
9
9
  import {
10
10
  invariant,
11
11
  isObject
@@ -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/dist/testUtils.js CHANGED
@@ -1,13 +1,13 @@
1
+ import {
2
+ omit,
3
+ pick
4
+ } from "./chunk-ZE3DLPMN.js";
1
5
  import {
2
6
  deepEqual
3
7
  } from "./chunk-JQFUKJU5.js";
4
8
  import {
5
9
  clampMin
6
10
  } from "./chunk-HTCYUMDR.js";
7
- import {
8
- omit,
9
- pick
10
- } from "./chunk-ZE3DLPMN.js";
11
11
  import {
12
12
  arrayWithPrevAndIndex,
13
13
  filterAndMap
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.7.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"
@@ -29,6 +29,11 @@
29
29
  "types": "./dist/assertions.d.ts",
30
30
  "require": "./dist/assertions.cjs"
31
31
  },
32
+ "./cache": {
33
+ "import": "./dist/cache.js",
34
+ "types": "./dist/cache.d.ts",
35
+ "require": "./dist/cache.cjs"
36
+ },
32
37
  "./castValues": {
33
38
  "import": "./dist/castValues.js",
34
39
  "types": "./dist/castValues.d.ts",
@@ -74,6 +79,11 @@
74
79
  "types": "./dist/exhaustiveMatch.d.ts",
75
80
  "require": "./dist/exhaustiveMatch.cjs"
76
81
  },
82
+ "./internalUtils": {
83
+ "import": "./dist/internalUtils.js",
84
+ "types": "./dist/internalUtils.d.ts",
85
+ "require": "./dist/internalUtils.cjs"
86
+ },
77
87
  "./interpolate": {
78
88
  "import": "./dist/interpolate.js",
79
89
  "types": "./dist/interpolate.d.ts",
@@ -109,6 +119,11 @@
109
119
  "types": "./dist/promiseUtils.d.ts",
110
120
  "require": "./dist/promiseUtils.cjs"
111
121
  },
122
+ "./retryOnError": {
123
+ "import": "./dist/retryOnError.js",
124
+ "types": "./dist/retryOnError.d.ts",
125
+ "require": "./dist/retryOnError.cjs"
126
+ },
112
127
  "./rsResult": {
113
128
  "import": "./dist/rsResult.js",
114
129
  "types": "./dist/rsResult.d.ts",
@@ -198,6 +213,9 @@
198
213
  "assertions": [
199
214
  "./dist/assertions.d.ts"
200
215
  ],
216
+ "cache": [
217
+ "./dist/cache.d.ts"
218
+ ],
201
219
  "castValues": [
202
220
  "./dist/castValues.d.ts"
203
221
  ],
@@ -225,6 +243,9 @@
225
243
  "exhaustiveMatch": [
226
244
  "./dist/exhaustiveMatch.d.ts"
227
245
  ],
246
+ "internalUtils": [
247
+ "./dist/internalUtils.d.ts"
248
+ ],
228
249
  "interpolate": [
229
250
  "./dist/interpolate.d.ts"
230
251
  ],
@@ -246,6 +267,9 @@
246
267
  "promiseUtils": [
247
268
  "./dist/promiseUtils.d.ts"
248
269
  ],
270
+ "retryOnError": [
271
+ "./dist/retryOnError.d.ts"
272
+ ],
249
273
  "rsResult": [
250
274
  "./dist/rsResult.d.ts"
251
275
  ],