@http-client-toolkit/store-memory 0.0.1 → 0.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/lib/index.cjs +101 -64
- package/lib/index.d.cts +220 -193
- package/lib/index.d.ts +220 -193
- package/lib/index.js +106 -64
- package/package.json +2 -2
package/lib/index.d.cts
CHANGED
|
@@ -1,223 +1,250 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
CacheStore,
|
|
3
|
+
DedupeStore,
|
|
4
|
+
RateLimitStore,
|
|
5
|
+
RateLimitConfig,
|
|
6
|
+
AdaptiveRateLimitStore as AdaptiveRateLimitStore$1,
|
|
7
|
+
AdaptiveConfigSchema,
|
|
8
|
+
RequestPriority,
|
|
9
|
+
} from '@http-client-toolkit/core';
|
|
10
|
+
export {
|
|
11
|
+
AdaptiveConfig,
|
|
12
|
+
CacheStore,
|
|
13
|
+
DedupeStore,
|
|
14
|
+
AdaptiveRateLimitStore as IAdaptiveRateLimitStore,
|
|
15
|
+
RateLimitConfig,
|
|
16
|
+
RateLimitStore,
|
|
17
|
+
RequestPriority,
|
|
18
|
+
} from '@http-client-toolkit/core';
|
|
3
19
|
import { z } from 'zod';
|
|
4
20
|
|
|
5
21
|
interface InMemoryCacheStoreOptions {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
22
|
+
/** Cleanup interval in milliseconds. Set to 0 to disable automatic cleanup. Default: 60000 (1 minute) */
|
|
23
|
+
cleanupIntervalMs?: number;
|
|
24
|
+
/** Maximum number of items to store. When exceeded, least recently used items are evicted. Default: 1000 */
|
|
25
|
+
maxItems?: number;
|
|
26
|
+
/** Maximum memory usage in bytes (rough estimate). When exceeded, least recently used items are evicted. Default: 50MB */
|
|
27
|
+
maxMemoryBytes?: number;
|
|
28
|
+
/** When evicting items, remove this percentage of LRU items. Default: 0.1 (10%) */
|
|
29
|
+
evictionRatio?: number;
|
|
14
30
|
}
|
|
15
31
|
declare class InMemoryCacheStore<T = unknown> implements CacheStore<T> {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
32
|
+
private cache;
|
|
33
|
+
private cleanupInterval?;
|
|
34
|
+
private readonly maxItems;
|
|
35
|
+
private readonly maxMemoryBytes;
|
|
36
|
+
private readonly evictionRatio;
|
|
37
|
+
private totalSize;
|
|
38
|
+
constructor(options?: InMemoryCacheStoreOptions);
|
|
39
|
+
get(hash: string): Promise<T | undefined>;
|
|
40
|
+
set(hash: string, value: T, ttlSeconds: number): Promise<void>;
|
|
41
|
+
delete(hash: string): Promise<void>;
|
|
42
|
+
clear(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Get statistics about cache usage
|
|
45
|
+
*/
|
|
46
|
+
getStats(): {
|
|
47
|
+
totalItems: number;
|
|
48
|
+
expired: number;
|
|
49
|
+
memoryUsageBytes: number;
|
|
50
|
+
maxItems: number;
|
|
51
|
+
maxMemoryBytes: number;
|
|
52
|
+
memoryUtilization: number;
|
|
53
|
+
itemUtilization: number;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Clean up expired cache entries
|
|
57
|
+
*/
|
|
58
|
+
cleanup(): void;
|
|
59
|
+
/**
|
|
60
|
+
* Enforce memory and item count limits using LRU eviction
|
|
61
|
+
*/
|
|
62
|
+
private enforceMemoryLimits;
|
|
63
|
+
/**
|
|
64
|
+
* Evict the least recently used items
|
|
65
|
+
*/
|
|
66
|
+
private evictLRUItems;
|
|
67
|
+
/**
|
|
68
|
+
* Calculate rough memory usage
|
|
69
|
+
*/
|
|
70
|
+
private calculateMemoryUsage;
|
|
71
|
+
/**
|
|
72
|
+
* Get items sorted by last accessed time (for debugging/monitoring)
|
|
73
|
+
*/
|
|
74
|
+
getLRUItems(limit?: number): Array<{
|
|
75
|
+
hash: string;
|
|
76
|
+
lastAccessed: Date;
|
|
77
|
+
size: number;
|
|
78
|
+
}>;
|
|
79
|
+
/**
|
|
80
|
+
* Destroy the cache and cleanup resources
|
|
81
|
+
*/
|
|
82
|
+
destroy(): void;
|
|
83
|
+
/**
|
|
84
|
+
* Estimate the size in bytes of a cache entry (key + value + metadata)
|
|
85
|
+
*/
|
|
86
|
+
private estimateEntrySize;
|
|
71
87
|
}
|
|
72
88
|
|
|
73
89
|
interface InMemoryDedupeStoreOptions {
|
|
74
|
-
|
|
75
|
-
|
|
90
|
+
jobTimeoutMs?: number;
|
|
91
|
+
cleanupIntervalMs?: number;
|
|
76
92
|
}
|
|
77
93
|
declare class InMemoryDedupeStore<T = unknown> implements DedupeStore<T> {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
94
|
+
private jobs;
|
|
95
|
+
private readonly jobTimeoutMs;
|
|
96
|
+
private cleanupInterval?;
|
|
97
|
+
private totalJobsProcessed;
|
|
98
|
+
private destroyed;
|
|
99
|
+
constructor({
|
|
84
100
|
/** Job timeout in milliseconds. Defaults to 5 minutes. */
|
|
85
|
-
jobTimeoutMs,
|
|
101
|
+
jobTimeoutMs,
|
|
86
102
|
/** Cleanup interval in milliseconds. Defaults to 1 minute. */
|
|
87
|
-
cleanupIntervalMs,
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
103
|
+
cleanupIntervalMs,
|
|
104
|
+
}?: InMemoryDedupeStoreOptions);
|
|
105
|
+
waitFor(hash: string): Promise<T | undefined>;
|
|
106
|
+
register(hash: string): Promise<string>;
|
|
107
|
+
registerOrJoin(hash: string): Promise<{
|
|
108
|
+
jobId: string;
|
|
109
|
+
isOwner: boolean;
|
|
110
|
+
}>;
|
|
111
|
+
complete(hash: string, value: T): Promise<void>;
|
|
112
|
+
fail(hash: string, error: Error): Promise<void>;
|
|
113
|
+
isInProgress(hash: string): Promise<boolean>;
|
|
114
|
+
/**
|
|
115
|
+
* Get statistics about current dedupe jobs
|
|
116
|
+
*/
|
|
117
|
+
getStats(): {
|
|
118
|
+
activeJobs: number;
|
|
119
|
+
totalJobsProcessed: number;
|
|
120
|
+
expiredJobs: number;
|
|
121
|
+
oldestJobAgeMs: number;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Clean up expired jobs
|
|
125
|
+
*/
|
|
126
|
+
cleanup(): void;
|
|
127
|
+
/**
|
|
128
|
+
* Clear all jobs
|
|
129
|
+
*/
|
|
130
|
+
clear(): void;
|
|
131
|
+
/**
|
|
132
|
+
* Destroy the store and clean up resources
|
|
133
|
+
*/
|
|
134
|
+
destroy(): void;
|
|
118
135
|
}
|
|
119
136
|
|
|
120
137
|
interface InMemoryRateLimitStoreOptions {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
138
|
+
defaultConfig?: RateLimitConfig;
|
|
139
|
+
resourceConfigs?: Map<string, RateLimitConfig>;
|
|
140
|
+
cleanupIntervalMs?: number;
|
|
124
141
|
}
|
|
125
142
|
declare class InMemoryRateLimitStore implements RateLimitStore {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
143
|
+
private limits;
|
|
144
|
+
private defaultConfig;
|
|
145
|
+
private resourceConfigs;
|
|
146
|
+
private cleanupInterval?;
|
|
147
|
+
private totalRequests;
|
|
148
|
+
constructor({
|
|
132
149
|
/** Global/default rate-limit config applied when a resource-specific override is not provided. */
|
|
133
|
-
defaultConfig,
|
|
150
|
+
defaultConfig,
|
|
134
151
|
/** Optional per-resource overrides. */
|
|
135
|
-
resourceConfigs,
|
|
152
|
+
resourceConfigs,
|
|
136
153
|
/** Cleanup interval in milliseconds. Defaults to 1 minute. */
|
|
137
|
-
cleanupIntervalMs,
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
154
|
+
cleanupIntervalMs,
|
|
155
|
+
}?: InMemoryRateLimitStoreOptions);
|
|
156
|
+
canProceed(resource: string): Promise<boolean>;
|
|
157
|
+
record(resource: string): Promise<void>;
|
|
158
|
+
getStatus(resource: string): Promise<{
|
|
159
|
+
remaining: number;
|
|
160
|
+
resetTime: Date;
|
|
161
|
+
limit: number;
|
|
162
|
+
}>;
|
|
163
|
+
reset(resource: string): Promise<void>;
|
|
164
|
+
getWaitTime(resource: string): Promise<number>;
|
|
165
|
+
/**
|
|
166
|
+
* Set rate limit configuration for a specific resource
|
|
167
|
+
*/
|
|
168
|
+
setResourceConfig(resource: string, config: RateLimitConfig): void;
|
|
169
|
+
/**
|
|
170
|
+
* Get rate limit configuration for a resource
|
|
171
|
+
*/
|
|
172
|
+
getResourceConfig(resource: string): RateLimitConfig;
|
|
173
|
+
/**
|
|
174
|
+
* Get statistics for all resources
|
|
175
|
+
*/
|
|
176
|
+
getStats(): {
|
|
177
|
+
totalResources: number;
|
|
178
|
+
activeResources: number;
|
|
179
|
+
rateLimitedResources: number;
|
|
180
|
+
totalRequests: number;
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* Clear all rate limit data
|
|
184
|
+
*/
|
|
185
|
+
clear(): void;
|
|
186
|
+
/**
|
|
187
|
+
* Clean up expired requests for all resources
|
|
188
|
+
*/
|
|
189
|
+
cleanup(): void;
|
|
190
|
+
/**
|
|
191
|
+
* Destroy the store and clean up resources
|
|
192
|
+
*/
|
|
193
|
+
destroy(): void;
|
|
194
|
+
private getOrCreateRateLimitInfo;
|
|
195
|
+
private cleanupExpiredRequests;
|
|
178
196
|
}
|
|
179
197
|
|
|
180
198
|
interface AdaptiveRateLimitStoreOptions {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
199
|
+
/** Global/default rate-limit config applied when a resource-specific override is not provided. */
|
|
200
|
+
defaultConfig?: RateLimitConfig;
|
|
201
|
+
/** Optional per-resource overrides. */
|
|
202
|
+
resourceConfigs?: Map<string, RateLimitConfig>;
|
|
203
|
+
/** Adaptive configuration for priority-based rate limiting. */
|
|
204
|
+
adaptiveConfig?: Partial<z.input<typeof AdaptiveConfigSchema>>;
|
|
187
205
|
}
|
|
188
206
|
/**
|
|
189
207
|
* In-memory rate limiting store with adaptive priority-based capacity allocation
|
|
190
208
|
*/
|
|
191
209
|
declare class AdaptiveRateLimitStore implements AdaptiveRateLimitStore$1 {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
210
|
+
private defaultConfig;
|
|
211
|
+
private resourceConfigs;
|
|
212
|
+
private activityMetrics;
|
|
213
|
+
private capacityCalculator;
|
|
214
|
+
private lastCapacityUpdate;
|
|
215
|
+
private cachedCapacity;
|
|
216
|
+
constructor(options?: AdaptiveRateLimitStoreOptions);
|
|
217
|
+
canProceed(resource: string, priority?: RequestPriority): Promise<boolean>;
|
|
218
|
+
record(resource: string, priority?: RequestPriority): Promise<void>;
|
|
219
|
+
getStatus(resource: string): Promise<{
|
|
220
|
+
remaining: number;
|
|
221
|
+
resetTime: Date;
|
|
222
|
+
limit: number;
|
|
223
|
+
adaptive?: {
|
|
224
|
+
userReserved: number;
|
|
225
|
+
backgroundMax: number;
|
|
226
|
+
backgroundPaused: boolean;
|
|
227
|
+
recentUserActivity: number;
|
|
228
|
+
reason: string;
|
|
229
|
+
};
|
|
230
|
+
}>;
|
|
231
|
+
reset(resource: string): Promise<void>;
|
|
232
|
+
getWaitTime(resource: string, priority?: RequestPriority): Promise<number>;
|
|
233
|
+
private calculateCurrentCapacity;
|
|
234
|
+
private getOrCreateActivityMetrics;
|
|
235
|
+
private getCurrentUsage;
|
|
236
|
+
private cleanupOldRequests;
|
|
237
|
+
private getResourceLimit;
|
|
238
|
+
private getDefaultCapacity;
|
|
221
239
|
}
|
|
222
240
|
|
|
223
|
-
export {
|
|
241
|
+
export {
|
|
242
|
+
AdaptiveRateLimitStore,
|
|
243
|
+
type AdaptiveRateLimitStoreOptions,
|
|
244
|
+
InMemoryCacheStore,
|
|
245
|
+
type InMemoryCacheStoreOptions,
|
|
246
|
+
InMemoryDedupeStore,
|
|
247
|
+
type InMemoryDedupeStoreOptions,
|
|
248
|
+
InMemoryRateLimitStore,
|
|
249
|
+
type InMemoryRateLimitStoreOptions,
|
|
250
|
+
};
|