Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 1dd4dd2

Browse files
dignifiedquiredaviddias
authored andcommitted
feat: upgrade to new ipfs-block and blockservice
1 parent eb9b9f1 commit 1dd4dd2

11 files changed

+177
-156
lines changed

package.json

+6-8
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,26 @@
4545
},
4646
"dependencies": {
4747
"async": "^2.1.5",
48+
"bs58": "^4.0.0",
4849
"buffer-loader": "0.0.1",
4950
"cids": "~0.4.2",
50-
"ipfs-block": "~0.5.5",
51+
"ipfs-block": "~0.6.0",
5152
"is-ipfs": "~0.3.0",
5253
"multihashes": "~0.4.4",
5354
"multihashing-async": "~0.4.4",
5455
"protocol-buffers": "^3.2.1",
5556
"pull-stream": "^3.5.0",
5657
"pull-traverse": "^1.0.3",
57-
"stable": "^0.1.6",
58-
"bs58": "^4.0.0"
58+
"stable": "^0.1.6"
5959
},
6060
"devDependencies": {
6161
"aegir": "^11.0.0",
62-
"buffer-loader": "0.0.1",
6362
"chai": "^3.5.0",
6463
"chai-checkmark": "^1.0.1",
64+
"detect-node": "^2.0.3",
6565
"dirty-chai": "^1.2.2",
66-
"fs-pull-blob-store": "~0.4.1",
67-
"idb-pull-blob-store": "~0.5.1",
68-
"ipfs-block-service": "~0.8.3",
69-
"ipfs-repo": "~0.11.3",
66+
"ipfs-block-service": "~0.9.0",
67+
"ipfs-repo": "~0.12.0",
7068
"lodash": "^4.17.4",
7169
"ncp": "^2.0.0",
7270
"pre-commit": "^1.2.2",

src/resolver.js

+50-51
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const waterfall = require('async/waterfall')
4+
35
const util = require('./util')
46

57
exports = module.exports
@@ -10,65 +12,62 @@ exports.multicodec = 'dag-pb'
1012
* throw if not possible. `block` is an IPFS Block instance (contains data+key)
1113
*/
1214
exports.resolve = (block, path, callback) => {
13-
util.deserialize(block.data, gotNode)
14-
15-
function gotNode (err, node) {
16-
if (err) {
17-
return callback(err)
18-
}
19-
20-
const split = path.split('/')
21-
22-
if (split[0] === 'Links') {
23-
let remainderPath = ''
15+
waterfall([
16+
(cb) => util.deserialize(block.data, cb),
17+
(node, cb) => {
18+
const split = path.split('/')
19+
20+
if (split[0] === 'Links') {
21+
let remainderPath = ''
22+
23+
// all links
24+
if (!split[1]) {
25+
return cb(null, {
26+
value: node.links.map((l) => l.toJSON()),
27+
remainderPath: ''
28+
})
29+
}
2430

25-
// all links
26-
if (!split[1]) {
27-
return callback(null, {
28-
value: node.links.map((l) => l.toJSON()),
29-
remainderPath: ''
31+
// select one link
32+
33+
const values = {}
34+
35+
// populate both index number and name to enable both cases
36+
// for the resolver
37+
node.links.forEach((l, i) => {
38+
const link = l.toJSON()
39+
values[i] = {
40+
hash: link.multihash,
41+
name: link.name,
42+
size: link.size
43+
}
44+
// TODO by enabling something to resolve through link name, we are
45+
// applying a transformation (a view) to the data, confirm if this
46+
// is exactly what we want
47+
values[link.name] = link.multihash
3048
})
31-
}
3249

33-
// select one link
50+
let value = values[split[1]]
3451

35-
const values = {}
36-
37-
// populate both index number and name to enable both cases
38-
// for the resolver
39-
node.links.forEach((l, i) => {
40-
const link = l.toJSON()
41-
values[i] = {
42-
hash: link.multihash,
43-
name: link.name,
44-
size: link.size
52+
// if remainderPath exists, value needs to be CID
53+
if (split[2] === 'Hash') {
54+
value = { '/': value.hash }
55+
} else if (split[2] === 'Tsize') {
56+
value = { '/': value.size }
57+
} else if (split[2] === 'Name') {
58+
value = { '/': value.name }
4559
}
46-
// TODO by enabling something to resolve through link name, we are
47-
// applying a transformation (a view) to the data, confirm if this
48-
// is exactly what we want
49-
values[link.name] = link.multihash
50-
})
51-
52-
let value = values[split[1]]
53-
54-
// if remainderPath exists, value needs to be CID
55-
if (split[2] === 'Hash') {
56-
value = { '/': value.hash }
57-
} else if (split[2] === 'Tsize') {
58-
value = { '/': value.size }
59-
} else if (split[2] === 'Name') {
60-
value = { '/': value.name }
61-
}
6260

63-
remainderPath = split.slice(3).join('/')
61+
remainderPath = split.slice(3).join('/')
6462

65-
callback(null, { value: value, remainderPath: remainderPath })
66-
} else if (split[0] === 'Data') {
67-
callback(null, { value: node.data, remainderPath: '' })
68-
} else {
69-
callback(new Error('path not available'))
63+
cb(null, { value: value, remainderPath: remainderPath })
64+
} else if (split[0] === 'Data') {
65+
cb(null, { value: node.data, remainderPath: '' })
66+
} else {
67+
cb(new Error('path not available'))
68+
}
7069
}
71-
}
70+
], callback)
7271
}
7372

7473
/*

test/browser.js

+20-36
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,42 @@
11
/* eslint-env mocha */
22
/* global self */
3+
34
'use strict'
45

56
const series = require('async/series')
6-
const Store = require('idb-pull-blob-store')
7-
const _ = require('lodash')
87
const IPFSRepo = require('ipfs-repo')
9-
const repoContext = require.context('buffer!./test-repo', true)
10-
const pull = require('pull-stream')
8+
9+
const basePath = 'ipfs' + Math.random()
1110

1211
const idb = self.indexedDB ||
1312
self.mozIndexedDB ||
1413
self.webkitIndexedDB ||
1514
self.msIndexedDB
1615

17-
idb.deleteDatabase('ipfs')
18-
idb.deleteDatabase('ipfs/blocks')
16+
idb.deleteDatabase(basePath)
17+
idb.deleteDatabase(basePath + '/blocks')
1918

20-
describe('Browser tests', () => {
21-
const path = 'ipfs' + Math.random()
19+
describe('Browser', () => {
20+
const repo = new IPFSRepo(basePath)
2221

2322
before((done) => {
24-
const repoData = []
25-
26-
repoContext.keys().forEach((key) => {
27-
repoData.push({
28-
key: key.replace('./', ''),
29-
value: repoContext(key)
30-
})
31-
})
32-
33-
const mainBlob = new Store(path)
34-
const blocksBlob = new Store(path + '/blocks')
23+
series([
24+
(cb) => repo.init({}, cb),
25+
(cb) => repo.open(cb)
26+
], done)
27+
})
3528

36-
series(repoData.map((file) => (cb) => {
37-
if (_.startsWith(file.key, 'datastore/')) {
38-
return cb()
29+
after((done) => {
30+
series([
31+
(cb) => repo.close(cb),
32+
(cb) => {
33+
idb.deleteDatabase(basePath)
34+
idb.deleteDatabase(basePath + '/blocks')
35+
cb()
3936
}
40-
41-
const blocks = _.startsWith(file.key, 'blocks/')
42-
const blob = blocks ? blocksBlob : mainBlob
43-
const key = blocks ? file.key.replace(/^blocks\//, '') : file.key
44-
45-
pull(
46-
pull(
47-
pull.values([file.value]),
48-
blob.write(key, cb)
49-
)
50-
)
51-
}), done)
37+
], done)
5238
})
5339

54-
const repo = new IPFSRepo(path, {stores: Store})
55-
5640
require('./dag-node-test')(repo)
5741
require('./dag-link-test')(repo)
5842
})

test/dag-node-test.js

+17-34
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const DAGNode = dagPB.DAGNode
1515
const toDAGLink = require('../src/dag-node/util').toDAGLink
1616
const util = dagPB.util
1717
const series = require('async/series')
18+
const waterfall = require('async/waterfall')
19+
const isNode = require('detect-node')
1820

1921
const BlockService = require('ipfs-block-service')
2022
const Block = require('ipfs-block')
@@ -26,6 +28,8 @@ const testBlockNamedLinks = loadFixture(__dirname, '/fixtures/test-block-named-l
2628
const testBlockUnnamedLinks = loadFixture(__dirname, '/fixtures/test-block-unnamed-links')
2729

2830
module.exports = (repo) => {
31+
const bs = new BlockService(repo)
32+
2933
describe('DAGNode', () => {
3034
it('create a node', (done) => {
3135
expect(7).checks(done)
@@ -131,6 +135,7 @@ module.exports = (repo) => {
131135
})
132136

133137
it('create an empty node', (done) => {
138+
// this node is not in the repo as we don't copy node data to the browser
134139
expect(7).checks(done)
135140
const fromGoIPFS = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
136141

@@ -406,51 +411,29 @@ module.exports = (repo) => {
406411
})
407412

408413
it('marshal a node and store it with block-service', (done) => {
409-
const bs = new BlockService(repo)
410-
411414
DAGNode.create(new Buffer('some data'), (err, node) => {
412415
expect(err).to.not.exist()
413-
let cid
414416
let block
415417

416-
series([
417-
(cb) => {
418-
dagPB.util.serialize(node, (err, serialized) => {
419-
expect(err).to.not.exist()
420-
block = new Block(serialized)
421-
cb()
422-
})
423-
},
424-
(cb) => {
425-
util.cid(node, (err, _cid) => {
426-
expect(err).to.not.exist()
427-
cid = _cid
428-
cb()
429-
})
418+
waterfall([
419+
(cb) => dagPB.util.serialize(node, cb),
420+
(s, cb) => {
421+
block = new Block(s, new CID(node.multihash))
422+
bs.put(block, cb)
430423
},
431-
(cb) => {
432-
bs.put({
433-
block: block,
434-
cid: cid
435-
}, cb)
436-
},
437-
(cb) => {
438-
bs.get(cid, (err, retrievedBlock) => {
439-
expect(err).to.not.exist()
440-
expect(retrievedBlock.data).to.eql(block.data)
441-
retrievedBlock.key((err, key) => {
442-
expect(err).to.not.exist()
443-
expect(key).to.eql(cid.multihash)
444-
cb()
445-
})
446-
})
424+
(cb) => bs.get(block.cid, cb),
425+
(retrievedBlock, cb) => {
426+
expect(retrievedBlock).to.eql(block)
427+
cb()
447428
}
448429
], done)
449430
})
450431
})
451432

452433
it('deserialize go-ipfs block from ipldResolver', (done) => {
453-
const bs = new BlockService(repo)
434+
if (!isNode) {
435+
return done()
436+
}
454437

455438
const cidStr = 'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'
456439
const cid = new CID(cidStr)

test/node.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const ncp = require('ncp').ncp
5+
const rimraf = require('rimraf')
6+
const IPFSRepo = require('ipfs-repo')
7+
const series = require('async/series')
8+
const os = require('os')
9+
10+
describe('Node.js', () => {
11+
const repoExample = process.cwd() + '/test/test-repo'
12+
const repoTests = os.tmpDir() + '/t-r-' + Date.now()
13+
const repo = new IPFSRepo(repoTests)
14+
15+
before((done) => {
16+
series([
17+
(cb) => ncp(repoExample, repoTests, cb),
18+
(cb) => repo.open(cb)
19+
], done)
20+
})
21+
22+
after((done) => {
23+
series([
24+
(cb) => repo.close(cb),
25+
(cb) => rimraf(repoTests, cb)
26+
], done)
27+
})
28+
29+
require('./dag-link-test')(repo)
30+
require('./dag-node-test')(repo)
31+
})

0 commit comments

Comments
 (0)