@j0hanz/fetch-url-mcp 1.11.7 → 1.12.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/LICENSE +21 -0
- package/README.md +177 -99
- 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/mcp-interop.js +2 -2
- package/dist/lib/utils.d.ts +0 -2
- package/dist/lib/utils.d.ts.map +1 -1
- package/dist/lib/utils.js +4 -39
- 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 +13 -4
- 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/transform/shared.js
CHANGED
|
@@ -3,10 +3,10 @@ function isTransformMessage(message) {
|
|
|
3
3
|
if (!message || typeof message !== 'object')
|
|
4
4
|
return false;
|
|
5
5
|
const value = message;
|
|
6
|
-
const { id, url, html, htmlBuffer, encoding,
|
|
6
|
+
const { id, url, html, htmlBuffer, encoding, includeMetadataFooter, inputTruncated, } = value;
|
|
7
7
|
return (typeof id === 'string' &&
|
|
8
8
|
typeof url === 'string' &&
|
|
9
|
-
typeof
|
|
9
|
+
typeof includeMetadataFooter === 'boolean' &&
|
|
10
10
|
(html === undefined || typeof html === 'string') &&
|
|
11
11
|
(htmlBuffer === undefined || htmlBuffer instanceof Uint8Array) &&
|
|
12
12
|
(encoding === undefined || typeof encoding === 'string') &&
|
|
@@ -80,7 +80,7 @@ function handleCancelMessage(params) {
|
|
|
80
80
|
}
|
|
81
81
|
function executeTransformMessage(params) {
|
|
82
82
|
const { message, controllersById, decoder, runTransform, sendMessage } = params;
|
|
83
|
-
const { id, url, html, htmlBuffer, encoding,
|
|
83
|
+
const { id, url, html, htmlBuffer, encoding, includeMetadataFooter, inputTruncated, } = message;
|
|
84
84
|
if (!id.trim()) {
|
|
85
85
|
sendMessage(createValidationErrorMessage(id, url || '', 'Missing transform message id'));
|
|
86
86
|
return;
|
|
@@ -94,7 +94,7 @@ function executeTransformMessage(params) {
|
|
|
94
94
|
try {
|
|
95
95
|
const content = decodeHtml(html, htmlBuffer, encoding, decoder);
|
|
96
96
|
const result = runTransform(content, url, {
|
|
97
|
-
|
|
97
|
+
includeMetadataFooter,
|
|
98
98
|
signal: controller.signal,
|
|
99
99
|
...(inputTruncated ? { inputTruncated: true } : {}),
|
|
100
100
|
});
|
|
@@ -20,7 +20,7 @@ export declare function htmlToMarkdown(html: string, metadata?: MetadataBlock, o
|
|
|
20
20
|
}): string;
|
|
21
21
|
export declare function isExtractionSufficient(article: ExtractedArticle | null, originalHtmlOrDocument: string | Document): boolean;
|
|
22
22
|
export declare function determineContentExtractionSource(article: ExtractedArticle | null): article is ExtractedArticle;
|
|
23
|
-
export declare function createContentMetadataBlock(url: string, article: ExtractedArticle | null, extractedMeta: ExtractedMetadata, shouldExtractFromArticle: boolean,
|
|
23
|
+
export declare function createContentMetadataBlock(url: string, article: ExtractedArticle | null, extractedMeta: ExtractedMetadata, shouldExtractFromArticle: boolean, includeMetadataFooter: boolean): MetadataBlock | undefined;
|
|
24
24
|
export declare function transformHtmlToMarkdownInProcess(html: string, url: string, options: TransformOptions): MarkdownTransformResult;
|
|
25
25
|
interface TransformPoolStats {
|
|
26
26
|
queueDepth: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/transform/transform.ts"],"names":[],"mappings":"AA2CA,OAAO,EACL,wBAAwB,EACxB,wBAAwB,EACxB,oBAAoB,EACrB,MAAM,uBAAuB,CAAC;AAkB/B,OAAO,KAAK,EACV,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,uBAAuB,EACvB,aAAa,EACb,gBAAgB,EAChB,qBAAqB,EAEtB,MAAM,YAAY,CAAC;AA+BpB,UAAU,WAAW;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;CACnB;AA4LD,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,WAAW,GACnB,qBAAqB,GAAG,IAAI,CAE9B;AAED,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,qBAAqB,GAAG,IAAI,EACrC,OAAO,CAAC,EAAE;IAAE,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,GAChC,MAAM,CAER;AA2aD,wBAAgB,cAAc,CAC5B,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,EACX,OAAO,GAAE;IAAE,cAAc,CAAC,EAAE,OAAO,CA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AU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,wBAAgB,qBAAqB,IAAI,kBAAkB,GAAG,IAAI,CAEjE;AAED,wBAAsB,2BAA2B,IAAI,OAAO,CAAC,IAAI,CAAC,CAEjE;AAED,KAAK,yBAAyB,GAAG,gBAAgB,GAAG;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAuG1E,wBAAsB,uBAAuB,CAC3C,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,uBAAuB,CAAC,CAElC;AAED,wBAAsB,yBAAyB,CAC7C,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,yBAAyB,GACjC,OAAO,CAAC,uBAAuB,CAAC,CAElC;AAED,OAAO,EACL,wBAAwB,EACxB,wBAAwB,EACxB,oBAAoB,GACrB,CAAC"}
|
|
@@ -631,7 +631,7 @@ function shouldPreserveRawContent(url, content) {
|
|
|
631
631
|
}
|
|
632
632
|
function buildRawMarkdownPayload(params) {
|
|
633
633
|
const title = extractTitleFromRawMarkdown(params.rawContent);
|
|
634
|
-
let content = params.
|
|
634
|
+
let content = params.includeMetadataFooter
|
|
635
635
|
? addSourceToMarkdown(params.rawContent, params.url)
|
|
636
636
|
: params.rawContent;
|
|
637
637
|
if (params.url) {
|
|
@@ -648,7 +648,7 @@ function tryTransformRawContent(params) {
|
|
|
648
648
|
const { content, title } = buildRawMarkdownPayload({
|
|
649
649
|
rawContent: params.html,
|
|
650
650
|
url: params.url,
|
|
651
|
-
|
|
651
|
+
includeMetadataFooter: params.includeMetadataFooter,
|
|
652
652
|
});
|
|
653
653
|
return {
|
|
654
654
|
markdown: content,
|
|
@@ -672,8 +672,8 @@ const BINARY_SAMPLE_SIZE = 2000;
|
|
|
672
672
|
export function determineContentExtractionSource(article) {
|
|
673
673
|
return article !== null;
|
|
674
674
|
}
|
|
675
|
-
export function createContentMetadataBlock(url, article, extractedMeta, shouldExtractFromArticle,
|
|
676
|
-
if (!
|
|
675
|
+
export function createContentMetadataBlock(url, article, extractedMeta, shouldExtractFromArticle, includeMetadataFooter) {
|
|
676
|
+
if (!includeMetadataFooter)
|
|
677
677
|
return undefined;
|
|
678
678
|
const metadata = {
|
|
679
679
|
type: 'metadata',
|
|
@@ -826,8 +826,8 @@ function buildRawSource(base, params) {
|
|
|
826
826
|
};
|
|
827
827
|
}
|
|
828
828
|
function resolveBaseContentSource(input) {
|
|
829
|
-
const { html, url, article, extractedMeta,
|
|
830
|
-
const metadata = createContentMetadataBlock(url, article, extractedMeta, evaluatedArticleDoc !== null,
|
|
829
|
+
const { html, url, article, extractedMeta, includeMetadataFooter, evaluatedArticleDoc, document, truncated, signal, } = input;
|
|
830
|
+
const metadata = createContentMetadataBlock(url, article, extractedMeta, evaluatedArticleDoc !== null, includeMetadataFooter);
|
|
831
831
|
const preparedDocument = document
|
|
832
832
|
? prepareContentSourceDocument(document, url, signal)
|
|
833
833
|
: undefined;
|
|
@@ -879,7 +879,7 @@ function resolveContentSource(params) {
|
|
|
879
879
|
url: params.url,
|
|
880
880
|
article,
|
|
881
881
|
extractedMeta,
|
|
882
|
-
|
|
882
|
+
includeMetadataFooter: params.includeMetadataFooter,
|
|
883
883
|
evaluatedArticleDoc,
|
|
884
884
|
document,
|
|
885
885
|
truncated: truncated ?? false,
|
|
@@ -915,7 +915,7 @@ function resolveTransformContentResult(html, url, options, signal) {
|
|
|
915
915
|
const rawResult = stageTracker.run(url, 'transform:raw', () => tryTransformRawContent({
|
|
916
916
|
html,
|
|
917
917
|
url,
|
|
918
|
-
|
|
918
|
+
includeMetadataFooter: options.includeMetadataFooter,
|
|
919
919
|
inputTruncated: options.inputTruncated,
|
|
920
920
|
}));
|
|
921
921
|
if (rawResult)
|
|
@@ -923,7 +923,7 @@ function resolveTransformContentResult(html, url, options, signal) {
|
|
|
923
923
|
const context = stageTracker.run(url, 'transform:extract', () => resolveContentSource({
|
|
924
924
|
html,
|
|
925
925
|
url,
|
|
926
|
-
|
|
926
|
+
includeMetadataFooter: options.includeMetadataFooter,
|
|
927
927
|
signal,
|
|
928
928
|
inputTruncated: options.inputTruncated,
|
|
929
929
|
}));
|
|
@@ -968,7 +968,7 @@ function transformInputInProcess(htmlOrBuffer, url, options) {
|
|
|
968
968
|
}
|
|
969
969
|
function workerTransformOptions(options) {
|
|
970
970
|
return {
|
|
971
|
-
|
|
971
|
+
includeMetadataFooter: options.includeMetadataFooter,
|
|
972
972
|
...(options.signal ? { signal: options.signal } : {}),
|
|
973
973
|
...(options.inputTruncated
|
|
974
974
|
? { inputTruncated: options.inputTruncated }
|
|
@@ -59,7 +59,7 @@ export interface MarkdownTransformResult extends MarkdownPayload {
|
|
|
59
59
|
* Options for transform operations.
|
|
60
60
|
*/
|
|
61
61
|
export interface TransformOptions {
|
|
62
|
-
|
|
62
|
+
includeMetadataFooter: boolean;
|
|
63
63
|
signal?: AbortSignal;
|
|
64
64
|
inputTruncated?: boolean;
|
|
65
65
|
}
|
|
@@ -96,7 +96,7 @@ export interface TransformWorkerTransformMessage {
|
|
|
96
96
|
htmlBuffer?: Uint8Array | undefined;
|
|
97
97
|
encoding?: string | undefined;
|
|
98
98
|
url: string;
|
|
99
|
-
|
|
99
|
+
includeMetadataFooter: boolean;
|
|
100
100
|
inputTruncated?: boolean | undefined;
|
|
101
101
|
}
|
|
102
102
|
export interface TransformWorkerCancelledMessage {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/transform/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,C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,EAAE,MAAM,CAAC;CACZ;AAED,MAAM,WAAW,4BAA4B;IAC3C,IAAI,EAAE,QAAQ,CAAC;IACf,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,eAAe,CAAC;CACzB;AAED,MAAM,WAAW,2BAA2B;IAC1C,IAAI,EAAE,OAAO,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE;QACL,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;QAChB,GAAG,EAAE,MAAM,CAAC;QACZ,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAChC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;KAC/C,CAAC;CACH;AAED,MAAM,MAAM,8BAA8B,GACtC,4BAA4B,GAC5B,2BAA2B,GAC3B,+BAA+B,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { MarkdownTransformResult } from './types.js';
|
|
2
2
|
interface TransformWorkerPool {
|
|
3
3
|
transform(html: string, url: string, options: {
|
|
4
|
-
|
|
4
|
+
includeMetadataFooter: boolean;
|
|
5
5
|
signal?: AbortSignal;
|
|
6
6
|
inputTruncated?: boolean;
|
|
7
7
|
}): Promise<MarkdownTransformResult>;
|
|
@@ -28,12 +28,12 @@ declare class WorkerPool implements TransformWorkerPool {
|
|
|
28
28
|
private readonly restartBackoff;
|
|
29
29
|
constructor(size: number, timeoutMs: number);
|
|
30
30
|
transform(html: string, url: string, options: {
|
|
31
|
-
|
|
31
|
+
includeMetadataFooter: boolean;
|
|
32
32
|
signal?: AbortSignal;
|
|
33
33
|
inputTruncated?: boolean;
|
|
34
34
|
}): Promise<MarkdownTransformResult>;
|
|
35
35
|
transform(htmlBuffer: Uint8Array, url: string, options: {
|
|
36
|
-
|
|
36
|
+
includeMetadataFooter: boolean;
|
|
37
37
|
signal?: AbortSignal;
|
|
38
38
|
inputTruncated?: boolean;
|
|
39
39
|
encoding?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker-pool.d.ts","sourceRoot":"","sources":["../../src/transform/worker-pool.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,EACV,uBAAuB,EAKxB,MAAM,YAAY,CAAC;AAqIpB,UAAU,mBAAmB;IAC3B,SAAS,CACP,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,EACX,OAAO,EAAE;QACP,
|
|
1
|
+
{"version":3,"file":"worker-pool.d.ts","sourceRoot":"","sources":["../../src/transform/worker-pool.ts"],"names":[],"mappings":"AA2BA,OAAO,KAAK,EACV,uBAAuB,EAKxB,MAAM,YAAY,CAAC;AAqIpB,UAAU,mBAAmB;IAC3B,SAAS,CACP,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,MAAM,EACX,OAAO,EAAE;QACP,qBAAqB,EAAE,OAAO,CAAC;QAC/B,MAAM,CAAC,EAAE,WAAW,CAAC;QACrB,cAAc,CAAC,EAAE,OAAO,CAAC;KAC1B,GACA,OAAO,CAAC,uBAAuB,CAAC,CAAC;IACpC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,aAAa,IAAI,MAAM,CAAC;IACxB,gBAAgB,IAAI,MAAM,CAAC;IAC3B,WAAW,IAAI,MAAM,CAAC;CACvB;AA6JD,cAAM,UAAW,YAAW,mBAAmB;IAC7C,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAkC;IAExE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkC;IAC1D,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoB;IAChD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAoB;IAEhD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAgC;IACtD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmC;IAC5D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA0B;IAErD,O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B,OAAO,CAAC,QAAQ;IAUhB,OAAO,CAAC,QAAQ;IAUhB,OAAO,CAAC,QAAQ;IAShB,OAAO,CAAC,YAAY;IASpB,OAAO,CAAC,UAAU;IAgClB,OAAO,CAAC,iBAAiB;IAgCzB,OAAO,CAAC,gBAAgB;IA8CxB,OAAO,CAAC,YAAY;IAwBpB,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,iBAAiB;CAS1B;AAMD,wBAAgB,qBAAqB,IAAI,UAAU,CAIlD;AAED,wBAAgB,kBAAkB,IAAI;IACpC,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;CAClB,GAAG,IAAI,CAOP;AAED,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC,CAIxD"}
|
|
@@ -68,7 +68,7 @@ function buildWorkerDispatchPayload(task) {
|
|
|
68
68
|
type: 'transform',
|
|
69
69
|
id: task.id,
|
|
70
70
|
url: task.url,
|
|
71
|
-
|
|
71
|
+
includeMetadataFooter: task.includeMetadataFooter,
|
|
72
72
|
...(task.inputTruncated ? { inputTruncated: true } : {}),
|
|
73
73
|
};
|
|
74
74
|
if (!task.htmlBuffer) {
|
|
@@ -300,7 +300,7 @@ class WorkerPool {
|
|
|
300
300
|
const task = {
|
|
301
301
|
id,
|
|
302
302
|
url,
|
|
303
|
-
|
|
303
|
+
includeMetadataFooter: options.includeMetadataFooter,
|
|
304
304
|
...(options.inputTruncated
|
|
305
305
|
? { inputTruncated: options.inputTruncated }
|
|
306
306
|
: {}),
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@j0hanz/fetch-url-mcp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"mcpName": "io.github.j0hanz/fetch-url-mcp",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "An MCP server that fetches web pages and converts them to clean, readable Markdown.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
@@ -42,7 +42,16 @@
|
|
|
42
42
|
"web-scraper",
|
|
43
43
|
"llm-context",
|
|
44
44
|
"ai-tools",
|
|
45
|
-
"fetch-url-mcp"
|
|
45
|
+
"fetch-url-mcp",
|
|
46
|
+
"typescript",
|
|
47
|
+
"nodejs",
|
|
48
|
+
"stdio",
|
|
49
|
+
"streamable-http",
|
|
50
|
+
"structured-output",
|
|
51
|
+
"metadata-extraction",
|
|
52
|
+
"url-rewriting",
|
|
53
|
+
"worker-threads",
|
|
54
|
+
"content-cleanup"
|
|
46
55
|
],
|
|
47
56
|
"scripts": {
|
|
48
57
|
"clean": "node scripts/tasks.mjs clean",
|
|
@@ -88,7 +97,7 @@
|
|
|
88
97
|
"knip": "^6",
|
|
89
98
|
"prettier": "^3.8.1",
|
|
90
99
|
"tsx": "^4.21.0",
|
|
91
|
-
"typescript": "^
|
|
100
|
+
"typescript": "^6.0.2",
|
|
92
101
|
"typescript-eslint": "^8.57.2"
|
|
93
102
|
},
|
|
94
103
|
"engines": {
|
package/dist/lib/cache.d.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
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
|
package/dist/lib/cache.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,C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eAAe,GACxB,IAAI,CAEN;AACD,wBAAgB,IAAI,IAAI,SAAS,MAAM,EAAE,CAExC;AACD,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,MAAM,GAEd;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,EAAE,CAAA;CAAE,GACvE,SAAS,CASZ;AACD,wBAAgB,SAAS,IAAI,OAAO,CAEnC"}
|
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
|
-
}
|