first commit
This commit is contained in:
22
node_modules/flat-cache/LICENSE
generated
vendored
Normal file
22
node_modules/flat-cache/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) Roy Riojas and Jared Wray
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
74
node_modules/flat-cache/README.md
generated
vendored
Normal file
74
node_modules/flat-cache/README.md
generated
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
# flat-cache
|
||||
|
||||
> A stupidly simple key/value storage using files to persist the data
|
||||
|
||||
[](https://npmjs.org/package/flat-cache)
|
||||
[](https://github.com/jaredwray/flat-cache/actions/workflows/tests.yaml)
|
||||
[](https://codecov.io/github/jaredwray/flat-cache)
|
||||
[](https://npmjs.com/package/flat-cache)
|
||||
|
||||
## install
|
||||
|
||||
```bash
|
||||
npm i --save flat-cache
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const flatCache = require('flat-cache');
|
||||
// loads the cache, if one does not exists for the given
|
||||
// Id a new one will be prepared to be created
|
||||
const cache = flatCache.load('cacheId');
|
||||
|
||||
// sets a key on the cache
|
||||
cache.setKey('key', { foo: 'var' });
|
||||
|
||||
// get a key from the cache
|
||||
cache.getKey('key'); // { foo: 'var' }
|
||||
|
||||
// fetch the entire persisted object
|
||||
cache.all(); // { 'key': { foo: 'var' } }
|
||||
|
||||
// remove a key
|
||||
cache.removeKey('key'); // removes a key from the cache
|
||||
|
||||
// save it to disk
|
||||
cache.save(); // very important, if you don't save no changes will be persisted.
|
||||
// cache.save( true /* noPrune */) // can be used to prevent the removal of non visited keys
|
||||
|
||||
// loads the cache from a given directory, if one does
|
||||
// not exists for the given Id a new one will be prepared to be created
|
||||
const cache = flatCache.load('cacheId', path.resolve('./path/to/folder'));
|
||||
|
||||
// The following methods are useful to clear the cache
|
||||
// delete a given cache
|
||||
flatCache.clearCacheById('cacheId'); // removes the cacheId document if one exists.
|
||||
|
||||
// delete all cache
|
||||
flatCache.clearAll(); // remove the cache directory
|
||||
```
|
||||
|
||||
## Motivation for this module
|
||||
|
||||
I needed a super simple and dumb **in-memory cache** with optional disk persistance in order to make
|
||||
a script that will beutify files with `esformatter` only execute on the files that were changed since the last run.
|
||||
To make that possible we need to store the `fileSize` and `modificationTime` of the files. So a simple `key/value`
|
||||
storage was needed and Bam! this module was born.
|
||||
|
||||
## Important notes
|
||||
|
||||
- If no directory is especified when the `load` method is called, a folder named `.cache` will be created
|
||||
inside the module directory when `cache.save` is called. If you're committing your `node_modules` to any vcs, you
|
||||
might want to ignore the default `.cache` folder, or specify a custom directory.
|
||||
- The values set on the keys of the cache should be `stringify-able` ones, meaning no circular references
|
||||
- All the changes to the cache state are done to memory
|
||||
- I could have used a timer or `Object.observe` to deliver the changes to disk, but I wanted to keep this module
|
||||
intentionally dumb and simple
|
||||
- Non visited keys are removed when `cache.save()` is called. If this is not desired, you can pass `true` to the save call
|
||||
like: `cache.save( true /* noPrune */ )`.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LISCENCE) © [Jared Wray](https://jaredwray.com)
|
||||
|
||||
61
node_modules/flat-cache/package.json
generated
vendored
Normal file
61
node_modules/flat-cache/package.json
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
{
|
||||
"name": "flat-cache",
|
||||
"version": "5.0.0",
|
||||
"description": "A stupidly simple key/value storage using files to persist some data",
|
||||
"repository": "jaredwray/flat-cache",
|
||||
"license": "MIT",
|
||||
"author": {
|
||||
"name": "Jared Wray",
|
||||
"url": "https://jaredwray.com"
|
||||
},
|
||||
"main": "src/cache.js",
|
||||
"files": [
|
||||
"src/cache.js",
|
||||
"src/del.js",
|
||||
"src/utils.js"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"precommit": [
|
||||
"npm run test"
|
||||
],
|
||||
"prepush": [
|
||||
"npm run test"
|
||||
],
|
||||
"scripts": {
|
||||
"clean": "rimraf ./node_modules ./package-lock.json ./yarn.lock ./coverage",
|
||||
"test:ci": "xo && c8 --reporter=lcov mocha -R spec test/specs",
|
||||
"test": "xo --fix && c8 mocha -R spec test/specs"
|
||||
},
|
||||
"keywords": [
|
||||
"json cache",
|
||||
"simple cache",
|
||||
"file cache",
|
||||
"key par",
|
||||
"key value",
|
||||
"cache"
|
||||
],
|
||||
"devDependencies": {
|
||||
"c8": "^9.1.0",
|
||||
"chai": "^4.3.10",
|
||||
"glob-expand": "^0.2.1",
|
||||
"mocha": "^10.4.0",
|
||||
"rimraf": "^5.0.7",
|
||||
"sinon": "^18.0.0",
|
||||
"webpack": "^5.91.0",
|
||||
"write": "^2.0.0",
|
||||
"xo": "^0.58.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"flatted": "^3.3.1",
|
||||
"keyv": "^4.5.4"
|
||||
},
|
||||
"xo": {
|
||||
"rules": {
|
||||
"unicorn/prefer-module": "off",
|
||||
"n/prefer-global/process": "off",
|
||||
"unicorn/prevent-abbreviations": "off"
|
||||
}
|
||||
}
|
||||
}
|
||||
215
node_modules/flat-cache/src/cache.js
generated
vendored
Normal file
215
node_modules/flat-cache/src/cache.js
generated
vendored
Normal file
@@ -0,0 +1,215 @@
|
||||
/* eslint-disable unicorn/no-this-assignment, no-unused-expressions */
|
||||
const path = require('node:path');
|
||||
const fs = require('node:fs');
|
||||
const Keyv = require('keyv');
|
||||
const {writeJSON, tryParse} = require('./utils.js');
|
||||
const {del} = require('./del.js');
|
||||
|
||||
const cache = {
|
||||
/**
|
||||
* Load a cache identified by the given Id. If the element does not exists, then initialize an empty
|
||||
* cache storage. If specified `cacheDir` will be used as the directory to persist the data to. If omitted
|
||||
* then the cache module directory `./cache` will be used instead
|
||||
*
|
||||
* @method load
|
||||
* @param docId {String} the id of the cache, would also be used as the name of the file cache
|
||||
* @param [cacheDir] {String} directory for the cache entry
|
||||
*/
|
||||
load(documentId, cacheDir) {
|
||||
const me = this;
|
||||
me.keyv = new Keyv();
|
||||
|
||||
me.__visited = {};
|
||||
me.__persisted = {};
|
||||
|
||||
me._pathToFile = cacheDir ? path.resolve(cacheDir, documentId) : path.resolve(__dirname, '../.cache/', documentId);
|
||||
|
||||
if (fs.existsSync(me._pathToFile)) {
|
||||
me._persisted = tryParse(me._pathToFile, {});
|
||||
}
|
||||
},
|
||||
|
||||
get _persisted() {
|
||||
return this.__persisted;
|
||||
},
|
||||
|
||||
set _persisted(value) {
|
||||
this.__persisted = value;
|
||||
},
|
||||
|
||||
get _visited() {
|
||||
return this.__visited;
|
||||
},
|
||||
|
||||
set _visited(value) {
|
||||
this.__visited = value;
|
||||
},
|
||||
|
||||
/**
|
||||
* Load the cache from the provided file
|
||||
* @method loadFile
|
||||
* @param {String} pathToFile the path to the file containing the info for the cache
|
||||
*/
|
||||
loadFile(pathToFile) {
|
||||
const me = this;
|
||||
const dir = path.dirname(pathToFile);
|
||||
const fName = path.basename(pathToFile);
|
||||
|
||||
me.load(fName, dir);
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns the entire persisted object
|
||||
* @method all
|
||||
* @returns {*}
|
||||
*/
|
||||
all() {
|
||||
return this._persisted;
|
||||
},
|
||||
|
||||
keys() {
|
||||
return Object.keys(this._persisted);
|
||||
},
|
||||
/**
|
||||
* Sets a key to a given value
|
||||
* @method setKey
|
||||
* @param key {string} the key to set
|
||||
* @param value {object} the value of the key. Could be any object that can be serialized with JSON.stringify
|
||||
*/
|
||||
setKey(key, value) {
|
||||
this._visited[key] = true;
|
||||
this._persisted[key] = value;
|
||||
},
|
||||
/**
|
||||
* Remove a given key from the cache
|
||||
* @method removeKey
|
||||
* @param key {String} the key to remove from the object
|
||||
*/
|
||||
removeKey(key) {
|
||||
delete this._visited[key]; // Esfmt-ignore-line
|
||||
delete this._persisted[key]; // Esfmt-ignore-line
|
||||
},
|
||||
/**
|
||||
* Return the value of the provided key
|
||||
* @method getKey
|
||||
* @param key {String} the name of the key to retrieve
|
||||
* @returns {*} the value from the key
|
||||
*/
|
||||
getKey(key) {
|
||||
this._visited[key] = true;
|
||||
return this._persisted[key];
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove keys that were not accessed/set since the
|
||||
* last time the `prune` method was called.
|
||||
* @method _prune
|
||||
* @private
|
||||
*/
|
||||
_prune() {
|
||||
const me = this;
|
||||
const object = {};
|
||||
|
||||
const keys = Object.keys(me._visited);
|
||||
|
||||
// No keys visited for either get or set value
|
||||
if (keys.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const key of keys) {
|
||||
object[key] = me._persisted[key];
|
||||
}
|
||||
|
||||
me._visited = {};
|
||||
me._persisted = object;
|
||||
},
|
||||
|
||||
/**
|
||||
* Save the state of the cache identified by the docId to disk
|
||||
* as a JSON structure
|
||||
* @param [noPrune=false] {Boolean} whether to remove from cache the non visited files
|
||||
* @method save
|
||||
*/
|
||||
save(noPrune) {
|
||||
const me = this;
|
||||
!noPrune && me._prune();
|
||||
writeJSON(me._pathToFile, me._persisted);
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove the file where the cache is persisted
|
||||
* @method removeCacheFile
|
||||
* @return {Boolean} true or false if the file was successfully deleted
|
||||
*/
|
||||
removeCacheFile() {
|
||||
return del(this._pathToFile);
|
||||
},
|
||||
/**
|
||||
* Destroy the file cache and cache content.
|
||||
* @method destroy
|
||||
*/
|
||||
destroy() {
|
||||
const me = this;
|
||||
me._visited = {};
|
||||
me._persisted = {};
|
||||
|
||||
me.removeCacheFile();
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* Alias for create. Should be considered depreacted. Will be removed in next releases
|
||||
*
|
||||
* @method load
|
||||
* @param docId {String} the id of the cache, would also be used as the name of the file cache
|
||||
* @param [cacheDir] {String} directory for the cache entry
|
||||
* @returns {cache} cache instance
|
||||
*/
|
||||
load(documentId, cacheDir) {
|
||||
return this.create(documentId, cacheDir);
|
||||
},
|
||||
|
||||
/**
|
||||
* Load a cache identified by the given Id. If the element does not exists, then initialize an empty
|
||||
* cache storage.
|
||||
*
|
||||
* @method create
|
||||
* @param docId {String} the id of the cache, would also be used as the name of the file cache
|
||||
* @param [cacheDir] {String} directory for the cache entry
|
||||
* @returns {cache} cache instance
|
||||
*/
|
||||
create(documentId, cacheDir) {
|
||||
const object = Object.create(cache);
|
||||
object.load(documentId, cacheDir);
|
||||
return object;
|
||||
},
|
||||
|
||||
createFromFile(filePath) {
|
||||
const object = Object.create(cache);
|
||||
object.loadFile(filePath);
|
||||
return object;
|
||||
},
|
||||
/**
|
||||
* Clear the cache identified by the given id. Caches stored in a different cache directory can be deleted directly
|
||||
*
|
||||
* @method clearCache
|
||||
* @param docId {String} the id of the cache, would also be used as the name of the file cache
|
||||
* @param cacheDir {String} the directory where the cache file was written
|
||||
* @returns {Boolean} true if the cache folder was deleted. False otherwise
|
||||
*/
|
||||
clearCacheById(documentId, cacheDir) {
|
||||
const filePath = cacheDir ? path.resolve(cacheDir, documentId) : path.resolve(__dirname, '../.cache/', documentId);
|
||||
return del(filePath);
|
||||
},
|
||||
/**
|
||||
* Remove all cache stored in the cache directory
|
||||
* @method clearAll
|
||||
* @returns {Boolean} true if the cache folder was deleted. False otherwise
|
||||
*/
|
||||
clearAll(cacheDir) {
|
||||
const filePath = cacheDir ? path.resolve(cacheDir) : path.resolve(__dirname, '../.cache/');
|
||||
return del(filePath);
|
||||
},
|
||||
};
|
||||
31
node_modules/flat-cache/src/del.js
generated
vendored
Normal file
31
node_modules/flat-cache/src/del.js
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
|
||||
function del(targetPath) {
|
||||
if (!fs.existsSync(targetPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
if (fs.statSync(targetPath).isDirectory()) {
|
||||
// If it's a directory, delete its contents first
|
||||
for (const file of fs.readdirSync(targetPath)) {
|
||||
const currentPath = path.join(targetPath, file);
|
||||
|
||||
if (fs.statSync(currentPath).isFile()) {
|
||||
fs.unlinkSync(currentPath); // Delete file
|
||||
}
|
||||
}
|
||||
|
||||
fs.rmdirSync(targetPath); // Delete the now-empty directory
|
||||
} else {
|
||||
fs.unlinkSync(targetPath); // If it's a file, delete it directly
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`Error while deleting ${targetPath}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {del};
|
||||
43
node_modules/flat-cache/src/utils.js
generated
vendored
Normal file
43
node_modules/flat-cache/src/utils.js
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const flatted = require('flatted');
|
||||
|
||||
function tryParse(filePath, defaultValue) {
|
||||
let result;
|
||||
try {
|
||||
result = readJSON(filePath);
|
||||
} catch {
|
||||
result = defaultValue;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read json file synchronously using flatted
|
||||
*
|
||||
* @param {String} filePath Json filepath
|
||||
* @returns {*} parse result
|
||||
*/
|
||||
function readJSON(filePath) {
|
||||
return flatted.parse(
|
||||
fs.readFileSync(filePath, {
|
||||
encoding: 'utf8',
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write json file synchronously using circular-json
|
||||
*
|
||||
* @param {String} filePath Json filepath
|
||||
* @param {*} data Object to serialize
|
||||
*/
|
||||
function writeJSON(filePath, data) {
|
||||
fs.mkdirSync(path.dirname(filePath), {
|
||||
recursive: true,
|
||||
});
|
||||
fs.writeFileSync(filePath, flatted.stringify(data));
|
||||
}
|
||||
|
||||
module.exports = {tryParse, readJSON, writeJSON};
|
||||
Reference in New Issue
Block a user