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}`);
|
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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user