From 3d1e6b01c111b3a3507169ff81d2153495f20a41 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Thu, 5 May 2016 14:04:14 +0200 Subject: [PATCH] fix(core): consistent repo.exists checks --- package.json | 2 +- src/core/index.js | 26 ++++++++++++++++++-------- src/core/init.js | 9 ++++++--- src/core/utils.js | 15 +++++++++++++++ 4 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 src/core/utils.js diff --git a/package.json b/package.json index 1cc32fe4c0..bcafc88fdb 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "ipfs-data-importing": "^0.3.3", "ipfs-merkle-dag": "^0.5.0", "ipfs-multipart": "^0.1.0", - "ipfs-repo": "^0.7.1", + "ipfs-repo": "^0.8.0", "joi": "^8.0.2", "libp2p-ipfs": "^0.3.3", "lodash.get": "^4.2.1", diff --git a/src/core/index.js b/src/core/index.js index 474ade13fa..6b0c7f4e21 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -1,6 +1,5 @@ 'use strict' -const defaultRepo = require('./default-repo') const BlockService = require('ipfs-block-service') const Block = require('ipfs-block') const mDAG = require('ipfs-merkle-dag') @@ -11,10 +10,13 @@ const PeerInfo = require('peer-info') const multiaddr = require('multiaddr') const importer = require('ipfs-data-importing').import const libp2p = require('libp2p-ipfs') -const init = require('./init') const IPFSRepo = require('ipfs-repo') const PeerBook = require('peer-book') +const init = require('./init') +const defaultRepo = require('./default-repo') +const utils = require('./utils') + exports = module.exports = IPFS function IPFS (repo) { @@ -33,7 +35,7 @@ function IPFS (repo) { const peerInfoBook = new PeerBook() this.load = (callback) => { - repo.exists((err, exists) => { + utils.ifRepoExists(repo, (err) => { if (err) { throw err } @@ -58,11 +60,15 @@ function IPFS (repo) { opts = {} } - repo.exists((err, exists) => { - if (err) { return callback(err) } + utils.ifRepoExists(repo, (err) => { + if (err) { + return callback(err) + } repo.config.get((err, config) => { - if (err) { return callback(err) } + if (err) { + return callback(err) + } callback(null, config.Version.Current) }) @@ -100,8 +106,12 @@ function IPFS (repo) { callback = opts opts = {} } - repo.exists((err, res) => { - if (err) { return callback(err) } + + utils.ifRepoExists(repo, (err, res) => { + if (err) { + return callback(err) + } + repo.version.get(callback) }) }, diff --git a/src/core/init.js b/src/core/init.js index 76fa796a19..cdcfbf5bc7 100644 --- a/src/core/init.js +++ b/src/core/init.js @@ -14,9 +14,12 @@ module.exports = (repo, opts, callback) => { var config = require('../init-files/default-config.json') // Verify repo does not yet exist. - repo.exists((err, res) => { - if (err) { return callback(err) } - if (res === true) { + repo.exists((err, exists) => { + if (err) { + return callback(err) + } + + if (exists === true) { return callback(new Error('repo already exists')) } diff --git a/src/core/utils.js b/src/core/utils.js new file mode 100644 index 0000000000..4924d754f6 --- /dev/null +++ b/src/core/utils.js @@ -0,0 +1,15 @@ +'use strict' + +exports.ifRepoExists = (repo, cb) => { + repo.exists((err, exists) => { + if (err) { + return cb(err) + } + + if (exists === false) { + return cb(new Error('no ipfs repo found')) + } + + cb() + }) +}