File: common.js

package info (click to toggle)
firefox 147.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,320 kB
  • sloc: cpp: 7,607,359; javascript: 6,533,295; ansic: 3,775,223; python: 1,415,500; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (33 lines) | stat: -rw-r--r-- 1,244 bytes parent folder | download | duplicates (21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Compare two Uint8Arrays.
function compareArrays(actual, expected) {
  assert_true(actual instanceof Uint8Array, 'actual is Uint8Array');
  assert_true(expected instanceof Uint8Array, 'expected is Uint8Array');
  assert_equals(actual.byteLength, expected.byteLength, 'lengths equal');
  for (let i = 0; i < expected.byteLength; ++i)
    assert_equals(actual[i], expected[i], `Mismatch at position ${i}.`);
}

// Reads from |reader| until at least |targetLength| is read or the stream is
// closed. The data is returned as a combined Uint8Array.
async function readWithLength(reader, targetLength) {
  const chunks = [];
  let actualLength = 0;

  while (true) {
    let {value, done} = await reader.read();
    chunks.push(value);
    actualLength += value.byteLength;

    if (actualLength >= targetLength || done) {
      // It would be better to allocate |buffer| up front with the number of
      // of bytes expected but this is the best that can be done without a
      // BYOB reader to control the amount of data read.
      const buffer = new Uint8Array(actualLength);
      chunks.reduce((offset, chunk) => {
        buffer.set(chunk, offset);
        return offset + chunk.byteLength;
      }, 0);
      return buffer;
    }
  }
}