keyv 6.0.0-rc.1 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -66
- package/dist/index.cjs +419 -241
- package/dist/index.d.cts +76 -55
- package/dist/index.d.mts +76 -55
- package/dist/index.mjs +419 -241
- package/package.json +7 -7
package/dist/index.d.cts
CHANGED
|
@@ -396,7 +396,7 @@ declare class KeyvStats {
|
|
|
396
396
|
set enabled(value: boolean);
|
|
397
397
|
/**
|
|
398
398
|
* Build a composite key from a telemetry event.
|
|
399
|
-
* Format: "namespace
|
|
399
|
+
* Format: "namespace::key" if namespace is present, otherwise just "key".
|
|
400
400
|
*/
|
|
401
401
|
buildKeyEventName(event: KeyvTelemetryEvent): string;
|
|
402
402
|
/**
|
|
@@ -585,7 +585,9 @@ type KeyvOptions = {
|
|
|
585
585
|
*/
|
|
586
586
|
namespace?: string;
|
|
587
587
|
/**
|
|
588
|
-
* A custom serialization adapter with stringify and parse methods.
|
|
588
|
+
* A custom serialization adapter with stringify and parse methods. Pass `false` to store
|
|
589
|
+
* values as they are, without compression. Encryption needs serialization, so with an
|
|
590
|
+
* encryption adapter set, `false` makes every write fail with an error.
|
|
589
591
|
* @default KeyvJsonSerializer (built-in)
|
|
590
592
|
*/
|
|
591
593
|
serialization?: KeyvSerializationAdapter | false;
|
|
@@ -609,13 +611,6 @@ type KeyvOptions = {
|
|
|
609
611
|
* @default false
|
|
610
612
|
*/
|
|
611
613
|
stats?: boolean;
|
|
612
|
-
/**
|
|
613
|
-
* Will throw on all errors if this is enabled to true. By default, errors
|
|
614
|
-
* will only throw if there are no listeners to the error event.
|
|
615
|
-
* This maps to hookified's `throwOnEmitError` under the hood.
|
|
616
|
-
* @default false
|
|
617
|
-
*/
|
|
618
|
-
throwOnErrors?: boolean;
|
|
619
614
|
/**
|
|
620
615
|
* Enable sanitization of keys and namespaces by detecting dangerous patterns
|
|
621
616
|
* for SQL, MongoDB, or filesystem-based storage backends. Pass a `KeyvSanitizeOptions`
|
|
@@ -625,7 +620,9 @@ type KeyvOptions = {
|
|
|
625
620
|
sanitize?: KeyvSanitizeOptions;
|
|
626
621
|
/**
|
|
627
622
|
* Enable encryption of stored values. Pass a `KeyvEncryptionAdapter` with
|
|
628
|
-
* `encrypt` and `decrypt` methods.
|
|
623
|
+
* `encrypt` and `decrypt` methods. Encryption runs on the serialized value, so it needs
|
|
624
|
+
* serialization. With `serialization: false`, writes fail with an error instead of storing
|
|
625
|
+
* values unencrypted.
|
|
629
626
|
* @default undefined
|
|
630
627
|
*/
|
|
631
628
|
encryption?: KeyvEncryptionAdapter;
|
|
@@ -736,13 +733,13 @@ type KeyvCompression = KeyvCompressionAdapter;
|
|
|
736
733
|
type KeyvBridgeAdapterOptions = {
|
|
737
734
|
/**
|
|
738
735
|
* The namespace to use for keys.
|
|
739
|
-
* When set, all keys will be prefixed with the namespace followed by the
|
|
736
|
+
* When set, all keys will be prefixed with the namespace followed by the namespace separator.
|
|
740
737
|
*/
|
|
741
738
|
namespace?: string;
|
|
742
739
|
/**
|
|
743
|
-
* The separator
|
|
740
|
+
* The separator between the namespace and the key. Defaults to `::`.
|
|
744
741
|
*/
|
|
745
|
-
|
|
742
|
+
namespaceSeparator?: string;
|
|
746
743
|
};
|
|
747
744
|
/**
|
|
748
745
|
* Interface for a promise-based store that can be used with KeyvBridgeAdapter.
|
|
@@ -809,7 +806,7 @@ type KeyPrefixData = {
|
|
|
809
806
|
declare class KeyvBridgeAdapter extends Hookified implements KeyvStorageAdapter {
|
|
810
807
|
private _store;
|
|
811
808
|
private _namespace?;
|
|
812
|
-
private
|
|
809
|
+
private _namespaceSeparator;
|
|
813
810
|
private readonly _capabilities;
|
|
814
811
|
/**
|
|
815
812
|
* Whether the wrapped store manages its own namespace (exposes a `namespace` property).
|
|
@@ -838,13 +835,13 @@ declare class KeyvBridgeAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
838
835
|
*/
|
|
839
836
|
get capabilities(): KeyvStorageCapability;
|
|
840
837
|
/**
|
|
841
|
-
* Gets the
|
|
838
|
+
* Gets the separator between the namespace and the key. Defaults to `::`.
|
|
842
839
|
*/
|
|
843
|
-
get
|
|
840
|
+
get namespaceSeparator(): string;
|
|
844
841
|
/**
|
|
845
|
-
* Sets the
|
|
842
|
+
* Sets the separator between the namespace and the key.
|
|
846
843
|
*/
|
|
847
|
-
set
|
|
844
|
+
set namespaceSeparator(separator: string);
|
|
848
845
|
/**
|
|
849
846
|
* Gets the current namespace.
|
|
850
847
|
*/
|
|
@@ -899,6 +896,7 @@ declare class KeyvBridgeAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
899
896
|
* Stores multiple entries in the store at once.
|
|
900
897
|
* Delegates to the store's native setMany if available, otherwise loops over set.
|
|
901
898
|
* @param entries - Array of entries containing key, value, and optional absolute `expires`
|
|
899
|
+
* @returns One boolean per entry, `false` where the store reported that the write failed
|
|
902
900
|
*/
|
|
903
901
|
setMany<Value>(entries: KeyvStorageEntry<Value>[]): Promise<boolean[] | undefined>;
|
|
904
902
|
/**
|
|
@@ -929,9 +927,12 @@ declare class KeyvBridgeAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
929
927
|
*/
|
|
930
928
|
deleteMany(keys: string[]): Promise<boolean[]>;
|
|
931
929
|
/**
|
|
932
|
-
* Clears entries from the store.
|
|
933
|
-
*
|
|
934
|
-
*
|
|
930
|
+
* Clears entries from the store. With no namespace, the whole store is cleared. With a
|
|
931
|
+
* namespace, only that namespace's entries are removed: a store that manages its own
|
|
932
|
+
* namespace clears them itself, and otherwise the bridge finds them with the store's
|
|
933
|
+
* `iterator()`. A store with neither can't tell namespaces apart, so this throws rather
|
|
934
|
+
* than call the store's `clear()` and delete every namespace's entries.
|
|
935
|
+
* @throws {Error} If a namespace is set and the store neither manages it nor has `iterator()`.
|
|
935
936
|
*/
|
|
936
937
|
clear(): Promise<void>;
|
|
937
938
|
/**
|
|
@@ -1031,7 +1032,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1031
1032
|
*/
|
|
1032
1033
|
set encryption(encryption: KeyvEncryptionAdapter | undefined);
|
|
1033
1034
|
/**
|
|
1034
|
-
* Get the current namespace.
|
|
1035
|
+
* Get the current namespace. When Keyv has no namespace of its own, this is the namespace
|
|
1036
|
+
* configured on the storage adapter.
|
|
1035
1037
|
* @returns {string | undefined} The current namespace.
|
|
1036
1038
|
*/
|
|
1037
1039
|
get namespace(): string | undefined;
|
|
@@ -1061,18 +1063,6 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1061
1063
|
* @param {KeyvSerializationAdapter | undefined} serialization The serialization adapter to set.
|
|
1062
1064
|
*/
|
|
1063
1065
|
set serialization(serialization: KeyvSerializationAdapter | false | undefined);
|
|
1064
|
-
/**
|
|
1065
|
-
* Get the current throwOnErrors value. When enabled, all errors with throw. By default, errors
|
|
1066
|
-
* will only throw if there are no listeners to the error event.
|
|
1067
|
-
* @return {boolean} The current throwOnErrors value.
|
|
1068
|
-
*/
|
|
1069
|
-
get throwOnErrors(): boolean;
|
|
1070
|
-
/**
|
|
1071
|
-
* Set the current throwOnErrors value. When enabled, all errors will throw. By default, errors
|
|
1072
|
-
* will only throw if there are no listeners to the error event.
|
|
1073
|
-
* @param {boolean} value The throwOnErrors value to set.
|
|
1074
|
-
*/
|
|
1075
|
-
set throwOnErrors(value: boolean);
|
|
1076
1066
|
/**
|
|
1077
1067
|
* Get the current sanitize adapter. Sanitization is disabled by default. To
|
|
1078
1068
|
* enable it `sanitize.keys` or `sanitize.namespace` to true or set KeyvSanitizePatterns
|
|
@@ -1081,7 +1071,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1081
1071
|
*/
|
|
1082
1072
|
get sanitize(): KeyvSanitizeAdapter;
|
|
1083
1073
|
/**
|
|
1084
|
-
* Set the sanitize adapter directly and will run sanitization on namespace
|
|
1074
|
+
* Set the sanitize adapter directly and will run sanitization on namespace, including a
|
|
1075
|
+
* namespace kept from the storage adapter, and apply the result to the adapter.
|
|
1085
1076
|
* @param {KeyvSanitizeAdapter} value The sanitize adapter to use.
|
|
1086
1077
|
*/
|
|
1087
1078
|
set sanitize(value: KeyvSanitizeAdapter);
|
|
@@ -1110,7 +1101,9 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1110
1101
|
* with the `async` keyword is not mis-bridged).
|
|
1111
1102
|
* 2. If the store implements the full async storage interface (but doesn't declare `expires`),
|
|
1112
1103
|
* treat it as a legacy relative-`ttl` adapter and wrap it in KeyvBridgeAdapter.
|
|
1113
|
-
* 3. If the store is map-like (
|
|
1104
|
+
* 3. If the store is map-like (get/set/delete/has that aren't native `async` functions), wrap it
|
|
1105
|
+
* in KeyvMemoryAdapter. A store other than a Map is first asked once, through `has`, whether
|
|
1106
|
+
* it returns promises; if it does, wrap it in KeyvBridgeAdapter instead.
|
|
1114
1107
|
* 4. If the store has async get/set/delete/clear, wrap it in KeyvBridgeAdapter.
|
|
1115
1108
|
* 5. Otherwise, emit an error and fall back to a default in-memory KeyvMemoryAdapter.
|
|
1116
1109
|
*
|
|
@@ -1121,7 +1114,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1121
1114
|
resolveStore(store: KeyvAny): KeyvStorageAdapter;
|
|
1122
1115
|
/**
|
|
1123
1116
|
* Sets the storage adapter by resolving it via {@link resolveStore}, then wires up
|
|
1124
|
-
* error forwarding and namespace propagation.
|
|
1117
|
+
* error forwarding and namespace propagation. Keyv's namespace, when set, is applied to the
|
|
1118
|
+
* adapter. Without one, the adapter keeps the namespace it was configured with.
|
|
1125
1119
|
* @param {KeyvStorageAdapter | Map<any, any> | any} store The storage adapter to set.
|
|
1126
1120
|
*/
|
|
1127
1121
|
setStore(store: KeyvStorageAdapter | KeyvMapAny): void;
|
|
@@ -1149,6 +1143,16 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1149
1143
|
* @returns the raw store results in the same order as `keys`
|
|
1150
1144
|
*/
|
|
1151
1145
|
private storeGetMany;
|
|
1146
|
+
/**
|
|
1147
|
+
* Runs a batch operation without the keys or entries whose key is empty, then returns its results
|
|
1148
|
+
* in the original order with `fallback` for each empty key. As with the single-key methods, an
|
|
1149
|
+
* empty key never reaches hooks, telemetry or the store.
|
|
1150
|
+
* @param items - the keys or entries of the batch
|
|
1151
|
+
* @param fallback - the result for an item whose key is empty
|
|
1152
|
+
* @param run - runs the batch operation on the items whose key is not empty
|
|
1153
|
+
* @returns one result per item, in the same order as `items`
|
|
1154
|
+
*/
|
|
1155
|
+
private skipEmptyKeys;
|
|
1152
1156
|
/**
|
|
1153
1157
|
* Get many values for an array of keys.
|
|
1154
1158
|
* @param {string[]} keys the keys to get
|
|
@@ -1187,7 +1191,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1187
1191
|
* Set a raw value to the store without wrapping or serialization. This is the write-side counterpart to getRaw().
|
|
1188
1192
|
* The value should be a KeyvValue object with { value, expires? }. If you need TTL-based expiration,
|
|
1189
1193
|
* set `expires` on the value directly (e.g. `{ value: 'bar', expires: Date.now() + 60000 }`).
|
|
1190
|
-
* The store-level
|
|
1194
|
+
* The store-level expiry is derived automatically from `value.expires`, rounded up to a whole millisecond
|
|
1195
|
+
* (a non-finite `expires` means no store-level expiry).
|
|
1191
1196
|
* @param {string} key the key to set
|
|
1192
1197
|
* @param {KeyvValue<Value>} value the raw value envelope to store
|
|
1193
1198
|
* @returns {Promise<boolean>} `true` if it was set successfully, `false` on failure.
|
|
@@ -1196,7 +1201,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1196
1201
|
/**
|
|
1197
1202
|
* Set many raw values to the store without wrapping or serialization. This is the write-side counterpart to getManyRaw().
|
|
1198
1203
|
* Each entry's value should be a KeyvValue object with { value, expires? }. If you need TTL-based expiration,
|
|
1199
|
-
* set `expires` on each value directly. The store-level
|
|
1204
|
+
* set `expires` on each value directly. The store-level expiry is derived automatically from `value.expires`,
|
|
1205
|
+
* rounded up to a whole millisecond (a non-finite `expires` means no store-level expiry).
|
|
1200
1206
|
* @param {KeyvEntry<KeyvValue<Value>>[]} entries the raw entries to set
|
|
1201
1207
|
* @returns {Promise<boolean[]>} an array of booleans, one per entry: `true` if set successfully, `false` on failure.
|
|
1202
1208
|
*/
|
|
@@ -1236,7 +1242,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1236
1242
|
*/
|
|
1237
1243
|
hasMany(keys: string[]): Promise<boolean[]>;
|
|
1238
1244
|
/**
|
|
1239
|
-
* Clear the store. If a namespace is set only entries in that namespace are removed.
|
|
1245
|
+
* Clear the store. If a namespace is set only entries in that namespace are removed. When the
|
|
1246
|
+
* store can't limit the removal to the namespace, nothing is removed and `error` is emitted.
|
|
1240
1247
|
* Emits a `clear` event.
|
|
1241
1248
|
* @returns {Promise<void>} resolves once the entries have been cleared.
|
|
1242
1249
|
*/
|
|
@@ -1254,9 +1261,12 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1254
1261
|
iterator(): AsyncGenerator<[string, KeyvAny], void>;
|
|
1255
1262
|
/**
|
|
1256
1263
|
* Encodes a value for storage. Pipeline: serialize → compress → encrypt.
|
|
1257
|
-
* If serialization is not configured, returns the data as-is.
|
|
1264
|
+
* If serialization is not configured, returns the data as-is, skipping compression. With an
|
|
1265
|
+
* encryption adapter set, it throws instead, since the value can't be encrypted and storing it
|
|
1266
|
+
* unencrypted would defeat the adapter.
|
|
1258
1267
|
* @param {KeyvValue<T>} data The value envelope to encode.
|
|
1259
|
-
* @returns {Promise<unknown>} The encoded value
|
|
1268
|
+
* @returns {Promise<unknown>} The encoded value.
|
|
1269
|
+
* @throws {Error} If an encryption adapter is set while serialization is disabled.
|
|
1260
1270
|
*/
|
|
1261
1271
|
encode<T>(data: KeyvValue<T>): Promise<unknown>;
|
|
1262
1272
|
/**
|
|
@@ -1306,6 +1316,15 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1306
1316
|
* Initializes the namespace, applying sanitization if enabled.
|
|
1307
1317
|
*/
|
|
1308
1318
|
private initNamespace;
|
|
1319
|
+
/**
|
|
1320
|
+
* Sanitizes a namespace. When sanitizing leaves nothing, it returns {@link emptiedNamespace} so
|
|
1321
|
+
* the instance keeps a namespace of its own.
|
|
1322
|
+
*/
|
|
1323
|
+
private cleanNamespace;
|
|
1324
|
+
/**
|
|
1325
|
+
* Sanitizes the namespace kept from the storage adapter when Keyv has none of its own.
|
|
1326
|
+
*/
|
|
1327
|
+
private sanitizeStoreNamespace;
|
|
1309
1328
|
}
|
|
1310
1329
|
//#endregion
|
|
1311
1330
|
//#region src/adapters/memory.d.ts
|
|
@@ -1315,13 +1334,13 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1315
1334
|
type KeyvMemoryAdapterOptions = {
|
|
1316
1335
|
/**
|
|
1317
1336
|
* The namespace to use for keys.
|
|
1318
|
-
* When set, all keys will be prefixed with the namespace followed by the
|
|
1337
|
+
* When set, all keys will be prefixed with the namespace followed by the namespace separator.
|
|
1319
1338
|
*/
|
|
1320
1339
|
namespace?: string;
|
|
1321
1340
|
/**
|
|
1322
|
-
* The separator
|
|
1341
|
+
* The separator between the namespace and the key. Defaults to `::`.
|
|
1323
1342
|
*/
|
|
1324
|
-
|
|
1343
|
+
namespaceSeparator?: string;
|
|
1325
1344
|
};
|
|
1326
1345
|
/**
|
|
1327
1346
|
* Interface for a Map-like store that can be used with KeyvMemoryAdapter.
|
|
@@ -1354,14 +1373,14 @@ type KeyvMapType = {
|
|
|
1354
1373
|
* // Using with a custom store
|
|
1355
1374
|
* const customStore = new KeyvMemoryAdapter(myCustomMapLikeStore, {
|
|
1356
1375
|
* namespace: 'tenant-123',
|
|
1357
|
-
*
|
|
1376
|
+
* namespaceSeparator: '::'
|
|
1358
1377
|
* });
|
|
1359
1378
|
* ```
|
|
1360
1379
|
*/
|
|
1361
1380
|
declare class KeyvMemoryAdapter extends Hookified implements KeyvStorageAdapter {
|
|
1362
1381
|
private _store;
|
|
1363
1382
|
private _namespace?;
|
|
1364
|
-
private
|
|
1383
|
+
private _namespaceSeparator;
|
|
1365
1384
|
private readonly _capabilities;
|
|
1366
1385
|
/**
|
|
1367
1386
|
* Creates a new KeyvMemoryAdapter instance.
|
|
@@ -1383,13 +1402,13 @@ declare class KeyvMemoryAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
1383
1402
|
*/
|
|
1384
1403
|
set store(store: KeyvMapType);
|
|
1385
1404
|
/**
|
|
1386
|
-
* Gets the
|
|
1405
|
+
* Gets the separator between the namespace and the key. Defaults to `::`.
|
|
1387
1406
|
*/
|
|
1388
|
-
get
|
|
1407
|
+
get namespaceSeparator(): string;
|
|
1389
1408
|
/**
|
|
1390
|
-
* Sets the
|
|
1409
|
+
* Sets the separator between the namespace and the key.
|
|
1391
1410
|
*/
|
|
1392
|
-
set
|
|
1411
|
+
set namespaceSeparator(separator: string);
|
|
1393
1412
|
/**
|
|
1394
1413
|
* Gets the current namespace.
|
|
1395
1414
|
*/
|
|
@@ -1444,9 +1463,11 @@ declare class KeyvMemoryAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
1444
1463
|
*/
|
|
1445
1464
|
delete(key: string): Promise<boolean>;
|
|
1446
1465
|
/**
|
|
1447
|
-
* Clears entries from the store.
|
|
1448
|
-
*
|
|
1449
|
-
*
|
|
1466
|
+
* Clears entries from the store. With no namespace, the whole store is cleared. With a
|
|
1467
|
+
* namespace, only that namespace's entries are removed, found through the store's `keys()`.
|
|
1468
|
+
* A store without `keys()` can't tell namespaces apart, so this throws rather than call the
|
|
1469
|
+
* store's `clear()` and delete every namespace's entries.
|
|
1470
|
+
* @throws {Error} If a namespace is set and the store has no `keys()`.
|
|
1450
1471
|
*/
|
|
1451
1472
|
clear(): Promise<void>;
|
|
1452
1473
|
/**
|
|
@@ -1504,7 +1525,7 @@ declare class KeyvMemoryAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
1504
1525
|
* // Create with namespace for multi-tenant scenarios
|
|
1505
1526
|
* const tenantCache = createKeyv(new Map(), {
|
|
1506
1527
|
* namespace: 'tenant-123',
|
|
1507
|
-
*
|
|
1528
|
+
* namespaceSeparator: '::'
|
|
1508
1529
|
* });
|
|
1509
1530
|
* ```
|
|
1510
1531
|
*/
|
package/dist/index.d.mts
CHANGED
|
@@ -396,7 +396,7 @@ declare class KeyvStats {
|
|
|
396
396
|
set enabled(value: boolean);
|
|
397
397
|
/**
|
|
398
398
|
* Build a composite key from a telemetry event.
|
|
399
|
-
* Format: "namespace
|
|
399
|
+
* Format: "namespace::key" if namespace is present, otherwise just "key".
|
|
400
400
|
*/
|
|
401
401
|
buildKeyEventName(event: KeyvTelemetryEvent): string;
|
|
402
402
|
/**
|
|
@@ -585,7 +585,9 @@ type KeyvOptions = {
|
|
|
585
585
|
*/
|
|
586
586
|
namespace?: string;
|
|
587
587
|
/**
|
|
588
|
-
* A custom serialization adapter with stringify and parse methods.
|
|
588
|
+
* A custom serialization adapter with stringify and parse methods. Pass `false` to store
|
|
589
|
+
* values as they are, without compression. Encryption needs serialization, so with an
|
|
590
|
+
* encryption adapter set, `false` makes every write fail with an error.
|
|
589
591
|
* @default KeyvJsonSerializer (built-in)
|
|
590
592
|
*/
|
|
591
593
|
serialization?: KeyvSerializationAdapter | false;
|
|
@@ -609,13 +611,6 @@ type KeyvOptions = {
|
|
|
609
611
|
* @default false
|
|
610
612
|
*/
|
|
611
613
|
stats?: boolean;
|
|
612
|
-
/**
|
|
613
|
-
* Will throw on all errors if this is enabled to true. By default, errors
|
|
614
|
-
* will only throw if there are no listeners to the error event.
|
|
615
|
-
* This maps to hookified's `throwOnEmitError` under the hood.
|
|
616
|
-
* @default false
|
|
617
|
-
*/
|
|
618
|
-
throwOnErrors?: boolean;
|
|
619
614
|
/**
|
|
620
615
|
* Enable sanitization of keys and namespaces by detecting dangerous patterns
|
|
621
616
|
* for SQL, MongoDB, or filesystem-based storage backends. Pass a `KeyvSanitizeOptions`
|
|
@@ -625,7 +620,9 @@ type KeyvOptions = {
|
|
|
625
620
|
sanitize?: KeyvSanitizeOptions;
|
|
626
621
|
/**
|
|
627
622
|
* Enable encryption of stored values. Pass a `KeyvEncryptionAdapter` with
|
|
628
|
-
* `encrypt` and `decrypt` methods.
|
|
623
|
+
* `encrypt` and `decrypt` methods. Encryption runs on the serialized value, so it needs
|
|
624
|
+
* serialization. With `serialization: false`, writes fail with an error instead of storing
|
|
625
|
+
* values unencrypted.
|
|
629
626
|
* @default undefined
|
|
630
627
|
*/
|
|
631
628
|
encryption?: KeyvEncryptionAdapter;
|
|
@@ -736,13 +733,13 @@ type KeyvCompression = KeyvCompressionAdapter;
|
|
|
736
733
|
type KeyvBridgeAdapterOptions = {
|
|
737
734
|
/**
|
|
738
735
|
* The namespace to use for keys.
|
|
739
|
-
* When set, all keys will be prefixed with the namespace followed by the
|
|
736
|
+
* When set, all keys will be prefixed with the namespace followed by the namespace separator.
|
|
740
737
|
*/
|
|
741
738
|
namespace?: string;
|
|
742
739
|
/**
|
|
743
|
-
* The separator
|
|
740
|
+
* The separator between the namespace and the key. Defaults to `::`.
|
|
744
741
|
*/
|
|
745
|
-
|
|
742
|
+
namespaceSeparator?: string;
|
|
746
743
|
};
|
|
747
744
|
/**
|
|
748
745
|
* Interface for a promise-based store that can be used with KeyvBridgeAdapter.
|
|
@@ -809,7 +806,7 @@ type KeyPrefixData = {
|
|
|
809
806
|
declare class KeyvBridgeAdapter extends Hookified implements KeyvStorageAdapter {
|
|
810
807
|
private _store;
|
|
811
808
|
private _namespace?;
|
|
812
|
-
private
|
|
809
|
+
private _namespaceSeparator;
|
|
813
810
|
private readonly _capabilities;
|
|
814
811
|
/**
|
|
815
812
|
* Whether the wrapped store manages its own namespace (exposes a `namespace` property).
|
|
@@ -838,13 +835,13 @@ declare class KeyvBridgeAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
838
835
|
*/
|
|
839
836
|
get capabilities(): KeyvStorageCapability;
|
|
840
837
|
/**
|
|
841
|
-
* Gets the
|
|
838
|
+
* Gets the separator between the namespace and the key. Defaults to `::`.
|
|
842
839
|
*/
|
|
843
|
-
get
|
|
840
|
+
get namespaceSeparator(): string;
|
|
844
841
|
/**
|
|
845
|
-
* Sets the
|
|
842
|
+
* Sets the separator between the namespace and the key.
|
|
846
843
|
*/
|
|
847
|
-
set
|
|
844
|
+
set namespaceSeparator(separator: string);
|
|
848
845
|
/**
|
|
849
846
|
* Gets the current namespace.
|
|
850
847
|
*/
|
|
@@ -899,6 +896,7 @@ declare class KeyvBridgeAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
899
896
|
* Stores multiple entries in the store at once.
|
|
900
897
|
* Delegates to the store's native setMany if available, otherwise loops over set.
|
|
901
898
|
* @param entries - Array of entries containing key, value, and optional absolute `expires`
|
|
899
|
+
* @returns One boolean per entry, `false` where the store reported that the write failed
|
|
902
900
|
*/
|
|
903
901
|
setMany<Value>(entries: KeyvStorageEntry<Value>[]): Promise<boolean[] | undefined>;
|
|
904
902
|
/**
|
|
@@ -929,9 +927,12 @@ declare class KeyvBridgeAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
929
927
|
*/
|
|
930
928
|
deleteMany(keys: string[]): Promise<boolean[]>;
|
|
931
929
|
/**
|
|
932
|
-
* Clears entries from the store.
|
|
933
|
-
*
|
|
934
|
-
*
|
|
930
|
+
* Clears entries from the store. With no namespace, the whole store is cleared. With a
|
|
931
|
+
* namespace, only that namespace's entries are removed: a store that manages its own
|
|
932
|
+
* namespace clears them itself, and otherwise the bridge finds them with the store's
|
|
933
|
+
* `iterator()`. A store with neither can't tell namespaces apart, so this throws rather
|
|
934
|
+
* than call the store's `clear()` and delete every namespace's entries.
|
|
935
|
+
* @throws {Error} If a namespace is set and the store neither manages it nor has `iterator()`.
|
|
935
936
|
*/
|
|
936
937
|
clear(): Promise<void>;
|
|
937
938
|
/**
|
|
@@ -1031,7 +1032,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1031
1032
|
*/
|
|
1032
1033
|
set encryption(encryption: KeyvEncryptionAdapter | undefined);
|
|
1033
1034
|
/**
|
|
1034
|
-
* Get the current namespace.
|
|
1035
|
+
* Get the current namespace. When Keyv has no namespace of its own, this is the namespace
|
|
1036
|
+
* configured on the storage adapter.
|
|
1035
1037
|
* @returns {string | undefined} The current namespace.
|
|
1036
1038
|
*/
|
|
1037
1039
|
get namespace(): string | undefined;
|
|
@@ -1061,18 +1063,6 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1061
1063
|
* @param {KeyvSerializationAdapter | undefined} serialization The serialization adapter to set.
|
|
1062
1064
|
*/
|
|
1063
1065
|
set serialization(serialization: KeyvSerializationAdapter | false | undefined);
|
|
1064
|
-
/**
|
|
1065
|
-
* Get the current throwOnErrors value. When enabled, all errors with throw. By default, errors
|
|
1066
|
-
* will only throw if there are no listeners to the error event.
|
|
1067
|
-
* @return {boolean} The current throwOnErrors value.
|
|
1068
|
-
*/
|
|
1069
|
-
get throwOnErrors(): boolean;
|
|
1070
|
-
/**
|
|
1071
|
-
* Set the current throwOnErrors value. When enabled, all errors will throw. By default, errors
|
|
1072
|
-
* will only throw if there are no listeners to the error event.
|
|
1073
|
-
* @param {boolean} value The throwOnErrors value to set.
|
|
1074
|
-
*/
|
|
1075
|
-
set throwOnErrors(value: boolean);
|
|
1076
1066
|
/**
|
|
1077
1067
|
* Get the current sanitize adapter. Sanitization is disabled by default. To
|
|
1078
1068
|
* enable it `sanitize.keys` or `sanitize.namespace` to true or set KeyvSanitizePatterns
|
|
@@ -1081,7 +1071,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1081
1071
|
*/
|
|
1082
1072
|
get sanitize(): KeyvSanitizeAdapter;
|
|
1083
1073
|
/**
|
|
1084
|
-
* Set the sanitize adapter directly and will run sanitization on namespace
|
|
1074
|
+
* Set the sanitize adapter directly and will run sanitization on namespace, including a
|
|
1075
|
+
* namespace kept from the storage adapter, and apply the result to the adapter.
|
|
1085
1076
|
* @param {KeyvSanitizeAdapter} value The sanitize adapter to use.
|
|
1086
1077
|
*/
|
|
1087
1078
|
set sanitize(value: KeyvSanitizeAdapter);
|
|
@@ -1110,7 +1101,9 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1110
1101
|
* with the `async` keyword is not mis-bridged).
|
|
1111
1102
|
* 2. If the store implements the full async storage interface (but doesn't declare `expires`),
|
|
1112
1103
|
* treat it as a legacy relative-`ttl` adapter and wrap it in KeyvBridgeAdapter.
|
|
1113
|
-
* 3. If the store is map-like (
|
|
1104
|
+
* 3. If the store is map-like (get/set/delete/has that aren't native `async` functions), wrap it
|
|
1105
|
+
* in KeyvMemoryAdapter. A store other than a Map is first asked once, through `has`, whether
|
|
1106
|
+
* it returns promises; if it does, wrap it in KeyvBridgeAdapter instead.
|
|
1114
1107
|
* 4. If the store has async get/set/delete/clear, wrap it in KeyvBridgeAdapter.
|
|
1115
1108
|
* 5. Otherwise, emit an error and fall back to a default in-memory KeyvMemoryAdapter.
|
|
1116
1109
|
*
|
|
@@ -1121,7 +1114,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1121
1114
|
resolveStore(store: KeyvAny): KeyvStorageAdapter;
|
|
1122
1115
|
/**
|
|
1123
1116
|
* Sets the storage adapter by resolving it via {@link resolveStore}, then wires up
|
|
1124
|
-
* error forwarding and namespace propagation.
|
|
1117
|
+
* error forwarding and namespace propagation. Keyv's namespace, when set, is applied to the
|
|
1118
|
+
* adapter. Without one, the adapter keeps the namespace it was configured with.
|
|
1125
1119
|
* @param {KeyvStorageAdapter | Map<any, any> | any} store The storage adapter to set.
|
|
1126
1120
|
*/
|
|
1127
1121
|
setStore(store: KeyvStorageAdapter | KeyvMapAny): void;
|
|
@@ -1149,6 +1143,16 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1149
1143
|
* @returns the raw store results in the same order as `keys`
|
|
1150
1144
|
*/
|
|
1151
1145
|
private storeGetMany;
|
|
1146
|
+
/**
|
|
1147
|
+
* Runs a batch operation without the keys or entries whose key is empty, then returns its results
|
|
1148
|
+
* in the original order with `fallback` for each empty key. As with the single-key methods, an
|
|
1149
|
+
* empty key never reaches hooks, telemetry or the store.
|
|
1150
|
+
* @param items - the keys or entries of the batch
|
|
1151
|
+
* @param fallback - the result for an item whose key is empty
|
|
1152
|
+
* @param run - runs the batch operation on the items whose key is not empty
|
|
1153
|
+
* @returns one result per item, in the same order as `items`
|
|
1154
|
+
*/
|
|
1155
|
+
private skipEmptyKeys;
|
|
1152
1156
|
/**
|
|
1153
1157
|
* Get many values for an array of keys.
|
|
1154
1158
|
* @param {string[]} keys the keys to get
|
|
@@ -1187,7 +1191,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1187
1191
|
* Set a raw value to the store without wrapping or serialization. This is the write-side counterpart to getRaw().
|
|
1188
1192
|
* The value should be a KeyvValue object with { value, expires? }. If you need TTL-based expiration,
|
|
1189
1193
|
* set `expires` on the value directly (e.g. `{ value: 'bar', expires: Date.now() + 60000 }`).
|
|
1190
|
-
* The store-level
|
|
1194
|
+
* The store-level expiry is derived automatically from `value.expires`, rounded up to a whole millisecond
|
|
1195
|
+
* (a non-finite `expires` means no store-level expiry).
|
|
1191
1196
|
* @param {string} key the key to set
|
|
1192
1197
|
* @param {KeyvValue<Value>} value the raw value envelope to store
|
|
1193
1198
|
* @returns {Promise<boolean>} `true` if it was set successfully, `false` on failure.
|
|
@@ -1196,7 +1201,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1196
1201
|
/**
|
|
1197
1202
|
* Set many raw values to the store without wrapping or serialization. This is the write-side counterpart to getManyRaw().
|
|
1198
1203
|
* Each entry's value should be a KeyvValue object with { value, expires? }. If you need TTL-based expiration,
|
|
1199
|
-
* set `expires` on each value directly. The store-level
|
|
1204
|
+
* set `expires` on each value directly. The store-level expiry is derived automatically from `value.expires`,
|
|
1205
|
+
* rounded up to a whole millisecond (a non-finite `expires` means no store-level expiry).
|
|
1200
1206
|
* @param {KeyvEntry<KeyvValue<Value>>[]} entries the raw entries to set
|
|
1201
1207
|
* @returns {Promise<boolean[]>} an array of booleans, one per entry: `true` if set successfully, `false` on failure.
|
|
1202
1208
|
*/
|
|
@@ -1236,7 +1242,8 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1236
1242
|
*/
|
|
1237
1243
|
hasMany(keys: string[]): Promise<boolean[]>;
|
|
1238
1244
|
/**
|
|
1239
|
-
* Clear the store. If a namespace is set only entries in that namespace are removed.
|
|
1245
|
+
* Clear the store. If a namespace is set only entries in that namespace are removed. When the
|
|
1246
|
+
* store can't limit the removal to the namespace, nothing is removed and `error` is emitted.
|
|
1240
1247
|
* Emits a `clear` event.
|
|
1241
1248
|
* @returns {Promise<void>} resolves once the entries have been cleared.
|
|
1242
1249
|
*/
|
|
@@ -1254,9 +1261,12 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1254
1261
|
iterator(): AsyncGenerator<[string, KeyvAny], void>;
|
|
1255
1262
|
/**
|
|
1256
1263
|
* Encodes a value for storage. Pipeline: serialize → compress → encrypt.
|
|
1257
|
-
* If serialization is not configured, returns the data as-is.
|
|
1264
|
+
* If serialization is not configured, returns the data as-is, skipping compression. With an
|
|
1265
|
+
* encryption adapter set, it throws instead, since the value can't be encrypted and storing it
|
|
1266
|
+
* unencrypted would defeat the adapter.
|
|
1258
1267
|
* @param {KeyvValue<T>} data The value envelope to encode.
|
|
1259
|
-
* @returns {Promise<unknown>} The encoded value
|
|
1268
|
+
* @returns {Promise<unknown>} The encoded value.
|
|
1269
|
+
* @throws {Error} If an encryption adapter is set while serialization is disabled.
|
|
1260
1270
|
*/
|
|
1261
1271
|
encode<T>(data: KeyvValue<T>): Promise<unknown>;
|
|
1262
1272
|
/**
|
|
@@ -1306,6 +1316,15 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1306
1316
|
* Initializes the namespace, applying sanitization if enabled.
|
|
1307
1317
|
*/
|
|
1308
1318
|
private initNamespace;
|
|
1319
|
+
/**
|
|
1320
|
+
* Sanitizes a namespace. When sanitizing leaves nothing, it returns {@link emptiedNamespace} so
|
|
1321
|
+
* the instance keeps a namespace of its own.
|
|
1322
|
+
*/
|
|
1323
|
+
private cleanNamespace;
|
|
1324
|
+
/**
|
|
1325
|
+
* Sanitizes the namespace kept from the storage adapter when Keyv has none of its own.
|
|
1326
|
+
*/
|
|
1327
|
+
private sanitizeStoreNamespace;
|
|
1309
1328
|
}
|
|
1310
1329
|
//#endregion
|
|
1311
1330
|
//#region src/adapters/memory.d.ts
|
|
@@ -1315,13 +1334,13 @@ declare class Keyv<GenericValue = KeyvAny> extends Hookified {
|
|
|
1315
1334
|
type KeyvMemoryAdapterOptions = {
|
|
1316
1335
|
/**
|
|
1317
1336
|
* The namespace to use for keys.
|
|
1318
|
-
* When set, all keys will be prefixed with the namespace followed by the
|
|
1337
|
+
* When set, all keys will be prefixed with the namespace followed by the namespace separator.
|
|
1319
1338
|
*/
|
|
1320
1339
|
namespace?: string;
|
|
1321
1340
|
/**
|
|
1322
|
-
* The separator
|
|
1341
|
+
* The separator between the namespace and the key. Defaults to `::`.
|
|
1323
1342
|
*/
|
|
1324
|
-
|
|
1343
|
+
namespaceSeparator?: string;
|
|
1325
1344
|
};
|
|
1326
1345
|
/**
|
|
1327
1346
|
* Interface for a Map-like store that can be used with KeyvMemoryAdapter.
|
|
@@ -1354,14 +1373,14 @@ type KeyvMapType = {
|
|
|
1354
1373
|
* // Using with a custom store
|
|
1355
1374
|
* const customStore = new KeyvMemoryAdapter(myCustomMapLikeStore, {
|
|
1356
1375
|
* namespace: 'tenant-123',
|
|
1357
|
-
*
|
|
1376
|
+
* namespaceSeparator: '::'
|
|
1358
1377
|
* });
|
|
1359
1378
|
* ```
|
|
1360
1379
|
*/
|
|
1361
1380
|
declare class KeyvMemoryAdapter extends Hookified implements KeyvStorageAdapter {
|
|
1362
1381
|
private _store;
|
|
1363
1382
|
private _namespace?;
|
|
1364
|
-
private
|
|
1383
|
+
private _namespaceSeparator;
|
|
1365
1384
|
private readonly _capabilities;
|
|
1366
1385
|
/**
|
|
1367
1386
|
* Creates a new KeyvMemoryAdapter instance.
|
|
@@ -1383,13 +1402,13 @@ declare class KeyvMemoryAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
1383
1402
|
*/
|
|
1384
1403
|
set store(store: KeyvMapType);
|
|
1385
1404
|
/**
|
|
1386
|
-
* Gets the
|
|
1405
|
+
* Gets the separator between the namespace and the key. Defaults to `::`.
|
|
1387
1406
|
*/
|
|
1388
|
-
get
|
|
1407
|
+
get namespaceSeparator(): string;
|
|
1389
1408
|
/**
|
|
1390
|
-
* Sets the
|
|
1409
|
+
* Sets the separator between the namespace and the key.
|
|
1391
1410
|
*/
|
|
1392
|
-
set
|
|
1411
|
+
set namespaceSeparator(separator: string);
|
|
1393
1412
|
/**
|
|
1394
1413
|
* Gets the current namespace.
|
|
1395
1414
|
*/
|
|
@@ -1444,9 +1463,11 @@ declare class KeyvMemoryAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
1444
1463
|
*/
|
|
1445
1464
|
delete(key: string): Promise<boolean>;
|
|
1446
1465
|
/**
|
|
1447
|
-
* Clears entries from the store.
|
|
1448
|
-
*
|
|
1449
|
-
*
|
|
1466
|
+
* Clears entries from the store. With no namespace, the whole store is cleared. With a
|
|
1467
|
+
* namespace, only that namespace's entries are removed, found through the store's `keys()`.
|
|
1468
|
+
* A store without `keys()` can't tell namespaces apart, so this throws rather than call the
|
|
1469
|
+
* store's `clear()` and delete every namespace's entries.
|
|
1470
|
+
* @throws {Error} If a namespace is set and the store has no `keys()`.
|
|
1450
1471
|
*/
|
|
1451
1472
|
clear(): Promise<void>;
|
|
1452
1473
|
/**
|
|
@@ -1504,7 +1525,7 @@ declare class KeyvMemoryAdapter extends Hookified implements KeyvStorageAdapter
|
|
|
1504
1525
|
* // Create with namespace for multi-tenant scenarios
|
|
1505
1526
|
* const tenantCache = createKeyv(new Map(), {
|
|
1506
1527
|
* namespace: 'tenant-123',
|
|
1507
|
-
*
|
|
1528
|
+
* namespaceSeparator: '::'
|
|
1508
1529
|
* });
|
|
1509
1530
|
* ```
|
|
1510
1531
|
*/
|