fix(Google Drive Node): Fix file upload for streams (#11698)

This commit is contained in:
Elias Meire
2024-11-13 11:44:16 +01:00
committed by GitHub
parent a412ab7ebf
commit 770230fbfe
4 changed files with 122 additions and 12 deletions

View File

@@ -131,3 +131,29 @@ export function setParentFolder(
return 'root';
}
}
export async function processInChunks(
stream: Readable,
chunkSize: number,
process: (chunk: Buffer, offset: number) => void | Promise<void>,
) {
let buffer = Buffer.alloc(0);
let offset = 0;
for await (const chunk of stream) {
buffer = Buffer.concat([buffer, chunk]);
while (buffer.length >= chunkSize) {
const chunkToProcess = buffer.subarray(0, chunkSize);
await process(chunkToProcess, offset);
buffer = buffer.subarray(chunkSize);
offset += chunkSize;
}
}
// Process last chunk, could be smaller than chunkSize
if (buffer.length > 0) {
await process(buffer, offset);
}
}