Coverage

85%
20
17
3

gzipme.js

85%
20
17
3
LineHitsSource
11var fs = require('fs')
2 , path = require('path')
3 , zlib = require('zlib')
4 , existsSync = 'existsSync' in fs ? fs.existsSync : path.existsSync
5;
6
71const OPTS = {
8 level: zlib.Z_BEST_COMPRESSION
9 , memLevel: zlib.Z_BEST_COMPRESSION
10};
11
121module.exports = function(file, overwrite) {
134 if (!existsSync(file)) {
141 throw new Error("File "+ file +" doesn't exist.");
15 }
163 try {
173 var gzip = zlib.createGzip(OPTS);
183 var gzFile = file + '.gz';
193 if (overwrite && typeof overwrite === 'string') {
201 gzFile = overwrite;
211 overwrite = false;
22 }
233 var inputStream = fs.createReadStream(file);
243 var outStream = fs.createWriteStream(gzFile);
253 inputStream.pipe(gzip).pipe(outStream);
263 if(overwrite) {
271 fs.unlinkSync(file);
281 fs.renameSync(gzFile, file);
29 }
30 } catch (e) {
310 if (!overwrite && existsSync(gzFile)) {
320 fs.unlinkSync(gzFile);
33 }
340 throw new Error('Invalid file.');
35 }
36};