From 56e60b6ce12626f1a920eea6e340e00aac3fcceb Mon Sep 17 00:00:00 2001 From: Edythator <20105459+Edythator@users.noreply.github.com> Date: Tue, 25 Jan 2022 00:41:10 +0100 Subject: [PATCH 1/3] fix: add error message for file not found Add an error message when trying to locate a non-existing file inside an archive when using `extract-file`. --- lib/filesystem.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/filesystem.js b/lib/filesystem.js index 85e1eb01..3e426d0c 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -139,6 +139,11 @@ class Filesystem { followLinks = typeof followLinks === 'undefined' ? true : followLinks const info = this.getNode(p) + if (typeof info === 'undefined') { + console.error(`The file "${p}" was not found in the archive.`) + process.exit(1) + } + // if followLinks is false we don't resolve symlinks if (info.link && followLinks) { return this.getFile(info.link) From 8453d5c182f4d70d27bc67a583730802187bdc59 Mon Sep 17 00:00:00 2001 From: Edythator <20105459+Edythator@users.noreply.github.com> Date: Tue, 25 Jan 2022 02:50:19 +0100 Subject: [PATCH 2/3] fix: error message handling Reworked the error handling to make it more "proper" as well as having added tests. --- bin/asar.js | 8 ++++++-- lib/asar.js | 7 ++++++- lib/filesystem.js | 3 +-- test/cli-spec.js | 3 +++ test/expected/extractthis-non-existent-file.txt | 1 + 5 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 test/expected/extractthis-non-existent-file.txt diff --git a/bin/asar.js b/bin/asar.js index 8efcc1a9..e01b0d80 100755 --- a/bin/asar.js +++ b/bin/asar.js @@ -61,8 +61,12 @@ program.command('extract-file ') .alias('ef') .description('extract one file from archive') .action(function (archive, filename) { - require('fs').writeFileSync(require('path').basename(filename), - asar.extractFile(archive, filename)) + const fileData = asar.extractFile(archive, filename) + if (typeof fileData === 'undefined') { + console.error(`The file "${filename}" was not found in the archive.`) + return + } + require('fs').writeFileSync(require('path').basename(filename), fileData) }) program.command('extract ') diff --git a/lib/asar.js b/lib/asar.js index 050e1a76..37f643c3 100644 --- a/lib/asar.js +++ b/lib/asar.js @@ -167,7 +167,12 @@ module.exports.listPackage = function (archive, options) { module.exports.extractFile = function (archive, filename) { const filesystem = disk.readFilesystemSync(archive) - return disk.readFileSync(filesystem, filename, filesystem.getFile(filename)) + const file = filesystem.getFile(filename) + if (typeof file === 'undefined') { + return undefined + } + + return disk.readFileSync(filesystem, filename, file) } module.exports.extractAll = function (archive, dest) { diff --git a/lib/filesystem.js b/lib/filesystem.js index 3e426d0c..b3405a99 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -140,8 +140,7 @@ class Filesystem { const info = this.getNode(p) if (typeof info === 'undefined') { - console.error(`The file "${p}" was not found in the archive.`) - process.exit(1) + return undefined } // if followLinks is false we don't resolve symlinks diff --git a/test/cli-spec.js b/test/cli-spec.js index 2017d323..da88b2cf 100644 --- a/test/cli-spec.js +++ b/test/cli-spec.js @@ -75,6 +75,9 @@ describe('command line interface', function () { assert.strictEqual(actual, expected) }) */ + it('should throw an error when trying to extract a non-existent file', async () => { + return assertAsarOutputMatches('ef test/input/extractthis.asar dir1/non-existent', 'test/expected/extractthis-non-existent-file.txt') + }) it('should extract an archive', async () => { await execAsar('e test/input/extractthis.asar tmp/extractthis-cli/') return compDirs('tmp/extractthis-cli/', 'test/expected/extractthis') diff --git a/test/expected/extractthis-non-existent-file.txt b/test/expected/extractthis-non-existent-file.txt new file mode 100644 index 00000000..2ea1fc6d --- /dev/null +++ b/test/expected/extractthis-non-existent-file.txt @@ -0,0 +1 @@ +The file "dir1/non-existent" was not found in the archive. \ No newline at end of file From c555f9cf73dad857f9f2974d8cadcb6ecafb5745 Mon Sep 17 00:00:00 2001 From: Edythator <20105459+Edythator@users.noreply.github.com> Date: Tue, 25 Jan 2022 19:39:57 +0100 Subject: [PATCH 3/3] fix: add exception handler for simplicity Added an exception handler instead of relying on an if branch. --- bin/asar.js | 10 +++++----- lib/filesystem.js | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/asar.js b/bin/asar.js index e01b0d80..56270c04 100755 --- a/bin/asar.js +++ b/bin/asar.js @@ -61,12 +61,12 @@ program.command('extract-file ') .alias('ef') .description('extract one file from archive') .action(function (archive, filename) { - const fileData = asar.extractFile(archive, filename) - if (typeof fileData === 'undefined') { - console.error(`The file "${filename}" was not found in the archive.`) - return + try { + const fileData = asar.extractFile(archive, filename) + require('fs').writeFileSync(require('path').basename(filename), fileData) + } catch (err) { + console.log(err.message) } - require('fs').writeFileSync(require('path').basename(filename), fileData) }) program.command('extract ') diff --git a/lib/filesystem.js b/lib/filesystem.js index b3405a99..da776f05 100644 --- a/lib/filesystem.js +++ b/lib/filesystem.js @@ -140,7 +140,7 @@ class Filesystem { const info = this.getNode(p) if (typeof info === 'undefined') { - return undefined + throw new Error(`The file "${p}" was not found in the archive.`) } // if followLinks is false we don't resolve symlinks