Skip to content

Commit

Permalink
fetch: make body async iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Jun 22, 2019
1 parent 642eaf9 commit 11a2161
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions js/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ class Body implements domTypes.Body, domTypes.ReadableStream, io.ReadCloser {
tee(): [domTypes.ReadableStream, domTypes.ReadableStream] {
return notImplemented();
}

[Symbol.asyncIterator](): AsyncIterableIterator<Uint8Array> {
return io.toAsyncIterator(this);
}
}

export class Response implements domTypes.Response {
Expand Down
11 changes: 11 additions & 0 deletions js/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ testPerm({ net: true }, async function fetchBlob(): Promise<void> {
assertEquals(blob.size, Number(headers.get("Content-Length")));
});

testPerm({ net: true }, async function fetchAsyncIterator(): Promise<void> {
const response = await fetch("http://localhost:4545/package.json");
const headers = response.headers;
let total = 0;
for await (const chunk of response.body) {
total += chunk.length;
}

assertEquals(total, Number(headers.get("Content-Length")));
});

testPerm({ net: true }, async function responseClone(): Promise<void> {
const response = await fetch("http://localhost:4545/package.json");
const response1 = response.clone();
Expand Down

0 comments on commit 11a2161

Please sign in to comment.