Merge branch 'signed-integrity-cli-better-file-reading' into 'master'

signed-integrity cli: Improve file reading

See merge request rysiekpl/libresilient!24
merge-requests/23/merge
Michał "rysiek" Woźniak 2024-01-12 14:24:47 +00:00
commit 8f23bc6965
1 zmienionych plików z 21 dodań i 25 usunięć

Wyświetl plik

@ -117,32 +117,28 @@ let getFileIntegrity = async (path, algos) => {
// are we working with a file?
if (fileInfo.isFile) {
// initialize
content = new Uint8Array()
var buf = new Uint8Array(1000);
// read the first batch
var numread = file.readSync(buf);
// read the rest, if there is anything to read
while (numread !== null) {
//console.log(` +-- read: ${numread}`)
//console.log(` +-- length: ${content.length}`)
// there has to be a better way...
var new_content = new Uint8Array(content.length + numread);
//console.log(` +-- new length: ${new_content.length}`)
new_content.set(content)
if (buf.length === numread) {
new_content.set(buf, content.length)
} else {
new_content.set(buf.slice(0, numread), content.length)
const reader = file.readable.getReader();
const chunks = [];
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
content = new_content
// read some more
numread = file.readSync(buf);
chunks.push(value);
}
let length = 0;
for (const chunk of chunks) {
length += chunk.length;
}
content = new Uint8Array(length);
let index = 0;
for (const chunk of chunks) {
content.set(chunk, index);
index += chunk.length;
}
}