diff --git a/test/http/HttpResponse.test.ts b/test/http/HttpResponse.test.ts index bfe5469..15cacad 100644 --- a/test/http/HttpResponse.test.ts +++ b/test/http/HttpResponse.test.ts @@ -400,6 +400,57 @@ describe('HttpResponse', () => { }); expect(await res.text()).to.equal('Hello from stream'); }); + + it('AsyncIterable body', async () => { + // AsyncIterable is useful for streaming responses (e.g., AI chat responses) + const encoder = new TextEncoder(); + + async function* generateChunks(): AsyncIterable { + await Promise.resolve(); + yield encoder.encode('Async '); + yield encoder.encode('iterable '); + yield encoder.encode('content'); + } + + const res = new HttpResponse({ + body: generateChunks(), + }); + expect(await res.text()).to.equal('Async iterable content'); + }); + + it('AsyncIterable body with multiple chunks', async () => { + const encoder = new TextEncoder(); + const chunks = ['chunk1', 'chunk2', 'chunk3']; + + async function* generateChunks(): AsyncIterable { + await Promise.resolve(); + for (const chunk of chunks) { + yield encoder.encode(chunk); + } + } + + const res = new HttpResponse({ + body: generateChunks(), + }); + expect(await res.text()).to.equal('chunk1chunk2chunk3'); + }); + + it('AsyncIterable body with delayed chunks', async () => { + const encoder = new TextEncoder(); + + async function* generateDelayedChunks(): AsyncIterable { + yield encoder.encode('first'); + await new Promise((resolve) => setTimeout(resolve, 10)); + yield encoder.encode('second'); + await new Promise((resolve) => setTimeout(resolve, 10)); + yield encoder.encode('third'); + } + + const res = new HttpResponse({ + body: generateDelayedChunks(), + }); + expect(await res.text()).to.equal('firstsecondthird'); + }); }); describe('HttpHeadersInit types', () => { diff --git a/types/http.d.ts b/types/http.d.ts index 5373544..8e8ed73 100644 --- a/types/http.d.ts +++ b/types/http.d.ts @@ -12,7 +12,7 @@ import { InvocationContext } from './InvocationContext'; * Represents the body types that can be used in an HTTP response. * This is a local definition to avoid dependency on lib.dom. * Compatible with Node.js native Fetch API body types. - * Includes Node.js Readable streams for HTTP streaming scenarios. + * Includes Node.js Readable streams and AsyncIterable for HTTP streaming scenarios. */ export type HttpResponseBodyInit = | ReadableStream @@ -20,6 +20,7 @@ export type HttpResponseBodyInit = | Blob | ArrayBufferView | ArrayBuffer + | AsyncIterable | FormData | URLSearchParams | string