tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Blob-stream.any.js (3214B)


      1 // META: title=Blob Stream
      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 blob = new Blob(["PASS"]);
     41  const stream = blob.stream();
     42  const chunks = await read_all_chunks(stream);
     43  for (let [index, value] of chunks.entries()) {
     44    assert_equals(value, "PASS".charCodeAt(index));
     45  }
     46 }, "Blob.stream()")
     47 
     48 promise_test(async () => {
     49  const blob = new Blob();
     50  const stream = blob.stream();
     51  const chunks = await read_all_chunks(stream);
     52  assert_array_equals(chunks, []);
     53 }, "Blob.stream() empty Blob")
     54 
     55 promise_test(async () => {
     56  const input_arr = [8, 241, 48, 123, 151];
     57  const typed_arr = new Uint8Array(input_arr);
     58  const blob = new Blob([typed_arr]);
     59  const stream = blob.stream();
     60  const chunks = await read_all_chunks(stream);
     61  assert_array_equals(chunks, input_arr);
     62 }, "Blob.stream() non-unicode input")
     63 
     64 promise_test(async() => {
     65  const input_arr = [8, 241, 48, 123, 151];
     66  const typed_arr = new Uint8Array(input_arr);
     67  let blob = new Blob([typed_arr]);
     68  const stream = blob.stream();
     69  blob = null;
     70  await garbageCollect();
     71  const chunks = await read_all_chunks(stream, { perform_gc: true });
     72  assert_array_equals(chunks, input_arr);
     73 }, "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 chunksPromise = read_all_chunks(blob.stream());
     81  // It somehow matters to do GC here instead of doing `perform_gc: true`
     82  await garbageCollect();
     83  assert_array_equals(await chunksPromise, input_arr);
     84 }, "Blob.stream() garbage collection of stream shouldn't break stream " +
     85      "consumption")
     86 
     87 promise_test(async () => {
     88  const input_arr = [8, 241, 48, 123, 151];
     89  const typed_arr = new Uint8Array(input_arr);
     90  let blob = new Blob([typed_arr]);
     91  const stream = blob.stream();
     92  const chunks = await read_all_chunks(stream, { mode: "byob" });
     93  assert_array_equals(chunks, input_arr);
     94 }, "Reading Blob.stream() with BYOB reader")