🐛 Fix race condition in file download
This commit is contained in:
parent
662c2d5f43
commit
8b1ad6f7e2
3 changed files with 11 additions and 13 deletions
|
@ -15,9 +15,11 @@ export default function saveResponse(
|
|||
//Remember file size
|
||||
const headerLength = Number(response.headers['content-length']);
|
||||
|
||||
const writeStream = fs.createWriteStream(filePath);
|
||||
|
||||
//If we receive a part of the response, write it to disk
|
||||
let previousChunk: Promise<void> = new Promise((innerResolve) => { innerResolve(); });
|
||||
let totalLength = 0;
|
||||
const chunkPromises: Array<Promise<void>> = [];
|
||||
response.on('data', (chunk: Buffer) => {
|
||||
totalLength += chunk.length;
|
||||
|
||||
|
@ -26,18 +28,12 @@ export default function saveResponse(
|
|||
return reject(`Expected length ${headerLength} but received at least ${totalLength}.`);
|
||||
}
|
||||
|
||||
//If previous chunk was not yet written to disk, wait until it finished to avoid a race condition
|
||||
previousChunk.then(() => {
|
||||
previousChunk = new Promise((innerResolve, innerReject) => {
|
||||
//Write chunk to disk
|
||||
fs.appendFile(filePath, chunk, { encoding: 'binary' }, (error) => {
|
||||
if (error) {
|
||||
return reject(`Could not write to disk: [${error.code}] ${error.name}: ${error.message}.`);
|
||||
}
|
||||
innerResolve();
|
||||
});
|
||||
//Write chunk to disk
|
||||
chunkPromises.push(new Promise((writeResolve) => {
|
||||
writeStream.write(chunk, () => {
|
||||
writeResolve();
|
||||
});
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
//If we finished reading response, check for correctness, then return it
|
||||
|
@ -50,7 +46,7 @@ export default function saveResponse(
|
|||
//wait until everything is written to disk, then return file name
|
||||
//TODO: need to automatically delete file once it is no longer used
|
||||
//TODO: need to provide methods to seek through file
|
||||
previousChunk.then(() => {
|
||||
Promise.all(chunkPromises).then(() => {
|
||||
resolve(filePath);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue