Last active
December 23, 2020 17:06
-
-
Save kmoskwiak/3af8ad98d2a248a0236d2c8a809315f3 to your computer and use it in GitHub Desktop.
generateChunks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function* generateChunks(filePath, size) { | |
const sharedBuffer = Buffer.alloc(size); | |
const stats = fs.statSync(filePath); // file details | |
const fd = fs.openSync(filePath); // file descriptor | |
let bytesRead = 0; // how many bytes were read | |
let end = size; | |
for(let i = 0; i < Math.ceil(stats.size / size); i++) { | |
await readBytes(fd, sharedBuffer); | |
bytesRead = (i + 1) * size; | |
if(bytesRead > stats.size) { | |
// When we reach the end of file, | |
// we have to calculate how many bytes were actually read | |
end = size - (bytesRead - stats.size); | |
} | |
yield sharedBuffer.slice(0, end); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment