Installations
npm install recache
Releases
Unable to fetch releases
Developer
micnic
Developer Guide
Module System
CommonJS
Min. Node Version
>=10.0
Typescript Support
Yes
Node Version
16.10.0
NPM Version
7.24.0
Statistics
26 Stars
18 Commits
1 Watching
4 Branches
1 Contributors
Updated on 30 Jul 2023
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
208,493
Last day
14.3%
8
Compared to previous day
Last week
34.1%
59
Compared to previous week
Last month
-5.8%
227
Compared to previous month
Last year
-41.4%
2,908
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
recache
is a file system cache, it watches recursively a directory tree or a
file content and updates the data on changes, optionally it may provide the
content and the stats for each element stored in the cache.
Comparison with fs.watch
and fs.watchFile
recache
provides a more reliable cross-platform way to deal with file system
watching, subdirectories are being watched recursively. Under the hood recache
uses fs.watch()
and fs.watchFile()
, but alleviates the different caveats
that node fs watch methods have by checking fs stats.
Comparison with chokidar
In general chokidar
and recache
solve the same problems related to
unreliable default node fs watching methods. While chokidar
is more focused on
watching for fs changes in multiple locations and calling functions on specific
events, recache
is about watching a single directory tree or file and also
reading its paths, stats and content directly using only its API. recache
is a
pure JS solution and does not require any code compilation on installation.
Installation
npm i recache
API
recache(path[, options, callback])
path
: [ string ] - path for the root element to be cachedoptions
: [ object ] - cache configuration options, optionalfilter
: [ (path: string, stats: fs.Stats) => boolean ] - filter cached elements, by default all elements are cachedpersistent
: [ boolean ] - enable persistence of file system watchers, default is falsestore
: [ boolean ] - enable file content saving, default is false
callback
: [ (cache: recache.Cache) => void ] - function called when cache is ready
recache
will load directories and files provided in the path
argument.
Directories are recursively traversed, their content is loaded into the memory
and watched for changes. Following options can be defined for the cache:
filter
- a function which receives two arguments, the absolute path and file system stats of traversed directories or files, it has to return a boolean value to filter the elements should be cached, by default all elements from the provided path are cachedpersistent
- a boolean value to define if the process should continue while the cache is watching for changes, by default the created cache is not persistent.store
- a boolean value to enable storing the files content in the cache, by default files content is not saved in the cache, note that if this option is enabled files content should not surpass the available memory
The last argument is a callback function which is executed only once, in the
beginning when all the files and directories are loaded in the memory, the same
functionality can be achieved by listening for the ready
event.
Members
.data
Metadata about the cache, it is an empty object which should be used by the user.
.path
The absolute path of the root element of the cache.
.get([location])
location: string
Get the element from the provided relative location. If no location is provided
then the element from the root location is returned. If the required location
was no found then null
is returned. Returned element is object that has the
following readonly properties:
content
- the content of the element, for directories it is an array of strings which represents the name of the contained files or subdirectories, for files it is a buffer with the content of the file if it was set to be stored in the options ornull
otherwisedata
- the metadata of the element, it is an empty object which may be filled by the user of the cachelocation
- the relative location of the element to the root element of the cachepath
- the absolute path of the element in the file systemstats
- the file system stats of the element
.has(location)
location: string
Checks if the cache has an element on the provided location.
.list([filter])
filter: (location: string, index: number, list: string[]) => boolean
List all possible readable sources with an optional parameter to filter elements. If no filter parameter is provided then all readable elements of the cache are returned as an array of strings.
.start([callback])
callback: (cache: recache.Cache) => void
Starts the cache, this method is called automatically when the cache is created, it is not necessary to call it manually. It may be called only after the cache was stopped. This method accepts a callback function which is executed only once when the cache is started.
.stop()
Stops watching the file system for changes, this method is called automatically when the cache is destroyed, it is not necessary to call it manually.
.destroy()
Destroy the cached data.
Emitted events
error
- (error: Error) - when an error raised, provides the error as the callback argumentready
- (cache: recache.Cache) - when all the files and directories are loaded for the first timeupdate
- (cache: recache.Cache) - when one or multiple changes were made inside the cachedirectory
- (element: recache.CacheElement) - when a new directory is added to the cache, provides the directory object as the callback argumentfile
- (element: recache.CacheElement) - when a new file is added to the cache, provides the file object as the callback argumentchange
- (element: recache.CacheElement) - when a directory or a file is changed, provides the element object as the callback argumentunlink
- (element: recache.CacheElement) - when a directory or a file is removed, provides the element object as the callback argumentdestroy
- when the cache is destroyed, no parameter is provided
Example
1const recache = require('recache'); 2 3const cache = recache('/path/to/files', { 4 filter: (path, stats) => { // Filter cache elements 5 6 // Filter for hidden files 7 if (stats.isFile()) { 8 return /^(?!\.).+$/.test(path); 9 } 10 11 return false; 12 }, 13 persistent: true, // Make persistent cache 14 store: true // Enable file content storage 15}, (cache) => { 16 console.log('Cache ready!'); 17 18 // cache.read(...); 19}); 20 21cache.on('error', (error) => { 22 console.log('Something unexpected happened'); 23 console.log(error.stack); 24}); 25 26cache.on('ready', (cache) => { 27 console.log('Cache ready!'); 28 29 // cache.read(...); 30}); 31 32cache.on('update', (cache) => { 33 console.log('Cache updated!'); 34 35 // cache.read(...); 36}); 37 38cache.on('directory', (directory) => { 39 console.log('new directory added: "' + directory.location + '"'); 40}); 41 42cache.on('file', (file) => { 43 console.log('new file added: "' + file.location + '"'); 44}); 45 46cache.on('change', (element) => { 47 if (element.stats.isDirectory()) { 48 console.log('directory "' + element.location + '" changed'); 49 } else { 50 console.log('file "' + element.location + '" changed'); 51 } 52}); 53 54cache.on('unlink', (element) => { 55 if (element.stats.isDirectory()) { 56 console.log('directory "' + element.location + '" removed'); 57 } else { 58 console.log('file "' + element.location + '" removed'); 59 } 60}); 61 62cache.on('destroy', () => { 63 console.log('Cache destroyed!'); 64}); 65 66/* List cache elements */ 67cache.list(); 68/* 69File 70=> [''] 71 72Directory 73=> ['', '1.txt', '2.txt', '3.txt', ...] 74*/ 75 76/* Get cache elements */ 77cache.get(); 78/* 79File 80=> { 81 content: Buffer<00 01 02 ...> // file content 82 data: {}, // file metadata 83 location: '<root>/file', // file location 84 path: '/path/to/file', // file path 85 stats: { // file stats 86 atime: ... 87 ctime: ... 88 mtime: ... 89 ... 90 } 91 } 92 93Directory 94=> { 95 content: ['1.txt', '2.txt', '3.txt', ...], // directory content 96 data: {}, // directory metadata 97 location: '<root>/directory', // directory location 98 path: '/path/to/directory', // directory path 99 stats: { // directory stats 100 atime: ... 101 ctime: ... 102 mtime: ... 103 ... 104 } 105 } 106*/
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/18 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/micnic/recache/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/micnic/recache/main.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:33: update your workflow using https://app.stepsecurity.io/secureworkflow/micnic/recache/main.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:27
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 1 are checked with a SAST tool
Score
3.4
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More