Skip to content

Commit

Permalink
refactor: some refactoring in mfs
Browse files Browse the repository at this point in the history
  • Loading branch information
niinpatel committed Mar 22, 2019
1 parent 9f06ade commit 0847d79
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/mfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
})
Expand All @@ -72,28 +72,28 @@ 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

const statPath = normalisePath(store.baseDir + opts.key)

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

Expand All @@ -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
}

0 comments on commit 0847d79

Please sign in to comment.