@echofiles/echo-pdf 0.5.0 → 0.7.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 +201 -0
- package/README.md +100 -563
- package/bin/echo-pdf.js +147 -536
- package/dist/file-utils.d.ts +0 -3
- package/dist/file-utils.js +0 -18
- package/dist/local/document.d.ts +10 -0
- package/dist/local/document.js +133 -0
- package/dist/local/index.d.ts +3 -135
- package/dist/local/index.js +2 -555
- package/dist/local/semantic.d.ts +2 -0
- package/dist/local/semantic.js +231 -0
- package/dist/local/shared.d.ts +50 -0
- package/dist/local/shared.js +173 -0
- package/dist/local/types.d.ts +183 -0
- package/dist/local/types.js +2 -0
- package/dist/node/pdfium-local.js +30 -6
- package/dist/pdf-config.js +2 -65
- package/dist/pdf-types.d.ts +2 -59
- package/dist/provider-client.js +1 -1
- package/dist/provider-keys.js +4 -1
- package/dist/types.d.ts +2 -88
- package/echo-pdf.config.json +10 -21
- package/package.json +25 -22
- package/bin/lib/http.js +0 -97
- package/bin/lib/mcp-stdio.js +0 -99
- package/dist/auth.d.ts +0 -18
- package/dist/auth.js +0 -36
- package/dist/core/index.d.ts +0 -50
- package/dist/core/index.js +0 -7
- package/dist/file-ops.d.ts +0 -11
- package/dist/file-ops.js +0 -36
- package/dist/file-store-do.d.ts +0 -36
- package/dist/file-store-do.js +0 -298
- package/dist/http-error.d.ts +0 -9
- package/dist/http-error.js +0 -14
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/mcp-server.d.ts +0 -3
- package/dist/mcp-server.js +0 -124
- package/dist/node/semantic-local.d.ts +0 -16
- package/dist/node/semantic-local.js +0 -113
- package/dist/pdf-agent.d.ts +0 -18
- package/dist/pdf-agent.js +0 -217
- package/dist/pdf-storage.d.ts +0 -8
- package/dist/pdf-storage.js +0 -86
- package/dist/pdfium-engine.d.ts +0 -9
- package/dist/pdfium-engine.js +0 -180
- package/dist/r2-file-store.d.ts +0 -20
- package/dist/r2-file-store.js +0 -176
- package/dist/response-schema.d.ts +0 -15
- package/dist/response-schema.js +0 -159
- package/dist/tool-registry.d.ts +0 -16
- package/dist/tool-registry.js +0 -175
- package/dist/worker.d.ts +0 -7
- package/dist/worker.js +0 -386
- package/scripts/export-fixtures.sh +0 -204
- package/wrangler.toml +0 -19
package/bin/lib/mcp-stdio.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
const mcpReadLoop = (onMessage, onError) => {
|
|
2
|
-
let buffer = Buffer.alloc(0)
|
|
3
|
-
let expectedLength = null
|
|
4
|
-
process.stdin.on("data", (chunk) => {
|
|
5
|
-
buffer = Buffer.concat([buffer, chunk])
|
|
6
|
-
while (true) {
|
|
7
|
-
if (expectedLength === null) {
|
|
8
|
-
const headerEnd = buffer.indexOf("\r\n\r\n")
|
|
9
|
-
if (headerEnd === -1) break
|
|
10
|
-
const headerRaw = buffer.slice(0, headerEnd).toString("utf-8")
|
|
11
|
-
const lines = headerRaw.split("\r\n")
|
|
12
|
-
const cl = lines.find((line) => line.toLowerCase().startsWith("content-length:"))
|
|
13
|
-
if (!cl) {
|
|
14
|
-
onError(new Error("Missing Content-Length"))
|
|
15
|
-
buffer = buffer.slice(headerEnd + 4)
|
|
16
|
-
continue
|
|
17
|
-
}
|
|
18
|
-
expectedLength = Number(cl.split(":")[1]?.trim() || "0")
|
|
19
|
-
buffer = buffer.slice(headerEnd + 4)
|
|
20
|
-
}
|
|
21
|
-
if (!Number.isFinite(expectedLength) || expectedLength < 0) {
|
|
22
|
-
onError(new Error("Invalid Content-Length"))
|
|
23
|
-
expectedLength = null
|
|
24
|
-
continue
|
|
25
|
-
}
|
|
26
|
-
if (buffer.length < expectedLength) break
|
|
27
|
-
const body = buffer.slice(0, expectedLength).toString("utf-8")
|
|
28
|
-
buffer = buffer.slice(expectedLength)
|
|
29
|
-
expectedLength = null
|
|
30
|
-
try {
|
|
31
|
-
const maybePromise = onMessage(JSON.parse(body))
|
|
32
|
-
if (maybePromise && typeof maybePromise.then === "function") {
|
|
33
|
-
maybePromise.catch(onError)
|
|
34
|
-
}
|
|
35
|
-
} catch (error) {
|
|
36
|
-
onError(error)
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const mcpWrite = (obj) => {
|
|
43
|
-
const body = Buffer.from(JSON.stringify(obj))
|
|
44
|
-
const header = Buffer.from(`Content-Length: ${body.length}\r\n\r\n`)
|
|
45
|
-
process.stdout.write(header)
|
|
46
|
-
process.stdout.write(body)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export const runMcpStdio = async (deps) => {
|
|
50
|
-
const {
|
|
51
|
-
serviceUrl,
|
|
52
|
-
headers,
|
|
53
|
-
postJson,
|
|
54
|
-
withUploadedLocalFile,
|
|
55
|
-
} = deps
|
|
56
|
-
mcpReadLoop(async (msg) => {
|
|
57
|
-
const method = msg?.method
|
|
58
|
-
const id = Object.hasOwn(msg || {}, "id") ? msg.id : null
|
|
59
|
-
if (msg?.jsonrpc !== "2.0" || typeof method !== "string") {
|
|
60
|
-
mcpWrite({ jsonrpc: "2.0", id, error: { code: -32600, message: "Invalid Request" } })
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
if (method === "notifications/initialized") return
|
|
64
|
-
if (method === "initialize" || method === "tools/list") {
|
|
65
|
-
const data = await postJson(`${serviceUrl}/mcp`, msg, headers)
|
|
66
|
-
mcpWrite(data)
|
|
67
|
-
return
|
|
68
|
-
}
|
|
69
|
-
if (method === "tools/call") {
|
|
70
|
-
try {
|
|
71
|
-
const tool = String(msg?.params?.name || "")
|
|
72
|
-
const args = (msg?.params?.arguments && typeof msg.params.arguments === "object")
|
|
73
|
-
? msg.params.arguments
|
|
74
|
-
: {}
|
|
75
|
-
const preparedArgs = await withUploadedLocalFile(serviceUrl, tool, args)
|
|
76
|
-
const payload = {
|
|
77
|
-
...msg,
|
|
78
|
-
params: {
|
|
79
|
-
...(msg.params || {}),
|
|
80
|
-
arguments: preparedArgs,
|
|
81
|
-
},
|
|
82
|
-
}
|
|
83
|
-
const data = await postJson(`${serviceUrl}/mcp`, payload, headers)
|
|
84
|
-
mcpWrite(data)
|
|
85
|
-
} catch (error) {
|
|
86
|
-
mcpWrite({
|
|
87
|
-
jsonrpc: "2.0",
|
|
88
|
-
id,
|
|
89
|
-
error: { code: -32603, message: error instanceof Error ? error.message : String(error) },
|
|
90
|
-
})
|
|
91
|
-
}
|
|
92
|
-
return
|
|
93
|
-
}
|
|
94
|
-
const data = await postJson(`${serviceUrl}/mcp`, msg, headers)
|
|
95
|
-
mcpWrite(data)
|
|
96
|
-
}, (error) => {
|
|
97
|
-
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`)
|
|
98
|
-
})
|
|
99
|
-
}
|
package/dist/auth.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { Env } from "./types.js";
|
|
2
|
-
export interface AuthCheckOptions {
|
|
3
|
-
readonly authHeader?: string;
|
|
4
|
-
readonly authEnv?: string;
|
|
5
|
-
readonly allowMissingSecret?: boolean;
|
|
6
|
-
readonly misconfiguredCode: string;
|
|
7
|
-
readonly unauthorizedCode: string;
|
|
8
|
-
readonly contextName: string;
|
|
9
|
-
}
|
|
10
|
-
export type AuthCheckResult = {
|
|
11
|
-
readonly ok: true;
|
|
12
|
-
} | {
|
|
13
|
-
readonly ok: false;
|
|
14
|
-
readonly status: number;
|
|
15
|
-
readonly code: string;
|
|
16
|
-
readonly message: string;
|
|
17
|
-
};
|
|
18
|
-
export declare const checkHeaderAuth: (request: Request, env: Env, options: AuthCheckOptions) => AuthCheckResult;
|
package/dist/auth.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
export const checkHeaderAuth = (request, env, options) => {
|
|
2
|
-
const authHeader = typeof options.authHeader === "string" ? options.authHeader.trim() : "";
|
|
3
|
-
const authEnv = typeof options.authEnv === "string" ? options.authEnv.trim() : "";
|
|
4
|
-
const hasHeader = authHeader.length > 0;
|
|
5
|
-
const hasEnv = authEnv.length > 0;
|
|
6
|
-
if (!hasHeader && !hasEnv)
|
|
7
|
-
return { ok: true };
|
|
8
|
-
if (!hasHeader || !hasEnv) {
|
|
9
|
-
return {
|
|
10
|
-
ok: false,
|
|
11
|
-
status: 500,
|
|
12
|
-
code: options.misconfiguredCode,
|
|
13
|
-
message: `${options.contextName} auth must configure both authHeader and authEnv`,
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
const required = env[authEnv];
|
|
17
|
-
if (typeof required !== "string" || required.length === 0) {
|
|
18
|
-
if (options.allowMissingSecret === true)
|
|
19
|
-
return { ok: true };
|
|
20
|
-
return {
|
|
21
|
-
ok: false,
|
|
22
|
-
status: 500,
|
|
23
|
-
code: options.misconfiguredCode,
|
|
24
|
-
message: `${options.contextName} auth is configured but env "${authEnv}" is missing`,
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
if (request.headers.get(authHeader) !== required) {
|
|
28
|
-
return {
|
|
29
|
-
ok: false,
|
|
30
|
-
status: 401,
|
|
31
|
-
code: options.unauthorizedCode,
|
|
32
|
-
message: "Unauthorized",
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
return { ok: true };
|
|
36
|
-
};
|
package/dist/core/index.d.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
export { callTool, listToolSchemas } from "../tool-registry.js";
|
|
2
|
-
export type { ToolRuntimeContext } from "../tool-registry.js";
|
|
3
|
-
export type { ToolSchema } from "../pdf-types.js";
|
|
4
|
-
export type { Env, FileStore, JsonObject } from "../types.js";
|
|
5
|
-
import type { ReturnMode } from "../types.js";
|
|
6
|
-
export interface PdfExtractPagesArgs {
|
|
7
|
-
readonly fileId?: string;
|
|
8
|
-
readonly url?: string;
|
|
9
|
-
readonly base64?: string;
|
|
10
|
-
readonly filename?: string;
|
|
11
|
-
readonly pages: ReadonlyArray<number>;
|
|
12
|
-
readonly renderScale?: number;
|
|
13
|
-
readonly returnMode?: ReturnMode;
|
|
14
|
-
}
|
|
15
|
-
export interface PdfOcrPagesArgs {
|
|
16
|
-
readonly fileId?: string;
|
|
17
|
-
readonly url?: string;
|
|
18
|
-
readonly base64?: string;
|
|
19
|
-
readonly filename?: string;
|
|
20
|
-
readonly pages: ReadonlyArray<number>;
|
|
21
|
-
readonly renderScale?: number;
|
|
22
|
-
readonly provider?: string;
|
|
23
|
-
readonly model?: string;
|
|
24
|
-
readonly prompt?: string;
|
|
25
|
-
}
|
|
26
|
-
export interface PdfTablesToLatexArgs {
|
|
27
|
-
readonly fileId?: string;
|
|
28
|
-
readonly url?: string;
|
|
29
|
-
readonly base64?: string;
|
|
30
|
-
readonly filename?: string;
|
|
31
|
-
readonly pages: ReadonlyArray<number>;
|
|
32
|
-
readonly renderScale?: number;
|
|
33
|
-
readonly provider?: string;
|
|
34
|
-
readonly model?: string;
|
|
35
|
-
readonly prompt?: string;
|
|
36
|
-
}
|
|
37
|
-
export interface FileOpsArgs {
|
|
38
|
-
readonly op: "list" | "read" | "delete" | "put";
|
|
39
|
-
readonly fileId?: string;
|
|
40
|
-
readonly includeBase64?: boolean;
|
|
41
|
-
readonly text?: string;
|
|
42
|
-
readonly filename?: string;
|
|
43
|
-
readonly mimeType?: string;
|
|
44
|
-
readonly base64?: string;
|
|
45
|
-
readonly returnMode?: ReturnMode;
|
|
46
|
-
}
|
|
47
|
-
export declare const pdf_extract_pages: (args: PdfExtractPagesArgs, ctx: import("../tool-registry.js").ToolRuntimeContext) => Promise<unknown>;
|
|
48
|
-
export declare const pdf_ocr_pages: (args: PdfOcrPagesArgs, ctx: import("../tool-registry.js").ToolRuntimeContext) => Promise<unknown>;
|
|
49
|
-
export declare const pdf_tables_to_latex: (args: PdfTablesToLatexArgs, ctx: import("../tool-registry.js").ToolRuntimeContext) => Promise<unknown>;
|
|
50
|
-
export declare const file_ops: (args: FileOpsArgs, ctx: import("../tool-registry.js").ToolRuntimeContext) => Promise<unknown>;
|
package/dist/core/index.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { callTool, listToolSchemas } from "../tool-registry.js";
|
|
2
|
-
import { callTool } from "../tool-registry.js";
|
|
3
|
-
const asJsonObject = (value) => value;
|
|
4
|
-
export const pdf_extract_pages = async (args, ctx) => callTool("pdf_extract_pages", asJsonObject(args), ctx);
|
|
5
|
-
export const pdf_ocr_pages = async (args, ctx) => callTool("pdf_ocr_pages", asJsonObject(args), ctx);
|
|
6
|
-
export const pdf_tables_to_latex = async (args, ctx) => callTool("pdf_tables_to_latex", asJsonObject(args), ctx);
|
|
7
|
-
export const file_ops = async (args, ctx) => callTool("file_ops", asJsonObject(args), ctx);
|
package/dist/file-ops.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { FileStore, ReturnMode } from "./types.js";
|
|
2
|
-
export declare const runFileOp: (fileStore: FileStore, input: {
|
|
3
|
-
readonly op: "list" | "read" | "delete" | "put";
|
|
4
|
-
readonly fileId?: string;
|
|
5
|
-
readonly includeBase64?: boolean;
|
|
6
|
-
readonly text?: string;
|
|
7
|
-
readonly filename?: string;
|
|
8
|
-
readonly mimeType?: string;
|
|
9
|
-
readonly base64?: string;
|
|
10
|
-
readonly returnMode?: ReturnMode;
|
|
11
|
-
}) => Promise<unknown>;
|
package/dist/file-ops.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { fromBase64, normalizeReturnMode, toInlineFilePayload } from "./file-utils.js";
|
|
2
|
-
export const runFileOp = async (fileStore, input) => {
|
|
3
|
-
if (input.op === "list") {
|
|
4
|
-
return { files: await fileStore.list() };
|
|
5
|
-
}
|
|
6
|
-
if (input.op === "put") {
|
|
7
|
-
const bytes = input.base64 ? fromBase64(input.base64) : new TextEncoder().encode(input.text ?? "");
|
|
8
|
-
const meta = await fileStore.put({
|
|
9
|
-
filename: input.filename ?? `file-${Date.now()}.txt`,
|
|
10
|
-
mimeType: input.mimeType ?? "text/plain; charset=utf-8",
|
|
11
|
-
bytes,
|
|
12
|
-
});
|
|
13
|
-
const returnMode = normalizeReturnMode(input.returnMode);
|
|
14
|
-
if (returnMode === "file_id")
|
|
15
|
-
return { returnMode, file: meta };
|
|
16
|
-
if (returnMode === "url")
|
|
17
|
-
return { returnMode, file: meta, url: `/api/files/get?fileId=${encodeURIComponent(meta.id)}` };
|
|
18
|
-
const stored = await fileStore.get(meta.id);
|
|
19
|
-
if (!stored)
|
|
20
|
-
throw new Error(`File not found after put: ${meta.id}`);
|
|
21
|
-
return {
|
|
22
|
-
returnMode,
|
|
23
|
-
...toInlineFilePayload(stored, true),
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
if (!input.fileId) {
|
|
27
|
-
throw new Error("fileId is required");
|
|
28
|
-
}
|
|
29
|
-
if (input.op === "delete") {
|
|
30
|
-
return { deleted: await fileStore.delete(input.fileId), fileId: input.fileId };
|
|
31
|
-
}
|
|
32
|
-
const file = await fileStore.get(input.fileId);
|
|
33
|
-
if (!file)
|
|
34
|
-
throw new Error(`File not found: ${input.fileId}`);
|
|
35
|
-
return toInlineFilePayload(file, Boolean(input.includeBase64));
|
|
36
|
-
};
|
package/dist/file-store-do.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import type { StoragePolicy } from "./pdf-types.js";
|
|
2
|
-
import type { DurableObjectNamespace, DurableObjectState, StoredFileMeta, StoredFileRecord } from "./types.js";
|
|
3
|
-
interface StoreStats {
|
|
4
|
-
readonly fileCount: number;
|
|
5
|
-
readonly totalBytes: number;
|
|
6
|
-
}
|
|
7
|
-
export declare class FileStoreDO {
|
|
8
|
-
private readonly state;
|
|
9
|
-
constructor(state: DurableObjectState);
|
|
10
|
-
fetch(request: Request): Promise<Response>;
|
|
11
|
-
}
|
|
12
|
-
export declare class DurableObjectFileStore {
|
|
13
|
-
private readonly namespace;
|
|
14
|
-
private readonly policy;
|
|
15
|
-
constructor(namespace: DurableObjectNamespace, policy: StoragePolicy);
|
|
16
|
-
private stub;
|
|
17
|
-
put(input: {
|
|
18
|
-
readonly filename: string;
|
|
19
|
-
readonly mimeType: string;
|
|
20
|
-
readonly bytes: Uint8Array;
|
|
21
|
-
}): Promise<StoredFileMeta>;
|
|
22
|
-
get(fileId: string): Promise<StoredFileRecord | null>;
|
|
23
|
-
list(): Promise<ReadonlyArray<StoredFileMeta>>;
|
|
24
|
-
delete(fileId: string): Promise<boolean>;
|
|
25
|
-
stats(): Promise<{
|
|
26
|
-
policy: StoragePolicy;
|
|
27
|
-
stats: StoreStats;
|
|
28
|
-
}>;
|
|
29
|
-
cleanup(): Promise<{
|
|
30
|
-
policy: StoragePolicy;
|
|
31
|
-
deletedExpired: number;
|
|
32
|
-
deletedEvicted: number;
|
|
33
|
-
stats: StoreStats;
|
|
34
|
-
}>;
|
|
35
|
-
}
|
|
36
|
-
export {};
|
package/dist/file-store-do.js
DELETED
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
import { fromBase64, toBase64 } from "./file-utils.js";
|
|
2
|
-
const json = (data, status = 200) => new Response(JSON.stringify(data), {
|
|
3
|
-
status,
|
|
4
|
-
headers: { "Content-Type": "application/json; charset=utf-8" },
|
|
5
|
-
});
|
|
6
|
-
const readJson = async (request) => {
|
|
7
|
-
try {
|
|
8
|
-
const body = await request.json();
|
|
9
|
-
if (typeof body === "object" && body !== null && !Array.isArray(body)) {
|
|
10
|
-
return body;
|
|
11
|
-
}
|
|
12
|
-
return {};
|
|
13
|
-
}
|
|
14
|
-
catch {
|
|
15
|
-
return {};
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
const defaultPolicy = () => ({
|
|
19
|
-
maxFileBytes: 1_200_000,
|
|
20
|
-
maxTotalBytes: 52_428_800,
|
|
21
|
-
ttlHours: 24,
|
|
22
|
-
cleanupBatchSize: 50,
|
|
23
|
-
});
|
|
24
|
-
const parsePolicy = (input) => {
|
|
25
|
-
const raw = typeof input === "object" && input !== null && !Array.isArray(input)
|
|
26
|
-
? input
|
|
27
|
-
: {};
|
|
28
|
-
const fallback = defaultPolicy();
|
|
29
|
-
const maxFileBytes = Number(raw.maxFileBytes ?? fallback.maxFileBytes);
|
|
30
|
-
const maxTotalBytes = Number(raw.maxTotalBytes ?? fallback.maxTotalBytes);
|
|
31
|
-
const ttlHours = Number(raw.ttlHours ?? fallback.ttlHours);
|
|
32
|
-
const cleanupBatchSize = Number(raw.cleanupBatchSize ?? fallback.cleanupBatchSize);
|
|
33
|
-
return {
|
|
34
|
-
maxFileBytes: Number.isFinite(maxFileBytes) && maxFileBytes > 0 ? Math.floor(maxFileBytes) : fallback.maxFileBytes,
|
|
35
|
-
maxTotalBytes: Number.isFinite(maxTotalBytes) && maxTotalBytes > 0 ? Math.floor(maxTotalBytes) : fallback.maxTotalBytes,
|
|
36
|
-
ttlHours: Number.isFinite(ttlHours) && ttlHours > 0 ? ttlHours : fallback.ttlHours,
|
|
37
|
-
cleanupBatchSize: Number.isFinite(cleanupBatchSize) && cleanupBatchSize > 0 ? Math.floor(cleanupBatchSize) : fallback.cleanupBatchSize,
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
const toMeta = (value) => ({
|
|
41
|
-
id: value.id,
|
|
42
|
-
filename: value.filename,
|
|
43
|
-
mimeType: value.mimeType,
|
|
44
|
-
sizeBytes: value.sizeBytes,
|
|
45
|
-
createdAt: value.createdAt,
|
|
46
|
-
});
|
|
47
|
-
const listStoredValues = async (state) => {
|
|
48
|
-
const listed = await state.storage.list({ prefix: "file:" });
|
|
49
|
-
return [...listed.values()];
|
|
50
|
-
};
|
|
51
|
-
const computeStats = (files) => ({
|
|
52
|
-
fileCount: files.length,
|
|
53
|
-
totalBytes: files.reduce((sum, file) => sum + file.sizeBytes, 0),
|
|
54
|
-
});
|
|
55
|
-
const isExpired = (createdAt, ttlHours) => {
|
|
56
|
-
const createdMs = Date.parse(createdAt);
|
|
57
|
-
if (!Number.isFinite(createdMs))
|
|
58
|
-
return false;
|
|
59
|
-
return Date.now() - createdMs > ttlHours * 60 * 60 * 1000;
|
|
60
|
-
};
|
|
61
|
-
const deleteFiles = async (state, files) => {
|
|
62
|
-
let deleted = 0;
|
|
63
|
-
for (const file of files) {
|
|
64
|
-
const ok = await state.storage.delete(`file:${file.id}`);
|
|
65
|
-
if (ok)
|
|
66
|
-
deleted += 1;
|
|
67
|
-
}
|
|
68
|
-
return deleted;
|
|
69
|
-
};
|
|
70
|
-
export class FileStoreDO {
|
|
71
|
-
state;
|
|
72
|
-
constructor(state) {
|
|
73
|
-
this.state = state;
|
|
74
|
-
}
|
|
75
|
-
async fetch(request) {
|
|
76
|
-
const url = new URL(request.url);
|
|
77
|
-
if (request.method === "POST" && url.pathname === "/put") {
|
|
78
|
-
const body = await readJson(request);
|
|
79
|
-
const policy = parsePolicy(body.policy);
|
|
80
|
-
const filename = typeof body.filename === "string" ? body.filename : `file-${Date.now()}`;
|
|
81
|
-
const mimeType = typeof body.mimeType === "string" ? body.mimeType : "application/octet-stream";
|
|
82
|
-
const bytesBase64 = typeof body.bytesBase64 === "string" ? body.bytesBase64 : "";
|
|
83
|
-
const bytes = fromBase64(bytesBase64);
|
|
84
|
-
if (bytes.byteLength > policy.maxFileBytes) {
|
|
85
|
-
return json({
|
|
86
|
-
error: `file too large: ${bytes.byteLength} bytes exceeds maxFileBytes ${policy.maxFileBytes}`,
|
|
87
|
-
code: "FILE_TOO_LARGE",
|
|
88
|
-
policy,
|
|
89
|
-
}, 413);
|
|
90
|
-
}
|
|
91
|
-
let files = await listStoredValues(this.state);
|
|
92
|
-
const expired = files.filter((file) => isExpired(file.createdAt, policy.ttlHours));
|
|
93
|
-
if (expired.length > 0) {
|
|
94
|
-
await deleteFiles(this.state, expired);
|
|
95
|
-
files = await listStoredValues(this.state);
|
|
96
|
-
}
|
|
97
|
-
let stats = computeStats(files);
|
|
98
|
-
const projected = stats.totalBytes + bytes.byteLength;
|
|
99
|
-
if (projected > policy.maxTotalBytes) {
|
|
100
|
-
const needFree = projected - policy.maxTotalBytes;
|
|
101
|
-
const candidates = [...files]
|
|
102
|
-
.sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt))
|
|
103
|
-
.slice(0, policy.cleanupBatchSize);
|
|
104
|
-
let freed = 0;
|
|
105
|
-
const evictList = [];
|
|
106
|
-
for (const file of candidates) {
|
|
107
|
-
evictList.push(file);
|
|
108
|
-
freed += file.sizeBytes;
|
|
109
|
-
if (freed >= needFree)
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
if (evictList.length > 0) {
|
|
113
|
-
await deleteFiles(this.state, evictList);
|
|
114
|
-
files = await listStoredValues(this.state);
|
|
115
|
-
stats = computeStats(files);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
if (stats.totalBytes + bytes.byteLength > policy.maxTotalBytes) {
|
|
119
|
-
return json({
|
|
120
|
-
error: `storage quota exceeded: total ${stats.totalBytes} + incoming ${bytes.byteLength} > maxTotalBytes ${policy.maxTotalBytes}`,
|
|
121
|
-
code: "STORAGE_QUOTA_EXCEEDED",
|
|
122
|
-
policy,
|
|
123
|
-
stats,
|
|
124
|
-
}, 507);
|
|
125
|
-
}
|
|
126
|
-
const id = crypto.randomUUID();
|
|
127
|
-
const value = {
|
|
128
|
-
id,
|
|
129
|
-
filename,
|
|
130
|
-
mimeType,
|
|
131
|
-
sizeBytes: bytes.byteLength,
|
|
132
|
-
createdAt: new Date().toISOString(),
|
|
133
|
-
bytesBase64,
|
|
134
|
-
};
|
|
135
|
-
await this.state.storage.put(`file:${id}`, value);
|
|
136
|
-
return json({ file: toMeta(value), policy });
|
|
137
|
-
}
|
|
138
|
-
if (request.method === "GET" && url.pathname === "/get") {
|
|
139
|
-
const fileId = url.searchParams.get("fileId");
|
|
140
|
-
if (!fileId)
|
|
141
|
-
return json({ error: "Missing fileId" }, 400);
|
|
142
|
-
const value = await this.state.storage.get(`file:${fileId}`);
|
|
143
|
-
if (!value)
|
|
144
|
-
return json({ file: null });
|
|
145
|
-
return json({ file: value });
|
|
146
|
-
}
|
|
147
|
-
if (request.method === "GET" && url.pathname === "/list") {
|
|
148
|
-
const listed = await this.state.storage.list({ prefix: "file:" });
|
|
149
|
-
const files = [...listed.values()].map(toMeta);
|
|
150
|
-
return json({ files });
|
|
151
|
-
}
|
|
152
|
-
if (request.method === "POST" && url.pathname === "/delete") {
|
|
153
|
-
const body = await readJson(request);
|
|
154
|
-
const fileId = typeof body.fileId === "string" ? body.fileId : "";
|
|
155
|
-
if (!fileId)
|
|
156
|
-
return json({ error: "Missing fileId" }, 400);
|
|
157
|
-
const key = `file:${fileId}`;
|
|
158
|
-
const existing = await this.state.storage.get(key);
|
|
159
|
-
if (!existing)
|
|
160
|
-
return json({ deleted: false });
|
|
161
|
-
await this.state.storage.delete(key);
|
|
162
|
-
return json({ deleted: true });
|
|
163
|
-
}
|
|
164
|
-
if (request.method === "GET" && url.pathname === "/stats") {
|
|
165
|
-
let policyInput;
|
|
166
|
-
const encoded = url.searchParams.get("policy");
|
|
167
|
-
if (encoded) {
|
|
168
|
-
try {
|
|
169
|
-
policyInput = JSON.parse(encoded);
|
|
170
|
-
}
|
|
171
|
-
catch {
|
|
172
|
-
policyInput = undefined;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
const policy = parsePolicy(policyInput);
|
|
176
|
-
const files = await listStoredValues(this.state);
|
|
177
|
-
const stats = computeStats(files);
|
|
178
|
-
return json({ policy, stats });
|
|
179
|
-
}
|
|
180
|
-
if (request.method === "POST" && url.pathname === "/cleanup") {
|
|
181
|
-
const body = await readJson(request);
|
|
182
|
-
const policy = parsePolicy(body.policy);
|
|
183
|
-
const files = await listStoredValues(this.state);
|
|
184
|
-
const expired = files.filter((file) => isExpired(file.createdAt, policy.ttlHours));
|
|
185
|
-
const deletedExpired = await deleteFiles(this.state, expired);
|
|
186
|
-
const afterExpired = await listStoredValues(this.state);
|
|
187
|
-
let stats = computeStats(afterExpired);
|
|
188
|
-
let deletedEvicted = 0;
|
|
189
|
-
if (stats.totalBytes > policy.maxTotalBytes) {
|
|
190
|
-
const sorted = [...afterExpired].sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt));
|
|
191
|
-
const evictList = [];
|
|
192
|
-
for (const file of sorted) {
|
|
193
|
-
evictList.push(file);
|
|
194
|
-
const projected = stats.totalBytes - evictList.reduce((sum, item) => sum + item.sizeBytes, 0);
|
|
195
|
-
if (projected <= policy.maxTotalBytes)
|
|
196
|
-
break;
|
|
197
|
-
if (evictList.length >= policy.cleanupBatchSize)
|
|
198
|
-
break;
|
|
199
|
-
}
|
|
200
|
-
deletedEvicted = await deleteFiles(this.state, evictList);
|
|
201
|
-
stats = computeStats(await listStoredValues(this.state));
|
|
202
|
-
}
|
|
203
|
-
return json({
|
|
204
|
-
policy,
|
|
205
|
-
deletedExpired,
|
|
206
|
-
deletedEvicted,
|
|
207
|
-
stats,
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
return json({ error: "Not found" }, 404);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
export class DurableObjectFileStore {
|
|
214
|
-
namespace;
|
|
215
|
-
policy;
|
|
216
|
-
constructor(namespace, policy) {
|
|
217
|
-
this.namespace = namespace;
|
|
218
|
-
this.policy = policy;
|
|
219
|
-
}
|
|
220
|
-
stub() {
|
|
221
|
-
return this.namespace.get(this.namespace.idFromName("echo-pdf-file-store"));
|
|
222
|
-
}
|
|
223
|
-
async put(input) {
|
|
224
|
-
const response = await this.stub().fetch("https://do/put", {
|
|
225
|
-
method: "POST",
|
|
226
|
-
headers: { "Content-Type": "application/json" },
|
|
227
|
-
body: JSON.stringify({
|
|
228
|
-
filename: input.filename,
|
|
229
|
-
mimeType: input.mimeType,
|
|
230
|
-
bytesBase64: toBase64(input.bytes),
|
|
231
|
-
policy: this.policy,
|
|
232
|
-
}),
|
|
233
|
-
});
|
|
234
|
-
const payload = (await response.json());
|
|
235
|
-
if (!response.ok || !payload.file) {
|
|
236
|
-
const details = payload;
|
|
237
|
-
const error = new Error(payload.error ?? "DO put failed");
|
|
238
|
-
error.status = response.status;
|
|
239
|
-
error.code = typeof details.code === "string" ? details.code : undefined;
|
|
240
|
-
error.details = { policy: details.policy, stats: details.stats };
|
|
241
|
-
throw error;
|
|
242
|
-
}
|
|
243
|
-
return payload.file;
|
|
244
|
-
}
|
|
245
|
-
async get(fileId) {
|
|
246
|
-
const response = await this.stub().fetch(`https://do/get?fileId=${encodeURIComponent(fileId)}`);
|
|
247
|
-
const payload = (await response.json());
|
|
248
|
-
if (!response.ok)
|
|
249
|
-
throw new Error("DO get failed");
|
|
250
|
-
if (!payload.file)
|
|
251
|
-
return null;
|
|
252
|
-
return {
|
|
253
|
-
id: payload.file.id,
|
|
254
|
-
filename: payload.file.filename,
|
|
255
|
-
mimeType: payload.file.mimeType,
|
|
256
|
-
sizeBytes: payload.file.sizeBytes,
|
|
257
|
-
createdAt: payload.file.createdAt,
|
|
258
|
-
bytes: fromBase64(payload.file.bytesBase64),
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
async list() {
|
|
262
|
-
const response = await this.stub().fetch("https://do/list");
|
|
263
|
-
const payload = (await response.json());
|
|
264
|
-
if (!response.ok)
|
|
265
|
-
throw new Error("DO list failed");
|
|
266
|
-
return payload.files ?? [];
|
|
267
|
-
}
|
|
268
|
-
async delete(fileId) {
|
|
269
|
-
const response = await this.stub().fetch("https://do/delete", {
|
|
270
|
-
method: "POST",
|
|
271
|
-
headers: { "Content-Type": "application/json" },
|
|
272
|
-
body: JSON.stringify({ fileId }),
|
|
273
|
-
});
|
|
274
|
-
const payload = (await response.json());
|
|
275
|
-
if (!response.ok)
|
|
276
|
-
throw new Error("DO delete failed");
|
|
277
|
-
return payload.deleted === true;
|
|
278
|
-
}
|
|
279
|
-
async stats() {
|
|
280
|
-
const policyEncoded = encodeURIComponent(JSON.stringify(this.policy));
|
|
281
|
-
const response = await this.stub().fetch(`https://do/stats?policy=${policyEncoded}`);
|
|
282
|
-
const payload = (await response.json());
|
|
283
|
-
if (!response.ok)
|
|
284
|
-
throw new Error("DO stats failed");
|
|
285
|
-
return payload;
|
|
286
|
-
}
|
|
287
|
-
async cleanup() {
|
|
288
|
-
const response = await this.stub().fetch("https://do/cleanup", {
|
|
289
|
-
method: "POST",
|
|
290
|
-
headers: { "Content-Type": "application/json" },
|
|
291
|
-
body: JSON.stringify({ policy: this.policy }),
|
|
292
|
-
});
|
|
293
|
-
const payload = (await response.json());
|
|
294
|
-
if (!response.ok)
|
|
295
|
-
throw new Error("DO cleanup failed");
|
|
296
|
-
return payload;
|
|
297
|
-
}
|
|
298
|
-
}
|
package/dist/http-error.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export declare class HttpError extends Error {
|
|
2
|
-
readonly status: number;
|
|
3
|
-
readonly code: string;
|
|
4
|
-
readonly details?: unknown;
|
|
5
|
-
constructor(status: number, code: string, message: string, details?: unknown);
|
|
6
|
-
}
|
|
7
|
-
export declare const badRequest: (code: string, message: string, details?: unknown) => HttpError;
|
|
8
|
-
export declare const notFound: (code: string, message: string, details?: unknown) => HttpError;
|
|
9
|
-
export declare const unprocessable: (code: string, message: string, details?: unknown) => HttpError;
|
package/dist/http-error.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export class HttpError extends Error {
|
|
2
|
-
status;
|
|
3
|
-
code;
|
|
4
|
-
details;
|
|
5
|
-
constructor(status, code, message, details) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.status = status;
|
|
8
|
-
this.code = code;
|
|
9
|
-
this.details = details;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
export const badRequest = (code, message, details) => new HttpError(400, code, message, details);
|
|
13
|
-
export const notFound = (code, message, details) => new HttpError(404, code, message, details);
|
|
14
|
-
export const unprocessable = (code, message, details) => new HttpError(422, code, message, details);
|
package/dist/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./core/index.js";
|
package/dist/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./core/index.js";
|
package/dist/mcp-server.d.ts
DELETED