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