Skip to content

Commit 988bcbb

Browse files
kt3kry
authored andcommitted
fetch: make body async iterable (#2563)
1 parent 201ddd2 commit 988bcbb

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

js/fetch.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ class Body implements domTypes.Body, domTypes.ReadableStream, io.ReadCloser {
237237
tee(): [domTypes.ReadableStream, domTypes.ReadableStream] {
238238
return notImplemented();
239239
}
240+
241+
[Symbol.asyncIterator](): AsyncIterableIterator<Uint8Array> {
242+
return io.toAsyncIterator(this);
243+
}
240244
}
241245

242246
export class Response implements domTypes.Response {

js/fetch_test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ testPerm({ net: true }, async function fetchBlob(): Promise<void> {
3333
assertEquals(blob.size, Number(headers.get("Content-Length")));
3434
});
3535

36+
testPerm({ net: true }, async function fetchAsyncIterator(): Promise<void> {
37+
const response = await fetch("http://localhost:4545/package.json");
38+
const headers = response.headers;
39+
let total = 0;
40+
for await (const chunk of response.body) {
41+
total += chunk.length;
42+
}
43+
44+
assertEquals(total, Number(headers.get("Content-Length")));
45+
});
46+
3647
testPerm({ net: true }, async function responseClone(): Promise<void> {
3748
const response = await fetch("http://localhost:4545/package.json");
3849
const response1 = response.clone();

0 commit comments

Comments
 (0)