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

Commit

Permalink
feat: migrate resolver to async API
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 26, 2016
1 parent c51ddc9 commit 2d3d220
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 139 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ipld-dag-pb",
"version": "0.0.1",
"description": "JavaScript Implementation of the MerkleDAG Node in Protobuf.",
"main": "src/index.js",
"main": "lib/index.js",
"jsnext:main": "src/index.js",
"scripts": {
"lint": "aegir-lint",
Expand Down Expand Up @@ -35,8 +35,8 @@
"url": "https://github.com/ipfs/js-ipfs-merkle-dag.git"
},
"dependencies": {
"cids": "^0.1.1",
"ipfs-block": "^0.3.0",
"cids": "^0.2.0",
"ipfs-block": "^0.4.0",
"is-ipfs": "^0.2.0",
"multihashes": "^0.2.2",
"multihashing": "^0.2.1",
Expand All @@ -46,16 +46,16 @@
"stable": "^0.1.5"
},
"devDependencies": {
"aegir": "^8.0.1",
"aegir": "^8.1.2",
"async": "^2.1.2",
"bs58": "^3.0.0",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"chai-checkmark": "^1.0.1",
"fs-pull-blob-store": "^0.3.0",
"idb-pull-blob-store": "^0.4.0",
"ipfs-block-service": "^0.5.0",
"ipfs-repo": "^0.9.0",
"idb-pull-blob-store": "^0.5.1",
"ipfs-block-service": "^0.6.0",
"ipfs-repo": "^0.10.0",
"lodash": "^4.15.0",
"ncp": "^2.0.0",
"pre-commit": "^1.1.3",
Expand Down
118 changes: 64 additions & 54 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,92 @@ exports.multicodec = 'dag-pb'
* resolve: receives a path and a block and returns the value on path,
* throw if not possible. `block` is an IPFS Block instance (contains data+key)
*/
exports.resolve = (block, path) => {
const node = util.deserialize(block.data)
exports.resolve = (block, path, callback) => {
util.deserialize(block.data, gotNode)

const split = path.split('/')
function gotNode (err, node) {
if (err) {
return callback(err)
}

const split = path.split('/')

if (split[0] === 'links') {
let remainderPath = ''
if (split[0] === 'links') {
let remainderPath = ''

// all links
if (!split[1]) {
return {
value: node.links.map((l) => {
return l.toJSON()
}),
remainderPath: ''
// all links
if (!split[1]) {
return callback(null, {
value: node.links.map((l) => {
return l.toJSON()
}),
remainderPath: ''
})
}
}

// select one link
// select one link

const values = {}
const values = {}

// populate both index number and name to enable both cases
// for the resolver
node.links.forEach((l, i) => {
const link = l.toJSON()
values[i] = link.Hash
values[link.Name] = link.Hash
})
// populate both index number and name to enable both cases
// for the resolver
node.links.forEach((l, i) => {
const link = l.toJSON()
values[i] = link.Hash
values[link.Name] = link.Hash
})

let value = values[split[1]]
let value = values[split[1]]

// if remainderPath exists, value needs to be CID
if (split[2]) {
split.shift()
split.shift()
remainderPath = split.join('/')
// if remainderPath exists, value needs to be CID
if (split[2]) {
split.shift()
split.shift()
remainderPath = split.join('/')

value = {
'/': value
value = {
'/': value
}
}
}

return { value: value, remainderPath: remainderPath }
} else if (split[0] === 'data') {
return { value: node.data, remainderPath: '' }
} else {
throw new Error('path not available')
callback(null, { value: value, remainderPath: remainderPath })
} else if (split[0] === 'data') {
callback(null, { value: node.data, remainderPath: '' })
} else {
callback(new Error('path not available'))
}
}
}

/*
* tree: returns a flattened array with paths: values of the project. options
* are option (i.e. nestness)
*/
exports.tree = (block, options) => {
exports.tree = (block, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

if (!options) {
options = {}
}
const node = util.deserialize(block.data)
const paths = []
node.links.forEach((link) => {
paths.push({
path: link.name,
value: bs58.encode(link.hash).toString()

util.deserialize(block.data, (err, node) => {
if (err) {
return callback(err)
}
const paths = []
node.links.forEach((link) => {
paths.push({
path: link.name,
value: bs58.encode(link.hash).toString()
})
})
})

if (node.data && node.data.length > 0) {
paths.push({ path: 'data', value: node.data })
}
return paths
if (node.data && node.data.length > 0) {
paths.push({ path: 'data', value: node.data })
}
callback(null, paths)
})
}

// TODO recheck this API
/*
* patch: modifies or adds value on path, yields a new block with that change
*/
exports.patch = (block, path, value) => {}
2 changes: 1 addition & 1 deletion test/dag-node-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const CID = require('cids')
const bs58 = require('bs58')

module.exports = (repo) => {
describe.only('DAGNode', () => {
describe('DAGNode', () => {
it('create a node', (done) => {
expect(7).checks(done)

Expand Down
Loading

0 comments on commit 2d3d220

Please sign in to comment.