diff --git a/src/cdn/funcs/saveResponse.ts b/src/cdn/funcs/saveResponse.ts index 2daa6cd..00d5f51 100644 --- a/src/cdn/funcs/saveResponse.ts +++ b/src/cdn/funcs/saveResponse.ts @@ -12,25 +12,13 @@ export default function saveResponse( return reject(`Expected status code 200 but received ${response.statusCode}.`); } - //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 response.pipe(writeStream); - //If we finished reading response, check for correctness, then return it - response.on('end', () => { - //Check that length is correct - /*if (totalLength !== headerLength) { - return reject(`Expected length ${headerLength} but received ${totalLength}.`); - }*/ - - //wait until everything is written to disk, then return file name - writeStream.end(() => { - //TODO: this is no longer firing ever since we replaced on('data') event by pipe() - resolve(filePath); - }); + //Wait until everything is written to disk, then return file name + writeStream.on('close', () => { + resolve(filePath); }); }