
Research
/Security News
Mini Shai-Hulud Campaign Hits Red Hat Cloud Services npm Packages
A mini Shai-Hulud campaign compromised Red Hat Cloud Services npm packages to steal developer and CI/CD secrets during installation.
safe-memory-cache
Advanced tools
Secure and size-limited in-memory cache for Node.js and browsers.
__proto__)delete (and no memory leaks caused by delete), plays well with garbage collector. But also doesn't drop the whole cache when full, frees up graduallyvar safeMemoryCache = require('safe-memory-cache')
var cache = safeMemoryCache(options)
cache.set("key1","value1")
cache.get("key1") == "value1"
cache.clear()
cache.get("key1") == undefined
cache.destroy() //only needed if you use maxTTL
If your engine supports Map, you can use the map based version. It doesn't need (nor have) sanitization on keys and it uses Maps as buckets for storage.
const safeMemoryCache = require('safe-memory-cache/map')
| name | type | required | description |
|---|---|---|---|
| limit | number | Y | Maximum number of items to store in cache. When cache length is close to the limit, oldest items are removed to make more room. |
| maxTTL | number | N | Time in miliseconds within which an element should no longer be in cache if it was not accessed. Actual time is approximate and will be less or equal maxTTL |
| strongSanitizer | bool | N | When set to true sanitizes keys to prevent memory issues in older JS engines. Defaults to false. No sanitization if you use the Map based version. |
| buckets | number | N | Overrides the number of buckets used internally. Default is 2 |
| cleanupListener | function | N | Calls the function with a storage bucket that's been removed |
If you expect N keys to be used most frequently, limit/buckets should be at least N.
Caching in general. When you need to cache results of some long running process or a lot of them and you don't have a strong requirement to keep every item until its exact expiry time.
Some older JavaScript engines had memory leaks triggered by object keys which contain control characters like - or .
strongSanitizer:true makes sure the keys are converted to alphanumeric characters only.
Prevents any potential hacks like overwriting __proto__ too.
Default sanitizer is only preventing __proto__ key from breaking the functionality.
Objects used for storing key/value pairs don't inherit from any of the native prototypes, nor Object
delete keyword removes fields from objects, but changes the hidden class of the object which takes up some memory. As a result, adding and deleting unique fields to a plain JavaScript object may cause memory consumption to grow. Some JavaScript engines had it leak memory in various ways.
Then how do you remove old items from cache if you can't use delete?
Cache consists of a number of buckets and the oldest bucket is removed when new room is needed. Therefore the oldest (1/buckets) of entries gets removed.
FAQs
Secure and size-limited in-memory cache for Node.js and browsers
We found that safe-memory-cache demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
/Security News
A mini Shai-Hulud campaign compromised Red Hat Cloud Services npm packages to steal developer and CI/CD secrets during installation.

Research
/Security News
The North Korean malware loader hides in a Packagist-listed package and its GitHub branch to fetch and execute remote code in a likely Contagious Interview-style lure.

Security News
The Rust project is moving toward formal rules on LLM use in contributions after months of internal debate over maintainer burden, code quality, and contributor experience.