@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;" />
1 lines • 4.97 kB
Source Map (JSON)
{"version":3,"file":"request-handler.mjs","names":[],"sources":["../../../../src/lib/integrations/node-http/request-handler.ts"],"sourcesContent":["import type { IncomingMessage } from \"http\";\nimport { Readable } from \"node:stream\";\n\nexport type IncomingWithBody = IncomingMessage & {\n body?: unknown;\n complete?: boolean;\n};\n\nexport function readableStreamToNodeStream(\n webStream: ReadableStream,\n): Readable {\n const reader = webStream.getReader();\n\n return new Readable({\n async read() {\n try {\n const { done, value } = await reader.read();\n if (done) {\n this.push(null);\n } else {\n this.push(Buffer.from(value));\n }\n } catch (err) {\n this.destroy(err as Error);\n }\n },\n });\n}\n\nexport function nodeStreamToReadableStream(\n nodeStream: Readable,\n): ReadableStream<Uint8Array> {\n return new ReadableStream({\n start(controller) {\n nodeStream.on(\"data\", (chunk) => {\n controller.enqueue(\n chunk instanceof Buffer ? new Uint8Array(chunk) : chunk,\n );\n });\n nodeStream.on(\"end\", () => {\n controller.close();\n });\n nodeStream.on(\"error\", (err) => {\n controller.error(err);\n });\n },\n cancel() {\n nodeStream.destroy();\n },\n });\n}\n\nexport function getFullUrl(req: IncomingMessage): string {\n // Use req.url (path relative to mount point) for Hono routing to work correctly.\n // Express sets req.url to the path after the mount point (e.g., \"/\" when mounted at \"/copilotkit\").\n // Pure Node HTTP sets req.url to the full path.\n const path = req.url || \"/\";\n const host =\n (req.headers[\"x-forwarded-host\"] as string) ||\n (req.headers.host as string) ||\n \"localhost\";\n const proto =\n (req.headers[\"x-forwarded-proto\"] as string) ||\n ((req.socket as any).encrypted ? \"https\" : \"http\");\n\n return `${proto}://${host}${path}`;\n}\n\nexport function toHeaders(rawHeaders: IncomingMessage[\"headers\"]): Headers {\n const headers = new Headers();\n\n for (const [key, value] of Object.entries(rawHeaders)) {\n if (value === undefined) continue;\n\n if (Array.isArray(value)) {\n value.forEach((entry) => headers.append(key, entry));\n continue;\n }\n\n headers.append(key, value);\n }\n\n return headers;\n}\n\nexport function isStreamConsumed(req: IncomingWithBody): boolean {\n const readableState = (req as any)._readableState;\n\n return Boolean(\n req.readableEnded ||\n req.complete ||\n readableState?.ended ||\n readableState?.endEmitted,\n );\n}\n\nexport function synthesizeBodyFromParsedBody(\n parsedBody: unknown,\n headers: Headers,\n): { body: BodyInit | null; contentType?: string } {\n if (parsedBody === null || parsedBody === undefined) {\n return { body: null };\n }\n\n if (parsedBody instanceof Buffer || parsedBody instanceof Uint8Array) {\n return { body: parsedBody };\n }\n\n if (typeof parsedBody === \"string\") {\n return {\n body: parsedBody,\n contentType: headers.get(\"content-type\") ?? \"text/plain\",\n };\n }\n\n return {\n body: JSON.stringify(parsedBody),\n contentType: \"application/json\",\n };\n}\n\nexport function isDisturbedOrLockedError(error: unknown): boolean {\n return (\n error instanceof TypeError &&\n typeof error.message === \"string\" &&\n (error.message.includes(\"disturbed\") || error.message.includes(\"locked\"))\n );\n}\n"],"mappings":";;;;AAQA,SAAgB,2BACd,WACU;CACV,MAAM,SAAS,UAAU,WAAW;AAEpC,QAAO,IAAI,SAAS,EAClB,MAAM,OAAO;AACX,MAAI;GACF,MAAM,EAAE,MAAM,UAAU,MAAM,OAAO,MAAM;AAC3C,OAAI,KACF,MAAK,KAAK,KAAK;OAEf,MAAK,KAAK,OAAO,KAAK,MAAM,CAAC;WAExB,KAAK;AACZ,QAAK,QAAQ,IAAa;;IAG/B,CAAC;;AAGJ,SAAgB,2BACd,YAC4B;AAC5B,QAAO,IAAI,eAAe;EACxB,MAAM,YAAY;AAChB,cAAW,GAAG,SAAS,UAAU;AAC/B,eAAW,QACT,iBAAiB,SAAS,IAAI,WAAW,MAAM,GAAG,MACnD;KACD;AACF,cAAW,GAAG,aAAa;AACzB,eAAW,OAAO;KAClB;AACF,cAAW,GAAG,UAAU,QAAQ;AAC9B,eAAW,MAAM,IAAI;KACrB;;EAE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