Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
vsm-dictionary-cacher
Advanced tools
Wrapper around a VSM-dictionary, that manages a cache of string-match results
vsm-dictionary-cacher
augments a given VSM-dictionary with a layer of
caching functionality.
This speeds up requests for string-matches, refTerms, and dictInfos; and fixedTerms-preloading.
Install like (after also installing a vsm-dictionary-...
of choice) :
npm install vsm-dictionary-cacher
Then use like:
const Dictionary = require('vsm-dictionary-local'); // ...or any other VsmDictionary implementation.
const cacher = require('vsm-dictionary-cacher');
const CachedDictionary = cacher(Dictionary); // This makes a cache-enabled subclass.
var dict = new CachedDictionary(); // This makes an instance.
// This will query the Dictionary as normal, bypassing the cache.
dict.getMatchesForString('abc', {filter: {dictID: ['Foo']}}, (err, res) => {
console.dir(res);
// This will get the result from the cache, instead of re-running the query.
dict.getMatchesForString('abc', {filter: {dictID: ['Foo']}}, (err, res) => {
console.dir(res);
});
// These will *not* get their result from the cache.
dict.getMatchesForString('abc', {filter: {dictID: ['BAR']}}, (err, res) => {});
dict.getMatchesForString('QQQ', {filter: {dictID: ['Foo']}}, (err, res) => {});
// And similar behavior for the other three cached functions:
// - dict.getRefTerms({}, cb)
// - dict.getDictInfos({}, cb)
// - dict.loadFixedTerms([], {}, cb)
});
Specify options like:
const CachedDictionary = cacher(Dictionary, { maxItems: 1000 });
<script src="https://unpkg.com/vsm-dictionary-cacher@^1.0.0/dist/vsm-dictionary-cacher.min.js"></script>
after which it is accessible as the global variable VsmDictionaryCacher
.
Then it can be wrapped around a VsmDictionary, e.g. a VsmDictionaryLocal
, like:
....
<script src="https://unpkg.com/vsm-dictionary-local@^2.0.0/dist/vsm-dictionary-local.min.js"></script>
<script>
var dict = new (VsmDictionaryCacher(VsmDictionaryLocal)) (options);
dict.getMatchesForString(....
</script>
This package provides a factory function that accepts any VsmDictionary
(sub)class, and returns a (further) subclass of it,
which inserts cache handling code into several functions:
It speeds up requests for string-matches, to getMatchesForString()
, in
three ways:
It stores results from requests to this function in a cache.
These results are returned for subsequent requests
that use same search-string & options, instead of re-running the query.
This helps e.g.
vsm-autocomplete
avoid making duplicate requests to an online dictionary server.
It creates a more responsive autocomplete when the user types, and then backspaces.
It also prevents sending a second (or more) query to the underlying
datastore, if this query has the same search-string & options as an
ongoing query, whose results haven't arrived yet.
Instead, it puts such identical queries in a queue, and when the first one's
result comes in, it shares its error+result with the queued ones,
almost immediately (=on individual, next event-loops).
This helps e.g.
vsm-autocomplete
avoid making duplicate requests
when a user types and backspaces quickly, before any results could come in.
Note: when a query on the underlying storage fails, then no item will be
added to the cache, and no attempt to re-query will be made by the queued
ones.
This also means that on error, the same error would be returned by all
queued requests.
It can also remember for which strings there were no 'normal entry'-type
matches.
Then for subsequently queried strings, that start with such a 'no matches'
string, it can immediately return an empty list for the 'entry-matches' too.
(But it still checks for refTerm/number/etc-type matches).
This helps e.g.
vsm-autocomplete
avoid making unnecessary requests for search-strings, for which a substring already returned no entry-matches.
It maintains a cache for getRefTerms()
.
There are usually only a small number of refTerms, and they are just Strings.
Therefore, at a first call for anything, it queries all of them, and puts
them into a cache that is used for all further lookups.
This makes e.g.
vsm-autocomplete
not launch two queries per search-string (i.e. one for entries, and one for refTerms).
And for cacheEmpty-hits, it will even serve all data from either cache or computation.
It also catches and prevents any concurrent calls, until this cache data is received.
It partially maintains a cache for getDictInfos()
.
It caches all dictInfo-objects that are returned from any queries to the
underlying datastore, no matter what options
were used.
Then it may use this cache:
It uses it: only for requests that filter for a list of dictIDs
(i.e. having a options.filter.id
),
or for requests that ask all dictInfos (i.e. having no options.filter
).
• Then it collects all dictInfos with a cache-hit in the
dictInfos-cache,
• and sends a query only for the dictInfos that had a cache-miss,
and that are not marked as being-queried by another concurrent
getDictInfos()
call,
• after marking these as being-queried now too.
+ Note: queries to the underlying datastore are made, explicitly
unpaginated (i.e. with options = { perPage: Number.MAX_VALUE, ... }
).
When all dictInfos that it depends on have come in, (possibly as partial results from several other concurrent calls), then it finally returns its own, complete, assembled result.
The implementation of this is not trivial. But it ensures that for a
large amount of concurrent requests (which may be launched when an app
starts up), no dictID is queried twice.
And it works even if clearCache()
(see below) is called during this
process.
A
vsm-autocomplete
needs a corresponding dictInfo for each of its string-matches.
So if its string-matches already came from cache-hits, then the above makes all its other data also come only from cache.
If a
vsm-box
with a template, or severalvsm-box
es loaded on a same page, would launch multiple concurrent requests for dictInfos, then this caching may result in a lot less queries to the underlying datastore.
It enhances the cache management of loadFixedTerms()
.
getEntries()
, and puts the processed
results in its own simple cache (in the VsmDictionary parent class).vsm-box
es based on a template,
then each of them may call the (shared) VsmDictionary's loadFixedTerms()
and launch a query. This would query and add results to the VsmDictionary's
fixedTermsCache
, no matter whether these results were in there already.• It maintains a list of fixedTerms (idts
) that have ever been
queried.
• It removes, from a request's idts
-argument, any that were queried
before,
• and then launches the query only for the remaining idts
,
• after marking them as 'pending/having-been-queried-now',
• so that concurrent calls can be prevented from requesting anything
twice.
Note: the options.z
, for z-object-pruning, is not taken into account here,
because VsmDictionary's fixedTerms-cache does so neither.
This prevents that
loadFixedTerms()
is called multiple times for the same data, when loading multiplevsm-box
es with the same template.
An options object can be given as second argument to the factory function (see example above), with these optional properties:
maxItems
: {Number}:getMatchesForString()
query (which is
often a list of match-objects).predictEmpties
: {Boolean}:true
, then it keeps a list of strings (per options-object) for which
getEntryMatchesForString()
returned no results (i.e.: { items: [] }
).{ items: [] }
for 'abc'.true
.
maxItems
does not apply to this collection of strings either.clearCache()
(see below).getEntryMatchesForString()
, not in
getMatchesForString()
, because the latter may still add 'extra' matches
(refTerm/number/fixedTerm), other than the 'entry'-type matches.
An extra function is added to the VsmDictionary subclass:
clearCache()
:predictEmpties
.This project is licensed under the AGPL license - see LICENSE.md.
FAQs
Wrapper around a VSM-dictionary, that manages a cache of string-match results
The npm package vsm-dictionary-cacher receives a total of 4 weekly downloads. As such, vsm-dictionary-cacher popularity was classified as not popular.
We found that vsm-dictionary-cacher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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 malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.