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

Commit aa9fd4d

Browse files
Merge pull request #194 from ipfs/refactor/core
refactor(core): split IPFS into multiple files
2 parents 506cbf1 + 7f25c2d commit aa9fd4d

File tree

13 files changed

+553
-485
lines changed

13 files changed

+553
-485
lines changed

src/core/index.js

Lines changed: 35 additions & 399 deletions
Large diffs are not rendered by default.

src/core/init.js

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/core/ipfs/block.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
module.exports = function block (self) {
4+
return {
5+
get: (multihash, callback) => {
6+
self._blockS.getBlock(multihash, callback)
7+
},
8+
put: (block, callback) => {
9+
self._blockS.addBlock(block, callback)
10+
},
11+
del: (multihash, callback) => {
12+
self._blockS.deleteBlock(multihash, callback)
13+
},
14+
stat: (multihash, callback) => {
15+
self._blockS.getBlock(multihash, (err, block) => {
16+
if (err) {
17+
return callback(err)
18+
}
19+
callback(null, {
20+
Key: multihash,
21+
Size: block.data.length
22+
})
23+
})
24+
}
25+
}
26+
}

src/core/ipfs/bootstrap.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict'
2+
3+
module.exports = function bootstrap (self) {
4+
return {
5+
list: (callback) => {
6+
self._repo.config.get((err, config) => {
7+
if (err) {
8+
return callback(err)
9+
}
10+
callback(null, config.Bootstrap)
11+
})
12+
},
13+
add: (multiaddr, callback) => {
14+
self._repo.config.get((err, config) => {
15+
if (err) {
16+
return callback(err)
17+
}
18+
config.Bootstrap.push(multiaddr)
19+
self._repo.config.set(config, callback)
20+
})
21+
},
22+
rm: (multiaddr, callback) => {
23+
self._repo.config.get((err, config) => {
24+
if (err) {
25+
return callback(err)
26+
}
27+
28+
config.Bootstrap = config.Bootstrap.filter((mh) => {
29+
if (mh === multiaddr) {
30+
return false
31+
} else {
32+
return true
33+
}
34+
})
35+
self._repo.config.set(config, callback)
36+
})
37+
}
38+
}
39+
}

src/core/ipfs/config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
module.exports = function config (self) {
4+
return {
5+
// cli only feature built with show and replace
6+
// edit: (callback) => {},
7+
replace: (config, callback) => {
8+
self._repo.config.set(config, callback)
9+
},
10+
show: (callback) => {
11+
self._repo.config.get(callback)
12+
}
13+
}
14+
}

src/core/ipfs/files.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
const importer = require('ipfs-data-importing').import
4+
5+
module.exports = function libp2p (self) {
6+
return {
7+
add: (path, options, callback) => {
8+
options.path = path
9+
options.dagService = self._dagS
10+
importer(options, callback)
11+
}
12+
}
13+
}

src/core/ipfs/id.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
module.exports = function id (self) {
4+
return (opts, callback) => {
5+
if (typeof opts === 'function') {
6+
callback = opts
7+
opts = {}
8+
}
9+
if (!self._peerInfo) { // because of split second warmup
10+
setTimeout(ready, 100)
11+
} else {
12+
ready()
13+
}
14+
15+
function ready () {
16+
callback(null, {
17+
ID: self._peerInfo.id.toB58String(),
18+
PublicKey: self._peerInfo.id.pubKey.toString('base64'),
19+
Addresses: self._peerInfo.multiaddrs.map((ma) => { return ma.toString() }),
20+
AgentVersion: 'js-ipfs',
21+
ProtocolVersion: '9000'
22+
})
23+
}
24+
}
25+
}

src/core/ipfs/init.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict'
2+
3+
const peerId = require('peer-id')
4+
const BlockService = require('ipfs-block-service')
5+
const DagService = require('ipfs-merkle-dag').DAGService
6+
const path = require('path')
7+
8+
module.exports = function init (self) {
9+
return (opts, callback) => {
10+
opts = opts || {}
11+
opts.emptyRepo = opts.emptyRepo || false
12+
opts.bits = opts.bits || 2048
13+
14+
// Pre-set config values.
15+
var config = require('../../init-files/default-config.json')
16+
17+
// Verify repo does not yet exist.
18+
self._repo.exists((err, exists) => {
19+
if (err) {
20+
return callback(err)
21+
}
22+
23+
if (exists === true) {
24+
return callback(new Error('repo already exists'))
25+
}
26+
27+
generateAndSetKeypair()
28+
})
29+
30+
// Generate peer identity keypair + transform to desired format + add to config.
31+
function generateAndSetKeypair () {
32+
var keys = peerId.create({
33+
bits: opts.bits
34+
})
35+
config.Identity = {
36+
PeerID: keys.toB58String(),
37+
PrivKey: keys.privKey.toString('base64')
38+
}
39+
40+
writeVersion()
41+
}
42+
43+
function writeVersion () {
44+
const version = '3'
45+
46+
self._repo.version.set(version, (err) => {
47+
if (err) { return callback(err) }
48+
49+
writeConfig()
50+
})
51+
}
52+
53+
// Write the config to the repo.
54+
function writeConfig () {
55+
self._repo.config.set(config, (err) => {
56+
if (err) { return callback(err) }
57+
58+
addDefaultAssets()
59+
})
60+
}
61+
62+
// Add the default assets to the repo.
63+
function addDefaultAssets () {
64+
// Skip this step on the browser, or if emptyRepo was supplied.
65+
const isNode = !global.window
66+
if (!isNode || opts.emptyRepo) {
67+
return doneImport(null)
68+
}
69+
70+
const importer = require('ipfs-data-importing')
71+
const blocks = new BlockService(self._repo)
72+
const dag = new DagService(blocks)
73+
74+
const initDocsPath = path.join(__dirname, '../../init-files/init-docs')
75+
76+
importer.import(initDocsPath, dag, {
77+
recursive: true
78+
}, doneImport)
79+
80+
function doneImport (err, stat) {
81+
if (err) { return callback(err) }
82+
83+
// All finished!
84+
callback(null, true)
85+
}
86+
}
87+
}
88+
}

src/core/ipfs/libp2p.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict'
2+
3+
const peerId = require('peer-id')
4+
const PeerInfo = require('peer-info')
5+
const multiaddr = require('multiaddr')
6+
const Libp2pNode = require('libp2p-ipfs').Node
7+
8+
module.exports = function libp2p (self) {
9+
const OFFLINE_ERROR = new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')
10+
11+
return {
12+
start: (callback) => {
13+
self._libp2pNode = new Libp2pNode(self._peerInfo)
14+
self._libp2pNode.start(() => {
15+
// TODO connect to bootstrap nodes, it will get us more addrs
16+
self._peerInfo.multiaddrs.forEach((ma) => {
17+
console.log('Swarm listening on', ma.toString())
18+
})
19+
callback()
20+
})
21+
},
22+
stop: (callback) => {
23+
self._libp2pNode.swarm.close(callback)
24+
},
25+
swarm: {
26+
peers: (callback) => {
27+
if (!self._libp2pNode) {
28+
return callback(OFFLINE_ERROR)
29+
}
30+
31+
callback(null, self._peerInfoBook.getAll())
32+
},
33+
// all the addrs we know
34+
addrs: (callback) => {
35+
if (!self._libp2pNode) {
36+
return callback(OFFLINE_ERROR)
37+
}
38+
// TODO
39+
throw new Error('Not implemented')
40+
},
41+
localAddrs: (callback) => {
42+
if (!self._libp2pNode) {
43+
return callback(OFFLINE_ERROR)
44+
}
45+
46+
callback(null, self._peerInfo.multiaddrs)
47+
},
48+
connect: (ma, callback) => {
49+
if (!self._libp2pNode) {
50+
return callback(OFFLINE_ERROR)
51+
}
52+
53+
const idStr = ma.toString().match(/\/ipfs\/(.*)/)
54+
if (!idStr) {
55+
return callback(new Error('invalid multiaddr'))
56+
}
57+
const id = peerId.createFromB58String(idStr[1])
58+
const peer = new PeerInfo(id)
59+
60+
ma = ma.toString().replace(/\/ipfs\/(.*)/, '') // FIXME remove this when multiaddr supports ipfs
61+
62+
peer.multiaddr.add(multiaddr(ma))
63+
self._peerInfoBook.put(peer)
64+
65+
self._libp2pNode.swarm.dial(peer, (err) => {
66+
callback(err, id)
67+
})
68+
},
69+
disconnect: (callback) => {
70+
if (!self._libp2pNode) {
71+
return callback(OFFLINE_ERROR)
72+
}
73+
74+
throw new Error('Not implemented')
75+
},
76+
filters: () => {
77+
// TODO
78+
throw new Error('Not implemented')
79+
}
80+
},
81+
routing: {},
82+
records: {},
83+
ping: () => {
84+
throw new Error('Not implemented')
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)