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

Commit db550a1

Browse files
committed
feat(core): migrate to awesome dag-pb
1 parent 12325a2 commit db550a1

File tree

4 files changed

+85
-88
lines changed

4 files changed

+85
-88
lines changed

src/core/components/files.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,13 @@ function prepareFile (self, file, callback) {
102102
const bs58mh = multihashes.toB58String(file.multihash)
103103
waterfall([
104104
(cb) => self.object.get(file.multihash, cb),
105-
(node, cb) => node.size((err, size) => {
106-
if (err) {
107-
return cb(err)
108-
}
105+
(node, cb) => {
109106
cb(null, {
110107
path: file.path || bs58mh,
111108
hash: bs58mh,
112-
size: size
109+
size: node.size
113110
})
114-
})
111+
}
115112
], callback)
116113
}
117114

src/core/components/object.js

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,25 @@ function parseBuffer (buf, encoding, callback) {
3434
}
3535

3636
function parseJSONBuffer (buf, callback) {
37-
let node
37+
let data
38+
let links
39+
3840
try {
3941
const parsed = JSON.parse(buf.toString())
40-
const links = (parsed.Links || []).map((link) => {
42+
43+
links = (parsed.Links || []).map((link) => {
4144
return new DAGLink(
42-
link.Name,
43-
link.Size,
44-
mh.fromB58String(link.Hash)
45+
link.Name || link.name,
46+
link.Size || link.size,
47+
mh.fromB58String(link.Hash || link.hash || link.multihash)
4548
)
4649
})
47-
node = new DAGNode(new Buffer(parsed.Data), links)
50+
data = new Buffer(parsed.Data)
4851
} catch (err) {
4952
return callback(new Error('failed to parse JSON: ' + err))
5053
}
51-
callback(null, node)
54+
55+
DAGNode.create(data, links, callback)
5256
}
5357

5458
function parseProtoBuffer (buf, callback) {
@@ -68,15 +72,15 @@ module.exports = function object (self) {
6872
self.object.get(multihash, options, cb)
6973
},
7074
(node, cb) => {
71-
node = edit(node)
72-
73-
node.multihash((err, multihash) => {
75+
// edit applies the edit func passed to
76+
// editAndSave
77+
edit(node, (err, node) => {
7478
if (err) {
7579
return cb(err)
7680
}
7781
self._ipldResolver.put({
7882
node: node,
79-
cid: new CID(multihash)
83+
cid: new CID(node.multihash)
8084
}, (err) => {
8185
cb(err, node)
8286
})
@@ -88,16 +92,14 @@ module.exports = function object (self) {
8892

8993
return {
9094
new: promisify((callback) => {
91-
const node = new DAGNode()
92-
93-
node.multihash((err, multihash) => {
95+
DAGNode.create(new Buffer(0), (err, node) => {
9496
if (err) {
9597
return callback(err)
9698
}
9799
self._ipldResolver.put({
98100
node: node,
99-
cid: new CID(multihash)
100-
}, function (err) {
101+
cid: new CID(node.multihash)
102+
}, (err) => {
101103
if (err) {
102104
return callback(err)
103105
}
@@ -126,34 +128,40 @@ module.exports = function object (self) {
126128
})
127129
return
128130
} else {
129-
node = new DAGNode(obj)
131+
DAGNode.create(obj, (err, _node) => {
132+
if (err) {
133+
return callback(err)
134+
}
135+
node = _node
136+
next()
137+
})
130138
}
131139
} else if (obj.multihash) {
132140
// already a dag node
133141
node = obj
142+
next()
134143
} else if (typeof obj === 'object') {
135-
node = new DAGNode(obj.Data, obj.Links)
144+
DAGNode.create(obj.Data, obj.Links, (err, _node) => {
145+
if (err) {
146+
return callback(err)
147+
}
148+
node = _node
149+
next()
150+
})
136151
} else {
137152
return callback(new Error('obj not recognized'))
138153
}
139154

140-
next()
141-
142155
function next () {
143-
node.multihash((err, multihash) => {
156+
self._ipldResolver.put({
157+
node: node,
158+
cid: new CID(node.multihash)
159+
}, (err) => {
144160
if (err) {
145161
return callback(err)
146162
}
147-
self._ipldResolver.put({
148-
node: node,
149-
cid: new CID(multihash)
150-
}, (err, block) => {
151-
if (err) {
152-
return callback(err)
153-
}
154163

155-
self.object.get(multihash, callback)
156-
})
164+
self.object.get(node.multihash, callback)
157165
})
158166
}
159167
}),
@@ -223,64 +231,47 @@ module.exports = function object (self) {
223231
const blockSize = serialized.length
224232
const linkLength = node.links.reduce((a, l) => a + l.size, 0)
225233

226-
node.toJSON((err, nodeJSON) => {
227-
if (err) {
228-
return callback(err)
229-
}
234+
const nodeJSON = node.toJSON()
230235

231-
callback(null, {
232-
Hash: nodeJSON.Hash,
233-
NumLinks: node.links.length,
234-
BlockSize: blockSize,
235-
LinksSize: blockSize - node.data.length,
236-
DataSize: node.data.length,
237-
CumulativeSize: blockSize + linkLength
238-
})
236+
callback(null, {
237+
Hash: nodeJSON.multihash,
238+
NumLinks: node.links.length,
239+
BlockSize: blockSize,
240+
LinksSize: blockSize - node.data.length,
241+
DataSize: node.data.length,
242+
CumulativeSize: blockSize + linkLength
239243
})
240244
})
241245
})
242246
}),
243247

244248
patch: promisify({
245249
addLink (multihash, link, options, callback) {
246-
editAndSave((node) => {
247-
node.addRawLink(link)
248-
return node
250+
editAndSave((node, cb) => {
251+
DAGNode.addLink(node, link, cb)
249252
})(multihash, options, callback)
250253
},
251254

252255
rmLink (multihash, linkRef, options, callback) {
253-
editAndSave((node) => {
254-
node.links = node.links.filter((link) => {
255-
if (typeof linkRef === 'string') {
256-
return link.name !== linkRef
257-
}
258-
259-
if (Buffer.isBuffer(linkRef)) {
260-
return !link.hash.equals(linkRef)
261-
}
262-
263-
if (linkRef.name) {
264-
return link.name !== linkRef.name
265-
}
266-
267-
return !link.hash.equals(linkRef.hash)
268-
})
269-
return node
256+
editAndSave((node, cb) => {
257+
if (linkRef.constructor &&
258+
linkRef.constructor.name === 'DAGLink') {
259+
linkRef = linkRef._name
260+
}
261+
DAGNode.rmLink(node, linkRef, cb)
270262
})(multihash, options, callback)
271263
},
272264

273265
appendData (multihash, data, options, callback) {
274-
editAndSave((node) => {
275-
node.data = Buffer.concat([node.data, data])
276-
return node
266+
editAndSave((node, cb) => {
267+
const newData = Buffer.concat([node.data, data])
268+
DAGNode.create(newData, node.links, cb)
277269
})(multihash, options, callback)
278270
},
279271

280272
setData (multihash, data, options, callback) {
281-
editAndSave((node) => {
282-
node.data = data
283-
return node
273+
editAndSave((node, cb) => {
274+
DAGNode.create(data, node.links, cb)
284275
})(multihash, options, callback)
285276
}
286277
})

test/core/both/test-bitswap.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ describe('bitswap', () => {
9898

9999
function addNode (num, done) {
100100
num = leftPad(num, 3, 0)
101+
101102
const apiUrl = `/ip4/127.0.0.1/tcp/31${num}`
102103
const remoteNode = new API(apiUrl)
103104

104105
connectNodes(remoteNode, inProcNode, (err) => {
106+
console.log('connected')
105107
done(err, remoteNode)
106108
})
107109
}
@@ -205,17 +207,24 @@ describe('bitswap', () => {
205207

206208
it('2 peers', (done) => {
207209
const file = new Buffer(`I love IPFS <3 ${Math.random()}`)
210+
console.log('1')
208211

209212
waterfall([
210213
// 0. Start node
211214
(cb) => addNode(12, cb),
212215
// 1. Add file to tmp instance
213-
(remote, cb) => remote.add([{
214-
path: 'awesome.txt',
215-
content: file
216-
}], cb),
216+
(remote, cb) => {
217+
console.log('2')
218+
remote.files.add([{
219+
path: 'awesome.txt',
220+
content: file
221+
}], cb)
222+
},
217223
// 2. Request file from local instance
218-
(val, cb) => inProcNode.files.cat(val[0].hash, cb),
224+
(val, cb) => {
225+
console.log('3')
226+
inProcNode.files.cat(val[0].hash, cb)
227+
},
219228
(res, cb) => res.pipe(bl(cb))
220229
], (err, res) => {
221230
expect(err).to.not.exist
@@ -237,13 +246,8 @@ describe('bitswap', () => {
237246
it('returns an array of wanted blocks', (done) => {
238247
inProcNode.goOnline((err) => {
239248
expect(err).to.not.exist
240-
241-
expect(
242-
inProcNode.bitswap.wantlist()
243-
).to.be.eql(
244-
[]
245-
)
246-
249+
expect(inProcNode.bitswap.wantlist())
250+
.to.be.eql([])
247251
inProcNode.goOffline(done)
248252
})
249253
})

test/utils/temp-node.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,16 @@ function createTempNode (num, callback) {
3131
num = leftPad(num, 3, 0)
3232

3333
series([
34-
(cb) => ipfs.init({ emptyRepo: true, bits: 1024 }, cb),
34+
(cb) => ipfs.init({
35+
emptyRepo: true,
36+
bits: 1024
37+
}, cb),
3538
(cb) => setAddresses(repo, num, cb),
3639
(cb) => ipfs.load(cb)
3740
], (err) => {
38-
if (err) return callback(err)
41+
if (err) {
42+
return callback(err)
43+
}
3944
callback(null, ipfs)
4045
})
4146
}

0 commit comments

Comments
 (0)