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

Commit

Permalink
feat: resolve out of scope
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 26, 2016
1 parent 1158fa4 commit bea41ea
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"lodash.clonedeep": "^4.3.2",
"lodash.defaults": "^4.0.1",
"lodash.includes": "^4.3.0",
"lodash.times": "^4.3.2",
"multiaddr": "^2.0.0",
"multihashes": "^0.2.2",
"multihashing": "^0.2.1",
Expand Down
Empty file removed src/dag-node.js
Empty file.
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict'

exports.DAGNode = require('./dag-node.js')
exports.util = require('./util.js')
exports.resolver = require('./resolver.js')
38 changes: 36 additions & 2 deletions src/resolver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const util = require('./util')
const _times = require('lodash.times')

exports = module.exports

Expand All @@ -11,7 +12,7 @@ exports.multicodec = 'dag-cbor'
* throw if not possible. `block` is an IPFS Block instance (contains data + key)
*/
exports.resolve = (block, path) => {
const node = util.deserialize(block.data)
let node = util.deserialize(block.data)

// root

Expand All @@ -35,7 +36,40 @@ exports.resolve = (block, path) => {
}

// out of scope
// TODO

// TODO this was my first try at writting this out of scope traversal code,
// it REALLY needs way more testing.
path = path.split('/')
let value
let stop = false

_times(path.length, () => {
if (stop) {
return
}
let partialPath = path.shift()

if (Array.isArray(node) && !Buffer.isBuffer(node)) {
value = node[Number(partialPath)]
} if (node[partialPath]) {
value = node[partialPath]
} else {
// can't traverse more
if (!value) {
throw new Error('path not available at root')
} else {
stop = true
path.unshift(partialPath)
result = {
value: value,
remainderPath: path.length > 0 ? path.join('/') : ''
}
}
}
node = value
})

return result
}

/*
Expand Down
2 changes: 1 addition & 1 deletion test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('IPLD format resolver (local)', () => {
expect(result.value).to.equal('baz')
})

it.skip('path out of scope', () => {
it('path out of scope', () => {
const result = resolver.resolve(nodeBlock, 'someLink/a/b/c')
expect(result.value).to.eql({ '/': 'LINK' })
expect(result.remainderPath).to.equal('a/b/c')
Expand Down

0 comments on commit bea41ea

Please sign in to comment.