Skip to content

Instantly share code, notes, and snippets.

@kmoskwiak
Last active December 23, 2020 17:06
Show Gist options
  • Save kmoskwiak/3af8ad98d2a248a0236d2c8a809315f3 to your computer and use it in GitHub Desktop.
Save kmoskwiak/3af8ad98d2a248a0236d2c8a809315f3 to your computer and use it in GitHub Desktop.
generateChunks
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