Total Complexity | 8 |
Complexity/F | 1 |
Lines of Code | 45 |
Function Count | 8 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | /** global: fileBytesReader */ |
||
20 | const fileBytesReader = function(file) { |
||
1 ignored issue
–
show
|
|||
21 | let _i = 0; |
||
22 | let _fileSize = file.size; |
||
23 | let _reader = new FileReader(); |
||
24 | let _file = file; |
||
25 | |||
26 | function readChunk(length) { |
||
27 | return new Promise((resolve, reject) => { |
||
28 | let blob = _file.slice(_i, _i += length); |
||
29 | |||
30 | _reader.onload = () => { |
||
31 | // return Uint8Array |
||
32 | resolve(new Uint8Array(_reader.result)); |
||
33 | }; |
||
34 | |||
35 | _reader.onerror = reject; |
||
36 | |||
37 | _reader.readAsArrayBuffer(blob); |
||
38 | }); |
||
39 | } |
||
40 | |||
41 | async function readBytes(length) { |
||
42 | return await readChunk(length); |
||
43 | } |
||
44 | |||
45 | async function readByte() { |
||
46 | let bytes = await readBytes(1); |
||
47 | return bytes[0]; |
||
48 | } |
||
49 | |||
50 | function getCurrentPosition() { |
||
51 | return _i; |
||
52 | } |
||
53 | |||
54 | function getLength() { |
||
55 | return _fileSize; |
||
56 | } |
||
57 | |||
58 | return { |
||
59 | readByte: readByte, |
||
60 | readBytes: readBytes, |
||
61 | getCurrentPosition: getCurrentPosition, |
||
62 | getLength: getLength, |
||
63 | }; |
||
64 | }; |
||
65 |