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

Commit 2eeea35

Browse files
committed
feat: block API uses CIDs
1 parent 198a64a commit 2eeea35

File tree

7 files changed

+38
-30
lines changed

7 files changed

+38
-30
lines changed

src/core/components/block.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,53 @@
22

33
const Block = require('ipfs-block')
44
const multihash = require('multihashes')
5+
const CID = require('cids')
56

67
module.exports = function block (self) {
78
return {
8-
get: (hash, callback) => {
9-
hash = cleanHash(hash)
10-
self._blockS.get(hash, callback)
9+
get: (cid, callback) => {
10+
cid = cleanCid(cid)
11+
self._blockService.get(cid, callback)
1112
},
12-
put: (block, callback) => {
13+
put: (block, cid, callback) => {
1314
if (Array.isArray(block)) {
1415
return callback(new Error('Array is not supported'))
1516
}
17+
1618
if (Buffer.isBuffer(block)) {
1719
block = new Block(block)
1820
}
1921

20-
self._blockS.put(block, (err) => {
22+
self._blockService.put({
23+
block: block,
24+
cid: cid
25+
}, (err) => {
2126
callback(err, block)
2227
})
2328
},
24-
del: (hash, callback) => {
25-
hash = cleanHash(hash)
26-
self._blockS.delete(hash, callback)
29+
rm: (cid, callback) => {
30+
cid = cleanCid(cid)
31+
self._blockService.delete(cid, callback)
2732
},
28-
stat: (hash, callback) => {
29-
hash = cleanHash(hash)
33+
stat: (cid, callback) => {
34+
cid = cleanCid(cid)
3035

31-
self._blockS.get(hash, (err, block) => {
36+
self._blockService.get(cid, (err, block) => {
3237
if (err) {
3338
return callback(err)
3439
}
3540
callback(null, {
36-
key: multihash.toB58String(hash),
41+
key: multihash.toB58String(cid.multihash),
3742
size: block.data.length
3843
})
3944
})
4045
}
4146
}
4247
}
4348

44-
function cleanHash (hash) {
45-
if (typeof hash === 'string') {
46-
return multihash.fromB58String(hash)
49+
function cleanCid (cid) {
50+
if (typeof cid === 'string') {
51+
return new CID(cid)
4752
}
48-
return hash
53+
return cid
4954
}

src/core/components/files.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = function files (self) {
1717
return pull(
1818
pull.map(normalizeContent),
1919
pull.flatten(),
20-
importer(self._dagS),
20+
importer(self._dagService),
2121
pull.asyncMap(prepareFile.bind(null, self))
2222
)
2323
}
@@ -36,7 +36,7 @@ module.exports = function files (self) {
3636

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

55-
self._dagS.get(hash, (err, node) => {
55+
self._dagService.get(hash, (err, node) => {
5656
if (err) {
5757
return callback(err)
5858
}
@@ -65,7 +65,7 @@ module.exports = function files (self) {
6565
}
6666

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

7777
get: promisify((hash, callback) => {
7878
callback(null, toStream.source(pull(
79-
exporter(hash, self._dagS),
79+
exporter(hash, self._dagService),
8080
pull.map((file) => {
8181
if (file.content) {
8282
file.content = toStream.source(file.content)
@@ -89,7 +89,7 @@ module.exports = function files (self) {
8989
}),
9090

9191
getPull: promisify((hash, callback) => {
92-
callback(null, exporter(hash, self._dagS))
92+
callback(null, exporter(hash, self._dagService))
9393
})
9494
}
9595
}

src/core/components/go-offline.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module.exports = function goOffline (self) {
44
return (cb) => {
5-
self._blockS.goOffline()
5+
self._blockService.goOffline()
66
self._bitswap.stop()
77
self.libp2p.stop(cb)
88
}

src/core/components/go-online.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = function goOnline (self) {
2020
self._libp2pNode.peerBook
2121
)
2222
self._bitswap.start()
23-
self._blockS.goOnline(self._bitswap)
23+
self._blockService.goOnline(self._bitswap)
2424
cb()
2525
})
2626
}

src/core/components/object.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module.exports = function object (self) {
6565
waterfall([
6666
(cb) => self.object.get(multihash, options, cb),
6767
(node, cb) => {
68-
self._dagS.put(edit(node), (err) => {
68+
self._dagService.put(edit(node), (err) => {
6969
cb(err, node)
7070
})
7171
}
@@ -77,7 +77,7 @@ module.exports = function object (self) {
7777
new: promisify((cb) => {
7878
const node = new DAGNode()
7979

80-
self._dagS.put(node, function (err) {
80+
self._dagService.put(node, function (err) {
8181
if (err) {
8282
return cb(err)
8383
}
@@ -114,7 +114,7 @@ module.exports = function object (self) {
114114
return cb(new Error('obj not recognized'))
115115
}
116116

117-
self._dagS.put(node, (err, block) => {
117+
self._dagService.put(node, (err, block) => {
118118
if (err) {
119119
return cb(err)
120120
}
@@ -137,7 +137,7 @@ module.exports = function object (self) {
137137
return cb(err)
138138
}
139139

140-
self._dagS.get(mh, cb)
140+
self._dagService.get(mh, cb)
141141
}),
142142

143143
data: promisify((multihash, options, cb) => {

src/core/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ function IPFS (repoInstance) {
4343
this._peerInfo = null
4444
this._libp2pNode = null
4545
this._bitswap = null
46-
this._blockS = new BlockService(this._repo)
47-
this._dagS = new DAGService(this._blockS)
46+
this._blockService = new BlockService(this._repo)
47+
this._dagService = new DAGService(this._blockService)
4848

4949
// IPFS Core exposed components
50+
5051
// for booting up a node
5152
this.goOnline = goOnline(this)
5253
this.goOffline = goOffline(this)
5354
this.isOnline = isOnline(this)
5455
this.load = load(this)
5556
this.init = init(this)
57+
5658
// interface-ipfs-core defined API
5759
this.version = version(this)
5860
this.id = id(this)

test/core/both/test-block.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ const common = {
1717
}
1818
}
1919

20+
console.log('->')
2021
test.block(common)

0 commit comments

Comments
 (0)