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

refactor(core): split IPFS into multiple files #194

Merged
merged 1 commit into from
May 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
434 changes: 35 additions & 399 deletions src/core/index.js

Large diffs are not rendered by default.

86 changes: 0 additions & 86 deletions src/core/init.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/core/ipfs/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

module.exports = function block (self) {
return {
get: (multihash, callback) => {
self._blockS.getBlock(multihash, callback)
},
put: (block, callback) => {
self._blockS.addBlock(block, callback)
},
del: (multihash, callback) => {
self._blockS.deleteBlock(multihash, callback)
},
stat: (multihash, callback) => {
self._blockS.getBlock(multihash, (err, block) => {
if (err) {
return callback(err)
}
callback(null, {
Key: multihash,
Size: block.data.length
})
})
}
}
}
39 changes: 39 additions & 0 deletions src/core/ipfs/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

module.exports = function bootstrap (self) {
return {
list: (callback) => {
self._repo.config.get((err, config) => {
if (err) {
return callback(err)
}
callback(null, config.Bootstrap)
})
},
add: (multiaddr, callback) => {
self._repo.config.get((err, config) => {
if (err) {
return callback(err)
}
config.Bootstrap.push(multiaddr)
self._repo.config.set(config, callback)
})
},
rm: (multiaddr, callback) => {
self._repo.config.get((err, config) => {
if (err) {
return callback(err)
}

config.Bootstrap = config.Bootstrap.filter((mh) => {
if (mh === multiaddr) {
return false
} else {
return true
}
})
self._repo.config.set(config, callback)
})
}
}
}
14 changes: 14 additions & 0 deletions src/core/ipfs/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

module.exports = function config (self) {
return {
// cli only feature built with show and replace
// edit: (callback) => {},
replace: (config, callback) => {
self._repo.config.set(config, callback)
},
show: (callback) => {
self._repo.config.get(callback)
}
}
}
13 changes: 13 additions & 0 deletions src/core/ipfs/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const importer = require('ipfs-data-importing').import

module.exports = function libp2p (self) {
return {
add: (path, options, callback) => {
options.path = path
options.dagService = self._dagS
importer(options, callback)
}
}
}
25 changes: 25 additions & 0 deletions src/core/ipfs/id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

module.exports = function id (self) {
return (opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
if (!self._peerInfo) { // because of split second warmup
setTimeout(ready, 100)
} else {
ready()
}

function ready () {
callback(null, {
ID: self._peerInfo.id.toB58String(),
PublicKey: self._peerInfo.id.pubKey.toString('base64'),
Addresses: self._peerInfo.multiaddrs.map((ma) => { return ma.toString() }),
AgentVersion: 'js-ipfs',
ProtocolVersion: '9000'
})
}
}
}
88 changes: 88 additions & 0 deletions src/core/ipfs/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'use strict'

const peerId = require('peer-id')
const BlockService = require('ipfs-block-service')
const DagService = require('ipfs-merkle-dag').DAGService
const path = require('path')

module.exports = function init (self) {
return (opts, callback) => {
opts = opts || {}
opts.emptyRepo = opts.emptyRepo || false
opts.bits = opts.bits || 2048

// Pre-set config values.
var config = require('../../init-files/default-config.json')

// Verify repo does not yet exist.
self._repo.exists((err, exists) => {
if (err) {
return callback(err)
}

if (exists === true) {
return callback(new Error('repo already exists'))
}

generateAndSetKeypair()
})

// Generate peer identity keypair + transform to desired format + add to config.
function generateAndSetKeypair () {
var keys = peerId.create({
bits: opts.bits
})
config.Identity = {
PeerID: keys.toB58String(),
PrivKey: keys.privKey.toString('base64')
}

writeVersion()
}

function writeVersion () {
const version = '3'

self._repo.version.set(version, (err) => {
if (err) { return callback(err) }

writeConfig()
})
}

// Write the config to the repo.
function writeConfig () {
self._repo.config.set(config, (err) => {
if (err) { return callback(err) }

addDefaultAssets()
})
}

// Add the default assets to the repo.
function addDefaultAssets () {
// Skip this step on the browser, or if emptyRepo was supplied.
const isNode = !global.window
if (!isNode || opts.emptyRepo) {
return doneImport(null)
}

const importer = require('ipfs-data-importing')
const blocks = new BlockService(self._repo)
const dag = new DagService(blocks)

const initDocsPath = path.join(__dirname, '../../init-files/init-docs')

importer.import(initDocsPath, dag, {
recursive: true
}, doneImport)

function doneImport (err, stat) {
if (err) { return callback(err) }

// All finished!
callback(null, true)
}
}
}
}
87 changes: 87 additions & 0 deletions src/core/ipfs/libp2p.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict'

const peerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const Libp2pNode = require('libp2p-ipfs').Node

module.exports = function libp2p (self) {
const OFFLINE_ERROR = new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.')

return {
start: (callback) => {
self._libp2pNode = new Libp2pNode(self._peerInfo)
self._libp2pNode.start(() => {
// TODO connect to bootstrap nodes, it will get us more addrs
self._peerInfo.multiaddrs.forEach((ma) => {
console.log('Swarm listening on', ma.toString())
})
callback()
})
},
stop: (callback) => {
self._libp2pNode.swarm.close(callback)
},
swarm: {
peers: (callback) => {
if (!self._libp2pNode) {
return callback(OFFLINE_ERROR)
}

callback(null, self._peerInfoBook.getAll())
},
// all the addrs we know
addrs: (callback) => {
if (!self._libp2pNode) {
return callback(OFFLINE_ERROR)
}
// TODO
throw new Error('Not implemented')
},
localAddrs: (callback) => {
if (!self._libp2pNode) {
return callback(OFFLINE_ERROR)
}

callback(null, self._peerInfo.multiaddrs)
},
connect: (ma, callback) => {
if (!self._libp2pNode) {
return callback(OFFLINE_ERROR)
}

const idStr = ma.toString().match(/\/ipfs\/(.*)/)
if (!idStr) {
return callback(new Error('invalid multiaddr'))
}
const id = peerId.createFromB58String(idStr[1])
const peer = new PeerInfo(id)

ma = ma.toString().replace(/\/ipfs\/(.*)/, '') // FIXME remove this when multiaddr supports ipfs

peer.multiaddr.add(multiaddr(ma))
self._peerInfoBook.put(peer)

self._libp2pNode.swarm.dial(peer, (err) => {
callback(err, id)
})
},
disconnect: (callback) => {
if (!self._libp2pNode) {
return callback(OFFLINE_ERROR)
}

throw new Error('Not implemented')
},
filters: () => {
// TODO
throw new Error('Not implemented')
}
},
routing: {},
records: {},
ping: () => {
throw new Error('Not implemented')
}
}
}
Loading