@binyamin/data-cache
Advanced tools
Comparing version 1.1.0 to 2.0.0
/** | ||
* Retrieve a value | ||
* Set a value | ||
* @param {string} key - Uses dot-notation (no brackets) | ||
* @param {BufferEncoding} [encoding="utf-8"] - (Optional) | ||
* @returns {*} | ||
* @param {any} value - data to store | ||
*/ | ||
export function get(key: string, encoding?: BufferEncoding): any; | ||
export function set(key: string, value: any, ext?: string): void; | ||
/** | ||
* Set a value | ||
* Retrieve a value | ||
* @param {string} key - Uses dot-notation (no brackets) | ||
* @param {*} value - data to store | ||
* @param {BufferEncoding} [encoding='utf-8'] - (Optional) | ||
* @returns {unknown} | ||
*/ | ||
export function set(key: string, value: any, ext?: string): void; | ||
export function get(key: string, encoding?: BufferEncoding): unknown; |
103
index.js
@@ -1,10 +0,14 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { cwd } from 'node:process'; | ||
const _cachedir = path.join(process.cwd(), ".cache"); | ||
const _cachedir = path.join(cwd(), '.cache'); | ||
function _initCache() { | ||
if(fs.existsSync(_cachedir) === false) { | ||
fs.mkdirSync(_cachedir) | ||
} | ||
/** @returns {*} */ | ||
function _readFile(abspath, encoding) { | ||
let filedata = fs.readFileSync(abspath, { encoding }); | ||
if (abspath.endsWith('.json')) { | ||
filedata = JSON.parse(filedata) | ||
} | ||
return filedata; | ||
} | ||
@@ -15,18 +19,15 @@ | ||
* @param {string} key - Uses dot-notation (no brackets) | ||
* @param {*} value - data to store | ||
* @param {any} value - data to store | ||
*/ | ||
export function set(key, value, ext = '') { | ||
if (!value) throw new Error('param `value` is not present'); | ||
function set(key, value, ext="") { | ||
_initCache(); | ||
let keypath = path.resolve(_cachedir, key.replace(/\./g, path.sep)); | ||
if(!value) throw new Error("param `value` is not present"); | ||
let keypath = path.resolve(_cachedir, key.replace(/\./g, path.sep)); | ||
if(typeof value === "object") { | ||
value = JSON.stringify(value, null, 4) | ||
ext = ".json"; | ||
} | ||
fs.mkdirSync(path.parse(keypath).dir, {recursive: true}); | ||
fs.writeFileSync(keypath+ext, value) | ||
if (typeof value === 'object') { | ||
value = JSON.stringify(value, null, 4) | ||
ext = '.json'; | ||
} | ||
fs.mkdirSync(path.parse(keypath).dir, { recursive: true }); | ||
fs.writeFileSync(keypath + ext, value) | ||
} | ||
@@ -37,21 +38,53 @@ | ||
* @param {string} key - Uses dot-notation (no brackets) | ||
* @param {BufferEncoding} [encoding="utf-8"] - (Optional) | ||
* @returns {*} | ||
* @param {BufferEncoding} [encoding='utf-8'] - (Optional) | ||
* @returns {unknown} | ||
*/ | ||
function get(key, encoding="utf8") { | ||
const keypath = path.resolve(_cachedir, key.replace(/\./g, path.sep)); | ||
export function get(key, encoding = 'utf8') { | ||
const keypath = path.resolve(_cachedir, key.replace(/\./g, path.sep)); | ||
if(fs.existsSync(keypath)) { | ||
return fs.readFileSync(keypath, {encoding}) | ||
} else if(fs.existsSync(keypath+".json")) { | ||
const filedata = fs.readFileSync(keypath+".json", {encoding}); | ||
return JSON.parse(filedata); | ||
} | ||
/** | ||
* Note: | ||
* The order here is intentional, in order to | ||
* limit the number of file-system calls. | ||
* | ||
* Pseudo-code: | ||
* The entry "foo" may be a directory or a file. | ||
* [1] We read it as a file. If that succeeds, `return`. | ||
* [2] If we get EISDIR, it's a directory. Read it and | ||
* `return` the contents. | ||
* [3] If we get ENOENT, it doesn't exist. We try | ||
* "foo.json", which should never be a directory. | ||
* [4] If we get ENOENT again, `return`. Otherwise, | ||
* `return` data or re-throw errors, as appropriate. | ||
*/ | ||
return undefined; | ||
} | ||
// [1] | ||
try { | ||
return _readFile(keypath, encoding); | ||
} catch(err) { | ||
// [2] | ||
if (err.code === 'EISDIR') { | ||
const data = {}; | ||
const dir = fs.readdirSync(keypath); | ||
for (const filepath of dir) { | ||
const key = path.parse(filepath).name; | ||
data[key] = _readFile(path.resolve(keypath, filepath), encoding); | ||
} | ||
module.exports = { | ||
get, | ||
set | ||
return data; | ||
} | ||
// [3] | ||
else if (err.code === 'ENOENT') { | ||
try { | ||
return _readFile(keypath + '.json', encoding); | ||
} catch (err) { | ||
// [4] | ||
if (err.code !== 'ENOENT') throw err; | ||
return null; | ||
} | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} |
{ | ||
"name": "@binyamin/data-cache", | ||
"private": false, | ||
"version": "1.1.0", | ||
"description": "Tiny module for caching data", | ||
"main": "index.js", | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"devDependencies": { | ||
"@types/node": "^14.10.2", | ||
"chai": "^4.2.0", | ||
"mocha": "^8.1.3", | ||
"mock-fs": "^4.13.0", | ||
"typescript": "^4.0.2" | ||
}, | ||
"scripts": { | ||
"test": "mocha", | ||
"types": "tsc index.js --allowJS --declaration --emitDeclarationOnly" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/binyamin/node-data-cache.git" | ||
}, | ||
"keywords": [ | ||
"cache", | ||
"caching", | ||
"memory" | ||
], | ||
"author": "Binyamin Aron Green (https://binyam.in/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/binyamin/node-data-cache/issues" | ||
}, | ||
"homepage": "https://github.com/binyamin/node-data-cache#readme", | ||
"dependencies": {} | ||
"name": "@binyamin/data-cache", | ||
"type": "module", | ||
"private": false, | ||
"version": "2.0.0", | ||
"description": "Tiny module for caching data", | ||
"main": "index.js", | ||
"types": "./index.d.ts", | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"devDependencies": { | ||
"@types/node": "^18.15.11", | ||
"ava": "^5.2.0", | ||
"mock-fs": "^5.2.0", | ||
"typescript": "^5.0.2" | ||
}, | ||
"scripts": { | ||
"test": "ava", | ||
"types": "tsc index.js --allowJS --declaration --emitDeclarationOnly" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/binyamin/data-cache.git" | ||
}, | ||
"keywords": [ | ||
"cache", | ||
"caching", | ||
"memory" | ||
], | ||
"author": "Binyamin Aron Green (https://binyam.in/)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/binyamin/data-cache/issues" | ||
}, | ||
"homepage": "https://github.com/binyamin/data-cache#readme" | ||
} |
# Data-Cache | ||
[![npm bundle size](https://img.shields.io/bundlephobia/min/@binyamin/data-cache)](https://npmjs.com/package/@binyamin/data-cache) | ||
@@ -8,3 +9,4 @@ [![CI Test](https://github.com/binyamin/data-cache/workflows/Test/badge.svg)](https://github.com/binyamin/data-cache/actions) | ||
## Install | ||
``` | ||
```console | ||
$ npm install @binyamin/data-cache | ||
@@ -14,9 +16,10 @@ ``` | ||
## Usage | ||
```js | ||
const datacache = require("@binyamin/data-cache"); | ||
import * as datacache from '@binyamin/data-cache'; | ||
datacache.set("data", "value"); | ||
// => Creates a file `.cache/data`, with a value of "value" | ||
datacache.set('data', 'value'); | ||
// => Creates a file `.cache/data`, with a value of 'value' | ||
datacache.get("data"); | ||
datacache.get('data'); | ||
// => "value" | ||
@@ -26,2 +29,3 @@ ``` | ||
## License | ||
MIT © [Binyamin Green](https://binyam.in) |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
5233
4
91
29
0
Yes