@cacheable/utils 2.3.2 → 2.3.4
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/index.cjs +37 -5
- package/dist/index.d.cts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +37 -5
- package/package.json +7 -10
package/dist/index.cjs
CHANGED
|
@@ -315,24 +315,50 @@ function wrapSync(function_, options) {
|
|
|
315
315
|
}
|
|
316
316
|
async function getOrSet(key, function_, options) {
|
|
317
317
|
const keyString = typeof key === "function" ? key(options) : key;
|
|
318
|
-
let value
|
|
318
|
+
let value;
|
|
319
|
+
try {
|
|
320
|
+
value = await options.cache.get(keyString);
|
|
321
|
+
} catch (error) {
|
|
322
|
+
options.cache.emit("error", error);
|
|
323
|
+
if (options.throwErrors === true || options.throwErrors === "store") {
|
|
324
|
+
throw error;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
319
327
|
if (value === void 0) {
|
|
320
328
|
const cacheId = options.cacheId ?? "default";
|
|
321
329
|
const coalesceKey = `${cacheId}::${keyString}`;
|
|
322
330
|
value = await coalesceAsync(coalesceKey, async () => {
|
|
331
|
+
let result;
|
|
323
332
|
try {
|
|
324
|
-
|
|
325
|
-
|
|
333
|
+
try {
|
|
334
|
+
result = await function_();
|
|
335
|
+
} catch (error) {
|
|
336
|
+
throw new ErrorEnvelope(
|
|
337
|
+
error,
|
|
338
|
+
"function"
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
try {
|
|
342
|
+
await options.cache.set(keyString, result, options.ttl);
|
|
343
|
+
} catch (error) {
|
|
344
|
+
throw new ErrorEnvelope(error, "store");
|
|
345
|
+
}
|
|
326
346
|
return result;
|
|
327
|
-
} catch (
|
|
347
|
+
} catch (caught) {
|
|
348
|
+
const errorType = caught instanceof ErrorEnvelope ? caught.context : (
|
|
349
|
+
/* c8 ignore next 1 */
|
|
350
|
+
void 0
|
|
351
|
+
);
|
|
352
|
+
const error = caught instanceof ErrorEnvelope ? caught.error : caught;
|
|
328
353
|
options.cache.emit("error", error);
|
|
329
354
|
if (options.cacheErrors) {
|
|
330
355
|
await options.cache.set(keyString, error, options.ttl);
|
|
331
356
|
}
|
|
332
|
-
if (options.throwErrors) {
|
|
357
|
+
if (options.throwErrors === true || options.throwErrors === errorType) {
|
|
333
358
|
throw error;
|
|
334
359
|
}
|
|
335
360
|
}
|
|
361
|
+
return result;
|
|
336
362
|
});
|
|
337
363
|
}
|
|
338
364
|
return value;
|
|
@@ -361,6 +387,12 @@ function createWrapKey(function_, arguments_, options) {
|
|
|
361
387
|
}
|
|
362
388
|
return `${keyPrefix}::${function_.name}::${hashSync(arguments_, { serialize })}`;
|
|
363
389
|
}
|
|
390
|
+
var ErrorEnvelope = class {
|
|
391
|
+
constructor(error, context) {
|
|
392
|
+
this.error = error;
|
|
393
|
+
this.context = context;
|
|
394
|
+
}
|
|
395
|
+
};
|
|
364
396
|
|
|
365
397
|
// src/run-if-fn.ts
|
|
366
398
|
function runIfFn(valueOrFunction, ...arguments_) {
|
package/dist/index.d.cts
CHANGED
|
@@ -141,10 +141,17 @@ type CacheSyncInstance = {
|
|
|
141
141
|
emit: (event: string, ...args: any[]) => boolean;
|
|
142
142
|
};
|
|
143
143
|
type GetOrSetKey = string | ((options?: GetOrSetOptions) => string);
|
|
144
|
+
type GetOrSetThrowErrorsContext = "function" | "store";
|
|
144
145
|
type GetOrSetFunctionOptions = {
|
|
145
146
|
ttl?: number | string;
|
|
146
147
|
cacheErrors?: boolean;
|
|
147
|
-
|
|
148
|
+
/** Whether or not to throw errors:
|
|
149
|
+
* - `false` (default) - do not throw any errors
|
|
150
|
+
* - `true` - throw any error
|
|
151
|
+
* - `"function"` - only throw errors that occur in the provided function / setter
|
|
152
|
+
* - `"store"` - only throw errors that occur when getting/setting the cache
|
|
153
|
+
*/
|
|
154
|
+
throwErrors?: boolean | GetOrSetThrowErrorsContext;
|
|
148
155
|
};
|
|
149
156
|
type GetOrSetOptions = GetOrSetFunctionOptions & {
|
|
150
157
|
cacheId?: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -141,10 +141,17 @@ type CacheSyncInstance = {
|
|
|
141
141
|
emit: (event: string, ...args: any[]) => boolean;
|
|
142
142
|
};
|
|
143
143
|
type GetOrSetKey = string | ((options?: GetOrSetOptions) => string);
|
|
144
|
+
type GetOrSetThrowErrorsContext = "function" | "store";
|
|
144
145
|
type GetOrSetFunctionOptions = {
|
|
145
146
|
ttl?: number | string;
|
|
146
147
|
cacheErrors?: boolean;
|
|
147
|
-
|
|
148
|
+
/** Whether or not to throw errors:
|
|
149
|
+
* - `false` (default) - do not throw any errors
|
|
150
|
+
* - `true` - throw any error
|
|
151
|
+
* - `"function"` - only throw errors that occur in the provided function / setter
|
|
152
|
+
* - `"store"` - only throw errors that occur when getting/setting the cache
|
|
153
|
+
*/
|
|
154
|
+
throwErrors?: boolean | GetOrSetThrowErrorsContext;
|
|
148
155
|
};
|
|
149
156
|
type GetOrSetOptions = GetOrSetFunctionOptions & {
|
|
150
157
|
cacheId?: string;
|
package/dist/index.js
CHANGED
|
@@ -269,24 +269,50 @@ function wrapSync(function_, options) {
|
|
|
269
269
|
}
|
|
270
270
|
async function getOrSet(key, function_, options) {
|
|
271
271
|
const keyString = typeof key === "function" ? key(options) : key;
|
|
272
|
-
let value
|
|
272
|
+
let value;
|
|
273
|
+
try {
|
|
274
|
+
value = await options.cache.get(keyString);
|
|
275
|
+
} catch (error) {
|
|
276
|
+
options.cache.emit("error", error);
|
|
277
|
+
if (options.throwErrors === true || options.throwErrors === "store") {
|
|
278
|
+
throw error;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
273
281
|
if (value === void 0) {
|
|
274
282
|
const cacheId = options.cacheId ?? "default";
|
|
275
283
|
const coalesceKey = `${cacheId}::${keyString}`;
|
|
276
284
|
value = await coalesceAsync(coalesceKey, async () => {
|
|
285
|
+
let result;
|
|
277
286
|
try {
|
|
278
|
-
|
|
279
|
-
|
|
287
|
+
try {
|
|
288
|
+
result = await function_();
|
|
289
|
+
} catch (error) {
|
|
290
|
+
throw new ErrorEnvelope(
|
|
291
|
+
error,
|
|
292
|
+
"function"
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
try {
|
|
296
|
+
await options.cache.set(keyString, result, options.ttl);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
throw new ErrorEnvelope(error, "store");
|
|
299
|
+
}
|
|
280
300
|
return result;
|
|
281
|
-
} catch (
|
|
301
|
+
} catch (caught) {
|
|
302
|
+
const errorType = caught instanceof ErrorEnvelope ? caught.context : (
|
|
303
|
+
/* c8 ignore next 1 */
|
|
304
|
+
void 0
|
|
305
|
+
);
|
|
306
|
+
const error = caught instanceof ErrorEnvelope ? caught.error : caught;
|
|
282
307
|
options.cache.emit("error", error);
|
|
283
308
|
if (options.cacheErrors) {
|
|
284
309
|
await options.cache.set(keyString, error, options.ttl);
|
|
285
310
|
}
|
|
286
|
-
if (options.throwErrors) {
|
|
311
|
+
if (options.throwErrors === true || options.throwErrors === errorType) {
|
|
287
312
|
throw error;
|
|
288
313
|
}
|
|
289
314
|
}
|
|
315
|
+
return result;
|
|
290
316
|
});
|
|
291
317
|
}
|
|
292
318
|
return value;
|
|
@@ -315,6 +341,12 @@ function createWrapKey(function_, arguments_, options) {
|
|
|
315
341
|
}
|
|
316
342
|
return `${keyPrefix}::${function_.name}::${hashSync(arguments_, { serialize })}`;
|
|
317
343
|
}
|
|
344
|
+
var ErrorEnvelope = class {
|
|
345
|
+
constructor(error, context) {
|
|
346
|
+
this.error = error;
|
|
347
|
+
this.context = context;
|
|
348
|
+
}
|
|
349
|
+
};
|
|
318
350
|
|
|
319
351
|
// src/run-if-fn.ts
|
|
320
352
|
function runIfFn(valueOrFunction, ...arguments_) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cacheable/utils",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.4",
|
|
4
4
|
"description": "Cacheable Utilities for Caching Libraries",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -27,19 +27,16 @@
|
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"private": false,
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"hashery": "^1.
|
|
31
|
-
"keyv": "^5.
|
|
30
|
+
"hashery": "^1.3.0",
|
|
31
|
+
"keyv": "^5.6.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@
|
|
35
|
-
"@
|
|
36
|
-
"
|
|
37
|
-
"@vitest/coverage-v8": "^4.0.14",
|
|
38
|
-
"lru-cache": "^11.2.2",
|
|
34
|
+
"@faker-js/faker": "^10.2.0",
|
|
35
|
+
"@types/node": "^24.10.10",
|
|
36
|
+
"lru-cache": "^11.2.4",
|
|
39
37
|
"rimraf": "^6.1.2",
|
|
40
38
|
"tsup": "^8.5.1",
|
|
41
|
-
"typescript": "^5.9.3"
|
|
42
|
-
"vitest": "^4.0.14"
|
|
39
|
+
"typescript": "^5.9.3"
|
|
43
40
|
},
|
|
44
41
|
"keywords": [
|
|
45
42
|
"cacheable",
|