Skip to content

Commit

Permalink
fixup! Replace callbacks with promises
Browse files Browse the repository at this point in the history
  • Loading branch information
SemenchenkoVitaliy committed May 13, 2019
1 parent 3549983 commit 1bb1c97
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 26 deletions.
3 changes: 1 addition & 2 deletions lib/filestorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
const fs = require('fs');
const { promisify } = require('util');

const { iter } = require('@metarhia/common');

const list = [
'readFile',
'writeFile',
Expand All @@ -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]))
);
}
27 changes: 9 additions & 18 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -68,22 +59,22 @@ 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);
}

await fs.rmdir(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);
Expand Down Expand Up @@ -117,7 +108,7 @@ module.exports = {
computeHash,
getDataStats,
mkdirRecursive,
rmdirRecursive,
rmRecursive,
compress,
uncompress,
};
2 changes: 1 addition & 1 deletion test/filestorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit 1bb1c97

Please sign in to comment.