Skip to content

Commit

Permalink
Don't give up when unable to extract some files (#244)
Browse files Browse the repository at this point in the history
* Don't give up when unable to extract some files

Add try/catch block around file extraction so that the rest of the files are extracted if there is an issue with only a subset of the files in the archive.

* Fix unmatched curly brace

Added a closing curly brace because I'd missed one out

* removed a semicolon that was upsetting the linter

* Throw AggregateError if file couldn't be extracted

Collect file extraction errors and throw AggregateError after every file extraction has been attempted

* Removed logging & fixed extractionErrors handling 

...as suggested by Samuel Attard

* Remove dependancy on AggregateError
  • Loading branch information
GraemeARobinson authored Sep 23, 2023
1 parent f60a4c1 commit dd5de4c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ module.exports.extractAll = function (archive, dest) {
// create destination directory
fs.mkdirpSync(dest)

const extractionErrors = []
for (const fullPath of filenames) {
// Remove leading slash
const filename = fullPath.substr(1)
Expand All @@ -200,14 +201,23 @@ module.exports.extractAll = function (archive, dest) {
const linkTo = path.join(relativePath, path.basename(file.link))
fs.symlinkSync(linkTo, destFilename)
} else {
// it's a file, extract it
const content = disk.readFileSync(filesystem, filename, file)
fs.writeFileSync(destFilename, content)
if (file.executable) {
fs.chmodSync(destFilename, '755')
// it's a file, try to extract it
try {
const content = disk.readFileSync(filesystem, filename, file)
fs.writeFileSync(destFilename, content)
if (file.executable) {
fs.chmodSync(destFilename, '755')
}
} catch (e) {
extractionErrors.push(e)
}
}
}
if (extractionErrors.length) {
throw new Error(
'Unable to extract some files:\n\n' +
extractionErrors.map(error => error.stack).join('\n\n'))
}
}

module.exports.uncache = function (archive) {
Expand Down

0 comments on commit dd5de4c

Please sign in to comment.