Install: fail on incomplete download and clean up tempfile (#2608)

- Fail when the connection closes before the response is complete
- Create tempfile only when needed, and clean it up on failure
This commit is contained in:
Alex Bradley 2021-03-05 07:21:34 -08:00 committed by GitHub
parent 68ccba8f74
commit dcf913c17e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -105,8 +105,6 @@ try {
npmLog.info('sharp', `Using cached ${tarPathCache}`); npmLog.info('sharp', `Using cached ${tarPathCache}`);
extractTarball(tarPathCache); extractTarball(tarPathCache);
} else { } else {
const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
const tmpFile = fs.createWriteStream(tarPathTemp);
const url = distBaseUrl + tarFilename; const url = distBaseUrl + tarFilename;
npmLog.info('sharp', `Downloading ${url}`); npmLog.info('sharp', `Downloading ${url}`);
simpleGet({ url: url, agent: agent() }, function (err, response) { simpleGet({ url: url, agent: agent() }, function (err, response) {
@ -117,13 +115,24 @@ try {
} else if (response.statusCode !== 200) { } else if (response.statusCode !== 200) {
fail(new Error(`Status ${response.statusCode} ${response.statusMessage}`)); fail(new Error(`Status ${response.statusCode} ${response.statusMessage}`));
} else { } else {
const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
const tmpFileStream = fs.createWriteStream(tarPathTemp);
response response
.on('error', fail) .on('error', function (err) {
.pipe(tmpFile); tmpFileStream.destroy(err);
})
.on('close', function () {
if (!response.complete) {
tmpFileStream.destroy(new Error('Download incomplete (connection was terminated)'));
} }
}); })
tmpFile .pipe(tmpFileStream);
.on('error', fail) tmpFileStream
.on('error', function (err) {
// Clean up temporary file
fs.unlinkSync(tarPathTemp);
fail(err);
})
.on('close', function () { .on('close', function () {
try { try {
// Attempt to rename // Attempt to rename
@ -136,6 +145,8 @@ try {
extractTarball(tarPathCache); extractTarball(tarPathCache);
}); });
} }
});
}
} }
} catch (err) { } catch (err) {
fail(err); fail(err);