@http-client-toolkit/store-memory 0.0.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/LICENSE +15 -0
- package/README.md +103 -0
- package/lib/index.cjs +761 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.cts +223 -0
- package/lib/index.d.ts +223 -0
- package/lib/index.js +752 -0
- package/lib/index.js.map +1 -0
- package/package.json +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Ally Murray
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @http-client-toolkit/store-memory
|
|
2
|
+
|
|
3
|
+
In-memory store implementations for [@http-client-toolkit/core](https://www.npmjs.com/package/@http-client-toolkit/core). Fast, zero-dependency stores for development, testing, or single-process production use.
|
|
4
|
+
|
|
5
|
+
Part of the [http-client-toolkit](https://github.com/AllyMurray/http-client-toolkit) monorepo.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @http-client-toolkit/core @http-client-toolkit/store-memory
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires Node.js >= 20.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { HttpClient } from '@http-client-toolkit/core';
|
|
19
|
+
import {
|
|
20
|
+
InMemoryCacheStore,
|
|
21
|
+
InMemoryDedupeStore,
|
|
22
|
+
InMemoryRateLimitStore,
|
|
23
|
+
} from '@http-client-toolkit/store-memory';
|
|
24
|
+
|
|
25
|
+
const client = new HttpClient({
|
|
26
|
+
cache: new InMemoryCacheStore(),
|
|
27
|
+
dedupe: new InMemoryDedupeStore(),
|
|
28
|
+
rateLimit: new InMemoryRateLimitStore(),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const data = await client.get<{ name: string }>(
|
|
32
|
+
'https://api.example.com/user/1',
|
|
33
|
+
);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Stores
|
|
37
|
+
|
|
38
|
+
### InMemoryCacheStore
|
|
39
|
+
|
|
40
|
+
LRU cache with TTL support and dual eviction limits (item count + memory usage).
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
const cache = new InMemoryCacheStore({
|
|
44
|
+
maxItems: 1000, // Default: 1000
|
|
45
|
+
maxMemoryBytes: 50_000_000, // Default: 50 MB
|
|
46
|
+
cleanupIntervalMs: 60_000, // Default: 60s. Set to 0 to disable.
|
|
47
|
+
evictionRatio: 0.1, // Default: 10% evicted when limits exceeded
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Call `cache.destroy()` when done to clear the cleanup timer.
|
|
52
|
+
|
|
53
|
+
### InMemoryDedupeStore
|
|
54
|
+
|
|
55
|
+
Prevents duplicate concurrent requests. If a request for the same hash is already in-flight, subsequent callers wait for the original to complete.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
const dedupe = new InMemoryDedupeStore({
|
|
59
|
+
jobTimeoutMs: 300_000, // Default: 5 minutes
|
|
60
|
+
cleanupIntervalMs: 60_000, // Default: 60s
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### InMemoryRateLimitStore
|
|
65
|
+
|
|
66
|
+
Sliding window rate limiter with optional per-resource configuration.
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
69
|
+
const rateLimit = new InMemoryRateLimitStore({
|
|
70
|
+
defaultConfig: { limit: 60, windowMs: 60_000 },
|
|
71
|
+
resourceConfigs: new Map([['slow-api', { limit: 10, windowMs: 60_000 }]]),
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### AdaptiveRateLimitStore
|
|
76
|
+
|
|
77
|
+
Priority-aware rate limiter that dynamically allocates capacity between user and background requests based on recent activity patterns.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { AdaptiveRateLimitStore } from '@http-client-toolkit/store-memory';
|
|
81
|
+
|
|
82
|
+
const rateLimit = new AdaptiveRateLimitStore({
|
|
83
|
+
defaultConfig: { limit: 200, windowMs: 3_600_000 },
|
|
84
|
+
resourceConfigs: new Map([['search', { limit: 50, windowMs: 60_000 }]]),
|
|
85
|
+
adaptiveConfig: {
|
|
86
|
+
highActivityThreshold: 10,
|
|
87
|
+
moderateActivityThreshold: 3,
|
|
88
|
+
monitoringWindowMs: 900_000,
|
|
89
|
+
maxUserScaling: 2.0,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
| Activity Level | Behavior |
|
|
95
|
+
| ------------------------ | ------------------------------------------------------------------- |
|
|
96
|
+
| **High** | Prioritizes user requests, pauses background if trend is increasing |
|
|
97
|
+
| **Moderate** | Balanced allocation with trend-aware scaling |
|
|
98
|
+
| **Low** | Scales up background capacity |
|
|
99
|
+
| **Sustained inactivity** | Gives full capacity to background |
|
|
100
|
+
|
|
101
|
+
## License
|
|
102
|
+
|
|
103
|
+
ISC
|