From 7785c728d8f62b55fcce29961dc1edc54711d266 Mon Sep 17 00:00:00 2001 From: acyza <101238421+acyza@users.noreply.github.com> Date: Thu, 22 Dec 2022 11:27:46 +0800 Subject: [PATCH] fix: throw error when source is not exists (#78) --- lib/utils.js | 5 +++++ test/zip/uncompress_stream.test.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/utils.js b/lib/utils.js index a7c4e91..4c31f3a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -78,6 +78,11 @@ exports.makeUncompressFn = StreamClass => { return (source, destDir, opts) => { opts = opts || {}; opts.source = source; + if (!source) { // !source 和 sourceType中返回undeined对应 + const error = new Error('Type is not supported, must be a file path, file buffer, or a readable stream'); + error.name = 'IlligalSourceError'; + throw error; + } if (destType(destDir) !== 'path') { const error = new Error('uncompress destination must be a directory'); error.name = 'IlligalDestError'; diff --git a/test/zip/uncompress_stream.test.js b/test/zip/uncompress_stream.test.js index 4fbb8b6..79db439 100644 --- a/test/zip/uncompress_stream.test.js +++ b/test/zip/uncompress_stream.test.js @@ -227,3 +227,18 @@ describe('test/zip/uncompress_stream.test.js', () => { }); }); }); + +it('should emit error if uncompress source is undefined', done => { + const timeout = setTimeout(()=>{ + done("uncompress timeout"); + }, 1000); + try { + compressing.zip.uncompress(undefined, originalDir) + .finally(() => clearTimeout(timeout)); + }catch(err) { + clearTimeout(timeout); + assert(err.name === 'IlligalSourceError'); + assert(err.message === 'Type is not supported, must be a file path, file buffer, or a readable stream'); + done(); + } +})