You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

keyv

Package Overview
Dependencies
Maintainers
2
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

keyv - npm Package Compare versions

Comparing version
5.6.0
to
6.0.0-alpha.1
+58
-50
dist/index.cjs

@@ -242,3 +242,2 @@ "use strict";

var Keyv = class extends event_manager_default {
opts;
iterator;

@@ -265,2 +264,3 @@ hooks = new hooks_manager_default();

_throwOnErrors = false;
_emitErrors = true;
/**

@@ -275,3 +275,3 @@ * Keyv Constructor

store ??= {};
this.opts = {
const mergedOptions = {
namespace: "keyv",

@@ -281,20 +281,15 @@ serialize: import_serialize.defaultSerialize,

emitErrors: true,
// @ts-expect-error - Map is not a KeyvStoreAdapter
store: /* @__PURE__ */ new Map(),
...options
};
if (store && store.get) {
this.opts.store = store;
mergedOptions.store = store;
} else {
this.opts = {
...this.opts,
...store
};
Object.assign(mergedOptions, store);
}
this._store = this.opts.store ?? /* @__PURE__ */ new Map();
this._compression = this.opts.compression;
this._serialize = this.opts.serialize;
this._deserialize = this.opts.deserialize;
if (this.opts.namespace) {
this._namespace = this.opts.namespace;
this._store = mergedOptions.store ?? /* @__PURE__ */ new Map();
this._compression = mergedOptions.compression;
this._serialize = mergedOptions.serialize;
this._deserialize = mergedOptions.deserialize;
if (mergedOptions.namespace) {
this._namespace = mergedOptions.namespace;
}

@@ -309,3 +304,6 @@ if (this._store) {

this._store.namespace = this._namespace;
if (typeof this._store[Symbol.iterator] === "function" && this._store instanceof Map) {
if (
// biome-ignore lint/suspicious/noExplicitAny: need to check Map iterator
typeof this._store[Symbol.iterator] === "function" && this._store instanceof Map
) {
this.iterator = this.generateIterator(

@@ -321,14 +319,17 @@ this._store

}
if (this.opts.stats) {
this.stats.enabled = this.opts.stats;
if (mergedOptions.stats) {
this.stats.enabled = mergedOptions.stats;
}
if (this.opts.ttl) {
this._ttl = this.opts.ttl;
if (mergedOptions.ttl) {
this._ttl = mergedOptions.ttl;
}
if (this.opts.useKeyPrefix !== void 0) {
this._useKeyPrefix = this.opts.useKeyPrefix;
if (mergedOptions.useKeyPrefix !== void 0) {
this._useKeyPrefix = mergedOptions.useKeyPrefix;
}
if (this.opts.throwOnErrors !== void 0) {
this._throwOnErrors = this.opts.throwOnErrors;
if (mergedOptions.emitErrors !== void 0) {
this._emitErrors = mergedOptions.emitErrors;
}
if (mergedOptions.throwOnErrors !== void 0) {
this._throwOnErrors = mergedOptions.throwOnErrors;
}
}

@@ -350,3 +351,2 @@ /**

this._store = store;
this.opts.store = store;
if (typeof store.on === "function") {

@@ -396,7 +396,3 @@ store.on("error", (error) => this.emit("error", error));

this._namespace = namespace;
this.opts.namespace = namespace;
this._store.namespace = namespace;
if (this.opts.store) {
this.opts.store.namespace = namespace;
}
}

@@ -415,3 +411,2 @@ /**

set ttl(ttl) {
this.opts.ttl = ttl;
this._ttl = ttl;

@@ -431,3 +426,2 @@ }

set serialize(serialize) {
this.opts.serialize = serialize;
this._serialize = serialize;

@@ -447,3 +441,2 @@ }

set deserialize(deserialize) {
this.opts.deserialize = deserialize;
this._deserialize = deserialize;

@@ -465,3 +458,2 @@ }

this._useKeyPrefix = value;
this.opts.useKeyPrefix = value;
}

@@ -481,4 +473,18 @@ /**

this._throwOnErrors = value;
this.opts.throwOnErrors = value;
}
/**
* Get the current emitErrors value. This will enable or disable emitting errors on methods.
* @return {boolean} The current emitErrors value.
* @default true
*/
get emitErrors() {
return this._emitErrors;
}
/**
* Set the current emitErrors value. This will enable or disable emitting errors on methods.
* @param {boolean} value The emitErrors value to set.
*/
set emitErrors(value) {
this._emitErrors = value;
}
generateIterator(iterator) {

@@ -538,3 +544,3 @@ const function_ = async function* () {

async get(key, options) {
const { store } = this.opts;
const store = this._store;
const isArray = Array.isArray(key);

@@ -558,3 +564,3 @@ const keyPrefixed = isArray ? this._getKeyPrefixArray(key) : this._getKeyPrefix(key);

}
const deserializedData = typeof rawData === "string" || this.opts.compression ? await this.deserializeData(rawData) : rawData;
const deserializedData = typeof rawData === "string" || this._compression ? await this.deserializeData(rawData) : rawData;
if (deserializedData === void 0 || deserializedData === null) {

@@ -585,3 +591,3 @@ this.hooks.trigger("postGet" /* POST_GET */, {

async getMany(keys, options) {
const { store } = this.opts;
const store = this._store;
const keyPrefixed = this._getKeyPrefixArray(keys);

@@ -593,3 +599,3 @@ const isDataExpired = (data) => typeof data.expires === "number" && Date.now() > data.expires;

const rawData2 = await store.get(key);
const deserializedRow = typeof rawData2 === "string" || this.opts.compression ? await this.deserializeData(rawData2) : rawData2;
const deserializedRow = typeof rawData2 === "string" || this._compression ? await this.deserializeData(rawData2) : rawData2;
if (deserializedRow === void 0 || deserializedRow === null) {

@@ -650,3 +656,3 @@ return void 0;

async getRaw(key) {
const { store } = this.opts;
const store = this._store;
const keyPrefixed = this._getKeyPrefix(key);

@@ -663,3 +669,3 @@ this.hooks.trigger("preGetRaw" /* PRE_GET_RAW */, { key: keyPrefixed });

}
const deserializedData = typeof rawData === "string" || this.opts.compression ? await this.deserializeData(rawData) : rawData;
const deserializedData = typeof rawData === "string" || this._compression ? await this.deserializeData(rawData) : rawData;
if (deserializedData !== void 0 && deserializedData.expires !== void 0 && deserializedData.expires !== null && // biome-ignore lint/style/noNonNullAssertion: need to fix

@@ -688,3 +694,3 @@ deserializedData.expires < Date.now()) {

async getManyRaw(keys) {
const { store } = this.opts;
const store = this._store;
const keyPrefixed = this._getKeyPrefixArray(keys);

@@ -759,3 +765,3 @@ if (keys.length === 0) {

}
const { store } = this.opts;
const store = this._store;
const expires = typeof data.ttl === "number" ? Date.now() + data.ttl : void 0;

@@ -823,3 +829,5 @@ if (typeof data.value === "symbol") {

);
results = await this._store.setMany(serializedEntries);
results = await this._store.setMany(
serializedEntries
);
}

@@ -841,3 +849,3 @@ } catch (error) {

async delete(key) {
const { store } = this.opts;
const store = this._store;
if (Array.isArray(key)) {

@@ -875,3 +883,3 @@ return this.deleteMany(key);

try {
const { store } = this.opts;
const store = this._store;
const keyPrefixed = this._getKeyPrefixArray(keys);

@@ -904,3 +912,3 @@ this.hooks.trigger("preDelete" /* PRE_DELETE */, { key: keyPrefixed });

this.emit("clear");
const { store } = this.opts;
const store = this._store;
try {

@@ -920,3 +928,3 @@ await store.clear();

const keyPrefixed = this._getKeyPrefix(key);
const { store } = this.opts;
const store = this._store;
if (store.has !== void 0 && !(store instanceof Map)) {

@@ -953,3 +961,3 @@ return store.has(keyPrefixed);

const keyPrefixed = this._getKeyPrefixArray(keys);
const { store } = this.opts;
const store = this._store;
if (store.hasMany !== void 0) {

@@ -969,3 +977,3 @@ return store.hasMany(keyPrefixed);

async disconnect() {
const { store } = this.opts;
const store = this._store;
this.emit("disconnect");

@@ -978,3 +986,3 @@ if (typeof store.disconnect === "function") {

emit(event, ...arguments_) {
if (event === "error" && !this.opts.emitErrors) {
if (event === "error" && !this._emitErrors) {
return;

@@ -981,0 +989,0 @@ }

@@ -161,8 +161,4 @@ type EventListener = (...arguments_: any[]) => void;

};
type KeyvOptions_ = Omit<KeyvOptions, "store"> & {
store: KeyvStoreAdapter | (Map<any, any> & KeyvStoreAdapter);
};
type IteratorFunction = (argument: any) => AsyncGenerator<any, void>;
declare class Keyv<GenericValue = any> extends EventManager {
opts: KeyvOptions_;
iterator?: IteratorFunction;

@@ -188,2 +184,3 @@ hooks: HooksManager;

private _throwOnErrors;
private _emitErrors;
/**

@@ -280,2 +277,13 @@ * Keyv Constructor

set throwOnErrors(value: boolean);
/**
* Get the current emitErrors value. This will enable or disable emitting errors on methods.
* @return {boolean} The current emitErrors value.
* @default true
*/
get emitErrors(): boolean;
/**
* Set the current emitErrors value. This will enable or disable emitting errors on methods.
* @param {boolean} value The emitErrors value to set.
*/
set emitErrors(value: boolean);
generateIterator(iterator: IteratorFunction): IteratorFunction;

@@ -282,0 +290,0 @@ _checkIterableAdapter(): boolean;

@@ -161,8 +161,4 @@ type EventListener = (...arguments_: any[]) => void;

};
type KeyvOptions_ = Omit<KeyvOptions, "store"> & {
store: KeyvStoreAdapter | (Map<any, any> & KeyvStoreAdapter);
};
type IteratorFunction = (argument: any) => AsyncGenerator<any, void>;
declare class Keyv<GenericValue = any> extends EventManager {
opts: KeyvOptions_;
iterator?: IteratorFunction;

@@ -188,2 +184,3 @@ hooks: HooksManager;

private _throwOnErrors;
private _emitErrors;
/**

@@ -280,2 +277,13 @@ * Keyv Constructor

set throwOnErrors(value: boolean);
/**
* Get the current emitErrors value. This will enable or disable emitting errors on methods.
* @return {boolean} The current emitErrors value.
* @default true
*/
get emitErrors(): boolean;
/**
* Set the current emitErrors value. This will enable or disable emitting errors on methods.
* @param {boolean} value The emitErrors value to set.
*/
set emitErrors(value: boolean);
generateIterator(iterator: IteratorFunction): IteratorFunction;

@@ -282,0 +290,0 @@ _checkIterableAdapter(): boolean;

@@ -216,3 +216,2 @@ // src/index.ts

var Keyv = class extends event_manager_default {
opts;
iterator;

@@ -239,2 +238,3 @@ hooks = new hooks_manager_default();

_throwOnErrors = false;
_emitErrors = true;
/**

@@ -249,3 +249,3 @@ * Keyv Constructor

store ??= {};
this.opts = {
const mergedOptions = {
namespace: "keyv",

@@ -255,20 +255,15 @@ serialize: defaultSerialize,

emitErrors: true,
// @ts-expect-error - Map is not a KeyvStoreAdapter
store: /* @__PURE__ */ new Map(),
...options
};
if (store && store.get) {
this.opts.store = store;
mergedOptions.store = store;
} else {
this.opts = {
...this.opts,
...store
};
Object.assign(mergedOptions, store);
}
this._store = this.opts.store ?? /* @__PURE__ */ new Map();
this._compression = this.opts.compression;
this._serialize = this.opts.serialize;
this._deserialize = this.opts.deserialize;
if (this.opts.namespace) {
this._namespace = this.opts.namespace;
this._store = mergedOptions.store ?? /* @__PURE__ */ new Map();
this._compression = mergedOptions.compression;
this._serialize = mergedOptions.serialize;
this._deserialize = mergedOptions.deserialize;
if (mergedOptions.namespace) {
this._namespace = mergedOptions.namespace;
}

@@ -283,3 +278,6 @@ if (this._store) {

this._store.namespace = this._namespace;
if (typeof this._store[Symbol.iterator] === "function" && this._store instanceof Map) {
if (
// biome-ignore lint/suspicious/noExplicitAny: need to check Map iterator
typeof this._store[Symbol.iterator] === "function" && this._store instanceof Map
) {
this.iterator = this.generateIterator(

@@ -295,14 +293,17 @@ this._store

}
if (this.opts.stats) {
this.stats.enabled = this.opts.stats;
if (mergedOptions.stats) {
this.stats.enabled = mergedOptions.stats;
}
if (this.opts.ttl) {
this._ttl = this.opts.ttl;
if (mergedOptions.ttl) {
this._ttl = mergedOptions.ttl;
}
if (this.opts.useKeyPrefix !== void 0) {
this._useKeyPrefix = this.opts.useKeyPrefix;
if (mergedOptions.useKeyPrefix !== void 0) {
this._useKeyPrefix = mergedOptions.useKeyPrefix;
}
if (this.opts.throwOnErrors !== void 0) {
this._throwOnErrors = this.opts.throwOnErrors;
if (mergedOptions.emitErrors !== void 0) {
this._emitErrors = mergedOptions.emitErrors;
}
if (mergedOptions.throwOnErrors !== void 0) {
this._throwOnErrors = mergedOptions.throwOnErrors;
}
}

@@ -324,3 +325,2 @@ /**

this._store = store;
this.opts.store = store;
if (typeof store.on === "function") {

@@ -370,7 +370,3 @@ store.on("error", (error) => this.emit("error", error));

this._namespace = namespace;
this.opts.namespace = namespace;
this._store.namespace = namespace;
if (this.opts.store) {
this.opts.store.namespace = namespace;
}
}

@@ -389,3 +385,2 @@ /**

set ttl(ttl) {
this.opts.ttl = ttl;
this._ttl = ttl;

@@ -405,3 +400,2 @@ }

set serialize(serialize) {
this.opts.serialize = serialize;
this._serialize = serialize;

@@ -421,3 +415,2 @@ }

set deserialize(deserialize) {
this.opts.deserialize = deserialize;
this._deserialize = deserialize;

@@ -439,3 +432,2 @@ }

this._useKeyPrefix = value;
this.opts.useKeyPrefix = value;
}

@@ -455,4 +447,18 @@ /**

this._throwOnErrors = value;
this.opts.throwOnErrors = value;
}
/**
* Get the current emitErrors value. This will enable or disable emitting errors on methods.
* @return {boolean} The current emitErrors value.
* @default true
*/
get emitErrors() {
return this._emitErrors;
}
/**
* Set the current emitErrors value. This will enable or disable emitting errors on methods.
* @param {boolean} value The emitErrors value to set.
*/
set emitErrors(value) {
this._emitErrors = value;
}
generateIterator(iterator) {

@@ -512,3 +518,3 @@ const function_ = async function* () {

async get(key, options) {
const { store } = this.opts;
const store = this._store;
const isArray = Array.isArray(key);

@@ -532,3 +538,3 @@ const keyPrefixed = isArray ? this._getKeyPrefixArray(key) : this._getKeyPrefix(key);

}
const deserializedData = typeof rawData === "string" || this.opts.compression ? await this.deserializeData(rawData) : rawData;
const deserializedData = typeof rawData === "string" || this._compression ? await this.deserializeData(rawData) : rawData;
if (deserializedData === void 0 || deserializedData === null) {

@@ -559,3 +565,3 @@ this.hooks.trigger("postGet" /* POST_GET */, {

async getMany(keys, options) {
const { store } = this.opts;
const store = this._store;
const keyPrefixed = this._getKeyPrefixArray(keys);

@@ -567,3 +573,3 @@ const isDataExpired = (data) => typeof data.expires === "number" && Date.now() > data.expires;

const rawData2 = await store.get(key);
const deserializedRow = typeof rawData2 === "string" || this.opts.compression ? await this.deserializeData(rawData2) : rawData2;
const deserializedRow = typeof rawData2 === "string" || this._compression ? await this.deserializeData(rawData2) : rawData2;
if (deserializedRow === void 0 || deserializedRow === null) {

@@ -624,3 +630,3 @@ return void 0;

async getRaw(key) {
const { store } = this.opts;
const store = this._store;
const keyPrefixed = this._getKeyPrefix(key);

@@ -637,3 +643,3 @@ this.hooks.trigger("preGetRaw" /* PRE_GET_RAW */, { key: keyPrefixed });

}
const deserializedData = typeof rawData === "string" || this.opts.compression ? await this.deserializeData(rawData) : rawData;
const deserializedData = typeof rawData === "string" || this._compression ? await this.deserializeData(rawData) : rawData;
if (deserializedData !== void 0 && deserializedData.expires !== void 0 && deserializedData.expires !== null && // biome-ignore lint/style/noNonNullAssertion: need to fix

@@ -662,3 +668,3 @@ deserializedData.expires < Date.now()) {

async getManyRaw(keys) {
const { store } = this.opts;
const store = this._store;
const keyPrefixed = this._getKeyPrefixArray(keys);

@@ -733,3 +739,3 @@ if (keys.length === 0) {

}
const { store } = this.opts;
const store = this._store;
const expires = typeof data.ttl === "number" ? Date.now() + data.ttl : void 0;

@@ -797,3 +803,5 @@ if (typeof data.value === "symbol") {

);
results = await this._store.setMany(serializedEntries);
results = await this._store.setMany(
serializedEntries
);
}

@@ -815,3 +823,3 @@ } catch (error) {

async delete(key) {
const { store } = this.opts;
const store = this._store;
if (Array.isArray(key)) {

@@ -849,3 +857,3 @@ return this.deleteMany(key);

try {
const { store } = this.opts;
const store = this._store;
const keyPrefixed = this._getKeyPrefixArray(keys);

@@ -878,3 +886,3 @@ this.hooks.trigger("preDelete" /* PRE_DELETE */, { key: keyPrefixed });

this.emit("clear");
const { store } = this.opts;
const store = this._store;
try {

@@ -894,3 +902,3 @@ await store.clear();

const keyPrefixed = this._getKeyPrefix(key);
const { store } = this.opts;
const store = this._store;
if (store.has !== void 0 && !(store instanceof Map)) {

@@ -927,3 +935,3 @@ return store.has(keyPrefixed);

const keyPrefixed = this._getKeyPrefixArray(keys);
const { store } = this.opts;
const store = this._store;
if (store.hasMany !== void 0) {

@@ -943,3 +951,3 @@ return store.hasMany(keyPrefixed);

async disconnect() {
const { store } = this.opts;
const store = this._store;
this.emit("disconnect");

@@ -952,3 +960,3 @@ if (typeof store.disconnect === "function") {

emit(event, ...arguments_) {
if (event === "error" && !this.opts.emitErrors) {
if (event === "error" && !this._emitErrors) {
return;

@@ -955,0 +963,0 @@ }

{
"name": "keyv",
"version": "5.6.0",
"version": "6.0.0-alpha.1",
"description": "Simple key-value storage with support for multiple backends",

@@ -53,21 +53,14 @@ "type": "module",

"dependencies": {
"@keyv/serialize": "^1.1.1"
"@keyv/serialize": "^6.0.0-alpha.1"
},
"devDependencies": {
"@biomejs/biome": "^2.3.13",
"@faker-js/faker": "^10.2.0",
"@vitest/coverage-v8": "^4.0.18",
"lru.min": "^1.1.1",
"quick-lru": "^7.0.0",
"@biomejs/biome": "^2.3.8",
"@faker-js/faker": "^10.1.0",
"@vitest/coverage-v8": "^4.0.14",
"rimraf": "^6.1.2",
"timekeeper": "^2.3.1",
"tsd": "^0.33.0",
"vitest": "^4.0.14",
"@keyv/compress-gzip": "^2.0.3",
"@keyv/sqlite": "^4.0.6",
"@keyv/test-suite": "^2.1.2",
"@keyv/mongo": "^3.1.0",
"@keyv/compress-brotli": "^2.0.5",
"@keyv/compress-lz4": "^1.0.1",
"@keyv/memcache": "^2.0.2"
"vitest": "^4.0.18"
},

@@ -74,0 +67,0 @@ "tsd": {

+15
-15

@@ -28,8 +28,3 @@ <h1 align="center"><img width="250" src="https://jaredwray.com/images/keyv.svg" alt="keyv"></h1>

# Bun Support
We make a best effort to support [Bun](https://bun.sh/) as a runtime. Our default and primary target is Node.js, but we run tests against Bun to ensure compatibility. If you encounter any issues while using Keyv with Bun, please report them at our [GitHub issues](https://github.com/jaredwray/keyv/issues).
# Table of Contents
- [Bun Support](#bun-support)
- [Usage](#usage)

@@ -67,2 +62,3 @@ - [Type-safe Usage](#type-safe-usage)

- [.iterator()](#iterator)
- [Bun Support](#bun-support)
- [How to Contribute](#how-to-contribute)

@@ -323,11 +319,11 @@ - [License](#license)

---|---|---
Redis | [@keyv/redis](https://github.com/jaredwray/keyv/tree/master/packages/redis) | Yes
Valkey | [@keyv/valkey](https://github.com/jaredwray/keyv/tree/master/packages/valkey) | Yes
MongoDB | [@keyv/mongo](https://github.com/jaredwray/keyv/tree/master/packages/mongo) | Yes
SQLite | [@keyv/sqlite](https://github.com/jaredwray/keyv/tree/master/packages/sqlite) | No
PostgreSQL | [@keyv/postgres](https://github.com/jaredwray/keyv/tree/master/packages/postgres) | No
MySQL | [@keyv/mysql](https://github.com/jaredwray/keyv/tree/master/packages/mysql) | No
Etcd | [@keyv/etcd](https://github.com/jaredwray/keyv/tree/master/packages/etcd) | Yes
Memcache | [@keyv/memcache](https://github.com/jaredwray/keyv/tree/master/packages/memcache) | Yes
DynamoDB | [@keyv/dynamo](https://github.com/jaredwray/keyv/tree/master/packages/dynamo) | Yes
Redis | [@keyv/redis](https://github.com/jaredwray/keyv/tree/master/storage/redis) | Yes
Valkey | [@keyv/valkey](https://github.com/jaredwray/keyv/tree/master/storage/valkey) | Yes
MongoDB | [@keyv/mongo](https://github.com/jaredwray/keyv/tree/master/storage/mongo) | Yes
SQLite | [@keyv/sqlite](https://github.com/jaredwray/keyv/tree/master/storage/sqlite) | No
PostgreSQL | [@keyv/postgres](https://github.com/jaredwray/keyv/tree/master/storage/postgres) | No
MySQL | [@keyv/mysql](https://github.com/jaredwray/keyv/tree/master/storage/mysql) | No
Etcd | [@keyv/etcd](https://github.com/jaredwray/keyv/tree/master/storage/etcd) | Yes
Memcache | [@keyv/memcache](https://github.com/jaredwray/keyv/tree/master/storage/memcache) | Yes
DynamoDB | [@keyv/dynamo](https://github.com/jaredwray/keyv/tree/master/storage/dynamo) | Yes

@@ -421,3 +417,3 @@ # Third-party Storage Adapters

For more details about BigMap, see the [@keyv/bigmap documentation](https://github.com/jaredwray/keyv/tree/main/packages/bigmap).
For more details about BigMap, see the [@keyv/bigmap documentation](https://github.com/jaredwray/keyv/tree/main/storage/bigmap).

@@ -822,2 +818,6 @@ # Compression

# Bun Support
We make a best effort to support [Bun](https://bun.sh/) as a runtime. Our default and primary target is Node.js, but we run tests against Bun to ensure compatibility. If you encounter any issues while using Keyv with Bun, please report them at our [GitHub issues](https://github.com/jaredwray/keyv/issues).
# How to Contribute

@@ -824,0 +824,0 @@