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

Commit bea41ea

Browse files
committed
feat: resolve out of scope
1 parent 1158fa4 commit bea41ea

5 files changed

Lines changed: 38 additions & 4 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"lodash.clonedeep": "^4.3.2",
4646
"lodash.defaults": "^4.0.1",
4747
"lodash.includes": "^4.3.0",
48+
"lodash.times": "^4.3.2",
4849
"multiaddr": "^2.0.0",
4950
"multihashes": "^0.2.2",
5051
"multihashing": "^0.2.1",

src/dag-node.js

Whitespace-only changes.

src/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict'
22

3-
exports.DAGNode = require('./dag-node.js')
43
exports.util = require('./util.js')
54
exports.resolver = require('./resolver.js')

src/resolver.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const util = require('./util')
4+
const _times = require('lodash.times')
45

56
exports = module.exports
67

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

1617
// root
1718

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

3738
// out of scope
38-
// TODO
39+
40+
// TODO this was my first try at writting this out of scope traversal code,
41+
// it REALLY needs way more testing.
42+
path = path.split('/')
43+
let value
44+
let stop = false
45+
46+
_times(path.length, () => {
47+
if (stop) {
48+
return
49+
}
50+
let partialPath = path.shift()
51+
52+
if (Array.isArray(node) && !Buffer.isBuffer(node)) {
53+
value = node[Number(partialPath)]
54+
} if (node[partialPath]) {
55+
value = node[partialPath]
56+
} else {
57+
// can't traverse more
58+
if (!value) {
59+
throw new Error('path not available at root')
60+
} else {
61+
stop = true
62+
path.unshift(partialPath)
63+
result = {
64+
value: value,
65+
remainderPath: path.length > 0 ? path.join('/') : ''
66+
}
67+
}
68+
}
69+
node = value
70+
})
71+
72+
return result
3973
}
4074

4175
/*

test/resolver.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('IPLD format resolver (local)', () => {
4747
expect(result.value).to.equal('baz')
4848
})
4949

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

0 commit comments

Comments
 (0)