Skip to content

Commit c9a3667

Browse files
authored
Add test for BYOB reader for Blob.stream() (#37925)
1 parent cde25e7 commit c9a3667

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

FileAPI/blob/Blob-stream.any.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
// Helper function that triggers garbage collection while reading a chunk
77
// if perform_gc is true.
88
async function read_and_gc(reader, perform_gc) {
9-
const read_promise = reader.read();
9+
// Passing Uint8Array for byte streams; non-byte streams will simply ignore it
10+
const read_promise = reader.read(new Uint8Array(64));
1011
if (perform_gc) {
1112
await garbageCollect();
1213
}
@@ -16,10 +17,10 @@ async function read_and_gc(reader, perform_gc) {
1617
// Takes in a ReadableStream and reads from it until it is done, returning
1718
// an array that contains the results of each read operation. If perform_gc
1819
// is true, garbage collection is triggered while reading every chunk.
19-
async function read_all_chunks(stream, perform_gc = false) {
20+
async function read_all_chunks(stream, { perform_gc = false, mode } = {}) {
2021
assert_true(stream instanceof ReadableStream);
2122
assert_true('getReader' in stream);
22-
const reader = stream.getReader();
23+
const reader = stream.getReader({ mode });
2324

2425
assert_true('read' in reader);
2526
let read_value = await read_and_gc(reader, perform_gc);
@@ -67,7 +68,16 @@ promise_test(async() => {
6768
const stream = blob.stream();
6869
blob = null;
6970
await garbageCollect();
70-
const chunks = await read_all_chunks(stream, /*perform_gc=*/true);
71+
const chunks = await read_all_chunks(stream, { perform_gc: true });
7172
assert_array_equals(chunks, input_arr);
7273
}, "Blob.stream() garbage collection of blob shouldn't break stream" +
7374
"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")

0 commit comments

Comments
 (0)