Skip to content

Commit 1714d1f

Browse files
nidhijajuchromium-wpt-export-bot
authored andcommitted
Fetch: Support BYOB reading for Response.body
ReadableByteStream is a variant of ReadableStream specialized for bytes[1]. Given the performance benefits, this CL adds BYOB support for Fetch by making Response.body a byte stream to allow for reading with a bring-your-own-buffer(BYOB) reader. The corresponding spec PR for this was landed at whatwg/fetch#1593. Tests for reading from Blob with a BYOB reader were factored out, as support for that will be implemented in follow-up CLs. [1] https://streams.spec.whatwg.org/#readable-byte-stream Bug: 1243329 Change-Id: I381b9f2272a7f1202fa748ae5c039ca0a998de00
1 parent d03a2d7 commit 1714d1f

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

FileAPI/blob/Blob-stream-BYOB.any.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// META: title=Blob Stream BYOB
2+
// META: script=../support/Blob.js
3+
// META: script=/common/gc.js
4+
'use strict';
5+
6+
// Helper function that triggers garbage collection while reading a chunk
7+
// if perform_gc is true.
8+
async function read_and_gc(reader, perform_gc) {
9+
// Passing Uint8Array for byte streams; non-byte streams will simply ignore it
10+
const read_promise = reader.read(new Uint8Array(64));
11+
if (perform_gc) {
12+
await garbageCollect();
13+
}
14+
return read_promise;
15+
}
16+
17+
// Takes in a ReadableStream and reads from it until it is done, returning
18+
// an array that contains the results of each read operation. If perform_gc
19+
// is true, garbage collection is triggered while reading every chunk.
20+
async function read_all_chunks(stream, { perform_gc = false, mode } = {}) {
21+
assert_true(stream instanceof ReadableStream);
22+
assert_true('getReader' in stream);
23+
const reader = stream.getReader({ mode });
24+
25+
assert_true('read' in reader);
26+
let read_value = await read_and_gc(reader, perform_gc);
27+
28+
let out = [];
29+
let i = 0;
30+
while (!read_value.done) {
31+
for (let val of read_value.value) {
32+
out[i++] = val;
33+
}
34+
read_value = await read_and_gc(reader, perform_gc);
35+
}
36+
return out;
37+
}
38+
39+
promise_test(async () => {
40+
const input_arr = [8, 241, 48, 123, 151];
41+
const typed_arr = new Uint8Array(input_arr);
42+
let blob = new Blob([typed_arr]);
43+
const stream = blob.stream();
44+
const chunks = await read_all_chunks(stream, { mode: "byob" });
45+
assert_array_equals(chunks, input_arr);
46+
}, "Reading Blob.stream() with BYOB reader")

FileAPI/blob/Blob-stream.any.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,4 @@ promise_test(async() => {
7171
const chunks = await read_all_chunks(stream, { perform_gc: true });
7272
assert_array_equals(chunks, input_arr);
7373
}, "Blob.stream() garbage collection of blob shouldn't break stream" +
74-
"consumption")
75-
76-
promise_test(async () => {
77-
const input_arr = [8, 241, 48, 123, 151];
78-
const typed_arr = new Uint8Array(input_arr);
79-
let blob = new Blob([typed_arr]);
80-
const stream = blob.stream();
81-
const chunks = await read_all_chunks(stream, { mode: "byob" });
82-
assert_array_equals(chunks, input_arr);
83-
}, "Reading Blob.stream() with BYOB reader")
74+
"consumption")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// META: global=window,worker
2+
// META: title=Response consume BYOB blob
3+
// META: script=../resources/utils.js
4+
5+
var textData = JSON.stringify("This is response's body");
6+
var blob = new Blob([textData], { "type": "text/plain" });
7+
8+
promise_test(function (test) {
9+
var response = new Response(blob);
10+
return validateStreamFromString(response.body.getReader({ mode: "byob" }), textData);
11+
}, `Read blob response's body as readableStream with mode="byob"}`);

fetch/api/response/response-consume-stream.any.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ var blob = new Blob([textData], { "type" : "text/plain" });
2020
var urlSearchParamsData = "name=value";
2121
var urlSearchParams = new URLSearchParams(urlSearchParamsData);
2222

23-
for (const mode of [undefined, "byob"]) {
24-
promise_test(function(test) {
25-
var response = new Response(blob);
26-
return validateStreamFromString(response.body.getReader({ mode }), textData);
27-
}, `Read blob response's body as readableStream with mode=${mode}`);
23+
promise_test(function (test) {
24+
var response = new Response(blob);
25+
return validateStreamFromString(response.body.getReader(), textData);
26+
}, "Read blob response's body as readableStream");
2827

28+
for (const mode of [undefined, "byob"]) {
2929
promise_test(function(test) {
3030
var response = new Response(textData);
3131
return validateStreamFromString(response.body.getReader({ mode }), textData);

0 commit comments

Comments
 (0)