@cacheable/utils 2.1.0 → 2.3.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 +182 -8
- package/dist/index.cjs +645 -1
- package/dist/index.d.cts +92 -9
- package/dist/index.d.ts +92 -9
- package/dist/index.js +598 -1
- package/package.json +14 -13
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
|
|
|
@@ -94,6 +126,57 @@ declare function isObject<T = Record<string, unknown>>(value: unknown): value is
|
|
|
94
126
|
|
|
95
127
|
declare function lessThan(number1?: number, number2?: number): boolean;
|
|
96
128
|
|
|
129
|
+
type CacheInstance = {
|
|
130
|
+
get: (key: string) => Promise<any | undefined>;
|
|
131
|
+
has: (key: string) => Promise<boolean>;
|
|
132
|
+
set: (key: string, value: any, ttl?: number | string) => Promise<void>;
|
|
133
|
+
on: (event: string, listener: (...args: any[]) => void) => void;
|
|
134
|
+
emit: (event: string, ...args: any[]) => boolean;
|
|
135
|
+
};
|
|
136
|
+
type CacheSyncInstance = {
|
|
137
|
+
get: (key: string) => any | undefined;
|
|
138
|
+
has: (key: string) => boolean;
|
|
139
|
+
set: (key: string, value: any, ttl?: number | string) => void;
|
|
140
|
+
on: (event: string, listener: (...args: any[]) => void) => void;
|
|
141
|
+
emit: (event: string, ...args: any[]) => boolean;
|
|
142
|
+
};
|
|
143
|
+
type GetOrSetKey = string | ((options?: GetOrSetOptions) => string);
|
|
144
|
+
type GetOrSetFunctionOptions = {
|
|
145
|
+
ttl?: number | string;
|
|
146
|
+
cacheErrors?: boolean;
|
|
147
|
+
throwErrors?: boolean;
|
|
148
|
+
};
|
|
149
|
+
type GetOrSetOptions = GetOrSetFunctionOptions & {
|
|
150
|
+
cacheId?: string;
|
|
151
|
+
cache: CacheInstance;
|
|
152
|
+
};
|
|
153
|
+
type CreateWrapKey = (function_: AnyFunction, arguments_: any[], options?: WrapFunctionOptions) => string;
|
|
154
|
+
type WrapFunctionOptions = {
|
|
155
|
+
ttl?: number | string;
|
|
156
|
+
keyPrefix?: string;
|
|
157
|
+
createKey?: CreateWrapKey;
|
|
158
|
+
cacheErrors?: boolean;
|
|
159
|
+
cacheId?: string;
|
|
160
|
+
serialize?: (object: any) => string;
|
|
161
|
+
};
|
|
162
|
+
type WrapOptions = WrapFunctionOptions & {
|
|
163
|
+
cache: CacheInstance;
|
|
164
|
+
serialize?: (object: any) => string;
|
|
165
|
+
};
|
|
166
|
+
type WrapSyncOptions = WrapFunctionOptions & {
|
|
167
|
+
cache: CacheSyncInstance;
|
|
168
|
+
serialize?: (object: any) => string;
|
|
169
|
+
};
|
|
170
|
+
type AnyFunction = (...arguments_: any[]) => any;
|
|
171
|
+
declare function wrapSync<T>(function_: AnyFunction, options: WrapSyncOptions): AnyFunction;
|
|
172
|
+
declare function getOrSet<T>(key: GetOrSetKey, function_: () => Promise<T>, options: GetOrSetOptions): Promise<T | undefined>;
|
|
173
|
+
declare function wrap<T>(function_: AnyFunction, options: WrapOptions): AnyFunction;
|
|
174
|
+
type CreateWrapKeyOptions = {
|
|
175
|
+
keyPrefix?: string;
|
|
176
|
+
serialize?: (object: any) => string;
|
|
177
|
+
};
|
|
178
|
+
declare function createWrapKey(function_: AnyFunction, arguments_: any[], options?: CreateWrapKeyOptions): string;
|
|
179
|
+
|
|
97
180
|
type Function_<P, T> = (...arguments_: P[]) => T;
|
|
98
181
|
declare function runIfFn<T, P>(valueOrFunction: T | Function_<P, T>, ...arguments_: P[]): T;
|
|
99
182
|
|
|
@@ -209,4 +292,4 @@ declare function getCascadingTtl(cacheableTtl?: number | string, primaryTtl?: nu
|
|
|
209
292
|
*/
|
|
210
293
|
declare function calculateTtlFromExpiration(ttl: number | undefined, expires: number | undefined): number | undefined;
|
|
211
294
|
|
|
212
|
-
export { type CacheableItem, type CacheableStoreItem, HashAlgorithm, Stats, type StatsOptions, calculateTtlFromExpiration, coalesceAsync, getCascadingTtl, getTtlFromExpires, hash, hashToNumber, isKeyvInstance, isObject, lessThan, runIfFn, shorthandToMilliseconds, shorthandToTime, sleep };
|
|
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
|
|
|
@@ -94,6 +126,57 @@ declare function isObject<T = Record<string, unknown>>(value: unknown): value is
|
|
|
94
126
|
|
|
95
127
|
declare function lessThan(number1?: number, number2?: number): boolean;
|
|
96
128
|
|
|
129
|
+
type CacheInstance = {
|
|
130
|
+
get: (key: string) => Promise<any | undefined>;
|
|
131
|
+
has: (key: string) => Promise<boolean>;
|
|
132
|
+
set: (key: string, value: any, ttl?: number | string) => Promise<void>;
|
|
133
|
+
on: (event: string, listener: (...args: any[]) => void) => void;
|
|
134
|
+
emit: (event: string, ...args: any[]) => boolean;
|
|
135
|
+
};
|
|
136
|
+
type CacheSyncInstance = {
|
|
137
|
+
get: (key: string) => any | undefined;
|
|
138
|
+
has: (key: string) => boolean;
|
|
139
|
+
set: (key: string, value: any, ttl?: number | string) => void;
|
|
140
|
+
on: (event: string, listener: (...args: any[]) => void) => void;
|
|
141
|
+
emit: (event: string, ...args: any[]) => boolean;
|
|
142
|
+
};
|
|
143
|
+
type GetOrSetKey = string | ((options?: GetOrSetOptions) => string);
|
|
144
|
+
type GetOrSetFunctionOptions = {
|
|
145
|
+
ttl?: number | string;
|
|
146
|
+
cacheErrors?: boolean;
|
|
147
|
+
throwErrors?: boolean;
|
|
148
|
+
};
|
|
149
|
+
type GetOrSetOptions = GetOrSetFunctionOptions & {
|
|
150
|
+
cacheId?: string;
|
|
151
|
+
cache: CacheInstance;
|
|
152
|
+
};
|
|
153
|
+
type CreateWrapKey = (function_: AnyFunction, arguments_: any[], options?: WrapFunctionOptions) => string;
|
|
154
|
+
type WrapFunctionOptions = {
|
|
155
|
+
ttl?: number | string;
|
|
156
|
+
keyPrefix?: string;
|
|
157
|
+
createKey?: CreateWrapKey;
|
|
158
|
+
cacheErrors?: boolean;
|
|
159
|
+
cacheId?: string;
|
|
160
|
+
serialize?: (object: any) => string;
|
|
161
|
+
};
|
|
162
|
+
type WrapOptions = WrapFunctionOptions & {
|
|
163
|
+
cache: CacheInstance;
|
|
164
|
+
serialize?: (object: any) => string;
|
|
165
|
+
};
|
|
166
|
+
type WrapSyncOptions = WrapFunctionOptions & {
|
|
167
|
+
cache: CacheSyncInstance;
|
|
168
|
+
serialize?: (object: any) => string;
|
|
169
|
+
};
|
|
170
|
+
type AnyFunction = (...arguments_: any[]) => any;
|
|
171
|
+
declare function wrapSync<T>(function_: AnyFunction, options: WrapSyncOptions): AnyFunction;
|
|
172
|
+
declare function getOrSet<T>(key: GetOrSetKey, function_: () => Promise<T>, options: GetOrSetOptions): Promise<T | undefined>;
|
|
173
|
+
declare function wrap<T>(function_: AnyFunction, options: WrapOptions): AnyFunction;
|
|
174
|
+
type CreateWrapKeyOptions = {
|
|
175
|
+
keyPrefix?: string;
|
|
176
|
+
serialize?: (object: any) => string;
|
|
177
|
+
};
|
|
178
|
+
declare function createWrapKey(function_: AnyFunction, arguments_: any[], options?: CreateWrapKeyOptions): string;
|
|
179
|
+
|
|
97
180
|
type Function_<P, T> = (...arguments_: P[]) => T;
|
|
98
181
|
declare function runIfFn<T, P>(valueOrFunction: T | Function_<P, T>, ...arguments_: P[]): T;
|
|
99
182
|
|
|
@@ -209,4 +292,4 @@ declare function getCascadingTtl(cacheableTtl?: number | string, primaryTtl?: nu
|
|
|
209
292
|
*/
|
|
210
293
|
declare function calculateTtlFromExpiration(ttl: number | undefined, expires: number | undefined): number | undefined;
|
|
211
294
|
|
|
212
|
-
export { type CacheableItem, type CacheableStoreItem, HashAlgorithm, Stats, type StatsOptions, calculateTtlFromExpiration, coalesceAsync, getCascadingTtl, getTtlFromExpires, hash, hashToNumber, isKeyvInstance, isObject, lessThan, runIfFn, shorthandToMilliseconds, shorthandToTime, sleep };
|
|
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 };
|