|
| 1 | +/** |
| 2 | + * Real behavior proof: huggingface model-discovery bounded response reads. |
| 3 | + * |
| 4 | + * Starts real node:http servers that stream oversized JSON bodies with no |
| 5 | + * Content-Length. Verifies the shared bounded reader rejects at the 16 MiB |
| 6 | + * provider-JSON cap and cancels the stream early. The negative control |
| 7 | + * confirms the old unbounded `response.json()` would buffer everything. |
| 8 | + * |
| 9 | + * Usage: node --import tsx test/_proof_huggingface_bound.mts |
| 10 | + */ |
| 11 | +import { createServer } from "node:http"; |
| 12 | +import type { AddressInfo } from "node:net"; |
| 13 | + |
| 14 | +const CAP = 16 * 1024 * 1024; |
| 15 | +const TOTAL = 18 * 1024 * 1024; |
| 16 | + |
| 17 | +let pass = 0; |
| 18 | +let fail = 0; |
| 19 | + |
| 20 | +function check(label: string, ok: boolean, detail = "") { |
| 21 | + if (ok) { pass++; console.log(`PASS ${label}${detail ? ` :: ${detail}` : ""}`); } |
| 22 | + else { fail++; console.error(`FAIL ${label}${detail ? ` :: ${detail}` : ""}`); } |
| 23 | +} |
| 24 | + |
| 25 | +function startServer(handler: (req: unknown, res: unknown) => void): Promise<{ url: string; shutdown: () => Promise<void> }> { |
| 26 | + return new Promise((resolve) => { |
| 27 | + const server = createServer(handler as Parameters<typeof createServer>[0]); |
| 28 | + server.listen(0, "127.0.0.1", () => { |
| 29 | + const addr = server.address() as AddressInfo; |
| 30 | + resolve({ url: `http://127.0.0.1:${addr.port}`, shutdown: () => new Promise((r) => server.close(() => r())) }); |
| 31 | + }); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +async function proofOversized() { |
| 36 | + // Start a fresh server for each test to avoid state issues |
| 37 | + const { url, shutdown } = await startServer((_req, res) => { |
| 38 | + res.writeHead(200, { "Content-Type": "application/json" }); |
| 39 | + let sent = 0; |
| 40 | + const chunk = Buffer.alloc(65536, 0x41); |
| 41 | + function writeChunk() { |
| 42 | + if (sent >= TOTAL) { res.end("\n]}"); return; } |
| 43 | + const header = sent === 0 ? '{"data":[{"id":"x","name":"' : ""; |
| 44 | + const payload = header ? Buffer.concat([Buffer.from(header), chunk]) : chunk; |
| 45 | + sent += payload.length; |
| 46 | + if (!res.write(payload)) res.once("drain", writeChunk); |
| 47 | + else setImmediate(writeChunk); |
| 48 | + } |
| 49 | + writeChunk(); |
| 50 | + }); |
| 51 | + |
| 52 | + console.log(`[proof] oversized server on :${new URL(url).port}, cap=${CAP}, total≈${TOTAL}`); |
| 53 | + |
| 54 | + // Positive: bounded read rejects at cap |
| 55 | + try { |
| 56 | + const { readResponseWithLimit } = await import("openclaw/plugin-sdk/response-limit-runtime"); |
| 57 | + const res = await fetch(url); |
| 58 | + await readResponseWithLimit(res, CAP, { |
| 59 | + onOverflow: ({ maxBytes }) => new Error(`JSON response exceeds ${maxBytes} bytes`), |
| 60 | + }); |
| 61 | + check("oversized body: throws bounded cap error", false, "should have thrown"); |
| 62 | + } catch (err: unknown) { |
| 63 | + const msg = err instanceof Error ? err.message : String(err); |
| 64 | + check("oversized body: throws bounded cap error", msg.includes(String(CAP)), |
| 65 | + `threw=true msg="${msg.slice(0, 80)}"`); |
| 66 | + } |
| 67 | + |
| 68 | + await shutdown(); |
| 69 | + |
| 70 | + // Negative: unbounded buffers everything (separate server) |
| 71 | + const { url: url2, shutdown: shutdown2 } = await startServer((_req, res) => { |
| 72 | + res.writeHead(200, { "Content-Type": "application/json" }); |
| 73 | + let sent = 0; |
| 74 | + const chunk = Buffer.alloc(65536, 0x41); |
| 75 | + function writeChunk() { |
| 76 | + if (sent >= TOTAL) { res.end("\n]}"); return; } |
| 77 | + const header = sent === 0 ? '{"data":[{"id":"x","name":"' : ""; |
| 78 | + const payload = header ? Buffer.concat([Buffer.from(header), chunk]) : chunk; |
| 79 | + sent += payload.length; |
| 80 | + if (!res.write(payload)) res.once("drain", writeChunk); |
| 81 | + else setImmediate(writeChunk); |
| 82 | + } |
| 83 | + writeChunk(); |
| 84 | + }); |
| 85 | + |
| 86 | + const res2 = await fetch(url2); |
| 87 | + const text = await res2.text(); |
| 88 | + check("negative control: unbounded read buffers PAST cap", |
| 89 | + text.length > CAP, `buffered=${text.length} (> ${CAP})`); |
| 90 | + |
| 91 | + await shutdown2(); |
| 92 | +} |
| 93 | + |
| 94 | +async function proofHappyPath() { |
| 95 | + const { url, shutdown } = await startServer((_req, res) => { |
| 96 | + res.writeHead(200, { "Content-Type": "application/json" }); |
| 97 | + res.end(JSON.stringify({ object: "list", data: [{ id: "test-model", name: "Test" }] })); |
| 98 | + }); |
| 99 | + |
| 100 | + const { readProviderJsonResponse } = await import("openclaw/plugin-sdk/provider-http"); |
| 101 | + const res = await fetch(url); |
| 102 | + const body = await readProviderJsonResponse<{ object: string; data: unknown[] }>( |
| 103 | + res, "huggingface-model-discovery"); |
| 104 | + check("happy path: small JSON parsed correctly", |
| 105 | + body?.object === "list" && Array.isArray(body?.data) && body.data.length === 1, |
| 106 | + `object=${body?.object} dataLen=${body?.data?.length}`); |
| 107 | + |
| 108 | + await shutdown(); |
| 109 | +} |
| 110 | + |
| 111 | +async function main() { |
| 112 | + console.log(`node --import tsx test/_proof_huggingface_bound.mts\n`); |
| 113 | + await proofOversized(); |
| 114 | + await proofHappyPath(); |
| 115 | + console.log(`\n[proof] ${pass} PASS, ${fail} FAIL`); |
| 116 | + if (fail > 0) process.exit(1); |
| 117 | +} |
| 118 | + |
| 119 | +main(); |
0 commit comments