@cacheable/utils 1.0.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.
@@ -0,0 +1,195 @@
1
+ /**
2
+ * CacheableItem
3
+ * @typedef {Object} CacheableItem
4
+ * @property {string} key - The key of the cacheable item
5
+ * @property {any} value - The value of the cacheable item
6
+ * @property {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
7
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
8
+ * undefined then it will not have a time-to-live.
9
+ */
10
+ type CacheableItem = {
11
+ key: string;
12
+ value: any;
13
+ ttl?: number | string;
14
+ };
15
+ /**
16
+ * CacheableStoreItem
17
+ * @typedef {Object} CacheableStoreItem
18
+ * @property {string} key - The key of the cacheable store item
19
+ * @property {any} value - The value of the cacheable store item
20
+ * @property {number} [expires] - The expiration time in milliseconds since epoch. If not set, the item does not expire.
21
+ */
22
+ type CacheableStoreItem = {
23
+ key: string;
24
+ value: any;
25
+ expires?: number;
26
+ };
27
+
28
+ /**
29
+ * Enqueue a promise for the group identified by `key`.
30
+ *
31
+ * All requests received for the same key while a request for that key
32
+ * is already being executed will wait. Once the running request settles
33
+ * then all the waiting requests in the group will settle, too.
34
+ * This minimizes how many times the function itself runs at the same time.
35
+ * This function resolves or rejects according to the given function argument.
36
+ *
37
+ * @url https://github.com/douglascayers/promise-coalesce
38
+ */
39
+ declare function coalesceAsync<T>(
40
+ /**
41
+ * Any identifier to group requests together.
42
+ */
43
+ key: string,
44
+ /**
45
+ * The function to run.
46
+ */
47
+ fnc: () => T | PromiseLike<T>): Promise<T>;
48
+
49
+ declare enum HashAlgorithm {
50
+ SHA256 = "sha256",
51
+ SHA512 = "sha512",
52
+ MD5 = "md5",
53
+ DJB2 = "djb2"
54
+ }
55
+ /**
56
+ * Hashes an object using the specified algorithm. The default algorithm is 'sha256'.
57
+ * @param object The object to hash
58
+ * @param algorithm The hash algorithm to use
59
+ * @returns {string} The hash of the object
60
+ */
61
+ declare function hash(object: any, algorithm?: HashAlgorithm): string;
62
+ declare function hashToNumber(hash: string, min?: number, max?: number): number;
63
+
64
+ /**
65
+ * Converts a shorthand time string or number into milliseconds.
66
+ * The shorthand can be a string like '1s', '2m', '3h', '4d', or a number representing milliseconds.
67
+ * If the input is undefined, it returns undefined.
68
+ * If the input is a string that does not match the expected format, it throws an error.
69
+ * @param shorthand - A shorthand time string or number representing milliseconds.
70
+ * @returns The equivalent time in milliseconds or undefined.
71
+ */
72
+ declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined;
73
+ /**
74
+ * Converts a shorthand time string or number into a timestamp.
75
+ * If the shorthand is undefined, it returns the current date's timestamp.
76
+ * If the shorthand is a valid time format, it adds that duration to the current date's timestamp.
77
+ * @param shorthand - A shorthand time string or number representing milliseconds.
78
+ * @param fromDate - An optional Date object to calculate from. Defaults to the current date if not provided.
79
+ * @returns The timestamp in milliseconds since epoch.
80
+ */
81
+ declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number;
82
+
83
+ declare const sleep: (ms: number) => Promise<unknown>;
84
+
85
+ type StatsOptions = {
86
+ enabled?: boolean;
87
+ };
88
+ declare class Stats {
89
+ private _hits;
90
+ private _misses;
91
+ private _gets;
92
+ private _sets;
93
+ private _deletes;
94
+ private _clears;
95
+ private _vsize;
96
+ private _ksize;
97
+ private _count;
98
+ private _enabled;
99
+ constructor(options?: StatsOptions);
100
+ /**
101
+ * @returns {boolean} - Whether the stats are enabled
102
+ */
103
+ get enabled(): boolean;
104
+ /**
105
+ * @param {boolean} enabled - Whether to enable the stats
106
+ */
107
+ set enabled(enabled: boolean);
108
+ /**
109
+ * @returns {number} - The number of hits
110
+ * @readonly
111
+ */
112
+ get hits(): number;
113
+ /**
114
+ * @returns {number} - The number of misses
115
+ * @readonly
116
+ */
117
+ get misses(): number;
118
+ /**
119
+ * @returns {number} - The number of gets
120
+ * @readonly
121
+ */
122
+ get gets(): number;
123
+ /**
124
+ * @returns {number} - The number of sets
125
+ * @readonly
126
+ */
127
+ get sets(): number;
128
+ /**
129
+ * @returns {number} - The number of deletes
130
+ * @readonly
131
+ */
132
+ get deletes(): number;
133
+ /**
134
+ * @returns {number} - The number of clears
135
+ * @readonly
136
+ */
137
+ get clears(): number;
138
+ /**
139
+ * @returns {number} - The vsize (value size) of the cache instance
140
+ * @readonly
141
+ */
142
+ get vsize(): number;
143
+ /**
144
+ * @returns {number} - The ksize (key size) of the cache instance
145
+ * @readonly
146
+ */
147
+ get ksize(): number;
148
+ /**
149
+ * @returns {number} - The count of the cache instance
150
+ * @readonly
151
+ */
152
+ get count(): number;
153
+ incrementHits(): void;
154
+ incrementMisses(): void;
155
+ incrementGets(): void;
156
+ incrementSets(): void;
157
+ incrementDeletes(): void;
158
+ incrementClears(): void;
159
+ incrementVSize(value: any): void;
160
+ decreaseVSize(value: any): void;
161
+ incrementKSize(key: string): void;
162
+ decreaseKSize(key: string): void;
163
+ incrementCount(): void;
164
+ decreaseCount(): void;
165
+ setCount(count: number): void;
166
+ roughSizeOfString(value: string): number;
167
+ roughSizeOfObject(object: any): number;
168
+ reset(): void;
169
+ resetStoreValues(): void;
170
+ }
171
+
172
+ /**
173
+ * Converts a exspires value to a TTL value.
174
+ * @param expires - The expires value to convert.
175
+ * @returns {number | undefined} The TTL value in milliseconds, or undefined if the expires value is not valid.
176
+ */
177
+ declare function getTtlFromExpires(expires: number | undefined): number | undefined;
178
+ /**
179
+ * Get the TTL value from the cacheableTtl, primaryTtl, and secondaryTtl values.
180
+ * @param cacheableTtl - The cacheableTtl value to use.
181
+ * @param primaryTtl - The primaryTtl value to use.
182
+ * @param secondaryTtl - The secondaryTtl value to use.
183
+ * @returns {number | undefined} The TTL value in milliseconds, or undefined if all values are undefined.
184
+ */
185
+ declare function getCascadingTtl(cacheableTtl?: number | string, primaryTtl?: number, secondaryTtl?: number): number | undefined;
186
+ /**
187
+ * Calculate the TTL value from the expires value. If the ttl is undefined, it will be set to the expires value. If the
188
+ * expires value is undefined, it will be set to the ttl value. If both values are defined, the smaller of the two will be used.
189
+ * @param ttl
190
+ * @param expires
191
+ * @returns
192
+ */
193
+ declare function calculateTtlFromExpiration(ttl: number | undefined, expires: number | undefined): number | undefined;
194
+
195
+ export { type CacheableItem, type CacheableStoreItem, HashAlgorithm, Stats, type StatsOptions, calculateTtlFromExpiration, coalesceAsync, getCascadingTtl, getTtlFromExpires, hash, hashToNumber, shorthandToMilliseconds, shorthandToTime, sleep };
@@ -0,0 +1,195 @@
1
+ /**
2
+ * CacheableItem
3
+ * @typedef {Object} CacheableItem
4
+ * @property {string} key - The key of the cacheable item
5
+ * @property {any} value - The value of the cacheable item
6
+ * @property {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable
7
+ * format such as `1s` for 1 second or `1h` for 1 hour. Setting undefined means that it will use the default time-to-live. If both are
8
+ * undefined then it will not have a time-to-live.
9
+ */
10
+ type CacheableItem = {
11
+ key: string;
12
+ value: any;
13
+ ttl?: number | string;
14
+ };
15
+ /**
16
+ * CacheableStoreItem
17
+ * @typedef {Object} CacheableStoreItem
18
+ * @property {string} key - The key of the cacheable store item
19
+ * @property {any} value - The value of the cacheable store item
20
+ * @property {number} [expires] - The expiration time in milliseconds since epoch. If not set, the item does not expire.
21
+ */
22
+ type CacheableStoreItem = {
23
+ key: string;
24
+ value: any;
25
+ expires?: number;
26
+ };
27
+
28
+ /**
29
+ * Enqueue a promise for the group identified by `key`.
30
+ *
31
+ * All requests received for the same key while a request for that key
32
+ * is already being executed will wait. Once the running request settles
33
+ * then all the waiting requests in the group will settle, too.
34
+ * This minimizes how many times the function itself runs at the same time.
35
+ * This function resolves or rejects according to the given function argument.
36
+ *
37
+ * @url https://github.com/douglascayers/promise-coalesce
38
+ */
39
+ declare function coalesceAsync<T>(
40
+ /**
41
+ * Any identifier to group requests together.
42
+ */
43
+ key: string,
44
+ /**
45
+ * The function to run.
46
+ */
47
+ fnc: () => T | PromiseLike<T>): Promise<T>;
48
+
49
+ declare enum HashAlgorithm {
50
+ SHA256 = "sha256",
51
+ SHA512 = "sha512",
52
+ MD5 = "md5",
53
+ DJB2 = "djb2"
54
+ }
55
+ /**
56
+ * Hashes an object using the specified algorithm. The default algorithm is 'sha256'.
57
+ * @param object The object to hash
58
+ * @param algorithm The hash algorithm to use
59
+ * @returns {string} The hash of the object
60
+ */
61
+ declare function hash(object: any, algorithm?: HashAlgorithm): string;
62
+ declare function hashToNumber(hash: string, min?: number, max?: number): number;
63
+
64
+ /**
65
+ * Converts a shorthand time string or number into milliseconds.
66
+ * The shorthand can be a string like '1s', '2m', '3h', '4d', or a number representing milliseconds.
67
+ * If the input is undefined, it returns undefined.
68
+ * If the input is a string that does not match the expected format, it throws an error.
69
+ * @param shorthand - A shorthand time string or number representing milliseconds.
70
+ * @returns The equivalent time in milliseconds or undefined.
71
+ */
72
+ declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined;
73
+ /**
74
+ * Converts a shorthand time string or number into a timestamp.
75
+ * If the shorthand is undefined, it returns the current date's timestamp.
76
+ * If the shorthand is a valid time format, it adds that duration to the current date's timestamp.
77
+ * @param shorthand - A shorthand time string or number representing milliseconds.
78
+ * @param fromDate - An optional Date object to calculate from. Defaults to the current date if not provided.
79
+ * @returns The timestamp in milliseconds since epoch.
80
+ */
81
+ declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number;
82
+
83
+ declare const sleep: (ms: number) => Promise<unknown>;
84
+
85
+ type StatsOptions = {
86
+ enabled?: boolean;
87
+ };
88
+ declare class Stats {
89
+ private _hits;
90
+ private _misses;
91
+ private _gets;
92
+ private _sets;
93
+ private _deletes;
94
+ private _clears;
95
+ private _vsize;
96
+ private _ksize;
97
+ private _count;
98
+ private _enabled;
99
+ constructor(options?: StatsOptions);
100
+ /**
101
+ * @returns {boolean} - Whether the stats are enabled
102
+ */
103
+ get enabled(): boolean;
104
+ /**
105
+ * @param {boolean} enabled - Whether to enable the stats
106
+ */
107
+ set enabled(enabled: boolean);
108
+ /**
109
+ * @returns {number} - The number of hits
110
+ * @readonly
111
+ */
112
+ get hits(): number;
113
+ /**
114
+ * @returns {number} - The number of misses
115
+ * @readonly
116
+ */
117
+ get misses(): number;
118
+ /**
119
+ * @returns {number} - The number of gets
120
+ * @readonly
121
+ */
122
+ get gets(): number;
123
+ /**
124
+ * @returns {number} - The number of sets
125
+ * @readonly
126
+ */
127
+ get sets(): number;
128
+ /**
129
+ * @returns {number} - The number of deletes
130
+ * @readonly
131
+ */
132
+ get deletes(): number;
133
+ /**
134
+ * @returns {number} - The number of clears
135
+ * @readonly
136
+ */
137
+ get clears(): number;
138
+ /**
139
+ * @returns {number} - The vsize (value size) of the cache instance
140
+ * @readonly
141
+ */
142
+ get vsize(): number;
143
+ /**
144
+ * @returns {number} - The ksize (key size) of the cache instance
145
+ * @readonly
146
+ */
147
+ get ksize(): number;
148
+ /**
149
+ * @returns {number} - The count of the cache instance
150
+ * @readonly
151
+ */
152
+ get count(): number;
153
+ incrementHits(): void;
154
+ incrementMisses(): void;
155
+ incrementGets(): void;
156
+ incrementSets(): void;
157
+ incrementDeletes(): void;
158
+ incrementClears(): void;
159
+ incrementVSize(value: any): void;
160
+ decreaseVSize(value: any): void;
161
+ incrementKSize(key: string): void;
162
+ decreaseKSize(key: string): void;
163
+ incrementCount(): void;
164
+ decreaseCount(): void;
165
+ setCount(count: number): void;
166
+ roughSizeOfString(value: string): number;
167
+ roughSizeOfObject(object: any): number;
168
+ reset(): void;
169
+ resetStoreValues(): void;
170
+ }
171
+
172
+ /**
173
+ * Converts a exspires value to a TTL value.
174
+ * @param expires - The expires value to convert.
175
+ * @returns {number | undefined} The TTL value in milliseconds, or undefined if the expires value is not valid.
176
+ */
177
+ declare function getTtlFromExpires(expires: number | undefined): number | undefined;
178
+ /**
179
+ * Get the TTL value from the cacheableTtl, primaryTtl, and secondaryTtl values.
180
+ * @param cacheableTtl - The cacheableTtl value to use.
181
+ * @param primaryTtl - The primaryTtl value to use.
182
+ * @param secondaryTtl - The secondaryTtl value to use.
183
+ * @returns {number | undefined} The TTL value in milliseconds, or undefined if all values are undefined.
184
+ */
185
+ declare function getCascadingTtl(cacheableTtl?: number | string, primaryTtl?: number, secondaryTtl?: number): number | undefined;
186
+ /**
187
+ * Calculate the TTL value from the expires value. If the ttl is undefined, it will be set to the expires value. If the
188
+ * expires value is undefined, it will be set to the ttl value. If both values are defined, the smaller of the two will be used.
189
+ * @param ttl
190
+ * @param expires
191
+ * @returns
192
+ */
193
+ declare function calculateTtlFromExpiration(ttl: number | undefined, expires: number | undefined): number | undefined;
194
+
195
+ export { type CacheableItem, type CacheableStoreItem, HashAlgorithm, Stats, type StatsOptions, calculateTtlFromExpiration, coalesceAsync, getCascadingTtl, getTtlFromExpires, hash, hashToNumber, shorthandToMilliseconds, shorthandToTime, sleep };