diff --git a/lib/filestorage.js b/lib/filestorage.js index 361a8e3..e285c81 100644 --- a/lib/filestorage.js +++ b/lib/filestorage.js @@ -37,8 +37,7 @@ class FileStorage { await utils.mkdirRecursive(path.dirname(file)); await fs.writeFile(file, data); - const stats = utils.getDataStats(data, opts.checksum, opts.dedupHash); - return stats; + return utils.getDataStats(data, opts.checksum, opts.dedupHash); } // Update file in the storage diff --git a/lib/fs.js b/lib/fs.js index d4840d6..7e408c7 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -6,6 +6,8 @@ const fs = require('fs'); const { promisify } = require('util'); +const { iter } = require('@metarhia/common'); + const list = [ 'readFile', 'writeFile', @@ -21,8 +23,8 @@ const list = [ if (process.version.slice(1).split('.')[0] >= 10) { module.exports = fs.promises; } else { - module.exports = list.reduce((acc, name) => { - acc[name] = promisify(fs[name]); - return acc; - }, {}); + module.exports = iter(list).collectWith( + {}, + (obj, name) => (obj[name] = promisify(fs[name])) + ); } diff --git a/lib/utils.js b/lib/utils.js index d572aac..849641e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -4,6 +4,8 @@ const fs = require('./fs'); const path = require('path'); const crypto = require('crypto'); const { crc32 } = require('crc'); +const util = require('util'); +const common = require('@metarhia/common'); const { zip, gzip } = require('compressing'); const CHECKSUM = 'CRC32'; @@ -41,20 +43,9 @@ const getDataStats = (data, checksum, dedupHash) => ({ size: Buffer.byteLength(data), }); -const mkdirRecursive = async dir => { - try { - await fs.access(dir); - } catch (err) { - if (err.code === 'ENOENT') { - await mkdirRecursive(path.dirname(dir)); - await fs.mkdir(dir); - } else { - throw new Error(`Cannot access directory '${dir}': ${err.message}`); - } - } -}; +const mkdirRecursive = util.promisify(common.mkdirp); -const rmdirRecursive = async dir => { +const rmRecursive = async dir => { try { await fs.access(dir); } catch (err) { @@ -68,7 +59,7 @@ const rmdirRecursive = async dir => { file = path.join(dir, file); const stats = await fs.stat(file); - if (stats.isDirectory()) await rmdirRecursive(file); + if (stats.isDirectory()) await rmRecursive(file); else await fs.unlink(file); } @@ -76,14 +67,14 @@ const rmdirRecursive = async dir => { }; const compress = async (file, minCompressSize, compression) => { - const stats = await fs.stat(file); - if (stats.size <= minCompressSize) return false; - const compressor = compressors[compression]; if (!compressor) { throw new Error(`Unknown compression type ${compression} specified`); } + const stats = await fs.stat(file); + if (stats.size <= minCompressSize) return false; + const filec = file + 'z'; await compressor.compressFile(file, filec); await fs.rename(filec, file); @@ -117,7 +108,7 @@ module.exports = { computeHash, getDataStats, mkdirRecursive, - rmdirRecursive, + rmRecursive, compress, uncompress, }; diff --git a/test/filestorage.js b/test/filestorage.js index 0b65cf4..32f26c7 100644 --- a/test/filestorage.js +++ b/test/filestorage.js @@ -22,7 +22,7 @@ fsTest.beforeEach(async () => { fsTest.afterEach(async (test, cb) => { try { - await utils.rmdirRecursive(testDir); + await utils.rmRecursive(testDir); } catch (err) { test.fail(err); } diff --git a/test/utils.js b/test/utils.js index b9e642a..462253f 100644 --- a/test/utils.js +++ b/test/utils.js @@ -92,5 +92,5 @@ metatests.test('', async test => { const d = await utils.uncompress(file, opts); test.strictSame(d, data); - await utils.rmdirRecursive(testDir); + await utils.rmRecursive(testDir); });