UNPKG

@copilotkit/runtime

Version:

<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />

74 lines (72 loc) 2.46 kB
import "reflect-metadata"; import { Readable } from "node:stream"; //#region src/lib/integrations/node-http/request-handler.ts function readableStreamToNodeStream(webStream) { const reader = webStream.getReader(); return new Readable({ async read() { try { const { done, value } = await reader.read(); if (done) this.push(null); else this.push(Buffer.from(value)); } catch (err) { this.destroy(err); } } }); } function nodeStreamToReadableStream(nodeStream) { return new ReadableStream({ start(controller) { nodeStream.on("data", (chunk) => { controller.enqueue(chunk instanceof Buffer ? new Uint8Array(chunk) : chunk); }); nodeStream.on("end", () => { controller.close(); }); nodeStream.on("error", (err) => { controller.error(err); }); }, cancel() { nodeStream.destroy(); } }); } function getFullUrl(req) { const path = req.url || "/"; const host = req.headers["x-forwarded-host"] || req.headers.host || "localhost"; return `${req.headers["x-forwarded-proto"] || (req.socket.encrypted ? "https" : "http")}://${host}${path}`; } function toHeaders(rawHeaders) { const headers = new Headers(); for (const [key, value] of Object.entries(rawHeaders)) { if (value === void 0) continue; if (Array.isArray(value)) { value.forEach((entry) => headers.append(key, entry)); continue; } headers.append(key, value); } return headers; } function isStreamConsumed(req) { const readableState = req._readableState; return Boolean(req.readableEnded || req.complete || readableState?.ended || readableState?.endEmitted); } function synthesizeBodyFromParsedBody(parsedBody, headers) { if (parsedBody === null || parsedBody === void 0) return { body: null }; if (parsedBody instanceof Buffer || parsedBody instanceof Uint8Array) return { body: parsedBody }; if (typeof parsedBody === "string") return { body: parsedBody, contentType: headers.get("content-type") ?? "text/plain" }; return { body: JSON.stringify(parsedBody), contentType: "application/json" }; } function isDisturbedOrLockedError(error) { return error instanceof TypeError && typeof error.message === "string" && (error.message.includes("disturbed") || error.message.includes("locked")); } //#endregion export { getFullUrl, isDisturbedOrLockedError, isStreamConsumed, nodeStreamToReadableStream, readableStreamToNodeStream, synthesizeBodyFromParsedBody, toHeaders }; //# sourceMappingURL=request-handler.mjs.map