From dd5de4cbb152c2f12a041bfea9b2aa895f6785cd Mon Sep 17 00:00:00 2001 From: GraemeARobinson <99871539+GraemeARobinson@users.noreply.github.com> Date: Sat, 23 Sep 2023 02:08:09 +0100 Subject: [PATCH] Don't give up when unable to extract some files (#244) * 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 --- lib/asar.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/asar.js b/lib/asar.js index 050e1a7..a0c4563 100644 --- a/lib/asar.js +++ b/lib/asar.js @@ -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) @@ -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) {