@cacheable/utils 2.2.0 → 2.3.1
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 +44 -6
- package/dist/index.cjs +66 -58
- package/dist/index.d.cts +41 -9
- package/dist/index.d.ts +41 -9
- package/dist/index.js +64 -48
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -100,25 +100,63 @@ console.log(result); // Data for my-key only executed once
|
|
|
100
100
|
|
|
101
101
|
The `@cacheable/utils` package provides hash functions that can be used to generate unique keys for caching operations. These functions are useful for creating consistent and unique identifiers for cached items.
|
|
102
102
|
|
|
103
|
+
The hashing API provides both **async** (for cryptographic algorithms) and **sync** (for non-cryptographic algorithms) methods.
|
|
104
|
+
|
|
105
|
+
## Async Hashing (Cryptographic Algorithms)
|
|
106
|
+
|
|
107
|
+
Use `hash()` and `hashToNumber()` for cryptographic algorithms like SHA-256, SHA-384, and SHA-512:
|
|
108
|
+
|
|
103
109
|
```typescript
|
|
104
|
-
import { hash } from '@cacheable/utils';
|
|
110
|
+
import { hash, hashToNumber, HashAlgorithm } from '@cacheable/utils';
|
|
105
111
|
|
|
106
|
-
|
|
112
|
+
// Hash using SHA-256 (default)
|
|
113
|
+
const key = await hash('my-cache-key');
|
|
107
114
|
console.log(key); // Unique hash for 'my-cache-key'
|
|
115
|
+
|
|
116
|
+
// Hash with specific algorithm
|
|
117
|
+
const sha512Hash = await hash('my-data', { algorithm: HashAlgorithm.SHA512 });
|
|
118
|
+
|
|
119
|
+
// Convert hash to number within range
|
|
120
|
+
const min = 0;
|
|
121
|
+
const max = 10;
|
|
122
|
+
const result = await hashToNumber({foo: 'bar'}, { min, max, algorithm: HashAlgorithm.SHA256 });
|
|
123
|
+
console.log(result); // A number between 0 and 10 based on the hash value
|
|
108
124
|
```
|
|
109
125
|
|
|
110
|
-
|
|
126
|
+
## Sync Hashing (Non-Cryptographic Algorithms)
|
|
127
|
+
|
|
128
|
+
Use `hashSync()` and `hashToNumberSync()` for faster, non-cryptographic algorithms like DJB2, FNV1, MURMER, and CRC32:
|
|
111
129
|
|
|
112
130
|
```typescript
|
|
113
|
-
import {
|
|
131
|
+
import { hashSync, hashToNumberSync, HashAlgorithm } from '@cacheable/utils';
|
|
132
|
+
|
|
133
|
+
// Hash using DJB2 (default for sync)
|
|
134
|
+
const key = hashSync('my-cache-key');
|
|
135
|
+
console.log(key); // Unique hash for 'my-cache-key'
|
|
136
|
+
|
|
137
|
+
// Hash with specific algorithm
|
|
138
|
+
const fnv1Hash = hashSync('my-data', { algorithm: HashAlgorithm.FNV1 });
|
|
114
139
|
|
|
140
|
+
// Convert hash to number within range
|
|
115
141
|
const min = 0;
|
|
116
142
|
const max = 10;
|
|
117
|
-
|
|
118
|
-
const result = hashToNumber({foo: 'bar'}, min, max, HashAlgorithm.DJB2);
|
|
143
|
+
const result = hashToNumberSync({foo: 'bar'}, { min, max, algorithm: HashAlgorithm.DJB2 });
|
|
119
144
|
console.log(result); // A number between 0 and 10 based on the hash value
|
|
120
145
|
```
|
|
121
146
|
|
|
147
|
+
## Available Hash Algorithms
|
|
148
|
+
|
|
149
|
+
**Cryptographic (Async):**
|
|
150
|
+
- `HashAlgorithm.SHA256` - SHA-256 (default for async methods)
|
|
151
|
+
- `HashAlgorithm.SHA384` - SHA-384
|
|
152
|
+
- `HashAlgorithm.SHA512` - SHA-512
|
|
153
|
+
|
|
154
|
+
**Non-Cryptographic (Sync):**
|
|
155
|
+
- `HashAlgorithm.DJB2` - DJB2 (default for sync methods)
|
|
156
|
+
- `HashAlgorithm.FNV1` - FNV-1
|
|
157
|
+
- `HashAlgorithm.MURMER` - Murmur hash
|
|
158
|
+
- `HashAlgorithm.CRC32` - CRC32
|
|
159
|
+
|
|
122
160
|
# Shorthand Time Helpers
|
|
123
161
|
|
|
124
162
|
The `@cacheable/utils` package provides a shorthand function to convert human-readable time strings into milliseconds. This is useful for setting time-to-live (TTL) values in caching operations.
|
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// src/index.ts
|
|
@@ -39,7 +29,9 @@ __export(index_exports, {
|
|
|
39
29
|
getOrSet: () => getOrSet,
|
|
40
30
|
getTtlFromExpires: () => getTtlFromExpires,
|
|
41
31
|
hash: () => hash,
|
|
32
|
+
hashSync: () => hashSync,
|
|
42
33
|
hashToNumber: () => hashToNumber,
|
|
34
|
+
hashToNumberSync: () => hashToNumberSync,
|
|
43
35
|
isKeyvInstance: () => isKeyvInstance,
|
|
44
36
|
isObject: () => isObject,
|
|
45
37
|
lessThan: () => lessThan,
|
|
@@ -176,72 +168,86 @@ async function coalesceAsync(key, fnc) {
|
|
|
176
168
|
}
|
|
177
169
|
|
|
178
170
|
// src/hash.ts
|
|
179
|
-
var
|
|
171
|
+
var import_hashery = require("hashery");
|
|
180
172
|
var HashAlgorithm = /* @__PURE__ */ ((HashAlgorithm2) => {
|
|
181
|
-
HashAlgorithm2["SHA256"] = "
|
|
182
|
-
HashAlgorithm2["
|
|
183
|
-
HashAlgorithm2["
|
|
173
|
+
HashAlgorithm2["SHA256"] = "SHA-256";
|
|
174
|
+
HashAlgorithm2["SHA384"] = "SHA-384";
|
|
175
|
+
HashAlgorithm2["SHA512"] = "SHA-512";
|
|
184
176
|
HashAlgorithm2["DJB2"] = "djb2";
|
|
177
|
+
HashAlgorithm2["FNV1"] = "fnv1";
|
|
178
|
+
HashAlgorithm2["MURMER"] = "murmer";
|
|
179
|
+
HashAlgorithm2["CRC32"] = "crc32";
|
|
185
180
|
return HashAlgorithm2;
|
|
186
181
|
})(HashAlgorithm || {});
|
|
187
|
-
function hash(object, options = {
|
|
188
|
-
algorithm: "
|
|
182
|
+
async function hash(object, options = {
|
|
183
|
+
algorithm: "SHA-256" /* SHA256 */,
|
|
189
184
|
serialize: JSON.stringify
|
|
190
185
|
}) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
return hasher.digest("hex");
|
|
186
|
+
const algorithm = options?.algorithm ?? "SHA-256" /* SHA256 */;
|
|
187
|
+
const serialize = options?.serialize ?? JSON.stringify;
|
|
188
|
+
const objectString = serialize(object);
|
|
189
|
+
const hashery = new import_hashery.Hashery();
|
|
190
|
+
return hashery.toHash(objectString, { algorithm });
|
|
191
|
+
}
|
|
192
|
+
function hashSync(object, options = {
|
|
193
|
+
algorithm: "djb2" /* DJB2 */,
|
|
194
|
+
serialize: JSON.stringify
|
|
195
|
+
}) {
|
|
196
|
+
const algorithm = options?.algorithm ?? "djb2" /* DJB2 */;
|
|
197
|
+
const serialize = options?.serialize ?? JSON.stringify;
|
|
198
|
+
const objectString = serialize(object);
|
|
199
|
+
const hashery = new import_hashery.Hashery();
|
|
200
|
+
return hashery.toHashSync(objectString, { algorithm });
|
|
207
201
|
}
|
|
208
|
-
function hashToNumber(object, options = {
|
|
202
|
+
async function hashToNumber(object, options = {
|
|
209
203
|
min: 0,
|
|
210
204
|
max: 10,
|
|
211
|
-
algorithm: "
|
|
205
|
+
algorithm: "SHA-256" /* SHA256 */,
|
|
212
206
|
serialize: JSON.stringify
|
|
213
207
|
}) {
|
|
214
208
|
const min = options?.min ?? 0;
|
|
215
209
|
const max = options?.max ?? 10;
|
|
210
|
+
const algorithm = options?.algorithm ?? "SHA-256" /* SHA256 */;
|
|
211
|
+
const serialize = options?.serialize ?? JSON.stringify;
|
|
212
|
+
const hashLength = options?.hashLength ?? 16;
|
|
216
213
|
if (min >= max) {
|
|
217
214
|
throw new Error(
|
|
218
215
|
`Invalid range: min (${min}) must be less than max (${max})`
|
|
219
216
|
);
|
|
220
217
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
const range = max - min + 1;
|
|
230
|
-
const result = min + hashNumber % range;
|
|
231
|
-
if (result < min) {
|
|
232
|
-
return min;
|
|
233
|
-
}
|
|
234
|
-
if (result > max) {
|
|
235
|
-
return max;
|
|
236
|
-
}
|
|
237
|
-
return result;
|
|
218
|
+
const objectString = serialize(object);
|
|
219
|
+
const hashery = new import_hashery.Hashery();
|
|
220
|
+
return hashery.toNumber(objectString, {
|
|
221
|
+
algorithm,
|
|
222
|
+
min,
|
|
223
|
+
max,
|
|
224
|
+
hashLength
|
|
225
|
+
});
|
|
238
226
|
}
|
|
239
|
-
function
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
227
|
+
function hashToNumberSync(object, options = {
|
|
228
|
+
min: 0,
|
|
229
|
+
max: 10,
|
|
230
|
+
algorithm: "djb2" /* DJB2 */,
|
|
231
|
+
serialize: JSON.stringify
|
|
232
|
+
}) {
|
|
233
|
+
const min = options?.min ?? 0;
|
|
234
|
+
const max = options?.max ?? 10;
|
|
235
|
+
const algorithm = options?.algorithm ?? "djb2" /* DJB2 */;
|
|
236
|
+
const serialize = options?.serialize ?? JSON.stringify;
|
|
237
|
+
const hashLength = options?.hashLength ?? 16;
|
|
238
|
+
if (min >= max) {
|
|
239
|
+
throw new Error(
|
|
240
|
+
`Invalid range: min (${min}) must be less than max (${max})`
|
|
241
|
+
);
|
|
243
242
|
}
|
|
244
|
-
|
|
243
|
+
const objectString = serialize(object);
|
|
244
|
+
const hashery = new import_hashery.Hashery();
|
|
245
|
+
return hashery.toNumberSync(objectString, {
|
|
246
|
+
algorithm,
|
|
247
|
+
min,
|
|
248
|
+
max,
|
|
249
|
+
hashLength
|
|
250
|
+
});
|
|
245
251
|
}
|
|
246
252
|
|
|
247
253
|
// src/is-keyv-instance.ts
|
|
@@ -351,9 +357,9 @@ function wrap(function_, options) {
|
|
|
351
357
|
function createWrapKey(function_, arguments_, options) {
|
|
352
358
|
const { keyPrefix, serialize } = options || {};
|
|
353
359
|
if (!keyPrefix) {
|
|
354
|
-
return `${function_.name}::${
|
|
360
|
+
return `${function_.name}::${hashSync(arguments_, { serialize })}`;
|
|
355
361
|
}
|
|
356
|
-
return `${keyPrefix}::${function_.name}::${
|
|
362
|
+
return `${keyPrefix}::${function_.name}::${hashSync(arguments_, { serialize })}`;
|
|
357
363
|
}
|
|
358
364
|
|
|
359
365
|
// src/run-if-fn.ts
|
|
@@ -623,7 +629,9 @@ function calculateTtlFromExpiration(ttl, expires) {
|
|
|
623
629
|
getOrSet,
|
|
624
630
|
getTtlFromExpires,
|
|
625
631
|
hash,
|
|
632
|
+
hashSync,
|
|
626
633
|
hashToNumber,
|
|
634
|
+
hashToNumberSync,
|
|
627
635
|
isKeyvInstance,
|
|
628
636
|
isObject,
|
|
629
637
|
lessThan,
|
package/dist/index.d.cts
CHANGED
|
@@ -66,27 +66,59 @@ key: string,
|
|
|
66
66
|
fnc: () => T | PromiseLike<T>): Promise<T>;
|
|
67
67
|
|
|
68
68
|
declare enum HashAlgorithm {
|
|
69
|
-
SHA256 = "
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
DJB2 = "djb2"
|
|
69
|
+
SHA256 = "SHA-256",
|
|
70
|
+
SHA384 = "SHA-384",
|
|
71
|
+
SHA512 = "SHA-512",
|
|
72
|
+
DJB2 = "djb2",
|
|
73
|
+
FNV1 = "fnv1",
|
|
74
|
+
MURMER = "murmer",
|
|
75
|
+
CRC32 = "crc32"
|
|
73
76
|
}
|
|
74
77
|
type HashOptions = {
|
|
75
78
|
algorithm?: HashAlgorithm;
|
|
76
79
|
serialize?: (object: any) => string;
|
|
77
80
|
};
|
|
78
|
-
type
|
|
81
|
+
type HashToNumberOptions = HashOptions & {
|
|
79
82
|
min?: number;
|
|
80
83
|
max?: number;
|
|
84
|
+
hashLength?: number;
|
|
81
85
|
};
|
|
82
86
|
/**
|
|
83
|
-
* Hashes an object using the specified
|
|
87
|
+
* Hashes an object asynchronously using the specified cryptographic algorithm.
|
|
88
|
+
* This method should be used for cryptographic algorithms (SHA-256, SHA-384, SHA-512).
|
|
89
|
+
* For non-cryptographic algorithms, use hashSync() for better performance.
|
|
90
|
+
* @param object The object to hash
|
|
91
|
+
* @param options The hash options to use
|
|
92
|
+
* @returns {Promise<string>} The hash of the object
|
|
93
|
+
*/
|
|
94
|
+
declare function hash(object: any, options?: HashOptions): Promise<string>;
|
|
95
|
+
/**
|
|
96
|
+
* Hashes an object synchronously using the specified non-cryptographic algorithm.
|
|
97
|
+
* This method should be used for non-cryptographic algorithms (DJB2, FNV1, MURMER, CRC32).
|
|
98
|
+
* For cryptographic algorithms, use hash() instead.
|
|
84
99
|
* @param object The object to hash
|
|
85
100
|
* @param options The hash options to use
|
|
86
101
|
* @returns {string} The hash of the object
|
|
87
102
|
*/
|
|
88
|
-
declare function
|
|
89
|
-
|
|
103
|
+
declare function hashSync(object: any, options?: HashOptions): string;
|
|
104
|
+
/**
|
|
105
|
+
* Hashes an object asynchronously and converts it to a number within a specified range.
|
|
106
|
+
* This method should be used for cryptographic algorithms (SHA-256, SHA-384, SHA-512).
|
|
107
|
+
* For non-cryptographic algorithms, use hashToNumberSync() for better performance.
|
|
108
|
+
* @param object The object to hash
|
|
109
|
+
* @param options The hash options to use including min/max range
|
|
110
|
+
* @returns {Promise<number>} A number within the specified range
|
|
111
|
+
*/
|
|
112
|
+
declare function hashToNumber(object: any, options?: HashToNumberOptions): Promise<number>;
|
|
113
|
+
/**
|
|
114
|
+
* Hashes an object synchronously and converts it to a number within a specified range.
|
|
115
|
+
* This method should be used for non-cryptographic algorithms (DJB2, FNV1, MURMER, CRC32).
|
|
116
|
+
* For cryptographic algorithms, use hashToNumber() instead.
|
|
117
|
+
* @param object The object to hash
|
|
118
|
+
* @param options The hash options to use including min/max range
|
|
119
|
+
* @returns {number} A number within the specified range
|
|
120
|
+
*/
|
|
121
|
+
declare function hashToNumberSync(object: any, options?: HashToNumberOptions): number;
|
|
90
122
|
|
|
91
123
|
declare function isKeyvInstance(keyv: any): boolean;
|
|
92
124
|
|
|
@@ -260,4 +292,4 @@ declare function getCascadingTtl(cacheableTtl?: number | string, primaryTtl?: nu
|
|
|
260
292
|
*/
|
|
261
293
|
declare function calculateTtlFromExpiration(ttl: number | undefined, expires: number | undefined): number | undefined;
|
|
262
294
|
|
|
263
|
-
export { type AnyFunction, type CacheInstance, type CacheSyncInstance, type CacheableItem, type CacheableStoreItem, type CreateWrapKey, type CreateWrapKeyOptions, type GetOrSetFunctionOptions, type GetOrSetKey, type GetOrSetOptions, HashAlgorithm, Stats, type StatsOptions, type WrapFunctionOptions, type WrapOptions, type WrapSyncOptions, calculateTtlFromExpiration, coalesceAsync, createWrapKey, getCascadingTtl, getOrSet, getTtlFromExpires, hash, hashToNumber, isKeyvInstance, isObject, lessThan, runIfFn, shorthandToMilliseconds, shorthandToTime, sleep, wrap, wrapSync };
|
|
295
|
+
export { type AnyFunction, type CacheInstance, type CacheSyncInstance, type CacheableItem, type CacheableStoreItem, type CreateWrapKey, type CreateWrapKeyOptions, type GetOrSetFunctionOptions, type GetOrSetKey, type GetOrSetOptions, HashAlgorithm, type HashOptions, type HashToNumberOptions, Stats, type StatsOptions, type WrapFunctionOptions, type WrapOptions, type WrapSyncOptions, calculateTtlFromExpiration, coalesceAsync, createWrapKey, getCascadingTtl, getOrSet, getTtlFromExpires, hash, hashSync, hashToNumber, hashToNumberSync, isKeyvInstance, isObject, lessThan, runIfFn, shorthandToMilliseconds, shorthandToTime, sleep, wrap, wrapSync };
|
package/dist/index.d.ts
CHANGED
|
@@ -66,27 +66,59 @@ key: string,
|
|
|
66
66
|
fnc: () => T | PromiseLike<T>): Promise<T>;
|
|
67
67
|
|
|
68
68
|
declare enum HashAlgorithm {
|
|
69
|
-
SHA256 = "
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
DJB2 = "djb2"
|
|
69
|
+
SHA256 = "SHA-256",
|
|
70
|
+
SHA384 = "SHA-384",
|
|
71
|
+
SHA512 = "SHA-512",
|
|
72
|
+
DJB2 = "djb2",
|
|
73
|
+
FNV1 = "fnv1",
|
|
74
|
+
MURMER = "murmer",
|
|
75
|
+
CRC32 = "crc32"
|
|
73
76
|
}
|
|
74
77
|
type HashOptions = {
|
|
75
78
|
algorithm?: HashAlgorithm;
|
|
76
79
|
serialize?: (object: any) => string;
|
|
77
80
|
};
|
|
78
|
-
type
|
|
81
|
+
type HashToNumberOptions = HashOptions & {
|
|
79
82
|
min?: number;
|
|
80
83
|
max?: number;
|
|
84
|
+
hashLength?: number;
|
|
81
85
|
};
|
|
82
86
|
/**
|
|
83
|
-
* Hashes an object using the specified
|
|
87
|
+
* Hashes an object asynchronously using the specified cryptographic algorithm.
|
|
88
|
+
* This method should be used for cryptographic algorithms (SHA-256, SHA-384, SHA-512).
|
|
89
|
+
* For non-cryptographic algorithms, use hashSync() for better performance.
|
|
90
|
+
* @param object The object to hash
|
|
91
|
+
* @param options The hash options to use
|
|
92
|
+
* @returns {Promise<string>} The hash of the object
|
|
93
|
+
*/
|
|
94
|
+
declare function hash(object: any, options?: HashOptions): Promise<string>;
|
|
95
|
+
/**
|
|
96
|
+
* Hashes an object synchronously using the specified non-cryptographic algorithm.
|
|
97
|
+
* This method should be used for non-cryptographic algorithms (DJB2, FNV1, MURMER, CRC32).
|
|
98
|
+
* For cryptographic algorithms, use hash() instead.
|
|
84
99
|
* @param object The object to hash
|
|
85
100
|
* @param options The hash options to use
|
|
86
101
|
* @returns {string} The hash of the object
|
|
87
102
|
*/
|
|
88
|
-
declare function
|
|
89
|
-
|
|
103
|
+
declare function hashSync(object: any, options?: HashOptions): string;
|
|
104
|
+
/**
|
|
105
|
+
* Hashes an object asynchronously and converts it to a number within a specified range.
|
|
106
|
+
* This method should be used for cryptographic algorithms (SHA-256, SHA-384, SHA-512).
|
|
107
|
+
* For non-cryptographic algorithms, use hashToNumberSync() for better performance.
|
|
108
|
+
* @param object The object to hash
|
|
109
|
+
* @param options The hash options to use including min/max range
|
|
110
|
+
* @returns {Promise<number>} A number within the specified range
|
|
111
|
+
*/
|
|
112
|
+
declare function hashToNumber(object: any, options?: HashToNumberOptions): Promise<number>;
|
|
113
|
+
/**
|
|
114
|
+
* Hashes an object synchronously and converts it to a number within a specified range.
|
|
115
|
+
* This method should be used for non-cryptographic algorithms (DJB2, FNV1, MURMER, CRC32).
|
|
116
|
+
* For cryptographic algorithms, use hashToNumber() instead.
|
|
117
|
+
* @param object The object to hash
|
|
118
|
+
* @param options The hash options to use including min/max range
|
|
119
|
+
* @returns {number} A number within the specified range
|
|
120
|
+
*/
|
|
121
|
+
declare function hashToNumberSync(object: any, options?: HashToNumberOptions): number;
|
|
90
122
|
|
|
91
123
|
declare function isKeyvInstance(keyv: any): boolean;
|
|
92
124
|
|
|
@@ -260,4 +292,4 @@ declare function getCascadingTtl(cacheableTtl?: number | string, primaryTtl?: nu
|
|
|
260
292
|
*/
|
|
261
293
|
declare function calculateTtlFromExpiration(ttl: number | undefined, expires: number | undefined): number | undefined;
|
|
262
294
|
|
|
263
|
-
export { type AnyFunction, type CacheInstance, type CacheSyncInstance, type CacheableItem, type CacheableStoreItem, type CreateWrapKey, type CreateWrapKeyOptions, type GetOrSetFunctionOptions, type GetOrSetKey, type GetOrSetOptions, HashAlgorithm, Stats, type StatsOptions, type WrapFunctionOptions, type WrapOptions, type WrapSyncOptions, calculateTtlFromExpiration, coalesceAsync, createWrapKey, getCascadingTtl, getOrSet, getTtlFromExpires, hash, hashToNumber, isKeyvInstance, isObject, lessThan, runIfFn, shorthandToMilliseconds, shorthandToTime, sleep, wrap, wrapSync };
|
|
295
|
+
export { type AnyFunction, type CacheInstance, type CacheSyncInstance, type CacheableItem, type CacheableStoreItem, type CreateWrapKey, type CreateWrapKeyOptions, type GetOrSetFunctionOptions, type GetOrSetKey, type GetOrSetOptions, HashAlgorithm, type HashOptions, type HashToNumberOptions, Stats, type StatsOptions, type WrapFunctionOptions, type WrapOptions, type WrapSyncOptions, calculateTtlFromExpiration, coalesceAsync, createWrapKey, getCascadingTtl, getOrSet, getTtlFromExpires, hash, hashSync, hashToNumber, hashToNumberSync, isKeyvInstance, isObject, lessThan, runIfFn, shorthandToMilliseconds, shorthandToTime, sleep, wrap, wrapSync };
|
package/dist/index.js
CHANGED
|
@@ -122,72 +122,86 @@ async function coalesceAsync(key, fnc) {
|
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
// src/hash.ts
|
|
125
|
-
import
|
|
125
|
+
import { Hashery } from "hashery";
|
|
126
126
|
var HashAlgorithm = /* @__PURE__ */ ((HashAlgorithm2) => {
|
|
127
|
-
HashAlgorithm2["SHA256"] = "
|
|
128
|
-
HashAlgorithm2["
|
|
129
|
-
HashAlgorithm2["
|
|
127
|
+
HashAlgorithm2["SHA256"] = "SHA-256";
|
|
128
|
+
HashAlgorithm2["SHA384"] = "SHA-384";
|
|
129
|
+
HashAlgorithm2["SHA512"] = "SHA-512";
|
|
130
130
|
HashAlgorithm2["DJB2"] = "djb2";
|
|
131
|
+
HashAlgorithm2["FNV1"] = "fnv1";
|
|
132
|
+
HashAlgorithm2["MURMER"] = "murmer";
|
|
133
|
+
HashAlgorithm2["CRC32"] = "crc32";
|
|
131
134
|
return HashAlgorithm2;
|
|
132
135
|
})(HashAlgorithm || {});
|
|
133
|
-
function hash(object, options = {
|
|
134
|
-
algorithm: "
|
|
136
|
+
async function hash(object, options = {
|
|
137
|
+
algorithm: "SHA-256" /* SHA256 */,
|
|
135
138
|
serialize: JSON.stringify
|
|
136
139
|
}) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
return hasher.digest("hex");
|
|
140
|
+
const algorithm = options?.algorithm ?? "SHA-256" /* SHA256 */;
|
|
141
|
+
const serialize = options?.serialize ?? JSON.stringify;
|
|
142
|
+
const objectString = serialize(object);
|
|
143
|
+
const hashery = new Hashery();
|
|
144
|
+
return hashery.toHash(objectString, { algorithm });
|
|
145
|
+
}
|
|
146
|
+
function hashSync(object, options = {
|
|
147
|
+
algorithm: "djb2" /* DJB2 */,
|
|
148
|
+
serialize: JSON.stringify
|
|
149
|
+
}) {
|
|
150
|
+
const algorithm = options?.algorithm ?? "djb2" /* DJB2 */;
|
|
151
|
+
const serialize = options?.serialize ?? JSON.stringify;
|
|
152
|
+
const objectString = serialize(object);
|
|
153
|
+
const hashery = new Hashery();
|
|
154
|
+
return hashery.toHashSync(objectString, { algorithm });
|
|
153
155
|
}
|
|
154
|
-
function hashToNumber(object, options = {
|
|
156
|
+
async function hashToNumber(object, options = {
|
|
155
157
|
min: 0,
|
|
156
158
|
max: 10,
|
|
157
|
-
algorithm: "
|
|
159
|
+
algorithm: "SHA-256" /* SHA256 */,
|
|
158
160
|
serialize: JSON.stringify
|
|
159
161
|
}) {
|
|
160
162
|
const min = options?.min ?? 0;
|
|
161
163
|
const max = options?.max ?? 10;
|
|
164
|
+
const algorithm = options?.algorithm ?? "SHA-256" /* SHA256 */;
|
|
165
|
+
const serialize = options?.serialize ?? JSON.stringify;
|
|
166
|
+
const hashLength = options?.hashLength ?? 16;
|
|
162
167
|
if (min >= max) {
|
|
163
168
|
throw new Error(
|
|
164
169
|
`Invalid range: min (${min}) must be less than max (${max})`
|
|
165
170
|
);
|
|
166
171
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
const range = max - min + 1;
|
|
176
|
-
const result = min + hashNumber % range;
|
|
177
|
-
if (result < min) {
|
|
178
|
-
return min;
|
|
179
|
-
}
|
|
180
|
-
if (result > max) {
|
|
181
|
-
return max;
|
|
182
|
-
}
|
|
183
|
-
return result;
|
|
172
|
+
const objectString = serialize(object);
|
|
173
|
+
const hashery = new Hashery();
|
|
174
|
+
return hashery.toNumber(objectString, {
|
|
175
|
+
algorithm,
|
|
176
|
+
min,
|
|
177
|
+
max,
|
|
178
|
+
hashLength
|
|
179
|
+
});
|
|
184
180
|
}
|
|
185
|
-
function
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
181
|
+
function hashToNumberSync(object, options = {
|
|
182
|
+
min: 0,
|
|
183
|
+
max: 10,
|
|
184
|
+
algorithm: "djb2" /* DJB2 */,
|
|
185
|
+
serialize: JSON.stringify
|
|
186
|
+
}) {
|
|
187
|
+
const min = options?.min ?? 0;
|
|
188
|
+
const max = options?.max ?? 10;
|
|
189
|
+
const algorithm = options?.algorithm ?? "djb2" /* DJB2 */;
|
|
190
|
+
const serialize = options?.serialize ?? JSON.stringify;
|
|
191
|
+
const hashLength = options?.hashLength ?? 16;
|
|
192
|
+
if (min >= max) {
|
|
193
|
+
throw new Error(
|
|
194
|
+
`Invalid range: min (${min}) must be less than max (${max})`
|
|
195
|
+
);
|
|
189
196
|
}
|
|
190
|
-
|
|
197
|
+
const objectString = serialize(object);
|
|
198
|
+
const hashery = new Hashery();
|
|
199
|
+
return hashery.toNumberSync(objectString, {
|
|
200
|
+
algorithm,
|
|
201
|
+
min,
|
|
202
|
+
max,
|
|
203
|
+
hashLength
|
|
204
|
+
});
|
|
191
205
|
}
|
|
192
206
|
|
|
193
207
|
// src/is-keyv-instance.ts
|
|
@@ -297,9 +311,9 @@ function wrap(function_, options) {
|
|
|
297
311
|
function createWrapKey(function_, arguments_, options) {
|
|
298
312
|
const { keyPrefix, serialize } = options || {};
|
|
299
313
|
if (!keyPrefix) {
|
|
300
|
-
return `${function_.name}::${
|
|
314
|
+
return `${function_.name}::${hashSync(arguments_, { serialize })}`;
|
|
301
315
|
}
|
|
302
|
-
return `${keyPrefix}::${function_.name}::${
|
|
316
|
+
return `${keyPrefix}::${function_.name}::${hashSync(arguments_, { serialize })}`;
|
|
303
317
|
}
|
|
304
318
|
|
|
305
319
|
// src/run-if-fn.ts
|
|
@@ -568,7 +582,9 @@ export {
|
|
|
568
582
|
getOrSet,
|
|
569
583
|
getTtlFromExpires,
|
|
570
584
|
hash,
|
|
585
|
+
hashSync,
|
|
571
586
|
hashToNumber,
|
|
587
|
+
hashToNumberSync,
|
|
572
588
|
isKeyvInstance,
|
|
573
589
|
isObject,
|
|
574
590
|
lessThan,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cacheable/utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "Cacheable Utilities for Caching Libraries",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -21,15 +21,15 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"private": false,
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@biomejs/biome": "^2.3.
|
|
24
|
+
"@biomejs/biome": "^2.3.5",
|
|
25
25
|
"@faker-js/faker": "^10.1.0",
|
|
26
|
-
"@types/node": "^24.
|
|
27
|
-
"@vitest/coverage-v8": "^4.0.
|
|
26
|
+
"@types/node": "^24.10.1",
|
|
27
|
+
"@vitest/coverage-v8": "^4.0.9",
|
|
28
28
|
"lru-cache": "^11.2.2",
|
|
29
|
-
"rimraf": "^6.0
|
|
30
|
-
"tsup": "^8.5.
|
|
29
|
+
"rimraf": "^6.1.0",
|
|
30
|
+
"tsup": "^8.5.1",
|
|
31
31
|
"typescript": "^5.9.3",
|
|
32
|
-
"vitest": "^4.0.
|
|
32
|
+
"vitest": "^4.0.9"
|
|
33
33
|
},
|
|
34
34
|
"keywords": [
|
|
35
35
|
"cacheable",
|
|
@@ -44,7 +44,8 @@
|
|
|
44
44
|
"LICENSE"
|
|
45
45
|
],
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"
|
|
47
|
+
"hashery": "^1.2.0",
|
|
48
|
+
"keyv": "^5.5.4"
|
|
48
49
|
},
|
|
49
50
|
"scripts": {
|
|
50
51
|
"build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean",
|