@j0hanz/fetch-url-mcp 1.11.7 → 1.11.8
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 +25 -33
- package/dist/http/health.d.ts.map +1 -1
- package/dist/http/health.js +0 -2
- package/dist/http/native.d.ts.map +1 -1
- package/dist/http/native.js +0 -25
- package/dist/lib/config.d.ts +0 -7
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +0 -9
- package/dist/lib/core.d.ts +0 -3
- package/dist/lib/core.d.ts.map +1 -1
- package/dist/lib/core.js +0 -7
- package/dist/lib/fetch-pipeline.d.ts +1 -14
- package/dist/lib/fetch-pipeline.d.ts.map +1 -1
- package/dist/lib/fetch-pipeline.js +4 -147
- package/dist/lib/http.d.ts +0 -3
- package/dist/lib/http.d.ts.map +1 -1
- package/dist/lib/http.js +2 -105
- package/dist/lib/utils.d.ts +0 -2
- package/dist/lib/utils.d.ts.map +1 -1
- package/dist/lib/utils.js +1 -36
- package/dist/resources/index.d.ts +1 -23
- package/dist/resources/index.d.ts.map +1 -1
- package/dist/resources/index.js +3 -294
- package/dist/schemas.d.ts +0 -14
- package/dist/schemas.d.ts.map +1 -1
- package/dist/schemas.js +1 -77
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +1 -2
- package/dist/tools/fetch-url.d.ts +0 -2
- package/dist/tools/fetch-url.d.ts.map +1 -1
- package/dist/tools/fetch-url.js +12 -43
- package/dist/transform/shared.js +4 -4
- package/dist/transform/transform.d.ts +1 -1
- package/dist/transform/transform.d.ts.map +1 -1
- package/dist/transform/transform.js +10 -10
- package/dist/transform/types.d.ts +2 -2
- package/dist/transform/types.d.ts.map +1 -1
- package/dist/transform/worker-pool.d.ts +3 -3
- package/dist/transform/worker-pool.d.ts.map +1 -1
- package/dist/transform/worker-pool.js +2 -2
- package/package.json +1 -1
- package/dist/lib/cache.d.ts +0 -48
- package/dist/lib/cache.d.ts.map +0 -1
- package/dist/lib/cache.js +0 -264
package/dist/lib/cache.js
DELETED
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'node:events';
|
|
2
|
-
import { config } from './config.js';
|
|
3
|
-
import { logWarn } from './core.js';
|
|
4
|
-
import { getErrorMessage, sha256Hex, stableStringify as stableJsonStringify, } from './utils.js';
|
|
5
|
-
const PRIMARY_HASH_LENGTH = 32;
|
|
6
|
-
const VARY_HASH_LENGTH = 16;
|
|
7
|
-
const STDIO_CACHE_SCOPE_ID = 'stdio';
|
|
8
|
-
export function toCacheScopeId(sessionId) {
|
|
9
|
-
return sessionId ? `session:${sessionId}` : STDIO_CACHE_SCOPE_ID;
|
|
10
|
-
}
|
|
11
|
-
function normalizeScopeIds(scopeIds) {
|
|
12
|
-
const normalized = (scopeIds ?? [STDIO_CACHE_SCOPE_ID]).filter((value) => typeof value === 'string' && value.length > 0);
|
|
13
|
-
return normalized.length > 0
|
|
14
|
-
? [...new Set(normalized)]
|
|
15
|
-
: [STDIO_CACHE_SCOPE_ID];
|
|
16
|
-
}
|
|
17
|
-
export function createCacheKey(namespace, url, vary) {
|
|
18
|
-
if (!namespace || !url)
|
|
19
|
-
return null;
|
|
20
|
-
const urlHash = sha256Hex(url).substring(0, PRIMARY_HASH_LENGTH);
|
|
21
|
-
if (!vary)
|
|
22
|
-
return `${namespace}:${urlHash}`;
|
|
23
|
-
let varyString;
|
|
24
|
-
if (typeof vary === 'string') {
|
|
25
|
-
varyString = vary;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
try {
|
|
29
|
-
varyString = stableJsonStringify(vary);
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
const varyHash = varyString
|
|
36
|
-
? sha256Hex(varyString).substring(0, VARY_HASH_LENGTH)
|
|
37
|
-
: undefined;
|
|
38
|
-
return varyHash
|
|
39
|
-
? `${namespace}:${urlHash}.${varyHash}`
|
|
40
|
-
: `${namespace}:${urlHash}`;
|
|
41
|
-
}
|
|
42
|
-
export function parseCacheKey(cacheKey) {
|
|
43
|
-
if (!cacheKey)
|
|
44
|
-
return null;
|
|
45
|
-
const separatorIndex = cacheKey.indexOf(':');
|
|
46
|
-
if (separatorIndex === -1)
|
|
47
|
-
return null;
|
|
48
|
-
const namespace = cacheKey.slice(0, separatorIndex);
|
|
49
|
-
const urlHash = cacheKey.slice(separatorIndex + 1);
|
|
50
|
-
if (!namespace || !urlHash)
|
|
51
|
-
return null;
|
|
52
|
-
return { namespace, urlHash };
|
|
53
|
-
}
|
|
54
|
-
class InMemoryCacheStore {
|
|
55
|
-
max = config.cache.maxKeys;
|
|
56
|
-
maxBytes = config.cache.maxSizeBytes;
|
|
57
|
-
ttlMs = config.cache.ttl * 1000;
|
|
58
|
-
entries = new Map();
|
|
59
|
-
updateEmitter = new EventEmitter();
|
|
60
|
-
currentBytes = 0;
|
|
61
|
-
isEnabled() {
|
|
62
|
-
return config.cache.enabled;
|
|
63
|
-
}
|
|
64
|
-
isExpired(entry, now = Date.now()) {
|
|
65
|
-
return entry.expiresAtMs <= now;
|
|
66
|
-
}
|
|
67
|
-
keys() {
|
|
68
|
-
if (!this.isEnabled())
|
|
69
|
-
return [];
|
|
70
|
-
const now = Date.now();
|
|
71
|
-
const result = [];
|
|
72
|
-
for (const [key, entry] of this.entries) {
|
|
73
|
-
if (!this.isExpired(entry, now))
|
|
74
|
-
result.push(key);
|
|
75
|
-
}
|
|
76
|
-
return result;
|
|
77
|
-
}
|
|
78
|
-
onUpdate(listener) {
|
|
79
|
-
const safeListener = (event) => {
|
|
80
|
-
try {
|
|
81
|
-
const result = listener(event);
|
|
82
|
-
if (result instanceof Promise) {
|
|
83
|
-
void result.catch((error) => {
|
|
84
|
-
this.logError('Cache update listener failed (async)', event.cacheKey, error);
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
catch (error) {
|
|
89
|
-
this.logError('Cache update listener failed', event.cacheKey, error);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
this.updateEmitter.on('update', safeListener);
|
|
93
|
-
return () => {
|
|
94
|
-
this.updateEmitter.off('update', safeListener);
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
get(cacheKey, options) {
|
|
98
|
-
if (!cacheKey || (!this.isEnabled() && !options?.force))
|
|
99
|
-
return undefined;
|
|
100
|
-
const entry = this.entries.get(cacheKey);
|
|
101
|
-
if (!entry)
|
|
102
|
-
return undefined;
|
|
103
|
-
const now = Date.now();
|
|
104
|
-
if (this.isExpired(entry, now)) {
|
|
105
|
-
const removed = this.delete(cacheKey);
|
|
106
|
-
// listChanged=false: lazy eviction on read is silent — only writes change
|
|
107
|
-
// the list. Clients must not rely on list-changed events from reads.
|
|
108
|
-
this.notify(cacheKey, false, removed?.scopeIds);
|
|
109
|
-
return undefined;
|
|
110
|
-
}
|
|
111
|
-
const scopeId = options?.scopeId;
|
|
112
|
-
if (scopeId && !normalizeScopeIds(entry.scopeIds).includes(scopeId)) {
|
|
113
|
-
entry.scopeIds = normalizeScopeIds([
|
|
114
|
-
...normalizeScopeIds(entry.scopeIds),
|
|
115
|
-
scopeId,
|
|
116
|
-
]);
|
|
117
|
-
this.notify(cacheKey, true, [scopeId]);
|
|
118
|
-
}
|
|
119
|
-
// Refresh LRU position
|
|
120
|
-
this.entries.delete(cacheKey);
|
|
121
|
-
this.entries.set(cacheKey, entry);
|
|
122
|
-
return entry;
|
|
123
|
-
}
|
|
124
|
-
delete(cacheKey) {
|
|
125
|
-
const entry = this.entries.get(cacheKey);
|
|
126
|
-
if (entry) {
|
|
127
|
-
this.currentBytes -= entry.content.length;
|
|
128
|
-
this.entries.delete(cacheKey);
|
|
129
|
-
return entry;
|
|
130
|
-
}
|
|
131
|
-
return undefined;
|
|
132
|
-
}
|
|
133
|
-
evictOldestEntry() {
|
|
134
|
-
const firstKey = this.entries.keys().next();
|
|
135
|
-
return !firstKey.done ? this.delete(firstKey.value) : undefined;
|
|
136
|
-
}
|
|
137
|
-
ensureCapacity(cacheKey, entrySize) {
|
|
138
|
-
let listChanged = false;
|
|
139
|
-
const scopeIds = new Set();
|
|
140
|
-
while (this.currentBytes + entrySize > this.maxBytes) {
|
|
141
|
-
const evicted = this.evictOldestEntry();
|
|
142
|
-
if (evicted) {
|
|
143
|
-
listChanged = true;
|
|
144
|
-
for (const scopeId of normalizeScopeIds(evicted.scopeIds)) {
|
|
145
|
-
scopeIds.add(scopeId);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
break;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return { ok: true, listChanged, scopeIds: [...scopeIds] };
|
|
153
|
-
}
|
|
154
|
-
set(cacheKey, content, metadata, options) {
|
|
155
|
-
if (!cacheKey || !content)
|
|
156
|
-
return;
|
|
157
|
-
if (!this.isEnabled() && !options?.force)
|
|
158
|
-
return;
|
|
159
|
-
const now = Date.now();
|
|
160
|
-
const expiresAtMs = now + this.ttlMs;
|
|
161
|
-
const entrySize = content.length;
|
|
162
|
-
// Reject oversized entries before deleting the old one to avoid data loss
|
|
163
|
-
if (entrySize > this.maxBytes) {
|
|
164
|
-
logWarn('Cache entry exceeds max size', {
|
|
165
|
-
key: cacheKey,
|
|
166
|
-
size: entrySize,
|
|
167
|
-
max: this.maxBytes,
|
|
168
|
-
});
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
const existingEntry = this.entries.get(cacheKey);
|
|
172
|
-
const isUpdate = existingEntry !== undefined;
|
|
173
|
-
if (isUpdate) {
|
|
174
|
-
this.delete(cacheKey);
|
|
175
|
-
}
|
|
176
|
-
const capacity = this.ensureCapacity(cacheKey, entrySize);
|
|
177
|
-
if (!capacity.ok)
|
|
178
|
-
return;
|
|
179
|
-
let listChanged = !isUpdate || capacity.listChanged;
|
|
180
|
-
const nextScopeIds = normalizeScopeIds([
|
|
181
|
-
...(existingEntry?.scopeIds ?? []),
|
|
182
|
-
...(metadata.scopeIds ?? []),
|
|
183
|
-
]);
|
|
184
|
-
const entry = {
|
|
185
|
-
url: metadata.url,
|
|
186
|
-
content,
|
|
187
|
-
fetchedAt: new Date(now).toISOString(),
|
|
188
|
-
expiresAt: new Date(expiresAtMs).toISOString(),
|
|
189
|
-
expiresAtMs,
|
|
190
|
-
scopeIds: nextScopeIds,
|
|
191
|
-
...(metadata.title ? { title: metadata.title } : {}),
|
|
192
|
-
};
|
|
193
|
-
this.entries.set(cacheKey, entry);
|
|
194
|
-
this.currentBytes += entrySize;
|
|
195
|
-
// Eviction (LRU: first insertion-order key) - Count based
|
|
196
|
-
if (this.entries.size > this.max && this.evictOldestEntry()) {
|
|
197
|
-
listChanged = true;
|
|
198
|
-
}
|
|
199
|
-
this.notify(cacheKey, listChanged, [
|
|
200
|
-
...new Set([...capacity.scopeIds, ...nextScopeIds]),
|
|
201
|
-
]);
|
|
202
|
-
}
|
|
203
|
-
notify(cacheKey, listChanged, scopeIds) {
|
|
204
|
-
if (this.updateEmitter.listenerCount('update') === 0)
|
|
205
|
-
return;
|
|
206
|
-
const parts = parseCacheKey(cacheKey);
|
|
207
|
-
if (!parts)
|
|
208
|
-
return;
|
|
209
|
-
this.updateEmitter.emit('update', {
|
|
210
|
-
cacheKey,
|
|
211
|
-
...parts,
|
|
212
|
-
listChanged,
|
|
213
|
-
scopeIds: normalizeScopeIds(scopeIds),
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Read an entry without updating its LRU position.
|
|
218
|
-
* Use this for metadata access (e.g. resource listing) to avoid polluting the
|
|
219
|
-
* eviction order; expired entries are treated as absent but not evicted here.
|
|
220
|
-
*/
|
|
221
|
-
peek(cacheKey) {
|
|
222
|
-
if (!cacheKey)
|
|
223
|
-
return undefined;
|
|
224
|
-
const entry = this.entries.get(cacheKey);
|
|
225
|
-
if (!entry)
|
|
226
|
-
return undefined;
|
|
227
|
-
if (this.isExpired(entry))
|
|
228
|
-
return undefined;
|
|
229
|
-
return entry;
|
|
230
|
-
}
|
|
231
|
-
logError(message, cacheKey, error) {
|
|
232
|
-
logWarn(message, {
|
|
233
|
-
key: cacheKey.length > 100 ? cacheKey.slice(0, 100) : cacheKey,
|
|
234
|
-
error: getErrorMessage(error),
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
const store = new InMemoryCacheStore();
|
|
239
|
-
export function onCacheUpdate(listener) {
|
|
240
|
-
return store.onUpdate(listener);
|
|
241
|
-
}
|
|
242
|
-
export function get(cacheKey, options) {
|
|
243
|
-
return store.get(cacheKey, options);
|
|
244
|
-
}
|
|
245
|
-
export function set(cacheKey, content, metadata, options) {
|
|
246
|
-
store.set(cacheKey, content, metadata, options);
|
|
247
|
-
}
|
|
248
|
-
export function keys() {
|
|
249
|
-
return store.keys();
|
|
250
|
-
}
|
|
251
|
-
export function getEntryMeta(cacheKey) {
|
|
252
|
-
const entry = store.peek(cacheKey);
|
|
253
|
-
if (!entry)
|
|
254
|
-
return undefined;
|
|
255
|
-
return {
|
|
256
|
-
url: entry.url,
|
|
257
|
-
scopeIds: normalizeScopeIds(entry.scopeIds),
|
|
258
|
-
...(entry.title !== undefined ? { title: entry.title } : {}),
|
|
259
|
-
...(entry.fetchedAt ? { fetchedAt: entry.fetchedAt } : {}),
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
export function isEnabled() {
|
|
263
|
-
return store.isEnabled();
|
|
264
|
-
}
|