@rwaterman/utils 1.1.1 → 1.1.2
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/helpers/memoize.js +10 -1
- package/lib/helpers/retry.js +1 -1
- package/lib/helpers/timeout.js +3 -0
- package/package.json +1 -1
package/lib/helpers/memoize.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
|
+
function defaultKey(...args) {
|
|
2
|
+
return JSON.stringify(args, (_, value) => {
|
|
3
|
+
if (value === undefined)
|
|
4
|
+
return '\u0000undefined';
|
|
5
|
+
if (typeof value === 'bigint')
|
|
6
|
+
return `${value}n`;
|
|
7
|
+
return value;
|
|
8
|
+
});
|
|
9
|
+
}
|
|
1
10
|
export function memoize(fn, options = {}) {
|
|
2
11
|
const cache = new Map();
|
|
3
|
-
const keyOf = options.key ??
|
|
12
|
+
const keyOf = options.key ?? defaultKey;
|
|
4
13
|
return (...args) => {
|
|
5
14
|
const key = keyOf(...args);
|
|
6
15
|
const hit = cache.get(key);
|
package/lib/helpers/retry.js
CHANGED
|
@@ -6,7 +6,7 @@ export async function retry(params) {
|
|
|
6
6
|
for (let attempt = 0; attempt < numTimes; ++attempt) {
|
|
7
7
|
if (attempt > 0 && delayMs > 0)
|
|
8
8
|
await delay(Math.min(delayMs * factor ** (attempt - 1), maxDelayMs));
|
|
9
|
-
const { err, res } = await to(params.yourAsyncFn());
|
|
9
|
+
const { err, res } = await to((async () => params.yourAsyncFn())());
|
|
10
10
|
if (err === null)
|
|
11
11
|
return { res, errors };
|
|
12
12
|
errors.push(err);
|
package/lib/helpers/timeout.js
CHANGED
|
@@ -4,7 +4,10 @@ export class TimeoutError extends Error {
|
|
|
4
4
|
this.name = 'TimeoutError';
|
|
5
5
|
}
|
|
6
6
|
}
|
|
7
|
+
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
7
8
|
export async function timeout(promise, ms) {
|
|
9
|
+
if (ms > MAX_TIMER_MS)
|
|
10
|
+
return promise;
|
|
8
11
|
let timer;
|
|
9
12
|
const expiry = new Promise((_, reject) => {
|
|
10
13
|
timer = setTimeout(() => reject(new TimeoutError(ms)), ms);
|