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

Commit

Permalink
feat: block API uses CIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 29, 2016
1 parent 198a64a commit 2eeea35
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
37 changes: 21 additions & 16 deletions src/core/components/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,53 @@

const Block = require('ipfs-block')
const multihash = require('multihashes')
const CID = require('cids')

module.exports = function block (self) {
return {
get: (hash, callback) => {
hash = cleanHash(hash)
self._blockS.get(hash, callback)
get: (cid, callback) => {
cid = cleanCid(cid)
self._blockService.get(cid, callback)
},
put: (block, callback) => {
put: (block, cid, callback) => {
if (Array.isArray(block)) {
return callback(new Error('Array is not supported'))
}

if (Buffer.isBuffer(block)) {
block = new Block(block)
}

self._blockS.put(block, (err) => {
self._blockService.put({
block: block,
cid: cid
}, (err) => {
callback(err, block)
})
},
del: (hash, callback) => {
hash = cleanHash(hash)
self._blockS.delete(hash, callback)
rm: (cid, callback) => {
cid = cleanCid(cid)
self._blockService.delete(cid, callback)
},
stat: (hash, callback) => {
hash = cleanHash(hash)
stat: (cid, callback) => {
cid = cleanCid(cid)

self._blockS.get(hash, (err, block) => {
self._blockService.get(cid, (err, block) => {
if (err) {
return callback(err)
}
callback(null, {
key: multihash.toB58String(hash),
key: multihash.toB58String(cid.multihash),
size: block.data.length
})
})
}
}
}

function cleanHash (hash) {
if (typeof hash === 'string') {
return multihash.fromB58String(hash)
function cleanCid (cid) {
if (typeof cid === 'string') {
return new CID(cid)
}
return hash
return cid
}
12 changes: 6 additions & 6 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function files (self) {
return pull(
pull.map(normalizeContent),
pull.flatten(),
importer(self._dagS),
importer(self._dagService),
pull.asyncMap(prepareFile.bind(null, self))
)
}
Expand All @@ -36,7 +36,7 @@ module.exports = function files (self) {

pull(
pull.values(normalizeContent(data)),
importer(self._dagS),
importer(self._dagService),
pull.asyncMap(prepareFile.bind(null, self)),
sort((a, b) => {
if (a.path < b.path) return 1
Expand All @@ -52,7 +52,7 @@ module.exports = function files (self) {
return callback(new Error('You must supply a multihash'))
}

self._dagS.get(hash, (err, node) => {
self._dagService.get(hash, (err, node) => {
if (err) {
return callback(err)
}
Expand All @@ -65,7 +65,7 @@ module.exports = function files (self) {
}

pull(
exporter(hash, self._dagS),
exporter(hash, self._dagService),
pull.collect((err, files) => {
if (err) return callback(err)
callback(null, toStream.source(files[0].content))
Expand All @@ -76,7 +76,7 @@ module.exports = function files (self) {

get: promisify((hash, callback) => {
callback(null, toStream.source(pull(
exporter(hash, self._dagS),
exporter(hash, self._dagService),
pull.map((file) => {
if (file.content) {
file.content = toStream.source(file.content)
Expand All @@ -89,7 +89,7 @@ module.exports = function files (self) {
}),

getPull: promisify((hash, callback) => {
callback(null, exporter(hash, self._dagS))
callback(null, exporter(hash, self._dagService))
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/go-offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = function goOffline (self) {
return (cb) => {
self._blockS.goOffline()
self._blockService.goOffline()
self._bitswap.stop()
self.libp2p.stop(cb)
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/go-online.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function goOnline (self) {
self._libp2pNode.peerBook
)
self._bitswap.start()
self._blockS.goOnline(self._bitswap)
self._blockService.goOnline(self._bitswap)
cb()
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/core/components/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = function object (self) {
waterfall([
(cb) => self.object.get(multihash, options, cb),
(node, cb) => {
self._dagS.put(edit(node), (err) => {
self._dagService.put(edit(node), (err) => {
cb(err, node)
})
}
Expand All @@ -77,7 +77,7 @@ module.exports = function object (self) {
new: promisify((cb) => {
const node = new DAGNode()

self._dagS.put(node, function (err) {
self._dagService.put(node, function (err) {
if (err) {
return cb(err)
}
Expand Down Expand Up @@ -114,7 +114,7 @@ module.exports = function object (self) {
return cb(new Error('obj not recognized'))
}

self._dagS.put(node, (err, block) => {
self._dagService.put(node, (err, block) => {
if (err) {
return cb(err)
}
Expand All @@ -137,7 +137,7 @@ module.exports = function object (self) {
return cb(err)
}

self._dagS.get(mh, cb)
self._dagService.get(mh, cb)
}),

data: promisify((multihash, options, cb) => {
Expand Down
6 changes: 4 additions & 2 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ function IPFS (repoInstance) {
this._peerInfo = null
this._libp2pNode = null
this._bitswap = null
this._blockS = new BlockService(this._repo)
this._dagS = new DAGService(this._blockS)
this._blockService = new BlockService(this._repo)
this._dagService = new DAGService(this._blockService)

// IPFS Core exposed components

// for booting up a node
this.goOnline = goOnline(this)
this.goOffline = goOffline(this)
this.isOnline = isOnline(this)
this.load = load(this)
this.init = init(this)

// interface-ipfs-core defined API
this.version = version(this)
this.id = id(this)
Expand Down
1 change: 1 addition & 0 deletions test/core/both/test-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ const common = {
}
}

console.log('->')
test.block(common)

0 comments on commit 2eeea35

Please sign in to comment.