Skip to content

Commit

Permalink
perf: generate block hashes as we parse the stream
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed Sep 7, 2021
1 parent 74a305c commit d04670c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/integrity.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const BLOCK_SIZE = 4 * 1024 * 1024

const pipeline = promisify(stream.pipeline)

function hashBlock (block) {
return crypto.createHash(ALGORITHM).update(block).digest('hex')
}

async function getFileIntegrity (path) {
const read = fs.createReadStream(path)
const hash = crypto.createHash(ALGORITHM)
Expand All @@ -32,7 +36,7 @@ async function getFileIntegrity (path) {
currentBlockSize += diffToSlice
currentBlock.push(chunk.slice(0, diffToSlice))
if (currentBlockSize === BLOCK_SIZE) {
blocks.push(Buffer.concat(currentBlock))
blocks.push(hashBlock(Buffer.concat(currentBlock)))
currentBlock = []
currentBlockSize = 0
}
Expand All @@ -44,7 +48,7 @@ async function getFileIntegrity (path) {
callback()
},
flush (callback) {
blocks.push(Buffer.concat(currentBlock))
blocks.push(hashBlock(Buffer.concat(currentBlock)))
currentBlock = []
callback()
}
Expand All @@ -55,7 +59,7 @@ async function getFileIntegrity (path) {
algorithm: ALGORITHM,
hash: hash.read(),
blockSize: BLOCK_SIZE,
blocks: blocks.map(block => crypto.createHash(ALGORITHM).update(block).digest('hex'))
blocks: blocks
}
}

Expand Down

0 comments on commit d04670c

Please sign in to comment.