@cacheable/memoize
Advanced tools
+1
-102
@@ -1,102 +0,1 @@ | ||
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // src/index.ts | ||
| var index_exports = {}; | ||
| __export(index_exports, { | ||
| createWrapKey: () => createWrapKey, | ||
| getOrSet: () => getOrSet, | ||
| wrap: () => wrap, | ||
| wrapSync: () => wrapSync | ||
| }); | ||
| module.exports = __toCommonJS(index_exports); | ||
| var import_utils = require("@cacheable/utils"); | ||
| function wrapSync(function_, options) { | ||
| const { ttl, keyPrefix, cache } = options; | ||
| return (...arguments_) => { | ||
| let cacheKey = createWrapKey(function_, arguments_, keyPrefix); | ||
| if (options.createKey) { | ||
| cacheKey = options.createKey(function_, arguments_, options); | ||
| } | ||
| let value = cache.get(cacheKey); | ||
| if (value === void 0) { | ||
| try { | ||
| value = function_(...arguments_); | ||
| cache.set(cacheKey, value, ttl); | ||
| } catch (error) { | ||
| cache.emit("error", error); | ||
| if (options.cacheErrors) { | ||
| cache.set(cacheKey, error, ttl); | ||
| } | ||
| } | ||
| } | ||
| return value; | ||
| }; | ||
| } | ||
| async function getOrSet(key, function_, options) { | ||
| const keyString = typeof key === "function" ? key(options) : key; | ||
| let value = await options.cache.get(keyString); | ||
| if (value === void 0) { | ||
| const cacheId = options.cacheId ?? "default"; | ||
| const coalesceKey = `${cacheId}::${keyString}`; | ||
| value = await (0, import_utils.coalesceAsync)(coalesceKey, async () => { | ||
| try { | ||
| const result = await function_(); | ||
| await options.cache.set(keyString, result, options.ttl); | ||
| return result; | ||
| } catch (error) { | ||
| options.cache.emit("error", error); | ||
| if (options.cacheErrors) { | ||
| await options.cache.set(keyString, error, options.ttl); | ||
| } | ||
| if (options.throwErrors) { | ||
| throw error; | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| return value; | ||
| } | ||
| function wrap(function_, options) { | ||
| const { keyPrefix, cache } = options; | ||
| return async (...arguments_) => { | ||
| let cacheKey = createWrapKey(function_, arguments_, keyPrefix); | ||
| if (options.createKey) { | ||
| cacheKey = options.createKey(function_, arguments_, options); | ||
| } | ||
| return getOrSet( | ||
| cacheKey, | ||
| async () => function_(...arguments_), | ||
| options | ||
| ); | ||
| }; | ||
| } | ||
| function createWrapKey(function_, arguments_, keyPrefix) { | ||
| if (!keyPrefix) { | ||
| return `${function_.name}::${(0, import_utils.hash)(arguments_)}`; | ||
| } | ||
| return `${keyPrefix}::${function_.name}::${(0, import_utils.hash)(arguments_)}`; | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| createWrapKey, | ||
| getOrSet, | ||
| wrap, | ||
| wrapSync | ||
| }); | ||
| "use strict";var p=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var O=(t,e)=>{for(var r in e)p(t,r,{get:e[r],enumerable:!0})},x=(t,e,r,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of d(e))!f.call(t,n)&&n!==r&&p(t,n,{get:()=>e[n],enumerable:!(a=h(e,n))||a.enumerable});return t};var b=t=>x(p({},"__esModule",{value:!0}),t);var v={};O(v,{createWrapKey:()=>u,getOrSet:()=>g,wrap:()=>K,wrapSync:()=>m});module.exports=b(v);var y=require("@cacheable/utils");function m(t,e){let{ttl:r,keyPrefix:a,cache:n,serialize:c}=e;return(...s)=>{let i=u(t,s,{keyPrefix:a,serialize:c});e.createKey&&(i=e.createKey(t,s,e));let o=n.get(i);if(o===void 0)try{o=t(...s),n.set(i,o,r)}catch(l){n.emit("error",l),e.cacheErrors&&n.set(i,l,r)}return o}}async function g(t,e,r){let a=typeof t=="function"?t(r):t,n=await r.cache.get(a);if(n===void 0){let s=`${r.cacheId??"default"}::${a}`;n=await(0,y.coalesceAsync)(s,async()=>{try{let i=await e();return await r.cache.set(a,i,r.ttl),i}catch(i){if(r.cache.emit("error",i),r.cacheErrors&&await r.cache.set(a,i,r.ttl),r.throwErrors)throw i}})}return n}function K(t,e){let{keyPrefix:r,serialize:a}=e;return async(...n)=>{let c=u(t,n,{keyPrefix:r,serialize:a});return e.createKey&&(c=e.createKey(t,n,e)),g(c,async()=>t(...n),e)}}function u(t,e,r){let{keyPrefix:a,serialize:n}=r||{};return a?`${a}::${t.name}::${(0,y.hash)(e,{serialize:n})}`:`${t.name}::${(0,y.hash)(e,{serialize:n})}`}0&&(module.exports={createWrapKey,getOrSet,wrap,wrapSync}); |
+9
-2
@@ -32,8 +32,11 @@ type CacheInstance = { | ||
| cacheId?: string; | ||
| serialize?: (object: any) => string; | ||
| }; | ||
| type WrapOptions = WrapFunctionOptions & { | ||
| cache: CacheInstance; | ||
| serialize?: (object: any) => string; | ||
| }; | ||
| type WrapSyncOptions = WrapFunctionOptions & { | ||
| cache: CacheSyncInstance; | ||
| serialize?: (object: any) => string; | ||
| }; | ||
@@ -44,4 +47,8 @@ type AnyFunction = (...arguments_: any[]) => any; | ||
| declare function wrap<T>(function_: AnyFunction, options: WrapOptions): AnyFunction; | ||
| declare function createWrapKey(function_: AnyFunction, arguments_: any[], keyPrefix?: string): string; | ||
| type CreateWrapKeyOptions = { | ||
| keyPrefix?: string; | ||
| serialize?: (object: any) => string; | ||
| }; | ||
| declare function createWrapKey(function_: AnyFunction, arguments_: any[], options?: CreateWrapKeyOptions): string; | ||
| export { type AnyFunction, type CacheInstance, type CacheSyncInstance, type CreateWrapKey, type GetOrSetFunctionOptions, type GetOrSetKey, type GetOrSetOptions, type WrapFunctionOptions, type WrapOptions, type WrapSyncOptions, createWrapKey, getOrSet, wrap, wrapSync }; | ||
| export { type AnyFunction, type CacheInstance, type CacheSyncInstance, type CreateWrapKey, type CreateWrapKeyOptions, type GetOrSetFunctionOptions, type GetOrSetKey, type GetOrSetOptions, type WrapFunctionOptions, type WrapOptions, type WrapSyncOptions, createWrapKey, getOrSet, wrap, wrapSync }; |
+9
-2
@@ -32,8 +32,11 @@ type CacheInstance = { | ||
| cacheId?: string; | ||
| serialize?: (object: any) => string; | ||
| }; | ||
| type WrapOptions = WrapFunctionOptions & { | ||
| cache: CacheInstance; | ||
| serialize?: (object: any) => string; | ||
| }; | ||
| type WrapSyncOptions = WrapFunctionOptions & { | ||
| cache: CacheSyncInstance; | ||
| serialize?: (object: any) => string; | ||
| }; | ||
@@ -44,4 +47,8 @@ type AnyFunction = (...arguments_: any[]) => any; | ||
| declare function wrap<T>(function_: AnyFunction, options: WrapOptions): AnyFunction; | ||
| declare function createWrapKey(function_: AnyFunction, arguments_: any[], keyPrefix?: string): string; | ||
| type CreateWrapKeyOptions = { | ||
| keyPrefix?: string; | ||
| serialize?: (object: any) => string; | ||
| }; | ||
| declare function createWrapKey(function_: AnyFunction, arguments_: any[], options?: CreateWrapKeyOptions): string; | ||
| export { type AnyFunction, type CacheInstance, type CacheSyncInstance, type CreateWrapKey, type GetOrSetFunctionOptions, type GetOrSetKey, type GetOrSetOptions, type WrapFunctionOptions, type WrapOptions, type WrapSyncOptions, createWrapKey, getOrSet, wrap, wrapSync }; | ||
| export { type AnyFunction, type CacheInstance, type CacheSyncInstance, type CreateWrapKey, type CreateWrapKeyOptions, type GetOrSetFunctionOptions, type GetOrSetKey, type GetOrSetOptions, type WrapFunctionOptions, type WrapOptions, type WrapSyncOptions, createWrapKey, getOrSet, wrap, wrapSync }; |
+1
-74
@@ -1,74 +0,1 @@ | ||
| // src/index.ts | ||
| import { coalesceAsync, hash } from "@cacheable/utils"; | ||
| function wrapSync(function_, options) { | ||
| const { ttl, keyPrefix, cache } = options; | ||
| return (...arguments_) => { | ||
| let cacheKey = createWrapKey(function_, arguments_, keyPrefix); | ||
| if (options.createKey) { | ||
| cacheKey = options.createKey(function_, arguments_, options); | ||
| } | ||
| let value = cache.get(cacheKey); | ||
| if (value === void 0) { | ||
| try { | ||
| value = function_(...arguments_); | ||
| cache.set(cacheKey, value, ttl); | ||
| } catch (error) { | ||
| cache.emit("error", error); | ||
| if (options.cacheErrors) { | ||
| cache.set(cacheKey, error, ttl); | ||
| } | ||
| } | ||
| } | ||
| return value; | ||
| }; | ||
| } | ||
| async function getOrSet(key, function_, options) { | ||
| const keyString = typeof key === "function" ? key(options) : key; | ||
| let value = await options.cache.get(keyString); | ||
| if (value === void 0) { | ||
| const cacheId = options.cacheId ?? "default"; | ||
| const coalesceKey = `${cacheId}::${keyString}`; | ||
| value = await coalesceAsync(coalesceKey, async () => { | ||
| try { | ||
| const result = await function_(); | ||
| await options.cache.set(keyString, result, options.ttl); | ||
| return result; | ||
| } catch (error) { | ||
| options.cache.emit("error", error); | ||
| if (options.cacheErrors) { | ||
| await options.cache.set(keyString, error, options.ttl); | ||
| } | ||
| if (options.throwErrors) { | ||
| throw error; | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| return value; | ||
| } | ||
| function wrap(function_, options) { | ||
| const { keyPrefix, cache } = options; | ||
| return async (...arguments_) => { | ||
| let cacheKey = createWrapKey(function_, arguments_, keyPrefix); | ||
| if (options.createKey) { | ||
| cacheKey = options.createKey(function_, arguments_, options); | ||
| } | ||
| return getOrSet( | ||
| cacheKey, | ||
| async () => function_(...arguments_), | ||
| options | ||
| ); | ||
| }; | ||
| } | ||
| function createWrapKey(function_, arguments_, keyPrefix) { | ||
| if (!keyPrefix) { | ||
| return `${function_.name}::${hash(arguments_)}`; | ||
| } | ||
| return `${keyPrefix}::${function_.name}::${hash(arguments_)}`; | ||
| } | ||
| export { | ||
| createWrapKey, | ||
| getOrSet, | ||
| wrap, | ||
| wrapSync | ||
| }; | ||
| import{coalesceAsync as l,hash as p}from"@cacheable/utils";function d(n,t){let{ttl:e,keyPrefix:a,cache:r,serialize:c}=t;return(...s)=>{let i=u(n,s,{keyPrefix:a,serialize:c});t.createKey&&(i=t.createKey(n,s,t));let y=r.get(i);if(y===void 0)try{y=n(...s),r.set(i,y,e)}catch(o){r.emit("error",o),t.cacheErrors&&r.set(i,o,e)}return y}}async function g(n,t,e){let a=typeof n=="function"?n(e):n,r=await e.cache.get(a);if(r===void 0){let s=`${e.cacheId??"default"}::${a}`;r=await l(s,async()=>{try{let i=await t();return await e.cache.set(a,i,e.ttl),i}catch(i){if(e.cache.emit("error",i),e.cacheErrors&&await e.cache.set(a,i,e.ttl),e.throwErrors)throw i}})}return r}function f(n,t){let{keyPrefix:e,serialize:a}=t;return async(...r)=>{let c=u(n,r,{keyPrefix:e,serialize:a});return t.createKey&&(c=t.createKey(n,r,t)),g(c,async()=>n(...r),t)}}function u(n,t,e){let{keyPrefix:a,serialize:r}=e||{};return a?`${a}::${n.name}::${p(t,{serialize:r})}`:`${n.name}::${p(t,{serialize:r})}`}export{u as createWrapKey,g as getOrSet,f as wrap,d as wrapSync}; |
+3
-3
| { | ||
| "name": "@cacheable/memoize", | ||
| "version": "1.1.1", | ||
| "version": "2.0.0", | ||
| "description": "Memoization utilities for cacheable", | ||
@@ -50,6 +50,6 @@ "type": "module", | ||
| "dependencies": { | ||
| "@cacheable/utils": "^1.1.1" | ||
| "@cacheable/utils": "^2.0.0" | ||
| }, | ||
| "scripts": { | ||
| "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean", | ||
| "build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean --minify", | ||
| "prepublish": "pnpm build", | ||
@@ -56,0 +56,0 @@ "lint": "biome check --write --error-on-warnings", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
16749
-12.04%62
-71.69%3
200%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
Updated