Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Read-through, in-memory, least recently used (lru) cache
First, instantiate the cache – passing options if necessary.
const Bluecache = require('bluecache');
const options = {
max: 500,
maxAge: '1h'
};
const cache = Bluecache(options);
Traditional cache "getting" and "setting" takes place within a single call, promoting functional use. The cache
instance is a Promise-returning function which takes two parameters: a cache key and a priming value.
cache(Promise.resolve('dinosaur'), (key) => {
console.log(`the invoked key was: ${key}`); // "the invoked key was: dinosaur"
return Promise.resolve('rar');
})
.then((value) => {
console.log(`the resolved value is: ${value}`); // "the resolved value is: rar"
});
Options are passed to lru-cache at instantiation:
max
: The maximum size of the cache, checked by applying the length function to all values in the cachemaxAge
: Maximum age in ms (or a valid ms expression); lazily enforced; expired keys will return undefined
length
: Function called to calculate the length of stored items (e.g. function (n) { return n.length; }
); defaults to function (n) { return 1; }
dispose
: Function called on items immediately before they are dropped from the cache; called with parameters (key
, value
)stale
: Allow the cache to return a stale (expired via maxAge
) value before it is deletedIn addition, the following options are specific to bluecache:
pruneInterval
: Interval at which the cache will pro-actively remove stale entries; by default stale items remain in memory until the next attempted readNote: the underlying cache stores a memo for the promised value and a default length of 1 while the value is being resolved. After the value is first resolved, the length
is updated to reflect the desired options.length
passed at instantiation. (In short, peak cache "max" may exceed the specified max
while values are being resolved.)
key
, primingValue
)Attempts to get the current value of key
from the cache. If the key
was previously used, the "recently-used"-ness of the key
is updated and the cached value is returned. If the key
does not exist, the primingValue
is determined and the underlying cache value is set. If the primingValue
is a function, it is invoked with the resolved key
(resolved as a String
) as its single argument.
Both key
and primingValue
can be a Boolean
, Number
, String
, Symbol
, Object
, a Function
that returns one of these primitives, or a Promise
that resolves to one of these primitives.
By immediately caching and returning a Promise
, the cache avoids a stampede for the target primingValue
. However, a stampede may occur for a key
because it resolves on each cache call. If you plan to asynchronously resolve the key
, consider caching the key
function as well.
A rejected Promise
is returned if key
is empty (null
or undefined
) or if there is an error resolving the primingValue
.
key
)Returns a Promise
that resolves to undefined
after deleting key
from the cache.
Clears the cache entirely, throwing away all values. Returns a Promise
that resolves to null
after the cache has been reset.
eventName
, eventHandler
)eventName
is a string, corresponding to a supported event. eventHandler
is a function which responds to the data provided by the target event.
cache.on('cache:hit', (data) => {
console.log(`The cache took ${data.ms} milliseconds to respond to key: ${data.key}.`);
});
The cache instance is also an event emitter which provides an on
method against which the implementing application can listen for the below events.
cache:hit
{
'key': <String>,
'ms': <Number:Integer:Milliseconds>
}
Note: ms
is milliseconds elapsed between cache invocation and final resolution of the cached value.
cache:miss
{
'key': <String>,
'ms': <Number:Integer:Milliseconds>
}
Note: ms
is milliseconds elapsed between cache invocation and final resolution of the priming value.
Object
options in favor of ms expressionpruneInterval
option_lrucache
are considered breaking)#reset
instance method which was abused in practice; use a key-specific #del
insteadPRs are welcome! For bugs, please include a failing test which passes when your PR is applied.
Thanks @ismriv!
To run the unit test suite:
npm test
Or, to determine unit test coverage:
npm run coverage
This project maintains 100% coverage of statements, branches, and functions.
FAQs
Read-through, in-memory, least recently used (lru) cache
The npm package bluecache receives a total of 449 weekly downloads. As such, bluecache popularity was classified as not popular.
We found that bluecache 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
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.