From 9203834f780556b8d1e471c656e2187fde86b75d Mon Sep 17 00:00:00 2001 From: Wesley Aptekar-Cassels Date: Tue, 9 Jan 2024 04:33:28 -0500 Subject: [PATCH] signed-integrity cli: Improve file reading This code uses the Deno reader API to collect a list of chunks of the file, then allocates an array for the entire file at once, and copies the chunks into the final array, instead of continuously reallocating new arrays that are larger and larger. This is significantly faster than the previous code in the case of large files. --- plugins/signed-integrity/cli.js | 46 +++++++++++++++------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/plugins/signed-integrity/cli.js b/plugins/signed-integrity/cli.js index d0063a3..8b4991e 100644 --- a/plugins/signed-integrity/cli.js +++ b/plugins/signed-integrity/cli.js @@ -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; } }