mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
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:
parent
68ccba8f74
commit
dcf913c17e
@ -105,8 +105,6 @@ try {
|
||||
npmLog.info('sharp', `Using cached ${tarPathCache}`);
|
||||
extractTarball(tarPathCache);
|
||||
} else {
|
||||
const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
|
||||
const tmpFile = fs.createWriteStream(tarPathTemp);
|
||||
const url = distBaseUrl + tarFilename;
|
||||
npmLog.info('sharp', `Downloading ${url}`);
|
||||
simpleGet({ url: url, agent: agent() }, function (err, response) {
|
||||
@ -117,13 +115,24 @@ try {
|
||||
} else if (response.statusCode !== 200) {
|
||||
fail(new Error(`Status ${response.statusCode} ${response.statusMessage}`));
|
||||
} else {
|
||||
const tarPathTemp = path.join(os.tmpdir(), `${process.pid}-${tarFilename}`);
|
||||
const tmpFileStream = fs.createWriteStream(tarPathTemp);
|
||||
response
|
||||
.on('error', fail)
|
||||
.pipe(tmpFile);
|
||||
.on('error', function (err) {
|
||||
tmpFileStream.destroy(err);
|
||||
})
|
||||
.on('close', function () {
|
||||
if (!response.complete) {
|
||||
tmpFileStream.destroy(new Error('Download incomplete (connection was terminated)'));
|
||||
}
|
||||
});
|
||||
tmpFile
|
||||
.on('error', fail)
|
||||
})
|
||||
.pipe(tmpFileStream);
|
||||
tmpFileStream
|
||||
.on('error', function (err) {
|
||||
// Clean up temporary file
|
||||
fs.unlinkSync(tarPathTemp);
|
||||
fail(err);
|
||||
})
|
||||
.on('close', function () {
|
||||
try {
|
||||
// Attempt to rename
|
||||
@ -136,6 +145,8 @@ try {
|
||||
extractTarball(tarPathCache);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
fail(err);
|
||||
|
Loading…
x
Reference in New Issue
Block a user