node-kv-storage
Advanced tools
Comparing version 1.0.1 to 1.1.0
42
index.js
@@ -0,1 +1,2 @@ | ||
const path = require('path'); | ||
const storage = require('node-persist'); | ||
@@ -62,3 +63,3 @@ | ||
// const _databaseName = new WeakMap(); | ||
const _databaseName = new WeakMap(); | ||
const _databasePromise = new WeakMap(); | ||
@@ -68,19 +69,29 @@ | ||
// const checkFileExists = (filePath) => fs.promises.access(filePath, fs.F_OK).then(() => true).catch(() => false); | ||
function init(name) { | ||
const baseDir = process.env.NODE_KV_STORAGE_DIR || global.NODE_KV_STORAGE_DIR || process.cwd(); | ||
const dirName = name === DEFAULT_STORAGE_AREA_NAME ? 'storage' : name; | ||
const db = storage.create({ | ||
dir: '.node-persist/' + (name === DEFAULT_STORAGE_AREA_NAME ? 'storage' : name), | ||
dir: path.resolve(baseDir, '.node-persist', dirName), | ||
stringify: JSON.stringify, | ||
parse: JSON.parse, | ||
encoding: 'utf8', | ||
logging: false, | ||
logging: !!process.env.DEBUG, | ||
ttl: false, | ||
}) | ||
_databasePromise.set(this, db.init().then(() => db)) | ||
return db.init().then(() => db); | ||
} | ||
function getDatabasePromise(area) { | ||
let promise = _databasePromise.get(area); | ||
if (!promise) { | ||
const name = _databaseName.get(area); | ||
promise = init(name); | ||
_databasePromise.set(area, promise); | ||
} | ||
return promise; | ||
} | ||
class StorageArea { | ||
constructor (name) { | ||
init.call(this, name); | ||
_databaseName.set(this, name); | ||
} | ||
@@ -90,3 +101,3 @@ | ||
const coercedKey = coerceKey(key); | ||
const store = await _databasePromise.get(this) | ||
const store = await getDatabasePromise(this); | ||
@@ -102,4 +113,3 @@ if (value === undefined) { | ||
const coercedKey = coerceKey(key); | ||
const store = await _databasePromise.get(this) | ||
const store = await getDatabasePromise(this); | ||
return await store.getItem(coercedKey); | ||
@@ -110,3 +120,3 @@ } | ||
const coercedKey = coerceKey(key); | ||
const store = await _databasePromise.get(this) | ||
const store = await getDatabasePromise(this); | ||
await store.removeItem(coercedKey); | ||
@@ -116,3 +126,3 @@ } | ||
async clear () { | ||
const store = await _databasePromise.get(this) | ||
const store = await getDatabasePromise(this); | ||
await store.clear() | ||
@@ -122,3 +132,3 @@ } | ||
async *keys () { | ||
const store = await _databasePromise.get(this) | ||
const store = await getDatabasePromise(this); | ||
// TODO: Do in true async iterative fashion | ||
@@ -130,3 +140,3 @@ const keys = await store.keys() | ||
async *values () { | ||
const store = await _databasePromise.get(this) | ||
const store = await getDatabasePromise(this); | ||
// TODO: Do in true async iterative fashion | ||
@@ -138,3 +148,3 @@ const keys = await store.keys() | ||
async *entries () { | ||
const store = await _databasePromise.get(this) | ||
const store = await getDatabasePromise(this); | ||
// TODO: Do in true async iterative fashion | ||
@@ -146,3 +156,3 @@ const keys = await store.keys() | ||
async *[Symbol.asyncIterator]() { | ||
const store = await _databasePromise.get(this) | ||
const store = await getDatabasePromise(this); | ||
// TODO: Do in true async iterative fashion | ||
@@ -149,0 +159,0 @@ const keys = await store.keys() |
{ | ||
"name": "node-kv-storage", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Wrapper around node-persist that conforms to the std:kv-storage interface", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18010
130
2