Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit d762025

Browse files
fix(core): consistent repo.exists checks
1 parent 036eaf6 commit d762025

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

src/core/index.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const defaultRepo = require('./default-repo')
43
const BlockService = require('ipfs-block-service')
54
const Block = require('ipfs-block')
65
const mDAG = require('ipfs-merkle-dag')
@@ -11,10 +10,13 @@ const PeerInfo = require('peer-info')
1110
const multiaddr = require('multiaddr')
1211
const importer = require('ipfs-data-importing').import
1312
const libp2p = require('libp2p-ipfs')
14-
const init = require('./init')
1513
const IPFSRepo = require('ipfs-repo')
1614
const PeerBook = require('peer-book')
1715

16+
const init = require('./init')
17+
const defaultRepo = require('./default-repo')
18+
const utils = require('./utils')
19+
1820
exports = module.exports = IPFS
1921

2022
function IPFS (repo) {
@@ -33,7 +35,7 @@ function IPFS (repo) {
3335
const peerInfoBook = new PeerBook()
3436

3537
this.load = (callback) => {
36-
repo.exists((err, exists) => {
38+
utils.ifRepoExists(repo, (err) => {
3739
if (err) {
3840
throw err
3941
}
@@ -58,11 +60,15 @@ function IPFS (repo) {
5860
opts = {}
5961
}
6062

61-
repo.exists((err, exists) => {
62-
if (err) { return callback(err) }
63+
utils.ifRepoExists(repo, (err) => {
64+
if (err) {
65+
return callback(err)
66+
}
6367

6468
repo.config.get((err, config) => {
65-
if (err) { return callback(err) }
69+
if (err) {
70+
return callback(err)
71+
}
6672

6773
callback(null, config.Version.Current)
6874
})
@@ -100,8 +106,12 @@ function IPFS (repo) {
100106
callback = opts
101107
opts = {}
102108
}
103-
repo.exists((err, res) => {
104-
if (err) { return callback(err) }
109+
110+
utils.ifRepoExists(repo, (err, res) => {
111+
if (err) {
112+
return callback(err)
113+
}
114+
105115
repo.version.get(callback)
106116
})
107117
},
@@ -392,6 +402,21 @@ function IPFS (repo) {
392402
ping: notImpl
393403
}
394404

405+
this.bitswap = {
406+
start: () => {
407+
408+
},
409+
stat: () => {
410+
// TODO
411+
},
412+
wantlist: () => {
413+
// TODO
414+
},
415+
unwant: () => {
416+
// TODO
417+
}
418+
}
419+
395420
this.files = {
396421
add: (path, options, callback) => {
397422
options.path = path

src/core/init.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ module.exports = (repo, opts, callback) => {
1414
var config = require('../init-files/default-config.json')
1515

1616
// Verify repo does not yet exist.
17-
repo.exists((err, res) => {
18-
if (err) { return callback(err) }
19-
if (res === true) {
17+
repo.exists((err, exists) => {
18+
if (err) {
19+
return callback(err)
20+
}
21+
22+
if (exists === true) {
2023
return callback(new Error('repo already exists'))
2124
}
2225

src/core/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
exports.ifRepoExists = (repo, cb) => {
4+
repo.exists((err, exists) => {
5+
if (err) {
6+
return cb(err)
7+
}
8+
9+
if (exists === false) {
10+
return cb(new Error('no ipfs repo found'))
11+
}
12+
13+
cb()
14+
})
15+
}

0 commit comments

Comments
 (0)