UNPKG

@tanstack/ai-sandbox

Version:

Provider-agnostic sandbox layer for TanStack AI — run harness adapters inside isolated sandboxes (defineSandbox, defineWorkspace, withSandbox) with a uniform SandboxHandle, workspace bootstrap, policy, and resumable lifecycle.

47 lines (46 loc) • 1.65 kB
// src/node-web.ts /** Read the full request body as bytes. */ async function readRawBody(req) { const chunks = []; for await (const chunk of req) chunks.push(typeof chunk === "string" ? new TextEncoder().encode(chunk) : chunk); return Buffer.concat(chunks); } /** Build a Web `Request` from a node request and its body bytes. */ function toWebRequest(req, rawBody) { const url = `http://${req.headers.host ?? "127.0.0.1"}${req.url ?? "/"}`; const headers = new Headers(); for (const [key, value] of Object.entries(req.headers)) { if (value === void 0) continue; if (Array.isArray(value)) for (const item of value) headers.append(key, item); else headers.set(key, value); } const method = req.method ?? "GET"; return new Request(url, { method, headers, body: method !== "GET" && method !== "HEAD" ? rawBody : void 0 }); } /** * Write a Web `Response` to a node response. The body is written chunk by * chunk, so an SSE stream reaches the client while the server writes it. * When the client or the bridge closes the socket first, the Web stream is * cancelled and this function returns. */ async function sendWebResponse(res, response) { res.statusCode = response.status; response.headers.forEach((value, key) => { res.setHeader(key, value); }); if (response.body === null) { res.end(); return; } res.flushHeaders(); const reader = response.body.getReader(); res.on("close", () => void reader.cancel()); for (let chunk = await reader.read(); !chunk.done; chunk = await reader.read()) res.write(chunk.value); res.end(); } export { readRawBody, sendWebResponse, toWebRequest }; //# sourceMappingURL=node-web.js.map