Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions test/http/HttpResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,57 @@ describe('HttpResponse', () => {
});
expect(await res.text()).to.equal('Hello from stream');
});

it('AsyncIterable<Uint8Array> body', async () => {
// AsyncIterable is useful for streaming responses (e.g., AI chat responses)
const encoder = new TextEncoder();

async function* generateChunks(): AsyncIterable<Uint8Array> {
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<Uint8Array> body with multiple chunks', async () => {
const encoder = new TextEncoder();
const chunks = ['chunk1', 'chunk2', 'chunk3'];

async function* generateChunks(): AsyncIterable<Uint8Array> {
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<Uint8Array> body with delayed chunks', async () => {
const encoder = new TextEncoder();

async function* generateDelayedChunks(): AsyncIterable<Uint8Array> {
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', () => {
Expand Down
3 changes: 2 additions & 1 deletion types/http.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ 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
| Readable
| Blob
| ArrayBufferView
| ArrayBuffer
| AsyncIterable<Uint8Array>
| FormData
| URLSearchParams
| string
Expand Down
Loading