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

Commit

Permalink
feat: main resolver (understands dag-pb)
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 16, 2016
1 parent 00d5d46 commit 0818945
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"rimraf": "^2.5.4"
},
"dependencies": {
"async": "^2.1.1",
"babel-runtime": "^6.11.6",
"interface-pull-blob-store": "^0.5.0",
"ipfs-block": "^0.3.0",
Expand Down
44 changes: 42 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const Block = require('ipfs-block')
const pull = require('pull-stream')
const traverse = require('pull-traverse')
const utils = require('./utils')
const CID = require('cids')
const until = require('async/until')
const IPFSRepo = require('ipfs-repo')
const MemoryStore = require('../node_modules/interface-pull-blob-store/lib/reference.js')
const BlockService = require('ipfs-block-service')
Expand Down Expand Up @@ -38,8 +40,46 @@ class IPLDResolver {
}
}

resolve (cid, path) {
// TODO
resolve (cid, path, callback) {
if (path === '/') {
return this.get(cid, callback)
}

let value

until(
() => {
if (!path || path === '' || path === '/') {
return true
} else {
// continue traversing
if (value) {
cid = new CID(value['/'])
}
return false
}
},
(cb) => {
// get block
// use local resolver
// update path value
this.bs.get(cid, (err, block) => {
if (err) {
return cb(err)
}
const result = this.resolvers[cid.codec].resolver.resolve(block, path)
value = result.value
path = result.remainderPath
cb()
})
},
(err, results) => {
if (err) {
return callback(err)
}
return callback(null, value)
}
)
}

// Node operations (get and retrieve nodes, not values)
Expand Down
68 changes: 62 additions & 6 deletions test/test-ipld-dag-pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const pull = require('pull-stream')
const IPLDResolver = require('../src')

module.exports = (repo) => {
const bs = new BlockService(repo)
const resolver = new IPLDResolver(bs)

describe('IPLD Resolver with dag-pb (MerkleDAG Protobuf)', () => {
const bs = new BlockService(repo)
const resolver = new IPLDResolver(bs)

let node1
let node2
let node3
Expand Down Expand Up @@ -126,8 +126,64 @@ module.exports = (repo) => {
})

describe('IPLD Path Resolver', () => {
it.skip('resolves path of a non nested value', () => {})
it.skip('resolves path of a level 1 nested value', () => {})
it.skip('resolves path of a level 2 nested value', () => {})
let resolver

let node1
let node2
let node3

before((done) => {
resolver = new IPLDResolver()

node1 = new dagPB.DAGNode(new Buffer('I am 1'))
node2 = new dagPB.DAGNode(new Buffer('I am 2'))
node3 = new dagPB.DAGNode(new Buffer('I am 3'))

node2.addNodeLink('1', node1)

node3.addNodeLink('1', node1)
node3.addNodeLink('2', node2)

pull(
pull.values([
node1,
node2,
node3
]),
resolver.putStream(done)
)
})

it('root path (same as get)', (done) => {
resolver.resolve(node1.cid(), '/', (err, result) => {
expect(err).to.not.exist
expect(result.cid()).to.eql(node1.cid())
done()
})
})

it('value within 1st node scope', (done) => {
resolver.resolve(node1.cid(), 'data', (err, result) => {
expect(err).to.not.exist
expect(result).to.eql(new Buffer('I am 1'))
done()
})
})

it('value within nested scope (1 level)', (done) => {
resolver.resolve(node2.cid(), 'links/0/data', (err, result) => {
expect(err).to.not.exist
expect(result).to.eql(new Buffer('I am 1'))
done()
})
})

it('value within nested scope (2 levels)', (done) => {
resolver.resolve(node3.cid(), 'links/1/links/0/data', (err, result) => {
expect(err).to.not.exist
expect(result).to.eql(new Buffer('I am 1'))
done()
})
})
})
}

0 comments on commit 0818945

Please sign in to comment.