@j0hanz/fetch-url-mcp 1.11.5 → 1.11.6

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.
@@ -1,7 +1,7 @@
1
1
  import { freemem, hostname, totalmem } from 'node:os';
2
2
  import { monitorEventLoopDelay, performance } from 'node:perf_hooks';
3
3
  import process from 'node:process';
4
- import { keys as cacheKeys } from '../lib/core.js';
4
+ import { keys as cacheKeys } from '../lib/cache.js';
5
5
  import { config, serverVersion } from '../lib/core.js';
6
6
  import { getTransformPoolStats } from '../transform/transform.js';
7
7
  import { sendJson } from './helpers.js';
@@ -1 +1 @@
1
- {"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../../src/http/native.ts"],"names":[],"mappings":"AAooCA,wBAAsB,eAAe,IAAI,OAAO,CAAC;IAC/C,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5C,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC,CA0DD"}
1
+ {"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../../src/http/native.ts"],"names":[],"mappings":"AAmoCA,wBAAsB,eAAe,IAAI,OAAO,CAAC;IAC/C,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5C,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd,CAAC,CA0DD"}
@@ -7,14 +7,10 @@ import { hostname } from 'node:os';
7
7
  import process from 'node:process';
8
8
  import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
9
9
  import { isInitializeRequest } from '@modelcontextprotocol/sdk/types.js';
10
- import { config, enableHttpMode } from '../lib/core.js';
11
- import { logError, logInfo, registerMcpSessionServer, runWithRequestContext, } from '../lib/core.js';
12
- import { composeCloseHandlers, createSessionStore, createSlotTracker, ensureSessionCapacity, reserveSessionSlot, startSessionCleanupLoop, } from '../lib/core.js';
10
+ import { composeCloseHandlers, config, createSessionStore, createSlotTracker, enableHttpMode, ensureSessionCapacity, logError, logInfo, registerMcpSessionServer, reserveSessionSlot, runWithRequestContext, startSessionCleanupLoop, } from '../lib/core.js';
13
11
  import { handleDownload } from '../lib/http.js';
14
12
  import { acceptsEventStream, acceptsJsonAndEventStream, isJsonRpcBatchRequest, isMcpMessageBody, isMcpRequestBody, } from '../lib/mcp-interop.js';
15
- import { toError } from '../lib/utils.js';
16
- import { applyHttpServerTuning, drainConnectionsOnShutdown, } from '../lib/utils.js';
17
- import { isObject } from '../lib/utils.js';
13
+ import { applyHttpServerTuning, drainConnectionsOnShutdown, isObject, toError, } from '../lib/utils.js';
18
14
  import { createMcpServerForHttpSession } from '../server.js';
19
15
  import { applyInsufficientScopeAuthHeaders, applyUnauthorizedAuthHeaders, assertHttpModeConfiguration, authService, buildAuthFingerprint, buildProtectedResourceMetadataDocument, corsPolicy, DEFAULT_MCP_PROTOCOL_VERSION, ensureMcpProtocolVersion, hostOriginPolicy, isInsufficientScopeError, isOAuthMetadataEnabled, isProtectedResourceMetadataPath, SUPPORTED_MCP_PROTOCOL_VERSIONS, } from './auth.js';
20
16
  import { disableEventLoopMonitoring, isVerboseHealthRequest, resetEventLoopMonitoring, sendHealthRouteResponse, shouldHandleHealthRoute, } from './health.js';
@@ -0,0 +1,48 @@
1
+ export declare function toCacheScopeId(sessionId?: string): string;
2
+ interface CacheEntry {
3
+ url: string;
4
+ title?: string;
5
+ content: string;
6
+ fetchedAt: string;
7
+ expiresAt: string;
8
+ scopeIds?: string[];
9
+ }
10
+ interface CacheKeyParts {
11
+ namespace: string;
12
+ urlHash: string;
13
+ }
14
+ interface CacheSetOptions {
15
+ force?: boolean;
16
+ }
17
+ interface CacheGetOptions {
18
+ force?: boolean;
19
+ scopeId?: string;
20
+ }
21
+ interface CacheEntryMetadata {
22
+ url: string;
23
+ title?: string;
24
+ scopeIds?: string[];
25
+ }
26
+ interface CacheUpdateEvent {
27
+ cacheKey: string;
28
+ namespace: string;
29
+ urlHash: string;
30
+ listChanged: boolean;
31
+ scopeIds: string[];
32
+ }
33
+ type CacheUpdateListener = (event: CacheUpdateEvent) => unknown;
34
+ export declare function createCacheKey(namespace: string, url: string, vary?: Record<string, unknown> | string): string | null;
35
+ export declare function parseCacheKey(cacheKey: string): CacheKeyParts | null;
36
+ export declare function onCacheUpdate(listener: CacheUpdateListener): () => void;
37
+ export declare function get(cacheKey: string | null, options?: CacheGetOptions): CacheEntry | undefined;
38
+ export declare function set(cacheKey: string | null, content: string, metadata: CacheEntryMetadata, options?: CacheSetOptions): void;
39
+ export declare function keys(): readonly string[];
40
+ export declare function getEntryMeta(cacheKey: string): {
41
+ url: string;
42
+ title?: string;
43
+ fetchedAt?: string;
44
+ scopeIds: string[];
45
+ } | undefined;
46
+ export declare function isEnabled(): boolean;
47
+ export {};
48
+ //# sourceMappingURL=cache.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/lib/cache.ts"],"names":[],"mappings":"AAcA,wBAAgB,cAAc,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,CAEzD;AAYD,UAAU,UAAU;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;CACrB;AACD,UAAU,aAAa;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AACD,UAAU,eAAe;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AACD,UAAU,eAAe;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AACD,U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AAS,CASZ;AACD,wBAAgB,SAAS,IAAI,OAAO,CAEnC"}
@@ -0,0 +1,273 @@
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
+ const varyString = typeof vary === 'string'
24
+ ? vary
25
+ : (() => {
26
+ try {
27
+ return stableJsonStringify(vary);
28
+ }
29
+ catch {
30
+ return null;
31
+ }
32
+ })();
33
+ if (varyString === null)
34
+ return null;
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 wrapped = (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', wrapped);
93
+ return () => {
94
+ this.updateEmitter.off('update', wrapped);
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
+ if (entrySize > this.maxBytes) {
139
+ logWarn('Cache entry exceeds max size', {
140
+ key: cacheKey,
141
+ size: entrySize,
142
+ max: this.maxBytes,
143
+ });
144
+ return { ok: false, listChanged: false, scopeIds: [] };
145
+ }
146
+ let listChanged = false;
147
+ const scopeIds = new Set();
148
+ while (this.currentBytes + entrySize > this.maxBytes) {
149
+ const evicted = this.evictOldestEntry();
150
+ if (evicted) {
151
+ listChanged = true;
152
+ for (const scopeId of normalizeScopeIds(evicted.scopeIds)) {
153
+ scopeIds.add(scopeId);
154
+ }
155
+ }
156
+ else {
157
+ break;
158
+ }
159
+ }
160
+ return { ok: true, listChanged, scopeIds: [...scopeIds] };
161
+ }
162
+ set(cacheKey, content, metadata, options) {
163
+ if (!cacheKey || !content)
164
+ return;
165
+ if (!this.isEnabled() && !options?.force)
166
+ return;
167
+ const now = Date.now();
168
+ const expiresAtMs = now + this.ttlMs;
169
+ const entrySize = content.length;
170
+ // Reject oversized entries before deleting the old one to avoid data loss
171
+ if (entrySize > this.maxBytes) {
172
+ logWarn('Cache entry exceeds max size', {
173
+ key: cacheKey,
174
+ size: entrySize,
175
+ max: this.maxBytes,
176
+ });
177
+ return;
178
+ }
179
+ const existingEntry = this.entries.get(cacheKey);
180
+ const isUpdate = existingEntry !== undefined;
181
+ const existingScopeIds = normalizeScopeIds(existingEntry?.scopeIds);
182
+ if (isUpdate) {
183
+ this.delete(cacheKey);
184
+ }
185
+ const capacity = this.ensureCapacity(cacheKey, entrySize);
186
+ if (!capacity.ok)
187
+ return;
188
+ let listChanged = !isUpdate || capacity.listChanged;
189
+ const nextScopeIds = normalizeScopeIds([
190
+ ...existingScopeIds,
191
+ ...normalizeScopeIds(metadata.scopeIds),
192
+ ]);
193
+ const entry = {
194
+ url: metadata.url,
195
+ content,
196
+ fetchedAt: new Date(now).toISOString(),
197
+ expiresAt: new Date(expiresAtMs).toISOString(),
198
+ expiresAtMs,
199
+ scopeIds: nextScopeIds,
200
+ ...(metadata.title ? { title: metadata.title } : {}),
201
+ };
202
+ this.entries.set(cacheKey, entry);
203
+ this.currentBytes += entrySize;
204
+ // Eviction (LRU: first insertion-order key) - Count based
205
+ if (this.entries.size > this.max && this.evictOldestEntry()) {
206
+ listChanged = true;
207
+ }
208
+ this.notify(cacheKey, listChanged, [
209
+ ...new Set([...capacity.scopeIds, ...nextScopeIds]),
210
+ ]);
211
+ }
212
+ notify(cacheKey, listChanged, scopeIds) {
213
+ if (this.updateEmitter.listenerCount('update') === 0)
214
+ return;
215
+ const parts = parseCacheKey(cacheKey);
216
+ if (!parts)
217
+ return;
218
+ this.updateEmitter.emit('update', {
219
+ cacheKey,
220
+ ...parts,
221
+ listChanged,
222
+ scopeIds: normalizeScopeIds(scopeIds),
223
+ });
224
+ }
225
+ /**
226
+ * Read an entry without updating its LRU position.
227
+ * Use this for metadata access (e.g. resource listing) to avoid polluting the
228
+ * eviction order; expired entries are treated as absent but not evicted here.
229
+ */
230
+ peek(cacheKey) {
231
+ if (!cacheKey)
232
+ return undefined;
233
+ const entry = this.entries.get(cacheKey);
234
+ if (!entry)
235
+ return undefined;
236
+ if (this.isExpired(entry))
237
+ return undefined;
238
+ return entry;
239
+ }
240
+ logError(message, cacheKey, error) {
241
+ logWarn(message, {
242
+ key: cacheKey.length > 100 ? cacheKey.slice(0, 100) : cacheKey,
243
+ error: getErrorMessage(error),
244
+ });
245
+ }
246
+ }
247
+ const store = new InMemoryCacheStore();
248
+ export function onCacheUpdate(listener) {
249
+ return store.onUpdate(listener);
250
+ }
251
+ export function get(cacheKey, options) {
252
+ return store.get(cacheKey, options);
253
+ }
254
+ export function set(cacheKey, content, metadata, options) {
255
+ store.set(cacheKey, content, metadata, options);
256
+ }
257
+ export function keys() {
258
+ return store.keys();
259
+ }
260
+ export function getEntryMeta(cacheKey) {
261
+ const entry = store.peek(cacheKey);
262
+ if (!entry)
263
+ return undefined;
264
+ return {
265
+ url: entry.url,
266
+ scopeIds: normalizeScopeIds(entry.scopeIds),
267
+ ...(entry.title !== undefined ? { title: entry.title } : {}),
268
+ ...(entry.fetchedAt ? { fetchedAt: entry.fetchedAt } : {}),
269
+ };
270
+ }
271
+ export function isEnabled() {
272
+ return store.isEnabled();
273
+ }
@@ -2,47 +2,8 @@ import { type McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
2
  import type { SessionEntry } from './session.js';
3
3
  import type { SessionStore } from './session.js';
4
4
  export { config, enableHttpMode, serverVersion } from './config.js';
5
+ export { createCacheKey, get, getEntryMeta, isEnabled, keys, onCacheUpdate, parseCacheKey, set, } from './cache.js';
5
6
  type McpLogLevel = 'debug' | 'info' | 'notice' | 'warning' | 'error' | 'critical' | 'alert' | 'emergency';
6
- interface CacheEntry {
7
- url: string;
8
- title?: string;
9
- content: string;
10
- fetchedAt: string;
11
- expiresAt: string;
12
- }
13
- interface CacheKeyParts {
14
- namespace: string;
15
- urlHash: string;
16
- }
17
- interface CacheSetOptions {
18
- force?: boolean;
19
- }
20
- interface CacheGetOptions {
21
- force?: boolean;
22
- }
23
- interface CacheEntryMetadata {
24
- url: string;
25
- title?: string;
26
- }
27
- interface CacheUpdateEvent {
28
- cacheKey: string;
29
- namespace: string;
30
- urlHash: string;
31
- listChanged: boolean;
32
- }
33
- type CacheUpdateListener = (event: CacheUpdateEvent) => unknown;
34
- export declare function createCacheKey(namespace: string, url: string, vary?: Record<string, unknown> | string): string | null;
35
- export declare function parseCacheKey(cacheKey: string): CacheKeyParts | null;
36
- export declare function onCacheUpdate(listener: CacheUpdateListener): () => void;
37
- export declare function get(cacheKey: string | null, options?: CacheGetOptions): CacheEntry | undefined;
38
- export declare function set(cacheKey: string | null, content: string, metadata: CacheEntryMetadata, options?: CacheSetOptions): void;
39
- export declare function keys(): readonly string[];
40
- export declare function getEntryMeta(cacheKey: string): {
41
- url: string;
42
- title?: string;
43
- fetchedAt?: string;
44
- } | undefined;
45
- export declare function isEnabled(): boolean;
46
7
  type LogMetadata = Record<string, unknown>;
47
8
  interface RequestContext {
48
9
  readonly requestId: string;
@@ -1 +1 @@
1
- {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../src/lib/core.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,yCAAyC,CAAC;AAGzE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AASjD,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEpE,KAAK,WAAW,GACZ,OAAO,GACP,MAAM,GACN,QAAQ,GACR,SAAS,GACT,OAAO,GACP,UAAU,GACV,OAAO,GACP,WAAW,CAAC;AAKhB,UAAU,UAAU;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CACnB;AACD,UAAU,aAAa;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AACD,UAAU,eAAe;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AACD,UAAU,eAAe;IACvB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AACD,UAAU,kBAAkB;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC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,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAQhD;AACD,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EACL,oBAAoB,EACpB,kBAAkB,EAClB,iBAAiB,EACjB,qBAAqB,EACrB,kBAAkB,GACnB,MAAM,cAAc,CAAC;AAmJtB,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,YAAY,EACnB,YAAY,EAAE,MAAM,EACpB,OAAO,CAAC,EAAE;IACR,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IACjE,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,GACA,eAAe,CAOjB"}
package/dist/lib/core.js CHANGED
@@ -1,242 +1,11 @@
1
1
  import { AsyncLocalStorage } from 'node:async_hooks';
2
- import { EventEmitter } from 'node:events';
3
2
  import process from 'node:process';
4
3
  import { getSystemErrorMessage, inspect, stripVTControlCharacters, } from 'node:util';
5
4
  import {} from '@modelcontextprotocol/sdk/server/mcp.js';
6
5
  import { config } from './config.js';
7
- import { getErrorMessage, isAbortError, sha256Hex, stableStringify as stableJsonStringify, startAbortableIntervalLoop, } from './utils.js';
6
+ import { getErrorMessage, isAbortError, startAbortableIntervalLoop, } from './utils.js';
8
7
  export { config, enableHttpMode, serverVersion } from './config.js';
9
- const PRIMARY_HASH_LENGTH = 32;
10
- const VARY_HASH_LENGTH = 16;
11
- export function createCacheKey(namespace, url, vary) {
12
- if (!namespace || !url)
13
- return null;
14
- const urlHash = sha256Hex(url).substring(0, PRIMARY_HASH_LENGTH);
15
- if (!vary)
16
- return `${namespace}:${urlHash}`;
17
- const varyString = typeof vary === 'string'
18
- ? vary
19
- : (() => {
20
- try {
21
- return stableJsonStringify(vary);
22
- }
23
- catch {
24
- return null;
25
- }
26
- })();
27
- if (varyString === null)
28
- return null;
29
- const varyHash = varyString
30
- ? sha256Hex(varyString).substring(0, VARY_HASH_LENGTH)
31
- : undefined;
32
- return varyHash
33
- ? `${namespace}:${urlHash}.${varyHash}`
34
- : `${namespace}:${urlHash}`;
35
- }
36
- export function parseCacheKey(cacheKey) {
37
- if (!cacheKey)
38
- return null;
39
- const separatorIndex = cacheKey.indexOf(':');
40
- if (separatorIndex === -1)
41
- return null;
42
- const namespace = cacheKey.slice(0, separatorIndex);
43
- const urlHash = cacheKey.slice(separatorIndex + 1);
44
- if (!namespace || !urlHash)
45
- return null;
46
- return { namespace, urlHash };
47
- }
48
- class InMemoryCacheStore {
49
- max = config.cache.maxKeys;
50
- maxBytes = config.cache.maxSizeBytes;
51
- ttlMs = config.cache.ttl * 1000;
52
- entries = new Map();
53
- updateEmitter = new EventEmitter();
54
- currentBytes = 0;
55
- isEnabled() {
56
- return config.cache.enabled;
57
- }
58
- isExpired(entry, now = Date.now()) {
59
- return entry.expiresAtMs <= now;
60
- }
61
- keys() {
62
- if (!this.isEnabled())
63
- return [];
64
- const now = Date.now();
65
- const result = [];
66
- for (const [key, entry] of this.entries) {
67
- if (!this.isExpired(entry, now))
68
- result.push(key);
69
- }
70
- return result;
71
- }
72
- onUpdate(listener) {
73
- const wrapped = (event) => {
74
- try {
75
- const result = listener(event);
76
- if (result instanceof Promise) {
77
- void result.catch((error) => {
78
- this.logError('Cache update listener failed (async)', event.cacheKey, error);
79
- });
80
- }
81
- }
82
- catch (error) {
83
- this.logError('Cache update listener failed', event.cacheKey, error);
84
- }
85
- };
86
- this.updateEmitter.on('update', wrapped);
87
- return () => {
88
- this.updateEmitter.off('update', wrapped);
89
- };
90
- }
91
- get(cacheKey, options) {
92
- if (!cacheKey || (!this.isEnabled() && !options?.force))
93
- return undefined;
94
- const entry = this.entries.get(cacheKey);
95
- if (!entry)
96
- return undefined;
97
- const now = Date.now();
98
- if (this.isExpired(entry, now)) {
99
- this.delete(cacheKey);
100
- // listChanged=false: lazy eviction on read is silent — only writes change
101
- // the list. Clients must not rely on list-changed events from reads.
102
- this.notify(cacheKey, false);
103
- return undefined;
104
- }
105
- // Refresh LRU position
106
- this.entries.delete(cacheKey);
107
- this.entries.set(cacheKey, entry);
108
- return entry;
109
- }
110
- delete(cacheKey) {
111
- const entry = this.entries.get(cacheKey);
112
- if (entry) {
113
- this.currentBytes -= entry.content.length;
114
- this.entries.delete(cacheKey);
115
- return true;
116
- }
117
- return false;
118
- }
119
- evictOldestEntry() {
120
- const firstKey = this.entries.keys().next();
121
- return !firstKey.done && this.delete(firstKey.value);
122
- }
123
- ensureCapacity(cacheKey, entrySize) {
124
- if (entrySize > this.maxBytes) {
125
- logWarn('Cache entry exceeds max size', {
126
- key: cacheKey,
127
- size: entrySize,
128
- max: this.maxBytes,
129
- });
130
- return { ok: false, listChanged: false };
131
- }
132
- let listChanged = false;
133
- while (this.currentBytes + entrySize > this.maxBytes) {
134
- if (this.evictOldestEntry()) {
135
- listChanged = true;
136
- }
137
- else {
138
- break;
139
- }
140
- }
141
- return { ok: true, listChanged };
142
- }
143
- set(cacheKey, content, metadata, options) {
144
- if (!cacheKey || !content)
145
- return;
146
- if (!this.isEnabled() && !options?.force)
147
- return;
148
- const now = Date.now();
149
- const expiresAtMs = now + this.ttlMs;
150
- const entrySize = content.length;
151
- // Reject oversized entries before deleting the old one to avoid data loss
152
- if (entrySize > this.maxBytes) {
153
- logWarn('Cache entry exceeds max size', {
154
- key: cacheKey,
155
- size: entrySize,
156
- max: this.maxBytes,
157
- });
158
- return;
159
- }
160
- const isUpdate = this.entries.has(cacheKey);
161
- if (isUpdate) {
162
- this.delete(cacheKey);
163
- }
164
- const capacity = this.ensureCapacity(cacheKey, entrySize);
165
- if (!capacity.ok)
166
- return;
167
- let listChanged = !isUpdate || capacity.listChanged;
168
- const entry = {
169
- url: metadata.url,
170
- content,
171
- fetchedAt: new Date(now).toISOString(),
172
- expiresAt: new Date(expiresAtMs).toISOString(),
173
- expiresAtMs,
174
- ...(metadata.title ? { title: metadata.title } : {}),
175
- };
176
- this.entries.set(cacheKey, entry);
177
- this.currentBytes += entrySize;
178
- // Eviction (LRU: first insertion-order key) - Count based
179
- if (this.entries.size > this.max && this.evictOldestEntry()) {
180
- listChanged = true;
181
- }
182
- this.notify(cacheKey, listChanged);
183
- }
184
- notify(cacheKey, listChanged) {
185
- if (this.updateEmitter.listenerCount('update') === 0)
186
- return;
187
- const parts = parseCacheKey(cacheKey);
188
- if (!parts)
189
- return;
190
- this.updateEmitter.emit('update', { cacheKey, ...parts, listChanged });
191
- }
192
- /**
193
- * Read an entry without updating its LRU position.
194
- * Use this for metadata access (e.g. resource listing) to avoid polluting the
195
- * eviction order; expired entries are treated as absent but not evicted here.
196
- */
197
- peek(cacheKey) {
198
- if (!cacheKey)
199
- return undefined;
200
- const entry = this.entries.get(cacheKey);
201
- if (!entry)
202
- return undefined;
203
- if (this.isExpired(entry))
204
- return undefined;
205
- return entry;
206
- }
207
- logError(message, cacheKey, error) {
208
- logWarn(message, {
209
- key: cacheKey.length > 100 ? cacheKey.slice(0, 100) : cacheKey,
210
- error: getErrorMessage(error),
211
- });
212
- }
213
- }
214
- const store = new InMemoryCacheStore();
215
- export function onCacheUpdate(listener) {
216
- return store.onUpdate(listener);
217
- }
218
- export function get(cacheKey, options) {
219
- return store.get(cacheKey, options);
220
- }
221
- export function set(cacheKey, content, metadata, options) {
222
- store.set(cacheKey, content, metadata, options);
223
- }
224
- export function keys() {
225
- return store.keys();
226
- }
227
- export function getEntryMeta(cacheKey) {
228
- const entry = store.peek(cacheKey);
229
- if (!entry)
230
- return undefined;
231
- return {
232
- url: entry.url,
233
- ...(entry.title !== undefined ? { title: entry.title } : {}),
234
- ...(entry.fetchedAt ? { fetchedAt: entry.fetchedAt } : {}),
235
- };
236
- }
237
- export function isEnabled() {
238
- return store.isEnabled();
239
- }
8
+ export { createCacheKey, get, getEntryMeta, isEnabled, keys, onCacheUpdate, parseCacheKey, set, } from './cache.js';
240
9
  const requestContext = new AsyncLocalStorage({
241
10
  name: 'requestContext',
242
11
  });