This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from ipfs/refactor/core
refactor(core): split IPFS into multiple files
- Loading branch information
Showing
13 changed files
with
553 additions
and
485 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} | ||
} |
Oops, something went wrong.