From 0847d79db0a726fcb881ed3983e3ff16333b9f06 Mon Sep 17 00:00:00 2001 From: Nitin Patel Date: Fri, 22 Mar 2019 18:59:26 +0530 Subject: [PATCH] refactor: some refactoring in mfs --- src/mfs.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/mfs.js b/src/mfs.js index fea8bda..ad80ec2 100644 --- a/src/mfs.js +++ b/src/mfs.js @@ -21,7 +21,7 @@ module.exports = function (options) { } store.createWriteStream = function (opts, cb) { - if (typeof opts === 'string') opts = {key: opts} + if (typeof opts === 'string') opts = { key: opts } if (opts.name) opts.key = opts.name if (!cb) cb = noop @@ -54,7 +54,7 @@ module.exports = function (options) { } store.createReadStream = function (opts) { - if (typeof opts === 'string') opts = {key: opts} + if (typeof opts === 'string') opts = { key: opts } if (opts.name) opts.key = opts.name const readPath = normalisePath(store.baseDir + opts.key) @@ -63,7 +63,7 @@ module.exports = function (options) { const readableStream = ipfs.files.readReadableStream(readPath) readableStream.on('error', (error) => { - if (error.toString().indexOf('does not exist') > -1 || error.toString().indexOf('Not a directory') > -1) { + if (isNotFoundError(error)) { error.notFound = true } }) @@ -72,7 +72,7 @@ module.exports = function (options) { } store.exists = function (opts, cb) { - if (typeof opts === 'string') opts = {key: opts} + if (typeof opts === 'string') opts = { key: opts } if (opts.name) opts.key = opts.name if (!cb) cb = noop @@ -80,20 +80,20 @@ module.exports = function (options) { log(`stat ${statPath}`) ipfs.files.stat(statPath, {}, (error) => { - if (error) { - if (error.toString().indexOf('does not exist') > -1 || error.toString().indexOf('Not a directory') > -1) { - return cb(null, false) - } + if (!error) { + return cb(null, true) + } - return cb(error) + if (isNotFoundError(error)) { + return cb(null, false) } - cb(null, true) + return cb(error) }) } store.remove = function (opts, cb) { - if (typeof opts === 'string') opts = {key: opts} + if (typeof opts === 'string') opts = { key: opts } if (opts.name) opts.key = opts.name if (!cb) cb = noop @@ -111,3 +111,7 @@ function noop () {} function normalisePath (path) { return path.replace(/\/(\/)+/g, '/') } + +function isNotFoundError (error) { + return error.toString().indexOf('does not exist') > -1 || error.toString().indexOf('Not a directory') > -1 +}