summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoshiya Hinosawa <stibium121@gmail.com>2019-06-22 23:22:27 +0900
committerRyan Dahl <ry@tinyclouds.org>2019-06-22 07:22:27 -0700
commit988bcbb8842d12202f278808698a6b546718e764 (patch)
treeb5559f8a485d3aa19167a41bfc14dcfc5de68691
parent201ddd29a7908d457ed43b030476707d32848868 (diff)
fetch: make body async iterable (#2563)
-rw-r--r--js/fetch.ts4
-rw-r--r--js/fetch_test.ts11
2 files changed, 15 insertions, 0 deletions
diff --git a/js/fetch.ts b/js/fetch.ts
index b1cd8f4aa..d5f01b870 100644
--- a/js/fetch.ts
+++ b/js/fetch.ts
@@ -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 {
diff --git a/js/fetch_test.ts b/js/fetch_test.ts
index b93f7845a..032727dbc 100644
--- a/js/fetch_test.ts
+++ b/js/fetch_test.ts
@@ -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();