Skip to content

Commit

Permalink
fix: handle error from yazl when file not exists (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondYuan authored and fengmk2 committed Nov 21, 2018
1 parent 07eaf90 commit 3713a0b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/zip/file_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ZipFileStream extends stream.Transform {
const zipStream = zipfile.outputStream;
zipStream.on('data', data => this.push(data));
zipStream.on('end', () => this.ready(true));
zipfile.on('error', err => this.emit('error', err));

if (sourceType !== 'file') {
assert(opts.relativePath, 'opts.relativePath is required when compressing a buffer, or a stream');
Expand Down
16 changes: 16 additions & 0 deletions test/zip/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ describe('test/zip/index.test.js', () => {
assert(fs.existsSync(destFile));
});

it('zip.compressFile(file, stream) should handle error if file not exist', function* () {
const sourceFile = path.join(__dirname, '..', 'fixtures', 'xx.log', 'not_exist');
destDir = path.join(os.tmpdir(), uuid.v4());
mkdirp.sync(destDir);
const destFile = path.join(destDir, uuid.v4() + '.zip');
console.log('dest', destFile);
const fileStream = fs.createWriteStream(destFile);
let err;
try {
yield compressing.zip.compressFile(sourceFile, fileStream);
} catch (e) {
err = e;
}
assert(err);
});

it('zip.compressFile(file, destStream) should error if destStream emit error', function* () {
const sourceFile = path.join(__dirname, '..', 'fixtures', 'xx.log');
destDir = path.join(os.tmpdir(), uuid.v4());
Expand Down

0 comments on commit 3713a0b

Please sign in to comment.